SPH
Sod.cpp
Go to the documentation of this file.
1 
6 #include "Sph.h"
7 #include "catch.hpp"
8 #include "sod/Solution.h"
10 
11 using namespace Sph;
12 
13 INLINE Float smoothingFunc(const Float x, const Float x1, const Float x2) {
14 #if 0
15  const Float w = exp(-(x - 0.5_f) / 0.0005_f);
16  if (x > 0.52_f) {
17  return x2;
18  } else if (x < 0.48_f) {
19  return x1;
20  }
21  return (x1 * w + x2) / (1._f * w + 1._f);
22 #else
23  return (x > 0._f) ? x2 : x1;
24 #endif
25 }
26 
28  const Float x2,
29  const Float rho1,
30  const Float rho2,
31  const Float eta) {
32  Array<Vector> r;
33  Float delta = 2 * 0.0127_f;
34  Float dx1 = delta * rho2 / (rho1 + rho2);
35  Float dx2 = delta * rho1 / (rho1 + rho2);
36  Float dx = 0._f;
37  for (Float x = x1; x <= x2; x += dx) {
38  dx = (x < 0._f) ? dx1 : dx2;
39  r.push(Vector(x, 0._f, 0._f, eta * dx));
40  }
41  return r;
42 }
43 
44 class SodOutput : public IOutput {
45 private:
47  AutoPtr<TextOutput> analytic;
48 
49 public:
50  SodOutput(const std::string& name)
51  : IOutput(Path("dummy")) {
54  main = makeAuto<TextOutput>(Path("sod/sod_%d.txt"), name, flags);
55  analytic = makeAuto<TextOutput>(Path("sod/sod_analytic_%d.txt"), name, flags);
56  }
57 
58  virtual Expected<Path> dump(const Storage& storage, const Statistics& stats) override {
59  const Float t = stats.get<Float>(StatisticsId::RUN_TIME);
60  SodConfig conf;
61  Storage an = analyticSod(conf, t);
62  analytic->dump(an, stats);
63  return main->dump(storage, stats);
64  }
65 };
66 
67 class SodBc : public IBoundaryCondition {
68 private:
69  SodConfig sod;
70  Float eta = 0.01_f;
71 
72 public:
73  SodBc(const SodConfig& sod)
74  : sod(sod) {}
75 
76  virtual void initialize(Storage& storage) override {
77  this->reset(storage);
78  }
79 
80 
81  virtual void finalize(Storage& storage) override {
82  this->reset(storage);
83  }
84 
85 private:
86  void reset(Storage& storage) {
87  ArrayView<Vector> r, v, dv;
88  tie(r, v, dv) = storage.getAll<Vector>(QuantityId::POSITION);
93  EosMaterial& mat = dynamic_cast<EosMaterial&>(storage.getMaterial(0).material());
94  for (Size i = 0; i < v.size(); ++i) {
95  if (r[i][X] > 0.5_f - eta) {
96  v[i] = dv[i] = Vector(0._f);
97  rho[i] = sod.rho_r;
98  p[i] = sod.P_r;
99  u[i] = mat.getEos().getInternalEnergy(rho[i], p[i]);
100  du[i] = 0._f;
101  } else if (r[i][X] < -0.5_f + eta) {
102  v[i] = dv[i] = Vector(0._f);
103  rho[i] = sod.rho_l;
104  p[i] = sod.P_l;
105  u[i] = mat.getEos().getInternalEnergy(rho[i], p[i]);
106  du[i] = 0._f;
107  }
108  v[i][X] = max(v[i][X], 0._f);
109  }
110  }
111 };
112 
113 class Sod : public IRun {
114 public:
115  Sod() {
116  // Global settings of the problem
117  this->settings.set(RunSettingsId::RUN_NAME, std::string("Sod Shock Tube Problem"))
118  .set(RunSettingsId::RUN_END_TIME, 0.3_f)
120  .set(RunSettingsId::RUN_OUTPUT_PATH, std::string(""))
121  .set(RunSettingsId::RUN_OUTPUT_NAME, std::string("sod_%d.txt"))
122  .set(RunSettingsId::SPH_AV_ALPHA, 1._f)
123  .set(RunSettingsId::SPH_AV_BETA, 2._f)
132  }
133 
134  virtual void setUp(SharedPtr<Storage> storage) override {
135  SodConfig conf;
136 
137  // Material properties
138  BodySettings body;
148  .set(BodySettingsId::ENERGY, 2.5_f)
149  .set(BodySettingsId::ENERGY_MIN, 0.001_f);
150  *storage = Storage(Factory::getMaterial(body));
151 
152  this->output = makeAuto<SodOutput>(this->settings.get<std::string>(RunSettingsId::RUN_NAME));
153 
154  // 1) setup initial positions, with different spacing in each region
155  const Float x1 = -0.5_f;
156  const Float x2 = 0.5_f;
158  storage->insert<Vector>(
160 
161  // 2) setup initial masses of particles
162  storage->insert<Float>(QuantityId::MASS, OrderEnum::ZERO, 1._f);
165  for (Size i = 0; i < r.size(); ++i) {
166  // mass = 1/N *integral density * dx
167  m[i] = (-x1 * conf.rho_l + x2 * conf.rho_r) / r.size();
168  }
169 
170  // 3) manually create the solver (no other way to get 1D solver right now),
171  // this also creates quantities (density, etc).
172  this->solver =
173  makeAuto<SummationSolver<1>>(*scheduler, settings, EquationHolder{}, makeAuto<SodBc>(conf));
174  this->solver->create(*storage, storage->getMaterial(0));
175  MaterialInitialContext context;
176  context.scheduler = scheduler;
177  EosMaterial& mat = dynamic_cast<EosMaterial&>(storage->getMaterial(0).material());
178  mat.create(*storage, context);
179 
180  // 4) compute other quantities
184  for (Size i = 0; i < r.size(); ++i) {
185  p[i] = smoothingFunc(r[i][0], conf.P_l, conf.P_r);
186  rho[i] = smoothingFunc(r[i][0], conf.rho_l, conf.rho_r);
187  u[i] = mat.getEos().getInternalEnergy(rho[i], p[i]);
188  }
189  Statistics stats;
190  solver->integrate(*storage, stats);
191 
192 
193  // AutoPtr<IEos> eos = Factory::getEos(body);
194  // u[i] = eos->getInternalEnergy(
195  // smoothingFunc(r[i][0], conf.rho_l, conf.rho_r), smoothingFunc(r[i][0], conf.P_l, conf.P_r));
196  }
197 
198 protected:
199  virtual void tearDown(const Storage& UNUSED(storage), const Statistics& UNUSED(stats)) override {}
200 };
201 
202 TEST_CASE("Sod", "[sod]") {
203  Sod run;
204  Storage storage;
205  run.run(storage);
206 }
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 max(const T &f1, const T &f2)
Definition: MathBasic.h:20
constexpr Float INFTY
Definition: MathUtils.h:38
INLINE T exp(const T f)
Definition: MathUtils.h:269
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
@ PRESSURE
Pressure, reduced by yielding and fracture model (multiplied by 1-damage); always a scalar quantity.
@ VELOCITY
Current velocities of particles, always a vector quantity.
@ POSITION
Positions of particles, always a vector quantity.
@ ENERGY
Specific internal energy, always a scalar quantity.
@ DENSITY
Density, always a scalar quantity.
@ PRESSURE
Pressure, affected by yielding and fragmentation model, always a scalar quantity.
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ ENERGY
Specific internal energy, always a scalar quantity.
@ DENSITY
Density, always a scalar quantity.
@ MASS
Paricles masses, always a scalar quantity.
@ SECOND
Quantity with 1st and 2nd derivative.
@ ZERO
Quantity without derivatives, or "zero order" of quantity.
INLINE Float smoothingFunc(const Float x, const Float x1, const Float x2)
Definition: Sod.cpp:13
Array< Vector > sodDistribution(const Float x1, const Float x2, const Float rho1, const Float rho2, const Float eta)
Definition: Sod.cpp:27
TEST_CASE("Sod", "[sod]")
Definition: Sod.cpp:202
Storage analyticSod(const SodConfig &sod, const Float t)
Definition: Solution.cpp:52
Includes common headers.
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
@ RUN_TIME
Current time of the simulation in code units. Does not necessarily have to be 0 when run starts.
Solver using direction summation to compute density and smoothing length.
BasicVector< Float > Vector
Definition: Vector.h:539
@ X
Definition: Vector.h:22
int main(int argc, char *argv[])
Definition: main.cpp:7
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE TCounter size() const
Definition: ArrayView.h:101
Generic dynamically allocated resizable storage.
Definition: Array.h:43
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
INLINE TCounter size() const noexcept
Definition: Array.h:193
Material holding equation of state.
Definition: Materials.h:21
const IEos & getEos() const
Returns the equation of state.
Definition: Materials.cpp:26
virtual void create(Storage &storage, const MaterialInitialContext &context) override
Create all quantities needed by the material.
Definition: Materials.cpp:30
Container holding equation terms.
Definition: EquationTerm.h:238
Wrapper of type that either contains a value of given type, or an error message.
Definition: Expected.h:25
Base object for boundary conditions.
Definition: Boundary.h:29
virtual Float getInternalEnergy(const Float rho, const Float p) const =0
Inverted function; computes specific internal energy u from given density rho and pressure p.
Interface for saving quantities of SPH particles to a file.
Definition: Output.h:76
Defines the interface for a run.
Definition: IRun.h:61
Statistics run(Storage &storage)
Runs the simulation.
Definition: IRun.cpp:187
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE IMaterial & material()
Returns the reference to the material of the particles.
Definition: IMaterial.h:43
Object representing a path on a filesystem.
Definition: Path.h:17
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
Settings & set(const TEnum idx, TValue &&value, std::enable_if_t<!std::is_enum< std::decay_t< TValue >>::value, int >=0)
Saves a value into the settings.
Definition: Settings.h:226
Definition: Sod.cpp:67
SodBc(const SodConfig &sod)
Definition: Sod.cpp:73
virtual void finalize(Storage &storage) override
Applies the boundary conditions after the derivatives are computed.
Definition: Sod.cpp:81
virtual void initialize(Storage &storage) override
Applies the boundary conditions before the derivatives are computed.
Definition: Sod.cpp:76
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats) override
Saves data from particle storage into the file.
Definition: Sod.cpp:58
SodOutput(const std::string &name)
Definition: Sod.cpp:50
Definition: Sod.cpp:113
virtual void setUp(SharedPtr< Storage > storage) override
Prepares the run, creates logger, output, ...
Definition: Sod.cpp:134
Sod()
Definition: Sod.cpp:115
virtual void tearDown(const Storage &UNUSED(storage), const Statistics &UNUSED(stats)) override
Definition: Sod.cpp:199
Object holding various statistics about current run.
Definition: Statistics.h:22
TValue get(const StatisticsId idx) const
Returns value of a statistic.
Definition: Statistics.h:88
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 > & getDt(const QuantityId key)
Retrieves a quantity derivative from the storage, given its key and value type.
Definition: Storage.cpp:217
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
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats) override
Saves data from particle storage into the file.
Definition: Output.cpp:192
@ PRESSURE
Use force from pressure gradient in the solver.
@ NONE
Gass or material with no stress tensor.
@ UNIFORM_GRID
Partitioning particles into a grid uniform in space.
@ COURANT
Time step determined using CFL condition.
@ PREDICTOR_CORRECTOR
Predictor-corrector scheme.
@ CUBIC_SPLINE
M4 B-spline (piecewise cubic polynomial)
@ NONE
No fragmentation.
@ IDEAL_GAS
Equation of state for ideal gas.
@ ENERGY_MIN
Estimated minimal value of energy used to determine timestepping error.
@ RHEOLOGY_DAMAGE
Model of fragmentation used within the rheological model.
@ DENSITY_RANGE
Allowed range of density. Densities of all particles all clamped to fit in the range.
@ ENERGY
Initial specific internal energy.
@ ADIABATIC_INDEX
Adiabatic index used by some equations of state (such as ideal gas)
@ DENSITY
Density at zero pressure.
@ SMOOTHING_LENGTH_ETA
Eta-factor between smoothing length and particle concentration (h = eta * n^(-1/d) )
@ EOS
Equation of state for this material, see EosEnum for options.
@ ENERGY_RANGE
Allowed range of specific internal energy.
@ RHEOLOGY_YIELDING
Model of stress reducing used within the rheological model.
@ RUN_OUTPUT_INTERVAL
Time interval of dumping data to disk.
@ TIMESTEPPING_MAX_TIMESTEP
@ SPH_KERNEL
Index of SPH Kernel, see KernelEnum.
@ SPH_SOLVER_FORCES
List of forces to compute by the solver.
@ SPH_AV_BETA
Artificial viscosity beta coefficient.
@ SPH_AV_ALPHA
Artificial viscosity alpha coefficient.
@ RUN_OUTPUT_PATH
Path where all output files (dumps, logs, ...) will be written.
@ TIMESTEPPING_INITIAL_TIMESTEP
@ TIMESTEPPING_INTEGRATOR
Selected timestepping integrator.
@ TIMESTEPPING_COURANT_NUMBER
Courant number.
@ SPH_FINDER
Structure for searching nearest neighbours of particles.
@ RUN_NAME
User-specified name of the run, used in some output files.
@ RUN_OUTPUT_NAME
File name of the output file (including extension), where d is a placeholder for output number.
AutoPtr< IMaterial > getMaterial(const BodySettings &settings)
Definition: Factory.cpp:508
Definition: MemoryPool.h:5
Shared data used when creating all bodies in the simulation.
Definition: IMaterial.h:89
SharedPtr< IScheduler > scheduler
Definition: IMaterial.h:93
Float P_r
Definition: Solution.h:18
Float P_l
Definition: Solution.h:14
Float rho_r
Definition: Solution.h:17
Float gamma
Definition: Solution.h:21
Float rho_l
Definition: Solution.h:13