GridFluidSolver3.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: GridFluidSolver3.h
3 > Project Name: CubbyFlow
4 > Author: Dongmin Kim
5 > Purpose: Abstract base class for grid-based 3-D fluid solver.
6 > Created Time: 2017/08/16
7 > Copyright (c) 2018, Dongmin Kim
8 *************************************************************************/
9 #ifndef CUBBYFLOW_GRID_FLUID_SOLVER3_H
10 #define CUBBYFLOW_GRID_FLUID_SOLVER3_H
11 
19 
20 namespace CubbyFlow
21 {
34  {
35  public:
36  class Builder;
37 
40 
43  const Size3& resolution,
44  const Vector3D& gridSpacing,
45  const Vector3D& gridOrigin);
46 
48  virtual ~GridFluidSolver3();
49 
51  const Vector3D& GetGravity() const;
52 
54  void SetGravity(const Vector3D& 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 AdvectionSolver3Ptr& newSolver);
93 
96 
98  void SetDiffusionSolver(const GridDiffusionSolver3Ptr& newSolver);
99 
102 
104  void SetPressureSolver(const GridPressureSolver3Ptr& newSolver);
105 
107  int GetClosedDomainBoundaryFlag() const;
108 
110  void SetClosedDomainBoundaryFlag(int flag);
111 
121  const GridSystemData3Ptr& GetGridSystemData() const;
122 
135  void ResizeGrid(
136  const Size3& newSize,
137  const Vector3D& newGridSpacing,
138  const Vector3D& newGridOrigin) const;
139 
147  Size3 GetResolution() const;
148 
156  Vector3D GetGridSpacing() const;
157 
165  Vector3D GetGridOrigin() const;
166 
173  const FaceCenteredGrid3Ptr& GetVelocity() const;
174 
176  const Collider3Ptr& GetCollider() const;
177 
179  void SetCollider(const Collider3Ptr& newCollider);
180 
182  const GridEmitter3Ptr& GetEmitter() const;
183 
185  void SetEmitter(const GridEmitter3Ptr& 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 ScalarField3Ptr GetFluidSDF() const;
244 
246  void ComputeGravity(double timeIntervalInSeconds);
247 
254  void ApplyBoundaryCondition() const;
255 
258 
261 
264 
267 
270 
271  private:
272  Vector3D m_gravity = Vector3D(0.0, -9.8, 0.0);
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  GridSystemData3Ptr m_grids;
279  Collider3Ptr m_collider;
280  GridEmitter3Ptr m_emitter;
281 
282  AdvectionSolver3Ptr m_advectionSolver;
283  GridDiffusionSolver3Ptr m_diffusionSolver;
284  GridPressureSolver3Ptr m_pressureSolver;
285  GridBoundaryConditionSolver3Ptr 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 GridFluidSolver3Ptr = std::shared_ptr<GridFluidSolver3>;
298 
302  template <typename DerivedBuilder>
304  {
305  public:
307  DerivedBuilder& WithResolution(const Size3& resolution);
308 
310  DerivedBuilder& WithGridSpacing(const Vector3D& gridSpacing);
311 
313  DerivedBuilder& WithGridSpacing(double gridSpacing);
314 
321  DerivedBuilder& WithDomainSizeX(double domainSizeX);
322 
324  DerivedBuilder& WithOrigin(const Vector3D& gridOrigin);
325 
326  protected:
327  Size3 m_resolution{ 1, 1, 1};
329  Vector3D m_gridOrigin{ 0, 0, 0 };
330  double m_domainSizeX = 1.0;
331  bool m_useDomainSize = false;
332 
333  Vector3D 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_gridSpacing.z = gridSpacing;
357  m_useDomainSize = false;
358  return static_cast<T&>(*this);
359  }
360 
361  template <typename T>
363  {
364  m_domainSizeX = domainSizeX;
365  m_useDomainSize = true;
366  return static_cast<T&>(*this);
367  }
368 
369  template <typename T>
371  {
372  m_gridOrigin = gridOrigin;
373  return static_cast<T&>(*this);
374  }
375 
376  template <typename T>
378  {
379  Vector3D gridSpacing = m_gridSpacing;
380 
381  if (m_useDomainSize)
382  {
383  gridSpacing.Set(m_domainSizeX / static_cast<double>(m_resolution.x));
384  }
385 
386  return gridSpacing;
387  }
388 
392  class GridFluidSolver3::Builder final : public GridFluidSolverBuilderBase3<GridFluidSolver3::Builder>
393  {
394  public:
396  GridFluidSolver3 Build() const;
397 
400  {
401  return std::make_shared<GridFluidSolver3>(m_resolution, GetGridSpacing(), m_gridOrigin);
402  }
403  };
404 }
405 
406 #endif
3-D vector class.
Definition: Vector3.h:26
virtual void ComputeViscosity(double timeIntervalInSeconds)
Computes the viscosity term using the diffusion solver.
const AdvectionSolver3Ptr & GetAdvectionSolver() const
Returns the advection solver instance.
void Set(T s)
Set all x, y, and z components to s.
Definition: Vector3-Impl.h:27
std::shared_ptr< Collider3 > Collider3Ptr
Shared pointer type for the Collider3.
Definition: Collider3.h:112
Abstract base class for 3-D collocated vector grid structure.
Definition: CollocatedVectorGrid3.h:19
void SetMaxCFL(double newCFL)
Sets the max allowed CFL number.
virtual ~GridFluidSolver3()
Default destructor.
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 ResizeGrid(const Size3 &newSize, const Vector3D &newGridSpacing, const Vector3D &newGridOrigin) const
Resizes grid system data.
void SetCollider(const Collider3Ptr &newCollider)
Sets the collider.
std::shared_ptr< AdvectionSolver3 > AdvectionSolver3Ptr
Shared pointer type for the 3-D advection solver.
Definition: AdvectionSolver3.h:113
constexpr int DIRECTION_ALL
All direction.
Definition: Constants.h:187
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.
void SetEmitter(const GridEmitter3Ptr &newEmitter)
Sets the emitter.
std::shared_ptr< ScalarField3 > ScalarField3Ptr
Shared pointer for the ScalarField3 type.
Definition: ScalarField3.h:44
std::shared_ptr< GridEmitter3 > GridEmitter3Ptr
Shared pointer type for the GridEmitter3.
Definition: GridEmitter3.h:63
const FaceCenteredGrid3Ptr & GetVelocity() const
Returns the velocity field.
void SetUseCompressedLinearSystem(bool onoff)
Sets whether the solver should use compressed linear system.
Vector3D m_gridOrigin
Definition: GridFluidSolver3.h:329
std::shared_ptr< VectorField3 > VectorField3Ptr
Shared pointer for the VectorField3 type.
Definition: VectorField3.h:44
GridFluidSolver3()
Default constructor.
DerivedBuilder & WithOrigin(const Vector3D &gridOrigin)
Returns builder with grid origin.
Definition: GridFluidSolver3.h:370
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.h:78
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.h:104
int GetClosedDomainBoundaryFlag() const
Returns the closed domain boundary flag.
const GridPressureSolver3Ptr & GetPressureSolver() const
Returns the pressure solver instance.
3-D face-centered (a.k.a MAC or staggered) grid.
Definition: FaceCenteredGrid3.h:25
VectorField3Ptr GetColliderVelocityField() const
Returns the velocity field of the collider.
Size3 GetResolution() const
Returns the resolution of the grid system data.
const GridDiffusionSolver3Ptr & GetDiffusionSolver() const
Returns the diffusion solver instance.
3-D point class.
Definition: Point3.h:26
virtual void OnBeginAdvanceTimeStep(double timeIntervalInSeconds)
Called at the beginning of a time-step.
Base class for grid-based fluid solver builder.
Definition: GridFluidSolver3.h:303
std::shared_ptr< FaceCenteredGrid3 > FaceCenteredGrid3Ptr
Shared pointer type for the FaceCenteredGrid3.
Definition: FaceCenteredGrid3.h:276
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.
Abstract base class for physics-based animation.
Definition: PhysicsAnimation.h:22
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.h:392
Definition: pybind11Utils.h:24
Vector3D GetGridSpacing() const
Definition: GridFluidSolver3.h:377
Abstract base class for grid-based 3-D fluid solver.
Definition: GridFluidSolver3.h:33
Vector3D m_gridSpacing
Definition: GridFluidSolver3.h:328
DerivedBuilder & WithResolution(const Size3 &resolution)
Returns builder with grid resolution.
Definition: GridFluidSolver3.h:337
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.h:93
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.h:344
void SetViscosityCoefficient(double newValue)
Sets the viscosity coefficient.
Size3 m_resolution
Definition: GridFluidSolver3.h:327
void ExtrapolateIntoCollider(ScalarGrid3 *grid)
Extrapolates given field into the collider-occupied region.
Abstract base class for 3-D scalar grid structure.
Definition: ScalarGrid3.h:21
GridFluidSolver3 Build() const
Builds GridFluidSolver3.
const Collider3Ptr & GetCollider() const
Returns the collider.
std::shared_ptr< GridSystemData3 > GridSystemData3Ptr
Shared pointer type of GridSystemData3.
Definition: GridSystemData3.h:224
bool m_useDomainSize
Definition: GridFluidSolver3.h:331
double GetMaxCFL() const
Returns the max allowed CFL number.
ScalarField3Ptr GetColliderSDF() const
Returns the signed-distance field representation of the collider.
double m_domainSizeX
Definition: GridFluidSolver3.h:330
std::shared_ptr< GridFluidSolver3 > GridFluidSolver3Ptr
Shared pointer type for the GridFluidSolver3.
Definition: GridFluidSolver3.h:297
DerivedBuilder & WithDomainSizeX(double domainSizeX)
Returns builder with domain size in x-direction.
Definition: GridFluidSolver3.h:362
void ComputeGravity(double timeIntervalInSeconds)
Computes the gravity term.
GridFluidSolver3Ptr MakeShared() const
Builds shared pointer of GridFluidSolver3 instance.
Definition: GridFluidSolver3.h:399
const Vector3D & GetGravity() const
Returns the gravity vector of the system.
Vector3< double > Vector3D
Double-type 3D vector.
Definition: Vector3.h:353
virtual void ComputeAdvection(double timeIntervalInSeconds)
Computes the advection term using the advection solver.