Skip to content

Model particleflow: a spray by a quadrature method of moments (QMOM) - #12

Open
ptrembli wants to merge 11 commits into
mainfrom
feature/qmom-model
Open

ptrembli wants to merge 11 commits into
mainfrom
feature/qmom-model

Conversation

@ptrembli

@ptrembli ptrembli commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Closes #1.

A new model, physics/particleflow/: a disperse phase of particles described by the moments of their
velocity distribution up to the third order (M0, M1, M2, diagonal M3), closed on two weighted
velocity nodes. The particles are transported at their own velocity, so two crossing jets go through
each other. They are dragged towards the velocity Uf of a carrier fluid (one-way coupling) and
accelerated by gravity. Ported from the model particlefluid of canoP legacy (branch
48-temporary-spray-model-implementation-as-an-independent-model).

Reading order

Read the commits one by one. Each message lists the legacy functions it ports and every choice
that differs from the legacy model. model.py is split into sections, and each commit only adds
sections at the end of the file.

  1. physics: the relaxations receive the time step: framework only. The signature becomes
    relaxation(w, qn, p, dt, dim) for every model. The C++ changes are comments only.
  2. particleflow: variables, parameters and states of the model
  3. particleflow: inversion of the moments on two velocity nodes: cons_to_prim / prim_to_cons.
  4. particleflow: kinetic flux, quasi-second-order reconstruction, time step and walls
  5. particleflow: drag, gravity and reprojection after the time step
  6. particleflow: initial, boundary and refinement conditions: the model runs from this commit.
  7. particleflow: three examples: settings/settings_particleflow_{taylor_green,jet_crossing,focusing_ring}.ini.
  8. particleflow: energy-preserving inversion: the legacy option param.energy_preserving (off by
    default), in its own commit so it can be dropped.
  9. docs: model particleflow: Readme, documentation, CHANGELOG.
  10. test: physical properties of the model particleflow
  11. test: regression cases of the model particleflow

Differences from the legacy model

  • No maximum principle. The legacy limiter computes a weight from the skewness of the secondary
    axes. That quantity is ill-conditioned when the spread of the velocities is much smaller than their
    mean, so the limiter amplified round-off errors and broke the symmetry of the Taylor-Green case.
    Variants with guards or smooth weights did not remove this sensitivity at 2048² with tau = 1.
    The realizability fallback of the legacy face states is removed too.
  • Exactly antisymmetric kinetic flux. The left and right contributions are summed apart, so a
    mirror-symmetric problem stays bitwise symmetric.
  • Relaxations once per time step, after the average of the Runge-Kutta stages (Lie splitting):
    reprojection on the two nodes, then the drag and the gravity integrated exactly over dt. In the
    legacy model the drag was applied in each stage: it was exact only to first order in dt / tau.

Numerics

  • Space: quasi-second order (Vikas et al. 2011). Only the node densities n1 and n2 are
    reconstructed at the faces, with the limiter simulation.scalar_limiter; the velocities stay those
    of the cell. The moments stay realizable for cfl <= 2/3 with minmod and cfl <= 1/2 with the
    other limiters.
  • Time: SSP Runge-Kutta 2 (Heun) or forward Euler.
  • Known limit of the closure: two nodes cannot represent three or more crossing streams. In the
    Taylor-Green case with tau = 1, compared with an exact Lagrangian solution, the large-scale bands
    are right but the caustics inside them are smeared.

Tests

  • Physics (ctest -L physics):
    • mirror: a focusing ring that crosses on a reflective wall. The half domain gives exactly half of
      the full domain (difference 0).
    • outflow: crossing slabs leaving through the y faces stay uniform along y (difference 0).
    • drag (new): a uniform velocity follows the exact solution of the drag and the gravity, to 1e-16
      or better, at time_order = 2.
  • Regression: 12 cases particleflow_*, 2 of them in 3D, with references produced by canoP. They
    cover every initial, boundary and refinement condition, every relaxation, energy_preserving, first
    order and AMR. regression.py now expands symmetric tensors (M2_xx, M2_xy, ...). Reruns with 4
    threads instead of 2, and with an empty kernel cache, give the references bitwise. The other models'
    cases are unchanged.
  • CI cost: 8 more pykokkos kernels (the same in 2D and 3D), about 1 min of compilation without
    the cache.
  • Other checks: 14 existing regression cases pass after commit 1. tools/canop_physics.py (studio
    and docs) reports no error at any commit. On CPU, Taylor-Green 256² with tau = 1 up to t = 2 is
    bitwise identical to the earlier version of the port run with its maximum principle off. That
    version also ran on GPU (A100) at 2048² up to t = 20.

🤖 Generated with Claude Code

guyontristan and others added 11 commits December 1, 2026 08:35
A relaxation was an instantaneous projection, applied once after the time step:
relaxation(w, qn, p, dim). A relaxation with a finite time scale (the drag of particles towards
a carrier flow, integrated exactly over the step) needs the time step, so the generated dispatch
and the kernel now pass it: relaxation(w, qn, p, dt, dim).

- physics/loops.py: relaxations_kernel takes dt and gives it to relaxations().
- physics/common.py: the generated relaxations() and each relaxation_relax_<name>() take dt;
  Physics.relaxations() passes the dt it receives from canoP.
- twophase7eq relaxations (pressure, velocity, pressure_velocity): new signature, dt unused
  (instantaneous projections, unchanged).
- relaxations/__init__.py of onephase, twophase5eq, twophase7eq: the documented signature.
- src/: comments only. canoP already passed dt to Physics::relaxations (python/Physics.h); the
  C++ code is unchanged.

No change of results: the 14 regression cases of the existing models give the same outputs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
First commit of the model particleflow, a disperse phase of particles (spray) described by the
moments of its velocity distribution and closed by two velocity nodes (quadrature method of
moments, QMOM). It is ported from the model particlefluid of canoP legacy (branch
48-temporary-spray-model-implementation-as-an-independent-model, src/particlefluid). The model
runs from the commit "initial, boundary and refinement conditions"; until then, its directory only
holds what the next commits use.

physics/particleflow/__init__.py
- variables(): conservative M0, M1_<i>, M2_<ij> (lower triangle: xx, xy, yy[, xz, yz, zz]),
  M3_<i> (diagonal third-order moments only, as legacy), Uf_<i> (carrier velocity, given in each
  cell); primitive n1, n2, V1_<i>, V2_<i>, Uf_<i>; derived Up_<i> (mean velocity), a_<ij>
  (covariance), a_<iii> (central third-order moments). Same variables as the cons_t/prim_t of
  legacy QdataParticlefluid.h.
- params(): tau, tau_BGK, thresh_rho, thresh_var as in legacy set_param (same defaults);
  the gravity, a scalar in legacy (param.gravity), is a vector gravity_x/y/z (default 0);
  ghost_velocity is renamed cfl_velocity (lower bound of the velocity of the time step).
  Not ported: param.max_princ, the maximum principle of the inversion (its limiter compares the
  skewnesses of the axes, differences of raw moments that are mostly rounding errors when the spread
  of the velocities is small compared to their mean, and the solution then depends on the rounding
  errors). param.energy_preserving comes in its own commit.
- The docstring describes the whole model: equations, scheme, time step, CFL bounds, the
  differences with legacy (drag after the time step) and the parameters.

physics/particleflow/model.py (kernel functions; next commits append their sections)
- States and parameters: classes Prim and Cons (components of 3D, those of z unused in 2D),
  Params and make_params, gravity(), zero_prim(), zero_cons().
- Components: accessors of the nodes (vel, set_vel), of the carrier velocity and of the moments
  (m1, m2 with the index sym of the lower triangle, m3).
- Arrays: load/store of the states in the arrays [octant][variable][cell] of canoP.
- Derived output fields: derived_field() for Up, a_ij, a_iii (legacy cons_to_field).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The closure of the model: the two weighted nodes (n1, V1), (n2, V2) of the moments of a cell, and
the moments of two nodes. Ported from legacy cons_to_prim, prim_to_cons and ensure_uniqueness
(QdataManagerParticlefluid.cpp), without the maximum principle (param.max_princ).

model.py, section "Moments of the nodes and inversion":
- prim_to_cons(q): M0, M1, M2 (lower triangle) and the diagonal M3 of the two nodes, with the
  carrier velocity.
- cons_to_prim(w), as legacy:
  1. density below thresh_rho: vacuum (zero nodes);
  2. mean velocity v, covariance a_ij = M2_ij / M0 - v_i v_j and central third-order moments
     b_i = M3_i / M0 - 3 M2_ii v_i / M0 + 2 v_i^3; negative variances (rounding errors of
     realizable moments) are set to zero;
  3. trace of the covariance below thresh_var: one node, at the mean velocity;
  4. main axis m = the largest variance (the first one in case of a tie);
  5. weights from the skewness of the main axis: beta = b_m / sqrt(b_m^2 + 4 a_mm^3),
     n1,2 = M0 (1 +- beta) / 2; a weight below thresh_rho leaves one node, at v;
  6. nodes on the line through v along the main axis: deviation sqrt(a_mm) along m and
     a_im / sqrt(a_mm) along the other axes i, scaled by sqrt(n2/n1) and sqrt(n1/n2), so that the
     two nodes give M0, M1, M2_mm, M2_im and M3_m exactly;
  7. the nodes are labelled so that n1 >= n2 (swap_nodes, legacy ensure_uniqueness).
- The two nodes give M2_ii of the other axes only up to a_im^2 / a_mm <= a_ii and M3 of the other
  axes approximately: this is the closure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tep and walls

The transport of the moments. Ported from legacy hyperbolic_flux_basic
(HyperbolicFluxParticlefluid.cpp), custom_limiter, flux_function, prim_to_invdt and swap_dir
(QdataManagerParticlefluid.cpp), with one change: the flux of a face is exactly antisymmetric.

hyperbolic_fluxes/kinetic.py, riemann():
- each node crosses the face from the side it leaves: the nodes of the left face state with
  V_x > 0 and those of the right face state with V_x < 0, each carrying n V_x (1, V, V V, V_i^3)
  (node_flux); the carrier velocity has no flux;
- the contributions of the left nodes and of the right nodes are summed apart, then added. The
  flux of the mirror image of a face (left and right states exchanged and mirrored) is then exactly
  the opposite in floating point, and a problem with mirror symmetries keeps them to the last bit
  (Taylor-Green). Legacy added the four node fluxes in one sum, whose rounding depends on the side.

model.py, sections "Time step", "Face states, rotations", "Walls", "Fluxes of a face":
- hyperbolic_inv_dt: max(sum_d |V1_d|, sum_d |V2_d|, cfl_velocity) / h (legacy prim_to_invdt,
  with cfl_velocity for particles at rest at the start).
- face_state: quasi-second order (Vikas et al. 2011, legacy custom_limiter): only the densities n1,
  n2 of the nodes get half their limited slope at second order; the velocities of the nodes stay
  those of the cell, which keeps the face densities non-negative and the moments realizable within
  the CFL condition (see the docstring of __init__.py).
- rotate_prim, rotate_cons: the direction of the face becomes x (legacy swap_dir).
- Walls (kinds given by the boundary conditions): reflective (normal velocity of the nodes
  reversed), no-slip (every velocity reversed), outflow, imposed state, reconstructed ghost state;
  reflect_moments mirrors the moments odd in the normal velocity.
- combine_prim, wall_gradient: required by the diffusion fluxes of loops.py (this model has none).
- add_cons, store_flux: the sum of fluxes and their storage times dt / h.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The source terms of the particles, as relaxations applied once after the time step (they use the
time step given by the commit "the relaxations receive the time step"). Ported from legacy
source_term_reprojection, source_term_drag, source_term_gravity and source_term_drag_and_BGK
(SourceTermParticlefluid.cpp).

model.py, section "Relaxations: exact maps of the moments":
- affine_velocity_map(w, alpha, b): maps every velocity v of the distribution to alpha v + b,
  exactly on the moments (M1, M2, M3 as polynomials of alpha and b); the moments stay realizable.
- copy_cons.

relaxations/ (value of physics.relaxation):
- reprojection: w <- prim_to_cons(cons_to_prim(w)), the moments of their two nodes, no drag.
- drag: reprojection, then dV/dt = (Uf - V) / tau + g integrated exactly over dt: the affine map
  V -> alpha V + (1 - alpha)(Uf + tau g), alpha = exp(-dt / tau). The reprojected moments stay on two
  nodes.
- gravity: reprojection, then every velocity shifted by g dt.
- drag_bgk: as drag, then a BGK relaxation of the velocities to their mean,
  V -> beta V + (1 - beta) Up, beta = exp(-dt / tau_BGK).

Difference with legacy: legacy applied these terms in each stage of the time integration, before
the average of the Runge-Kutta stages. Averaging two distributions whose velocities differ changes
the weights of the nodes after the reprojection, and the drag is then exact only to first order in
dt / tau. Applied once after the step (Lie splitting from the transport), the drag is exact for a
uniform state at any dt / tau. The docstring of __init__.py says it too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The problems of legacy InitialConditionParticlefluid.cpp, BoundaryConditionParticlefluid.cpp and
RefineConditionParticlefluid.cpp, one file per option. With this commit the model runs.

initial_conditions/ (physics.initial_condition), each with its parameters in its own section:
- bump: a Gaussian bump of particles with a uniform velocity, one node;
- confining_channel: particles in a carrier flow along x converging towards an axis;
- empty: a uniform density of particles at rest, vacuum by default (inflow problems);
- focusing_ring: a ring of particles moving towards its centre, no carrier flow;
- jet_crossing_1d: two slabs of particles moving towards each other, in a carrier flow;
- rotational_channel: a disk of particles at rest in a carrier flow in solid rotation;
- straight_channel: particles at the velocity of a uniform carrier flow;
- taylor_green: a uniform density of particles in the Taylor-Green vortices of the carrier flow.
  The position is folded into [0, 1/4]^2 with the signs of the velocity set by the mirrors, so that
  the initial state has the mirror symmetries of the vortices to the last bit.

boundary_conditions/ (physics.boundary_condition):
- neuman (free outflow), reflective (specular reflection of the nodes);
- jet_crossing, left_inlet, top_inlet: jets of particles injected through faces of the domain.

refine_conditions/ (physics.refine_condition): M0_gradient, any_gradient (M0 and M1), the relative
jumps of legacy.

model.py, sections "Geometry of the problems" (dist2) and "Refinement" (scalar_gradient).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Input files of three problems of legacy, small enough for a workstation (a few minutes on 4 cores):

- settings_particleflow_taylor_green.ini: particles at rest in the Taylor-Green vortices of the
  carrier flow, drag with tau = 0.1 (the particles collect between the vortices), uniform 128^2,
  t = 4; periodic domain.
- settings_particleflow_jet_crossing.ini: two jets injected through the faces of the domain cross
  each other (boundary condition jet_crossing), no drag (relaxation = reprojection), AMR levels 5
  to 8 refined on the gradient of M0.
- settings_particleflow_focusing_ring.ini: a ring of particles moving towards its centre, where the
  two-node closure makes the particles cross (crossing trajectories), uniform 64^2.

All use the kinetic flux, the quasi-second order in space, RK2 and cfl = 0.4 (the CFL bounds are
in the docstring of physics/particleflow/__init__.py).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…egacy option)

The option energy_preserving of legacy cons_to_prim, in its own commit so that it can be reviewed,
or dropped, alone. Off by default (0): the results of the previous commits are unchanged.

With energy_preserving = 1, the inversion replaces each cross correlation a_ij by
sign(a_ij) sqrt(a_ii a_jj) (a correlation of +-1) before placing the nodes. The deviation of the
nodes along a secondary axis i is then sqrt(a_ii) instead of a_im / sqrt(a_mm), so the two nodes
keep every variance a_ii, hence the trace of M2 (the kinetic energy of the particles), at the
price of the cross moments M2_ij. Legacy applied it to the pair (x, y) only (2D); here to every
pair (i, j), the same in 2D. A zero correlation stays zero, as the sign() of legacy.

- physics/particleflow/__init__.py: the parameter and its description (experimental).
- physics/particleflow/model.py: the field of Params and the replacement in cons_to_prim, after
  the choice of the main axis and before the weights.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Readme.md and docs/index.md: particleflow in the list of models.
- docs/user-guide/models.md: a section particleflow (variables, kinetic flux at quasi-second order,
  relaxations after the time step); "the four models".
- CHANGELOG.md: the new model, under Unreleased.

The reference of the options and parameters (docs/physics-guide) is generated from the files of
physics/ when the documentation is built.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Three checks of test/physics/physics.py (label "physics"), without reference outputs, now include
the model particleflow:

