Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
OffLatticeRandomFieldGenerator.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 "OffLatticeRandomFieldGenerator.hpp"
37
38#include <algorithm>
39#include <iomanip>
40#include <fstream>
41#include <memory>
42#include <numeric>
43
44#include "Exception.hpp"
45#include "RandomNumberGenerator.hpp"
46#include "RandomFieldUtilityFunctions.hpp"
47
48template <unsigned SPACE_DIM>
50 std::array<double, SPACE_DIM> lowerCorner,
51 std::array<double, SPACE_DIM> upperCorner,
52 std::array<bool, SPACE_DIM> periodicity,
53 double lengthScale,
54 double boxWidth)
55 : mLowerCorner(lowerCorner),
56 mUpperCorner(upperCorner),
57 mPeriodicity(periodicity),
58 mLengthScale(lengthScale),
59 mpBoxCollection(nullptr)
60{
61 // Reset the box width if the default value is being used
62 if (boxWidth == DOUBLE_UNSET)
63 {
64 const double max_box_width = 4.0 * lengthScale;
65 boxWidth = std::min(max_box_width, upperCorner[0] - lowerCorner[0]);
66 }
67
68 // Set up the box collection
69 c_vector<double, 2 * SPACE_DIM> domain_size;
70 for (unsigned dim = 0; dim < SPACE_DIM; ++dim)
71 {
72 domain_size[2 * dim] = lowerCorner[dim];
73 domain_size[2 * dim + 1] = upperCorner[dim];
74 }
75
77 bool periodic_x = periodicity[0];
78 bool periodic_y = SPACE_DIM > 1 ? periodicity[1] : false;
79 bool periodic_z = SPACE_DIM > 2 ? periodicity[2] : false;
80
81 mpBoxCollection = std::make_unique<ObsoleteBoxCollection<SPACE_DIM>>(
82 boxWidth,
83 domain_size,
84 periodic_x,
85 periodic_y,
86 periodic_z
87 );
88
89 mpBoxCollection->SetupLocalBoxesHalfOnly();
91}
92
93template <unsigned SPACE_DIM>
95 const unsigned seed)
96{
97 mOpenSimplex = OpenSimplex2S(seed);
98}
99
100template <unsigned SPACE_DIM>
102 const std::vector<Node<SPACE_DIM>*>& rNodes)
103{
104 return this->SampleRandomFieldAtTime(rNodes, 100.0 * mLengthScale * RandomNumberGenerator::Instance()->ranf());
105}
106
107template <unsigned SPACE_DIM>
109 const std::vector<Node<SPACE_DIM>*>& rNodes,
110 const double time)
111{
112 std::vector<double> samples(rNodes.size());
113 for (unsigned i = 0; i < samples.size(); ++i)
114 {
115 const c_vector<double, SPACE_DIM> node_location = rNodes[i]->rGetLocation();
116 switch (SPACE_DIM)
117 {
118 case 1:
119 {
120 samples[i] = random_field::Reshape(mOpenSimplex.noise2_XBeforeY(node_location[0] * mLengthScale, time + 0.5));
121 break;
122 }
123 case 2:
124 {
125 samples[i] = random_field::Reshape(mOpenSimplex.noise3_XYBeforeZ(node_location[0] * mLengthScale, node_location[1] * mLengthScale, time));
126 break;
127 }
128 case 3:
129 {
130 samples[i] = random_field::Reshape(mOpenSimplex.noise4_XYBeforeZW(node_location[0] * mLengthScale, node_location[1] * mLengthScale, node_location[2] * mLengthScale, time));
131 break;
132 }
133 default:
134 // This can't happen
136 }
137 }
138
139 return samples;
140}
141// Explicit instantiation
const double DOUBLE_UNSET
Definition Exception.hpp:57
#define NEVER_REACHED
Definition Node.hpp:59
std::vector< double > SampleRandomField(const std::vector< Node< SPACE_DIM > * > &rNodes)
std::unique_ptr< ObsoleteBoxCollection< SPACE_DIM > > mpBoxCollection
std::vector< double > SampleRandomFieldAtTime(const std::vector< Node< SPACE_DIM > * > &rNodes, double time)
OffLatticeRandomFieldGenerator(std::array< double, SPACE_DIM > lowerCorner, std::array< double, SPACE_DIM > upperCorner, std::array< bool, SPACE_DIM > periodicity, double lengthScale, double boxWidth=DOUBLE_UNSET)
static RandomNumberGenerator * Instance()