PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Functions
initialcondition.h File Reference
#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 "ParticleSwarm.h"
#include "walkingsearch.h"
#include "grid.h"
#include "logging.h"
#include "io.h"
#include "interpolation.h"
#include "AnalyticalSolutions.h"
#include "ParticleMotion.h"
#include "Boundaries.h"
#include "runloop.h"
Include dependency graph for initialcondition.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PetscErrorCode PopulateInitialUcont (UserCtx *user)
 Populate Ucont for one fresh-start block from the initial source (-ic_source_*).
 
PetscErrorCode InitializeEulerianState (SimCtx *simCtx)
 High-level orchestrator to set the complete initial state of the Eulerian solver.
 

Function Documentation

◆ PopulateInitialUcont()

PetscErrorCode PopulateInitialUcont ( UserCtx *  user)

Populate Ucont for one fresh-start block from the initial source (-ic_source_*).

A staged field, when there is one, is read first: Ucat, or Ucont. The source's expressions then add their flux at every owned face. With -ic_source_field_weight w the staged field is scaled by w, and with a bulk velocity the expressions are scaled by PicurvSourceCellBulkFactor(). Collective.

Parameters
[in,out]userBlock context whose velocity field is populated.
Returns
PETSc error code.

Populate Ucont for one fresh-start block from the initial source (-ic_source_*).

See also
PopulateInitialUcont()

Definition at line 53 of file initialcondition.c.

