PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
|
// Setup code for running any simulation More...
#include "setup.h"
Go to the source code of this file.
Functions | |
PetscErrorCode | CreateSimulationContext (int argc, char **argv, SimCtx **p_simCtx) |
Allocates and populates the master SimulationContext object. | |
static PetscErrorCode | AllocateContextHierarchy (SimCtx *simCtx) |
Allocates the memory for the UserMG and UserCtx hierarchy. | |
static PetscErrorCode | SetupSolverParameters (SimCtx *simCtx) |
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, const char *fieldName) |
Updates the local vector (including ghost points) from its corresponding global vector. | |
PetscErrorCode | SetupBoundaryConditions (SimCtx *simCtx) |
(Orchestrator) Sets up all boundary conditions for the simulation. | |
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 3D array of Cmpnts structures using PetscCalloc. | |
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) |
Gets the global starting index of cells owned by this rank and the number of such cells. | |
PetscErrorCode | GetGhostedCellRange (const DMDALocalInfo *info_nodes, const RankNeighbors *neighbors, PetscInt dim, PetscInt *xs_cell_global_out, PetscInt *xm_cell_local_out) |
Gets the global cell range for a rank, including boundary cells. | |
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 for all blocks. | |
PetscErrorCode | Contra2Cart (UserCtx *user) |
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on cell faces. | |
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. | |
static PetscInt | Gidx (PetscInt i, PetscInt j, PetscInt k, UserCtx *user) |
PetscErrorCode | Divergence (UserCtx *user) |
// Setup code for running any simulation
Test program for DMSwarm interpolation using the fdf-curvIB method. Provides the setup to start any simulation with DMSwarm and DMDAs.
Definition in 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() . |
Definition at line 41 of file setup.c.
|
static |
Allocates the memory for the UserMG and UserCtx hierarchy.
This function performs the foundational memory allocation for the solver's data structures. It reads the number of multigrid levels and grid blocks from the master SimulationContext, then allocates the arrays for MGCtx and UserCtx.
It performs three critical tasks:
simCtx
back-pointer in every single UserCtx, linking it to the master configuration.simCtx | The master SimulationContext, which contains configuration and will store the resulting allocated hierarchy in its usermg member. |
Definition at line 459 of file setup.c.
|
static |
Definition at line 552 of file setup.c.
PetscErrorCode SetupGridAndSolvers | ( | SimCtx * | simCtx | ) |
The main orchestrator for setting up all grid-related components.
(Implementation of the orchestrator function itself)
Definition at line 577 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.
Definition at line 610 of file setup.c.
PetscErrorCode UpdateLocalGhosts | ( | UserCtx * | user, |
const char * | fieldName | ||
) |
Updates the local vector (including ghost points) from its corresponding global vector.
This function identifies the correct global vector, local vector, and DM based on the provided fieldName and performs the standard PETSc DMGlobalToLocalBegin/End sequence. Includes optional debugging output (max norms before/after).
user | The UserCtx structure containing the vectors and DMs. |
fieldName | The name of the field to update ("Ucat", "Ucont", "P", "Nvert", etc.). |
Definition at line 779 of file setup.c.
PetscErrorCode SetupBoundaryConditions | ( | SimCtx * | simCtx | ) |
(Orchestrator) Sets up all boundary conditions for the simulation.
Definition at line 1035 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). |
Definition at line 1085 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). |
Definition at line 1130 of file setup.c.
PetscErrorCode Allocate3DArrayVector | ( | Cmpnts **** | array, |
PetscInt | nz, | ||
PetscInt | ny, | ||
PetscInt | nx | ||
) |
Allocates a 3D array of Cmpnts structures using PetscCalloc.
Deallocates a 3D array of Cmpnts structures allocated by Allocate3DArrayVector.
This function dynamically allocates memory for a 3D array of Cmpnts (vector) structures with dimensions nz (layers) x ny (rows) x nx (columns). It uses PetscCalloc1 to ensure that all allocated memory is zero-initialized.
The allocation procedure is similar to Allocate3DArrayScalar:
After allocation, the array can be accessed as array[k][j][i] and each element (a Cmpnts structure) will have its x, y, and z fields initialized to 0.0.
[out] | array | Pointer to the 3D array to be allocated. |
[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. |
Definition at line 1187 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. |
Definition at line 1245 of file setup.c.
PetscErrorCode GetOwnedCellRange | ( | const DMDALocalInfo * | info_nodes, |
PetscInt | dim, | ||
PetscInt * | xs_cell_global_out, | ||
PetscInt * | xm_cell_local_out | ||
) |
Gets the global starting index of cells owned by this rank and the number of such cells.
Determines the global starting index and number of CELLS owned by the current processor in a specified dimension.
A cell's global index is considered the same as its origin node's global index. This function assumes a node-centered DMDA where info_nodes
provides all necessary information:
info_nodes->xs, ys, zs
: Global starting index of the first node owned by this rank (excluding ghosts).info_nodes->xm, ym, zm
: Number of nodes owned by this rank in each dimension (excluding ghosts).info_nodes->mx, my, mz
: Total number of global nodes in each dimension for the entire domain.A cell C_k
(0-indexed) is defined by its origin node N_k
and extends to node N_{k+1}
. Thus, the last node in the global domain cannot be an origin for a cell. The last possible cell origin node index is GlobalNodesInDim - 2
.
[in] | info_nodes | Pointer to the DMDALocalInfo struct for the current rank. This struct contains local ownership information (xs, xm, etc.) and global domain dimensions (mx, my, mz for nodes). |
[in] | dim | The dimension for which to get the cell range (0 for i/x, 1 for j/y, 2 for k/z). |
[out] | xs_cell_global_out | Pointer to store the global index of the first cell whose origin node is owned by this rank. If the rank owns no valid cell origins in this dimension, this will be the rank's starting node index, but xm_cell_local_out will be 0. |
[out] | xm_cell_local_out | Pointer to store the number of cells for which this rank owns the origin node AND that origin node is a valid cell origin within the global domain. |
Definition at line 1344 of file setup.c.
PetscErrorCode GetGhostedCellRange | ( | const DMDALocalInfo * | info_nodes, |
const RankNeighbors * | neighbors, | ||
PetscInt | dim, | ||
PetscInt * | xs_cell_global_out, | ||
PetscInt * | xm_cell_local_out | ||
) |
Gets the global cell range for a rank, including boundary cells.
This function first calls GetOwnedCellRange to get the conservative range of fully-contained cells. It then extends this range by applying the "Lower-Rank-Owns-Boundary" principle. A rank claims ownership of the boundary cells it shares with neighbors in the positive (+x, +y, +z) directions.
This results in a final cell range that is gap-free and suitable for building the definitive particle ownership map.
[in] | info_nodes | Pointer to the DMDALocalInfo struct. |
[in] | neighbors | Pointer to the RankNeighbors struct containing neighbor info. |
[in] | dim | The dimension (0 for i/x, 1 for j/y, 2 for k/z). |
[out] | xs_cell_global_out | Pointer to store the final starting cell index. |
[out] | xm_cell_local_out | Pointer to store the final number of cells. |
Definition at line 1452 of file setup.c.
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. If DMDAGetNeighbors returns a negative rank that is not MPI_PROC_NULL (which can happen in some PETSc/MPI configurations for non-periodic boundaries if not fully standard), this function will sanitize it to MPI_PROC_NULL to prevent issues.
[in,out] | user | Pointer to the UserCtx structure where neighbor info will be stored. |
Definition at line 1494 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). |
Definition at line 1617 of file setup.c.
PetscErrorCode SetupDomainRankInfo | ( | SimCtx * | simCtx | ) |
Sets up the full rank communication infrastructure for all blocks.
Sets up the full rank communication infrastructure, including neighbor ranks and bounding box exchange.
This function orchestrates the setup of the parallel communication map. It uses the existing helper functions to sequentially gather and broadcast the bounding box information for each computational block. All ranks participate in each step, assembling the final, unified multi-block list in parallel.
[in,out] | simCtx | The master simulation context. After this call, simCtx->bboxlist and the relevant user->neighbors fields will be populated on all ranks. |
Definition at line 1687 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->Ucat
and calling UpdateLocalGhosts(user, "Ucat")
to populate user->lUcat
. Definition at line 1785 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 . |
Definition at line 1940 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. Definition at line 2017 of file setup.c.
|
static |
Definition at line 2054 of file setup.c.
PetscErrorCode Divergence | ( | UserCtx * | user | ) |
Definition at line 2071 of file setup.c.