PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
AnalyticalSolutions.c
Go to the documentation of this file.
1/**
2 * @file AnalyticalSolutions.c
3 * @brief Prescribes the Eulerian fields from the case's source instead of solving for them.
4 *
5 * @details With `eulerian_field_source: analytical`, the field is the initial condition's source,
6 * evaluated at the step's time through the operations every target of a source shares: fluxes at
7 * the faces (PicurvSourceAddFluxes()) and a bulk velocity (PicurvSourceCellBulkFactor()). A Cartesian
8 * source also sets `Ucat` and an optional pressure at the cell centres, exactly; a curvilinear one
9 * derives `Ucat` from its fluxes.
10 *
11 * The boundary values are this module's own `Ubcs`, so after setting the state it runs the part of
12 * the solved flow's finalization that does not refresh them from the boundary handlers
13 * (FinalizeCellFieldsFromBoundaryValues()).
14 */
15
16#include "AnalyticalSolutions.h"
17#include "expression.h"
18#include "Boundaries.h"
19#include "Metric.h"
20#include "logging.h"
21#include "setup.h"
22
23/** @brief The prescribed field: the case's source, and what it reuses from step to step. */
25 PicurvVelocitySource source; /* `-ic_source_*`. */
26 PicurvExpression *pressure; /* `-ic_source_pressure` [Pa], or NULL for zero pressure. */
27 PetscBool time_dependent;
28 PetscBool pointwise; /* Reads only x, y, z, t in the Cartesian frame, and varies in space. */
29 PetscBool evaluated;
30 PetscReal factor; /* From the bulk velocity. */
31 Vec *fraction; /* Per block. */
32 Vec *length;
33 PicurvSourceFaceSlots (*faces)[6]; /* Per block, what the source reads on each face. */
34};
35
36/** @brief Read the source once, and work out what it depends on. Collective. */
37static PetscErrorCode AnalyticalFieldCreate(SimCtx *simCtx)
38{
39 UserCtx *user_finest = simCtx->usermg.mgctx[simCtx->usermg.mglevels - 1].user;
40 struct AnalyticalField *field;
41 PetscBool uses;
42
43 PetscFunctionBeginUser;
45 PetscCall(PetscNew(&field));
46 PetscCall(PicurvVelocitySourceCreate("ic_source", PETSC_FALSE, PETSC_FALSE, PETSC_FALSE, &field->source));
47 PetscCall(PicurvSourcePressureCreate("ic_source", PETSC_FALSE, &field->pressure));
48 if (field->pressure) {
50 field->time_dependent = uses;
51 }
52 field->time_dependent = (PetscBool)(field->time_dependent || field->source.time_dependent ||
54 field->pointwise = (PetscBool)(field->source.frame == PICURV_SOURCE_FRAME_CARTESIAN && !field->source.bulk);
55 {
56 PetscBool varies = PETSC_FALSE;
57 for (PetscInt c = 0; c < field->source.count; ++c) {
58 for (PetscInt var = 0; var < PICURV_SOURCE_VAR_COUNT; ++var) {
59 PetscCall(PicurvExpressionUsesVariable(field->source.component[c], var, &uses));
60 if (!uses) continue;
61 if (var >= PICURV_SOURCE_VAR_XI) field->pointwise = PETSC_FALSE;
62 if (var <= PICURV_SOURCE_VAR_Z) varies = PETSC_TRUE;
63 }
64 }
65 field->pointwise = (PetscBool)(field->pointwise && varies);
66 }
67 field->factor = 1.0;
68 PetscCall(PetscCalloc3(simCtx->block_number, &field->fraction, simCtx->block_number, &field->length,
69 simCtx->block_number, &field->faces));
70 for (PetscInt bi = 0; bi < simCtx->block_number; ++bi) {
71 PetscCall(DMCreateGlobalVector(user_finest[bi].fda, &field->fraction[bi]));
72 PetscCall(DMCreateGlobalVector(user_finest[bi].fda, &field->length[bi]));
73 PetscCall(ComputeGridLineFractions(&user_finest[bi], field->fraction[bi], field->length[bi]));
74 for (PetscInt face = 0; face < 6; ++face) {
75 PetscCall(PicurvSourceFaceSlotsCreate(&user_finest[bi], (BCFace)face, field->fraction[bi], field->length[bi],
76 &field->faces[bi][face]));
77 }
78 }
79 simCtx->analyticalField = field;
81 PetscFunctionReturn(0);
82}
83
84/**
85 * @brief Implementation of \ref AnalyticalFieldDestroy().
86 * @see AnalyticalFieldDestroy()
87 */
88PetscErrorCode AnalyticalFieldDestroy(SimCtx *simCtx)
89{
90 struct AnalyticalField *field = simCtx->analyticalField;
91
92 PetscFunctionBeginUser;
94 if (field) {
95 PetscCall(PicurvVelocitySourceDestroy(&field->source));
96 PetscCall(PicurvExpressionDestroy(&field->pressure));
97 for (PetscInt bi = 0; bi < simCtx->block_number; ++bi) {
98 PetscCall(VecDestroy(&field->fraction[bi]));
99 PetscCall(VecDestroy(&field->length[bi]));
100 for (PetscInt face = 0; face < 6; ++face) PetscCall(PicurvSourceFaceSlotsDestroy(&field->faces[bi][face]));
101 }
102 PetscCall(PetscFree3(field->fraction, field->length, field->faces));
103 PetscCall(PetscFree(simCtx->analyticalField));
104 }
106 PetscFunctionReturn(0);
107}
108
109/**
110 * @brief Implementation of \ref AnalyticalFieldSupportsInterpolationError().
111 * @see AnalyticalFieldSupportsInterpolationError()
112 */
114{
116 const PetscBool pointwise = (simCtx && simCtx->analyticalField) ? simCtx->analyticalField->pointwise : PETSC_FALSE;
118 return pointwise;
119}
120
121/** @brief Set Ubcs on every physical boundary face to the source at the face centre. */
122static PetscErrorCode SetBoundaryVelocityFromSource(UserCtx *user, const struct AnalyticalField *field, PetscInt block,
123 PetscReal physical_time)
124{
125 const PetscReal U_ref = user->simCtx->scaling.U_ref;
126 Cmpnts ***ubcs;
127
128 PetscFunctionBeginUser;
130 PetscCall(DMDAVecGetArray(user->fda, user->Bcs.Ubcs, &ubcs));
131 for (PetscInt face = 0; face < 6; ++face) {
132 const PicurvSourceFaceSlots *slots = &field->faces[block][face];
133 for (PetscInt q = 0; q < slots->elements.n[1]; ++q)
134 for (PetscInt p = 0; p < slots->elements.n[0]; ++p) {
135 const PetscInt s = p + slots->elements.n[0] * q;
136 PetscInt f[3];
137 Cmpnts v;
138 /* A + face's boundary value sits one past its flux, in the dummy layer. */
139 f[slots->elements.normal] = slots->elements.layer + face % 2; f[slots->elements.a] = slots->elements.lo[0] + p; f[slots->elements.b] = slots->elements.lo[1] + q;
140 slots->values[s][PICURV_SOURCE_VAR_T] = physical_time;
141 PetscCall(PicurvVelocitySourceEvaluate(&field->source, slots->values[s], NULL, slots->edges[s], &v));
142 ubcs[f[2]][f[1]][f[0]].x = field->factor * v.x / U_ref;
143 ubcs[f[2]][f[1]][f[0]].y = field->factor * v.y / U_ref;
144 ubcs[f[2]][f[1]][f[0]].z = field->factor * v.z / U_ref;
145 }
146 }
147 PetscCall(DMDAVecRestoreArray(user->fda, user->Bcs.Ubcs, &ubcs));
149 PetscFunctionReturn(0);
150}
151
152/**
153 * @brief Implementation of \ref AnalyticalSolutionEngine().
154 * @details The field is the case's source, evaluated at the step's time. As on an inlet, a source
155 * that does not read `t` is evaluated once.
156 * @see AnalyticalSolutionEngine()
157 */
158PetscErrorCode AnalyticalSolutionEngine(SimCtx *simCtx)
159{
160 UserCtx *user_finest = simCtx->usermg.mgctx[simCtx->usermg.mglevels - 1].user;
161 struct AnalyticalField *field;
162 PetscReal physical_time;
163
164 PetscFunctionBeginUser;
166 if (!simCtx->analyticalField) PetscCall(AnalyticalFieldCreate(simCtx));
167 field = simCtx->analyticalField;
168 if (field->evaluated && !field->time_dependent) { PROFILE_FUNCTION_END; PetscFunctionReturn(0); }
169 PetscCall(PicurvPhysicalTime(simCtx, simCtx->ti, &physical_time));
170 for (PetscInt bi = 0; bi < simCtx->block_number; ++bi) {
171 UserCtx *user = &user_finest[bi];
172 const FieldId staggered_fields[] = {FIELD_ID_UCONT};
173 if (field->source.bulk) {
174 PetscCall(VecSet(user->Ucat, 0.0));
175 PetscCall(PicurvSourceCellBulkFactor(user, &field->source, field->fraction[bi], field->length[bi], physical_time, &field->factor));
176 }
177 PetscCall(VecSet(user->Ucont, 0.0));
178 PetscCall(PicurvSourceAddFluxes(user, &field->source, field->fraction[bi], field->length[bi], physical_time, field->factor, PETSC_TRUE));
179 PetscCall(SetBoundaryVelocityFromSource(user, field, bi, physical_time));
180 /* A curvilinear source's Ucat follows from its fluxes, below. */
181 PetscCall(PicurvSourceSetCellFields(user, (field->source.frame != PICURV_SOURCE_FRAME_CURVILINEAR) ? &field->source : NULL,
182 field->factor, field->pressure, field->fraction[bi], field->length[bi], physical_time));
183 if (!field->pressure) PetscCall(VecSet(user->P, 0.0));
184 PetscCall(SynchronizePeriodicStaggeredFields(user, 1, staggered_fields));
185 PetscCall(UpdateLocalGhosts(user, FIELD_ID_UCONT));
186 if (field->source.frame == PICURV_SOURCE_FRAME_CURVILINEAR) PetscCall(Contra2Cart(user));
188 }
189 field->evaluated = PETSC_TRUE;
191 PetscFunctionReturn(0);
192}
193
194/**
195 * @brief Implementation of \ref SetAnalyticalSolutionForParticles().
196 * @details Only a pointwise source reaches here; see AnalyticalFieldSupportsInterpolationError().
197 * @see SetAnalyticalSolutionForParticles()
198 */
199PetscErrorCode SetAnalyticalSolutionForParticles(Vec tempVec, SimCtx *simCtx)
200{
201 const struct AnalyticalField *field = simCtx->analyticalField;
202 const ScalingCtx *scaling = &simCtx->scaling;
203 PetscInt nLocal;
204 PetscReal *data, values[PICURV_SOURCE_VAR_COUNT], physical_time;
205
206 PetscFunctionBeginUser;
208 PetscCheck(AnalyticalFieldSupportsInterpolationError(simCtx), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
209 "The analytical field cannot be evaluated at a particle: it needs a Cartesian source in x, y, z and t.");
210 PetscCall(PetscArrayzero(values, PICURV_SOURCE_VAR_COUNT));
211 PetscCall(PicurvPhysicalTime(simCtx, simCtx->ti, &physical_time));
212 values[PICURV_SOURCE_VAR_T] = physical_time;
213 PetscCall(VecGetLocalSize(tempVec, &nLocal));
214 PetscCall(VecGetArray(tempVec, &data));
215 /* Interleaved [x0,y0,z0, x1,y1,z1, ...], replaced by the velocity there. */
216 for (PetscInt i = 0; i < nLocal; i += 3) {
217 Cmpnts v;
218 values[PICURV_SOURCE_VAR_X] = data[i] * scaling->L_ref;
219 values[PICURV_SOURCE_VAR_Y] = data[i + 1] * scaling->L_ref;
220 values[PICURV_SOURCE_VAR_Z] = data[i + 2] * scaling->L_ref;
221 PetscCall(PicurvVelocitySourceEvaluate(&field->source, values, NULL, NULL, &v));
222 data[i] = v.x / scaling->U_ref;
223 data[i + 1] = v.y / scaling->U_ref;
224 data[i + 2] = v.z / scaling->U_ref;
225 }
226 PetscCall(VecRestoreArray(tempVec, &data));
228 PetscFunctionReturn(0);
229}
PetscErrorCode AnalyticalFieldDestroy(SimCtx *simCtx)
Implementation of AnalyticalFieldDestroy().
PicurvVelocitySource source
static PetscErrorCode SetBoundaryVelocityFromSource(UserCtx *user, const struct AnalyticalField *field, PetscInt block, PetscReal physical_time)
Set Ubcs on every physical boundary face to the source at the face centre.
static PetscErrorCode AnalyticalFieldCreate(SimCtx *simCtx)
Read the source once, and work out what it depends on.
PetscErrorCode AnalyticalSolutionEngine(SimCtx *simCtx)
Implementation of AnalyticalSolutionEngine().
PetscBool AnalyticalFieldSupportsInterpolationError(const SimCtx *simCtx)
Implementation of AnalyticalFieldSupportsInterpolationError().
PetscErrorCode SetAnalyticalSolutionForParticles(Vec tempVec, SimCtx *simCtx)
Implementation of SetAnalyticalSolutionForParticles().
PicurvExpression * pressure
PicurvSourceFaceSlots(* faces)[6]
The prescribed field: the case's source, and what it reuses from step to step.
PetscErrorCode FinalizeCellFieldsFromBoundaryValues(UserCtx *user)
Brings Ucat's and P's dummy cells, periodic endpoints, corners and ghosts in line with their interior...
PetscErrorCode SynchronizePeriodicStaggeredFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes persistent component-staggered vector fields.
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
The expression language shared by every configured field, the keyed hashes its random draws and other...
BoundaryFaceElements elements
The owned face elements the slots are.
Definition expression.h:317
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
PetscBool bulk_time_dependent
Definition expression.h:193
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 PicurvVelocitySourceEvaluate(const PicurvVelocitySource *source, const PetscReal *values, const PicurvExpressionDrawKey *key, const Cmpnts edges[3], Cmpnts *value)
Evaluate a source at one point.
Definition expression.c:993
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...
@ PICURV_SOURCE_VAR_XI
Arc-length fraction along the xi grid line, 0 at the - face.
Definition expression.h:163
@ PICURV_SOURCE_VAR_T
Physical time [s].
Definition expression.h:162
@ PICURV_SOURCE_VAR_Y
Physical y [m].
Definition expression.h:160
@ PICURV_SOURCE_VAR_X
Physical x of the point [m].
Definition expression.h:159
@ PICURV_SOURCE_VAR_COUNT
Definition expression.h:171
@ PICURV_SOURCE_VAR_Z
Physical z [m].
Definition expression.h:161
PetscErrorCode PicurvExpressionUsesVariable(const PicurvExpression *expression, PetscInt index, PetscBool *uses)
Whether a compiled expression reads one of its variables.
Definition expression.c:847
PetscErrorCode PicurvVelocitySourceDestroy(PicurvVelocitySource *source)
Free a source's compiled expressions and bulk velocity.
PicurvExpression * component[3]
Definition expression.h:190
PetscBool time_dependent
Some component reads t or draws, so a face re-evaluates it each step.
Definition expression.h:191
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...
PetscErrorCode PicurvSourceFaceSlotsCreate(UserCtx *user, BCFace face_id, Vec fraction, Vec length, PicurvSourceFaceSlots *slots)
Gather each owned slot of a boundary face: face-centre position, the grid-line fractions and lengths ...
PetscReal(* values)[PICURV_SOURCE_VAR_COUNT]
Variables per slot; t is the caller's.
Definition expression.h:318
PicurvSourceFrame frame
Definition expression.h:188
PetscErrorCode PicurvSourceFaceSlotsDestroy(PicurvSourceFaceSlots *slots)
Release what PicurvSourceFaceSlotsCreate() allocated.
Cmpnts(* edges)[3]
Directions per slot, for the curvilinear frame.
Definition expression.h:319
@ PICURV_SOURCE_FRAME_CARTESIAN
Three values: x, y, z velocity components.
Definition expression.h:176
@ PICURV_SOURCE_FRAME_CURVILINEAR
Three values along the unit xi, eta, zeta grid-line directions.
Definition expression.h:177
PetscInt count
1 for the normal frame, otherwise 3; 0 for staged data alone.
Definition expression.h:189
What a source reads at each boundary-face slot this rank owns, gathered once.
Definition expression.h:316
A prescribed velocity, for any target: one compiled expression per value, the frame they are in,...
Definition expression.h:187
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_UCONT
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
Logging utilities and macros for PETSc-based applications.
#define PROFILE_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
Definition logging.h:1014
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
Definition logging.h:1033
#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
PetscErrorCode Contra2Cart(UserCtx *user)
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on...
Definition setup.c:3313
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2593
UserCtx * user
Definition variables.h:768
struct AnalyticalField * analyticalField
The prescribed field's source, once read; see AnalyticalSolutionEngine().
Definition variables.h:952
PetscInt block_number
Definition variables.h:1009
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
UserMG usermg
Definition variables.h:1082
PetscReal L_ref
Definition variables.h:898
PetscInt layer
Index along the normal of the face's metric and flux.
Definition variables.h:418
PetscInt normal
Logical axis of the face normal.
Definition variables.h:416
Vec Ucont
Definition variables.h:1197
Vec Ubcs
Physical Cartesian velocity at boundary faces. Full 3D array but only boundary-face entries are meani...
Definition variables.h:172
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
ScalingCtx scaling
Definition variables.h:1003
PetscInt mglevels
Definition variables.h:775
PetscInt n[2]
Owned elements along a and b; 0 when this rank does not serve the face.
Definition variables.h:420
PetscScalar y
Definition variables.h:145
MGCtx * mgctx
Definition variables.h:778
PetscReal ti
Definition variables.h:925
PetscReal U_ref
Definition variables.h:899
PetscInt lo[2]
First owned indices along a and b.
Definition variables.h:419
PetscInt b
The two tangential axes, a < b.
Definition variables.h:417
BCFace
Identifies the six logical faces of a structured computational block.
Definition variables.h:308
A 3D point or vector with PetscScalar components.
Definition variables.h:144
The master context for the entire simulation.
Definition variables.h:916
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157