Chaste Commit::43b116bb45f11066c455b15e05c77f8bbe23ac85
AbstractPdeModifier.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 "AbstractPdeModifier.hpp"
37#include "VtkMeshWriter.hpp"
38#include "ReplicatableVector.hpp"
39#include "UniformSourceEllipticPde.hpp"
40#include "AveragedSourceEllipticPde.hpp"
41#include "AveragedSourceParabolicPde.hpp"
42
43template<unsigned DIM>
45 boost::shared_ptr<AbstractBoundaryCondition<DIM> > pBoundaryCondition,
46 const bool isNeumannBoundaryCondition,
47 Vec solution)
49 mpPde(pPde),
50 mpBoundaryCondition(pBoundaryCondition),
51 mIsNeumannBoundaryCondition(isNeumannBoundaryCondition)
52{
53 if (solution)
54 {
55 mSolution = solution;
56 }
57}
58
59template<unsigned DIM>
61{
62 if (mDeleteFeMesh and mpFeMesh!=nullptr)
63 {
64 delete mpFeMesh;
65 }
66 if (mSolution)
67 {
68 PetscTools::Destroy(mSolution);
69 }
70}
71
72template<unsigned DIM>
73boost::shared_ptr<AbstractLinearPde<DIM,DIM> > AbstractPdeModifier<DIM>::GetPde()
74{
75 return mpPde;
76}
77
78template<unsigned DIM>
79boost::shared_ptr<AbstractBoundaryCondition<DIM> > AbstractPdeModifier<DIM>::GetBoundaryCondition()
80{
81 return mpBoundaryCondition;
82}
83
84template<unsigned DIM>
86{
87 return mIsNeumannBoundaryCondition;
88}
89
90template<unsigned DIM>
92{
93 mDependentVariableName = rName;
94}
95
96template<unsigned DIM>
98{
99 return mDependentVariableName;
100}
101
102template<unsigned DIM>
104{
105 return ((boost::dynamic_pointer_cast<AveragedSourceEllipticPde<DIM> >(mpPde) != nullptr) ||
106 (boost::dynamic_pointer_cast<AveragedSourceParabolicPde<DIM> >(mpPde) != nullptr)||
107 (boost::dynamic_pointer_cast<UniformSourceEllipticPde<DIM> >(mpPde) != nullptr));
108}
109
110template<unsigned DIM>
111void AbstractPdeModifier<DIM>::SetUpSourceTermsForAveragedSourcePde(TetrahedralMesh<DIM,DIM>* pMesh, std::map<CellPtr, unsigned>* pCellPdeElementMap)
112{
113 assert(HasAveragedSourcePde());
114 if (boost::dynamic_pointer_cast<AveragedSourceEllipticPde<DIM> >(mpPde) != nullptr)
115 {
116 boost::static_pointer_cast<AveragedSourceEllipticPde<DIM> >(mpPde)->SetupSourceTerms(*pMesh, pCellPdeElementMap);
117 }
118 else if (boost::dynamic_pointer_cast<AveragedSourceParabolicPde<DIM> >(mpPde) != nullptr)
119 {
120 boost::static_pointer_cast<AveragedSourceParabolicPde<DIM> >(mpPde)->SetupSourceTerms(*pMesh, pCellPdeElementMap);
121 }
122 else if (boost::dynamic_pointer_cast<UniformSourceEllipticPde<DIM> >(mpPde) != nullptr)
123 {
124 //Don't do anything as don't need to set up source terms as just constant.
125 }
126}
127
128template<unsigned DIM>
130{
131 return mSolution;
132}
133
134template<unsigned DIM>
136{
137 return mSolution;
138}
139
140template<unsigned DIM>
142{
143 return mpFeMesh;
144}
145
146template<unsigned DIM>
147void AbstractPdeModifier<DIM>::SetupSolve(AbstractCellPopulation<DIM,DIM>& rCellPopulation, std::string outputDirectory)
148{
149 // Cache the output directory
150 this->mOutputDirectory = outputDirectory;
151
152 if (mOutputSolutionAtPdeNodes)
153 {
155 {
156 OutputFileHandler output_file_handler(outputDirectory+"/", false);
157 mpVizPdeSolutionResultsFile = output_file_handler.OpenOutputFile("results.vizpdesolution");
158 }
159 }
160
161 // To make sure cell volume can be calculated
162 rCellPopulation.Update();
163}
164
165template<unsigned DIM>
167{
168 if (mOutputSolutionAtPdeNodes)
169 {
171 {
172 (*mpVizPdeSolutionResultsFile) << SimulationTime::Instance()->GetTime() << "\t";
173
174 assert(mpFeMesh != nullptr);
175 assert(!mDependentVariableName.empty());
176
177 for (unsigned i=0; i<mpFeMesh->GetNumNodes(); i++)
178 {
179 (*mpVizPdeSolutionResultsFile) << i << " ";
180 const c_vector<double,DIM>& r_location = mpFeMesh->GetNode(i)->rGetLocation();
181 for (unsigned k=0; k<DIM; k++)
182 {
183 (*mpVizPdeSolutionResultsFile) << r_location[k] << " ";
184 }
185
186 assert(mSolution != nullptr);
187 ReplicatableVector solution_repl(mSolution);
188 (*mpVizPdeSolutionResultsFile) << solution_repl[i] << " ";
189 }
190
191 (*mpVizPdeSolutionResultsFile) << "\n";
192 }
193 }
194#ifdef CHASTE_VTK
195 if (DIM > 1)
196 {
197 std::ostringstream time_string;
199 std::string results_file = "pde_results_" + mDependentVariableName + "_" + time_string.str();
200 auto* p_vtk_mesh_writer = new VtkMeshWriter<DIM,DIM>(mOutputDirectory, results_file, false);
201
202 ReplicatableVector solution_repl(mSolution);
203 std::vector<double> pde_solution;
204 for (unsigned i=0; i<mpFeMesh->GetNumNodes(); i++)
205 {
206 pde_solution.push_back(solution_repl[i]);
207 }
208
209 p_vtk_mesh_writer->AddPointData(mDependentVariableName, pde_solution);
210 p_vtk_mesh_writer->AddPointData("IsDirichletBoundaryNode", mIsDirichletBoundaryNode);
211
212 p_vtk_mesh_writer->WriteFilesUsingMesh(*mpFeMesh);
213 delete p_vtk_mesh_writer;
214 }
215#endif //CHASTE_VTK
216}
217
218template<unsigned DIM>
220{
221 if (mOutputSolutionAtPdeNodes)
222 {
224 {
225 mpVizPdeSolutionResultsFile->close();
226 }
227 }
228}
229
230template<unsigned DIM>
232{
233 return mOutputGradient;
234}
235
236template<unsigned DIM>
238{
239 mOutputGradient = outputGradient;
240}
241
242template<unsigned DIM>
244{
245 mOutputSolutionAtPdeNodes = outputSolutionAtPdeNodes;
246}
247
248template<unsigned DIM>
250{
251 // No parameters to output, so just call method on direct parent class
253}
254
255// Explicit instantiation
256template class AbstractPdeModifier<1>;
257template class AbstractPdeModifier<2>;
258template class AbstractPdeModifier<3>;
virtual void OutputSimulationModifierParameters(out_stream &rParamsFile)=0
virtual void Update(bool hasHadBirthsOrDeaths=true)=0
void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory) override
bool IsNeumannBoundaryCondition() const
void UpdateAtEndOfSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation) override
void SetOutputSolutionAtPdeNodes(bool outputSolutionAtPdeNodes)
void SetOutputGradient(bool outputGradient)
TetrahedralMesh< DIM, DIM > * GetFeMesh() const
boost::shared_ptr< AbstractLinearPde< DIM, DIM > > GetPde()
void OutputSimulationModifierParameters(out_stream &rParamsFile) override
std::string & rGetDependentVariableName()
void UpdateAtEndOfOutputTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation) override
void SetDependentVariableName(const std::string &rName)
void SetUpSourceTermsForAveragedSourcePde(TetrahedralMesh< DIM, DIM > *pMesh, std::map< CellPtr, unsigned > *pCellPdeElementMap=nullptr)
AbstractPdeModifier(boost::shared_ptr< AbstractLinearPde< DIM, DIM > > pPde=NULL, boost::shared_ptr< AbstractBoundaryCondition< DIM > > pBoundaryCondition=boost::shared_ptr< AbstractBoundaryCondition< DIM > >(), bool isNeumannBoundaryCondition=true, Vec solution=nullptr)
boost::shared_ptr< AbstractBoundaryCondition< DIM > > GetBoundaryCondition()
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
static void Destroy(Vec &rVec)
static bool AmMaster()
double GetTime() const
static SimulationTime * Instance()
unsigned GetTimeStepsElapsed() const