SPH
SymmetricGravity.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "gravity/IGravity.h"
9 #include "quantities/Quantity.h"
10 #include "quantities/Storage.h"
11 #include "system/Settings.h"
12 #include "system/Statistics.h"
13 
15 
16 class SymmetricGravity : public IGravity {
17 private:
18  AutoPtr<IGravity> gravity;
19  mutable Storage all;
20  Array<Size> idxs;
21 
22 public:
23  explicit SymmetricGravity(AutoPtr<IGravity>&& gravity)
24  : gravity(std::move(gravity)) {
25  SPH_ASSERT(this->gravity);
26  }
27 
28  virtual void build(IScheduler& scheduler, const Storage& storage) override {
29  if (all.empty()) {
30  // lazy init
31  Array<Vector> r(2 * storage.getParticleCnt());
32  all.insert<Vector>(QuantityId::POSITION, OrderEnum::SECOND, std::move(r));
33  all.insert<Float>(QuantityId::MASS, OrderEnum::ZERO, 0._f);
34  }
35 
38  Array<Vector>& r_a = all.getValue<Vector>(QuantityId::POSITION);
39  Array<Float>& m_a = all.getValue<Float>(QuantityId::MASS);
40  r_a.clear();
41  m_a.clear();
42  idxs.clear();
43 
44  for (Size i = 0; i < r.size(); ++i) {
45  if (r[i][Z] <= 0._f) {
46  // this is the ghost particle created by the boundary conditions
47  continue;
48  }
49  r_a.push(r[i]);
50  r_a.push(r[i] - 2._f * Vector(0, 0, r[i][Z]));
51  m_a.push(m[i]);
52  m_a.push(m[i]);
53  idxs.push(i);
54  idxs.push(Size(-1));
55  }
56 
57  gravity->build(scheduler, all);
58 
60  dv.resize(r_a.size());
61  dv.fill(Vector(0._f));
62  }
63 
64  virtual void evalAll(IScheduler& scheduler, ArrayView<Vector> dv, Statistics& stats) const override {
66  gravity->evalAll(scheduler, dv_a, stats);
67 
68  for (Size i = 0; i < idxs.size(); ++i) {
69  if (idxs[i] == Size(-1)) {
70  // duplicate
71  continue;
72  }
73  dv[idxs[i]] += dv_a[i];
74  }
75  }
76 
77  virtual Vector eval(const Vector& r0) const override {
78  return gravity->eval(r0);
79  }
80 
81  virtual Float evalEnergy(IScheduler& scheduler, Statistics& stats) const override {
82  return gravity->evalEnergy(scheduler, stats);
83  }
84 
85  virtual RawPtr<const IBasicFinder> getFinder() const override {
86  // the tree is built for a different set of particles
87  return nullptr;
88  }
89 };
90 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
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
Base class for solvers of gravity.
INLINE bool all(const Indices &i)
Definition: Indices.h:144
#define NAMESPACE_SPH_END
Definition: Object.h:12
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ MASS
Paricles masses, always a scalar quantity.
Holder of quantity values and their temporal derivatives.
@ SECOND
Quantity with 1st and 2nd derivative.
@ ZERO
Quantity without derivatives, or "zero order" of quantity.
Statistics gathered and periodically displayed during the run.
Container for storing particle quantities and materials.
BasicVector< Float > Vector
Definition: Vector.h:539
@ Z
Definition: Vector.h:24
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
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
void fill(const T &t)
Sets all elements of the array to given value.
Definition: Array.h:187
void clear()
Removes all elements from the array, but does NOT release the memory.
Definition: Array.h:434
INLINE TCounter size() const noexcept
Definition: Array.h:193
Interface for computing gravitational interactions of particles.
Definition: IGravity.h:14
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Non-owning wrapper of pointer.
Definition: RawPtr.h:19
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Size getParticleCnt() const
Returns the number of particles.
Definition: Storage.cpp:445
Array< TValue > & getValue(const QuantityId key)
Retrieves a quantity values from the storage, given its key and value type.
Definition: Storage.cpp:191
virtual void build(IScheduler &scheduler, const Storage &storage) override
Builds the accelerating structure.
virtual Float evalEnergy(IScheduler &scheduler, Statistics &stats) const override
Computes the total potential energy of the particles.
virtual Vector eval(const Vector &r0) const override
Evaluates the gravitational acceleration at given point.
SymmetricGravity(AutoPtr< IGravity > &&gravity)
virtual RawPtr< const IBasicFinder > getFinder() const override
Optionally returns a finder used by the gravity implementation.
virtual void evalAll(IScheduler &scheduler, ArrayView< Vector > dv, Statistics &stats) const override
Evaluates the gravitational acceleration concurrently.
Generic storage and input/output routines of settings.
Overload of std::swap for Sph::Array.
Definition: Array.h:578