PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Functions
Internal Scattering Helpers

Lower-level functions used by the main scattering routines. More...

Collaboration diagram for Internal Scattering Helpers:

Functions

PetscErrorCode AccumulateParticleField (DM swarm, ParticleFieldId particle_field_id, DM gridSumDM, Vec gridSumVec)
 Accumulates a particle field (scalar or vector) into a target grid sum vector.
 
PetscErrorCode NormalizeGridVectorByCount (DM countDM, Vec countVec, DM dataDM, Vec sumVec, Vec avgVec)
 Normalizes a grid vector of sums by a grid vector of counts to produce an average.
 

Detailed Description

Lower-level functions used by the main scattering routines.

Function Documentation

◆ AccumulateParticleField()

PetscErrorCode AccumulateParticleField ( DM  swarm,
ParticleFieldId  particle_field_id,
DM  gridSumDM,
Vec  gridSumVec 
)

Accumulates a particle field (scalar or vector) into a target grid sum vector.

This function iterates through local particles, identifies their cell using the "DMSwarm_CellID" field, and adds the particle's field value (particleFieldName) to the corresponding cell location in the gridSumVec. It handles both scalar (DOF=1) and vector (DOF=3) fields automatically based on the DOF of gridSumDM.

IMPORTANT: The caller must ensure gridSumVec is zeroed before calling this function if a fresh sum calculation is desired.

Parameters
[in]swarmThe DMSwarm containing particles.
[in]particle_field_idTyped identity of the particle field (must match DOF).
[in]gridSumDMThe DMDA associated with gridSumVec. Its DOF determines how many components are accumulated.
[in,out]gridSumVecThe Vec (associated with gridSumDM) to accumulate sums into.
Returns
PetscErrorCode 0 on success. Errors if fields don't exist or DMs are incompatible.

Definition at line 1494 of file interpolation.c.

1496{
1497 PetscErrorCode ierr;
1498 PetscInt dof;
1499 PetscInt nlocal, p;
1500 const PetscReal *particle_arr = NULL;
1501 const PetscInt *cell_id_arr = NULL;
1502
1503 // DMDA Accessors
1504 PetscScalar ***arr_1d = NULL; // For scalar fields
1505 PetscScalar ****arr_3d = NULL; // For vector fields
1506
1507 // Ghosted dimension variables
1508 PetscInt gxs, gys, gzs, gxm, gym, gzm;
1509 char msg[ERROR_MSG_BUFFER_SIZE];
1510 const ParticleFieldDescriptor *descriptor = NULL;
1511 const char *particleFieldName = NULL;
1512
1513 PetscFunctionBeginUser;
1515
1516 ierr = ParticleFieldGetDescriptor(particle_field_id, &descriptor); CHKERRQ(ierr);
1517 PetscCheck(descriptor->data_type == PETSC_REAL, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
1518 "Particle-to-grid accumulation requires PETSC_REAL data; field '%s' uses %s.",
1519 descriptor->canonical_name, PetscDataTypes[descriptor->data_type]);
1520 particleFieldName = descriptor->canonical_name;
1521
1522 // --- 1. Validation & Setup ---
1523 if (!swarm || !gridSumDM || !localAccumulatorVec)
1524 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null input in AccumulateParticleField.");
1525
1526 // Get DMDA information
1527 ierr = DMDAGetInfo(gridSumDM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL); CHKERRQ(ierr);
1528 // Get Ghosted Corners (Global indices of the ghosted patch start, and its dimensions)
1529 ierr = DMDAGetGhostCorners(gridSumDM, &gxs, &gys, &gzs, &gxm, &gym, &gzm); CHKERRQ(ierr);
1530
1531 // --- 2. Verify Vector Type (Global vs Local) ---
1532 {
1533 PetscInt vecSize;
1534 PetscInt expectedLocalSize = gxm * gym * gzm * dof;
1535 ierr = VecGetSize(localAccumulatorVec, &vecSize); CHKERRQ(ierr);
1536
1537 if (vecSize != expectedLocalSize) {
1538 PetscSNPrintf(msg, sizeof(msg),
1539 "Vector dimension mismatch! Expected Ghosted Local Vector size %" PetscInt_FMT " (gxm*gym*gzm*dof), got %" PetscInt_FMT ". "
1540 "Did you pass a Global Vector instead of a Local Vector?",
1541 expectedLocalSize, vecSize);
1542 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "%s", msg);
1543 }
1544 }
1545
1546 // --- 3. Acquire Particle Data ---
1547 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
1548 // These calls will fail nicely if the field doesn't exist
1549 ierr = DMSwarmGetField(swarm, particleFieldName, NULL, NULL, (void **)&particle_arr); CHKERRQ(ierr);
1550 ierr = DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_CELL_ID), NULL, NULL, (void **)&cell_id_arr); CHKERRQ(ierr);
1551
1552 // --- 4. Acquire Grid Accessors ---
1553 // DMDAVecGetArray* handles the mapping from Global (i,j,k) to the underlying Local Array index
1554 if (dof == 1) {
1555 ierr = DMDAVecGetArray(gridSumDM, localAccumulatorVec, &arr_1d); CHKERRQ(ierr);
1556 } else if (dof == 3) {
1557 ierr = DMDAVecGetArrayDOF(gridSumDM, localAccumulatorVec, &arr_3d); CHKERRQ(ierr);
1558 } else {
1559 PetscSNPrintf(msg, sizeof(msg), "Unsupported DOF=%" PetscInt_FMT ". AccumulateParticleField supports DOF 1 or 3.", dof);
1560 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "%s", msg);
1561 }
1562
1563 // --- 5. Accumulate Loop ---
1564 // Iterate through all local particles
1565 for (p = 0; p < nlocal; ++p) {
1566 // Retrieve Geometric Grid Index (0-based)
1567 PetscInt i_geom = cell_id_arr[p * 3 + 0];
1568 PetscInt j_geom = cell_id_arr[p * 3 + 1];
1569 PetscInt k_geom = cell_id_arr[p * 3 + 2];
1570
1571 // Apply Shift (+1) to match Memory Layout (Index 0 is boundary/ghost)
1572 PetscInt i = i_geom + 1;
1573 PetscInt j = j_geom + 1;
1574 PetscInt k = k_geom + 1;
1575
1576 // Bounds Check: Ensure (i,j,k) falls within the Local Ghosted Patch.
1577 // This allows writing to ghost slots which will later be reduced to the owner rank.
1578 if (i >= gxs && i < gxs + gxm &&
1579 j >= gys && j < gys + gym &&
1580 k >= gzs && k < gzs + gzm)
1581 {
1582 if (dof == 1) {
1583 arr_1d[k][j][i] += particle_arr[p];
1584 } else {
1585 // For DOF=3, unroll the loop for slight optimization
1586 arr_3d[k][j][i][0] += particle_arr[p * 3 + 0];
1587 arr_3d[k][j][i][1] += particle_arr[p * 3 + 1];
1588 arr_3d[k][j][i][2] += particle_arr[p * 3 + 2];
1589 }
1590 }
1591 // Note: Particles outside the ghost layer are skipped. This is expected behavior
1592 // if particles have not yet been migrated or localized correctly.
1593 }
1594
1595 // --- 6. Restore Arrays and Fields ---
1596 if (dof == 1) {
1597 ierr = DMDAVecRestoreArray(gridSumDM, localAccumulatorVec, &arr_1d); CHKERRQ(ierr);
1598 } else {
1599 ierr = DMDAVecRestoreArrayDOF(gridSumDM, localAccumulatorVec, &arr_3d); CHKERRQ(ierr);
1600 }
1601
1602 ierr = DMSwarmRestoreField(swarm, particleFieldName, NULL, NULL, (void **)&particle_arr); CHKERRQ(ierr);
1603 ierr = DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_CELL_ID), NULL, NULL, (void **)&cell_id_arr); CHKERRQ(ierr);
1604
1606 PetscFunctionReturn(0);
1607}
#define ERROR_MSG_BUFFER_SIZE
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:1029
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
const char * ParticleFieldName(ParticleFieldId field_id)
Return the canonical PETSc DMSwarm name for an ID.
@ PARTICLE_FIELD_ID_CELL_ID
PetscErrorCode ParticleFieldGetDescriptor(ParticleFieldId field_id, const ParticleFieldDescriptor **descriptor)
Return immutable metadata for a valid particle field ID.
Immutable metadata for one persistent particle field.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ NormalizeGridVectorByCount()

