8#define INTERPOLATION_DISTANCE_TOLERANCE 1.0e-14
19 PetscFunctionBeginUser;
22 ierr = DMCreate(PETSC_COMM_WORLD, &user->
swarm); CHKERRQ(ierr);
23 ierr = DMSetType(user->
swarm, DMSWARM); CHKERRQ(ierr);
24 ierr = DMSetDimension(user->
swarm, 3); CHKERRQ(ierr);
25 ierr = DMSwarmSetType(user->
swarm, DMSWARM_BASIC); CHKERRQ(ierr);
29 PetscFunctionReturn(0);
37PetscErrorCode
RegisterSwarmField(DM swarm,
const char *fieldName, PetscInt fieldDim, PetscDataType dtype)
40 PetscFunctionBeginUser;
43 ierr = DMSwarmRegisterPetscDatatypeField(swarm, fieldName, fieldDim, dtype); CHKERRQ(ierr);
46 fieldName, fieldDim, PetscDataTypes[dtype]);
49 PetscFunctionReturn(0);
63 PetscFunctionBeginUser;
77 ierr = DMSwarmFinalizeFieldRegister(swarm); CHKERRQ(ierr);
81 PetscFunctionReturn(0);
104#define SEEDING_LOGIC_MARGIN 1.0e-7
105#define SEEDING_FACE_OFFSET 1.0e-6
110 origin[0] = table->
origin[0] + c % table->
n[0];
111 origin[1] = table->
origin[1] + (c / table->
n[0]) % table->
n[1];
112 origin[2] = table->
origin[2] + c / (table->
n[0] * table->
n[1]);
121 for (PetscInt d = 0; d < 3; ++d) logic[d] = PetscMax(PetscMin(u[d], 1.0 -
SEEDING_LOGIC_MARGIN), 0.0);
131 PetscInt i, PetscInt j, PetscInt k,
const PetscReal logic[3],
134 PetscReal J[3][3], detJ, l[3] = {logic[0], logic[1], logic[2]};
136 PetscFunctionBeginUser;
139 PetscCall(
MetricJacobian(user, coor, i, j, k, l[0], l[1], l[2], J, &detJ));
142 const PetscInt a = face / 2, b = (a + 1) % 3, c = (a + 2) % 3;
144 l[a] = (face % 2) ? 1.0 : 0.0;
145 PetscCall(
MetricJacobian(user, coor, i, j, k, l[0], l[1], l[2], J, NULL));
146 cross.
x = J[1][b] * J[2][c] - J[2][b] * J[1][c];
147 cross.
y = J[2][b] * J[0][c] - J[0][b] * J[2][c];
148 cross.
z = J[0][b] * J[1][c] - J[1][b] * J[0][c];
149 *density = PetscSqrtReal(cross.
x * cross.
x + cross.
y * cross.
y + cross.
z * cross.
z);
152 PetscFunctionReturn(0);
182 PetscInt numParticles, PetscBool build_bound,
185 const PetscReal gauss[2] = {0.5 - 0.5 / PetscSqrtReal(3.0), 0.5 + 0.5 / PetscSqrtReal(3.0)};
186 const PetscInt nodes[3] = {user->
info.mx, user->
info.my, user->
info.mz};
187 PetscInt start, n_cells, n_rows_local, global_cells[3], global_origin[3] = {0, 0, 0}, quantum_bits = 62;
188 PetscInt64 n_global_cells;
189 PetscReal local_max = 0.0, global_max = 0.0;
190 PetscReal *measure = NULL;
191 uint64_t *rows = NULL, *row_before = NULL, *segment = NULL, *segment_before = NULL, *quanta = NULL;
193 MPI_Comm row_comm = MPI_COMM_NULL;
194 PetscMPIInt row_rank = 0;
196 PetscFunctionBeginUser;
198 PetscCall(PetscMemzero(table,
sizeof(*table)));
204 for (PetscInt d = 0; d < 3; ++d) global_cells[d] = nodes[d] - 2;
206 const PetscInt a = face / 2;
207 PetscBool holds_face = PETSC_FALSE;
210 if (face % 2) global_origin[a] = nodes[a] - 3;
213 table->
origin[a] = global_origin[a];
215 table->
n[0] = table->
n[1] = table->
n[2] = 0;
218 n_cells = table->
n[0] * table->
n[1] * table->
n[2];
219 n_rows_local = table->
n[1] * table->
n[2];
220 n_global_cells = (PetscInt64)global_cells[0] * global_cells[1] * global_cells[2];
221 while (quantum_bits > 0 && ((PetscInt64)1 << (62 - quantum_bits)) < n_global_cells) --quantum_bits;
224 PetscCall(PetscMalloc2(n_cells, &measure, n_cells, &quanta));
225 PetscCall(PetscMalloc2(n_cells, &table->
pid_lo, n_cells, &table->
pid_hi));
226 if (build_bound) PetscCall(PetscMalloc1(n_cells, &table->
bound));
227 for (PetscInt c = 0; c < n_cells; ++c) {
229 PetscReal density = 0.0;
231 const PetscInt i = origin[0], j = origin[1], k = origin[2];
233 for (PetscInt g = 0; g < 8; ++g) {
234 const PetscReal l[3] = {gauss[g & 1], gauss[(g >> 1) & 1], gauss[(g >> 2) & 1]};
236 measure[c] += 0.125 * density;
238 PetscCheck(measure[c] > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
239 "Cell with origin node (%d,%d,%d) has %s %g; particle seeding needs positive %s.",
240 (
int)i, (
int)j, (
int)k, face < 0 ?
"volume" :
"inlet area", (double)measure[c],
241 face < 0 ?
"volumes" :
"areas");
242 local_max = PetscMax(local_max, measure[c]);
244 table->
bound[c] = 0.0;
245 for (PetscInt q = 0; q < 27; ++q) {
246 const PetscReal l[3] = {0.5 * (q % 3), 0.5 * ((q / 3) % 3), 0.5 * (q / 9)};
248 table->
bound[c] = PetscMax(table->
bound[c], density);
253 PetscInt64 local_cells = n_cells, owned_cells = 0;
254 PetscCallMPI(MPI_Allreduce(&local_cells, &owned_cells, 1, MPIU_INT64, MPI_SUM, PETSC_COMM_WORLD));
255 PetscCheck(owned_cells == n_global_cells, PETSC_COMM_WORLD, PETSC_ERR_PLIB,
256 "Seeding cells owned across ranks (%" PetscInt64_FMT
") differ from the global count (%" PetscInt64_FMT
").",
257 owned_cells, n_global_cells);
259 PetscCallMPI(MPI_Allreduce(&local_max, &global_max, 1, MPIU_REAL, MPI_MAX, PETSC_COMM_WORLD));
260 PetscCheck(global_max > 0.0, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE,
261 face < 0 ?
"Volumetric particle seeding found no owned volume on any rank."
262 :
"Surface particle seeding found no inlet area on any rank; it needs an INLET face.");
264 uint64_t local_total = 0;
265 for (PetscInt c = 0; c < n_cells; ++c) {
266 quanta[c] = (uint64_t)PetscMax(1.0, PetscFloorReal(ldexp((
double)(measure[c] / global_max), (
int)quantum_bits) + 0.5));
267 local_total += quanta[c];
269 PetscCallMPI(MPI_Allreduce(&local_total, &total, 1, MPI_UINT64_T, MPI_SUM, PETSC_COMM_WORLD));
270 table->
total = (PetscReal)ldexp((
double)total, -(int)quantum_bits) * global_max;
273 if (numParticles == 0) {
276 PetscCall(PetscArrayzero(table->
pid_lo, n_cells));
277 PetscCall(PetscArrayzero(table->
pid_hi, n_cells));
278 PetscCall(PetscFree2(measure, quanta));
280 PetscFunctionReturn(0);
284 const PetscInt n_rows = global_cells[1] * global_cells[2];
285 PetscCall(PetscCalloc4(n_rows, &rows, n_rows, &row_before, n_rows_local, &segment, n_rows_local, &segment_before));
286 for (PetscInt c = 0; c < n_cells; ++c) {
287 const PetscInt lj = (c / table->
n[0]) % table->
n[1], lk = c / (table->
n[0] * table->
n[1]);
288 const PetscInt row = (table->
origin[1] + lj - global_origin[1]) + global_cells[1] * (table->
origin[2] + lk - global_origin[2]);
289 rows[row] += quanta[c];
290 segment[lj + table->
n[1] * lk] += quanta[c];
292 PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, rows, (PetscMPIInt)n_rows, MPI_UINT64_T, MPI_SUM, PETSC_COMM_WORLD));
294 for (PetscInt r = 0; r < n_rows; ++r) { row_before[r] = total; total += rows[r]; }
296 PetscCallMPI(MPI_Comm_split(PETSC_COMM_WORLD, n_cells > 0 ? (PetscMPIInt)(table->
origin[1] + nodes[1] * table->
origin[2]) : MPI_UNDEFINED,
297 (PetscMPIInt)table->
origin[0], &row_comm));
298 if (row_comm != MPI_COMM_NULL) {
299 PetscCallMPI(MPI_Exscan(segment, segment_before, (PetscMPIInt)n_rows_local, MPI_UINT64_T, MPI_SUM, row_comm));
300 PetscCallMPI(MPI_Comm_rank(row_comm, &row_rank));
301 if (row_rank == 0) PetscCall(PetscArrayzero(segment_before, n_rows_local));
302 PetscCallMPI(MPI_Comm_free(&row_comm));
306 for (PetscInt lrow = 0; lrow < n_rows_local; ++lrow) {
307 const PetscInt lj = lrow % table->
n[1], lk = lrow / table->
n[1];
308 const PetscInt row = (table->
origin[1] + lj - global_origin[1]) + global_cells[1] * (table->
origin[2] + lk - global_origin[2]);
309 uint64_t before = row_before[row] + segment_before[lrow];
310 for (PetscInt li = 0; li < table->
n[0]; ++li) {
311 const PetscInt c = li + table->
n[0] * lrow;
312 table->
pid_lo[c] = (PetscInt64)(((
unsigned __int128)numParticles * before) / total);
314 table->
pid_hi[c] = (PetscInt64)(((
unsigned __int128)numParticles * before) / total);
318 PetscCall(PetscFree4(rows, row_before, segment, segment_before));
319 PetscCall(PetscFree2(measure, quanta));
321 PetscFunctionReturn(0);
327 PetscFunctionBeginUser;
330 PetscCall(PetscFree(table->
bound));
332 PetscFunctionReturn(0);
360 PetscInt *ci_metric_lnode_out, PetscInt *cj_metric_lnode_out, PetscInt *ck_metric_lnode_out,
361 PetscReal *xi_metric_logic_out, PetscReal *eta_metric_logic_out, PetscReal *zta_metric_logic_out,
364 PetscReal logic[3], u[4];
366 PetscFunctionBeginUser;
371 *ci_metric_lnode_out = origin[0]; *cj_metric_lnode_out = origin[1]; *ck_metric_lnode_out = origin[2];
376 for (PetscInt attempt = 0; attempt < 1000; ++attempt) {
381 *ck_metric_lnode_out, logic, &density));
383 const PetscInt cell[3] = {*ci_metric_lnode_out, *cj_metric_lnode_out, *ck_metric_lnode_out};
385 PetscCall(
MetricLogicalToPhysical(user, coor, cell[0], cell[1], cell[2], logic[0], logic[1], logic[2], &point));
387 const PetscReal u_n = PetscMax(velocity_out->
x * normal.
x + velocity_out->
y * normal.
y + velocity_out->
z * normal.
z, 0.0);
388 if (u_n > flux_bound) {
389 LOG_ALLOW(
LOCAL,
LOG_WARNING,
"Rank %d: inward velocity %g exceeds the bound %g in face cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
"); placement there is biased.\n",
390 user->
simCtx->
rank, (
double)u_n, (
double)flux_bound, cell[0], cell[1], cell[2]);
392 density *= u_n / flux_bound;
394 if (u[3] * table->
bound[c] <= density)
break;
395 if (attempt == 999) {
396 LOG_ALLOW(
LOCAL,
LOG_WARNING,
"Rank %d: 1000 rejected draws in cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
"); accepting the last one.\n",
397 user->
simCtx->
rank, *ci_metric_lnode_out, *cj_metric_lnode_out, *ck_metric_lnode_out);
400 *xi_metric_logic_out = logic[0]; *eta_metric_logic_out = logic[1]; *zta_metric_logic_out = logic[2];
403 PetscFunctionReturn(0);
411 PetscInt particlesPerProcess)
414 DM swarm = user->
swarm;
415 PetscReal *positions_field = NULL;
416 PetscInt64 *particleIDs = NULL;
417 PetscInt *cellIDs_petsc = NULL;
418 PetscInt *status_field = NULL;
419 PetscMPIInt rank,size;
420 const Cmpnts ***coor_nodes_local_array;
425 PetscFunctionBeginUser;
430 if (!user) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Null user.");
436 Coor_local = user->
lCoor;
437 if (!Coor_local) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB,
"user->lCoor is NULL: grid coordinates are not set up.");
438 ierr = DMDAVecGetArrayRead(user->
fda, Coor_local, (
void*)&coor_nodes_local_array); CHKERRQ(ierr);
439 ierr = PetscMemzero(&seed_table,
sizeof(seed_table)); CHKERRQ(ierr);
449 PetscInt seed_cell = 0;
450 PetscInt64 seed_pid = 0;
451 if (seeds_from_table) {
453 ierr =
BuildSeedingCellTable(user, coor_nodes_local_array, face, simCtx->
np, PETSC_TRUE, &seed_table); CHKERRQ(ierr);
454 PetscCheck(seed_table.
count == particlesPerProcess, PETSC_COMM_SELF, PETSC_ERR_PLIB,
455 "Seeding table holds %" PetscInt_FMT
" particles but the swarm was sized for %" PetscInt_FMT
".",
456 seed_table.
count, particlesPerProcess);
457 if (seed_table.
n[0] * seed_table.
n[1] * seed_table.
n[2] > 0) seed_pid = seed_table.
pid_lo[0];
458 LOG_ALLOW(
LOCAL,
LOG_INFO,
"Rank %d: Seeding %" PetscInt_FMT
" particles stratified over %" PetscInt_FMT
" owned cells by %s.\n",
459 rank, particlesPerProcess, seed_table.
n[0] * seed_table.
n[1] * seed_table.
n[2],
460 face < 0 ?
"volume" :
"inlet area");
472 PetscInt base_pid_for_rank = 0;
474 PetscInt local_count = particlesPerProcess;
475 ierr = MPI_Exscan(&local_count, &base_pid_for_rank, 1, MPIU_INT, MPI_SUM, PETSC_COMM_WORLD); CHKERRMPI(ierr);
476 if (rank == 0) base_pid_for_rank = 0;
482 PetscInt point_cell[3] = {-1, -1, -1};
486 PetscMPIInt candidate, owner = size;
487 ierr = PetscMemzero(&probe,
sizeof(probe)); CHKERRQ(ierr);
489 probe.
cell[0] = -1; probe.
cell[1] = -1; probe.
cell[2] = -1;
493 ierr = MPI_Allreduce(&candidate, &owner, 1, MPI_INT, MPI_MIN, PETSC_COMM_WORLD); CHKERRMPI(ierr);
495 if (rank == owner) { point_cell[0] = probe.
cell[0]; point_cell[1] = probe.
cell[1]; point_cell[2] = probe.
cell[2]; }
496 ierr = MPI_Bcast(point_cell, 3, MPIU_INT, owner, PETSC_COMM_WORLD); CHKERRMPI(ierr);
497 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"Point source (%.6e, %.6e, %.6e) lies in cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") on rank %d.\n",
498 simCtx->
psrc_x, simCtx->
psrc_y, simCtx->
psrc_z, point_cell[0], point_cell[1], point_cell[2], owner);
500 LOG_ALLOW(
GLOBAL,
LOG_WARNING,
"Point source (%.6e, %.6e, %.6e) was not located on any rank; its particles will be searched for.\n",
506 for (PetscInt p = 0; p < particlesPerProcess; p++) {
507 PetscInt ci_metric_lnode, cj_metric_lnode, ck_metric_lnode;
508 PetscReal xi_metric_logic = 0.0, eta_metric_logic = 0.0, zta_metric_logic = 0.0;
509 Cmpnts phys_coords = {0.0, 0.0, 0.0};
510 PetscBool particle_placed_by_this_rank = PETSC_FALSE;
511 PetscInt64 pid = (PetscInt64)base_pid_for_rank + p;
513 if (seeds_from_table) {
514 while (seed_pid == seed_table.
pid_hi[seed_cell]) seed_pid = seed_table.
pid_lo[++seed_cell];
516 ierr =
DrawSeedingPoint(user, coor_nodes_local_array, &seed_table, seed_cell, pid, NULL, (
Cmpnts){0.0, 0.0, 0.0}, 0.0,
517 &ci_metric_lnode, &cj_metric_lnode, &ck_metric_lnode,
518 &xi_metric_logic, &eta_metric_logic, &zta_metric_logic, NULL); CHKERRQ(ierr);
520 ci_metric_lnode, cj_metric_lnode, ck_metric_lnode,
521 xi_metric_logic, eta_metric_logic, zta_metric_logic,
522 &phys_coords); CHKERRQ(ierr);
523 particle_placed_by_this_rank = PETSC_TRUE;
527 phys_coords.
x = simCtx->
psrc_x;
528 phys_coords.
y = simCtx->
psrc_y;
529 phys_coords.
z = simCtx->
psrc_z;
530 ci_metric_lnode = point_cell[0]; cj_metric_lnode = point_cell[1]; ck_metric_lnode = point_cell[2];
531 particle_placed_by_this_rank = (PetscBool)(point_cell[0] >= 0);
533 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
"Unknown ParticleInitialization mode %d.", simCtx->
ParticleInitialization);
537 positions_field[3*p+0] = phys_coords.
x;
538 positions_field[3*p+1] = phys_coords.
y;
539 positions_field[3*p+2] = phys_coords.
z;
541 particleIDs[p] = pid;
544 if (particle_placed_by_this_rank) {
545 cellIDs_petsc[3*p+0] = ci_metric_lnode; cellIDs_petsc[3*p+1] = cj_metric_lnode; cellIDs_petsc[3*p+2] = ck_metric_lnode;
547 cellIDs_petsc[3*p+0] = -1; cellIDs_petsc[3*p+1] = -1; cellIDs_petsc[3*p+2] = -1;
552 if (seeds_from_table) {
554 "%s placement on rank %d at (%.6e, %.6e, %.6e): cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") logical (%.4f, %.4f, %.4f)\n",
556 phys_coords.
x, phys_coords.
y, phys_coords.
z, ci_metric_lnode, cj_metric_lnode, ck_metric_lnode,
557 xi_metric_logic, eta_metric_logic, zta_metric_logic);
558 }
else if (particle_placed_by_this_rank) {
560 "%s placement on rank %d at (%.6e, %.6e, %.6e): cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
")\n",
562 phys_coords.
x, phys_coords.
y, phys_coords.
z, ci_metric_lnode, cj_metric_lnode, ck_metric_lnode);
566 "%s: not placed by rank %d; parked at (%.6e, %.6e, %.6e) for the settlement to locate\n",
568 phys_coords.
x, phys_coords.
y, phys_coords.
z);
577 ierr = DMDAVecRestoreArrayRead(user->
fda, Coor_local, (
void*)&coor_nodes_local_array); CHKERRQ(ierr);
581 rank, particlesPerProcess);
585 PetscFunctionReturn(0);
594 PetscFunctionBeginUser;
598 PetscReal *field = NULL;
602 PetscCheck(descriptor->
data_type == PETSC_REAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
603 "Default initialization supports only PETSC_REAL particle fields; '%s' uses %s.",
605 PetscCall(DMSwarmGetField(swarm, descriptor->
canonical_name, NULL, NULL, (
void **)&field));
606 for (PetscInt p = first; p < end; ++p) {
609 PetscCall(DMSwarmRestoreField(swarm, descriptor->
canonical_name, NULL, NULL, (
void **)&field));
612 PetscFunctionReturn(0);
621 PetscInt particlesPerProcess)
624 PetscFunctionBeginUser;
629 if (!user) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Null user.");
638 LOG_ALLOW(
GLOBAL,
LOG_ERROR,
"Particle Initialization on inlet surface selected, but no INLET face was identified from bcs.dat. Cannot proceed.\n");
639 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE,
"Surface particle seeding requires an INLET face in the boundary conditions.");
652 PetscInt n_local = 0;
653 ierr = DMSwarmGetLocalSize(user->
swarm, &n_local); CHKERRQ(ierr);
661 PetscFunctionReturn(0);
671PetscErrorCode
DistributeParticles(PetscInt numParticles, PetscMPIInt rank, PetscMPIInt size, PetscInt* particlesPerProcess, PetscInt* remainder) {
673 PetscFunctionBeginUser;
677 *particlesPerProcess = numParticles / size;
678 *remainder = numParticles % size;
681 if (rank < *remainder) {
682 *particlesPerProcess += 1;
689 PetscFunctionReturn(0);
708 const Cmpnts ***coor = NULL;
710 PetscFunctionBeginUser;
712 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lCoor, (
void *)&coor));
714 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lCoor, (
void *)&coor));
715 *localCount = table.
count;
718 PetscFunctionReturn(0);
727 PetscMPIInt rank, size;
728 PetscInt remainder = 0;
730 PetscFunctionBeginUser;
733 if (numParticles < 0) {
736 return PETSC_ERR_ARG_OUTOFRANGE;
752 PetscCheck(user->
inletFaceDefined, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE,
753 "Surface particle seeding needs an INLET face.");
756 ierr =
DistributeParticles(numParticles, rank, size, particlesPerProcess, &remainder); CHKERRQ(ierr);
763 ierr = DMSwarmSetCellDM(user->
swarm, user->
da); CHKERRQ(ierr);
777 ierr = DMSwarmSetLocalSizes(user->
swarm, *particlesPerProcess, *particlesPerProcess / 10); CHKERRQ(ierr);
783 ierr = DMView(user->
swarm, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr);
789 PetscFunctionReturn(0);
805 const PetscReal *positions,
const PetscInt *cellIndices,
806 PetscReal *velocities,PetscInt *LocStatus,PetscReal *diffusivity,
Cmpnts *diffusivitygradient, PetscReal *psi,
Particle *particle) {
807 PetscFunctionBeginUser;
813 if (particle == NULL) {
814 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Output Particle pointer is NULL. \n");
817 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Input PIDs pointer is NULL.\n");
819 if(positions == NULL){
820 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Input positions pointer is NULL.\n");
822 if(cellIndices == NULL){
823 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Input cellIndices pointer is NULL.\n");
825 if(LocStatus == NULL){
826 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Input LocStatus pointer is NULL.\n");
829 particle->
PID = PIDs[i];
837 particle->
weights.
x = weights[3 * i];
838 particle->
weights.
y = weights[3 * i + 1];
839 particle->
weights.
z = weights[3 * i + 2];
842 particle->
loc.
x = positions[3 * i];
843 particle->
loc.
y = positions[3 * i + 1];
844 particle->
loc.
z = positions[3 * i + 2];
846 if(velocities == NULL){
847 particle->
vel.
x = 0.0;
848 particle->
vel.
y = 0.0;
849 particle->
vel.
z = 0.0;
851 particle->
vel.
x = velocities[3 * i];
852 particle->
vel.
y = velocities[3 * i + 1];
853 particle->
vel.
z = velocities[3 * i + 2];
856 particle->
diffusivity = (diffusivity == NULL) ? 1.0 : diffusivity[i];
858 if(diffusivitygradient == NULL){
866 particle->
psi = (psi == NULL) ? 0.0 : psi[i];
868 particle->
cell[0] = cellIndices[3 * i];
869 particle->
cell[1] = cellIndices[3 * i + 1];
870 particle->
cell[2] = cellIndices[3 * i + 2];
880 PetscFunctionReturn(0);
888 PetscReal *positions,
889 PetscReal *velocities,
891 PetscInt *cellIndices,
893 PetscReal *diffusivity,
894 Cmpnts *diffusivitygradient,
897 PetscFunctionBeginUser;
901 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Input Particle pointer is NULL.\n");
906 positions[3 * i + 0] = particle->
loc.
x;
907 positions[3 * i + 1] = particle->
loc.
y;
908 positions[3 * i + 2] = particle->
loc.
z;
913 velocities[3 * i + 0] = particle->
vel.
x;
914 velocities[3 * i + 1] = particle->
vel.
y;
915 velocities[3 * i + 2] = particle->
vel.
z;
920 weights[3 * i + 0] = particle->
weights.
x;
921 weights[3 * i + 1] = particle->
weights.
y;
922 weights[3 * i + 2] = particle->
weights.
z;
927 cellIndices[3 * i + 0] = particle->
cell[0];
928 cellIndices[3 * i + 1] = particle->
cell[1];
929 cellIndices[3 * i + 2] = particle->
cell[2];
942 if(diffusivitygradient){
949 psi[i] = particle->
psi;
953 PetscFunctionReturn(0);
962 PetscFunctionBeginUser;
966 if (!d || !particle) {
967 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
968 "Null pointer argument (d or particle).");
977 "face distance d[%" PetscInt_FMT
"] = %g <= %g; "
978 "clamping to avoid zero/negative.\n",
992 PetscFunctionReturn(0);
1004 PetscErrorCode ierr;
1005 PetscInt particlesPerProcess = 0;
1008 PetscFunctionBeginUser;
1020 PetscBool should_initialize_new_particles = PETSC_FALSE;
1022 should_initialize_new_particles = PETSC_TRUE;
1025 should_initialize_new_particles = PETSC_TRUE;
1029 should_initialize_new_particles = PETSC_TRUE;
1035 if (should_initialize_new_particles) {
1056 PetscFunctionReturn(0);
1066#define PARTICLE_INLET_PID_BASE ((PetscInt64)1 << 40)
1067#define PARTICLE_INLET_PIDS_PER_CELL ((PetscInt64)1 << 10)
1068#define PARTICLE_PID_LIMIT ((PetscInt64)1 << 53)
1069#define PARTICLE_INLET_FACE_COUNT 6
1070#define PARTICLE_INLET_SUMS 4
1088 static const char *
const names[] = {
"t"};
1090 PetscBool found = PETSC_FALSE;
1092 PetscFunctionBeginUser;
1094 PetscCall(PetscOptionsGetInt(NULL, NULL,
"-particle_inlet_count", &count, &found));
1097 "-particle_inlet_count must be between 0 and %d (got %" PetscInt_FMT
").",
1100 "Injected particle IDs start at %" PetscInt64_FMT
", so a run with particle inlets seeds fewer "
1104 for (PetscInt n = 0; n < count; ++n) {
1106 char option[256], text[4096];
1109 PetscCall(PetscSNPrintf(option,
sizeof(option),
"-particle_inlet_%" PetscInt_FMT
"_face", n));
1110 PetscCall(PetscOptionsGetInt(NULL, NULL, option, &
face, &found));
1114 PetscCall(PetscSNPrintf(option,
sizeof(option),
"-particle_inlet_%" PetscInt_FMT
"_particles_per_cell", n));
1115 PetscCall(PetscOptionsGetString(NULL, NULL, option, text,
sizeof(text), &found));
1116 PetscCheck(found, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
"%s is missing.", option);
1118 PetscCall(PetscSNPrintf(option,
sizeof(option),
"particle_inlet_%" PetscInt_FMT
"_fields", n));
1121 "An inlet's particle field values cannot be combined with the verification scalar source, "
1122 "which prescribes Psi at every step.");
1125 PetscFunctionReturn(0);
1134 PetscFunctionBeginUser;
1145 PetscFunctionReturn(0);
1163 DM swarm = user->
swarm;
1165 const PetscInt nodes[3] = {user->
info.mx, user->
info.my, user->
info.mz};
1166 const PetscInt normal = (PetscInt)inlet->
face / 2, a = (normal == 0) ? 1 : 0, b = (normal == 2) ? 1 : 2;
1167 const PetscBool negative = (PetscBool)((PetscInt)inlet->
face % 2 == 0);
1168 const PetscReal sign = negative ? 1.0 : -1.0, L_ref = simCtx->
scaling.
L_ref;
1171 const Vec metric_vec = (normal == 0) ? user->
lCsi : (normal == 1) ? user->
lEta : user->
lZet;
1172 const Cmpnts ***coor = NULL, ***ucont = NULL, ***metric = NULL;
1173 const PetscReal ***diffusivity = NULL;
1174 PetscInt64 *cell_id = NULL;
1175 PetscInt *count = NULL;
1176 Cmpnts *travel = NULL, *inward = NULL, *normal_of = NULL;
1177 PetscReal *flux_bound = NULL;
1178 PetscInt first = 0, n_new = 0;
1179 PetscReal density = 0.0;
1182 PetscFunctionBeginUser;
1185 PetscCheck(!PetscIsInfOrNanReal(density) && density >= 0.0, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
1186 "The particles per cell of inlet %s is %g at t = %g; it must be finite and not negative.",
1191 PetscCall(PetscMalloc5(n_cells, &count, n_cells, &cell_id, n_cells, &travel, n_cells, &normal_of, n_cells, &flux_bound));
1192 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lCoor, (
void *)&coor));
1193 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lUcont, (
void *)&ucont));
1194 PetscCall(DMDAVecGetArrayRead(user->
fda, metric_vec, (
void *)&metric));
1195 for (PetscInt c = 0; c < n_cells; ++c) {
1198 PetscInt f[3] = {origin[0] + 1, origin[1] + 1, origin[2] + 1};
1199 f[normal] = negative ? 0 : nodes[normal] - 2;
1200 const Cmpnts u = ucont[f[2]][f[1]][f[0]], m = metric[f[2]][f[1]][f[0]];
1201 const PetscReal flux = PetscMax(sign * ((normal == 0) ? u.
x : (normal == 1) ? u.
y : u.
z), 0.0);
1202 const PetscReal expected = density * flux * simCtx->
dt;
1203 const PetscReal whole = PetscFloorReal(expected);
1205 const PetscReal along_m = sign * flux * simCtx->
dt / (m.x * m.x + m.y * m.y + m.z * m.z);
1208 "Inlet %s would inject %g particles through one face cell in one step; the limit is %" PetscInt64_FMT
1209 ". Lower particles_per_cell or the time step.",
BCFaceToString(inlet->
face), (
double)expected,
1212 count[c] = (PetscInt)whole;
1214 travel[c].
x = along_m * m.x; travel[c].
y = along_m * m.y; travel[c].
z = along_m * m.z;
1222 const PetscReal norm_m = PetscSqrtReal(m.x * m.x + m.y * m.y + m.z * m.z);
1223 normal_of[c].x = sign * m.x / norm_m; normal_of[c].y = sign * m.y / norm_m; normal_of[c].z = sign * m.z / norm_m;
1224 flux_bound[c] = 0.0;
1225 for (PetscInt q = 0; count[c] > 0 && q < 9; ++q) {
1226 PetscReal lattice[3], logic[3];
1228 lattice[a] = 0.5 * (q % 3); lattice[b] = 0.5 * (q / 3); lattice[normal] = 0.0;
1230 PetscCall(
MetricLogicalToPhysical(user, coor, origin[0], origin[1], origin[2], logic[0], logic[1], logic[2], &point));
1232 flux_bound[c] = PetscMax(flux_bound[c], u_q.
x * normal_of[c].x + u_q.
y * normal_of[c].y + u_q.
z * normal_of[c].z);
1234 flux_bound[c] *= 1.1;
1236 sums[0] += flux * simCtx->
scaling.
U_ref * L_ref * L_ref;
1237 sums[1] += expected;
1239 sums[2] += (PetscReal)n_new;
1240 PetscCall(DMDAVecRestoreArrayRead(user->
fda, metric_vec, (
void *)&metric));
1241 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lUcont, (
void *)&ucont));
1244 PetscCall(DMSwarmGetLocalSize(swarm, &first));
1245 PetscCall(PetscMalloc1(n_new, &inward));
1247 PetscReal *positions = NULL, *weights = NULL;
1248 PetscInt *cell = NULL, *status = NULL;
1249 PetscInt64 *pids = NULL;
1252 PetscCall(DMSwarmSetLocalSizes(swarm, first + n_new, -1));
1254 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lDiffusivity, (
void *)&diffusivity));
1260 for (PetscInt c = 0; c < n_cells; ++c) {
1261 for (PetscInt k = 0; k < count[c]; ++k, ++p) {
1264 const PetscBool weighted = (PetscBool)(flux_bound[c] > 0.0);
1265 PetscInt ci, cj, ck;
1266 PetscReal xi, eta, zta;
1271 PetscCall(
DrawSeedingPoint(user, coor,
table, c, pid, weighted ? velocity : NULL, normal_of[c], flux_bound[c],
1272 &ci, &cj, &ck, &xi, &eta, &zta, &u_p));
1273 if (!weighted) { u_p.
x = travel[c].
x / simCtx->
dt; u_p.
y = travel[c].
y / simCtx->
dt; u_p.
z = travel[c].
z / simCtx->
dt; }
1275 positions[3 * p + 0] = point.
x; positions[3 * p + 1] = point.
y; positions[3 * p + 2] = point.
z;
1276 weights[3 * p + 0] = xi; weights[3 * p + 1] = eta; weights[3 * p + 2] = zta;
1277 cell[3 * p + 0] = ci; cell[3 * p + 1] = cj; cell[3 * p + 2] = ck;
1283 inward[p - first].x = share * u_p.
x * simCtx->
dt + kick.
x;
1284 inward[p - first].y = share * u_p.
y * simCtx->
dt + kick.
y;
1285 inward[p - first].z = share * u_p.
z * simCtx->
dt + kick.
z;
1286 LOG_PARTICLE(simCtx, pid,
"inject",
"through %s on rank %d at (%.6e, %.6e, %.6e): cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
")\n",
1295 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lDiffusivity, (
void *)&diffusivity));
1297 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lCoor, (
void *)&coor));
1304 PetscReal *positions = NULL, *psi = NULL;
1308 for (PetscInt q = 0; q < n_new; ++q) {
1309 positions[3 * (first + q) + 0] += inward[q].x;
1310 positions[3 * (first + q) + 1] += inward[q].y;
1311 positions[3 * (first + q) + 2] += inward[q].z;
1312 sums[3] += psi[first + q];
1317 PetscCall(PetscFree(inward));
1318 PetscCall(PetscFree5(count, cell_id, travel, normal_of, flux_bound));
1320 PetscFunctionReturn(0);
1333 PetscInt64 faces = 0, first_id;
1336 PetscFunctionBeginUser;
1340 const PetscInt64 cells[3] = {user->
info.mx - 2, user->
info.my - 2, user->
info.mz - 2};
1341 faces = 2 * (cells[0] * cells[1] + cells[1] * cells[2] + cells[2] * cells[0]);
1343 first_id = (PetscInt64)simCtx->
step * faces;
1345 PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
1346 "Step %" PetscInt_FMT
" on a grid with %" PetscInt64_FMT
" boundary face cells passes the last particle ID a "
1347 "checkpoint can store (2^53).", simCtx->
step, faces);
1352 PetscBool have_cells_per_volume = PETSC_FALSE;
1357 const Cmpnts ***coor = NULL;
1360 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lCoor, (
void *)&coor));
1362 if (!have_cells_per_volume) {
1371 have_cells_per_volume = PETSC_TRUE;
1373 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lCoor, (
void *)&coor));
1377 PetscCall(
InjectAtInlet(user, inlet, &event, first_id, velocity, local[n]));
1382 if (simCtx->
rank == 0) {
1386 "step,physical_time,face,flow_rate,expected,injected,psi", &file));
1388 fprintf(file,
"%d,%.10e,%s,%.10e,%.10e,%.0f,%.10e\n", (
int)simCtx->
step, (
double)event.physical_time,
1390 (
double)global[n][2], (
double)global[n][3]);
1392 PetscCheck(fclose(file) == 0, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE,
"Unable to close the particle-injection diagnostics file.");
1395 PetscInt n_local = 0;
1396 PetscInt64 local_total, total = 0;
1399 PetscCall(DMSwarmGetLocalSize(user->
swarm, &n_local));
1400 local_total = n_local;
1401 PetscCallMPI(MPI_Allreduce(&local_total, &total, 1, MPIU_INT64, MPI_SUM, PETSC_COMM_WORLD));
1402 PetscCall(PetscSNPrintf(
object,
sizeof(
object),
"the %" PetscInt64_FMT
" particles after injection (three components each)", total));
1406 PetscFunctionReturn(0);
PetscErrorCode CanRankServiceFace(const DMDALocalInfo *info, PetscInt IM_nodes_global, PetscInt JM_nodes_global, PetscInt KM_nodes_global, BCFace face_id, PetscBool *can_service_out)
Determines if the current MPI rank owns any part of a specified global face.
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 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.
Configured initial values of particle-carried fields.
PetscErrorCode ParticleFieldPlanApply(UserCtx *user, ParticleFieldPlan *plan, const ParticleFieldEvent *event, PetscInt first, PetscInt end)
Write the plan's values into swarm entries [first, end) of this rank.
PetscErrorCode ParticleFieldPlanDestroy(ParticleFieldPlan **plan)
Free a plan and its compiled expressions.
PetscErrorCode ParticleFieldPlanCreate(const char *prefix, ParticleFieldPlan **plan)
Read a plan from the options database.
PetscReal physical_time
Time, in seconds, the evaluated particles appear at.
When and why a plan is applied.
Header file for Particle Motion and migration related functions.
static void ParticleUniformsFromHash(uint64_t hash, PetscReal u[4])
Four uniforms in [0,1) from one keyed draw hash.
PetscErrorCode PreCheckAndResizeSwarm(UserCtx *user, PetscInt ti, const char *ext)
Checks particle count in the reference file and resizes the swarm if needed.
PetscErrorCode CalculateBrownianDisplacement(UserCtx *user, PetscInt64 pid, PetscReal diff_eff, Cmpnts *displacement)
Calculates the stochastic displacement vector (Brownian motion) for a single particle.
PetscErrorCode UpdateParticleWeights(PetscReal *d, Particle *particle)
Implementation of UpdateParticleWeights().
#define PARTICLE_INLET_PID_BASE
#define PARTICLE_INLET_SUMS
PetscErrorCode CreateParticleSwarm(UserCtx *user, PetscInt numParticles, PetscInt *particlesPerProcess)
Implementation of CreateParticleSwarm().
PetscInt64 * pid_hi
One past the last particle ID of each cell.
PetscErrorCode InjectParticlesAtInlets(UserCtx *user)
Implementation of InjectParticlesAtInlets().
PetscReal cells_per_volume
The grid's cells over its volume, set with the table.
PetscErrorCode DistributeParticles(PetscInt numParticles, PetscMPIInt rank, PetscMPIInt size, PetscInt *particlesPerProcess, PetscInt *remainder)
Implementation of DistributeParticles().
#define INTERPOLATION_DISTANCE_TOLERANCE
static PetscErrorCode DistributeParticlesByOwnedMeasure(UserCtx *user, PetscInt face, PetscInt numParticles, PetscInt *localCount)
Number of particles this rank seeds for volumetric or surface seeding.
#define SEEDING_FACE_OFFSET
PetscErrorCode ParticleInletsDestroy(SimCtx *simCtx)
Implementation of ParticleInletsDestroy().
SeedingCellTable table
The owned cells on the face; built at the first injection.
PetscInt face
-1 for the volume, otherwise the BCFace the points lie on
static PetscErrorCode BuildSeedingCellTable(UserCtx *user, const Cmpnts ***coor, PetscInt face, PetscInt numParticles, PetscBool build_bound, SeedingCellTable *table)
Decide which particles each owned cell seeds, the same way at every rank layout.
ParticleFieldPlan * plan
This inlet's field values; NULL uses the case's.
PetscInt origin[3]
Origin node of the box's first cell.
static PetscErrorCode FreeSeedingCellTable(SeedingCellTable *table)
Free the arrays of a SeedingCellTable.
PicurvExpression * particles_per_cell
Mean particles per cell the entering fluid carries, in t.
#define PARTICLE_INLET_PIDS_PER_CELL
static PetscErrorCode InitializeParticleBasicProperties(UserCtx *user, PetscInt particlesPerProcess)
Initialize position-independent particle fields after a particle is created.
PetscInt64 * pid_lo
First particle ID of each cell.
static void SeedingCellOrigin(const SeedingCellTable *table, PetscInt c, PetscInt origin[3])
Origin node of cell c of a seeding table, cells numbered i fastest.
PetscInt count
Particles this rank seeds.
PetscReal total
Measure of every cell on every rank: the domain's volume or the face's area.
#define PARTICLE_INLET_FACE_COUNT
PetscInt n[3]
Cells per axis.
PetscReal * bound
Largest measure density in each cell (NULL when not built)
#define SEEDING_LOGIC_MARGIN
PetscErrorCode UnpackSwarmFields(PetscInt i, const PetscInt64 *PIDs, const PetscReal *weights, const PetscReal *positions, const PetscInt *cellIndices, PetscReal *velocities, PetscInt *LocStatus, PetscReal *diffusivity, Cmpnts *diffusivitygradient, PetscReal *psi, Particle *particle)
Implementation of UnpackSwarmFields().
PetscErrorCode ParticleInletsCreate(SimCtx *simCtx)
Implementation of ParticleInletsCreate().
PetscErrorCode InitializeSwarm(UserCtx *user)
Implementation of InitializeSwarm().
static void SeedingLogicalPoint(const PetscReal u[3], PetscInt face, PetscReal logic[3])
Logical seeding coordinates from u: each clamped to [0, 1 - SEEDING_LOGIC_MARGIN],...
PetscErrorCode AssignInitialPropertiesToSwarm(UserCtx *user, PetscInt particlesPerProcess)
Implementation of AssignInitialPropertiesToSwarm().
PetscErrorCode UpdateSwarmFields(PetscInt i, const Particle *particle, PetscReal *positions, PetscReal *velocities, PetscReal *weights, PetscInt *cellIndices, PetscInt *status, PetscReal *diffusivity, Cmpnts *diffusivitygradient, PetscReal *psi)
Implementation of UpdateSwarmFields().
static PetscErrorCode SeedingMeasureDensity(UserCtx *user, const Cmpnts ***coor, PetscInt face, PetscInt i, PetscInt j, PetscInt k, const PetscReal logic[3], PetscReal *density)
Measure density at a logical point: the Jacobian determinant for the volume, or the area element |dx/...
PetscErrorCode InitializeParticleSwarm(SimCtx *simCtx)
Implementation of InitializeParticleSwarm().
PetscErrorCode RegisterSwarmField(DM swarm, const char *fieldName, PetscInt fieldDim, PetscDataType dtype)
Implementation of RegisterSwarmField().
static PetscErrorCode InitializeDefaultParticleFields(DM swarm, PetscInt first, PetscInt end)
Set local particles first to end - 1 of every field the particle catalog marks PARTICLE_FIELD_CAPABIL...
#define PARTICLE_PID_LIMIT
static PetscErrorCode DrawSeedingPoint(UserCtx *user, const Cmpnts ***coor, const SeedingCellTable *table, PetscInt c, PetscInt64 pid, const VelocityPointInterpolator *velocity, Cmpnts normal, PetscReal flux_bound, PetscInt *ci_metric_lnode_out, PetscInt *cj_metric_lnode_out, PetscInt *ck_metric_lnode_out, PetscReal *xi_metric_logic_out, PetscReal *eta_metric_logic_out, PetscReal *zta_metric_logic_out, Cmpnts *velocity_out)
Draw one particle's seeding point inside its cell, uniform in the cell's measure, or on a face in pro...
PetscErrorCode RegisterParticleFields(DM swarm)
Implementation of RegisterParticleFields().
static PetscErrorCode InjectAtInlet(UserCtx *user, struct ParticleInlet *inlet, const ParticleFieldEvent *event, PetscInt64 first_id, const VelocityPointInterpolator *velocity, PetscReal sums[4])
Add one inlet's particles for this step.
One inlet face that injects particles.
The owned cells a seeding mode places particles in, and which particles each gets.
Header file for Particle Swarm management functions.
PetscErrorCode PicurvExpressionEvaluate(const PicurvExpression *expression, const PetscReal *values, const PicurvExpressionDrawKey *key, PetscReal *result)
Evaluate a compiled expression.
PetscErrorCode PicurvExpressionDestroy(PicurvExpression **expression)
Free a compiled expression.
uint64_t PicurvDrawHash(PetscInt64 seed, PicurvDrawSpace space, uint64_t stream, PetscInt64 id, PetscInt64 salt)
Hash that keys a draw: a pure function of its arguments, so a draw does not depend on the rank that m...
static PetscInt64 PicurvFaceCellId(const PetscInt nodes[3], BCFace face, PetscInt p, PetscInt q)
Global identity of a boundary face cell, unique over the six faces of a block and independent of the ...
@ PICURV_DRAW_SPACE_INLET
Injection at an inlet: stream 0 counts per face cell, stream 1 distance in.
@ PICURV_DRAW_SPACE_PLACEMENT
Seeding points; the salt is the rejection attempt.
PetscReal PicurvUnitFromHash(uint64_t hash)
Uniform on [0, 1) from the top 53 bits of a hash.
PetscErrorCode PicurvExpressionCompile(const char *text, const char *const *names, PetscInt name_count, PetscBool allow_random, PicurvExpression **expression)
Compile one expression against a list of variable names.
The arrays one method's point evaluation reads.
PetscErrorCode InterpolateVelocityAtPoint(const VelocityPointInterpolator *interp, const PetscInt cell[3], const PetscReal weights[3], Cmpnts point, Cmpnts *u)
The velocity a particle at a point would be given: the value the swarm pass of the configured method ...
PetscErrorCode InterpolateVelocityAtPointEnd(VelocityPointInterpolator **interp)
Closes the arrays InterpolateVelocityAtPointBegin() opened.
PetscErrorCode InterpolateVelocityAtPointBegin(UserCtx *user, VelocityPointInterpolator **interp)
Opens the arrays for evaluating the particle velocity at points, with the configured interpolation me...
PetscErrorCode ReadAllSwarmFields(UserCtx *user, PetscInt ti)
Reads multiple fields (positions, velocity, CellID, and weight) into a DMSwarm.
PetscErrorCode PicurvPhysicalTime(const SimCtx *simCtx, PetscReal solver_time, PetscReal *physical)
Convert a solver time to physical seconds, t * L_ref / U_ref.
#define LOG_PARTICLE(simCtx, pid, stage, fmt,...)
Reports one particle at one stage of a step: the consolidated per-particle line.
PetscBool is_function_allowed(const char *functionName)
Checks if a given function is in the allow-list.
#define LOG_ALLOW_SYNC(scope, level, fmt,...)
Synchronized logging macro that checks both the log level and whether the calling function is in the ...
#define LOCAL
Logging scope definitions for controlling message output.
#define GLOBAL
Scope for global logging across all processes.
const char * BCFaceToString(BCFace face)
Returns the canonical log token for a boundary-face enum value.
static PetscErrorCode PicurvOpenDiagnosticsCsv(SimCtx *simCtx, const char *dir, const char *filename, const char *header, FILE **file)
Opens a per-step diagnostics CSV: PicurvOpenRunCsv() with the common settings.
#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_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
@ LOG_ERROR
Critical errors that may halt the program.
@ LOG_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ 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.
const char * ParticleInitializationToString(ParticleInitializationType ParticleInitialization)
Returns the canonical log token for a particle-initialization mode.
static LogLevel get_log_level(void)
Retrieves the current logging level from the environment variable LOG_LEVEL.
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_LOCATION_STATUS
@ PARTICLE_FIELD_ID_WEIGHT
@ PARTICLE_FIELD_ID_POSITION
@ PARTICLE_FIELD_ID_CELL_ID
@ PARTICLE_FIELD_ID_COUNT
PetscReal default_real_value
unsigned int capabilities
ParticleFieldRegistration registration
@ PARTICLE_FIELD_CAPABILITY_DEFAULT_INITIALIZE
@ PARTICLE_FIELD_REGISTRATION_PETSC
PetscErrorCode ParticleFieldGetDescriptor(ParticleFieldId field_id, const ParticleFieldDescriptor **descriptor)
Return immutable metadata for a valid particle field ID.
const char * canonical_name
Immutable metadata for one persistent particle field.
PetscErrorCode GetOwnedCellRange(const DMDALocalInfo *info_nodes, PetscInt dim, PetscInt *xs_cell_global_out, PetscInt *xm_cell_local_out)
Determines the global starting index and number of CELLS owned by the current processor in a specifie...
PetscErrorCode PicurvCheckPetscIntRange(MPI_Comm comm, PetscInt64 entries, const char *object)
Refuse a PETSc object whose global size this build's PetscInt cannot index.
PetscInt particleInletCount
Entries of particleInlets.
PetscBool inletFaceDefined
BoundaryFaceConfig boundary_faces[6]
BCFace identifiedInletBCFace
SimCtx * simCtx
Back-pointer to the master simulation context.
@ PARTICLE_INIT_SURFACE_RANDOM
Random placement on the inlet face.
@ PARTICLE_INIT_POINT_SOURCE
All particles at a fixed (psrc_x,psrc_y,psrc_z) — for validation.
@ PARTICLE_INIT_VOLUME
Random volumetric distribution across the domain.
ParticleLocationStatus
Defines the state of a particle with respect to its location and migration status during the iterativ...
Cmpnts max_coords
Maximum x, y, z coordinates of the bounding box.
Cmpnts diffusivitygradient
Cmpnts min_coords
Minimum x, y, z coordinates of the bounding box.
PetscMPIInt destination_rank
PetscReal psrc_z
Point source location for PARTICLE_INIT_POINT_SOURCE.
ParticleLocationStatus location_status
char analysis_dir[PETSC_MAX_PATH_LEN]
char particleRestartMode[16]
ParticleInitializationType ParticleInitialization
PetscInt particleRandomSeed
Base seed for every particle RNG stream (-particle_random_seed).
@ EXEC_MODE_POSTPROCESSOR
struct ParticleInlet * particleInlets
Inlet faces that inject particles, or NULL.
struct ParticleFieldPlan * particleFieldPlan
Configured initial particle-field values, or NULL.
BCFace
Identifies the six logical faces of a structured computational block.
A 3D point or vector with PetscScalar components.
Defines a particle's core properties for Lagrangian tracking.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.
PetscBool VerificationScalarOverrideActive(const SimCtx *simCtx)
Reports whether a verification-only scalar override is active.
PetscErrorCode LocateParticleOrFindMigrationTarget(UserCtx *user, const struct FacePlaneArrays *planes, Particle *particle, ParticleLocationStatus *status_out)