Chaste  Release::2017.1
EllipticGrowingDomainPdeModifier.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 "EllipticGrowingDomainPdeModifier.hpp"
37 #include "CellBasedEllipticPdeSolver.hpp"
38 #include "AveragedSourceEllipticPde.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  // If the solution at the previous timestep exists...
63  PetscInt previous_solution_size = 0;
64  if (this->mSolution)
65  {
66  VecGetSize(this->mSolution, &previous_solution_size);
67  }
68 
69  // ...then record whether it is the correct size...
70  bool is_previous_solution_size_correct = (previous_solution_size == (int)this->mpFeMesh->GetNumNodes());
71 
72  // ...and if it is, store it as an initial guess for the PDE solver
73  Vec initial_guess;
74  if (is_previous_solution_size_correct)
75  {
76  // This Vec is copied by the solver's Solve() method, so must be deleted here too
77  VecDuplicate(this->mSolution, &initial_guess);
78  VecCopy(this->mSolution, initial_guess);
80  }
81 
82  // Add the BCs to the BCs container
83  std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc = this->ConstructBoundaryConditionsContainer();
84 
85  // Use CellBasedEllipticPdeSolver as cell wise PDE
87  boost::static_pointer_cast<AbstractLinearEllipticPde<DIM,DIM> >(this->GetPde()).get(),
88  p_bcc.get());
89 
90  // If we have an initial guess, use this when solving the system...
91  if (is_previous_solution_size_correct)
92  {
93  this->mSolution = solver.Solve(initial_guess);
94  PetscTools::Destroy(initial_guess);
95  }
96  else // ...otherwise do not supply one
97  {
98  // The solver creates a Vec, so we have to keep a handle on the old one to destroy it
99  Vec old_solution_copy = this->mSolution;
100 
101  this->mSolution = solver.Solve();
102 
103  // On the first go round the vector has yet to be initialised, so we don't destroy it
104  if (old_solution_copy != nullptr)
105  {
106  PetscTools::Destroy(old_solution_copy);
107  }
108  }
109 
110  this->UpdateCellData(rCellPopulation);
111 }
112 
113 template<unsigned DIM>
115 {
116  if (boost::dynamic_pointer_cast<AveragedSourceEllipticPde<DIM> >(this->GetPde()))
117  {
118  EXCEPTION("EllipticGrowingDomainPdeModifier cannot be used with an AveragedSourceEllipticPde. Use an EllipticBoxDomainPdeModifier instead.");
119  }
120 
121  AbstractGrowingDomainPdeModifier<DIM>::SetupSolve(rCellPopulation, outputDirectory);
122 
123  // Call these methods to solve the PDE on the initial step and output the results
124  UpdateAtEndOfTimeStep(rCellPopulation);
125  this->UpdateAtEndOfOutputTimeStep(rCellPopulation);
126 }
127 
128 template<unsigned DIM>
129 std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > EllipticGrowingDomainPdeModifier<DIM>::ConstructBoundaryConditionsContainer()
130 {
131  std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc(new BoundaryConditionsContainer<DIM,DIM,1>(false));
132 
133  // To be well-defined, elliptic PDE problems on growing domains require Dirichlet boundary conditions
134  assert(!(this->IsNeumannBoundaryCondition()));
136  node_iter != this->mpFeMesh->GetBoundaryNodeIteratorEnd();
137  ++node_iter)
138  {
139  p_bcc->AddDirichletBoundaryCondition(*node_iter, this->mpBoundaryCondition.get());
140  }
141 
142  return p_bcc;
143 }
144 
145 template<unsigned DIM>
147 {
148  // No parameters to output, so just call method on direct parent class
150 }
151 
152 // Explicit instantiation
156 
157 // Serialization for Boost >= 1.36
virtual void UpdateAtEndOfOutputTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
virtual std::shared_ptr< BoundaryConditionsContainer< DIM, DIM, 1 > > ConstructBoundaryConditionsContainer()
void UpdateCellData(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
#define EXCEPTION(message)
Definition: Exception.hpp:143
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
virtual unsigned GetNumNodes() const
BoundaryNodeIterator GetBoundaryNodeIteratorBegin() const
void OutputSimulationModifierParameters(out_stream &rParamsFile)
boost::shared_ptr< AbstractBoundaryCondition< DIM > > mpBoundaryCondition
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
void OutputSimulationModifierParameters(out_stream &rParamsFile)
void GenerateFeMesh(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
virtual void UpdateAtEndOfTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
BoundaryNodeIterator GetBoundaryNodeIteratorEnd() const
EllipticGrowingDomainPdeModifier(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)
boost::shared_ptr< AbstractLinearPde< DIM, DIM > > GetPde()
TetrahedralMesh< DIM, DIM > * mpFeMesh