PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Functions
test_poisson_rhs.c File Reference

C unit tests for Poisson, RHS, body-force, and diffusivity helpers. More...

#include "test_support.h"
#include "poisson.h"
#include "rhs.h"
#include "ParticlePhysics.h"
#include "setup.h"
#include "verification_sources.h"
Include dependency graph for test_poisson_rhs.c:

Go to the source code of this file.

Functions

static PetscErrorCode EnsurePoissonAndRhsVectors (UserCtx *user)
 Allocates Poisson/RHS support vectors required by the tests.
 
static PetscErrorCode TestUpdatePressureAddsPhi (void)
 Tests that pressure updates add the correction potential.
 
static PetscErrorCode TestPoissonRHSZeroDivergence (void)
 Tests that the Poisson RHS is zero for zero-divergence velocity.
 
static PetscErrorCode TestComputeBodyForcesDispatcher (void)
 Tests the body-force dispatcher across supported source terms.
 
static PetscErrorCode TestComputeEulerianDiffusivityMolecularOnly (void)
 Tests Eulerian diffusivity for the molecular-only configuration.
 
static PetscErrorCode TestConvectionZeroField (void)
 Tests that convection vanishes for a quiescent field.
 
static PetscErrorCode TestViscousUniformField (void)
 Tests that viscous terms vanish for a uniform field.
 
static PetscErrorCode TestViscousWallModelledFaceCarriesTheStoredStress (void)
 Tests that a wall-modelled face carries the stored wall stress and no gradient flux.
 
static PetscErrorCode ScaleMinimalMetricsToSpacing (UserCtx *user, PetscReal h)
 Rescales the minimal fixture's unit metrics to cubic cells of edge h.
 
static PetscErrorCode ClarkToMolecularRatioPerX (PetscReal h, PetscInt probe, PetscReal *ratio_per_x)
 Returns the Clark contribution over the molecular viscous one, per unit x, at one cell.
 
static PetscErrorCode TestClarkGradientModelScalesWithFilterWidthSquared (void)
 Tests that the Clark gradient model scales as the square of the filter width.
 
static PetscErrorCode TestComputeRHSZeroFieldNoForcing (void)
 Tests that the full RHS remains zero without forcing on a quiescent field.
 
static PetscErrorCode TestComputeEulerianDiffusivityGradientConstantField (void)
 Tests diffusivity-gradient computation on a constant field.
 
static PetscErrorCode TestComputeEulerianDiffusivityVerificationLinearX (void)
 Tests verification-driven linear diffusivity override and its gradient.
 
static PetscErrorCode TestDiffusivityGradientFillsPeriodicDummyCells (void)
 Tests that the diffusivity gradient fills its periodic dummy cells.
 
static PetscErrorCode TestAssemblePoissonOperatorPopulatesRows (void)
 Tests that Poisson matrix assembly produces a populated operator on a tiny Cartesian grid.
 
static PetscErrorCode RandomizeMinimalFaceMetrics (UserCtx *user)
 Fills every face metric and Jacobian of the minimal fixture with reproducible random values, so each face gradient carries all of its cross terms.
 
static PetscErrorCode TestOperatorAndProjectionShareOneFaceGradient (void)
 Tests that the operator is exactly the divergence of the projection's gradient.
 
static PetscErrorCode TestProjectionZeroPhiLeavesVelocityUnchanged (void)
 Tests that projection leaves a zero pressure-correction field unchanged.
 
static PetscErrorCode TestProjectionLinearPhiCorrectsVelocity (void)
 Tests that projection applies the expected x-direction correction for a linear pressure field.
 
static PetscErrorCode PerturbInteriorFaceFluxes (UserCtx *user, PetscReal amplitude)
 Adds a smooth divergent perturbation to every interior face flux.
 
static PetscErrorCode TestPoissonSolverMultigridProjectsToDivergenceFree (void)
 Tests that the production multigrid Poisson solve and projection remove a divergence.
 
static PetscErrorCode TestPoissonSolverMultigridKeepsThePressureLevel (void)
 Tests that the multigrid solve returns Phi without a constant, so P keeps its level.
 
static PetscErrorCode CountFileOccurrences (const char *path, const char *needle, PetscInt *count)
 Counts the occurrences of needle in a text file; a missing file counts zero.
 
static PetscErrorCode TestPoissonSolverMultigridReusesItsSolver (void)
 Tests that the multigrid solver is built once and reused on later steps.
 
static PetscErrorCode TestPoissonSolverMultigridHonoursDistinctSweeps (void)
 Tests that unequal pre- and post-smoothing counts give two distinct smoothers.
 
static PetscErrorCode TestPoissonNullSpaceRemovesTheInteriorMean (void)
 Tests the null space attached to the operator.
 
static PetscErrorCode TestPoissonSolverMultigridRefusesAnOvercoarsenedHierarchy (void)
 Tests that an over-coarsened hierarchy stops the run at the Poisson solve.
 
int main (int argc, char **argv)
 Runs the unit-poisson-rhs PETSc test binary.
 

Detailed Description

C unit tests for Poisson, RHS, body-force, and diffusivity helpers.

Definition in file test_poisson_rhs.c.

Function Documentation

◆ EnsurePoissonAndRhsVectors()

static PetscErrorCode EnsurePoissonAndRhsVectors ( UserCtx *  user)
static

Allocates Poisson/RHS support vectors required by the tests.

Definition at line 17 of file test_poisson_rhs.c.

18{
19 PetscFunctionBeginUser;
20 if (!user->Phi) PetscCall(DMCreateGlobalVector(user->da, &user->Phi));
21 if (!user->lPhi) PetscCall(DMCreateLocalVector(user->da, &user->lPhi));
22 if (!user->Aj) PetscCall(DMCreateGlobalVector(user->da, &user->Aj));
23 if (!user->lAj) PetscCall(DMCreateLocalVector(user->da, &user->lAj));
24 if (!user->Diffusivity) PetscCall(DMCreateGlobalVector(user->da, &user->Diffusivity));
25 if (!user->lDiffusivity) PetscCall(DMCreateLocalVector(user->da, &user->lDiffusivity));
26 PetscFunctionReturn(0);
27}
Vec Diffusivity
Definition variables.h:1200
Vec lDiffusivity
Definition variables.h:1200
Here is the caller graph for this function:

◆ TestUpdatePressureAddsPhi()

static PetscErrorCode TestUpdatePressureAddsPhi ( void  )
static

Tests that pressure updates add the correction potential.

Definition at line 32 of file test_poisson_rhs.c.

33{
34 SimCtx *simCtx = NULL;
35 UserCtx *user = NULL;
36
37 PetscFunctionBeginUser;
38 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
39 PetscCall(EnsurePoissonAndRhsVectors(user));
40 PetscCall(VecSet(user->P, 1.0));
41 PetscCall(VecSet(user->Phi, 0.25));
42
43 PetscCall(UpdatePressure(user));
44 PetscCall(PicurvAssertVecConstant(user->P, 1.25, 1.0e-12, "UpdatePressure should add Phi into P"));
45
46 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
47 PetscFunctionReturn(0);
48}
PetscErrorCode UpdatePressure(UserCtx *user)
Adds the pressure correction to the pressure, P += Phi, and refreshes both fields' periodic images an...
Definition poisson.c:405
static PetscErrorCode EnsurePoissonAndRhsVectors(UserCtx *user)
Allocates Poisson/RHS support vectors required by the tests.
PetscErrorCode PicurvCreateMinimalContexts(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz)
Builds minimal SimCtx and UserCtx fixtures for C unit tests.
PetscErrorCode PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Destroys minimal SimCtx/UserCtx fixtures and all owned PETSc objects.
PetscErrorCode PicurvAssertVecConstant(Vec vec, PetscScalar expected, PetscReal tol, const char *context)
Asserts that a PETSc vector is spatially constant within tolerance.
The master context for the entire simulation.
Definition variables.h:916
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPoissonRHSZeroDivergence()

static PetscErrorCode TestPoissonRHSZeroDivergence ( void  )
static

Tests that the Poisson RHS is zero for zero-divergence velocity.

Definition at line 53 of file test_poisson_rhs.c.

54{
55 SimCtx *simCtx = NULL;
56 UserCtx *user = NULL;
57 Vec B = NULL;
58
59 PetscFunctionBeginUser;
60 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
61 PetscCall(EnsurePoissonAndRhsVectors(user));
62
63 simCtx->dt = 0.5;
64 PetscCall(VecSet(user->Ucont, 0.0));
65 PetscCall(VecSet(user->Nvert, 0.0));
66 PetscCall(VecSet(user->Aj, 1.0));
67 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
68 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
69 PetscCall(DMGlobalToLocalBegin(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
70 PetscCall(DMGlobalToLocalEnd(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
71 PetscCall(DMGlobalToLocalBegin(user->da, user->Aj, INSERT_VALUES, user->lAj));
72 PetscCall(DMGlobalToLocalEnd(user->da, user->Aj, INSERT_VALUES, user->lAj));
73
74 PetscCall(VecDuplicate(user->P, &B));
75 PetscCall(ComputePoissonRHS(user, B));
76 PetscCall(PicurvAssertVecConstant(B, 0.0, 1.0e-12, "zero velocity divergence should produce zero Poisson RHS"));
77 PetscCall(PicurvAssertRealNear(0.0, simCtx->poissonSourceImbalance, 1.0e-12, "global Poisson RHS sum should be zero"));
78
79 PetscCall(VecDestroy(&B));
80 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
81 PetscFunctionReturn(0);
82}
PetscErrorCode ComputePoissonRHS(UserCtx *user, Vec B)
Forms the right-hand side of the pressure-correction equation.
Definition poisson.c:344
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Asserts that two real values agree within tolerance.
Vec lNvert
Definition variables.h:1197
PetscReal poissonSourceImbalance
Definition variables.h:1095
PetscReal dt
Definition variables.h:933
Vec Ucont
Definition variables.h:1197
Vec lUcont
Definition variables.h:1197
Vec Nvert
Definition variables.h:1197
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestComputeBodyForcesDispatcher()

static PetscErrorCode TestComputeBodyForcesDispatcher ( void  )
static

Tests the body-force dispatcher across supported source terms.

Definition at line 87 of file test_poisson_rhs.c.

88{
89 SimCtx *simCtx = NULL;
90 UserCtx *user = NULL;
91 Vec rct = NULL;
92 Cmpnts ***rct_arr = NULL;
93 Cmpnts ***l_csi = NULL;
94 Cmpnts ***l_eta = NULL;
95 Cmpnts ***l_zet = NULL;
96
97 PetscFunctionBeginUser;
98 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
99 PetscCall(VecDuplicate(user->Ucont, &rct));
100 PetscCall(VecZeroEntries(rct));
101
108 simCtx->bulkVelocityCorrection = 2.0;
109 simCtx->dt = 1.0;
110 simCtx->forceScalingFactor = 1.0;
111 simCtx->drivingForceMagnitude = 0.0;
112 PetscCall(VecSet(user->Nvert, 0.0));
113 PetscCall(DMGlobalToLocalBegin(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
114 PetscCall(DMGlobalToLocalEnd(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
115 PetscCall(DMDAVecGetArray(user->fda, user->lCsi, &l_csi));
116 PetscCall(DMDAVecGetArray(user->fda, user->lEta, &l_eta));
117 PetscCall(DMDAVecGetArray(user->fda, user->lZet, &l_zet));
118 l_csi[1][1][1].x = 1.0; l_csi[1][1][1].y = 0.0; l_csi[1][1][1].z = 0.0;
119 l_eta[1][1][1].x = 0.0; l_eta[1][1][1].y = 1.0; l_eta[1][1][1].z = 0.0;
120 l_zet[1][1][1].x = 0.0; l_zet[1][1][1].y = 0.0; l_zet[1][1][1].z = 1.0;
121 PetscCall(DMDAVecRestoreArray(user->fda, user->lZet, &l_zet));
122 PetscCall(DMDAVecRestoreArray(user->fda, user->lEta, &l_eta));
123 PetscCall(DMDAVecRestoreArray(user->fda, user->lCsi, &l_csi));
124
125 PetscCall(ComputeBodyForces(user, rct));
126 PetscCall(DMDAVecGetArrayRead(user->fda, rct, &rct_arr));
127 PetscCall(PicurvAssertRealNear(1.5, simCtx->drivingForceMagnitude, 1.0e-12,
128 "ComputeBodyForces should update the driven-flow controller magnitude"));
129 PetscCall(PicurvAssertRealNear(0.0, rct_arr[1][1][1].y, 1.0e-12, "ComputeBodyForces should keep y unchanged"));
130 PetscCall(PicurvAssertRealNear(0.0, rct_arr[1][1][1].z, 1.0e-12, "ComputeBodyForces should keep z unchanged"));
131 PetscCall(DMDAVecRestoreArrayRead(user->fda, rct, &rct_arr));
132
133 PetscCall(VecDestroy(&rct));
134 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
135 PetscFunctionReturn(0);
136}
PetscErrorCode ComputeBodyForces(UserCtx *user, Vec Rct)
General dispatcher for applying all active body forces (momentum sources).
Definition rhs.c:426
@ PERIODIC
Definition variables.h:325
BoundaryFaceConfig boundary_faces[6]
Definition variables.h:1182
PetscReal forceScalingFactor
Definition variables.h:1018
@ BC_HANDLER_PERIODIC_DRIVEN_CONSTANT_FLUX
Definition variables.h:354
BCHandlerType handler_type
Definition variables.h:427
PetscReal bulkVelocityCorrection
Definition variables.h:1030
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
PetscScalar y
Definition variables.h:145
BCType mathematical_type
Definition variables.h:426
PetscReal drivingForceMagnitude
Definition variables.h:1018
@ BC_FACE_NEG_X
Definition variables.h:309
@ BC_FACE_POS_X
Definition variables.h:309
A 3D point or vector with PetscScalar components.
Definition variables.h:144
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestComputeEulerianDiffusivityMolecularOnly()

static PetscErrorCode TestComputeEulerianDiffusivityMolecularOnly ( void  )
static

Tests Eulerian diffusivity for the molecular-only configuration.

Definition at line 141 of file test_poisson_rhs.c.

142{
143 SimCtx *simCtx = NULL;
144 UserCtx *user = NULL;
145 PetscReal ***diff_arr = NULL;
146
147 PetscFunctionBeginUser;
148 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
149 PetscCall(EnsurePoissonAndRhsVectors(user));
150
151 simCtx->ren = 2.0;
152 simCtx->schmidt_number = 4.0;
153 simCtx->les = PETSC_FALSE;
154 PetscCall(VecZeroEntries(user->Diffusivity));
155
156 PetscCall(ComputeEulerianDiffusivity(user));
157 PetscCall(DMDAVecGetArrayRead(user->da, user->Diffusivity, &diff_arr));
158 PetscCall(PicurvAssertRealNear(0.125, diff_arr[1][1][1], 1.0e-12, "molecular diffusivity interior value"));
159 PetscCall(DMDAVecRestoreArrayRead(user->da, user->Diffusivity, &diff_arr));
160
161 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
162 PetscFunctionReturn(0);
163}
PetscErrorCode ComputeEulerianDiffusivity(UserCtx *user)
Computes the effective diffusivity scalar field (Gamma_eff) on the Eulerian grid.
PetscReal schmidt_number
Definition variables.h:1005
PetscReal ren
Definition variables.h:965
PetscInt les
Active LES closure; an LESModelType value.
Definition variables.h:1041
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestConvectionZeroField()

static PetscErrorCode TestConvectionZeroField ( void  )
static

Tests that convection vanishes for a quiescent field.

Definition at line 168 of file test_poisson_rhs.c.

169{
170 SimCtx *simCtx = NULL;
171 UserCtx *user = NULL;
172 Vec conv = NULL;
173
174 PetscFunctionBeginUser;
175 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
176 PetscCall(VecDuplicate(user->lUcont, &conv));
177 PetscCall(VecSet(user->Ucont, 0.0));
178 PetscCall(VecSet(user->Ucat, 0.0));
179 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
180 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
181 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
182 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
183
184 PetscCall(ComputeConvection(user, user->lUcont, user->lUcat, conv));
185 PetscCall(PicurvAssertVecConstant(conv, 0.0, 1.0e-12, "Convection should vanish for a quiescent field"));
186
187 PetscCall(VecDestroy(&conv));
188 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
189 PetscFunctionReturn(0);
190}
PetscErrorCode ComputeConvection(UserCtx *user, Vec Ucont, Vec Ucat, Vec Conv)
Computes the convective contribution to the contravariant momentum RHS.
Definition rhs.c:96
Vec lUcat
Definition variables.h:1197
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestViscousUniformField()

static PetscErrorCode TestViscousUniformField ( void  )
static

Tests that viscous terms vanish for a uniform field.

Definition at line 195 of file test_poisson_rhs.c.

196{
197 SimCtx *simCtx = NULL;
198 UserCtx *user = NULL;
199 Vec visc = NULL;
200
201 PetscFunctionBeginUser;
202 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
203 PetscCall(VecDuplicate(user->lUcont, &visc));
204 PetscCall(VecSet(user->Ucont, 1.0));
205 PetscCall(VecSet(user->Ucat, 1.0));
206 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
207 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
208 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
209 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
210
211 PetscCall(ComputeViscous(user, user->lUcat, visc));
212 PetscCall(PicurvAssertVecConstant(visc, 0.0, 1.0e-12, "Viscous should vanish for a uniform field"));
213
214 PetscCall(VecDestroy(&visc));
215 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
216 PetscFunctionReturn(0);
217}
PetscErrorCode ComputeViscous(UserCtx *user, Vec Ucat, Vec Visc)
Computes the viscous contribution to the contravariant momentum RHS.
Definition rhs.c:346
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestViscousWallModelledFaceCarriesTheStoredStress()

static PetscErrorCode TestViscousWallModelledFaceCarriesTheStoredStress ( void  )
static

Tests that a wall-modelled face carries the stored wall stress and no gradient flux.

The wall model acts only through this face, so the check is the one that matters: with a resolved velocity whose gradient flux would vanish everywhere, the first cell's viscous term must be exactly minus the stored stress times the face area, and it must vanish again once the wall model is off, when the face reverts to its gradient flux.

Definition at line 226 of file test_poisson_rhs.c.

227{
228 SimCtx *simCtx = NULL;
229 UserCtx *user = NULL;
230 Vec visc = NULL;
231 Cmpnts ***stress = NULL, ***v = NULL;
232 const PetscReal tau = 0.37;
233
234 PetscFunctionBeginUser;
235 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
236 PetscCall(VecDuplicate(user->lUcont, &visc));
237 PetscCall(VecSet(user->Ucat, 1.0));
238 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
239 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
242
243 PetscCall(DMCreateGlobalVector(user->fda, &user->Wall_Stress));
244 PetscCall(DMCreateLocalVector(user->fda, &user->lWall_Stress));
245 PetscCall(VecSet(user->Wall_Stress, 0.0));
246 PetscCall(DMDAVecGetArray(user->fda, user->Wall_Stress, &stress));
247 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k)
248 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j)
249 if (user->info.xs == 0) stress[k][j][0].y = tau;
250 PetscCall(DMDAVecRestoreArray(user->fda, user->Wall_Stress, &stress));
251 PetscCall(DMGlobalToLocalBegin(user->fda, user->Wall_Stress, INSERT_VALUES, user->lWall_Stress));
252 PetscCall(DMGlobalToLocalEnd(user->fda, user->Wall_Stress, INSERT_VALUES, user->lWall_Stress));
253
255 PetscCall(ComputeViscous(user, user->lUcat, visc));
256 PetscCall(DMDAVecGetArrayRead(user->fda, visc, &v));
257 PetscCall(PicurvAssertRealNear(-tau, v[2][2][1].y, 1.0e-12,
258 "the first cell must receive minus the stored stress times the unit face area"));
259 PetscCall(PicurvAssertRealNear(0.0, v[2][2][2].y, 1.0e-12,
260 "a cell away from the wall must not see the wall stress"));
261 PetscCall(DMDAVecRestoreArrayRead(user->fda, visc, &v));
262
263 simCtx->wallfunction = 0;
264 PetscCall(ComputeViscous(user, user->lUcat, visc));
265 PetscCall(DMDAVecGetArrayRead(user->fda, visc, &v));
266 PetscCall(PicurvAssertRealNear(0.0, v[2][2][1].y, 1.0e-12,
267 "without a wall model the face must carry its gradient flux, zero here"));
268 PetscCall(DMDAVecRestoreArrayRead(user->fda, visc, &v));
269
270 PetscCall(VecDestroy(&user->lWall_Stress));
271 PetscCall(VecDestroy(&user->Wall_Stress));
272 PetscCall(VecDestroy(&visc));
273 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
274 PetscFunctionReturn(0);
275}
Vec Wall_Stress
Definition variables.h:1193
@ WALL
Definition variables.h:319
@ BC_HANDLER_WALL_NOSLIP
Definition variables.h:345
@ WALL_FUNCTION_WERNER
Definition variables.h:603
PetscInt wallfunction
Enable wall functions on WALL faces.
Definition variables.h:1042
Vec lWall_Stress
Definition variables.h:1193
DMDALocalInfo info
Definition variables.h:1169
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ScaleMinimalMetricsToSpacing()

static PetscErrorCode ScaleMinimalMetricsToSpacing ( UserCtx *  user,
PetscReal  h 
)
static

Rescales the minimal fixture's unit metrics to cubic cells of edge h.

Definition at line 280 of file test_poisson_rhs.c.

281{
282 Vec area_vectors[12] = {user->Csi, user->Eta, user->Zet, user->ICsi, user->IEta, user->IZet,
283 user->JCsi, user->JEta, user->JZet, user->KCsi, user->KEta, user->KZet};
284 Vec local_area[12] = {user->lCsi, user->lEta, user->lZet, user->lICsi, user->lIEta, user->lIZet,
285 user->lJCsi, user->lJEta, user->lJZet, user->lKCsi, user->lKEta, user->lKZet};
286 Vec jacobians[4] = {user->Aj, user->IAj, user->JAj, user->KAj};
287 Vec local_jac[4] = {user->lAj, user->lIAj, user->lJAj, user->lKAj};
288
289 PetscFunctionBeginUser;
290 for (PetscInt v = 0; v < 12; ++v) {
291 PetscCall(VecScale(area_vectors[v], h * h)); /* a face of a cube of edge h */
292 PetscCall(DMGlobalToLocalBegin(user->fda, area_vectors[v], INSERT_VALUES, local_area[v]));
293 PetscCall(DMGlobalToLocalEnd(user->fda, area_vectors[v], INSERT_VALUES, local_area[v]));
294 }
295 for (PetscInt v = 0; v < 4; ++v) {
296 PetscCall(VecSet(jacobians[v], 1.0 / (h * h * h)));
297 PetscCall(DMGlobalToLocalBegin(user->da, jacobians[v], INSERT_VALUES, local_jac[v]));
298 PetscCall(DMGlobalToLocalEnd(user->da, jacobians[v], INSERT_VALUES, local_jac[v]));
299 }
300 PetscFunctionReturn(0);
301}
Vec lIEta
Definition variables.h:1254
Vec lIZet
Definition variables.h:1254
Vec lKEta
Definition variables.h:1256
Vec lJCsi
Definition variables.h:1255
Vec lKZet
Definition variables.h:1256
Vec lJEta
Definition variables.h:1255
Vec lKCsi
Definition variables.h:1256
Vec lJZet
Definition variables.h:1255
Vec lICsi
Definition variables.h:1254
Here is the caller graph for this function:

◆ ClarkToMolecularRatioPerX()

static PetscErrorCode ClarkToMolecularRatioPerX ( PetscReal  h,
PetscInt  probe,
PetscReal *  ratio_per_x 
)
static

Returns the Clark contribution over the molecular viscous one, per unit x, at one cell.

The field is u_x = x^2 on cubic cells of edge h, so du/dx = 2x and the exact gradient-model stress tau_11 = (h^2/12)(du/dx)^2 varies along x. Both the model's and viscosity's contributions leave ComputeViscous() through the same flux differences, so their ratio does not depend on how ComputeViscous() normalizes its output.

Definition at line 312 of file test_poisson_rhs.c.

313{
314 SimCtx *simCtx = NULL;
315 UserCtx *user = NULL;
316 Vec molecular = NULL, with_clark = NULL;
317 Cmpnts ***ucat = NULL, ***visc0 = NULL, ***visc1 = NULL;
318 const PetscInt n = 10, mid = n / 2;
319
320 PetscFunctionBeginUser;
321 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, n, n, n));
322 PetscCall(ScaleMinimalMetricsToSpacing(user, h));
323 simCtx->ren = 1.0;
324 simCtx->les = NO_LES_MODEL;
325
326 PetscCall(DMDAVecGetArray(user->fda, user->Ucat, &ucat));
327 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k)
328 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j)
329 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i) {
330 const PetscReal x = (i - 0.5) * h; /* cell centre */
331 ucat[k][j][i].x = x * x; ucat[k][j][i].y = 0.0; ucat[k][j][i].z = 0.0;
332 }
333 PetscCall(DMDAVecRestoreArray(user->fda, user->Ucat, &ucat));
334 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
335 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
336 PetscCall(VecSet(user->lUcont, 0.0));
337
338 PetscCall(VecDuplicate(user->lUcont, &molecular));
339 PetscCall(VecDuplicate(user->lUcont, &with_clark));
340 simCtx->les_gradient_model = 0;
341 PetscCall(ComputeViscous(user, user->lUcat, molecular));
342 simCtx->les_gradient_model = 1;
343 PetscCall(ComputeViscous(user, user->lUcat, with_clark));
344
345 PetscCall(DMDAVecGetArrayRead(user->fda, molecular, &visc0));
346 PetscCall(DMDAVecGetArrayRead(user->fda, with_clark, &visc1));
347 {
348 const PetscReal v_molecular = visc0[mid][mid][probe].x;
349 const PetscReal v_clark = visc1[mid][mid][probe].x - v_molecular;
350 PetscCheck(PetscAbsReal(v_molecular) > 0.0, PETSC_COMM_SELF, PETSC_ERR_PLIB,
351 "The molecular viscous term vanished at the probe; the fixture is wrong.");
352 *ratio_per_x = (v_clark / v_molecular) / ((probe - 0.5) * h);
353 }
354 PetscCall(DMDAVecRestoreArrayRead(user->fda, with_clark, &visc1));
355 PetscCall(DMDAVecRestoreArrayRead(user->fda, molecular, &visc0));
356 PetscCall(VecDestroy(&with_clark));
357 PetscCall(VecDestroy(&molecular));
358 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
359 PetscFunctionReturn(0);
360}
static PetscErrorCode ScaleMinimalMetricsToSpacing(UserCtx *user, PetscReal h)
Rescales the minimal fixture's unit metrics to cubic cells of edge h.
@ NO_LES_MODEL
Definition variables.h:584
PetscInt les_gradient_model
Add the Clark gradient (tensor-diffusivity) term to the viscous flux.
Definition variables.h:1045
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestClarkGradientModelScalesWithFilterWidthSquared()

static PetscErrorCode TestClarkGradientModelScalesWithFilterWidthSquared ( void  )
static

Tests that the Clark gradient model scales as the square of the filter width.

The gradient-model stress is tau_ij = (Delta_k^2/12) du_i/dx_k du_j/dx_k, so for a fixed physical field its force grows as Delta^2 relative to the molecular viscous force. Halving the spacing must therefore divide the Clark-to-molecular ratio at a given x by four. Fixtures with unit cells hide any error in the power of Delta, which is why the spacing is varied here.

Definition at line 371 of file test_poisson_rhs.c.

372{
373 PetscReal coarse = 0.0, fine = 0.0;
374
375 PetscFunctionBeginUser;
376 /* Probe cells chosen so the two spacings give comparable x without being on a
377 boundary: cell 5 at h = 0.2 and cell 5 at h = 0.1. The ratio is per unit x. */
378 PetscCall(ClarkToMolecularRatioPerX(0.2, 5, &coarse));
379 PetscCall(ClarkToMolecularRatioPerX(0.1, 5, &fine));
380 PetscCall(PicurvAssertBool((PetscBool)(coarse < 0.0 && fine < 0.0),
381 "the gradient model removes momentum where du/dx grows along x"));
382 PetscCall(PicurvAssertRealNear(4.0, coarse / fine, 1.0e-8,
383 "halving the filter width divides the gradient-model force by four"));
384 /* Exact value: the molecular flux carries both halves of the symmetric stress,
385 2 nu du/dx per unit area, so its difference across a cell is 4 nu h^3 for
386 u = x^2; the model's is -(2/3) h^5 x. Their ratio per unit x is -h^2/(6 nu),
387 which pins the 1/12 coefficient as well as the power of Delta. */
388 PetscCall(PicurvAssertRealNear(-0.2 * 0.2 / 6.0, coarse, 1.0e-10,
389 "gradient-model force matches (Delta^2/12) du/dx du/dx at h = 0.2"));
390 PetscCall(PicurvAssertRealNear(-0.1 * 0.1 / 6.0, fine, 1.0e-10,
391 "gradient-model force matches (Delta^2/12) du/dx du/dx at h = 0.1"));
392 PetscFunctionReturn(0);
393}
static PetscErrorCode ClarkToMolecularRatioPerX(PetscReal h, PetscInt probe, PetscReal *ratio_per_x)
Returns the Clark contribution over the molecular viscous one, per unit x, at one cell.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Asserts that one boolean condition is true.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestComputeRHSZeroFieldNoForcing()

static PetscErrorCode TestComputeRHSZeroFieldNoForcing ( void  )
static

Tests that the full RHS remains zero without forcing on a quiescent field.

Definition at line 399 of file test_poisson_rhs.c.

400{
401 SimCtx *simCtx = NULL;
402 UserCtx *user = NULL;
403
404 PetscFunctionBeginUser;
405 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
406 PetscCall(VecSet(user->Ucont, 0.0));
407 PetscCall(VecSet(user->Ucat, 0.0));
408 PetscCall(VecSet(user->Nvert, 0.0));
409 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
410 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
411 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
412 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucat, INSERT_VALUES, user->lUcat));
413 PetscCall(DMGlobalToLocalBegin(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
414 PetscCall(DMGlobalToLocalEnd(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
415
416 PetscCall(ComputeRHS(user, user->Rhs));
417 PetscCall(PicurvAssertVecConstant(user->Rhs, 0.0, 1.0e-12, "ComputeRHS should remain zero without forcing on a quiescent field"));
418
419 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
420 PetscFunctionReturn(0);
421}
PetscErrorCode ComputeRHS(UserCtx *user, Vec Rhs)
Computes the Right-Hand Side (RHS) of the momentum equations.
Definition rhs.c:616
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestComputeEulerianDiffusivityGradientConstantField()

static PetscErrorCode TestComputeEulerianDiffusivityGradientConstantField ( void  )
static

Tests diffusivity-gradient computation on a constant field.

Definition at line 426 of file test_poisson_rhs.c.

427{
428 SimCtx *simCtx = NULL;
429 UserCtx *user = NULL;
430
431 PetscFunctionBeginUser;
432 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
433 PetscCall(VecSet(user->Diffusivity, 0.75));
434 PetscCall(DMGlobalToLocalBegin(user->da, user->Diffusivity, INSERT_VALUES, user->lDiffusivity));
435 PetscCall(DMGlobalToLocalEnd(user->da, user->Diffusivity, INSERT_VALUES, user->lDiffusivity));
436
437 PetscCall(ComputeEulerianDiffusivityGradient(user));
438 PetscCall(PicurvAssertVecConstant(user->DiffusivityGradient, 0.0, 1.0e-12, "constant diffusivity should yield zero diffusivity gradient"));
439
440 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
441 PetscFunctionReturn(0);
442}
PetscErrorCode ComputeEulerianDiffusivityGradient(UserCtx *user)
Computes the Eulerian gradient of the effective diffusivity field.
Vec DiffusivityGradient
Definition variables.h:1201
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestComputeEulerianDiffusivityVerificationLinearX()

static PetscErrorCode TestComputeEulerianDiffusivityVerificationLinearX ( void  )
static

Tests verification-driven linear diffusivity override and its gradient.

Definition at line 447 of file test_poisson_rhs.c.

448{
449 SimCtx *simCtx = NULL;
450 UserCtx *user = NULL;
451 PetscReal ***diff_arr = NULL;
452 Cmpnts ***grad_arr = NULL;
453 Cmpnts ***cent = NULL;
454 const PetscReal gamma0 = 0.5;
455 const PetscReal slope_x = 0.25;
456
457 PetscFunctionBeginUser;
458 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
459 PetscCall(PetscStrncpy(simCtx->eulerianSource, "analytical", sizeof(simCtx->eulerianSource)));
460 PetscCall(VerificationFieldCompile("0.5 + 0.25*x", &simCtx->verificationDiffusivity));
461
463 "verification diffusivity override should report as active"));
464 PetscCall(ComputeEulerianDiffusivity(user));
465 PetscCall(ComputeEulerianDiffusivityGradient(user));
466
467 PetscCall(DMDAVecGetArrayRead(user->da, user->Diffusivity, &diff_arr));
468 PetscCall(DMDAVecGetArrayRead(user->fda, user->Cent, &cent));
469 PetscCall(PicurvAssertRealNear(gamma0 + slope_x * cent[2][2][2].x, diff_arr[2][2][2], 1.0e-12,
470 "verification override should populate the linear diffusivity field"));
471 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->Cent, &cent));
472 PetscCall(DMDAVecRestoreArrayRead(user->da, user->Diffusivity, &diff_arr));
473
474 PetscCall(DMDAVecGetArrayRead(user->fda, user->DiffusivityGradient, &grad_arr));
475 PetscCall(PicurvAssertRealNear(slope_x / user->IM, grad_arr[2][2][2].x, 1.0e-12,
476 "linear verification diffusivity should yield the current finite-difference x gradient"));
477 PetscCall(PicurvAssertRealNear(0.0, grad_arr[2][2][2].y, 1.0e-12,
478 "linear verification diffusivity should keep y gradient zero"));
479 PetscCall(PicurvAssertRealNear(0.0, grad_arr[2][2][2].z, 1.0e-12,
480 "linear verification diffusivity should keep z gradient zero"));
481 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->DiffusivityGradient, &grad_arr));
482
483 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
484 PetscFunctionReturn(0);
485}
VerificationFieldConfig verificationDiffusivity
Definition variables.h:996
char eulerianSource[PETSC_MAX_PATH_LEN]
Definition variables.h:938
PetscInt IM
Definition variables.h:1171
PetscErrorCode VerificationFieldCompile(const char *text, VerificationFieldConfig *field)
Compiles a verification field's expression, in physical x, y, z and t, and enables the field.
PetscBool VerificationDiffusivityOverrideActive(const SimCtx *simCtx)
Reports whether a verification-only diffusivity override is active.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestDiffusivityGradientFillsPeriodicDummyCells()

static PetscErrorCode TestDiffusivityGradientFillsPeriodicDummyCells ( void  )
static

Tests that the diffusivity gradient fills its periodic dummy cells.

A particle within half a cell of a periodic seam interpolates across it, so the dummy cells on each side must hold the gradient of the cell they image.

Definition at line 491 of file test_poisson_rhs.c.

492{
493 SimCtx *simCtx = NULL;
494 UserCtx *user = NULL;
495 PetscReal ***diff = NULL;
496 Cmpnts ***grad = NULL;
497 const FieldId fields[] = {FIELD_ID_DIFFUSIVITY};
498
499 PetscFunctionBeginUser;
500 PetscCall(PicurvCreateMinimalContextsWithPeriodicity(&simCtx, &user, 8, 6, 6, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE));
503 const DMDALocalInfo info = user->info;
504 PetscCall(DMDAVecGetArray(user->da, user->Diffusivity, &diff));
505 for (PetscInt k = info.zs; k < info.zs + info.zm; ++k)
506 for (PetscInt j = info.ys; j < info.ys + info.ym; ++j)
507 for (PetscInt i = info.xs; i < info.xs + info.xm; ++i) diff[k][j][i] = 1.0 + 0.1 * (PetscReal)(i * i);
508 PetscCall(DMDAVecRestoreArray(user->da, user->Diffusivity, &diff));
509 PetscCall(SynchronizePeriodicCellFields(user, 1, fields));
510 PetscCall(UpdateLocalGhosts(user, FIELD_ID_DIFFUSIVITY));
511
512 PetscCall(ComputeEulerianDiffusivityGradient(user));
513
514 PetscCall(DMDAVecGetArrayRead(user->fda, user->DiffusivityGradient, &grad));
515 PetscCall(PicurvAssertBool((PetscBool)(PetscAbsReal(grad[2][2][1].x) > 1.0e-6), "the first cell should carry a gradient"));
516 PetscCall(PicurvAssertRealNear(grad[2][2][info.mx - 2].x, grad[2][2][0].x, 1.0e-12,
517 "the low dummy cell should image the last cell's gradient"));
518 PetscCall(PicurvAssertRealNear(grad[2][2][1].x, grad[2][2][info.mx - 1].x, 1.0e-12,
519 "the high dummy cell should image the first cell's gradient"));
520 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->DiffusivityGradient, &grad));
521
522 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
523 PetscFunctionReturn(0);
524}
PetscErrorCode SynchronizePeriodicCellFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes periodic endpoint cells for a list of cell-centered fields.
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_DIFFUSIVITY
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2593
PetscErrorCode PicurvCreateMinimalContextsWithPeriodicity(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz, PetscBool x_periodic, PetscBool y_periodic, PetscBool z_periodic)
Builds minimal SimCtx and UserCtx fixtures for C unit tests with configurable periodicity.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestAssemblePoissonOperatorPopulatesRows()

static PetscErrorCode TestAssemblePoissonOperatorPopulatesRows ( void  )
static

Tests that Poisson matrix assembly produces a populated operator on a tiny Cartesian grid.

Definition at line 528 of file test_poisson_rhs.c.

529{
530 SimCtx *simCtx = NULL;
531 UserCtx *user = NULL;
532 PetscInt rows = 0, cols = 0;
533 PetscInt ncols = 0;
534 const PetscInt *col_idx = NULL;
535 const PetscScalar *values = NULL;
536 PetscInt interior_row = 0;
537
538 PetscFunctionBeginUser;
539 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
540 PetscCall(VecSet(user->Nvert, 0.0));
541 PetscCall(DMGlobalToLocalBegin(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
542 PetscCall(DMGlobalToLocalEnd(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
543
544 PetscCall(AssemblePoissonOperator(user));
545 PetscCall(PicurvAssertBool((PetscBool)(user->A != NULL), "AssemblePoissonOperator should allocate the Poisson operator"));
546
547 PetscCall(MatGetSize(user->A, &rows, &cols));
548 PetscCall(PicurvAssertIntEqual(user->info.mx * user->info.my * user->info.mz, rows, "Poisson operator row count should match the DA node count"));
549 PetscCall(PicurvAssertIntEqual(rows, cols, "Poisson operator should be square"));
550
551 interior_row = (2 * user->info.my + 2) * user->info.mx + 2;
552 PetscCall(MatGetRow(user->A, interior_row, &ncols, &col_idx, &values));
553 PetscCall(PicurvAssertBool((PetscBool)(ncols > 1), "Interior Poisson row should couple to neighboring nodes"));
554 PetscCall(PicurvAssertBool((PetscBool)(values != NULL), "Interior Poisson row should expose non-null coefficients"));
555 PetscCall(MatRestoreRow(user->A, interior_row, &ncols, &col_idx, &values));
556
557 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
558 PetscFunctionReturn(0);
559}
PetscErrorCode AssemblePoissonOperator(UserCtx *user)
Assembles the pressure-correction operator on one multigrid level.
Definition poisson.c:244
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RandomizeMinimalFaceMetrics()

static PetscErrorCode RandomizeMinimalFaceMetrics ( UserCtx *  user)
static

Fills every face metric and Jacobian of the minimal fixture with reproducible random values, so each face gradient carries all of its cross terms.

Definition at line 564 of file test_poisson_rhs.c.

565{
566 Vec face_metrics[9] = {user->ICsi, user->IEta, user->IZet, user->JCsi, user->JEta,
567 user->JZet, user->KCsi, user->KEta, user->KZet};
568 Vec local_metrics[9] = {user->lICsi, user->lIEta, user->lIZet, user->lJCsi, user->lJEta,
569 user->lJZet, user->lKCsi, user->lKEta, user->lKZet};
570 Vec jacobians[4] = {user->Aj, user->IAj, user->JAj, user->KAj};
571 Vec local_jac[4] = {user->lAj, user->lIAj, user->lJAj, user->lKAj};
572 PetscRandom rnd;
573
574 PetscFunctionBeginUser;
575 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rnd));
576 PetscCall(PetscRandomSetInterval(rnd, 0.5, 1.5));
577 PetscCall(PetscRandomSetSeed(rnd, 2026));
578 PetscCall(PetscRandomSeed(rnd));
579 for (PetscInt v = 0; v < 9; ++v) {
580 PetscCall(VecSetRandom(face_metrics[v], rnd));
581 PetscCall(DMGlobalToLocalBegin(user->fda, face_metrics[v], INSERT_VALUES, local_metrics[v]));
582 PetscCall(DMGlobalToLocalEnd(user->fda, face_metrics[v], INSERT_VALUES, local_metrics[v]));
583 }
584 for (PetscInt v = 0; v < 4; ++v) {
585 PetscCall(VecSetRandom(jacobians[v], rnd));
586 PetscCall(DMGlobalToLocalBegin(user->da, jacobians[v], INSERT_VALUES, local_jac[v]));
587 PetscCall(DMGlobalToLocalEnd(user->da, jacobians[v], INSERT_VALUES, local_jac[v]));
588 }
589 PetscCall(PetscRandomDestroy(&rnd));
590 PetscFunctionReturn(0);
591}
Here is the caller graph for this function:

◆ TestOperatorAndProjectionShareOneFaceGradient()

static PetscErrorCode TestOperatorAndProjectionShareOneFaceGradient ( void  )
static

Tests that the operator is exactly the divergence of the projection's gradient.

With random non-orthogonal metrics and one solid cell, projecting a zero flux with a random Phi and forming the right-hand side of the result must reproduce -A Phi on every fluid row. The identity holds for any metric values only if the operator and the projection use the same face gradient, including its one-sided forms beside walls and solid cells.

Definition at line 601 of file test_poisson_rhs.c.

602{
603 SimCtx *simCtx = NULL;
604 UserCtx *user = NULL;
605 Vec B = NULL, APhi = NULL;
606 PetscRandom rnd;
607 PetscReal ***nvert;
608 const PetscReal ***b, ***aphi;
609 PetscReal worst = 0.0;
610
611 PetscFunctionBeginUser;
612 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 7, 7, 7));
613 PetscCall(EnsurePoissonAndRhsVectors(user));
614 simCtx->dt = 0.3;
615 PetscCall(RandomizeMinimalFaceMetrics(user));
616
617 PetscCall(VecSet(user->Nvert, 0.0));
618 PetscCall(DMDAVecGetArray(user->da, user->Nvert, &nvert));
619 if (user->info.xs <= 3 && 3 < user->info.xs + user->info.xm &&
620 user->info.ys <= 3 && 3 < user->info.ys + user->info.ym &&
621 user->info.zs <= 3 && 3 < user->info.zs + user->info.zm) nvert[3][3][3] = 1.0;
622 PetscCall(DMDAVecRestoreArray(user->da, user->Nvert, &nvert));
623 PetscCall(DMGlobalToLocalBegin(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
624 PetscCall(DMGlobalToLocalEnd(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
625
626 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rnd));
627 PetscCall(PetscRandomSetSeed(rnd, 7));
628 PetscCall(PetscRandomSeed(rnd));
629 PetscCall(VecSetRandom(user->Phi, rnd));
630 PetscCall(PetscRandomDestroy(&rnd));
631 PetscCall(DMGlobalToLocalBegin(user->da, user->Phi, INSERT_VALUES, user->lPhi));
632 PetscCall(DMGlobalToLocalEnd(user->da, user->Phi, INSERT_VALUES, user->lPhi));
633 PetscCall(VecSet(user->Ucont, 0.0));
634 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
635 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
636
637 PetscCall(AssemblePoissonOperator(user));
638 PetscCall(ProjectVelocity(user));
639 PetscCall(VecDuplicate(user->P, &B));
640 PetscCall(ComputePoissonRHS(user, B));
641 PetscCall(VecDuplicate(user->Phi, &APhi));
642 PetscCall(MatMult(user->A, user->Phi, APhi));
643
644 PetscCall(DMDAVecGetArrayRead(user->da, B, &b));
645 PetscCall(DMDAVecGetArrayRead(user->da, APhi, &aphi));
646 PetscCall(DMDAVecGetArray(user->da, user->Nvert, &nvert));
647 for (PetscInt k = PetscMax(user->info.zs, 1); k < PetscMin(user->info.zs + user->info.zm, user->info.mz - 1); k++) {
648 for (PetscInt j = PetscMax(user->info.ys, 1); j < PetscMin(user->info.ys + user->info.ym, user->info.my - 1); j++) {
649 for (PetscInt i = PetscMax(user->info.xs, 1); i < PetscMin(user->info.xs + user->info.xm, user->info.mx - 1); i++) {
650 if (nvert[k][j][i] > 0.1) continue;
651 worst = PetscMax(worst, PetscAbsReal(b[k][j][i] + aphi[k][j][i]) / (1.0 + PetscAbsReal(aphi[k][j][i])));
652 }
653 }
654 }
655 PetscCall(DMDAVecRestoreArray(user->da, user->Nvert, &nvert));
656 PetscCall(DMDAVecRestoreArrayRead(user->da, APhi, &aphi));
657 PetscCall(DMDAVecRestoreArrayRead(user->da, B, &b));
658 PetscCall(PicurvAssertRealNear(0.0, worst, 1.0e-12,
659 "the right-hand side of the projected flux should equal -A Phi on every fluid row"));
660
661 PetscCall(VecDestroy(&APhi));
662 PetscCall(VecDestroy(&B));
663 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
664 PetscFunctionReturn(0);
665}
PetscErrorCode ProjectVelocity(UserCtx *user)
Corrects the contravariant flux with the gradient of Phi.
Definition poisson.c:425
static PetscErrorCode RandomizeMinimalFaceMetrics(UserCtx *user)
Fills every face metric and Jacobian of the minimal fixture with reproducible random values,...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestProjectionZeroPhiLeavesVelocityUnchanged()

static PetscErrorCode TestProjectionZeroPhiLeavesVelocityUnchanged ( void  )
static

Tests that projection leaves a zero pressure-correction field unchanged.

Definition at line 670 of file test_poisson_rhs.c.

671{
672 SimCtx *simCtx = NULL;
673 UserCtx *user = NULL;
674
675 PetscFunctionBeginUser;
676 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
677 PetscCall(VecSet(user->Ucont, 0.0));
678 PetscCall(VecSet(user->Phi, 0.0));
679 PetscCall(VecSet(user->Nvert, 0.0));
680 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
681 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
682 PetscCall(DMGlobalToLocalBegin(user->da, user->Phi, INSERT_VALUES, user->lPhi));
683 PetscCall(DMGlobalToLocalEnd(user->da, user->Phi, INSERT_VALUES, user->lPhi));
684 PetscCall(DMGlobalToLocalBegin(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
685 PetscCall(DMGlobalToLocalEnd(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
686
687 PetscCall(ProjectVelocity(user));
688 PetscCall(PicurvAssertVecConstant(user->Ucont, 0.0, 1.0e-12, "ProjectVelocity should leave a zero-velocity field unchanged when Phi is zero"));
689
690 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
691 PetscFunctionReturn(0);
692}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestProjectionLinearPhiCorrectsVelocity()

static PetscErrorCode TestProjectionLinearPhiCorrectsVelocity ( void  )
static

Tests that projection applies the expected x-direction correction for a linear pressure field.

Definition at line 696 of file test_poisson_rhs.c.

697{
698 SimCtx *simCtx = NULL;
699 UserCtx *user = NULL;
700 PetscReal ***phi = NULL;
701 Cmpnts ***ucont = NULL;
702
703 PetscFunctionBeginUser;
704 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
705 simCtx->dt = COEF_TIME_ACCURACY;
706 PetscCall(VecSet(user->Ucont, 0.0));
707 PetscCall(VecSet(user->Nvert, 0.0));
708 PetscCall(DMDAVecGetArray(user->da, user->Phi, &phi));
709 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k) {
710 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j) {
711 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i) {
712 phi[k][j][i] = (PetscReal)i;
713 }
714 }
715 }
716 PetscCall(DMDAVecRestoreArray(user->da, user->Phi, &phi));
717 PetscCall(DMGlobalToLocalBegin(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
718 PetscCall(DMGlobalToLocalEnd(user->fda, user->Ucont, INSERT_VALUES, user->lUcont));
719 PetscCall(DMGlobalToLocalBegin(user->da, user->Phi, INSERT_VALUES, user->lPhi));
720 PetscCall(DMGlobalToLocalEnd(user->da, user->Phi, INSERT_VALUES, user->lPhi));
721 PetscCall(DMGlobalToLocalBegin(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
722 PetscCall(DMGlobalToLocalEnd(user->da, user->Nvert, INSERT_VALUES, user->lNvert));
723
724 PetscCall(ProjectVelocity(user));
725
726 PetscCall(DMDAVecGetArrayRead(user->fda, user->Ucont, &ucont));
727 PetscCall(PicurvAssertRealNear(-1.0, ucont[2][2][2].x, 1.0e-10, "ProjectVelocity should subtract the x pressure gradient under identity metrics"));
728 PetscCall(PicurvAssertRealNear(0.0, ucont[2][2][2].y, 1.0e-10, "ProjectVelocity should leave the y component unchanged for an x-only gradient"));
729 PetscCall(PicurvAssertRealNear(0.0, ucont[2][2][2].z, 1.0e-10, "ProjectVelocity should leave the z component unchanged for an x-only gradient"));
730 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->Ucont, &ucont));
731
732 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
733 PetscFunctionReturn(0);
734}
#define COEF_TIME_ACCURACY
Coefficient controlling the temporal accuracy scheme (e.g., 1.5 for 2nd Order Backward Difference).
Definition variables.h:96
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PerturbInteriorFaceFluxes()

static PetscErrorCode PerturbInteriorFaceFluxes ( UserCtx *  user,
PetscReal  amplitude 
)
static

Adds a smooth divergent perturbation to every interior face flux.

Boundary faces are left alone, so the inflow still balances the outflow and the Neumann pressure problem stays solvable.

Definition at line 740 of file test_poisson_rhs.c.

741{
742 DMDALocalInfo info = user->info;
743 Cmpnts ***ucont;
744
745 PetscFunctionBeginUser;
746 PetscCall(DMDAVecGetArray(user->fda, user->Ucont, &ucont));
747 for (PetscInt k = PetscMax(info.zs, 1); k < PetscMin(info.zs + info.zm, info.mz - 1); k++) {
748 for (PetscInt j = PetscMax(info.ys, 1); j < PetscMin(info.ys + info.ym, info.my - 1); j++) {
749 for (PetscInt i = PetscMax(info.xs, 1); i < PetscMin(info.xs + info.xm, info.mx - 1); i++) {
750 if (i <= info.mx - 3) ucont[k][j][i].x += amplitude * PetscSinReal(1.3 * i + 0.7 * j + 0.4 * k);
751 if (j <= info.my - 3) ucont[k][j][i].y += amplitude * PetscCosReal(0.9 * i + 1.1 * j + 0.3 * k);
752 if (k <= info.mz - 3) ucont[k][j][i].z += amplitude * PetscSinReal(0.5 * i + 0.8 * j + 1.7 * k);
753 }
754 }
755 }
756 PetscCall(DMDAVecRestoreArray(user->fda, user->Ucont, &ucont));
757 PetscCall(UpdateLocalGhosts(user, FIELD_ID_UCONT));
758 PetscFunctionReturn(0);
759}
@ FIELD_ID_UCONT
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPoissonSolverMultigridProjectsToDivergenceFree()

static PetscErrorCode TestPoissonSolverMultigridProjectsToDivergenceFree ( void  )
static

Tests that the production multigrid Poisson solve and projection remove a divergence.

Runs PoissonSolver_Multigrid through the real setup path with a three-level hierarchy, then the same UpdatePressure/ProjectVelocity pair the time loop uses, and requires the projected field to be divergence-free to the solve tolerance.

Definition at line 767 of file test_poisson_rhs.c.

768{
769 SimCtx *simCtx = NULL;
770 UserCtx *user = NULL;
771 char tmpdir[PETSC_MAX_PATH_LEN];
772 PetscReal before;
773
774 PetscFunctionBeginUser;
776 NULL, PETSC_FALSE,
777 "-im 17\n-jm 17\n-km 17\n-mg_level 3\n"
778 "-ps_ksp_rtol 1.0e-12\n-ps_ksp_atol 1.0e-14\n-ps_ksp_max_it 200\n",
779 &simCtx, &user, tmpdir, sizeof(tmpdir)));
780 PetscCall(PicurvAssertIntEqual(3, simCtx->usermg.mglevels, "fixture should build a three-level hierarchy"));
781
782 PetscCall(PerturbInteriorFaceFluxes(user, 0.2));
783 PetscCall(ComputeDivergence(user));
784 before = simCtx->MaxDiv;
785 PetscCall(PicurvAssertBool((PetscBool)(before > 1.0e-2), "perturbation should make the field divergent"));
786
787 PetscCall(PoissonSolver_Multigrid(&simCtx->usermg));
788 PetscCall(UpdatePressure(user));
789 PetscCall(ProjectVelocity(user));
790 PetscCall(UpdateLocalGhosts(user, FIELD_ID_UCONT));
791 PetscCall(ComputeDivergence(user));
792 PetscCall(PicurvAssertBool((PetscBool)(simCtx->MaxDiv < 1.0e-9 * before),
793 "multigrid solve plus projection should leave no divergence above the solve tolerance"));
794
795 PetscCall(PicurvDestroyRuntimeContext(&simCtx));
796 PetscCall(PicurvRemoveTempDir(tmpdir));
797 PetscFunctionReturn(0);
798}
PetscErrorCode PoissonSolver_Multigrid(UserMG *usermg)
Solves the pressure-correction equation for every block with geometric multigrid.
Definition poisson.c:922
PetscErrorCode ComputeDivergence(UserCtx *user)
Computes the discrete divergence of the contravariant velocity field.
Definition setup.c:3599
static PetscErrorCode PerturbInteriorFaceFluxes(UserCtx *user, PetscReal amplitude)
Adds a smooth divergent perturbation to every interior face flux.
PetscErrorCode PicurvDestroyRuntimeContext(SimCtx **simCtx_ptr)
Finalizes and frees a runtime context built by PicurvBuildTinyRuntimeContext.
PetscErrorCode PicurvBuildTinyRuntimeContextWithOptions(const char *bcs_contents, PetscBool enable_particles, const char *extra_options, SimCtx **simCtx_out, UserCtx **user_out, char *tmpdir, size_t tmpdir_len)
Builds the tiny runtime context with extra control-file options appended.
PetscErrorCode PicurvRemoveTempDir(const char *path)
Recursively removes a temporary directory created by PicurvMakeTempDir.
UserMG usermg
Definition variables.h:1082
PetscReal MaxDiv
Definition variables.h:1096
PetscInt mglevels
Definition variables.h:775
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPoissonSolverMultigridKeepsThePressureLevel()

static PetscErrorCode TestPoissonSolverMultigridKeepsThePressureLevel ( void  )
static

Tests that the multigrid solve returns Phi without a constant, so P keeps its level.

The residual cannot see a constant in Phi, so nothing in the Krylov iteration removes one; near a steady state the iterations at the round-off floor leave one, and P += Phi adds it up. A constant in the initial guess stands in for it here: the returned Phi must still have zero mean over the interior.

Definition at line 807 of file test_poisson_rhs.c.

808{
809 SimCtx *simCtx = NULL;
810 UserCtx *user = NULL;
811 char tmpdir[PETSC_MAX_PATH_LEN];
812 PetscReal ***phi, local[2] = {0.0, 0.0}, peak;
813
814 PetscFunctionBeginUser;
816 NULL, PETSC_FALSE, "-im 17\n-jm 17\n-km 17\n-mg_level 3\n", &simCtx, &user, tmpdir, sizeof(tmpdir)));
817 PetscCall(PoissonSolver_Multigrid(&simCtx->usermg));
818 PetscCall(PerturbInteriorFaceFluxes(user, 0.2));
819 PetscCall(KSPSetInitialGuessNonzero(user->ksp, PETSC_TRUE));
820 PetscCall(VecSet(user->Phi, 5.0));
821 PetscCall(PoissonSolver_Multigrid(&simCtx->usermg));
822
823 const DMDALocalInfo info = user->info;
824 PetscCall(DMDAVecGetArrayRead(user->da, user->Phi, (void *)&phi));
825 for (PetscInt k = PetscMax(info.zs, 1); k < PetscMin(info.zs + info.zm, info.mz - 1); k++)
826 for (PetscInt j = PetscMax(info.ys, 1); j < PetscMin(info.ys + info.ym, info.my - 1); j++)
827 for (PetscInt i = PetscMax(info.xs, 1); i < PetscMin(info.xs + info.xm, info.mx - 1); i++) {
828 local[0] += phi[k][j][i];
829 local[1] += 1.0;
830 }
831 PetscCall(DMDAVecRestoreArrayRead(user->da, user->Phi, (void *)&phi));
832 PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, local, 2, MPIU_REAL, MPI_SUM, PETSC_COMM_WORLD));
833 PetscCall(VecNorm(user->Phi, NORM_INFINITY, &peak));
834 PetscCall(PicurvAssertBool((PetscBool)(peak > 1.0e-6), "the perturbation should need a correction"));
835 PetscCall(PicurvAssertRealNear(0.0, local[0] / local[1], 1.0e-12 * peak,
836 "Phi should carry no constant over the interior"));
837
838 PetscCall(PicurvDestroyRuntimeContext(&simCtx));
839 PetscCall(PicurvRemoveTempDir(tmpdir));
840 PetscFunctionReturn(0);
841}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CountFileOccurrences()

static PetscErrorCode CountFileOccurrences ( const char *  path,
const char *  needle,
PetscInt *  count 
)
static

Counts the occurrences of needle in a text file; a missing file counts zero.

Definition at line 846 of file test_poisson_rhs.c.

847{
848 FILE *file;
849 char line[1024];
850
851 PetscFunctionBeginUser;
852 *count = 0;
853 file = fopen(path, "r");
854 if (!file) PetscFunctionReturn(0);
855 while (fgets(line, sizeof(line), file)) {
856 if (strstr(line, needle)) (*count)++;
857 }
858 fclose(file);
859 PetscFunctionReturn(0);
860}
Here is the caller graph for this function:

◆ TestPoissonSolverMultigridReusesItsSolver()

static PetscErrorCode TestPoissonSolverMultigridReusesItsSolver ( void  )
static

Tests that the multigrid solver is built once and reused on later steps.

Two consecutive solves must use the same Krylov solver and operator, both must converge, and the convergence log must hold one header per step.

Definition at line 867 of file test_poisson_rhs.c.

868{
869 SimCtx *simCtx = NULL;
870 UserCtx *user = NULL;
871 char tmpdir[PETSC_MAX_PATH_LEN], log_path[PETSC_MAX_PATH_LEN + 64];
872 KSP first_ksp;
873 Mat first_operator;
874 KSPConvergedReason reason;
875 PetscInt headers = 0;
876
877 PetscFunctionBeginUser;
879 NULL, PETSC_FALSE,
880 "-im 17\n-jm 17\n-km 17\n-mg_level 3\n"
881 "-ps_ksp_rtol 1.0e-12\n-ps_ksp_atol 1.0e-14\n-ps_ksp_max_it 200\n",
882 &simCtx, &user, tmpdir, sizeof(tmpdir)));
883
884 simCtx->step = simCtx->StartStep + 1;
885 PetscCall(PerturbInteriorFaceFluxes(user, 0.2));
886 PetscCall(PoissonSolver_Multigrid(&simCtx->usermg));
887 first_ksp = user->ksp;
888 first_operator = user->A;
889 PetscCall(PicurvAssertBool((PetscBool)(first_ksp != NULL), "the first solve should build and keep the solver"));
890 PetscCall(KSPGetConvergedReason(user->ksp, &reason));
891 PetscCall(PicurvAssertBool((PetscBool)(reason > 0), "the first solve should converge"));
892
893 simCtx->step++;
894 PetscCall(PerturbInteriorFaceFluxes(user, 0.1));
895 PetscCall(PoissonSolver_Multigrid(&simCtx->usermg));
896 PetscCall(PicurvAssertBool((PetscBool)(user->ksp == first_ksp), "the second solve should reuse the solver"));
897 PetscCall(PicurvAssertBool((PetscBool)(user->A == first_operator), "the second solve should reuse the operator"));
898 PetscCall(KSPGetConvergedReason(user->ksp, &reason));
899 PetscCall(PicurvAssertBool((PetscBool)(reason > 0), "the reused solver should converge"));
900
901 PetscCall(PetscSNPrintf(log_path, sizeof(log_path), "%s/Poisson_Solver_Convergence_History_Block_0.csv", simCtx->log_dir));
902 PetscCall(CountFileOccurrences(log_path, "step,block,iter,", &headers));
903 PetscCall(PicurvAssertIntEqual(1, headers, "the convergence CSV should carry its header once"));
904 PetscCall(CountFileOccurrences(log_path, ",0,0,", &headers));
905 PetscCall(PicurvAssertIntEqual(2, headers, "the convergence CSV should hold an iteration-0 row per solve"));
906
907 PetscCall(PicurvDestroyRuntimeContext(&simCtx));
908 PetscCall(PicurvRemoveTempDir(tmpdir));
909 PetscFunctionReturn(0);
910}
static PetscErrorCode CountFileOccurrences(const char *path, const char *needle, PetscInt *count)
Counts the occurrences of needle in a text file; a missing file counts zero.
PetscInt StartStep
Definition variables.h:926
char log_dir[PETSC_MAX_PATH_LEN]
Definition variables.h:941
PetscInt step
Definition variables.h:924
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPoissonSolverMultigridHonoursDistinctSweeps()

static PetscErrorCode TestPoissonSolverMultigridHonoursDistinctSweeps ( void  )
static

Tests that unequal pre- and post-smoothing counts give two distinct smoothers.

With pre_sweeps 1 and post_sweeps 3 every smoothed level must run one sweep before the coarse correction and three after it, the post-smoother must keep the configured smoother type, and the solve must still remove the divergence. With equal counts the shared smoother runs the configured sweeps.

Definition at line 919 of file test_poisson_rhs.c.

920{
921 const char *const configurations[2] = {
922 "-im 17\n-jm 17\n-km 17\n-mg_level 3\n-mg_pre_it 1\n-mg_post_it 3\n"
923 "-ps_mg_levels_1_ksp_type richardson\n-ps_mg_levels_2_ksp_type richardson\n"
924 "-ps_ksp_rtol 1.0e-12\n-ps_ksp_atol 1.0e-14\n-ps_ksp_max_it 200\n",
925 "-im 17\n-jm 17\n-km 17\n-mg_level 3\n-mg_pre_it 2\n-mg_post_it 2\n",
926 };
927
928 PetscFunctionBeginUser;
929 for (PetscInt c = 0; c < 2; c++) {
930 SimCtx *simCtx = NULL;
931 UserCtx *user = NULL;
932 char tmpdir[PETSC_MAX_PATH_LEN];
933 PC pc;
934 PetscReal before;
935
936 PetscCall(PicurvBuildTinyRuntimeContextWithOptions(NULL, PETSC_FALSE, configurations[c],
937 &simCtx, &user, tmpdir, sizeof(tmpdir)));
938 PetscCall(PerturbInteriorFaceFluxes(user, 0.2));
939 PetscCall(ComputeDivergence(user));
940 before = simCtx->MaxDiv;
941 PetscCall(PoissonSolver_Multigrid(&simCtx->usermg));
942 PetscCall(KSPGetPC(user->ksp, &pc));
943
944 for (PetscInt l = 1; l < simCtx->usermg.mglevels; l++) {
945 KSP pre_smoother, post_smoother;
946 PetscInt pre_its, post_its;
947 PetscBool is_richardson;
948
949 PetscCall(PCMGGetSmootherDown(pc, l, &pre_smoother));
950 PetscCall(KSPGetTolerances(pre_smoother, NULL, NULL, NULL, &pre_its));
951 if (c == 0) {
952 PetscCall(PCMGGetSmootherUp(pc, l, &post_smoother));
953 PetscCall(KSPGetTolerances(post_smoother, NULL, NULL, NULL, &post_its));
954 PetscCall(PicurvAssertBool((PetscBool)(pre_smoother != post_smoother), "unequal sweeps should give a separate post-smoother"));
955 PetscCall(PicurvAssertIntEqual(1, pre_its, "pre-smoothing should run pre_sweeps iterations"));
956 PetscCall(PicurvAssertIntEqual(3, post_its, "post-smoothing should run post_sweeps iterations"));
957 PetscCall(PetscObjectTypeCompare((PetscObject)post_smoother, KSPRICHARDSON, &is_richardson));
958 PetscCall(PicurvAssertBool(is_richardson, "the post-smoother should keep the configured smoother type"));
959 } else {
960 /* PCMGGetSmootherUp() would itself split a shared smoother, so only the
961 shared one is inspected here. */
962 PetscCall(PicurvAssertIntEqual(2, pre_its, "the shared smoother should run the configured sweeps"));
963 }
964 }
965
966 if (c == 0) {
967 PetscCall(UpdatePressure(user));
968 PetscCall(ProjectVelocity(user));
969 PetscCall(UpdateLocalGhosts(user, FIELD_ID_UCONT));
970 PetscCall(ComputeDivergence(user));
971 PetscCall(PicurvAssertBool((PetscBool)(simCtx->MaxDiv < 1.0e-9 * before),
972 "the solve with distinct sweeps should still remove the divergence"));
973 }
974 PetscCall(PicurvDestroyRuntimeContext(&simCtx));
975 PetscCall(PicurvRemoveTempDir(tmpdir));
976 }
977 PetscFunctionReturn(0);
978}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPoissonNullSpaceRemovesTheInteriorMean()

