PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
Metric.h File Reference
#include <petsc.h>
#include "variables.h"
#include "logging.h"
#include <stdlib.h>
#include "io.h"
#include "setup.h"
Include dependency graph for Metric.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  FacePlaneArrays
 Read-only flat arrays of one family of face planes (grid cells or dual cells). More...
 

Typedefs

typedef struct FacePlaneArrays FacePlaneArrays
 Read-only flat arrays of one family of face planes (grid cells or dual cells).
 

Functions

PetscErrorCode MetricLogicalToPhysical (UserCtx *user, const Cmpnts ***X, PetscInt i, PetscInt j, PetscInt k, PetscReal xi, PetscReal eta, PetscReal zta, Cmpnts *Xp)
 Maps a logical point inside one hexahedral cell to physical space.
 
PetscErrorCode MetricGetCellVertices (UserCtx *user, const Cmpnts ***X, PetscInt i, PetscInt j, PetscInt k, Cmpnts V[8])
 Collects the eight node coordinates of one logical hexahedral cell.
 
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)
 Evaluates the trilinear mapping Jacobian and its determinant in one cell.
 
PetscErrorCode MetricVelocityContravariant (const PetscReal J[3][3], PetscReal detJ, const PetscReal u[3], PetscReal uc[3])
 Converts a Cartesian velocity vector to contravariant logical components.
 
static Cmpnts RemoveFaceNormalComponent (Cmpnts velocity, Cmpnts metric)
 velocity less its component along the face metric metric, which leaves it tangent to the face.
 
PetscErrorCode CalculateFaceNormalAndArea (Cmpnts csi, Cmpnts eta, Cmpnts zet, double ni[3], double nj[3], double nk[3], double *Ai, double *Aj, double *Ak)
 Computes the unit normal vectors and areas of the three faces of a computational cell.
 
PetscErrorCode InvertCovariantMetricTensor (double covariantTensor[3][3], double contravariantTensor[3][3])
 Inverts the 3x3 matrix of face-area vectors to obtain the covariant directions.
 
PetscErrorCode ComputeCellCharacteristicLengthScale (PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, double *dx, double *dy, double *dz)
 Computes characteristic length scales (dx, dy, dz) for a curvilinear cell.
 
PetscErrorCode ComputeCellDirectionalExtents (PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, double *l_xi, double *l_eta, double *l_zeta)
 Computes a cell's extent along each of its own grid directions.
 
PetscErrorCode ComputeCellEdgeVectors (PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, Cmpnts edges[3])
 Computes a cell's edge vectors along its three grid directions.
 
PetscErrorCode ApplyPeriodicCorrectionsToCellCenters (UserCtx *user)
 Builds translated periodic images for the dummy cell centers of periodic axes.
 
PetscErrorCode ApplyPeriodicCorrectionsToIFaceCenter (UserCtx *user)
 Builds translated periodic images for i-face centers (Centx).
 
PetscErrorCode ApplyPeriodicCorrectionsToJFaceCenter (UserCtx *user)
 Builds translated periodic images for j-face centers (Centy).
 
PetscErrorCode ApplyPeriodicCorrectionsToKFaceCenter (UserCtx *user)
 Builds translated periodic images for k-face centers (Centz).
 
PetscErrorCode ComputeFaceMetrics (UserCtx *user)
 Computes the primary face metric components (Csi, Eta, Zet), including boundary extrapolation, and stores them in the corresponding global Vec members of the UserCtx structure (user->Csi, user->Eta, user->Zet).
 
PetscErrorCode ComputeCellCenteredJacobianInverse (UserCtx *user)
 Calculates the cell-centered inverse Jacobian determinant (1/J) for INTERIOR cells and stores it in user->Aj.
 
PetscErrorCode CheckAndFixGridOrientation (UserCtx *user)
 Verify and, when consistently inverted, repair the right-handed metric basis (Csi, Eta, Zet) and a positive Jacobian (Aj) over the whole domain.
 
PetscErrorCode ComputeCellCenters (UserCtx *user)
 Computes the physical location of each cell's center from the nodal coordinates.
 
PetscErrorCode ComputeGridLineFractions (UserCtx *user, Vec fraction, Vec length)
 Arc-length position of every owned cell centre along its three grid lines.
 
PetscErrorCode ComputeIFaceMetrics (UserCtx *user)
 Computes metrics centered on constant-i faces (i-faces).
 
PetscErrorCode ComputeJFaceMetrics (UserCtx *user)
 Computes metrics centered on constant-j faces (j-faces).
 
PetscErrorCode ComputeKFaceMetrics (UserCtx *user)
 Computes metrics centered on constant-k faces (k-faces).
 
PetscErrorCode ComputeMetricsDivergence (UserCtx *user)
 Performs a diagnostic check on the divergence of the face area metric vectors.
 
PetscErrorCode ComputeMetricNorms (UserCtx *user)
 Computes the max-min values of the grid metrics.
 
PetscErrorCode CalculateAllGridMetrics (SimCtx *simCtx)
 Orchestrates the calculation of all grid metrics.
 
PetscBool MetricFacePlane (const Cmpnts v1, const Cmpnts v2, const Cmpnts v3, const Cmpnts v4, const Cmpnts cell_centroid, FacePlane *plane, PetscBool *degenerate)
 Builds one face plane: the unit outward normal of (v2 - v1) x (v4 - v1) and the vertex mean.
 
void MetricBuildCellFacePlanes (const Cell *cell, CellFacePlanes *planes)
 Builds the six face planes of a hexahedron, as the particle search tests them.
 
PetscErrorCode MetricComputeFacePlanes (UserCtx *user)
 Builds the block's particle face planes once, as catalogued per-face fields.
 
PetscErrorCode MetricDestroyFacePlanes (UserCtx *user)
 Frees the face-plane fields MetricComputeFacePlanes() built, if any.
 
PetscErrorCode MetricGetFacePlaneArrays (UserCtx *user, PetscBool dual, FacePlaneArrays *arrays)
 Opens a family's face-plane arrays for reading; MetricComputeFacePlanes() must have run.
 
