SPHSolver2.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_SPH_SOLVER2_HPP
12 #define CUBBYFLOW_SPH_SOLVER2_HPP
13 
16 
17 namespace CubbyFlow
18 {
33 {
34  public:
35  class Builder;
36 
38  SPHSolver2();
39 
42  SPHSolver2(double targetDensity, double targetSpacing,
43  double relativeKernelRadius);
44 
46  SPHSolver2(const SPHSolver2&) = delete;
47 
49  SPHSolver2(SPHSolver2&&) noexcept = delete;
50 
52  ~SPHSolver2() override = default;
53 
55  SPHSolver2& operator=(const SPHSolver2&) = delete;
56 
58  SPHSolver2& operator=(SPHSolver2&&) noexcept = delete;
59 
61  [[nodiscard]] double GetEosExponent() const;
62 
70  void SetEosExponent(double newEosExponent);
71 
73  [[nodiscard]] double GetNegativePressureScale() const;
74 
83  void SetNegativePressureScale(double newNegativePressureScale);
84 
86  [[nodiscard]] double GetViscosityCoefficient() const;
87 
89  void SetViscosityCoefficient(double newViscosityCoefficient);
90 
92  [[nodiscard]] double GetPseudoViscosityCoefficient() const;
93 
100  void SetPseudoViscosityCoefficient(double newPseudoViscosityCoefficient);
101 
103  [[nodiscard]] double GetSpeedOfSound() const;
104 
112  void SetSpeedOfSound(double newSpeedOfSound);
113 
121  [[nodiscard]] double GetTimeStepLimitScale() const;
122 
130  void SetTimeStepLimitScale(double newScale);
131 
133  [[nodiscard]] SPHSystemData2Ptr GetSPHSystemData() const;
134 
136  [[nodiscard]] static Builder GetBuilder();
137 
138  protected:
140  [[nodiscard]] unsigned int GetNumberOfSubTimeSteps(
141  double timeIntervalInSeconds) const override;
142 
144  void AccumulateForces(double timeStepInSeconds) override;
145 
147  void OnBeginAdvanceTimeStep(double timeStepInSeconds) override;
148 
150  void OnEndAdvanceTimeStep(double timeStepInSeconds) override;
151 
154  virtual void AccumulateNonPressureForces(double timeStepInSeconds);
155 
158  virtual void AccumulatePressureForce(double timeStepInSeconds);
159 
161  void ComputePressure();
162 
165  const ConstArrayView1<double>& densities,
166  const ConstArrayView1<double>& pressures,
167  ArrayView1<Vector2D> pressureForces);
168 
172 
174  void ComputePseudoViscosity(double timeStepInSeconds);
175 
176  private:
178  double m_eosExponent = 7.0;
179 
182  double m_negativePressureScale = 0.0;
183 
185  double m_viscosityCoefficient = 0.01;
186 
190  double m_pseudoViscosityCoefficient = 10.0;
191 
195  double m_speedOfSound = 100.0;
196 
198  double m_timeStepLimitScale = 1.0;
199 };
200 
202 using SPHSolver2Ptr = std::shared_ptr<SPHSolver2>;
203 
207 template <typename DerivedBuilder>
209 {
210  public:
212  [[nodiscard]] DerivedBuilder& WithTargetDensity(double targetDensity);
213 
215  [[nodiscard]] DerivedBuilder& WithTargetSpacing(double targetSpacing);
216 
218  [[nodiscard]] DerivedBuilder& WithRelativeKernelRadius(
219  double relativeKernelRadius);
220 
221  protected:
222  double m_targetDensity = WATER_DENSITY;
223  double m_targetSpacing = 0.1;
224  double m_relativeKernelRadius = 1.8;
225 };
226 
227 template <typename T>
229 {
230  m_targetDensity = targetDensity;
231  return static_cast<T&>(*this);
232 }
233 
234 template <typename T>
236 {
237  m_targetSpacing = targetSpacing;
238  return static_cast<T&>(*this);
239 }
240 
241 template <typename T>
243  double relativeKernelRadius)
244 {
245  m_relativeKernelRadius = relativeKernelRadius;
246  return static_cast<T&>(*this);
247 }
248 
252 class SPHSolver2::Builder final : public SPHSolverBuilderBase2<Builder>
253 {
254  public:
256  [[nodiscard]] SPHSolver2 Build() const;
257 
259  [[nodiscard]] SPHSolver2Ptr MakeShared() const;
260 };
261 } // namespace CubbyFlow
262 
263 #endif
constexpr double WATER_DENSITY
Definition: Constants.hpp:303
void SetNegativePressureScale(double newNegativePressureScale)
Sets the negative pressure scale.
std::shared_ptr< SPHSolver2 > SPHSolver2Ptr
Shared pointer type for the SPHSolver2.
Definition: SPHSolver2.hpp:202
virtual void AccumulatePressureForce(double timeStepInSeconds)
Base class for SPH-based fluid solver builder.
Definition: SPHSolver2.hpp:208
SPHSystemData2Ptr GetSPHSystemData() const
Returns the SPH system data.
void OnEndAdvanceTimeStep(double timeStepInSeconds) override
Performs post-processing step before the simulation.
void SetEosExponent(double newEosExponent)
Sets the exponent part of the equation-of-state.
void ComputePseudoViscosity(double timeStepInSeconds)
Computes pseudo viscosity.
double GetNegativePressureScale() const
Returns the negative pressure scale.
double GetSpeedOfSound() const
Returns the speed of sound.
double GetViscosityCoefficient() const
Returns the viscosity coefficient.
virtual void AccumulateNonPressureForces(double timeStepInSeconds)
void SetViscosityCoefficient(double newViscosityCoefficient)
Sets the viscosity coefficient.
void SetPseudoViscosityCoefficient(double newPseudoViscosityCoefficient)
Sets the pseudo viscosity coefficient.
DerivedBuilder & WithRelativeKernelRadius(double relativeKernelRadius)
Returns builder with relative kernel radius.
Definition: SPHSolver2.hpp:242
double GetPseudoViscosityCoefficient() const
Returns the pseudo viscosity coefficient.
void AccumulateForces(double timeStepInSeconds) override
Accumulates the force to the forces array in the particle system.
Definition: pybind11Utils.hpp:20
void ComputePressure()
Computes the pressure.
Front-end to create SPHSolver2 objects step by step.
Definition: SPHSolver2.hpp:252
DerivedBuilder & WithTargetSpacing(double targetSpacing)
Returns builder with target spacing.
Definition: SPHSolver2.hpp:235
SPHSolver2 & operator=(const SPHSolver2 &)=delete
Deleted copy assignment operator.
Basic 2-D particle system solver.
Definition: ParticleSystemSolver2.hpp:36
SPHSolver2()
Constructs a solver with empty particle set.
unsigned int GetNumberOfSubTimeSteps(double timeIntervalInSeconds) const override
Returns the number of sub-time-steps.
static Builder GetBuilder()
Returns builder fox SPHSolver2.
double GetTimeStepLimitScale() const
Multiplier that scales the max allowed time-step.
~SPHSolver2() override=default
Default virtual destructor.
Generic N-dimensional array class interface.
Definition: Array.hpp:32
void SetTimeStepLimitScale(double newScale)
Sets the multiplier that scales the max allowed time-step.
void SetSpeedOfSound(double newSpeedOfSound)
Sets the speed of sound.
std::shared_ptr< SPHSystemData2 > SPHSystemData2Ptr
Shared pointer for the SPHSystemData2 type.
Definition: SPHSystemData.hpp:253
DerivedBuilder & WithTargetDensity(double targetDensity)
Returns builder with target density.
Definition: SPHSolver2.hpp:228
double GetEosExponent() const
Returns the exponent part of the equation-of-state.
2-D SPH solver.
Definition: SPHSolver2.hpp:32
void OnBeginAdvanceTimeStep(double timeStepInSeconds) override
Performs pre-processing step before the simulation.