SPH
Storage.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/ForwardDecl.h"
9 #include "objects/Exceptions.h"
12 #include "objects/wrappers/Flags.h"
16 #include "quantities/QuantityIds.h"
17 
19 
20 class IMaterial;
21 class Quantity;
22 class Box;
23 class StorageSequence;
25 enum class OrderEnum;
26 enum class VisitorEnum;
27 
28 
32 };
33 
37 };
38 
41 private:
43 
44  ActIterator iter;
45 
46 public:
48 
50 
52 
53  bool operator==(const StorageIterator& other) const;
54 
55  bool operator!=(const StorageIterator& other) const;
56 };
57 
60 private:
62 
63  ActIterator iter;
64 
65 public:
67 
69 
71 
72  bool operator==(const ConstStorageIterator& other) const;
73 
74  bool operator!=(const ConstStorageIterator& other) const;
75 };
76 
80 private:
82 
83 public:
85 
91 
94 
96  Size size() const;
97 };
98 
102 private:
103  const FlatMap<QuantityId, Quantity>& quantities;
104 
105 public:
107 
113 
116 
118  Size size() const;
119 };
120 
122 class IStorageUserData : public Polymorphic {};
123 
126 public:
127  explicit InvalidStorageAccess(const QuantityId id);
128 
129  explicit InvalidStorageAccess(const std::string& message);
130 };
131 
230 class Storage : public Noncopyable {
231 private:
234 
236  struct MatRange {
238  SharedPtr<IMaterial> material;
239 
241  Size from = 0;
242 
244  Size to = 0;
245 
246  MatRange() = default;
247 
248  MatRange(const SharedPtr<IMaterial>& material, const Size from, const Size to);
249  };
250 
255  Array<MatRange> mats;
256 
260  ArrayView<Size> matIds;
261 
265  Array<WeakPtr<Storage>> dependent;
266 
271 
272 public:
277 
282  explicit Storage(const SharedPtr<IMaterial>& material);
283 
285 
286  Storage(Storage&& other);
287 
288  Storage& operator=(Storage&& other);
289 
293  bool has(const QuantityId key) const;
294 
296  template <typename TValue>
297  bool has(const QuantityId key, const OrderEnum order) const;
298 
302  Quantity& getQuantity(const QuantityId key);
303 
305  const Quantity& getQuantity(const QuantityId key) const;
306 
312  template <typename TValue>
314 
316  template <typename TValue>
317  StaticArray<const Array<TValue>&, 3> getAll(const QuantityId key) const;
318 
324  template <typename TValue>
325  Array<TValue>& getValue(const QuantityId key);
326 
328  template <typename TValue>
329  const Array<TValue>& getValue(const QuantityId key) const;
330 
336  template <typename TValue>
337  Array<TValue>& getDt(const QuantityId key);
338 
340  template <typename TValue>
341  const Array<TValue>& getDt(const QuantityId key) const;
342 
348  template <typename TValue>
349  Array<TValue>& getD2t(const QuantityId key);
350 
352  template <typename TValue>
353  const Array<TValue>& getD2t(const QuantityId key) const;
354 
358  template <typename TValue, typename... TArgs>
359  auto getValues(const QuantityId first, const QuantityId second, const TArgs... others) {
360  return tie(getValue<TValue>(first), getValue<TValue>(second), getValue<TValue>(others)...);
361  }
362 
364  template <typename TValue, typename... TArgs>
365  auto getValues(const QuantityId first, const QuantityId second, const TArgs... others) const {
366  // better not const_cast here as we are deducing return type
367  return tie(getValue<TValue>(first), getValue<TValue>(second), getValue<TValue>(others)...);
368  }
369 
383  template <typename TValue>
384  Quantity& insert(const QuantityId key, const OrderEnum order, const TValue& defaultValue);
385 
394  template <typename TValue>
395  Quantity& insert(const QuantityId key, const OrderEnum order, Array<TValue>&& values);
396 
406  void addDependent(const WeakPtr<Storage>& other);
407 
413  MaterialView getMaterial(const Size matIdx) const;
414 
416  MaterialView getMaterialOfParticle(const Size particleIdx) const;
417 
421  void setMaterial(const Size matIdx, const SharedPtr<IMaterial>& material);
422 
427  Interval getRange(const QuantityId id, const Size matIdx) const;
428 
432  template <typename TValue>
434 
438  bool isHomogeneous(const BodySettingsId param) const;
439 
442 
445 
451  void propagate(const Function<void(Storage& storage)>& functor);
452 
456  Size getMaterialCnt() const;
457 
459  Size getQuantityCnt() const;
460 
464  Size getParticleCnt() const;
465 
467  bool empty() const;
468 
480  void merge(Storage&& other);
481 
485  void zeroHighestDerivatives();
486 
487  enum class IndicesFlag {
489  INDICES_SORTED = 1 << 0,
490  };
491 
503 
510 
514  void removeAll();
515 
523  Storage clone(const Flags<VisitorEnum> buffers) const;
524 
526  enum class ResizeFlag {
528  KEEP_EMPTY_UNCHANGED = 1 << 0,
529  };
530 
540  void resize(const Size newParticleCnt, const Flags<ResizeFlag> flags = EMPTY_FLAGS);
541 
545  void swap(Storage& other, const Flags<VisitorEnum> flags);
546 
547  enum class ValidFlag {
549  COMPLETE = 1 << 0,
550  };
551 
558 
563 
568 
569 private:
573  void addMissingBuffers(const Storage& source);
574 
576  void update();
577 };
578 
582 Box getBoundingBox(const Storage& storage, const Float radius = 2._f);
583 
588 Vector getCenterOfMass(const Storage& storage);
589 
600 void setPersistentIndices(Storage& storage);
601 
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
const float radius
Definition: CurveDialog.cpp:18
Wrapper over enum allowing setting (and querying) individual bits of the stored value.
const EmptyFlags EMPTY_FLAGS
Definition: Flags.h:16
Key-value associative container implemented as a sorted array.
Generic wrappers of lambdas, functors and other callables.
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
#define NAMESPACE_SPH_END
Definition: Object.h:12
Return value of function that may fail, containing either SUCCEES (true) or error message.
Quantity identifiers.
QuantityId
Unique IDs of basic quantities of SPH particles.
Definition: QuantityIds.h:19
VisitorEnum
Types of iteration over storage.
Definition: Quantity.h:23
OrderEnum
Definition: Quantity.h:15
StaticArray< T0 &, sizeof...(TArgs)+1 > tie(T0 &t0, TArgs &... rest)
Creates a static array from a list of l-value references.
Definition: StaticArray.h:281
Vector getCenterOfMass(const Storage &storage)
Returns the center of mass of all particles.
Definition: Storage.cpp:842
void setPersistentIndices(Storage &storage)
Adds or updates a quantity holding particle indices to the storage.
Definition: Storage.cpp:865
Box getBoundingBox(const Storage &storage, const Float radius=2._f)
Convenience function to get the bounding box of all particles.
Definition: Storage.cpp:832
Helper class used to allow calling a function only from within T.
Definition: Object.h:94
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
Helper class for iterating over quantities stored in Storage, const version.
Definition: Storage.h:59
bool operator!=(const ConstStorageIterator &other) const
Definition: Storage.cpp:47
bool operator==(const ConstStorageIterator &other) const
Definition: Storage.cpp:43
ConstStorageIterator & operator++()
Definition: Storage.cpp:34
ConstStorageElement operator*()
Definition: Storage.cpp:39
ConstStorageIterator(const ActIterator iterator, Badge< ConstStorageSequence >)
Definition: Storage.cpp:31
Helper class, provides functions begin and end, returning const iterators to the first and last quant...
Definition: Storage.h:101
ConstStorageSequence(const FlatMap< QuantityId, Quantity > &quantities, Badge< Storage >)
Definition: Storage.cpp:66
Size size() const
Returns the number of quantities.
Definition: Storage.cpp:77
ConstStorageIterator end()
Returns iterator pointing to the one-past-the-end element of the quantity storage.
Definition: Storage.cpp:73
ConstStorageIterator begin()
Returns iterator pointing to the beginning of the quantity storage.
Definition: Storage.cpp:69
Generic exception.
Definition: Exceptions.h:10
Wrapper of an integral value providing functions for reading and modifying individual bits.
Definition: Flags.h:20
Material settings and functions specific for one material.
Definition: IMaterial.h:110
Base class for arbitrary data stored in the storage alongside particles.
Definition: Storage.h:122
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
Exception thrown when accessing missing quantities, casting to different types, etc.
Definition: Storage.h:125
InvalidStorageAccess(const QuantityId id)
Definition: Storage.cpp:81
Non-owning wrapper of a material and particles with this material.
Definition: IMaterial.h:30
Generic container for storing scalar, vector or tensor quantity and its derivatives.
Definition: Quantity.h:200
Array with fixed number of allocated elements.
Definition: StaticArray.h:19
Helper class for iterating over quantities stored in Storage.
Definition: Storage.h:40
StorageIterator & operator++()
Definition: Storage.cpp:14
bool operator!=(const StorageIterator &other) const
Definition: Storage.cpp:27
StorageIterator(const ActIterator iterator, Badge< StorageSequence >)
Definition: Storage.cpp:11
StorageElement operator*()
Definition: Storage.cpp:19
bool operator==(const StorageIterator &other) const
Definition: Storage.cpp:23
Helper class, provides functions begin and end, returning iterators to the first and last quantity in...
Definition: Storage.h:79
Size size() const
Returns the number of quantities.
Definition: Storage.cpp:62
StorageSequence(FlatMap< QuantityId, Quantity > &quantities, Badge< Storage >)
Definition: Storage.cpp:51
StorageIterator end()
Returns iterator pointing to the one-past-the-end element of the quantity storage.
Definition: Storage.cpp:58
StorageIterator begin()
Returns iterator pointing to the beginning of the quantity storage.
Definition: Storage.cpp:54
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Size getMaterialCnt() const
Return the number of materials in the storage.
Definition: Storage.cpp:437
Array< TValue > getMaterialParams(const BodySettingsId param) const
Returns the given material parameter for all materials in the storage.
Definition: Storage.cpp:408
bool isHomogeneous(const BodySettingsId param) const
Checks if the particles in the storage are homogeneous with respect to given parameter.
Definition: Storage.cpp:386
void merge(Storage &&other)
Merges another storage into this object.
Definition: Storage.cpp:472
Quantity & getQuantity(const QuantityId key)
Retrieves quantity with given key from the storage.
Definition: Storage.cpp:150
void resize(const Size newParticleCnt, const Flags< ResizeFlag > flags=EMPTY_FLAGS)
Changes number of particles for all quantities stored in the storage.
Definition: Storage.cpp:579
Outcome isValid(const Flags< ValidFlag > flags=ValidFlag::COMPLETE) const
Checks whether the storage is in valid state.
Definition: Storage.cpp:609
void setMaterial(const Size matIdx, const SharedPtr< IMaterial > &material)
Modifies material with given index.
Definition: Storage.cpp:377
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
auto getValues(const QuantityId first, const QuantityId second, const TArgs... others) const
Retrieves an array of quantities from the key, const version.
Definition: Storage.h:365
ValidFlag
Definition: Storage.h:547
@ COMPLETE
Checks that the storage is complete, i.e. there are no empty buffers.
MaterialView getMaterialOfParticle(const Size particleIdx) const
Returns material view for material of given particle.
Definition: Storage.cpp:372
bool empty() const
Checks if the storage is empty, i.e. without particles.
Definition: Storage.cpp:453
void addDependent(const WeakPtr< Storage > &other)
Registers a dependent storage.
Definition: Storage.cpp:339
StaticArray< Array< TValue > &, 3 > getAll(const QuantityId key)
Retrieves quantity buffers from the storage, given its key and value type.
Definition: Storage.cpp:163
void swap(Storage &other, const Flags< VisitorEnum > flags)
Swap quantities or given subset of quantities between two storages.
Definition: Storage.cpp:602
Storage()
Creates a storage with no material.
Array< TValue > & getDt(const QuantityId key)
Retrieves a quantity derivative from the storage, given its key and value type.
Definition: Storage.cpp:217
void removeAll()
Removes all particles with all quantities (including materials) from the storage.
Definition: Storage.cpp:810
void setUserData(SharedPtr< IStorageUserData > newData)
Stores new user data into the storage.
Definition: Storage.cpp:824
Size getParticleCnt() const
Returns the number of particles.
Definition: Storage.cpp:445
StorageSequence getQuantities()
Returns the sequence of quantities.
Definition: Storage.cpp:416
auto getValues(const QuantityId first, const QuantityId second, const TArgs... others)
Retrieves an array of quantities from the key.
Definition: Storage.h:359
Size getQuantityCnt() const
Returns the number of stored quantities.
Definition: Storage.cpp:441
void propagate(const Function< void(Storage &storage)> &functor)
Executes a given functor recursively for all dependent storages.
Definition: Storage.cpp:424
Array< Size > duplicate(ArrayView< const Size > idxs, const Flags< IndicesFlag > flags=EMPTY_FLAGS)
Duplicates some particles in the storage.
Definition: Storage.cpp:674
SharedPtr< IStorageUserData > getUserData() const
Returns the stored user data.
Definition: Storage.cpp:828
Interval getRange(const QuantityId id, const Size matIdx) const
Returns the bounding range of given quantity.
Definition: Storage.cpp:402
IndicesFlag
Definition: Storage.h:487
@ INDICES_SORTED
Use if the given array is already sorted (optimization)
void remove(ArrayView< const Size > idxs, const Flags< IndicesFlag > flags=EMPTY_FLAGS)
Removes specified particles from the storage.
Definition: Storage.cpp:756
Storage & operator=(Storage &&other)
Definition: Storage.cpp:118
Array< TValue > & getD2t(const QuantityId key)
Retrieves a quantity second derivative from the storage, given its key and value type.
Definition: Storage.cpp:243
MaterialView getMaterial(const Size matIdx) const
Returns an object containing a reference to given material.
Definition: Storage.cpp:366
bool has(const QuantityId key) const
Checks if the storage contains quantity with given key.
Definition: Storage.cpp:130
ResizeFlag
Options for the storage resize.
Definition: Storage.h:526
@ KEEP_EMPTY_UNCHANGED
Empty buffers will not be resized to new values.
void zeroHighestDerivatives()
Sets all highest-level derivatives of quantities to zero.
Definition: Storage.cpp:556
Storage clone(const Flags< VisitorEnum > buffers) const
Clones specified buffers of the storage.
Definition: Storage.cpp:563
Array< TValue > & getValue(const QuantityId key)
Retrieves a quantity values from the storage, given its key and value type.
Definition: Storage.cpp:191
BodySettingsId
Settings of a single body / gas phase / ...
Definition: Settings.h:1394
QuantityId id
Definition: Storage.h:35
const Quantity & quantity
Definition: Storage.h:36
Object with deleted copy constructor and copy operator.
Definition: Object.h:54
Base class for all polymorphic objects.
Definition: Object.h:88
Quantity & quantity
Definition: Storage.h:31
QuantityId id
Definition: Storage.h:30