BioDynaMo  v1.05.119-a4ff3934
bound_space_op.h
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 #ifndef CORE_OPERATION_BOUND_SPACE_OP_H_
16 #define CORE_OPERATION_BOUND_SPACE_OP_H_
17 
18 #include "core/agent/agent.h"
21 #include "core/param/param.h"
22 #include "core/simulation.h"
23 
24 namespace bdm {
25 
26 inline void ApplyBoundingBox(Agent* agent, Param::BoundSpaceMode mode,
27  real_t lb, real_t rb) {
28  // Need to create a small distance from the positive edge of each dimension;
29  // otherwise it will fall out of the boundary of the simulation space
30  real_t eps = 1e-10;
31  auto pos = agent->GetPosition();
32  if (mode == Param::BoundSpaceMode::kClosed) {
33  bool updated = false;
34  for (int i = 0; i < 3; i++) {
35  if (pos[i] < lb) {
36  pos[i] = lb;
37  updated = true;
38  } else if (pos[i] >= rb) {
39  pos[i] = rb - eps;
40  updated = true;
41  }
42  }
43  if (updated) {
44  agent->SetPosition(pos);
45  }
46  } else if (mode == Param::BoundSpaceMode::kTorus) {
47  auto length = rb - lb;
48  for (auto& el : pos) {
49  if (el < lb) {
50  auto d = std::abs(lb - el);
51  if (d > length) {
52  d = std::fmod(d, length);
53  }
54  el = rb - d;
55  } else if (el > rb) {
56  auto d = std::abs(el - rb);
57  if (d > length) {
58  d = std::fmod(d, length);
59  }
60  el = lb + d;
61  }
62  }
63  agent->SetPosition(pos);
64  }
65 }
66 
69 struct BoundSpace : public AgentOperationImpl {
71 
72  void operator()(Agent* agent) override {
73  auto* param = Simulation::GetActive()->GetParam();
74  if (param->bound_space) {
75  ApplyBoundingBox(agent, param->bound_space, param->min_bound,
76  param->max_bound);
77  }
78  }
79 };
80 
81 } // namespace bdm
82 
83 #endif // CORE_OPERATION_BOUND_SPACE_OP_H_
bdm
Definition: agent.cc:39
operation.h
bdm::BoundSpace::BDM_OP_HEADER
BDM_OP_HEADER(BoundSpace)
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::Param::BoundSpaceMode
BoundSpaceMode
Definition: param.h:196
bdm::Agent::SetPosition
virtual void SetPosition(const Real3 &pos)=0
operation_registry.h
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::ApplyBoundingBox
void ApplyBoundingBox(Agent *agent, Param::BoundSpaceMode mode, real_t lb, real_t rb)
Definition: bound_space_op.h:26
bdm::AgentOperationImpl
Interface for implementing an operation.
Definition: operation.h:76
bdm::BoundSpace::operator()
void operator()(Agent *agent) override
Definition: bound_space_op.h:72
agent.h
simulation.h
bdm::BoundSpace
Definition: bound_space_op.h:69
bdm::Simulation::GetParam
const Param * GetParam() const
Returns the simulation parameters.
Definition: simulation.cc:254
bdm::Agent::GetPosition
virtual const Real3 & GetPosition() const =0
param.h
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68