Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
ImmersedBoundaryKinematicFeedbackForce.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 "ImmersedBoundaryKinematicFeedbackForce.hpp"
37#include "ImmersedBoundaryEnumerations.hpp"
39
40template <unsigned DIM>
47
48template <unsigned DIM>
50 std::vector<std::pair<Node<DIM>*, Node<DIM>*> >& rNodePairs,
52{
53 // Update memory allocation if number of nodes has changed/on first call
54 if (mPreviousLocations.size() != rCellPopulation.GetNumNodes())
55 {
56 mPreviousLocations.resize(rCellPopulation.GetNumNodes());
57 UpdatePreviousLocations(rCellPopulation);
58 }
59
60 /*
61 * Calculate force only if neither node is in a lamina, and if nodes are in
62 * different elements.
63 */
64 auto condition_satisfied = [&rCellPopulation](const std::pair<Node<DIM>*, Node<DIM>*>& pair) -> bool
65 {
66 // Laminas do not participate in this force class
67 if (pair.first->GetRegion() == LAMINA_REGION || pair.second->GetRegion() == LAMINA_REGION)
68 {
69 return false;
70 }
71 // This force only acts on nodes in different elements
72 if (*(pair.first->ContainingElementsBegin()) == *(pair.second->ContainingElementsBegin()))
73 {
74 return false;
75 }
76 // Return true if the nodes are within threshold distance, else false
77 auto vec_a2b = rCellPopulation.rGetMesh().GetVectorFromAtoB(pair.first->rGetLocation(), pair.second->rGetLocation());
78 return norm_2(vec_a2b) < rCellPopulation.GetInteractionDistance();
79 };
80
81 for (auto&& pair : rNodePairs)
82 {
83 if (condition_satisfied(pair))
84 {
85 Node<DIM>* p_node_a = pair.first;
86 Node<DIM>* p_node_b = pair.second;
87
88 // Get the current and previous displacement between nodes
89 auto previous_disp = rCellPopulation.rGetMesh().GetVectorFromAtoB(mPreviousLocations[p_node_a->GetIndex()],
90 mPreviousLocations[p_node_b->GetIndex()]);
91 auto current_disp = rCellPopulation.rGetMesh().GetVectorFromAtoB(p_node_a->rGetLocation(),
92 p_node_b->rGetLocation());
93
94 // Calculate the relative velocity and fill in unit_perp, the direction of the force that will act
95 c_vector<double, DIM> unit_perp;
96 double relative_vel_comp = CalculateRelativeVelocityComponent(previous_disp, current_disp, unit_perp);
97
98 unsigned a_idx = *(p_node_a->ContainingElementsBegin());
99 unsigned b_idx = *(p_node_b->ContainingElementsBegin());
100
101 double node_a_elem_spacing = rCellPopulation.rGetMesh().GetAverageNodeSpacingOfElement(a_idx, false);
102 double node_b_elem_spacing = rCellPopulation.rGetMesh().GetAverageNodeSpacingOfElement(b_idx, false);
103
104 double elem_spacing = 0.5 * (node_a_elem_spacing + node_b_elem_spacing);
105
106 double eff_spring_const = mSpringConst * elem_spacing / rCellPopulation.GetIntrinsicSpacing();
107
108 /*
109 * We must scale each applied force by a factor of elem_spacing / local spacing, so that forces
110 * balance when spread to the grid later (where the multiplicative factor is the local spacing)
111 */
112 // df = K dx
113 c_vector<double, DIM> force = unit_perp * (relative_vel_comp * eff_spring_const);
114
115 c_vector<double, DIM> force_on_b = force * (elem_spacing / node_a_elem_spacing);
116 p_node_b->AddAppliedForceContribution(force_on_b);
117
118 c_vector<double, DIM> force_on_a = force * (-1.0 * elem_spacing / node_b_elem_spacing);
119 p_node_a->AddAppliedForceContribution(force_on_a);
120 }
121 }
122
123 UpdatePreviousLocations(rCellPopulation);
124
125 if (this->mAdditiveNormalNoise)
126 {
127 this->AddNormalNoiseToNodes(rCellPopulation);
128 }
129}
130
131template <unsigned DIM>
133 const c_vector<double, DIM>& rPreviousDisp,
134 const c_vector<double, DIM>& rCurrentDisp,
135 c_vector<double, DIM>& rUnitPerp)
136{
137 // Get a unit vector perpendicular to the line joining the nodes at the previous time step
138 rUnitPerp = Create_c_vector(-rPreviousDisp[1], rPreviousDisp[0]);
139 rUnitPerp /= norm_2(rUnitPerp);
140
141 // Calculate the relative velocity component in the direction of the perpendicular
142 return inner_prod(rCurrentDisp / SimulationTime::Instance()->GetTimeStep(), rUnitPerp);
143}
144
145template<unsigned DIM>
148{
149 /*
150 * Populate the mPreviousLocations vector with the current location of
151 * nodes, so it's ready for next time step.
152 */
153 for (const auto& p_node : rCellPopulation.rGetMesh().rGetNodes())
154 {
155 if (p_node->GetRegion() != LAMINA_REGION)
156 {
157 if (p_node->GetIndex() >= mPreviousLocations.size())
158 {
159 mPreviousLocations.resize(p_node->GetIndex() + 1);
160 }
161
162 mPreviousLocations[p_node->GetIndex()] = p_node->rGetLocation();
163 }
164 }
165}
166
167template<unsigned DIM>
169 out_stream& rParamsFile)
170{
171 *rParamsFile << "\t\t\t<SpringConstant>" << mSpringConst << "</SpringConstant>\n";
172
173 // Call method on direct parent class
175}
176
177template<unsigned DIM>
179{
180 return mSpringConst;
181}
182
183template<unsigned DIM>
185 double springConst)
186{
187 mSpringConst = springConst;
188}
189
190// Explicit instantiation
194
195// Serialization for Boost >= 1.36
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
virtual void OutputImmersedBoundaryForceParameters(out_stream &rParamsFile)=0
ImmersedBoundaryMesh< DIM, DIM > & rGetMesh()
void UpdatePreviousLocations(ImmersedBoundaryCellPopulation< DIM > &rCellPopulation)
double CalculateRelativeVelocityComponent(const c_vector< double, DIM > &rPreviousDisp, const c_vector< double, DIM > &rCurrentDisp, c_vector< double, DIM > &rUnitPerp)
void AddImmersedBoundaryForceContribution(std::vector< std::pair< Node< DIM > *, Node< DIM > * > > &rNodePairs, ImmersedBoundaryCellPopulation< DIM > &rCellPopulation)
const std::vector< Node< SPACE_DIM > * > & rGetNodes() const
double GetAverageNodeSpacingOfElement(unsigned index, bool recalculate=true)
c_vector< double, SPACE_DIM > GetVectorFromAtoB(const c_vector< double, SPACE_DIM > &rLocation1, const c_vector< double, SPACE_DIM > &rLocation2)
Definition Node.hpp:59
ContainingElementIterator ContainingElementsBegin() const
Definition Node.hpp:485
const c_vector< double, SPACE_DIM > & rGetLocation() const
Definition Node.cpp:139
unsigned GetIndex() const
Definition Node.cpp:158
void AddAppliedForceContribution(const c_vector< double, SPACE_DIM > &rForceContribution)
Definition Node.cpp:224
static SimulationTime * Instance()