SPH
Friction.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "quantities/IMaterial.h"
6 #include "sph/kernel/Kernel.h"
7 
9 
12 class NaiveViscosity : public IEquationTerm {
13 private:
14  class Derivative : public AccelerationTemplate<Derivative> {
15  private:
18 
20  ArrayView<Vector> divGradV;
21  ArrayView<Vector> gradDivV;
22 
24  Float eta;
25  Float zeta;
26 
27  static constexpr Float multiplier = 1.e17_f;
28 
29  public:
30  explicit Derivative(const RunSettings& settings)
32 
33  INLINE void additionalCreate(Accumulated& results) {
35  results.insert<Vector>(
37  }
38 
39  INLINE void additionalInitialize(const Storage& input, Accumulated& results) {
41  tie(r, v, dummy) = input.getAll<Vector>(QuantityId::POSITION);
43 
44  gradDivV =
47 
48  MaterialView mat0 = input.getMaterial(0);
49  eta = mat0->getParam<Float>(BodySettingsId::SHEAR_VISCOSITY);
50  zeta = mat0->getParam<Float>(BodySettingsId::BULK_VISCOSITY);
53  }
54 
55  INLINE bool additionalEquals(const Derivative& other) const {
56  return eta == other.eta && zeta == other.zeta;
57  }
58 
59  template <bool Symmetrize>
60  INLINE Tuple<Vector, Float> eval(const Size i, const Size j, const Vector& grad) {
61  const Vector dgv = laplacian(v[j] - v[i], grad, r[j] - r[i]);
62  const Vector gdv = gradientOfDivergence(v[j] - v[i], grad, r[j] - r[i]);
63  const Vector shear = eta * (dgv + gdv / 3._f);
64  const Vector iso = zeta * gdv;
65  const Vector term = (shear + iso) / (rho[i] * rho[j]);
66  gradDivV[i] += m[j] / rho[j] * gdv * multiplier;
67  divGradV[i] += m[j] / rho[j] * dgv * multiplier;
68  if (Symmetrize) {
69  gradDivV[j] -= m[i] / rho[i] * gdv * multiplier;
70  divGradV[j] -= m[i] / rho[i] * dgv * multiplier;
71  }
72 
73  return { term, 0._f };
74  }
75  };
76 
77 public:
78  virtual void setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) override {
79  derivatives.require(makeAuto<Derivative>(settings));
80  }
81 
82  virtual void initialize(IScheduler& UNUSED(scheduler),
83  Storage& UNUSED(storage),
84  const Float UNUSED(t)) override {}
85 
86  virtual void finalize(IScheduler& UNUSED(scheduler),
87  Storage& UNUSED(storage),
88  const Float UNUSED(t)) override {}
89 
90  virtual void create(Storage& storage, IMaterial& UNUSED(material)) const override {
93  }
94 };
95 
97 class ViscousStress : public IEquationTerm {
98 private:
99  class Derivative : public AccelerationTemplate<Derivative> {
100  private:
101  ArrayView<const Float> m, rho;
103 
105  ArrayView<Vector> frict;
106 
107  Float eta;
108 
109  public:
110  explicit Derivative(const RunSettings& settings)
111  : AccelerationTemplate<Derivative>(settings) {}
112 
113  INLINE void additionalCreate(Accumulated& results) {
115  }
116 
117  INLINE void additionalInitialize(const Storage& input, Accumulated& results) {
121 
122  eta = input.getMaterial(0)->getParam<Float>(BodySettingsId::SHEAR_VISCOSITY);
124  }
125 
126  INLINE bool additionalEquals(const Derivative& other) const {
127  return eta == other.eta;
128  }
129 
130  template <bool Symmetrize>
131  INLINE Tuple<Vector, Float> eval(const Size i, const Size j, const Vector& grad) {
132  SymmetricTensor sigmai =
133  2._f * gradV[i] - 2._f / 3._f * gradV[i].trace() * SymmetricTensor::identity();
134  SymmetricTensor sigmaj =
135  2._f * gradV[j] - 2._f / 3._f * gradV[j].trace() * SymmetricTensor::identity();
136 
137  const Vector f = eta * (sigmai / pow<2>(rho[i]) + sigmaj / pow<2>(rho[j])) * grad;
138  frict[i] += m[j] * f;
139  if (Symmetrize) {
140  frict[j] -= m[i] * f;
141  }
142 
143  return { f, 0._f };
144  }
145  };
146 
147 
148 public:
149  virtual void setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) override {
151  derivatives.require(makeDerivative<VelocityGradient>(settings, flags));
152  derivatives.require(makeAuto<Derivative>(settings));
153  }
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),
160  Storage& UNUSED(storage),
161  const Float UNUSED(t)) override {}
162 
163  virtual void create(Storage& storage, IMaterial& material) const override {
164  const Float u0 = material.getParam<Float>(BodySettingsId::ENERGY);
167  storage.insert<SymmetricTensor>(
169  }
170 };
171 
172 class SimpleDamping : public IEquationTerm {
173 private:
174  class Derivative : public AccelerationTemplate<Derivative> {
177 
178  Float k = 0._f;
179 
180  public:
181  explicit Derivative(const RunSettings& settings)
182  : AccelerationTemplate<Derivative>(settings) {}
183 
184  INLINE void additionalCreate(Accumulated& UNUSED(results)) {}
185 
186  INLINE void additionalInitialize(const Storage& input, Accumulated& UNUSED(results)) {
188  tie(r, v, dummy) = input.getAll<Vector>(QuantityId::POSITION);
190 
191  k = input.getMaterial(0)->getParam<Float>(BodySettingsId::DAMPING_COEFFICIENT);
192  }
193 
194  INLINE bool additionalEquals(const Derivative& UNUSED(other)) const {
195  return true;
196  }
197 
198  template <bool Symmetrize>
199  INLINE Tuple<Vector, Float> eval(const Size i, const Size j, const Vector& UNUSED(grad)) {
200  const Float csbar = 0.5_f * (cs[i] + cs[j]);
201  const Vector f = k * (v[i] - v[j]) / csbar;
203  return { -f, 0._f };
204  }
205  };
206 
207 public:
208  virtual void setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) override {
209  derivatives.require(makeAuto<Derivative>(settings));
210  }
211 
212  virtual void initialize(IScheduler& UNUSED(scheduler),
213  Storage& UNUSED(storage),
214  const Float UNUSED(t)) override {}
215 
216  virtual void finalize(IScheduler& UNUSED(scheduler),
217  Storage& UNUSED(storage),
218  const Float UNUSED(t)) override {}
219 
220  virtual void create(Storage& UNUSED(storage), IMaterial& UNUSED(material)) const override {}
221 };
222 
223 
@ UNIQUE
Only a single derivative accumulates to this buffer.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
@ SUM_ONLY_UNDAMAGED
Only undamaged particles (particles with damage > 0) from the same body (body with the same flag) wil...
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 Vector gradientOfDivergence(const Vector &value, const Vector &grad, const Vector &dr)
Second derivative of vector quantity, applying gradient on a divergence.
Definition: Kernel.h:527
INLINE T laplacian(const T &value, const Vector &grad, const Vector &dr)
SPH approximation of laplacian, computed from a kernel gradient.
Definition: Kernel.h:519
constexpr INLINE Float pow< 2 >(const Float v)
Definition: MathUtils.h:128
#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
@ VELOCITY_GRADIENT
Velocity gradient.
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ ENERGY
Specific internal energy, always a scalar quantity.
@ VELOCITY_LAPLACIAN
@ DENSITY
Density, always a scalar quantity.
@ MASS
Paricles masses, always a scalar quantity.
@ SOUND_SPEED
Sound speed, always a scalar quantity.
@ VELOCITY_GRADIENT_OF_DIVERGENCE
@ 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
BasicVector< Float > Vector
Definition: Vector.h:539
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
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
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Non-owning wrapper of a material and particles with this material.
Definition: IMaterial.h:30
virtual void finalize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: Friction.h:86
virtual void create(Storage &storage, IMaterial &UNUSED(material)) const override
Definition: Friction.h:90
virtual void initialize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: Friction.h:82
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
Definition: Friction.h:78
virtual void initialize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: Friction.h:212
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
Definition: Friction.h:208
virtual void create(Storage &UNUSED(storage), IMaterial &UNUSED(material)) const override
Definition: Friction.h:220
virtual void finalize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: Friction.h:216
Container storing all quantities used within the simulations.
Definition: Storage.h:230
bool isHomogeneous(const BodySettingsId param) const
Checks if the particles in the storage are homogeneous with respect to given parameter.
Definition: Storage.cpp:386
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
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
Symmetric tensor of 2nd order.
static INLINE SymmetricTensor identity()
Returns an identity tensor.
static INLINE SymmetricTensor null()
Returns a tensor with all zeros.
Heterogeneous container capable of storing a fixed number of values.
Definition: Tuple.h:146
Flebbe et al. (1994)
Definition: Friction.h:97
virtual void finalize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: Friction.h:159
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
Definition: Friction.h:149
virtual void create(Storage &storage, IMaterial &material) const override
Creates all quantities needed by the term using given material.
Definition: Friction.h:163
virtual void initialize(IScheduler &UNUSED(scheduler), Storage &UNUSED(storage), const Float UNUSED(t)) override
Definition: Friction.h:155
@ ENERGY
Initial specific internal energy.