Nektar++
Monodomain.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File Monodomain.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: Monodomain cardiac electrophysiology homogenised model.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 
40 
41 using namespace std;
42 
43 namespace Nektar
44 {
45  /**
46  * @class Monodomain
47  *
48  * Base model of cardiac electrophysiology of the form
49  * \f{align*}{
50  * \frac{\partial u}{\partial t} = \nabla^2 u + J_{ion},
51  * \f}
52  * where the reaction term, \f$J_{ion}\f$ is defined by a specific cell
53  * model.
54  *
55  * This implementation, at present, treats the reaction terms explicitly
56  * and the diffusive element implicitly.
57  */
58 
59  /**
60  * Registers the class with the Factory.
61  */
62  string Monodomain::className
64  "Monodomain",
65  Monodomain::create,
66  "Monodomain model of cardiac electrophysiology.");
67 
68 
69  /**
70  *
71  */
72  Monodomain::Monodomain(
75  : UnsteadySystem(pSession, pGraph)
76  {
77  }
78 
79 
80  /**
81  *
82  */
84  {
86 
87  m_session->LoadParameter("Chi", m_chi);
88  m_session->LoadParameter("Cm", m_capMembrane);
89 
90  std::string vCellModel;
91  m_session->LoadSolverInfo("CELLMODEL", vCellModel, "");
92 
93  ASSERTL0(vCellModel != "", "Cell Model not specified.");
94 
96  vCellModel, m_session, m_fields[0]);
97 
98  m_intVariables.push_back(0);
99 
100  // Load variable coefficients
101  StdRegions::VarCoeffType varCoeffEnum[6] = {
108  };
109  std::string varCoeffString[6] = {"xx","xy","yy","xz","yz","zz"};
110  std::string aniso_var[3] = {"fx", "fy", "fz"};
111 
112  const int nq = m_fields[0]->GetNpoints();
113  const int nVarDiffCmpts = m_spacedim * (m_spacedim + 1) / 2;
114 
115  // Allocate storage for variable coeffs and initialize to 1.
116  for (int i = 0, k = 0; i < m_spacedim; ++i)
117  {
118  for (int j = 0; j < i+1; ++j)
119  {
120  if (i == j)
121  {
122  m_vardiff[varCoeffEnum[k]] = Array<OneD, NekDouble>(nq, 1.0);
123  }
124  else
125  {
126  m_vardiff[varCoeffEnum[k]] = Array<OneD, NekDouble>(nq, 0.0);
127  }
128  ++k;
129  }
130  }
131 
132  // Apply fibre map f \in [0,1], scale to conductivity range
133  // [o_min,o_max], specified by the session parameters o_min and o_max
134  if (m_session->DefinesFunction("AnisotropicConductivity"))
135  {
136  if (m_session->DefinesCmdLineArgument("verbose"))
137  {
138  cout << "Loading Anisotropic Fibre map." << endl;
139  }
140 
141  NekDouble o_min = m_session->GetParameter("o_min");
142  NekDouble o_max = m_session->GetParameter("o_max");
143  int k = 0;
144 
145  Array<OneD, NekDouble> vTemp_i;
146  Array<OneD, NekDouble> vTemp_j;
147 
148  /*
149  * Diffusivity matrix D is upper triangular and defined as
150  * d_00 d_01 d_02
151  * d_11 d_12
152  * d_22
153  *
154  * Given a principle fibre direction _f_ the diffusivity is given
155  * by
156  * d_ij = { D_2 + (D_1 - D_2) f_i f_j if i==j
157  * { (D_1 - D_2) f_i f_j if i!=j
158  *
159  * The vector _f_ is given in terms of the variables fx,fy,fz in the
160  * function AnisotropicConductivity. The values of D_1 and D_2 are
161  * the parameters o_max and o_min, respectively.
162  */
163 
164  // Loop through columns of D
165  for (int j = 0; j < m_spacedim; ++j)
166  {
167  ASSERTL0(m_session->DefinesFunction("AnisotropicConductivity",
168  aniso_var[j]),
169  "Function 'AnisotropicConductivity' not correctly "
170  "defined.");
171  GetFunction("AnisotropicConductivity")->Evaluate(aniso_var[j], vTemp_j);
172 
173  // Loop through rows of D
174  for (int i = 0; i < j + 1; ++i)
175  {
176  ASSERTL0(m_session->DefinesFunction(
177  "AnisotropicConductivity",aniso_var[i]),
178  "Function 'AnisotropicConductivity' not correctly "
179  "defined.");
180  GetFunction("AnisotropicConductivity")->Evaluate(aniso_var[i], vTemp_i);
181 
182  Vmath::Vmul(nq, vTemp_i, 1, vTemp_j, 1,
183  m_vardiff[varCoeffEnum[k]], 1);
184 
185  Vmath::Smul(nq, o_max-o_min,
186  m_vardiff[varCoeffEnum[k]], 1,
187  m_vardiff[varCoeffEnum[k]], 1);
188 
189  if (i == j)
190  {
191  Vmath::Sadd(nq, o_min,
192  m_vardiff[varCoeffEnum[k]], 1,
193  m_vardiff[varCoeffEnum[k]], 1);
194  }
195 
196  ++k;
197  }
198  }
199  }
200  else
201  {
202  // Otherwise apply isotropic conductivity value (o_max) to
203  // diagonal components of tensor
204  NekDouble o_max = m_session->GetParameter("o_max");
205  for (int i = 0; i < nVarDiffCmpts; ++i)
206  {
207  Vmath::Smul(nq,o_max,
208  m_vardiff[varCoeffEnum[i]], 1,
209  m_vardiff[varCoeffEnum[i]], 1);
210  }
211  }
212 
213  // Scale by scar map (range 0->1) derived from intensity map
214  // (range d_min -> d_max)
215  if (m_session->DefinesFunction("IsotropicConductivity"))
216  {
217  if (m_session->DefinesCmdLineArgument("verbose"))
218  {
219  cout << "Loading Isotropic Conductivity map." << endl;
220  }
221 
222  const std::string varName = "intensity";
224  GetFunction( "IsotropicConductivity")->Evaluate(varName, vTemp);
225 
226  // If the d_min and d_max parameters are defined, then we need to
227  // rescale the isotropic conductivity to convert from the source
228  // domain (e.g. late-gad intensity) to conductivity
229  if ( m_session->DefinesParameter("d_min") ||
230  m_session->DefinesParameter("d_max") ) {
231  const NekDouble f_min = m_session->GetParameter("d_min");
232  const NekDouble f_max = m_session->GetParameter("d_max");
233  const NekDouble scar_min = 0.0;
234  const NekDouble scar_max = 1.0;
235 
236  // Threshold based on d_min, d_max
237  for (int j = 0; j < nq; ++j)
238  {
239  vTemp[j] = (vTemp[j] < f_min ? f_min : vTemp[j]);
240  vTemp[j] = (vTemp[j] > f_max ? f_max : vTemp[j]);
241  }
242 
243  // Rescale to s \in [0,1] (0 maps to d_max, 1 maps to d_min)
244  Vmath::Sadd(nq, -f_min, vTemp, 1, vTemp, 1);
245  Vmath::Smul(nq, -1.0/(f_max-f_min), vTemp, 1, vTemp, 1);
246  Vmath::Sadd(nq, 1.0, vTemp, 1, vTemp, 1);
247  Vmath::Smul(nq, scar_max - scar_min, vTemp, 1, vTemp, 1);
248  Vmath::Sadd(nq, scar_min, vTemp, 1, vTemp, 1);
249  }
250 
251  // Scale anisotropic conductivity values
252  for (int i = 0; i < nVarDiffCmpts; ++i)
253  {
254  Vmath::Vmul(nq, vTemp, 1,
255  m_vardiff[varCoeffEnum[i]], 1,
256  m_vardiff[varCoeffEnum[i]], 1);
257  }
258  }
259 
260 
261  // Write out conductivity values
262  for (int j = 0, k = 0; j < m_spacedim; ++j)
263  {
264  // Loop through rows of D
265  for (int i = 0; i < j + 1; ++i)
266  {
267  // Transform variable coefficient and write out to file.
268  m_fields[0]->FwdTrans_IterPerExp(m_vardiff[varCoeffEnum[k]],
269  m_fields[0]->UpdateCoeffs());
270  std::stringstream filename;
271  filename << "Conductivity_" << varCoeffString[k] << ".fld";
272  WriteFld(filename.str());
273 
274  ++k;
275  }
276  }
277 
278  // Search through the loaded filters and pass the cell model to any
279  // CheckpointCellModel filters loaded.
280  for (auto &x : m_filters)
281  {
282  if (x.first == "CheckpointCellModel")
283  {
284  std::shared_ptr<FilterCheckpointCellModel> c
285  = std::dynamic_pointer_cast<FilterCheckpointCellModel>(
286  x.second);
287  c->SetCellModel(m_cell);
288  }
289  if (x.first == "CellHistoryPoints")
290  {
291  std::shared_ptr<FilterCellHistoryPoints> c
292  = std::dynamic_pointer_cast<FilterCellHistoryPoints>(
293  x.second);
294  c->SetCellModel(m_cell);
295  }
296  }
297 
298  // Load stimuli
300 
301  if (!m_explicitDiffusion)
302  {
304  }
306  }
307 
308 
309  /**
310  *
311  */
313  {
314 
315  }
316 
317 
318  /**
319  * @param inarray Input array.
320  * @param outarray Output array.
321  * @param time Current simulation time.
322  * @param lambda Timestep.
323  */
325  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
326  Array<OneD, Array<OneD, NekDouble> >&outarray,
327  const NekDouble time,
328  const NekDouble lambda)
329  {
330  int nvariables = inarray.size();
331  int nq = m_fields[0]->GetNpoints();
333  // lambda = \Delta t
334  factors[StdRegions::eFactorLambda] = 1.0/lambda*m_chi*m_capMembrane;
335 
336  // We solve ( \nabla^2 - HHlambda ) Y[i] = rhs [i]
337  // inarray = input: \hat{rhs} -> output: \hat{Y}
338  // outarray = output: nabla^2 \hat{Y}
339  // where \hat = modal coeffs
340  for (int i = 0; i < nvariables; ++i)
341  {
342  // Multiply 1.0/timestep
343  Vmath::Smul(nq, -factors[StdRegions::eFactorLambda], inarray[i], 1,
344  m_fields[i]->UpdatePhys(), 1);
345 
346  // Solve a system of equations with Helmholtz solver and transform
347  // back into physical space.
348  m_fields[i]->HelmSolve(m_fields[i]->GetPhys(),
349  m_fields[i]->UpdateCoeffs(),
350  factors, m_vardiff);
351 
352  m_fields[i]->BwdTrans( m_fields[i]->GetCoeffs(),
353  m_fields[i]->UpdatePhys());
354  m_fields[i]->SetPhysState(true);
355 
356  // Copy the solution vector (required as m_fields must be set).
357  outarray[i] = m_fields[i]->GetPhys();
358  }
359  }
360 
361 
362  /**
363  *
364  */
366  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
367  Array<OneD, Array<OneD, NekDouble> >&outarray,
368  const NekDouble time)
369  {
370  // Compute I_ion
371  m_cell->TimeIntegrate(inarray, outarray, time);
372 
373  // Compute I_stim
374  for (unsigned int i = 0; i < m_stimulus.size(); ++i)
375  {
376  m_stimulus[i]->Update(outarray, time);
377  }
378  }
379 
380 
381  /**
382  *
383  */
385  bool dumpInitialConditions,
386  const int domain)
387  {
389  dumpInitialConditions,
390  domain);
391  m_cell->Initialise();
392  }
393 
394 
395  /**
396  *
397  */
399  {
401  if (m_session->DefinesFunction("d00") &&
402  m_session->GetFunctionType("d00", "intensity")
404  {
405  AddSummaryItem(s, "Diffusivity-x",
406  m_session->GetFunction("d00", "intensity")->GetExpression());
407  }
408  if (m_session->DefinesFunction("d11") &&
409  m_session->GetFunctionType("d11", "intensity")
411  {
412  AddSummaryItem(s, "Diffusivity-y",
413  m_session->GetFunction("d11", "intensity")->GetExpression());
414  }
415  if (m_session->DefinesFunction("d22") &&
416  m_session->GetFunctionType("d22", "intensity")
418  {
419  AddSummaryItem(s, "Diffusivity-z",
420  m_session->GetFunction("d22", "intensity")->GetExpression());
421  }
422  m_cell->GenerateSummary(s);
423  }
424 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:200
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:145
void DefineOdeRhs(FuncPointerT func, ObjectPointerT obj)
void DefineImplicitSolve(FuncPointerT func, ObjectPointerT obj)
virtual void v_GenerateSummary(SummaryList &s)
Prints a summary of the model parameters.
Definition: Monodomain.cpp:398
void DoImplicitSolve(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, NekDouble time, NekDouble lambda)
Solve for the diffusion term.
Definition: Monodomain.cpp:324
void DoOdeRhs(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble time)
Computes the reaction terms and .
Definition: Monodomain.cpp:365
CellModelSharedPtr m_cell
Cell model.
Definition: Monodomain.h:102
StdRegions::VarCoeffMap m_vardiff
Variable diffusivity.
Definition: Monodomain.h:107
virtual void v_InitObject()
Init object for UnsteadySystem class.
Definition: Monodomain.cpp:83
NekDouble m_capMembrane
Definition: Monodomain.h:110
std::vector< StimulusSharedPtr > m_stimulus
Definition: Monodomain.h:104
virtual ~Monodomain()
Desctructor.
Definition: Monodomain.cpp:312
virtual void v_SetInitialConditions(NekDouble initialtime, bool dumpInitialConditions, const int domain)
Sets a custom initial condition.
Definition: Monodomain.cpp:384
int m_spacedim
Spatial dimension (>= expansion dim).
virtual SOLVER_UTILS_EXPORT void v_SetInitialConditions(NekDouble initialtime=0.0, bool dumpInitialConditions=true, const int domain=0)
Array< OneD, MultiRegions::ExpListSharedPtr > m_fields
Array holding all dependent variables.
SOLVER_UTILS_EXPORT void WriteFld(const std::string &outname)
Write field data to the given filename.
LibUtilities::SessionReaderSharedPtr m_session
The session reader.
SOLVER_UTILS_EXPORT SessionFunctionSharedPtr GetFunction(std::string name, const MultiRegions::ExpListSharedPtr &field=MultiRegions::NullExpListSharedPtr, bool cache=false)
Get a SessionFunction by name.
Base class for unsteady solvers.
LibUtilities::TimeIntegrationSchemeOperators m_ode
The time integration scheme operators to use.
std::vector< std::pair< std::string, FilterSharedPtr > > m_filters
bool m_explicitDiffusion
Indicates if explicit or implicit treatment of diffusion is used.
virtual SOLVER_UTILS_EXPORT void v_GenerateSummary(SummaryList &s)
Print a summary of time stepping parameters.
virtual SOLVER_UTILS_EXPORT void v_InitObject()
Init object for UnsteadySystem class.
static std::vector< StimulusSharedPtr > LoadStimuli(const LibUtilities::SessionReaderSharedPtr &pSession, const MultiRegions::ExpListSharedPtr &pField)
Definition: Stimulus.cpp:92
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::vector< std::pair< std::string, std::string > > SummaryList
Definition: Misc.h:46
EquationSystemFactory & GetEquationSystemFactory()
void AddSummaryItem(SummaryList &l, const std::string &name, const std::string &value)
Adds a summary item to the summary info list.
Definition: Misc.cpp:47
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:314
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
CellModelFactory & GetCellModelFactory()
Definition: CellModel.cpp:46
double NekDouble
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition: Vmath.cpp:192
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
Definition: Vmath.cpp:225
void Sadd(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Add vector y = alpha - x.
Definition: Vmath.cpp:341