Chaste  Release::2018.1
EllipticBoxDomainPdeModifier.cpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 "EllipticBoxDomainPdeModifier.hpp"
37 #include "SimpleLinearEllipticSolver.hpp"
38 
39 template<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)
46  : AbstractBoxDomainPdeModifier<DIM>(pPde,
47  pBoundaryCondition,
48  isNeumannBoundaryCondition,
49  pMeshCuboid,
50  stepSize,
51  solution)
52 {
53 }
54 
55 template<unsigned DIM>
57 {
58 }
59 
60 template<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 SimpleLinearEllipticSolver as Averaged Source PDE
74  SimpleLinearEllipticSolver<DIM,DIM> solver(this->mpFeMesh,
75  boost::static_pointer_cast<AbstractLinearEllipticPde<DIM,DIM> >(this->GetPde()).get(),
76  p_bcc.get());
77 
79  Vec old_solution_copy = this->mSolution;
80  this->mSolution = solver.Solve();
81  if (old_solution_copy != nullptr)
82  {
83  PetscTools::Destroy(old_solution_copy);
84  }
85 
86  this->UpdateCellData(rCellPopulation);
87 }
88 
89 template<unsigned DIM>
90 void EllipticBoxDomainPdeModifier<DIM>::SetupSolve(AbstractCellPopulation<DIM,DIM>& rCellPopulation, std::string outputDirectory)
91 {
92  AbstractBoxDomainPdeModifier<DIM>::SetupSolve(rCellPopulation,outputDirectory);
93 
94  // Call these methods to solve the PDE on the initial step and output the results
95  UpdateAtEndOfTimeStep(rCellPopulation);
96  this->UpdateAtEndOfOutputTimeStep(rCellPopulation);
97 }
98 
99 template<unsigned DIM>
100 std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > EllipticBoxDomainPdeModifier<DIM>::ConstructBoundaryConditionsContainer(AbstractCellPopulation<DIM,DIM>& rCellPopulation)
101 {
102  std::shared_ptr<BoundaryConditionsContainer<DIM,DIM,1> > p_bcc(new BoundaryConditionsContainer<DIM,DIM,1>(false));
103 
104  // To be well-defined, elliptic PDE problems on box domains require at least some Dirichlet boundary conditions
106  assert(!(this->IsNeumannBoundaryCondition()));
107 
108  if (!this->mSetBcsOnBoxBoundary)
109  {
110  // Get the set of coarse element indices that contain cells
111  std::set<unsigned> coarse_element_indices_in_map;
112  for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = rCellPopulation.Begin();
113  cell_iter != rCellPopulation.End();
114  ++cell_iter)
115  {
116  coarse_element_indices_in_map.insert(this->mCellPdeElementMap[*cell_iter]);
117  }
118 
119  // Find the node indices associated with elements whose indices are NOT in the set coarse_element_indices_in_map
120  std::set<unsigned> coarse_mesh_boundary_node_indices;
121  for (unsigned i=0; i<this->mpFeMesh->GetNumElements(); i++)
122  {
123  if (coarse_element_indices_in_map.find(i) == coarse_element_indices_in_map.end())
124  {
125  Element<DIM,DIM>* p_element = this->mpFeMesh->GetElement(i);
126  for (unsigned j=0; j<DIM+1; j++)
127  {
128  unsigned node_index = p_element->GetNodeGlobalIndex(j);
129  coarse_mesh_boundary_node_indices.insert(node_index);
130  }
131  }
132  }
133 
134  // Apply boundary condition to the nodes in the set coarse_mesh_boundary_node_indices
135  for (std::set<unsigned>::iterator iter = coarse_mesh_boundary_node_indices.begin();
136  iter != coarse_mesh_boundary_node_indices.end();
137  ++iter)
138  {
139  p_bcc->AddDirichletBoundaryCondition(this->mpFeMesh->GetNode(*iter), this->mpBoundaryCondition.get(), 0, false);
140  }
141  }
142  else // Apply BC at boundary nodes of box domain FE mesh
143  {
144  for (typename TetrahedralMesh<DIM,DIM>::BoundaryNodeIterator node_iter = this->mpFeMesh->GetBoundaryNodeIteratorBegin();
145  node_iter != this->mpFeMesh->GetBoundaryNodeIteratorEnd();
146  ++node_iter)
147  {
148  p_bcc->AddDirichletBoundaryCondition(*node_iter, this->mpBoundaryCondition.get());
149  }
150  }
151 
152  return p_bcc;
153 }
154 
155 template<unsigned DIM>
157 {
158  // No parameters to output, so just call method on direct parent class
160 }
161 
162 // Explicit instantiation
163 template class EllipticBoxDomainPdeModifier<1>;
164 template class EllipticBoxDomainPdeModifier<2>;
165 template class EllipticBoxDomainPdeModifier<3>;
166 
167 // Serialization for Boost >= 1.36
170 
unsigned GetNodeGlobalIndex(unsigned localIndex) const
BoundaryNodeIterator GetBoundaryNodeIteratorBegin() const
void OutputSimulationModifierParameters(out_stream &rParamsFile)
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
virtual void UpdateAtEndOfTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void OutputSimulationModifierParameters(out_stream &rParamsFile)
EllipticBoxDomainPdeModifier(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 void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
virtual std::shared_ptr< BoundaryConditionsContainer< DIM, DIM, 1 > > ConstructBoundaryConditionsContainer(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)