SPH
CachedGravity.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "gravity/IGravity.h"
9 #include "system/Settings.h"
10 #include "system/Statistics.h"
11 
13 
20 class CachedGravity : public IGravity {
21 private:
22  AutoPtr<IGravity> gravity;
23  Float period;
24 
25  mutable Array<Vector> cachedDv;
26  mutable Float t_last = -INFTY;
27 
28 public:
34  explicit CachedGravity(const Float recomputationPeriod, AutoPtr<IGravity>&& gravity)
35  : gravity(std::move(gravity)) {
36  period = recomputationPeriod;
37  SPH_ASSERT(period > 0._f);
38  SPH_ASSERT(this->gravity);
39  }
40 
41  virtual void build(IScheduler& scheduler, const Storage& storage) override {
42  // here we have no information about the time, so we must build the gravity every time step; that's
43  // fine as long as building time is significantly lower than the evaluation time.
44  gravity->build(scheduler, storage);
45  }
46 
47  virtual void evalAll(IScheduler& scheduler, ArrayView<Vector> dv, Statistics& stats) const override {
48  const Float t = stats.get<Float>(StatisticsId::RUN_TIME);
49  SPH_ASSERT(t >= t_last);
50  if (dv.size() == cachedDv.size() && t - t_last < period) {
51  // we can re-use the cached accelerations
53  } else {
54  // recompute and cache gravity
55  cachedDv.resize(dv.size());
56  cachedDv.fill(Vector(0._f));
57  gravity->evalAll(scheduler, cachedDv, stats);
58  t_last = t;
59  }
60 
61  // note that dv might already contain some accelerations, thus sum, not assign!
62  for (Size i = 0; i < dv.size(); ++i) {
63  dv[i] += cachedDv[i];
64  }
65  }
66 
67  virtual Vector eval(const Vector& r0) const override {
68  // we could cache this as well, but the function is mainly used for testing and some utilities where
69  // performance does not matter, so it's probably not worth it.
70  return gravity->eval(r0);
71  }
72 
73  virtual Float evalEnergy(IScheduler& scheduler, Statistics& stats) const override {
74  return gravity->evalEnergy(scheduler, stats);
75  }
76 
77  virtual RawPtr<const IBasicFinder> getFinder() const override {
78  return gravity->getFinder();
79  }
80 };
81 
#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.
constexpr Float INFTY
Definition: MathUtils.h:38
#define NAMESPACE_SPH_END
Definition: Object.h:12
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.
@ GRAVITY_EVAL_TIME
Wallclock duration of gravity evaluation.
BasicVector< Float > Vector
Definition: Vector.h:539
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE TCounter size() const
Definition: ArrayView.h:101
Generic dynamically allocated resizable storage.
Definition: Array.h:43
void resize(const TCounter newSize)
Resizes the array to new size.
Definition: Array.h:215
void fill(const T &t)
Sets all elements of the array to given value.
Definition: Array.h:187
INLINE TCounter size() const noexcept
Definition: Array.h:193
Wrapper of other IGravity implementation that approximates the gravity using cached values.
Definition: CachedGravity.h:20
virtual Float evalEnergy(IScheduler &scheduler, Statistics &stats) const override
Computes the total potential energy of the particles.
Definition: CachedGravity.h:73
virtual void build(IScheduler &scheduler, const Storage &storage) override
Builds the accelerating structure.
Definition: CachedGravity.h:41
virtual void evalAll(IScheduler &scheduler, ArrayView< Vector > dv, Statistics &stats) const override
Evaluates the gravitational acceleration concurrently.
Definition: CachedGravity.h:47
CachedGravity(const Float recomputationPeriod, AutoPtr< IGravity > &&gravity)
Creates the cached gravity.
Definition: CachedGravity.h:34
virtual RawPtr< const IBasicFinder > getFinder() const override
Optionally returns a finder used by the gravity implementation.
Definition: CachedGravity.h:77
virtual Vector eval(const Vector &r0) const override
Evaluates the gravitational acceleration at given point.
Definition: CachedGravity.h:67
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
TValue get(const StatisticsId idx) const
Returns value of a statistic.
Definition: Statistics.h:88
Statistics & set(const StatisticsId idx, TValue &&value)
Sets new values of a statistic.
Definition: Statistics.h:52
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Generic storage and input/output routines of settings.
Overload of std::swap for Sph::Array.
Definition: Array.h:578