Chaste Commit::43b116bb45f11066c455b15e05c77f8bbe23ac85
EllipticGrowingDomainPdeModifier.cpp
1/*
2
3Copyright (c) 2005-2025, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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
40template<unsigned DIM>
42 boost::shared_ptr<AbstractBoundaryCondition<DIM> > pBoundaryCondition,
43 bool isNeumannBoundaryCondition,
44 Vec solution)
46 pBoundaryCondition,
47 isNeumannBoundaryCondition,
48 solution)
49{
50}
51
52template<unsigned DIM>
54{
55 this->GenerateFeMesh(rCellPopulation);
56
57 // If the solution at the previous timestep exists...
58 PetscInt previous_solution_size = 0;
59 if (this->mSolution)
60 {
61 VecGetSize(this->mSolution, &previous_solution_size);
62 }
63
64 // ...then record whether it is the correct size...
65 const bool is_previous_solution_size_correct = (previous_solution_size == static_cast<int>(this->mpFeMesh->GetNumNodes()));
66
67 // ...and if it is, store it as an initial guess for the PDE solver
68 Vec initial_guess;
69 if (is_previous_solution_size_correct)
70 {
71 // This Vec is copied by the solver's Solve() method, so must be deleted here too
72 VecDuplicate(this->mSolution, &initial_guess);
73 VecCopy(this->mSolution, initial_guess);
74 PetscTools::Destroy(this->mSolution);
75 }
76
77 // Add the BCs to the BCs container
78 std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc = this->ConstructBoundaryConditionsContainer();
79
80 // Use CellBasedEllipticPdeSolver as cell wise PDE
81 CellBasedEllipticPdeSolver<DIM> solver(this->mpFeMesh,
82 boost::static_pointer_cast<AbstractLinearEllipticPde<DIM,DIM> >(this->GetPde()).get(),
83 p_bcc.get());
84
85 // If we have an initial guess, use this when solving the system...
86 if (is_previous_solution_size_correct)
87 {
88 this->mSolution = solver.Solve(initial_guess);
89 PetscTools::Destroy(initial_guess);
90 }
91 else // ...otherwise do not supply one
92 {
93 // The solver creates a Vec, so we have to keep a handle on the old one to destroy it
94 Vec old_solution_copy = this->mSolution;
95
96 this->mSolution = solver.Solve();
97
98 // On the first go round the vector has yet to be initialised, so we don't destroy it
99 if (old_solution_copy != nullptr)
100 {
101 PetscTools::Destroy(old_solution_copy);
102 }
103 }
104
105 this->UpdateCellData(rCellPopulation);
106}
107
108template<unsigned DIM>
110{
111 if (boost::dynamic_pointer_cast<AveragedSourceEllipticPde<DIM> >(this->GetPde()))
112 {
113 EXCEPTION("EllipticGrowingDomainPdeModifier cannot be used with an AveragedSourceEllipticPde. Use an EllipticBoxDomainPdeModifier instead.");
114 }
115
116 AbstractGrowingDomainPdeModifier<DIM>::SetupSolve(rCellPopulation, outputDirectory);
117
118 // Call these methods to solve the PDE on the initial step and output the results
119 UpdateAtEndOfTimeStep(rCellPopulation);
120 this->UpdateAtEndOfOutputTimeStep(rCellPopulation);
121}
122
123template<unsigned DIM>
124std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > EllipticGrowingDomainPdeModifier<DIM>::ConstructBoundaryConditionsContainer()
125{
126 std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc(new BoundaryConditionsContainer<DIM,DIM,1>(false));
127
128 // To be well-defined, elliptic PDE problems on growing domains require Dirichlet boundary conditions
129 assert(!(this->IsNeumannBoundaryCondition()));
130 for (auto node_iter = this->mpFeMesh->GetBoundaryNodeIteratorBegin();
131 node_iter != this->mpFeMesh->GetBoundaryNodeIteratorEnd();
132 ++node_iter)
133 {
134 p_bcc->AddDirichletBoundaryCondition(*node_iter, this->mpBoundaryCondition.get());
135 this->mIsDirichletBoundaryNode[(*node_iter)->GetIndex()] = 1.0;
136 }
137
138 return p_bcc;
139}
140
141template<unsigned DIM>
143{
144 // No parameters to output, so just call method on direct parent class
146}
147
148// Explicit instantiation
152
153// Serialization for Boost >= 1.36
#define EXCEPTION(message)
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
void OutputSimulationModifierParameters(out_stream &rParamsFile) override
void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory) override
void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory) override
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)
void OutputSimulationModifierParameters(out_stream &rParamsFile) override
void UpdateAtEndOfTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation) override
virtual std::shared_ptr< BoundaryConditionsContainer< DIM, DIM, 1 > > ConstructBoundaryConditionsContainer()
static void Destroy(Vec &rVec)