Chaste Commit::9e4a273f0754a391514ab12ed9d4a38fc9933db2
RK4NumericalMethod.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 "RK4NumericalMethod.hpp"
37
38template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
43
44template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
48
49template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
51{
52 // Store initial node locations
53 auto old_node_locations = this->SaveCurrentNodeLocations();
54
55 // Apply boundary conditions to the initial positions, then resave
56 this->ImposeBoundaryConditions(old_node_locations);
57 old_node_locations = this->SaveCurrentNodeLocations();
58
59 // Compute k1 = F(r^t) / nu
60 auto k1 = this->ComputeForcesIncludingDamping();
61
62 // Update to r^t + dt*k1/2 and compute k2
63 unsigned index = 0;
64 for (auto node_iter = this->mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
65 node_iter != this->mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
66 ++node_iter, ++index)
67 {
68 c_vector<double, SPACE_DIM> new_location = node_iter->rGetLocation() + (dt/2.0) * k1[index];
69 this->SafeNodePositionUpdate(node_iter->GetIndex(), new_location);
70 }
71 this->ImposeBoundaryConditions(old_node_locations);
72
73 // Compute k2 = F(r^t + dt*k1/2) / nu
74 auto k2 = this->ComputeForcesIncludingDamping();
75
76 // Update to r^t + dt*k2/2 (from old locations) and compute k3
77 index = 0;
78 for (auto node_iter = this->mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
79 node_iter != this->mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
80 ++node_iter, ++index)
81 {
82 c_vector<double, SPACE_DIM> old_location = old_node_locations.find(&(*node_iter))->second;
83 c_vector<double, SPACE_DIM> new_location = old_location + (dt/2.0) * k2[index];
84 this->SafeNodePositionUpdate(node_iter->GetIndex(), new_location);
85 }
86 this->ImposeBoundaryConditions(old_node_locations);
87
88 // Compute k3 = F(r^t + dt*k2/2) / nu
89 auto k3 = this->ComputeForcesIncludingDamping();
90
91 // Update to r^t + dt*k3 (from old locations) and compute k4
92 index = 0;
93 for (auto node_iter = this->mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
94 node_iter != this->mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
95 ++node_iter, ++index)
96 {
97 c_vector<double, SPACE_DIM> old_location = old_node_locations.find(&(*node_iter))->second;
98 c_vector<double, SPACE_DIM> new_location = old_location + dt * k3[index];
99 this->SafeNodePositionUpdate(node_iter->GetIndex(), new_location);
100 }
101 this->ImposeBoundaryConditions(old_node_locations);
102
103 // Compute k4 = F(r^t + dt*k3) / nu
104 auto k4 = this->ComputeForcesIncludingDamping();
105
106 // Final RK4 position update: r^(t+1) = r^t + (dt/6)*(k1 + 2*k2 + 2*k3 + k4)
107 index = 0;
108 for (auto node_iter = this->mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
109 node_iter != this->mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
110 ++node_iter, ++index)
111 {
112 c_vector<double, SPACE_DIM> effective_force = (k1[index] + 2.0*k2[index] + 2.0*k3[index] + k4[index]) / 6.0;
113 c_vector<double, SPACE_DIM> old_location = old_node_locations.find(&(*node_iter))->second;
114 c_vector<double, SPACE_DIM> displacement = dt * effective_force;
115
116 this->DetectStepSizeExceptions(node_iter->GetIndex(), displacement, dt);
117
118 c_vector<double, SPACE_DIM> new_location = old_location + displacement;
119 this->SafeNodePositionUpdate(node_iter->GetIndex(), new_location);
120 }
121}
122
123template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
125{
126 // No new parameters to output, so just call method on direct parent class
128}
129
130// Explicit instantiation
131template class RK4NumericalMethod<1,1>;
132template class RK4NumericalMethod<1,2>;
133template class RK4NumericalMethod<2,2>;
134template class RK4NumericalMethod<1,3>;
135template class RK4NumericalMethod<2,3>;
136template class RK4NumericalMethod<3,3>;
137
138// Serialization for Boost >= 1.36
#define EXPORT_TEMPLATE_CLASS_ALL_DIMS(CLASS)
virtual void OutputNumericalMethodParameters(out_stream &rParamsFile)
virtual void OutputNumericalMethodParameters(out_stream &rParamsFile)
void UpdateAllNodePositions(double dt)