#include <petscpf.h>#include <petscdmswarm.h>#include <stdlib.h>#include <time.h>#include <math.h>#include <petsctime.h>#include <petscsys.h>#include <petscdmcomposite.h>#include <petscsystypes.h>#include "variables.h"#include "field_catalog.h"#include "ParticleSwarm.h"#include "walkingsearch.h"#include "grid.h"#include "logging.h"#include "io.h"#include "interpolation.h"#include "ParticleMotion.h"#include "Boundaries.h"Go to the source code of this file.
Macros | |
| #define | Allocate3DArray(array, nz, ny, nx) |
| #define | Deallocate3DArray(array, nz, ny) |
Functions | |
| int | PicurvHandleVersionArgument (int argc, char **argv, const char *executable_name) |
| Print the shared native build identity when a version flag is present. | |
| int | PicurvCheckLinkedPetscIndexWidth (int argc, char **argv, const char *executable_name) |
| Refuse to start when the loaded libpetsc's PetscInt width differs from this build's. | |
| PetscErrorCode | PicurvCheckPetscIntRange (MPI_Comm comm, PetscInt64 entries, const char *object) |
| Refuse a PETSc object whose global size this build's PetscInt cannot index. | |
| PetscErrorCode | CreateSimulationContext (int argc, char **argv, SimCtx **p_simCtx) |
| Allocates and populates the master SimulationContext object. | |
| PetscBool | RuntimeWalltimeGuardParsePositiveSeconds (const char *text, PetscReal *seconds_out) |
| Parse a positive floating-point seconds value from runtime metadata. | |
| PetscErrorCode | SetupSimulationEnvironment (SimCtx *simCtx) |
| Verifies and prepares the complete I/O environment for a simulation run. | |
| PetscErrorCode | SetupGridAndSolvers (SimCtx *simCtx) |
| The main orchestrator for setting up all grid-related components. | |
| PetscErrorCode | CreateAndInitializeAllVectors (SimCtx *simCtx) |
| Creates and initializes all PETSc Vec objects for all fields. | |
| PetscErrorCode | UpdateLocalGhosts (UserCtx *user, FieldId field_id) |
| Updates the local vector (including ghost points) from its corresponding global vector. | |
| PetscErrorCode | RepairPeriodicFieldGhosts (UserCtx *user, const FieldView *view, Vec local_vec) |
| Applies a catalogued field's periodic normal-face ghost repair to a local vector. | |
| PetscErrorCode | SetupBoundaryConditions (SimCtx *simCtx) |
| (Orchestrator) Sets up all boundary conditions for the simulation. | |
| PetscErrorCode | InitializeSolutionConvergenceState (SimCtx *simCtx) |
| Allocates any runtime storage required by solution-convergence logging. | |
| PetscErrorCode | DestroySolutionConvergenceState (SimCtx *simCtx) |
| Frees any runtime storage allocated for solution-convergence logging. | |
| PetscErrorCode | Allocate3DArrayScalar (PetscReal ****array, PetscInt nz, PetscInt ny, PetscInt nx) |
| Allocates a 3D array of PetscReal values using PetscCalloc. | |
| PetscErrorCode | Deallocate3DArrayScalar (PetscReal ***array, PetscInt nz, PetscInt ny) |
| Deallocates a 3D array of PetscReal values allocated by Allocate3DArrayScalar. | |
| PetscErrorCode | Allocate3DArrayVector (Cmpnts ****array, PetscInt nz, PetscInt ny, PetscInt nx) |
Allocates a contiguous 3D array of Cmpnts values. | |
| PetscErrorCode | Deallocate3DArrayVector (Cmpnts ***array, PetscInt nz, PetscInt ny) |
| Deallocates a 3D array of Cmpnts structures allocated by Allocate3DArrayVector. | |
| PetscErrorCode | GetOwnedCellRange (const DMDALocalInfo *info_nodes, PetscInt dim, PetscInt *xs_cell_global_out, PetscInt *xm_cell_local_out) |
| Determines the global starting index and number of CELLS owned by the current processor in a specified dimension. | |
| static void | GetPeriodicAxes (const SimCtx *simCtx, PetscBool periodic[3]) |
| Which grid axes are periodic. | |
| static void | GetOwnedInteriorCellBounds (const DMDALocalInfo *info, PetscInt lo[3], PetscInt hi[3]) |
| Index bounds of the interior cells this rank owns in the solver's cell-centred layout: the owned range with the dummy layer at index 0 and at index m-1 removed. | |
| PetscErrorCode | ComputeAndStoreNeighborRanks (UserCtx *user) |
| Computes and stores the Cartesian neighbor ranks for the DMDA decomposition. | |
| PetscErrorCode | SetDMDAProcLayout (DM dm, UserCtx *user) |
| Sets the processor layout for a given DMDA based on PETSc options. | |
| PetscErrorCode | SetupDomainRankInfo (SimCtx *simCtx) |
| Sets up the full rank communication infrastructure, including neighbor ranks and bounding box exchange. | |
| PetscErrorCode | Contra2Cart (UserCtx *user) |
| Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on cell faces. | |
| static PetscErrorCode | ContravariantToCartesianInCell (Cmpnts ***lucont, Cmpnts ***lcsi, Cmpnts ***leta, Cmpnts ***lzet, PetscInt i, PetscInt j, PetscInt k, Cmpnts w, Cmpnts *ucat) |
| Cartesian velocity at a point of one cell from the cell's face fluxes. | |
| PetscErrorCode | Cart2Contra (UserCtx *user) |
| Convert the ghosted Cartesian velocity field to contravariant face fluxes. | |
| PetscErrorCode | SetupDomainCellDecompositionMap (UserCtx *user) |
| Creates and distributes a map of the domain's cell decomposition to all ranks. | |
| PetscErrorCode | BinarySearchInt64 (PetscInt n, const PetscInt64 arr[], PetscInt64 key, PetscBool *found) |
| Performs a binary search for a key in a sorted array of PetscInt64. | |
| PetscErrorCode | ComputeDivergence (UserCtx *user) |
| Computes the discrete divergence of the contravariant velocity field. | |
| void | TransformScalarDerivativesToPhysical (PetscReal jacobian, Cmpnts csi_metrics, Cmpnts eta_metrics, Cmpnts zet_metrics, PetscReal dPhi_dcsi, PetscReal dPhi_deta, PetscReal dPhi_dzet, Cmpnts *gradPhi) |
| Transforms scalar derivatives from computational space to physical space. | |
| PetscErrorCode | ComputeScalarFieldDerivatives (UserCtx *user, PetscInt i, PetscInt j, PetscInt k, PetscReal ***field_data, Cmpnts *grad) |
| Computes the gradient of a cell-centered SCALAR field at a specific grid point. | |
| PetscErrorCode | ComputeVectorFieldDerivatives (UserCtx *user, PetscInt i, PetscInt j, PetscInt k, Cmpnts ***field_data, Cmpnts *dudx, Cmpnts *dvdx, Cmpnts *dwdx) |
| Computes the derivatives of a cell-centered vector field at a specific grid point. | |
| PetscErrorCode | LESConfigSetDefaults (LESConfig *config) |
| Installs the shipped defaults for every LES closure parameter. | |
| PetscErrorCode | DestroyUserVectors (UserCtx *user) |
| Destroys all PETSc Vec objects within a single UserCtx structure. | |
| PetscErrorCode | DestroyUserContext (UserCtx *user) |
| Destroys all resources allocated within a single UserCtx structure. | |
| PetscErrorCode | FinalizeSimulation (SimCtx *simCtx) |
| Main cleanup function for the entire simulation context. | |
| #define Allocate3DArray | ( | array, | |
| nz, | |||
| ny, | |||
| nx | |||
| ) |
| #define Deallocate3DArray | ( | array, | |
| nz, | |||
| ny | |||
| ) |
| int PicurvHandleVersionArgument | ( | int | argc, |
| char ** | argv, | ||
| const char * | executable_name | ||
| ) |
Print the shared native build identity when a version flag is present.
Prints two lines: <name> <release>+g<commit>[.dirty], then the PETSc this binary was compiled against, petsc <version> <debug|optimized> <int32|int64> <arch> <dir>, the third field being the width of PetscInt. The second line is also kept in the binary behind a PICURV_PETSC_BUILD: marker, so the conductor can read it from a binary that cannot start.
| [in] | argc | Native executable argument count. |
| [in] | argv | Native executable argument vector. |
| [in] | executable_name | Name printed when argv[0] is absent; otherwise the basename of argv[0] is printed, so simulator64 names itself as the conductor staged it. |
Print the shared native build identity when a version flag is present.
The public argument and return contract is documented in include/setup.h.
Definition at line 119 of file setup.c.
| int PicurvCheckLinkedPetscIndexWidth | ( | int | argc, |
| char ** | argv, | ||
| const char * | executable_name | ||
| ) |
Refuse to start when the loaded libpetsc's PetscInt width differs from this build's.
The 32- and 64-bit-index PETSc builds share the library name libpetsc.so.MAJOR.MINOR, so another build's library first on LD_LIBRARY_PATH (a PETSc module, say) is loaded without complaint and corrupts every size it is handed. The library reports its own width through PetscDataTypeGetSize(PETSC_INT), whose signature does not depend on it, so this is safe to call before PetscInitialize().
| [in] | argc | Native executable argument count. |
| [in] | argv | Native executable argument vector; the message names argv[0]'s basename. |
| [in] | executable_name | Name used in the message when argv[0] is absent. |
Refuse to start when the loaded libpetsc's PetscInt width differs from this build's.
Definition at line 79 of file setup.c.
| PetscErrorCode PicurvCheckPetscIntRange | ( | MPI_Comm | comm, |
| PetscInt64 | entries, | ||
| const char * | object | ||
| ) |
Refuse a PETSc object whose global size this build's PetscInt cannot index.
A vector's or matrix's global size, and every index into it, is a PetscInt. Past PETSC_MAX_INT a 32-bit-index build overflows silently, corrupting sizes, ownership ranges and checkpoint headers, so every object PICurv sizes passes through here before it is created: particle fields at setup, each grid DM where grid.c creates it, and so every solver's vectors and matrices built on those DMs.
| [in] | comm | Communicator on which the refusal is raised (collective when it is). |
| [in] | entries | Global entries the object needs, computed in 64 bits. |
| [in] | object | What the object is, for the message (e.g. "a 3-component grid DM"). |
Refuse a PETSc object whose global size this build's PetscInt cannot index.
Definition at line 102 of file setup.c.
| PetscErrorCode CreateSimulationContext | ( | int | argc, |
| char ** | argv, | ||
| SimCtx ** | p_simCtx | ||
| ) |
Allocates and populates the master SimulationContext object.
This function serves as the single, authoritative entry point for all simulation configuration. It merges the setup logic from both the legacy FSI/IBM solver and the modern particle solver into a unified, robust process.
The function follows a strict sequence:
SimulationContext and populates every field with a sane, hardcoded default value. This ensures the simulation starts from a known, predictable state.-func_config_file option to load a list of function names allowed to produce log output. This configuration (the file path and the list of function names) is stored within the SimulationContext for later reference and cleanup.PetscOptionsGet... calls for every possible command-line flag, overriding the default values set in step 1.| [in] | argc | Argument count passed from main. |
| [in] | argv | Argument vector passed from main. |
| [out] | p_simCtx | On success, this will point to the newly created and fully configured SimulationContext pointer. The caller is responsible for eventually destroying this object by calling FinalizeSimulation(). |
Allocates and populates the master SimulationContext object.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 453 of file setup.c.
| PetscBool RuntimeWalltimeGuardParsePositiveSeconds | ( | const char * | text, |
| PetscReal * | seconds_out | ||
| ) |
Parse a positive floating-point seconds value from runtime metadata.
This helper is used for shell-exported walltime metadata such as PICURV_JOB_START_EPOCH and PICURV_WALLTIME_LIMIT_SECONDS.
| [in] | text | String to parse. |
| [out] | seconds_out | Parsed positive seconds value when successful. |
PETSC_TRUE when parsing succeeds, else PETSC_FALSE.Parse a positive floating-point seconds value from runtime metadata.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 144 of file setup.c.
| PetscErrorCode SetupSimulationEnvironment | ( | SimCtx * | simCtx | ) |
Verifies and prepares the complete I/O environment for a simulation run.
This function performs a comprehensive series of checks and setup actions to ensure a valid and clean environment. It is parallel-safe; all filesystem operations and checks are performed by Rank 0, with collective error handling.
The function's responsibilities include:
restart_dir or post-processing source directories exist when needed.| [in] | simCtx | The fully configured SimulationContext object. |
Verifies and prepares the complete I/O environment for a simulation run.
Definition at line 1728 of file setup.c.
| PetscErrorCode SetupGridAndSolvers | ( | SimCtx * | simCtx | ) |
The main orchestrator for setting up all grid-related components.
This function is the high-level driver for creating the entire computational domain, including the multigrid hierarchy, PETSc DMDA and Vec objects, and calculating all necessary grid metrics.
| simCtx | The fully configured SimulationContext. |
The main orchestrator for setting up all grid-related components.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 2066 of file setup.c.
| PetscErrorCode CreateAndInitializeAllVectors | ( | SimCtx * | simCtx | ) |
Creates and initializes all PETSc Vec objects for all fields.
This function iterates through every UserCtx in the multigrid and multi-block hierarchy. For each context, it creates the comprehensive set of global and local PETSc Vecs required by the flow solver (e.g., Ucont, P, Nvert, metrics, turbulence fields, etc.). Each vector is initialized to zero.
Creates and initializes all PETSc Vec objects for all fields.
Definition at line 2099 of file setup.c.
Updates the local vector (including ghost points) from its corresponding global vector.
This function resolves the correct global vector, local vector, and DM from the authoritative field catalog and performs the standard PETSc DMGlobalToLocalBegin/End sequence. For registered single-face-family and component-staggered fields, it then repairs the adjacent periodic ghost in each normal direction so central differences see the correct neighboring face. Cell-centered fields and tangential face directions retain PETSc's native periodic wraparound. Includes optional debugging output (max norms before/after).
| user | The UserCtx structure containing the vectors and DMs. |
| field_id | Typed identity of the catalogued field to update. |
Updates the local vector (including ghost points) from its corresponding global vector.
Resolves the typed field view, performs the existing PETSc scatter, and applies the established periodic normal-face repair when needed.
Definition at line 2593 of file setup.c.
Applies a catalogued field's periodic normal-face ghost repair to a local vector.
The step UpdateLocalGhosts() runs after its scatter: for single-face-family and component-staggered fields, each active periodic face's adjacent normal ghost is rewritten from the deeper PETSc ghost; other fields are left unchanged. The result depends only on the local vector's scattered entries, so it may be reapplied after any partial rescatter of them.
| user | Block context owning the field's boundary metadata. |
| view | Field view from FieldGetView(); its DM lays out local_vec. |
| local_vec | Local vector to repair: the field's own, or a scratch copy of it. |
Applies a catalogued field's periodic normal-face ghost repair to a local vector.
Definition at line 2556 of file setup.c.
| PetscErrorCode SetupBoundaryConditions | ( | SimCtx * | simCtx | ) |
(Orchestrator) Sets up all boundary conditions for the simulation.
| simCtx | Simulation context controlling the operation. |
(Orchestrator) Sets up all boundary conditions for the simulation.
Definition at line 2755 of file setup.c.
| PetscErrorCode InitializeSolutionConvergenceState | ( | SimCtx * | simCtx | ) |
Allocates any runtime storage required by solution-convergence logging.
This helper is setup-owned because it allocates mode-specific buffers once the finest-level Eulerian vectors already exist. It does not compute or log any metrics; it only prepares storage for later use by LOG_SOLUTION_CONVERGENCE().
Allocation depends on the active solution-convergence mode:
Ucat / P reference slot per configured period step and per blockmean_speed and mean_keThe helper also resets the recorded-sample counter so warmup behavior starts from a clean state each time solver setup is rebuilt.
| [in,out] | simCtx | Master simulation context owning the runtime storage. |
Allocates any runtime storage required by solution-convergence logging.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 176 of file setup.c.
| PetscErrorCode DestroySolutionConvergenceState | ( | SimCtx * | simCtx | ) |
Frees any runtime storage allocated for solution-convergence logging.
This helper is the cleanup counterpart to InitializeSolutionConvergenceState(). It releases periodic reference buffers and statistical-history arrays, then resets the associated pointers and counters. It is safe to call during teardown even if a given mode never allocated optional storage.
| [in,out] | simCtx | Master simulation context owning the runtime storage. |
Frees any runtime storage allocated for solution-convergence logging.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 230 of file setup.c.
| PetscErrorCode Allocate3DArrayScalar | ( | PetscReal **** | array, |
| PetscInt | nz, | ||
| PetscInt | ny, | ||
| PetscInt | nx | ||
| ) |
Allocates a 3D array of PetscReal values using PetscCalloc.
This function dynamically allocates memory for a 3D array of PetscReal values with dimensions nz (layers) x ny (rows) x nx (columns). It uses PetscCalloc1 to ensure the memory is zero-initialized.
The allocation is done in three steps:
This setup allows the array to be accessed as array[k][j][i], and the memory for the data is contiguous, which improves cache efficiency.
| [out] | array | Pointer to the 3D array to be allocated. |
| [in] | nz | Number of layers (z-direction). |
| [in] | ny | Number of rows (y-direction). |
| [in] | nx | Number of columns (x-direction). |
Allocates a 3D array of PetscReal values using PetscCalloc.
Definition at line 2819 of file setup.c.
| PetscErrorCode Deallocate3DArrayScalar | ( | PetscReal *** | array, |
| PetscInt | nz, | ||
| PetscInt | ny | ||
| ) |
Deallocates a 3D array of PetscReal values allocated by Allocate3DArrayScalar.
This function frees the memory allocated for a 3D array of PetscReal values. It assumes the memory was allocated using Allocate3DArrayScalar, which allocated three separate memory blocks: one for the contiguous data, one for the row pointers, and one for the layer pointers.
| [in] | array | Pointer to the 3D array to be deallocated. |
| [in] | nz | Number of layers (z-direction). |
| [in] | ny | Number of rows (y-direction). |
Deallocates a 3D array of PetscReal values allocated by Allocate3DArrayScalar.
Definition at line 2856 of file setup.c.
| PetscErrorCode Allocate3DArrayVector | ( | Cmpnts **** | array, |
| PetscInt | nz, | ||
| PetscInt | ny, | ||
| PetscInt | nx | ||
| ) |
Allocates a contiguous 3D array of Cmpnts values.
The memory layout mirrors Allocate3DArrayScalar: layer pointers, row pointers, and contiguous payload are allocated such that indexing as array[k][j][i] is valid while keeping payload data contiguous.
| [out] | array | Pointer to the 3D vector array to allocate. |
| [in] | nz | Number of layers in the z-direction. |
| [in] | ny | Number of rows in the y-direction. |
| [in] | nx | Number of columns in the x-direction. |
Allocates a contiguous 3D array of Cmpnts values.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 2902 of file setup.c.
| PetscErrorCode Deallocate3DArrayVector | ( | Cmpnts *** | array, |
| PetscInt | nz, | ||
| PetscInt | ny | ||
| ) |
Deallocates a 3D array of Cmpnts structures allocated by Allocate3DArrayVector.
This function frees the memory allocated for a 3D array of Cmpnts structures. It assumes the memory was allocated using Allocate3DArrayVector, which created three separate memory blocks: one for the contiguous vector data, one for the row pointers, and one for the layer pointers.
| [in] | array | Pointer to the 3D array to be deallocated. |
| [in] | nz | Number of layers in the z-direction. |
| [in] | ny | Number of rows in the y-direction. |
Deallocates a 3D array of Cmpnts structures allocated by Allocate3DArrayVector.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 2954 of file setup.c.
| PetscErrorCode GetOwnedCellRange | ( | const DMDALocalInfo * | info_nodes, |
| PetscInt | dim, | ||
| PetscInt * | xs_cell_global_out, | ||
| PetscInt * | xm_cell_local_out | ||
| ) |
Determines the global starting index and number of CELLS owned by the current processor in a specified dimension.
Ownership is defined by the rank owning the cell's origin node (min i,j,k corner).
| [in] | info_nodes | Pointer to the DMDALocalInfo struct obtained from the NODE-based DMDA (e.g., user->da or user->fda, assuming they have consistent nodal partitioning for defining cell origins). |
| [in] | dim | The dimension to compute the range for (0 for x/i, 1 for y/j, 2 for z/k). |
| [out] | xs_cell_global_out | Pointer to store the starting global cell index owned by this process. |
| [out] | xm_cell_local_out | Pointer to store the number of owned cells in this dimension. |
Determines the global starting index and number of CELLS owned by the current processor in a specified dimension.
Definition at line 3021 of file setup.c.
|
inlinestatic |
Which grid axes are periodic.
From the run's flags, which the loader derives from the boundary files and which selected DM_BOUNDARY_PERIODIC when the DMDAs were built. The loader refuses a case whose opposite faces or whose blocks disagree on periodicity, so the flags hold for every face, block and multigrid level.
| [in] | simCtx | Simulation context. |
| [out] | periodic | Whether the i, j and k axes are periodic. |
Definition at line 361 of file setup.h.
|
inlinestatic |
Index bounds of the interior cells this rank owns in the solver's cell-centred layout: the owned range with the dummy layer at index 0 and at index m-1 removed.
lo[a] <= c < hi[a] along each axis. A periodic axis is treated like a walled one: its seam face at index 0 is not included, and callers that set it extend the range themselves.
| [in] | info | Local info of the block's da (or fda; they share the decomposition). |
| [out] | lo | First owned interior index along i, j, k. |
| [out] | hi | One past the last owned interior index along i, j, k. |
Definition at line 378 of file setup.h.
| PetscErrorCode ComputeAndStoreNeighborRanks | ( | UserCtx * | user | ) |
Computes and stores the Cartesian neighbor ranks for the DMDA decomposition.
This function retrieves the neighbor information from the primary DMDA (user->da) and stores the face neighbors (xm, xp, ym, yp, zm, zp) in the user->neighbors structure. It assumes a standard PETSc ordering for the neighbors array returned by DMDAGetNeighbors. Logs warnings if the assumed indices seem incorrect (e.g., center rank mismatch).
| [in,out] | user | Pointer to the UserCtx structure where neighbor info will be stored. |
Computes and stores the Cartesian neighbor ranks for the DMDA decomposition.
Definition at line 3116 of file setup.c.
| PetscErrorCode SetDMDAProcLayout | ( | DM | dm, |
| UserCtx * | user | ||
| ) |
Sets the processor layout for a given DMDA based on PETSc options.
Reads the desired number of processors in x, y, and z directions using PETSc options (e.g., -dm_processors_x, -dm_processors_y, -dm_processors_z). If an option is not provided for a direction, PETSC_DECIDE is used for that direction. Applies the layout using DMDASetNumProcs.
Also stores the retrieved/decided values in user->procs_x/y/z if user context is provided.
| dm | The DMDA object to configure the layout for. |
| user | Pointer to the UserCtx structure (optional, used to store layout values). |
Sets the processor layout for a given DMDA based on PETSc options.
Definition at line 3166 of file setup.c.
| PetscErrorCode SetupDomainRankInfo | ( | SimCtx * | simCtx | ) |
Sets up the full rank communication infrastructure, including neighbor ranks and bounding box exchange.
This function orchestrates the following steps:
The final result is that each rank has access to its immediate neighbors and the bounding box information of all ranks.
| [in,out] | simCtx | Pointer to initialized simulation context that owns all block UserCtx objects. |
Sets up the full rank communication infrastructure, including neighbor ranks and bounding box exchange.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 3242 of file setup.c.
| PetscErrorCode Contra2Cart | ( | UserCtx * | user | ) |
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on cell faces.
This function performs the transformation from a contravariant velocity representation (which is natural on a curvilinear grid) to a Cartesian (x,y,z) representation. For each interior computational cell owned by the rank, it performs the following:
[MetricTensor] * [ucat] = [ucont] for the Cartesian velocity vector ucat = (u,v,w) using Cramer's rule.user->Ucat vector.The function operates on local, ghosted versions of the input vectors (user->lUcont, user->lCsi, etc.) to ensure stencils are valid across processor boundaries.
| [in,out] | user | Pointer to the UserCtx structure. The function reads from user->lUcont, user->lCsi, user->lEta, user->lZet, user->lNvert and writes to the global user->Ucat vector. |
user->lUcont and all local metric vectors (user->lCsi, etc.) have been populated with up-to-date ghost values via UpdateLocalGhosts.Ucat for interior cells (not on physical boundaries) and for cells not marked as solid/blanked by user->lNvert.user->lUcat.SynchronizePeriodicCellFields(user, 1, {FIELD_ID_UCAT}) and then call UpdateLocalGhosts(user, FIELD_ID_UCAT). The latter remains necessary for the fully nonperiodic case because periodic synchronization is then a no-op.Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on cell faces.
Definition at line 3313 of file setup.c.
|
inlinestatic |
Cartesian velocity at a point of one cell from the cell's face fluxes.
Each contravariant flux (U^1, U^2, U^3) and each face-area vector row (Csi, Eta, Zet) is interpolated linearly between the cell's two faces of that family, at the fraction w.x, w.y, w.z of the way from the minimum face, and S_i . u = U^i is solved by Cramer's rule. With w = (1/2, 1/2, 1/2) this is the cell-centre value Contra2Cart() stores. The flux through each face family varies linearly along that family's own axis, so on a cell whose face-area vectors do not vary along their own axis (any rectilinear cell) the velocity's divergence is the cell's discrete flux divergence, and zero when the fluxes are divergence-free. It is inline and has no profiling hook: it runs once per cell or particle, inside Contra2Cart()'s loop on the solver's hot path.
| [in] | lucont | Ghosted local Ucont array. |
| [in] | lcsi | Ghosted local Csi array. |
| [in] | leta | Ghosted local Eta array. |
| [in] | lzet | Ghosted local Zet array. |
| [in] | i | Cell index in cell-centred array numbering (physical cell i - 1). |
| [in] | j | As i, for the j direction. |
| [in] | k | As i, for the k direction. |
| [in] | w | Position in the cell, each component in [0, 1] from the minimum face. |
| [out] | ucat | Cartesian velocity at that position. |
PETSC_ERR_FLOP_COUNT when the face-area vectors are degenerate. Definition at line 503 of file setup.h.
| PetscErrorCode Cart2Contra | ( | UserCtx * | user | ) |
Convert the ghosted Cartesian velocity field to contravariant face fluxes.
Interpolates user->lUcat to each logical face and dots it with the corresponding face-area metric vector. Writes independent entries of user->Ucont. Periodic and physical endpoint values in user->lUcat must be finalized and its MPI ghosts current before entry. The resulting Ucont is not synchronized; callers must use the staggered-field workflow before stencil use.
| [in,out] | user | UserCtx for the block; Ucont is written in place. |
Convert the ghosted Cartesian velocity field to contravariant face fluxes.
Definition at line 3396 of file setup.c.
| PetscErrorCode SetupDomainCellDecompositionMap | ( | UserCtx * | user | ) |
Creates and distributes a map of the domain's cell decomposition to all ranks.
This function is a critical part of the simulation setup. It determines the global cell ownership for each MPI rank and makes this information available to all other ranks. This "decomposition map" is essential for the robust "Walk and Handoff" particle migration strategy, allowing any rank to quickly identify the owner of a target cell.
The process involves:
GetOwnedCellRange helper function.MPI_Allgather collective operation to build a complete array (user->RankCellInfoMap) containing the ownership information for every rank.This function should be called once during initialization after the primary DMDA (user->da) has been set up.
| [in,out] | user | Pointer to the UserCtx structure. The function will allocate and populate user->RankCellInfoMap and set user->num_ranks. |
Creates and distributes a map of the domain's cell decomposition to all ranks.
Definition at line 3467 of file setup.c.
| PetscErrorCode BinarySearchInt64 | ( | PetscInt | n, |
| const PetscInt64 | arr[], | ||
| PetscInt64 | key, | ||
| PetscBool * | found | ||
| ) |
Performs a binary search for a key in a sorted array of PetscInt64.
This is a standard binary search algorithm implemented as a PETSc-style helper function. It efficiently determines if a given key exists within a sorted array.
| [in] | n | The number of elements in the array. |
| [in] | arr | A pointer to the sorted array of PetscInt64 values to be searched. |
| [in] | key | The PetscInt64 value to search for. |
| [out] | found | A pointer to a PetscBool that will be set to PETSC_TRUE if the key is found, and PETSC_FALSE otherwise. |
arr must be sorted in ascending order for the algorithm to work correctly.Performs a binary search for a key in a sorted array of PetscInt64.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 3532 of file setup.c.
| PetscErrorCode ComputeDivergence | ( | UserCtx * | user | ) |
Computes the discrete divergence of the contravariant velocity field.
This diagnostic/kernel routine evaluates continuity residuals on the local block and writes the resulting divergence field into the configured output vector(s).
| [in,out] | user | Block-level context containing velocity and metric fields. |
Computes the discrete divergence of the contravariant velocity field.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 3599 of file setup.c.
| void TransformScalarDerivativesToPhysical | ( | PetscReal | jacobian, |
| Cmpnts | csi_metrics, | ||
| Cmpnts | eta_metrics, | ||
| Cmpnts | zet_metrics, | ||
| PetscReal | dPhi_dcsi, | ||
| PetscReal | dPhi_deta, | ||
| PetscReal | dPhi_dzet, | ||
| Cmpnts * | gradPhi | ||
| ) |
Transforms scalar derivatives from computational space to physical space.
using the chain rule. Formula: dPhi/dx = J * ( dPhi/dCsi * dCsi/dx + dPhi/dEta * dEta/dx + ... )
| jacobian | Local mapping Jacobian determinant. | |
| csi_metrics | Physical gradient of the xi coordinate. | |
| eta_metrics | Physical gradient of the eta coordinate. | |
| zet_metrics | Physical gradient of the zeta coordinate. | |
| dPhi_dcsi | Scalar derivative in the xi direction. | |
| dPhi_deta | Scalar derivative in the eta direction. | |
| dPhi_dzet | Scalar derivative in the zeta direction. | |
| [out] | gradPhi | Physical Cartesian scalar gradient. |
Transforms scalar derivatives from computational space to physical space.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 3753 of file setup.c.
| PetscErrorCode ComputeScalarFieldDerivatives | ( | UserCtx * | user, |
| PetscInt | i, | ||
| PetscInt | j, | ||
| PetscInt | k, | ||
| PetscReal *** | field_data, | ||
| Cmpnts * | grad | ||
| ) |
Computes the gradient of a cell-centered SCALAR field at a specific grid point.
| user | Block metric context. | |
| i | Cell index in the xi direction. | |
| j | Cell index in the eta direction. | |
| k | Cell index in the zeta direction. | |
| field_data | Ghosted scalar values indexed as [k][j][i]. | |
| [out] | grad | Physical Cartesian gradient at the requested cell. |
Computes the gradient of a cell-centered SCALAR field at a specific grid point.
Definition at line 3799 of file setup.c.
| PetscErrorCode ComputeVectorFieldDerivatives | ( | UserCtx * | user, |
| PetscInt | i, | ||
| PetscInt | j, | ||
| PetscInt | k, | ||
| Cmpnts *** | field_data, | ||
| Cmpnts * | dudx, | ||
| Cmpnts * | dvdx, | ||
| Cmpnts * | dwdx | ||
| ) |
Computes the derivatives of a cell-centered vector field at a specific grid point.
This function orchestrates the calculation of spatial derivatives. It first computes the derivatives in computational space (d/dcsi, d/deta, d/dzet) using a central difference scheme and then transforms them into physical space (d/dx, d/dy, d/dz).
| user | Block metric context. | |
| i | Cell index in the xi direction. | |
| j | Cell index in the eta direction. | |
| k | Cell index in the zeta direction. | |
| field_data | Ghosted Cartesian vector values indexed as [k][j][i]. | |
| [out] | dudx | Gradient of the x velocity component. |
| [out] | dvdx | Gradient of the y velocity component. |
| [out] | dwdx | Gradient of the z velocity component. |
Computes the derivatives of a cell-centered vector field at a specific grid point.
Definition at line 3842 of file setup.c.
| PetscErrorCode LESConfigSetDefaults | ( | LESConfig * | config | ) |
Installs the shipped defaults for every LES closure parameter.
One definition of the defaults, shared by the control-file ingress and by test fixtures, so the two cannot drift apart. It lives with the rest of the context construction because defaults are ingress, not closure behaviour, and because the postprocessor builds a simulation context without linking the LES module.
Local averaging and clamped limiting are the defaults because they are the only settings valid on arbitrary geometry; cases with homogeneous directions or an interest in backscatter opt into the alternatives.
| [out] | config | Configuration block to populate. |
PETSC_ERR_ARG_NULL for a null argument.Installs the shipped defaults for every LES closure parameter.
Full API contract is documented with the header declaration in include/setup.h.
Definition at line 288 of file setup.c.
| PetscErrorCode DestroyUserVectors | ( | UserCtx * | user | ) |
Destroys all PETSc Vec objects within a single UserCtx structure.
This helper function systematically destroys the Vec objects stored in a UserCtx. The vectors are organized into functional groups for clarity:
All destroys are protected with NULL checks to handle conditional allocations safely.
| [in,out] | user | Pointer to the UserCtx containing the vectors to destroy. |
Destroys all PETSc Vec objects within a single UserCtx structure.
Definition at line 3894 of file setup.c.
| PetscErrorCode DestroyUserContext | ( | UserCtx * | user | ) |
Destroys all resources allocated within a single UserCtx structure.
This function cleans up all memory and PETSc objects associated with a single UserCtx (grid level). It calls the helper functions and destroys remaining objects in the proper dependency order:
This function should be called for each UserCtx in the multigrid hierarchy.
| [in,out] | user | Pointer to the UserCtx to be destroyed. |
Destroys all resources allocated within a single UserCtx structure.
Definition at line 4046 of file setup.c.
| PetscErrorCode FinalizeSimulation | ( | SimCtx * | simCtx | ) |
Main cleanup function for the entire simulation context.
This function is responsible for destroying ALL memory and PETSc objects allocated during the simulation, including:
This function should be called ONCE at the end of the simulation, after all computation is complete, but BEFORE PetscFinalize().
Call order in main:
| [in,out] | simCtx | Pointer to the master SimulationContext to be destroyed. The pointer is invalid after this call returns. |
Main cleanup function for the entire simulation context.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/setup.h.
Definition at line 4137 of file setup.c.