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

C unit tests for interpolation and signed-distance geometry helpers. More...

#include "test_support.h"
#include "interpolation.h"
#include "walkingsearch.h"
Include dependency graph for test_geometry.c:

Go to the source code of this file.

Functions

static PetscErrorCode TestScalarInterpolationConstantField (void)
 Tests scalar interpolation on a constant field.
 
static PetscErrorCode TestVectorInterpolationConstantField (void)
 Tests vector interpolation on a constant field.
 
static PetscErrorCode TestSignedDistanceAndClassification (void)
 Tests signed-distance computation and inside/outside classification.
 
int main (int argc, char **argv)
 Runs the unit-geometry PETSc test binary.
 

Detailed Description

C unit tests for interpolation and signed-distance geometry helpers.

Definition in file test_geometry.c.

Function Documentation

◆ TestScalarInterpolationConstantField()

static PetscErrorCode TestScalarInterpolationConstantField ( void  )
static

Tests scalar interpolation on a constant field.

Definition at line 14 of file test_geometry.c.

15{
16 SimCtx *simCtx = NULL;
17 UserCtx *user = NULL;
18 Vec input = NULL, output = NULL;
19 PetscReal ***in_arr = NULL, ***out_arr = NULL;
20
21 PetscFunctionBeginUser;
22 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
23 PetscCall(VecDuplicate(user->P, &input));
24 PetscCall(VecDuplicate(user->P, &output));
25 PetscCall(VecSet(input, 8.0));
26 PetscCall(VecSet(output, 0.0));
27
28 PetscCall(DMDAVecGetArrayRead(user->da, input, &in_arr));
29 PetscCall(DMDAVecGetArray(user->da, output, &out_arr));
30 PetscCall(InterpolateFieldFromCornerToCenter_Scalar(in_arr, out_arr, user));
31 PetscCall(PicurvAssertRealNear(8.0, out_arr[1][1][1], 1.0e-12, "scalar corner->center preserves constants"));
32 PetscCall(DMDAVecRestoreArrayRead(user->da, input, &in_arr));
33 PetscCall(DMDAVecRestoreArray(user->da, output, &out_arr));
34
35 PetscCall(VecDestroy(&input));
36 PetscCall(VecDestroy(&output));
37 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
38 PetscFunctionReturn(0);
39}
PetscErrorCode InterpolateFieldFromCornerToCenter_Scalar(PetscReal ***field_arr, PetscReal ***centfield_arr, UserCtx *user)
Safely interpolate a scalar field from corner nodes (from the coordinate DM) to cell centers (from th...
PetscErrorCode PicurvCreateMinimalContexts(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz)
Builds minimal SimCtx and UserCtx fixtures for C unit tests.
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Asserts that two real values agree within tolerance.
PetscErrorCode PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Destroys minimal SimCtx/UserCtx fixtures and all owned PETSc objects.
The master context for the entire simulation.
Definition variables.h:916
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestVectorInterpolationConstantField()

static PetscErrorCode TestVectorInterpolationConstantField ( void  )
static

Tests vector interpolation on a constant field.

Definition at line 44 of file test_geometry.c.

45{
46 SimCtx *simCtx = NULL;
47 UserCtx *user = NULL;
48 Vec input = NULL, output = NULL;
49 Cmpnts ***in_arr = NULL, ***out_arr = NULL;
50
51 PetscFunctionBeginUser;
52 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
53 PetscCall(VecDuplicate(user->Ucat, &input));
54 PetscCall(VecDuplicate(user->Ucat, &output));
55 PetscCall(VecSet(input, 3.5));
56 PetscCall(VecSet(output, 0.0));
57
58 PetscCall(DMDAVecGetArrayRead(user->fda, input, &in_arr));
59 PetscCall(DMDAVecGetArray(user->fda, output, &out_arr));
60 PetscCall(InterpolateFieldFromCornerToCenter_Vector(in_arr, out_arr, user));
61 PetscCall(PicurvAssertRealNear(3.5, out_arr[1][1][1].x, 1.0e-12, "vector corner->center x preserves constants"));
62 PetscCall(PicurvAssertRealNear(3.5, out_arr[1][1][1].y, 1.0e-12, "vector corner->center y preserves constants"));
63 PetscCall(PicurvAssertRealNear(3.5, out_arr[1][1][1].z, 1.0e-12, "vector corner->center z preserves constants"));
64 PetscCall(DMDAVecRestoreArrayRead(user->fda, input, &in_arr));
65 PetscCall(DMDAVecRestoreArray(user->fda, output, &out_arr));
66
67 PetscCall(VecDestroy(&input));
68 PetscCall(VecDestroy(&output));
69 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
70 PetscFunctionReturn(0);
71}
PetscErrorCode InterpolateFieldFromCornerToCenter_Vector(Cmpnts ***field_arr, Cmpnts ***centfield_arr, UserCtx *user)
Safely interpolate a vector field from corner nodes (from the coordinate DM) to cell centers (from th...
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:

◆ TestSignedDistanceAndClassification()

static PetscErrorCode TestSignedDistanceAndClassification ( void  )
static

Tests signed-distance computation and inside/outside classification.

Definition at line 76 of file test_geometry.c.

77{
78 /* The unit cube in walking-search vertex order; its z = 0 face is BACK. */
79 const PetscReal corner[8][3] = {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0},
80 {0, 1, 1}, {1, 1, 1}, {1, 0, 1}, {0, 0, 1}};
81 const Cmpnts inside_point = {0.5, 0.5, 0.25};
82 const Cmpnts outside_point = {0.5, 0.5, -1.0};
83 Cell cell;
84 CellFacePlanes planes;
85 PetscReal d_inside[NUM_FACES], d_outside[NUM_FACES];
86 PetscReal distances[NUM_FACES] = {1.0, 1.0, 0.0, 1.0, 1.0, 1.0};
87 PetscInt result = -99;
88
89 PetscFunctionBeginUser;
90 for (int v = 0; v < 8; ++v) {
91 cell.vertices[v].x = corner[v][0]; cell.vertices[v].y = corner[v][1]; cell.vertices[v].z = corner[v][2];
92 }
93 MetricBuildCellFacePlanes(&cell, &planes);
94 PetscCall(CalculateDistancesToCellFacePlanes(inside_point, &planes, d_inside, 1.0e-12, NULL));
95 PetscCall(CalculateDistancesToCellFacePlanes(outside_point, &planes, d_outside, 1.0e-12, NULL));
96 PetscCall(PicurvAssertRealNear(0.25, d_inside[BACK], 1.0e-14, "inside point should yield its positive distance to the face"));
97 PetscCall(PicurvAssertRealNear(-1.0, d_outside[BACK], 1.0e-14, "outside point should yield its negative distance to the face"));
98
99 PetscCall(DeterminePointPosition(distances, &result));
100 PetscCall(PicurvAssertIntEqual(1, result, "one zero face distance should classify as on-face"));
101
102 distances[0] = -0.1;
103 PetscCall(DeterminePointPosition(distances, &result));
104 PetscCall(PicurvAssertIntEqual(-1, result, "negative face distance should classify as outside"));
105 PetscFunctionReturn(0);
106}
void MetricBuildCellFacePlanes(const Cell *cell, CellFacePlanes *planes)
Builds the six face planes of a hexahedron, as the particle search tests them.
Definition Metric.c:108
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
PetscScalar y
Definition variables.h:145
@ BACK
Definition variables.h:197
@ NUM_FACES
Definition variables.h:197
Cmpnts vertices[8]
Coordinates of the eight vertices of the cell.
Definition variables.h:219
Defines the vertices of a single hexahedral grid cell.
Definition variables.h:218
The six face planes of one hexahedron, indexed by Face, and its characteristic size.
Definition variables.h:229
PetscErrorCode CalculateDistancesToCellFacePlanes(const Cmpnts p, const CellFacePlanes *planes, PetscReal *d, const PetscReal threshold, Cmpnts *normals)
Signed distances from a point to precomputed cell face planes.
PetscErrorCode DeterminePointPosition(PetscReal *d, PetscInt *result)
Classifies a point based on precomputed face distances.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Runs the unit-geometry PETSc test binary.

Definition at line 111 of file test_geometry.c.

112{
113 PetscErrorCode ierr;
114 const PicurvTestCase cases[] = {
115 {"scalar-interpolation-constant", TestScalarInterpolationConstantField},
116 {"vector-interpolation-constant", TestVectorInterpolationConstantField},
117 {"signed-distance-and-classification", TestSignedDistanceAndClassification},
118 };
119
120 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv geometry tests");
121 if (ierr) {
122 return (int)ierr;
123 }
124
125 ierr = PicurvRunTests("unit-geometry", cases, sizeof(cases) / sizeof(cases[0]));
126 if (ierr) {
127 PetscFinalize();
128 return (int)ierr;
129 }
130
131 ierr = PetscFinalize();
132 return (int)ierr;
133}
static PetscErrorCode TestSignedDistanceAndClassification(void)
Tests signed-distance computation and inside/outside classification.
static PetscErrorCode TestScalarInterpolationConstantField(void)
Tests scalar interpolation on a constant field.
static PetscErrorCode TestVectorInterpolationConstantField(void)
Tests vector interpolation on a constant field.
PetscErrorCode PicurvRunTests(const char *suite_name, const PicurvTestCase *cases, size_t case_count)
Runs a named C test suite and prints pass/fail progress markers.
Named test case descriptor consumed by PicurvRunTests.
Here is the call graph for this function: