PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Data Structures | Functions
AnalyticalSolutions.c File Reference

Prescribes the Eulerian fields from the case's source instead of solving for them. More...

#include "AnalyticalSolutions.h"
#include "expression.h"
#include "Boundaries.h"
#include "Metric.h"
#include "logging.h"
#include "setup.h"
Include dependency graph for AnalyticalSolutions.c:

Go to the source code of this file.

Data Structures

struct  AnalyticalField
 The prescribed field: the case's source, and what it reuses from step to step. More...
 

Functions

static PetscErrorCode AnalyticalFieldCreate (SimCtx *simCtx)
 Read the source once, and work out what it depends on.
 
PetscErrorCode AnalyticalFieldDestroy (SimCtx *simCtx)
 Implementation of AnalyticalFieldDestroy().
 
PetscBool AnalyticalFieldSupportsInterpolationError (const SimCtx *simCtx)
 Implementation of AnalyticalFieldSupportsInterpolationError().
 
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.
 
PetscErrorCode AnalyticalSolutionEngine (SimCtx *simCtx)
 Implementation of AnalyticalSolutionEngine().
 
PetscErrorCode SetAnalyticalSolutionForParticles (Vec tempVec, SimCtx *simCtx)
 Implementation of SetAnalyticalSolutionForParticles().
 

Detailed Description

Prescribes the Eulerian fields from the case's source instead of solving for them.

With eulerian_field_source: analytical, the field is the initial condition's source, evaluated at the step's time through the operations every target of a source shares: fluxes at the faces (PicurvSourceAddFluxes()) and a bulk velocity (PicurvSourceCellBulkFactor()). A Cartesian source also sets Ucat and an optional pressure at the cell centres, exactly; a curvilinear one derives Ucat from its fluxes.

The boundary values are this module's own Ubcs, so after setting the state it runs the part of the solved flow's finalization that does not refresh them from the boundary handlers (FinalizeCellFieldsFromBoundaryValues()).

Definition in file AnalyticalSolutions.c.


Data Structure Documentation

◆ AnalyticalField

struct AnalyticalField

The prescribed field: the case's source, and what it reuses from step to step.

Definition at line 24 of file AnalyticalSolutions.c.

Collaboration diagram for AnalyticalField:
[legend]
Data Fields
PicurvVelocitySource source
PicurvExpression * pressure
PetscBool time_dependent
PetscBool pointwise
PetscBool evaluated
PetscReal factor
Vec * fraction
Vec * length
PicurvSourceFaceSlots(* faces)[6]

Function Documentation

◆ AnalyticalFieldCreate()

static PetscErrorCode AnalyticalFieldCreate ( SimCtx *  simCtx)
static

Read the source once, and work out what it depends on.

Collective.

Definition at line 37 of file AnalyticalSolutions.c.

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}
PicurvVelocitySource source
PicurvExpression * pressure
PicurvSourceFaceSlots(* faces)[6]
The prescribed field: the case's source, and what it reuses from step to step.
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
PetscBool bulk_time_dependent
Definition expression.h:193
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
@ 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_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
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 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 ...
PicurvSourceFrame frame
Definition expression.h:188
@ PICURV_SOURCE_FRAME_CARTESIAN
Three values: x, y, z velocity components.
Definition expression.h:176
PetscInt count
1 for the normal frame, otherwise 3; 0 for staged data alone.
Definition expression.h:189
#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
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
UserMG usermg
Definition variables.h:1082
PetscInt mglevels
Definition variables.h:775
MGCtx * mgctx
Definition variables.h:778
BCFace
Identifies the six logical faces of a structured computational block.
Definition variables.h:308
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:

◆ AnalyticalFieldDestroy()

PetscErrorCode AnalyticalFieldDestroy ( SimCtx *  simCtx)

Implementation of AnalyticalFieldDestroy().

Release what AnalyticalSolutionEngine() keeps from step to step, if anything.

See also
AnalyticalFieldDestroy()

Definition at line 88 of file AnalyticalSolutions.c.

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}
PetscErrorCode PicurvExpressionDestroy(PicurvExpression **expression)
Free a compiled expression.
Definition expression.c:818
PetscErrorCode PicurvVelocitySourceDestroy(PicurvVelocitySource *source)
Free a source's compiled expressions and bulk velocity.
PetscErrorCode PicurvSourceFaceSlotsDestroy(PicurvSourceFaceSlots *slots)
Release what PicurvSourceFaceSlotsCreate() allocated.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AnalyticalFieldSupportsInterpolationError()

PetscBool AnalyticalFieldSupportsInterpolationError ( const SimCtx *  simCtx)

Implementation of AnalyticalFieldSupportsInterpolationError().

Reports whether the analytical field can be compared with interpolated particle velocities.

See also
AnalyticalFieldSupportsInterpolationError()

Definition at line 113 of file AnalyticalSolutions.c.

114{
116 const PetscBool pointwise = (simCtx && simCtx->analyticalField) ? simCtx->analyticalField->pointwise : PETSC_FALSE;
118 return pointwise;
119}
Here is the caller graph for this function:

◆ SetBoundaryVelocityFromSource()

static PetscErrorCode SetBoundaryVelocityFromSource ( UserCtx *  user,
const struct AnalyticalField *  field,
PetscInt  block,
PetscReal  physical_time 
)
static

Set Ubcs on every physical boundary face to the source at the face centre.

Definition at line 122 of file AnalyticalSolutions.c.

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}
BoundaryFaceElements elements
The owned face elements the slots are.
Definition expression.h:317
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
PetscReal(* values)[PICURV_SOURCE_VAR_COUNT]
Variables per slot; t is the caller's.
Definition expression.h:318
Cmpnts(* edges)[3]
Directions per slot, for the curvilinear frame.
Definition expression.h:319
What a source reads at each boundary-face slot this rank owns, gathered once.
Definition expression.h:316
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
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 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 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
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
A 3D point or vector with PetscScalar components.
Definition variables.h:144
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AnalyticalSolutionEngine()

PetscErrorCode AnalyticalSolutionEngine ( SimCtx *  simCtx)

Implementation of AnalyticalSolutionEngine().

Sets the Eulerian fields from the case's source (-ic_source_*) at the step's time.

The field is the case's source, evaluated at the step's time. As on an inlet, a source that does not read t is evaluated once.

See also
AnalyticalSolutionEngine()

Definition at line 158 of file AnalyticalSolutions.c.

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}
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 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 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 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 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...
@ PICURV_SOURCE_FRAME_CURVILINEAR
Three values along the unit xi, eta, zeta grid-line directions.
Definition expression.h:177
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
#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
Vec Ucont
Definition variables.h:1197
PetscReal ti
Definition variables.h:925
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetAnalyticalSolutionForParticles()

PetscErrorCode SetAnalyticalSolutionForParticles ( Vec  tempVec,
SimCtx *  simCtx 
)

Implementation of SetAnalyticalSolutionForParticles().

Evaluates the analytical velocity at particle positions.

Only a pointwise source reaches here; see AnalyticalFieldSupportsInterpolationError().

See also
SetAnalyticalSolutionForParticles()

Definition at line 199 of file AnalyticalSolutions.c.

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}
PetscBool AnalyticalFieldSupportsInterpolationError(const SimCtx *simCtx)
Implementation of AnalyticalFieldSupportsInterpolationError().
@ 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
PetscReal L_ref
Definition variables.h:898
Here is the call graph for this function:
Here is the caller graph for this function: