SPH
MorrisMonaghan.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "quantities/IMaterial.h"
9 #include "quantities/Storage.h"
12 #include "system/Settings.h"
13 
15 
22 public:
23  class Derivative : public AccelerationTemplate<Derivative> {
24  private:
25  ArrayView<const Float> alpha, dalpha, cs, rho;
27 
28  const Float eps = 1.e-2_f;
29 
30  public:
31  explicit Derivative(const RunSettings& settings)
32  : AccelerationTemplate<Derivative>(settings) {}
33 
35 
36  INLINE void additionalInitialize(const Storage& input, Accumulated& UNUSED(results)) {
38  tie(r, v, dummy) = input.getAll<Vector>(QuantityId::POSITION);
39  tie(alpha, dalpha) = input.getAll<Float>(QuantityId::AV_ALPHA);
41  rho = input.getValue<Float>(QuantityId::DENSITY);
42  }
43 
44  INLINE bool additionalEquals(const Derivative& UNUSED(other)) const {
45  return true;
46  }
47 
48  template <bool Symmetric>
49  INLINE Tuple<Vector, Float> eval(const Size i, const Size j, const Vector& grad) {
50  const Float Pi = evalAv(i, j);
51  const Float heating = 0.5_f * Pi * dot(v[i] - v[j], grad);
52 
53  return { Pi * grad, heating };
54  }
55 
56  INLINE Float evalAv(const Size i, const Size j) const {
57  const Float dvdr = dot(v[i] - v[j], r[i] - r[j]);
58  if (dvdr >= 0._f) {
59  return 0._f;
60  }
61  const Float hbar = 0.5_f * (r[i][H] + r[j][H]);
62  const Float csbar = 0.5_f * (cs[i] + cs[j]);
63  const Float rhobar = 0.5_f * (rho[i] + rho[j]);
64  const Float alphabar = 0.5_f * (alpha[i] + alpha[j]);
65  const Float betabar = 2._f * alphabar;
66  const Float mu = hbar * dvdr / (getSqrLength(r[i] - r[j]) + eps * sqr(hbar));
67  return 1._f / rhobar * (-alphabar * csbar * mu + betabar * sqr(mu));
68  }
69  };
70 
71  virtual void setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) override {
72  derivatives.require(makeDerivative<VelocityDivergence>(settings));
73  derivatives.require(makeAuto<Derivative>(settings));
74  }
75 
76  virtual void initialize(IScheduler& UNUSED(scheduler),
77  Storage& UNUSED(storage),
78  const Float UNUSED(t)) override {}
79 
80  virtual void finalize(IScheduler& UNUSED(scheduler), Storage& storage, const Float UNUSED(t)) override {
82  ArrayView<Float> alpha, dalpha;
83  tie(alpha, dalpha) = storage.getAll<Float>(QuantityId::AV_ALPHA);
86  constexpr Float eps = 0.1_f;
87  for (Size matIdx = 0; matIdx < storage.getMaterialCnt(); ++matIdx) {
88  MaterialView material = storage.getMaterial(matIdx);
89  const Interval bounds = material->getParam<Interval>(BodySettingsId::AV_ALPHA_RANGE);
90  for (Size i : material.sequence()) {
91  const Float tau = r[i][H] / (eps * cs[i]);
92  SPH_ASSERT(tau > 0._f);
93  const Float decayTerm = -(alpha[i] - Float(bounds.lower())) / tau;
94  const Float sourceTerm = max(-(Float(bounds.upper()) - alpha[i]) * divv[i], 0._f);
95  dalpha[i] = decayTerm + sourceTerm;
96  SPH_ASSERT(isReal(dalpha[i]));
97  }
98  }
99  }
100 
101  virtual void create(Storage& storage, IMaterial& material) const override {
102  storage.insert<Float>(
104 
106  const Interval avRange = material.getParam<Interval>(BodySettingsId::AV_ALPHA_RANGE);
107  material.setRange(QuantityId::AV_ALPHA, avRange, 0._f);
108  }
109 };
110 
INLINE bool isReal(const AntisymmetricTensor &t)
#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
Base class for all particle materials.
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
#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
@ AV_ALPHA
Coefficient alpha of the artificial viscosity. Coefficient beta is always 2*alpha.
@ VELOCITY_DIVERGENCE
Velocity divergence.
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ DENSITY
Density, always a scalar quantity.
@ SOUND_SPEED
Sound speed, always a scalar quantity.
@ FIRST
Quantity with 1st derivative.
@ ZERO
Quantity without derivatives, or "zero order" of quantity.
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
Container for storing particle quantities and materials.
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
INLINE float dot(const BasicVector< float > &v1, const BasicVector< float > &v2)
Make sure the vector is trivially constructible and destructible, needed for fast initialization of a...
Definition: Vector.h:548
@ H
Definition: Vector.h:25
Helper template specifically used to implement forces.
Storage for accumulating derivatives.
Definition: Accumulated.h:30
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
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
INLINE TValue getParam(const BodySettingsId paramIdx) const
Returns a parameter associated with given particle.
Definition: IMaterial.h:137
void setRange(const QuantityId id, const Interval &range, const Float minimal)
Sets the timestepping parameters of given quantity.
Definition: IMaterial.cpp:17
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE Float lower() const
Returns lower bound of the interval.
Definition: Interval.h:74
INLINE Float upper() const
Returns upper bound of the interval.
Definition: Interval.h:79
Non-owning wrapper of a material and particles with this material.
Definition: IMaterial.h:30
INLINE IndexSequence sequence()
Returns iterable index sequence.
Definition: IMaterial.h:75
Derivative(const RunSettings &settings)
INLINE Float evalAv(const Size i, const Size j) const
INLINE bool additionalEquals(const Derivative &UNUSED(other)) const
INLINE void additionalInitialize(const Storage &input, Accumulated &UNUSED(results))
INLINE Tuple< Vector, Float > eval(const Size i, const Size j, const Vector &grad)
INLINE void additionalCreate(Accumulated &UNUSED(results))
Time-dependent artificial viscosity with non-homogeneous oefficients alpha and beta.
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
virtual void initialize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
virtual void finalize(IScheduler &UNUSED(scheduler), Storage &storage, const Float UNUSED(t)) override
virtual void create(Storage &storage, IMaterial &material) const override
Creates all quantities needed by the term using given material.
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
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
StaticArray< Array< TValue > &, 3 > getAll(const QuantityId key)
Retrieves quantity buffers from the storage, given its key and value type.
Definition: Storage.cpp:163
MaterialView getMaterial(const Size matIdx) const
Returns an object containing a reference to given material.
Definition: Storage.cpp:366
Array< TValue > & getValue(const QuantityId key)
Retrieves a quantity values from the storage, given its key and value type.
Definition: Storage.cpp:191
Heterogeneous container capable of storing a fixed number of values.
Definition: Tuple.h:146
Generic storage and input/output routines of settings.
@ AV_ALPHA_RANGE
Lower and upper bound of the alpha coefficient, used only for time-dependent artificial viscosity.