SPH
HelperTerms.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "io/FileSystem.h"
9 #include "run/ScriptUtils.h"
12 
14 
17 private:
18  class Derivative : public DerivativeTemplate<Derivative> {
19  private:
20  ArrayView<Size> neighCnts;
21 
22  public:
23  explicit Derivative(const RunSettings& settings)
24  : DerivativeTemplate<Derivative>(settings) {}
25 
26  INLINE void additionalCreate(Accumulated& results) {
28  }
29 
30  INLINE void additionalInitialize(const Storage& UNUSED(input), Accumulated& results) {
32  }
33 
34  INLINE bool additionalEquals(const Derivative& UNUSED(other)) const {
35  return true;
36  }
37 
38  template <bool Symmetrize>
39  INLINE void eval(const Size i, const Size j, const Vector& UNUSED(grad)) {
40  // there is no need to use this in asymmetric solver, since we already know all the neighbours
41  SPH_ASSERT(Symmetrize);
42  neighCnts[i]++;
43  if (Symmetrize) {
44  neighCnts[j]++;
45  }
46  }
47  };
48 
49 public:
50  virtual void setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) override {
51  derivatives.require(makeAuto<Derivative>(settings));
52  }
53 
54  virtual void initialize(IScheduler& UNUSED(scheduler),
55  Storage& UNUSED(storage),
56  const Float UNUSED(t)) override {}
57 
58  virtual void finalize(IScheduler& UNUSED(scheduler),
59  Storage& UNUSED(storage),
60  const Float UNUSED(t)) override {}
61 
62  virtual void create(Storage& UNUSED(storage), IMaterial& UNUSED(material)) const override {}
63 };
64 
65 
70 class SurfaceNormal : public IEquationTerm {
71 private:
72  class Derivative : public DerivativeTemplate<Derivative> {
73  private:
76 
77  public:
78  explicit Derivative(const RunSettings& settings)
79  : DerivativeTemplate<Derivative>(settings) {}
80 
81  INLINE void additionalCreate(Accumulated& results) {
83  }
84 
85  INLINE void additionalInitialize(const Storage& input, Accumulated& results) {
88  }
89 
90  INLINE bool additionalEquals(const Derivative& UNUSED(other)) const {
91  return true;
92  }
93 
94  template <bool Symmetrize>
95  INLINE void eval(const Size i, const Size j, const Vector& UNUSED(grad)) {
96  const Vector dr = (r[j] - r[i]) / (r[i][H] + r[j][H]);
97  const Float length = getLength(dr);
98  if (length != 0) {
99  const Vector normalized = dr / length;
100  n[i] += normalized;
101  if (Symmetrize) {
102  n[j] -= normalized;
103  }
104  }
105  }
106  };
107 
108 public:
109  virtual void setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) override {
110  derivatives.require(makeAuto<Derivative>(settings));
111  }
112 
113  virtual void initialize(IScheduler& UNUSED(scheduler),
114  Storage& UNUSED(storage),
115  const Float UNUSED(t)) override {}
116 
117  virtual void finalize(IScheduler& UNUSED(scheduler),
118  Storage& UNUSED(storage),
119  const Float UNUSED(t)) override {}
120 
121  virtual void create(Storage& storage, IMaterial& UNUSED(material)) const override {
123  }
124 };
125 
127 private:
128  std::string script;
129  Float period;
130  bool oneShot;
131  bool done;
132  Float nextTime;
133 
134 #ifdef SPH_USE_CHAISCRIPT
135  Chai::Particles particles;
136 #endif
137 
138 public:
139  explicit ChaiScriptTerm(const Path& scriptFile, const Float period, const bool oneShot)
140  : period(period)
141  , oneShot(oneShot) {
142  nextTime = period;
143  done = false;
144 #ifdef SPH_USE_CHAISCRIPT
145  script = FileSystem::readFile(scriptFile);
146 #else
147  (void)scriptFile;
148  throw InvalidSetup("Code not built with ChaiScript support. Re-build with flag 'use_chaiscript'.");
149 #endif
150  }
151 
152  virtual void setDerivatives(DerivativeHolder& UNUSED(derivatives),
153  const RunSettings& UNUSED(settings)) override {}
154 
155  virtual void initialize(IScheduler& UNUSED(scheduler),
156  Storage& UNUSED(storage),
157  const Float UNUSED(t)) override {}
158 
159  virtual void finalize(IScheduler& UNUSED(scheduler), Storage& storage, const Float t) override {
160 #ifdef SPH_USE_CHAISCRIPT
161  if (t < nextTime || done) {
162  return;
163  }
164  nextTime += period;
165  particles.bindToStorage(storage);
166 
167  chaiscript::ChaiScript chai;
168  Chai::registerBindings(chai);
169  chai.add(chaiscript::var(std::ref(particles)), "particles");
170  chai.add(chaiscript::const_var(t), "time");
171  chai.eval(script);
172  particles.store();
173 
174  if (oneShot) {
175  done = true;
176  }
177 #else
178  (void)storage;
179  (void)t;
180  (void)period;
181  (void)oneShot;
182 #endif
183  }
184 
185  virtual void create(Storage& UNUSED(storage), IMaterial& UNUSED(material)) const override {}
186 };
187 
188 
@ UNIQUE
Only a single derivative accumulates to this buffer.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Right-hand side term of SPH equations.
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 INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ SURFACE_NORMAL
Vector approximating surface normal.
@ NEIGHBOUR_CNT
Number of neighbouring particles (in radius h * kernel.radius)
@ ZERO
Quantity without derivatives, or "zero order" of quantity.
Utility functions and classes exposed to the embedded scripting language.
INLINE Float getLength(const Vector &v)
Returns the length of the vector. Enabled only for vectors of floating-point precision.
Definition: Vector.h:579
BasicVector< Float > Vector
Definition: Vector.h:539
@ H
Definition: Vector.h:25
Storage for accumulating derivatives.
Definition: Accumulated.h:30
Array< TValue > & getBuffer(const QuantityId id, const OrderEnum order)
Returns the buffer of given quantity and given order.
Definition: Accumulated.cpp:53
void insert(const QuantityId id, const OrderEnum order, const BufferSource source)
Creates a new storage with given ID.
ChaiScriptTerm(const Path &scriptFile, const Float period, const bool oneShot)
Definition: HelperTerms.h:139
virtual void initialize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: HelperTerms.h:155
virtual void setDerivatives(DerivativeHolder &UNUSED(derivatives), const RunSettings &UNUSED(settings)) override
Definition: HelperTerms.h:152
virtual void create(Storage &UNUSED(storage), IMaterial &UNUSED(material)) const override
Definition: HelperTerms.h:185
virtual void finalize(IScheduler &UNUSED(scheduler), Storage &storage, const Float t) override
Definition: HelperTerms.h:159
Container holding derivatives and the storage they accumulate to.
Definition: Derivative.h:173
virtual void require(AutoPtr< IDerivative > &&derivative)
Adds derivative if not already present.
Definition: Derivative.cpp:77
Helper template for derivatives that define both the symmetrized and asymmetric variant.
Represents a term or terms appearing in evolutionary equations.
Definition: EquationTerm.h:22
Material settings and functions specific for one material.
Definition: IMaterial.h:110
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Thrown when components of the run are mutually incompatible.
Definition: Exceptions.h:24
Helper term counting the number of neighbours of each particle.
Definition: HelperTerms.h:16
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
Definition: HelperTerms.h:50
virtual void finalize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: HelperTerms.h:58
virtual void initialize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: HelperTerms.h:54
virtual void create(Storage &UNUSED(storage), IMaterial &UNUSED(material)) const override
Definition: HelperTerms.h:62
Object representing a path on a filesystem.
Definition: Path.h:17
Container storing all quantities used within the simulations.
Definition: Storage.h:230
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 > & getValue(const QuantityId key)
Retrieves a quantity values from the storage, given its key and value type.
Definition: Storage.cpp:191
Term computing normals of free surface.
Definition: HelperTerms.h:70
virtual void initialize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: HelperTerms.h:113
virtual void finalize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: HelperTerms.h:117
virtual void create(Storage &storage, IMaterial &UNUSED(material)) const override
Definition: HelperTerms.h:121
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
Definition: HelperTerms.h:109
std::string readFile(const Path &path)
Reads the whole file into the string.
Definition: FileSystem.cpp:14