Jet  v1.3.3
grid_fluid_solver3.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_GRID_FLUID_SOLVER3_H_
8 #define INCLUDE_JET_GRID_FLUID_SOLVER3_H_
9 
10 #include <jet/advection_solver3.h>
12 #include <jet/collider3.h>
16 #include <jet/grid_emitter3.h>
18 #include <jet/grid_system_data3.h>
19 #include <jet/physics_animation.h>
20 
21 namespace jet {
22 
35  public:
36  class Builder;
37 
40 
43  const Vector3D& gridOrigin);
44 
46  virtual ~GridFluidSolver3();
47 
49  const Vector3D& gravity() const;
50 
52  void setGravity(const Vector3D& newGravity);
53 
55  double viscosityCoefficient() const;
56 
65  void setViscosityCoefficient(double newValue);
66 
73  double cfl(double timeIntervalInSeconds) const;
74 
76  double maxCfl() const;
77 
79  void setMaxCfl(double newCfl);
80 
83 
85  void setUseCompressedLinearSystem(bool onoff);
86 
89 
91  void setAdvectionSolver(const AdvectionSolver3Ptr& newSolver);
92 
95 
98 
101 
104 
107 
110 
121 
134  void resizeGrid(const Size3& newSize, const Vector3D& newGridSpacing,
135  const Vector3D& newGridOrigin);
136 
144  Size3 resolution() const;
145 
154 
163 
171 
173  const Collider3Ptr& collider() const;
174 
176  void setCollider(const Collider3Ptr& newCollider);
177 
179  const GridEmitter3Ptr& emitter() const;
180 
182  void setEmitter(const GridEmitter3Ptr& newEmitter);
183 
185  static Builder builder();
186 
187  protected:
189  void onInitialize() override;
190 
192  void onAdvanceTimeStep(double timeIntervalInSeconds) override;
193 
204  unsigned int numberOfSubTimeSteps(
205  double timeIntervalInSeconds) const override;
206 
208  virtual void onBeginAdvanceTimeStep(double timeIntervalInSeconds);
209 
211  virtual void onEndAdvanceTimeStep(double timeIntervalInSeconds);
212 
221  virtual void computeExternalForces(double timeIntervalInSeconds);
222 
224  virtual void computeViscosity(double timeIntervalInSeconds);
225 
227  virtual void computePressure(double timeIntervalInSeconds);
228 
230  virtual void computeAdvection(double timeIntervalInSeconds);
231 
240  virtual ScalarField3Ptr fluidSdf() const;
241 
243  void computeGravity(double timeIntervalInSeconds);
244 
252 
255 
258 
261 
264 
267 
268  private:
269  Vector3D _gravity = Vector3D(0.0, -9.8, 0.0);
270  double _viscosityCoefficient = 0.0;
271  double _maxCfl = 5.0;
272  bool _useCompressedLinearSys = false;
273  int _closedDomainBoundaryFlag = kDirectionAll;
274 
275  GridSystemData3Ptr _grids;
276  Collider3Ptr _collider;
277  GridEmitter3Ptr _emitter;
278 
279  AdvectionSolver3Ptr _advectionSolver;
280  GridDiffusionSolver3Ptr _diffusionSolver;
281  GridPressureSolver3Ptr _pressureSolver;
282  GridBoundaryConditionSolver3Ptr _boundaryConditionSolver;
283 
284  void beginAdvanceTimeStep(double timeIntervalInSeconds);
285 
286  void endAdvanceTimeStep(double timeIntervalInSeconds);
287 
288  void updateCollider(double timeIntervalInSeconds);
289 
290  void updateEmitter(double timeIntervalInSeconds);
291 };
292 
294 typedef std::shared_ptr<GridFluidSolver3> GridFluidSolver3Ptr;
295 
299 template <typename DerivedBuilder>
301  public:
303  DerivedBuilder& withResolution(const Size3& resolution);
304 
306  DerivedBuilder& withGridSpacing(const Vector3D& gridSpacing);
307 
309  DerivedBuilder& withGridSpacing(double gridSpacing);
310 
317  DerivedBuilder& withDomainSizeX(double domainSizeX);
318 
320  DerivedBuilder& withOrigin(const Vector3D& gridOrigin);
321 
322  protected:
323  Size3 _resolution{1, 1, 1};
326  double _domainSizeX = 1.0;
327  bool _useDomainSize = false;
328 
330 };
331 
332 template <typename T>
334  _resolution = resolution;
335  return static_cast<T&>(*this);
336 }
337 
338 template <typename T>
340  const Vector3D& gridSpacing) {
341  _gridSpacing = gridSpacing;
342  _useDomainSize = false;
343  return static_cast<T&>(*this);
344 }
345 
346 template <typename T>
348  _gridSpacing.x = gridSpacing;
349  _gridSpacing.y = gridSpacing;
350  _gridSpacing.z = gridSpacing;
351  _useDomainSize = false;
352  return static_cast<T&>(*this);
353 }
354 
355 template <typename T>
357  _domainSizeX = domainSizeX;
358  _useDomainSize = true;
359  return static_cast<T&>(*this);
360 }
361 
362 template <typename T>
364  _gridOrigin = gridOrigin;
365  return static_cast<T&>(*this);
366 }
367 
368 template <typename T>
370  Vector3D gridSpacing = _gridSpacing;
371  if (_useDomainSize) {
372  gridSpacing.set(_domainSizeX / static_cast<double>(_resolution.x));
373  }
374  return gridSpacing;
375 }
376 
381  : public GridFluidSolverBuilderBase3<GridFluidSolver3::Builder> {
382  public:
385 
388  return std::make_shared<GridFluidSolver3>(_resolution, getGridSpacing(),
389  _gridOrigin);
390  }
391 };
392 
393 } // namespace jet
394 
395 #endif // INCLUDE_JET_GRID_FLUID_SOLVER3_H_
jet::FaceCenteredGrid3Ptr
std::shared_ptr< FaceCenteredGrid3 > FaceCenteredGrid3Ptr
Shared pointer type for the FaceCenteredGrid3.
Definition: face_centered_grid3.h:285
advection_solver3.h
grid_diffusion_solver3.h
jet::Vector< T, 3 >::set
void set(T s)
Set all x, y, and z components to s.
jet::GridFluidSolver3::advectionSolver
const AdvectionSolver3Ptr & advectionSolver() const
Returns the advection solver instance.
jet::GridFluidSolverBuilderBase3::withOrigin
DerivedBuilder & withOrigin(const Vector3D &gridOrigin)
Returns builder with grid origin.
Definition: grid_fluid_solver3.h:363
jet::GridFluidSolver3::GridFluidSolver3
GridFluidSolver3(const Size3 &resolution, const Vector3D &gridSpacing, const Vector3D &gridOrigin)
Constructs solver with initial grid size.
jet::GridPressureSolver3Ptr
std::shared_ptr< GridPressureSolver3 > GridPressureSolver3Ptr
Shared pointer type for the GridPressureSolver3.
Definition: grid_pressure_solver3.h:78
jet::GridFluidSolver3Ptr
std::shared_ptr< GridFluidSolver3 > GridFluidSolver3Ptr
Shared pointer type for the GridFluidSolver3.
Definition: grid_fluid_solver3.h:294
jet::GridFluidSolver3::resolution
Size3 resolution() const
Returns the resolution of the grid system data.
jet::GridFluidSolver3::computeViscosity
virtual void computeViscosity(double timeIntervalInSeconds)
Computes the viscosity term using the diffusion solver.
jet::GridFluidSolverBuilderBase3::withResolution
DerivedBuilder & withResolution(const Size3 &resolution)
Returns builder with grid resolution.
Definition: grid_fluid_solver3.h:333
jet::AdvectionSolver3Ptr
std::shared_ptr< AdvectionSolver3 > AdvectionSolver3Ptr
Shared pointer type for the 3-D advection solver.
Definition: advection_solver3.h:115
jet::GridDiffusionSolver3Ptr
std::shared_ptr< GridDiffusionSolver3 > GridDiffusionSolver3Ptr
Shared pointer type for the GridDiffusionSolver3.
Definition: grid_diffusion_solver3.h:94
jet::PhysicsAnimation
Abstract base class for physics-based animation.
Definition: physics_animation.h:20
jet::GridFluidSolver3::computeExternalForces
virtual void computeExternalForces(double timeIntervalInSeconds)
Computes the external force terms.
jet::GridFluidSolver3::Builder
Front-end to create GridFluidSolver3 objects step by step.
Definition: grid_fluid_solver3.h:381
jet::GridFluidSolver3::gridSystemData
const GridSystemData3Ptr & gridSystemData() const
Returns the grid system data.
jet::GridFluidSolver3::setCollider
void setCollider(const Collider3Ptr &newCollider)
Sets the collider.
jet::GridFluidSolver3::setClosedDomainBoundaryFlag
void setClosedDomainBoundaryFlag(int flag)
Sets the closed domain boundary flag.
jet::GridFluidSolver3::computePressure
virtual void computePressure(double timeIntervalInSeconds)
Computes the pressure term using the pressure solver.
jet::GridFluidSolver3::setGravity
void setGravity(const Vector3D &newGravity)
Sets the gravity of the system.
jet::GridFluidSolver3::~GridFluidSolver3
virtual ~GridFluidSolver3()
Default destructor.
jet::GridFluidSolver3::setViscosityCoefficient
void setViscosityCoefficient(double newValue)
Sets the viscosity coefficient.
jet::GridFluidSolverBuilderBase3::withGridSpacing
DerivedBuilder & withGridSpacing(double gridSpacing)
Returns builder with grid spacing.
Definition: grid_fluid_solver3.h:347
jet::GridFluidSolver3::applyBoundaryCondition
void applyBoundaryCondition()
Applies the boundary condition to the velocity field.
jet::GridFluidSolverBuilderBase3::_resolution
Size3 _resolution
Definition: grid_fluid_solver3.h:323
jet::GridFluidSolver3::viscosityCoefficient
double viscosityCoefficient() const
Returns the viscosity coefficient.
jet::GridFluidSolver3::extrapolateIntoCollider
void extrapolateIntoCollider(FaceCenteredGrid3 *grid)
Extrapolates given field into the collider-occupied region.
jet::GridFluidSolver3::extrapolateIntoCollider
void extrapolateIntoCollider(CollocatedVectorGrid3 *grid)
Extrapolates given field into the collider-occupied region.
jet::CollocatedVectorGrid3
Abstract base class for 3-D collocated vector grid structure.
Definition: collocated_vector_grid3.h:18
jet::kDirectionAll
constexpr int kDirectionAll
All direction.
Definition: constants.h:333
collider3.h
jet::GridFluidSolverBuilderBase3::_gridSpacing
Vector3D _gridSpacing
Definition: grid_fluid_solver3.h:324
jet::GridFluidSolver3::collider
const Collider3Ptr & collider() const
Returns the collider.
cell_centered_scalar_grid3.h
jet::GridFluidSolver3::computeGravity
void computeGravity(double timeIntervalInSeconds)
Computes the gravity term.
jet::GridFluidSolver3::setAdvectionSolver
void setAdvectionSolver(const AdvectionSolver3Ptr &newSolver)
Sets the advection solver.
jet::GridFluidSolverBuilderBase3::withDomainSizeX
DerivedBuilder & withDomainSizeX(double domainSizeX)
Returns builder with domain size in x-direction.
Definition: grid_fluid_solver3.h:356
jet::GridFluidSolver3::emitter
const GridEmitter3Ptr & emitter() const
Returns the emitter.
grid_boundary_condition_solver3.h
jet
Definition: advection_solver2.h:18
jet::GridFluidSolver3::diffusionSolver
const GridDiffusionSolver3Ptr & diffusionSolver() const
Returns the diffusion solver instance.
jet::GridFluidSolver3::closedDomainBoundaryFlag
int closedDomainBoundaryFlag() const
Returns the closed domain boundary flag.
jet::GridSystemData3Ptr
std::shared_ptr< GridSystemData3 > GridSystemData3Ptr
Shared pointer type of GridSystemData3.
Definition: grid_system_data3.h:237
jet::GridFluidSolver3::gridSpacing
Vector3D gridSpacing() const
Returns the grid spacing of the grid system data.
jet::GridFluidSolver3::GridFluidSolver3
GridFluidSolver3()
Default constructor.
jet::GridFluidSolverBuilderBase3
Base class for grid-based fluid solver builder.
Definition: grid_fluid_solver3.h:300
jet::GridFluidSolver3::setDiffusionSolver
void setDiffusionSolver(const GridDiffusionSolver3Ptr &newSolver)
Sets the diffusion solver.
jet::GridFluidSolver3::setUseCompressedLinearSystem
void setUseCompressedLinearSystem(bool onoff)
Sets whether the solver should use compressed linear system.
grid_emitter3.h
jet::GridFluidSolver3::resizeGrid
void resizeGrid(const Size3 &newSize, const Vector3D &newGridSpacing, const Vector3D &newGridOrigin)
Resizes grid system data.
jet::GridFluidSolver3::colliderSdf
ScalarField3Ptr colliderSdf() const
Returns the signed-distance field representation of the collider.
jet::GridFluidSolver3::computeAdvection
virtual void computeAdvection(double timeIntervalInSeconds)
Computes the advection term using the advection solver.
jet::GridFluidSolverBuilderBase3::_useDomainSize
bool _useDomainSize
Definition: grid_fluid_solver3.h:327
jet::FaceCenteredGrid3
3-D face-centered (a.k.a MAC or staggered) grid.
Definition: face_centered_grid3.h:26
jet::GridFluidSolver3::builder
static Builder builder()
Returns builder fox GridFluidSolver3.
jet::ScalarField3Ptr
std::shared_ptr< ScalarField3 > ScalarField3Ptr
Shared pointer for the ScalarField3 type.
Definition: scalar_field3.h:40
jet::Size3
3-D size class.
Definition: size3.h:19
jet::GridBoundaryConditionSolver3Ptr
std::shared_ptr< GridBoundaryConditionSolver3 > GridBoundaryConditionSolver3Ptr
Shared pointer type for the GridBoundaryConditionSolver3.
Definition: grid_boundary_condition_solver3.h:104
jet::GridFluidSolver3::extrapolateIntoCollider
void extrapolateIntoCollider(ScalarGrid3 *grid)
Extrapolates given field into the collider-occupied region.
jet::GridFluidSolverBuilderBase3::_gridOrigin
Vector3D _gridOrigin
Definition: grid_fluid_solver3.h:325
jet::GridFluidSolver3::gridOrigin
Vector3D gridOrigin() const
Returns the origin of the grid system data.
jet::GridFluidSolver3::setMaxCfl
void setMaxCfl(double newCfl)
Sets the max allowed CFL number.
jet::GridFluidSolver3::cfl
double cfl(double timeIntervalInSeconds) const
Returns the CFL number from the current velocity field for given time interval.
jet::GridFluidSolver3::onBeginAdvanceTimeStep
virtual void onBeginAdvanceTimeStep(double timeIntervalInSeconds)
Called at the beginning of a time-step.
jet::GridFluidSolverBuilderBase3::getGridSpacing
Vector3D getGridSpacing() const
Definition: grid_fluid_solver3.h:369
jet::GridFluidSolver3
Abstract base class for grid-based 3-D fluid solver.
Definition: grid_fluid_solver3.h:34
jet::GridFluidSolver3::Builder::makeShared
GridFluidSolver3Ptr makeShared() const
Builds shared pointer of GridFluidSolver3 instance.
Definition: grid_fluid_solver3.h:387
jet::GridFluidSolver3::Builder::build
GridFluidSolver3 build() const
Builds GridFluidSolver3.
jet::GridFluidSolverBuilderBase3::_domainSizeX
double _domainSizeX
Definition: grid_fluid_solver3.h:326
grid_pressure_solver3.h
jet::GridFluidSolver3::onAdvanceTimeStep
void onAdvanceTimeStep(double timeIntervalInSeconds) override
Called when advancing a single time-step.
jet::ScalarGrid3
Abstract base class for 3-D scalar grid structure.
Definition: scalar_grid3.h:21
jet::Vector3D
Vector3< double > Vector3D
Double-type 3D vector.
Definition: vector3.h:349
jet::GridFluidSolver3::setEmitter
void setEmitter(const GridEmitter3Ptr &newEmitter)
Sets the emitter.
face_centered_grid3.h
grid_system_data3.h
jet::Vector< T, 3 >
3-D vector class.
Definition: vector3.h:25
jet::GridFluidSolver3::colliderVelocityField
VectorField3Ptr colliderVelocityField() const
Returns the velocity field of the collider.
physics_animation.h
jet::GridFluidSolver3::onEndAdvanceTimeStep
virtual void onEndAdvanceTimeStep(double timeIntervalInSeconds)
Called at the end of a time-step.
jet::GridFluidSolver3::pressureSolver
const GridPressureSolver3Ptr & pressureSolver() const
Returns the pressure solver instance.
jet::GridFluidSolver3::onInitialize
void onInitialize() override
Called when it needs to setup initial condition.
jet::GridFluidSolver3::velocity
const FaceCenteredGrid3Ptr & velocity() const
Returns the velocity field.
jet::Collider3Ptr
std::shared_ptr< Collider3 > Collider3Ptr
Shared pointer type for the Collider2.
Definition: collider3.h:118
jet::GridFluidSolver3::useCompressedLinearSystem
bool useCompressedLinearSystem() const
Returns true if the solver is using compressed linear system.
jet::GridFluidSolver3::fluidSdf
virtual ScalarField3Ptr fluidSdf() const
jet::GridFluidSolver3::maxCfl
double maxCfl() const
Returns the max allowed CFL number.
jet::GridFluidSolver3::gravity
const Vector3D & gravity() const
Returns the gravity vector of the system.
jet::VectorField3Ptr
std::shared_ptr< VectorField3 > VectorField3Ptr
Shared pointer for the VectorField3 type.
Definition: vector_field3.h:40
jet::GridEmitter3Ptr
std::shared_ptr< GridEmitter3 > GridEmitter3Ptr
Shared pointer type for the GridEmitter3.
Definition: grid_emitter3.h:70
jet::GridFluidSolver3::numberOfSubTimeSteps
unsigned int numberOfSubTimeSteps(double timeIntervalInSeconds) const override
Returns the required sub-time-steps for given time interval.
jet::GridFluidSolverBuilderBase3::withGridSpacing
DerivedBuilder & withGridSpacing(const Vector3D &gridSpacing)
Returns builder with grid spacing.
Definition: grid_fluid_solver3.h:339
jet::GridFluidSolver3::setPressureSolver
void setPressureSolver(const GridPressureSolver3Ptr &newSolver)
Sets the pressure solver.