BioDynaMo  v1.05.120-25dc9790
gene_regulation.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_BEHAVIOR_GENE_REGULATION_H_
16 #define CORE_BEHAVIOR_GENE_REGULATION_H_
17 
18 #include <functional>
19 #include <vector>
20 
21 #include "core/behavior/behavior.h"
22 #include "core/param/param.h"
23 #include "core/scheduler.h"
24 #include "core/simulation.h"
25 #include "core/util/root.h"
26 
27 namespace bdm {
28 
36 class GeneRegulation : public Behavior {
38 
39  public:
41 
42  virtual ~GeneRegulation() = default;
43 
44  void Initialize(const NewAgentEvent& event) override {
45  Base::Initialize(event);
46 
47  auto* other = event.existing_behavior;
48  if (auto* gr = dynamic_cast<GeneRegulation*>(other)) {
49  concentrations_ = gr->concentrations_;
50  first_derivatives_ = gr->first_derivatives_;
51  } else {
52  Log::Fatal("GeneRegulation::EventConstructor",
53  "other was not of type GeneRegulation");
54  }
55  }
56 
66  void AddGene(const std::function<real_t(real_t, real_t)>& first_derivative,
67  real_t initial_concentration) {
68  first_derivatives_.push_back(first_derivative);
69  concentrations_.push_back(initial_concentration);
70  }
71 
72  const std::vector<real_t>& GetValues() const { return concentrations_; }
73 
76  void Run(Agent* agent) override {
77  auto* sim = Simulation::GetActive();
78  auto* param = sim->GetParam();
79  auto* scheduler = sim->GetScheduler();
80 
81  const auto& timestep = param->simulation_time_step;
82  uint64_t simulated_steps = scheduler->GetSimulatedSteps();
83  const auto absolute_time = simulated_steps * timestep;
84 
85  if (param->numerical_ode_solver == Param::NumericalODESolver::kEuler) {
86  // Euler
87  for (uint64_t i = 0; i < first_derivatives_.size(); i++) {
88  real_t slope = first_derivatives_[i](absolute_time, concentrations_[i]);
89  concentrations_[i] += slope * timestep;
90  }
91  } else if (param->numerical_ode_solver == Param::NumericalODESolver::kRK4) {
92  // Runge-Kutta 4
93  for (uint64_t i = 0; i < first_derivatives_.size(); i++) {
94  real_t interval_midpoint = absolute_time + timestep / 2.0;
95  real_t interval_endpoint = absolute_time + timestep;
96 
97  real_t k1 = first_derivatives_[i](absolute_time, concentrations_[i]);
98  real_t k2 = first_derivatives_[i](
99  interval_midpoint, concentrations_[i] + timestep * k1 / 2.0);
100  real_t k3 = first_derivatives_[i](
101  interval_midpoint, concentrations_[i] + timestep * k2 / 2.0);
102  real_t k4 = first_derivatives_[i](interval_endpoint,
103  concentrations_[i] + timestep * k3);
104 
105  concentrations_[i] += timestep / 6.0 * (k1 + 2 * k2 + 2 * k3 + k4);
106  }
107  }
108  }
109 
110  private:
112  std::vector<real_t> concentrations_ = {};
113 
117  std::vector<std::function<real_t(real_t, real_t)>> first_derivatives_ = {};
118 };
119 
120 } // namespace bdm
121 
122 #endif // CORE_BEHAVIOR_GENE_REGULATION_H_
bdm::NewAgentEvent
Definition: new_agent_event.h:61
bdm
Definition: agent.cc:39
bdm::Behavior
Definition: behavior.h:29
bdm::GeneRegulation::AddGene
void AddGene(const std::function< real_t(real_t, real_t)> &first_derivative, real_t initial_concentration)
Definition: gene_regulation.h:66
bdm::GeneRegulation::Run
void Run(Agent *agent) override
Definition: gene_regulation.h:76
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::GeneRegulation::concentrations_
std::vector< real_t > concentrations_
Store the current concentration for each gene.
Definition: gene_regulation.h:112
bdm::GeneRegulation::GetValues
const std::vector< real_t > & GetValues() const
Definition: gene_regulation.h:72
scheduler.h
bdm::Behavior::AlwaysCopyToNew
void AlwaysCopyToNew()
Always copy this behavior to new agents.
Definition: behavior.h:63
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::GeneRegulation::BDM_BEHAVIOR_HEADER
BDM_BEHAVIOR_HEADER(GeneRegulation, Behavior, 1)
root.h
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
bdm::GeneRegulation::GeneRegulation
GeneRegulation()
Definition: gene_regulation.h:40
bdm::GeneRegulation::first_derivatives_
std::vector< std::function< real_t(real_t, real_t)> > first_derivatives_
Definition: gene_regulation.h:117
behavior.h
simulation.h
bdm::GeneRegulation::Initialize
void Initialize(const NewAgentEvent &event) override
Definition: gene_regulation.h:44
bdm::GeneRegulation
Definition: gene_regulation.h:36
param.h
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
bdm::GeneRegulation::~GeneRegulation
virtual ~GeneRegulation()=default