SPH
KelvinHelmholtz.cpp
Go to the documentation of this file.
1 
6 #include "Sph.h"
7 #include "catch.hpp"
8 #include <iostream>
9 
10 using namespace Sph;
11 
13 public:
14  virtual Array<Vector> generate(IScheduler&, const Size n, const IDomain& domain) const override {
15  const Box box = domain.getBoundingBox();
16  Interval inner(0.5_f * box.lower()[Y], 0.5_f * box.upper()[Y]);
17  const Float h = sqrt(1._f / n);
18  const Float dx = h;
19  const Float dy = sqrt(3._f) / 2._f * dx;
20  Array<Vector> r;
21  int row = 0;
22  Float factor = 1._f;
23  Float prevFactor = 1._f;
24  for (Float y = box.lower()[Y]; y <= box.upper()[Y]; y += factor * dy, row++) {
25  factor = (inner.contains(y)) ? sqrt(2._f) / 2._f : 1._f;
26  if (factor != prevFactor && y > 0._f) {
27  y += (1._f - sqrt(2._f) / 2._f) * dy;
28  }
29  prevFactor = factor;
30 
31  for (Float x = box.lower()[X]; x <= box.upper()[X]; x += factor * dx) {
32  Float delta = (row % 2) ? 0.5_f * factor * dx : 0._f;
33  r.push(Vector(x + delta, y, 0._f, factor * h));
34  }
35  }
36  return r;
37  }
38 };
39 
40 class KelvinHelmholtz : public IRun {
41 public:
43  // Global settings of the problem
44  this->settings.set(RunSettingsId::RUN_NAME, std::string("Kelvin-Helmholtz instability"))
45  .set(RunSettingsId::RUN_END_TIME, 8._f)
48  .set(RunSettingsId::RUN_OUTPUT_PATH, std::string(""))
49  .set(RunSettingsId::RUN_OUTPUT_NAME, std::string("kh/kh_%d.txt"))
50  .set(RunSettingsId::SPH_AV_ALPHA, 1.5_f)
51  .set(RunSettingsId::SPH_AV_BETA, 3._f)
52  .set(RunSettingsId::SPH_USE_AC, true)
64  //.set(RunSettingsId::DOMAIN_BOUNDARY, BoundaryEnum::FROZEN_PARTICLES)
65  .set(RunSettingsId::DOMAIN_SIZE, Vector(1.01_f, 1._f, 1._f));
66  }
67 
68  virtual void setUp(SharedPtr<Storage> storage) override {
69  BodySettings body;
70  body.set(BodySettingsId::DENSITY, 1._f)
79 
80  *storage = Storage(Factory::getMaterial(body));
81  AutoPtr<IDistribution> dist = makeAuto<KelvinHelmholtzDistribution>();
82  BlockDomain domain(Vector(0._f), Vector(1._f, 1._f, 1.e-3_f));
83  Array<Vector> pos = dist->generate(*scheduler, 5000, domain);
84  const Float eta = 1.5_f;
85  Size highCnt = 0._f;
86  for (Size i = 0; i < pos.size(); ++i) {
87  pos[i][H] *= eta;
88  highCnt += int(abs(pos[i][Y]) < 0.25_f);
89  }
90  // rho * S / N ??
91  const Float m = 2._f * body.get<Float>(BodySettingsId::DENSITY) * 0.5_f / highCnt;
92 
93  storage->insert<Vector>(QuantityId::POSITION, OrderEnum::SECOND, std::move(pos));
95 
96  EquationHolder eqs = getStandardEquations(settings);
97  this->solver = makeAuto<SymmetricSolver<2>>(*scheduler, settings, eqs);
98  IMaterial& mat = storage->getMaterial(0);
99  solver->create(*storage, mat);
100  MaterialInitialContext context(settings);
101  mat.create(*storage, context);
102 
107  EosMaterial& eosmat = dynamic_cast<EosMaterial&>(mat);
108  for (Size i = 0; i < rho.size(); ++i) {
109  if (abs(r[i][Y]) < 0.25_f) {
110  rho[i] *= 2._f;
111  v[i][X] = 0.5_f;
112  } else {
113  v[i][X] = -0.5_f;
114  }
115  const Float A = 0.025_f;
116  const Float lambda = 1._f / 6._f;
117  if (abs(r[i][Y] - 0.25_f) < 0.025_f) {
118  v[i][Y] = A * sin(-2._f * PI * (r[i][X] + 1._f) / lambda);
119  } else if (abs(r[i][Y] + 0.25_f) < 0.025_f) {
120  v[i][Y] = A * sin(2._f * PI * (r[i][X] + 1._f) / lambda);
121  }
122  u[i] = eosmat.getEos().getInternalEnergy(rho[i], 2.5_f);
123  }
124  }
125 
126 protected:
127  virtual void tearDown(const Storage& UNUSED(storage), const Statistics& UNUSED(stats)) override {}
128 };
129 
130 TEST_CASE("Kelvin-Helmholtz", "[kh]") {
131  KelvinHelmholtz run;
132  Storage storage;
133  run.run(storage);
134 }
const EmptyFlags EMPTY_FLAGS
Definition: Flags.h:16
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
TEST_CASE("Kelvin-Helmholtz", "[kh]")
INLINE T sin(const T f)
Definition: MathUtils.h:296
INLINE T sqrt(const T f)
Return a squared root of a value.
Definition: MathUtils.h:78
constexpr Float INFTY
Definition: MathUtils.h:38
INLINE auto abs(const T &f)
Definition: MathUtils.h:276
constexpr Float PI
Mathematical constants.
Definition: MathUtils.h:361
#define UNUSED(x)
Definition: Object.h:37
@ 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.
Includes common headers.
NAMESPACE_SPH_BEGIN EquationHolder getStandardEquations(const RunSettings &settings, const EquationHolder &other)
Standard SPH equation set, using density and specific energy as independent variables.
BasicVector< Float > Vector
Definition: Vector.h:539
@ H
Definition: Vector.h:25
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
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
Block aligned with coordinate axes, defined by its center and length of each side.
Definition: Domain.h:185
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE const Vector & lower() const
Returns lower bounds of the box.
Definition: Box.h:82
INLINE const Vector & upper() const
Returns upper bounds of the box.
Definition: Box.h:94
Material holding equation of state.
Definition: Materials.h:21
Container holding equation terms.
Definition: EquationTerm.h:238
Base class for generating vertices with specific distribution.
Definition: Distribution.h:21
virtual Array< Vector > generate(IScheduler &scheduler, const Size n, const IDomain &domain) const =0
Generates the requested number of particles in the domain.
Base class for computational domains.
Definition: Domain.h:29
virtual Box getBoundingBox() const =0
Returns the bounding box of the domain.
Material settings and functions specific for one material.
Definition: IMaterial.h:110
Defines the interface for a run.
Definition: IRun.h:61
Statistics run(Storage &storage)
Runs the simulation.
Definition: IRun.cpp:187
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE bool contains(const Float &value) const
Checks whether value is inside the interval.
Definition: Interval.h:55
virtual Array< Vector > generate(IScheduler &, const Size n, const IDomain &domain) const override
Generates the requested number of particles in the domain.
virtual void setUp(SharedPtr< Storage > storage) override
Prepares the run, creates logger, output, ...
virtual void tearDown(const Storage &UNUSED(storage), const Statistics &UNUSED(stats)) override
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
Object holding various statistics about current run.
Definition: Statistics.h:22
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
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
@ 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.
@ TEXT_FILE
Formatted human-readable text file.
@ EULER_EXPLICIT
Explicit (forward) 1st-order integration.
@ CUBIC_SPLINE
M4 B-spline (piecewise cubic polynomial)
@ NONE
No fragmentation.
@ IDEAL_GAS
Equation of state for ideal gas.
@ 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.
@ 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.
@ PERIODIC
Periodic boundary conditions.
@ RUN_OUTPUT_INTERVAL
Time interval of dumping data to disk.
@ TIMESTEPPING_MAX_TIMESTEP
@ SPH_USE_AC
Enables the artificial thermal conductivity term.
@ 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
@ SPH_ADAPTIVE_SMOOTHING_LENGTH
Solution for evolutions of the smoothing length.
@ RUN_OUTPUT_TYPE
Selected format of the output file, see IoEnum.
@ TIMESTEPPING_INTEGRATOR
Selected timestepping integrator.
@ TIMESTEPPING_COURANT_NUMBER
Courant number.
@ SPH_FINDER
Structure for searching nearest neighbours of particles.
@ DOMAIN_TYPE
Computational domain, enforced by boundary conditions.
@ RUN_NAME
User-specified name of the run, used in some output files.
@ DOMAIN_SIZE
(Vector) size of a block domain
@ DOMAIN_BOUNDARY
Type of boundary conditions.
@ RUN_OUTPUT_NAME
File name of the output file (including extension), where d is a placeholder for output number.
@ BLOCK
Block with edge sizes given by vector.
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