GridFluidSolver3.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_GRID_FLUID_SOLVER3_HPP
12 #define CUBBYFLOW_GRID_FLUID_SOLVER3_HPP
13 
21 
22 namespace CubbyFlow
23 {
36 {
37  public:
38  class Builder;
39 
42 
44  GridFluidSolver3(const Vector3UZ& resolution, const Vector3D& gridSpacing,
45  const Vector3D& gridOrigin);
46 
48  GridFluidSolver3(const GridFluidSolver3&) = delete;
49 
51  GridFluidSolver3(GridFluidSolver3&&) noexcept = delete;
52 
54  ~GridFluidSolver3() override = default;
55 
58 
60  GridFluidSolver3& operator=(GridFluidSolver3&&) noexcept = delete;
61 
63  [[nodiscard]] const Vector3D& GetGravity() const;
64 
66  void SetGravity(const Vector3D& newGravity);
67 
69  [[nodiscard]] double GetViscosityCoefficient() const;
70 
79  void SetViscosityCoefficient(double newValue);
80 
87  [[nodiscard]] double GetCFL(double timeIntervalInSeconds) const;
88 
90  [[nodiscard]] double GetMaxCFL() const;
91 
93  void SetMaxCFL(double newCFL);
94 
96  [[nodiscard]] bool GetUseCompressedLinearSystem() const;
97 
99  void SetUseCompressedLinearSystem(bool isOn);
100 
102  [[nodiscard]] const AdvectionSolver3Ptr& GetAdvectionSolver() const;
103 
105  void SetAdvectionSolver(const AdvectionSolver3Ptr& newSolver);
106 
108  [[nodiscard]] const GridDiffusionSolver3Ptr& GetDiffusionSolver() const;
109 
111  void SetDiffusionSolver(const GridDiffusionSolver3Ptr& newSolver);
112 
114  [[nodiscard]] const GridPressureSolver3Ptr& GetPressureSolver() const;
115 
117  void SetPressureSolver(const GridPressureSolver3Ptr& newSolver);
118 
120  [[nodiscard]] int GetClosedDomainBoundaryFlag() const;
121 
123  void SetClosedDomainBoundaryFlag(int flag);
124 
134  [[nodiscard]] const GridSystemData3Ptr& GetGridSystemData() const;
135 
148  void ResizeGrid(const Vector3UZ& newSize, const Vector3D& newGridSpacing,
149  const Vector3D& newGridOrigin) const;
150 
158  [[nodiscard]] Vector3UZ GetResolution() const;
159 
167  [[nodiscard]] Vector3D GetGridSpacing() const;
168 
176  [[nodiscard]] Vector3D GetGridOrigin() const;
177 
184  [[nodiscard]] const FaceCenteredGrid3Ptr& GetVelocity() const;
185 
187  [[nodiscard]] const Collider3Ptr& GetCollider() const;
188 
190  void SetCollider(const Collider3Ptr& newCollider);
191 
193  [[nodiscard]] const GridEmitter3Ptr& GetEmitter() const;
194 
196  void SetEmitter(const GridEmitter3Ptr& newEmitter);
197 
199  [[nodiscard]] static Builder GetBuilder();
200 
201  protected:
203  void OnInitialize() override;
204 
206  void OnAdvanceTimeStep(double timeIntervalInSeconds) override;
207 
218  [[nodiscard]] unsigned int GetNumberOfSubTimeSteps(
219  double timeIntervalInSeconds) const override;
220 
222  virtual void OnBeginAdvanceTimeStep(double timeIntervalInSeconds);
223 
225  virtual void OnEndAdvanceTimeStep(double timeIntervalInSeconds);
226 
235  virtual void ComputeExternalForces(double timeIntervalInSeconds);
236 
238  virtual void ComputeViscosity(double timeIntervalInSeconds);
239 
241  virtual void ComputePressure(double timeIntervalInSeconds);
242 
244  virtual void ComputeAdvection(double timeIntervalInSeconds);
245 
255  [[nodiscard]] virtual ScalarField3Ptr GetFluidSDF() const;
256 
258  void ComputeGravity(double timeIntervalInSeconds);
259 
266  void ApplyBoundaryCondition() const;
267 
270 
273 
276 
278  [[nodiscard]] ScalarField3Ptr GetColliderSDF() const;
279 
281  [[nodiscard]] VectorField3Ptr GetColliderVelocityField() const;
282 
283  private:
284  void BeginAdvanceTimeStep(double timeIntervalInSeconds);
285 
286  void EndAdvanceTimeStep(double timeIntervalInSeconds);
287 
288  void UpdateCollider(double timeIntervalInSeconds) const;
289 
290  void UpdateEmitter(double timeIntervalInSeconds) const;
291 
292  GridSystemData3Ptr m_grids;
293  Collider3Ptr m_collider;
294  GridEmitter3Ptr m_emitter;
295 
296  AdvectionSolver3Ptr m_advectionSolver;
297  GridDiffusionSolver3Ptr m_diffusionSolver;
298  GridPressureSolver3Ptr m_pressureSolver;
299  GridBoundaryConditionSolver3Ptr m_boundaryConditionSolver;
300 
301  Vector3D m_gravity = Vector3D{ 0.0, -9.8, 0.0 };
302  double m_viscosityCoefficient = 0.0;
303  double m_maxCFL = 5.0;
304  int m_closedDomainBoundaryFlag = DIRECTION_ALL;
305  bool m_useCompressedLinearSys = false;
306 };
307 
309 using GridFluidSolver3Ptr = std::shared_ptr<GridFluidSolver3>;
310 
314 template <typename DerivedBuilder>
316 {
317  public:
319  [[nodiscard]] DerivedBuilder& WithResolution(const Vector3UZ& resolution);
320 
322  [[nodiscard]] DerivedBuilder& WithGridSpacing(const Vector3D& gridSpacing);
323 
325  [[nodiscard]] DerivedBuilder& WithGridSpacing(double gridSpacing);
326 
333  [[nodiscard]] DerivedBuilder& WithDomainSizeX(double domainSizeX);
334 
336  [[nodiscard]] DerivedBuilder& WithOrigin(const Vector3D& gridOrigin);
337 
338  protected:
339  [[nodiscard]] Vector3D GetGridSpacing() const;
340 
341  Vector3UZ m_resolution{ 1, 1, 1 };
342  Vector3D m_gridSpacing{ 1, 1, 1 };
343  Vector3D m_gridOrigin{ 0, 0, 0 };
344  double m_domainSizeX = 1.0;
345  bool m_useDomainSize = false;
346 };
347 
348 template <typename T>
350 {
351  m_resolution = resolution;
352  return static_cast<T&>(*this);
353 }
354 
355 template <typename T>
357 {
358  m_gridSpacing = gridSpacing;
359  m_useDomainSize = false;
360  return static_cast<T&>(*this);
361 }
362 
363 template <typename T>
365 {
366  m_gridSpacing.x = gridSpacing;
367  m_gridSpacing.y = gridSpacing;
368  m_gridSpacing.z = gridSpacing;
369  m_useDomainSize = false;
370  return static_cast<T&>(*this);
371 }
372 
373 template <typename T>
375 {
376  m_domainSizeX = domainSizeX;
377  m_useDomainSize = true;
378  return static_cast<T&>(*this);
379 }
380 
381 template <typename T>
383 {
384  m_gridOrigin = gridOrigin;
385  return static_cast<T&>(*this);
386 }
387 
388 template <typename T>
390 {
391  Vector3D gridSpacing = m_gridSpacing;
392 
393  if (m_useDomainSize)
394  {
395  gridSpacing.Fill(m_domainSizeX / static_cast<double>(m_resolution.x));
396  }
397 
398  return gridSpacing;
399 }
400 
405  : public GridFluidSolverBuilderBase3<Builder>
406 {
407  public:
409  [[nodiscard]] GridFluidSolver3 Build() const;
410 
412  [[nodiscard]] GridFluidSolver3Ptr MakeShared() const
413  {
414  return std::make_shared<GridFluidSolver3>(
415  m_resolution, GetGridSpacing(), m_gridOrigin);
416  }
417 };
418 } // namespace CubbyFlow
419 
420 #endif
virtual void ComputeViscosity(double timeIntervalInSeconds)
Computes the viscosity term using the diffusion solver.
const AdvectionSolver3Ptr & GetAdvectionSolver() const
Returns the advection solver instance.
std::shared_ptr< Collider3 > Collider3Ptr
Shared pointer type for the Collider3.
Definition: Collider.hpp:144
void SetMaxCFL(double newCFL)
Sets the max allowed CFL number.
static Builder GetBuilder()
Returns builder fox GridFluidSolver3.
void OnAdvanceTimeStep(double timeIntervalInSeconds) override
Called when advancing a single time-step.
virtual void ComputePressure(double timeIntervalInSeconds)
Computes the pressure term using the pressure solver.
virtual ScalarField3Ptr GetFluidSDF() const
Returns the signed-distance representation of the fluid.
void OnInitialize() override
Called when it needs to setup initial condition.
void SetCollider(const Collider3Ptr &newCollider)
Sets the collider.
std::shared_ptr< AdvectionSolver3 > AdvectionSolver3Ptr
Shared pointer type for the 3-D advection solver.
Definition: AdvectionSolver3.hpp:125
constexpr int DIRECTION_ALL
All direction.
Definition: Constants.hpp:332
void SetDiffusionSolver(const GridDiffusionSolver3Ptr &newSolver)
Sets the diffusion solver.
void SetAdvectionSolver(const AdvectionSolver3Ptr &newSolver)
Sets the advection solver.
Vector3D GetGridOrigin() const
Returns the origin of the grid system data.
Vector3UZ GetResolution() const
Returns the resolution of the grid system data.
void SetEmitter(const GridEmitter3Ptr &newEmitter)
Sets the emitter.
std::shared_ptr< ScalarField3 > ScalarField3Ptr
Shared pointer for the ScalarField3 type.
Definition: ScalarField.hpp:70
std::shared_ptr< GridEmitter3 > GridEmitter3Ptr
Shared pointer type for the GridEmitter3.
Definition: GridEmitter3.hpp:87
const FaceCenteredGrid3Ptr & GetVelocity() const
Returns the velocity field.
Abstract base class for N-D scalar grid structure.
Definition: ScalarGrid.hpp:24
std::shared_ptr< VectorField3 > VectorField3Ptr
Shared pointer for the VectorField3 type.
Definition: VectorField.hpp:90
GridFluidSolver3()
Default constructor.
DerivedBuilder & WithOrigin(const Vector3D &gridOrigin)
Returns builder with grid origin.
Definition: GridFluidSolver3.hpp:382
virtual void ComputeExternalForces(double timeIntervalInSeconds)
Computes the external force terms.
const GridEmitter3Ptr & GetEmitter() const
Returns the emitter.
std::shared_ptr< GridPressureSolver3 > GridPressureSolver3Ptr
Shared pointer type for the GridPressureSolver3.
Definition: GridPressureSolver3.hpp:96
void SetPressureSolver(const GridPressureSolver3Ptr &newSolver)
Sets the pressure solver.
void SetGravity(const Vector3D &newGravity)
Sets the gravity of the system.
std::shared_ptr< GridBoundaryConditionSolver3 > GridBoundaryConditionSolver3Ptr
Shared pointer type for the GridBoundaryConditionSolver3.
Definition: GridBoundaryConditionSolver3.hpp:117
int GetClosedDomainBoundaryFlag() const
Returns the closed domain boundary flag.
const GridPressureSolver3Ptr & GetPressureSolver() const
Returns the pressure solver instance.
VectorField3Ptr GetColliderVelocityField() const
Returns the velocity field of the collider.
const GridDiffusionSolver3Ptr & GetDiffusionSolver() const
Returns the diffusion solver instance.
void Fill(const T &val)
Definition: Matrix-Impl.hpp:226
virtual void OnBeginAdvanceTimeStep(double timeIntervalInSeconds)
Called at the beginning of a time-step.
~GridFluidSolver3() override=default
Default virtual destructor.
Base class for grid-based fluid solver builder.
Definition: GridFluidSolver3.hpp:315
std::shared_ptr< FaceCenteredGrid3 > FaceCenteredGrid3Ptr
Shared pointer type for the FaceCenteredGrid3.
Definition: FaceCenteredGrid.hpp:437
void ApplyBoundaryCondition() const
Applies the boundary condition to the velocity field.
double GetViscosityCoefficient() const
Returns the viscosity coefficient.
bool GetUseCompressedLinearSystem() const
Returns true if the solver is using compressed linear system.
Definition: Matrix.hpp:27
Abstract base class for physics-based animation.
Definition: PhysicsAnimation.hpp:24
unsigned int GetNumberOfSubTimeSteps(double timeIntervalInSeconds) const override
Returns the required sub-time-steps for given time interval.
Front-end to create GridFluidSolver3 objects step by step.
Definition: GridFluidSolver3.hpp:404
Definition: pybind11Utils.hpp:20
Vector3D GetGridSpacing() const
Definition: GridFluidSolver3.hpp:389
Abstract base class for grid-based 3-D fluid solver.
Definition: GridFluidSolver3.hpp:35
virtual void OnEndAdvanceTimeStep(double timeIntervalInSeconds)
Called at the end of a time-step.
Vector3D GetGridSpacing() const
Returns the grid spacing of the grid system data.
const GridSystemData3Ptr & GetGridSystemData() const
Returns the grid system data.
std::shared_ptr< GridDiffusionSolver3 > GridDiffusionSolver3Ptr
Shared pointer type for the GridDiffusionSolver3.
Definition: GridDiffusionSolver3.hpp:108
double GetCFL(double timeIntervalInSeconds) const
Returns the CFL number from the current velocity field for given time interval.
void SetClosedDomainBoundaryFlag(int flag)
Sets the closed domain boundary flag.
DerivedBuilder & WithGridSpacing(const Vector3D &gridSpacing)
Returns builder with grid spacing.
Definition: GridFluidSolver3.hpp:356
void SetViscosityCoefficient(double newValue)
Sets the viscosity coefficient.
N-D face-centered (a.k.a MAC or staggered) grid.
Definition: FaceCenteredGrid.hpp:31
void ExtrapolateIntoCollider(ScalarGrid3 *grid)
Extrapolates given field into the collider-occupied region.
GridFluidSolver3 & operator=(const GridFluidSolver3 &)=delete
Deleted copy assignment operator.
DerivedBuilder & WithResolution(const Vector3UZ &resolution)
Returns builder with grid resolution.
Definition: GridFluidSolver3.hpp:349
void SetUseCompressedLinearSystem(bool isOn)
Sets whether the solver should use compressed linear system.
const Collider3Ptr & GetCollider() const
Returns the collider.
std::shared_ptr< GridSystemData3 > GridSystemData3Ptr
Shared pointer type of GridSystemData3.
Definition: GridSystemData.hpp:276
double GetMaxCFL() const
Returns the max allowed CFL number.
void ResizeGrid(const Vector3UZ &newSize, const Vector3D &newGridSpacing, const Vector3D &newGridOrigin) const
Resizes grid system data.
ScalarField3Ptr GetColliderSDF() const
Returns the signed-distance field representation of the collider.
Abstract base class for N-D collocated vector grid structure.
Definition: CollocatedVectorGrid.hpp:22
std::shared_ptr< GridFluidSolver3 > GridFluidSolver3Ptr
Shared pointer type for the GridFluidSolver3.
Definition: GridFluidSolver3.hpp:309
DerivedBuilder & WithDomainSizeX(double domainSizeX)
Returns builder with domain size in x-direction.
Definition: GridFluidSolver3.hpp:374
void ComputeGravity(double timeIntervalInSeconds)
Computes the gravity term.
GridFluidSolver3Ptr MakeShared() const
Builds shared pointer of GridFluidSolver3 instance.
Definition: GridFluidSolver3.hpp:412
const Vector3D & GetGravity() const
Returns the gravity vector of the system.
virtual void ComputeAdvection(double timeIntervalInSeconds)
Computes the advection term using the advection solver.