Chaste Commit::43b116bb45f11066c455b15e05c77f8bbe23ac85
AbstractGrowingDomainPdeModifier.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 "AbstractGrowingDomainPdeModifier.hpp"
37#include "VertexBasedCellPopulation.hpp"
38#include "MeshBasedCellPopulation.hpp"
39#include "CaBasedCellPopulation.hpp"
40#include "NodeBasedCellPopulation.hpp"
41#include "ReplicatableVector.hpp"
42#include "LinearBasisFunction.hpp"
43
44template <unsigned DIM>
46 boost::shared_ptr<AbstractBoundaryCondition<DIM> > pBoundaryCondition,
47 bool isNeumannBoundaryCondition,
48 Vec solution)
49 : AbstractPdeModifier<DIM>(pPde,
50 pBoundaryCondition,
51 isNeumannBoundaryCondition,
52 solution)
53{
54}
55
56template<unsigned DIM>
58{
59 if (this->mDeleteFeMesh)
60 {
61 // If a mesh has been created on a previous time step then we need to tidy it up
62 assert(this->mpFeMesh != nullptr);
63 delete this->mpFeMesh;
64 }
65 else
66 {
68 // This placement assumes that if this->mDeleteFeMesh is false it is uninitialised and needs to
69 // be checked. If true, it has been checked elsewhere.
70 this->mDeleteFeMesh = (dynamic_cast<MeshBasedCellPopulation<DIM>*>(&rCellPopulation) == nullptr);
71 }
72
73 // Get the finite element mesh via the cell population. Set to NULL first in case mesh generation fails.
74 this->mpFeMesh = nullptr;
75 this->mpFeMesh = rCellPopulation.GetTetrahedralMeshForPdeModifier();
76
77 // initialise Dirichlet boundary node tracking
78 this->mIsDirichletBoundaryNode = std::vector<double>(this->mpFeMesh->GetNumNodes(), 0.0);
79}
80
81template<unsigned DIM>
83{
84 // Store the PDE solution in an accessible form
85 ReplicatableVector solution_repl(this->mSolution);
86
87 // Local cell index used by the CA simulation
88 unsigned cell_index = 0;
89
90 unsigned index_in_solution_repl = 0;
91 for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = rCellPopulation.Begin();
92 cell_iter != rCellPopulation.End();
93 ++cell_iter)
94 {
95 unsigned tet_node_index = rCellPopulation.GetLocationIndexUsingCell(*cell_iter);
96
98 if (dynamic_cast<VertexBasedCellPopulation<DIM>*>(&rCellPopulation) != nullptr)
99 {
100 // Offset to relate elements in vertex mesh to nodes in tetrahedral mesh
101 tet_node_index += rCellPopulation.GetNumNodes();
102 }
103 else if (dynamic_cast<CaBasedCellPopulation<DIM>*>(&rCellPopulation) != nullptr)
104 {
105 // Here local cell index corresponds to tet node
106 tet_node_index = cell_index;
107 cell_index++;
108 }
109 else if (dynamic_cast<NodeBasedCellPopulation<DIM>*>(&rCellPopulation) != nullptr)
110 {
111 tet_node_index = index_in_solution_repl;
112 index_in_solution_repl++;
113 }
114
115 double solution_at_node = solution_repl[tet_node_index];
116
117 cell_iter->GetCellData()->SetItem(this->mDependentVariableName, solution_at_node);
118
119 if (this->mOutputGradient)
120 {
121 // Now calculate the gradient of the solution and store this in CellVecData
122 c_vector<double, DIM> solution_gradient = zero_vector<double>(DIM);
123
124 Node<DIM>* p_tet_node = this->mpFeMesh->GetNode(tet_node_index);
125
126 // Get the containing elements and average the contribution from each one
127 for (typename Node<DIM>::ContainingElementIterator element_iter = p_tet_node->ContainingElementsBegin();
128 element_iter != p_tet_node->ContainingElementsEnd();
129 ++element_iter)
130 {
131 // Calculate the basis functions at any point (e.g. zero) in the element
132 c_matrix<double, DIM, DIM> jacobian, inverse_jacobian;
133 double jacobian_det;
134 this->mpFeMesh->GetInverseJacobianForElement(*element_iter, jacobian, jacobian_det, inverse_jacobian);
135 const ChastePoint<DIM> zero_point;
136 c_matrix<double, DIM, DIM+1> grad_phi;
137 LinearBasisFunction<DIM>::ComputeTransformedBasisFunctionDerivatives(zero_point, inverse_jacobian, grad_phi);
138
139 // Add the contribution from this element
140 for (unsigned node_index=0; node_index<DIM+1; node_index++)
141 {
142 double nodal_value = solution_repl[this->mpFeMesh->GetElement(*element_iter)->GetNodeGlobalIndex(node_index)];
143
144 for (unsigned j=0; j<DIM; j++)
145 {
146 solution_gradient(j) += nodal_value* grad_phi(j, node_index);
147 }
148 }
149 }
150
151 // Divide by number of containing elements
152 solution_gradient /= p_tet_node->GetNumContainingElements();
153
154 switch (DIM)
155 {
156 case 1:
157 cell_iter->GetCellData()->SetItem(this->mDependentVariableName+"_grad_x", solution_gradient(0));
158 break;
159 case 2:
160 cell_iter->GetCellData()->SetItem(this->mDependentVariableName+"_grad_x", solution_gradient(0));
161 cell_iter->GetCellData()->SetItem(this->mDependentVariableName+"_grad_y", solution_gradient(1));
162 break;
163 case 3:
164 cell_iter->GetCellData()->SetItem(this->mDependentVariableName+"_grad_x", solution_gradient(0));
165 cell_iter->GetCellData()->SetItem(this->mDependentVariableName+"_grad_y", solution_gradient(1));
166 cell_iter->GetCellData()->SetItem(this->mDependentVariableName+"_grad_z", solution_gradient(2));
167 break;
168 default:
170 }
171 }
172 }
173}
174
175template<unsigned DIM>
177{
178 // No parameters to output, so just call method on direct parent class
180}
181
182// Explicit instantiation
#define NEVER_REACHED
unsigned GetLocationIndexUsingCell(CellPtr pCell)
virtual TetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * GetTetrahedralMeshForPdeModifier()=0
virtual unsigned GetNumNodes()=0
void GenerateFeMesh(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
AbstractGrowingDomainPdeModifier(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, Vec solution=nullptr)
void UpdateCellData(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
void OutputSimulationModifierParameters(out_stream &rParamsFile) override
void OutputSimulationModifierParameters(out_stream &rParamsFile) override
static void ComputeTransformedBasisFunctionDerivatives(const ChastePoint< ELEMENT_DIM > &rPoint, const c_matrix< double, ELEMENT_DIM, ELEMENT_DIM > &rInverseJacobian, c_matrix< double, ELEMENT_DIM, ELEMENT_DIM+1 > &rReturnValue)
Definition Node.hpp:59
ContainingElementIterator ContainingElementsEnd() const
Definition Node.hpp:493
unsigned GetNumContainingElements() const
Definition Node.cpp:312
ContainingElementIterator ContainingElementsBegin() const
Definition Node.hpp:485