PetscErrorCode MetricRestoreFacePlaneArrays (UserCtx *user, FacePlaneArrays *arrays)
 Closes arrays opened by MetricGetFacePlaneArrays().
 
void MetricGatherCellFacePlanes (const FacePlaneArrays *arrays, const DMDALocalInfo *info, PetscInt i, PetscInt j, PetscInt k, CellFacePlanes *planes)
 Assembles one cell's six outward face planes from the shared per-face planes.
 
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).
 

Data Structure Documentation

◆ FacePlaneArrays

struct FacePlaneArrays

Read-only flat arrays of one family of face planes (grid cells or dual cells).

Definition at line 528 of file Metric.h.

Collaboration diagram for FacePlaneArrays:
[legend]
Data Fields
PetscBool dual Dual-cell planes (from lCent) rather than grid-cell planes.
const Cmpnts * normal[3] Unit normals toward +i, +j, +k; zero where there is no plane.
const Cmpnts * centroid[3] Face vertex means.
const PetscReal * size Grid-cell sizes; NULL for dual cells.

Typedef Documentation

◆ FacePlaneArrays

Read-only flat arrays of one family of face planes (grid cells or dual cells).

Function Documentation

◆ MetricLogicalToPhysical()

PetscErrorCode MetricLogicalToPhysical ( UserCtx *  user,
const Cmpnts ***  X,
PetscInt  i,
PetscInt  j,
PetscInt  k,
PetscReal  xi,
PetscReal  eta,
PetscReal  zta,
Cmpnts *  Xp 
)

Maps a logical point inside one hexahedral cell to physical space.

Uses trilinear interpolation of the eight cell vertices. Logical coordinates are measured from the cell's lower logical corner and normally lie in [0, 1].

Parameters
userGrid context used for cell indexing.
XGhosted node-coordinate array for the block.
iCell index in the xi direction.
jCell index in the eta direction.
kCell index in the zeta direction.
xiLocal xi coordinate within the cell.
etaLocal eta coordinate within the cell.
ztaLocal zeta coordinate within the cell.
[out]XpPhysical Cartesian position evaluated at the logical point.
Returns
PetscErrorCode 0 on success.

Maps a logical point inside one hexahedral cell to physical space.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricLogicalToPhysical()

Definition at line 475 of file Metric.c.

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}
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
PetscErrorCode MetricGetCellVertices(UserCtx *user, const Cmpnts ***X, PetscInt i, PetscInt j, PetscInt k, Cmpnts V[8])
Implementation of MetricGetCellVertices().
Definition Metric.c:24
#define PROFILE_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
Definition logging.h:1014
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:1029
A 3D point or vector with PetscScalar components.
Definition variables.h:144
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricGetCellVertices()

PetscErrorCode MetricGetCellVertices ( UserCtx *  user,
const Cmpnts ***  X,
PetscInt  i,
PetscInt  j,
PetscInt  k,
Cmpnts  V[8] 
)

Collects the eight node coordinates of one logical hexahedral cell.

Vertex ordering matches the trilinear metric kernels in this module.

Parameters
userGrid context used for cell indexing.
XGhosted node-coordinate array for the block.
iCell index in the xi direction.
jCell index in the eta direction.
kCell index in the zeta direction.
[out]VEight physical vertex coordinates in metric-kernel order.
Returns
PetscErrorCode 0 on success.

Collects the eight node coordinates of one logical hexahedral cell.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricGetCellVertices()

Definition at line 24 of file Metric.c.

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}
#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 GLOBAL
Scope for global logging across all processes.
Definition logging.h:46
@ LOG_VERBOSE
Extremely detailed logs, typically for development use only.
Definition logging.h:34
Here is the caller graph for this function:

◆ MetricJacobian()

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 
)

Evaluates the trilinear mapping Jacobian and its determinant in one cell.

The returned matrix is \(\partial(x,y,z)/\partial(\xi,\eta,\zeta)\); callers use its determinant to reject degenerate or inverted cells.

Parameters
userGrid context used for cell indexing.
XGhosted node-coordinate array for the block.
iCell index in the xi direction.
jCell index in the eta direction.
kCell index in the zeta direction.
xiLocal xi coordinate within the cell.
etaLocal eta coordinate within the cell.
ztaLocal zeta coordinate within the cell.
[out]JMapping Jacobian in row-major Cartesian/logical form.
[out]detJDeterminant of J.
Returns
PetscErrorCode 0 on success.

Evaluates the trilinear mapping Jacobian and its determinant in one cell.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricJacobian()

Definition at line 502 of file Metric.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricVelocityContravariant()

PetscErrorCode MetricVelocityContravariant ( const PetscReal  J[3][3],
PetscReal  detJ,
const PetscReal  u[3],
PetscReal  uc[3] 
)

Converts a Cartesian velocity vector to contravariant logical components.

Uses the inverse of the physical-to-logical Jacobian; detJ must describe the same mapping as J and must be nonzero.

Parameters
JMapping Jacobian at the evaluation point.
detJDeterminant of J.
uCartesian velocity components.
[out]ucContravariant xi/eta/zeta velocity components.
Returns
PetscErrorCode 0 on success.

Converts a Cartesian velocity vector to contravariant logical components.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricVelocityContravariant()

Definition at line 560 of file Metric.c.

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}
Here is the caller graph for this function:

◆ RemoveFaceNormalComponent()

static Cmpnts RemoveFaceNormalComponent ( Cmpnts  velocity,
Cmpnts  metric 
)
inlinestatic

velocity less its component along the face metric metric, which leaves it tangent to the face.

A face of zero area has no normal, and velocity is returned unchanged. Shared by the boundary handlers and the wall model, which both need the wall-parallel part of a velocity on a curvilinear face.

Parameters
velocityVelocity to project.
metricThe face's area vector (any length, any orientation).
Returns
The tangential part of velocity.

Definition at line 99 of file Metric.h.