54{
55 SimCtx *simCtx = user->simCtx;
56 const PetscBool staged_flux = (PetscBool)(simCtx->initialConditionStaged && simCtx->initialConditionField == IC_FIELD_UCONT);
58 PicurvExpression *pressure = NULL;
59 PetscReal physical_time, weight = 1.0, factor = 1.0;
60 Vec fraction, length;
61
62 PetscFunctionBeginUser;
64 PetscCall(PicurvVelocitySourceCreate("ic_source", PETSC_FALSE, simCtx->initialConditionStaged, PETSC_TRUE, &source));
65 PetscCall(PicurvPhysicalTime(simCtx, simCtx->ti, &physical_time));
66 PetscCall(VecSet(user->Ucat, 0.0));
67 PetscCall(VecSet(user->Ucont, 0.0));
68 if (simCtx->initialConditionStaged) {
69 PetscCall(PetscOptionsGetReal(NULL, NULL, "-ic_source_field_weight", &weight, NULL));
70 PetscCall(ReadStagedInitialField(user, staged_flux ? "vfield00000_0" : "ufield00000_0", staged_flux ? user->Ucont : user->Ucat));
71 if (weight != 1.0) PetscCall(VecScale(staged_flux ? user->Ucont : user->Ucat, weight));
72 if (!staged_flux) PetscCall(UcontFromInteriorUcat(user));
73 }
74 PetscCall(PicurvSourcePressureCreate("ic_source", PETSC_TRUE, &pressure));
75 if (source.count > 0 || pressure) {
76 PetscCall(DMCreateGlobalVector(user->fda, &fraction));
77 PetscCall(DMCreateGlobalVector(user->fda, &length));
78 PetscCall(ComputeGridLineFractions(user, fraction, length));
79 if (source.bulk) PetscCall(PicurvSourceCellBulkFactor(user, &source, fraction, length, physical_time, &factor));
80 if (source.count > 0) PetscCall(PicurvSourceAddFluxes(user, &source, fraction, length, physical_time, factor, PETSC_FALSE));
81 PetscCall(PicurvSourceSetCellFields(user, NULL, 1.0, pressure, fraction, length, physical_time));
82 PetscCall(VecDestroy(&fraction));
83 PetscCall(VecDestroy(&length));
84 }
85 PetscCall(PicurvExpressionDestroy(&pressure));
86 PetscCall(PicurvVelocitySourceDestroy(&source));
88 PetscFunctionReturn(0);
89}
PetscErrorCode ComputeGridLineFractions(UserCtx *user, Vec fraction, Vec length)
Arc-length position of every owned cell centre along its three grid lines.
Definition Metric.c:1362
PicurvExpression * bulk
Target mean speed [m/s] in t, or NULL.
Definition expression.h:192
PetscErrorCode PicurvExpressionDestroy(PicurvExpression **expression)
Free a compiled expression.
Definition expression.c:818
PetscErrorCode PicurvSourceAddFluxes(UserCtx *user, const PicurvVelocitySource *source, Vec fraction, Vec length, PetscReal physical_time, PetscReal factor, PetscBool boundary_faces)
Add a source's flux to every owned face of Ucont, evaluating it at each face centre.
PetscErrorCode PicurvVelocitySourceCreate(const char *prefix, PetscBool on_face, PetscBool has_data, PetscBool allow_draws, PicurvVelocitySource *source)
Read and compile a velocity source from the options database.
Definition expression.c:872
PetscErrorCode PicurvSourceCellBulkFactor(UserCtx *user, const PicurvVelocitySource *source, Vec fraction, Vec length, PetscReal physical_time, PetscReal *factor)
Factor on a source's expressions that makes the volume mean of the whole field, along the source's ow...
PetscErrorCode PicurvVelocitySourceDestroy(PicurvVelocitySource *source)
Free a source's compiled expressions and bulk velocity.
PetscErrorCode PicurvSourcePressureCreate(const char *prefix, PetscBool allow_draws, PicurvExpression **pressure)
Read and compile a source's optional pressure, -<prefix>_pressure, an expression in Pa over the cell ...
Definition expression.c:925
PetscErrorCode PicurvSourceSetCellFields(UserCtx *user, const PicurvVelocitySource *velocity, PetscReal factor, const PicurvExpression *pressure, Vec fraction, Vec length, PetscReal physical_time)
Set a block's Ucat, its P, or both at every interior cell centre this rank owns, from a source evalua...
PetscInt count
1 for the normal frame, otherwise 3; 0 for staged data alone.
Definition expression.h:189
A prescribed velocity, for any target: one compiled expression per value, the frame they are in,...
Definition expression.h:187
static PetscErrorCode UcontFromInteriorUcat(UserCtx *user)
Derive Ucont from a Ucat whose interior cells are set: sync the periodic and ghost cells,...
static PetscErrorCode ReadStagedInitialField(UserCtx *user, const char *name, Vec target)
Read one staged initial field (ufield00000_0 or vfield00000_0) from the initial-condition directory i...
PetscErrorCode PicurvPhysicalTime(const SimCtx *simCtx, PetscReal solver_time, PetscReal *physical)
Convert a solver time to physical seconds, t * L_ref / U_ref.
Definition io.c:2986
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:1029
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
Vec Ucont
Definition variables.h:1197
PetscBool initialConditionStaged
Definition variables.h:977
@ IC_FIELD_UCONT
Definition variables.h:203
InitialConditionField initialConditionField
Definition variables.h:978
PetscReal ti
Definition variables.h:925
The master context for the entire simulation.
Definition variables.h:916
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitializeEulerianState()

PetscErrorCode InitializeEulerianState ( SimCtx *  simCtx)

High-level orchestrator to set the complete initial state of the Eulerian solver.

This function is called once from main() before the time loop begins. It inspects the simulation context to determine whether to perform a fresh start (t=0) or restart from saved files. It then delegates to the appropriate helper function. Finally, it initializes the solver's history vectors (Ucont_o, P_o, etc.) to ensure the first time step has the necessary data.

Parameters
simCtxSimulation context controlling the operation.
Returns
PetscErrorCode 0 on success.

High-level orchestrator to set the complete initial state of the Eulerian solver.

See also
InitializeEulerianState()

Definition at line 242 of file initialcondition.c.

