SPH
Particle.cpp
Go to the documentation of this file.
1 #include "quantities/Particle.h"
2 #include "quantities/Quantity.h"
3 #include "quantities/Storage.h"
4 
6 
9 
10  template <typename TValue>
11  void visit(const QuantityId id, const Quantity& q, const Size idx) {
12  const auto& values = q.getAll<TValue>();
13  switch (q.getOrderEnum()) {
14  case OrderEnum::SECOND:
15  data[id].d2t = values[2][idx];
17  case OrderEnum::FIRST:
18  data[id].dt = values[1][idx];
20  case OrderEnum::ZERO:
21  data[id].value = values[0][idx];
22  break;
23  default:
25  }
26  }
27 };
28 
29 
30 Particle::Particle(const Storage& storage, const Size idx)
31  : idx(idx) {
32  for (ConstStorageElement i : storage.getQuantities()) {
33  quantities.insert(i.id, InternalQuantityData{});
34 
35  ParticleVisitor visitor{ quantities };
36  dispatch(i.quantity.getValueEnum(), visitor, i.id, i.quantity, idx);
37  }
38 }
39 
40 Particle::Particle(const QuantityId id, const Dynamic& value, const Size idx)
41  : idx(idx) {
42  quantities.insert(id, InternalQuantityData{});
43  quantities[id].value = value;
44 }
45 
47  *this = other;
48 }
49 
51  *this = std::move(other);
52 }
53 
55  quantities = other.quantities.clone();
56  material = other.material.clone();
57  idx = other.idx;
58  return *this;
59 }
60 
62  quantities = std::move(other.quantities);
63  material = std::move(other.material);
64  idx = other.idx;
65  return *this;
66 }
67 
68 Particle& Particle::addValue(const QuantityId id, const Dynamic& value) {
69  if (!quantities.contains(id)) {
70  quantities.insert(id, InternalQuantityData{});
71  }
72  quantities[id].value = value;
73  return *this;
74 }
75 
76 Particle& Particle::addDt(const QuantityId id, const Dynamic& value) {
77  if (!quantities.contains(id)) {
78  quantities.insert(id, InternalQuantityData{});
79  }
80  quantities[id].dt = value;
81  return *this;
82 }
83 
84 Particle& Particle::addD2t(const QuantityId id, const Dynamic& value) {
85  if (!quantities.contains(id)) {
86  quantities.insert(id, InternalQuantityData{});
87  }
88  quantities[id].d2t = value;
89  return *this;
90 }
91 
93  if (!material.contains(id)) {
94  material.insert(id, NOTHING);
95  }
96  material[id] = value;
97  return *this;
98 }
99 
100 
102  Optional<const InternalQuantityData&> quantity = quantities.tryGet(id);
103  SPH_ASSERT(quantity);
104  return quantity->value;
105 }
106 
108  Optional<const InternalQuantityData&> quantity = quantities.tryGet(id);
109  SPH_ASSERT(quantity);
110  return quantity->dt;
111 }
112 
114  Optional<const InternalQuantityData&> quantity = quantities.tryGet(id);
115  SPH_ASSERT(quantity);
116  return quantity->d2t;
117 }
118 
120  Optional<const Dynamic&> value = material.tryGet(id);
121  SPH_ASSERT(value);
122  return value.value();
123 }
124 
125 
127  : iter(iterator) {}
128 
130  ++iter;
131  return *this;
132 }
133 
135  const InternalQuantityData& internal = iter->value;
136  DynamicId type;
137  if (internal.value) {
138  type = internal.value.getType();
139  SPH_ASSERT(internal.dt.empty() || internal.dt.getType() == type);
140  SPH_ASSERT(internal.d2t.empty() || internal.d2t.getType() == type);
141  } else if (internal.dt) {
142  type = internal.dt.getType();
143  SPH_ASSERT(internal.d2t.empty() || internal.d2t.getType() == type);
144  } else {
145  SPH_ASSERT(internal.d2t);
146  type = internal.d2t.getType();
147  }
148  return { iter->key, type, internal.value, internal.dt, internal.d2t };
149 }
150 
152  return iter != other.iter;
153 }
154 
156  : first(particle.quantities.begin(), {})
157  , last(particle.quantities.end(), {}) {}
158 
160  return first;
161 }
162 
164  return last;
165 }
166 
168  return QuantitySequence(*this);
169 }
170 
171 
173  : iter(iterator) {}
174 
176  ++iter;
177  return *this;
178 }
179 
181  return { iter->key, iter->value };
182 }
183 
185  return iter != other.iter;
186 }
187 
188 
190  : first(particle.material.begin(), {})
191  , last(particle.material.end(), {}) {}
192 
194  return first;
195 }
196 
198  return last;
199 }
200 
202  return ParamSequence(*this);
203 }
204 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
DynamicId
Enum representing a value type stored in a Value object.
Definition: Dynamic.h:18
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
#define SPH_FALLTHROUGH
Definition: Object.h:44
#define NAMESPACE_SPH_END
Definition: Object.h:12
const NothingType NOTHING
Definition: Optional.h:16
decltype(auto) dispatch(const ValueEnum value, TVisitor &&visitor, TArgs &&... args)
Selects type based on run-time ValueEnum value and runs visit<Type>() method of the visitor.
QuantityId
Unique IDs of basic quantities of SPH particles.
Definition: QuantityIds.h:19
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.
Container for storing particle quantities and materials.
Helper class used to allow calling a function only from within T.
Definition: Object.h:94
Convenient object for storing a single value of different types.
Definition: Dynamic.h:35
DynamicId getType() const
Definition: Dynamic.h:115
FlatMap clone() const
Definition: FlatMap.h:182
INLINE bool contains(const TKey &key) const
Returns true if the map contains element of given key.
Definition: FlatMap.h:140
INLINE TValue & insert(const TKey &key, const TValue &value)
Adds a new element into the map or sets new value of element with the same key.
Definition: FlatMap.h:65
INLINE Optional< TValue & > tryGet(const TKey &key)
Returns a reference to the value matching the given key, or NOTHING if no such value exists.
Definition: FlatMap.h:118
Wrapper of type value of which may or may not be present.
Definition: Optional.h:23
INLINE Type & value()
Returns the reference to the stored value.
Definition: Optional.h:172
bool operator!=(const ParamIterator &other) const
Inequality operator.
Definition: Particle.cpp:184
ParamIterator & operator++()
Advances the iterator to next quantity.
Definition: Particle.cpp:175
ParamData operator*() const
Returns all data associated with currently referenced parameter.
Definition: Particle.cpp:180
ParamIterator(const ActIterator iterator, Badge< Particle >)
Constructs the iterator from internal type.
Definition: Particle.cpp:172
Helper for enumerating all stored parameters.
Definition: Particle.h:219
ParamIterator begin() const
Returns iterator pointing to the first parameter.
Definition: Particle.cpp:193
ParamSequence(const Particle &particle)
Creates a sequence of all material parameters of given particle.
Definition: Particle.cpp:189
ParamIterator end() const
Returns iterator pointing to the one-past-last parameter.
Definition: Particle.cpp:197
Iterator used to enumerate all stored quantities.
Definition: Particle.h:144
QuantityData operator*() const
Returns all data associated with currently referenced quantity.
Definition: Particle.cpp:134
bool operator!=(const QuantityIterator &other) const
Inequality operator.
Definition: Particle.cpp:151
QuantityIterator(const ActIterator iterator, Badge< Particle >)
Constructs the iterator from internal type.
Definition: Particle.cpp:126
QuantityIterator & operator++()
Advances the iterator to next quantity.
Definition: Particle.cpp:129
Helper for enumerating all stored quantities.
Definition: Particle.h:167
QuantityIterator end() const
Returns iterator pointing to the one-past-last quantity.
Definition: Particle.cpp:163
QuantityIterator begin() const
Returns iterator pointing to the first quantity.
Definition: Particle.cpp:159
QuantitySequence(const Particle &particle)
Creates a sequence of all quantities of given particle.
Definition: Particle.cpp:155
Object holding information about single particle.
Definition: Particle.h:17
QuantitySequence getQuantities() const
Returns a range for enumerating all stored quantities.
Definition: Particle.cpp:167
Dynamic getDt(const QuantityId id) const
Retrieves a quantity derivative of the particle.
Definition: Particle.cpp:107
Particle()=default
Default constructor, defined only for convenient usage in containers, etc.
Dynamic getValue(const QuantityId id) const
Retrieves a quantity value of the particle.
Definition: Particle.cpp:101
Particle & operator=(const Particle &other)
Definition: Particle.cpp:54
Particle & addD2t(const QuantityId id, const Dynamic &value)
Adds another quantity 2nd derivative or updates the 2nd derivative of quantity previously stored.
Definition: Particle.cpp:84
Dynamic getParameter(const BodySettingsId id) const
Retrieves a material parameter of the particle.
Definition: Particle.cpp:119
Particle & addParameter(const BodySettingsId id, const Dynamic &value)
Adds another material parameter or updates the one stored previously.
Definition: Particle.cpp:92
Dynamic getD2t(const QuantityId id) const
Retrieves a quantity 2nd derivative of the particle.
Definition: Particle.cpp:113
Particle & addValue(const QuantityId id, const Dynamic &value)
Adds another quantity value or updates the value of quantity previously stored.
Definition: Particle.cpp:68
ParamSequence getParameters() const
Returns a range for enumerating all stored parameters.
Definition: Particle.cpp:201
Particle & addDt(const QuantityId id, const Dynamic &value)
Adds another quantity derivative or updates the derivative of quantity previously stored.
Definition: Particle.cpp:76
Generic container for storing scalar, vector or tensor quantity and its derivatives.
Definition: Quantity.h:200
OrderEnum getOrderEnum() const
Returns the order of the quantity.
Definition: Quantity.h:241
StaticArray< Array< TValue > &, 3 > getAll()
Returns all buffers of given type stored in this quantity.
Definition: Quantity.h:338
Container storing all quantities used within the simulations.
Definition: Storage.h:230
StorageSequence getQuantities()
Returns the sequence of quantities.
Definition: Storage.cpp:416
BodySettingsId
Settings of a single body / gas phase / ...
Definition: Settings.h:1394
void visit(const QuantityId id, const Quantity &q, const Size idx)
Definition: Particle.cpp:11
FlatMap< QuantityId, Particle::InternalQuantityData > & data
Definition: Particle.cpp:8
Stored info about a material parameter.
Definition: Particle.h:187
Dynamic value
Parameter value.
Definition: Particle.h:193
Stored info about a quantity.
Definition: Particle.h:125
Dynamic value
Quantity value.
Definition: Particle.h:134