Nektar++
CellModel.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File CellModel.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Cell model base class.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
36 
38 
40 //#include <LibUtilities/LinearAlgebra/Blas.hpp>
41 
42 using namespace std;
43 
44 namespace Nektar
45 {
47  {
48  static CellModelFactory instance;
49  return instance;
50  }
51 
52  /**
53  * @class CellModel
54  *
55  * The CellModel class and derived classes implement a range of cell model
56  * ODE systems. A cell model comprises a system of ion concentration
57  * variables and zero or more gating variables. Gating variables are
58  * time-integrated using the Rush-Larsen method and for each variable y,
59  * the corresponding y_inf and tau_y value is computed by Update(). The tau
60  * values are stored in separate storage to inarray/outarray, #m_gates_tau.
61  */
62 
63  /**
64  * Cell model base class constructor.
65  */
66  CellModel::CellModel(const LibUtilities::SessionReaderSharedPtr& pSession,
67  const MultiRegions::ExpListSharedPtr& pField)
68  {
69  m_session = pSession;
70  m_field = pField;
71  m_lastTime = 0.0;
72  m_substeps = pSession->GetParameter("Substeps");
73  m_nvar = 0;
74  m_useNodal = false;
75 
76  // Number of points in nodal space is the number of coefficients
77  // in modified basis
78  std::set<enum LibUtilities::ShapeType> s;
79  for (unsigned int i = 0; i < m_field->GetNumElmts(); ++i)
80  {
81  s.insert(m_field->GetExp(i)->DetShapeType());
82  }
83 
84  // Use nodal projection if only triangles
85  if (s.size() == 1 && (s.count(LibUtilities::eTriangle) == 1 ||
86  s.count(LibUtilities::eTetrahedron) == 1))
87  {
88  // This is disabled for now as it causes problems at high order.
89  // m_useNodal = true;
90  }
91 
92  // ---------------------------
93  // Move to nodal points
94  if (m_useNodal)
95  {
96  m_nq = pField->GetNcoeffs();
97  int order = m_field->GetExp(0)->GetBasis(0)->GetNumModes();
98 
99  // Set up a nodal tri
109 
114  }
115  else
116  {
117  m_nq = pField->GetTotPoints();
118  }
119  }
120 
121 
122  /**
123  * Initialise the cell model. Allocate workspace and variable storage.
124  */
125  void CellModel::Initialise()
126  {
127  ASSERTL1(m_nvar > 0, "Cell model must have at least 1 variable.");
128 
129  m_cellSol = Array<OneD, Array<OneD, NekDouble> >(m_nvar);
130  m_wsp = Array<OneD, Array<OneD, NekDouble> >(m_nvar);
131  for (unsigned int i = 0; i < m_nvar; ++i)
132  {
133  m_cellSol[i] = Array<OneD, NekDouble>(m_nq);
134  m_wsp[i] = Array<OneD, NekDouble>(m_nq);
135  }
136  m_gates_tau = Array<OneD, Array<OneD, NekDouble> >(m_gates.size());
137  for (unsigned int i = 0; i < m_gates.size(); ++i)
138  {
139  m_gates_tau[i] = Array<OneD, NekDouble>(m_nq);
140  }
141 
142  if (m_session->DefinesFunction("CellModelInitialConditions"))
143  {
144  LoadCellModel();
145  }
146  else
147  {
148  v_SetInitialConditions();
149  }
150  }
151 
152  /**
153  * Integrates the cell model for one PDE time-step. Cell model is
154  * sub-stepped.
155  *
156  * Ion concentrations and membrane potential are integrated using forward
157  * Euler, while gating variables are integrated using the Rush-Larsen
158  * scheme.
159  */
160  void CellModel::TimeIntegrate(
161  const Array<OneD, const Array<OneD, NekDouble> > &inarray,
162  Array<OneD, Array<OneD, NekDouble> > &outarray,
163  const NekDouble time)
164  {
165  int phys_offset = 0;
166  int coef_offset = 0;
167  int nvar = inarray.size();
169 
170  // ---------------------------
171  // Check nodal temp array set up
172  if (m_useNodal)
173  {
174  if (!m_nodalTmp.size())
175  {
176  m_nodalTmp = Array<OneD, Array<OneD, NekDouble> >(nvar);
177  for (unsigned int k = 0; k < nvar; ++k)
178  {
179  m_nodalTmp[k] = Array<OneD, NekDouble>(m_nq);
180  }
181  }
182 
183  // Move to nodal points
184  Array<OneD, NekDouble> tmpCoeffs(max(m_nodalTri->GetNcoeffs(), m_nodalTet->GetNcoeffs()));
185 
186  for (unsigned int k = 0; k < nvar; ++k)
187  {
188  for (unsigned int i = 0; i < m_field->GetNumElmts(); ++i)
189  {
190  phys_offset = m_field->GetPhys_Offset(i);
191  coef_offset = m_field->GetCoeff_Offset(i);
192  if (m_field->GetExp(0)->DetShapeType() == LibUtilities::eTriangle)
193  {
194  m_field->GetExp(0)->FwdTrans(inarray[k] + phys_offset, tmpCoeffs);
195  m_nodalTri->ModalToNodal(tmpCoeffs, tmp=m_nodalTmp[k]+coef_offset);
196  }
197  else
198  {
199  m_field->GetExp(0)->FwdTrans(inarray[k] + phys_offset, tmpCoeffs);
200  m_nodalTet->ModalToNodal(tmpCoeffs, tmp=m_nodalTmp[k]+coef_offset);
201  }
202  }
203  }
204  // Copy new transmembrane potential into cell model
205  Vmath::Vcopy(m_nq, m_nodalTmp[0], 1, m_cellSol[0], 1);
206  }
207  else
208  {
209  // Copy new transmembrane potential into cell model
210  Vmath::Vcopy(m_nq, inarray[0], 1, m_cellSol[0], 1);
211  }
212  // -------------------------
213 
214  NekDouble delta_t = (time - m_lastTime)/m_substeps;
215 
216 
217  // Perform substepping
218  for (unsigned int i = 0; i < m_substeps - 1; ++i)
219  {
220  Update(m_cellSol, m_wsp, time);
221  // Voltage
222  Vmath::Svtvp(m_nq, delta_t, m_wsp[0], 1, m_cellSol[0], 1, m_cellSol[0], 1);
223  // Ion concentrations
224  for (unsigned int j = 0; j < m_concentrations.size(); ++j)
225  {
226  Vmath::Svtvp(m_nq, delta_t, m_wsp[m_concentrations[j]], 1, m_cellSol[m_concentrations[j]], 1, m_cellSol[m_concentrations[j]], 1);
227  }
228  // Gating variables: Rush-Larsen scheme
229  for (unsigned int j = 0; j < m_gates.size(); ++j)
230  {
231  Vmath::Sdiv(m_nq, -delta_t, m_gates_tau[j], 1, m_gates_tau[j], 1);
232  Vmath::Vexp(m_nq, m_gates_tau[j], 1, m_gates_tau[j], 1);
233  Vmath::Vsub(m_nq, m_cellSol[m_gates[j]], 1, m_wsp[m_gates[j]], 1, m_cellSol[m_gates[j]], 1);
234  Vmath::Vvtvp(m_nq, m_cellSol[m_gates[j]], 1, m_gates_tau[j], 1, m_wsp[m_gates[j]], 1, m_cellSol[m_gates[j]], 1);
235  }
236  }
237 
238  // Perform final cell model step
239  Update(m_cellSol, m_wsp, time);
240 
241  // Output dV/dt from last step but integrate remaining cell model vars
242  // Transform cell model I_total from nodal to modal space
243  if (m_useNodal)
244  {
245  Array<OneD, NekDouble> tmpCoeffs(max(m_nodalTri->GetNcoeffs(), m_nodalTet->GetNcoeffs()));
246 
247  for (unsigned int k = 0; k < nvar; ++k)
248  {
249  for (unsigned int i = 0; i < m_field->GetNumElmts(); ++i)
250  {
251  int phys_offset = m_field->GetPhys_Offset(i);
252  int coef_offset = m_field->GetCoeff_Offset(i);
253  if (m_field->GetExp(0)->DetShapeType() == LibUtilities::eTriangle)
254  {
255  m_nodalTri->NodalToModal(m_wsp[k]+coef_offset, tmpCoeffs);
256  m_field->GetExp(0)->BwdTrans(tmpCoeffs, tmp=outarray[k] + phys_offset);
257  }
258  else
259  {
260  m_nodalTet->NodalToModal(m_wsp[k]+coef_offset, tmpCoeffs);
261  m_field->GetExp(0)->BwdTrans(tmpCoeffs, tmp=outarray[k] + phys_offset);
262  }
263  }
264  }
265  }
266  else
267  {
268  Vmath::Vcopy(m_nq, m_wsp[0], 1, outarray[0], 1);
269  }
270 
271  // Ion concentrations
272  for (unsigned int j = 0; j < m_concentrations.size(); ++j)
273  {
274  Vmath::Svtvp(m_nq, delta_t, m_wsp[m_concentrations[j]], 1, m_cellSol[m_concentrations[j]], 1, m_cellSol[m_concentrations[j]], 1);
275  }
276 
277  // Gating variables: Rush-Larsen scheme
278  for (unsigned int j = 0; j < m_gates.size(); ++j)
279  {
280  Vmath::Sdiv(m_nq, -delta_t, m_gates_tau[j], 1, m_gates_tau[j], 1);
281  Vmath::Vexp(m_nq, m_gates_tau[j], 1, m_gates_tau[j], 1);
282  Vmath::Vsub(m_nq, m_cellSol[m_gates[j]], 1, m_wsp[m_gates[j]], 1, m_cellSol[m_gates[j]], 1);
283  Vmath::Vvtvp(m_nq, m_cellSol[m_gates[j]], 1, m_gates_tau[j], 1, m_wsp[m_gates[j]], 1, m_cellSol[m_gates[j]], 1);
284  }
285 
286  m_lastTime = time;
287  }
288 
289  Array<OneD, NekDouble> CellModel::GetCellSolutionCoeffs(unsigned int idx)
290  {
291  ASSERTL0(idx < m_nvar, "Index out of range for cell model.");
292 
293  Array<OneD, NekDouble> outarray(m_field->GetNcoeffs());
295 
296  if (m_useNodal)
297  {
298  for (unsigned int i = 0; i < m_field->GetNumElmts(); ++i)
299  {
300  int coef_offset = m_field->GetCoeff_Offset(i);
301  if (m_field->GetExp(0)->DetShapeType() == LibUtilities::eTriangle)
302  {
303  m_nodalTri->NodalToModal(m_cellSol[idx]+coef_offset, tmp=outarray+coef_offset);
304  }
305  else
306  {
307  m_nodalTet->NodalToModal(m_cellSol[idx]+coef_offset, tmp=outarray+coef_offset);
308  }
309  }
310  }
311  else
312  {
313  m_field->FwdTrans_IterPerExp(m_cellSol[idx], outarray);
314  }
315 
316  return outarray;
317  }
318 
319  Array<OneD, NekDouble> CellModel::GetCellSolution(unsigned int idx)
320  {
321  return m_cellSol[idx];
322  }
323 
324  void CellModel::LoadCellModel()
325  {
326  const bool root = (m_session->GetComm()->GetRank() == 0);
327  const std::string fncName = "CellModelInitialConditions";
328  const int nvar = m_cellSol[0].size();
329  std::string varName;
330  Array<OneD, NekDouble> coeffs(m_field->GetNcoeffs());
332  int j = 0;
333 
334  SpatialDomains::MeshGraphSharedPtr vGraph = m_field->GetGraph();
335 
336  if (root)
337  {
338  cout << "Cell model initial conditions: " << endl;
339  }
340 
341  // First determine all the files we need to load
342  std::set<std::string> filelist;
343  for (j = 1; j < nvar; ++j)
344  {
345  // Get the name of the jth variable
346  varName = GetCellVarName(j);
347 
348  if (m_session->GetFunctionType(fncName, varName) ==
350  {
351  filelist.insert(m_session->GetFunctionFilename(fncName,
352  varName));
353  }
354  }
355 
356  // Read files
357  typedef std::vector<LibUtilities::FieldDefinitionsSharedPtr> FDef;
358  typedef std::vector<std::vector<NekDouble> > FData;
359  std::map<std::string, FDef> FieldDef;
360  std::map<std::string, FData> FieldData;
361  LibUtilities::FieldMetaDataMap fieldMetaDataMap;
362 
363  for (auto &setIt : filelist)
364  {
365  if (root)
366  {
367  cout << " - Reading file: " << setIt << endl;
368  }
369  FieldDef[setIt] = FDef(0);
370  FieldData[setIt] = FData(0);
372  LibUtilities::FieldIO::CreateForFile(m_session, setIt);
373  fld->Import(setIt, FieldDef[setIt], FieldData[setIt],
374  fieldMetaDataMap);
375  }
376 
377  // Get time of checkpoint from file if available
378  auto iter = fieldMetaDataMap.find("Time");
379  if(iter != fieldMetaDataMap.end())
380  {
381  m_lastTime = boost::lexical_cast<NekDouble>(iter->second);
382  }
383 
384  // Load each cell model variable
385  // j=0 and j=1 are for transmembrane or intra/extra-cellular volt.
386  Vmath::Zero(m_nq, m_cellSol[0], 1);
387  for(j = 1; j < m_cellSol.size(); ++j)
388  {
389  // Get the name of the jth variable
390  varName = GetCellVarName(j);
391 
392  // Check if this variable is defined in a file or analytically
393  if (m_session->GetFunctionType(fncName, varName) ==
395  {
396  const std::string file =
397  m_session->GetFunctionFilename(fncName, varName);
398 
399  if (root)
400  {
401  cout << " - Field " << varName << ": from file "
402  << file << endl;
403  }
404 
405  // Extract the data into the modal coefficients
406  for(int i = 0; i < FieldDef[file].size(); ++i)
407  {
408  m_field->ExtractDataToCoeffs(FieldDef[file][i],
409  FieldData[file][i],
410  varName,
411  coeffs);
412  }
413 
414  // If using nodal cell model then we do a modal->nodal transform
415  // otherwise we do a backward transform onto physical points.
416  if (m_useNodal)
417  {
418  for (unsigned int i = 0; i < m_field->GetNumElmts(); ++i)
419  {
420  int coef_offset = m_field->GetCoeff_Offset(i);
421  if (m_field->GetExp(0)->DetShapeType() ==
423  {
424  m_nodalTri->ModalToNodal(coeffs+coef_offset,
425  tmp=m_cellSol[j]+coef_offset);
426  }
427  else
428  {
429  m_nodalTet->ModalToNodal(coeffs+coef_offset,
430  tmp=m_cellSol[j]+coef_offset);
431  }
432  }
433  }
434  else
435  {
436  m_field->BwdTrans(coeffs, m_cellSol[j]);
437  }
438  }
439  else if (m_session->GetFunctionType(fncName, varName) ==
441  {
443  m_session->GetFunction(fncName, varName);
444 
445  if (root)
446  {
447  cout << " - Field " << varName << ": "
448  << equ->GetExpression() << endl;
449  }
450 
451  const unsigned int nphys = m_field->GetNpoints();
452  Array<OneD, NekDouble> x0(nphys);
453  Array<OneD, NekDouble> x1(nphys);
454  Array<OneD, NekDouble> x2(nphys);
455  m_field->GetCoords(x0,x1,x2);
456 
457  if (m_useNodal)
458  {
459  Array<OneD, NekDouble> phys(nphys);
460  Array<OneD, NekDouble> tmpCoeffs(max(m_nodalTri->GetNcoeffs(), m_nodalTet->GetNcoeffs()));
461 
462  equ->Evaluate(x0, x1, x2, phys);
463  for (unsigned int i = 0; i < m_field->GetNumElmts(); ++i)
464  {
465  int phys_offset = m_field->GetPhys_Offset(i);
466  int coef_offset = m_field->GetCoeff_Offset(i);
467  if (m_field->GetExp(0)->DetShapeType() ==
469  {
470  m_field->GetExp(0)->FwdTrans(
471  phys + phys_offset, tmpCoeffs);
472  m_nodalTri->ModalToNodal(
473  tmpCoeffs,
474  tmp = m_cellSol[j] + coef_offset);
475  }
476  else
477  {
478  m_field->GetExp(0)->FwdTrans(
479  phys + phys_offset, tmpCoeffs);
480  m_nodalTet->ModalToNodal(
481  tmpCoeffs,
482  tmp = m_cellSol[j] + coef_offset);
483  }
484  }
485  }
486  else
487  {
488  equ->Evaluate(x0, x1, x2, m_cellSol[j]);
489  }
490  }
491  }
492  }
493 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:250
Describes the specification for a Basis.
Definition: Basis.h:50
Provides a generic Factory class.
Definition: NekFactory.hpp:105
Defines a specification for a set of points.
Definition: Points.h:60
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:306
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:52
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:131
@ eNodalTriEvenlySpaced
2D Evenly-spaced points on a Triangle
Definition: PointsType.h:71
@ eGaussRadauMAlpha2Beta0
Gauss Radau pinned at x=-1, .
Definition: PointsType.h:59
@ eGaussLobattoLegendre
1D Gauss-Lobatto-Legendre quadrature points
Definition: PointsType.h:51
@ eNodalTetEvenlySpaced
3D Evenly-spaced points on a Tetrahedron
Definition: PointsType.h:72
@ eGaussRadauMAlpha1Beta0
Gauss Radau pinned at x=-1, .
Definition: PointsType.h:58
@ eModified_B
Principle Modified Functions .
Definition: BasisType.h:49
@ eModified_C
Principle Modified Functions .
Definition: BasisType.h:50
@ eModified_A
Principle Modified Functions .
Definition: BasisType.h:48
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
CellModelFactory & GetCellModelFactory()
Definition: CellModel.cpp:46
double NekDouble
void Vexp(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:116
void Svtvp(int n, const T alpha, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
svtvp (scalar times vector plus vector): z = alpha*x + y
Definition: Vmath.cpp:565
void Vvtvp(int n, const T *w, const int incw, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
vvtvp (vector times vector plus vector): z = w*x + y
Definition: Vmath.cpp:513
void Sdiv(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha/y.
Definition: Vmath.cpp:291
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:436
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1199
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.cpp:372