Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
AbstractPdeModifier.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 "AbstractPdeModifier.hpp"
37#include "VtkMeshWriter.hpp"
38#include "ReplicatableVector.hpp"
39#include "AveragedSourceEllipticPde.hpp"
40#include "AveragedSourceParabolicPde.hpp"
41
42template<unsigned DIM>
44 boost::shared_ptr<AbstractBoundaryCondition<DIM> > pBoundaryCondition,
45 bool isNeumannBoundaryCondition,
46 Vec solution)
48 mpPde(pPde),
49 mpBoundaryCondition(pBoundaryCondition),
50 mIsNeumannBoundaryCondition(isNeumannBoundaryCondition),
51 mSolution(nullptr),
52 mOutputDirectory(""),
53 mOutputGradient(false),
54 mOutputSolutionAtPdeNodes(false),
55 mDeleteFeMesh(false)
56{
57 if (solution)
58 {
59 mSolution = solution;
60 }
61}
62
63template<unsigned DIM>
65{
66 if (mDeleteFeMesh and mpFeMesh!=nullptr)
67 {
68 delete mpFeMesh;
69 }
70 if (mSolution)
71 {
72 PetscTools::Destroy(mSolution);
73 }
74}
75
76template<unsigned DIM>
77boost::shared_ptr<AbstractLinearPde<DIM,DIM> > AbstractPdeModifier<DIM>::GetPde()
78{
79 return mpPde;
80}
81
82template<unsigned DIM>
83boost::shared_ptr<AbstractBoundaryCondition<DIM> > AbstractPdeModifier<DIM>::GetBoundaryCondition()
84{
85 return mpBoundaryCondition;
86}
87
88template<unsigned DIM>
90{
91 return mIsNeumannBoundaryCondition;
92}
93
94template<unsigned DIM>
96{
97 mDependentVariableName = rName;
98}
99
100template<unsigned DIM>
102{
103 return mDependentVariableName;
104}
105
106template<unsigned DIM>
108{
109 return ((boost::dynamic_pointer_cast<AveragedSourceEllipticPde<DIM> >(mpPde) != nullptr) ||
110 (boost::dynamic_pointer_cast<AveragedSourceParabolicPde<DIM> >(mpPde) != nullptr));
111}
112
113template<unsigned DIM>
114void AbstractPdeModifier<DIM>::SetUpSourceTermsForAveragedSourcePde(TetrahedralMesh<DIM,DIM>* pMesh, std::map<CellPtr, unsigned>* pCellPdeElementMap)
115{
116 assert(HasAveragedSourcePde());
117 if (boost::dynamic_pointer_cast<AveragedSourceEllipticPde<DIM> >(mpPde) != nullptr)
118 {
119 boost::static_pointer_cast<AveragedSourceEllipticPde<DIM> >(mpPde)->SetupSourceTerms(*pMesh, pCellPdeElementMap);
120 }
121 else if (boost::dynamic_pointer_cast<AveragedSourceParabolicPde<DIM> >(mpPde) != nullptr)
122 {
123 boost::static_pointer_cast<AveragedSourceParabolicPde<DIM> >(mpPde)->SetupSourceTerms(*pMesh, pCellPdeElementMap);
124 }
125}
126
127template<unsigned DIM>
129{
130 return mSolution;
131}
132
133template<unsigned DIM>
135{
136 return mSolution;
137}
138
139template<unsigned DIM>
141{
142 return mpFeMesh;
143}
144
145template<unsigned DIM>
146void AbstractPdeModifier<DIM>::SetupSolve(AbstractCellPopulation<DIM,DIM>& rCellPopulation, std::string outputDirectory)
147{
148 // Cache the output directory
149 this->mOutputDirectory = outputDirectory;
150
151 if (mOutputSolutionAtPdeNodes)
152 {
154 {
155 OutputFileHandler output_file_handler(outputDirectory+"/", false);
156 mpVizPdeSolutionResultsFile = output_file_handler.OpenOutputFile("results.vizpdesolution");
157 }
158 }
159}
160
161template<unsigned DIM>
163{
164 if (mOutputSolutionAtPdeNodes)
165 {
167 {
168 (*mpVizPdeSolutionResultsFile) << SimulationTime::Instance()->GetTime() << "\t";
169
170 assert(mpFeMesh != nullptr);
171 assert(mDependentVariableName != "");
172
173 for (unsigned i=0; i<mpFeMesh->GetNumNodes(); i++)
174 {
175 (*mpVizPdeSolutionResultsFile) << i << " ";
176 const c_vector<double,DIM>& r_location = mpFeMesh->GetNode(i)->rGetLocation();
177 for (unsigned k=0; k<DIM; k++)
178 {
179 (*mpVizPdeSolutionResultsFile) << r_location[k] << " ";
180 }
181
182 assert(mSolution != nullptr);
183 ReplicatableVector solution_repl(mSolution);
184 (*mpVizPdeSolutionResultsFile) << solution_repl[i] << " ";
185 }
186
187 (*mpVizPdeSolutionResultsFile) << "\n";
188 }
189 }
190#ifdef CHASTE_VTK
191 if (DIM > 1)
192 {
193 std::ostringstream time_string;
195 std::string results_file = "pde_results_" + mDependentVariableName + "_" + time_string.str();
196 VtkMeshWriter<DIM,DIM>* p_vtk_mesh_writer = new VtkMeshWriter<DIM,DIM>(mOutputDirectory, results_file, false);
197
198 ReplicatableVector solution_repl(mSolution);
199 std::vector<double> pde_solution;
200 for (unsigned i=0; i<mpFeMesh->GetNumNodes(); i++)
201 {
202 pde_solution.push_back(solution_repl[i]);
203 }
204
205 p_vtk_mesh_writer->AddPointData(mDependentVariableName, pde_solution);
206
207 p_vtk_mesh_writer->WriteFilesUsingMesh(*mpFeMesh);
208 delete p_vtk_mesh_writer;
209 }
210#endif //CHASTE_VTK
211}
212
213template<unsigned DIM>
215{
216 if (mOutputSolutionAtPdeNodes)
217 {
219 {
220 mpVizPdeSolutionResultsFile->close();
221 }
222 }
223}
224
225template<unsigned DIM>
227{
228 return mOutputGradient;
229}
230
231template<unsigned DIM>
233{
234 mOutputGradient = outputGradient;
235}
236
237template<unsigned DIM>
239{
240 mOutputSolutionAtPdeNodes = outputSolutionAtPdeNodes;
241}
242
243template<unsigned DIM>
245{
246 // No parameters to output, so just call method on direct parent class
248}
249
250// Explicit instantiation
251template class AbstractPdeModifier<1>;
252template class AbstractPdeModifier<2>;
253template class AbstractPdeModifier<3>;
virtual void OutputSimulationModifierParameters(out_stream &rParamsFile)=0
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
virtual void UpdateAtEndOfOutputTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void OutputSimulationModifierParameters(out_stream &rParamsFile)
void SetOutputSolutionAtPdeNodes(bool outputSolutionAtPdeNodes)
void SetOutputGradient(bool outputGradient)
TetrahedralMesh< DIM, DIM > * GetFeMesh() const
boost::shared_ptr< AbstractLinearPde< DIM, DIM > > GetPde()
virtual void UpdateAtEndOfSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
std::string & rGetDependentVariableName()
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
void AddPointData(std::string name, std::vector< double > data)
void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)