Chaste  Release::2018.1
ParabolicGrowingDomainPdeModifier.cpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 "ParabolicGrowingDomainPdeModifier.hpp"
37 #include "CellBasedParabolicPdeSolver.hpp"
38 #include "AveragedSourceParabolicPde.hpp"
39 
40 template<unsigned DIM>
42  boost::shared_ptr<AbstractBoundaryCondition<DIM> > pBoundaryCondition,
43  bool isNeumannBoundaryCondition,
44  Vec solution)
46  pBoundaryCondition,
47  isNeumannBoundaryCondition,
48  solution)
49 {
50 }
51 
52 template<unsigned DIM>
54 {
55 }
56 
57 template<unsigned DIM>
59 {
60  this->GenerateFeMesh(rCellPopulation);
61 
62  // Set up boundary conditions
63  std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc = ConstructBoundaryConditionsContainer();
64 
65  // Construct the solution vector from cell data (takes care of cells dividing);
66  UpdateSolutionVector(rCellPopulation);
67 
68  // Use CellBasedParabolicPdeSolver as cell wise PDE
69  CellBasedParabolicPdeSolver<DIM> solver(this->mpFeMesh,
70  boost::static_pointer_cast<AbstractLinearParabolicPde<DIM,DIM> >(this->mpPde).get(),
71  p_bcc.get());
72 
74  SimulationTime* p_simulation_time = SimulationTime::Instance();
75  double current_time = p_simulation_time->GetTime();
76  double dt = p_simulation_time->GetTimeStep();
77  solver.SetTimes(current_time,current_time + dt);
78  solver.SetTimeStep(dt);
79 
80  // Use previous solution as the initial condition
81  Vec previous_solution = this->mSolution;
82  solver.SetInitialCondition(previous_solution);
83 
84  // Note that the linear solver creates a vector, so we have to keep a handle on the old one
85  // in order to destroy it
86  this->mSolution = solver.Solve();
87  PetscTools::Destroy(previous_solution);
88  this->UpdateCellData(rCellPopulation);
89 }
90 
91 template<unsigned DIM>
93 {
94  AbstractGrowingDomainPdeModifier<DIM>::SetupSolve(rCellPopulation, outputDirectory);
95 
96  if (boost::dynamic_pointer_cast<AveragedSourceParabolicPde<DIM> >(this->mpPde))
97  {
98  EXCEPTION("ParabolicGrowingDomainPdeModifier cannot be used with an AveragedSourceParabolicPde. Use a ParabolicBoxDomainPdeModifier instead.");
99  }
100 
101  // Setup a finite element mesh on which to save the initial condition
102  this->GenerateFeMesh(rCellPopulation);
103 
104  // Copy the cell data to mSolution (this is the initial condition)
105  UpdateSolutionVector(rCellPopulation);
106 
107  // Output the initial conditions on FeMesh
108  this->UpdateAtEndOfOutputTimeStep(rCellPopulation);
109 }
110 
111 template<unsigned DIM>
112 std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > ParabolicGrowingDomainPdeModifier<DIM>::ConstructBoundaryConditionsContainer()
113 {
114  std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc(new BoundaryConditionsContainer<DIM,DIM,1>(false));
115 
116  if (this->IsNeumannBoundaryCondition())
117  {
118  // Impose any Neumann boundary conditions
120  elem_iter != this->mpFeMesh->GetBoundaryElementIteratorEnd();
121  ++elem_iter)
122  {
123  p_bcc->AddNeumannBoundaryCondition(*elem_iter, this->mpBoundaryCondition.get());
124  }
125  }
126  else
127  {
128  // Impose any Dirichlet boundary conditions
129  for (typename TetrahedralMesh<DIM,DIM>::BoundaryNodeIterator node_iter = this->mpFeMesh->GetBoundaryNodeIteratorBegin();
130  node_iter != this->mpFeMesh->GetBoundaryNodeIteratorEnd();
131  ++node_iter)
132  {
133  p_bcc->AddDirichletBoundaryCondition(*node_iter, this->mpBoundaryCondition.get());
134  }
135  }
136 
137  return p_bcc;
138 }
139 
140 template<unsigned DIM>
142 {
143  // Clear (if it's not the first time) and resize the solution vector
144  if (this->mSolution)
145  {
146  PetscTools::Destroy(this->mSolution);
147  }
148  this->mSolution = PetscTools::CreateAndSetVec(this->mpFeMesh->GetNumNodes(), 0.0);
149 
150  std::string& variable_name = this->mDependentVariableName;
151 
152  for (typename TetrahedralMesh<DIM,DIM>::NodeIterator node_iter = this->mpFeMesh->GetNodeIteratorBegin();
153  node_iter != this->mpFeMesh->GetNodeIteratorEnd();
154  ++node_iter)
155  {
156  // Loop over nodes of the finite element mesh and get appropriate solution values from CellData
157  for (typename TetrahedralMesh<DIM,DIM>::NodeIterator node_iter = this->mpFeMesh->GetNodeIteratorBegin();
158  node_iter != this->mpFeMesh->GetNodeIteratorEnd();
159  ++node_iter)
160  {
161  unsigned node_index = node_iter->GetIndex();
162  bool dirichlet_bc_applies = (node_iter->IsBoundaryNode()) && (!(this->IsNeumannBoundaryCondition()));
163  double boundary_value = this->GetBoundaryCondition()->GetValue(node_iter->rGetLocation());
164 
165  double solution_at_node = rCellPopulation.GetCellDataItemAtPdeNode(node_index, variable_name, dirichlet_bc_applies, boundary_value);
166 
167  PetscVecTools::SetElement(this->mSolution, node_index, solution_at_node);
168  }
169  }
170 }
171 
172 template<unsigned DIM>
174 {
175  // No parameters to output, so just call method on direct parent class
177 }
178 
179 // Explicit instantiation
183 
184 // Serialization for Boost >= 1.36
187 
void OutputSimulationModifierParameters(out_stream &rParamsFile)
BoundaryElementIterator GetBoundaryElementIteratorBegin() const
void UpdateSolutionVector(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
#define EXCEPTION(message)
Definition: Exception.hpp:143
static void SetElement(Vec vector, PetscInt row, double value)
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
static SimulationTime * Instance()
virtual void UpdateAtEndOfTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
ParabolicGrowingDomainPdeModifier(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, Vec solution=nullptr)
virtual std::shared_ptr< BoundaryConditionsContainer< DIM, DIM, 1 > > ConstructBoundaryConditionsContainer()
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
BoundaryNodeIterator GetBoundaryNodeIteratorBegin() const
void OutputSimulationModifierParameters(out_stream &rParamsFile)
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
virtual double GetCellDataItemAtPdeNode(unsigned pdeNodeIndex, std::string &rVariableName, bool dirichletBoundaryConditionApplies=false, double dirichletBoundaryValue=0.0)=0
static Vec CreateAndSetVec(int size, double value)
Definition: PetscTools.cpp:254
NodeIterator GetNodeIteratorBegin(bool skipDeletedNodes=true)
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352