SPH
Stellar.cpp
Go to the documentation of this file.
1 #include "sph/initial/Stellar.h"
4 #include "physics/Constants.h"
5 #include "physics/Eos.h"
6 #include "quantities/Quantity.h"
7 #include "sph/Materials.h"
9 #include "system/Factory.h"
10 #include "thread/Scheduler.h"
11 
13 
14 Lut<Float> Stellar::solveLaneEmden(const Float n, const Float dz, const Float z_max) {
15  Float phi = 1._f;
16  Float dphi = 0._f;
17  const Float z0 = 1.e-3_f;
18  Float z = z0;
19  Array<Float> solution;
20  while (phi >= 0._f && z < z_max) {
21  solution.push(phi);
22  const Float d2phi = -2._f / z * dphi - pow(phi, n);
23  dphi += d2phi * dz;
24  phi += dphi * dz;
25  z += dz;
26  }
27  return Lut<Float>(Interval(z0, z), std::move(solution));
28 }
29 
30 /*Float Stellar::PolytropeParams::polytropeConstant() const {*/
31 /* const Float R = Constants::gasConstant;
32  const Float a = Constants::radiationDensity;
33  return 1._f / beta * R / mu * cbrt((1._f - beta) / beta * 3._f * R / (mu * a));*/
34 /*return cbrt(3._f / PI) * Constants::planckConstant * Constants::speedOfLight /
35  (8._f * pow(Constants::atomicMass, 4._f / 3._f));
36 }*/
37 
39  const Float G = Constants::gravity;
40 
41  Lut<Float> phi = solveLaneEmden(n);
42  const Float z_star = phi.getRange().upper();
43  const Float dphi_star = phi.derivative()(z_star);
44 
45  const Float rho_c = 3._f * mass / (4._f * PI * pow<3>(radius)) * z_star / (-3._f * dphi_star);
46  const Float P_c = G * sqr(mass) / pow<4>(radius) * 1._f / (-4._f * PI * (n + 1) * z_star * dphi_star);
47 
48  IdealGasEos eos((n + 1._f) / n);
49  Array<Float> rho(phi.size()), u(phi.size()), P(phi.size());
50  for (auto el : iterateWithIndex(phi)) {
51  const Size i = el.index();
52  const Float x = el.value().y;
53  rho[i] = rho_c * pow(x, n);
54  P[i] = P_c * pow(x, n + 1);
55  u[i] = eos.getInternalEnergy(rho[i], P[i]);
56  }
57 
58  Star star;
59  const Interval range(0._f, radius);
60  star.p = Lut<Float>(range, std::move(P));
61  star.rho = Lut<Float>(range, std::move(rho));
62  star.u = Lut<Float>(range, std::move(u));
63 
64  return star;
65 }
66 
68  const Size N,
69  const Float radius,
70  const Float mass,
71  const Float n) {
72  SphericalDomain domain(Vector(0._f), radius);
73  Array<Vector> points = distribution.generate(SEQUENTIAL, N, domain);
74 
75  Star star = polytropicStar(radius, mass, n);
76 
77  Array<Float> m(points.size());
78  Array<Float> rho(points.size());
79  Array<Float> u(points.size());
80  Array<Float> p(points.size());
81  const Float v = domain.getVolume() / points.size();
82  for (Size i = 0; i < points.size(); ++i) {
83  const Float r = getLength(points[i]);
84  rho[i] = star.rho(r);
85  u[i] = star.u(r);
86  p[i] = star.p(r);
87  m[i] = rho[i] * v;
88  }
89 
90  BodySettings body;
92  body.set(BodySettingsId::ADIABATIC_INDEX, (n + 1._f) / n);
96 
99  for (Size i = 0; i < points.size(); ++i) {
100  points[i][H] *= eta;
101  }
102 
103  Storage storage(makeShared<EosMaterial>(body));
104 
105  storage.insert<Vector>(QuantityId::POSITION, OrderEnum::SECOND, std::move(points));
106  storage.insert<Float>(QuantityId::MASS, OrderEnum::ZERO, std::move(m));
107  storage.insert<Float>(QuantityId::ENERGY, OrderEnum::ZERO, std::move(u));
108  storage.insert<Float>(QuantityId::DENSITY, OrderEnum::ZERO, std::move(rho));
109  storage.insert<Float>(QuantityId::PRESSURE, OrderEnum::ZERO, std::move(p));
111 
112  return storage;
113 }
114 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Definitions of physical constaints (in SI units).
const float radius
Definition: CurveDialog.cpp:18
Filling spatial domain with SPH particles.
Object defining computational domain.
Equations of state.
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
Helper objects allowing to iterate in reverse, iterate over multiple containeres, etc.
IndexAdapter< TContainer > iterateWithIndex(TContainer &&container)
SPH-specific implementation of particle materials.
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
constexpr INLINE Float pow< 3 >(const Float v)
Definition: MathUtils.h:132
constexpr INLINE Float pow< 4 >(const Float v)
Definition: MathUtils.h:136
constexpr INLINE Float pow(const Float v)
Power for floats.
constexpr Float INFTY
Definition: MathUtils.h:38
constexpr Float PI
Mathematical constants.
Definition: MathUtils.h:361
#define NAMESPACE_SPH_END
Definition: Object.h:12
@ 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.
@ SOUND_SPEED
Sound speed, always a scalar quantity.
Holder of quantity values and their temporal derivatives.
@ SECOND
Quantity with 1st and 2nd derivative.
@ ZERO
Quantity without derivatives, or "zero order" of quantity.
SequentialScheduler SEQUENTIAL
Global instance of the sequential scheduler.
Definition: Scheduler.cpp:43
Interface for executing tasks (potentially) asynchronously.
constexpr int N
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
@ H
Definition: Vector.h:25
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
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.
Equation of state for ideal gas.
Definition: Eos.h:30
virtual Float getInternalEnergy(const Float rho, const Float p) const override
Inverted function; computes specific internal energy u from given density rho and pressure p.
Definition: Eos.cpp:20
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE Float upper() const
Returns upper bound of the interval.
Definition: Interval.h:79
Lut derivative() const
Computes the derivative of the function.
Definition: Lut.h:120
Interval getRange() const
Returns the definition interval of the function.
Definition: Lut.h:115
Size size() const
Returns the number of tabulated value.
Definition: Lut.h:110
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
Spherical domain, defined by the center of sphere and its radius.
Definition: Domain.h:102
virtual Float getVolume() const override
Returns the total volume of the domain.
Definition: Domain.cpp:18
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
Creating code components based on values from settings.
@ NONE
Gass or material with no stress tensor.
@ 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.
@ ADIABATIC_INDEX
Adiabatic index used by some equations of state (such as ideal gas)
@ 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.
@ RHEOLOGY_YIELDING
Model of stress reducing used within the rheological model.
constexpr Float gravity
Gravitational constant (CODATA 2014)
Definition: Constants.h:29
Star polytropicStar(const Float radius, const Float mass, const Float n)
Computes radial profiles of state quantities for a polytropic star.
Definition: Stellar.cpp:38
Lut< Float > solveLaneEmden(const Float n, const Float dz=1.e-3_f, const Float z_max=1.e3_f)
Solves the Lane-Emden equation given the polytrope index.
Definition: Stellar.cpp:14
Storage generateIc(const IDistribution &distribution, const Size particleCnt, const Float radius, const Float mass, const Float n)
Creates a spherical polytropic star.
Definition: Stellar.cpp:67
Lut< Float > u
Definition: Stellar.h:15
Lut< Float > p
Definition: Stellar.h:16
Lut< Float > rho
Definition: Stellar.h:14