SPH
Balsara.h
Go to the documentation of this file.
1 #pragma once
2 
7 
10 #include "system/Settings.h"
11 
13 
16 template <typename Type>
17 std::enable_if_t<!std::is_constructible<Type, const RunSettings&>::value, Type> makeFromSettings(
18  const RunSettings& UNUSED(settings)) {
19  // default constructible
20  return Type();
21 }
22 
24 template <typename Type>
25 std::enable_if_t<std::is_constructible<Type, const RunSettings&>::value, Type> makeFromSettings(
26  const RunSettings& settings) {
27  // constructible from settings
28  return Type(settings);
29 }
30 
31 
34 
47 template <typename AV>
48 class BalsaraSwitch : public IEquationTerm {
49 
50  static_assert(std::is_base_of<IEquationTerm, AV>::value, "AV must be derived from IEquationTerm");
51 
52  class Derivative : public AccelerationTemplate<Derivative> {
53  private:
58  typename AV::Derivative av;
59  const Float eps = 1.e-4_f;
60 
61  public:
62  explicit Derivative(const RunSettings& settings)
64  , av(settings) {}
65 
66  INLINE void additionalCreate(Accumulated& results) {
67  av.create(results);
68  }
69 
70  INLINE void additionalInitialize(const Storage& input, Accumulated& results) {
72  tie(r, v, dummy) = input.getAll<Vector>(QuantityId::POSITION);
76 
77  av.initialize(input, results);
78  }
79 
80  INLINE bool additionalEquals(const Derivative& other) const {
81  return av.equals(other.av);
82  }
83 
84  template <bool Symmetrize>
85  INLINE Tuple<Vector, Float> eval(const Size i, const Size j, const Vector& grad) {
86  const Float Pi = 0.5_f * (factor(i) + factor(j)) * av.evalAv(i, j);
87  SPH_ASSERT(isReal(Pi));
88  const Float heating = 0.5_f * Pi * dot(v[i] - v[j], grad);
89  return { Pi * grad, heating };
90  }
91 
92  INLINE Float factor(const Size i) {
93  const Float dv = abs(divv[i]);
94  const Float rv = getLength(rotv[i]);
95  return dv / (dv + rv + eps * cs[i] / r[i][H]);
96  }
97  };
98 
99  AV av;
100  bool storeFactor;
101 
102 public:
103  explicit BalsaraSwitch(const RunSettings& settings)
104  : av(makeFromSettings<AV>(settings)) {
105  storeFactor = settings.get<bool>(RunSettingsId::SPH_AV_BALSARA_STORE);
106  }
107 
108  virtual void setDerivatives(DerivativeHolder& derivatives, const RunSettings& settings) override {
109  // no need to use the correction tensor here, velocity derivatives are only used to
110  // compute the Balsara factor, which is an arbitrary correction to AV anyway
111  derivatives.require(makeDerivative<VelocityDivergence>(settings));
112  derivatives.require(makeDerivative<VelocityRotation>(settings));
113  derivatives.require(makeAuto<Derivative>(settings));
114  }
115 
116  virtual void initialize(IScheduler& scheduler, Storage& storage, const Float t) override {
117  av.initialize(scheduler, storage, t);
118  }
119 
120  virtual void finalize(IScheduler& scheduler, Storage& storage, const Float t) override {
121  av.finalize(scheduler, storage, t);
122 
123  if (storeFactor) {
129  for (Size i = 0; i < factor.size(); ++i) {
130  const Float dv = abs(divv[i]);
131  const Float rv = getLength(rotv[i]);
132  factor[i] = dv / (dv + rv + 1.e-4_f * cs[i] / r[i][H]);
133  }
134  }
135  }
136 
137  virtual void create(Storage& storage, IMaterial& material) const override {
140  if (storeFactor) {
142  }
143  av.create(storage, material);
144  }
145 };
146 
INLINE bool isReal(const AntisymmetricTensor &t)
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN std::enable_if_t<!std::is_constructible< Type, const RunSettings & >::value, Type > makeFromSettings(const RunSettings &UNUSED(settings))
Helper function allowing to construct an object from settings if the object defines such constructor,...
Definition: Balsara.h:17
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
INLINE auto abs(const T &f)
Definition: MathUtils.h:276
#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_DIVERGENCE
Velocity divergence.
@ AV_BALSARA
Balsara factor.
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ VELOCITY_ROTATION
Velocity rotation.
@ SOUND_SPEED
Sound speed, 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
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
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
Implementation of the Balsara switch , designed to reduce artificial viscosity in shear flows and avo...
Definition: Balsara.h:48
virtual void setDerivatives(DerivativeHolder &derivatives, const RunSettings &settings) override
Sets derivatives required by this term.
Definition: Balsara.h:108
BalsaraSwitch(const RunSettings &settings)
Definition: Balsara.h:103
virtual void finalize(IScheduler &scheduler, Storage &storage, const Float t) override
Computes all the derivatives and/or quantity values based on accumulated derivatives.
Definition: Balsara.h:120
virtual void create(Storage &storage, IMaterial &material) const override
Creates all quantities needed by the term using given material.
Definition: Balsara.h:137
virtual void initialize(IScheduler &scheduler, Storage &storage, const Float t) override
Initialize all the derivatives and/or quantity values before derivatives are computed.
Definition: Balsara.h:116
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
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
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
Heterogeneous container capable of storing a fixed number of values.
Definition: Tuple.h:146
Generic storage and input/output routines of settings.
@ SPH_AV_BALSARA_STORE
If true, Balsara factors will be saved as quantity AV_BALSARA. Mainly for debugging purposes.