Chaste  Release::2017.1
AbstractPdeModifier.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 "AbstractPdeModifier.hpp"
37 #include "VtkMeshWriter.hpp"
38 #include "ReplicatableVector.hpp"
39 #include "AveragedSourceEllipticPde.hpp"
40 #include "AveragedSourceParabolicPde.hpp"
41 
42 template<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 
63 template<unsigned DIM>
65 {
66  if (mDeleteFeMesh and mpFeMesh!=nullptr)
67  {
68  delete mpFeMesh;
69  }
70  if (mSolution)
71  {
73  }
74 }
75 
76 template<unsigned DIM>
77 boost::shared_ptr<AbstractLinearPde<DIM,DIM> > AbstractPdeModifier<DIM>::GetPde()
78 {
79  return mpPde;
80 }
81 
82 template<unsigned DIM>
83 boost::shared_ptr<AbstractBoundaryCondition<DIM> > AbstractPdeModifier<DIM>::GetBoundaryCondition()
84 {
85  return mpBoundaryCondition;
86 }
87 
88 template<unsigned DIM>
90 {
92 }
93 
94 template<unsigned DIM>
96 {
97  mDependentVariableName = rName;
98 }
99 
100 template<unsigned DIM>
102 {
103  return mDependentVariableName;
104 }
105 
106 template<unsigned DIM>
108 {
109  return ((boost::dynamic_pointer_cast<AveragedSourceEllipticPde<DIM> >(mpPde) != nullptr) ||
110  (boost::dynamic_pointer_cast<AveragedSourceParabolicPde<DIM> >(mpPde) != nullptr));
111 }
112 
113 template<unsigned DIM>
114 void 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 
127 template<unsigned DIM>
129 {
130  return mSolution;
131 }
132 
133 template<unsigned DIM>
135 {
136  return mSolution;
137 }
138 
139 template<unsigned DIM>
141 {
142  return mpFeMesh;
143 }
144 
145 template<unsigned DIM>
146 void AbstractPdeModifier<DIM>::SetupSolve(AbstractCellPopulation<DIM,DIM>& rCellPopulation, std::string outputDirectory)
147 {
148  // Cache the output directory
149  this->mOutputDirectory = outputDirectory;
150 
152  {
153  if (PetscTools::AmMaster())
154  {
155  OutputFileHandler output_file_handler(outputDirectory+"/", false);
156  mpVizPdeSolutionResultsFile = output_file_handler.OpenOutputFile("results.vizpdesolution");
157  }
158  }
159 }
160 
161 template<unsigned DIM>
163 {
165  {
166  if (PetscTools::AmMaster())
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;
194  time_string << SimulationTime::Instance()->GetTimeStepsElapsed();
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 
213 template<unsigned DIM>
215 {
217  {
218  if (PetscTools::AmMaster())
219  {
221  }
222  }
223 }
224 
225 template<unsigned DIM>
227 {
228  return mOutputGradient;
229 }
230 
231 template<unsigned DIM>
233 {
234  mOutputGradient = outputGradient;
235 }
236 
237 template<unsigned DIM>
238 void AbstractPdeModifier<DIM>::SetOutputSolutionAtPdeNodes(bool outputSolutionAtPdeNodes)
239 {
240  mOutputSolutionAtPdeNodes = outputSolutionAtPdeNodes;
241 }
242 
243 template<unsigned DIM>
245 {
246  // No parameters to output, so just call method on direct parent class
248 }
249 
250 // Explicit instantiation
251 template class AbstractPdeModifier<1>;
252 template class AbstractPdeModifier<2>;
253 template class AbstractPdeModifier<3>;
virtual void UpdateAtEndOfOutputTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void SetOutputGradient(bool outputGradient)
Node< SPACE_DIM > * GetNode(unsigned index) const
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
static SimulationTime * Instance()
static bool AmMaster()
Definition: PetscTools.cpp:120
virtual unsigned GetNumNodes() const
unsigned GetTimeStepsElapsed() const
out_stream mpVizPdeSolutionResultsFile
virtual void OutputSimulationModifierParameters(out_stream &rParamsFile)=0
void SetDependentVariableName(const std::string &rName)
virtual void UpdateAtEndOfSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
boost::shared_ptr< AbstractBoundaryCondition< DIM > > mpBoundaryCondition
void SetOutputSolutionAtPdeNodes(bool outputSolutionAtPdeNodes)
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)
void OutputSimulationModifierParameters(out_stream &rParamsFile)
std::string & rGetDependentVariableName()
boost::shared_ptr< AbstractBoundaryCondition< DIM > > GetBoundaryCondition()
void SetUpSourceTermsForAveragedSourcePde(TetrahedralMesh< DIM, DIM > *pMesh, std::map< CellPtr, unsigned > *pCellPdeElementMap=nullptr)
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
double GetTime() const
TetrahedralMesh< DIM, DIM > * GetFeMesh() const
void AddPointData(std::string name, std::vector< double > data)
boost::shared_ptr< AbstractLinearPde< DIM, DIM > > GetPde()
boost::shared_ptr< AbstractLinearPde< DIM, DIM > > mpPde
TetrahedralMesh< DIM, DIM > * mpFeMesh