243{
244 PetscErrorCode ierr;
245 UserCtx *user_finest = simCtx->usermg.mgctx[simCtx->usermg.mglevels - 1].user;
246
247 PetscFunctionBeginUser;
248
250
251 LOG_ALLOW(GLOBAL, LOG_INFO, "--- Initializing Eulerian State ---\n");
252
253 if (simCtx->StartStep > 0) {
254 if(strcmp(simCtx->eulerianSource,"analytical")==0){
255 LOG_ALLOW(GLOBAL,LOG_INFO,"Initializing analytical field from its source (t=%.4f, step=%" PetscInt_FMT ").\n",simCtx->StartTime,simCtx->StartStep);
256 ierr = AnalyticalSolutionEngine(simCtx); CHKERRQ(ierr);
257 }
258 else{
259 LOG_ALLOW(GLOBAL, LOG_INFO, "Starting from RESTART files (t=%.4f, step=%" PetscInt_FMT ").\n",
260 simCtx->StartTime, simCtx->StartStep);
261 ierr = SetInitialFluidState_Load(simCtx); CHKERRQ(ierr);
262 }
263 /* Statistics resume from the same bundle the flow state came from, and are
264 * restored regardless of the Eulerian source: an analytical restart still
265 * continues a window that was accumulating before it. */
266 ierr = RestoreFieldStatisticsState(simCtx, simCtx->StartStep); CHKERRQ(ierr);
267 } else { // StartStep = 0
268 LOG_ALLOW(GLOBAL, LOG_INFO, "Performing a FRESH START (t=0, step=0).\n");
269 if(strcmp(simCtx->eulerianSource,"solve")==0){
270 ierr = SetInitialFluidState_FreshStart(simCtx); CHKERRQ(ierr);
271 }else if(strcmp(simCtx->eulerianSource,"load")==0){
272 LOG_ALLOW(GLOBAL,LOG_INFO,"FRESH START in LOAD mode. Reading files (t=%.4f,step=%" PetscInt_FMT ").\n",
273 simCtx->StartTime,simCtx->StartStep);
274 ierr=SetInitialFluidState_Load(simCtx);CHKERRQ(ierr);
275 }else if(strcmp(simCtx->eulerianSource,"analytical")==0){
276 LOG_ALLOW(GLOBAL,LOG_INFO,"FRESH START in ANALYTICAL mode (t=%.4f,step=%" PetscInt_FMT ").\n",
277 simCtx->StartTime,simCtx->StartStep);
278 ierr=AnalyticalSolutionEngine(simCtx);CHKERRQ(ierr);
279 }
280 }
281
282 // This crucial step, taken from the end of the legacy setup, ensures
283 // that the history vectors (Ucont_o, Ucont_rm1, etc.) are correctly
284 // populated before the first call to the time-stepping loop.
285 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
286 ierr = UpdateSolverHistoryVectors(&user_finest[bi],
287 (PetscBool)(simCtx->StartStep > 0 && simCtx->restartHistoryAvailable)); CHKERRQ(ierr);
288 }
289
290 LOG_ALLOW(GLOBAL, LOG_INFO, "--- Eulerian State Initialized and History Vectors Populated ---\n");
291
293 PetscFunctionReturn(0);
294}
PetscErrorCode AnalyticalSolutionEngine(SimCtx *simCtx)
Sets the Eulerian fields from the case's source (-ic_source_*) at the step's time.
static PetscErrorCode SetInitialFluidState_Load(SimCtx *simCtx)
Restore Eulerian fields from checkpoint files for a restart simulation.
static PetscErrorCode SetInitialFluidState_FreshStart(SimCtx *simCtx)
Initialize Eulerian fields for a new simulation without restart data.
PetscErrorCode RestoreFieldStatisticsState(SimCtx *simCtx, PetscInt ti)
Restores field-statistics window state and accumulators from a checkpoint.
Definition io.c:1604
#define GLOBAL
Scope for global logging across all processes.
Definition logging.h:46
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
Definition logging.h:214
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:31
PetscErrorCode UpdateSolverHistoryVectors(UserCtx *user, PetscBool preserve_previous_state)
Copies the current time step's solution fields into history vectors (e.g., U(t_n) -> U_o,...
Definition runloop.c:376
UserCtx * user
Definition variables.h:768
PetscInt block_number
Definition variables.h:1009
PetscReal StartTime
Definition variables.h:932
UserMG usermg
Definition variables.h:1082
PetscInt StartStep
Definition variables.h:926
char eulerianSource[PETSC_MAX_PATH_LEN]
Definition variables.h:938
PetscInt mglevels
Definition variables.h:775
MGCtx * mgctx
Definition variables.h:778
PetscBool restartHistoryAvailable
Definition variables.h:947
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
Here is the call graph for this function:
Here is the caller graph for this function: