Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
AbstractImmersedBoundaryForce.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 <random>
37
38#include "AbstractImmersedBoundaryForce.hpp"
39
40template <unsigned DIM>
42 : mAdditiveNormalNoise(false),
43 mNormalNoiseMean(1.0),
44 mNormalNoiseStdDev(0.0)
45{
46}
47
48template<unsigned DIM>
52
53template<unsigned DIM>
56{
58
59 // Add random forces to elements
60 for (unsigned elem_idx = 0; elem_idx < rCellPopulation.GetNumElements(); ++elem_idx)
61 {
62 unsigned num_nodes_this_elem = rCellPopulation.GetElement(elem_idx)->GetNumNodes();
63
64 // Generate a vector with 0, 1, 2, ..., num_nodes-1
65 std::vector<unsigned> random_node_order;
66 random_node_order.reserve(num_nodes_this_elem);
67 for (unsigned node_idx = 0; node_idx < num_nodes_this_elem; ++node_idx)
68 {
69 random_node_order.push_back(node_idx);
70 }
71
72 // Shuffle the vector
73 std::shuffle(random_node_order.begin(), random_node_order.end(), std::mt19937(std::random_device()()));
74
75 // Only go to half way (rounded down - forget about the very last node for now)
76 unsigned half_way = num_nodes_this_elem / 2;
77 for (unsigned i = 0; i < half_way; ++i)
78 {
79 unsigned rand_node_a = random_node_order[2 * i];
80 unsigned rand_node_b = random_node_order[2 * i + 1];
81
82 c_vector<double, DIM> random_force;
83 for (unsigned dim = 0; dim < DIM; ++dim)
84 {
85 random_force[dim] = p_gen->NormalRandomDeviate(mNormalNoiseMean, mNormalNoiseStdDev);
86 }
87
88 Node<DIM>* p_node_a = rCellPopulation.GetElement(elem_idx)->GetNode(rand_node_a);
89 Node<DIM>* p_node_b = rCellPopulation.GetElement(elem_idx)->GetNode(rand_node_b);
90
91 double avg_magnitude = 0.5 * (norm_2(p_node_a->rGetAppliedForce()) +
92 norm_2(p_node_b->rGetAppliedForce()));
93
94 random_force *= avg_magnitude;
95 p_node_a->AddAppliedForceContribution(random_force);
96
97 random_force *= -1.0;
98 p_node_b->AddAppliedForceContribution(random_force);
99 }
100 }
101
102 // Do the same for laminas
103 for (unsigned lam_idx = 0; lam_idx < rCellPopulation.GetNumLaminas(); ++lam_idx)
104 {
105 unsigned num_nodes_this_elem = rCellPopulation.GetLamina(lam_idx)->GetNumNodes();
106
107 // Generate a vector with 0, 1, 2, ..., num_nodes-1
108 std::vector<unsigned> random_node_order;
109 random_node_order.reserve(num_nodes_this_elem);
110 for (unsigned node_idx = 0; node_idx < num_nodes_this_elem; ++node_idx)
111 {
112 random_node_order.push_back(node_idx);
113 }
114
115 // Shuffle the vector
116 std::shuffle(random_node_order.begin(), random_node_order.end(), std::mt19937(std::random_device()()));
117
118 // Only go to half way (rounded down - forget about the very last node for now)
119 unsigned half_way = num_nodes_this_elem / 2;
120 for (unsigned i = 0; i < half_way; ++i)
121 {
122 unsigned rand_node_a = random_node_order[2 * i];
123 unsigned rand_node_b = random_node_order[2 * i + 1];
124
125 c_vector<double, DIM> random_force;
126 for (unsigned dim = 0; dim < DIM; ++dim)
127 {
128 random_force[dim] = p_gen->NormalRandomDeviate(mNormalNoiseMean, mNormalNoiseStdDev);
129 }
130
131 Node<DIM>* p_node_a = rCellPopulation.GetLamina(lam_idx)->GetNode(rand_node_a);
132 Node<DIM>* p_node_b = rCellPopulation.GetLamina(lam_idx)->GetNode(rand_node_b);
133
134 double avg_magnitude = 0.5 * (norm_2(p_node_a->rGetAppliedForce()) +
135 norm_2(p_node_b->rGetAppliedForce()));
136
137 random_force *= avg_magnitude;
138 p_node_a->AddAppliedForceContribution(random_force);
139
140 random_force *= -1.0;
141 p_node_b->AddAppliedForceContribution(random_force);
142 }
143 }
144}
145
146template <unsigned DIM>
148{
149 return mAdditiveNormalNoise;
150}
151
152template <unsigned DIM>
154 bool additiveNormalNoise)
155{
156 mAdditiveNormalNoise = additiveNormalNoise;
157}
158
159template <unsigned DIM>
161{
162 return mNormalNoiseMean;
163}
164
165template <unsigned DIM>
167 double normalNoiseMean)
168{
169 mNormalNoiseMean = normalNoiseMean;
170}
171
172template <unsigned DIM>
174{
175 return mNormalNoiseStdDev;
176}
177
178template <unsigned DIM>
180 double normalNoiseStdDev)
181{
182 mNormalNoiseStdDev = normalNoiseStdDev;
183}
184
185template<unsigned DIM>
187 out_stream& rParamsFile)
188{
189 *rParamsFile << "\t\t\t<AdditiveNormalNoise>" << mAdditiveNormalNoise << "</AdditiveNormalNoise>\n";
190 *rParamsFile << "\t\t\t<NormalNoiseMean>" << mNormalNoiseMean << "</NormalNoiseMean>\n";
191 *rParamsFile << "\t\t\t<NormalNoiseStdDev>" << mNormalNoiseStdDev << "</NormalNoiseStdDev>\n";
192}
193
194// Explicit instantiation
void SetNormalNoiseStdDev(double normalNoiseStdDev)
void SetNormalNoiseMean(double normalNoiseMean)
virtual void OutputImmersedBoundaryForceParameters(out_stream &rParamsFile)=0
void AddNormalNoiseToNodes(ImmersedBoundaryCellPopulation< DIM > &rCellPopulation)
void SetAdditiveNormalNoise(bool additiveNormalNoise)
ImmersedBoundaryElement< DIM - 1, DIM > * GetLamina(unsigned laminaIndex)
ImmersedBoundaryElement< DIM, DIM > * GetElement(unsigned elementIndex)
Definition Node.hpp:59
c_vector< double, SPACE_DIM > & rGetAppliedForce()
Definition Node.cpp:208
void AddAppliedForceContribution(const c_vector< double, SPACE_DIM > &rForceContribution)
Definition Node.cpp:224
double NormalRandomDeviate(double mean, double stdDev)
static RandomNumberGenerator * Instance()