PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Metric.c
Go to the documentation of this file.
1/* Metric.c ------------------------------------------------------------------
2 * Utility routines for curvilinear-grid metric operations used by the
3 * particle-swarm module.
4 *
5 * – Logical (xi,eta,zta) → physical (x,y,z) mapping via trilinear blend.
6 * – Jacobian matrix and determinant for contravariant velocity conversion.
7 *
8 * The only data this file needs from the application is the DMDA that stores
9 * vertex coordinates in the usual PETSc coordinate DM (user->da) and the
10 * coordinate array type `Cmpnts` (three-component struct {x,y,z}).
11 * ---------------------------------------------------------------------------*/
12
13#include <petsc.h>
14#include "Metric.h" /* forward declarations + Cmpnts + UserCtx */
15
16
17/* ------------------------------------------------------------------------- */
18/**
19 * @brief Implementation of \ref MetricGetCellVertices().
20 * @details Full API contract (arguments, ownership, side effects) is documented with
21 * the header declaration in `include/Metric.h`.
22 * @see MetricGetCellVertices()
23 */
24PetscErrorCode MetricGetCellVertices(UserCtx *user,
25 const Cmpnts ***X, /* coord array */
26 PetscInt i,PetscInt j,PetscInt k,
27 Cmpnts V[8])
28{
29 (void)user;
30 PetscFunctionBeginUser;
31 for (PetscInt c = 0; c < 8; ++c) {
32 PetscInt ii = i + ((c & 1) ? 1 : 0);
33 PetscInt jj = j + ((c & 2) ? 1 : 0);
34 PetscInt kk = k + ((c & 4) ? 1 : 0);
35 LOG_LOOP_ALLOW(GLOBAL, LOG_VERBOSE,i+j+k,10," ii: %" PetscInt_FMT ",jj:%" PetscInt_FMT ",kk:%" PetscInt_FMT " - Retrieved.\n",ii,jj,kk);
36 V[c] = X[kk][jj][ii];
37 }
38 PetscFunctionReturn(0);
39}
40
41/**
42 * @brief Implementation of \ref MetricFacePlane().
43 * @details Full API contract (arguments, ownership, side effects) is documented with
44 * the header declaration in `include/Metric.h`.
45 * @see MetricFacePlane()
46 */
47PetscBool MetricFacePlane(const Cmpnts v1, const Cmpnts v2, const Cmpnts v3, const Cmpnts v4,
48 const Cmpnts cell_centroid, FacePlane *plane, PetscBool *degenerate)
49{
51 const PetscReal edge1_x = v2.x - v1.x, edge1_y = v2.y - v1.y, edge1_z = v2.z - v1.z;
52 const PetscReal edge2_x = v4.x - v1.x, edge2_y = v4.y - v1.y, edge2_z = v4.z - v1.z;
53 PetscReal normal_x = edge1_y * edge2_z - edge1_z * edge2_y;
54 PetscReal normal_y = edge1_z * edge2_x - edge1_x * edge2_z;
55 PetscReal normal_z = edge1_x * edge2_y - edge1_y * edge2_x;
56 const PetscReal normal_magnitude = sqrt(normal_x * normal_x + normal_y * normal_y + normal_z * normal_z);
57 PetscBool ambiguous = PETSC_FALSE;
58
59 plane->centroid.x = 0.25 * (v1.x + v2.x + v3.x + v4.x);
60 plane->centroid.y = 0.25 * (v1.y + v2.y + v3.y + v4.y);
61 plane->centroid.z = 0.25 * (v1.z + v2.z + v3.z + v4.z);
62 *degenerate = (PetscBool)(normal_magnitude < 1.0e-12);
63 if (!*degenerate) {
64 // Orient outward: flip a normal that points from the face toward the cell centroid.
65 const PetscReal dot_prod_orientation = normal_x * (cell_centroid.x - plane->centroid.x) +
66 normal_y * (cell_centroid.y - plane->centroid.y) +
67 normal_z * (cell_centroid.z - plane->centroid.z);
68 if (dot_prod_orientation > 1.0e-9) {
69 normal_x = -normal_x; normal_y = -normal_y; normal_z = -normal_z;
70 } else if (dot_prod_orientation == 0.0) {
71 ambiguous = PETSC_TRUE; // Keep the (v2-v1)x(v4-v1) direction
72 }
73 normal_x /= normal_magnitude;
74 normal_y /= normal_magnitude;
75 normal_z /= normal_magnitude;
76 }
77 plane->normal.x = normal_x; plane->normal.y = normal_y; plane->normal.z = normal_z;
79 return ambiguous;
80}
81
82/**
83 * @brief Centroid of a hexahedral cell: the mean of its eight vertices.
84 * @param[in] cell The cell.
85 * @return The centroid.
86 */
87static inline Cmpnts MetricHexCentroid(const Cell *cell)
88{
89 Cmpnts centroid = {0.0, 0.0, 0.0};
90
91 for (int v = 0; v < 8; ++v) {
92 centroid.x += cell->vertices[v].x;
93 centroid.y += cell->vertices[v].y;
94 centroid.z += cell->vertices[v].z;
95 }
96 centroid.x /= 8.0;
97 centroid.y /= 8.0;
98 centroid.z /= 8.0;
99 return centroid;
100}
101
102/**
103 * @brief Implementation of \ref MetricBuildCellFacePlanes().
104 * @details Full API contract (arguments, ownership, side effects) is documented with
105 * the header declaration in `include/Metric.h`.
106 * @see MetricBuildCellFacePlanes()
107 */
109{
111 // Face vertex orders: each face's v1, v2, v4 span its normal, as the walking search has always used.
112 static const int face_vertices[NUM_FACES][4] = {
113 [LEFT] = {0, 7, 4, 3}, [RIGHT] = {1, 2, 5, 6}, [BOTTOM] = {0, 1, 6, 7},
114 [TOP] = {3, 4, 5, 2}, [FRONT] = {4, 7, 6, 5}, [BACK] = {0, 3, 2, 1}};
115 const Cmpnts cell_centroid = MetricHexCentroid(cell);
116 PetscInt mask = 0;
117
118 for (int f = 0; f < NUM_FACES; ++f) {
119 const int *v = face_vertices[f];
120 PetscBool degenerate;
121 if (MetricFacePlane(cell->vertices[v[0]], cell->vertices[v[1]], cell->vertices[v[2]], cell->vertices[v[3]],
122 cell_centroid, &planes->face[f], &degenerate)) {
123 LOG_ALLOW(LOCAL, LOG_WARNING, "Face %d: dot product for normal orientation is zero; keeping the (v2-v1)x(v4-v1) direction.\n", f);
124 }
125 if (degenerate) mask |= (PetscInt)1 << f;
126 }
127 planes->degenerate = mask;
128 planes->size = 0.0;
130}
131
132/** @brief The catalogued fields MetricComputeFacePlanes() owns. */
138
139/** @brief The plane fields by family: [dual][normal = 0, centroid = 1][axis]. */
145
146/** @brief Offset from a face's lower-corner origin to where its family stores it (see Metric.h). */
147static const PetscInt kPrimalFaceShift[3][3] = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}};
148
149/**
150 * @brief Where a face-plane field's vectors live in @p user, and its DM. FieldGetView() needs the
151 * vectors to exist; this reaches them before they are created and after they are destroyed.
152 * @param[in] user Block context.
153 * @param[in] id One of #kFacePlaneFields.
154 * @param[out] dm The field's DM.
155 * @param[out] global The UserCtx member holding its global vector.
156 * @param[out] local The UserCtx member holding its local vector.
157 * @return PETSc error code.
158 */
159static PetscErrorCode FacePlaneFieldSlots(UserCtx *user, FieldId id, DM *dm, Vec **global, Vec **local)
160{
161 const FieldDescriptor *descriptor = NULL;
162
163 PetscFunctionBeginUser;
165 PetscCall(FieldGetDescriptor(id, &descriptor));
166 *dm = (descriptor->dm_kind == FIELD_DM_DA) ? user->da : user->fda;
167 *global = (Vec *)((char *)user + descriptor->global_vec_offset);
168 *local = (Vec *)((char *)user + descriptor->local_vec_offset);
170 PetscFunctionReturn(0);
171}
172
173/**
174 * @brief The eight vertices of the cell whose origin node is (i, j, k), in RetrieveCurrentCell()
175 * order, from a flat ghosted local array indexed as MetricCellFacePlanesIndex().
176 */
177static inline void MetricGatherHexVertices(const Cmpnts *X, const DMDALocalInfo *info,
178 PetscInt i, PetscInt j, PetscInt k, Cell *hex)
179{
180 hex->vertices[0] = X[MetricCellFacePlanesIndex(info, i, j, k)];
181 hex->vertices[1] = X[MetricCellFacePlanesIndex(info, i + 1, j, k)];
182 hex->vertices[2] = X[MetricCellFacePlanesIndex(info, i + 1, j + 1, k)];
183 hex->vertices[3] = X[MetricCellFacePlanesIndex(info, i, j + 1, k)];
184 hex->vertices[4] = X[MetricCellFacePlanesIndex(info, i, j + 1, k + 1)];
185 hex->vertices[5] = X[MetricCellFacePlanesIndex(info, i + 1, j + 1, k + 1)];
186 hex->vertices[6] = X[MetricCellFacePlanesIndex(info, i + 1, j, k + 1)];
187 hex->vertices[7] = X[MetricCellFacePlanesIndex(info, i, j, k + 1)];
188}
189
190/** @brief The local vectors of the primal or the dual planes, as MetricGetFacePlaneArrays() reads them. */
191static inline void MetricFacePlaneLocals(const UserCtx *user, PetscBool dual, Vec normals[3], Vec centroids[3])
192{
193 if (dual) {
194 normals[0] = user->lDualPlaneNormalI; normals[1] = user->lDualPlaneNormalJ; normals[2] = user->lDualPlaneNormalK;
195 centroids[0] = user->lDualPlaneCentroidI; centroids[1] = user->lDualPlaneCentroidJ; centroids[2] = user->lDualPlaneCentroidK;
196 } else {
197 normals[0] = user->lPlaneNormalI; normals[1] = user->lPlaneNormalJ; normals[2] = user->lPlaneNormalK;
198 centroids[0] = user->lPlaneCentroidI; centroids[1] = user->lPlaneCentroidJ; centroids[2] = user->lPlaneCentroidK;
199 }
200}
201
202/**
203 * @brief Implementation of \ref MetricComputeFacePlanes().
204 * @details Full API contract (arguments, ownership, side effects) is documented with
205 * the header declaration in `include/Metric.h`.
206 * @see MetricComputeFacePlanes()
207 */
208PetscErrorCode MetricComputeFacePlanes(UserCtx *user)
209{
210 const DMDALocalInfo *info = &user->info;
211 const PetscInt gs[3] = {info->gxs, info->gys, info->gzs};
212 const PetscInt ge[3] = {info->gxs + info->gxm, info->gys + info->gym, info->gzs + info->gzm};
213 Vec Coor;
214 const PetscScalar *src_flat[2] = {NULL, NULL};
215 Cmpnts *out[2][6];
216 PetscReal *size = NULL;
217
218 PetscFunctionBeginUser;
219 if (user->SearchCellSize) PetscFunctionReturn(0);
221 for (size_t f = 0; f < sizeof(kFacePlaneFields) / sizeof(kFacePlaneFields[0]); ++f) {
222 DM dm = NULL;
223 Vec *global = NULL, *local = NULL;
224 PetscCall(FacePlaneFieldSlots(user, kFacePlaneFields[f], &dm, &global, &local));
225 PetscCall(DMCreateGlobalVector(dm, global));
226 PetscCall(DMCreateLocalVector(dm, local));
227 PetscCall(VecSet(*local, 0.0)); // A zero normal marks a face no plane was built for
228 }
229 for (int which = 0; which < 2; ++which) {
230 for (int f = 0; f < 6; ++f) {
231 FieldView view;
232 PetscCall(FieldGetView(user, kFacePlaneVectorFields[which][f / 3][f % 3], &view));
233 PetscCall(VecGetArray(view.local_vec, (PetscScalar **)&out[which][f]));
234 }
235 }
236 PetscCall(VecGetArray(user->lSearchCellSize, &size));
237
238 Coor = user->lCoor;
239 PetscCall(VecGetArrayRead(Coor, &src_flat[0]));
240 PetscCall(VecGetArrayRead(user->lCent, &src_flat[1]));
241 PetscBool periodic[3];
242 GetPeriodicAxes(user->simCtx, periodic);
243 const PetscInt mx[3] = {info->mx, info->my, info->mz};
244 PetscInt n_ambiguous = 0, first_ambiguous[4] = {-1, -1, -1, -1}; // Faces oriented by fallback
245 for (int which = 0; which < 2; ++which) {
246 const Cmpnts *X = (const Cmpnts *)src_flat[which];
247 // Points a cell may use, as the walk and the interpolation use them: physical nodes 0..mx-2
248 // (the last DA point, and its wrapped ghost copies, are a dummy); cell centres 1..mx-2 across
249 // a wall and 0..mx-1 on a periodic axis, where the boundary centres are the seam images.
250 // No plane is oriented by a cell that touches any other point.
251 PetscInt lo[3], hi[3];
252 for (int a = 0; a < 3; ++a) {
253 lo[a] = PetscMax(gs[a], (which && !periodic[a]) ? 1 : 0);
254 hi[a] = PetscMin(ge[a] - 1, (which && periodic[a]) ? mx[a] - 1 : mx[a] - 2);
255 }
256 #define PICURV_AT(i, j, k) X[MetricCellFacePlanesIndex(info, (i), (j), (k))]
257 for (PetscInt k = gs[2]; k < ge[2]; ++k) {
258 for (PetscInt j = gs[1]; j < ge[1]; ++j) {
259 for (PetscInt i = gs[0]; i < ge[0]; ++i) {
260 const PetscInt o[3] = {i, j, k};
261 for (PetscInt a = 0; a < 3; ++a) {
262 const PetscInt b = (a + 1) % 3, c = (a + 2) % 3;
263 // The face's own points, and the cell that orients it: the one above (origin o) when all
264 // its points are usable, else the one below. Without either the face keeps no plane.
265 if (o[b] < lo[b] || o[b] + 1 > hi[b] || o[c] < lo[c] || o[c] + 1 > hi[c]) continue;
266 PetscInt cell[3] = {i, j, k};
267 PetscBool above = (PetscBool)(o[a] >= lo[a] && o[a] + 1 <= hi[a]);
268 if (!above) {
269 if (o[a] - 1 < lo[a] || o[a] > hi[a]) continue;
270 cell[a] -= 1;
271 }
272 Cell hex;
273 MetricGatherHexVertices(X, info, cell[0], cell[1], cell[2], &hex);
274 const Cmpnts centroid = MetricHexCentroid(&hex);
275 // The face at o in the vertex order of the minus face (LEFT, BOTTOM, BACK) of cell o.
276 PetscInt e1[3] = {0, 0, 0}, e2[3] = {0, 0, 0};
277 if (a == 0) { e1[2] = 1; e2[1] = 1; } // LEFT: o, o+k, o+j+k, o+j
278 else if (a == 1) { e1[0] = 1; e2[2] = 1; } // BOTTOM: o, o+i, o+i+k, o+k
279 else { e1[1] = 1; e2[0] = 1; } // BACK: o, o+j, o+i+j, o+i
280 FacePlane plane;
281 PetscBool degenerate;
282 if (MetricFacePlane(PICURV_AT(i, j, k), PICURV_AT(i + e1[0], j + e1[1], k + e1[2]),
283 PICURV_AT(i + e1[0] + e2[0], j + e1[1] + e2[1], k + e1[2] + e2[2]),
284 PICURV_AT(i + e2[0], j + e2[1], k + e2[2]), centroid, &plane, &degenerate)) {
285 if (n_ambiguous++ == 0) { first_ambiguous[0] = a; first_ambiguous[1] = i; first_ambiguous[2] = j; first_ambiguous[3] = k; }
286 }
287 if (degenerate) continue;
288 // Stored pointing toward +a: the minus face of the cell above, the plus face of the one below.
289 const PetscReal sign = above ? -1.0 : 1.0;
290 const PetscInt *sh = which ? (const PetscInt[3]){0, 0, 0} : kPrimalFaceShift[a];
291 const PetscInt at = MetricCellFacePlanesIndex(info, i + sh[0], j + sh[1], k + sh[2]);
292 out[which][a][at].x = sign * plane.normal.x;
293 out[which][a][at].y = sign * plane.normal.y;
294 out[which][a][at].z = sign * plane.normal.z;
295 out[which][3 + a][at] = plane.centroid;
296 }
297 if (which == 0 && i >= lo[0] && i + 1 <= hi[0] && j >= lo[1] && j + 1 <= hi[1] && k >= lo[2] && k + 1 <= hi[2]) {
298 Cell hex;
299 MetricGatherHexVertices(X, info, i, j, k, &hex);
300 PetscCall(GetCellCharacteristicSize(&hex, &size[MetricCellFacePlanesIndex(info, i + 1, j + 1, k + 1)]));
301 }
302 }
303 }
304 }
305 #undef PICURV_AT
306 }
307 PetscCall(VecRestoreArrayRead(user->lCent, &src_flat[1]));
308 PetscCall(VecRestoreArrayRead(Coor, &src_flat[0]));
309 if (n_ambiguous > 0) {
310 LOG_ALLOW(LOCAL, LOG_WARNING, "Rank %d: %" PetscInt_FMT " particle face plane(s) had an ambiguous orientation and keep the (v2-v1)x(v4-v1) direction; first: face family %" PetscInt_FMT " at (%" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT ").\n",
311 (int)user->simCtx->rank, n_ambiguous, first_ambiguous[0], first_ambiguous[1], first_ambiguous[2], first_ambiguous[3]);
312 }
313
314 // Owned values become the global fields; the catalogued ghost update then gives every rank the
315 // owner's planes, which on every face a particle can reach are the ones it computed itself
316 // (the periodic face repair writes only the ghosts beyond the physical range, which none reads).
317 PetscCall(VecRestoreArray(user->lSearchCellSize, &size));
318 for (int which = 0; which < 2; ++which) {
319 for (int f = 0; f < 6; ++f) {
320 FieldView view;
321 PetscCall(FieldGetView(user, kFacePlaneVectorFields[which][f / 3][f % 3], &view));
322 PetscCall(VecRestoreArray(view.local_vec, (PetscScalar **)&out[which][f]));
323 }
324 }
325 for (size_t f = 0; f < sizeof(kFacePlaneFields) / sizeof(kFacePlaneFields[0]); ++f) {
326 FieldView view;
327 const PetscScalar *local = NULL;
328 PetscScalar *global = NULL;
329 PetscCall(FieldGetView(user, kFacePlaneFields[f], &view));
330 const PetscInt dof = view.descriptor->dof;
331 // Copy the owned block (DMLocalToGlobal cannot insert on a periodic axis held by one rank).
332 PetscCall(VecGetArrayRead(view.local_vec, &local));
333 PetscCall(VecGetArray(view.global_vec, &global));
334 for (PetscInt k = info->zs, g = 0; k < info->zs + info->zm; ++k) {
335 for (PetscInt j = info->ys; j < info->ys + info->ym; ++j) {
336 for (PetscInt i = info->xs; i < info->xs + info->xm; ++i, ++g) {
337 PetscCall(PetscArraycpy(&global[dof * g], &local[dof * MetricCellFacePlanesIndex(info, i, j, k)], dof));
338 }
339 }
340 }
341 PetscCall(VecRestoreArray(view.global_vec, &global));
342 PetscCall(VecRestoreArrayRead(view.local_vec, &local));
343 PetscCall(UpdateLocalGhosts(user, kFacePlaneFields[f]));
344 }
346 PetscFunctionReturn(0);
347}
348
349/**
350 * @brief Implementation of \ref MetricDestroyFacePlanes().
351 * @details Full API contract (arguments, ownership, side effects) is documented with
352 * the header declaration in `include/Metric.h`.
353 * @see MetricDestroyFacePlanes()
354 */
355PetscErrorCode MetricDestroyFacePlanes(UserCtx *user)
356{
357 PetscFunctionBeginUser;
359 for (size_t f = 0; f < sizeof(kFacePlaneFields) / sizeof(kFacePlaneFields[0]); ++f) {
360 DM dm = NULL;
361 Vec *global = NULL, *local = NULL;
362 PetscCall(FacePlaneFieldSlots(user, kFacePlaneFields[f], &dm, &global, &local));
363 PetscCall(VecDestroy(global));
364 PetscCall(VecDestroy(local));
365 }
367 PetscFunctionReturn(0);
368}
369
370/**
371 * @brief Implementation of \ref MetricGetFacePlaneArrays().
372 * @details Full API contract (arguments, ownership, side effects) is documented with
373 * the header declaration in `include/Metric.h`.
374 * @see MetricGetFacePlaneArrays()
375 */
376PetscErrorCode MetricGetFacePlaneArrays(UserCtx *user, PetscBool dual, FacePlaneArrays *arrays)
377{
378 Vec normals[3], centroids[3];
379
380 PetscFunctionBeginUser;
381 PetscCheck(user->lSearchCellSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
382 "Particle face planes are not built: MetricComputeFacePlanes() runs in CalculateAllGridMetrics() "
383 "for a particle run, collectively, once the grid is final.");
384 MetricFacePlaneLocals(user, dual, normals, centroids);
385 arrays->dual = dual;
386 for (int a = 0; a < 3; ++a) {
387 PetscCall(VecGetArrayRead(normals[a], (const PetscScalar **)&arrays->normal[a]));
388 PetscCall(VecGetArrayRead(centroids[a], (const PetscScalar **)&arrays->centroid[a]));
389 }
390 arrays->size = NULL;
391 if (!dual) PetscCall(VecGetArrayRead(user->lSearchCellSize, &arrays->size));
392 PetscFunctionReturn(0);
393}
394
395/**
396 * @brief Implementation of \ref MetricRestoreFacePlaneArrays().
397 * @details Full API contract (arguments, ownership, side effects) is documented with
398 * the header declaration in `include/Metric.h`.
399 * @see MetricRestoreFacePlaneArrays()
400 */
402{
403 Vec normals[3], centroids[3];
404
405 PetscFunctionBeginUser;
406 MetricFacePlaneLocals(user, arrays->dual, normals, centroids);
407 for (int a = 0; a < 3; ++a) {
408 PetscCall(VecRestoreArrayRead(normals[a], (const PetscScalar **)&arrays->normal[a]));
409 PetscCall(VecRestoreArrayRead(centroids[a], (const PetscScalar **)&arrays->centroid[a]));
410 }
411 if (!arrays->dual) PetscCall(VecRestoreArrayRead(user->lSearchCellSize, &arrays->size));
412 PetscFunctionReturn(0);
413}
414
415/**
416 * @brief Implementation of \ref MetricGatherCellFacePlanes().
417 * @details Full API contract (arguments, ownership, side effects) is documented with
418 * the header declaration in `include/Metric.h`.
419 * @see MetricGatherCellFacePlanes()
420 */
421void MetricGatherCellFacePlanes(const FacePlaneArrays *arrays, const DMDALocalInfo *info,
422 PetscInt i, PetscInt j, PetscInt k, CellFacePlanes *planes)
423{
424 static const PetscInt dual_shift[3] = {0, 0, 0};
425 static const Face minus_face[3] = {LEFT, BOTTOM, BACK}, plus_face[3] = {RIGHT, TOP, FRONT};
426
427 planes->degenerate = 0;
428 for (PetscInt a = 0; a < 3; ++a) {
429 const PetscInt *sh = arrays->dual ? dual_shift : kPrimalFaceShift[a];
430 for (int side = 0; side < 2; ++side) {
431 const PetscInt at = MetricCellFacePlanesIndex(info, i + sh[0] + (a == 0 ? side : 0),
432 j + sh[1] + (a == 1 ? side : 0), k + sh[2] + (a == 2 ? side : 0));
433 const Face f = side ? plus_face[a] : minus_face[a];
434 const Cmpnts n = arrays->normal[a][at];
435 FacePlane *p = &planes->face[f];
436 // Stored toward +a: outward for the plus face, reversed (exactly) for the minus face.
437 if (side) p->normal = n;
438 else { p->normal.x = -n.x; p->normal.y = -n.y; p->normal.z = -n.z; }
439 p->centroid = arrays->centroid[a][at];
440 if (n.x == 0.0 && n.y == 0.0 && n.z == 0.0) planes->degenerate |= (PetscInt)1 << f;
441 }
442 }
443 planes->size = arrays->dual ? 0.0 : arrays->size[MetricCellFacePlanesIndex(info, i + 1, j + 1, k + 1)];
444}
445
446
447/* ------------------------------------------------------------------------- */
448/**
449 * @brief Blend eight corner values at the supplied trilinear reference coordinates.
450 */
451static inline void TrilinearBlend(const Cmpnts V[8],
452 PetscReal xi,PetscReal eta,PetscReal zta,
453 Cmpnts *Xp)
454{
455 PetscReal x=0,y=0,z=0;
456 for (PetscInt c=0;c<8;++c) {
457 PetscReal N = ((c&1)?xi : 1.0-xi ) *
458 ((c&2)?eta: 1.0-eta) *
459 ((c&4)?zta: 1.0-zta);
460 x += N * V[c].x;
461 y += N * V[c].y;
462 z += N * V[c].z;
463 }
464 Xp->x = x; Xp->y = y; Xp->z = z;
465}
466
467
468/* ------------------------------------------------------------------------- */
469/**
470 * @brief Implementation of \ref MetricLogicalToPhysical().
471 * @details Full API contract (arguments, ownership, side effects) is documented with
472 * the header declaration in `include/Metric.h`.
473 * @see MetricLogicalToPhysical()
474 */
475PetscErrorCode MetricLogicalToPhysical(UserCtx *user,
476 const Cmpnts ***X,
477 PetscInt i,PetscInt j,PetscInt k,
478 PetscReal xi,PetscReal eta,PetscReal zta,
479 Cmpnts *Xp)
480{
481 PetscErrorCode ierr;
482 Cmpnts V[8];
483 PetscFunctionBeginUser;
484
486
487 ierr = MetricGetCellVertices(user,X,i,j,k,V); CHKERRQ(ierr);
488 TrilinearBlend(V,xi,eta,zta,Xp);
489
491
492 PetscFunctionReturn(0);
493}
494
495/* ------------------------------------------------------------------------- */
496/**
497 * @brief Implementation of \ref MetricJacobian().
498 * @details Full API contract (arguments, ownership, side effects) is documented with
499 * the header declaration in `include/Metric.h`.
500 * @see MetricJacobian()
501 */
502PetscErrorCode MetricJacobian(UserCtx *user,
503 const Cmpnts ***X,
504 PetscInt i,PetscInt j,PetscInt k,
505 PetscReal xi,PetscReal eta,PetscReal zta,
506 PetscReal J[3][3], PetscReal *detJ)
507{
508 PetscErrorCode ierr;
509 Cmpnts V[8];
510 PetscFunctionBeginUser;
511
513
514 ierr = MetricGetCellVertices(user,X,i,j,k,V); CHKERRQ(ierr);
515
516 /* derivatives of the [0,1] trilinear shape functions TrilinearBlend uses */
517 PetscReal dN_dXi[8], dN_dEta[8], dN_dZta[8];
518 for (PetscInt c=0;c<8;++c) {
519 PetscReal sx = (c & 1) ? 1.0 : -1.0;
520 PetscReal sy = (c & 2) ? 1.0 : -1.0;
521 PetscReal sz = (c & 4) ? 1.0 : -1.0;
522 dN_dXi [c] = sx * ( (c&2?eta:1-eta) ) * ( (c&4?zta:1-zta) );
523 dN_dEta[c] = sy * ( (c&1?xi :1-xi ) ) * ( (c&4?zta:1-zta) );
524 dN_dZta[c] = sz * ( (c&1?xi :1-xi ) ) * ( (c&2?eta:1-eta) );
525 }
526
527 /* assemble Jacobian */
528 PetscReal x_xi=0,y_xi=0,z_xi=0,
529 x_eta=0,y_eta=0,z_eta=0,
530 x_zta=0,y_zta=0,z_zta=0;
531 for (PetscInt c=0;c<8;++c) {
532 x_xi += dN_dXi [c]*V[c].x; y_xi += dN_dXi [c]*V[c].y; z_xi += dN_dXi [c]*V[c].z;
533 x_eta += dN_dEta[c]*V[c].x; y_eta += dN_dEta[c]*V[c].y; z_eta += dN_dEta[c]*V[c].z;
534 x_zta += dN_dZta[c]*V[c].x; y_zta += dN_dZta[c]*V[c].y; z_zta += dN_dZta[c]*V[c].z;
535 }
536
537 J[0][0]=x_xi; J[0][1]=x_eta; J[0][2]=x_zta;
538 J[1][0]=y_xi; J[1][1]=y_eta; J[1][2]=y_zta;
539 J[2][0]=z_xi; J[2][1]=z_eta; J[2][2]=z_zta;
540
541 if (detJ) {
542 *detJ = x_xi*(y_eta*z_zta - y_zta*z_eta)
543 - x_eta*(y_xi*z_zta - y_zta*z_xi)
544 + x_zta*(y_xi*z_eta - y_eta*z_xi);
545 }
546
548
549 PetscFunctionReturn(0);
550}
551
552
553/* ------------------------------------------------------------------------- */
554/**
555 * @brief Implementation of \ref MetricVelocityContravariant().
556 * @details Full API contract (arguments, ownership, side effects) is documented with
557 * the header declaration in `include/Metric.h`.
558 * @see MetricVelocityContravariant()
559 */
560PetscErrorCode MetricVelocityContravariant(const PetscReal J[3][3], PetscReal detJ,
561 const PetscReal u[3], PetscReal uc[3])
562{
563 PetscFunctionBeginUser;
564
566
567 /* contravariant basis vectors (row of adjugate(J)) divided by detJ */
568 PetscReal gxi[3] = { J[1][1]*J[2][2]-J[1][2]*J[2][1],
569 -J[0][1]*J[2][2]+J[0][2]*J[2][1],
570 J[0][1]*J[1][2]-J[0][2]*J[1][1] };
571 PetscReal geta[3] = { -J[1][0]*J[2][2]+J[1][2]*J[2][0],
572 J[0][0]*J[2][2]-J[0][2]*J[2][0],
573 -J[0][0]*J[1][2]+J[0][2]*J[1][0] };
574 PetscReal gzta[3] = { J[1][0]*J[2][1]-J[1][1]*J[2][0],
575 -J[0][0]*J[2][1]+J[0][1]*J[2][0],
576 J[0][0]*J[1][1]-J[0][1]*J[1][0] };
577
578 PetscReal invDet = 1.0 / detJ;
579 for (int d=0; d<3; ++d) { gxi[d] *= invDet; geta[d] *= invDet; gzta[d] *= invDet; }
580
581 uc[0] = gxi [0]*u[0] + gxi [1]*u[1] + gxi [2]*u[2];
582 uc[1] = geta[0]*u[0] + geta[1]*u[1] + geta[2]*u[2];
583 uc[2] = gzta[0]*u[0] + gzta[1]*u[1] + gzta[2]*u[2];
584
586
587 PetscFunctionReturn(0);
588}
589
590/////////////////////////////////////////////////////////////////////////////////
591/**
592 * @brief Implementation of \ref InvertCovariantMetricTensor().
593 * @see InvertCovariantMetricTensor()
594 */
595PetscErrorCode InvertCovariantMetricTensor(double covariantTensor[3][3], double contravariantTensor[3][3])
596{
597 PetscFunctionBeginUser;
599
600 const double a11=covariantTensor[0][0], a12=covariantTensor[0][1], a13=covariantTensor[0][2];
601 const double a21=covariantTensor[1][0], a22=covariantTensor[1][1], a23=covariantTensor[1][2];
602 const double a31=covariantTensor[2][0], a32=covariantTensor[2][1], a33=covariantTensor[2][2];
603
604 double det = a11*(a33*a22-a32*a23) - a21*(a33*a12-a32*a13) + a31*(a23*a12-a22*a13);
605
606 /* Degeneracy has to be judged against the rows' own magnitude, not against a fixed
607 number. The rows here are face-area vectors, so the determinant carries their
608 product - for an orthogonal cell it is exactly the square of the cell volume, and
609 it shrinks as the mesh is refined while the cell stays perfectly well formed. An
610 absolute floor therefore rejects fine grids: a wall-resolved cell of 5e-9 volume
611 has a determinant near 3e-17. The ratio below is 1 for an orthogonal cell and
612 approaches 0 only as the rows become coplanar, which is the actual failure. */
613 const double row_scale = sqrt(a11*a11 + a12*a12 + a13*a13) *
614 sqrt(a21*a21 + a22*a22 + a23*a23) *
615 sqrt(a31*a31 + a32*a32 + a33*a33);
616
617 if (row_scale <= 0.0 || fabs(det) <= 1.0e-10 * row_scale) {
618 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_MAT_LU_ZRPVT,
619 "Matrix is singular: |det| = %g is degenerate against the rows' own scale "
620 "%g (ratio %g); the rows are coplanar or one of them vanishes.",
621 (double)fabs(det), (double)row_scale,
622 (double)(row_scale > 0.0 ? fabs(det)/row_scale : 0.0));
623 }
624
625 contravariantTensor[0][0] = (a33*a22-a32*a23)/det;
626 contravariantTensor[0][1] = -(a33*a12-a32*a13)/det;
627 contravariantTensor[0][2] = (a23*a12-a22*a13)/det;
628 contravariantTensor[1][0] = -(a33*a21-a31*a23)/det;
629 contravariantTensor[1][1] = (a33*a11-a31*a13)/det;
630 contravariantTensor[1][2] = -(a23*a11-a21*a13)/det;
631 contravariantTensor[2][0] = (a32*a21-a31*a22)/det;
632 contravariantTensor[2][1] = -(a32*a11-a31*a12)/det;
633 contravariantTensor[2][2] = (a22*a11-a21*a12)/det;
634
636 PetscFunctionReturn(0);
637}
638
639
640/**
641 * @brief Implementation of \ref CalculateFaceNormalAndArea().
642 * @see CalculateFaceNormalAndArea()
643 */
644PetscErrorCode CalculateFaceNormalAndArea(Cmpnts csi, Cmpnts eta, Cmpnts zet, double ni[3], double nj[3], double nk[3], double *Ai, double *Aj, double *Ak)
645{
646 PetscFunctionBeginUser;
648 double g[3][3];
649 double G[3][3];
650
651 g[0][0]=csi.x, g[0][1]=csi.y, g[0][2]=csi.z;
652 g[1][0]=eta.x, g[1][1]=eta.y, g[1][2]=eta.z;
653 g[2][0]=zet.x, g[2][1]=zet.y, g[2][2]=zet.z;
654
655 PetscCall(InvertCovariantMetricTensor(g, G));
656 double xcsi=G[0][0], ycsi=G[1][0], zcsi=G[2][0];
657 double xeta=G[0][1], yeta=G[1][1], zeta=G[2][1];
658 double xzet=G[0][2], yzet=G[1][2], zzet=G[2][2];
659
660 double nx_i = xcsi, ny_i = ycsi, nz_i = zcsi;
661 double nx_j = xeta, ny_j = yeta, nz_j = zeta;
662 double nx_k = xzet, ny_k = yzet, nz_k = zzet;
663
664 double sum_i=sqrt(nx_i*nx_i+ny_i*ny_i+nz_i*nz_i);
665 double sum_j=sqrt(nx_j*nx_j+ny_j*ny_j+nz_j*nz_j);
666 double sum_k=sqrt(nx_k*nx_k+ny_k*ny_k+nz_k*nz_k);
667
668 *Ai = sqrt( g[0][0]*g[0][0] + g[0][1]*g[0][1] + g[0][2]*g[0][2] ); // area
669 *Aj = sqrt( g[1][0]*g[1][0] + g[1][1]*g[1][1] + g[1][2]*g[1][2] );
670 *Ak =sqrt( g[2][0]*g[2][0] + g[2][1]*g[2][1] + g[2][2]*g[2][2] );
671
672 nx_i /= sum_i, ny_i /= sum_i, nz_i /= sum_i;
673 nx_j /= sum_j, ny_j /= sum_j, nz_j /= sum_j;
674 nx_k /= sum_k, ny_k /= sum_k, nz_k /= sum_k;
675
676 ni[0] = nx_i, ni[1] = ny_i, ni[2] = nz_i;
677 nj[0] = nx_j, nj[1] = ny_j, nj[2] = nz_j;
678 nk[0] = nx_k, nk[1] = ny_k, nk[2] = nz_k;
679
681 PetscFunctionReturn(0);
682}
683
684/**
685 * @brief Implementation of \ref ComputeCellCharacteristicLengthScale().
686 * @see ComputeCellCharacteristicLengthScale()
687 */
688PetscErrorCode ComputeCellCharacteristicLengthScale(PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, double *dx, double *dy, double *dz)
689{
690 PetscFunctionBeginUser;
692 double ni[3], nj[3], nk[3];
693 double Li, Lj, Lk;
694 double Ai, Aj, Ak;
695 double vol = 1./ajc;
696
697 PetscCall(CalculateFaceNormalAndArea(csi, eta, zet, ni, nj, nk, &Ai, &Aj, &Ak));
698 Li = vol / Ai;
699 Lj = vol / Aj;
700 Lk = vol / Ak;
701
702 // Length scale vector = di * ni_vector + dj * nj_vector + dk * nk_vector
703 *dx = fabs( Li * ni[0] + Lj * nj[0] + Lk * nk[0] );
704 *dy = fabs( Li * ni[1] + Lj * nj[1] + Lk * nk[1] );
705 *dz = fabs( Li * ni[2] + Lj * nj[2] + Lk * nk[2] );
706
708 PetscFunctionReturn(0);
709}
710
711/**
712 * @brief Implementation of \ref ComputeCellDirectionalExtents().
713 * @details Full API contract is documented with the header declaration in
714 * `include/Metric.h`.
715 * @see ComputeCellDirectionalExtents()
716 */
717PetscErrorCode ComputeCellDirectionalExtents(PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet,
718 double *l_xi, double *l_eta, double *l_zeta)
719{
720 const double area_xi = sqrt(csi.x*csi.x + csi.y*csi.y + csi.z*csi.z);
721 const double area_eta = sqrt(eta.x*eta.x + eta.y*eta.y + eta.z*eta.z);
722 const double area_zeta = sqrt(zet.x*zet.x + zet.y*zet.y + zet.z*zet.z);
723
724 PetscFunctionBeginUser;
726 PetscCheck(ajc > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
727 "Cell Jacobian must be positive to define cell extents; received %g.", (double)ajc);
728 PetscCheck(area_xi > 0.0 && area_eta > 0.0 && area_zeta > 0.0, PETSC_COMM_SELF,
729 PETSC_ERR_ARG_OUTOFRANGE,
730 "A cell face has zero area (%g, %g, %g), so the cell has no extent across it.",
731 area_xi, area_eta, area_zeta);
732
733 /* Volume over the area of a face pair is the distance between those faces: the
734 cell's extent in that grid direction. It uses nothing but the cell's own metrics,
735 so it is the same wherever and however the cell sits in space. */
736 *l_xi = (1.0/ajc)/area_xi;
737 *l_eta = (1.0/ajc)/area_eta;
738 *l_zeta = (1.0/ajc)/area_zeta;
740 PetscFunctionReturn(0);
741}
742
743/**
744 * @brief Implementation of \ref ComputeCellEdgeVectors().
745 * @details Full API contract is documented with the header declaration in
746 * `include/Metric.h`.
747 * @see ComputeCellEdgeVectors()
748 */
749PetscErrorCode ComputeCellEdgeVectors(PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet,
750 Cmpnts edges[3])
751{
752 PetscFunctionBeginUser;
754 PetscCheck(ajc > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
755 "Cell Jacobian must be positive to define cell edges; received %g.", (double)ajc);
756 /* The face-area vectors are J grad(xi), J grad(eta), J grad(zeta), with J the cell
757 volume. The covariant basis dx/dxi equals J grad(eta) x grad(zeta), so it is
758 (eta x zet) / J - the cell's edge along xi, direction and length together. */
759 edges[0].x = ajc * (eta.y*zet.z - eta.z*zet.y);
760 edges[0].y = ajc * (eta.z*zet.x - eta.x*zet.z);
761 edges[0].z = ajc * (eta.x*zet.y - eta.y*zet.x);
762 edges[1].x = ajc * (zet.y*csi.z - zet.z*csi.y);
763 edges[1].y = ajc * (zet.z*csi.x - zet.x*csi.z);
764 edges[1].z = ajc * (zet.x*csi.y - zet.y*csi.x);
765 edges[2].x = ajc * (csi.y*eta.z - csi.z*eta.y);
766 edges[2].y = ajc * (csi.z*eta.x - csi.x*eta.z);
767 edges[2].z = ajc * (csi.x*eta.y - csi.y*eta.x);
769 PetscFunctionReturn(0);
770}
771
772
773/* -------------------------------------------------------------------------- */
774/**
775 * @brief Implementation of \ref CheckAndFixGridOrientation().
776 * @see CheckAndFixGridOrientation()
777 */
779{
780 PetscErrorCode ierr;
781 PetscReal aj_min, aj_max;
782 PetscMPIInt rank;
783
784 PetscFunctionBeginUser;
785
787
788 /* ---------------- step 1: global extrema of Aj ---------------- */
789 rank = user->simCtx->rank;
790
791 ierr = VecMin(user->Aj, NULL, &aj_min); CHKERRQ(ierr); /* already global */
792 ierr = VecMax(user->Aj, NULL, &aj_max); CHKERRQ(ierr);
793
795 "[orientation] Global Aj range: [%.3e , %.3e]\n",
796 (double)aj_min, (double)aj_max);
797
798 /* ---------------- step 2: detect malformed mesh ---------------- */
799 if (aj_min < 0.0 && aj_max > 0.0)
800 SETERRABORT(PETSC_COMM_WORLD, PETSC_ERR_USER,
801 "Mixed Jacobian signs detected – grid is topologically inconsistent.");
802
803 /* A uniformly left-handed grid is refused. The former "repair" negated Csi, Eta,
804 Zet and Aj on this level only, left the face and cell-centre metrics as they
805 were, and then reset the orientation flag to +1; a mirrored duct solved that way
806 reached forty times its bulk velocity. Renumbering one logical axis is exact. */
807 PetscInt orientation = +1;
808 if (aj_max < 0.0) {
809 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_USER,
810 "The grid is left-handed: every cell Jacobian is negative, as a mirrored or "
811 "oddly permuted grid is. The solver needs right-handed logical axes. Renumber "
812 "one logical axis (grid.gen: append reverse:axis=i|j|k to the transforms); "
813 "that axis's two faces trade names.");
814 }
815
816 /* ---------------- step 4: store result in UserCtx -------------- */
817 user->GridOrientation = orientation;
818
819 if (!rank)
820 LOG_ALLOW(LOCAL, LOG_INFO, "[orientation] Grid confirmed right-handed.\n");
821
823
824 PetscFunctionReturn(0);
825}
826
827/**
828 * @brief Implementation of \ref ApplyPeriodicCorrectionsToCellCenters().
829 * @see ApplyPeriodicCorrectionsToCellCenters()
830 */
832{
833 PetscErrorCode ierr;
834 DMDALocalInfo info = user->info;
835 PetscInt xs = info.xs, xe = info.xs + info.xm;
836 PetscInt ys = info.ys, ye = info.ys + info.ym;
837 PetscInt zs = info.zs, ze = info.zs + info.zm;
838 PetscInt mx = info.mx, my = info.my, mz = info.mz;
839 const BCFace neg_faces[3] = {BC_FACE_NEG_X, BC_FACE_NEG_Y, BC_FACE_NEG_Z};
840 Cmpnts ***cent, ***lcent;
841
842 PetscFunctionBeginUser;
844
845 // The dummy slot before the first cell of a periodic axis holds the last cell's centre
846 // shifted back by the validated seam translation T, and the slot after the last cell holds
847 // the first cell's centre shifted forward by T; exact for any translated period. Axes are
848 // corrected in turn, refreshing ghosts between them, so edge and corner dummies collect the
849 // translation of every periodic axis they lie across.
850 for (PetscInt axis = 0; axis < 3; axis++) {
851 if (user->boundary_faces[neg_faces[axis]].mathematical_type != PERIODIC) continue;
852 PetscCheck(user->periodic_translation_valid[axis], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
853 "Periodic cell-centre correction on axis %d needs the validated seam translation.", (int)axis);
854 const Cmpnts T = user->periodic_translation[axis];
855 ierr = UpdateLocalGhosts(user, FIELD_ID_CENT); CHKERRQ(ierr);
856 ierr = DMDAVecGetArray(user->fda, user->Cent, &cent); CHKERRQ(ierr);
857 ierr = DMDAVecGetArray(user->fda, user->lCent, &lcent); CHKERRQ(ierr);
858 for (PetscInt k = zs; k < ze; k++) {
859 for (PetscInt j = ys; j < ye; j++) {
860 for (PetscInt i = xs; i < xe; i++) {
861 const PetscInt idx[3] = {i, j, k}, n[3] = {mx, my, mz};
862 PetscInt src_i = i, src_j = j, src_k = k;
863 PetscReal sign;
864 if (idx[axis] == 0) sign = -1.0; // before the first cell: last cell - T
865 else if (idx[axis] == n[axis] - 1) sign = 1.0; // after the last cell: first cell + T
866 else continue;
867 // Wrapped local ghost of the source cell: index -2 is the last physical cell,
868 // n + 1 the first.
869 if (axis == 0) src_i = (sign < 0.0) ? -2 : mx + 1;
870 if (axis == 1) src_j = (sign < 0.0) ? -2 : my + 1;
871 if (axis == 2) src_k = (sign < 0.0) ? -2 : mz + 1;
872 cent[k][j][i].x = lcent[src_k][src_j][src_i].x + sign * T.x;
873 cent[k][j][i].y = lcent[src_k][src_j][src_i].y + sign * T.y;
874 cent[k][j][i].z = lcent[src_k][src_j][src_i].z + sign * T.z;
875 }
876 }
877 }
878 ierr = DMDAVecRestoreArray(user->fda, user->lCent, &lcent); CHKERRQ(ierr);
879 ierr = DMDAVecRestoreArray(user->fda, user->Cent, &cent); CHKERRQ(ierr);
880 }
881
883 PetscFunctionReturn(0);
884}
885
886/**
887 * @brief Implementation of \ref ApplyPeriodicCorrectionsToIFaceCenter().
888 * @see ApplyPeriodicCorrectionsToIFaceCenter()
889 */
891{
892 const FieldId fields[] = {FIELD_ID_CENTX};
893
894 PetscFunctionBeginUser;
896 PetscCall(SynchronizePeriodicFaceFields(user, 'i', 1, fields));
898 PetscFunctionReturn(0);
899}
900
901
902/**
903 * @brief Implementation of \ref ApplyPeriodicCorrectionsToJFaceCenter().
904 * @see ApplyPeriodicCorrectionsToJFaceCenter()
905 */
907{
908 const FieldId fields[] = {FIELD_ID_CENTY};
909
910 PetscFunctionBeginUser;
912 PetscCall(SynchronizePeriodicFaceFields(user, 'j', 1, fields));
914 PetscFunctionReturn(0);
915}
916
917
918/**
919 * @brief Implementation of \ref ApplyPeriodicCorrectionsToKFaceCenter().
920 * @see ApplyPeriodicCorrectionsToKFaceCenter()
921 */
923{
924 const FieldId fields[] = {FIELD_ID_CENTZ};
925
926 PetscFunctionBeginUser;
928 PetscCall(SynchronizePeriodicFaceFields(user, 'k', 1, fields));
930 PetscFunctionReturn(0);
931}
932
933/**
934 * @brief Implementation of \ref ComputeFaceMetrics().
935 * @see ComputeFaceMetrics()
936 */
937PetscErrorCode ComputeFaceMetrics(UserCtx *user)
938{
939 PetscErrorCode ierr;
940 DMDALocalInfo info;
941 Cmpnts ***csi_arr, ***eta_arr, ***zet_arr;
942 Cmpnts ***nodal_coords_arr;
943 Vec localCoords_from_dm;
944
945 PetscFunctionBeginUser;
946
948
949 LOG_ALLOW(GLOBAL, LOG_INFO, "Starting calculation and update for Csi, Eta, Zet.\n");
950
951 info = user->info;
952
953 // --- 1. Get Nodal Physical Coordinates (Local Ghosted Array directly) ---
954 localCoords_from_dm = user->lCoor;
955 if (!localCoords_from_dm) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE, "user->lCoor is NULL: grid coordinates are not set up.\n");
956 ierr = DMDAVecGetArrayRead(user->fda, localCoords_from_dm, &nodal_coords_arr); CHKERRQ(ierr);
957
958 // --- 2. Get arrays for output global Vecs from UserCtx ---
959 ierr = DMDAVecGetArray(user->fda, user->Csi, &csi_arr); CHKERRQ(ierr);
960 ierr = DMDAVecGetArray(user->fda, user->Eta, &eta_arr); CHKERRQ(ierr);
961 ierr = DMDAVecGetArray(user->fda, user->Zet, &zet_arr); CHKERRQ(ierr);
962
963 // Define owned node ranges (global indices)
964 PetscInt xs = info.xs, xe = info.xs + info.xm;
965 PetscInt ys = info.ys, ye = info.ys + info.ym;
966 PetscInt zs = info.zs, ze = info.zs + info.zm;
967
968 // Global domain dimensions (total number of nodes)
969 PetscInt mx = info.mx;
970 PetscInt my = info.my;
971 PetscInt mz = info.mz;
972
973 // --- 3. Calculate Csi, Eta, Zet for INTERIOR Stencils ---
974 // Start loops from 1 if at global boundary 0 to ensure k_node-1 etc. are valid.
975 PetscInt k_loop_start = (zs == 0) ? zs + 1 : zs;
976 PetscInt j_loop_start = (ys == 0) ? ys + 1 : ys;
977 PetscInt i_loop_start = (xs == 0) ? xs + 1 : xs;
978
979 // These represent the surface area of the curvilinear cell face and the normal rotated such that the direction of increasing coordinate is maintained.
980 // The metric vectors (Csi, Eta, Zet) are defined to point in the direction of their corresponding increasing computational coordinate.
981
982 // Calculate Csi
983 for (PetscInt k_node = k_loop_start; k_node < ze; ++k_node) {
984 for (PetscInt j_node = j_loop_start; j_node < ye; ++j_node) {
985 for (PetscInt i_node = xs; i_node < xe; ++i_node) {
986
987 PetscReal dx_deta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].x + nodal_coords_arr[k_node-1][j_node][i_node].x - nodal_coords_arr[k_node][j_node-1][i_node].x - nodal_coords_arr[k_node-1][j_node-1][i_node].x);
988 PetscReal dy_deta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].y + nodal_coords_arr[k_node-1][j_node][i_node].y - nodal_coords_arr[k_node][j_node-1][i_node].y - nodal_coords_arr[k_node-1][j_node-1][i_node].y);
989 PetscReal dz_deta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].z + nodal_coords_arr[k_node-1][j_node][i_node].z - nodal_coords_arr[k_node][j_node-1][i_node].z - nodal_coords_arr[k_node-1][j_node-1][i_node].z);
990 PetscReal dx_dzeta = 0.5 * (nodal_coords_arr[k_node][j_node-1][i_node].x + nodal_coords_arr[k_node][j_node][i_node].x - nodal_coords_arr[k_node-1][j_node-1][i_node].x - nodal_coords_arr[k_node-1][j_node][i_node].x);
991 PetscReal dy_dzeta = 0.5 * (nodal_coords_arr[k_node][j_node-1][i_node].y + nodal_coords_arr[k_node][j_node][i_node].y - nodal_coords_arr[k_node-1][j_node-1][i_node].y - nodal_coords_arr[k_node-1][j_node][i_node].y);
992 PetscReal dz_dzeta = 0.5 * (nodal_coords_arr[k_node][j_node-1][i_node].z + nodal_coords_arr[k_node][j_node][i_node].z - nodal_coords_arr[k_node-1][j_node-1][i_node].z - nodal_coords_arr[k_node-1][j_node][i_node].z);
993
994 csi_arr[k_node][j_node][i_node].x = dy_deta * dz_dzeta - dz_deta * dy_dzeta;
995 csi_arr[k_node][j_node][i_node].y = dz_deta * dx_dzeta - dx_deta * dz_dzeta;
996 csi_arr[k_node][j_node][i_node].z = dx_deta * dy_dzeta - dy_deta * dx_dzeta;
997 }
998 }
999 }
1000
1001 // Calculate Eta
1002 for (PetscInt k_node = k_loop_start; k_node < ze; ++k_node) {
1003 for (PetscInt j_node = ys; j_node < ye; ++j_node) {
1004 for (PetscInt i_node = i_loop_start; i_node < xe; ++i_node) {
1005
1006 PetscReal dx_dxi = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].x + nodal_coords_arr[k_node-1][j_node][i_node].x - nodal_coords_arr[k_node][j_node][i_node-1].x - nodal_coords_arr[k_node-1][j_node][i_node-1].x);
1007 PetscReal dy_dxi = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].y + nodal_coords_arr[k_node-1][j_node][i_node].y - nodal_coords_arr[k_node][j_node][i_node-1].y - nodal_coords_arr[k_node-1][j_node][i_node-1].y);
1008 PetscReal dz_dxi = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].z + nodal_coords_arr[k_node-1][j_node][i_node].z - nodal_coords_arr[k_node][j_node][i_node-1].z - nodal_coords_arr[k_node-1][j_node][i_node-1].z);
1009 PetscReal dx_dzeta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].x + nodal_coords_arr[k_node][j_node][i_node-1].x - nodal_coords_arr[k_node-1][j_node][i_node].x - nodal_coords_arr[k_node-1][j_node][i_node-1].x);
1010 PetscReal dy_dzeta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].y + nodal_coords_arr[k_node][j_node][i_node-1].y - nodal_coords_arr[k_node-1][j_node][i_node].y - nodal_coords_arr[k_node-1][j_node][i_node-1].y);
1011 PetscReal dz_dzeta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].z + nodal_coords_arr[k_node][j_node][i_node-1].z - nodal_coords_arr[k_node-1][j_node][i_node].z - nodal_coords_arr[k_node-1][j_node][i_node-1].z);
1012
1013 eta_arr[k_node][j_node][i_node].x = dy_dzeta * dz_dxi - dz_dzeta * dy_dxi;
1014 eta_arr[k_node][j_node][i_node].y = dz_dzeta * dx_dxi - dx_dzeta * dz_dxi;
1015 eta_arr[k_node][j_node][i_node].z = dx_dzeta * dy_dxi - dy_dzeta * dx_dxi;
1016 }
1017 }
1018 }
1019
1020 // Calculate Zet
1021 for (PetscInt k_node = zs; k_node < ze; ++k_node) {
1022 for (PetscInt j_node = j_loop_start; j_node < ye; ++j_node) {
1023 for (PetscInt i_node = i_loop_start; i_node < xe; ++i_node) {
1024
1025 PetscReal dx_dxi = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].x + nodal_coords_arr[k_node][j_node-1][i_node].x - nodal_coords_arr[k_node][j_node][i_node-1].x - nodal_coords_arr[k_node][j_node-1][i_node-1].x);
1026 PetscReal dy_dxi = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].y + nodal_coords_arr[k_node][j_node-1][i_node].y - nodal_coords_arr[k_node][j_node][i_node-1].y - nodal_coords_arr[k_node][j_node-1][i_node-1].y);
1027 PetscReal dz_dxi = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].z + nodal_coords_arr[k_node][j_node-1][i_node].z - nodal_coords_arr[k_node][j_node][i_node-1].z - nodal_coords_arr[k_node][j_node-1][i_node-1].z);
1028 PetscReal dx_deta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].x + nodal_coords_arr[k_node][j_node][i_node-1].x - nodal_coords_arr[k_node][j_node-1][i_node].x - nodal_coords_arr[k_node][j_node-1][i_node-1].x);
1029 PetscReal dy_deta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].y + nodal_coords_arr[k_node][j_node][i_node-1].y - nodal_coords_arr[k_node][j_node-1][i_node].y - nodal_coords_arr[k_node][j_node-1][i_node-1].y);
1030 PetscReal dz_deta = 0.5 * (nodal_coords_arr[k_node][j_node][i_node].z + nodal_coords_arr[k_node][j_node][i_node-1].z - nodal_coords_arr[k_node][j_node-1][i_node].z - nodal_coords_arr[k_node][j_node-1][i_node-1].z);
1031
1032 zet_arr[k_node][j_node][i_node].x = dy_dxi * dz_deta - dz_dxi * dy_deta;
1033 zet_arr[k_node][j_node][i_node].y = dz_dxi * dx_deta - dx_dxi * dz_deta;
1034 zet_arr[k_node][j_node][i_node].z = dx_dxi * dy_deta - dy_dxi * dx_deta;
1035 }
1036 }
1037 }
1038
1039 // --- 4. Boundary Extrapolation ---
1040 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Extrapolating boundary values for Csi, Eta, Zet.\n");
1041 PetscInt i_bnd, j_bnd, k_bnd;
1042
1043 if (xs == 0) { // If this rank owns the global i=0 boundary
1044 i_bnd = 0;
1045 for (k_bnd = zs; k_bnd < ze; ++k_bnd) {
1046 for (j_bnd = ys; j_bnd < ye; ++j_bnd) {
1047 if (i_bnd + 1 < mx) {
1048 eta_arr[k_bnd][j_bnd][i_bnd] = eta_arr[k_bnd][j_bnd][i_bnd+1];
1049 zet_arr[k_bnd][j_bnd][i_bnd] = zet_arr[k_bnd][j_bnd][i_bnd+1];
1050 }
1051 }
1052 }
1053 }
1054 if (xe == mx) { // If this rank owns the global i=mx-1 boundary
1055 i_bnd = mx - 1;
1056 for (k_bnd = zs; k_bnd < ze; ++k_bnd) {
1057 for (j_bnd = ys; j_bnd < ye; ++j_bnd) {
1058 if (i_bnd - 1 >= 0) {
1059 eta_arr[k_bnd][j_bnd][i_bnd] = eta_arr[k_bnd][j_bnd][i_bnd-1];
1060 zet_arr[k_bnd][j_bnd][i_bnd] = zet_arr[k_bnd][j_bnd][i_bnd-1];
1061 }
1062 }
1063 }
1064 }
1065 if (ys == 0) {
1066 j_bnd = 0;
1067 for (k_bnd = zs; k_bnd < ze; ++k_bnd) {
1068 for (i_bnd = xs; i_bnd < xe; ++i_bnd) {
1069 if (j_bnd + 1 < my) {
1070 csi_arr[k_bnd][j_bnd][i_bnd] = csi_arr[k_bnd][j_bnd+1][i_bnd];
1071 zet_arr[k_bnd][j_bnd][i_bnd] = zet_arr[k_bnd][j_bnd+1][i_bnd];
1072 }
1073 }
1074 }
1075 }
1076 if (ye == my) {
1077 j_bnd = my - 1;
1078 for (k_bnd = zs; k_bnd < ze; ++k_bnd) {
1079 for (i_bnd = xs; i_bnd < xe; ++i_bnd) {
1080 if (j_bnd - 1 >= 0) {
1081 csi_arr[k_bnd][j_bnd][i_bnd] = csi_arr[k_bnd][j_bnd-1][i_bnd];
1082 zet_arr[k_bnd][j_bnd][i_bnd] = zet_arr[k_bnd][j_bnd-1][i_bnd];
1083 }
1084 }
1085 }
1086 }
1087 if (zs == 0) {
1088 k_bnd = 0;
1089 for (j_bnd = ys; j_bnd < ye; ++j_bnd) {
1090 for (i_bnd = xs; i_bnd < xe; ++i_bnd) {
1091 if (k_bnd + 1 < mz) {
1092 csi_arr[k_bnd][j_bnd][i_bnd] = csi_arr[k_bnd+1][j_bnd][i_bnd];
1093 eta_arr[k_bnd][j_bnd][i_bnd] = eta_arr[k_bnd+1][j_bnd][i_bnd];
1094 }
1095 }
1096 }
1097 }
1098 if (ze == mz) {
1099 k_bnd = mz - 1;
1100 for (j_bnd = ys; j_bnd < ye; ++j_bnd) {
1101 for (i_bnd = xs; i_bnd < xe; ++i_bnd) {
1102 if (k_bnd - 1 >= 0) {
1103 csi_arr[k_bnd][j_bnd][i_bnd] = csi_arr[k_bnd-1][j_bnd][i_bnd];
1104 eta_arr[k_bnd][j_bnd][i_bnd] = eta_arr[k_bnd-1][j_bnd][i_bnd];
1105 }
1106 }
1107 }
1108 }
1109
1110 if (info.xs==0 && info.ys==0 && info.zs==0) {
1111 PetscReal dot = zet_arr[0][0][0].z; /* dot with global +z */
1112 LOG_ALLOW(GLOBAL,LOG_DEBUG,"Zet(k=0)·ez = %.3f (should be >0 for right-handed grid)\n", dot);
1113 }
1114
1115 // --- 5. Restore all arrays ---
1116 ierr = DMDAVecRestoreArrayRead(user->fda, localCoords_from_dm, &nodal_coords_arr); CHKERRQ(ierr);
1117 ierr = DMDAVecRestoreArray(user->fda, user->Csi, &csi_arr); CHKERRQ(ierr);
1118 ierr = DMDAVecRestoreArray(user->fda, user->Eta, &eta_arr); CHKERRQ(ierr);
1119 ierr = DMDAVecRestoreArray(user->fda, user->Zet, &zet_arr); CHKERRQ(ierr);
1120
1121 // --- 6. Assemble Global Vectors ---
1122 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Assembling global Csi, Eta, Zet.\n");
1123 ierr = VecAssemblyBegin(user->Csi); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->Csi); CHKERRQ(ierr);
1124 ierr = VecAssemblyBegin(user->Eta); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->Eta); CHKERRQ(ierr);
1125 ierr = VecAssemblyBegin(user->Zet); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->Zet); CHKERRQ(ierr);
1126
1127 // --- 7. Update Local Ghosted Versions ---
1128 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Updating local lCsi, lEta, lZet.\n");
1129 ierr = UpdateLocalGhosts(user, FIELD_ID_CSI); CHKERRQ(ierr);
1130 ierr = UpdateLocalGhosts(user, FIELD_ID_ETA); CHKERRQ(ierr);
1131 ierr = UpdateLocalGhosts(user, FIELD_ID_ZET); CHKERRQ(ierr);
1132
1133 LOG_ALLOW(GLOBAL, LOG_INFO, "Completed calculation, extrapolation, and update for Csi, Eta, Zet.\n");
1134
1136
1137 PetscFunctionReturn(0);
1138}
1139
1140
1141/**
1142 * @brief Implementation of \ref ComputeCellCenteredJacobianInverse().
1143 * @details Full API contract (arguments, ownership, side effects) is documented with
1144 * the header declaration in `include/Metric.h`.
1145 * @see ComputeCellCenteredJacobianInverse()
1146 */
1148{
1149 PetscErrorCode ierr;
1150 DMDALocalInfo info;
1151 PetscScalar ***aj_arr;
1152 Cmpnts ***nodal_coords_arr;
1153 Vec localCoords_from_dm;
1154
1155 PetscFunctionBeginUser;
1157 LOG_ALLOW(GLOBAL, LOG_INFO, "Starting calculation, extrapolation, and update for Aj.\n");
1158
1159 // --- 1. Get Nodal Coordinates and Output Array ---
1160 localCoords_from_dm = user->lCoor;
1161 ierr = DMDAVecGetArrayRead(user->fda, localCoords_from_dm, &nodal_coords_arr); CHKERRQ(ierr);
1162 info = user->info;
1163 ierr = DMDAVecGetArray(user->da, user->Aj, &aj_arr); CHKERRQ(ierr);
1164
1165 // Define owned node ranges (global indices)
1166 PetscInt xs = info.xs, xe = info.xs + info.xm;
1167 PetscInt ys = info.ys, ye = info.ys + info.ym;
1168 PetscInt zs = info.zs, ze = info.zs + info.zm;
1169
1170 // Global domain dimensions (total number of nodes)
1171 PetscInt mx = info.mx;
1172 PetscInt my = info.my;
1173 PetscInt mz = info.mz;
1174
1175 // --- 2. Calculate Aj for INTERIOR Stencils ---
1176
1177 PetscInt k_start_node = (zs == 0) ? zs + 1 : zs;
1178 PetscInt j_start_node = (ys == 0) ? ys + 1 : ys;
1179 PetscInt i_start_node = (xs == 0) ? xs + 1 : xs;
1180
1181 PetscInt k_end_node = (ze == mz) ? ze - 1 : ze;
1182 PetscInt j_end_node = (ye == my) ? ye - 1 : ye;
1183 PetscInt i_end_node = (xe == mx) ? xe - 1 : xe;
1184
1185 for (PetscInt k_node = k_start_node; k_node < k_end_node; ++k_node) {
1186 for (PetscInt j_node = j_start_node; j_node < j_end_node; ++j_node) {
1187 for (PetscInt i_node = i_start_node; i_node < i_end_node; ++i_node) {
1188
1189 PetscReal dx_dxi = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].x + nodal_coords_arr[k_node][j_node-1][i_node].x + nodal_coords_arr[k_node-1][j_node][i_node].x + nodal_coords_arr[k_node-1][j_node-1][i_node].x) - (nodal_coords_arr[k_node][j_node][i_node-1].x + nodal_coords_arr[k_node][j_node-1][i_node-1].x + nodal_coords_arr[k_node-1][j_node][i_node-1].x + nodal_coords_arr[k_node-1][j_node-1][i_node-1].x) );
1190
1191 PetscReal dy_dxi = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].y + nodal_coords_arr[k_node][j_node-1][i_node].y + nodal_coords_arr[k_node-1][j_node][i_node].y + nodal_coords_arr[k_node-1][j_node-1][i_node].y) - (nodal_coords_arr[k_node][j_node][i_node-1].y + nodal_coords_arr[k_node][j_node-1][i_node-1].y + nodal_coords_arr[k_node-1][j_node][i_node-1].y + nodal_coords_arr[k_node-1][j_node-1][i_node-1].y) );
1192
1193 PetscReal dz_dxi = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].z + nodal_coords_arr[k_node][j_node-1][i_node].z + nodal_coords_arr[k_node-1][j_node][i_node].z + nodal_coords_arr[k_node-1][j_node-1][i_node].z) - (nodal_coords_arr[k_node][j_node][i_node-1].z + nodal_coords_arr[k_node][j_node-1][i_node-1].z + nodal_coords_arr[k_node-1][j_node][i_node-1].z + nodal_coords_arr[k_node-1][j_node-1][i_node-1].z) );
1194
1195 PetscReal dx_deta = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].x + nodal_coords_arr[k_node][j_node][i_node-1].x + nodal_coords_arr[k_node-1][j_node][i_node].x + nodal_coords_arr[k_node-1][j_node][i_node-1].x) - (nodal_coords_arr[k_node][j_node-1][i_node].x + nodal_coords_arr[k_node][j_node-1][i_node-1].x + nodal_coords_arr[k_node-1][j_node-1][i_node].x + nodal_coords_arr[k_node-1][j_node-1][i_node-1].x) );
1196
1197 PetscReal dy_deta = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].y + nodal_coords_arr[k_node][j_node][i_node-1].y + nodal_coords_arr[k_node-1][j_node][i_node].y + nodal_coords_arr[k_node-1][j_node][i_node-1].y) - (nodal_coords_arr[k_node][j_node-1][i_node].y + nodal_coords_arr[k_node][j_node-1][i_node-1].y + nodal_coords_arr[k_node-1][j_node-1][i_node].y + nodal_coords_arr[k_node-1][j_node-1][i_node-1].y) );
1198
1199 PetscReal dz_deta = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].z + nodal_coords_arr[k_node][j_node][i_node-1].z + nodal_coords_arr[k_node-1][j_node][i_node].z + nodal_coords_arr[k_node-1][j_node][i_node-1].z) - (nodal_coords_arr[k_node][j_node-1][i_node].z + nodal_coords_arr[k_node][j_node-1][i_node-1].z + nodal_coords_arr[k_node-1][j_node-1][i_node].z + nodal_coords_arr[k_node-1][j_node-1][i_node-1].z) );
1200
1201 PetscReal dx_dzeta = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].x + nodal_coords_arr[k_node][j_node-1][i_node].x + nodal_coords_arr[k_node][j_node][i_node-1].x + nodal_coords_arr[k_node][j_node-1][i_node-1].x) - (nodal_coords_arr[k_node-1][j_node][i_node].x + nodal_coords_arr[k_node-1][j_node-1][i_node].x + nodal_coords_arr[k_node-1][j_node][i_node-1].x + nodal_coords_arr[k_node-1][j_node-1][i_node-1].x) );
1202
1203 PetscReal dy_dzeta = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].y + nodal_coords_arr[k_node][j_node-1][i_node].y + nodal_coords_arr[k_node][j_node][i_node-1].y + nodal_coords_arr[k_node][j_node-1][i_node-1].y) - (nodal_coords_arr[k_node-1][j_node][i_node].y + nodal_coords_arr[k_node-1][j_node-1][i_node].y + nodal_coords_arr[k_node-1][j_node][i_node-1].y + nodal_coords_arr[k_node-1][j_node-1][i_node-1].y) );
1204
1205 PetscReal dz_dzeta = 0.25 * ( (nodal_coords_arr[k_node][j_node][i_node].z + nodal_coords_arr[k_node][j_node-1][i_node].z + nodal_coords_arr[k_node][j_node][i_node-1].z + nodal_coords_arr[k_node][j_node-1][i_node-1].z) - (nodal_coords_arr[k_node-1][j_node][i_node].z + nodal_coords_arr[k_node-1][j_node-1][i_node].z + nodal_coords_arr[k_node-1][j_node][i_node-1].z + nodal_coords_arr[k_node-1][j_node-1][i_node-1].z) );
1206
1207 PetscReal jacobian_det = dx_dxi * (dy_deta * dz_dzeta - dz_deta * dy_dzeta) - dy_dxi * (dx_deta * dz_dzeta - dz_deta * dx_dzeta) + dz_dxi * (dx_deta * dy_dzeta - dy_deta * dx_dzeta);
1208 if (PetscAbsReal(jacobian_det) < 1.0e-18) { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FLOP_COUNT, "Jacobian is near zero..."); }
1209 aj_arr[k_node][j_node][i_node] = 1.0 / jacobian_det;
1210 }
1211 }
1212 }
1213
1214 // --- 4. Boundary Extrapolation for Aj ---
1215 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Extrapolating boundary values for Aj. \n");
1216 PetscInt i_bnd, j_bnd, k_bnd;
1217
1218 if (xs == 0) {
1219 i_bnd = 0;
1220 for (k_bnd = zs; k_bnd < ze; ++k_bnd) {
1221 for (j_bnd = ys; j_bnd < ye; ++j_bnd) {
1222 if (i_bnd + 1 < mx) aj_arr[k_bnd][j_bnd][i_bnd] = aj_arr[k_bnd][j_bnd][i_bnd+1];
1223 }
1224 }
1225 }
1226 if (xe == mx) {
1227 i_bnd = mx - 1;
1228 for (k_bnd = zs; k_bnd < ze; ++k_bnd) {
1229 for (j_bnd = ys; j_bnd < ye; ++j_bnd) {
1230 if (i_bnd - 1 >= 0) aj_arr[k_bnd][j_bnd][i_bnd] = aj_arr[k_bnd][j_bnd][i_bnd-1];
1231 }
1232 }
1233 }
1234 // (Similar extrapolation blocks for Y and Z boundaries for aj_arr)
1235 if (ys == 0) {
1236 j_bnd = 0;
1237 for (k_bnd = zs; k_bnd < ze; ++k_bnd) {
1238 for (i_bnd = xs; i_bnd < xe; ++i_bnd) {
1239 if (j_bnd + 1 < my) aj_arr[k_bnd][j_bnd][i_bnd] = aj_arr[k_bnd][j_bnd+1][i_bnd];
1240 }
1241 }
1242 }
1243 if (ye == my) {
1244 j_bnd = my - 1;
1245 for (k_bnd = zs; k_bnd < ze; ++k_bnd) {
1246 for (i_bnd = xs; i_bnd < xe; ++i_bnd) {
1247 if (j_bnd - 1 >= 0) aj_arr[k_bnd][j_bnd][i_bnd] = aj_arr[k_bnd][j_bnd-1][i_bnd];
1248 }
1249 }
1250 }
1251 if (zs == 0) {
1252 k_bnd = 0;
1253 for (j_bnd = ys; j_bnd < ye; ++j_bnd) {
1254 for (i_bnd = xs; i_bnd < xe; ++i_bnd) {
1255 if (k_bnd + 1 < mz) aj_arr[k_bnd][j_bnd][i_bnd] = aj_arr[k_bnd+1][j_bnd][i_bnd];
1256 }
1257 }
1258 }
1259 if (ze == mz) {
1260 k_bnd = mz - 1;
1261 for (j_bnd = ys; j_bnd < ye; ++j_bnd) {
1262 for (i_bnd = xs; i_bnd < xe; ++i_bnd) {
1263 if (k_bnd - 1 >= 0) aj_arr[k_bnd][j_bnd][i_bnd] = aj_arr[k_bnd-1][j_bnd][i_bnd];
1264 }
1265 }
1266 }
1267
1268 // --- 5. Restore arrays ---
1269 ierr = DMDAVecRestoreArrayRead(user->fda, localCoords_from_dm, &nodal_coords_arr); CHKERRQ(ierr);
1270 ierr = DMDAVecRestoreArray(user->da, user->Aj, &aj_arr); CHKERRQ(ierr);
1271
1272 // --- 6. Assemble Global Vector ---
1273 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Assembling global Aj.\n");
1274 ierr = VecAssemblyBegin(user->Aj); CHKERRQ(ierr);
1275 ierr = VecAssemblyEnd(user->Aj); CHKERRQ(ierr);
1276
1277 // --- 7. Update Local Ghosted Version ---
1278 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Updating local lAj.\n");
1279 ierr = UpdateLocalGhosts(user, FIELD_ID_AJ); CHKERRQ(ierr);
1280
1281 LOG_ALLOW(GLOBAL, LOG_INFO, "Completed calculation, extrapolation, and update for Aj.\n");
1283 PetscFunctionReturn(0);
1284}
1285
1286
1287/**
1288 * @brief Implementation of \ref ComputeCellCenters().
1289 * @see ComputeCellCenters()
1290 */
1291PetscErrorCode ComputeCellCenters(UserCtx *user)
1292{
1293 PetscErrorCode ierr;
1294 DMDALocalInfo info;
1295 Vec lCoords;
1296 const Cmpnts ***coor;
1297 Cmpnts ***cent;
1298 PetscInt xs,ys,zs,xe,ye,ze,mx,my,mz;
1299
1300 PetscFunctionBeginUser;
1301
1303
1304 LOG_ALLOW(LOCAL, LOG_INFO, "Rank %d: Computing cell centers and spacing for level %" PetscInt_FMT " block %" PetscInt_FMT "...\n", user->simCtx->rank, user->thislevel, user->_this);
1305
1306 info = user->info;
1307 lCoords = user->lCoor;
1308 ierr = DMDAVecGetArrayRead(user->fda, lCoords, &coor); CHKERRQ(ierr);
1309
1310 ierr = DMDAVecGetArray(user->fda, user->Cent, &cent); CHKERRQ(ierr);
1311
1312 xs = info.xs; xe = info.xs + info.xm;
1313 ys = info.ys; ye = info.ys + info.ym;
1314 zs = info.zs; ze = info.zs + info.zm;
1315 mx = info.mx; my = info.my; mz = info.mz;
1316
1317 PetscInt k_start_node = (zs == 0) ? zs + 1 : zs;
1318 PetscInt j_start_node = (ys == 0) ? ys + 1 : ys;
1319 PetscInt i_start_node = (xs == 0) ? xs + 1 : xs;
1320
1321 PetscInt k_end_node = (ze == mz) ? ze - 1 : ze;
1322 PetscInt j_end_node = (ye == my) ? ye - 1 : ye;
1323 PetscInt i_end_node = (xe == mx) ? xe - 1 : xe;
1324
1325 // Loop over the interior OWNED cells (stencil requires i-1, j-1, k-1)
1326 for (PetscInt k=k_start_node; k<k_end_node; k++) {
1327 for (PetscInt j=j_start_node; j<j_end_node; j++) {
1328 for (PetscInt i=i_start_node; i<i_end_node; i++) {
1329 // Calculate cell center as the average of its 8 corner nodes
1330 cent[k][j][i].x = 0.125 * (coor[k][j][i].x + coor[k][j-1][i].x + coor[k-1][j][i].x + coor[k-1][j-1][i].x + coor[k][j][i-1].x + coor[k][j-1][i-1].x + coor[k-1][j][i-1].x + coor[k-1][j-1][i-1].x);
1331 cent[k][j][i].y = 0.125 * (coor[k][j][i].y + coor[k][j-1][i].y + coor[k-1][j][i].y + coor[k-1][j-1][i].y + coor[k][j][i-1].y + coor[k][j-1][i-1].y + coor[k-1][j][i-1].y + coor[k-1][j-1][i-1].y);
1332 cent[k][j][i].z = 0.125 * (coor[k][j][i].z + coor[k][j-1][i].z + coor[k-1][j][i].z + coor[k-1][j-1][i].z + coor[k][j][i-1].z + coor[k][j-1][i-1].z + coor[k-1][j][i-1].z + coor[k-1][j-1][i-1].z);
1333 }
1334 }
1335 }
1336
1337 ierr = DMDAVecRestoreArrayRead(user->fda, lCoords, &coor); CHKERRQ(ierr);
1338 ierr = DMDAVecRestoreArray(user->fda, user->Cent, &cent); CHKERRQ(ierr);
1339
1340 // Assemble and update ghost regions for the new data
1341 ierr = VecAssemblyBegin(user->Cent); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->Cent); CHKERRQ(ierr);
1342 ierr = UpdateLocalGhosts(user, FIELD_ID_CENT); CHKERRQ(ierr);
1343
1344 ierr = ApplyPeriodicCorrectionsToCellCenters(user); CHKERRQ(ierr);
1345
1346 // Final assembly and ghost update after corrections
1347 ierr = VecAssemblyBegin(user->Cent); CHKERRQ(ierr);
1348 ierr = VecAssemblyEnd(user->Cent); CHKERRQ(ierr);
1349 ierr = UpdateLocalGhosts(user, FIELD_ID_CENT); CHKERRQ(ierr);
1350
1352
1353 PetscFunctionReturn(0);
1354}
1355
1356/**
1357 * @brief Implementation of \ref ComputeGridLineFractions().
1358 * @details Full API contract is documented with the header declaration in
1359 * `include/Metric.h`.
1360 * @see ComputeGridLineFractions()
1361 */
1362PetscErrorCode ComputeGridLineFractions(UserCtx *user, Vec fraction, Vec length)
1363{
1364 const DMDALocalInfo *info = &user->info;
1365 const PetscInt start[3] = {info->xs, info->ys, info->zs}, nodes[3] = {info->mx, info->my, info->mz};
1366 PetscInt lo[3], hi[3];
1367 const Cmpnts ***csi, ***eta, ***zet;
1368 const PetscReal ***aj;
1369 Cmpnts ***frac, ***len;
1370
1371 PetscFunctionBeginUser;
1373 GetOwnedInteriorCellBounds(info, lo, hi);
1374 PetscCall(DMDAVecGetArrayRead(user->fda, user->lCsi, &csi));
1375 PetscCall(DMDAVecGetArrayRead(user->fda, user->lEta, &eta));
1376 PetscCall(DMDAVecGetArrayRead(user->fda, user->lZet, &zet));
1377 PetscCall(DMDAVecGetArrayRead(user->da, user->lAj, &aj));
1378 PetscCall(DMDAVecGetArray(user->fda, fraction, &frac));
1379 PetscCall(DMDAVecGetArray(user->fda, length, &len));
1380
1381 for (PetscInt axis = 0; axis < 3; ++axis) {
1382 /* A line along `axis` is named by the other two indices; ranks with the same ranges
1383 of those indices share lines, and their order along `axis` is the order in the line. */
1384 const PetscInt a = (axis + 1) % 3, b = (axis + 2) % 3;
1385 const PetscInt na = hi[a] - lo[a], nb = hi[b] - lo[b], n_lines = PetscMax(0, na) * PetscMax(0, nb);
1386 PetscReal *segment, *before, *total;
1387 MPI_Comm line_comm;
1388 PetscMPIInt line_rank;
1389
1390 PetscCall(PetscCalloc3(n_lines, &segment, n_lines, &before, n_lines, &total));
1391 for (PetscInt pass = 0; pass < 2; ++pass) {
1392 for (PetscInt ib = lo[b]; ib < hi[b]; ++ib)
1393 for (PetscInt ia = lo[a]; ia < hi[a]; ++ia) {
1394 const PetscInt line = (ia - lo[a]) + na * (ib - lo[b]);
1395 PetscReal s = before[line];
1396 for (PetscInt n = lo[axis]; n < hi[axis]; ++n) {
1397 PetscInt idx[3];
1398 Cmpnts edges[3];
1399 idx[axis] = n; idx[a] = ia; idx[b] = ib;
1400 const PetscInt i = idx[0], j = idx[1], k = idx[2];
1401 PetscCall(ComputeCellEdgeVectors(aj[k][j][i], csi[k][j][i], eta[k][j][i], zet[k][j][i], edges));
1402 const PetscReal ds = PetscSqrtReal(edges[axis].x * edges[axis].x + edges[axis].y * edges[axis].y +
1403 edges[axis].z * edges[axis].z);
1404 if (pass == 0) { segment[line] += ds; continue; }
1405 /* Fraction at the cell centre: everything before the cell, plus half of it. */
1406 const PetscReal f = (s + 0.5 * ds) / total[line];
1407 s += ds;
1408 if (axis == 0) { frac[k][j][i].x = f; len[k][j][i].x = total[line]; }
1409 if (axis == 1) { frac[k][j][i].y = f; len[k][j][i].y = total[line]; }
1410 if (axis == 2) { frac[k][j][i].z = f; len[k][j][i].z = total[line]; }
1411 }
1412 }
1413 if (pass == 1) break;
1414 PetscCallMPI(MPI_Comm_split(PETSC_COMM_WORLD, (PetscMPIInt)(start[a] + nodes[a] * start[b]),
1415 (PetscMPIInt)start[axis], &line_comm));
1416 PetscCallMPI(MPI_Exscan(segment, before, (PetscMPIInt)n_lines, MPIU_REAL, MPI_SUM, line_comm));
1417 PetscCallMPI(MPI_Comm_rank(line_comm, &line_rank));
1418 if (line_rank == 0) PetscCall(PetscArrayzero(before, n_lines)); /* MPI_Exscan leaves rank 0's result undefined */
1419 PetscCallMPI(MPI_Allreduce(segment, total, (PetscMPIInt)n_lines, MPIU_REAL, MPI_SUM, line_comm));
1420 PetscCallMPI(MPI_Comm_free(&line_comm));
1421 }
1422 PetscCall(PetscFree3(segment, before, total));
1423 }
1424
1425 PetscCall(DMDAVecRestoreArray(user->fda, length, &len));
1426 PetscCall(DMDAVecRestoreArray(user->fda, fraction, &frac));
1427 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lAj, &aj));
1428 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lZet, &zet));
1429 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lEta, &eta));
1430 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lCsi, &csi));
1432 PetscFunctionReturn(0);
1433}
1434
1435/**
1436 * @brief Implementation of \ref ComputeIFaceMetrics().
1437 * @see ComputeIFaceMetrics()
1438 */
1439PetscErrorCode ComputeIFaceMetrics(UserCtx *user)
1440{
1441 PetscErrorCode ierr;
1442 DMDALocalInfo info;
1443 Vec lCoords;
1444 const Cmpnts ***coor;
1445 Cmpnts ***centx; //***gs;
1446 const Cmpnts ***centx_const;
1447 Cmpnts ***icsi, ***ieta, ***izet;
1448 PetscScalar ***iaj;
1449 PetscReal dxdc, dydc, dzdc, dxde, dyde, dzde, dxdz, dydz, dzdz;
1450
1451 PetscFunctionBeginUser;
1452
1454
1455 LOG_ALLOW(LOCAL, LOG_INFO, "Rank %d: Computing i-face metrics for level %" PetscInt_FMT " block %" PetscInt_FMT "...\n", user->simCtx->rank, user->thislevel, user->_this);
1456
1457 info = user->info;
1458 PetscInt xs = info.xs, xe = info.xs + info.xm, mx = info.mx;
1459 PetscInt ys = info.ys, ye = info.ys + info.ym, my = info.my;
1460 PetscInt zs = info.zs, ze = info.zs + info.zm, mz = info.mz;
1461 PetscInt lxe = xe;
1462 PetscInt lys = ys; PetscInt lye = ye;
1463 PetscInt lzs = zs; PetscInt lze = ze;
1464
1465 if (ys==0) lys = ys+1;
1466 if (zs==0) lzs = zs+1;
1467
1468 if (xe==mx) lxe=xe-1;
1469 if (ye==my) lye=ye-1;
1470 if (ze==mz) lze=ze-1;
1471
1472 // --- Part 1: Calculate the location of i-face centers (Centx) ---
1473 lCoords = user->lCoor;
1474 ierr = DMDAVecGetArrayRead(user->fda, lCoords, &coor); CHKERRQ(ierr);
1475 ierr = DMDAVecGetArray(user->fda, user->Centx, &centx); CHKERRQ(ierr);
1476
1477 //LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d: Calculating i-face centers (Centx) with i[%d,%d], j[%d,%d], k[%d,%d] ...\n", user->simCtx->rank,gxs,gxe,gys,gye,gzs,gze);
1478
1479 // Populate only owned physical face centers. Periodic endpoint and ghost
1480 // coordinates are established by the canonical face-field synchronizer.
1481 for (PetscInt k = PetscMax(zs, 1); k < PetscMin(ze, mz - 1); k++) {
1482 for (PetscInt j = PetscMax(ys, 1); j < PetscMin(ye, my - 1); j++) {
1483 for (PetscInt i = xs; i < PetscMin(xe, mx - 1); i++) {
1484 //----- DEBUG ------
1485 //LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d: Calculating i-face center at (k=%d, j=%d, i=%d)\n", user->simCtx->rank, k, j, i);
1486 //LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d: Using corner nodes: (%f,%f,%f), (%f,%f,%f), (%f,%f,%f), (%f,%f,%f)\n", user->simCtx->rank,
1487 // coor[k][j][i].x, coor[k][j][i].y, coor[k][j][i].z,
1488 // coor[k-1][j][i].x, coor[k-1][j][i].y, coor[k-1][j][i].z,
1489 // coor[k][j-1][i].x, coor[k][j-1][i].y, coor[k][j-1][i].z,
1490 // coor[k-1][j-1][i].x, coor[k-1][j-1][i].y, coor[k-1][j-1][i].z);
1491
1492 centx[k][j][i].x = 0.25 * (coor[k][j][i].x + coor[k-1][j][i].x + coor[k][j-1][i].x + coor[k-1][j-1][i].x);
1493 centx[k][j][i].y = 0.25 * (coor[k][j][i].y + coor[k-1][j][i].y + coor[k][j-1][i].y + coor[k-1][j-1][i].y);
1494 centx[k][j][i].z = 0.25 * (coor[k][j][i].z + coor[k-1][j][i].z + coor[k][j-1][i].z + coor[k-1][j-1][i].z);
1495
1496 //LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d: Calculated i-face center: (%f,%f,%f)\n", user->simCtx->rank, centx[k][j][i].x, centx[k][j][i].y, centx[k][j][i].z);
1497 }
1498 }
1499 }
1500
1501 //LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d: i-face center coordinates calculated. \n", user->simCtx->rank);
1502 /*
1503 if(xs==0){
1504 for(PetscInt k=gzs+1;k < gze; k++){
1505 for(PetscInt j=gys+1;j < gye; j++){
1506 PetscInt i=0;
1507 centx[k][j][i-1].x=centx[k][j][i].x-gs[k][j][i-2].x;
1508 centx[k][j][i-1].y=centx[k][j][i].y;
1509 centx[k][j][i-1].z=centx[k][j][i].z;
1510 }
1511 }
1512 }
1513 if (xe==mx){
1514 for(PetscInt k=gzs+1; k<gze; k++) {
1515 for (PetscInt j=gys+1; j<gye;j++) {
1516 PetscInt i=mx-1;
1517 centx[k][j][i].x=centx[k][j][i-1].x+gs[k][j][i+2].x;
1518 centx[k][j][i].y=centx[k][j][i-1].y;
1519 centx[k][j][i].z=centx[k][j][i-1].z;
1520 }
1521 }
1522 }
1523 */
1524
1525 ierr = DMDAVecRestoreArrayRead(user->fda, lCoords, &coor); CHKERRQ(ierr);
1526 ierr = DMDAVecRestoreArray(user->fda, user->Centx, &centx); CHKERRQ(ierr);
1527
1528
1529 {
1530 const FieldId face_centers[] = {FIELD_ID_CENTX};
1531 ierr = SynchronizePeriodicFaceFields(user, 'i', 1, face_centers); CHKERRQ(ierr);
1532 }
1533
1534 LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d: i-face centers (Centx) calculated and ghosts updated.\n", user->simCtx->rank);
1535
1536 // --- Part 2: Calculate metrics using face-centered coordinates ---
1537 ierr = DMDAVecGetArrayRead(user->fda, user->lCentx, &centx_const); CHKERRQ(ierr);
1538 ierr = DMDAVecGetArray(user->fda, user->ICsi, &icsi); CHKERRQ(ierr);
1539 ierr = DMDAVecGetArray(user->fda, user->IEta, &ieta); CHKERRQ(ierr);
1540 ierr = DMDAVecGetArray(user->fda, user->IZet, &izet); CHKERRQ(ierr);
1541 ierr = DMDAVecGetArray(user->da, user->IAj, &iaj); CHKERRQ(ierr);
1542
1543 // Loop over the OWNED region where we will store the final metrics
1544 for (PetscInt k=lzs; k<lze; k++) {
1545 for (PetscInt j=lys; j<lye; j++) {
1546 for (PetscInt i=xs; i<lxe; i++) {
1547
1548 // --- Stencil Logic for d/dcsi (derivative in i-direction) ---
1550 // Forward difference at the domain's min-i boundary
1551 dxdc = centx_const[k][j][i+1].x - centx_const[k][j][i].x;
1552 dydc = centx_const[k][j][i+1].y - centx_const[k][j][i].y;
1553 dzdc = centx_const[k][j][i+1].z - centx_const[k][j][i].z;
1554 } else if (i == mx - 2 && user->boundary_faces[BC_FACE_POS_X].mathematical_type != PERIODIC) {
1555 // Backward difference at the domain's max-i boundary
1556 dxdc = centx_const[k][j][i].x - centx_const[k][j][i-1].x;
1557 dydc = centx_const[k][j][i].y - centx_const[k][j][i-1].y;
1558 dzdc = centx_const[k][j][i].z - centx_const[k][j][i-1].z;
1559 } else { // Central difference in the interior (or if PERIODIC BCs)
1560 dxdc = 0.5 * (centx_const[k][j][i+1].x - centx_const[k][j][i-1].x);
1561 dydc = 0.5 * (centx_const[k][j][i+1].y - centx_const[k][j][i-1].y);
1562 dzdc = 0.5 * (centx_const[k][j][i+1].z - centx_const[k][j][i-1].z);
1563 }
1564
1565 // --- Stencil Logic for d/deta (derivative in j-direction) ---
1566 if (j == 1 && user->boundary_faces[BC_FACE_NEG_Y].mathematical_type != PERIODIC) {
1567 // Forward difference
1568 dxde = centx_const[k][j+1][i].x - centx_const[k][j][i].x;
1569 dyde = centx_const[k][j+1][i].y - centx_const[k][j][i].y;
1570 dzde = centx_const[k][j+1][i].z - centx_const[k][j][i].z;
1571 } else if (j == my - 2 && user->boundary_faces[BC_FACE_POS_Y].mathematical_type != PERIODIC) {
1572 // Backward difference
1573 dxde = centx_const[k][j][i].x - centx_const[k][j-1][i].x;
1574 dyde = centx_const[k][j][i].y - centx_const[k][j-1][i].y;
1575 dzde = centx_const[k][j][i].z - centx_const[k][j-1][i].z;
1576 } else { // Central difference (interior or PERIODIC)
1577 dxde = 0.5 * (centx_const[k][j+1][i].x - centx_const[k][j-1][i].x);
1578 dyde = 0.5 * (centx_const[k][j+1][i].y - centx_const[k][j-1][i].y);
1579 dzde = 0.5 * (centx_const[k][j+1][i].z - centx_const[k][j-1][i].z);
1580 }
1581
1582 // --- Stencil Logic for d/dzeta (derivative in k-direction) ---
1583 if (k == 1 && user->boundary_faces[BC_FACE_NEG_Z].mathematical_type != PERIODIC) {
1584 // Forward difference
1585 dxdz = centx_const[k+1][j][i].x - centx_const[k][j][i].x;
1586 dydz = centx_const[k+1][j][i].y - centx_const[k][j][i].y;
1587 dzdz = centx_const[k+1][j][i].z - centx_const[k][j][i].z;
1588 } else if (k == mz - 2 && user->boundary_faces[BC_FACE_POS_Z].mathematical_type != PERIODIC) {
1589 // Backward difference
1590 dxdz = centx_const[k][j][i].x - centx_const[k-1][j][i].x;
1591 dydz = centx_const[k][j][i].y - centx_const[k-1][j][i].y;
1592 dzdz = centx_const[k][j][i].z - centx_const[k-1][j][i].z;
1593 } else { // Central difference (Interior + PERIODIC)
1594 dxdz = 0.5 * (centx_const[k+1][j][i].x - centx_const[k-1][j][i].x);
1595 dydz = 0.5 * (centx_const[k+1][j][i].y - centx_const[k-1][j][i].y);
1596 dzdz = 0.5 * (centx_const[k+1][j][i].z - centx_const[k-1][j][i].z);
1597 }
1598
1599 // --- Metric calculations (identical to legacy FormMetrics) ---
1600 icsi[k][j][i].x = dyde * dzdz - dzde * dydz;
1601 icsi[k][j][i].y = -dxde * dzdz + dzde * dxdz;
1602 icsi[k][j][i].z = dxde * dydz - dyde * dxdz;
1603
1604 ieta[k][j][i].x = dydz * dzdc - dzdz * dydc;
1605 ieta[k][j][i].y = -dxdz * dzdc + dzdz * dxdc;
1606 ieta[k][j][i].z = dxdz * dydc - dydz * dxdc;
1607
1608 izet[k][j][i].x = dydc * dzde - dzdc * dyde;
1609 izet[k][j][i].y = -dxdc * dzde + dzdc * dxde;
1610 izet[k][j][i].z = dxdc * dyde - dydc * dxde;
1611
1612 iaj[k][j][i] = dxdc * icsi[k][j][i].x + dydc * icsi[k][j][i].y + dzdc * icsi[k][j][i].z;
1613 if (PetscAbsScalar(iaj[k][j][i]) > 1e-12) {
1614 iaj[k][j][i] = 1.0 / iaj[k][j][i];
1615 }
1616 }
1617 }
1618 }
1619
1620 ierr = DMDAVecRestoreArrayRead(user->fda, user->lCentx, &centx_const); CHKERRQ(ierr);
1621 ierr = DMDAVecRestoreArray(user->fda, user->ICsi, &icsi); CHKERRQ(ierr);
1622 ierr = DMDAVecRestoreArray(user->fda, user->IEta, &ieta); CHKERRQ(ierr);
1623 ierr = DMDAVecRestoreArray(user->fda, user->IZet, &izet); CHKERRQ(ierr);
1624 ierr = DMDAVecRestoreArray(user->da, user->IAj, &iaj); CHKERRQ(ierr);
1625
1626 // --- Part 3: Assemble global vectors and update local ghosts ---
1627 ierr = VecAssemblyBegin(user->ICsi); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->ICsi); CHKERRQ(ierr);
1628 ierr = VecAssemblyBegin(user->IEta); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->IEta); CHKERRQ(ierr);
1629 ierr = VecAssemblyBegin(user->IZet); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->IZet); CHKERRQ(ierr);
1630 ierr = VecAssemblyBegin(user->IAj); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->IAj); CHKERRQ(ierr);
1631
1632 ierr = UpdateLocalGhosts(user, FIELD_ID_ICSI); CHKERRQ(ierr);
1633 ierr = UpdateLocalGhosts(user, FIELD_ID_IETA); CHKERRQ(ierr);
1634 ierr = UpdateLocalGhosts(user, FIELD_ID_IZET); CHKERRQ(ierr);
1635 ierr = UpdateLocalGhosts(user, FIELD_ID_IAJ); CHKERRQ(ierr);
1636
1638
1639 PetscFunctionReturn(0);
1640}
1641
1642/**
1643 * @brief Implementation of \ref ComputeJFaceMetrics().
1644 * @see ComputeJFaceMetrics()
1645 */
1646PetscErrorCode ComputeJFaceMetrics(UserCtx *user)
1647{
1648 PetscErrorCode ierr;
1649 DMDALocalInfo info;
1650 Vec lCoords;
1651 const Cmpnts ***coor;
1652 Cmpnts ***centy; //***gs;
1653 const Cmpnts ***centy_const;
1654 Cmpnts ***jcsi, ***jeta, ***jzet;
1655 PetscScalar ***jaj;
1656 PetscReal dxdc, dydc, dzdc, dxde, dyde, dzde, dxdz, dydz, dzdz;
1657
1658 PetscFunctionBeginUser;
1659
1661
1662 LOG_ALLOW(LOCAL, LOG_INFO, "Rank %d: Computing j-face metrics for level %" PetscInt_FMT " block %" PetscInt_FMT "...\n", user->simCtx->rank, user->thislevel, user->_this);
1663
1664 info = user->info;
1665 PetscInt xs = info.xs, xe = info.xs + info.xm, mx = info.mx;
1666 PetscInt ys = info.ys, ye = info.ys + info.ym, my = info.my;
1667 PetscInt zs = info.zs, ze = info.zs + info.zm, mz = info.mz;
1668 PetscInt lxs = xs; PetscInt lxe = xe;
1669 PetscInt lye = ye;
1670 PetscInt lzs = zs; PetscInt lze = ze;
1671
1672 if (xs==0) lxs = xs+1;
1673 if (zs==0) lzs = zs+1;
1674
1675 if (xe==mx) lxe=xe-1;
1676 if (ye==my) lye=ye-1;
1677 if (ze==mz) lze=ze-1;
1678
1679 // --- Part 1: Calculate the location of i-face centers (Centx) ---
1680 lCoords = user->lCoor;
1681 ierr = DMDAVecGetArrayRead(user->fda, lCoords, &coor); CHKERRQ(ierr);
1682 ierr = DMDAVecGetArray(user->fda, user->Centy, &centy); CHKERRQ(ierr);
1683
1684 for (PetscInt k = PetscMax(zs, 1); k < PetscMin(ze, mz - 1); k++) {
1685 for (PetscInt j = ys; j < PetscMin(ye, my - 1); j++) {
1686 for (PetscInt i = PetscMax(xs, 1); i < PetscMin(xe, mx - 1); i++) {
1687 centy[k][j][i].x = 0.25 * (coor[k][j][i].x + coor[k-1][j][i].x + coor[k][j][i-1].x + coor[k-1][j][i-1].x);
1688 centy[k][j][i].y = 0.25 * (coor[k][j][i].y + coor[k-1][j][i].y + coor[k][j][i-1].y + coor[k-1][j][i-1].y);
1689 centy[k][j][i].z = 0.25 * (coor[k][j][i].z + coor[k-1][j][i].z + coor[k][j][i-1].z + coor[k-1][j][i-1].z);
1690 }
1691 }
1692 }
1693
1694 /*
1695 if(ys==0){
1696 for(PetscInt k=gzs+1;k < gze; k++){
1697 for(PetscInt i=gxs+1;j < gxe; i++){
1698 PetscInt j=0;
1699 centy[k][j-1][i].x=centy[k][j][i].x;
1700 centy[k][j-1][i].y=centy[k][j][i].y-gs[k][j-2][i].y;
1701 centy[k][j-1][i].z=centy[k][j][i].z;
1702 }
1703 }
1704 }
1705 if (ye==my){
1706 for(PetscInt k=gzs+1; k<gze; k++) {
1707 for (PetscInt i=gxs+1; j<gxe;i++) {
1708 PetscInt j=my-1;
1709 centy[k][j][i].x=centy[k][j-1][i].x
1710 centy[k][j][i].y=centy[k][j-1][i].y+gs[k][j+2][i].y;
1711 centy[k][j][i].z=centy[k][j-1][i].z;
1712 }
1713 }
1714 }
1715 */
1716
1717 ierr = DMDAVecRestoreArrayRead(user->fda, lCoords, &coor); CHKERRQ(ierr);
1718 ierr = DMDAVecRestoreArray(user->fda, user->Centy, &centy); CHKERRQ(ierr);
1719
1720 {
1721 const FieldId face_centers[] = {FIELD_ID_CENTY};
1722 ierr = SynchronizePeriodicFaceFields(user, 'j', 1, face_centers); CHKERRQ(ierr);
1723 }
1724
1725 LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d: j-face centers (Centx) calculated and ghosts updated.\n", user->simCtx->rank);
1726
1727 // --- Part 2: Calculate metrics using face-centered coordinates ---
1728 ierr = DMDAVecGetArrayRead(user->fda, user->lCenty, &centy_const); CHKERRQ(ierr);
1729 ierr = DMDAVecGetArray(user->fda, user->JCsi, &jcsi); CHKERRQ(ierr);
1730 ierr = DMDAVecGetArray(user->fda, user->JEta, &jeta); CHKERRQ(ierr);
1731 ierr = DMDAVecGetArray(user->fda, user->JZet, &jzet); CHKERRQ(ierr);
1732 ierr = DMDAVecGetArray(user->da, user->JAj, &jaj); CHKERRQ(ierr);
1733
1734 // Loop over the OWNED region where we will store the final metrics
1735 for (PetscInt k=lzs; k<lze; k++) {
1736 for (PetscInt j=ys; j<lye; j++) {
1737 for (PetscInt i=lxs; i<lxe; i++) {
1738
1739 // --- Stencil Logic for d/dcsi (derivative in i-direction) ---
1740 if (i == 1 && user->boundary_faces[BC_FACE_NEG_X].mathematical_type != PERIODIC) {
1741 // Forward difference at the domain's min-i boundary
1742 dxdc = centy_const[k][j][i+1].x - centy_const[k][j][i].x;
1743 dydc = centy_const[k][j][i+1].y - centy_const[k][j][i].y;
1744 dzdc = centy_const[k][j][i+1].z - centy_const[k][j][i].z;
1745 } else if (i == mx - 2 && user->boundary_faces[BC_FACE_POS_X].mathematical_type != PERIODIC) {
1746 // Backward difference at the domain's max-i boundary
1747 dxdc = centy_const[k][j][i].x - centy_const[k][j][i-1].x;
1748 dydc = centy_const[k][j][i].y - centy_const[k][j][i-1].y;
1749 dzdc = centy_const[k][j][i].z - centy_const[k][j][i-1].z;
1750 } else { // Central difference in the interior or PERIODIC
1751 dxdc = 0.5 * (centy_const[k][j][i+1].x - centy_const[k][j][i-1].x);
1752 dydc = 0.5 * (centy_const[k][j][i+1].y - centy_const[k][j][i-1].y);
1753 dzdc = 0.5 * (centy_const[k][j][i+1].z - centy_const[k][j][i-1].z);
1754 }
1755
1756 // --- Stencil Logic for d/deta (derivative in j-direction) ---
1757 if (j == 0 && user->boundary_faces[BC_FACE_NEG_Y].mathematical_type != PERIODIC) {
1758 // Forward difference
1759 dxde = centy_const[k][j+1][i].x - centy_const[k][j][i].x;
1760 dyde = centy_const[k][j+1][i].y - centy_const[k][j][i].y;
1761 dzde = centy_const[k][j+1][i].z - centy_const[k][j][i].z;
1762 } else if (j == my - 2 && user->boundary_faces[BC_FACE_POS_Y].mathematical_type != PERIODIC) {
1763 // Backward difference
1764 dxde = centy_const[k][j][i].x - centy_const[k][j-1][i].x;
1765 dyde = centy_const[k][j][i].y - centy_const[k][j-1][i].y;
1766 dzde = centy_const[k][j][i].z - centy_const[k][j-1][i].z;
1767 } else { // Central difference (interior or PERIODIC)
1768 dxde = 0.5 * (centy_const[k][j+1][i].x - centy_const[k][j-1][i].x);
1769 dyde = 0.5 * (centy_const[k][j+1][i].y - centy_const[k][j-1][i].y);
1770 dzde = 0.5 * (centy_const[k][j+1][i].z - centy_const[k][j-1][i].z);
1771 }
1772
1773 // --- Stencil Logic for d/dzeta (derivative in k-direction) ---
1774 if (k == 1 && user->boundary_faces[BC_FACE_NEG_Z].mathematical_type != PERIODIC) {
1775 // Forward difference
1776 dxdz = centy_const[k+1][j][i].x - centy_const[k][j][i].x;
1777 dydz = centy_const[k+1][j][i].y - centy_const[k][j][i].y;
1778 dzdz = centy_const[k+1][j][i].z - centy_const[k][j][i].z;
1779 } else if (k == mz - 2 && user->boundary_faces[BC_FACE_POS_Z].mathematical_type != PERIODIC) {
1780 // Backward difference
1781 dxdz = centy_const[k][j][i].x - centy_const[k-1][j][i].x;
1782 dydz = centy_const[k][j][i].y - centy_const[k-1][j][i].y;
1783 dzdz = centy_const[k][j][i].z - centy_const[k-1][j][i].z;
1784 } else { // Central difference (Interior or PERIODIC)
1785 dxdz = 0.5 * (centy_const[k+1][j][i].x - centy_const[k-1][j][i].x);
1786 dydz = 0.5 * (centy_const[k+1][j][i].y - centy_const[k-1][j][i].y);
1787 dzdz = 0.5 * (centy_const[k+1][j][i].z - centy_const[k-1][j][i].z);
1788 }
1789
1790 // --- Metric calculations (identical to legacy FormMetrics) ---
1791 jcsi[k][j][i].x = dyde * dzdz - dzde * dydz;
1792 jcsi[k][j][i].y = -dxde * dzdz + dzde * dxdz;
1793 jcsi[k][j][i].z = dxde * dydz - dyde * dxdz;
1794
1795 jeta[k][j][i].x = dydz * dzdc - dzdz * dydc;
1796 jeta[k][j][i].y = -dxdz * dzdc + dzdz * dxdc;
1797 jeta[k][j][i].z = dxdz * dydc - dydz * dxdc;
1798
1799 jzet[k][j][i].x = dydc * dzde - dzdc * dyde;
1800 jzet[k][j][i].y = -dxdc * dzde + dzdc * dxde;
1801 jzet[k][j][i].z = dxdc * dyde - dydc * dxde;
1802
1803 jaj[k][j][i] = dxdc * jcsi[k][j][i].x + dydc * jcsi[k][j][i].y + dzdc * jcsi[k][j][i].z;
1804 if (PetscAbsScalar(jaj[k][j][i]) > 1e-12) {
1805 jaj[k][j][i] = 1.0 / jaj[k][j][i];
1806 }
1807 }
1808 }
1809 }
1810
1811 ierr = DMDAVecRestoreArrayRead(user->fda, user->lCenty, &centy_const); CHKERRQ(ierr);
1812 ierr = DMDAVecRestoreArray(user->fda, user->JCsi, &jcsi); CHKERRQ(ierr);
1813 ierr = DMDAVecRestoreArray(user->fda, user->JEta, &jeta); CHKERRQ(ierr);
1814 ierr = DMDAVecRestoreArray(user->fda, user->JZet, &jzet); CHKERRQ(ierr);
1815 ierr = DMDAVecRestoreArray(user->da, user->JAj, &jaj); CHKERRQ(ierr);
1816
1817 // --- Part 3: Assemble global vectors and update local ghosts ---
1818 ierr = VecAssemblyBegin(user->JCsi); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->JCsi); CHKERRQ(ierr);
1819 ierr = VecAssemblyBegin(user->JEta); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->JEta); CHKERRQ(ierr);
1820 ierr = VecAssemblyBegin(user->JZet); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->JZet); CHKERRQ(ierr);
1821 ierr = VecAssemblyBegin(user->JAj); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->JAj); CHKERRQ(ierr);
1822
1823 ierr = UpdateLocalGhosts(user, FIELD_ID_JCSI); CHKERRQ(ierr);
1824 ierr = UpdateLocalGhosts(user, FIELD_ID_JETA); CHKERRQ(ierr);
1825 ierr = UpdateLocalGhosts(user, FIELD_ID_JZET); CHKERRQ(ierr);
1826 ierr = UpdateLocalGhosts(user, FIELD_ID_JAJ); CHKERRQ(ierr);
1827
1829
1830 PetscFunctionReturn(0);
1831}
1832
1833/**
1834 * @brief Implementation of \ref ComputeKFaceMetrics().
1835 * @see ComputeKFaceMetrics()
1836 */
1837PetscErrorCode ComputeKFaceMetrics(UserCtx *user)
1838{
1839 PetscErrorCode ierr;
1840 DMDALocalInfo info;
1841 Vec lCoords;
1842 const Cmpnts ***coor;
1843 Cmpnts ***centz; //***gs;
1844 const Cmpnts ***centz_const;
1845 Cmpnts ***kcsi, ***keta, ***kzet;
1846 PetscScalar ***kaj;
1847 PetscReal dxdc, dydc, dzdc, dxde, dyde, dzde, dxdz, dydz, dzdz;
1848
1849 PetscFunctionBeginUser;
1850
1852
1853 LOG_ALLOW(LOCAL, LOG_INFO, "Rank %d: Computing k-face metrics for level %" PetscInt_FMT " block %" PetscInt_FMT "...\n", user->simCtx->rank, user->thislevel, user->_this);
1854
1855 info = user->info;
1856 PetscInt xs = info.xs, xe = info.xs + info.xm, mx = info.mx;
1857 PetscInt ys = info.ys, ye = info.ys + info.ym, my = info.my;
1858 PetscInt zs = info.zs, ze = info.zs + info.zm, mz = info.mz;
1859 PetscInt lxs = xs; PetscInt lxe = xe;
1860 PetscInt lys = ys; PetscInt lye = ye;
1861 PetscInt lze = ze;
1862
1863 if (xs==0) lxs = xs+1;
1864 if (ys==0) lys = ys+1;
1865
1866 if (xe==mx) lxe=xe-1;
1867 if (ye==my) lye=ye-1;
1868 if (ze==mz) lze=ze-1;
1869
1870 // --- Part 1: Calculate the location of i-face centers (Centx) ---
1871 lCoords = user->lCoor;
1872 ierr = DMDAVecGetArrayRead(user->fda, lCoords, &coor); CHKERRQ(ierr);
1873 ierr = DMDAVecGetArray(user->fda, user->Centz, &centz); CHKERRQ(ierr);
1874
1875 for (PetscInt k = zs; k < PetscMin(ze, mz - 1); k++) {
1876 for (PetscInt j = PetscMax(ys, 1); j < PetscMin(ye, my - 1); j++) {
1877 for (PetscInt i = PetscMax(xs, 1); i < PetscMin(xe, mx - 1); i++) {
1878 centz[k][j][i].x = 0.25 * (coor[k][j][i].x + coor[k][j-1][i].x + coor[k][j][i-1].x + coor[k][j-1][i-1].x);
1879 centz[k][j][i].y = 0.25 * (coor[k][j][i].y + coor[k][j-1][i].y + coor[k][j][i-1].y + coor[k][j-1][i-1].y);
1880 centz[k][j][i].z = 0.25 * (coor[k][j][i].z + coor[k][j-1][i].z + coor[k][j][i-1].z + coor[k][j-1][i-1].z);
1881 }
1882 }
1883 }
1884
1885 /*
1886 if(zs==0){
1887 for(PetscInt j=gys+1;j < gye; j++){
1888 for(PetscInt i=gxs+1;j < gxe; i++){
1889 PetscInt k=0;
1890 centz[k-1][j][i].x=centz[k][j][i].x;
1891 centz[k-1][j][i].y=centz[k][j][i].y;
1892 centz[k-1][j][i].z=centz[k][j][i].z-gs[k-2][j][i].z;
1893 }
1894 }
1895 }
1896 if (ze==mz){
1897 for(PetscInt j=gys+1; j<gye; j++) {
1898 for (PetscInt i=gxs+1; j<gxe;i++) {
1899 PetscInt k=mz-1;
1900 centy[k][j][i].x=centy[k-1][j][i].x
1901 centy[k][j][i].y=centy[k-1][j][i].y;
1902 centz[k][j][i].z=centz[k-1][j][i].z+gs[k+2][j][1].z;
1903 }
1904 }
1905 }
1906 */
1907
1908 ierr = DMDAVecRestoreArrayRead(user->fda, lCoords, &coor); CHKERRQ(ierr);
1909 ierr = DMDAVecRestoreArray(user->fda, user->Centz, &centz); CHKERRQ(ierr);
1910
1911 {
1912 const FieldId face_centers[] = {FIELD_ID_CENTZ};
1913 ierr = SynchronizePeriodicFaceFields(user, 'k', 1, face_centers); CHKERRQ(ierr);
1914 }
1915
1916 LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d: k-face centers (Centx) calculated and ghosts updated.\n", user->simCtx->rank);
1917
1918 // --- Part 2: Calculate metrics using face-centered coordinates ---
1919 ierr = DMDAVecGetArrayRead(user->fda, user->lCentz, &centz_const); CHKERRQ(ierr);
1920 ierr = DMDAVecGetArray(user->fda, user->KCsi, &kcsi); CHKERRQ(ierr);
1921 ierr = DMDAVecGetArray(user->fda, user->KEta, &keta); CHKERRQ(ierr);
1922 ierr = DMDAVecGetArray(user->fda, user->KZet, &kzet); CHKERRQ(ierr);
1923 ierr = DMDAVecGetArray(user->da, user->KAj, &kaj); CHKERRQ(ierr);
1924
1925 // Loop over the OWNED region where we will store the final metrics
1926 for (PetscInt k=zs; k<lze; k++) {
1927 for (PetscInt j=lys; j<lye; j++) {
1928 for (PetscInt i=lxs; i<lxe; i++) {
1929
1930 // --- Stencil Logic for d/dcsi (derivative in i-direction) ---
1931 if (i == 1 && user->boundary_faces[BC_FACE_NEG_X].mathematical_type != PERIODIC) {
1932 // Forward difference at the domain's min-i boundary
1933 dxdc = centz_const[k][j][i+1].x - centz_const[k][j][i].x;
1934 dydc = centz_const[k][j][i+1].y - centz_const[k][j][i].y;
1935 dzdc = centz_const[k][j][i+1].z - centz_const[k][j][i].z;
1936 } else if (i == mx - 2 && user->boundary_faces[BC_FACE_POS_X].mathematical_type != PERIODIC) {
1937 // Backward difference at the domain's max-i boundary
1938 dxdc = centz_const[k][j][i].x - centz_const[k][j][i-1].x;
1939 dydc = centz_const[k][j][i].y - centz_const[k][j][i-1].y;
1940 dzdc = centz_const[k][j][i].z - centz_const[k][j][i-1].z;
1941 } else { // Central difference in the interior (or PERIODIC)
1942 dxdc = 0.5 * (centz_const[k][j][i+1].x - centz_const[k][j][i-1].x);
1943 dydc = 0.5 * (centz_const[k][j][i+1].y - centz_const[k][j][i-1].y);
1944 dzdc = 0.5 * (centz_const[k][j][i+1].z - centz_const[k][j][i-1].z);
1945 }
1946
1947 // --- Stencil Logic for d/deta (derivative in j-direction) ---
1948 if (j == 1 && user->boundary_faces[BC_FACE_NEG_Y].mathematical_type != PERIODIC) {
1949 // Forward difference
1950 dxde = centz_const[k][j+1][i].x - centz_const[k][j][i].x;
1951 dyde = centz_const[k][j+1][i].y - centz_const[k][j][i].y;
1952 dzde = centz_const[k][j+1][i].z - centz_const[k][j][i].z;
1953 } else if (j == my - 2 && user->boundary_faces[BC_FACE_POS_Y].mathematical_type != PERIODIC) {
1954 // Backward difference
1955 dxde = centz_const[k][j][i].x - centz_const[k][j-1][i].x;
1956 dyde = centz_const[k][j][i].y - centz_const[k][j-1][i].y;
1957 dzde = centz_const[k][j][i].z - centz_const[k][j-1][i].z;
1958 } else { // Central difference (interior or PERIODIC)
1959 dxde = 0.5 * (centz_const[k][j+1][i].x - centz_const[k][j-1][i].x);
1960 dyde = 0.5 * (centz_const[k][j+1][i].y - centz_const[k][j-1][i].y);
1961 dzde = 0.5 * (centz_const[k][j+1][i].z - centz_const[k][j-1][i].z);
1962 }
1963
1964 // --- Stencil Logic for d/dzeta (derivative in k-direction) ---
1965 if (k == 0 && user->boundary_faces[BC_FACE_NEG_Z].mathematical_type != PERIODIC) {
1966 // Forward difference
1967 dxdz = centz_const[k+1][j][i].x - centz_const[k][j][i].x;
1968 dydz = centz_const[k+1][j][i].y - centz_const[k][j][i].y;
1969 dzdz = centz_const[k+1][j][i].z - centz_const[k][j][i].z;
1970 } else if (k == mz - 2 && user->boundary_faces[BC_FACE_POS_Z].mathematical_type != PERIODIC) {
1971 // Backward difference
1972 dxdz = centz_const[k][j][i].x - centz_const[k-1][j][i].x;
1973 dydz = centz_const[k][j][i].y - centz_const[k-1][j][i].y;
1974 dzdz = centz_const[k][j][i].z - centz_const[k-1][j][i].z;
1975 } else { // Central difference (Interior or PERIODIC)
1976 dxdz = 0.5 * (centz_const[k+1][j][i].x - centz_const[k-1][j][i].x);
1977 dydz = 0.5 * (centz_const[k+1][j][i].y - centz_const[k-1][j][i].y);
1978 dzdz = 0.5 * (centz_const[k+1][j][i].z - centz_const[k-1][j][i].z);
1979 }
1980
1981 // --- Metric calculations (identical to legacy FormMetrics) ---
1982 kcsi[k][j][i].x = dyde * dzdz - dzde * dydz;
1983 kcsi[k][j][i].y = -dxde * dzdz + dzde * dxdz;
1984 kcsi[k][j][i].z = dxde * dydz - dyde * dxdz;
1985
1986 keta[k][j][i].x = dydz * dzdc - dzdz * dydc;
1987 keta[k][j][i].y = -dxdz * dzdc + dzdz * dxdc;
1988 keta[k][j][i].z = dxdz * dydc - dydz * dxdc;
1989
1990 kzet[k][j][i].x = dydc * dzde - dzdc * dyde;
1991 kzet[k][j][i].y = -dxdc * dzde + dzdc * dxde;
1992 kzet[k][j][i].z = dxdc * dyde - dydc * dxde;
1993
1994 kaj[k][j][i] = dxdc * kcsi[k][j][i].x + dydc * kcsi[k][j][i].y + dzdc * kcsi[k][j][i].z;
1995 if (PetscAbsScalar(kaj[k][j][i]) > 1e-12) {
1996 kaj[k][j][i] = 1.0 / kaj[k][j][i];
1997 }
1998 }
1999 }
2000 }
2001
2002 ierr = DMDAVecRestoreArrayRead(user->fda, user->lCentz, &centz_const); CHKERRQ(ierr);
2003 ierr = DMDAVecRestoreArray(user->fda, user->KCsi, &kcsi); CHKERRQ(ierr);
2004 ierr = DMDAVecRestoreArray(user->fda, user->KEta, &keta); CHKERRQ(ierr);
2005 ierr = DMDAVecRestoreArray(user->fda, user->KZet, &kzet); CHKERRQ(ierr);
2006 ierr = DMDAVecRestoreArray(user->da, user->KAj, &kaj); CHKERRQ(ierr);
2007
2008 // --- Part 3: Assemble global vectors and update local ghosts ---
2009 ierr = VecAssemblyBegin(user->KCsi); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->KCsi); CHKERRQ(ierr);
2010 ierr = VecAssemblyBegin(user->KEta); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->KEta); CHKERRQ(ierr);
2011 ierr = VecAssemblyBegin(user->KZet); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->KZet); CHKERRQ(ierr);
2012 ierr = VecAssemblyBegin(user->KAj); CHKERRQ(ierr); ierr = VecAssemblyEnd(user->KAj); CHKERRQ(ierr);
2013
2014 ierr = UpdateLocalGhosts(user, FIELD_ID_KCSI); CHKERRQ(ierr);
2015 ierr = UpdateLocalGhosts(user, FIELD_ID_KETA); CHKERRQ(ierr);
2016 ierr = UpdateLocalGhosts(user, FIELD_ID_KZET); CHKERRQ(ierr);
2017 ierr = UpdateLocalGhosts(user, FIELD_ID_KAJ); CHKERRQ(ierr);
2018
2020
2021 PetscFunctionReturn(0);
2022}
2023
2024/**
2025 * @brief Convert logical cell indices into the flattened global cell identifier.
2026 */
2027static PetscInt Gidx(PetscInt i, PetscInt j, PetscInt k, UserCtx *user)
2028{
2029 PetscInt nidx;
2030 DMDALocalInfo info = user->info;
2031
2032 PetscInt mx = info.mx, my = info.my;
2033
2034 AO ao;
2035 DMDAGetAO(user->da, &ao);
2036 nidx=i+j*mx+k*mx*my;
2037
2038 AOApplicationToPetsc(ao,1,&nidx);
2039
2040 return (nidx);
2041}
2042
2043/**
2044 * @brief Implementation of \ref ComputeMetricsDivergence().
2045 * @see ComputeMetricsDivergence()
2046 */
2048{
2049 DM da = user->da, fda = user->fda;
2050 DMDALocalInfo info = user->info;
2051 PetscInt xs = info.xs, xe = info.xs + info.xm;
2052 PetscInt ys = info.ys, ye = info.ys + info.ym;
2053 PetscInt zs = info.zs, ze = info.zs + info.zm;
2054 PetscInt mx = info.mx, my = info.my, mz = info.mz;
2055 PetscInt lxs, lys, lzs, lxe, lye, lze;
2056 PetscInt i, j, k;
2057 Vec Div;
2058 PetscReal ***div, ***aj;
2059 Cmpnts ***csi, ***eta, ***zet;
2060 PetscReal maxdiv;
2061
2062 PetscFunctionBeginUser;
2063
2065
2066 lxs = xs; lxe = xe;
2067 lys = ys; lye = ye;
2068 lzs = zs; lze = ze;
2069
2070 if (xs == 0) lxs = xs + 1;
2071 if (ys == 0) lys = ys + 1;
2072 if (zs == 0) lzs = zs + 1;
2073
2074 if (xe == mx) lxe = xe - 1;
2075 if (ye == my) lye = ye - 1;
2076 if (ze == mz) lze = ze - 1;
2077
2078 DMDAVecGetArray(fda, user->lCsi, &csi);
2079 DMDAVecGetArray(fda, user->lEta, &eta);
2080 DMDAVecGetArray(fda, user->lZet, &zet);
2081 DMDAVecGetArray(da, user->lAj, &aj);
2082
2083 VecDuplicate(user->P, &Div);
2084 VecSet(Div, 0.);
2085 DMDAVecGetArray(da, Div, &div);
2086
2087 for (k = lzs; k < lze; k++) {
2088 for (j = lys; j < lye; j++) {
2089 for (i = lxs; i < lxe; i++) {
2090 PetscReal divergence = (csi[k][j][i].x - csi[k][j][i-1].x +
2091 eta[k][j][i].x - eta[k][j-1][i].x +
2092 zet[k][j][i].x - zet[k-1][j][i].x +
2093 csi[k][j][i].y - csi[k][j][i-1].y +
2094 eta[k][j][i].y - eta[k][j-1][i].y +
2095 zet[k][j][i].y - zet[k-1][j][i].y +
2096 csi[k][j][i].z - csi[k][j][i-1].z +
2097 eta[k][j][i].z - eta[k][j-1][i].z +
2098 zet[k][j][i].z - zet[k-1][j][i].z) * aj[k][j][i];
2099 div[k][j][i] = fabs(divergence);
2100 }
2101 }
2102 }
2103
2104 DMDAVecRestoreArray(da, Div, &div);
2105
2106 PetscInt MaxFlatIndex = -1;
2107 VecMax(Div, &MaxFlatIndex, &maxdiv);
2108 LOG_ALLOW(GLOBAL,LOG_INFO,"The Maximum Metric Divergence is %e at flat index %" PetscInt_FMT ".\n",maxdiv,MaxFlatIndex);
2109
2110 for (k=zs; k<ze; k++) {
2111 for (j=ys; j<ye; j++) {
2112 for (i=xs; i<xe; i++) {
2113 if (Gidx(i,j,k,user) == MaxFlatIndex) {
2114 LOG_ALLOW(LOCAL,LOG_INFO,"The Maximum Metric Divergence(%e) is at location [%d][%d][%d]. \n", maxdiv,(int)k,(int)j,(int)i);
2115 }
2116 }
2117 }
2118 }
2119
2120
2121 DMDAVecRestoreArray(fda, user->lCsi, &csi);
2122 DMDAVecRestoreArray(fda, user->lEta, &eta);
2123 DMDAVecRestoreArray(fda, user->lZet, &zet);
2124 DMDAVecRestoreArray(da, user->lAj, &aj);
2125 VecDestroy(&Div);
2126
2127
2129
2130 PetscFunctionReturn(0);
2131}
2132
2133/**
2134 * @brief Implementation of \ref ComputeMetricNorms().
2135 * @see ComputeMetricNorms()
2136 */
2137PetscErrorCode ComputeMetricNorms(UserCtx *user)
2138{
2139
2140 DMDALocalInfo info = user->info;
2141 PetscInt xs = info.xs, xe = info.xs + info.xm;
2142 PetscInt ys = info.ys, ye = info.ys + info.ym;
2143 PetscInt zs = info.zs, ze = info.zs + info.zm;
2144 PetscInt i, j, k;
2145
2146 PetscFunctionBeginUser;
2147
2149
2150 PetscReal CsiMax, EtaMax, ZetMax;
2151 PetscReal ICsiMax, IEtaMax, IZetMax;
2152 PetscReal JCsiMax, JEtaMax, JZetMax;
2153 PetscReal KCsiMax, KEtaMax, KZetMax;
2154 PetscReal AjMax, IAjMax, JAjMax, KAjMax;
2155
2156 PetscInt CsiMaxArg, EtaMaxArg, ZetMaxArg;
2157 PetscInt ICsiMaxArg, IEtaMaxArg, IZetMaxArg;
2158 PetscInt JCsiMaxArg, JEtaMaxArg, JZetMaxArg;
2159 PetscInt KCsiMaxArg, KEtaMaxArg, KZetMaxArg;
2160 PetscInt AjMaxArg, IAjMaxArg, JAjMaxArg, KAjMaxArg;
2161
2162 // Max Values
2163 VecMax(user->lCsi,&CsiMaxArg,&CsiMax);
2164 VecMax(user->lEta,&EtaMaxArg,&EtaMax);
2165 VecMax(user->lZet,&ZetMaxArg,&ZetMax);
2166
2167 VecMax(user->lICsi,&ICsiMaxArg,&ICsiMax);
2168 VecMax(user->lIEta,&IEtaMaxArg,&IEtaMax);
2169 VecMax(user->lIZet,&IZetMaxArg,&IZetMax);
2170
2171 VecMax(user->lJCsi,&JCsiMaxArg,&JCsiMax);
2172 VecMax(user->lJEta,&JEtaMaxArg,&JEtaMax);
2173 VecMax(user->lJZet,&JZetMaxArg,&JZetMax);
2174
2175 VecMax(user->lKCsi,&KCsiMaxArg,&KCsiMax);
2176 VecMax(user->lKEta,&KEtaMaxArg,&KEtaMax);
2177 VecMax(user->lKZet,&KZetMaxArg,&KZetMax);
2178
2179 VecMax(user->lAj,&AjMaxArg,&AjMax);
2180 VecMax(user->lIAj,&IAjMaxArg,&IAjMax);
2181 VecMax(user->lJAj,&JAjMaxArg,&JAjMax);
2182 VecMax(user->lKAj,&KAjMaxArg,&KAjMax);
2183
2184 VecMax(user->lAj,&AjMaxArg,&AjMax);
2185 VecMax(user->lIAj,&IAjMaxArg,&IAjMax);
2186 VecMax(user->lJAj,&JAjMaxArg,&JAjMax);
2187 VecMax(user->lKAj,&KAjMaxArg,&KAjMax);
2188
2189 LOG_ALLOW(GLOBAL,LOG_INFO," Metric Norms for MG level %" PetscInt_FMT " .\n",user->thislevel);
2190
2191 LOG_ALLOW(GLOBAL,LOG_INFO,"The Max Metric Values are: CsiMax = %le, EtaMax = %le, ZetMax = %le.\n",CsiMax,EtaMax,ZetMax);
2192 LOG_ALLOW(GLOBAL,LOG_INFO,"The Max Metric Values are: ICsiMax = %le, IEtaMax = %le, IZetMax = %le.\n",ICsiMax,IEtaMax,IZetMax);
2193 LOG_ALLOW(GLOBAL,LOG_INFO,"The Max Metric Values are: JCsiMax = %le, JEtaMax = %le, JZetMax = %le.\n",JCsiMax,JEtaMax,JZetMax);
2194 LOG_ALLOW(GLOBAL,LOG_INFO,"The Max Metric Values are: KCsiMax = %le, KEtaMax = %le, KZetMax = %le.\n",KCsiMax,KEtaMax,KZetMax);
2195 LOG_ALLOW(GLOBAL,LOG_INFO,"The Max Volumes(Inverse) are: Aj = %le, IAj = %le, JAj = %le, KAj = %le.\n",AjMax,IAjMax,JAjMax,KAjMax);
2196
2197 for (k=zs; k<ze; k++) {
2198 for (j=ys; j<ye; j++) {
2199 for (i=xs; i<xe; i++) {
2200 if (Gidx(i,j,k,user) == CsiMaxArg) {
2201 LOG_ALLOW(LOCAL,LOG_INFO,"Max Csi = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", CsiMax,k,j,i);
2202 }
2203 if (Gidx(i,j,k,user) == EtaMaxArg) {
2204 LOG_ALLOW(LOCAL,LOG_INFO,"Max Eta = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", EtaMax,k,j,i);
2205 }
2206 if (Gidx(i,j,k,user) == ZetMaxArg) {
2207 LOG_ALLOW(LOCAL,LOG_INFO,"Max Zet = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", ZetMax,k,j,i);
2208 }
2209 if (Gidx(i,j,k,user) == ICsiMaxArg) {
2210 LOG_ALLOW(LOCAL,LOG_INFO,"Max ICsi = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", ICsiMax,k,j,i);
2211 }
2212 if (Gidx(i,j,k,user) == IEtaMaxArg) {
2213 LOG_ALLOW(LOCAL,LOG_INFO,"Max IEta = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", IEtaMax,k,j,i);
2214 }
2215 if (Gidx(i,j,k,user) == IZetMaxArg) {
2216 LOG_ALLOW(LOCAL,LOG_INFO,"Max IZet = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", IZetMax,k,j,i);
2217 }
2218 if (Gidx(i,j,k,user) == JCsiMaxArg) {
2219 LOG_ALLOW(LOCAL,LOG_INFO,"Max JCsi = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", JCsiMax,k,j,i);
2220 }
2221 if (Gidx(i,j,k,user) == JEtaMaxArg) {
2222 LOG_ALLOW(LOCAL,LOG_INFO,"Max JEta = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", JEtaMax,k,j,i);
2223 }
2224 if (Gidx(i,j,k,user) == JZetMaxArg) {
2225 LOG_ALLOW(LOCAL,LOG_INFO,"Max JZet = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", JZetMax,k,j,i);
2226 }
2227 if (Gidx(i,j,k,user) == KCsiMaxArg) {
2228 LOG_ALLOW(LOCAL,LOG_INFO,"Max KCsi = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", KCsiMax,k,j,i);
2229 }
2230 if (Gidx(i,j,k,user) == KEtaMaxArg) {
2231 LOG_ALLOW(LOCAL,LOG_INFO,"Max KEta = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", KEtaMax,k,j,i);
2232 }
2233 if (Gidx(i,j,k,user) == KZetMaxArg) {
2234 LOG_ALLOW(LOCAL,LOG_INFO,"Max KZet = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", KZetMax,k,j,i);
2235 }
2236 if (Gidx(i,j,k,user) == AjMaxArg) {
2237 LOG_ALLOW(LOCAL,LOG_INFO,"Max Aj = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", AjMax,k,j,i);
2238 }
2239 if (Gidx(i,j,k,user) == IAjMaxArg) {
2240 LOG_ALLOW(LOCAL,LOG_INFO,"Max IAj = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", IAjMax,k,j,i);
2241 }
2242 if (Gidx(i,j,k,user) == JAjMaxArg) {
2243 LOG_ALLOW(LOCAL,LOG_INFO,"Max JAj = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", JAjMax,k,j,i);
2244 }
2245 if (Gidx(i,j,k,user) == KAjMaxArg) {
2246 LOG_ALLOW(LOCAL,LOG_INFO,"Max KAj = %le is at [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] \n", KAjMax,k,j,i);
2247 }
2248 }
2249 }
2250 }
2251
2252 /*
2253 VecView(user->lCsi,PETSC_VIEWER_STDOUT_WORLD);
2254 VecView(user->lEta,PETSC_VIEWER_STDOUT_WORLD);
2255 VecView(user->lZet,PETSC_VIEWER_STDOUT_WORLD);
2256 */
2257
2259
2260 PetscFunctionReturn(0);
2261}
2262
2263/**
2264 * @brief Implementation of \ref CalculateAllGridMetrics().
2265 * @see CalculateAllGridMetrics()
2266 */
2267PetscErrorCode CalculateAllGridMetrics(SimCtx *simCtx)
2268{
2269 PetscErrorCode ierr;
2270 UserMG *usermg = &simCtx->usermg;
2271 MGCtx *mgctx = usermg->mgctx;
2272 PetscInt nblk = simCtx->block_number;
2273
2274 PetscFunctionBeginUser;
2275
2277
2278 LOG_ALLOW(GLOBAL, LOG_INFO, "Calculating grid metrics for all levels and blocks...\n");
2279
2280 // Loop through all levels and all blocks
2281 for (PetscInt level = usermg->mglevels -1 ; level >=0; level--) {
2282 for (PetscInt bi = 0; bi < nblk; bi++) {
2283 UserCtx *user = &mgctx[level].user[bi];
2284 LOG_ALLOW_SYNC(LOCAL, LOG_DEBUG, "Rank %d: Calculating metrics for level %" PetscInt_FMT ", block %" PetscInt_FMT "\n", simCtx->rank, level, bi);
2285
2286 // Call the modern, modular helper functions for each UserCtx.
2287 // These functions are self-contained and operate on the data within the provided context.
2288 ierr = ComputeFaceMetrics(user); CHKERRQ(ierr);
2289 ierr = ComputeCellCenteredJacobianInverse(user); CHKERRQ(ierr);
2290 ierr = CheckAndFixGridOrientation(user); CHKERRQ(ierr);
2291 ierr = ComputeCellCenters(user); CHKERRQ(ierr);
2292 ierr = ComputeIFaceMetrics(user); CHKERRQ(ierr);
2293 ierr = ComputeJFaceMetrics(user); CHKERRQ(ierr);
2294 ierr = ComputeKFaceMetrics(user); CHKERRQ(ierr);
2295
2296 // Apply Periodic Boundary Condition Adjustments if necessary
2297 ierr = ApplyMetricsPeriodicBCs(user); CHKERRQ(ierr);
2298 // Diagnostics
2299 ierr = ComputeMetricNorms(user);
2300 if (level == usermg->mglevels - 1) {
2301 ierr = ComputeMetricsDivergence(user); CHKERRQ(ierr);
2302 }
2303 }
2304 }
2305
2306 // Face planes for particle search and interpolation, from the final coordinates and lCent.
2307 if (ParticlesEnabled(simCtx)) {
2308 for (PetscInt bi = 0; bi < nblk; bi++) {
2309 UserCtx *user = &mgctx[usermg->mglevels - 1].user[bi];
2310 ierr = MetricComputeFacePlanes(user); CHKERRQ(ierr);
2311 }
2312 }
2313
2314 LOG_ALLOW(GLOBAL, LOG_INFO, "Grid metrics calculation complete.\n");
2315
2317
2318 PetscFunctionReturn(0);
2319}
2320/* ------------------------------------------------------------------------- */
2321/* End of Metric.c */
PetscErrorCode ApplyMetricsPeriodicBCs(UserCtx *user)
(Orchestrator) Updates all metric-related fields in the local ghost cell regions for periodic boundar...
PetscErrorCode SynchronizePeriodicFaceFields(UserCtx *user, char face_direction, PetscInt num_fields, const FieldId field_ids[])
Synchronizes persistent fields belonging to one face family.
PetscErrorCode ComputeCellEdgeVectors(PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, Cmpnts edges[3])
Implementation of ComputeCellEdgeVectors().
Definition Metric.c:749
#define PICURV_AT(i, j, k)
PetscErrorCode MetricRestoreFacePlaneArrays(UserCtx *user, FacePlaneArrays *arrays)
Implementation of MetricRestoreFacePlaneArrays().
Definition Metric.c:401
PetscErrorCode CalculateAllGridMetrics(SimCtx *simCtx)
Implementation of CalculateAllGridMetrics().
Definition Metric.c:2267
static const PetscInt kPrimalFaceShift[3][3]
Offset from a face's lower-corner origin to where its family stores it (see Metric....
Definition Metric.c:147
static const FieldId kFacePlaneFields[]
The catalogued fields MetricComputeFacePlanes() owns.
Definition Metric.c:133
PetscErrorCode MetricJacobian(UserCtx *user, const Cmpnts ***X, PetscInt i, PetscInt j, PetscInt k, PetscReal xi, PetscReal eta, PetscReal zta, PetscReal J[3][3], PetscReal *detJ)
Implementation of MetricJacobian().
Definition Metric.c:502
PetscErrorCode ComputeCellCenters(UserCtx *user)
Implementation of ComputeCellCenters().
Definition Metric.c:1291
PetscBool MetricFacePlane(const Cmpnts v1, const Cmpnts v2, const Cmpnts v3, const Cmpnts v4, const Cmpnts cell_centroid, FacePlane *plane, PetscBool *degenerate)
Implementation of MetricFacePlane().
Definition Metric.c:47
PetscErrorCode InvertCovariantMetricTensor(double covariantTensor[3][3], double contravariantTensor[3][3])
Implementation of InvertCovariantMetricTensor().
Definition Metric.c:595
static PetscErrorCode FacePlaneFieldSlots(UserCtx *user, FieldId id, DM *dm, Vec **global, Vec **local)
Where a face-plane field's vectors live in user, and its DM.
Definition Metric.c:159
PetscErrorCode ComputeMetricNorms(UserCtx *user)
Implementation of ComputeMetricNorms().
Definition Metric.c:2137
static void TrilinearBlend(const Cmpnts V[8], PetscReal xi, PetscReal eta, PetscReal zta, Cmpnts *Xp)
Blend eight corner values at the supplied trilinear reference coordinates.
Definition Metric.c:451
static const FieldId kFacePlaneVectorFields[2][2][3]
The plane fields by family: [dual][normal = 0, centroid = 1][axis].
Definition Metric.c:140
static PetscInt Gidx(PetscInt i, PetscInt j, PetscInt k, UserCtx *user)
Convert logical cell indices into the flattened global cell identifier.
Definition Metric.c:2027
PetscErrorCode CheckAndFixGridOrientation(UserCtx *user)
Implementation of CheckAndFixGridOrientation().
Definition Metric.c:778
PetscErrorCode MetricGetCellVertices(UserCtx *user, const Cmpnts ***X, PetscInt i, PetscInt j, PetscInt k, Cmpnts V[8])
Implementation of MetricGetCellVertices().
Definition Metric.c:24
void MetricGatherCellFacePlanes(const FacePlaneArrays *arrays, const DMDALocalInfo *info, PetscInt i, PetscInt j, PetscInt k, CellFacePlanes *planes)
Implementation of MetricGatherCellFacePlanes().
Definition Metric.c:421
PetscErrorCode ComputeJFaceMetrics(UserCtx *user)
Implementation of ComputeJFaceMetrics().
Definition Metric.c:1646
PetscErrorCode MetricDestroyFacePlanes(UserCtx *user)
Implementation of MetricDestroyFacePlanes().
Definition Metric.c:355
PetscErrorCode ComputeMetricsDivergence(UserCtx *user)
Implementation of ComputeMetricsDivergence().
Definition Metric.c:2047
PetscErrorCode MetricComputeFacePlanes(UserCtx *user)
Implementation of MetricComputeFacePlanes().
Definition Metric.c:208
PetscErrorCode MetricGetFacePlaneArrays(UserCtx *user, PetscBool dual, FacePlaneArrays *arrays)
Implementation of MetricGetFacePlaneArrays().
Definition Metric.c:376
PetscErrorCode ComputeGridLineFractions(UserCtx *user, Vec fraction, Vec length)
Implementation of ComputeGridLineFractions().
Definition Metric.c:1362
PetscErrorCode ComputeFaceMetrics(UserCtx *user)
Implementation of ComputeFaceMetrics().
Definition Metric.c:937
PetscErrorCode ComputeCellCharacteristicLengthScale(PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, double *dx, double *dy, double *dz)
Implementation of ComputeCellCharacteristicLengthScale().
Definition Metric.c:688
PetscErrorCode CalculateFaceNormalAndArea(Cmpnts csi, Cmpnts eta, Cmpnts zet, double ni[3], double nj[3], double nk[3], double *Ai, double *Aj, double *Ak)
Implementation of CalculateFaceNormalAndArea().
Definition Metric.c:644
PetscErrorCode ApplyPeriodicCorrectionsToIFaceCenter(UserCtx *user)
Implementation of ApplyPeriodicCorrectionsToIFaceCenter().
Definition Metric.c:890
void MetricBuildCellFacePlanes(const Cell *cell, CellFacePlanes *planes)
Implementation of MetricBuildCellFacePlanes().
Definition Metric.c:108
PetscErrorCode ComputeCellCenteredJacobianInverse(UserCtx *user)
Implementation of ComputeCellCenteredJacobianInverse().
Definition Metric.c:1147
PetscErrorCode ComputeKFaceMetrics(UserCtx *user)
Implementation of ComputeKFaceMetrics().
Definition Metric.c:1837
PetscErrorCode ApplyPeriodicCorrectionsToJFaceCenter(UserCtx *user)
Implementation of ApplyPeriodicCorrectionsToJFaceCenter().
Definition Metric.c:906
PetscErrorCode ComputeCellDirectionalExtents(PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, double *l_xi, double *l_eta, double *l_zeta)
Implementation of ComputeCellDirectionalExtents().
Definition Metric.c:717
static Cmpnts MetricHexCentroid(const Cell *cell)
Centroid of a hexahedral cell: the mean of its eight vertices.
Definition Metric.c:87
static void MetricGatherHexVertices(const Cmpnts *X, const DMDALocalInfo *info, PetscInt i, PetscInt j, PetscInt k, Cell *hex)
The eight vertices of the cell whose origin node is (i, j, k), in RetrieveCurrentCell() order,...
Definition Metric.c:177
static void MetricFacePlaneLocals(const UserCtx *user, PetscBool dual, Vec normals[3], Vec centroids[3])
The local vectors of the primal or the dual planes, as MetricGetFacePlaneArrays() reads them.
Definition Metric.c:191
PetscErrorCode MetricLogicalToPhysical(UserCtx *user, const Cmpnts ***X, PetscInt i, PetscInt j, PetscInt k, PetscReal xi, PetscReal eta, PetscReal zta, Cmpnts *Xp)
Implementation of MetricLogicalToPhysical().
Definition Metric.c:475
PetscErrorCode ComputeIFaceMetrics(UserCtx *user)
Implementation of ComputeIFaceMetrics().
Definition Metric.c:1439
PetscErrorCode ApplyPeriodicCorrectionsToCellCenters(UserCtx *user)
Implementation of ApplyPeriodicCorrectionsToCellCenters().
Definition Metric.c:831
PetscErrorCode ApplyPeriodicCorrectionsToKFaceCenter(UserCtx *user)
Implementation of ApplyPeriodicCorrectionsToKFaceCenter().
Definition Metric.c:922
PetscErrorCode MetricVelocityContravariant(const PetscReal J[3][3], PetscReal detJ, const PetscReal u[3], PetscReal uc[3])
Implementation of MetricVelocityContravariant().
Definition Metric.c:560
const Cmpnts * normal[3]
Unit normals toward +i, +j, +k; zero where there is no plane.
Definition Metric.h:530
const Cmpnts * centroid[3]
Face vertex means.
Definition Metric.h:531
static PetscInt MetricCellFacePlanesIndex(const DMDALocalInfo *info, PetscInt i, PetscInt j, PetscInt k)
Index of point (i, j, k) in a block's ghosted local arrays (coordinates, lCent, planes).
Definition Metric.h:577
const PetscReal * size
Grid-cell sizes; NULL for dual cells.
Definition Metric.h:532
PetscBool dual
Dual-cell planes (from lCent) rather than grid-cell planes.
Definition Metric.h:529
Read-only flat arrays of one family of face planes (grid cells or dual cells).
Definition Metric.h:528
const FieldDescriptor * descriptor
PetscErrorCode FieldGetView(UserCtx *user, FieldId field_id, FieldView *view)
Resolve the existing DM and global/local vectors for one field.
FieldDMKind dm_kind
PetscErrorCode FieldGetDescriptor(FieldId field_id, const FieldDescriptor **descriptor)
Return immutable metadata for a valid field identifier.
@ FIELD_DM_DA
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_DUAL_PLANE_CENTROID_I
@ FIELD_ID_PLANE_NORMAL_J
@ FIELD_ID_JETA
@ FIELD_ID_CENTZ
@ FIELD_ID_PLANE_CENTROID_I
@ FIELD_ID_CSI
@ FIELD_ID_IAJ
@ FIELD_ID_PLANE_NORMAL_K
@ FIELD_ID_KETA
@ FIELD_ID_PLANE_CENTROID_K
@ FIELD_ID_JAJ
@ FIELD_ID_PLANE_NORMAL_I
@ FIELD_ID_KAJ
@ FIELD_ID_AJ
@ FIELD_ID_CENTY
@ FIELD_ID_DUAL_PLANE_CENTROID_J
@ FIELD_ID_DUAL_PLANE_CENTROID_K
@ FIELD_ID_KZET
@ FIELD_ID_IETA
@ FIELD_ID_ICSI
@ FIELD_ID_ETA
@ FIELD_ID_CENT
@ FIELD_ID_JCSI
@ FIELD_ID_JZET
@ FIELD_ID_PLANE_CENTROID_J
@ FIELD_ID_DUAL_PLANE_NORMAL_I
@ FIELD_ID_DUAL_PLANE_NORMAL_J
@ FIELD_ID_DUAL_PLANE_NORMAL_K
@ FIELD_ID_IZET
@ FIELD_ID_ZET
@ FIELD_ID_KCSI
@ FIELD_ID_CENTX
@ FIELD_ID_SEARCH_CELL_SIZE
Immutable metadata for one field identity.
Non-owning runtime objects resolved for one field and UserCtx.
#define LOG_LOOP_ALLOW(scope, level, iterVar, interval, fmt,...)
Logs a message inside a loop, but only every interval iterations.
Definition logging.h:380
#define LOG_ALLOW_SYNC(scope, level, fmt,...)
Synchronized logging macro that checks both the log level and whether the calling function is in the ...
Definition logging.h:273
#define LOCAL
Logging scope definitions for controlling message output.
Definition logging.h:45
#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
#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
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:31
@ LOG_WARNING
Non-critical issues that warrant attention.
Definition logging.h:30
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
@ LOG_VERBOSE
Extremely detailed logs, typically for development use only.
Definition logging.h:34
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
static void GetOwnedInteriorCellBounds(const DMDALocalInfo *info, PetscInt lo[3], PetscInt hi[3])
Index bounds of the interior cells this rank owns in the solver's cell-centred layout: the owned rang...
Definition setup.h:378
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2593
static void GetPeriodicAxes(const SimCtx *simCtx, PetscBool periodic[3])
Which grid axes are periodic.
Definition setup.h:361
Vec lCent
Definition variables.h:1244
@ PERIODIC
Definition variables.h:325
UserCtx * user
Definition variables.h:768
Vec lSearchCellSize
Definition variables.h:1249
PetscMPIInt rank
Definition variables.h:919
Cmpnts normal
Unit outward normal of (v2 - v1) x (v4 - v1); unnormalised when degenerate.
Definition variables.h:224
BoundaryFaceConfig boundary_faces[6]
Definition variables.h:1182
PetscInt block_number
Definition variables.h:1009
Vec lIEta
Definition variables.h:1254
Cmpnts centroid
Mean of the face's four vertices.
Definition variables.h:225
Vec lIZet
Definition variables.h:1254
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
Vec Centz
Definition variables.h:1252
Vec lPlaneCentroidK
Definition variables.h:1248
Vec lDualPlaneCentroidK
Definition variables.h:1251
Vec lDualPlaneNormalI
Definition variables.h:1250
UserMG usermg
Definition variables.h:1082
Vec lPlaneNormalK
Definition variables.h:1247
PetscInt _this
Definition variables.h:1175
Vec lKEta
Definition variables.h:1256
Vec lPlaneCentroidI
Definition variables.h:1248
static PetscBool ParticlesEnabled(const SimCtx *simCtx)
Whether the run carries particles: a seeded or restored population, or inlets that inject them.
Definition variables.h:1143
Vec lJCsi
Definition variables.h:1255
Vec lDualPlaneNormalJ
Definition variables.h:1250
PetscScalar x
Definition variables.h:145
PetscReal size
Mean edge length (GetCellCharacteristicSize()); 0 where unused.
Definition variables.h:232
Vec Centx
Definition variables.h:1252
Vec lPlaneCentroidJ
Definition variables.h:1248
Vec lKZet
Definition variables.h:1256
Vec lDualPlaneCentroidI
Definition variables.h:1251
Vec lJEta
Definition variables.h:1255
Vec lDualPlaneNormalK
Definition variables.h:1250
PetscInt thislevel
Definition variables.h:1269
PetscScalar z
Definition variables.h:145
Vec lKCsi
Definition variables.h:1256
Vec lDualPlaneCentroidJ
Definition variables.h:1251
Vec lCenty
Definition variables.h:1253
PetscInt mglevels
Definition variables.h:775
Vec lJZet
Definition variables.h:1255
PetscInt degenerate
Bit f set when face f's normal magnitude is below 1e-12.
Definition variables.h:231
Vec lCoor
Definition variables.h:1243
Vec lCentx
Definition variables.h:1253
Vec lICsi
Definition variables.h:1254
PetscInt GridOrientation
Definition variables.h:1175
DMDALocalInfo info
Definition variables.h:1169
PetscScalar y
Definition variables.h:145
Vec lPlaneNormalI
Definition variables.h:1247
Cmpnts periodic_translation[3]
Definition variables.h:1178
Face
Enumerates the six faces of a cubic cell for distance calculations.
Definition variables.h:196
@ TOP
Definition variables.h:197
@ FRONT
Definition variables.h:197
@ BOTTOM
Definition variables.h:197
@ BACK
Definition variables.h:197
@ LEFT
Definition variables.h:197
@ NUM_FACES
Definition variables.h:197
@ RIGHT
Definition variables.h:197
Vec SearchCellSize
Definition variables.h:1249
MGCtx * mgctx
Definition variables.h:778
PetscBool periodic_translation_valid[3]
Definition variables.h:1179
Vec lPlaneNormalJ
Definition variables.h:1247
BCType mathematical_type
Definition variables.h:426
Vec Centy
Definition variables.h:1252
Vec lCentz
Definition variables.h:1253
FacePlane face[NUM_FACES]
Face planes in LEFT..BACK order.
Definition variables.h:230
Cmpnts vertices[8]
Coordinates of the eight vertices of the cell.
Definition variables.h:219
BCFace
Identifies the six logical faces of a structured computational block.
Definition variables.h:308
@ BC_FACE_NEG_X
Definition variables.h:309
@ BC_FACE_POS_Z
Definition variables.h:311
@ BC_FACE_POS_Y
Definition variables.h:310
@ BC_FACE_NEG_Z
Definition variables.h:311
@ BC_FACE_POS_X
Definition variables.h:309
@ BC_FACE_NEG_Y
Definition variables.h:310
Defines the vertices of a single hexahedral grid cell.
Definition variables.h:218
The six face planes of one hexahedron, indexed by Face, and its characteristic size.
Definition variables.h:229
A 3D point or vector with PetscScalar components.
Definition variables.h:144
One cell face as the particle search tests against it: a plane through the vertex mean.
Definition variables.h:223
Context for Multigrid operations.
Definition variables.h:767
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
User-level context for managing the entire multigrid hierarchy.
Definition variables.h:774
PetscErrorCode GetCellCharacteristicSize(const Cell *cell, PetscReal *cellSize)
Estimates a characteristic length of the cell for threshold scaling.