GridFluidSolver2.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_SOLVER2_HPP
12 #define CUBBYFLOW_GRID_FLUID_SOLVER2_HPP
13 
21 
22 namespace CubbyFlow
23 {
36 {
37  public:
38  class Builder;
39 
42 
44  GridFluidSolver2(const Vector2UZ& resolution, const Vector2D& gridSpacing,
45  const Vector2D& gridOrigin);
46 
48  GridFluidSolver2(const GridFluidSolver2&) = delete;
49 
51  GridFluidSolver2(GridFluidSolver2&&) noexcept = delete;
52 
54  ~GridFluidSolver2() override = default;
55 
58 
60  GridFluidSolver2& operator=(GridFluidSolver2&&) noexcept = delete;
61 
63  [[nodiscard]] const Vector2D& GetGravity() const;
64 
66  void SetGravity(const Vector2D& 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 AdvectionSolver2Ptr& GetAdvectionSolver() const;
103 
105  void SetAdvectionSolver(const AdvectionSolver2Ptr& newSolver);
106 
108  [[nodiscard]] const GridDiffusionSolver2Ptr& GetDiffusionSolver() const;
109 
111  void SetDiffusionSolver(const GridDiffusionSolver2Ptr& newSolver);
112 
114  [[nodiscard]] const GridPressureSolver2Ptr& GetPressureSolver() const;
115 
117  void SetPressureSolver(const GridPressureSolver2Ptr& newSolver);
118 
120  [[nodiscard]] int GetClosedDomainBoundaryFlag() const;
121 
123  void SetClosedDomainBoundaryFlag(int flag);
124 
134  [[nodiscard]] const GridSystemData2Ptr& GetGridSystemData() const;
135 
148  void ResizeGrid(const Vector2UZ& newSize, const Vector2D& newGridSpacing,
149  const Vector2D& newGridOrigin) const;
150 
158  [[nodiscard]] Vector2UZ GetResolution() const;
159 
167  [[nodiscard]] Vector2D GetGridSpacing() const;
168 
176  [[nodiscard]] Vector2D GetGridOrigin() const;
177 
184  [[nodiscard]] const FaceCenteredGrid2Ptr& GetVelocity() const;
185 
187  [[nodiscard]] const Collider2Ptr& GetCollider() const;
188 
190  void SetCollider(const Collider2Ptr& newCollider);
191 
193  [[nodiscard]] const GridEmitter2Ptr& GetEmitter() const;
194 
196  void SetEmitter(const GridEmitter2Ptr& 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 ScalarField2Ptr GetFluidSDF() const;
256 
258  void ComputeGravity(double timeIntervalInSeconds);
259 
266  void ApplyBoundaryCondition() const;
267 
270 
273 
276 
278  [[nodiscard]] ScalarField2Ptr GetColliderSDF() const;
279 
281  [[nodiscard]] VectorField2Ptr 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  GridSystemData2Ptr m_grids;
293  Collider2Ptr m_collider;
294  GridEmitter2Ptr m_emitter;
295 
296  AdvectionSolver2Ptr m_advectionSolver;
297  GridDiffusionSolver2Ptr m_diffusionSolver;
298  GridPressureSolver2Ptr m_pressureSolver;
299  GridBoundaryConditionSolver2Ptr m_boundaryConditionSolver;
300 
301  Vector2D m_gravity = Vector2D{ 0.0, -9.8 };
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 GridFluidSolver2Ptr = std::shared_ptr<GridFluidSolver2>;
310 
314 template <typename DerivedBuilder>
316 {
317  public:
319  [[nodiscard]] DerivedBuilder& WithResolution(const Vector2UZ& resolution);
320 
322  [[nodiscard]] DerivedBuilder& WithGridSpacing(const Vector2D& gridSpacing);
323 
325  [[nodiscard]] DerivedBuilder& WithGridSpacing(double gridSpacing);
326 
333  [[nodiscard]] DerivedBuilder& WithDomainSizeX(double domainSizeX);
334 
336  [[nodiscard]] DerivedBuilder& WithOrigin(const Vector2D& gridOrigin);
337 
338  protected:
339  [[nodiscard]] Vector2D GetGridSpacing() const;
340 
341  Vector2UZ m_resolution{ 1, 1 };
342  Vector2D m_gridSpacing{ 1, 1 };
343  Vector2D m_gridOrigin{ 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_useDomainSize = false;
369  return static_cast<T&>(*this);
370 }
371 
372 template <typename T>
374 {
375  m_domainSizeX = domainSizeX;
376  m_useDomainSize = true;
377  return static_cast<T&>(*this);
378 }
379 
380 template <typename T>
382 {
383  m_gridOrigin = gridOrigin;
384  return static_cast<T&>(*this);
385 }
386 
387 template <typename T>
389 {
390  Vector2D gridSpacing = m_gridSpacing;
391 
392  if (m_useDomainSize)
393  {
394  gridSpacing.Fill(m_domainSizeX / static_cast<double>(m_resolution.x));
395  }
396 
397  return gridSpacing;
398 }
399 
404  : public GridFluidSolverBuilderBase2<Builder>
405 {
406  public:
408  [[nodiscard]] GridFluidSolver2 Build() const;
409 
411  [[nodiscard]] GridFluidSolver2Ptr MakeShared() const
412  {
413  return std::make_shared<GridFluidSolver2>(
414  m_resolution, GetGridSpacing(), m_gridOrigin);
415  }
416 };
417 } // namespace CubbyFlow
418 
419 #endif
void OnAdvanceTimeStep(double timeIntervalInSeconds) override
Called when advancing a single time-step.
virtual void OnEndAdvanceTimeStep(double timeIntervalInSeconds)
Called at the end of a time-step.
void SetCollider(const Collider2Ptr &newCollider)
Sets the collider.
void SetEmitter(const GridEmitter2Ptr &newEmitter)
Sets the emitter.
virtual void ComputePressure(double timeIntervalInSeconds)
Computes the pressure term using the pressure solver.
std::shared_ptr< Collider2 > Collider2Ptr
Shared pointer type for the Collider2.
Definition: Collider.hpp:141
const GridPressureSolver2Ptr & GetPressureSolver() const
Returns the pressure solver instance.
Base class for grid-based fluid solver builder.
Definition: GridFluidSolver2.hpp:315
constexpr int DIRECTION_ALL
All direction.
Definition: Constants.hpp:332
DerivedBuilder & WithDomainSizeX(double domainSizeX)
Returns builder with domain size in x-direction.
Definition: GridFluidSolver2.hpp:373
void SetMaxCFL(double newCFL)
Sets the max allowed CFL number.
void SetViscosityCoefficient(double newValue)
Sets the viscosity coefficient.
DerivedBuilder & WithResolution(const Vector2UZ &resolution)
Returns builder with grid resolution.
Definition: GridFluidSolver2.hpp:349
const GridEmitter2Ptr & GetEmitter() const
Returns the emitter.
unsigned int GetNumberOfSubTimeSteps(double timeIntervalInSeconds) const override
Returns the required sub-time-steps for given time interval.
void SetClosedDomainBoundaryFlag(int flag)
Sets the closed domain boundary flag.
Abstract base class for N-D scalar grid structure.
Definition: ScalarGrid.hpp:24
std::shared_ptr< GridBoundaryConditionSolver2 > GridBoundaryConditionSolver2Ptr
Shared pointer type for the GridBoundaryConditionSolver2.
Definition: GridBoundaryConditionSolver2.hpp:117
virtual void ComputeExternalForces(double timeIntervalInSeconds)
Computes the external force terms.
static Builder GetBuilder()
Returns builder fox GridFluidSolver2.
double GetViscosityCoefficient() const
Returns the viscosity coefficient.
void Fill(const T &val)
Definition: Matrix-Impl.hpp:226
std::shared_ptr< GridSystemData2 > GridSystemData2Ptr
Shared pointer type of GridSystemData2.
Definition: GridSystemData.hpp:273
DerivedBuilder & WithGridSpacing(const Vector2D &gridSpacing)
Returns builder with grid spacing.
Definition: GridFluidSolver2.hpp:356
std::shared_ptr< ScalarField2 > ScalarField2Ptr
Shared pointer for the ScalarField2 type.
Definition: ScalarField.hpp:67
const Vector2D & GetGravity() const
Returns the gravity vector of the system.
Vector2D GetGridSpacing() const
Returns the grid spacing of the grid system data.
const AdvectionSolver2Ptr & GetAdvectionSolver() const
Returns the advection solver instance.
virtual void ComputeViscosity(double timeIntervalInSeconds)
Computes the viscosity term using the diffusion solver.
int GetClosedDomainBoundaryFlag() const
Returns the closed domain boundary flag.
std::shared_ptr< GridFluidSolver2 > GridFluidSolver2Ptr
Shared pointer type for the GridFluidSolver2.
Definition: GridFluidSolver2.hpp:309
Definition: Matrix.hpp:27
void ExtrapolateIntoCollider(ScalarGrid2 *grid)
Extrapolates given field into the collider-occupied region.
VectorField2Ptr GetColliderVelocityField() const
Returns the velocity field of the collider.
Abstract base class for physics-based animation.
Definition: PhysicsAnimation.hpp:24
void ComputeGravity(double timeIntervalInSeconds)
Computes the gravity term.
const FaceCenteredGrid2Ptr & GetVelocity() const
Returns the velocity field.
std::shared_ptr< GridEmitter2 > GridEmitter2Ptr
Shared pointer type for the GridEmitter2.
Definition: GridEmitter2.hpp:87
Definition: pybind11Utils.hpp:20
virtual void OnBeginAdvanceTimeStep(double timeIntervalInSeconds)
Called at the beginning of a time-step.
void ApplyBoundaryCondition() const
Applies the boundary condition to the velocity field.
Vector2D GetGridSpacing() const
Definition: GridFluidSolver2.hpp:388
std::shared_ptr< AdvectionSolver2 > AdvectionSolver2Ptr
Shared pointer type for the 2-D advection solver.
Definition: AdvectionSolver2.hpp:125
const GridSystemData2Ptr & GetGridSystemData() const
Returns the grid system data.
void SetGravity(const Vector2D &newGravity)
Sets the gravity of the system.
virtual void ComputeAdvection(double timeIntervalInSeconds)
Computes the advection term using the advection solver.
void SetAdvectionSolver(const AdvectionSolver2Ptr &newSolver)
Sets the advection solver.
Vector2UZ GetResolution() const
Returns the resolution of the grid system data.
DerivedBuilder & WithOrigin(const Vector2D &gridOrigin)
Returns builder with grid origin.
Definition: GridFluidSolver2.hpp:381
N-D face-centered (a.k.a MAC or staggered) grid.
Definition: FaceCenteredGrid.hpp:31
std::shared_ptr< VectorField2 > VectorField2Ptr
Shared pointer for the VectorField2 type.
Definition: VectorField.hpp:87
Front-end to create GridFluidSolver2 objects step by step.
Definition: GridFluidSolver2.hpp:403
void OnInitialize() override
Called when it needs to setup initial condition.
GridFluidSolver2 & operator=(const GridFluidSolver2 &)=delete
Deleted copy assignment operator.
std::shared_ptr< FaceCenteredGrid2 > FaceCenteredGrid2Ptr
Shared pointer type for the FaceCenteredGrid2.
Definition: FaceCenteredGrid.hpp:434
bool GetUseCompressedLinearSystem() const
Returns true if the solver is using compressed linear system.
~GridFluidSolver2() override=default
Default virtual destructor.
void SetDiffusionSolver(const GridDiffusionSolver2Ptr &newSolver)
Sets the diffusion solver.
GridFluidSolver2()
Default constructor.
ScalarField2Ptr GetColliderSDF() const
Returns the signed-distance field representation of the collider.
Vector2D GetGridOrigin() const
Returns the origin of the grid system data.
Abstract base class for grid-based 2-D fluid solver.
Definition: GridFluidSolver2.hpp:35
std::shared_ptr< GridPressureSolver2 > GridPressureSolver2Ptr
Shared pointer type for the GridPressureSolver2.
Definition: GridPressureSolver2.hpp:95
const GridDiffusionSolver2Ptr & GetDiffusionSolver() const
Returns the diffusion solver instance.
const Collider2Ptr & GetCollider() const
Returns the collider.
double GetMaxCFL() const
Returns the max allowed CFL number.
void SetUseCompressedLinearSystem(bool isOn)
Sets whether the solver should use compressed linear system.
Abstract base class for N-D collocated vector grid structure.
Definition: CollocatedVectorGrid.hpp:22
void ResizeGrid(const Vector2UZ &newSize, const Vector2D &newGridSpacing, const Vector2D &newGridOrigin) const
Resizes grid system data.
void SetPressureSolver(const GridPressureSolver2Ptr &newSolver)
Sets the pressure solver.
double GetCFL(double timeIntervalInSeconds) const
Returns the CFL number from the current velocity field for given time interval.
GridFluidSolver2Ptr MakeShared() const
Builds shared pointer of GridFluidSolver2 instance.
Definition: GridFluidSolver2.hpp:411
std::shared_ptr< GridDiffusionSolver2 > GridDiffusionSolver2Ptr
Shared pointer type for the GridDiffusionSolver2.
Definition: GridDiffusionSolver2.hpp:108
virtual ScalarField2Ptr GetFluidSDF() const
Returns the signed-distance representation of the fluid.