numerics
Loading...
Searching...
No Matches
fluid.hpp
Go to the documentation of this file.
1/// @file fluid.hpp
2/// @brief Weakly Compressible SPH (WCSPH) fluid solver -- public interface
3///
4/// FluidSolver is a data container + dispatch hub. All physics computation
5/// lives in apps/fluid_sim/backends/{seq,omp}/, mirroring the structure of
6/// src/backends/{seq,omp}/ in the numerics library.
7///
8/// @par Execution policies (params.policy)
9///
10/// Backend::seq (default)
11/// Single-threaded. Newton's 3rd law pair traversal (iterate_pairs)
12/// visits each unique {i,j} pair once -> O(n*k/2).
13///
14/// Backend::omp
15/// OpenMP parallel for over particles. Per-particle neighbour query
16/// so each thread writes only to particles_[i] -- no atomics needed.
17/// Falls back to seq when NUMERICS_HAS_OMP is not defined.
18///
19/// Policy dispatch in step() mirrors src/backends/dispatch.cpp:
20/// switch (params_.policy) { case Backend::omp: backends::omp::...; }
21#pragma once
22
23#include "particle.hpp"
24#include "rigid_body.hpp"
25#include "spatial_hash.hpp"
26#include "heat.hpp"
27#include "core/policy.hpp"
28#include <vector>
29
30namespace physics {
31
33 // SPH kernel
34 float h = 0.025f; ///< Smoothing length [m]
35
36 // Fluid properties
37 float rho0 = 1000.0f; ///< Rest density [kg/m^3]
38 int gamma = 7; ///< Tait EOS exponent
39 float c0 = 10.0f; ///< Speed of sound [m/s] -> B = rho_0c_0^2/gamma
40 float mu = 0.01f; ///< Dynamic viscosity [Pa*s]
41 float mass = 0.4f; ///< Particle mass [kg] (~= rho_0*dx^2, dx=0.8h)
42
43 // Body forces
44 float gx = 0.0f; ///< Gravity x [m/s^2]
45 float gy = -9.81f; ///< Gravity y [m/s^2]
46
47 // Time integration
48 float dt = 0.001f; ///< Timestep [s] (must satisfy CFL: dt < h/c0)
49
50 // Domain
51 float xmin = 0.0f;
52 float xmax = 1.0f;
53 float ymin = 0.0f;
54 float ymax = 0.7f;
55 float restitution = 0.3f; ///< Velocity restitution at walls
56
57 // Thermal
58 float alpha_T = 0.005f; ///< Thermal diffusivity [m^2/s]
59 float h_conv = 8.0f; ///< Convective coefficient with rigid bodies [1/s]
60
61 // Execution policy -- dispatched in FluidSolver::step()
62 num::Backend policy = num::Backend::seq; ///< seq = Newton pairs; omp = parallel
63};
64
66public:
67 explicit FluidSolver(const FluidParams& params);
68
69 void add_particle(float x, float y, float vx, float vy, float temperature);
70 void add_body(const RigidBody& body);
71 void clear();
72
73 /// Advance the simulation by one timestep (params_.dt).
74 /// Dispatches to backends::seq or backends::omp based on params_.policy.
75 void step();
76
77 const std::vector<Particle>& particles() const { return particles_; }
78 const std::vector<RigidBody>& bodies() const { return bodies_; }
79 std::vector<RigidBody>& bodies() { return bodies_; }
80 const FluidParams& params() const { return params_; }
81
82 float min_temp() const { return T_min_; }
83 float max_temp() const { return T_max_; }
84
85private:
86 FluidParams params_;
87 std::vector<Particle> particles_;
88 std::vector<RigidBody> bodies_;
89 SpatialHash grid_; ///< CellList2D-backed, rebuilt each step
90
91 float T_min_ = 0.0f;
92 float T_max_ = 100.0f;
93};
94
95} // namespace physics
float max_temp() const
Definition fluid.hpp:83
const FluidParams & params() const
Definition fluid.hpp:80
void add_particle(float x, float y, float vx, float vy, float temperature)
Definition fluid.cpp:28
const std::vector< RigidBody > & bodies() const
Definition fluid.hpp:78
void add_body(const RigidBody &body)
Definition fluid.cpp:39
float min_temp() const
Definition fluid.hpp:82
const std::vector< Particle > & particles() const
Definition fluid.hpp:77
std::vector< RigidBody > & bodies()
Definition fluid.hpp:79
Heat transfer parameters for the SPH backends.
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
@ seq
Naive textbook loops – always available.
SPH particle data structure.
Backend enum for linear algebra operations.
Rigid body (sphere) for SPH collision and heat exchange.
SPH neighbour search – now powered by num::CellList2D.
float rho0
Rest density [kg/m^3].
Definition fluid.hpp:37
float alpha_T
Thermal diffusivity [m^2/s].
Definition fluid.hpp:58
float mu
Dynamic viscosity [Pa*s].
Definition fluid.hpp:40
int gamma
Tait EOS exponent.
Definition fluid.hpp:38
float h
Smoothing length [m].
Definition fluid.hpp:34
num::Backend policy
seq = Newton pairs; omp = parallel
Definition fluid.hpp:62
float c0
Speed of sound [m/s] -> B = rho_0c_0^2/gamma.
Definition fluid.hpp:39
float gy
Gravity y [m/s^2].
Definition fluid.hpp:45
float restitution
Velocity restitution at walls.
Definition fluid.hpp:55
float gx
Gravity x [m/s^2].
Definition fluid.hpp:44
float h_conv
Convective coefficient with rigid bodies [1/s].
Definition fluid.hpp:59
float dt
Timestep [s] (must satisfy CFL: dt < h/c0)
Definition fluid.hpp:48
float mass
Particle mass [kg] (~= rho_0*dx^2, dx=0.8h)
Definition fluid.hpp:41
A rigid spherical body that interacts with fluid particles.
Definition rigid_body.hpp:8