BioDynaMo  v1.05.119-a4ff3934
agent.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_AGENT_AGENT_H_
16 #define CORE_AGENT_AGENT_H_
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <limits>
21 #include <memory>
22 #include <set>
23 #include <sstream>
24 #include <string>
25 #include <type_traits>
26 #include <unordered_map>
27 #include <vector>
28 
30 #include "core/agent/agent_uid.h"
34 #include "core/interaction_force.h"
35 #include "core/shape.h"
36 #include "core/util/macros.h"
37 #include "core/util/root.h"
38 #include "core/util/spinlock.h"
39 #include "core/util/type.h"
40 
41 namespace bdm {
42 
52 #define BDM_AGENT_HEADER(class_name, base_class, class_version_id) \
53  public: \
54  using Base = base_class; \
55  \
56  explicit class_name(TRootIOCtor* io_ctor) {} \
57  \
58  \
59  Agent* New() const override { return new class_name(); } \
60  \
61  Agent* NewCopy() const override { return new class_name(*this); } \
62  \
63  const char* GetTypeName() const override { return #class_name; } \
64  \
65  protected: \
66  \
67  Base* UpCast() { return static_cast<Base*>(this); } \
68  \
69  \
70  const Base* UpCast() const { return static_cast<const Base*>(this); } \
71  \
72  BDM_CLASS_DEF_OVERRIDE(class_name, class_version_id)
73 
74 // -----------------------------------------------------------------------------
75 
76 class Behavior;
77 
79 class Agent {
80  public:
81  Agent();
82 
83  explicit Agent(TRootIOCtor* io_ctor);
84 
85  Agent(const Agent& other);
86 
87  virtual ~Agent();
88 
90  virtual Agent* New() const = 0;
91 
93  virtual Agent* NewCopy() const = 0;
94 
101  virtual void Initialize(const NewAgentEvent& event);
102 
109  virtual void Update(const NewAgentEvent& event);
110 
121  void CreateNewAgents(const NewAgentEvent& event,
122  const std::initializer_list<Agent*>& prototypes) {
123  auto* ctxt = Simulation::GetActive()->GetExecutionContext();
124  event.existing_agent = this;
125  for (auto* p : prototypes) {
126  auto* new_agent = p->New();
127  new_agent->Initialize(event);
128  event.new_agents.push_back(new_agent);
129  ctxt->AddAgent(new_agent);
130  }
131  Update(event);
132  }
133 
134  virtual const char* GetTypeName() const { return "Agent"; }
135 
136  virtual Shape GetShape() const = 0;
137 
140  virtual std::set<std::string> GetRequiredVisDataMembers() const {
141  return {"position_", "diameter_"};
142  }
143 
144  virtual void RunDiscretization();
145 
146  void AssignNewUid();
147 
148  const AgentUid& GetUid() const;
149 
150  Spinlock* GetLock() { return &lock_; }
151 
170  virtual void CriticalRegion(std::vector<AgentPointer<>>* aptrs) {}
171 
172  uint32_t GetBoxIdx() const;
173 
174  void SetBoxIdx(uint32_t idx);
175 
176  void SetStaticnessNextTimestep(bool value) const {
177  is_static_next_ts_ = value;
178  }
179 
180  bool GetPropagateStaticness() const {
182  }
183 
184  void SetPropagateStaticness(bool value = true) {
186  }
187 
200  void PropagateStaticness(bool beginning = false);
201 
202  void UpdateStaticness();
203 
204  bool IsStatic() const { return is_static_; }
205 
207  template <typename TAgent = Agent>
209  static_assert(!std::is_pointer<TAgent>::value,
210  "Cannot be of pointer type!");
212  return AgentPointer<TAgent>(uid_);
213  } else {
214  return AgentPointer<TAgent>(
215  const_cast<TAgent*>(Cast<const Agent, const TAgent>(this)));
216  }
217  }
218 
219  // ---------------------------------------------------------------------------
220  // Behaviors
222  void AddBehavior(Behavior* behavior);
223 
227  void RemoveBehavior(const Behavior* behavior);
228 
230  void RunBehaviors();
231 
234  // ---------------------------------------------------------------------------
235 
236  virtual Real3 CalculateDisplacement(const InteractionForce* force,
237  real_t squared_radius, real_t dt) = 0;
238 
239  virtual void ApplyDisplacement(const Real3& displacement) = 0;
240 
241  virtual const Real3& GetPosition() const = 0;
242 
243  virtual void SetPosition(const Real3& pos) = 0;
244 
245  virtual real_t GetDiameter() const = 0;
246 
247  virtual void SetDiameter(real_t diameter) = 0;
248 
249  virtual void RemoveFromSimulation();
250 
251  void* operator new(size_t size) { // NOLINT
252  auto* mem_mgr = Simulation::GetActive()->GetMemoryManager();
253  if (mem_mgr) {
254  return mem_mgr->New(size);
255  } else {
256  return malloc(size);
257  }
258  }
259 
260  void operator delete(void* p) { // NOLINT
261  auto* mem_mgr = Simulation::GetActive()->GetMemoryManager();
262  if (mem_mgr) {
263  mem_mgr->Delete(p);
264  } else {
265  free(p);
266  }
267  }
268 
269  protected:
273  uint32_t box_idx_ = std::numeric_limits<uint32_t>::max();
276 
277  template <typename TFrom, typename TTo>
278  typename std::enable_if<std::is_base_of<TFrom, TTo>::value, TTo*>::type Cast(
279  TFrom* agent) const {
280  return static_cast<TTo*>(agent);
281  }
282 
283  template <typename TFrom, typename TTo>
284  typename std::enable_if<!std::is_base_of<TFrom, TTo>::value, TTo*>::type Cast(
285  TFrom* agent) const {
286  return dynamic_cast<TTo*>(agent);
287  }
288 
289  private:
291 
295 
297  bool is_static_ = false;
301  mutable bool is_static_next_ts_ = false;
303 
308  void InitializeBehaviors(const NewAgentEvent& event);
309 
316  void UpdateBehaviors(const NewAgentEvent& event);
317 
318  BDM_CLASS_DEF(Agent, 1)
319 };
320 
321 } // namespace bdm
322 
323 #endif // CORE_AGENT_AGENT_H_
bdm::NewAgentEvent
Definition: new_agent_event.h:61
bdm::Agent::SetStaticnessNextTimestep
void SetStaticnessNextTimestep(bool value) const
Definition: agent.h:176
shape.h
bdm::Agent::GetRequiredVisDataMembers
virtual std::set< std::string > GetRequiredVisDataMembers() const
Definition: agent.h:140
bdm::Agent::InitializeBehaviors
void InitializeBehaviors(const NewAgentEvent &event)
Definition: agent.cc:163
bdm::Shape
Shape
Definition: shape.h:20
inline_vector.h
bdm::Agent::RunBehaviors
void RunBehaviors()
Execute all behaviorsq.
Definition: agent.cc:146
spinlock.h
bdm::ExecutionContext::AddAgent
virtual void AddAgent(Agent *new_agent)=0
Adds the agent to the simulation (threadsafe, takes ownership). Note that we avoid the use of smart p...
bdm::Agent::UpdateBehaviors
void UpdateBehaviors(const NewAgentEvent &event)
Function to invoke the Update method of the behavior or remove it from current. Forwards the call to ...
Definition: agent.cc:183
agent_uid.h
bdm
Definition: agent.cc:39
bdm::Agent::RemoveFromSimulation
virtual void RemoveFromSimulation()
Definition: agent.cc:159
bdm::Behavior
Definition: behavior.h:29
bdm::InteractionForce
Definition: interaction_force.h:26
bdm::Agent::CreateNewAgents
void CreateNewAgents(const NewAgentEvent &event, const std::initializer_list< Agent * > &prototypes)
Definition: agent.h:121
bdm::AgentPointer
Definition: agent_pointer.h:58
bdm::Agent::is_static_
bool is_static_
If an agent is static, we should not compute the mechanical forces.
Definition: agent.h:297
macros.h
bdm::Agent::Initialize
virtual void Initialize(const NewAgentEvent &event)
Definition: agent.cc:65
bdm::Agent::CalculateDisplacement
virtual Real3 CalculateDisplacement(const InteractionForce *force, real_t squared_radius, real_t dt)=0
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::Agent::GetAllBehaviors
const InlineVector< Behavior *, 2 > & GetAllBehaviors() const
Return all behaviors.
Definition: agent.cc:154
bdm::MemoryManager::Delete
void Delete(void *p)
Definition: memory_manager.cc:389
agent_pointer.h
bdm::Agent::GetUid
const AgentUid & GetUid() const
Definition: agent.cc:123
bdm::Agent::ApplyDisplacement
virtual void ApplyDisplacement(const Real3 &displacement)=0
bdm::Agent::GetDiameter
virtual real_t GetDiameter() const =0
bdm::Agent::GetTypeName
virtual const char * GetTypeName() const
Definition: agent.h:134
type.h
bdm::Agent::SetDiameter
virtual void SetDiameter(real_t diameter)=0
bdm::Agent::GetShape
virtual Shape GetShape() const =0
bdm::Agent::SetPosition
virtual void SetPosition(const Real3 &pos)=0
bdm::Agent::SetPropagateStaticness
void SetPropagateStaticness(bool value=true)
Definition: agent.h:184
bdm::Agent::is_static_next_ts_
bool is_static_next_ts_
Flag to determine of an agent is static in the next timestep.
Definition: agent.h:302
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::Agent::run_behavior_loop_idx_
uint16_t run_behavior_loop_idx_
Definition: agent.h:294
bdm::Agent::SetBoxIdx
void SetBoxIdx(uint32_t idx)
Definition: agent.cc:127
bdm::Spinlock
Definition: spinlock.h:22
new_agent_event.h
bdm::Simulation::GetMemoryManager
MemoryManager * GetMemoryManager()
Definition: simulation.h:127
BDM_CLASS_DEF
#define BDM_CLASS_DEF(class_name, class_version_id)
Forward all calls to BDM_NULL_CLASS_DEF.
Definition: root.h:198
bdm::kIndirect
@ kIndirect
Definition: agent_pointer.h:42
root.h
math_array.h
bdm::Agent::RunDiscretization
virtual void RunDiscretization()
Definition: agent.cc:117
bdm::Agent::GetLock
Spinlock * GetLock()
Definition: agent.h:150
bdm::gAgentPointerMode
AgentPointerMode gAgentPointerMode
Definition: agent_pointer.cc:19
bdm::Agent::NewCopy
virtual Agent * NewCopy() const =0
Create a copy of this object.
bdm::Agent::New
virtual Agent * New() const =0
Create a new instance of this object using the default constructor.
bdm::Agent::AddBehavior
void AddBehavior(Behavior *behavior)
Add a behavior to this agent.
Definition: agent.cc:132
bdm::Agent::GetBoxIdx
uint32_t GetBoxIdx() const
Definition: agent.cc:125
bdm::Agent::GetAgentPtr
AgentPointer< TAgent > GetAgentPtr() const
Return agent pointer.
Definition: agent.h:208
bdm::AgentUid
Definition: agent_uid.h:25
bdm::Agent::propagate_staticness_neighborhood_
bool propagate_staticness_neighborhood_
Definition: agent.h:300
bdm::Agent::GetPropagateStaticness
bool GetPropagateStaticness() const
Definition: agent.h:180
bdm::Agent::Agent
Agent()
Definition: agent.cc:41
bdm::Simulation::GetExecutionContext
ExecutionContext * GetExecutionContext()
Returns a thread local execution context.
Definition: simulation.cc:271
bdm::MemoryManager::New
void * New(std::size_t size)
Definition: memory_manager.cc:358
bdm::Agent::~Agent
virtual ~Agent()
Definition: agent.cc:59
bdm::Agent::behaviors_
InlineVector< Behavior *, 2 > behaviors_
collection of behaviors which define the internal behavior
Definition: agent.h:275
bdm::Agent::Update
virtual void Update(const NewAgentEvent &event)
Definition: agent.cc:71
bdm::Agent::UpdateStaticness
void UpdateStaticness()
Definition: agent.cc:111
bdm::Agent::AssignNewUid
void AssignNewUid()
Definition: agent.cc:119
bdm::Agent::RemoveBehavior
void RemoveBehavior(const Behavior *behavior)
Definition: agent.cc:134
bdm::Agent::Cast
std::enable_if<!std::is_base_of< TFrom, TTo >::value, TTo * >::type Cast(TFrom *agent) const
Definition: agent.h:284
bdm::MathArray
Definition: math_array.h:36
bdm::Agent::PropagateStaticness
void PropagateStaticness(bool beginning=false)
Definition: agent.cc:77
bdm::InlineVector
Definition: inline_vector.h:49
bdm::Agent::Cast
std::enable_if< std::is_base_of< TFrom, TTo >::value, TTo * >::type Cast(TFrom *agent) const
Definition: agent.h:278
bdm::Agent::GetPosition
virtual const Real3 & GetPosition() const =0
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
bdm::Agent::box_idx_
uint32_t box_idx_
Grid box index.
Definition: agent.h:273
bdm::Agent::uid_
AgentUid uid_
unique id
Definition: agent.h:271
bdm::Agent::IsStatic
bool IsStatic() const
Definition: agent.h:204
interaction_force.h
bdm::Agent::lock_
Spinlock lock_
Definition: agent.h:290
bdm::Agent::CriticalRegion
virtual void CriticalRegion(std::vector< AgentPointer<>> *aptrs)
Definition: agent.h:170