Nektar++
BidomainRoth.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File BidomainRoth.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: Bidomain cardiac electrophysiology model - Roth formulation.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 
39 
40 using namespace std;
41 
42 namespace Nektar
43 {
44 
45 /**
46  * Registers the class with the Factory.
47  */
48 string BidomainRoth::className
50  "BidomainRoth",
51  BidomainRoth::create,
52  "Bidomain Roth model of cardiac electrophysiology.");
53 
54 
55 /**
56  *
57  */
58 BidomainRoth::BidomainRoth(
61  : UnsteadySystem(pSession, pGraph)
62 {
63 }
64 
65 
66 /**
67  *
68  */
70 {
72 
73  m_session->LoadParameter("Chi", m_chi);
74  m_session->LoadParameter("Cm", m_capMembrane);
75 
76  std::string vCellModel;
77  m_session->LoadSolverInfo("CELLMODEL", vCellModel, "");
78 
79  ASSERTL0(vCellModel != "", "Cell Model not specified.");
80 
82  vCellModel, m_session, m_fields[0]);
83 
84  m_intVariables.push_back(0);
85 
86  // Load variable coefficients
87  StdRegions::VarCoeffType varCoeffEnum[6] = {
94  };
95  std::string varCoeffString[6] = {"xx","xy","yy","xz","yz","zz"};
96  std::string aniso_var[3] = {"fx", "fy", "fz"};
97 
98  const int nq = m_fields[0]->GetNpoints();
99 
100  // Allocate storage for variable coeffs and initialize to 1.
101  for (int i = 0, k = 0; i < m_spacedim; ++i)
102  {
103  for (int j = 0; j < i+1; ++j)
104  {
105  if (i == j)
106  {
107  m_vardiffi[varCoeffEnum[k]] = Array<OneD, NekDouble>(nq, 1.0);
108  m_vardiffe[varCoeffEnum[k]] = Array<OneD, NekDouble>(nq, 1.0);
109  m_vardiffie[varCoeffEnum[k]] = Array<OneD, NekDouble>(nq, 1.0);
110  }
111  else
112  {
113  m_vardiffi[varCoeffEnum[k]] = Array<OneD, NekDouble>(nq, 0.0);
114  m_vardiffe[varCoeffEnum[k]] = Array<OneD, NekDouble>(nq, 0.0);
115  m_vardiffie[varCoeffEnum[k]] = Array<OneD, NekDouble>(nq, 0.0);
116  }
117  ++k;
118  }
119  }
120 
121  // Apply fibre map f \in [0,1], scale to conductivity range
122  // [o_min,o_max], specified by the session parameters o_min and o_max
123  if (m_session->DefinesFunction("ExtracellularAnisotropicConductivity"))
124  {
125  if (m_session->DefinesCmdLineArgument("verbose"))
126  {
127  cout << "Loading Extracellular Anisotropic Fibre map." << endl;
128  }
129 
130  NekDouble o_min = m_session->GetParameter("o_min");
131  NekDouble o_max = m_session->GetParameter("o_max");
132  int k = 0;
133 
134  Array<OneD, NekDouble> vTemp_i;
135  Array<OneD, NekDouble> vTemp_j;
136 
137  /*
138  * Diffusivity matrix D is upper triangular and defined as
139  * d_00 d_01 d_02
140  * d_11 d_12
141  * d_22
142  *
143  * Given a principle fibre direction _f_ the diffusivity is given
144  * by
145  * d_ij = { D_2 + (D_1 - D_2) f_i f_j if i==j
146  * { (D_1 - D_2) f_i f_j if i!=j
147  *
148  * The vector _f_ is given in terms of the variables fx,fy,fz in the
149  * function AnisotropicConductivity. The values of D_1 and D_2 are
150  * the parameters o_max and o_min, respectively.
151  */
152 
153  // Loop through columns of D
154  for (int j = 0; j < m_spacedim; ++j)
155  {
156  ASSERTL0(m_session->DefinesFunction(
157  "ExtracellularAnisotropicConductivity",
158  aniso_var[j]),
159  "Function 'AnisotropicConductivity' not correctly "
160  "defined.");
161 
162  GetFunction("ExtracellularAnisotropicConductivity")->Evaluate(aniso_var[j], vTemp_j);
163 
164  // Loop through rows of D
165  for (int i = 0; i < j + 1; ++i)
166  {
167  ASSERTL0(m_session->DefinesFunction(
168  "ExtracellularAnisotropicConductivity",
169  aniso_var[i]),
170  "Function 'ExtracellularAnisotropicConductivity' not "
171  "correctly defined.");
172 
173  GetFunction("ExtracellularAnisotropicConductivity")->Evaluate(aniso_var[i], vTemp_i);
174 
175  Vmath::Vmul(nq, vTemp_i, 1, vTemp_j, 1,
176  m_vardiffe[varCoeffEnum[k]], 1);
177 
178  Vmath::Smul(nq, o_max-o_min,
179  m_vardiffe[varCoeffEnum[k]], 1,
180  m_vardiffe[varCoeffEnum[k]], 1);
181 
182  if (i == j)
183  {
184  Vmath::Sadd(nq, o_min,
185  m_vardiffe[varCoeffEnum[k]], 1,
186  m_vardiffe[varCoeffEnum[k]], 1);
187  }
188 
189  }
190  }
191  }
192 
193  // Apply fibre map f \in [0,1], scale to conductivity range
194  // [o_min,o_max], specified by the session parameters o_min and o_max
195  if (m_session->DefinesFunction("IntracellularAnisotropicConductivity"))
196  {
197  if (m_session->DefinesCmdLineArgument("verbose"))
198  {
199  cout << "Loading Anisotropic Fibre map." << endl;
200  }
201 
202  NekDouble o_min = m_session->GetParameter("o_min");
203  NekDouble o_max = m_session->GetParameter("o_max");
204  int k = 0;
205 
206  Array<OneD, NekDouble> vTemp_i;
207  Array<OneD, NekDouble> vTemp_j;
208 
209  /*
210  * Diffusivity matrix D is upper triangular and defined as
211  * d_00 d_01 d_02
212  * d_11 d_12
213  * d_22
214  *
215  * Given a principle fibre direction _f_ the diffusivity is given
216  * by
217  * d_ij = { D_2 + (D_1 - D_2) f_i f_j if i==j
218  * { (D_1 - D_2) f_i f_j if i!=j
219  *
220  * The vector _f_ is given in terms of the variables fx,fy,fz in the
221  * function AnisotropicConductivity. The values of D_1 and D_2 are
222  * the parameters o_max and o_min, respectively.
223  */
224 
225  // Loop through columns of D
226  for (int j = 0; j < m_spacedim; ++j)
227  {
228  ASSERTL0(m_session->DefinesFunction(
229  "IntracellularAnisotropicConductivity",
230  aniso_var[j]),
231  "Function 'IntracellularAnisotropicConductivity' not "
232  "correctly defined.");
233 
234  GetFunction("IntracellularAnisotropicConductivity")->Evaluate(aniso_var[j], vTemp_j);
235 
236  // Loop through rows of D
237  for (int i = 0; i < j + 1; ++i)
238  {
239  ASSERTL0(m_session->DefinesFunction(
240  "IntracellularAnisotropicConductivity",
241  aniso_var[i]),
242  "Function 'IntracellularAnisotropicConductivity' not "
243  "correctly defined.");
244  GetFunction("IntracellularAnisotropicConductivity")->Evaluate(aniso_var[i], vTemp_i);
245 
246  Vmath::Vmul(nq, vTemp_i, 1, vTemp_j, 1,
247  m_vardiffi[varCoeffEnum[k]], 1);
248 
249  Vmath::Smul(nq, o_max-o_min,
250  m_vardiffi[varCoeffEnum[k]], 1,
251  m_vardiffi[varCoeffEnum[k]], 1);
252 
253  if (i == j)
254  {
255  Vmath::Sadd(nq, o_min,
256  m_vardiffi[varCoeffEnum[k]], 1,
257  m_vardiffi[varCoeffEnum[k]], 1);
258  }
259 
260  Vmath::Vadd(nq, m_vardiffe[varCoeffEnum[k]], 1,
261  m_vardiffi[varCoeffEnum[k]], 1,
262  m_vardiffie[varCoeffEnum[k]], 1);
263 
264  ++k;
265  }
266  }
267  }
268 
269 
270  // Write out conductivity values
271  for (int j = 0, k = 0; j < m_spacedim; ++j)
272  {
273  // Loop through rows of D
274  for (int i = 0; i < j + 1; ++i)
275  {
276  // Transform variable coefficient and write out to file.
277  m_fields[0]->FwdTrans_IterPerExp(m_vardiffi[varCoeffEnum[k]],
278  m_fields[0]->UpdateCoeffs());
279  std::stringstream filenamei;
280  filenamei << "IConductivity_" << varCoeffString[k] << ".fld";
281  WriteFld(filenamei.str());
282 
283  // Transform variable coefficient and write out to file.
284  m_fields[0]->FwdTrans_IterPerExp(m_vardiffe[varCoeffEnum[k]],
285  m_fields[0]->UpdateCoeffs());
286  std::stringstream filenamee;
287  filenamee << "EConductivity_" << varCoeffString[k] << ".fld";
288  WriteFld(filenamee.str());
289 
290  ++k;
291  }
292  }
293 
294  // Search through the loaded filters and pass the cell model to any
295  // CheckpointCellModel filters loaded.
296  for (auto &x : m_filters)
297  {
298  if (x.first == "CheckpointCellModel")
299  {
300  std::shared_ptr<FilterCheckpointCellModel> c
301  = std::dynamic_pointer_cast<FilterCheckpointCellModel>(
302  x.second);
303  c->SetCellModel(m_cell);
304  }
305  }
306  // Load stimuli
308 
309  if (!m_explicitDiffusion)
310  {
312  }
314 }
315 
316 
317 /**
318  *
319  */
321 {
322 
323 }
324 
325 
326 /**
327  * @param inarray Input array.
328  * @param outarray Output array.
329  * @param time Current simulation time.
330  * @param lambda Timestep.
331  */
333  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
334  Array<OneD, Array<OneD, NekDouble> >&outarray,
335  const NekDouble time,
336  const NekDouble lambda)
337 {
338  int nq = m_fields[0]->GetNpoints();
339 
340  StdRegions::ConstFactorMap factorsHelmholtz;
341  // lambda = \Delta t
342  factorsHelmholtz[StdRegions::eFactorLambda]
343  = 1.0/lambda*m_chi*m_capMembrane;
344 
345  // ------------------------------
346  // Solve Helmholtz problem for Vm
347  // ------------------------------
348  // Multiply 1.0/timestep
349  //Vmath::Vadd(nq, inarray[0], 1, ggrad, 1, m_fields[0]->UpdatePhys(), 1);
350  Vmath::Smul(nq, -factorsHelmholtz[StdRegions::eFactorLambda], inarray[0], 1,
351  m_fields[0]->UpdatePhys(), 1);
352 
353  // Solve a system of equations with Helmholtz solver and transform
354  // back into physical space.
355  m_fields[0]->HelmSolve(m_fields[0]->GetPhys(),
356  m_fields[0]->UpdateCoeffs(),
357  factorsHelmholtz, m_vardiffe);
358 
359  m_fields[0]->BwdTrans( m_fields[0]->GetCoeffs(),
360  m_fields[0]->UpdatePhys());
361  m_fields[0]->SetPhysState(true);
362 
363  // Copy the solution vector (required as m_fields must be set).
364  outarray[0] = m_fields[0]->GetPhys();
365 }
366 
367 
368 /**
369  *
370  */
372  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
373  Array<OneD, Array<OneD, NekDouble> >&outarray,
374  const NekDouble time)
375 {
376  int nq = m_fields[0]->GetNpoints();
377 
378  // Compute I_ion
379  m_cell->TimeIntegrate(inarray, outarray, time);
380 
381  // Compute I_stim
382  for (unsigned int i = 0; i < m_stimulus.size(); ++i)
383  {
384  m_stimulus[i]->Update(outarray, time);
385  }
386 
387  Array<OneD, NekDouble> ggrad0(nq), ggrad1(nq), ggrad2(nq), ggrad(nq);
388  StdRegions::ConstFactorMap factorsPoisson;
389  factorsPoisson[StdRegions::eFactorLambda] = 0.0;
390 
391  // ----------------------------
392  // Compute \nabla g_i \nabla Vm
393  // ----------------------------
394  m_fields[0]->PhysDeriv(inarray[0], ggrad0, ggrad1, ggrad2);
395  m_fields[0]->PhysDeriv(0, ggrad0, ggrad0);
396  m_fields[0]->PhysDeriv(1, ggrad1, ggrad1);
397  m_fields[0]->PhysDeriv(2, ggrad2, ggrad2);
398  if (m_session->DefinesFunction("IntracellularAnisotropicConductivity") &&
399  m_session->DefinesFunction("ExtracellularAnisotropicConductivity"))
400  {
402  1, ggrad0, 1);
404  1, ggrad1, 1);
406  1, ggrad2, 1);
407  }
408  // Add partial derivatives together
409  Vmath::Vadd(nq, ggrad0, 1, ggrad1, 1, ggrad, 1);
410  Vmath::Vadd(nq, ggrad2, 1, ggrad, 1, ggrad, 1);
411 
412  Vmath::Smul(nq, -1.0, ggrad, 1, m_fields[1]->UpdatePhys(), 1);
413 
414  // ----------------------------
415  // Solve Poisson problem for Ve
416  // ----------------------------
417  m_fields[1]->HelmSolve(m_fields[1]->GetPhys(),
418  m_fields[1]->UpdateCoeffs(), factorsPoisson, m_vardiffie);
419  m_fields[1]->BwdTrans(m_fields[1]->GetCoeffs(),
420  m_fields[1]->UpdatePhys());
421  m_fields[1]->SetPhysState(true);
422 
423  // ------------------------------
424  // Compute Laplacian of Ve (forcing term)
425  // ------------------------------
426  m_fields[1]->PhysDeriv(m_fields[1]->GetPhys(), ggrad0, ggrad1, ggrad2);
427  m_fields[1]->PhysDeriv(0, ggrad0, ggrad0);
428  m_fields[1]->PhysDeriv(1, ggrad1, ggrad1);
429  m_fields[1]->PhysDeriv(2, ggrad2, ggrad2);
430  if (m_session->DefinesFunction("IntracellularAnisotropicConductivity") &&
431  m_session->DefinesFunction("ExtracellularAnisotropicConductivity"))
432  {
434  1, ggrad0, 1);
436  1, ggrad1, 1);
438  1, ggrad2, 1);
439  }
440  // Add partial derivatives together
441  Vmath::Vadd(nq, ggrad0, 1, ggrad1, 1, ggrad, 1);
442  Vmath::Vadd(nq, ggrad2, 1, ggrad, 1, ggrad, 1);
443 
444  Vmath::Vadd(nq, ggrad, 1, outarray[0], 1, outarray[0], 1);
445 }
446 
447 
448 /**
449  *
450  */
452  bool dumpInitialConditions,
453  const int domain)
454 {
456  dumpInitialConditions,
457  domain);
458  m_cell->Initialise();
459 }
460 
461 
462 /**
463  *
464  */
466 {
468  m_cell->GenerateSummary(s);
469 }
470 
471 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
virtual ~BidomainRoth()
Desctructor.
void DoOdeRhs(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble time)
Computes the reaction terms and .
virtual void v_GenerateSummary(SummaryList &s)
Prints a summary of the model parameters.
StdRegions::VarCoeffMap m_vardiffi
Definition: BidomainRoth.h:104
std::vector< StimulusSharedPtr > m_stimulus
Definition: BidomainRoth.h:102
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.
CellModelSharedPtr m_cell
Cell model.
Definition: BidomainRoth.h:100
virtual void v_SetInitialConditions(NekDouble initialtime, bool dumpInitialConditions, const int domain)
Sets a custom initial condition.
StdRegions::VarCoeffMap m_vardiffie
Definition: BidomainRoth.h:106
virtual void v_InitObject()
Init object for UnsteadySystem class.
StdRegions::VarCoeffMap m_vardiffe
Definition: BidomainRoth.h:105
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)
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()
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 Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.cpp:322
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