SPH
Fluids.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "quantities/IMaterial.h"
11 #include "sph/kernel/Kernel.h"
12 
14 
17 
23 private:
24  // this kernel does not have to be normalized to 1, this constant is used only to shift practical values
25  // of the surface tension coefficient to 1.
26  static constexpr Float normalization = 32._f / PI;
27 
28 public:
29  INLINE Float valueImpl(const Float qSqr) const {
30  const Float q = sqrt(qSqr);
31  SPH_ASSERT(q >= 0);
32 
33  if (q < 0.5_f) {
34  return normalization * (2._f * pow<3>(1._f - q) * pow<3>(q) - 1._f / 64._f);
35  }
36  if (q < 1._f) {
37  return normalization * (pow<3>(1._f - q) * pow<3>(q));
38  }
39  return 0._f;
40  }
41 
42  INLINE Float gradImpl(const Float UNUSED(qSqr)) const {
43  // called by LutKernel, although the values are never used
44  return 0._f;
45  }
46 
47  INLINE Float radius() const {
48  return 1._f;
49  }
50 };
51 
52 class CohesionDerivative : public AccelerationTemplate<CohesionDerivative> {
53 private:
55  Float gamma;
56 
59 
62 
63 public:
64  explicit CohesionDerivative(const RunSettings& settings)
66  , kernel(CohesionKernel{}) {}
67 
69 
70  INLINE void additionalInitialize(const Storage& input, Accumulated& UNUSED(results)) {
73 
75  gamma = input.getMaterial(0)->getParam<Float>(BodySettingsId::SURFACE_TENSION);
76  }
77 
78  INLINE bool additionalEquals(const CohesionDerivative& UNUSED(other)) const {
79  return true;
80  }
81 
82  template <bool Symmetrize>
83  INLINE Tuple<Vector, Float> eval(const Size i, const Size j, const Vector& UNUSED(grad)) {
84  if (SPH_UNLIKELY(r[i] == r[j])) {
85  return { Vector(0._f), 0._f };
86  }
87  const Vector dr = getNormalized(r[i] - r[j]);
88  const Float C = kernel.value(r[i], r[j]);
89 
90  // cohesive term + surface area normalizing term
91  const Vector f = -gamma * C * dr - gamma * (n[i] - n[j]);
92  SPH_ASSERT(isReal(f));
93 
94  return { f, 0._f };
95  }
96 };
97 
99 class ColorFieldDerivative : public DerivativeTemplate<ColorFieldDerivative> {
100 private:
101  ArrayView<const Float> m, rho;
103 
105 
106 public:
107  explicit ColorFieldDerivative(const RunSettings& settings)
109 
112  }
113 
114  INLINE void additionalInitialize(const Storage& input, Accumulated& results) {
117 
119  }
120 
122  return true;
123  }
124 
125  template <bool Symmetrize>
126  INLINE void eval(const Size i, const Size j, const Vector& grad) {
127  n[i] += r[i][H] * m[j] / rho[j] * grad;
128  if (Symmetrize) {
129  n[j] -= r[j][H] * m[i] / rho[i] * grad;
130  }
131  }
132 };
133 
134 class CohesionTerm : public IEquationTerm {
135 public:
136  virtual void setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) override {
137  derivatives.require(makeAuto<CohesionDerivative>(settings));
138  derivatives.require(makeAuto<ColorFieldDerivative>(settings));
139  }
140 
141  virtual void initialize(IScheduler& UNUSED(scheduler),
142  Storage& UNUSED(storage),
143  const Float UNUSED(t)) override {}
144 
145  virtual void finalize(IScheduler& UNUSED(scheduler),
146  Storage& UNUSED(storage),
147  const Float UNUSED(t)) override {}
148 
149  virtual void create(Storage& storage, IMaterial& UNUSED(material)) const override {
151  }
152 };
153 
@ UNIQUE
Only a single derivative accumulates to this buffer.
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.
SPH kernels.
INLINE T sqrt(const T f)
Return a squared root of a value.
Definition: MathUtils.h:78
constexpr INLINE Float pow< 3 >(const Float v)
Definition: MathUtils.h:132
constexpr Float PI
Mathematical constants.
Definition: MathUtils.h:361
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define SPH_UNLIKELY(x)
Definition: Object.h:50
#define NAMESPACE_SPH_END
Definition: Object.h:12
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ DENSITY
Density, always a scalar quantity.
@ SURFACE_NORMAL
Vector approximating surface normal.
@ MASS
Paricles masses, always a scalar quantity.
@ 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
BasicVector< Float > Vector
Definition: Vector.h:539
INLINE Vector getNormalized(const Vector &v)
Definition: Vector.h:590
@ H
Definition: Vector.h:25
Helper template specifically used to implement forces.
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.
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE void additionalInitialize(const Storage &input, Accumulated &UNUSED(results))
Definition: Fluids.h:70
INLINE Tuple< Vector, Float > eval(const Size i, const Size j, const Vector &UNUSED(grad))
Definition: Fluids.h:83
INLINE bool additionalEquals(const CohesionDerivative &UNUSED(other)) const
Definition: Fluids.h:78
INLINE void additionalCreate(Accumulated &UNUSED(results))
Definition: Fluids.h:68
CohesionDerivative(const RunSettings &settings)
Definition: Fluids.h:64
Helper kernel used to simulate Lennard-Jones forces.
Definition: Fluids.h:22
INLINE Float radius() const
Definition: Fluids.h:47
INLINE Float gradImpl(const Float UNUSED(qSqr)) const
Definition: Fluids.h:42
INLINE Float valueImpl(const Float qSqr) const
Definition: Fluids.h:29
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
Definition: Fluids.h:136
virtual void initialize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: Fluids.h:141
virtual void create(Storage &storage, IMaterial &UNUSED(material)) const override
Definition: Fluids.h:149
virtual void finalize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: Fluids.h:145
Computes the color field of the fluid.
Definition: Fluids.h:99
INLINE bool additionalEquals(const ColorFieldDerivative &UNUSED(other)) const
Definition: Fluids.h:121
INLINE void eval(const Size i, const Size j, const Vector &grad)
Definition: Fluids.h:126
ColorFieldDerivative(const RunSettings &settings)
Definition: Fluids.h:107
INLINE void additionalInitialize(const Storage &input, Accumulated &results)
Definition: Fluids.h:114
INLINE void additionalCreate(Accumulated &results)
Definition: Fluids.h:110
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
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
auto getValues(const QuantityId first, const QuantityId second, const TArgs... others)
Retrieves an array of quantities from the key.
Definition: Storage.h:359
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
INLINE Float value(const Vector &r1, const Vector &r2) const
Definition: Kernel.h:573
Heterogeneous container capable of storing a fixed number of values.
Definition: Tuple.h:146
@ SURFACE_TENSION
Coefficient of surface tension.