15#ifndef ERROR_MSG_BUFFER_SIZE
16#define ERROR_MSG_BUFFER_SIZE 256
30 PetscFunctionBeginUser;
36 PetscInt lo[3], hi[3];
38 PetscInt is = lo[0], js = lo[1], ks = lo[2], ie = hi[0], je = hi[1], ke = hi[2];
41 for (PetscInt k = ks; k < ke; k++) {
42 for (PetscInt j = js; j < je; j++) {
43 for (PetscInt i = is; i < ie; i++) {
45 centfield_arr[k][j][i].
x = 0.125 * (field_arr[k][j][i].
x + field_arr[k][j-1][i].
x +
46 field_arr[k-1][j][i].
x + field_arr[k-1][j-1][i].
x +
47 field_arr[k][j][i-1].
x + field_arr[k][j-1][i-1].
x +
48 field_arr[k-1][j][i-1].
x + field_arr[k-1][j-1][i-1].
x);
50 centfield_arr[k][j][i].
y = 0.125 * (field_arr[k][j][i].
y + field_arr[k][j-1][i].
y +
51 field_arr[k-1][j][i].
y + field_arr[k-1][j-1][i].
y +
52 field_arr[k][j][i-1].
y + field_arr[k][j-1][i-1].
y +
53 field_arr[k-1][j][i-1].
y + field_arr[k-1][j-1][i-1].
y);
55 centfield_arr[k][j][i].
z = 0.125 * (field_arr[k][j][i].
z + field_arr[k][j-1][i].
z +
56 field_arr[k-1][j][i].
z + field_arr[k-1][j-1][i].
z +
57 field_arr[k][j][i-1].
z + field_arr[k][j-1][i-1].
z +
58 field_arr[k-1][j][i-1].
z + field_arr[k-1][j-1][i-1].
z);
64 PetscFunctionReturn(0);
72 PetscReal ***field_arr,
73 PetscReal ***centfield_arr,
78 PetscFunctionBeginUser;
84 PetscInt lo[3], hi[3];
86 PetscInt is = lo[0], js = lo[1], ks = lo[2], ie = hi[0], je = hi[1], ke = hi[2];
89 for (PetscInt k = ks; k < ke; k++) {
90 for (PetscInt j = js; j < je; j++) {
91 for (PetscInt i = is; i < ie; i++) {
93 centfield_arr[k][j][i] = 0.125 * (field_arr[k][j][i] + field_arr[k][j-1][i] +
94 field_arr[k-1][j][i] + field_arr[k-1][j-1][i] +
95 field_arr[k][j][i-1] + field_arr[k][j-1][i-1] +
96 field_arr[k-1][j][i-1] + field_arr[k-1][j-1][i-1]);
102 PetscFunctionReturn(0);
112 Vec lCoords, TestCent;
113 Cmpnts ***coor_arr, ***test_cent_arr;
116 PetscFunctionBeginUser;
121 ierr = VecDuplicate(user->
Cent, &TestCent); CHKERRQ(ierr);
124 lCoords = user->
lCoor;
125 ierr = DMDAVecGetArrayRead(user->
fda, lCoords, &coor_arr); CHKERRQ(ierr);
126 ierr = DMDAVecGetArray(user->
fda, TestCent, &test_cent_arr); CHKERRQ(ierr);
134 ierr = DMDAVecRestoreArrayRead(user->
fda, lCoords, &coor_arr); CHKERRQ(ierr);
135 ierr = DMDAVecRestoreArray(user->
fda, TestCent, &test_cent_arr); CHKERRQ(ierr);
139 ierr = VecAssemblyBegin(TestCent); CHKERRQ(ierr);
140 ierr = VecAssemblyEnd(TestCent); CHKERRQ(ierr);
145 ierr = VecAXPY(TestCent, -1.0, user->
Cent); CHKERRQ(ierr);
149 ierr = VecNorm(TestCent, NORM_2, &diff_norm); CHKERRQ(ierr);
152 if (diff_norm < 1.0e-12) {
158 ierr = VecDestroy(&TestCent); CHKERRQ(ierr);
161 PetscFunctionReturn(0);
180 PetscInt xs_node = info.xs, xm_node = info.xm, xe_node = xs_node + xm_node;
181 PetscInt ys_node = info.ys, ym_node = info.ym, ye_node = ys_node + ym_node;
182 PetscInt zs_node = info.zs, zm_node = info.zm, ze_node = zs_node + zm_node;
184 PetscInt nCellsX = info.mx - 2;
185 PetscInt nCellsY = info.my - 2;
186 PetscInt nCellsZ = info.mz - 2;
190 PetscInt IM = info.mx - 1;
191 PetscInt JM = info.my - 1;
192 PetscInt KM = info.mz - 1;
195 "[Rank %d] Starting -- Node ownership k=%" PetscInt_FMT
"..%" PetscInt_FMT
", j=%" PetscInt_FMT
"..%" PetscInt_FMT
", i=%" PetscInt_FMT
"..%" PetscInt_FMT
"\n",
196 rank, zs_node, ze_node-1, ys_node, ye_node-1, xs_node, xe_node-1);
199 for (PetscInt k = zs_node; k < ze_node; k++) {
200 for (PetscInt j = ys_node; j < ye_node; j++) {
201 for (PetscInt i = xs_node; i < xe_node; i++) {
202 Cmpnts sum = {0.0, 0.0, 0.0};
223 if(i >= IM || j >= JM || k >= KM){
228 for (PetscInt dk_offset = -1; dk_offset <= 0; dk_offset++) {
229 for (PetscInt dj_offset = -1; dj_offset <= 0; dj_offset++) {
230 for (PetscInt di_offset = -1; di_offset <= 0; di_offset++) {
233 PetscInt global_cell_k = k + dk_offset;
234 PetscInt global_cell_j = j + dj_offset;
235 PetscInt global_cell_i = i + di_offset;
238 if (global_cell_i >= 0 && global_cell_i < nCellsX &&
239 global_cell_j >= 0 && global_cell_j < nCellsY &&
240 global_cell_k >= 0 && global_cell_k < nCellsZ)
242 Cmpnts cell_val = centfield_arr[global_cell_k + 1][global_cell_j + 1][global_cell_i + 1];
245 rank,global_cell_k,global_cell_j,global_cell_i,cell_val.
x, cell_val.
y, cell_val.
z);
256 PetscInt i_global_write = i;
257 PetscInt j_global_write = j;
258 PetscInt k_global_write = k;
262 corner_arr[k_global_write][j_global_write][i_global_write].
x = sum.
x / (PetscReal)count;
263 corner_arr[k_global_write][j_global_write][i_global_write].
y = sum.
y / (PetscReal)count;
264 corner_arr[k_global_write][j_global_write][i_global_write].
z = sum.
z / (PetscReal)count;
267 corner_arr[k_global_write][j_global_write][i_global_write] = (
Cmpnts){0.0, 0.0, 0.0};
301 PetscReal ***centfield_arr,
302 PetscReal ***corner_arr,
312 PetscInt xs_node = info.xs, xm_node = info.xm, xe_node = xs_node + xm_node;
313 PetscInt ys_node = info.ys, ym_node = info.ym, ye_node = ys_node + ym_node;
314 PetscInt zs_node = info.zs, zm_node = info.zm, ze_node = zs_node + zm_node;
316 PetscInt nCellsX = info.mx - 2;
317 PetscInt nCellsY = info.my - 2;
318 PetscInt nCellsZ = info.mz - 2;
322 PetscInt IM = info.mx - 1;
323 PetscInt JM = info.my - 1;
324 PetscInt KM = info.mz - 1;
327 "[Rank %d] Starting -- Node ownership k=%" PetscInt_FMT
"..%" PetscInt_FMT
", j=%" PetscInt_FMT
"..%" PetscInt_FMT
", i=%" PetscInt_FMT
"..%" PetscInt_FMT
"\n",
328 rank, zs_node, ze_node-1, ys_node, ye_node-1, xs_node, xe_node-1);
331 for (PetscInt k = zs_node; k < ze_node; k++) {
332 for (PetscInt j = ys_node; j < ye_node; j++) {
333 for (PetscInt i = xs_node; i < xe_node; i++) {
355 if(i >= IM || j >= JM || k >= KM){
360 for (PetscInt dk_offset = -1; dk_offset <= 0; dk_offset++) {
361 for (PetscInt dj_offset = -1; dj_offset <= 0; dj_offset++) {
362 for (PetscInt di_offset = -1; di_offset <= 0; di_offset++) {
365 PetscInt global_cell_k = k + dk_offset;
366 PetscInt global_cell_j = j + dj_offset;
367 PetscInt global_cell_i = i + di_offset;
370 if (global_cell_i >= 0 && global_cell_i < nCellsX &&
371 global_cell_j >= 0 && global_cell_j < nCellsY &&
372 global_cell_k >= 0 && global_cell_k < nCellsZ)
374 PetscReal cell_val = centfield_arr[global_cell_k + 1][global_cell_j + 1][global_cell_i + 1];
377 rank,global_cell_k,global_cell_j,global_cell_i,cell_val);
386 PetscInt i_global_write = i;
387 PetscInt j_global_write = j;
388 PetscInt k_global_write = k;
392 corner_arr[k_global_write][j_global_write][i_global_write] = sum / (PetscReal)count;
395 corner_arr[k_global_write][j_global_write][i_global_write] = 0.0;
429 a1 = PetscMax(0.0, PetscMin(1.0, a1));
430 a2 = PetscMax(0.0, PetscMin(1.0, a2));
431 a3 = PetscMax(0.0, PetscMin(1.0, a3));
433 const PetscReal oa1 = 1.0 - a1;
434 const PetscReal oa2 = 1.0 - a2;
435 const PetscReal oa3 = 1.0 - a3;
437 w[0] = oa1 * oa2 * oa3;
438 w[1] = a1 * oa2 * oa3;
439 w[2] = oa1 * a2 * oa3;
440 w[3] = a1 * a2 * oa3;
441 w[4] = oa1 * oa2 * a3;
442 w[5] = a1 * oa2 * a3;
443 w[6] = oa1 * a2 * a3;
454 const PetscReal oa1 = 1.0 - a1;
455 const PetscReal oa2 = 1.0 - a2;
456 const PetscReal oa3 = 1.0 - a3;
458 w[0] = oa1 * oa2 * oa3;
459 w[1] = a1 * oa2 * oa3;
460 w[2] = oa1 * a2 * oa3;
461 w[3] = a1 * a2 * oa3;
462 w[4] = oa1 * oa2 * a3;
463 w[5] = a1 * oa2 * a3;
464 w[6] = oa1 * a2 * a3;
474 PetscReal ***fieldScal,
486 PetscReal wcorner[8];
498 sum += wcorner[0] * fieldScal[k ][j ][i ];
500 sum += wcorner[1] * fieldScal[k ][j ][i1];
502 sum += wcorner[2] * fieldScal[k ][j1][i ];
504 sum += wcorner[3] * fieldScal[k ][j1][i1];
506 sum += wcorner[4] * fieldScal[k1][j ][i ];
508 sum += wcorner[5] * fieldScal[k1][j ][i1];
510 sum += wcorner[6] * fieldScal[k1][j1][i ];
512 sum += wcorner[7] * fieldScal[k1][j1][i1];
516 PetscFunctionReturn(0);
537 PetscReal wcorner[8];
543 PetscReal sumW = 0.0;
544 Cmpnts accum = {0.0, 0.0, 0.0};
549 const PetscInt cornerOffsets[8][3] = {
561 for (PetscInt c = 0; c < 8; c++) {
562 const PetscInt di = cornerOffsets[c][0];
563 const PetscInt dj = cornerOffsets[c][1];
564 const PetscInt dk = cornerOffsets[c][2];
565 PetscInt iC = i + di;
566 PetscInt jC = j + dj;
567 PetscInt kC = k + dk;
583 accum.
x += wcorner[c] * fieldVec[kC][jC][iC].
x;
584 accum.
y += wcorner[c] * fieldVec[kC][jC][iC].
y;
585 accum.
z += wcorner[c] * fieldVec[kC][jC][iC].
z;
590 if (sumW > 1.0e-14) {
591 vec->
x = accum.
x / sumW;
592 vec->
y = accum.
y / sumW;
593 vec->
z = accum.
z / sumW;
595 vec->
x = 0.0; vec->
y = 0.0; vec->
z = 0.0;
598 PetscFunctionReturn(0);
615 PetscInt iCell = particle->
cell[0];
616 PetscInt jCell = particle->
cell[1];
617 PetscInt kCell = particle->
cell[2];
630 if (blockSize == 1) {
632 PetscReal ***fieldScal = (PetscReal ***) fieldPtr;
642 ((PetscReal*)swarmOut)[p] = val;
644 else if (blockSize == 3) {
656 ((PetscReal*)swarmOut)[3*p + 0] = vec.
x;
657 ((PetscReal*)swarmOut)[3*p + 1] = vec.
y;
658 ((PetscReal*)swarmOut)[3*p + 2] = vec.
z;
668 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
669 "InterpolateEulerFieldToSwarmForParticle: only blockSize=1 or 3 supported, got %" PetscInt_FMT
".",
670 (PetscInt)blockSize);
674 PetscFunctionReturn(0);
695 PetscInt bi, PetscInt bj, PetscInt bk)
698#define PICURV_APPEND(...) do { if (*used < size) { int _n = snprintf(text + *used, size - *used, __VA_ARGS__); \
699 if (_n > 0) *used = PetscMin(size - 1, *used + (size_t)_n); } } while (0)
703 for (PetscInt dk = 1; dk <= 2; ++dk)
for (PetscInt dj = 1; dj <= 2; ++dj)
for (PetscInt di = 1; di <= 2; ++di) {
705 const PetscReal ***f = (
const PetscReal ***)tg->
field_ptr;
706 PICURV_APPEND(
" %.6e", (
double)f[bk + dk][bj + dj][bi + di]);
709 const Cmpnts v = f[bk + dk][bj + dj][bi + di];
738 const PetscBool periodic[3],
const PetscInt cell[3],
739 const PetscReal weights[3],
Cmpnts loc, PetscInt base[3],
740 PetscReal a[3], PetscReal w[8], PetscBool *unclamped,
743 const PetscInt n_cells[3] = {info->mx - 2, info->my - 2, info->mz - 2};
749 *unclamped = PETSC_FALSE;
750 for (PetscInt c = 0; c < 3; ++c) {
752 base[c] = cell[c] + ((weights[c] < 0.5) ? -1 : 0);
754 if (base[c] < 0) { base[c] = 0; *unclamped = PETSC_TRUE; }
755 if (base[c] + 1 >= n_cells[c]) { base[c] = n_cells[c] - 2; *unclamped = PETSC_TRUE; }
759 *outside = (PetscBool)(base[0] + 1 < info->gxs || base[0] + 1 >= info->gxs + info->gxm - 1 ||
760 base[1] + 1 < info->gys || base[1] + 1 >= info->gys + info->gym - 1 ||
761 base[2] + 1 < info->gzs || base[2] + 1 >= info->gzs + info->gzm - 1);
762 if (*outside)
return PETSC_SUCCESS;
768 if (ierr)
return ierr;
775 return PETSC_SUCCESS;
784 const PetscReal a[3],
const PetscReal w[8], PetscBool unclamped,
787 const PetscInt bi = base[0], bj = base[1], bk = base[2];
791 vec->
x = w[0]*fieldVec[bk+1][bj+1][bi+1].
x + w[1]*fieldVec[bk+1][bj+1][bi+2].
x
792 + w[2]*fieldVec[bk+1][bj+2][bi+1].
x + w[3]*fieldVec[bk+1][bj+2][bi+2].
x
793 + w[4]*fieldVec[bk+2][bj+1][bi+1].
x + w[5]*fieldVec[bk+2][bj+1][bi+2].
x
794 + w[6]*fieldVec[bk+2][bj+2][bi+1].
x + w[7]*fieldVec[bk+2][bj+2][bi+2].
x;
795 vec->
y = w[0]*fieldVec[bk+1][bj+1][bi+1].
y + w[1]*fieldVec[bk+1][bj+1][bi+2].
y
796 + w[2]*fieldVec[bk+1][bj+2][bi+1].
y + w[3]*fieldVec[bk+1][bj+2][bi+2].
y
797 + w[4]*fieldVec[bk+2][bj+1][bi+1].
y + w[5]*fieldVec[bk+2][bj+1][bi+2].
y
798 + w[6]*fieldVec[bk+2][bj+2][bi+1].
y + w[7]*fieldVec[bk+2][bj+2][bi+2].
y;
799 vec->
z = w[0]*fieldVec[bk+1][bj+1][bi+1].
z + w[1]*fieldVec[bk+1][bj+1][bi+2].
z
800 + w[2]*fieldVec[bk+1][bj+2][bi+1].
z + w[3]*fieldVec[bk+1][bj+2][bi+2].
z
801 + w[4]*fieldVec[bk+2][bj+1][bi+1].
z + w[5]*fieldVec[bk+2][bj+1][bi+2].
z
802 + w[6]*fieldVec[bk+2][bj+2][bi+1].
z + w[7]*fieldVec[bk+2][bj+2][bi+2].
z;
808 return PETSC_SUCCESS;
816 const PetscReal w[8], PetscBool unclamped, PetscReal *val)
818 const PetscInt bi = base[0], bj = base[1], bk = base[2];
822 *val = w[0] * fieldScal[bk+1][bj+1][bi+1] + w[1] * fieldScal[bk+1][bj+1][bi+2]
823 + w[2] * fieldScal[bk+1][bj+2][bi+1] + w[3] * fieldScal[bk+1][bj+2][bi+2]
824 + w[4] * fieldScal[bk+2][bj+1][bi+1] + w[5] * fieldScal[bk+2][bj+1][bi+2]
825 + w[6] * fieldScal[bk+2][bj+2][bi+1] + w[7] * fieldScal[bk+2][bj+2][bi+2];
826 return PETSC_SUCCESS;
838 const PetscInt i = cell[0] + 1, j = cell[1] + 1, k = cell[2] + 1;
839 return (PetscBool)!(i - 1 < info->gxs || i >= info->gxs + info->gxm ||
840 j - 1 < info->gys || j >= info->gys + info->gym ||
841 k - 1 < info->gzs || k >= info->gzs + info->gzm);
847 return (PetscBool)!(cell[0] < info->gxs || cell[0] >= info->gxs + info->gxm - 1 ||
848 cell[1] < info->gys || cell[1] >= info->gys + info->gym - 1 ||
849 cell[2] < info->gzs || cell[2] >= info->gzs + info->gzm - 1);
872 DM swarm = user->
swarm;
876 PetscInt *cellIDs = NULL;
877 PetscReal *weights = NULL;
878 PetscInt64 *pids = NULL;
879 PetscReal *pos = NULL;
880 PetscInt *status = NULL;
890 PetscBool periodic[3];
894 for (PetscInt t = 0; t < ntargets; ++t) {
896 ierr = VecGetBlockSize(tg->
local_vec, &tg->
bs); CHKERRQ(ierr);
897 if (tg->
bs != 1 && tg->
bs != 3) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
"BlockSize must be 1 or 3.");
900 ierr = DMSwarmGetField(swarm, tg->
swarm_name, NULL, NULL, (
void **)&tg->
swarm_out); CHKERRQ(ierr);
904 ierr = DMSwarmGetLocalSize(swarm, &nLocal); CHKERRQ(ierr);
912 PetscInt n_outside = 0;
913 for (PetscInt p = 0; p < nLocal; p++) {
916 ierr =
UnpackSwarmFields(p, pids, weights, pos, cellIDs, NULL, status, NULL, NULL, NULL, &particle); CHKERRQ(ierr);
922 PetscReal a_new[3], w[8] = {0};
923 PetscBool needs_unclamped, outside;
925 base, a_new, w, &needs_unclamped, &outside); CHKERRQ(ierr);
926 const PetscInt bi = base[0], bj = base[1], bk = base[2];
927 const PetscReal a1_new = a_new[0], a2_new = a_new[1], a3_new = a_new[2];
932 "dual cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") outside the ghosted region: %" PetscInt_FMT
" field(s) zeroed\n",
933 bi, bj, bk, ntargets);
934 for (PetscInt t = 0; t < ntargets; ++t) {
935 for (PetscInt c = 0; c < targets[t].
bs; ++c) targets[t].swarm_out[targets[t].bs * p + c] = 0.0;
941 for (PetscInt t = 0; t < ntargets; ++t) {
942 PetscReal *swarmOut = targets[t].
swarm_out;
943 if (targets[t].bs == 1) {
946 &val); CHKERRQ(ierr);
951 needs_unclamped, &vec); CHKERRQ(ierr);
952 swarmOut[3*p + 0] = vec.
x;
953 swarmOut[3*p + 1] = vec.
y;
954 swarmOut[3*p + 2] = vec.
z;
963 "host cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") dual base (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") weights (%.6f, %.6f, %.6f)%s%s\n",
964 particle.
cell[0], particle.
cell[1], particle.
cell[2], bi, bj, bk, a1_new, a2_new, a3_new,
965 needs_unclamped ?
" unclamped extrapolation" :
"", text);
970 "[Rank %d] %" PetscInt_FMT
" particle(s) had their dual cell outside the ghosted region; their interpolated fields were zeroed.\n",
975 for (PetscInt t = 0; t < ntargets; ++t) {
978 ierr = DMSwarmRestoreField(swarm, tg->
swarm_name, NULL, NULL, (
void **)&tg->
swarm_out); CHKERRQ(ierr);
989 PetscFunctionReturn(0);
1005 PetscErrorCode ierr;
1006 DM swarm = user->
swarm;
1009 Cmpnts ***lucont = NULL, ***lcsi = NULL, ***leta = NULL, ***lzet = NULL;
1010 PetscInt *cellIDs = NULL;
1011 PetscReal *weights = NULL, *velocity = NULL;
1012 PetscInt64 *pids = NULL;
1021 ierr = DMDAVecGetArrayRead(user->
fda, user->
lUcont, (
void *)&lucont); CHKERRQ(ierr);
1022 ierr = DMDAVecGetArrayRead(user->
fda, user->
lCsi, (
void *)&lcsi); CHKERRQ(ierr);
1023 ierr = DMDAVecGetArrayRead(user->
fda, user->
lEta, (
void *)&leta); CHKERRQ(ierr);
1024 ierr = DMDAVecGetArrayRead(user->
fda, user->
lZet, (
void *)&lzet); CHKERRQ(ierr);
1025 ierr = DMSwarmGetLocalSize(swarm, &nLocal); CHKERRQ(ierr);
1029 ierr = DMSwarmGetField(swarm, swarmOutFieldName, NULL, NULL, (
void**)&velocity); CHKERRQ(ierr);
1031 PetscInt n_outside = 0;
1032 for (PetscInt p = 0; p < nLocal; p++) {
1034 const PetscInt i = cellIDs[3*p + 0] + 1, j = cellIDs[3*p + 1] + 1, k = cellIDs[3*p + 2] + 1;
1035 const Cmpnts w = {weights[3*p + 0], weights[3*p + 1], weights[3*p + 2]};
1036 Cmpnts u = {0.0, 0.0, 0.0};
1041 "host cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") faces outside the ghosted region: '%s' zeroed\n",
1042 i - 1, j - 1, k - 1, swarmOutFieldName);
1043 velocity[3*p + 0] = velocity[3*p + 1] = velocity[3*p + 2] = 0.0;
1047 velocity[3*p + 0] = u.
x;
1048 velocity[3*p + 1] = u.
y;
1049 velocity[3*p + 2] = u.
z;
1051 "face-flux '%s' in host cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") weights (%.6f, %.6f, %.6f) from fluxes x %.6e %.6e y %.6e %.6e z %.6e %.6e = (%.6e, %.6e, %.6e)\n",
1052 swarmOutFieldName, i - 1, j - 1, k - 1, w.
x, w.
y, w.
z,
1053 lucont[k][j][i-1].
x, lucont[k][j][i].
x, lucont[k][j-1][i].
y, lucont[k][j][i].
y,
1054 lucont[k-1][j][i].
z, lucont[k][j][i].
z, u.
x, u.
y, u.
z);
1056 if (n_outside > 0) {
1058 "[Rank %d] %" PetscInt_FMT
" particle(s) had their host-cell faces outside the ghosted region; '%s' was zeroed for them.\n",
1059 rank, n_outside, swarmOutFieldName);
1062 ierr = DMSwarmRestoreField(swarm, swarmOutFieldName, NULL, NULL, (
void**)&velocity); CHKERRQ(ierr);
1066 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
lZet, (
void *)&lzet); CHKERRQ(ierr);
1067 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
lEta, (
void *)&leta); CHKERRQ(ierr);
1068 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
lCsi, (
void *)&lcsi); CHKERRQ(ierr);
1069 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
lUcont, (
void *)&lucont); CHKERRQ(ierr);
1073 PetscFunctionReturn(0);
1089 DM dm = (bs == 3) ? user->
fda : user->
da;
1090 void *cell = NULL, *corner = NULL;
1095 PetscCall(
FieldGetView(user, corner_field_id, corner_view));
1096 PetscCall(VecSet(corner_view->
global_vec, 0.0));
1097 PetscCall(VecSet(corner_view->
local_vec, 0.0));
1098 PetscCall(DMDAVecGetArrayRead(dm, cell_local, &cell));
1099 PetscCall(DMDAVecGetArray(dm, corner_view->
global_vec, &corner));
1101 PetscCall(DMDAVecRestoreArray(dm, corner_view->
global_vec, &corner));
1102 PetscCall(DMDAVecRestoreArrayRead(dm, cell_local, &cell));
1105 PetscFunctionReturn(0);
1116 Vec fieldLocal_cellCentered,
1117 const char *fieldName,
1118 const char *swarmOutFieldName)
1120 PetscErrorCode ierr;
1121 DM swarm = user->
swarm;
1126 void *cornerPtr_read_with_ghosts;
1129 PetscInt *cellIDs = NULL;
1130 PetscReal *weights = NULL;
1131 PetscInt64 *pids = NULL;
1132 void *swarmOut = NULL;
1133 PetscReal *pos = NULL;
1134 PetscInt *status = NULL;
1141 ierr = VecGetBlockSize(fieldLocal_cellCentered, &bs); CHKERRQ(ierr);
1142 if (bs != 1 && bs != 3) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
"BlockSize must be 1 or 3.");
1146 DM dm_corner = (bs == 3) ? user->
fda : user->
da;
1152 Vec cornerLocal = corner_view.
local_vec;
1161 ierr = DMDAVecGetArrayRead(dm_corner, cornerLocal, &cornerPtr_read_with_ghosts); CHKERRQ(ierr);
1164 ierr = DMSwarmGetLocalSize(swarm, &nLocal); CHKERRQ(ierr);
1170 ierr = DMSwarmGetField(swarm, swarmOutFieldName, NULL, NULL, &swarmOut); CHKERRQ(ierr);
1172 LOG_ALLOW(
LOCAL,
LOG_TRACE,
" Rank %d holds data upto & including %" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
".\n",rank,info.gxs + info.gxm,info.gys+info.gym,info.gzs+info.gzm);
1175 PetscInt n_outside = 0;
1176 for (PetscInt p = 0; p < nLocal; p++) {
1180 ierr =
UnpackSwarmFields(p,pids,weights,pos,cellIDs,NULL,status,NULL,NULL,NULL,&particle); CHKERRQ(ierr);
1189 "cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") corner stencil outside the ghosted region: '%s' zeroed\n",
1190 particle.
cell[0], particle.
cell[1], particle.
cell[2], fieldName);
1192 ((PetscReal*)swarmOut)[3*p + 0] = 0.0;
1193 ((PetscReal*)swarmOut)[3*p + 1] = 0.0;
1194 ((PetscReal*)swarmOut)[3*p + 2] = 0.0;
1196 ((PetscReal*)swarmOut)[p] = 0.0;
1202 cornerPtr_read_with_ghosts,
1207 const PetscReal *out = (
const PetscReal *)swarmOut + bs * p;
1209 "corner-averaged '%s' in cell (%" PetscInt_FMT
",%" PetscInt_FMT
",%" PetscInt_FMT
") weights (%.6f, %.6f, %.6f) = %.6e %.6e %.6e\n",
1210 fieldName, particle.
cell[0], particle.
cell[1], particle.
cell[2],
1212 (
double)out[0], (
double)(bs == 3 ? out[1] : 0.0), (
double)(bs == 3 ? out[2] : 0.0));
1215 if (n_outside > 0) {
1217 "[Rank %d] %" PetscInt_FMT
" particle(s) had their corner stencil outside the ghosted region; '%s' was zeroed for them.\n",
1218 rank, n_outside, fieldName);
1222 ierr = DMDAVecRestoreArrayRead(dm_corner, cornerLocal, &cornerPtr_read_with_ghosts); CHKERRQ(ierr);
1225 ierr = DMSwarmRestoreField(swarm, swarmOutFieldName, NULL, NULL, &swarmOut); CHKERRQ(ierr);
1233 PetscFunctionReturn(0);
1256 PetscCall(PetscNew(&v));
1261 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lUcont, (
void *)&v->
field));
1262 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lCsi, (
void *)&v->
csi));
1263 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lEta, (
void *)&v->
eta));
1264 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lZet, (
void *)&v->
zet));
1272 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lUcat, (
void *)&v->
field));
1277 PetscFunctionReturn(0);
1287 const DMDALocalInfo *info = &interp->
user->
info;
1292 u->
x = u->
y = u->
z = 0.0;
1295 const Cmpnts w = {weights[0], weights[1], weights[2]};
1298 cell[0] + 1, cell[1] + 1, cell[2] + 1, w, u));
1302 weights[0], weights[1], weights[2], u));
1305 PetscReal a[3], w[8] = {0};
1306 PetscBool unclamped, outside;
1308 &unclamped, &outside));
1312 PetscFunctionReturn(0);
1327 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lZet, (
void *)&v->
zet));
1328 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lEta, (
void *)&v->
eta));
1329 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lCsi, (
void *)&v->
csi));
1330 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lUcont, (
void *)&v->
field));
1334 PetscCall(DMDAVecRestoreArrayRead(user->
fda, corner.
local_vec, (
void *)&v->
field));
1337 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lUcat, (
void *)&v->
field));
1339 PetscCall(PetscFree(*interp));
1341 PetscFunctionReturn(0);
1354 PetscCall(
FieldGetView(user, source_field_id, source_view));
1357 PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
1358 "Eulerian-to-particle interpolation requires a cell-centered source; '%s' uses layout %s.",
1361 PetscCheck((*target_descriptor)->data_type == PETSC_REAL,
1362 PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
1363 "Eulerian interpolation destination '%s' must use PETSC_REAL storage.",
1364 (*target_descriptor)->canonical_name);
1365 PetscCheck(source_view->
descriptor->
dof == (*target_descriptor)->components,
1366 PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
1367 "Eulerian source '%s' has %" PetscInt_FMT
" components but particle destination '%s' has %" PetscInt_FMT
".",
1369 (*target_descriptor)->canonical_name, (*target_descriptor)->components);
1371 PetscFunctionReturn(0);
1385 PetscInt ncenter = 0;
1389 PetscCheck(n <= (PetscInt)(
sizeof(targets) /
sizeof(targets[0])), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
1390 "At most %d fields interpolate in one pass; %d requested.", (
int)(
sizeof(targets) /
sizeof(targets[0])), (
int)n);
1391 for (PetscInt f = 0; f < n; ++f) {
1395 PetscCall(
ResolveInterpolationPair(user, source_field_ids[f], target_field_ids[f], &source_view, &target_descriptor));
1412 PetscFunctionReturn(0);
1422 PetscErrorCode ierr;
1434 " Interpolation of ucat to velocity begins on rank %d.\n",rank);
1438 ierr = MPI_Barrier(PETSC_COMM_WORLD); CHKERRQ(ierr);
1440 "[rank %d]Completed Interpolateting all fields to the swarm.\n",rank);
1444 PetscFunctionReturn(0);
1495 DM gridSumDM, Vec localAccumulatorVec)
1497 PetscErrorCode ierr;
1500 const PetscReal *particle_arr = NULL;
1501 const PetscInt *cell_id_arr = NULL;
1504 PetscScalar ***arr_1d = NULL;
1505 PetscScalar ****arr_3d = NULL;
1508 PetscInt gxs, gys, gzs, gxm, gym, gzm;
1511 const char *particleFieldName = NULL;
1513 PetscFunctionBeginUser;
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.",
1523 if (!swarm || !gridSumDM || !localAccumulatorVec)
1524 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Null input in AccumulateParticleField.");
1527 ierr = DMDAGetInfo(gridSumDM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL); CHKERRQ(ierr);
1529 ierr = DMDAGetGhostCorners(gridSumDM, &gxs, &gys, &gzs, &gxm, &gym, &gzm); CHKERRQ(ierr);
1534 PetscInt expectedLocalSize = gxm * gym * gzm * dof;
1535 ierr = VecGetSize(localAccumulatorVec, &vecSize); CHKERRQ(ierr);
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);
1547 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
1549 ierr = DMSwarmGetField(swarm, particleFieldName, NULL, NULL, (
void **)&particle_arr); CHKERRQ(ierr);
1555 ierr = DMDAVecGetArray(gridSumDM, localAccumulatorVec, &arr_1d); CHKERRQ(ierr);
1556 }
else if (dof == 3) {
1557 ierr = DMDAVecGetArrayDOF(gridSumDM, localAccumulatorVec, &arr_3d); CHKERRQ(ierr);
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);
1565 for (p = 0; p < nlocal; ++p) {
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];
1572 PetscInt i = i_geom + 1;
1573 PetscInt j = j_geom + 1;
1574 PetscInt k = k_geom + 1;
1578 if (i >= gxs && i < gxs + gxm &&
1579 j >= gys && j < gys + gym &&
1580 k >= gzs && k < gzs + gzm)
1583 arr_1d[k][j][i] += particle_arr[p];
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];
1597 ierr = DMDAVecRestoreArray(gridSumDM, localAccumulatorVec, &arr_1d); CHKERRQ(ierr);
1599 ierr = DMDAVecRestoreArrayDOF(gridSumDM, localAccumulatorVec, &arr_3d); CHKERRQ(ierr);
1602 ierr = DMSwarmRestoreField(swarm, particleFieldName, NULL, NULL, (
void **)&particle_arr); CHKERRQ(ierr);
1606 PetscFunctionReturn(0);
1612 DM dataDM, Vec sumVec, Vec avgVec)
1614 PetscErrorCode ierr;
1621 PetscScalar ***count_arr_3d = NULL;
1622 PetscScalar ***sum_arr_scalar = NULL;
1623 PetscScalar ***avg_arr_scalar = NULL;
1624 PetscScalar ****sum_arr_vector = NULL;
1625 PetscScalar ****avg_arr_vector = NULL;
1628 PetscFunctionBeginUser;
1632 rank = PetscGlobalRank;
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); }
1641 ierr = DMDAVecGetArrayRead(countDM, countVec, &count_arr_3d); CHKERRQ(ierr);
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);
1647 ierr = DMDAVecGetArrayDOFRead(dataDM, sumVec, &sum_arr_vector); CHKERRQ(ierr);
1648 ierr = DMDAVecGetArrayDOF(dataDM, avgVec, &avg_arr_vector); CHKERRQ(ierr);
1652 PetscInt xs, ys, zs, xm, ym, zm;
1653 ierr = DMDAGetCorners(countDM, &xs, &ys, &zs, &xm, &ym, &zm); CHKERRQ(ierr);
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);
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) {
1665 PetscScalar count = count_arr_3d[k][j][i];
1667 if (PetscRealPart(count) > 0.5) {
1668 if (data_dof == 1) {
1670 avg_arr_scalar[k][j][i] = sum_arr_scalar[k][j][i] / count;
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;
1679 if (data_dof == 1) {
1680 avg_arr_scalar[k][j][i] = 0.0;
1682 for (PetscInt c = 0; c < data_dof; ++c) {
1683 avg_arr_vector[k][j][i][c] = 0.0;
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);
1697 ierr = DMDAVecRestoreArrayDOFRead(dataDM, sumVec, &sum_arr_vector); CHKERRQ(ierr);
1698 ierr = DMDAVecRestoreArrayDOF(dataDM, avgVec, &avg_arr_vector); CHKERRQ(ierr);
1703 ierr = VecAssemblyBegin(avgVec); CHKERRQ(ierr);
1704 ierr = VecAssemblyEnd(avgVec); CHKERRQ(ierr);
1709 PetscFunctionReturn(0);
1726 Vec countVec, PetscBool recount, Vec meanVec)
1728 PetscInt data_dof = 0;
1729 Vec local_sum = NULL, sum = NULL;
1732 PetscFunctionBeginUser;
1734 PetscCheck(user && user->
swarm && dataDM && countVec && meanVec, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
1735 "Context, swarm, data DM, count and mean vectors are required.");
1737 PetscCall(DMDAGetInfo(dataDM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &data_dof, NULL, NULL, NULL, NULL, NULL));
1738 PetscCheck(data_dof == descriptor->
components, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
1739 "Field '%s' has %" PetscInt_FMT
" components but the data DM has DOF %" PetscInt_FMT
".",
1743 PetscCall(DMGetLocalVector(dataDM, &local_sum));
1744 PetscCall(VecSet(local_sum, 0.0));
1746 PetscCall(DMGetGlobalVector(dataDM, &sum));
1747 PetscCall(VecSet(sum, 0.0));
1748 PetscCall(DMLocalToGlobalBegin(dataDM, local_sum, ADD_VALUES, sum));
1749 PetscCall(DMLocalToGlobalEnd(dataDM, local_sum, ADD_VALUES, sum));
1750 PetscCall(DMRestoreLocalVector(dataDM, &local_sum));
1754 PetscCall(DMRestoreGlobalVector(dataDM, &sum));
1756 PetscFunctionReturn(0);
1763 Vec eulerFieldAverageVec)
1765 PetscErrorCode ierr;
1767 PetscInt expected_dof = 0;
1772 const char *particleFieldName = NULL;
1774 PetscFunctionBeginUser;
1779 if (!user) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"UserCtx pointer is NULL.");
1780 if (!user->
swarm) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"UserCtx->swarm is NULL.");
1781 if (!user->
ParticleCount) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"UserCtx->ParticleCount is NULL.");
1782 if (!eulerFieldAverageVec) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Output eulerFieldAverageVec is NULL.");
1785 PETSC_COMM_SELF, PETSC_ERR_SUP,
1786 "Particle field '%s' has no registered Eulerian scatter target.",
1789 PETSC_COMM_SELF, PETSC_ERR_PLIB,
1790 "Particle field '%s' advertises scatter support without an Eulerian target.",
1793 expected_dof = particle_descriptor->
components;
1795 PetscCheck(eulerian_descriptor->
dof == expected_dof, PETSC_COMM_SELF, PETSC_ERR_PLIB,
1796 "Particle field '%s' has %" PetscInt_FMT
" components but Eulerian target '%s' has %" PetscInt_FMT
".",
1797 particleFieldName, expected_dof, eulerian_descriptor->
canonical_name, eulerian_descriptor->
dof);
1799 targetDM = target_view.
dm;
1805 ierr = VecGetDM(eulerFieldAverageVec, &vec_dm); CHKERRQ(ierr);
1807 PetscSNPrintf(msg,
sizeof(msg),
"Provided eulerFieldAverageVec for field '%s' does not have an associated DM.", particleFieldName);
1808 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
"%s", msg);
1811 ierr = VecGetBlockSize(eulerFieldAverageVec, &vec_dof); CHKERRQ(ierr);
1813 if (vec_dm != targetDM) {
1814 const char *target_dm_name =
"targetDM", *vec_dm_name =
"vec_dm";
1816 PetscObjectGetName((PetscObject)targetDM, &target_dm_name);
1817 PetscObjectGetName((PetscObject)vec_dm, &vec_dm_name);
1818 PetscSNPrintf(msg,
sizeof(msg),
"Provided eulerFieldAverageVec associated with DM '%s', but field '%s' requires scatter to DM '%s'.", vec_dm_name, particleFieldName, target_dm_name);
1819 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
"%s", msg);
1822 if (vec_dof != expected_dof) {
1823 PetscSNPrintf(msg,
sizeof(msg),
"Field '%s' requires DOF %" PetscInt_FMT
", but provided eulerFieldAverageVec has DOF %" PetscInt_FMT
".", particleFieldName, expected_dof, vec_dof);
1824 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
"%s", msg);
1831 eulerFieldAverageVec); CHKERRQ(ierr);
1837 PetscFunctionReturn(0);
1843 PetscErrorCode ierr;
1844 PetscFunctionBeginUser;
1851 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
"UserCtx->ParticleCount is NULL. Compute counts before calling ScatterAllParticleFieldsToEulerFields.");
1863 PetscReal Avg_Psi = 0.0, Avg_swarm_Psi = 0.0;
1866 ierr = VecMean(user->
Psi,&Avg_Psi); CHKERRQ(ierr);
1869 ierr = VecMean(swarm_Psi,&Avg_swarm_Psi); CHKERRQ(ierr);
1886 ierr = VecMean(user->
Psi,&Avg_Psi); CHKERRQ(ierr);
1899 PetscFunctionReturn(0);
1911 PetscReal ***corner_arr,
1912 PetscReal ***faceX_arr,
1913 PetscReal ***faceY_arr,
1914 PetscReal ***faceZ_arr,
1917 PetscErrorCode ierr;
1920 PetscFunctionBeginUser;
1927 PetscInt xs, xm, ys, ym, zs, zm;
1933 PetscInt xe = xs + xm;
1934 PetscInt ye = ys + ym;
1935 PetscInt ze = zs + zm;
1938 for (PetscInt k = zs; k < ze; ++k) {
1939 PetscInt k_loc = k - zs;
1940 for (PetscInt j = ys; j < ye; ++j) {
1941 PetscInt j_loc = j - ys;
1942 for (PetscInt i = xs; i <= xe; ++i) {
1943 PetscInt i_loc = i - xs;
1945 PetscReal sum = corner_arr[k ][j ][i]
1946 + corner_arr[k+1][j ][i]
1947 + corner_arr[k ][j+1][i]
1948 + corner_arr[k+1][j+1][i];
1949 faceX_arr[k_loc][j_loc][i_loc] = sum * 0.25;
1955 for (PetscInt k = zs; k < ze; ++k) {
1956 PetscInt k_loc = k - zs;
1957 for (PetscInt j = ys; j <= ye; ++j) {
1958 PetscInt j_loc = j - ys;
1959 for (PetscInt i = xs; i < xe; ++i) {
1960 PetscInt i_loc = i - xs;
1962 PetscReal sum = corner_arr[k ][j][i ]
1963 + corner_arr[k+1][j][i ]
1964 + corner_arr[k ][j][i+1]
1965 + corner_arr[k+1][j][i+1];
1966 faceY_arr[k_loc][j_loc][i_loc] = sum * 0.25;
1972 for (PetscInt k = zs; k <= ze; ++k) {
1973 PetscInt k_loc = k - zs;
1974 for (PetscInt j = ys; j < ye; ++j) {
1975 PetscInt j_loc = j - ys;
1976 for (PetscInt i = xs; i < xe; ++i) {
1977 PetscInt i_loc = i - xs;
1979 PetscReal sum = corner_arr[k][j ][i ]
1980 + corner_arr[k][j ][i+1]
1981 + corner_arr[k][j+1][i ]
1982 + corner_arr[k][j+1][i+1];
1983 faceZ_arr[k_loc][j_loc][i_loc] = sum * 0.25;
1990 PetscFunctionReturn(0);
2005 PetscErrorCode ierr;
2009 PetscFunctionBeginUser;
2015 "Rank %d starting InterpolateFieldFromCornerToFaceCenter_Vector.\n", rank);
2019 PetscInt xs, xm, ys, ym, zs, zm;
2024 PetscInt xe = xs + xm;
2025 PetscInt ye = ys + ym;
2026 PetscInt ze = zs + zm;
2029 for (PetscInt k = zs; k < ze; ++k) {
2030 PetscInt k_loc = k - zs;
2031 for (PetscInt j = ys; j < ye; ++j) {
2032 PetscInt j_loc = j - ys;
2033 for (PetscInt i = xs; i <= xe; ++i) {
2034 PetscInt i_loc = i - xs;
2036 sum.
x = corner_arr[k ][j ][i].
x + corner_arr[k+1][j ][i].
x
2037 + corner_arr[k ][j+1][i].
x + corner_arr[k+1][j+1][i].
x;
2038 sum.
y = corner_arr[k ][j ][i].
y + corner_arr[k+1][j ][i].
y
2039 + corner_arr[k ][j+1][i].
y + corner_arr[k+1][j+1][i].
y;
2040 sum.
z = corner_arr[k ][j ][i].
z + corner_arr[k+1][j ][i].
z
2041 + corner_arr[k ][j+1][i].
z + corner_arr[k+1][j+1][i].
z;
2042 faceX_arr[k_loc][j_loc][i_loc].
x = sum.
x * 0.25;
2043 faceX_arr[k_loc][j_loc][i_loc].
y = sum.
y * 0.25;
2044 faceX_arr[k_loc][j_loc][i_loc].
z = sum.
z * 0.25;
2050 "Rank %d x-face Interpolation complete.\n", rank);
2053 for (PetscInt k = zs; k < ze; ++k) {
2054 PetscInt k_loc = k - zs;
2055 for (PetscInt j = ys; j <= ye; ++j) {
2056 PetscInt j_loc = j - ys;
2057 for (PetscInt i = xs; i < xe; ++i) {
2058 PetscInt i_loc = i - xs;
2060 sum.
x = corner_arr[k ][j][i ].
x + corner_arr[k+1][j][i ].
x
2061 + corner_arr[k ][j][i+1].
x + corner_arr[k+1][j][i+1].
x;
2062 sum.
y = corner_arr[k ][j][i ].
y + corner_arr[k+1][j][i ].
y
2063 + corner_arr[k ][j][i+1].
y + corner_arr[k+1][j][i+1].
y;
2064 sum.
z = corner_arr[k ][j][i ].
z + corner_arr[k+1][j][i ].
z
2065 + corner_arr[k ][j][i+1].
z + corner_arr[k+1][j][i+1].
z;
2066 faceY_arr[k_loc][j_loc][i_loc].
x = sum.
x * 0.25;
2067 faceY_arr[k_loc][j_loc][i_loc].
y = sum.
y * 0.25;
2068 faceY_arr[k_loc][j_loc][i_loc].
z = sum.
z * 0.25;
2074 "Rank %d y-face Interpolation complete.\n", rank);
2077 for (PetscInt k = zs; k <= ze; ++k) {
2078 PetscInt k_loc = k - zs;
2079 for (PetscInt j = ys; j < ye; ++j) {
2080 PetscInt j_loc = j - ys;
2081 for (PetscInt i = xs; i < xe; ++i) {
2082 PetscInt i_loc = i - xs;
2084 sum.
x = corner_arr[k][j ][i ].
x + corner_arr[k][j ][i+1].
x
2085 + corner_arr[k][j+1][i ].
x + corner_arr[k][j+1][i+1].
x;
2086 sum.
y = corner_arr[k][j ][i ].
y + corner_arr[k][j ][i+1].
y
2087 + corner_arr[k][j+1][i ].
y + corner_arr[k][j+1][i+1].
y;
2088 sum.
z = corner_arr[k][j ][i ].
z + corner_arr[k][j ][i+1].
z
2089 + corner_arr[k][j+1][i ].
z + corner_arr[k][j+1][i+1].
z;
2090 faceZ_arr[k_loc][j_loc][i_loc].
x = sum.
x * 0.25;
2091 faceZ_arr[k_loc][j_loc][i_loc].
y = sum.
y * 0.25;
2092 faceZ_arr[k_loc][j_loc][i_loc].
z = sum.
z * 0.25;
2098 "Rank %d z-face Interpolation complete.\n", rank);
2101 PetscFunctionReturn(0);
PetscErrorCode UpdateDummyCells(UserCtx *user, FieldId field_id)
Fills the dummy cells on the non-periodic faces of one cell-centred field.
PetscErrorCode UpdateCornerNodes(UserCtx *user, FieldId field_id)
Fills the edge and corner dummy cells of one cell-centred field.
PetscErrorCode MetricRestoreFacePlaneArrays(UserCtx *user, FacePlaneArrays *arrays)
Closes arrays opened by MetricGetFacePlaneArrays().
void MetricGatherCellFacePlanes(const FacePlaneArrays *arrays, const DMDALocalInfo *info, PetscInt i, PetscInt j, PetscInt k, CellFacePlanes *planes)
Assembles one cell's six outward face planes from the shared per-face planes.
PetscErrorCode MetricGetFacePlaneArrays(UserCtx *user, PetscBool dual, FacePlaneArrays *arrays)
Opens a family's face-plane arrays for reading; MetricComputeFacePlanes() must have run.
Read-only flat arrays of one family of face planes (grid cells or dual cells).
PetscErrorCode CalculateParticleCountPerCell(UserCtx *user)
Counts particles in each cell of the DMDA 'da' and stores the result in user->ParticleCount.
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)
Initializes a Particle struct with data from DMSwarm fields.
const FieldDescriptor * descriptor
PetscErrorCode FieldGetView(UserCtx *user, FieldId field_id, FieldView *view)
Resolve the existing DM and global/local vectors for one field.
@ FIELD_LAYOUT_CELL_CENTERED
const char * canonical_name
const char * FieldLayoutName(FieldLayout layout)
Return a stable printable label for a field layout.
PetscErrorCode FieldGetDescriptor(FieldId field_id, const FieldDescriptor **descriptor)
Return immutable metadata for a valid field identifier.
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_CELL_SCALAR_AT_CORNER
@ FIELD_ID_CELL_VECTOR_AT_CORNER
@ FIELD_ID_DIFFUSIVITY_GRADIENT
Immutable metadata for one field identity.
Non-owning runtime objects resolved for one field and UserCtx.
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.
PetscErrorCode ScatterParticleFieldToEulerField(UserCtx *user, ParticleFieldId particle_field_id, Vec eulerFieldAverageVec)
Scatters a particle field (scalar or vector) to the corresponding Eulerian field average.
PetscErrorCode ScatterAllParticleFieldsToEulerFields(UserCtx *user)
Scatters a predefined set of particle fields to their corresponding Eulerian fields.
PetscErrorCode ScatterParticleFieldCellMean(UserCtx *user, ParticleFieldId particle_field_id, DM dataDM, Vec countVec, PetscBool recount, Vec meanVec)
Cell mean of a particle field: the field summed over each cell's particles, divided by the cell's cou...
static PetscBool FaceFluxStencilIsLocal(const DMDALocalInfo *info, const PetscInt cell[3])
Whether the face fluxes of a host cell lie in the ghosted region: in cell-centred numbering (i,...
PetscErrorCode TrilinearInterpolation_Vector(Cmpnts ***fieldVec, PetscInt i, PetscInt j, PetscInt k, PetscReal a1, PetscReal a2, PetscReal a3, Cmpnts *vec)
Implementation of TrilinearInterpolation_Vector().
static PetscErrorCode InterpolateEulerFieldFromCenterToSwarm(UserCtx *user, PetscInt ntargets, CenterInterpolationTarget *targets)
Direct cell-center trilinear interpolation (second-order on curvilinear grids).
PetscErrorCode InterpolateFieldFromCornerToCenter_Vector(Cmpnts ***field_arr, Cmpnts ***centfield_arr, UserCtx *user)
Implementation of InterpolateFieldFromCornerToCenter_Vector().
PetscErrorCode InterpolateCornerToFaceCenter_Vector(Cmpnts ***corner_arr, Cmpnts ***faceX_arr, Cmpnts ***faceY_arr, Cmpnts ***faceZ_arr, UserCtx *user)
Implementation of InterpolateCornerToFaceCenter_Vector().
PetscErrorCode InterpolateFieldFromCornerToCenter_Scalar(PetscReal ***field_arr, PetscReal ***centfield_arr, UserCtx *user)
Implementation of InterpolateFieldFromCornerToCenter_Scalar().
void * field_ptr
Source array while the pass runs.
static PetscErrorCode CenterInterpolateScalar(PetscReal ***fieldScal, const PetscInt base[3], const PetscReal a[3], const PetscReal w[8], PetscBool unclamped, PetscReal *val)
A cell-centred scalar at a point, from the dual cell CenterDualCellWeights() found.
PetscErrorCode InterpolateAllFieldsToSwarm(UserCtx *user)
Implementation of InterpolateAllFieldsToSwarm().
Vec local_vec
Ghosted cell-centred source.
static PetscErrorCode ResolveInterpolationPair(UserCtx *user, FieldId source_field_id, ParticleFieldId target_field_id, FieldView *source_view, const ParticleFieldDescriptor **target_descriptor)
Check that an Eulerian source can be interpolated to a particle destination.
static PetscErrorCode CenterInterpolateVector(Cmpnts ***fieldVec, const PetscInt base[3], const PetscReal a[3], const PetscReal w[8], PetscBool unclamped, Cmpnts *vec)
A cell-centred vector at a point, from the dual cell CenterDualCellWeights() found.
static PetscBool CornerStencilIsLocal(const DMDALocalInfo *info, const PetscInt cell[3])
Whether the eight corners of a host cell (nodes cell to cell + 1) lie in the ghosted region.
const char * swarm_name
Destination swarm field.
PetscReal * swarm_out
Destination array while the pass runs.
#define PICURV_APPEND(...)
PetscErrorCode InterpolateVelocityAtPoint(const VelocityPointInterpolator *interp, const PetscInt cell[3], const PetscReal weights[3], Cmpnts point, Cmpnts *u)
Implementation of InterpolateVelocityAtPoint().
PetscErrorCode InterpolateVelocityAtPointEnd(VelocityPointInterpolator **interp)
Implementation of InterpolateVelocityAtPointEnd().
static PetscErrorCode InterpolateEulerVelocityFromFacesToSwarm(UserCtx *user, const char *swarmOutFieldName)
Particle velocity from the host cell's face fluxes (the FaceFlux method).
static void ComputeTrilinearWeightsUnclamped(PetscReal a1, PetscReal a2, PetscReal a3, PetscReal *w)
Unclamped trilinear weights for boundary extrapolation.
PetscInt bs
1 or 3 components.
#define ERROR_MSG_BUFFER_SIZE
PetscErrorCode TestCornerToCenterInterpolation(UserCtx *user)
Implementation of TestCornerToCenterInterpolation().
static PetscErrorCode InterpolateEulerFieldFromCornerToSwarm(UserCtx *user, Vec fieldLocal_cellCentered, const char *fieldName, const char *swarmOutFieldName)
Corner-averaged interpolation path (legacy).
static PetscErrorCode CenterDualCellWeights(const DMDALocalInfo *info, const FacePlaneArrays *dual, const PetscBool periodic[3], const PetscInt cell[3], const PetscReal weights[3], Cmpnts loc, PetscInt base[3], PetscReal a[3], PetscReal w[8], PetscBool *unclamped, PetscBool *outside)
The dual cell of cell centres a point interpolates from, and its trilinear weights.
PetscErrorCode TrilinearInterpolation_Scalar(PetscReal ***fieldScal, PetscInt i, PetscInt j, PetscInt k, PetscReal a1, PetscReal a2, PetscReal a3, PetscReal *val)
Implementation of TrilinearInterpolation_Scalar().
PetscErrorCode InterpolateFieldFromCenterToCorner_Vector(Cmpnts ***centfield_arr, Cmpnts ***corner_arr, UserCtx *user)
Implementation of InterpolateFieldFromCenterToCorner_Vector().
static PetscErrorCode InterpolateEulerFieldsToSwarm(UserCtx *user, PetscInt n, const FieldId source_field_ids[], const ParticleFieldId target_field_ids[])
Interpolates several Eulerian fields to their particle fields with the configured method.
static void ComputeTrilinearWeights(PetscReal a1, PetscReal a2, PetscReal a3, PetscReal *w)
Compute the eight trilinear interpolation weights for a particle's local coordinates.
PetscErrorCode InterpolateVelocityAtPointBegin(UserCtx *user, VelocityPointInterpolator **interp)
Implementation of InterpolateVelocityAtPointBegin().
const char * field_name
Source name, for logging.
PetscErrorCode InterpolateCornerToFaceCenter_Scalar(PetscReal ***corner_arr, PetscReal ***faceX_arr, PetscReal ***faceY_arr, PetscReal ***faceZ_arr, UserCtx *user)
Implementation of InterpolateCornerToFaceCenter_Scalar().
static PetscErrorCode InterpolateEulerFieldToSwarmForParticle(void *fieldPtr, Particle *particle, void *swarmOut, PetscInt p, PetscInt blockSize)
Interpolate one Eulerian field to a single located swarm particle.
InterpolationMethod method
static void AppendInterpolationTargetText(char *text, size_t size, size_t *used, const CenterInterpolationTarget *tg, PetscInt p, PetscInt bi, PetscInt bj, PetscInt bk)
Appends one target's interpolated value and its eight dual-cell corner values to a particle's interpo...
static PetscErrorCode StageCellFieldAtCorners(UserCtx *user, Vec cell_local, PetscInt bs, FieldView *corner_view)
Stage a ghosted cell-centred field at the grid corners, in the corner workspace for its block size: z...
PetscErrorCode InterpolateFieldFromCenterToCorner_Scalar(PetscReal ***centfield_arr, PetscReal ***corner_arr, UserCtx *user)
Implementation of InterpolateFieldFromCenterToCorner_Scalar().
One cell-centred field interpolated to a particle field in a shared pass.
The arrays one method's point evaluation reads.
#define InterpolateFieldFromCenterToCorner(blockSize, centfield_ptr, corner_ptr, user_ctx)
Macro to dispatch to the correct scalar or vector center-to-corner function based on a runtime block ...
#define InterpolateFieldFromCornerToCenter(field, centfield, user)
Generic macro to call the appropriate interpolation function based on the field type.
#define TrilinearInterpolation(fieldPtr, i, j, k, a1, a2, a3, outPtr)
Macro that calls either the scalar or vector trilinear interpolation function based on the type of th...
#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.
#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.
#define PARTICLE_LOG_ACTIVE(simCtx, pid)
True when LOG_PARTICLE() would print for this particle in the calling function.
#define LOG_LOOP_ALLOW_EXACT(scope, level, var, val, fmt,...)
Logs a custom message if a variable equals a specific value.
PetscErrorCode LOG_CORNER_FIELD_ANATOMY(UserCtx *user, FieldId corner_field_id, const char *stage_name)
Logs the node-layout anatomy of the transient center-to-corner interpolation field.
@ LOG_TRACE
Very fine-grained tracing information for in-depth debugging.
@ 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.
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_DIFFUSIVITY_GRADIENT
@ PARTICLE_FIELD_ID_DIFFUSIVITY
@ PARTICLE_FIELD_ID_VELOCITY
unsigned int capabilities
@ PARTICLE_FIELD_CAPABILITY_EULERIAN_SCATTER
PetscErrorCode ParticleFieldGetDescriptor(ParticleFieldId field_id, const ParticleFieldDescriptor **descriptor)
Return immutable metadata for a valid particle field ID.
const char * canonical_name
FieldId eulerian_scatter_target
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...
static void GetOwnedInteriorCellBounds(const DMDALocalInfo *info, PetscInt lo[3], PetscInt hi[3])
Index bounds of the interior cells this rank owns in the solver's cell-centred layout: the owned rang...
static PetscErrorCode ContravariantToCartesianInCell(Cmpnts ***lucont, Cmpnts ***lcsi, Cmpnts ***leta, Cmpnts ***lzet, PetscInt i, PetscInt j, PetscInt k, Cmpnts w, Cmpnts *ucat)
Cartesian velocity at a point of one cell from the cell's face fluxes.
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
static void GetPeriodicAxes(const SimCtx *simCtx, PetscBool periodic[3])
Which grid axes are periodic.
SimCtx * simCtx
Back-pointer to the master simulation context.
InterpolationMethod interpolationMethod
InterpolationMethod
Selects the grid-to-particle interpolation method.
The six face planes of one hexahedron, indexed by Face, and its characteristic size.
A 3D point or vector with PetscScalar components.
Defines a particle's core properties for Lagrangian tracking.
User-defined context containing data specific to a single computational grid level.
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.