VPTissue Reference Manual
TransportEquations.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 "TransportEquations.h"
21 
22 #include "bio/Cell.h"
23 #include "bio/Mesh.h"
24 #include "bio/Wall.h"
26 #include "sim/CoreData.h"
27 
28 #include <boost/property_tree/ptree.hpp>
29 #include <algorithm>
30 #include <cmath>
31 #include <sstream>
32 #include <stdexcept>
33 
34 using namespace std;
35 using namespace boost::property_tree;
36 using namespace SimPT_Sim::Util;
37 
38 namespace SimPT_Sim {
39 
40 TransportEquations::TransportEquations(const CoreData& cd)
41 {
42  Initialize(cd);
43 }
44 
45 void TransportEquations::GetDerivatives(vector<double>& derivs) const
46 {
47  // Clear any previous entries in the vector to 0, just to be safe.
48  derivs.assign(m_equation_count, 0.0);
49 
50  // Index over the concatenated vector of chem. values for cells & walls.
51  size_t i = 0;
52 
53  // First part of derivs vector relates to chem. values of cells.
54  for (auto c : m_mesh->GetCells()) {
55  m_cell_chemistry(c, &(derivs[i]));
56  i += m_effective_nchem;
57  }
58 
59  // Second part of derivs vector relates to chem. values of walls.
60  for (auto w : m_mesh->GetWalls()) {
61  m_wall_chemistry(w, &(derivs[i]), &(derivs[i + m_effective_nchem]));
62 
63  // Transport function adds to derivatives of cell chemicals
64  double* dchem_c1 = &(derivs[w->GetC1()->GetIndex() * m_effective_nchem]);
65  double* dchem_c2 = &(derivs[w->GetC2()->GetIndex() * m_effective_nchem]);
66  // TODO: (PK) this is not a very clean solution!
67  // quick fix: dummy values to prevent end user from writing into outer space and causing a crash
68  // start here if you want to implement chemical input/output into environment over boundaries
69  double dummy1;
70  double dummy2;
71  /* Check if c1/2 is the boundary polygon */
72  if (w->GetC1()->GetIndex() < 0) {
73  dchem_c1 = &dummy1;
74  }
75  if (w->GetC2()->GetIndex() < 0) {
76  dchem_c2 = &dummy2;
77  }
78  m_cell2cell_transport(w, dchem_c1, dchem_c2);
79  i += 2 * m_effective_nchem;
80  }
81 }
82 
83 int TransportEquations::GetEquationCount() const
84 {
85  return m_equation_count;
86 }
87 
88 void TransportEquations::GetVariables(vector<double> &values) const
89 {
90  // Index over the concatenated vector of chem. values for cells & walls.
91  size_t i = 0;
92 
93  // First part of variables vector relates to chem. values of cells.
94  for (auto const& cell : m_mesh->GetCells()) {
95  for (size_t ch = 0; ch < static_cast<size_t>(m_effective_nchem); ch++) {
96  values[i + ch] = cell->GetChemical(ch);
97  }
98  i += m_effective_nchem;
99  }
100 
101  // Second part of variables vector relates to chem. values of walls.
102  for (auto const& wall : m_mesh->GetWalls()) {
103  for (size_t ch = 0; ch < static_cast<size_t>(m_effective_nchem); ch++) {
104  values[i + ch] = wall->GetTransporters1(ch);
105  }
106  i += m_effective_nchem;
107 
108  for (size_t ch = 0; ch < static_cast<size_t>(m_effective_nchem); ch++) {
109  values[i + ch] = wall->GetTransporters2(ch);
110  }
111  i += m_effective_nchem;
112  }
113 }
114 
115 void TransportEquations::Initialize(const CoreData& cd)
116 {
117  assert(cd.Check() && "CoreData not ok.");
118  m_cd = cd;
119  m_mesh = m_cd.m_mesh.get();
120 
121  const auto model_group = m_cd.m_parameters->get<string>("model.group", "");
122  const auto factory = ComponentFactoryProxy::Create(model_group);
123  m_cell_chemistry = factory->CreateCellChemistry(m_cd);
124  m_cell2cell_transport = factory->CreateCellToCellTransport(m_cd);
125  m_wall_chemistry = factory->CreateWallChemistry(m_cd);
126 
127  const unsigned int mesh_cc = m_mesh->GetNumChemicals();
128  const unsigned int model_cc = m_cd.m_parameters->get<unsigned int>("model.cell_chemical_count");
129  if ((mesh_cc == 0) || (model_cc == 0) || (mesh_cc == model_cc)) {
130  m_effective_nchem = min(mesh_cc, model_cc);
131  } else {
132  ostringstream ss;
133  ss << "TransportEquations::Initialize>: mismatch in chemical counts: ";
134  ss << "mesh: " << mesh_cc << " , model: " << model_cc;
135  throw runtime_error(ss.str());
136  }
137 
138  m_equation_count = (2 * m_mesh->GetWalls().size() + m_mesh->GetCells().size()) * m_effective_nchem;
139 }
140 
141 void TransportEquations::SetVariables(const vector<double>& values) const
142 {
143  // Index over the concatenated vector of chem. values for cells & walls.
144  size_t i = 0;
145 
146  // First part of variables vector relates to chem. values of cells.
147  for (auto cell : m_mesh->GetCells()) {
148  for (size_t ch = 0; ch < static_cast<size_t>(m_effective_nchem); ch++) {
149  cell->SetChemical(ch, values[i + ch]);
150  }
151  i += m_effective_nchem;
152  }
153 
154  // Second part of variables vector relates to chem. values of walls.
155  for (const auto& wall : m_mesh->GetWalls()) {
156  for (size_t ch = 0; ch < static_cast<size_t>(m_effective_nchem); ch++) {
157  wall->SetTransporters1(ch, values[i + ch]);
158  }
159  i += m_effective_nchem;
160 
161  for (size_t ch = 0; ch < static_cast<size_t>(m_effective_nchem); ch++) {
162  wall->SetTransporters2(ch, values[i + ch]);
163  }
164  i += m_effective_nchem;
165  }
166 }
167 
168 } // namespace
Core data with mesh, parameters, random engine and time data.
Definition: CoreData.h:38
STL namespace.
Core data used during model execution.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
Namespace for the core simulator.
Interface for Cell.
Proxy for dealing with the factories.
Equations for diffusion and transport.
bool Check() const
Verify all pointers non-null.
Definition: CoreData.h:53
Interface for Wall.
Interface for Mesh.