MPS-Basic
Loading...
Searching...
No Matches
pressure_poisson_equation.cpp
Go to the documentation of this file.
2
3#include "../weight.hpp"
4
5#include <iostream>
6
8
10 int dimension,
11 double dt,
12 double relaxationCoefficient,
13 double compressibility,
14 double n0_forNumberDensity,
15 double n0_forLaplacian,
16 double lambda0,
17 double reForLaplacian,
18 double reForNumberDensity
19) {
20 this->dimension = dimension;
21 this->dt = dt;
22 this->relaxationCoefficient = relaxationCoefficient;
23 this->compressibility = compressibility;
24 this->n0_forNumberDensity = n0_forNumberDensity;
25 this->n0_forLaplacian = n0_forLaplacian;
26 this->lambda0 = lambda0;
27 this->reForLaplacian = reForLaplacian;
28 this->reForNumberDensity = reForNumberDensity;
29}
30
32 const Particles& particles, const DirichletBoundaryCondition& dirichletBoundaryCondition
33) {
34 this->particlesCount = particles.size();
35
37 setSourceTerm(particles, dirichletBoundaryCondition);
38 setMatrixTriplets(particles, dirichletBoundaryCondition);
39 coefficientMatrix.setFromTriplets(matrixTriplets.begin(), matrixTriplets.end());
40}
41
42std::vector<double> PressurePoissonEquation::solve() {
43 using std::cerr;
44 using std::endl;
45
46 Eigen::BiCGSTAB<Eigen::SparseMatrix<double, Eigen::RowMajor>> solver;
47 solver.compute(coefficientMatrix);
48 Eigen::VectorXd pressure = solver.solve(sourceTerm);
49 if (solver.info() != Eigen::Success) {
50 cerr << "Pressure calculation failed." << endl;
51 std::exit(-1);
52 }
53
54 // This conversion is done by giving std::vector the pointers to the first and the last elements of the
55 // Eigen::VectorXd. If this type of conversion appears frequently, consider defining a function to convert the
56 // vector type.
57 std::vector<double> pressureStdVec(pressure.data(), pressure.data() + pressure.size());
58 return pressureStdVec;
59}
60
66
74 const Particles& particles, const DirichletBoundaryCondition& dirichletBoundaryCondition
75) {
76 double n0 = this->n0_forNumberDensity;
77 double gamma = this->relaxationCoefficient;
78
79#pragma omp parallel for
80 for (auto& pi : particles) {
81 if (dirichletBoundaryCondition.contains(pi.id)) {
82 // When dirichlet boundary condition is set, the source term is the boundary condition value.
83 sourceTerm[pi.id] = dirichletBoundaryCondition.value(pi.id);
84 } else {
85 sourceTerm[pi.id] = gamma * (1.0 / (dt * dt)) * ((pi.numberDensity - n0) / n0);
86 }
87 }
88}
89
100 const Particles& particles, const DirichletBoundaryCondition& dirichletBoundaryCondition
101) {
102 auto a = 2.0 * dimension / (n0_forLaplacian * lambda0);
103 auto re = reForLaplacian;
105
106 for (auto& pi : particles) {
107 if (dirichletBoundaryCondition.contains(pi.id)) {
108 // When Dirichlet boundary conditions are set, only the diagonal term is set to 1 so that the pressure is at
109 // the specified value.
110 matrixTriplets.emplace_back(pi.id, pi.id, 1.0);
111 continue;
112 }
113
114 double coefficient_ii = 0.0;
115 for (auto& neighbor : pi.neighbors) {
116 auto& pj = particles[neighbor.id];
117 if (pj.boundaryCondition == FluidState::Ignored) {
118 continue;
119 }
120
121 if (neighbor.distance < re) {
122 double coefficient_ij = a * weight.weightFn(neighbor.distance, re) / pi.density;
123 matrixTriplets.emplace_back(pi.id, pj.id, -1.0 * coefficient_ij);
124 coefficient_ii += coefficient_ij;
125 }
126 }
127 coefficient_ii += (compressibility) / (dt * dt);
128 matrixTriplets.emplace_back(pi.id, pi.id, coefficient_ii);
129 }
130}
A collection of particles.
Definition particles.hpp:10
int size() const
Get the number of particles.
Definition particles.cpp:22
double value(int id) const
Get the boundary condition value.
bool contains(int id) const
Check if the boundary condition contains the particle.
Class for setting up and solving pressure Poisson equation.
void setSourceTerm(const Particles &particles, const DirichletBoundaryCondition &dirichletBoundaryCondition)
Set the source term for the pressure Poisson equation.
Eigen::SparseMatrix< double, Eigen::RowMajor > coefficientMatrix
Coefficient matrix for pressure Poisson equation.
std::vector< double > solve()
Solve pressure Poisson equation.
void setup(const Particles &particles, const DirichletBoundaryCondition &dirichletBoundaryCondition)
Setup pressure Poisson equation.
void setMatrixTriplets(const Particles &particles, const DirichletBoundaryCondition &dirichletBoundaryCondition)
Set the matrix triplets for the pressure Poisson equation.
Eigen::VectorXd sourceTerm
Source term for pressure Poisson equation.
std::vector< Eigen::Triplet< double > > matrixTriplets
Triplets for coefficient matrix.
Provides a weight function.
Definition weight.hpp:15
double weightFn(double dis, double re)
Weight function for MPS method.
Definition weight.cpp:10
@ Ignored
Ghost or dummy.