Loading...
Searching...
No Matches
MPMFluidSystemData-Impl.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_MPM_FLUID_SYSTEM_DATA_IMPL_HPP
12#define CUBBYFLOW_MPM_FLUID_SYSTEM_DATA_IMPL_HPP
13
14#include <algorithm>
15#include <cmath>
16#include <stdexcept>
17
18namespace CubbyFlow
19{
20template <size_t N>
31
32template <size_t N>
34{
35 Base::Resize(newNumberOfParticles);
36
37 m_volumeRatios.Resize(newNumberOfParticles, 1.0);
38 m_velocityGradients.Resize(newNumberOfParticles, MatrixType{});
39}
40
41template <size_t N>
42void MPMFluidSystemData<N>::Deserialize(const std::vector<uint8_t>& buffer)
43{
44 Base::Deserialize(buffer);
45 ResetFluidState();
46}
47
48template <size_t N>
50{
51 Base::Set(other);
52 ResetFluidState();
53}
54
55template <size_t N>
57{
58 return m_volumeRatios.View();
59}
60
61template <size_t N>
63{
64 return m_volumeRatios.View();
65}
66
67template <size_t N>
70{
71 return m_velocityGradients.View();
72}
73
74template <size_t N>
77{
78 return m_velocityGradients.View();
79}
80
81template <size_t N>
83 double timeStepInSeconds)
84{
85 if (!std::isfinite(timeStepInSeconds) || timeStepInSeconds < 0.0)
86 {
87 throw std::invalid_argument("Invalid MPM fluid time step.");
88 }
89
90 this->ValidateGridToParticleState();
91
92 Array1<double> nextVolumeRatios(this->NumberOfParticles(), 1.0);
93 Array1<MatrixType> nextVelocityGradients(this->NumberOfParticles(),
94 MatrixType{});
95
96 for (size_t i = 0; i < this->NumberOfParticles(); ++i)
97 {
98 if (!std::isfinite(m_volumeRatios[i]) || m_volumeRatios[i] <= 0.0)
99 {
100 throw std::invalid_argument("Invalid MPM fluid volume ratio.");
101 }
102
103 const MatrixType gradient = this->ComputeVelocityGradient(i);
104 const double volumeIncrement =
105 (MatrixType::MakeIdentity() + timeStepInSeconds * gradient)
106 .Determinant();
107 const double volumeRatio = m_volumeRatios[i] * volumeIncrement;
108
109 if (!std::isfinite(volumeIncrement) || volumeIncrement <= 0.0 ||
110 !std::isfinite(volumeRatio) || volumeRatio <= 0.0)
111 {
112 throw std::invalid_argument("Invalid MPM fluid volume update.");
113 }
114
115 nextVelocityGradients[i] = gradient;
117 }
118
119 this->TransferFromGridToParticlesUnchecked();
120
121 std::ranges::copy(nextVolumeRatios, m_volumeRatios.begin());
122 std::ranges::copy(nextVelocityGradients, m_velocityGradients.begin());
123}
124
125template <size_t N>
127{
128 m_volumeRatios.Fill(1.0);
129 m_velocityGradients.Fill(MatrixType{});
130}
131
132} // namespace CubbyFlow
133
134#endif
N-D weakly compressible MPM fluid transfer state.
Definition MPMFluidSystemData.hpp:26
void Resize(size_t newNumberOfParticles) override
Resizes particle state, initializing new fluid attributes.
Definition MPMFluidSystemData-Impl.hpp:33
void Set(const ParticleSystemData< N > &other) override
Copies inherited particle state and resets fluid state.
Definition MPMFluidSystemData-Impl.hpp:49
void Deserialize(const std::vector< uint8_t > &buffer) override
Deserializes inherited particle state and resets fluid state.
Definition MPMFluidSystemData-Impl.hpp:42
MPMFluidSystemData(const Vector< size_t, N > &resolution=Vector< size_t, N >::MakeConstant(1), const Vector< double, N > &gridSpacing=Vector< double, N >::MakeConstant(1.0), const Vector< double, N > &gridOrigin=Vector< double, N >{}, size_t numberOfParticles=0)
Constructs fluid MPM state with a vertex-centered background grid.
Definition MPMFluidSystemData-Impl.hpp:21
ConstArrayView1< double > VolumeRatios() const
Returns current-to-reference particle volume ratios.
Definition MPMFluidSystemData-Impl.hpp:56
ConstArrayView1< MatrixType > VelocityGradients() const
Returns particle velocity gradients interpolated from the grid.
Definition MPMFluidSystemData-Impl.hpp:69
void TransferFromGridToParticles()
Definition MPMSystemData-Impl.hpp:367
ValueType Determinant() const
Definition MatrixExpression-Impl.hpp:198
Definition Matrix.hpp:30
Iterator begin()
Definition Matrix-Impl.hpp:272
Definition pybind11Utils.hpp:22
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:648