Chaste  Release::3.4
OperatorSplittingMonodomainSolver.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "OperatorSplittingMonodomainSolver.hpp"
37 
38 
39 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
41 {
42  assert(this->mpLinearSystem->rGetLhsMatrix() != NULL);
43  assert(this->mpLinearSystem->rGetRhsVector() != NULL);
44 
46  // set up LHS matrix (and mass matrix)
48  if(computeMatrix)
49  {
50  mpMonodomainAssembler->SetMatrixToAssemble(this->mpLinearSystem->rGetLhsMatrix());
51  mpMonodomainAssembler->AssembleMatrix();
52 
53  MassMatrixAssembler<ELEMENT_DIM,SPACE_DIM> mass_matrix_assembler(this->mpMesh, HeartConfig::Instance()->GetUseMassLumping());
54  mass_matrix_assembler.SetMatrixToAssemble(mMassMatrix);
55  mass_matrix_assembler.Assemble();
56 
57  this->mpLinearSystem->FinaliseLhsMatrix();
58  PetscMatTools::Finalise(mMassMatrix);
59  }
60 
61  HeartEventHandler::BeginEvent(HeartEventHandler::ASSEMBLE_RHS);
62 
64  // Set up z in b=Mz
66  DistributedVectorFactory* p_factory = this->mpMesh->GetDistributedVectorFactory();
67  // dist stripe for the current Voltage
68  DistributedVector distributed_current_solution = p_factory->CreateDistributedVector(currentSolution);
69  // dist stripe for z (return value)
70  DistributedVector dist_vec_matrix_based = p_factory->CreateDistributedVector(mVecForConstructingRhs);
71 
73  double Cm = HeartConfig::Instance()->GetCapacitance();
74 
75  for (DistributedVector::Iterator index = dist_vec_matrix_based.Begin();
76  index!= dist_vec_matrix_based.End();
77  ++index)
78  {
79  double V = distributed_current_solution[index];
80  // in the main solver, the nodal ionic current and stimuli is computed and used.
81  // However in operator splitting, this part of the solve is diffusion only, no reaction terms
82  //double F = - Am*this->mpMonodomainTissue->rGetIionicCacheReplicated()[index.Global]
83  // - this->mpMonodomainTissue->rGetIntracellularStimulusCacheReplicated()[index.Global];
84 
85  dist_vec_matrix_based[index] = Am*Cm*V*PdeSimulationTime::GetPdeTimeStepInverse();
86  }
87  dist_vec_matrix_based.Restore();
88 
90  // b = Mz
92  MatMult(mMassMatrix, mVecForConstructingRhs, this->mpLinearSystem->rGetRhsVector());
93 
94  // assembling RHS is not finished yet, as Neumann bcs are added below, but
95  // the event will be begun again inside mpMonodomainAssembler->AssembleVector();
96  HeartEventHandler::EndEvent(HeartEventHandler::ASSEMBLE_RHS);
97 
99  // apply Neumann boundary conditions
101  mpNeumannSurfaceTermsAssembler->SetVectorToAssemble(this->mpLinearSystem->rGetRhsVector(), false/*don't zero vector!*/);
102  mpNeumannSurfaceTermsAssembler->AssembleVector();
103 
104  // finalise
105  this->mpLinearSystem->FinaliseRhsVector();
106 }
107 
108 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
110 {
111  double time = PdeSimulationTime::GetTime();
112  double dt = PdeSimulationTime::GetPdeTimeStep();
113  mpMonodomainTissue->SolveCellSystems(currentSolution, time, time+dt/2.0, true);
114 }
115 
116 
117 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
119 {
120  // solve cell models for second half timestep
121  double time = PdeSimulationTime::GetTime();
122  double dt = PdeSimulationTime::GetPdeTimeStep();
123  mpMonodomainTissue->SolveCellSystems(currentSolution, time + dt/2, PdeSimulationTime::GetNextTime(), true);
124 }
125 
126 
127 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
129 {
130  if (this->mpLinearSystem != NULL)
131  {
132  return;
133  }
134 
135  // call base class version...
137 
138  //..then do a bit extra
139  if(HeartConfig::Instance()->GetUseAbsoluteTolerance())
140  {
141  this->mpLinearSystem->SetAbsoluteTolerance(HeartConfig::Instance()->GetAbsoluteTolerance());
142  }
143  else
144  {
146  // re-implement when needed
147  //this->mpLinearSystem->SetRelativeTolerance(HeartConfig::Instance()->GetRelativeTolerance());
148  }
149 
150  this->mpLinearSystem->SetKspType(HeartConfig::Instance()->GetKSPSolver());
151  this->mpLinearSystem->SetPcType(HeartConfig::Instance()->GetKSPPreconditioner());
152  this->mpLinearSystem->SetMatrixIsSymmetric(true);
153  this->mpLinearSystem->SetUseFixedNumberIterations(HeartConfig::Instance()->GetUseFixedNumberIterationsLinearSolver(), HeartConfig::Instance()->GetEvaluateNumItsEveryNSolves());
154 
155  // initialise matrix-based RHS vector and matrix, and use the linear
156  // system rhs as a template
157  Vec& r_template = this->mpLinearSystem->rGetRhsVector();
158  VecDuplicate(r_template, &mVecForConstructingRhs);
159  PetscInt ownership_range_lo;
160  PetscInt ownership_range_hi;
161  VecGetOwnershipRange(r_template, &ownership_range_lo, &ownership_range_hi);
162  PetscInt local_size = ownership_range_hi - ownership_range_lo;
163  PetscTools::SetupMat(mMassMatrix, this->mpMesh->GetNumNodes(), this->mpMesh->GetNumNodes(),
164  this->mpMesh->CalculateMaximumNodeConnectivityPerProcess(),
165  local_size, local_size);
166 }
167 
168 
169 
170 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
175  : AbstractDynamicLinearPdeSolver<ELEMENT_DIM,SPACE_DIM,1>(pMesh),
176  mpBoundaryConditions(pBoundaryConditions),
177  mpMonodomainTissue(pTissue)
178 {
179  assert(pTissue);
180  assert(pBoundaryConditions);
181  this->mMatrixIsConstant = true;
182 
185 
186  // Tell tissue there's no need to replicate ionic caches
187  pTissue->SetCacheReplication(false);
188  mVecForConstructingRhs = NULL;
189 }
190 
191 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
193 {
194  delete mpMonodomainAssembler;
195  delete mpNeumannSurfaceTermsAssembler;
196 
197  if(mVecForConstructingRhs)
198  {
199  PetscTools::Destroy(mVecForConstructingRhs);
200  PetscTools::Destroy(mMassMatrix);
201  }
202 }
203 
204 
205 
207 // explicit instantiation
209 
215 
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpMesh
virtual void InitialiseForSolve(Vec initialSolution=NULL)
DistributedVector CreateDistributedVector(Vec vec, bool readOnly=false)
NaturalNeumannSurfaceTermAssembler< ELEMENT_DIM, SPACE_DIM, 1 > * mpNeumannSurfaceTermsAssembler
#define NEVER_REACHED
Definition: Exception.hpp:206
OperatorSplittingMonodomainSolver(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh, MonodomainTissue< ELEMENT_DIM, SPACE_DIM > *pTissue, BoundaryConditionsContainer< ELEMENT_DIM, SPACE_DIM, 1 > *pBoundaryConditions)
MonodomainTissue< ELEMENT_DIM, SPACE_DIM > * mpMonodomainTissue
void SetupLinearSystem(Vec currentSolution, bool computeMatrix)
static double GetNextTime()
static void SetupMat(Mat &rMat, int numRows, int numColumns, unsigned rowPreallocation, int numLocalRows=PETSC_DECIDE, int numLocalColumns=PETSC_DECIDE, bool ignoreOffProcEntries=true, bool newAllocationError=true)
Definition: PetscTools.cpp:268
static double GetPdeTimeStepInverse()
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:351
static double GetPdeTimeStep()
void SetCacheReplication(bool doCacheReplication)
static void Finalise(Mat matrix)
double GetCapacitance() const
double GetSurfaceAreaToVolumeRatio() const
void SetMatrixToAssemble(Mat &rMatToAssemble, bool zeroMatrixBeforeAssembly=true)
static HeartConfig * Instance()
static double GetTime()
MonodomainAssembler< ELEMENT_DIM, SPACE_DIM > * mpMonodomainAssembler