Jet  v1.3.3
grid_fluid_solver2.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_SOLVER2_H_
8 #define INCLUDE_JET_GRID_FLUID_SOLVER2_H_
9 
10 #include <jet/advection_solver2.h>
12 #include <jet/collider2.h>
16 #include <jet/grid_emitter2.h>
18 #include <jet/grid_system_data2.h>
19 #include <jet/physics_animation.h>
20 
21 namespace jet {
22 
35  public:
36  class Builder;
37 
40 
43  const Vector2D& gridOrigin);
44 
46  virtual ~GridFluidSolver2();
47 
49  const Vector2D& gravity() const;
50 
52  void setGravity(const Vector2D& 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 AdvectionSolver2Ptr& newSolver);
92 
95 
98 
101 
104 
107 
110 
121 
134  void resizeGrid(const Size2& newSize, const Vector2D& newGridSpacing,
135  const Vector2D& newGridOrigin);
136 
144  Size2 resolution() const;
145 
154 
163 
171 
173  const Collider2Ptr& collider() const;
174 
176  void setCollider(const Collider2Ptr& newCollider);
177 
179  const GridEmitter2Ptr& emitter() const;
180 
182  void setEmitter(const GridEmitter2Ptr& 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 ScalarField2Ptr fluidSdf() const;
241 
243  void computeGravity(double timeIntervalInSeconds);
244 
252 
255 
258 
261 
264 
267 
268  private:
269  Vector2D _gravity = Vector2D(0.0, -9.8);
270  double _viscosityCoefficient = 0.0;
271  double _maxCfl = 5.0;
272  bool _useCompressedLinearSys = false;
273  int _closedDomainBoundaryFlag = kDirectionAll;
274 
275  GridSystemData2Ptr _grids;
276  Collider2Ptr _collider;
277  GridEmitter2Ptr _emitter;
278 
279  AdvectionSolver2Ptr _advectionSolver;
280  GridDiffusionSolver2Ptr _diffusionSolver;
281  GridPressureSolver2Ptr _pressureSolver;
282  GridBoundaryConditionSolver2Ptr _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<GridFluidSolver2> GridFluidSolver2Ptr;
295 
299 template <typename DerivedBuilder>
301  public:
303  DerivedBuilder& withResolution(const Size2& resolution);
304 
306  DerivedBuilder& withGridSpacing(const Vector2D& gridSpacing);
307 
309  DerivedBuilder& withGridSpacing(double gridSpacing);
310 
317  DerivedBuilder& withDomainSizeX(double domainSizeX);
318 
320  DerivedBuilder& withOrigin(const Vector2D& gridOrigin);
321 
322  protected:
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 Vector2D& 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  _useDomainSize = false;
351  return static_cast<T&>(*this);
352 }
353 
354 template <typename T>
356  _domainSizeX = domainSizeX;
357  _useDomainSize = true;
358  return static_cast<T&>(*this);
359 }
360 
361 template <typename T>
363  _gridOrigin = gridOrigin;
364  return static_cast<T&>(*this);
365 }
366 
367 template <typename T>
369  Vector2D gridSpacing = _gridSpacing;
370  if (_useDomainSize) {
371  gridSpacing.set(_domainSizeX / static_cast<double>(_resolution.x));
372  }
373  return gridSpacing;
374 }
375 
380  : public GridFluidSolverBuilderBase2<GridFluidSolver2::Builder> {
381  public:
384 
387  return std::make_shared<GridFluidSolver2>(_resolution, getGridSpacing(),
388  _gridOrigin);
389  }
390 };
391 
392 } // namespace jet
393 
394 #endif // INCLUDE_JET_GRID_FLUID_SOLVER2_H_
jet::GridFluidSolver2::cfl
double cfl(double timeIntervalInSeconds) const
Returns the CFL number from the current velocity field for given time interval.
jet::GridFluidSolver2::numberOfSubTimeSteps
unsigned int numberOfSubTimeSteps(double timeIntervalInSeconds) const override
Returns the required sub-time-steps for given time interval.
jet::GridFluidSolver2::extrapolateIntoCollider
void extrapolateIntoCollider(FaceCenteredGrid2 *grid)
Extrapolates given field into the collider-occupied region.
grid_pressure_solver2.h
jet::GridFluidSolver2::velocity
const FaceCenteredGrid2Ptr & velocity() const
Returns the velocity field.
grid_system_data2.h
jet::GridFluidSolver2::advectionSolver
const AdvectionSolver2Ptr & advectionSolver() const
Returns the advection solver instance.
jet::GridFluidSolver2::setGravity
void setGravity(const Vector2D &newGravity)
Sets the gravity of the system.
jet::GridFluidSolver2::setAdvectionSolver
void setAdvectionSolver(const AdvectionSolver2Ptr &newSolver)
Sets the advection solver.
advection_solver2.h
jet::GridFluidSolver2::maxCfl
double maxCfl() const
Returns the max allowed CFL number.
jet::GridFluidSolverBuilderBase2::withOrigin
DerivedBuilder & withOrigin(const Vector2D &gridOrigin)
Returns builder with grid origin.
Definition: grid_fluid_solver2.h:362
jet::GridFluidSolver2::gridSpacing
Vector2D gridSpacing() const
Returns the grid spacing of the grid system data.
jet::GridFluidSolver2::setDiffusionSolver
void setDiffusionSolver(const GridDiffusionSolver2Ptr &newSolver)
Sets the diffusion solver.
jet::GridFluidSolverBuilderBase2::getGridSpacing
Vector2D getGridSpacing() const
Definition: grid_fluid_solver2.h:368
jet::Vector2D
Vector2< double > Vector2D
Double-type 2D vector.
Definition: vector2.h:340
jet::GridDiffusionSolver2Ptr
std::shared_ptr< GridDiffusionSolver2 > GridDiffusionSolver2Ptr
Shared pointer type for the GridDiffusionSolver2.
Definition: grid_diffusion_solver2.h:93
jet::PhysicsAnimation
Abstract base class for physics-based animation.
Definition: physics_animation.h:20
jet::GridFluidSolver2::setViscosityCoefficient
void setViscosityCoefficient(double newValue)
Sets the viscosity coefficient.
jet::GridFluidSolver2::setUseCompressedLinearSystem
void setUseCompressedLinearSystem(bool onoff)
Sets whether the solver should use compressed linear system.
jet::GridFluidSolver2::useCompressedLinearSystem
bool useCompressedLinearSystem() const
Returns true if the solver is using compressed linear system.
jet::GridFluidSolverBuilderBase2::withResolution
DerivedBuilder & withResolution(const Size2 &resolution)
Returns builder with grid resolution.
Definition: grid_fluid_solver2.h:333
jet::GridFluidSolver2::gridOrigin
Vector2D gridOrigin() const
Returns the origin of the grid system data.
grid_diffusion_solver2.h
jet::GridFluidSolver2::~GridFluidSolver2
virtual ~GridFluidSolver2()
Default destructor.
jet::GridFluidSolver2::extrapolateIntoCollider
void extrapolateIntoCollider(ScalarGrid2 *grid)
Extrapolates given field into the collider-occupied region.
jet::GridSystemData2Ptr
std::shared_ptr< GridSystemData2 > GridSystemData2Ptr
Shared pointer type of GridSystemData2.
Definition: grid_system_data2.h:237
jet::GridFluidSolver2::builder
static Builder builder()
Returns builder fox GridFluidSolver2.
grid_emitter2.h
jet::GridFluidSolver2::emitter
const GridEmitter2Ptr & emitter() const
Returns the emitter.
jet::GridFluidSolver2
Abstract base class for grid-based 2-D fluid solver.
Definition: grid_fluid_solver2.h:34
jet::GridFluidSolver2::viscosityCoefficient
double viscosityCoefficient() const
Returns the viscosity coefficient.
jet::GridFluidSolver2::onAdvanceTimeStep
void onAdvanceTimeStep(double timeIntervalInSeconds) override
Called when advancing a single time-step.
jet::GridFluidSolverBuilderBase2::withGridSpacing
DerivedBuilder & withGridSpacing(double gridSpacing)
Returns builder with grid spacing.
Definition: grid_fluid_solver2.h:347
grid_boundary_condition_solver2.h
jet::GridFluidSolver2::GridFluidSolver2
GridFluidSolver2()
Default constructor.
jet::kDirectionAll
constexpr int kDirectionAll
All direction.
Definition: constants.h:333
jet::FaceCenteredGrid2
2-D face-centered (a.k.a MAC or staggered) grid.
Definition: face_centered_grid2.h:26
jet::GridFluidSolver2::colliderSdf
ScalarField2Ptr colliderSdf() const
Returns the signed-distance field representation of the collider.
jet::GridFluidSolver2Ptr
std::shared_ptr< GridFluidSolver2 > GridFluidSolver2Ptr
Shared pointer type for the GridFluidSolver2.
Definition: grid_fluid_solver2.h:294
jet::GridFluidSolver2::computeViscosity
virtual void computeViscosity(double timeIntervalInSeconds)
Computes the viscosity term using the diffusion solver.
jet::GridFluidSolver2::collider
const Collider2Ptr & collider() const
Returns the collider.
jet
Definition: advection_solver2.h:18
jet::ScalarField2Ptr
std::shared_ptr< ScalarField2 > ScalarField2Ptr
Shared pointer for the ScalarField2 type.
Definition: scalar_field2.h:40
jet::GridFluidSolverBuilderBase2::_resolution
Size2 _resolution
Definition: grid_fluid_solver2.h:323
jet::GridFluidSolver2::resizeGrid
void resizeGrid(const Size2 &newSize, const Vector2D &newGridSpacing, const Vector2D &newGridOrigin)
Resizes grid system data.
jet::GridFluidSolver2::onEndAdvanceTimeStep
virtual void onEndAdvanceTimeStep(double timeIntervalInSeconds)
Called at the end of a time-step.
jet::ScalarGrid2
Abstract base class for 2-D scalar grid structure.
Definition: scalar_grid2.h:21
jet::GridFluidSolver2::setMaxCfl
void setMaxCfl(double newCfl)
Sets the max allowed CFL number.
jet::GridFluidSolverBuilderBase2::_domainSizeX
double _domainSizeX
Definition: grid_fluid_solver2.h:326
jet::Vector< T, 2 >
2-D vector class.
Definition: vector2.h:24
jet::AdvectionSolver2Ptr
std::shared_ptr< AdvectionSolver2 > AdvectionSolver2Ptr
Shared pointer type for the 2-D advection solver.
Definition: advection_solver2.h:113
jet::GridFluidSolver2::diffusionSolver
const GridDiffusionSolver2Ptr & diffusionSolver() const
Returns the diffusion solver instance.
jet::GridFluidSolver2::Builder::build
GridFluidSolver2 build() const
Builds GridFluidSolver2.
jet::GridFluidSolver2::onBeginAdvanceTimeStep
virtual void onBeginAdvanceTimeStep(double timeIntervalInSeconds)
Called at the beginning of a time-step.
jet::GridFluidSolver2::GridFluidSolver2
GridFluidSolver2(const Size2 &resolution, const Vector2D &gridSpacing, const Vector2D &gridOrigin)
Constructs solver with initial grid size.
jet::GridFluidSolverBuilderBase2::withDomainSizeX
DerivedBuilder & withDomainSizeX(double domainSizeX)
Returns builder with domain size in x-direction.
Definition: grid_fluid_solver2.h:355
jet::Collider2Ptr
std::shared_ptr< Collider2 > Collider2Ptr
Shared pointer type for the Collider2.
Definition: collider2.h:118
jet::FaceCenteredGrid2Ptr
std::shared_ptr< FaceCenteredGrid2 > FaceCenteredGrid2Ptr
Shared pointer type for the FaceCenteredGrid2.
Definition: face_centered_grid2.h:230
jet::CollocatedVectorGrid2
Abstract base class for 2-D collocated vector grid structure.
Definition: collocated_vector_grid2.h:18
collider2.h
cell_centered_scalar_grid2.h
jet::GridFluidSolver2::setPressureSolver
void setPressureSolver(const GridPressureSolver2Ptr &newSolver)
Sets the pressure solver.
jet::Vector< T, 2 >::set
void set(T s)
Set both x and y components to s.
jet::GridFluidSolver2::Builder::makeShared
GridFluidSolver2Ptr makeShared() const
Builds shared pointer of GridFluidSolver2 instance.
Definition: grid_fluid_solver2.h:386
face_centered_grid2.h
jet::Size2
2-D size class.
Definition: size2.h:19
jet::GridFluidSolver2::closedDomainBoundaryFlag
int closedDomainBoundaryFlag() const
Returns the closed domain boundary flag.
jet::GridFluidSolver2::fluidSdf
virtual ScalarField2Ptr fluidSdf() const
jet::VectorField2Ptr
std::shared_ptr< VectorField2 > VectorField2Ptr
Shared pointer for the VectorField2 type.
Definition: vector_field2.h:40
jet::GridFluidSolver2::gravity
const Vector2D & gravity() const
Returns the gravity vector of the system.
jet::GridFluidSolver2::setEmitter
void setEmitter(const GridEmitter2Ptr &newEmitter)
Sets the emitter.
jet::GridFluidSolver2::setCollider
void setCollider(const Collider2Ptr &newCollider)
Sets the collider.
jet::GridFluidSolverBuilderBase2::withGridSpacing
DerivedBuilder & withGridSpacing(const Vector2D &gridSpacing)
Returns builder with grid spacing.
Definition: grid_fluid_solver2.h:339
jet::GridPressureSolver2Ptr
std::shared_ptr< GridPressureSolver2 > GridPressureSolver2Ptr
Shared pointer type for the GridPressureSolver2.
Definition: grid_pressure_solver2.h:78
jet::GridFluidSolver2::applyBoundaryCondition
void applyBoundaryCondition()
Applies the boundary condition to the velocity field.
physics_animation.h
jet::GridFluidSolverBuilderBase2::_useDomainSize
bool _useDomainSize
Definition: grid_fluid_solver2.h:327
jet::GridFluidSolver2::extrapolateIntoCollider
void extrapolateIntoCollider(CollocatedVectorGrid2 *grid)
Extrapolates given field into the collider-occupied region.
jet::GridFluidSolver2::computePressure
virtual void computePressure(double timeIntervalInSeconds)
Computes the pressure term using the pressure solver.
jet::GridBoundaryConditionSolver2Ptr
std::shared_ptr< GridBoundaryConditionSolver2 > GridBoundaryConditionSolver2Ptr
Shared pointer type for the GridBoundaryConditionSolver2.
Definition: grid_boundary_condition_solver2.h:104
jet::GridFluidSolver2::computeGravity
void computeGravity(double timeIntervalInSeconds)
Computes the gravity term.
jet::GridEmitter2Ptr
std::shared_ptr< GridEmitter2 > GridEmitter2Ptr
Shared pointer type for the GridEmitter2.
Definition: grid_emitter2.h:70
jet::GridFluidSolver2::onInitialize
void onInitialize() override
Called when it needs to setup initial condition.
jet::GridFluidSolver2::setClosedDomainBoundaryFlag
void setClosedDomainBoundaryFlag(int flag)
Sets the closed domain boundary flag.
jet::GridFluidSolver2::colliderVelocityField
VectorField2Ptr colliderVelocityField() const
Returns the velocity field of the collider.
jet::GridFluidSolverBuilderBase2::_gridOrigin
Vector2D _gridOrigin
Definition: grid_fluid_solver2.h:325
jet::GridFluidSolver2::resolution
Size2 resolution() const
Returns the resolution of the grid system data.
jet::GridFluidSolverBuilderBase2::_gridSpacing
Vector2D _gridSpacing
Definition: grid_fluid_solver2.h:324
jet::GridFluidSolverBuilderBase2
Base class for grid-based fluid solver builder.
Definition: grid_fluid_solver2.h:300
jet::GridFluidSolver2::Builder
Front-end to create GridFluidSolver2 objects step by step.
Definition: grid_fluid_solver2.h:380
jet::GridFluidSolver2::computeExternalForces
virtual void computeExternalForces(double timeIntervalInSeconds)
Computes the external force terms.
jet::GridFluidSolver2::pressureSolver
const GridPressureSolver2Ptr & pressureSolver() const
Returns the pressure solver instance.
jet::GridFluidSolver2::gridSystemData
const GridSystemData2Ptr & gridSystemData() const
Returns the grid system data.
jet::GridFluidSolver2::computeAdvection
virtual void computeAdvection(double timeIntervalInSeconds)
Computes the advection term using the advection solver.