PhysicsHelpers.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_PHYSICS_HELPERS_HPP
12 #define CUBBYFLOW_PHYSICS_HELPERS_HPP
13 
14 #include <Core/Matrix/Matrix.hpp>
15 
16 namespace CubbyFlow
17 {
18 inline Vector2D ComputeDragForce(double dragCoefficient, double radius,
19  const Vector2D& velocity)
20 {
21  // Stoke's drag force assuming our Reynolds number is very low.
22  // http://en.wikipedia.org/wiki/Drag_(physics)#Very_low_Reynolds_numbers:_Stokes.27_drag
23  return -6.0 * PI_DOUBLE * dragCoefficient * radius * velocity;
24 }
25 
26 inline Vector3D ComputeDragForce(double dragCoefficient, double radius,
27  const Vector3D& velocity)
28 {
29  // Stoke's drag force assuming our Reynolds number is very low.
30  // http://en.wikipedia.org/wiki/Drag_(physics)#Very_low_Reynolds_numbers:_Stokes.27_drag
31  return -6.0 * PI_DOUBLE * dragCoefficient * radius * velocity;
32 }
33 
34 template <size_t N>
36  const Vector<double, N>& normal,
37  double frictionCoefficient)
38 {
39  Vector<double, N> velt = vel.Projected(normal);
40  if (velt.LengthSquared() > 0)
41  {
42  const double veln = std::max(-vel.Dot(normal), 0.0);
43  velt *= std::max(1.0 - frictionCoefficient * veln / velt.Length(), 0.0);
44  }
45 
46  return velt;
47 }
48 
49 inline double ComputePressureFromEos(double density, double targetDensity,
50  double eosScale, double eosExponent,
51  double negativePressureScale)
52 {
53  // See Murnaghan-Tait equation of state from
54  // https://en.wikipedia.org/wiki/Tait_equation
55  double p = eosScale / eosExponent *
56  (std::pow((density / targetDensity), eosExponent) - 1.0);
57 
58  // Negative pressure scaling
59  if (p < 0)
60  {
61  p *= negativePressureScale;
62  }
63 
64  return p;
65 }
66 } // namespace CubbyFlow
67 
68 #endif
ValueType LengthSquared() const
Definition: MatrixExpression-Impl.hpp:286
constexpr double PI_DOUBLE
Double-type PI.
Definition: Constants.hpp:77
double ComputePressureFromEos(double density, double targetDensity, double eosScale, double eosExponent, double negativePressureScale)
Definition: PhysicsHelpers.hpp:49
ValueType Length() const
Definition: MatrixExpression-Impl.hpp:278
Vector< double, N > ProjectAndApplyFriction(const Vector< double, N > &vel, const Vector< double, N > &normal, double frictionCoefficient)
Definition: PhysicsHelpers.hpp:35
Definition: Matrix.hpp:27
std::enable_if_t<(IsMatrixSizeDynamic< Rows, Cols >)||Cols==1) &&(IsMatrixSizeDynamic< R, C >)||C==1), U > Dot(const MatrixExpression< T, R, C, E > &expression) const
Definition: MatrixExpression-Impl.hpp:391
Definition: pybind11Utils.hpp:20
std::enable_if_t<(IsMatrixSizeDynamic< Rows, Cols >)||((Rows==2||Rows==3) &&Cols==1)) &&(IsMatrixSizeDynamic< R, C >)||((R==2||R==3) &&C==1)), Matrix< U, Rows, 1 > > Projected(const MatrixExpression< T, R, C, E > &normal) const
Returns the projected vector to the surface with given surface normal.
Definition: MatrixExpression-Impl.hpp:465
Vector2D ComputeDragForce(double dragCoefficient, double radius, const Vector2D &velocity)
Definition: PhysicsHelpers.hpp:18