SPH
VanDerWaals.cpp
Go to the documentation of this file.
1 #include "Sph.h"
2 #include <iostream>
3 
4 using namespace Sph;
5 
6 // van der Waals equation state, implementing the IEos interface.
7 class VanDerWaalsEos : public IEos {
8 private:
9  // van der Waals contants
10  Float a, b;
11 
12  // adiabatic index
13  Float gamma;
14 
15 public:
16  VanDerWaalsEos(const Float a, const Float b, const Float gamma)
17  : a(a)
18  , b(b)
19  , gamma(gamma) {}
20 
21  virtual Pair<Float> evaluate(const Float rho, const Float u) const override {
22  // returns a pair of values - pressure and sound speed at given density and internal energy
23  const Float R = Constants::gasConstant;
24 
25  // first, compute the temperature
26  const Float v = 1._f / rho;
27  const Float T = 2._f * (u + a / v) / (3._f * R);
28 
29  // now compute the pressure
30  const Float p = max(R * T / (v - b) - a / sqr(v), EPS);
31  SPH_ASSERT(isReal(p));
32 
34  const Float cs = sqrt(gamma * p / rho);
35  SPH_ASSERT(isReal(cs));
36 
37  return { p, cs };
38  }
39 
40 
41  virtual Float getInternalEnergy(const Float, const Float) const override {
42  // not needed for this example
44  }
45 
46  virtual Float getDensity(const Float, const Float) const override {
47  // not needed for this example
49  }
50 };
51 
52 class VanDerWallsSimulation : public IRun {
53 public:
54  virtual void setUp(SharedPtr<Storage> storage) override {
55  settings.set(RunSettingsId::RUN_NAME, std::string("Van der Waals"));
56 
57  // We simulate a gass, so the only force in the system is due to pressure gradient
59 
60  InitialConditions ic(settings);
61  SphericalDomain domain(Vector(0._f), 1.e3_f);
62  BodySettings body;
64  body.set(BodySettingsId::DENSITY, 2._f); // 2 kg/m^3
65 
66  // Prepare our custom EoS
67  AutoPtr<VanDerWaalsEos> eos = makeAuto<VanDerWaalsEos>(1.38_f, 0.032_f, 1.4_f);
68 
69  // Create new material; this allows us to specify a custom EoS.
70  AutoPtr<EosMaterial> material = makeAuto<EosMaterial>(body, std::move(eos));
71 
72  // Add the sphere with van der Waals gass into the particle storage; note that here we pass the
73  // material instead of 'body' parameter
74  ic.addMonolithicBody(*storage, domain, std::move(material));
75  }
76 
77  virtual void tearDown(const Storage& UNUSED(storage), const Statistics& UNUSED(stats)) override {}
78 };
79 
80 int main() {
81  try {
82  VanDerWallsSimulation simulation;
83  Storage storage;
84  simulation.run(storage);
85  } catch (const Exception& e) {
86  std::cout << "Error in simulation: " << e.what() << std::endl;
87  return -1;
88  }
89  return 0;
90 }
INLINE bool isReal(const AntisymmetricTensor &t)
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
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 EPS
Definition: MathUtils.h:30
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
INLINE T sqrt(const T f)
Return a squared root of a value.
Definition: MathUtils.h:78
#define UNUSED(x)
Definition: Object.h:37
Includes common headers.
int main()
Definition: VanDerWaals.cpp:80
BasicVector< Float > Vector
Definition: Vector.h:539
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
Generic exception.
Definition: Exceptions.h:10
virtual const char * what() const noexcept
Definition: Exceptions.h:18
Base class for equations of state.
Definition: Eos.h:16
Defines the interface for a run.
Definition: IRun.h:61
Statistics run(Storage &storage)
Runs the simulation.
Definition: IRun.cpp:187
Object for adding one or more bodies with given material into a Storage.
Definition: Initial.h:106
BodyView addMonolithicBody(Storage &storage, const BodySettings &body)
Creates a monolithic body by filling given domain with particles.
Definition: Initial.cpp:81
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
Spherical domain, defined by the center of sphere and its radius.
Definition: Domain.h:102
Array with fixed number of allocated elements.
Definition: StaticArray.h:19
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
VanDerWaalsEos(const Float a, const Float b, const Float gamma)
Definition: VanDerWaals.cpp:16
virtual Float getInternalEnergy(const Float, const Float) const override
Inverted function; computes specific internal energy u from given density rho and pressure p.
Definition: VanDerWaals.cpp:41
virtual Pair< Float > evaluate(const Float rho, const Float u) const override
Computes pressure and local sound speed from given density rho and specific internal energy u.
Definition: VanDerWaals.cpp:21
virtual Float getDensity(const Float, const Float) const override
Inverted function; computes density from pressure p and internal energy u.
Definition: VanDerWaals.cpp:46
virtual void tearDown(const Storage &UNUSED(storage), const Statistics &UNUSED(stats)) override
Definition: VanDerWaals.cpp:77
virtual void setUp(SharedPtr< Storage > storage) override
Prepares the run, creates logger, output, ...
Definition: VanDerWaals.cpp:54
@ PRESSURE
Use force from pressure gradient in the solver.
@ PARTICLE_COUNT
Number of SPH particles in the body.
@ DENSITY
Density at zero pressure.
@ SPH_SOLVER_FORCES
List of forces to compute by the solver.
@ RUN_NAME
User-specified name of the run, used in some output files.
constexpr Float gasConstant
Definition: Constants.h:14
Definition: MemoryPool.h:5