100{
101 const PetscReal area_sq = metric.x * metric.x + metric.y * metric.y + metric.z * metric.z;
102 const PetscReal normal = (area_sq > 0.0)
103 ? (velocity.x * metric.x + velocity.y * metric.y + velocity.z * metric.z) / area_sq : 0.0;
104 Cmpnts tangential;
105
106 tangential.x = velocity.x - normal * metric.x;
107 tangential.y = velocity.y - normal * metric.y;
108 tangential.z = velocity.z - normal * metric.z;
109 return tangential;
110}
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
PetscScalar y
Definition variables.h:145
Here is the caller graph for this function:

◆ CalculateFaceNormalAndArea()

PetscErrorCode CalculateFaceNormalAndArea ( Cmpnts  csi,
Cmpnts  eta,
Cmpnts  zet,
double  ni[3],
double  nj[3],
double  nk[3],
double *  Ai,
double *  Aj,
double *  Ak 
)

Computes the unit normal vectors and areas of the three faces of a computational cell.

Given the metric vectors (csi, eta, zet), this function calculates the geometric properties of the cell faces aligned with the i, j, and k directions.

Parameters
csiCovariant xi metric vector.
etaCovariant eta metric vector.
zetCovariant zeta metric vector.
[out]niUnit normal of the xi-normal face.
[out]njUnit normal of the eta-normal face.
[out]nkUnit normal of the zeta-normal face.
[out]AiArea of the xi-normal face.
[out]AjArea of the eta-normal face.
[out]AkArea of the zeta-normal face.
Deprecated:
Unreachable from any executable: its only caller is ComputeCellCharacteristicLengthScale, which has none. Kept for now with its unit tests; do not add new callers - use ComputeCellDirectionalExtents or ComputeCellEdgeVectors, which do not depend on the cell's orientation.
Returns
PetscErrorCode 0 on success.

Computes the unit normal vectors and areas of the three faces of a computational cell.

See also
CalculateFaceNormalAndArea()

Definition at line 644 of file Metric.c.

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}
PetscErrorCode InvertCovariantMetricTensor(double covariantTensor[3][3], double contravariantTensor[3][3])
Implementation of InvertCovariantMetricTensor().
Definition Metric.c:595
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InvertCovariantMetricTensor()

PetscErrorCode InvertCovariantMetricTensor ( double  covariantTensor[3][3],
double  contravariantTensor[3][3] 
)

Inverts the 3x3 matrix of face-area vectors to obtain the covariant directions.

The caller fills the rows with csi, eta and zet - the face-area vectors, which are the contravariant basis scaled by the cell Jacobian. The inverse's columns are the covariant (tangent) directions dx/dxi, dx/deta, dx/dzeta divided by that Jacobian, which is what CalculateFaceNormalAndArea() normalizes.

Degeneracy is judged relative to the rows' own magnitude, because the determinant of area vectors scales as the square of the cell volume: an absolute floor would reject a well-formed cell purely for being small.

Parameters
covariantTensorInput: rows are the three face-area vectors.
[out]contravariantTensorInverse written in place.
Deprecated:
Unreachable from any executable: its only caller is CalculateFaceNormalAndArea, which is reached only from the deprecated ComputeCellCharacteristicLengthScale. Kept for now with its unit tests; do not add new callers - use ComputeCellDirectionalExtents or ComputeCellEdgeVectors, which do not depend on the cell's orientation.
Returns
PetscErrorCode 0 on success; PETSC_ERR_MAT_LU_ZRPVT when the rows are coplanar or one of them vanishes.

Inverts the 3x3 matrix of face-area vectors to obtain the covariant directions.

See also
InvertCovariantMetricTensor()

Definition at line 595 of file Metric.c.

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}
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
Definition logging.h:1033
Here is the caller graph for this function:

◆ ComputeCellCharacteristicLengthScale()

PetscErrorCode ComputeCellCharacteristicLengthScale ( PetscReal  ajc,
Cmpnts  csi,
Cmpnts  eta,
Cmpnts  zet,
double *  dx,
double *  dy,
double *  dz 
)

Computes characteristic length scales (dx, dy, dz) for a curvilinear cell.

For a non-uniform, non-orthogonal cell, there is no single "dx". This function computes an effective length scale in each Cartesian direction based on the cell volume and the areas of its faces: the three outputs are the Cartesian components of the cell diagonal, so they change when an otherwise identical cell is rotated. Use ComputeCellDirectionalExtents for a measure of the cell itself.

Parameters
ajcCell Jacobian/volume metric at the cell center.
csiCovariant xi metric vector.
etaCovariant eta metric vector.
zetCovariant zeta metric vector.
[out]dxEffective physical length associated with xi variation.
[out]dyEffective physical length associated with eta variation.
[out]dzEffective physical length associated with zeta variation.
Deprecated:
No production caller. Its outputs are Cartesian projections of the cell diagonal, so they change when an identical cell is rotated; the LES filter widths and the Clark term stopped using it for that reason. Kept for now with its unit tests; do not add new callers - use ComputeCellDirectionalExtents or ComputeCellEdgeVectors, which do not depend on the cell's orientation.
Returns
PetscErrorCode 0 on success.

Computes characteristic length scales (dx, dy, dz) for a curvilinear cell.

See also
ComputeCellCharacteristicLengthScale()

Definition at line 688 of file Metric.c.

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}
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeCellDirectionalExtents()

PetscErrorCode ComputeCellDirectionalExtents ( PetscReal  ajc,
Cmpnts  csi,
Cmpnts  eta,
Cmpnts  zet,
double *  l_xi,
double *  l_eta,
double *  l_zeta 
)

Computes a cell's extent along each of its own grid directions.

Each extent is the cell volume divided by the area of the face pair it crosses, which is the distance between those two faces. Unlike ComputeCellCharacteristicLengthScale, whose outputs are the Cartesian components of the cell diagonal and so change when the same cell is rotated, these depend on the cell alone: a filter width built from them is the same for a cell in a straight section and for an identical cell turned by a bend.

Parameters
ajcInverse cell volume at the cell center.
csiFace-area vector of the xi faces.
etaFace-area vector of the eta faces.
zetFace-area vector of the zeta faces.
[out]l_xiExtent across the xi faces.
[out]l_etaExtent across the eta faces.
[out]l_zetaExtent across the zeta faces.
Returns
PetscErrorCode 0 on success; PETSC_ERR_ARG_OUTOFRANGE for a non-positive Jacobian or a face of zero area.

