SPHSolver3.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_SOLVER3_HPP
12 #define CUBBYFLOW_SPH_SOLVER3_HPP
13 
16 
17 namespace CubbyFlow
18 {
33 {
34  public:
35  class Builder;
36 
38  SPHSolver3();
39 
42  SPHSolver3(double targetDensity, double targetSpacing,
43  double relativeKernelRadius);
44 
46  SPHSolver3(const SPHSolver3&) = delete;
47 
49  SPHSolver3(SPHSolver3&&) noexcept = delete;
50 
52  ~SPHSolver3() override = default;
53 
55  SPHSolver3& operator=(const SPHSolver3&) = delete;
56 
58  SPHSolver3& operator=(SPHSolver3&&) 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]] SPHSystemData3Ptr 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<Vector3D> 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 SPHSolver3Ptr = std::shared_ptr<SPHSolver3>;
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 SPHSolver3::Builder final : public SPHSolverBuilderBase3<Builder>
253 {
254  public:
256  [[nodiscard]] SPHSolver3 Build() const;
257 
259  [[nodiscard]] SPHSolver3Ptr MakeShared() const;
260 };
261 } // namespace CubbyFlow
262 
263 #endif
constexpr double WATER_DENSITY
Definition: Constants.hpp:303
void OnBeginAdvanceTimeStep(double timeStepInSeconds) override
Performs pre-processing step before the simulation.
DerivedBuilder & WithRelativeKernelRadius(double relativeKernelRadius)
Returns builder with relative kernel radius.
Definition: SPHSolver3.hpp:242
void SetTimeStepLimitScale(double newScale)
Sets the multiplier that scales the max allowed time-step.
void SetPseudoViscosityCoefficient(double newPseudoViscosityCoefficient)
Sets the pseudo viscosity coefficient.
void SetEosExponent(double newEosExponent)
Sets the exponent part of the equation-of-state.
DerivedBuilder & WithTargetSpacing(double targetSpacing)
Returns builder with target spacing.
Definition: SPHSolver3.hpp:235
3-D SPH solver.
Definition: SPHSolver3.hpp:32
Basic 3-D particle system solver.
Definition: ParticleSystemSolver3.hpp:36
void SetSpeedOfSound(double newSpeedOfSound)
Sets the speed of sound.
void SetViscosityCoefficient(double newViscosityCoefficient)
Sets the viscosity coefficient.
double GetEosExponent() const
Returns the exponent part of the equation-of-state.
double GetNegativePressureScale() const
Returns the negative pressure scale.
~SPHSolver3() override=default
Default virtual destructor.
void OnEndAdvanceTimeStep(double timeStepInSeconds) override
Performs post-processing step before the simulation.
Definition: pybind11Utils.hpp:20
void AccumulateForces(double timeStepInSeconds) override
Accumulates the force to the forces array in the particle system.
DerivedBuilder & WithTargetDensity(double targetDensity)
Returns builder with target density.
Definition: SPHSolver3.hpp:228
SPHSolver3 & operator=(const SPHSolver3 &)=delete
Deleted copy assignment operator.
Front-end to create SPHSolver3 objects step by step.
Definition: SPHSolver3.hpp:252
SPHSolver3()
Constructs a solver with empty particle set.
static Builder GetBuilder()
Returns builder fox SPHSolver3.
unsigned int GetNumberOfSubTimeSteps(double timeIntervalInSeconds) const override
Returns the number of sub-time-steps.
double GetSpeedOfSound() const
Returns the speed of sound.
Generic N-dimensional array class interface.
Definition: Array.hpp:32
double GetTimeStepLimitScale() const
Multiplier that scales the max allowed time-step.
void ComputePseudoViscosity(double timeStepInSeconds)
Computes pseudo viscosity.
Base class for SPH-based fluid solver builder.
Definition: SPHSolver3.hpp:208
SPHSystemData3Ptr GetSPHSystemData() const
Returns the SPH system data.
std::shared_ptr< SPHSolver3 > SPHSolver3Ptr
Shared pointer type for the SPHSolver3.
Definition: SPHSolver3.hpp:202
double GetViscosityCoefficient() const
Returns the viscosity coefficient.
virtual void AccumulateNonPressureForces(double timeStepInSeconds)
double GetPseudoViscosityCoefficient() const
Returns the pseudo viscosity coefficient.
void SetNegativePressureScale(double newNegativePressureScale)
Sets the negative pressure scale.
void ComputePressure()
Computes the pressure.
std::shared_ptr< SPHSystemData3 > SPHSystemData3Ptr
Shared pointer for the SPHSystemData3 type.
Definition: SPHSystemData.hpp:256
virtual void AccumulatePressureForce(double timeStepInSeconds)