SPH
Stress.cpp
Go to the documentation of this file.
4 #include "system/Factory.h"
5 #include "thread/Scheduler.h"
6 
8 
9 template <typename Discr>
10 class StressAV::Derivative : public AccelerationTemplate<Derivative<Discr>> {
11 private:
13  Float n;
14  Float xi;
15 
20 
21  Discr discr;
22 
23 public:
24  explicit Derivative(const RunSettings& settings)
26  kernel = Factory::getKernel<3>(settings);
29  }
30 
31  INLINE void additionalCreate(Accumulated& UNUSED(results)) const {}
32 
33  INLINE void additionalInitialize(const Storage& input, Accumulated& UNUSED(results)) {
36  rho = input.getValue<Float>(QuantityId::DENSITY);
38  tie(r, v, dummy) = input.getAll<Vector>(QuantityId::POSITION);
39  }
40 
41  INLINE bool additionalEquals(const Derivative& other) const {
42  return n == other.n && xi == other.xi;
43  }
44 
45  template <bool Symmetrize>
46  INLINE Tuple<Vector, Float> eval(const Size i, const Size j, const Vector& grad) {
47  const Float w = kernel.value(r[i], r[j]);
48 
49  // weighting function
50  const Float phi = xi * pow(w / wp[i], n);
51 
52  // AV term, discretized consistently with the selected SPH formulation
53  const SymmetricTensor Pi = phi * discr(as[i], as[j], rho[i], rho[j]);
54  const Vector f = Pi * grad;
55  const Float heating = 0.5_f * dot(Pi * (v[i] - v[j]), grad);
56  return { f, heating };
57  }
58 };
59 
60 StressAV::StressAV(const RunSettings& settings) {
61  kernel = Factory::getKernel<3>(settings);
62 }
63 
64 void StressAV::setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) {
65  const DiscretizationEnum formulation =
68  switch (formulation) {
70  struct StandardDiscr {
71  INLINE SymmetricTensor operator()(const SymmetricTensor& asi,
72  const SymmetricTensor& asj,
73  const Float rhoi,
74  const Float rhoj) const {
75  return asi / sqr(rhoi) + asj / sqr(rhoj);
76  }
77  };
78  derivatives.require(makeAuto<Derivative<StandardDiscr>>(settings));
79  break;
81  struct BenzAsphaugDiscr {
82  INLINE SymmetricTensor operator()(const SymmetricTensor& asi,
83  const SymmetricTensor& asj,
84  const Float rhoi,
85  const Float rhoj) const {
86  return (asi + asj) / (rhoi * rhoj);
87  }
88  };
89  derivatives.require(makeAuto<Derivative<BenzAsphaugDiscr>>(settings));
90  break;
91  default:
93  }
94 }
95 
96 void StressAV::initialize(IScheduler& scheduler, Storage& storage, const Float UNUSED(t)) {
100 
101  parallelFor(scheduler, 0, p.size(), [&as, &s, &p](const Size i) {
102  // compute total stress tesor
103  const SymmetricTensor sigma = SymmetricTensor(s[i]) - p[i] * SymmetricTensor::identity();
104 
105  // find eigenvectors and corresponding eigenvalues
106  Eigen eigen = eigenDecomposition(sigma);
107  AffineMatrix matrix = eigen.vectors;
108  Vector sigma_diag = eigen.values;
109 
110  // for positive values of diagonal stress, compute artificial stress
111  const Vector as_diag = -max(sigma_diag, Vector(0._f));
112 
113  // transform back to original coordinates
114  as[i] = transform(SymmetricTensor(as_diag, Vector(0._f)), matrix.transpose());
115  });
116 }
117 
118 void StressAV::finalize(IScheduler& UNUSED(scheduler), Storage& UNUSED(storage), const Float UNUSED(t)) {}
119 
120 void StressAV::create(Storage& storage, IMaterial& UNUSED(material)) const {
122  ArrayView<Float> wp = q.getValue<Float>();
124  for (Size i = 0; i < r.size(); ++i) {
126  wp[i] = kernel.value(Vector(r[i][H], 0._f, 0._f), r[i][H]);
127  }
129 }
130 
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
INLINE AutoPtr< T > makeAuto(TArgs &&... args)
Definition: AutoPtr.h:124
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
DerivativeFlag
@ 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
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
constexpr INLINE Float pow(const Float v)
Power for floats.
#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
@ DEVIATORIC_STRESS
Deviatoric stress tensor, always a traceless tensor.
@ INTERPARTICLE_SPACING_KERNEL
Auxiliary quantity needed for evaluating artificial stress.
@ PRESSURE
Pressure, affected by yielding and fragmentation model, always a scalar quantity.
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ DENSITY
Density, always a scalar quantity.
@ AV_STRESS
Artificial stress by Monaghan .
@ ZERO
Quantity without derivatives, or "zero order" of quantity.
Interface for executing tasks (potentially) asynchronously.
INLINE void parallelFor(IScheduler &scheduler, const Size from, const Size to, TFunctor &&functor)
Executes a functor concurrently from all available threads.
Definition: Scheduler.h:98
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
Form of tensor artificial viscosity for SPH with stress tensor.
BasicVector< Float > Vector
Definition: Vector.h:539
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
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
Generic container for storing scalar, vector or tensor quantity and its derivatives.
Definition: Quantity.h:200
INLINE Array< TValue > & getValue()
Returns a reference to array of quantity values.
Definition: Quantity.h:287
TValue get(const TEnum idx, std::enable_if_t<!std::is_enum< std::decay_t< TValue >>::value, int >=0) const
Returns a value of given type from the settings.
Definition: Settings.h:326
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
StaticArray< Array< TValue > &, 3 > getAll(const QuantityId key)
Retrieves quantity buffers from the storage, given its key and value type.
Definition: Storage.cpp:163
Array< TValue > & getValue(const QuantityId key)
Retrieves a quantity values from the storage, given its key and value type.
Definition: Storage.cpp:191
INLINE bool additionalEquals(const Derivative &other) const
Definition: Stress.cpp:41
INLINE Tuple< Vector, Float > eval(const Size i, const Size j, const Vector &grad)
Definition: Stress.cpp:46
INLINE void additionalCreate(Accumulated &UNUSED(results)) const
Definition: Stress.cpp:31
INLINE void additionalInitialize(const Storage &input, Accumulated &UNUSED(results))
Definition: Stress.cpp:33
Derivative(const RunSettings &settings)
Definition: Stress.cpp:24
virtual void create(Storage &storage, IMaterial &material) const override
Creates all quantities needed by the term using given material.
Definition: Stress.cpp:120
virtual void initialize(IScheduler &scheduler, Storage &storage, const Float t) override
Initialize all the derivatives and/or quantity values before derivatives are computed.
Definition: Stress.cpp:96
virtual void finalize(IScheduler &scheduler, Storage &storage, const Float t) override
Computes all the derivatives and/or quantity values based on accumulated derivatives.
Definition: Stress.cpp:118
StressAV(const RunSettings &settings)
Definition: Stress.cpp:60
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
Definition: Stress.cpp:64
Symmetric tensor of 2nd order.
static INLINE SymmetricTensor null()
Returns a tensor with all zeros.
INLINE Float value(const Vector &r1, const Vector &r2) const
Definition: Kernel.h:573
Symmetric traceless 2nd order tensor.
Heterogeneous container capable of storing a fixed number of values.
Definition: Tuple.h:146
Creating code components based on values from settings.
DiscretizationEnum
Definition: Settings.h:735
@ STANDARD
P_i / rho_i^2 + P_j / rho_j^2.
@ BENZ_ASPHAUG
(P_i + P_j) / (rho_i rho_j)
@ SPH_AV_STRESS_FACTOR
Multiplicative factor of the artificial stress term (= strength of the viscosity)
@ SPH_AV_STRESS_EXPONENT
Weighting function exponent n in artificial stress term.
@ SPH_DISCRETIZATION
Specifies a discretization of SPH equations; see DiscretizationEnum.