Computes a cell's extent along each of its own grid directions.

Full API contract is documented with the header declaration in include/Metric.h.

See also
ComputeCellDirectionalExtents()

Definition at line 717 of file Metric.c.

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}
Here is the caller graph for this function:

◆ ComputeCellEdgeVectors()

PetscErrorCode ComputeCellEdgeVectors ( PetscReal  ajc,
Cmpnts  csi,
Cmpnts  eta,
Cmpnts  zet,
Cmpnts  edges[3] 
)

Computes a cell's edge vectors along its three grid directions.

Each is the covariant basis vector dx/dxi_k over one index step: the physical displacement from one cell to the next along that grid line, with both its direction and its length. Projecting a velocity gradient onto it gives the change in velocity across the cell in that direction, which is what a direction-resolved subgrid model needs and what does not depend on how the grid is oriented.

Parameters
ajcInverse cell volume at the cell center.
csiFace-area vector of the xi faces.
etaFace-area vector of the eta faces.
zetFace-area vector of the zeta faces.
[out]edgesEdge vectors along xi, eta and zeta, in that order.
Returns
PetscErrorCode 0 on success; PETSC_ERR_ARG_OUTOFRANGE for a non-positive Jacobian.

Computes a cell's edge vectors along its three grid directions.

Full API contract is documented with the header declaration in include/Metric.h.

See also
ComputeCellEdgeVectors()

Definition at line 749 of file Metric.c.

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}
Here is the caller graph for this function:

◆ ApplyPeriodicCorrectionsToCellCenters()

PetscErrorCode ApplyPeriodicCorrectionsToCellCenters ( UserCtx *  user)

Builds translated periodic images for the dummy cell centers of periodic axes.

PETSc wraps field indices but does not translate coordinates. This routine sets the dummy slot before the first cell of each periodic axis to the last cell's center minus the validated seam translation, and the slot after the last cell to the first cell's center plus it, axis by axis with a ghost refresh in between, so edge and corner dummies carry the translation of every periodic axis they cross. Grid spacing is not modified.

Parameters
userThe UserCtx containing grid and field data.
Returns
PetscErrorCode 0 on success.

Builds translated periodic images for the dummy cell centers of periodic axes.

See also
ApplyPeriodicCorrectionsToCellCenters()

Definition at line 831 of file Metric.c.

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}
@ FIELD_ID_CENT
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2593
Vec lCent
Definition variables.h:1244
@ PERIODIC
Definition variables.h:325
BoundaryFaceConfig boundary_faces[6]
Definition variables.h:1182
DMDALocalInfo info
Definition variables.h:1169
Cmpnts periodic_translation[3]
Definition variables.h:1178
PetscBool periodic_translation_valid[3]
Definition variables.h:1179
BCType mathematical_type
Definition variables.h:426
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_NEG_Z
Definition variables.h:311
@ BC_FACE_NEG_Y
Definition variables.h:310
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ApplyPeriodicCorrectionsToIFaceCenter()

PetscErrorCode ApplyPeriodicCorrectionsToIFaceCenter ( UserCtx *  user)

Builds translated periodic images for i-face centers (Centx).

Every active periodic axis can affect the ghost images of Centx. This must be called after Centx is computed and before it is used for metric calculations.

Parameters
userThe UserCtx containing grid and field data.
Returns
PetscErrorCode 0 on success.

Builds translated periodic images for i-face centers (Centx).

See also
ApplyPeriodicCorrectionsToIFaceCenter()

Definition at line 890 of file Metric.c.

891{
892 const FieldId fields[] = {FIELD_ID_CENTX};
893
894 PetscFunctionBeginUser;
896 PetscCall(SynchronizePeriodicFaceFields(user, 'i', 1, fields));
898 PetscFunctionReturn(0);
899}
PetscErrorCode SynchronizePeriodicFaceFields(UserCtx *user, char face_direction, PetscInt num_fields, const FieldId field_ids[])
Synchronizes persistent fields belonging to one face family.
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_CENTX
Here is the call graph for this function:

◆ ApplyPeriodicCorrectionsToJFaceCenter()

PetscErrorCode ApplyPeriodicCorrectionsToJFaceCenter ( UserCtx *  user)

Builds translated periodic images for j-face centers (Centy).

Every active periodic axis can affect the ghost images of Centy. This must be called after Centy is computed and before it is used for metric calculations.

Parameters
userThe UserCtx containing grid and field data.
Returns
PetscErrorCode 0 on success.

Builds translated periodic images for j-face centers (Centy).

See also
ApplyPeriodicCorrectionsToJFaceCenter()

Definition at line 906 of file Metric.c.

907{
908 const FieldId fields[] = {FIELD_ID_CENTY};
909
910 PetscFunctionBeginUser;
912 PetscCall(SynchronizePeriodicFaceFields(user, 'j', 1, fields));
914 PetscFunctionReturn(0);
915}
@ FIELD_ID_CENTY
Here is the call graph for this function:

◆ ApplyPeriodicCorrectionsToKFaceCenter()

PetscErrorCode ApplyPeriodicCorrectionsToKFaceCenter ( UserCtx *  user)

Builds translated periodic images for k-face centers (Centz).

Every active periodic axis can affect the ghost images of Centz. This must be called after Centz is computed and before it is used for metric calculations.

Parameters
userThe UserCtx containing grid and field data.
Returns
PetscErrorCode 0 on success.

Builds translated periodic images for k-face centers (Centz).

See also
ApplyPeriodicCorrectionsToKFaceCenter()

Definition at line 922 of file Metric.c.

923{
924 const FieldId fields[] = {FIELD_ID_CENTZ};
925
926 PetscFunctionBeginUser;
928 PetscCall(SynchronizePeriodicFaceFields(user, 'k', 1, fields));
930 PetscFunctionReturn(0);
931}
@ FIELD_ID_CENTZ
Here is the call graph for this function:

◆ ComputeFaceMetrics()

PetscErrorCode ComputeFaceMetrics ( UserCtx *  user)

Computes the primary face metric components (Csi, Eta, Zet), including boundary extrapolation, and stores them in the corresponding global Vec members of the UserCtx structure (user->Csi, user->Eta, user->Zet).

This is a self-contained routine that performs the following steps:

  1. Obtains local ghosted nodal coordinates using DMGetCoordinatesLocal.
  2. Calculates metrics for INTERIOR faces where finite difference stencils are valid.
  3. EXTRAPOLATES metrics for faces on the physical domain boundaries by copying from the nearest computed interior face.
  4. Assembles the global user->Csi, user->Eta, user->Zet Vecs.
  5. Updates the local ghosted user->lCsi, user->lEta, user->lZet Vecs.
Parameters
[in,out]userPointer to the UserCtx structure.
Returns
PetscErrorCode 0 on success.
Note
  • This function is a complete "compute and make ready" unit for Csi, Eta, and Zet.
  • It's recommended to call VecZeroEntries on user->Csi, Eta, Zet before this if they might contain old data.

Computes the primary face metric components (Csi, Eta, Zet), including boundary extrapolation, and stores them in the corresponding global Vec members of the UserCtx structure (user->Csi, user->Eta, user->Zet).

See also
ComputeFaceMetrics()

Definition at line 937 of file Metric.c.

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}
@ FIELD_ID_CSI
@ FIELD_ID_ETA
@ FIELD_ID_ZET
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
Definition logging.h:214
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:31
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
Vec lCoor
Definition variables.h:1243
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeCellCenteredJacobianInverse()

PetscErrorCode ComputeCellCenteredJacobianInverse ( UserCtx *  user)

Calculates the cell-centered inverse Jacobian determinant (1/J) for INTERIOR cells and stores it in user->Aj.

This version includes boundary extrapolation.

Nodal coordinates are obtained internally. Refer to previous Doxygen comments for details on physical locations and storage convention (aj_arr[k_n][j_n][i_n] for cell C(i_n-1,j_n-1,k_n-1)).

Parameters
[in,out]userPointer to the UserCtx structure.
Returns
PetscErrorCode 0 on success.

Calculates the cell-centered inverse Jacobian determinant (1/J) for INTERIOR cells and stores it in user->Aj.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
ComputeCellCenteredJacobianInverse()

Definition at line 1147 of file Metric.c.

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}
@ FIELD_ID_AJ
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckAndFixGridOrientation()

PetscErrorCode CheckAndFixGridOrientation ( UserCtx *  user)

Verify and, when consistently inverted, repair the right-handed metric basis (Csi, Eta, Zet) and a positive Jacobian (Aj) over the whole domain.

The metric-generation kernels are completely algebraic, so they will happily deliver a left-handed basis if the mesh file enumerates nodes in the opposite ζ-direction.
This routine makes the orientation explicit and—if needed—repairs it once per run:

Step Action
1 Compute global Aj_min, Aj_max.
2 Mixed signs (Aj_min < 0 && Aj_max > 0) → abort: the mesh is topologically inconsistent.
3 All negative (Aj_max < 0) → abort: the grid is left-handed. Negating the metric vectors is not a valid repair (a mirrored duct solved that way was wrong by a factor of forty), so the grid must be renumbered along one logical axis instead.
4 Store user->GridOrientation = +1, the only orientation the solver accepts.
Parameters
[in,out]userFully initialised UserCtx that already contains
Csi, Eta, Zet, Aj, their local ghosts, and valid distributed DMs.
Returns
0 on success or a PETSc error code on failure.
Note
Call immediately after ComputeCellCenteredJacobianInverse() and before any routine that differentiates or applies BCs.
Author metadata intentionally omitted in API docs.

Verify and, when consistently inverted, repair the right-handed metric basis (Csi, Eta, Zet) and a positive Jacobian (Aj) over the whole domain.

See also
CheckAndFixGridOrientation()

Definition at line 778 of file Metric.c.

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}
#define LOCAL
Logging scope definitions for controlling message output.
Definition logging.h:45
PetscMPIInt rank
Definition variables.h:919
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
PetscInt GridOrientation
Definition variables.h:1175
Here is the caller graph for this function:

◆ ComputeCellCenters()

PetscErrorCode ComputeCellCenters ( UserCtx *  user)

Computes the physical location of each cell's center from the nodal coordinates.

Parameters
userThe UserCtx for a specific grid level. The function populates user->Cent.
Returns
PetscErrorCode 0 on success, or a PETSc error code on failure.

Computes the physical location of each cell's center from the nodal coordinates.

See also
ComputeCellCenters()

Definition at line 1291 of file Metric.c.

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}
PetscErrorCode ApplyPeriodicCorrectionsToCellCenters(UserCtx *user)
Implementation of ApplyPeriodicCorrectionsToCellCenters().
Definition Metric.c:831
PetscInt _this
Definition variables.h:1175
PetscInt thislevel
Definition variables.h:1269
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeGridLineFractions()

PetscErrorCode ComputeGridLineFractions ( UserCtx *  user,
Vec  fraction,
Vec  length 
)

Arc-length position of every owned cell centre along its three grid lines.

For each grid line through an interior cell, the line's length is the sum of the cell edge lengths along it (ComputeCellEdgeVectors()), and a cell's fraction is the length before it plus half its own, over that total: 0 at the line's negative face and 1 at its positive face, proportional to distance along the line, so it is the same on uniform and stretched grids. Lines that cross ranks are summed with a scan along the line. Collective on PETSC_COMM_WORLD; needs the face metrics and the Jacobian with their local ghosts.

Parameters
[in]userBlock and level whose grid lines are measured.
[out]fractionGlobal vector on user->fda: the xi, eta, zeta fractions in .x, .y, .z at interior cells; other entries are untouched.
[out]lengthGlobal vector on user->fda: the lengths of the same three lines, in solver units.
Returns
Zero on success.

Arc-length position of every owned cell centre along its three grid lines.

Full API contract is documented with the header declaration in include/Metric.h.

See also
ComputeGridLineFractions()

Definition at line 1362 of file Metric.c.

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}
PetscErrorCode ComputeCellEdgeVectors(PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, Cmpnts edges[3])
Implementation of ComputeCellEdgeVectors().
Definition Metric.c:749
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeIFaceMetrics()

PetscErrorCode ComputeIFaceMetrics ( UserCtx *  user)

Computes metrics centered on constant-i faces (i-faces).

This function calculates the metric terms (ICsi, IEta, IZet) and the inverse Jacobian (IAj) located at the center of each i-face. The stencils use i-face-centered coordinates (Centx) which must be computed first. The logic is a direct adaptation of the legacy FormMetrics function.

Parameters
userThe UserCtx for a specific grid level. Populates user->ICsi, etc.
Returns
PetscErrorCode 0 on success, or a PETSc error code on failure.

Computes metrics centered on constant-i faces (i-faces).

See also
ComputeIFaceMetrics()

Definition at line 1439 of file Metric.c.

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}
@ FIELD_ID_IAJ
@ FIELD_ID_IETA
@ FIELD_ID_ICSI
@ FIELD_ID_IZET
Vec Centx
Definition variables.h:1252
Vec lCentx
Definition variables.h:1253
@ BC_FACE_POS_Z
Definition variables.h:311
@ BC_FACE_POS_Y
Definition variables.h:310
@ BC_FACE_POS_X
Definition variables.h:309
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeJFaceMetrics()

PetscErrorCode ComputeJFaceMetrics ( UserCtx *  user)

Computes metrics centered on constant-j faces (j-faces).

This function calculates the metric terms (JCsi, JEta, JZet) and the inverse Jacobian (JAj) located at the geometric center of each constant-j face. This is a critical step for staggered-grid finite difference schemes.

The process is a direct and faithful refactoring of the corresponding logic from the legacy FormMetrics function:

  1. It first calculates the physical (x,y,z) coordinates of the center of each i-face and stores them in the user->Centy vector.
  2. It then uses a boundary-aware, second-order finite difference stencil on the Centy field to compute the derivatives (e.g., d(x)/d(csi)).
    • Central differences are used in the grid interior.
    • One-sided differences are used at the physical domain boundaries.
  3. Finally, these derivatives are used to compute the final metric terms and the inverse Jacobian, which are stored in their respective Vec objects.
Parameters
userThe UserCtx for a specific grid level. This function populates the user->JCsi, user->JEta, user->JZet, and user->JAj vectors.
Returns
PetscErrorCode 0 on success, or a PETSc error code on failure.

Computes metrics centered on constant-j faces (j-faces).

See also
ComputeJFaceMetrics()

Definition at line 1646 of file Metric.c.

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}
@ FIELD_ID_JETA
@ FIELD_ID_JAJ
@ FIELD_ID_JCSI
@ FIELD_ID_JZET
Vec lCenty
Definition variables.h:1253
Vec Centy
Definition variables.h:1252
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeKFaceMetrics()

PetscErrorCode ComputeKFaceMetrics ( UserCtx *  user)

Computes metrics centered on constant-k faces (k-faces).

This function calculates the metric terms (KCsi, KEta, KZet) and the inverse Jacobian (KAj) located at the geometric center of each constant-j face. This is a critical step for staggered-grid finite difference schemes.

The process is a direct and faithful refactoring of the corresponding logic from the legacy FormMetrics function:

  1. It first calculates the physical (x,y,z) coordinates of the center of each i-face and stores them in the user->Centz vector.
  2. It then uses a boundary-aware, second-order finite difference stencil on the Centz field to compute the derivatives (e.g., d(x)/d(csi)).
    • Central differences are used in the grid interior.
    • One-sided differences are used at the physical domain boundaries.
  3. Finally, these derivatives are used to compute the final metric terms and the inverse Jacobian, which are stored in their respective Vec objects.
Parameters
userThe UserCtx for a specific grid level. This function populates the user->KCsi, user->KEta, user->KZet, and user->KAj vectors.
Returns
PetscErrorCode 0 on success, or a PETSc error code on failure.

Computes metrics centered on constant-k faces (k-faces).

See also
ComputeKFaceMetrics()

Definition at line 1837 of file Metric.c.

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}
@ FIELD_ID_KETA
@ FIELD_ID_KAJ
@ FIELD_ID_KZET
@ FIELD_ID_KCSI
Vec Centz
Definition variables.h:1252
Vec lCentz
Definition variables.h:1253
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeMetricsDivergence()

PetscErrorCode ComputeMetricsDivergence ( UserCtx *  user)

Performs a diagnostic check on the divergence of the face area metric vectors.

For a closed cell, the sum of the face area vectors should be zero (Gauss's divergence theorem). This function computes a measure of this divergence and reports the maximum value over the domain. A small value indicates a well-formed grid. This is a direct adaptation of the legacy function.

Parameters
userThe UserCtx for a specific grid level (typically the finest).
Returns
PetscErrorCode 0 on success, or a PETSc error code on failure.

Performs a diagnostic check on the divergence of the face area metric vectors.

See also
ComputeMetricsDivergence()

Definition at line 2047 of file Metric.c.

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}
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeMetricNorms()

PetscErrorCode ComputeMetricNorms ( UserCtx *  user)

Computes the max-min values of the grid metrics.

This function serves as a diagnostic tool to assess the quality of the grid metrics. It calculates the bounds of the face metrics (Csi, Eta, Zet).

Parameters
userThe UserCtx, containing all necessary grid data.
Returns
PetscErrorCode

Computes the max-min values of the grid metrics.

See also
ComputeMetricNorms()

Definition at line 2137 of file Metric.c.

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}
Vec lIEta
Definition variables.h:1254
Vec lIZet
Definition variables.h:1254
Vec lKEta
Definition variables.h:1256
Vec lJCsi
Definition variables.h:1255
Vec lKZet
Definition variables.h:1256
Vec lJEta
Definition variables.h:1255
Vec lKCsi
Definition variables.h:1256
Vec lJZet
Definition variables.h:1255
Vec lICsi
Definition variables.h:1254
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CalculateAllGridMetrics()

PetscErrorCode CalculateAllGridMetrics ( SimCtx *  simCtx)

Orchestrates the calculation of all grid metrics.

This function iterates through every UserCtx in the multigrid and multi-block hierarchy. For each context, it calls a series of modern, modular helper functions to compute the face metrics (Csi, Eta, Zet), the cell-centered inverse Jacobian (Aj), and to validate the grid's orientation.

Parameters
simCtxThe master SimCtx, containing the configured UserCtx hierarchy.
Returns
PetscErrorCode

Orchestrates the calculation of all grid metrics.

See also
CalculateAllGridMetrics()

Definition at line 2267 of file Metric.c.

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}
PetscErrorCode ApplyMetricsPeriodicBCs(UserCtx *user)
(Orchestrator) Updates all metric-related fields in the local ghost cell regions for periodic boundar...
PetscErrorCode ComputeCellCenters(UserCtx *user)
Implementation of ComputeCellCenters().
Definition Metric.c:1291
PetscErrorCode ComputeMetricNorms(UserCtx *user)
Implementation of ComputeMetricNorms().
Definition Metric.c:2137
PetscErrorCode CheckAndFixGridOrientation(UserCtx *user)
Implementation of CheckAndFixGridOrientation().
Definition Metric.c:778
PetscErrorCode ComputeJFaceMetrics(UserCtx *user)
Implementation of ComputeJFaceMetrics().
Definition Metric.c:1646
PetscErrorCode ComputeMetricsDivergence(UserCtx *user)
Implementation of ComputeMetricsDivergence().
Definition Metric.c:2047
PetscErrorCode MetricComputeFacePlanes(UserCtx *user)
Implementation of MetricComputeFacePlanes().
Definition Metric.c:208
PetscErrorCode ComputeFaceMetrics(UserCtx *user)
Implementation of ComputeFaceMetrics().
Definition Metric.c:937
PetscErrorCode ComputeCellCenteredJacobianInverse(UserCtx *user)
Implementation of ComputeCellCenteredJacobianInverse().
Definition Metric.c:1147
PetscErrorCode ComputeKFaceMetrics(UserCtx *user)
Implementation of ComputeKFaceMetrics().
Definition Metric.c:1837
PetscErrorCode ComputeIFaceMetrics(UserCtx *user)
Implementation of ComputeIFaceMetrics().
Definition Metric.c:1439
#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
UserCtx * user
Definition variables.h:768
PetscInt block_number
Definition variables.h:1009
UserMG usermg
Definition variables.h:1082
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
PetscInt mglevels
Definition variables.h:775
MGCtx * mgctx
Definition variables.h:778
Context for Multigrid operations.
Definition variables.h:767
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricFacePlane()

PetscBool MetricFacePlane ( const Cmpnts  v1,
const Cmpnts  v2,
const Cmpnts  v3,
const Cmpnts  v4,
const Cmpnts  cell_centroid,
FacePlane *  plane,
PetscBool *  degenerate 
)

Builds one face plane: the unit outward normal of (v2 - v1) x (v4 - v1) and the vertex mean.

The single definition of a cell face plane: the per-cell planes of MetricBuildCellFacePlanes() and the precomputed per-face planes both use it, so their distances are bit-identical. The normal points away from cell_centroid. A degenerate face (normal magnitude below 1e-12) is reported through degenerate and left unnormalised.

Parameters
[in]v1First face vertex; the normal's origin.
[in]v2Second face vertex; the normal's first edge is v2 - v1.
[in]v3Third face vertex; it enters only the centroid.
[in]v4Fourth face vertex; the normal's second edge is v4 - v1.
[in]cell_centroidCentroid of the cell the face belongs to, which fixes the outward side.
[out]planeThe face plane.
[out]degenerateWhether the normal magnitude is below 1e-12.
Returns
PETSC_TRUE when the orientation is ambiguous (the face plane contains the centroid offset).

Builds one face plane: the unit outward normal of (v2 - v1) x (v4 - v1) and the vertex mean.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricFacePlane()

Definition at line 47 of file Metric.c.

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}
Cmpnts normal
Unit outward normal of (v2 - v1) x (v4 - v1); unnormalised when degenerate.
Definition variables.h:224
Cmpnts centroid
Mean of the face's four vertices.
Definition variables.h:225
Here is the caller graph for this function:

◆ MetricBuildCellFacePlanes()

void MetricBuildCellFacePlanes ( const Cell *  cell,
CellFacePlanes *  planes 
)

Builds the six face planes of a hexahedron, as the particle search tests them.

Face vertex orders and orientation are those CalculateDistancesToCellFaces() has always used; degenerate faces set their bit in degenerate. size is set to 0. This per-cell form serves callers holding only a Cell; MetricComputeFacePlanes() precomputes the shared per-face form.

Parameters
[in]cellThe cell, vertices in RetrieveCurrentCell() order.
[out]planesThe cell's face planes.

Builds the six face planes of a hexahedron, as the particle search tests them.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricBuildCellFacePlanes()

Definition at line 108 of file Metric.c.

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}
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
static Cmpnts MetricHexCentroid(const Cell *cell)
Centroid of a hexahedral cell: the mean of its eight vertices.
Definition Metric.c:87
@ LOG_WARNING
Non-critical issues that warrant attention.
Definition logging.h:30
PetscReal size
Mean edge length (GetCellCharacteristicSize()); 0 where unused.
Definition variables.h:232
PetscInt degenerate
Bit f set when face f's normal magnitude is below 1e-12.
Definition variables.h:231
@ 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
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricComputeFacePlanes()

PetscErrorCode MetricComputeFacePlanes ( UserCtx *  user)

Builds the block's particle face planes once, as catalogued per-face fields.

