netxsimdg
Loading...
Searching...
No Matches
ModelArray.cpp
1
8#include "include/ModelArray.hpp"
9
10#include <algorithm>
11#include <cstdarg>
12#include <iterator>
13#include <set>
14#include <string>
15#include <utility>
16
17namespace Nextsim {
18
19ModelArray::SizeMap ModelArray::m_sz;
20ModelArray::DimensionMap ModelArray::m_dims;
21bool ModelArray::areMapsInvalid = true;
22
23ModelArray::ModelArray(const Type type)
24 : type(type)
25{
26 m_data.resize(std::max(std::size_t { 0 }, m_sz.at(type)), nComponents());
27 validateMaps();
28}
29
31 : ModelArray(orig.type)
32{
33 setData(orig.m_data);
34}
35
37{
38 type = orig.type;
39 setData(orig.m_data);
40
41 return *this;
42}
43
45{
46 setData(fill);
47
48 return *this;
49}
50
52{
53 ModelArray result = *this;
54 return result += addend;
55}
56
57ModelArray ModelArray::operator-(const ModelArray& subtrahend) const
58{
59 ModelArray result = *this;
60 return result -= subtrahend;
61}
62
64{
65 ModelArray result = *this;
66 return result *= multiplier;
67}
68
70{
71 ModelArray result = *this;
72 return result /= divisor;
73}
74
75ModelArray ModelArray::operator-() const
76{
77 ModelArray copy(type);
78 copy.m_data = -m_data;
79 return copy;
80}
81
82ModelArray ModelArray::operator+(const double& x) const
83{
84 ModelArray result = *this;
85 return result += x;
86}
87
88ModelArray ModelArray::operator-(const double& x) const
89{
90 ModelArray result = *this;
91 return result -= x;
92}
93
94ModelArray ModelArray::operator*(const double& x) const
95{
96 ModelArray result = *this;
97 return result *= x;
98}
99
100ModelArray ModelArray::operator/(const double& x) const
101{
102 ModelArray result = *this;
103 return result /= x;
104}
105
106ModelArray operator+(const double& x, const ModelArray& y) { return y + x; }
107
108ModelArray operator-(const double& x, const ModelArray& y) { return -(y - x); }
109
110ModelArray operator*(const double& x, const ModelArray& y) { return y * x; }
111
112ModelArray operator/(const double& x, const ModelArray& y)
113{
114 ModelArray xArray(y.getType());
115 xArray.setData(x);
116 return xArray /= y;
117}
118
120{
121 ModelArray maxed = ModelArray(type);
122 maxed.m_data.array() = m_data.array().max(max);
123 return maxed;
124}
125
127{
128 ModelArray mined = ModelArray(type);
129 mined.m_data.array() = m_data.array().min(min);
130 return mined;
131}
132
134{
135 ModelArray maxed = ModelArray(type);
136 maxed.m_data.array() = m_data.array().max(maxArr.m_data);
137 return maxed;
138}
139
141{
142 ModelArray mined = ModelArray(type);
143 mined.m_data.array() = m_data.array().min(minArr.m_data);
144 return mined;
145}
146
148{
149 m_data = this->max(max).m_data;
150 return *this;
151}
152
154{
155 m_data = this->min(min).m_data;
156 return *this;
157}
158
160{
161 m_data = this->max(maxArr).m_data;
162 return *this;
163}
164
166{
167 m_data = this->min(minArr).m_data;
168 return *this;
169}
170
171void ModelArray::setData(double value)
172{
173 resize();
174 m_data = value;
175}
176
177void ModelArray::setData(const double* pData)
178{
179 resize();
180 auto out = std::copy(pData, pData + m_sz.at(type) * nComponents(), m_data.data());
181}
182
183void ModelArray::setData(const DataType& from) { m_data = from; } // setData(from.data()); }
184
185void ModelArray::setData(const ModelArray& from) { setData(from.m_data.data()); }
186
187void ModelArray::setDimensions(Type type, const MultiDim& newDims)
188{
189 std::vector<Dimension>& dimSpecs = typeDimensions.at(type);
190 for (size_t i = 0; i < dimSpecs.size(); ++i) {
191 definedDimensions.at(dimSpecs[i]).length = newDims[i];
192 }
193 validateMaps();
194}
195
196void ModelArray::setNComponents(std::map<Type, size_t> cMap)
197{
198 for (auto entry : cMap) {
199 setNComponents(entry.first, entry.second);
200 }
201}
202
203void ModelArray::setDimension(Dimension dim, size_t length)
204{
205 definedDimensions.at(dim).length = length;
206 validateMaps();
207}
208
209const double& ModelArray::operator[](const MultiDim& loc) const
210{
211 return (*this)[indexr(this->dimensions().data(), loc)];
212}
213
214double& ModelArray::operator[](const MultiDim& dims)
215{
216 return const_cast<double&>(std::as_const(*this)[dims]);
217}
218
219ModelArray::Component ModelArray::components(const MultiDim& loc)
220{
221 return components(indexr(dimensions().data(), loc));
222}
223
224const ModelArray::ConstComponent ModelArray::components(const MultiDim& loc) const
225{
226 return components(indexr(dimensions().data(), loc));
227}
228
235size_t ModelArray::indexFromLocation(Type type, const MultiDim& loc)
236{
237 return indexr(m_dims.at(type).data(), loc);
238}
239
246ModelArray::MultiDim ModelArray::locationFromIndex(Type type, size_t index)
247{
248 MultiDim& dims = m_dims.at(type);
249 MultiDim loc(dims.size()); // Size to the known number of dimensions
250 for (size_t i = 0; i < loc.size(); ++i) {
251 size_t theDim = dims[i];
252 size_t pos = index % theDim;
253 loc[i] = pos;
254 index /= theDim;
255 }
256 return loc;
257}
258
259void ModelArray::validateMaps()
260{
261 m_dims.validate();
262 m_sz.validate();
263 areMapsInvalid = false;
264}
265
266void ModelArray::DimensionMap::validate()
267{
268 for (auto entry : typeDimensions) {
269 Type type = entry.first;
270 std::vector<size_t>& dims = m_dimensions[type];
271 std::vector<Dimension>& typeDims = entry.second;
272 dims.resize(typeDims.size());
273 for (size_t i = 0; i < typeDims.size(); ++i) {
274 dims[i] = definedDimensions.at(typeDims[i]).length;
275 }
276 }
277}
278
279void ModelArray::SizeMap::validate()
280{
281 for (auto entry : typeDimensions) {
282 size_t size = 1;
283 std::vector<Dimension>& typeDims = entry.second;
284 for (size_t i = 0; i < typeDims.size(); ++i) {
285 size *= definedDimensions.at(typeDims[i]).length;
286 }
287 m_sizes.at(entry.first) = size;
288 }
289}
290
291} /* namespace Nextsim */
A class that holds the array data for the model.
const double & operator[](size_t i) const
Returns the data at the specified one dimensional index.
Component components(size_t i)
Accesses the full Discontinuous Galerkin coefficient vector at the indexed location.
static TypeDimensions typeDimensions
The dimensions that make up each defined type. Defined in ModelArrayDetails.cpp.
size_t indexFromLocation(const MultiDim &loc) const
Returns the index for a given set of multi-dimensional location for this array's type.
const MultiDim & dimensions() const
Returns a vector<size_t> of the size of each dimension of this type of ModelArray.
ModelArray & clampBelow(double min)
Clamps the values in the array to the given minimum.
ModelArray operator*(const ModelArray &) const
Returns a ModelArray containing the per-element product of the object and the provided ModelArray.
ModelArray min(double min) const
Calculates element-wise minimum of the data and the given scalar value.
ModelArray & operator=(const ModelArray &)
Copy assignment operator.
ModelArray operator+(const ModelArray &) const
Returns a ModelArray containing the per-element sum of the object and the provided ModelArray.
ModelArray & clampAbove(double max)
Clamps the values in the array to the given maximum.
static void setDimensions(Type, const MultiDim &)
Sets the number and size of the dimensions of a specified type of ModelArray.
static void setDimension(Dimension dim, size_t length)
Sets the length of an individual dimension before propagating it to the defined array types.
void setData(double value)
Sets the value of every element in the object to the provided value.
void resize()
Conditionally updates the size of the object data buffer to match the class specification.
ModelArray operator/(const ModelArray &) const
Returns a ModelArray containing the per-element ratio between the object and the provided ModelArray.
const DataType & data() const
Returns a const reference to the Eigen data.
size_t nComponents() const
Returns the number of discontinuous Galerkin components held in this type of ModelArray.
MultiDim locationFromIndex(size_t index) const
Returns the multi-dimensional location for a given index for this array's type.
ModelArray max(double max) const
Calculates element-wise maximum of the data and the given scalar value.
size_t size() const
Returns the total number of elements of this type of ModelArray.
static std::map< Dimension, DimensionSpec > definedDimensions
The name and length of each dimension that is defined.
static void setNComponents(std::map< Type, size_t > cMap)
Sets the number of components for DG & CG array types.