18 Vec target_vec = NULL;
19 PetscReal scale_factor = 1.0;
20 char field_type[64] =
"Unknown";
21 PetscBool is_swarm_field = PETSC_FALSE;
22 const char *swarm_field_name = NULL;
25 PetscBool found = PETSC_FALSE;
27 PetscFunctionBeginUser;
29 if (!user) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"UserCtx is NULL.");
30 if (!field_name) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"field_name is NULL.");
36 target_vec = user->
Coor;
40 ierr =
FieldGetView(user, field_id, &view); CHKERRQ(ierr);
46 PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
47 "DimensionalizeField: '%s' is in neither field catalog.", field_name);
49 is_swarm_field = PETSC_TRUE;
53 field_type,
sizeof(field_type)); CHKERRQ(ierr);
56 if (PetscAbsReal(scale_factor - 1.0) < PETSC_MACHINE_EPSILON) {
57 LOG(
GLOBAL,
LOG_DEBUG,
"DimensionalizeField: Scaling factor for '%s' is 1.0. Skipping operation.\n", field_name);
59 PetscFunctionReturn(0);
63 LOG(
GLOBAL,
LOG_INFO,
"Scaling '%s' field (%s) by factor %.4e.\n", field_name, field_type, scale_factor);
67 ierr = DMSwarmCreateGlobalVectorFromField(user->
swarm, swarm_field_name, &target_vec); CHKERRQ(ierr);
68 ierr = VecScale(target_vec, scale_factor); CHKERRQ(ierr);
69 ierr = DMSwarmDestroyGlobalVectorFromField(user->
swarm, swarm_field_name, &target_vec); CHKERRQ(ierr);
73 ierr = VecScale(target_vec, scale_factor); CHKERRQ(ierr);
75 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE,
"Target vector for field '%s' was not found or is NULL.", field_name);
80 if (strcasecmp(field_name,
"Coordinates") == 0) {
85 PetscFunctionReturn(0);
100 Vec in_vec_local = NULL, out_vec_global = NULL;
101 DM dm_in = NULL, dm_out = NULL;
104 PetscFunctionBeginUser;
106 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"-> KERNEL: Running ComputeNodalAverage on '%s' -> '%s'.\n", in_field_name, out_field_name);
109 if (strcasecmp(in_field_name,
"P") == 0) { in_vec_local = user->
lP; dm_in = user->
da; dof = 1; }
110 else if (strcasecmp(in_field_name,
"Ucat") == 0) { in_vec_local = user->
lUcat; dm_in = user->
fda; dof = 3; }
111 else if (strcasecmp(in_field_name,
"Psi") == 0) { in_vec_local = user->
lPsi; dm_in = user->
da; dof = 1; }
112 else if (strcasecmp(in_field_name,
"Qcrit") == 0) { in_vec_local = user->
lQcrit; dm_in = user->
da; dof = 1; }
115 else if (strcasecmp(in_field_name,
"PostScalar") == 0) { in_vec_local = user->
lPostScalar; dm_in = user->
da; dof = 1; }
116 else if (strcasecmp(in_field_name,
"PostVector") == 0) { in_vec_local = user->
lPostVector; dm_in = user->
fda; dof = 3; }
118 else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
"Unknown input field name for nodal averaging: %s", in_field_name);
120 if (strcasecmp(out_field_name,
"P_nodal") == 0) { out_vec_global = user->
P_nodal; dm_out = user->
da; }
121 else if (strcasecmp(out_field_name,
"Ucat_nodal") == 0) { out_vec_global = user->
Ucat_nodal; dm_out = user->
fda; }
122 else if (strcasecmp(out_field_name,
"Psi_nodal") == 0) { out_vec_global = user->
Psi_nodal; dm_out = user->
da; }
123 else if (strcasecmp(out_field_name,
"Qcrit_nodal") == 0) { out_vec_global = user->
Qcrit_nodal; dm_out = user->
da; }
124 else if (strcasecmp(out_field_name,
"PostScalarNodal") == 0) { out_vec_global = user->
PostScalarNodal; dm_out = user->
da; }
125 else if (strcasecmp(out_field_name,
"PostVectorNodal") == 0) { out_vec_global = user->
PostVectorNodal; dm_out = user->
fda; }
127 else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
"Unknown output field name for nodal averaging: %s", out_field_name);
142 const PetscInt i_end = PetscMin(info.xs + info.xm, info.mx - 1);
143 const PetscInt j_end = PetscMin(info.ys + info.ym, info.my - 1);
144 const PetscInt k_end = PetscMin(info.zs + info.zm, info.mz - 1);
147 const PetscReal ***l_in_arr;
148 PetscReal ***g_out_arr;
149 ierr = DMDAVecGetArrayRead(dm_in,in_vec_local, (
void*)&l_in_arr); CHKERRQ(ierr);
150 ierr = DMDAVecGetArray(dm_out,out_vec_global, (
void*)&g_out_arr); CHKERRQ(ierr);
154 for (PetscInt k = info.zs; k < k_end; k++) {
155 for (PetscInt j = info.ys; j < j_end; j++) {
156 for (PetscInt i = info.xs; i < i_end; i++) {
157 g_out_arr[k][j][i] = 0.125 * (l_in_arr[k][j][i] + l_in_arr[k][j][i+1] +
158 l_in_arr[k][j+1][i] + l_in_arr[k][j+1][i+1] +
159 l_in_arr[k+1][j][i] + l_in_arr[k+1][j][i+1] +
160 l_in_arr[k+1][j+1][i] + l_in_arr[k+1][j+1][i+1]);
164 ierr = DMDAVecRestoreArrayRead(dm_in,in_vec_local, (
void*)&l_in_arr); CHKERRQ(ierr);
165 ierr = DMDAVecRestoreArray(dm_out,out_vec_global, (
void*)&g_out_arr); CHKERRQ(ierr);
167 }
else if (dof == 3) {
170 ierr = DMDAVecGetArrayRead(dm_in,in_vec_local, (
void*)&l_in_arr); CHKERRQ(ierr);
171 ierr = DMDAVecGetArray(dm_out,out_vec_global, (
void*)&g_out_arr); CHKERRQ(ierr);
173 for (PetscInt k = info.zs; k < k_end; k++) {
174 for (PetscInt j = info.ys; j < j_end; j++) {
175 for (PetscInt i = info.xs; i < i_end; i++) {
176 g_out_arr[k][j][i].
x = 0.125 * (l_in_arr[k][j][i].
x + l_in_arr[k][j][i+1].
x +
177 l_in_arr[k][j+1][i].
x + l_in_arr[k][j+1][i+1].
x +
178 l_in_arr[k+1][j][i].
x + l_in_arr[k+1][j][i+1].
x +
179 l_in_arr[k+1][j+1][i].
x + l_in_arr[k+1][j+1][i+1].
x);
181 g_out_arr[k][j][i].
y = 0.125 * (l_in_arr[k][j][i].
y + l_in_arr[k][j][i+1].
y +
182 l_in_arr[k][j+1][i].
y + l_in_arr[k][j+1][i+1].
y +
183 l_in_arr[k+1][j][i].
y + l_in_arr[k+1][j][i+1].
y +
184 l_in_arr[k+1][j+1][i].
y + l_in_arr[k+1][j+1][i+1].
y);
186 g_out_arr[k][j][i].
z = 0.125 * (l_in_arr[k][j][i].
z + l_in_arr[k][j][i+1].
z +
187 l_in_arr[k][j+1][i].
z + l_in_arr[k][j+1][i+1].
z +
188 l_in_arr[k+1][j][i].
z + l_in_arr[k+1][j][i+1].
z +
189 l_in_arr[k+1][j+1][i].
z + l_in_arr[k+1][j+1][i+1].
z);
193 ierr = DMDAVecRestoreArrayRead(dm_in,in_vec_local, (
void*)&l_in_arr); CHKERRQ(ierr);
194 ierr = DMDAVecRestoreArray(dm_out,out_vec_global, (
void*)&g_out_arr); CHKERRQ(ierr);
197 PetscFunctionReturn(0);
209 const char *outputs, PetscInt output_index,
210 char *out_name,
size_t name_size,
211 Vec *out_vec, PetscInt *out_components)
216 PetscFunctionBeginUser;
218 PetscCheck(user != NULL && definition != NULL && storage != NULL && out_name != NULL &&
219 out_vec != NULL && out_components != NULL,
220 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Context, state, and outputs are required.");
227 &derived); CHKERRQ(ierr);
246 ierr = PetscStrncpy(out_name, derived.
name, name_size); CHKERRQ(ierr);
251 PetscFunctionReturn(0);
262 const PetscBool average_direction[3],
263 const char *output_prefix, PetscInt ti)
265 static const char *
const axis_index[3] = {
"i",
"j",
"k"};
266 static const char *
const axis_coordinate[3] = {
"x",
"y",
"z"};
270 PetscInt extent[3], slots = 1, derived_count = 0, entries = 0;
271 PetscInt *components = NULL;
272 PetscBool *particle_sampled = NULL;
273 PetscReal *occupied = NULL, *with_particles = NULL, *centre = NULL, *values = NULL, length_scale = 1.0;
276 PetscFunctionBeginUser;
278 PetscCheck(user != NULL && definition != NULL && storage != NULL && output_prefix != NULL,
279 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Context, state, and output prefix are required.");
286 extent[0] = average_direction[0] ? 1 : info.mx;
287 extent[1] = average_direction[1] ? 1 : info.my;
288 extent[2] = average_direction[2] ? 1 : info.mz;
289 slots = extent[0] * extent[1] * extent[2];
294 PetscCall(PetscCalloc4(slots, &occupied, 3 * slots, ¢re, PetscMax(derived_count, 1), &components,
295 PetscMax(derived_count, 1), &names));
296 PetscCall(PetscCalloc2(slots, &with_particles, PetscMax(derived_count, 1), &particle_sampled));
298 PETSC_COMM_WORLD, NULL, occupied));
303 average_direction, PETSC_COMM_WORLD, NULL, with_particles));
306 PETSC_COMM_WORLD, NULL, centre));
307 PetscCall(PetscCalloc1(PetscMax(3 * derived_count * slots, 1), &values));
308 for (PetscInt index = 0; index < derived_count; ++index) {
323 average_direction, PETSC_COMM_WORLD, NULL, values + entries));
326 NULL, reduced.
weight, average_direction, PETSC_COMM_WORLD,
327 NULL, values + entries));
330 particle_sampled[index] = derived.
occupied;
331 PetscCall(PetscStrncpy(names[index], derived.
name,
sizeof(names[index])));
339 char path[PETSC_MAX_PATH_LEN];
342 PetscCall(PetscSNPrintf(path,
sizeof(path),
"%s_profile_%s_%05" PetscInt_FMT
".csv",
343 output_prefix, definition->
name, ti));
344 csv = fopen(path,
"w");
345 PetscCheck(csv != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Unable to open profile '%s'.", path);
346 for (PetscInt axis = 0; axis < 3; ++axis)
if (!average_direction[axis]) fprintf(csv,
"%s,", axis_index[axis]);
347 fprintf(csv,
"x,y,z");
348 for (PetscInt index = 0; index < derived_count; ++index) {
349 for (PetscInt c = 0; c < components[index]; ++c) {
350 fprintf(csv,
",%s%s%s", names[index], components[index] == 1 ?
"" :
"_",
351 components[index] == 1 ?
"" : axis_coordinate[c]);
355 for (PetscInt slot = 0; slot < slots; ++slot) {
356 const PetscInt index[3] = {slot % extent[0], (slot / extent[0]) % extent[1], slot / (extent[0] * extent[1])};
360 if (occupied[slot] <= 0.0)
continue;
362 for (PetscInt axis = 0; axis < 3; ++axis) {
363 if (!average_direction[axis]) fprintf(csv,
"%d,", (
int)(index[axis] - 1));
365 fprintf(csv,
"%.16e,%.16e,%.16e", (
double)(centre[3 * slot + 0] * length_scale),
366 (
double)(centre[3 * slot + 1] * length_scale), (
double)(centre[3 * slot + 2] * length_scale));
367 for (PetscInt output = 0; output < derived_count; ++output) {
368 for (PetscInt c = 0; c < components[output]; ++c) {
369 if (particle_sampled[output] && with_particles[slot] <= 0.0) fprintf(csv,
",");
370 else fprintf(csv,
",%.16e", (
double)values[offset + components[output] * slot + c]);
372 offset += components[output] * slots;
376 PetscCheck(fclose(csv) == 0, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE,
"Unable to close profile '%s'.", path);
379 PetscCall(PetscFree(values));
380 PetscCall(PetscFree2(with_particles, particle_sampled));
381 PetscCall(PetscFree4(occupied, centre, components, names));
385 PetscFunctionReturn(0);
395 const char *output_prefix, PetscInt ti)
401 PetscReal lowest = 1.0, highest = 0.0;
402 PetscReal mean_tke = 0.0;
403 PetscBool has_tke = PETSC_FALSE;
404 PetscInt derived_count = 0;
405 char path[PETSC_MAX_PATH_LEN];
407 PetscFunctionBeginUser;
409 PetscCheck(user != NULL && output_prefix != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
410 "Context and output prefix are required.");
413 PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
414 "No accumulated window state exists to summarize.");
419 window->
sample_count, &lowest, &highest); CHKERRQ(ierr);
425 if (derived_count > 0) {
435 has_tke = PETSC_TRUE;
441 PetscReal time_scale = 1.0;
447 const PetscReal weight_scale =
450 if (simCtx->
rank == 0) {
452 PetscBool exists = PETSC_FALSE;
454 ierr = PetscSNPrintf(path,
sizeof(path),
"%s_statistics_%s.csv",
456 ierr = PetscTestFile(path,
'r', &exists); CHKERRQ(ierr);
457 csv = fopen(path, exists ?
"a" :
"w");
458 PetscCheck(csv != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
459 "Unable to open statistics summary '%s'.", path);
461 fprintf(csv,
"step,state,samples,total_weight,represented_time,"
462 "valid_fraction_min,valid_fraction_max,mean_tke\n");
464 fprintf(csv,
"%" PetscInt_FMT
",%s,%" PetscInt_FMT
",%.10e,%.10e,%.6f,%.6f,",
468 (
double)lowest, (
double)highest);
469 if (has_tke) fprintf(csv,
"%.10e\n", (
double)mean_tke);
470 else fprintf(csv,
"\n");
471 PetscCheck(fclose(csv) == 0, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE,
472 "Unable to close statistics summary '%s'.", path);
477 PetscFunctionReturn(0);
490 const Cmpnts ***lucat, ***lcsi, ***leta, ***lzet;
491 const PetscReal***laj, ***lnvert;
494 PetscFunctionBeginUser;
509 ierr = DMDAVecGetArrayRead(user->
fda, user->
lUcat, (
void*)&lucat); CHKERRQ(ierr);
510 ierr = DMDAVecGetArrayRead(user->
fda, user->
lCsi, (
void*)&lcsi); CHKERRQ(ierr);
511 ierr = DMDAVecGetArrayRead(user->
fda, user->
lEta, (
void*)&leta); CHKERRQ(ierr);
512 ierr = DMDAVecGetArrayRead(user->
fda, user->
lZet, (
void*)&lzet); CHKERRQ(ierr);
513 ierr = DMDAVecGetArrayRead(user->
da, user->
lAj, (
void*)&laj); CHKERRQ(ierr);
514 ierr = DMDAVecGetArrayRead(user->
da, user->
lNvert, (
void*)&lnvert); CHKERRQ(ierr);
515 ierr = DMDAVecGetArray(user->
da, user->
Qcrit, (
void*)&gq); CHKERRQ(ierr);
518 PetscInt i_start = (info.xs == 0) ? 1 : info.xs;
519 PetscInt i_end = (info.xs + info.xm == info.mx) ? info.mx - 1 : info.xs + info.xm;
520 PetscInt j_start = (info.ys == 0) ? 1 : info.ys;
521 PetscInt j_end = (info.ys + info.ym == info.my) ? info.my - 1 : info.ys + info.ym;
522 PetscInt k_start = (info.zs == 0) ? 1 : info.zs;
523 PetscInt k_end = (info.zs + info.zm == info.mz) ? info.mz - 1 : info.zs + info.zm;
526 for (PetscInt k = k_start; k < k_end; k++) {
527 for (PetscInt j = j_start; j < j_end; j++) {
528 for (PetscInt i = i_start; i < i_end; i++) {
531 PetscReal uc = 0.5 * (lucat[k][j][i+1].
x - lucat[k][j][i-1].
x);
532 PetscReal vc = 0.5 * (lucat[k][j][i+1].
y - lucat[k][j][i-1].
y);
533 PetscReal wc = 0.5 * (lucat[k][j][i+1].
z - lucat[k][j][i-1].
z);
535 PetscReal ue = 0.5 * (lucat[k][j+1][i].
x - lucat[k][j-1][i].
x);
536 PetscReal ve = 0.5 * (lucat[k][j+1][i].
y - lucat[k][j-1][i].
y);
537 PetscReal we = 0.5 * (lucat[k][j+1][i].
z - lucat[k][j-1][i].
z);
539 PetscReal uz = 0.5 * (lucat[k+1][j][i].
x - lucat[k-1][j][i].
x);
540 PetscReal vz = 0.5 * (lucat[k+1][j][i].
y - lucat[k-1][j][i].
y);
541 PetscReal wz = 0.5 * (lucat[k+1][j][i].
z - lucat[k-1][j][i].
z);
544 PetscReal csi1 = 0.5 * (lcsi[k][j][i].
x + lcsi[k][j][i-1].
x) * laj[k][j][i];
545 PetscReal csi2 = 0.5 * (lcsi[k][j][i].
y + lcsi[k][j][i-1].
y) * laj[k][j][i];
546 PetscReal csi3 = 0.5 * (lcsi[k][j][i].
z + lcsi[k][j][i-1].
z) * laj[k][j][i];
548 PetscReal eta1 = 0.5 * (leta[k][j][i].
x + leta[k][j-1][i].
x) * laj[k][j][i];
549 PetscReal eta2 = 0.5 * (leta[k][j][i].
y + leta[k][j-1][i].
y) * laj[k][j][i];
550 PetscReal eta3 = 0.5 * (leta[k][j][i].
z + leta[k][j-1][i].
z) * laj[k][j][i];
552 PetscReal zet1 = 0.5 * (lzet[k][j][i].
x + lzet[k-1][j][i].
x) * laj[k][j][i];
553 PetscReal zet2 = 0.5 * (lzet[k][j][i].
y + lzet[k-1][j][i].
y) * laj[k][j][i];
554 PetscReal zet3 = 0.5 * (lzet[k][j][i].
z + lzet[k-1][j][i].
z) * laj[k][j][i];
557 PetscReal d11 = uc * csi1 + ue * eta1 + uz * zet1;
558 PetscReal d12 = uc * csi2 + ue * eta2 + uz * zet2;
559 PetscReal d13 = uc * csi3 + ue * eta3 + uz * zet3;
561 PetscReal d21 = vc * csi1 + ve * eta1 + vz * zet1;
562 PetscReal d22 = vc * csi2 + ve * eta2 + vz * zet2;
563 PetscReal d23 = vc * csi3 + ve * eta3 + vz * zet3;
565 PetscReal d31 = wc * csi1 + we * eta1 + wz * zet1;
566 PetscReal d32 = wc * csi2 + we * eta2 + wz * zet2;
567 PetscReal d33 = wc * csi3 + we * eta3 + wz * zet3;
571 PetscReal s12 = 0.5 * (d12 + d21);
572 PetscReal s13 = 0.5 * (d13 + d31);
574 PetscReal s23 = 0.5 * (d23 + d32);
578 PetscReal w12 = 0.5 * (d12 - d21);
579 PetscReal w13 = 0.5 * (d13 - d31);
580 PetscReal w23 = 0.5 * (d23 - d32);
583 PetscReal s_norm_sq = s11*s11 + s22*s22 + s33*s33 + 2.0*(s12*s12 + s13*s13 + s23*s23);
584 PetscReal w_norm_sq = 2.0 * (w12*w12 + w13*w13 + w23*w23);
586 gq[k][j][i] = 0.5 * (w_norm_sq - s_norm_sq);
588 if (lnvert[k][j][i] > 0.1) {
596 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
lUcat, (
void*)&lucat); CHKERRQ(ierr);
597 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
lCsi, (
void*)&lcsi); CHKERRQ(ierr);
598 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
lEta, (
void*)&leta); CHKERRQ(ierr);
599 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
lZet, (
void*)&lzet); CHKERRQ(ierr);
600 ierr = DMDAVecRestoreArrayRead(user->
da, user->
lAj, (
void*)&laj); CHKERRQ(ierr);
601 ierr = DMDAVecRestoreArrayRead(user->
da, user->
lNvert, (
void*)&lnvert); CHKERRQ(ierr);
602 ierr = DMDAVecRestoreArray(user->
da, user->
Qcrit, (
void*)&gq); CHKERRQ(ierr);
608 PetscReal length_scale = 1.0;
610 length_scale > 0.0) {
611 ierr = VecScale(user->
Qcrit, 1.0 / (length_scale * length_scale)); CHKERRQ(ierr);
624 PetscFunctionReturn(0);
638 PetscInt ip=1, jp=1, kp=1;
639 PetscReal p_ref = 0.0;
640 PetscReal p_ref_local = 0.0;
641 PetscInt found_local = 0, found_global = 0;
649 PetscFunctionBeginUser;
654 if (strcasecmp(relative_field_name,
"P") == 0) {
657 SETERRQ(PETSC_COMM_SELF, 1,
"NormalizeRelativeField only supports the primary 'P' field , not '%s' currently.", relative_field_name);
662 PetscCheck(ip >= 0 && ip < info.mx && jp >= 0 && jp < info.my &&
663 kp >= 0 && kp < info.mz,
664 PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
665 "Reference point (%" PetscInt_FMT
", %" PetscInt_FMT
", %" PetscInt_FMT
666 ") lies outside the %" PetscInt_FMT
"x%" PetscInt_FMT
"x%" PetscInt_FMT
667 " pressure layout.", ip, jp, kp, info.mx, info.my, info.mz);
668 if (ip >= info.xs && ip < info.xs + info.xm &&
669 jp >= info.ys && jp < info.ys + info.ym &&
670 kp >= info.zs && kp < info.zs + info.zm) {
671 const PetscReal ***pressure = NULL;
673 ierr = DMDAVecGetArrayRead(user->
da, P_vec, &pressure); CHKERRQ(ierr);
674 p_ref_local = pressure[kp][jp][ip];
675 ierr = DMDAVecRestoreArrayRead(user->
da, P_vec, &pressure); CHKERRQ(ierr);
678 ierr = MPI_Allreduce(&p_ref_local, &p_ref, 1, MPIU_REAL, MPI_SUM,
679 PETSC_COMM_WORLD); CHKERRQ(ierr);
680 ierr = MPI_Allreduce(&found_local, &found_global, 1, MPIU_INT, MPI_SUM,
681 PETSC_COMM_WORLD); CHKERRQ(ierr);
682 PetscCheck(found_global == 1, PETSC_COMM_WORLD, PETSC_ERR_PLIB,
683 "Reference pressure point must have exactly one owner; found %" PetscInt_FMT
".",
686 "%s reference point (%" PetscInt_FMT
", %" PetscInt_FMT
", %" PetscInt_FMT
687 ") has value %g.\n", relative_field_name, ip, jp, kp, (
double)p_ref);
690 ierr = VecShift(P_vec, -p_ref); CHKERRQ(ierr);
694 PetscFunctionReturn(0);
710 const PetscScalar (*vel_arr)[3];
711 PetscScalar *ske_arr;
713 PetscFunctionBeginUser;
715 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"-> KERNEL: Running ComputeSpecificKE ('%s' -> '%s').\n", velocity_field, ske_field);
718 ierr = DMSwarmGetLocalSize(user->
swarm, &n_local); CHKERRQ(ierr);
722 ierr = DMSwarmGetField(user->
swarm, velocity_field, NULL, NULL, (
void**)&vel_arr); CHKERRQ(ierr);
723 ierr = DMSwarmGetField(user->
post_swarm, ske_field, NULL, NULL, (
void**)&ske_arr); CHKERRQ(ierr);
726 for (PetscInt p = 0; p < n_local; p++) {
727 const PetscScalar u = vel_arr[p][0];
728 const PetscScalar v = vel_arr[p][1];
729 const PetscScalar w = vel_arr[p][2];
730 const PetscScalar vel_sq = u*u + v*v + w*w;
731 ske_arr[p] = 0.5 * vel_sq;
735 ierr = DMSwarmRestoreField(user->
swarm, velocity_field, NULL, NULL, (
void**)&vel_arr); CHKERRQ(ierr);
736 ierr = DMSwarmRestoreField(user->
post_swarm, ske_field, NULL, NULL, (
void**)&ske_arr); CHKERRQ(ierr);
739 PetscFunctionReturn(0);
752 const PetscReal (*pos_arr)[3];
753 PetscScalar *disp_out;
756 PetscFunctionBeginUser;
760 ierr = DMSwarmGetLocalSize(user->
swarm, &n_local); CHKERRQ(ierr);
763 const PetscReal x0 = simCtx->
psrc_x;
764 const PetscReal y0 = simCtx->
psrc_y;
765 const PetscReal z0 = simCtx->
psrc_z;
768 ierr = DMSwarmGetField(user->
post_swarm, disp_field, NULL, NULL, (
void**)&disp_out); CHKERRQ(ierr);
770 for (PetscInt p = 0; p < n_local; p++) {
771 const PetscReal dx = pos_arr[p][0] - x0;
772 const PetscReal dy = pos_arr[p][1] - y0;
773 const PetscReal dz = pos_arr[p][2] - z0;
774 disp_out[p] = PetscSqrtReal(dx*dx + dy*dy + dz*dz);
778 ierr = DMSwarmRestoreField(user->
post_swarm, disp_field, NULL, NULL, (
void**)&disp_out); CHKERRQ(ierr);
781 PetscFunctionReturn(0);
PetscErrorCode SynchronizePeriodicCellFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes periodic endpoint cells for a list of cell-centered fields.
PetscErrorCode FieldIdFromName(const char *field_name, FieldId *field_id)
Resolve a user-facing field name once into its typed identity.
PetscErrorCode FieldDimensionReferenceScale(const ScalingCtx *scaling, FieldDimension dimension, PetscReal *scale)
Return the factor that turns a solver value of one dimension into physical units.
PetscErrorCode FieldGetView(UserCtx *user, FieldId field_id, FieldView *view)
Resolve the existing DM and global/local vectors for one field.
PetscErrorCode FieldTryIdFromName(const char *field_name, FieldId *field_id, PetscBool *found)
Look a name up without treating an unknown name as an error.
FieldId
Compile-time identity for a catalogued Eulerian field.
Physical dimension as exponents of the reference length, velocity, and density.
Non-owning runtime objects resolved for one field and UserCtx.
PetscErrorCode PicurvFieldReferenceScale(SimCtx *simCtx, const char *field_name, PetscReal *scale, char *description, size_t description_length)
Physical scale one field is multiplied by to leave non-dimensional form.
PetscBool is_function_allowed(const char *functionName)
Checks if a given function is in the allow-list.
#define GLOBAL
Scope for global logging across all processes.
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
#define LOG(scope, level, fmt,...)
Logging macro for PETSc-based applications with scope control.
PetscErrorCode LOG_FIELD_ANATOMY(UserCtx *user, FieldId field_id, const char *stage_name)
Logs the anatomy of a specified field at key boundary locations, respecting the solver's specific gri...
@ LOG_INFO
Informational messages about program execution.
@ LOG_DEBUG
Detailed debugging information.
@ LOG_VERBOSE
Extremely detailed logs, typically for development use only.
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
static LogLevel get_log_level(void)
Retrieves the current logging level from the environment variable LOG_LEVEL.
Typed identities and metadata for persistent solver-particle fields.
const char * ParticleFieldName(ParticleFieldId field_id)
Return the canonical PETSc DMSwarm name for an ID.
ParticleFieldId
Compile-time identity for a persistent solver-particle field.
@ PARTICLE_FIELD_ID_POSITION
@ PARTICLE_FIELD_ID_INVALID
PetscErrorCode ParticleFieldGetDescriptor(ParticleFieldId field_id, const ParticleFieldDescriptor **descriptor)
Return immutable metadata for a valid particle field ID.
const char * canonical_name
PetscErrorCode ParticleFieldTryIdFromName(const char *field_name, ParticleFieldId *field_id, PetscBool *found)
Look a name up without treating an unknown name as an error.
Immutable metadata for one persistent particle field.
PetscErrorCode ComputeQCriterion(UserCtx *user)
Implementation of ComputeQCriterion().
PetscErrorCode ComputeWindowStatisticsProfile(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, const PetscBool average_direction[3], const char *output_prefix, PetscInt ti)
Implementation of ComputeWindowStatisticsProfile().
PetscErrorCode ComputeWindowStatisticNodal(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt output_index, char *out_name, size_t name_size, Vec *out_vec, PetscInt *out_components)
Implementation of ComputeWindowStatisticNodal().
PetscErrorCode ComputeSpecificKE(UserCtx *user, const char *velocity_field, const char *ske_field)
Implementation of ComputeSpecificKE().
PetscErrorCode ComputeDisplacement(UserCtx *user, const char *disp_field)
Implementation of ComputeDisplacement().
PetscErrorCode NormalizeRelativeField(UserCtx *user, const char *relative_field_name)
Implementation of NormalizeRelativeField().
PetscErrorCode ComputeWindowStatisticsSummary(UserCtx *user, PetscInt window_index, const char *output_prefix, PetscInt ti)
Implementation of ComputeWindowStatisticsSummary().
PetscErrorCode DimensionalizeField(UserCtx *user, const char *field_name)
Implementation of DimensionalizeField().
PetscErrorCode ComputeNodalAverage(UserCtx *user, const char *in_field_name, const char *out_field_name)
Implementation of ComputeNodalAverage().
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Per-window PETSc accumulator storage and pointwise application.
Vec weight
Per-point valid weight.
PetscInt components
One or three.
#define PICURV_STATISTICS_PAYLOAD_NAME_LENGTH
Maximum stored length of a payload name, including the terminator.
PetscErrorCode PicurvWindowStorageReduce(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const PetscBool average_direction[3], PicurvWindowStorage *reduced)
Merges a window's pointwise state over homogeneous directions.
char name[96]
Output field name, window qualified.
PetscErrorCode PicurvWindowDerive(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt index, Vec scalar_target, Vec vector_target, PicurvDerivedField *field)
Derives one output field from centered accumulator state.
PetscBool point_averaged
Reduced over homogeneous directions as the equal-weight mean of point values, not from the pooled sam...
Vec occupied_weight
Occupied-state weight; NULL without a particle-sampled field.
PetscErrorCode PicurvWindowDerivedCount(const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt *count)
Reports how many derived fields a requested output set produces.
PetscErrorCode PicurvWindowStorageCreate(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage)
Allocates the accumulator state one window owns on one block.
PetscErrorCode PicurvWindowValidFractionRange(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, PetscInt sample_count, PetscReal *minimum, PetscReal *maximum)
Reports the range of per-point valid fraction across a window's domain.
PetscBool occupied
Measured over the occupied states, so undefined where they are absent.
PetscErrorCode PicurvWindowSpatialMean(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, Vec field, PetscReal *mean)
Reports the spatial mean of a derived field over the points a window sampled.
PetscErrorCode PicurvWindowStorageDestroy(PicurvWindowStorage *storage)
Releases accumulator state previously created for one window.
One derived output field, resolved by enumeration index.
Independent accumulator state for one window on one block.
Spatial target resolution for the field-statistics pipeline.
@ PICURV_STATISTICS_MASK_FLUID
PetscErrorCode SpatialTargetPlanCreate(UserCtx *user, FieldId field_id, PicurvStatisticsMask mask, SpatialTargetPlan *plan)
Resolves the iteration domain for one field on one block.
PetscErrorCode PicurvSpatialRatioAverage(UserCtx *user, const SpatialTargetPlan *plan, Vec numerator, Vec denominator, Vec inclusion, const PetscBool average_direction[3], MPI_Comm comm, Vec ratio, PetscReal *reduced)
Averages two fields over a target domain and divides the results.
Resolved iteration domain for one field on one block.
Window lifecycle, scheduling, and weighting for the field-statistics pipeline.
PicurvWindowFieldRequest fields[16]
const char * PicurvWindowStateName(PicurvWindowState state)
Returns a stable human-readable name for a window state.
PicurvWindowDefinition definition
PetscBool FieldStatisticsIsActive(const struct SimCtx *simCtx)
Reports whether this run has live field-statistics state.
PetscInt field_id
Catalogued Eulerian field identity.
@ PICURV_WEIGHTING_PHYSICAL_TIME
Weight is the represented interval.
PetscReal represented_time
Physical time the window covers.
PicurvWeighting weighting
Runtime state of one window.
The scientifically immutable definition of one window.
Vec Qcrit_nodal
Q-criterion averaged to grid nodes; the field a .vts can place correctly.
SimCtx * simCtx
Back-pointer to the master simulation context.
PetscBool dimensionalize
Whether derived output leaves non-dimensional form, from global_operations.dimensionalize.
PetscReal psrc_z
Point source location for PARTICLE_INIT_POINT_SOURCE.
struct PicurvWindow * fieldStatisticsWindows
struct PicurvWindowStorage * fieldStatisticsStorage
Vec lQcrit
Cell-centred Q-criterion and its ghosted copy for nodal averaging.
A 3D point or vector with PetscScalar components.
Holds all configuration parameters for a post-processing run.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.