VPTissue Reference Manual
Transport.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "Transport.h"
21 
23 #include "math/OdeintFactory0.h"
24 #include "math/OdeintFactory2.h"
25 #include "math/OdeintTraits.h"
26 
27 #include <boost/property_tree/ptree.hpp>
28 #include <cassert>
29 
30 namespace SimPT_Default {
31 namespace TimeEvolver {
32 
33 using namespace std;
34 using namespace SimPT_Sim::ClockMan;
35 
37 {
38  Initialize(cd);
39 }
40 
42 {
43  assert( cd.Check() && "CoreData not ok.");
44  m_cd = cd;
45  const auto& p = m_cd.m_parameters;
46 
47  m_abs_tolerance = p->get<double>("ode_integration.abs_tolerance");
48  m_is_stationary = false;
49  m_ode_solver = p->get<string>("ode_integration.ode_solver", "runge_kutta_dopri5");
50  m_rel_tolerance = p->get<double>("ode_integration.rel_tolerance");
51  m_small_time_increment = p->get<double>("ode_integration.small_time_increment");
52  m_stationarity_check = p->get<bool>("termination.stationarity_check");
53 }
54 
55 std::tuple<SimTimingTraits::CumulativeTimings, bool> Transport::operator()(double time_slice, SimPhase)
56 {
57  // -------------------------------------------------------------------------------
58  // Intro.
59  // -------------------------------------------------------------------------------
60  Stopclock chrono_stat("transport", true);
61  bool is_stationary = false;
62  TransportEquations equations(m_cd);
63 
64  // -------------------------------------------------------------------------------
65  // Solve ODE's for cell2cell transport and diffusion.
66  // -------------------------------------------------------------------------------
67  const unsigned int num_eqns = equations.GetEquationCount();
68  if (num_eqns > 0) {
69  using State = vector<double>;
70  using System = OdeintTraits<State>::System;
71  using Solver = OdeintTraits<State>::Solver;
72 
73  // Get current state of ODEs from the mesh
74  State y_curr(num_eqns);
75  equations.GetVariables(y_curr);
76 
77  // Wraps the ODE function dydt = F(y,t)
78  System ode_function = [&equations](const State& y, State& dydt, const double) {
79  equations.SetVariables(y);
80  equations.GetDerivatives(dydt);
81  };
82 
83  // Construct a suitable ODE solver either by looking at config or picking a default.
84  Solver ode_solver;
85  if (OdeintFactory0().ProvidesSolver(m_ode_solver)) {
86  // Solvers with fixed time steps
87  ode_solver = OdeintFactory0().Create(m_ode_solver);
88  } else if (OdeintFactory2().ProvidesSolver(m_ode_solver)) {
89  // Solvers with adaptive time steps
90  ode_solver = OdeintFactory2().Create(m_ode_solver, m_abs_tolerance, m_rel_tolerance);
91  } else {
92  // Pick a safe default in case none was given
93  ode_solver = OdeintFactory2().Create("runge_kutta_dopri5", m_abs_tolerance, m_rel_tolerance);
94  }
95 
96  // ODE system's current start_time.
97  const auto start_time = m_cd.m_time_data->m_sim_time;
98  ode_solver(ode_function, y_curr, start_time, start_time + time_slice, m_small_time_increment);
99 
100  // Update mesh to reflect current state of ODEs
101  equations.SetVariables(y_curr);
102 
103  // --------------------------------------------------------------------
104  // Post-solve checks
105  // --------------------------------------------------------------------
106  if (m_stationarity_check) {
107  State dydt(num_eqns);
108  equations.GetDerivatives(dydt);
109  double norm = 0.0;
110  for (size_t i = 0; i < num_eqns; ++i) {
111  norm = max(norm, fabs(dydt[i]));
112  }
113  is_stationary = (norm <= m_abs_tolerance);
114  }
115  }
116 
117  // -------------------------------------------------------------------------------
118  // Update simulated time.
119  // -------------------------------------------------------------------------------
120  m_cd.m_time_data->m_sim_time += time_slice;
121 
122  // -------------------------------------------------------------------------------
123  // We are done ...
124  // -------------------------------------------------------------------------------
125  CumulativeTimings timings;
126  timings.Record(chrono_stat.GetName(), chrono_stat.Get());
127  return make_tuple(timings, is_stationary);
128 }
129 
130 } // namespace
131 } // namespace
Produces factory function for odeint integrator algorithm.
void GetVariables(std::vector< double > &values) const
Interact with the mesh to get values of transport variables.
Core data with mesh, parameters, random engine and time data.
Definition: CoreData.h:38
void SetVariables(std::vector< double > const &values) const
Interact with mesh to set values for transport variables.
Odeint traits header file.
STL namespace.
Interface for OdeintFactory0.
Produces factory function for odeint integrator algorithm.
std::string GetName() const
Return name of this stopwatch.
Definition: Stopwatch.h:88
Namespace for components of the Default model group.
Utility class to record durations in a cumulative manner.
Time evolver for diffusion and active transport.
std::tuple< SimTimingTraits::CumulativeTimings, bool > operator()(double time_slice, SimPhase phase=SimPhase::NONE)
Take a time step.
Definition: Transport.cpp:55
Transport(const CoreData &cd)
Initializing constructor.
Definition: Transport.cpp:36
void Initialize(const CoreData &cd)
Initialize or re-initialize.
Definition: Transport.cpp:41
T::duration Get() const
Returns the accumulated value without altering the stopwatch state.
Definition: Stopwatch.h:94
int GetEquationCount() const
Two equations per chemical for each walls, and one equation per chemical for each cell...
Equations for reaction and transport processes.
void GetDerivatives(std::vector< double > &derivs) const
Interact with the mesh to calculate values of derivatives,.
Provides a stopwatch interface to time: it accumulates time between start/stop pairs.
Definition: Stopwatch.h:39
Equations for diffusion and transport.
Interface for OdeintFactory2.
void Record(const std::string &name, const std::chrono::duration< R, P > &duration)
Record the duration for the given name.
bool Check() const
Verify all pointers non-null.
Definition: CoreData.h:53
Namespace for clock and timekeeping related classes.
Definition: ClockCLib.h:27