PetscErrorCode NormalizeGridVectorByCount ( DM  countDM,
Vec  countVec,
DM  dataDM,
Vec  sumVec,
Vec  avgVec 
)

Normalizes a grid vector of sums by a grid vector of counts to produce an average.

Calculates avgVec[i] = sumVec[i] / countVec[i] for each component of each OWNED cell where countVec[i] > 0. Sets avgVec[i] = 0 otherwise. Handles both scalar (DOF=1) and vector (DOF=3) data fields based on dataDM. Uses basic VecGetArray/VecGetArrayRead and manual index calculation.

Parameters
[in]countDMThe DMDA associated with countVec (must have DOF=1).
[in]countVecThe Vec containing particle counts per cell (read-only).
[in]dataDMThe DMDA associated with sumVec and avgVec (must have DOF=1 or DOF=3).
[in]sumVecThe Vec containing the accumulated sums per cell (read-only).
[in,out]avgVecThe Vec where the calculated averages will be stored (overwritten).
Returns
PetscErrorCode 0 on success.

Definition at line 1611 of file interpolation.c.

1613{
1614 PetscErrorCode ierr;
1615 PetscInt data_dof;
1616 PetscInt count_dof;
1617 PetscMPIInt rank;
1618 char msg[ERROR_MSG_BUFFER_SIZE];
1619
1620 // Pointers for DMDA array accessors - declare specific types
1621 PetscScalar ***count_arr_3d = NULL; // For DOF=1 count vector (3D DMDA)
1622 PetscScalar ***sum_arr_scalar = NULL; // For DOF=1 sum vector (3D DMDA)
1623 PetscScalar ***avg_arr_scalar = NULL; // For DOF=1 avg vector (3D DMDA)
1624 PetscScalar ****sum_arr_vector = NULL; // For DOF=3 sum vector (3D DMDA + DOF)
1625 PetscScalar ****avg_arr_vector = NULL; // For DOF=3 avg vector (3D DMDA + DOF)
1626
1627
1628 PetscFunctionBeginUser;
1629
1631
1632 rank = PetscGlobalRank;
1633
1634 // --- Validation ---
1635 ierr = DMDAGetInfo(countDM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &count_dof, NULL, NULL, NULL, NULL, NULL); CHKERRQ(ierr);
1636 ierr = DMDAGetInfo(dataDM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &data_dof, NULL, NULL, NULL, NULL, NULL); CHKERRQ(ierr);
1637 if (count_dof != 1) { PetscSNPrintf(msg, sizeof(msg), "countDM must have DOF=1, got %" PetscInt_FMT ".", count_dof); SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "%s", msg); }
1638 if (data_dof != 1 && data_dof != 3) { PetscSNPrintf(msg, sizeof(msg), "dataDM DOF must be 1 or 3, got %" PetscInt_FMT ".", data_dof); SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "%s", msg); }
1639
1640 // --- Get Array Access using appropriate DMDA accessors ---
1641 ierr = DMDAVecGetArrayRead(countDM, countVec, &count_arr_3d); CHKERRQ(ierr);
1642
1643 if (data_dof == 1) {
1644 ierr = DMDAVecGetArrayRead(dataDM, sumVec, &sum_arr_scalar); CHKERRQ(ierr);
1645 ierr = DMDAVecGetArray(dataDM, avgVec, &avg_arr_scalar); CHKERRQ(ierr);
1646 } else { // data_dof == 3
1647 ierr = DMDAVecGetArrayDOFRead(dataDM, sumVec, &sum_arr_vector); CHKERRQ(ierr);
1648 ierr = DMDAVecGetArrayDOF(dataDM, avgVec, &avg_arr_vector); CHKERRQ(ierr);
1649 }
1650
1651 // Get the corners (global start indices) and dimensions of the *local owned* region
1652 PetscInt xs, ys, zs, xm, ym, zm;
1653 ierr = DMDAGetCorners(countDM, &xs, &ys, &zs, &xm, &ym, &zm); CHKERRQ(ierr);
1654
1655 // --- Normalize Over Owned Cells ---
1656 LOG_ALLOW(LOCAL, LOG_DEBUG, "(Rank %d): Normalizing DOF=%" PetscInt_FMT " data over owned range [%" PetscInt_FMT ":%" PetscInt_FMT ", %" PetscInt_FMT ":%" PetscInt_FMT ", %" PetscInt_FMT ":%" PetscInt_FMT "].\n",
1657 rank, data_dof, xs, xs+xm, ys, ys+ym, zs, zs+zm);
1658
1659 // Loop using GLOBAL indices (i, j, k) over the range owned by this process
1660 for (PetscInt k = zs; k < zs + zm; ++k) {
1661 for (PetscInt j = ys; j < ys + ym; ++j) {
1662 for (PetscInt i = xs; i < xs + xm; ++i) {
1663
1664 // Access the count using standard 3D indexing
1665 PetscScalar count = count_arr_3d[k][j][i];
1666
1667 if (PetscRealPart(count) > 0.5) { // Use tolerance for float comparison
1668 if (data_dof == 1) {
1669 // Access scalar sum/avg using standard 3D indexing
1670 avg_arr_scalar[k][j][i] = sum_arr_scalar[k][j][i] / count;
1671 } else { // data_dof == 3
1672 // Access vector components using DOF indexing on the last dimension
1673 for (PetscInt c = 0; c < data_dof; ++c) {
1674 avg_arr_vector[k][j][i][c] = sum_arr_vector[k][j][i][c] / count;
1675 }
1676 }
1677 } else { // count is zero or negative
1678 // Set average to zero
1679 if (data_dof == 1) {
1680 avg_arr_scalar[k][j][i] = 0.0;
1681 } else { // data_dof == 3
1682 for (PetscInt c = 0; c < data_dof; ++c) {
1683 avg_arr_vector[k][j][i][c] = 0.0;
1684 }
1685 }
1686 } // end if count > 0.5
1687 } // end i loop
1688 } // end j loop
1689 } // end k loop
1690
1691 // --- Restore Arrays using appropriate functions ---
1692 ierr = DMDAVecRestoreArrayRead(countDM, countVec, &count_arr_3d); CHKERRQ(ierr);
1693 if (data_dof == 1) {
1694 ierr = DMDAVecRestoreArrayRead(dataDM, sumVec, &sum_arr_scalar); CHKERRQ(ierr);
1695 ierr = DMDAVecRestoreArray(dataDM, avgVec, &avg_arr_scalar); CHKERRQ(ierr);
1696 } else { // data_dof == 3
1697 ierr = DMDAVecRestoreArrayDOFRead(dataDM, sumVec, &sum_arr_vector); CHKERRQ(ierr);
1698 ierr = DMDAVecRestoreArrayDOF(dataDM, avgVec, &avg_arr_vector); CHKERRQ(ierr);
1699 }
1700
1701 // --- Assemble Final Average Vector ---
1702 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Assembling final average vector (DOF=%" PetscInt_FMT ").\n", data_dof);
1703 ierr = VecAssemblyBegin(avgVec); CHKERRQ(ierr);
1704 ierr = VecAssemblyEnd(avgVec); CHKERRQ(ierr);
1705
1706
1708
1709 PetscFunctionReturn(0);
1710}
#define LOCAL
Logging scope definitions for controlling message output.
Definition logging.h:45
#define GLOBAL
Scope for global logging across all processes.
Definition logging.h:46
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
Definition logging.h:214
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
Here is the caller graph for this function: