#include "momentumsolvers.h"Go to the source code of this file.
Data Structures | |
| struct | MomentumNewtonJacobian |
| struct | MomentumPreconditionerDescription |
| struct | MomentumPreconditionerModelOps |
| struct | MomentumPreconditionerEngine |
| struct | MomentumNewtonKrylovContext |
Typedefs | |
| typedef struct MomentumNewtonKrylovContext | MomentumNewtonKrylovContext |
Functions | |
| static PetscErrorCode | MomentumNewtonKrylov_Validate (UserCtx *user) |
| Rejects configurations outside the audited version-one feature set. | |
| static PetscErrorCode | MomentumNewtonKrylov_FormResidual (SNES snes, Vec X, Vec F, void *vctx) |
| Adapts a PETSc trial vector to the existing momentum residual path. | |
| static PetscErrorCode | MomentumNewtonKrylov_Monitor (SNES snes, PetscInt iteration, PetscReal norm, void *vctx) |
| Captures SNES iteration norms and optionally writes PICurv history rows. | |
| static PetscErrorCode | MomentumNewtonKrylov_LinearMonitor (KSP ksp, PetscInt iteration, PetscReal norm, void *vctx) |
| Writes the effective KSP tolerance and PETSc-reported norm for each inner iteration. | |
| static void | MomentumNewtonKrylov_OpenHistory (MomentumNewtonKrylovContext *ctx) |
| Opens the optional rank-zero Newton iteration-history files (per Newton and per Krylov iteration). | |
| static void | MomentumNewtonKrylov_WriteSummary (const MomentumNewtonKrylovContext *ctx, SNESConvergedReason reason, PetscInt nonlinear_its, PetscInt function_evals, PetscInt linear_its, PetscReal final_norm, PetscBool committed) |
| Appends one rank-zero structured Newton result for a physical step. | |
| static PetscErrorCode | MomentumNewtonKrylov_ApplyConstraints (MomentumNewtonKrylovContext *ctx, Vec X, Vec F) |
| Replaces every non-independent residual row with an explicit equation. | |
| static PetscErrorCode | MomentumNewtonKrylov_ReadLinearizationConfig (MomentumNewtonJacobian *jacobian, MomentumPreconditionerDescription *description) |
| Reads application-owned Jacobian and preconditioner mathematics. | |
| static PetscErrorCode | MomentumNewtonKrylov_FormJacobian (SNES snes, Vec current_solution, Mat jacobian_operator, Mat preconditioning_matrix, void *vctx) |
| Updates the Jacobian and then assembles any separate preconditioning matrix. | |
| static PetscErrorCode | FrozenMomentumJacobian_AssemblePointBlocks (UserCtx *user, Vec current_solution, Mat preconditioning_matrix) |
| Inserts only the audited interior frozen-momentum point blocks. | |
| static PetscErrorCode | MomentumPreconditionerEngine_CreateExactPointBlockMatrix (UserCtx *user, Mat *preconditioning_matrix) |
| Creates the frozen point-block P matrix with its exact scalar pattern. | |
| static FILE * | MomentumNewtonKrylov_OpenCsv (const MomentumNewtonKrylovContext *ctx, const char *kind, const char *header) |
| Opens one of the block's Newton CSV records on rank 0, or returns NULL. | |
| static MomentumRowType | MomentumNewtonKrylov_ClassifyRow (UserCtx *user, const PetscReal ***nvert, PetscInt i, PetscInt j, PetscInt k, PetscInt component, PetscInt *ri, PetscInt *rj, PetscInt *rk) |
| Row classification with solid-cell masking folded in. | |
| static PetscReal | FrozenMomentumJacobian_MetricNormSquared (Cmpnts metric) |
| Returns the squared Euclidean norm of one metric vector. | |
| static PetscReal | FrozenMomentumJacobian_FaceEddyViscosity (const UserCtx *user, const PetscReal ***nu_t, PetscInt axis, PetscInt i, PetscInt j, PetscInt k) |
| Returns the face-averaged eddy viscosity the residual uses on one face. | |
| static void | FrozenMomentumJacobian_PointBlock (const UserCtx *user, const Cmpnts ***ucont, const Cmpnts ***csi, const Cmpnts ***eta, const Cmpnts ***zet, const PetscReal ***aj, const PetscReal ***nu_t, PetscInt i, PetscInt j, PetscInt k, PetscScalar block[9]) |
| Returns the frozen-momentum point block for the current residual convention. | |
| static PetscErrorCode | FrozenMomentumJacobian_DescribePointBlock (UserCtx *user, MomentumPreconditionerDescription *description) |
| Describes the audited frozen-coefficient point-block model. | |
| static PetscErrorCode | MomentumPreconditionerEngine_ApplyConstraintRows (UserCtx *user, Mat preconditioning_matrix) |
| Inserts all common fixed, homogeneous, and periodic-duplicate rows. | |
| static PetscErrorCode | MomentumNewtonJacobian_Create (SNES snes, MomentumNewtonJacobian *jacobian) |
| Creates the selected Jacobian operator; currently PETSc MFFD only. | |
| static PetscErrorCode | MomentumNewtonJacobian_Update (SNES snes, Vec current_solution, MomentumNewtonJacobian *jacobian) |
| Updates the matrix-free finite-difference operator base. | |
| static PetscErrorCode | MomentumNewtonJacobian_Register (SNES snes, MomentumNewtonJacobian *jacobian, MomentumPreconditionerEngine *engine, MomentumNewtonKrylovContext *ctx) |
| Registers the application orchestration callback and both SNES matrices. | |
| static PetscErrorCode | MomentumNewtonJacobian_Destroy (MomentumNewtonJacobian *jacobian) |
| Destroys a partially or fully created Jacobian operator. | |
| static PetscErrorCode | MomentumPreconditionerEngine_Create (UserCtx *user, Mat jacobian_operator, const MomentumPreconditionerDescription *requested, MomentumPreconditionerEngine *engine) |
| Validates a model/structure and creates or aliases its matrix. | |
| static PetscErrorCode | MomentumPreconditionerEngine_Assemble (MomentumPreconditionerEngine *engine, UserCtx *user, Vec current_solution) |
| Runs model insertion, common row handling, and final assembly. | |
| static PetscErrorCode | MomentumPreconditionerEngine_ConfigurePetscPC (MomentumPreconditionerEngine *engine, PC pc) |
| Applies the validated model/structure-to-PETSc-PC mapping. | |
| static PetscErrorCode | MomentumPreconditionerEngine_ValidatePetscPC (MomentumPreconditionerEngine *engine, PC pc) |
| Rejects raw options that select an unvalidated PETSc PC backend. | |
| static PetscErrorCode | MomentumPreconditionerEngine_Destroy (MomentumPreconditionerEngine *engine) |
| Destroys only a separately owned preconditioning matrix. | |
| static PetscErrorCode | MomentumNewtonKrylov_Build (UserCtx *user) |
| Builds the solve context MomentumSolver_NewtonKrylov() keeps for the run. | |
| PetscErrorCode | MomentumSolver_NewtonKrylov_Destroy (UserCtx *user) |
| Implementation of MomentumSolver_NewtonKrylov_Destroy(). | |
| static PetscErrorCode | MomentumNewtonKrylov_AssessIterate (MomentumNewtonKrylovContext *ctx, Vec solution, PetscReal *residual, PetscReal *residual_2, PetscReal *reference, PetscBool *satisfied) |
| Measure the residual at the iterate a failed SNES solve holds, for the failure policy in MomentumSolver_NewtonKrylov(). | |
| PetscErrorCode | MomentumSolver_NewtonKrylov (UserCtx *user, IBMNodes *ibm, FSInfo *fsi) |
| Solves one physical momentum step with matrix-free Newton–Krylov. | |
Variables | |
| static const MomentumPreconditionerModelOps | frozen_momentum_point_block_ops |
| struct MomentumNewtonJacobian |
Definition at line 11 of file momentum_newton_krylov.c.
| Data Fields | ||
|---|---|---|
| MomentumNewtonJacobianType | type | |
| MomentumNewtonFiniteDifferenceMode | finite_difference_mode | |
| Mat | jacobian_operator | |
| struct MomentumPreconditionerDescription |
Definition at line 27 of file momentum_newton_krylov.c.
| Data Fields | ||
|---|---|---|
| MomentumPreconditionerModel | model | |
| MomentumPreconditionerStructure | structure | |
| PetscInt | block_size | |
| PetscInt | stencil_width | |
| struct MomentumPreconditionerEngine |
Definition at line 39 of file momentum_newton_krylov.c.
| Data Fields | ||
|---|---|---|
| MomentumPreconditionerDescription | description | |
| const MomentumPreconditionerModelOps * | model_ops | |
| Mat | preconditioning_matrix | |
| PetscBool | aliases_jacobian_operator | |
| PetscBool | owns_preconditioning_matrix | |
| const char * | petsc_pc_type | |
| struct MomentumNewtonKrylovContext |
Definition at line 48 of file momentum_newton_krylov.c.
| Data Fields | ||
|---|---|---|
| UserCtx * | user | |
| SNES | snes | |
| FILE * | history_file | |
| FILE * | linear_history_file | |
| PetscBool | have_initial_norm | |
| PetscReal | initial_norm | |
| MomentumNewtonJacobian | jacobian | |
| MomentumPreconditionerEngine | preconditioning_engine | |
| Vec | solution | |
| Vec | entry_backup | |
| typedef struct MomentumNewtonKrylovContext MomentumNewtonKrylovContext |
| Enumerator | |
|---|---|
| MOM_NK_JACOBIAN_FINITE_DIFFERENCE | |
Definition at line 3 of file momentum_newton_krylov.c.
| Enumerator | |
|---|---|
| MOM_NK_FD_MODE_MATRIX_FREE | |
Definition at line 7 of file momentum_newton_krylov.c.
| Enumerator | |
|---|---|
| MOM_NK_PC_MODEL_NONE | |
| MOM_NK_PC_MODEL_FROZEN_MOMENTUM_JACOBIAN | |
Definition at line 17 of file momentum_newton_krylov.c.
| Enumerator | |
|---|---|
| MOM_NK_PC_STRUCTURE_NONE | |
| MOM_NK_PC_STRUCTURE_POINT_BLOCK | |
Definition at line 22 of file momentum_newton_krylov.c.
|
static |
Rejects configurations outside the audited version-one feature set.
| user | Single-block momentum context to validate. |
Definition at line 225 of file momentum_newton_krylov.c.
|
static |
Adapts a PETSc trial vector to the existing momentum residual path.
A matrix-free SNES residual must be a deterministic function of the trial vector X alone: F(X) may not depend on any state left by a previous residual or MFFD evaluation, or finite-difference Jacobian actions become inconsistent. To honor that contract this callback fully derives the Cartesian velocity state (Ucat/lUcat) from X before the first boundary sweep – see the inline comment below for why ApplyBoundaryConditions()'s own internal reconstruction is not sufficient for the first outlet pass.
State invariants:
Side effects: overwrites user->Ucont/lUcont, user->Ucat/lUcat, user->Rhs, the boundary Ubcs targets, and boundary flux/area diagnostics; writes F.
| snes | Calling nonlinear solver. |
| X | Trial solution (read-only). |
| F | Residual output. |
| vctx | Pointer to MomentumNewtonKrylovContext. |
Definition at line 1135 of file momentum_newton_krylov.c.
|
static |
Captures SNES iteration norms and optionally writes PICurv history rows.
SNES supplies the already-computed norm, so this monitor never causes an additional nonlinear residual evaluation. PETSc monitors selected through -mom_nk_snes_monitor remain independent and may run alongside this callback.
Definition at line 95 of file momentum_newton_krylov.c.
|
static |
Writes the effective KSP tolerance and PETSc-reported norm for each inner iteration.
The tolerance is queried after SNES has applied any inexact-Newton forcing update, so the log distinguishes changing Eisenstat–Walker requests from changing linear convergence behavior.
Definition at line 124 of file momentum_newton_krylov.c.
|
static |
Opens the optional rank-zero Newton iteration-history files (per Newton and per Krylov iteration).
Definition at line 168 of file momentum_newton_krylov.c.
|
static |
Appends one rank-zero structured Newton result for a physical step.
File failures are deliberately diagnostic-only: rollback and PETSc cleanup must retain their original error behavior.
Definition at line 186 of file momentum_newton_krylov.c.
|
static |
Replaces every non-independent residual row with an explicit equation.
Which rows are non-independent is decided by ClassifyMomentumRow(), shared with the residual path; only the action taken differs. Conditioned face-normal rows use F=X-Uconditioned, unconditioned legacy dummy/tangential rows use F=X, and periodic duplicates use Fdup=Xdup-Xrep. These equations prevent the zero Jacobian rows that EnforceRHSBoundaryConditions()'s zeroing would produce in a matrix-free Newton operator. Immersed and interface rows are rejected before this callback is installed. A TwoD-masked component needs no constraint: ComputeRHS() zeroes its spatial terms, so its row is the time derivative alone, which holds a velocity that is constant in time exactly, as the Picard solver's skipped row does.
| ctx | Active solve context. |
| X | Unconditioned PETSc trial state. |
| F | Residual vector to update in place. |
Definition at line 1055 of file momentum_newton_krylov.c.
|
static |
Reads application-owned Jacobian and preconditioner mathematics.
Definition at line 315 of file momentum_newton_krylov.c.
|
static |
Updates the Jacobian and then assembles any separate preconditioning matrix.
Definition at line 868 of file momentum_newton_krylov.c.
|
static |
Inserts only the audited interior frozen-momentum point blocks.
Definition at line 586 of file momentum_newton_krylov.c.
|
static |
Creates the frozen point-block P matrix with its exact scalar pattern.
Row and column ownership follows the velocity DMDA global vector. Physical rows reserve the three same-point components, fixed rows reserve only their diagonal, and periodic duplicate rows additionally reserve their wrapped representative. DMDA AO, local mapping, and stencil metadata are retained so the existing insertion paths keep their exact ordering.
Definition at line 891 of file momentum_newton_krylov.c.
|
static |
Opens one of the block's Newton CSV records on rank 0, or returns NULL.
PicurvOpenRunCsv() owns the lifecycle: a fresh run's first open truncates the file, the header is written to an empty file, and a continued run's first open marks the seam. A failure only warns: these files are diagnostics, and must not change the solve's error behaviour.
Definition at line 152 of file momentum_newton_krylov.c.
|
static |
Row classification with solid-cell masking folded in.
A masked row's residual is identically zero for every X, so it has a zero Jacobian row and a zero column and its unknown is undetermined. It therefore needs the same identity treatment as any other row carrying no unknown. Checked after the boundary classification so a periodic duplicate keeps its representative column.
Definition at line 384 of file momentum_newton_krylov.c.
|
inlinestatic |
Returns the squared Euclidean norm of one metric vector.
Definition at line 398 of file momentum_newton_krylov.c.
|
static |
Returns the face-averaged eddy viscosity the residual uses on one face.
The preconditioner has to reproduce the operator it preconditions, so the interior face average here tracks ComputeViscous().
The WALL branch below does not, and is retained only because it cannot currently change an assembled entry. It is unreachable by construction, not by configuration: FrozenMomentumJacobian_PointBlock() puts the eddy viscosity only on the three diagonal entries, and the row-at-a-time insertion in FrozenMomentumJacobian_AssemblePointBlocks() means row c carries only axis == c. The two coordinates that trigger the branch are exactly those ClassifyMomentumRow() reports as non-MOM_ROW_PHYSICAL, and only physical rows assemble a block; the WALL-on-a-periodic-axis loophole is closed by the pairing check in BoundarySystem_Validate(). Both rules state the same boundary fact: the wall-normal flux at a no-slip wall is not an unknown.
On a wall-resolved face ComputeViscous() uses the molecular gradient flux with no eddy viscosity, which this branch matches. On a wall-modelled face it applies the wall stress ApplyWallFunction() stored, with no gradient flux at all: that flux depends on the reference cell, the second from the wall, through the law, and not on the cells either side of the face. Widening the stencil would make the branch reachable, and the block would then need the law's derivative with respect to the reference velocity in place of any viscosity here. Tracked as issue #8.
Definition at line 428 of file momentum_newton_krylov.c.
|
static |
Returns the frozen-momentum point block for the current residual convention.
Definition at line 452 of file momentum_newton_krylov.c.
|
static |
Describes the audited frozen-coefficient point-block model.
Definition at line 569 of file momentum_newton_krylov.c.
|
static |
Inserts all common fixed, homogeneous, and periodic-duplicate rows.
Definition at line 672 of file momentum_newton_krylov.c.
|
static |
Creates the selected Jacobian operator; currently PETSc MFFD only.
Definition at line 717 of file momentum_newton_krylov.c.
|
static |
Updates the matrix-free finite-difference operator base.
Definition at line 735 of file momentum_newton_krylov.c.
|
static |
Registers the application orchestration callback and both SNES matrices.
Definition at line 747 of file momentum_newton_krylov.c.
|
static |
Destroys a partially or fully created Jacobian operator.
Definition at line 761 of file momentum_newton_krylov.c.
|
static |
Validates a model/structure and creates or aliases its matrix.
Definition at line 771 of file momentum_newton_krylov.c.
|
static |
Runs model insertion, common row handling, and final assembly.
Definition at line 806 of file momentum_newton_krylov.c.
|
static |
Applies the validated model/structure-to-PETSc-PC mapping.
Definition at line 824 of file momentum_newton_krylov.c.
|
static |
Rejects raw options that select an unvalidated PETSc PC backend.
Definition at line 835 of file momentum_newton_krylov.c.
|
static |
Destroys only a separately owned preconditioning matrix.
Definition at line 854 of file momentum_newton_krylov.c.
|
static |
Builds the solve context MomentumSolver_NewtonKrylov() keeps for the run.
Definition at line 1196 of file momentum_newton_krylov.c.
| PetscErrorCode MomentumSolver_NewtonKrylov_Destroy | ( | UserCtx * | user | ) |
Implementation of MomentumSolver_NewtonKrylov_Destroy().
Releases the Newton–Krylov solve context MomentumSolver_NewtonKrylov() keeps for the run.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/momentumsolvers.h.
Definition at line 1239 of file momentum_newton_krylov.c.
|
static |
Measure the residual at the iterate a failed SNES solve holds, for the failure policy in MomentumSolver_NewtonKrylov().
SNES judges the residual by absolute and relative tolerances that do not scale with the grid or the flow. Near a steady state the residual can reach the round-off floor of its own evaluation; the line search then cannot decrease it and SNES reports divergence for a state that is as converged as the discretization allows. The caller therefore judges the held iterate itself: by the dual-time solver's dimensionless test, |R|_inf <= mom_resid_atol * a0 |U|_inf / dt, and by whether its residual is finite and no larger than the residual the step started from.
| [in] | ctx | Solve context; its SNES supplies the residual function and vector. |
| [in] | solution | Iterate to test; it is left unchanged. |
| [out] | residual | |R|_inf at solution. |
| [out] | residual_2 | |R|_2 at solution, the norm SNES reports as its initial norm. |
| [out] | reference | a0 |U|_inf / dt at solution. |
| [out] | satisfied | True when mom_resid_atol > 0 and the dimensionless test holds. |
Definition at line 1276 of file momentum_newton_krylov.c.
Solves one physical momentum step with matrix-free Newton–Krylov.
The Jacobian operator is the finite-difference, matrix-free operator of the complete deterministic residual. With no mathematical preconditioner its matrix is also passed to PETSc's preconditioning slot and PCNONE is derived. The optional provisional frozen-momentum, point-block model approximates same-cell frozen coupling, owns a separate matrix, and derives a block-Jacobi PETSc backend. It does not relax the version-one physics or boundary-condition restrictions. The PETSc solver objects are built on the first call and kept for the run in UserCtx::momentumNewtonKrylov (MomentumSolver_NewtonKrylov_Destroy() releases them), with UserCtx::Rhs; a step that fails releases both, so the next call builds afresh. Rows removed by legacy boundary residual enforcement are made explicit: conditioned normal rows use X-Uconditioned, untouched dummy/tangential rows use X, and periodic duplicates use Xdup-Xrepresentative. Unsupported masked, interface, and component-disabled rows are rejected before setup.
| user | Single-block momentum context. |
| ibm | Must be NULL; immersed boundaries are not supported in version one. |
| fsi | Must be NULL; moving-body coupling is not supported in version one. |
simCtx->mom_last_converged is false). PETSC_ERR_CONV_FAILED after rollback when that residual is non-finite or grew. Definition at line 1303 of file momentum_newton_krylov.c.
|
static |
Definition at line 711 of file momentum_newton_krylov.c.