GridFluidSolver2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: GridFluidSolver2.h
3 > Project Name: CubbyFlow
4 > Author: Dongmin Kim
5 > Purpose: Abstract base class for grid-based 2-D fluid solver.
6 > Created Time: 2017/08/10
7 > Copyright (c) 2018, Dongmin Kim
8 *************************************************************************/
9 #ifndef CUBBYFLOW_GRID_FLUID_SOLVER2_H
10 #define CUBBYFLOW_GRID_FLUID_SOLVER2_H
11 
19 
20 namespace CubbyFlow
21 {
34  {
35  public:
36  class Builder;
37 
40 
43  const Size2& resolution,
44  const Vector2D& gridSpacing,
45  const Vector2D& gridOrigin);
46 
48  virtual ~GridFluidSolver2();
49 
51  const Vector2D& GetGravity() const;
52 
54  void SetGravity(const Vector2D& newGravity);
55 
57  double GetViscosityCoefficient() const;
58 
67  void SetViscosityCoefficient(double newValue);
68 
74  double GetCFL(double timeIntervalInSeconds) const;
75 
77  double GetMaxCFL() const;
78 
80  void SetMaxCFL(double newCFL);
81 
83  bool GetUseCompressedLinearSystem() const;
84 
86  void SetUseCompressedLinearSystem(bool onoff);
87 
90 
92  void SetAdvectionSolver(const AdvectionSolver2Ptr& newSolver);
93 
96 
98  void SetDiffusionSolver(const GridDiffusionSolver2Ptr& newSolver);
99 
102 
104  void SetPressureSolver(const GridPressureSolver2Ptr& newSolver);
105 
107  int GetClosedDomainBoundaryFlag() const;
108 
110  void SetClosedDomainBoundaryFlag(int flag);
111 
121  const GridSystemData2Ptr& GetGridSystemData() const;
122 
135  void ResizeGrid(
136  const Size2& newSize,
137  const Vector2D& newGridSpacing,
138  const Vector2D& newGridOrigin) const;
139 
147  Size2 GetResolution() const;
148 
156  Vector2D GetGridSpacing() const;
157 
165  Vector2D GetGridOrigin() const;
166 
173  const FaceCenteredGrid2Ptr& GetVelocity() const;
174 
176  const Collider2Ptr& GetCollider() const;
177 
179  void SetCollider(const Collider2Ptr& newCollider);
180 
182  const GridEmitter2Ptr& GetEmitter() const;
183 
185  void SetEmitter(const GridEmitter2Ptr& newEmitter);
186 
188  static Builder GetBuilder();
189 
190  protected:
192  void OnInitialize() override;
193 
195  void OnAdvanceTimeStep(double timeIntervalInSeconds) override;
196 
207  unsigned int GetNumberOfSubTimeSteps(double timeIntervalInSeconds) const override;
208 
210  virtual void OnBeginAdvanceTimeStep(double timeIntervalInSeconds);
211 
213  virtual void OnEndAdvanceTimeStep(double timeIntervalInSeconds);
214 
223  virtual void ComputeExternalForces(double timeIntervalInSeconds);
224 
226  virtual void ComputeViscosity(double timeIntervalInSeconds);
227 
229  virtual void ComputePressure(double timeIntervalInSeconds);
230 
232  virtual void ComputeAdvection(double timeIntervalInSeconds);
233 
243  virtual ScalarField2Ptr GetFluidSDF() const;
244 
246  void ComputeGravity(double timeIntervalInSeconds);
247 
254  void ApplyBoundaryCondition() const;
255 
258 
261 
264 
267 
270 
271  private:
272  Vector2D m_gravity = Vector2D(0.0, -9.8);
273  double m_viscosityCoefficient = 0.0;
274  double m_maxCFL = 5.0;
275  bool m_useCompressedLinearSys = false;
276  int m_closedDomainBoundaryFlag = DIRECTION_ALL;
277 
278  GridSystemData2Ptr m_grids;
279  Collider2Ptr m_collider;
280  GridEmitter2Ptr m_emitter;
281 
282  AdvectionSolver2Ptr m_advectionSolver;
283  GridDiffusionSolver2Ptr m_diffusionSolver;
284  GridPressureSolver2Ptr m_pressureSolver;
285  GridBoundaryConditionSolver2Ptr m_boundaryConditionSolver;
286 
287  void BeginAdvanceTimeStep(double timeIntervalInSeconds);
288 
289  void EndAdvanceTimeStep(double timeIntervalInSeconds);
290 
291  void UpdateCollider(double timeIntervalInSeconds) const;
292 
293  void UpdateEmitter(double timeIntervalInSeconds) const;
294  };
295 
297  using GridFluidSolver2Ptr = std::shared_ptr<GridFluidSolver2>;
298 
302  template <typename DerivedBuilder>
304  {
305  public:
307  DerivedBuilder& WithResolution(const Size2& resolution);
308 
310  DerivedBuilder& WithGridSpacing(const Vector2D& gridSpacing);
311 
313  DerivedBuilder& WithGridSpacing(double gridSpacing);
314 
321  DerivedBuilder& WithDomainSizeX(double domainSizeX);
322 
324  DerivedBuilder& WithOrigin(const Vector2D& gridOrigin);
325 
326  protected:
330  double m_domainSizeX = 1.0;
331  bool m_useDomainSize = false;
332 
333  Vector2D GetGridSpacing() const;
334  };
335 
336  template <typename T>
338  {
339  m_resolution = resolution;
340  return static_cast<T&>(*this);
341  }
342 
343  template <typename T>
345  {
346  m_gridSpacing = gridSpacing;
347  m_useDomainSize = false;
348  return static_cast<T&>(*this);
349  }
350 
351  template <typename T>
353  {
354  m_gridSpacing.x = gridSpacing;
355  m_gridSpacing.y = gridSpacing;
356  m_useDomainSize = false;
357  return static_cast<T&>(*this);
358  }
359 
360  template <typename T>
362  {
363  m_domainSizeX = domainSizeX;
364  m_useDomainSize = true;
365  return static_cast<T&>(*this);
366  }
367 
368  template <typename T>
370  {
371  m_gridOrigin = gridOrigin;
372  return static_cast<T&>(*this);
373  }
374 
375  template <typename T>
377  {
378  Vector2D gridSpacing = m_gridSpacing;
379 
380  if (m_useDomainSize)
381  {
382  gridSpacing.Set(m_domainSizeX / static_cast<double>(m_resolution.x));
383  }
384 
385  return gridSpacing;
386  }
387 
391  class GridFluidSolver2::Builder final : public GridFluidSolverBuilderBase2<GridFluidSolver2::Builder>
392  {
393  public:
395  GridFluidSolver2 Build() const;
396 
399  {
400  return std::make_shared<GridFluidSolver2>(m_resolution, GetGridSpacing(), m_gridOrigin);
401  }
402  };
403 }
404 
405 #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.
bool m_useDomainSize
Definition: GridFluidSolver2.h:331
void SetCollider(const Collider2Ptr &newCollider)
Sets the collider.
void SetEmitter(const GridEmitter2Ptr &newEmitter)
Sets the emitter.
Vector2D m_gridSpacing
Definition: GridFluidSolver2.h:328
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: Collider2.h:112
const GridPressureSolver2Ptr & GetPressureSolver() const
Returns the pressure solver instance.
Base class for grid-based fluid solver builder.
Definition: GridFluidSolver2.h:303
constexpr int DIRECTION_ALL
All direction.
Definition: Constants.h:187
DerivedBuilder & WithDomainSizeX(double domainSizeX)
Returns builder with domain size in x-direction.
Definition: GridFluidSolver2.h:361
void SetMaxCFL(double newCFL)
Sets the max allowed CFL number.
void SetViscosityCoefficient(double newValue)
Sets the viscosity coefficient.
void SetUseCompressedLinearSystem(bool onoff)
Sets whether the solver should use compressed linear system.
const GridEmitter2Ptr & GetEmitter() const
Returns the emitter.
Size2 m_resolution
Definition: GridFluidSolver2.h:327
unsigned int GetNumberOfSubTimeSteps(double timeIntervalInSeconds) const override
Returns the required sub-time-steps for given time interval.
GridFluidSolver2 Build() const
Builds GridFluidSolver2.
void SetClosedDomainBoundaryFlag(int flag)
Sets the closed domain boundary flag.
Abstract base class for 2-D collocated vector grid structure.
Definition: CollocatedVectorGrid2.h:19
std::shared_ptr< GridBoundaryConditionSolver2 > GridBoundaryConditionSolver2Ptr
Shared pointer type for the GridBoundaryConditionSolver2.
Definition: GridBoundaryConditionSolver2.h:104
virtual void ComputeExternalForces(double timeIntervalInSeconds)
Computes the external force terms.
2-D point class.
Definition: Point2.h:25
static Builder GetBuilder()
Returns builder fox GridFluidSolver2.
double GetViscosityCoefficient() const
Returns the viscosity coefficient.
std::shared_ptr< GridSystemData2 > GridSystemData2Ptr
Shared pointer type of GridSystemData2.
Definition: GridSystemData2.h:224
DerivedBuilder & WithGridSpacing(const Vector2D &gridSpacing)
Returns builder with grid spacing.
Definition: GridFluidSolver2.h:344
std::shared_ptr< ScalarField2 > ScalarField2Ptr
Shared pointer for the ScalarField2 type.
Definition: ScalarField2.h:44
const Vector2D & GetGravity() const
Returns the gravity vector of the system.
2-D face-centered (a.k.a MAC or staggered) grid.
Definition: FaceCenteredGrid2.h:25
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.
DerivedBuilder & WithResolution(const Size2 &resolution)
Returns builder with grid resolution.
Definition: GridFluidSolver2.h:337
std::shared_ptr< GridFluidSolver2 > GridFluidSolver2Ptr
Shared pointer type for the GridFluidSolver2.
Definition: GridFluidSolver2.h:297
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.h:22
void ComputeGravity(double timeIntervalInSeconds)
Computes the gravity term.
virtual ~GridFluidSolver2()
Default destructor.
const FaceCenteredGrid2Ptr & GetVelocity() const
Returns the velocity field.
std::shared_ptr< GridEmitter2 > GridEmitter2Ptr
Shared pointer type for the GridEmitter2.
Definition: GridEmitter2.h:63
Definition: pybind11Utils.h:24
virtual void OnBeginAdvanceTimeStep(double timeIntervalInSeconds)
Called at the beginning of a time-step.
void ApplyBoundaryCondition() const
Applies the boundary condition to the velocity field.
Vector2< double > Vector2D
Double-type 2D vector.
Definition: Vector2.h:341
Vector2D GetGridSpacing() const
Definition: GridFluidSolver2.h:376
std::shared_ptr< AdvectionSolver2 > AdvectionSolver2Ptr
Shared pointer type for the 2-D advection solver.
Definition: AdvectionSolver2.h:111
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.
void Set(T s)
Set both x and y components to s.
Definition: Vector2-Impl.h:28
DerivedBuilder & WithOrigin(const Vector2D &gridOrigin)
Returns builder with grid origin.
Definition: GridFluidSolver2.h:369
std::shared_ptr< VectorField2 > VectorField2Ptr
Shared pointer for the VectorField2 type.
Definition: VectorField2.h:44
Front-end to create GridFluidSolver2 objects step by step.
Definition: GridFluidSolver2.h:391
void OnInitialize() override
Called when it needs to setup initial condition.
double m_domainSizeX
Definition: GridFluidSolver2.h:330
Vector2D m_gridOrigin
Definition: GridFluidSolver2.h:329
std::shared_ptr< FaceCenteredGrid2 > FaceCenteredGrid2Ptr
Shared pointer type for the FaceCenteredGrid2.
Definition: FaceCenteredGrid2.h:228
bool GetUseCompressedLinearSystem() const
Returns true if the solver is using compressed linear system.
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.h:33
std::shared_ptr< GridPressureSolver2 > GridPressureSolver2Ptr
Shared pointer type for the GridPressureSolver2.
Definition: GridPressureSolver2.h:78
const GridDiffusionSolver2Ptr & GetDiffusionSolver() const
Returns the diffusion solver instance.
const Collider2Ptr & GetCollider() const
Returns the collider.
2-D vector class.
Definition: Vector2.h:26
double GetMaxCFL() const
Returns the max allowed CFL number.
Size2 GetResolution() const
Returns the resolution of the grid system data.
void SetPressureSolver(const GridPressureSolver2Ptr &newSolver)
Sets the pressure solver.
Abstract base class for 2-D scalar grid structure.
Definition: ScalarGrid2.h:21
void ResizeGrid(const Size2 &newSize, const Vector2D &newGridSpacing, const Vector2D &newGridOrigin) const
Resizes grid system data.
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.h:398
std::shared_ptr< GridDiffusionSolver2 > GridDiffusionSolver2Ptr
Shared pointer type for the GridDiffusionSolver2.
Definition: GridDiffusionSolver2.h:93
virtual ScalarField2Ptr GetFluidSDF() const
Returns the signed-distance representation of the fluid.