Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
ParabolicBoxDomainPdeModifier.cpp
1/*
2
3Copyright (c) 2005-2024, 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 "ParabolicBoxDomainPdeModifier.hpp"
37#include "SimpleLinearParabolicSolver.hpp"
38
39template<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)
47 pBoundaryCondition,
48 isNeumannBoundaryCondition,
49 pMeshCuboid,
50 stepSize,
51 solution)
52{
53}
54
55template<unsigned DIM>
59
60template<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.
70 this->SetUpSourceTermsForAveragedSourcePde(this->mpFeMesh, &this->mCellPdeElementMap);
71
72 // Use SimpleLinearParabolicSolver as averaged Source PDE
73 SimpleLinearParabolicSolver<DIM,DIM> solver(this->mpFeMesh,
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
95template<unsigned DIM>
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
107template<unsigned DIM>
108std::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
121 for (typename TetrahedralMesh<DIM,DIM>::BoundaryElementIterator elem_iter = this->mpFeMesh->GetBoundaryElementIteratorBegin();
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
131 for (typename TetrahedralMesh<DIM,DIM>::BoundaryNodeIterator node_iter = this->mpFeMesh->GetBoundaryNodeIteratorBegin();
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
143template<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
164template<unsigned DIM>
166{
167 // No parameters to output, so just call method on direct parent class
169}
170
171// Explicit instantiation
175
176// Serialization for Boost >= 1.36
179
#define EXCEPTION(message)
#define UNUSED_OPT(var)
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
void OutputSimulationModifierParameters(out_stream &rParamsFile)
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
void SetTimes(double tStart, double tEnd)
std::vector< Node< SPACE_DIM > * >::const_iterator BoundaryNodeIterator
std::vector< BoundaryElement< ELEMENT_DIM-1, SPACE_DIM > * >::const_iterator BoundaryElementIterator
void SetupInitialSolutionVector(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
virtual std::shared_ptr< BoundaryConditionsContainer< DIM, DIM, 1 > > ConstructBoundaryConditionsContainer(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
virtual void UpdateAtEndOfTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void OutputSimulationModifierParameters(out_stream &rParamsFile)
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)
static Vec CreateAndSetVec(int size, double value)
static void Destroy(Vec &rVec)
double GetTime() const
double GetTimeStep() const
static SimulationTime * Instance()