|
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.
Macros | |
| #define | __FUNCT__ "CreateSimulationContext" |
| #define | __FUNCT__ "PetscMkdirRecursive" |
| #define | __FUNCT__ "SetupSimulationEnvironment" |
| #define | __FUNCT__ "AllocateContextHeirarchy" |
| #define | __FUNCT__ "SetupSolverParameters" |
| #define | __FUNCT__ "SetupGridAndSolvers" |
| #define | __FUNCT__ "CreateAndInitializeAllVectors" |
| #define | __FUNCT__ "UpdateLocalGhosts" |
| #define | __FUNCT__ "SetupBoundaryConditions" |
| #define | __FUNCT__ "GetOwnedCellRange" |
| #define | __FUNCT__ "ComputeAndStoreNeighborRanks" |
| #define | __FUNCT__ "SetDMDAProcLayout" |
| #define | __FUNCT__ "SetupDomainRankInfo" |
| #define | __FUNCT__ "Contra2Cart" |
| #define | __FUNCT__ "SetupDomainCellDecompositionMap" |
| #define | __FUNCT__ "BinarySearchInt64" |
| #define | __FUNCT__ "ComputeDivergence" |
| #define | __FUNCT__ "InitializeRandomGenerators" |
| #define | __FUNCT__ "InitializeLogicalSpaceRNGs" |
| #define | __FUNCT__ "ComputeVectorFieldDerivatives" |
Functions | |
| PetscErrorCode | CreateSimulationContext (int argc, char **argv, SimCtx **p_simCtx) |
| Allocates and populates the master SimulationContext object. | |
| static PetscErrorCode | PetscMkdirRecursive (const char *path) |
Creates a directory path recursively, similar to mkdir -p. | |
| PetscErrorCode | SetupSimulationEnvironment (SimCtx *simCtx) |
| Verifies and prepares the complete I/O environment for a simulation run. | |
| 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 | 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 | ComputeDivergence (UserCtx *user) |
| PetscErrorCode | InitializeRandomGenerators (UserCtx *user, PetscRandom *randx, PetscRandom *randy, PetscRandom *randz) |
| Initializes random number generators for assigning particle properties. | |
| PetscErrorCode | InitializeLogicalSpaceRNGs (PetscRandom *rand_logic_i, PetscRandom *rand_logic_j, PetscRandom *rand_logic_k) |
| Initializes random number generators for logical space operations [0.0, 1.0). | |
| static void | TransformDerivativesToPhysical (PetscReal jacobian, Cmpnts csi_metrics, Cmpnts eta_metrics, Cmpnts zet_metrics, Cmpnts deriv_csi, Cmpnts deriv_eta, Cmpnts deriv_zet, Cmpnts *dudx, Cmpnts *dvdx, Cmpnts *dwdx) |
| Transforms derivatives from computational space to physical space using the chain rule. | |
| 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. | |
// 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 44 of file setup.c.
|
static |
Creates a directory path recursively, similar to mkdir -p.
This function is designed to be called by Rank 0 only. It takes a full path, parses it, and creates each directory component in sequence.
| [in] | path | The full directory path to create. |
Definition at line 491 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. |
Definition at line 558 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 730 of file setup.c.
|
static |
Definition at line 827 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 857 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 897 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 1091 of file setup.c.
| PetscErrorCode SetupBoundaryConditions | ( | SimCtx * | simCtx | ) |
(Orchestrator) Sets up all boundary conditions for the simulation.
Definition at line 1354 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 1411 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 1456 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 1513 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 1571 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 PhysicalGlobalNodesInDim - 2.
+1 node is a "user-defined ghost" not meant for physical calculations. It will internally subtract 1 from the global dimensions (mx, my, mz) before calculating the number of cells.| [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. |
| [out] | xm_cell_local_out | Pointer to store the number of cells for which this rank owns the origin node. |
Definition at line 1663 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 1770 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 1897 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 1981 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 2084 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 2242 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 2318 of file setup.c.
|
static |
Definition at line 2358 of file setup.c.
| PetscErrorCode ComputeDivergence | ( | UserCtx * | user | ) |
Definition at line 2377 of file setup.c.
| PetscErrorCode InitializeRandomGenerators | ( | UserCtx * | user, |
| PetscRandom * | randx, | ||
| PetscRandom * | randy, | ||
| PetscRandom * | randz | ||
| ) |
Initializes random number generators for assigning particle properties.
This function creates and configures separate PETSc random number generators for the x, y, and z coordinates.
| [in,out] | user | Pointer to the UserCtx structure containing simulation context. |
| [out] | randx | Pointer to store the RNG for the x-coordinate. |
| [out] | randy | Pointer to store the RNG for the y-coordinate. |
| [out] | randz | Pointer to store the RNG for the z-coordinate. |
Definition at line 2549 of file setup.c.
| PetscErrorCode InitializeLogicalSpaceRNGs | ( | PetscRandom * | rand_logic_i, |
| PetscRandom * | rand_logic_j, | ||
| PetscRandom * | rand_logic_k | ||
| ) |
Initializes random number generators for logical space operations [0.0, 1.0).
This function creates and configures three separate PETSc random number generators, one for each logical dimension (i, j, k or xi, eta, zeta equivalent). Each RNG is configured to produce uniformly distributed real numbers in the interval [0.0, 1.0). These are typically used for selecting owned cells or generating intra-cell logical coordinates.
| [out] | rand_logic_i | Pointer to store the RNG for the i-logical dimension. |
| [out] | rand_logic_j | Pointer to store the RNG for the j-logical dimension. |
| [out] | rand_logic_k | Pointer to store the RNG for the k-logical dimension. |
Definition at line 2600 of file setup.c.
|
static |
Transforms derivatives from computational space to physical space using the chain rule.
Definition at line 2643 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 | The user context for the current computational block. |
| i,j,k | The grid indices of the cell center where derivatives are required. |
| field_data | A 3D array pointer to the raw local data of the vector field (e.g., from lUcat). |
| dudx | Output: A Cmpnts struct storing [du/dx, du/dy, du/dz]. |
| dvdx | Output: A Cmpnts struct storing [dv/dx, dv/dy, dv/dz]. |
| dwdx | Output: A Cmpnts struct storing [dw/dx, dw/dy, dw/dz]. |
Definition at line 2678 of file setup.c.