static PetscErrorCode TestPoissonNullSpaceRemovesTheInteriorMean ( void  )
static

Tests the null space attached to the operator.

Removing it from a constant must leave zero everywhere, and from a linear field must leave zero mean over the interior with zero dummy layers.

Definition at line 985 of file test_poisson_rhs.c.

986{
987 SimCtx *simCtx = NULL;
988 UserCtx *user = NULL;
989 char tmpdir[PETSC_MAX_PATH_LEN];
990 MatNullSpace nullsp = NULL;
991 Vec x = NULL;
992 PetscReal ***xa, norm, sum;
993
994 PetscFunctionBeginUser;
996 NULL, PETSC_FALSE, "-im 9\n-jm 9\n-km 9\n-mg_level 2\n", &simCtx, &user, tmpdir, sizeof(tmpdir)));
997 PetscCall(PoissonSolver_Multigrid(&simCtx->usermg));
998 PetscCall(MatGetNullSpace(user->A, &nullsp));
999 PetscCall(PicurvAssertBool((PetscBool)(nullsp != NULL), "the operator should carry the Neumann null space"));
1000
1001 PetscCall(VecDuplicate(user->Phi, &x));
1002 PetscCall(VecSet(x, 3.0));
1003 PetscCall(MatNullSpaceRemove(nullsp, x));
1004 PetscCall(VecNorm(x, NORM_INFINITY, &norm));
1005 PetscCall(PicurvAssertRealNear(0.0, norm, 1.0e-12, "removing the null space should annihilate a constant"));
1006
1007 PetscCall(DMDAVecGetArray(user->da, x, &xa));
1008 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; k++)
1009 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; j++)
1010 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; i++) xa[k][j][i] = (PetscReal)i;
1011 PetscCall(DMDAVecRestoreArray(user->da, x, &xa));
1012 PetscCall(MatNullSpaceRemove(nullsp, x));
1013 PetscCall(VecSum(x, &sum));
1014 PetscCall(PicurvAssertRealNear(0.0, sum, 1.0e-10, "the interior mean should vanish"));
1015 PetscCall(DMDAVecGetArray(user->da, x, &xa));
1016 if (user->info.xs == 0) PetscCall(PicurvAssertRealNear(0.0, xa[user->info.zs][user->info.ys][0], 1.0e-14, "dummy layers should be zero"));
1017 PetscCall(DMDAVecRestoreArray(user->da, x, &xa));
1018
1019 PetscCall(VecDestroy(&x));
1020 PetscCall(PicurvDestroyRuntimeContext(&simCtx));
1021 PetscCall(PicurvRemoveTempDir(tmpdir));
1022 PetscFunctionReturn(0);
1023}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPoissonSolverMultigridRefusesAnOvercoarsenedHierarchy()

static PetscErrorCode TestPoissonSolverMultigridRefusesAnOvercoarsenedHierarchy ( void  )
static

Tests that an over-coarsened hierarchy stops the run at the Poisson solve.

Three levels on a 9-node grid leave a coarse operator the preconditioner cannot factor. The solve must return PETSC_ERR_NOT_CONVERGED rather than hand an unsolved Phi to the projection.

Definition at line 1031 of file test_poisson_rhs.c.

1032{
1033 SimCtx *simCtx = NULL;
1034 UserCtx *user = NULL;
1035 char tmpdir[PETSC_MAX_PATH_LEN];
1036 PetscErrorCode solve_error;
1037
1038 PetscFunctionBeginUser;
1040 NULL, PETSC_FALSE,
1041 "-im 9\n-jm 9\n-km 9\n-mg_level 3\n"
1042 /* The shipped profiles solve the coarse level with a replicated direct factor. */
1043 "-ps_ksp_type fgmres\n-ps_pc_type mg\n"
1044 "-ps_mg_coarse_ksp_type preonly\n-ps_mg_coarse_pc_type redundant\n",
1045 &simCtx, &user, tmpdir, sizeof(tmpdir)));
1046 PetscCall(PerturbInteriorFaceFluxes(user, 0.2));
1047
1048 PetscCall(PetscPushErrorHandler(PetscReturnErrorHandler, NULL));
1049 solve_error = PoissonSolver_Multigrid(&simCtx->usermg);
1050 PetscCall(PetscPopErrorHandler());
1051 PetscCall(PicurvAssertIntEqual(PETSC_ERR_NOT_CONVERGED, (PetscInt)solve_error,
1052 "an unfactorable coarse level should stop the solve, not reach the projection"));
1053
1054 PetscCall(PicurvDestroyRuntimeContext(&simCtx));
1055 PetscCall(PicurvRemoveTempDir(tmpdir));
1056 PetscFunctionReturn(0);
1057}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Runs the unit-poisson-rhs PETSc test binary.

Definition at line 1062 of file test_poisson_rhs.c.

1063{
1064 PetscErrorCode ierr;
1065 const PicurvTestCase cases[] = {
1066 {"update-pressure-adds-phi", TestUpdatePressureAddsPhi},
1067 {"poisson-rhs-zero-divergence", TestPoissonRHSZeroDivergence},
1068 {"compute-body-forces-dispatcher", TestComputeBodyForcesDispatcher},
1069 {"compute-eulerian-diffusivity-molecular-only", TestComputeEulerianDiffusivityMolecularOnly},
1070 {"convection-zero-field", TestConvectionZeroField},
1071 {"viscous-uniform-field", TestViscousUniformField},
1072 {"clark-gradient-model-scales-with-filter-width-squared", TestClarkGradientModelScalesWithFilterWidthSquared},
1073 {"compute-rhs-zero-field-no-forcing", TestComputeRHSZeroFieldNoForcing},
1074 {"viscous-wall-modelled-face-carries-the-stored-stress", TestViscousWallModelledFaceCarriesTheStoredStress},
1075 {"compute-eulerian-diffusivity-gradient-constant-field", TestComputeEulerianDiffusivityGradientConstantField},
1076 {"compute-eulerian-diffusivity-verification-linear-x", TestComputeEulerianDiffusivityVerificationLinearX},
1077 {"diffusivity-gradient-fills-periodic-dummy-cells", TestDiffusivityGradientFillsPeriodicDummyCells},
1078 {"assemble-poisson-operator-populates-rows", TestAssemblePoissonOperatorPopulatesRows},
1079 {"operator-and-projection-share-one-face-gradient", TestOperatorAndProjectionShareOneFaceGradient},
1080 {"projection-zero-phi-leaves-velocity-unchanged", TestProjectionZeroPhiLeavesVelocityUnchanged},
1081 {"projection-linear-phi-corrects-velocity", TestProjectionLinearPhiCorrectsVelocity},
1082 {"poisson-solver-multigrid-projects-to-divergence-free", TestPoissonSolverMultigridProjectsToDivergenceFree},
1083 {"poisson-solver-multigrid-keeps-the-pressure-level", TestPoissonSolverMultigridKeepsThePressureLevel},
1084 {"poisson-solver-multigrid-reuses-its-solver", TestPoissonSolverMultigridReusesItsSolver},
1085 {"poisson-solver-multigrid-honours-distinct-sweeps", TestPoissonSolverMultigridHonoursDistinctSweeps},
1086 {"poisson-null-space-removes-the-interior-mean", TestPoissonNullSpaceRemovesTheInteriorMean},
1087 {"poisson-solver-multigrid-refuses-an-overcoarsened-hierarchy", TestPoissonSolverMultigridRefusesAnOvercoarsenedHierarchy},
1088 };
1089
1090 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv Poisson/RHS tests");
1091 if (ierr) {
1092 return (int)ierr;
1093 }
1094
1095 ierr = PicurvRunTests("unit-poisson-rhs", cases, sizeof(cases) / sizeof(cases[0]));
1096 if (ierr) {
1097 PetscFinalize();
1098 return (int)ierr;
1099 }
1100
1101 ierr = PetscFinalize();
1102 return (int)ierr;
1103}
static PetscErrorCode TestPoissonSolverMultigridReusesItsSolver(void)
Tests that the multigrid solver is built once and reused on later steps.
static PetscErrorCode TestClarkGradientModelScalesWithFilterWidthSquared(void)
Tests that the Clark gradient model scales as the square of the filter width.
static PetscErrorCode TestComputeEulerianDiffusivityMolecularOnly(void)
Tests Eulerian diffusivity for the molecular-only configuration.
static PetscErrorCode TestComputeRHSZeroFieldNoForcing(void)
Tests that the full RHS remains zero without forcing on a quiescent field.
static PetscErrorCode TestViscousUniformField(void)
Tests that viscous terms vanish for a uniform field.
static PetscErrorCode TestConvectionZeroField(void)
Tests that convection vanishes for a quiescent field.
static PetscErrorCode TestComputeBodyForcesDispatcher(void)
Tests the body-force dispatcher across supported source terms.
static PetscErrorCode TestUpdatePressureAddsPhi(void)
Tests that pressure updates add the correction potential.
static PetscErrorCode TestProjectionZeroPhiLeavesVelocityUnchanged(void)
Tests that projection leaves a zero pressure-correction field unchanged.
static PetscErrorCode TestOperatorAndProjectionShareOneFaceGradient(void)
Tests that the operator is exactly the divergence of the projection's gradient.
static PetscErrorCode TestPoissonSolverMultigridRefusesAnOvercoarsenedHierarchy(void)
Tests that an over-coarsened hierarchy stops the run at the Poisson solve.
static PetscErrorCode TestPoissonSolverMultigridProjectsToDivergenceFree(void)
Tests that the production multigrid Poisson solve and projection remove a divergence.
static PetscErrorCode TestPoissonSolverMultigridHonoursDistinctSweeps(void)
Tests that unequal pre- and post-smoothing counts give two distinct smoothers.
static PetscErrorCode TestPoissonRHSZeroDivergence(void)
Tests that the Poisson RHS is zero for zero-divergence velocity.
static PetscErrorCode TestDiffusivityGradientFillsPeriodicDummyCells(void)
Tests that the diffusivity gradient fills its periodic dummy cells.
static PetscErrorCode TestComputeEulerianDiffusivityVerificationLinearX(void)
Tests verification-driven linear diffusivity override and its gradient.
static PetscErrorCode TestPoissonNullSpaceRemovesTheInteriorMean(void)
Tests the null space attached to the operator.
static PetscErrorCode TestAssemblePoissonOperatorPopulatesRows(void)
Tests that Poisson matrix assembly produces a populated operator on a tiny Cartesian grid.
static PetscErrorCode TestProjectionLinearPhiCorrectsVelocity(void)
Tests that projection applies the expected x-direction correction for a linear pressure field.
static PetscErrorCode TestPoissonSolverMultigridKeepsThePressureLevel(void)
Tests that the multigrid solve returns Phi without a constant, so P keeps its level.
static PetscErrorCode TestViscousWallModelledFaceCarriesTheStoredStress(void)
Tests that a wall-modelled face carries the stored wall stress and no gradient flux.
static PetscErrorCode TestComputeEulerianDiffusivityGradientConstantField(void)
Tests diffusivity-gradient computation on a constant field.
PetscErrorCode PicurvRunTests(const char *suite_name, const PicurvTestCase *cases, size_t case_count)
Runs a named C test suite and prints pass/fail progress markers.
Named test case descriptor consumed by PicurvRunTests.
Here is the call graph for this function: