PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
Boundaries.h
Go to the documentation of this file.
1#ifndef BOUNDARIES_H
2#define BOUNDARIES_H
3
4#include <petscpf.h>
5#include <petscdmswarm.h>
6#include <stdlib.h>
7#include <time.h>
8#include <math.h>
9#include <petsctime.h>
10#include <petscsys.h>
11#include <petscdmcomposite.h>
12#include <petscsystypes.h>
13
14// Include additional headers
15#include "variables.h" // Shared type definitions
16#include "ParticleSwarm.h" // Particle swarm functions
17#include "walkingsearch.h" // Particle location functions
18#include "grid.h" // Grid functions
19#include "logging.h" // Logging macros
20#include "io.h" // Data Input and Output functions
21#include "interpolation.h" // Interpolation routines
22#include "AnalyticalSolution.h" // Analytical Solution for testing
23#include "ParticleMotion.h" // Functions related to motion of particles
24#include "BC_Handlers.h" // Boundary Handlers
25#include "wallfunction.h" // wall functions for LES
26//================================================================================
27//
28// PUBLIC SYSTEM-LEVEL FUNCTIONS
29//
30// These are the main entry points for interacting with the boundary system.
31//
32//================================================================================
33
34/**
35 * @brief Initializes the entire boundary system.
36 * @param user The main UserCtx struct.
37 * @param bcs_filename The path to the boundary conditions configuration file.
38 */
39PetscErrorCode BoundarySystem_Create(UserCtx *user, const char *bcs_filename);
40
41/**
42 * @brief Executes one full boundary condition update cycle for a time step.
43 * @param user The main UserCtx struct.
44 */
45PetscErrorCode BoundarySystem_ExecuteStep(UserCtx *user);
46
47/**
48 * @brief Cleans up and destroys all boundary system resources.
49 * @param user The main UserCtx struct.
50 */
51PetscErrorCode BoundarySystem_Destroy(UserCtx *user);
52
53/**
54 * @brief Determines if the current MPI rank owns any part of the globally defined inlet face,
55 * making it responsible for placing particles on that portion of the surface.
56 *
57 * The determination is based on the rank's owned nodes (from `DMDALocalInfo`) and
58 * the global node counts, in conjunction with the `user->identifiedInletBCFace`.
59 * A rank can service an inlet face if it owns the cells adjacent to that global boundary
60 * and has a non-zero extent (owns cells) in the tangential dimensions of that face.
61 *
62 * @param user Pointer to the UserCtx structure, containing `identifiedInletBCFace`.
63 * @param info Pointer to the DMDALocalInfo for the current rank's DA (node-based).
64 * @param IM_nodes_global Global number of nodes in the I-direction (e.g., user->IM + 1 if user->IM is cell count).
65 * @param JM_nodes_global Global number of nodes in the J-direction.
66 * @param KM_nodes_global Global number of nodes in the K-direction.
67 * @param[out] can_service_inlet_out Pointer to a PetscBool; set to PETSC_TRUE if the rank
68 * services (part of) the inlet, PETSC_FALSE otherwise.
69 * @return PetscErrorCode 0 on success, non-zero on failure.
70 */
71PetscErrorCode CanRankServiceInletFace(UserCtx *user, const DMDALocalInfo *info,
72 PetscInt IM_nodes_global, PetscInt JM_nodes_global, PetscInt KM_nodes_global,
73 PetscBool *can_service_inlet_out);
74
75/**
76 * @brief Determines if the current MPI rank owns any part of a specified global face.
77 *
78 * This function is a general utility for parallel boundary operations. It checks if the
79 * local domain of the current MPI rank is adjacent to a specified global boundary face.
80 * A rank "services" a face if it owns the cells adjacent to that global boundary and has
81 * a non-zero extent (i.e., owns at least one cell) in the tangential dimensions of that face.
82 *
83 * @param info Pointer to the DMDALocalInfo for the current rank's DA.
84 * @param IM_nodes_global Global number of nodes in the I-direction (e.g., user->IM + 1 if user->IM is cell count).
85 * @param JM_nodes_global Global number of nodes in the J-direction.
86 * @param KM_nodes_global Global number of nodes in the K-direction.
87 * @param face_id The specific global face (e.g., BC_FACE_NEG_Z) to check.
88 * @param[out] can_service_out Pointer to a PetscBool; set to PETSC_TRUE if the rank
89 * services the face, PETSC_FALSE otherwise.
90 * @return PetscErrorCode 0 on success.
91 */
92PetscErrorCode CanRankServiceFace(const DMDALocalInfo *info, PetscInt IM_nodes_global, PetscInt JM_nodes_global, PetscInt KM_nodes_global,
93 BCFace face_id, PetscBool *can_service_out);
94
95/**
96 * @brief Places particles in a deterministic grid/raster pattern on a specified domain face.
97 *
98 * This function creates a set of equidistant, parallel lines of particles near the four
99 * edges of the face specified by user->identifiedInletBCFace. The number of lines drawn
100 * from each edge is hardcoded within this function (default is 2).
101 *
102 * For example, if grid_layers=2 on face BC_FACE_NEG_X, the function will create particle lines at:
103 * - y ~ 0*dy, y ~ 1*dy (parallel to the Z-axis, starting from the J=0 edge)
104 * - y ~ y_max, y ~ y_max-dy (parallel to the Z-axis, starting from the J=max edge)
105 * - z ~ 0*dz, z ~ 1*dz (parallel to the Y-axis, starting from the K=0 edge)
106 * - z ~ z_max, z ~ z_max-dz (parallel to the Y-axis, starting from the K=max edge)
107 *
108 * The particle's final position is set just inside the target cell face to ensure it is
109 * correctly located. The total number of particles (simCtx->np) is distributed as evenly
110 * as possible among all generated lines.
111 *
112 * The function includes extensive validation to stop with an error if the requested grid
113 * placement is geometrically impossible (e.g., in a 2D domain or if layers would overlap).
114 * It also issues warnings for non-fatal but potentially unintended configurations.
115 *
116 * @param user Pointer to UserCtx, which must contain a valid identifiedInletBCFace.
117 * @param info Pointer to DMDALocalInfo for the current rank's grid layout.
118 * @param xs_gnode_rank, ys_gnode_rank, zs_gnode_rank Local starting node indices (incl. ghosts) for the rank's DA.
119 * @param IM_cells_global, JM_cells_global, KM_cells_global Global cell counts.
120 * @param particle_global_id The unique global ID of the particle being placed (from 0 to np-1).
121 * @param[out] ci_metric_lnode_out Local I-node index of the selected cell's origin.
122 * @param[out] cj_metric_lnode_out Local J-node index of the selected cell's origin.
123 * @param[out] ck_metric_lnode_out Local K-node index of the selected cell's origin.
124 * @param[out] xi_metric_logic_out Logical xi-coordinate [0,1] within the cell.
125 * @param[out] eta_metric_logic_out Logical eta-coordinate [0,1] within the cell.
126 * @param[out] zta_metric_logic_out Logical zta-coordinate [0,1] within the cell.
127 * @param[out] placement_successful_out PETSC_TRUE if the point belongs to this rank, PETSC_FALSE otherwise.
128 * @return PetscErrorCode
129 */
131 UserCtx *user, const DMDALocalInfo *info,
132 PetscInt xs_gnode_rank, PetscInt ys_gnode_rank, PetscInt zs_gnode_rank,
133 PetscInt IM_cells_global, PetscInt JM_cells_global, PetscInt KM_cells_global,
134 PetscInt64 particle_global_id,
135 PetscInt *ci_metric_lnode_out, PetscInt *cj_metric_lnode_out, PetscInt *ck_metric_lnode_out,
136 PetscReal *xi_metric_logic_out, PetscReal *eta_metric_logic_out, PetscReal *zta_metric_logic_out,
137 PetscBool *placement_successful_out);
138
139
140/**
141 * @brief Assuming the current rank services the inlet face, this function selects a random
142 * cell (owned by this rank on that face) and random logical coordinates within that cell,
143 * suitable for placing a particle on the inlet surface.
144 *
145 * It is the caller's responsibility to ensure CanRankServiceInletFace returned true.
146 *
147 * @param user Pointer to UserCtx.
148 * @param info Pointer to DMDALocalInfo for the current rank (node-based).
149 * @param xs_gnode, ys_gnode, zs_gnode Local starting node indices (incl. ghosts) for the rank's DA.
150 * @param IM_nodes_global, JM_nodes_global, KM_nodes_global Global node counts.
151 * @param rand_logic_i_ptr, rand_logic_j_ptr, rand_logic_k_ptr Pointers to RNGs for logical coords.
152 * @param[out] ci_metric_lnode_out, cj_metric_lnode_out, ck_metric_lnode_out Local node indices of the selected cell's origin (these are local to the rank's DA including ghosts).
153 * @param[out] xi_metric_logic_out, eta_metric_logic_out, zta_metric_logic_out Logical coords [0,1] within the cell.
154 * @return PetscErrorCode
155 */
157 UserCtx *user, const DMDALocalInfo *info,
158 PetscInt xs_gnode_rank, PetscInt ys_gnode_rank, PetscInt zs_gnode_rank, // Local starting node index (with ghosts) of the rank's DA patch
159 PetscInt IM_nodes_global, PetscInt JM_nodes_global, PetscInt KM_nodes_global,
160 PetscRandom *rand_logic_i_ptr, PetscRandom *rand_logic_j_ptr, PetscRandom *rand_logic_k_ptr,
161 PetscInt *ci_metric_lnode_out, PetscInt *cj_metric_lnode_out, PetscInt *ck_metric_lnode_out,
162 PetscReal *xi_metric_logic_out, PetscReal *eta_metric_logic_out, PetscReal *zta_metric_logic_out);
163
164PetscErrorCode TranslateModernBCsToLegacy(UserCtx *user);
165
166/**
167 * @brief Applies inlet boundary conditions based on the modern BC handling system.
168 *
169 * This function iterates through all 6 domain faces. For each face identified as an
170 * INLET, it applies the velocity profile specified by its assigned handler and
171 * parameters (e.g., 'constant_velocity' with vx,vy,vz or 'parabolic' with u_max).
172 *
173 * It calculates the contravariant flux (Ucont), Cartesian velocity on the face (Ubcs),
174 * and the staggered Cartesian velocity (Ucat). It also computes the total incoming
175 * flux and area across all MPI ranks.
176 *
177 * @param user The main UserCtx struct containing the BC configuration and PETSc objects.
178 * @return PetscErrorCode 0 on success.
179 */
180PetscErrorCode InflowFlux(UserCtx *user);
181
182/**
183 * @brief Calculates the total outgoing flux through all OUTLET faces for reporting.
184 *
185 * NOTE: In a mixed modern/legacy environment, this function is for DIAGNOSTICS ONLY.
186 * It reads the contravariant velocities and calculates the total flux passing through
187 * faces marked as OUTLET. It does NOT apply any boundary conditions itself, as that
188 * is still the responsibility of the legacy FormBCS function.
189 *
190 * @param user The main UserCtx struct containing BC config and PETSc vectors.
191 * @return PetscErrorCode 0 on success.
192 */
193PetscErrorCode OutflowFlux(UserCtx *user);
194
195// TO BE FIXED
196PetscErrorCode FormBCS(UserCtx *user);
197
198/**
199 * @brief Acts as a temporary bridge to the legacy boundary condition implementation.
200 *
201 * This function is the key to our integration strategy. It matches the signature
202 * of the modern `BoundarySystem_ExecuteStep` function that SetEulerianFields
203 * expects to call.
204 *
205 * However, instead of containing new handler-based logic, it simply calls the
206 * monolithic legacy `FormBCS` function. This allows the modern orchestrator to
207 * drive the old solver logic without modification.
208 *
209 * @param user The UserCtx for a single block.
210 * @return PetscErrorCode
211 */
212PetscErrorCode BoundarySystem_ExecuteStep_Legacy(UserCtx *user);
213
214#endif // BOUNDARIES_H
PetscErrorCode GetRandomCellAndLogicalCoordsOnInletFace(UserCtx *user, const DMDALocalInfo *info, PetscInt xs_gnode_rank, PetscInt ys_gnode_rank, PetscInt zs_gnode_rank, PetscInt IM_nodes_global, PetscInt JM_nodes_global, PetscInt KM_nodes_global, PetscRandom *rand_logic_i_ptr, PetscRandom *rand_logic_j_ptr, PetscRandom *rand_logic_k_ptr, PetscInt *ci_metric_lnode_out, PetscInt *cj_metric_lnode_out, PetscInt *ck_metric_lnode_out, PetscReal *xi_metric_logic_out, PetscReal *eta_metric_logic_out, PetscReal *zta_metric_logic_out)
Assuming the current rank services the inlet face, this function selects a random cell (owned by this...
Definition Boundaries.c:459
PetscErrorCode BoundarySystem_ExecuteStep_Legacy(UserCtx *user)
Acts as a temporary bridge to the legacy boundary condition implementation.
Definition Boundaries.c:673
PetscErrorCode OutflowFlux(UserCtx *user)
Calculates the total outgoing flux through all OUTLET faces for reporting.
Definition Boundaries.c:932
PetscErrorCode BoundarySystem_Create(UserCtx *user, const char *bcs_filename)
Initializes the entire boundary system.
PetscErrorCode CanRankServiceInletFace(UserCtx *user, const DMDALocalInfo *info, PetscInt IM_nodes_global, PetscInt JM_nodes_global, PetscInt KM_nodes_global, PetscBool *can_service_inlet_out)
Determines if the current MPI rank owns any part of the globally defined inlet face,...
Definition Boundaries.c:25
PetscErrorCode InflowFlux(UserCtx *user)
Applies inlet boundary conditions based on the modern BC handling system.
Definition Boundaries.c:706
PetscErrorCode CanRankServiceFace(const DMDALocalInfo *info, PetscInt IM_nodes_global, PetscInt JM_nodes_global, PetscInt KM_nodes_global, BCFace face_id, PetscBool *can_service_out)
Determines if the current MPI rank owns any part of a specified global face.
Definition Boundaries.c:150
PetscErrorCode GetDeterministicFaceGridLocation(UserCtx *user, const DMDALocalInfo *info, PetscInt xs_gnode_rank, PetscInt ys_gnode_rank, PetscInt zs_gnode_rank, PetscInt IM_cells_global, PetscInt JM_cells_global, PetscInt KM_cells_global, PetscInt64 particle_global_id, PetscInt *ci_metric_lnode_out, PetscInt *cj_metric_lnode_out, PetscInt *ck_metric_lnode_out, PetscReal *xi_metric_logic_out, PetscReal *eta_metric_logic_out, PetscReal *zta_metric_logic_out, PetscBool *placement_successful_out)
Places particles in a deterministic grid/raster pattern on a specified domain face.
Definition Boundaries.c:267
PetscErrorCode BoundarySystem_ExecuteStep(UserCtx *user)
Executes one full boundary condition update cycle for a time step.
PetscErrorCode BoundarySystem_Destroy(UserCtx *user)
Cleans up and destroys all boundary system resources.
PetscErrorCode FormBCS(UserCtx *user)
PetscErrorCode TranslateModernBCsToLegacy(UserCtx *user)
Definition Boundaries.c:635
Header file for Particle Motion and migration related functions.
Header file for Particle Swarm management functions.
Public interface for grid, solver, and metric setup routines.
Public interface for data input/output routines.
Logging utilities and macros for PETSc-based applications.
Main header file for a complex fluid dynamics solver.
BCFace
Identifies the six logical faces of a structured computational block.
Definition variables.h:200
User-defined context containing data specific to a single computational grid level.
Definition variables.h:661
Header file for particle location functions using the walking search algorithm.