Chaste  Release::2017.1
ParabolicBoxDomainPdeModifier.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 "ParabolicBoxDomainPdeModifier.hpp"
37 #include "SimpleLinearParabolicSolver.hpp"
38 
39 template<unsigned DIM>
41  boost::shared_ptr<AbstractBoundaryCondition<DIM> > pBoundaryCondition,
42  bool isNeumannBoundaryCondition,
43  boost::shared_ptr<ChasteCuboid<DIM> > pMeshCuboid,
44  double stepSize,
45  Vec solution)
46  : AbstractBoxDomainPdeModifier<DIM>(pPde,
47  pBoundaryCondition,
48  isNeumannBoundaryCondition,
49  pMeshCuboid,
50  stepSize,
51  solution)
52 {
53 }
54 
55 template<unsigned DIM>
57 {
58 }
59 
60 template<unsigned DIM>
62 {
63  // Set up boundary conditions
64  std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc = ConstructBoundaryConditionsContainer(rCellPopulation);
65 
66  this->UpdateCellPdeElementMap(rCellPopulation);
67 
68  // When using a PDE mesh which doesn't coincide with the cells, we must set up the source terms before solving the PDE.
69  // Pass in already updated CellPdeElementMap to speed up finding cells.
71 
72  // Use SimpleLinearParabolicSolver as averaged Source PDE
74  boost::static_pointer_cast<AbstractLinearParabolicPde<DIM,DIM> >(this->GetPde()).get(),
75  p_bcc.get());
76 
78  SimulationTime* p_simulation_time = SimulationTime::Instance();
79  double current_time = p_simulation_time->GetTime();
80  double dt = p_simulation_time->GetTimeStep();
81  solver.SetTimes(current_time,current_time + dt);
82  solver.SetTimeStep(dt);
83 
84  // Use previous solution as the initial condition
85  Vec previous_solution = this->mSolution;
86  solver.SetInitialCondition(previous_solution);
87 
88  // Note that the linear solver creates a vector, so we have to keep a handle on the old one
89  // in order to destroy it
90  this->mSolution = solver.Solve();
91  PetscTools::Destroy(previous_solution);
92  this->UpdateCellData(rCellPopulation);
93 }
94 
95 template<unsigned DIM>
96 void ParabolicBoxDomainPdeModifier<DIM>::SetupSolve(AbstractCellPopulation<DIM,DIM>& rCellPopulation, std::string outputDirectory)
97 {
98  AbstractBoxDomainPdeModifier<DIM>::SetupSolve(rCellPopulation,outputDirectory);
99 
100  // Copy the cell data to mSolution (this is the initial condition)
101  SetupInitialSolutionVector(rCellPopulation);
102 
103  // Output the initial conditions on FeMesh
104  this->UpdateAtEndOfOutputTimeStep(rCellPopulation);
105 }
106 
107 template<unsigned DIM>
108 std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > ParabolicBoxDomainPdeModifier<DIM>::ConstructBoundaryConditionsContainer(AbstractCellPopulation<DIM,DIM>& rCellPopulation)
109 {
110  std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc(new BoundaryConditionsContainer<DIM,DIM,1>(false));
111 
112  if (!this->mSetBcsOnBoxBoundary)
113  {
114  EXCEPTION("Boundary conditions cannot yet be set on the cell population boundary for a ParabolicBoxDomainPdeModifier");
115  }
116  else // Apply BC at boundary nodes of box domain FE mesh
117  {
118  if (this->IsNeumannBoundaryCondition())
119  {
120  // Impose any Neumann boundary conditions
122  elem_iter != this->mpFeMesh->GetBoundaryElementIteratorEnd();
123  ++elem_iter)
124  {
125  p_bcc->AddNeumannBoundaryCondition(*elem_iter, this->mpBoundaryCondition.get());
126  }
127  }
128  else
129  {
130  // Impose any Dirichlet boundary conditions
132  node_iter != this->mpFeMesh->GetBoundaryNodeIteratorEnd();
133  ++node_iter)
134  {
135  p_bcc->AddDirichletBoundaryCondition(*node_iter, this->mpBoundaryCondition.get());
136  }
137  }
138  }
139 
140  return p_bcc;
141 }
142 
143 template<unsigned DIM>
145 {
146  // Specify homogeneous initial conditions based upon the values stored in CellData.
147  // Note need all the CellDataValues to be the same.
148 
149  double initial_condition = rCellPopulation.Begin()->GetCellData()->GetItem(this->mDependentVariableName);
150 
151  for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = rCellPopulation.Begin();
152  cell_iter != rCellPopulation.End();
153  ++cell_iter)
154  {
155  double initial_condition_at_cell = cell_iter->GetCellData()->GetItem(this->mDependentVariableName);
156  UNUSED_OPT(initial_condition_at_cell);
157  assert(fabs(initial_condition_at_cell - initial_condition)<1e-12);
158  }
159 
160  // Initialise mSolution
161  this->mSolution = PetscTools::CreateAndSetVec(this->mpFeMesh->GetNumNodes(), initial_condition);
162 }
163 
164 template<unsigned DIM>
166 {
167  // No parameters to output, so just call method on direct parent class
169 }
170 
171 // Explicit instantiation
172 template class ParabolicBoxDomainPdeModifier<1>;
173 template class ParabolicBoxDomainPdeModifier<2>;
174 template class ParabolicBoxDomainPdeModifier<3>;
175 
176 // Serialization for Boost >= 1.36
179 
virtual void UpdateAtEndOfOutputTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
ParabolicBoxDomainPdeModifier(boost::shared_ptr< AbstractLinearPde< DIM, DIM > > pPde=boost::shared_ptr< AbstractLinearPde< DIM, DIM > >(), boost::shared_ptr< AbstractBoundaryCondition< DIM > > pBoundaryCondition=boost::shared_ptr< AbstractBoundaryCondition< DIM > >(), bool isNeumannBoundaryCondition=true, boost::shared_ptr< ChasteCuboid< DIM > > pMeshCuboid=boost::shared_ptr< ChasteCuboid< DIM > >(), double stepSize=1.0, Vec solution=nullptr)
BoundaryElementIterator GetBoundaryElementIteratorBegin() const
virtual void UpdateAtEndOfTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void SetupInitialSolutionVector(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void UpdateCellPdeElementMap(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
#define EXCEPTION(message)
Definition: Exception.hpp:143
static SimulationTime * Instance()
virtual unsigned GetNumNodes() const
void OutputSimulationModifierParameters(out_stream &rParamsFile)
BoundaryNodeIterator GetBoundaryNodeIteratorBegin() const
void OutputSimulationModifierParameters(out_stream &rParamsFile)
boost::shared_ptr< AbstractBoundaryCondition< DIM > > mpBoundaryCondition
std::map< CellPtr, unsigned > mCellPdeElementMap
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
static Vec CreateAndSetVec(int size, double value)
Definition: PetscTools.cpp:254
void SetUpSourceTermsForAveragedSourcePde(TetrahedralMesh< DIM, DIM > *pMesh, std::map< CellPtr, unsigned > *pCellPdeElementMap=nullptr)
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
#define UNUSED_OPT(var)
Definition: Exception.hpp:216
BoundaryNodeIterator GetBoundaryNodeIteratorEnd() const
virtual std::shared_ptr< BoundaryConditionsContainer< DIM, DIM, 1 > > ConstructBoundaryConditionsContainer(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void UpdateCellData(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
boost::shared_ptr< AbstractLinearPde< DIM, DIM > > GetPde()
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
BoundaryElementIterator GetBoundaryElementIteratorEnd() const
TetrahedralMesh< DIM, DIM > * mpFeMesh