Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
UniformGridRandomFieldGenerator.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 "UniformGridRandomFieldGenerator.hpp"
37
38#include <iomanip>
39#include <fstream>
40#include <numeric>
41
42#include "Exception.hpp"
43#include "FileFinder.hpp"
44#include "Node.hpp"
45#include "OutputFileHandler.hpp"
46#include "RandomNumberGenerator.hpp"
47#include "Warnings.hpp"
48#include "RandomFieldUtilityFunctions.hpp"
49
50template <unsigned SPACE_DIM>
52 std::array<double, SPACE_DIM> lowerCorner,
53 std::array<double, SPACE_DIM> upperCorner,
54 std::array<unsigned, SPACE_DIM> numGridPts,
55 std::array<bool, SPACE_DIM> periodicity,
56 double lengthScale)
57 : mLowerCorner(lowerCorner),
58 mUpperCorner(upperCorner),
59 mNumGridPts(numGridPts),
60 mPeriodicity(periodicity),
61 mLengthScale(lengthScale),
62 mCacheDir("CachedRandomFields/")
63{
64 // Calculate the total number of grid points
65 mNumTotalGridPts = std::accumulate(mNumGridPts.begin(),
66 mNumGridPts.end(),
67 1u,
68 std::multiplies<unsigned>());
69
70 // Check parameters are sensible
71 for (unsigned dim = 0; dim < SPACE_DIM; ++dim)
72 {
73 assert(mLowerCorner[dim] < mUpperCorner[dim]);
74 assert(mNumGridPts[dim] > 0);
75 }
76 assert(mLengthScale > 0.0);
77
78 // Calculate the grid spacings
79 for (unsigned dim = 0; dim < SPACE_DIM; ++dim)
80 {
81 mGridSpacing[dim] = (mUpperCorner[dim] - mLowerCorner[dim]) / mNumGridPts[dim];
82 mOneOverGridSpacing[dim] = mNumGridPts[dim] / (mUpperCorner[dim] - mLowerCorner[dim]);
83 }
84
86}
87
88template <unsigned SPACE_DIM>
90{
91 mOpenSimplex = OpenSimplex2S(seed);
92}
93
94template <unsigned SPACE_DIM>
96{
97 return this->SampleRandomFieldAtTime(rand());
98}
99
100template <unsigned SPACE_DIM>
102{
103 std::vector<double> samples(mNumTotalGridPts);
104
105 switch(SPACE_DIM)
106 {
107 case 1:
108 {
109 for (unsigned x = 0; x < mNumGridPts[0]; x++)
110 {
111 samples[x] = random_field::Reshape(mOpenSimplex.noise2_XBeforeY(x * mLengthScale, time));
112 }
113 break;
114 }
115 case 2:
116 {
117 for (unsigned x = 0; x < mNumGridPts[0]; x++)
118 {
119 for (unsigned y = 0; y < mNumGridPts[1]; y++)
120 {
121 samples[mNumGridPts[1] * y + x] = random_field::Reshape(mOpenSimplex.noise3_XYBeforeZ(x * mLengthScale, y * mLengthScale, time));
122 }
123 }
124 break;
125 }
126 case 3:
127 {
128 for (unsigned x = 0; x < mNumGridPts[0]; x++)
129 {
130 for (unsigned y = 0; y < mNumGridPts[1]; y++)
131 {
132 for (unsigned z = 0; z < mNumGridPts[2]; z++)
133 {
134 samples[mNumGridPts[2] * z * y + mNumGridPts[1] * y + x] = random_field::Reshape(mOpenSimplex.noise4_XYBeforeZW(x * mLengthScale, y * mLengthScale, z * mLengthScale, time));
135 }
136 }
137 }
138 break;
139 }
140 default:
142 break;
143 }
144
145 return samples;
146}
147
148template <unsigned SPACE_DIM>
149double UniformGridRandomFieldGenerator<SPACE_DIM>::Interpolate(const std::vector<double>& rRandomField,
150 const c_vector<double, SPACE_DIM>& rLocation) const
151{
152 assert(mNumTotalGridPts == rRandomField.size());
153
154 // Find the nearest node
155 std::array<long, SPACE_DIM> lower_left;
156
157 for (unsigned dim = 0; dim < SPACE_DIM; ++dim)
159 lower_left[dim] = std::floor((rLocation[dim] - mLowerCorner[dim]) / mGridSpacing[dim]);
160
161 if (lower_left[dim] < 0)
162 {
163 lower_left[dim] = 0;
164 WARN_ONCE_ONLY("Interpolating outside random field grid: does the random field need to be larger?");
165 }
166 else if (lower_left[dim] >= mNumGridPts[dim])
167 {
168 lower_left[dim] = mNumGridPts[dim] - 1;
169 WARN_ONCE_ONLY("Interpolating outside random field grid: does the random field need to be larger?");
170 }
171 }
173 double interpolated_value{};
174
175 // Perform the interpolation
176 switch(SPACE_DIM)
177 {
178 case 1:
179 {
180 // The value of the field at points either side of rLocation
181 std::array<long, SPACE_DIM> upper_idx = lower_left;
182 upper_idx[0] = (upper_idx[0] + 1) % mNumGridPts[0];
183
184 const double field_at_lower = rRandomField[GetLinearIndex(lower_left)];
185 const double field_at_upper = rRandomField[GetLinearIndex(upper_idx)];
186
187 // Perform a simple linear interpolation
188 const std::array<double, SPACE_DIM> lower_location = GetPositionUsingGridIndex(lower_left);
189 const double interpolant = (rLocation[0] - lower_location[0]) * mOneOverGridSpacing[0];
190
191 interpolated_value = field_at_lower * (1.0 - interpolant) + field_at_upper * interpolant;
192 break;
194 case 2:
195 {
196 // The value of the field at the four points of the square containing rLocation
197 std::array<long, SPACE_DIM> x_upper = lower_left;
198 x_upper[0] = (x_upper[0] + 1) % mNumGridPts[0];
199
200 std::array<long, SPACE_DIM> y_upper = lower_left;
201 y_upper[1] = (y_upper[1] + 1) % mNumGridPts[1];
202
203 std::array<long, SPACE_DIM> xy_upper = x_upper;
204 xy_upper[1] = y_upper[1];
205
206 const double field_xy_lower = rRandomField[GetLinearIndex(lower_left)];
207 const double field_x_upper = rRandomField[GetLinearIndex(x_upper)];
208 const double field_y_upper = rRandomField[GetLinearIndex(y_upper)];
209 const double field_xy_upper = rRandomField[GetLinearIndex(xy_upper)];
210
211 // Perform a simple bilinear interpolation
212 const std::array<double, SPACE_DIM> lower_location = GetPositionUsingGridIndex(lower_left);
213 const double dist_x_lower = rLocation[0] - lower_location[0];
214 const double dist_x_upper = mGridSpacing[0] - dist_x_lower;
215 const double dist_y_lower = rLocation[1] - lower_location[1];
216 const double dist_y_upper = mGridSpacing[1] - dist_y_lower;
217
218 interpolated_value = mOneOverGridSpacing[0] * mOneOverGridSpacing[1] * (field_xy_lower * dist_x_upper * dist_y_upper +
219 field_x_upper * dist_x_lower * dist_y_upper +
220 field_y_upper * dist_x_upper * dist_y_lower +
221 field_xy_upper * dist_x_lower * dist_y_lower);
222
223 break;
224 }
225 case 3:
226 {
227 // The value of the field at the eight points of the cube containing rLocation
228 std::array<long, SPACE_DIM> x_upper = lower_left;
229 x_upper[0] = (x_upper[0] + 1) % mNumGridPts[0];
230
231 std::array<long, SPACE_DIM> y_upper = lower_left;
232 y_upper[1] = (y_upper[1] + 1) % mNumGridPts[1];
233
234 std::array<long, SPACE_DIM> z_upper = lower_left;
235 z_upper[2] = (z_upper[2] + 1) % mNumGridPts[2];
236
237 std::array<long, SPACE_DIM> xy_upper = x_upper;
238 xy_upper[1] = y_upper[1];
239
240 std::array<long, SPACE_DIM> xz_upper = x_upper;
241 xz_upper[2] = z_upper[2];
242
243 std::array<long, SPACE_DIM> yz_upper = y_upper;
244 yz_upper[2] = z_upper[2];
245
246 std::array<long, SPACE_DIM> xyz_upper = xy_upper;
247 xyz_upper[2] = z_upper[2];
248
249 const double field_xyz_lower = rRandomField[GetLinearIndex(lower_left)];
250 const double field_x_upper = rRandomField[GetLinearIndex(x_upper)];
251 const double field_y_upper = rRandomField[GetLinearIndex(y_upper)];
252 const double field_z_upper = rRandomField[GetLinearIndex(z_upper)];
253 const double field_xy_upper = rRandomField[GetLinearIndex(xy_upper)];
254 const double field_xz_upper = rRandomField[GetLinearIndex(xz_upper)];
255 const double field_yz_upper = rRandomField[GetLinearIndex(yz_upper)];
256 const double field_xyz_upper = rRandomField[GetLinearIndex(xyz_upper)];
257
258 // Perform a trilinear interpolation
259 const std::array<double, SPACE_DIM> lower_location = GetPositionUsingGridIndex(lower_left);
260 const double dist_x_lower = rLocation[0] - lower_location[0];
261 const double dist_y_lower = rLocation[1] - lower_location[1];
262 const double dist_z_lower = rLocation[2] - lower_location[2];
263
264 const double xd = dist_x_lower / mGridSpacing[0];
265 const double yd = dist_y_lower / mGridSpacing[1];
266 const double zd = dist_z_lower / mGridSpacing[2];
267
268 const double c00 = field_xyz_lower * (1.0 - xd) + (field_x_upper * xd);
269 const double c01 = field_z_upper * (1.0 - xd) + (field_xz_upper * xd);
270 const double c10 = field_y_upper * (1.0 - xd) + (field_xy_upper * xd);
271 const double c11 = field_yz_upper * (1.0 - xd) + (field_xyz_upper * xd);
272
273 const double c0 = c00 * (1.0 - yd) + (c10 * yd);
274 const double c1 = c01 * (1.0 - yd) + (c11 * yd);
275
276 const double c = c0 * (1.0 - zd) + (c1 * zd);
277 interpolated_value = c;
278
279 break;
280 }
281 default:
283 }
284
285 return interpolated_value;
286}
287
288template <unsigned SPACE_DIM>
289long UniformGridRandomFieldGenerator<SPACE_DIM>::GetLinearIndex(std::array<long, SPACE_DIM> gridIndex) const
290{
291 long linear_index;
292
293 switch(SPACE_DIM)
294 {
295 case 1:
296 {
297 linear_index = gridIndex[0];
298 break;
299 }
300 case 2:
301 {
302 linear_index = gridIndex[0] +
303 gridIndex[1] * mNumGridPts[0];
304 break;
305 }
306 case 3:
307 {
308 linear_index = gridIndex[0] +
309 gridIndex[1] * mNumGridPts[0] +
310 gridIndex[2] * mNumGridPts[0] * mNumGridPts[1];
311 break;
312 }
313 default:
315 }
316
317 return linear_index;
318}
319
320template <unsigned SPACE_DIM>
321std::array<double, SPACE_DIM> UniformGridRandomFieldGenerator<SPACE_DIM>::GetPositionUsingGridIndex(std::array<long, SPACE_DIM> gridIndex) const
322{
323 std::array<double, SPACE_DIM> position = {{}};
324
325 for (unsigned dim = 0; dim < SPACE_DIM; ++dim)
326 {
327 position[dim] = mLowerCorner[dim] + mGridSpacing[dim] * gridIndex[dim];
328 }
329
330 return position;
331}
332
333// Explicit instantiation
#define NEVER_REACHED
std::vector< double > SampleRandomFieldAtTime(double time)
std::array< double, SPACE_DIM > GetPositionUsingGridIndex(std::array< long, SPACE_DIM > gridIndex) const
UniformGridRandomFieldGenerator(std::array< double, SPACE_DIM > lowerCorner, std::array< double, SPACE_DIM > upperCorner, std::array< unsigned, SPACE_DIM > numGridPts, std::array< bool, SPACE_DIM > periodicity, double lengthScale)
double Interpolate(const std::vector< double > &rRandomField, const c_vector< double, SPACE_DIM > &rLocation) const
std::array< double, SPACE_DIM > mOneOverGridSpacing
long GetLinearIndex(std::array< long, SPACE_DIM > gridIndex) const
std::array< unsigned, SPACE_DIM > mNumGridPts