Each face carries one plane, shared by the two cells on either side: a unit normal pointing toward +i, +j or +k and the mean of its four vertices, built by MetricFacePlane() in the vertex order of the minus face (LEFT, BOTTOM, BACK) of the cell above it and oriented by that cell (by the cell below where only it is local). Grid-cell faces come from the node coordinates and are stored in the I/J/K-face layouts (PlaneNormal*, PlaneCentroid*: the face whose lower corner is node (i,j,k) sits at [k+1][j+1][i] for I, [k+1][j][i+1] for J, [k][j+1][i+1] for K); dual-cell faces come from lCent and sit at their lower-corner cell centre (DualPlaneNormal*, DualPlaneCentroid*). SearchCellSize holds each cell's GetCellCharacteristicSize() at its shifted cell-centred index. A face with no plane (degenerate, or beyond the ghosted region) has a zero normal. Owned values are the global fields; the locals are their ghost update through UpdateLocalGhosts() and the fields' catalogued sync classes. Collective. Built by CalculateAllGridMetrics() on the finest level of a particle run, after the coordinates and lCent are final; a no-op once built. Coordinates are fixed thereafter (there are no moving grids); a grid that changes must MetricDestroyFacePlanes() and rebuild.

Parameters
[in,out]userBlock whose planes are built; MetricDestroyFacePlanes() frees them.
Returns
PetscErrorCode 0 on success, or a PETSc error.

Builds the block's particle face planes once, as catalogued per-face fields.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricComputeFacePlanes()

Definition at line 208 of file Metric.c.

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}
#define PICURV_AT(i, j, k)
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
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
static const FieldId kFacePlaneVectorFields[2][2][3]
The plane fields by family: [dual][normal = 0, centroid = 1][axis].
Definition Metric.c:140
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 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 FieldDescriptor * descriptor
PetscErrorCode FieldGetView(UserCtx *user, FieldId field_id, FieldView *view)
Resolve the existing DM and global/local vectors for one field.
Non-owning runtime objects resolved for one field and UserCtx.
static void GetPeriodicAxes(const SimCtx *simCtx, PetscBool periodic[3])
Which grid axes are periodic.
Definition setup.h:361
Vec lSearchCellSize
Definition variables.h:1249
Vec SearchCellSize
Definition variables.h:1249
Defines the vertices of a single hexahedral grid cell.
Definition variables.h:218
One cell face as the particle search tests against it: a plane through the vertex mean.
Definition variables.h:223
PetscErrorCode GetCellCharacteristicSize(const Cell *cell, PetscReal *cellSize)
Estimates a characteristic length of the cell for threshold scaling.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricDestroyFacePlanes()

PetscErrorCode MetricDestroyFacePlanes ( UserCtx *  user)

Frees the face-plane fields MetricComputeFacePlanes() built, if any.

Parameters
[in,out]userBlock whose planes are freed; their Vecs are reset to NULL.
Returns
PetscErrorCode 0 on success, or a PETSc error.

Frees the face-plane fields MetricComputeFacePlanes() built, if any.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricDestroyFacePlanes()

Definition at line 355 of file Metric.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricGetFacePlaneArrays()

PetscErrorCode MetricGetFacePlaneArrays ( UserCtx *  user,
PetscBool  dual,
FacePlaneArrays *  arrays 
)

Opens a family's face-plane arrays for reading; MetricComputeFacePlanes() must have run.

Parameters
[in]userBlock whose planes are read.
[in]dualPETSC_TRUE for the dual-cell planes, PETSC_FALSE for the grid-cell planes.
[out]arraysThe open arrays; close with MetricRestoreFacePlaneArrays().
Returns
PetscErrorCode 0 on success, or PETSC_ERR_ARG_WRONGSTATE if the planes are not built.

Opens a family's face-plane arrays for reading; MetricComputeFacePlanes() must have run.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricGetFacePlaneArrays()

Definition at line 376 of file Metric.c.

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}
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
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
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricRestoreFacePlaneArrays()

PetscErrorCode MetricRestoreFacePlaneArrays ( UserCtx *  user,
FacePlaneArrays *  arrays 
)

Closes arrays opened by MetricGetFacePlaneArrays().

Parameters
[in]userBlock whose planes were read.
[in,out]arraysThe arrays to close.
Returns
PetscErrorCode 0 on success, or a PETSc error.

Closes arrays opened by MetricGetFacePlaneArrays().

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricRestoreFacePlaneArrays()

Definition at line 401 of file Metric.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricGatherCellFacePlanes()

void MetricGatherCellFacePlanes ( const FacePlaneArrays *  arrays,
const DMDALocalInfo *  info,
PetscInt  i,
PetscInt  j,
PetscInt  k,
CellFacePlanes *  planes 
)

Assembles one cell's six outward face planes from the shared per-face planes.

A cell's plus faces use the stored normals and its minus faces their exact negation; a face whose stored normal is zero sets its degenerate bit. For a grid cell (i, j, k) is its origin node; for a dual cell, the lower-corner cell centre.

Parameters
[in]arraysOpen plane arrays.
[in]infoThe block's DMDA local info, for its ghosted extents.
[in]iCell origin i index.
[in]jCell origin j index.
[in]kCell origin k index.
[out]planesThe cell's planes, with size from SearchCellSize (0 for dual cells).

Assembles one cell's six outward face planes from the shared per-face planes.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/Metric.h.

See also
MetricGatherCellFacePlanes()

Definition at line 421 of file Metric.c.

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}
Face
Enumerates the six faces of a cubic cell for distance calculations.
Definition variables.h:196
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MetricCellFacePlanesIndex()

static PetscInt MetricCellFacePlanesIndex ( const DMDALocalInfo *  info,
PetscInt  i,
PetscInt  j,
PetscInt  k 
)
inlinestatic

Index of point (i, j, k) in a block's ghosted local arrays (coordinates, lCent, planes).

Parameters
[in]infoThe block's DMDA local info, for its ghosted extents.
[in]iGlobal i index, within the ghosted range.
[in]jGlobal j index, within the ghosted range.
[in]kGlobal k index, within the ghosted range.
Returns
The flat index, x fastest.

Definition at line 577 of file Metric.h.

578{
579 const PetscInt di = i - info->gxs, dj = j - info->gys, dk = k - info->gzs;
580 return (dk * info->gym + dj) * info->gxm + di;
581}
Here is the caller graph for this function: