Chaste Commit::a9c8bf7350f67d7cf086e6fe3cf5461521554546
PlaneBoundaryCondition.cpp
1/*
2
3Copyright (c) 2005-2026, 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 "PlaneBoundaryCondition.hpp"
37#include "AbstractCentreBasedCellPopulation.hpp"
38#include "VertexBasedCellPopulation.hpp"
39
40template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
42 c_vector<double, SPACE_DIM> point,
43 c_vector<double, SPACE_DIM> normal)
44 : AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM>(pCellPopulation),
45 mPointOnPlane(point),
46 mUseJiggledNodesOnPlane(false)
47{
49 {
50 EXCEPTION("PlaneBoundaryCondition requires a subclass of AbstractOffLatticeCellPopulation.");
51 }
52
53 assert(norm_2(normal) > 0.0);
54 mNormalToPlane = normal/norm_2(normal);
55}
56
57template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
59{
60 return mPointOnPlane;
61}
62
63template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
65{
66 return mNormalToPlane;
67}
68
69template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
71{
72 if constexpr (SPACE_DIM == 1)
73 {
74 if (useJiggledNodesOnPlane)
75 {
76 EXCEPTION("Jiggling of nodes is not supported in 1D.");
77 }
78 }
79 mUseJiggledNodesOnPlane = useJiggledNodesOnPlane;
80}
81
82template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
84{
85 return mUseJiggledNodesOnPlane;
86}
87
88template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
90 [[maybe_unused]] const std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> >& rOldLocations)
91{
92 if constexpr ((SPACE_DIM == 2) || (SPACE_DIM == 3))
93 {
94
95 assert((dynamic_cast<AbstractCentreBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(this->mpCellPopulation))
96 || (SPACE_DIM==ELEMENT_DIM && (dynamic_cast<VertexBasedCellPopulation<SPACE_DIM>*>(this->mpCellPopulation))) );
97
98 // This is a magic number
99 double max_jiggle = 1e-4;
100
101 if (dynamic_cast<AbstractCentreBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(this->mpCellPopulation))
102 {
103 for (typename AbstractCellPopulation<ELEMENT_DIM,SPACE_DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
104 cell_iter != this->mpCellPopulation->End();
105 ++cell_iter)
106 {
107 unsigned node_index = this->mpCellPopulation->GetLocationIndexUsingCell(*cell_iter);
108 Node<SPACE_DIM>* p_node = this->mpCellPopulation->GetNode(node_index);
109
110 c_vector<double, SPACE_DIM> node_location = p_node->rGetLocation();
111
112 double signed_distance = inner_prod(node_location - mPointOnPlane, mNormalToPlane);
113 if (signed_distance > 0.0)
114 {
115 // For the closest point on the plane we travel from node_location the signed_distance in the direction of -mNormalToPlane
116 c_vector<double, SPACE_DIM> nearest_point;
117 if (mUseJiggledNodesOnPlane)
118 {
119 nearest_point = node_location - (signed_distance+max_jiggle*RandomNumberGenerator::Instance()->ranf())*mNormalToPlane;
120 }
121 else
122 {
123 nearest_point = node_location - signed_distance*mNormalToPlane;
124 }
125 p_node->rGetModifiableLocation() = nearest_point;
126 }
127 }
128 }
129 else
130 {
131 assert(SPACE_DIM == ELEMENT_DIM); // LCOV_EXCL_LINE
132 assert(dynamic_cast<VertexBasedCellPopulation<SPACE_DIM>*>(this->mpCellPopulation));
133
134 // Iterate over all nodes and update their positions according to the boundary conditions
135 unsigned num_nodes = this->mpCellPopulation->GetNumNodes();
136 for (unsigned node_index=0; node_index<num_nodes; node_index++)
137 {
138 Node<SPACE_DIM>* p_node = this->mpCellPopulation->GetNode(node_index);
139 c_vector<double, SPACE_DIM> node_location = p_node->rGetLocation();
140
141 double signed_distance = inner_prod(node_location - mPointOnPlane, mNormalToPlane);
142 if (signed_distance > 0.0)
143 {
144 // For the closest point on the plane we travel from node_location the signed_distance in the direction of -mNormalToPlane
145 c_vector<double, SPACE_DIM> nearest_point;
146 if (mUseJiggledNodesOnPlane)
147 {
148 nearest_point = node_location - (signed_distance+max_jiggle*RandomNumberGenerator::Instance()->ranf())*mNormalToPlane;
149 }
150 else
151 {
152 nearest_point = node_location - signed_distance*mNormalToPlane;
153 }
154 p_node->rGetModifiableLocation() = nearest_point;
155 }
156 }
157 }
158 }
159 else if constexpr (SPACE_DIM == 1)
160 {
161 /*
162 * In 1D, this boundary condition is currently applied only to centre-based cell populations.
163 * We therefore iterate over cells and update the location of the node associated with each cell.
164 */
165 assert((dynamic_cast<AbstractCentreBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>*>(this->mpCellPopulation)));
166
167 for (typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
168 cell_iter != this->mpCellPopulation->End();
169 ++cell_iter)
170 {
171 unsigned node_index = this->mpCellPopulation->GetLocationIndexUsingCell(*cell_iter);
172 Node<SPACE_DIM>* p_node = this->mpCellPopulation->GetNode(node_index);
173
174 c_vector<double, SPACE_DIM> node_location = p_node->rGetLocation();
175
176 double signed_distance = inner_prod(node_location - mPointOnPlane, mNormalToPlane);
177 if (signed_distance > 0.0)
178 {
179 c_vector<double, SPACE_DIM> nearest_point = node_location - signed_distance * mNormalToPlane;
180 p_node->rGetModifiableLocation() = nearest_point;
181 }
182 }
183 }
184 else
185 {
187 }
188}
189
190template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
192{
193 bool condition_satisfied = true;
194
195 for (typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
196 cell_iter != this->mpCellPopulation->End();
197 ++cell_iter)
198 {
199 c_vector<double, SPACE_DIM> cell_location = this->mpCellPopulation->GetLocationOfCellCentre(*cell_iter);
200
201 if (inner_prod(cell_location - mPointOnPlane, mNormalToPlane) > 0.0)
202 {
203 condition_satisfied = false;
204 break;
205 }
206 }
207
208 return condition_satisfied;
209}
210
211template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
213{
214 *rParamsFile << "\t\t\t<PointOnPlane>";
215 for (unsigned index=0; index != SPACE_DIM-1U; index++) // Note: inequality avoids testing index < 0U when DIM=1
216 {
217 *rParamsFile << mPointOnPlane[index] << ",";
218 }
219 *rParamsFile << mPointOnPlane[SPACE_DIM-1] << "</PointOnPlane>\n";
220
221 *rParamsFile << "\t\t\t<NormalToPlane>";
222 for (unsigned index=0; index != SPACE_DIM-1U; index++) // Note: inequality avoids testing index < 0U when DIM=1
223 {
224 *rParamsFile << mNormalToPlane[index] << ",";
225 }
226 *rParamsFile << mNormalToPlane[SPACE_DIM-1] << "</NormalToPlane>\n";
227 *rParamsFile << "\t\t\t<UseJiggledNodesOnPlane>" << mUseJiggledNodesOnPlane << "</UseJiggledNodesOnPlane>\n";
228
229 // Call method on direct parent class
231}
232
233// Explicit instantiation
234template class PlaneBoundaryCondition<1,1>;
235template class PlaneBoundaryCondition<1,2>;
236template class PlaneBoundaryCondition<2,2>;
237template class PlaneBoundaryCondition<1,3>;
238template class PlaneBoundaryCondition<2,3>;
239template class PlaneBoundaryCondition<3,3>;
240
241// Serialization for Boost >= 1.36
#define EXCEPTION(message)
#define NEVER_REACHED
#define EXPORT_TEMPLATE_CLASS_ALL_DIMS(CLASS)
AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > * mpCellPopulation
virtual void OutputCellPopulationBoundaryConditionParameters(out_stream &rParamsFile)=0
Definition Node.hpp:59
c_vector< double, SPACE_DIM > & rGetModifiableLocation()
Definition Node.cpp:151
const c_vector< double, SPACE_DIM > & rGetLocation() const
Definition Node.cpp:139
const c_vector< double, SPACE_DIM > & rGetNormalToPlane() const
void ImposeBoundaryCondition(const std::map< Node< SPACE_DIM > *, c_vector< double, SPACE_DIM > > &rOldLocations)
void OutputCellPopulationBoundaryConditionParameters(out_stream &rParamsFile)
PlaneBoundaryCondition(AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > *pCellPopulation, c_vector< double, SPACE_DIM > point, c_vector< double, SPACE_DIM > normal)
c_vector< double, SPACE_DIM > mNormalToPlane
const c_vector< double, SPACE_DIM > & rGetPointOnPlane() const
void SetUseJiggledNodesOnPlane(bool useJiggledNodesOnPlane)
static RandomNumberGenerator * Instance()