SPH
SimpleSolver.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "physics/Eos.h"
5 #include "quantities/Quantity.h"
6 #include "sph/Materials.h"
7 #include "sph/kernel/Kernel.h"
8 #include "system/Factory.h"
9 #include "thread/ThreadLocal.h"
10 #include "timestepping/ISolver.h"
11 
13 
15 class SimpleSolver : public ISolver {
16 private:
18 
20  IScheduler& scheduler;
21 
23  LutKernel<DIMENSIONS> kernel;
24 
25  struct ThreadData {
28 
30  Array<Size> idxs;
31 
33  Array<Vector> grads;
34  };
35 
36  ThreadLocal<ThreadData> threadData;
37 
38 public:
39  SimpleSolver(IScheduler& scheduler, const RunSettings& settings)
40  : scheduler(scheduler)
41  , threadData(scheduler) {
42  finder = Factory::getFinder(settings);
43  kernel = Factory::getKernel<DIMENSIONS>(settings);
44  }
45 
46  virtual void integrate(Storage& storage, Statistics& UNUSED(stats)) override {
47  // compute pressure and sound speed using equation of state
48  IMaterial& material = storage.getMaterial(0);
49  const IEos& eos = dynamic_cast<EosMaterial&>(material).getEos();
54  parallelFor(scheduler, 0, p.size(), [&](const Size i) { //
55  tie(p[i], cs[i]) = eos.evaluate(rho[i], u[i]);
56  });
57 
58  // build the structure for finding neighbors
60  finder->build(scheduler, r);
61 
62  // find the largest smoothing length
63  const auto maxHIter = std::max_element(
64  r.begin(), r.end(), [](const Vector& r1, const Vector& r2) { return r1[H] < r2[H]; });
65  const Float searchRadius = (*maxHIter)[H] * kernel.radius();
66 
72  auto functor = [&](Size i, ThreadData& data) {
73  finder->findAll(r[i], searchRadius, data.neighs);
74  for (const NeighbourRecord& n : data.neighs) {
75  const Size j = n.index;
76  const Float h_bar = 0.5_f * (r[i][H] + r[j][H]);
77  if (sqr(h_bar * kernel.radius()) > n.distanceSqr) {
78  // not actual neighbor
79  continue;
80  }
81 
82  const Vector grad = kernel.grad(r[j] - r[i], h_bar);
83 
84  // equation of motion
85  dv[i] -= m[j] * (p[i] / sqr(rho[i]) + p[j] / sqr(rho[j])) * grad;
86 
87  // continuity equation
88  drho[i] += m[j] * dot(v[j] - v[i], grad);
89 
90  // energy equation
91  du[i] += m[j] * p[i] / sqr(rho[i]) * dot(v[j] - v[i], grad);
92  }
93  };
94  parallelFor(scheduler, threadData, 0, r.size(), functor);
95  }
96 
97  virtual void create(Storage& storage, IMaterial& material) const override {
98  storage.insert<Float>(
101 
102  storage.insert<Float>(
105 
108  }
109 };
110 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
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
Base interface for all solvers.
SPH kernels.
SPH-specific implementation of particle materials.
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
#define UNUSED(x)
Definition: Object.h:37
#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.
@ FIRST
Quantity with 1st derivative.
@ ZERO
Quantity without derivatives, or "zero order" of quantity.
INLINE void parallelFor(IScheduler &scheduler, const Size from, const Size to, TFunctor &&functor)
Executes a functor concurrently from all available threads.
Definition: Scheduler.h:98
Template for thread-local storage.
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
INLINE TCounter size() const
Definition: ArrayView.h:101
INLINE Iterator< StorageType > begin()
Definition: ArrayView.h:55
INLINE Iterator< StorageType > end()
Definition: ArrayView.h:63
Material holding equation of state.
Definition: Materials.h:21
virtual Size findAll(const Size index, const Float radius, Array< NeighbourRecord > &neighbours) const =0
Finds all neighbours within given radius from the point given by index.
Base class for equations of state.
Definition: Eos.h:16
Material settings and functions specific for one material.
Definition: IMaterial.h:110
INLINE TValue getParam(const BodySettingsId paramIdx) const
Returns a parameter associated with given particle.
Definition: IMaterial.h:137
void setRange(const QuantityId id, const Interval &range, const Float minimal)
Sets the timestepping parameters of given quantity.
Definition: IMaterial.cpp:17
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Base class for all solvers.
Definition: ISolver.h:20
void build(IScheduler &scheduler, ArrayView< const Vector > points, Flags< FinderFlag > flags=FinderFlag::MAKE_RANK)
Constructs the struct with an array of vectors.
INLINE Vector grad(const Vector &r, const Float h) const noexcept
Definition: Kernel.h:32
INLINE Float radius() const noexcept
Definition: Kernel.h:107
Minimalistic SPH solver, mainly used for benchmarking and educational purposes.
Definition: SimpleSolver.h:15
virtual void create(Storage &storage, IMaterial &material) const override
Initializes all quantities needed by the solver in the storage.
Definition: SimpleSolver.h:97
virtual void integrate(Storage &storage, Statistics &UNUSED(stats)) override
Definition: SimpleSolver.h:46
SimpleSolver(IScheduler &scheduler, const RunSettings &settings)
Definition: SimpleSolver.h:39
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
Array< TValue > & getD2t(const QuantityId key)
Retrieves a quantity second derivative from the storage, given its key and value type.
Definition: Storage.cpp:243
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
Creating code components based on values from settings.
@ ENERGY_MIN
Estimated minimal value of energy used to determine timestepping error.
@ DENSITY_RANGE
Allowed range of density. Densities of all particles all clamped to fit in the range.
@ ENERGY
Initial specific internal energy.
@ DENSITY
Density at zero pressure.
@ ENERGY_RANGE
Allowed range of specific internal energy.
AutoPtr< ISymmetricFinder > getFinder(const RunSettings &settings)
Definition: Factory.cpp:156
AutoPtr< IEos > getEos(const BodySettings &settings)
Definition: Factory.cpp:47
Holds information about a neighbour particles.
Size index
Index of particle in the storage.
Float distanceSqr
Squared distance of the particle from the queried particle / position.