- mirror: a ring of particles focusing on a point of the plane x = 0.5 (focusing_ring, centre
  (0.5, 0.45), radius 0.15, thickness 0.15), with reflective walls, the relaxation drag_bgk and the
  gravity along y. The particles cross at the centre of the ring, on the wall of the half domain,
  where the states have two nodes. The half domain [0, 0.5] x [0, 1] with a wall at x = 0.5 gives
  exactly the half of the full domain (difference 0 on M0, M1, M2, M3): the kinetic flux sums the
  left and right contributions apart, so a mirror-symmetric problem stays bitwise symmetric, and the
  reflective wall is that symmetry plane.
- outflow: two slabs of particles crossing along x (jet_crossing_1d: two-node states), dragged
  towards the carrier velocity (1, 1), with outflow (neuman) faces: they leave through ymax and
  the state stays uniform along y (variation 0).
- drag (new check, added to test/CMakeLists.txt): a bump of particles with a uniform velocity V0,
  no carrier flow, periodic, time_order = 2. The relaxations are applied once after each time step
  and integrate the drag and the gravity exactly, so the velocity M1 / M0 stays uniform and equals
  V0 exp(-t/tau) + g tau (1 - exp(-t/tau)) (relaxations drag and drag_bgk) or V0 + g t (gravity) at
  the final time: error 3e-17 to 7e-16 of |V0| (tolerance 1e-12), mass conserved to 1e-16. Here
  dt / tau = 0.2: a drag applied in each stage before the average of the stages, as in canoP legacy,
  would be off by (1 - exp(-dt/tau))^2 / 2 ~ 1.6e-2 per step and fail.

docs/developer-guide/tests.md: the physics tests also check the exact drag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Twelve regression cases of the model particleflow (label "regression", "3d" for two of them), short
runs on 1024 to 4096 cells compared cell by cell with their reference outputs.

test/regression/regression.py, cases.py: the conservative variables of particleflow are M0, the
vectors M1, M3, Uf and the symmetric tensor M2, written as its lower triangle (M2_xx, M2_xy, M2_yy
in 2D; xx, xy, yy, xz, yz, zz in 3D). expand() gives the components of the tensors listed in
SYM_TENSORS, and the scale of a component in the comparison is the maximum of all the components of
its vector or tensor (suffix _[xyz]+; unchanged for the variables of the other models).

The cases (settings files settings_particleflow_*.ini with overrides) cover every initial,
boundary and refinement condition, every relaxation, the option energy_preserving, the first order,
AMR and 3D:

- taylor_green: drag towards the Taylor-Green vortices (tau = 0.1), periodic, RK2, minmod;
- taylor_green_crossing, _first_order, _energy_preserving: tau = 1, the particles cross the
  vortices (two-node states), at second and first order, and with param.energy_preserving = 1;
- focusing_ring: free particles crossing at the centre (reprojection), outflow faces;
- focusing_ring_walls_gravity: reflective walls, relaxation gravity;
- rotational_channel: particles at rest in a carrier flow in solid rotation, drag, walls;
- confining_channel: two jets through xmin (left_inlet) crossing in a converging carrier flow with
  a background of particles, relaxation drag_bgk;
- jet_crossing_amr: the example jet_crossing (two jets in vacuum) with AMR, levels 4 to 6;
- top_inlet_amr: two annular jets (top_inlet), AMR with refinement any_gradient, drag_bgk, gravity;
- top_inlet_3d: the annular jets in 3D (two-node states in 3D), drag;
- straight_channel_3d: jets (left_inlet) in a uniform carrier flow with a background, 3D, drag.

The references are produced by canoP (reference = "canop", provenance "canoP d4e733b"), with 2
OpenMP threads: canoP legacy (particlefluid) limits the moments and applies the drag in each stage,
so it cannot produce them. All their states are finite, with 8 % to 100 % of two-node cells. Runs
with 4 threads and with an empty kernel cache give the references bitwise (difference 0). 1.4 MB.

The 8 kernels of particleflow (the same in 2D and 3D) compile in about 1 min 10 s after the set up
of the compilation: the kernels kept by the CI go from 49 to 57.

test/regression/README.md and docs/developer-guide/tests.md: the cases of particleflow and why their
references are produced by canoP; the scale of the components of a tensor; 57 kernels in the CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ptrembli
ptrembli requested a review from guyontristan December 1, 2026 07:50
@ptrembli ptrembli linked an issue Dec 1, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement qmom model

2 participants