SPH
Column.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 #include "quantities/Quantity.h"
10 #include "quantities/QuantityIds.h"
11 #include "quantities/Storage.h"
12 #include "system/Statistics.h"
13 
15 
30 class ITextColumn : public Polymorphic {
31 public:
38  virtual Dynamic evaluate(const Storage& storage,
39  const Statistics& stats,
40  const Size particleIdx) const = 0;
41 
49  virtual void accumulate(Storage& storage, const Dynamic value, const Size particleIdx) const = 0;
50 
54  virtual std::string getName() const = 0;
55 
57  virtual ValueEnum getType() const = 0;
58 };
59 
60 
72 template <typename TValue>
73 class ValueColumn : public ITextColumn {
74 private:
75  QuantityId id;
76 
77 public:
78  explicit ValueColumn(const QuantityId id)
79  : id(id) {}
80 
81  virtual Dynamic evaluate(const Storage& storage,
82  const Statistics& UNUSED(stats),
83  const Size particleIdx) const override {
84  ArrayView<const TValue> values = storage.getValue<TValue>(id);
85  if (particleIdx >= values.size()) {
86  throw Exception("Cannot evaluate value of particle " + std::to_string(particleIdx) +
87  ", storage only contains " + std::to_string(values.size()) + " particles");
88  }
89  return values[particleIdx];
90  }
91 
92  virtual void accumulate(Storage& storage, const Dynamic value, const Size particleIdx) const override {
93  if (!storage.has(id)) {
94  // lazy initialization
95  storage.insert<TValue>(id, OrderEnum::ZERO, TValue(0._f));
96  }
97  Array<TValue>& array = storage.getValue<TValue>(id);
98  array.resize(particleIdx + 1);
99  array[particleIdx] = value.get<TValue>();
100  }
101 
102  virtual std::string getName() const override {
103  return getMetadata(id).quantityName;
104  }
105 
106  virtual ValueEnum getType() const override {
108  }
109 };
110 
114 template <typename TValue>
116 private:
117  QuantityId id;
118 
119 public:
120  explicit DerivativeColumn(const QuantityId id)
121  : id(id) {}
122 
123  virtual Dynamic evaluate(const Storage& storage,
124  const Statistics& UNUSED(stats),
125  const Size particleIdx) const override {
126  ArrayView<const TValue> values = storage.getDt<TValue>(id);
127  if (particleIdx >= values.size()) {
128  throw Exception("Cannot evaluate derivative of particle " + std::to_string(particleIdx) +
129  ", storage only contains " + std::to_string(values.size()) + " particles");
130  }
131  return values[particleIdx];
132  }
133 
134  virtual void accumulate(Storage& storage, const Dynamic value, const Size particleIdx) const override {
135  if (!storage.has(id)) {
136  // lazy initialization
137  storage.insert<TValue>(id, OrderEnum::FIRST, TValue(0._f));
138  } else if (storage.getQuantity(id).getOrderEnum() == OrderEnum::ZERO) {
139  // has the quantity, but not derivatives; we need to resize it manually to side-step the check
140  // of equality in Storage::insert.
142  }
143  Array<TValue>& array = storage.getDt<TValue>(id);
144  array.resize(particleIdx + 1);
145  array[particleIdx] = value.get<TValue>();
146  }
147 
148  virtual std::string getName() const override {
149  return getMetadata(id).derivativeName;
150  }
151 
152  virtual ValueEnum getType() const override {
154  }
155 };
156 
160 template <typename TValue>
162 private:
163  QuantityId id;
164 
165 public:
167  : id(id) {}
168 
169  virtual Dynamic evaluate(const Storage& storage,
170  const Statistics& UNUSED(stats),
171  const Size particleIdx) const override {
172  ArrayView<const TValue> values = storage.getD2t<TValue>(id);
173  if (particleIdx >= values.size()) {
174  throw Exception("Cannot evaluate 2nd derivative of particle " + std::to_string(particleIdx) +
175  ", storage only contains " + values.size() + " particles");
176  }
177  return values[particleIdx];
178  }
179 
180  virtual void accumulate(Storage& storage, const Dynamic value, const Size particleIdx) const override {
181  if (!storage.has(id)) {
182  // lazy initialization
183  storage.insert<TValue>(id, OrderEnum::SECOND, TValue(0._f));
184  } else if (storage.getQuantity(id).getOrderEnum() < OrderEnum::SECOND) {
185  // has the quantity, but not derivatives; we need to resize it manually to side-step the check
186  // of equality in Storage::insert.
188  }
189  Array<TValue>& array = storage.getD2t<TValue>(id);
190  array.resize(particleIdx + 1);
191  array[particleIdx] = value.get<TValue>();
192  }
193 
194  virtual std::string getName() const override {
196  }
197 
198  virtual ValueEnum getType() const override {
200  }
201 };
202 
205 public:
206  virtual Dynamic evaluate(const Storage& storage,
207  const Statistics& UNUSED(stats),
208  const Size particleIdx) const override {
210  if (particleIdx >= values.size()) {
211  throw Exception("Cannot evaluate smoothing length of particle " + std::to_string(particleIdx) +
212  ", storage only contains " + std::to_string(values.size()) + " particles");
213  }
214  return values[particleIdx][H];
215  }
216 
217  virtual void accumulate(Storage& storage, const Dynamic value, const Size particleIdx) const override {
218  if (!storage.has(QuantityId::POSITION)) {
219  // lazy initialization
221  }
223  array.resize(particleIdx + 1);
224  array[particleIdx][H] = value.get<Float>();
225  }
226 
227  virtual std::string getName() const override {
228  return "Smoothing length";
229  }
230 
231  virtual ValueEnum getType() const override {
232  return ValueEnum::SCALAR;
233  }
234 };
235 
239 template <typename TValue>
240 class DamageColumn : public ITextColumn {
241 public:
242  virtual Dynamic evaluate(const Storage& storage,
243  const Statistics& UNUSED(stats),
244  const Size particleIdx) const override {
245  ArrayView<const TValue> values = storage.getValue<TValue>(QuantityId::DAMAGE);
246  if (particleIdx >= values.size()) {
247  throw Exception("Cannot evaluate damage of particle " + std::to_string(particleIdx) +
248  ", storage only contains " + values.size() + " particles");
249  }
250  return pow<3>(values[particleIdx]);
251  }
252 
253  virtual void accumulate(Storage& storage, const Dynamic value, const Size particleIdx) const override {
254  Array<TValue>& array = storage.getValue<TValue>(QuantityId::DAMAGE);
255  array.resize(particleIdx + 1);
256  array[particleIdx] = root<3>(value.get<TValue>());
257  }
258 
259  virtual std::string getName() const override {
260  return "Damage";
261  }
262 
263  virtual ValueEnum getType() const override {
264  return ValueEnum::SCALAR;
265  }
266 };
267 
270 public:
271  virtual Dynamic evaluate(const Storage& UNUSED(storage),
272  const Statistics& UNUSED(stats),
273  const Size particleIdx) const override {
274  return particleIdx;
275  }
276 
277  virtual void accumulate(Storage&, const Dynamic, const Size) const override {
278  // do nothing
279  }
280 
281  virtual std::string getName() const override {
282  return "Particle index";
283  }
284 
285  virtual ValueEnum getType() const override {
286  return ValueEnum::INDEX;
287  }
288 };
289 
291 class TimeColumn : public ITextColumn {
292 public:
293  virtual Dynamic evaluate(const Storage& UNUSED(storage),
294  const Statistics& stats,
295  const Size UNUSED(particleIdx)) const override {
296  return stats.get<Float>(StatisticsId::RUN_TIME);
297  }
298 
299  virtual void accumulate(Storage&, const Dynamic, const Size) const override {
300  // do nothing
301  }
302 
303  virtual std::string getName() const override {
304  return "Time";
305  }
306 
307  virtual ValueEnum getType() const override {
308  return ValueEnum::SCALAR;
309  }
310 };
311 
312 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Object holding a single values of various types.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr INLINE Float pow< 3 >(const Float v)
Definition: MathUtils.h:132
INLINE Float root< 3 >(const Float f)
Definition: MathUtils.h:105
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
ValueEnum
QuantityMetadata getMetadata(const QuantityId key)
Returns the quantity information using quantity ID.
Definition: QuantityIds.cpp:27
Quantity identifiers.
QuantityId
Unique IDs of basic quantities of SPH particles.
Definition: QuantityIds.h:19
@ DAMAGE
Damage.
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
Holder of quantity values and their temporal derivatives.
@ SECOND
Quantity with 1st and 2nd derivative.
@ FIRST
Quantity with 1st derivative.
@ ZERO
Quantity without derivatives, or "zero order" of quantity.
Statistics gathered and periodically displayed during the run.
@ RUN_TIME
Current time of the simulation in code units. Does not necessarily have to be 0 when run starts.
Container for storing particle quantities and materials.
BasicVector< Float > Vector
Definition: Vector.h:539
@ H
Definition: Vector.h:25
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE TCounter size() const
Definition: ArrayView.h:101
void resize(const TCounter newSize)
Resizes the array to new size.
Definition: Array.h:215
Prints actual values of scalar damage.
Definition: Column.h:240
virtual Dynamic evaluate(const Storage &storage, const Statistics &UNUSED(stats), const Size particleIdx) const override
Definition: Column.h:242
virtual ValueEnum getType() const override
Returns the value type of the column.
Definition: Column.h:263
virtual std::string getName() const override
Returns a name of the column.
Definition: Column.h:259
virtual void accumulate(Storage &storage, const Dynamic value, const Size particleIdx) const override
Reads the value of the column and saves it into the storage, if possible.
Definition: Column.h:253
Returns first derivatives of given quantity as stored in storage.
Definition: Column.h:115
virtual ValueEnum getType() const override
Returns the value type of the column.
Definition: Column.h:152
virtual std::string getName() const override
Returns a name of the column.
Definition: Column.h:148
virtual void accumulate(Storage &storage, const Dynamic value, const Size particleIdx) const override
Reads the value of the column and saves it into the storage, if possible.
Definition: Column.h:134
virtual Dynamic evaluate(const Storage &storage, const Statistics &UNUSED(stats), const Size particleIdx) const override
Definition: Column.h:123
DerivativeColumn(const QuantityId id)
Definition: Column.h:120
Convenient object for storing a single value of different types.
Definition: Dynamic.h:35
INLINE T & get()
Returns the reference to the stored value given its type.
Definition: Dynamic.h:88
Generic exception.
Definition: Exceptions.h:10
Base class for conversion of quantities into the output data.
Definition: Column.h:30
virtual std::string getName() const =0
Returns a name of the column.
virtual ValueEnum getType() const =0
Returns the value type of the column.
virtual Dynamic evaluate(const Storage &storage, const Statistics &stats, const Size particleIdx) const =0
Returns the value of the output column for given particle.
virtual void accumulate(Storage &storage, const Dynamic value, const Size particleIdx) const =0
Reads the value of the column and saves it into the storage, if possible.
Helper column printing particle numbers.
Definition: Column.h:269
virtual std::string getName() const override
Returns a name of the column.
Definition: Column.h:281
virtual ValueEnum getType() const override
Returns the value type of the column.
Definition: Column.h:285
virtual Dynamic evaluate(const Storage &UNUSED(storage), const Statistics &UNUSED(stats), const Size particleIdx) const override
Definition: Column.h:271
virtual void accumulate(Storage &, const Dynamic, const Size) const override
Reads the value of the column and saves it into the storage, if possible.
Definition: Column.h:277
void setOrder(const OrderEnum order)
Definition: Quantity.h:297
OrderEnum getOrderEnum() const
Returns the order of the quantity.
Definition: Quantity.h:241
Returns second derivatives of given quantity as stored in storage.
Definition: Column.h:161
virtual void accumulate(Storage &storage, const Dynamic value, const Size particleIdx) const override
Reads the value of the column and saves it into the storage, if possible.
Definition: Column.h:180
virtual ValueEnum getType() const override
Returns the value type of the column.
Definition: Column.h:198
virtual Dynamic evaluate(const Storage &storage, const Statistics &UNUSED(stats), const Size particleIdx) const override
Definition: Column.h:169
virtual std::string getName() const override
Returns a name of the column.
Definition: Column.h:194
SecondDerivativeColumn(const QuantityId id)
Definition: Column.h:166
Returns smoothing lengths of particles.
Definition: Column.h:204
virtual std::string getName() const override
Returns a name of the column.
Definition: Column.h:227
virtual Dynamic evaluate(const Storage &storage, const Statistics &UNUSED(stats), const Size particleIdx) const override
Definition: Column.h:206
virtual void accumulate(Storage &storage, const Dynamic value, const Size particleIdx) const override
Reads the value of the column and saves it into the storage, if possible.
Definition: Column.h:217
virtual ValueEnum getType() const override
Returns the value type of the column.
Definition: Column.h:231
Object holding various statistics about current run.
Definition: Statistics.h:22
TValue get(const StatisticsId idx) const
Returns value of a statistic.
Definition: Statistics.h:88
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Quantity & getQuantity(const QuantityId key)
Retrieves quantity with given key from the storage.
Definition: Storage.cpp:150
Quantity & insert(const QuantityId key, const OrderEnum order, const TValue &defaultValue)
Creates a quantity in the storage, given its key, value type and order.
Definition: Storage.cpp:270
Array< TValue > & getDt(const QuantityId key)
Retrieves a quantity derivative from the storage, given its key and value type.
Definition: Storage.cpp:217
Array< TValue > & getD2t(const QuantityId key)
Retrieves a quantity second derivative from the storage, given its key and value type.
Definition: Storage.cpp:243
bool has(const QuantityId key) const
Checks if the storage contains quantity with given key.
Definition: Storage.cpp:130
Array< TValue > & getValue(const QuantityId key)
Retrieves a quantity values from the storage, given its key and value type.
Definition: Storage.cpp:191
Helper column printing current run time. This value is the same for every particle.
Definition: Column.h:291
virtual std::string getName() const override
Returns a name of the column.
Definition: Column.h:303
virtual void accumulate(Storage &, const Dynamic, const Size) const override
Reads the value of the column and saves it into the storage, if possible.
Definition: Column.h:299
virtual ValueEnum getType() const override
Returns the value type of the column.
Definition: Column.h:307
virtual Dynamic evaluate(const Storage &UNUSED(storage), const Statistics &stats, const Size UNUSED(particleIdx)) const override
Definition: Column.h:293
Returns values of given quantity as stored in storage.
Definition: Column.h:73
virtual void accumulate(Storage &storage, const Dynamic value, const Size particleIdx) const override
Reads the value of the column and saves it into the storage, if possible.
Definition: Column.h:92
virtual Dynamic evaluate(const Storage &storage, const Statistics &UNUSED(stats), const Size particleIdx) const override
Definition: Column.h:81
ValueColumn(const QuantityId id)
Definition: Column.h:78
virtual ValueEnum getType() const override
Returns the value type of the column.
Definition: Column.h:106
virtual std::string getName() const override
Returns a name of the column.
Definition: Column.h:102
Convert type to ValueType enum.
Base class for all polymorphic objects.
Definition: Object.h:88
std::string derivativeName
Name of the 1st derivative.
Definition: QuantityIds.h:248
std::string quantityName
Full name of the quantity (i.e. 'Density', 'Deviatoric stress', ...)
Definition: QuantityIds.h:242
std::string secondDerivativeName
Name of the second derivative.
Definition: QuantityIds.h:253