85 const DMDALocalInfo *info = NULL;
87 PetscInt owned_start[3];
88 PetscInt owned_end[3];
89 PetscInt global_size[3];
90 PetscBool periodic[3];
92 PetscFunctionBeginUser;
94 PetscCheck(user != NULL && plan != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
95 "Block context and plan output are required.");
97 "Only the fluid mask is implemented.");
99 PetscCheck(simCtx != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
100 "Block context must carry a simulation context.");
104 PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
105 "Field '%s' is component-staggered; its components live on different face "
106 "families and cannot share one pointwise target domain.",
110 owned_start[0] = info->xs; owned_end[0] = info->xs + info->xm; global_size[0] = info->mx;
111 owned_start[1] = info->ys; owned_end[1] = info->ys + info->ym; global_size[1] = info->my;
112 owned_start[2] = info->zs; owned_end[2] = info->zs + info->zm; global_size[2] = info->mz;
119 for (PetscInt dim = 0; dim < 3; ++dim) {
120 PetscBool node_like = PETSC_FALSE;
121 PetscInt layout_lo = 0;
122 PetscInt layout_hi = 0;
125 ResolveLayoutSpan(node_like, periodic[dim], global_size[dim], &layout_lo, &layout_hi);
130 plan->
start[dim] = PetscMax(owned_start[dim], layout_lo);
131 plan->
end[dim] = PetscMin(owned_end[dim], layout_hi);
135 PetscFunctionReturn(0);
208 Vec numerator, Vec denominator, Vec inclusion,
209 const PetscBool average_direction[3], MPI_Comm comm,
210 Vec ratio, PetscReal *reduced)
212 DM da = NULL, numerator_dm = NULL;
214 PetscInt retained_extent[3];
215 PetscInt buffer_size = 1;
216 PetscInt averaged_count = 0;
218 PetscReal ***den = NULL, ***inc = NULL, ***nvert = NULL;
219 PetscScalar ****num = NULL, ****out = NULL;
220 PetscReal *num_sum = NULL, *den_sum = NULL;
222 PetscFunctionBeginUser;
224 PetscCheck(user != NULL && plan != NULL && numerator != NULL && average_direction != NULL,
225 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
226 "Context, plan, numerator, and direction selector are required.");
232 PetscCall(VecGetDM(numerator, &numerator_dm));
233 PetscCheck(numerator_dm != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
234 "The numerator must carry the DMDA it was created from.");
235 PetscCall(DMDAGetInfo(numerator_dm, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dof,
236 NULL, NULL, NULL, NULL, NULL));
238 retained_extent[0] = average_direction[0] ? 1 : info.mx;
239 retained_extent[1] = average_direction[1] ? 1 : info.my;
240 retained_extent[2] = average_direction[2] ? 1 : info.mz;
241 for (PetscInt axis = 0; axis < 3; axis++) {
242 if (average_direction[axis]) averaged_count++;
243 buffer_size *= retained_extent[axis];
245 PetscCheck(reduced == NULL || averaged_count > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
246 "Reduced values exist only when at least one direction is averaged over.");
248 PetscCall(DMDAVecGetArrayDOFRead(numerator_dm, numerator, &num));
249 if (denominator) PetscCall(DMDAVecGetArrayRead(da, denominator, &den));
250 if (inclusion) PetscCall(DMDAVecGetArrayRead(da, inclusion, &inc));
251 if (ratio) PetscCall(DMDAVecGetArrayDOF(numerator_dm, ratio, &out));
252 PetscCall(DMDAVecGetArrayRead(da, user->
lNvert, &nvert));
254 if (averaged_count == 0) {
260 for (PetscInt k = plan->
start[2]; k < plan->
end[2]; k++)
261 for (PetscInt j = plan->
start[1]; j < plan->
end[1]; j++)
262 for (PetscInt i = plan->
start[0]; i < plan->
end[0]; i++) {
263 const PetscReal divisor = denominator ? den[k][j][i] : 1.0;
265 !(inc && inc[k][j][i] <= 0.0));
267 for (PetscInt c = 0; c < dof; c++) {
268 out[k][j][i][c] = (admitted && PetscAbsReal(divisor) > 0.0)
269 ? (num[k][j][i][c] / divisor) : 0.0;
274 PETSC_ERR_ARG_OUTOFRANGE,
275 "Averaging over %" PetscInt_FMT
" direction(s) would need a reduction buffer "
276 "of %" PetscInt_FMT
" entries, above the %d entry limit. Average over more "
279 PetscCall(PetscCalloc2(buffer_size * dof, &num_sum, buffer_size, &den_sum));
281 for (PetscInt k = plan->
start[2]; k < plan->
end[2]; k++)
282 for (PetscInt j = plan->
start[1]; j < plan->
end[1]; j++)
283 for (PetscInt i = plan->
start[0]; i < plan->
end[0]; i++) {
289 if (inc && inc[k][j][i] <= 0.0)
continue;
292 for (PetscInt c = 0; c < dof; c++) num_sum[slot * dof + c] += num[k][j][i][c];
293 den_sum[slot] += denominator ? den[k][j][i] : 1.0;
296 PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, num_sum, (PetscMPIInt)(buffer_size * dof),
297 MPIU_REAL, MPI_SUM, comm));
298 PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, den_sum, (PetscMPIInt)buffer_size,
299 MPIU_REAL, MPI_SUM, comm));
300 for (PetscInt slot = 0; slot < buffer_size; slot++) {
301 for (PetscInt c = 0; c < dof; c++) {
302 num_sum[slot * dof + c] = (PetscAbsReal(den_sum[slot]) > 0.0)
303 ? (num_sum[slot * dof + c] / den_sum[slot]) : 0.0;
308 for (PetscInt k = plan->
start[2]; k < plan->
end[2]; k++)
309 for (PetscInt j = plan->
start[1]; j < plan->
end[1]; j++)
310 for (PetscInt i = plan->
start[0]; i < plan->
end[0]; i++) {
311 const PetscInt slot =
SpatialAverageSlot(average_direction, retained_extent, i, j, k);
313 for (PetscInt c = 0; c < dof; c++) out[k][j][i][c] = num_sum[slot * dof + c];
316 if (reduced) PetscCall(PetscArraycpy(reduced, num_sum, buffer_size * dof));
318 PetscCall(PetscFree2(num_sum, den_sum));
321 PetscCall(DMDAVecRestoreArrayDOFRead(numerator_dm, numerator, &num));
322 if (denominator) PetscCall(DMDAVecRestoreArrayRead(da, denominator, &den));
323 if (inclusion) PetscCall(DMDAVecRestoreArrayRead(da, inclusion, &inc));
324 if (ratio) PetscCall(DMDAVecRestoreArrayDOF(numerator_dm, ratio, &out));
325 PetscCall(DMDAVecRestoreArrayRead(da, user->
lNvert, &nvert));
328 PetscFunctionReturn(0);