BioDynaMo  v1.05.120-25dc9790
octree_environment.cc
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #include <algorithm>
16 
18 
19 #include "unibn_octree.h"
20 
21 namespace bdm {
22 
24  unibn::Octree<Real3, AgentContainer>* octree_ = nullptr;
25 };
26 
28  impl_ = std::unique_ptr<OctreeEnvironment::UnibnImpl>(
30  impl_->octree_ = new unibn::Octree<Real3, AgentContainer>();
31  container_ = new AgentContainer();
32 }
33 
35  delete impl_->octree_;
36  delete container_;
37 }
38 
41  auto* param = Simulation::GetActive()->GetParam();
42 
43  // Update the flattened indices map
45  if (container_->rm_->GetNumAgents() != 0) {
46  Clear();
47  auto inf = Math::kInfinity;
48  std::array<real_t, 6> tmp_dim = {{inf, -inf, inf, -inf, inf, -inf}};
50  RoundOffGridDimensions(tmp_dim);
52 
53  unibn::OctreeParams params;
54  params.bucketSize = param->unibn_bucketsize;
55 
56  impl_->octree_->initialize(*container_, params);
57  } else {
58  // There are no sim objects in this simulation
59  auto* param = Simulation::GetActive()->GetParam();
60 
61  bool uninitialized = impl_->octree_ == nullptr;
62  if (uninitialized && param->bound_space) {
63  // Simulation has never had any simulation objects
64  // Initialize grid dimensions with `Param::min_bound_` and
65  // `Param::max_bound_`
66  // This is required for the DiffusionGrid
67  int min = param->min_bound;
68  int max = param->max_bound;
69  grid_dimensions_ = {min, max, min, max, min, max};
70  threshold_dimensions_ = {min, max};
71  has_grown_ = true;
72  } else if (!uninitialized) {
73  // all simulation objects have been removed in the last iteration
74  // grid state remains the same, but we have to set has_grown_ to false
75  // otherwise the DiffusionGrid will attempt to resize
76  has_grown_ = false;
77  } else {
78  Log::Fatal(
79  "OctreeEnvironment",
80  "You tried to initialize an empty simulation without bound space. "
81  "Therefore we cannot determine the size of the simulation space. "
82  "Please add simulation objects, or set Param::bound_space_, "
83  "Param::min_bound_, and Param::max_bound_.");
84  }
85  }
86 }
87 
89  const Agent& query,
90  real_t squared_radius) {
91  ForEachNeighbor(lambda, query.GetPosition(), squared_radius, &query);
92 }
93 
95  const Real3& query_position,
96  real_t squared_radius,
97  const Agent* query_agent) {
98  std::vector<uint32_t> neighbors;
99  std::vector<double> distances;
100 
101  // Find neighbors
102  impl_->octree_->radiusNeighbors<unibn::L2Distance<Real3>>(
103  query_position, static_cast<double>(std::sqrt(squared_radius)), neighbors,
104  distances);
105 
107  int i = 0;
108  for (auto& n : neighbors) {
109  Agent* nb_so = rm->GetAgent(container_->flat_idx_map_.GetAgentHandle(n));
110  if (nb_so != query_agent) {
111  lambda(nb_so, static_cast<real_t>(distances[i]));
112  }
113  i++;
114  }
115 }
116 
118  const Agent& query, void* criteria) {
119  Log::Fatal("OctreeEnvironment::ForEachNeighbor",
120  "You tried to call a specific ForEachNeighbor in an "
121  "environment that does not yet support it.");
122 }
123 
124 std::array<int32_t, 6> OctreeEnvironment::GetDimensions() const {
125  return grid_dimensions_;
126 }
127 
128 std::array<int32_t, 2> OctreeEnvironment::GetDimensionThresholds() const {
129  return threshold_dimensions_;
130 }
131 
133  Log::Fatal("OctreeEnvironment::GetLoadBalanceInfo",
134  "You tried to call GetLoadBalanceInfo in an environment that does "
135  "not support it.");
136  return nullptr;
137 }
138 
141  return nullptr;
142 };
143 
145  int32_t inf = std::numeric_limits<int32_t>::max();
146  grid_dimensions_ = {inf, -inf, inf, -inf, inf, -inf};
147  threshold_dimensions_ = {inf, -inf};
148 }
149 
151  const std::array<real_t, 6>& grid_dimensions) {
152  grid_dimensions_[0] = floor(grid_dimensions[0]);
153  grid_dimensions_[2] = floor(grid_dimensions[2]);
154  grid_dimensions_[4] = floor(grid_dimensions[4]);
155  grid_dimensions_[1] = ceil(grid_dimensions[1]);
156  grid_dimensions_[3] = ceil(grid_dimensions[3]);
157  grid_dimensions_[5] = ceil(grid_dimensions[5]);
158 }
159 
161  // Determine if the grid dimensions have changed (changed in the sense that
162  // the grid has grown outwards)
163  auto min_gd =
164  *std::min_element(grid_dimensions_.begin(), grid_dimensions_.end());
165  auto max_gd =
166  *std::max_element(grid_dimensions_.begin(), grid_dimensions_.end());
167  if (min_gd < threshold_dimensions_[0]) {
168  threshold_dimensions_[0] = min_gd;
169  has_grown_ = true;
170  }
171  if (max_gd > threshold_dimensions_[1]) {
172  threshold_dimensions_[1] = max_gd;
173  has_grown_ = true;
174  }
175 }
176 
177 } // namespace bdm
bdm::AgentFlatIdxMap::Update
void Update()
Definition: agent_flat_idx_map.h:49
bdm::OctreeEnvironment::Clear
void Clear() override
Definition: octree_environment.cc:144
bdm::Environment::NeighborMutexBuilder
Definition: environment.h:103
bdm::AgentFlatIdxMap::GetAgentHandle
AgentHandle GetAgentHandle(uint64_t idx) const
Definition: agent_flat_idx_map.h:63
bdm::ResourceManager::GetNumAgents
size_t GetNumAgents(int numa_node=-1) const
Definition: resource_manager.h:256
bdm::OctreeEnvironment::GetNeighborMutexBuilder
NeighborMutexBuilder * GetNeighborMutexBuilder() override
Definition: octree_environment.cc:140
bdm::OctreeEnvironment::impl_
std::unique_ptr< UnibnImpl > impl_
Definition: octree_environment.h:76
bdm
Definition: agent.cc:39
bdm::OctreeEnvironment::UpdateImplementation
void UpdateImplementation() override
Definition: octree_environment.cc:39
bdm::OctreeEnvironment::RoundOffGridDimensions
void RoundOffGridDimensions(const std::array< real_t, 6 > &grid_dimensions)
Definition: octree_environment.cc:150
bdm::Environment::CalcSimDimensionsAndLargestAgent
void CalcSimDimensionsAndLargestAgent(std::array< real_t, 6 > *ret_grid_dimensions)
Definition: environment.h:193
bdm::OctreeEnvironment::container_
AgentContainer * container_
Definition: octree_environment.h:77
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::AgentContainer::rm_
ResourceManager * rm_
Definition: octree_environment.h:40
bdm::LoadBalanceInfo
Definition: load_balance_info.h:24
bdm::AgentContainer
Definition: octree_environment.h:28
bdm::AgentContainer::flat_idx_map_
AgentFlatIdxMap flat_idx_map_
Definition: octree_environment.h:39
bdm::Environment::has_grown_
bool has_grown_
Definition: environment.h:126
bdm::OctreeEnvironment::ForEachNeighbor
void ForEachNeighbor(Functor< void, Agent *, real_t > &lambda, const Agent &query, real_t squared_radius) override
Definition: octree_environment.cc:88
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::OctreeEnvironment::GetLoadBalanceInfo
LoadBalanceInfo * GetLoadBalanceInfo() override
Definition: octree_environment.cc:132
bdm::Functor< void, Agent *, real_t >
bdm::OctreeEnvironment::threshold_dimensions_
std::array< int32_t, 2 > threshold_dimensions_
Definition: octree_environment.h:83
bdm::OctreeEnvironment::OctreeEnvironment
OctreeEnvironment()
Definition: octree_environment.cc:27
bdm::Math::kInfinity
static constexpr real_t kInfinity
Helpful constant to identify 'infinity'.
Definition: math.h:35
bdm::OctreeEnvironment::GetDimensionThresholds
std::array< int32_t, 2 > GetDimensionThresholds() const override
Definition: octree_environment.cc:128
bdm::Simulation::GetResourceManager
ResourceManager * GetResourceManager()
Returns the ResourceManager instance.
Definition: simulation.cc:244
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
octree_environment.h
bdm::OctreeEnvironment::CheckGridGrowth
void CheckGridGrowth()
Definition: octree_environment.cc:160
bdm::Simulation::GetParam
const Param * GetParam() const
Returns the simulation parameters.
Definition: simulation.cc:254
bdm::MathArray< real_t, 3 >
bdm::Agent::GetPosition
virtual const Real3 & GetPosition() const =0
bdm::OctreeEnvironment::UnibnImpl::octree_
unibn::Octree< Real3, AgentContainer > * octree_
Definition: octree_environment.cc:24
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
bdm::OctreeEnvironment::grid_dimensions_
std::array< int32_t, 6 > grid_dimensions_
Definition: octree_environment.h:80
bdm::OctreeEnvironment::GetDimensions
std::array< int32_t, 6 > GetDimensions() const override
Definition: octree_environment.cc:124
bdm::OctreeEnvironment::~OctreeEnvironment
~OctreeEnvironment() override
Definition: octree_environment.cc:34
bdm::OctreeEnvironment::UnibnImpl
Definition: octree_environment.cc:23