125 DMBoundaryType xperiod = (simCtx->
i_periodic) ? DM_BOUNDARY_PERIODIC : DM_BOUNDARY_NONE;
126 DMBoundaryType yperiod = (simCtx->
j_periodic) ? DM_BOUNDARY_PERIODIC : DM_BOUNDARY_NONE;
127 DMBoundaryType zperiod = (simCtx->
k_periodic) ? DM_BOUNDARY_PERIODIC : DM_BOUNDARY_NONE;
128 PetscInt stencil_width = 2;
130 PetscInt *lx = NULL, *ly = NULL, *lz = NULL;
133 PetscFunctionBeginUser;
141 DMDAGetInfo(coarse_user->
da, NULL, NULL, NULL, NULL, &m, &n, &p, NULL, NULL, NULL, NULL, NULL, NULL);
145 PetscInt *lx_contrib, *ly_contrib, *lz_contrib;
146 ierr = PetscMalloc3(m, &lx_contrib, n, &ly_contrib, p, &lz_contrib); CHKERRQ(ierr);
147 ierr = PetscMemzero(lx_contrib, m *
sizeof(PetscInt)); CHKERRQ(ierr);
148 ierr = PetscMemzero(ly_contrib, n *
sizeof(PetscInt)); CHKERRQ(ierr);
149 ierr = PetscMemzero(lz_contrib, p *
sizeof(PetscInt)); CHKERRQ(ierr);
152 DMDAGetLocalInfo(coarse_user->
da, &info);
153 PetscInt xs = info.xs, xe = info.xs + info.xm, mx = info.mx;
154 PetscInt ys = info.ys, ye = info.ys + info.ym, my = info.my;
155 PetscInt zs = info.zs, ze = info.zs + info.zm, mz = info.mz;
158 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
159 PetscInt proc_i = rank % m;
160 PetscInt proc_j = (rank / m) % n;
161 PetscInt proc_k = rank / (m * n);
164 if (user->
isc) lx_contrib[proc_i] = (xe - xs);
166 if (m == 1) lx_contrib[0] = user->
IM + 1;
167 else if (xs == 0) lx_contrib[0] = 2 * xe - 1;
168 else if (xe == mx) lx_contrib[proc_i] = user->
IM + 1 - (2 * xs - 1);
169 else lx_contrib[proc_i] = (xe - xs) * 2;
173 if (user->
jsc) ly_contrib[proc_j] = (ye - ys);
175 if (n == 1) ly_contrib[0] = user->
JM + 1;
176 else if (ys == 0) ly_contrib[0] = 2 * ye - 1;
177 else if (ye == my) ly_contrib[proc_j] = user->
JM + 1 - (2 * ys - 1);
178 else ly_contrib[proc_j] = (ye - ys) * 2;
182 if (user->
ksc) lz_contrib[proc_k] = (ze - zs);
184 if (p == 1) lz_contrib[0] = user->
KM + 1;
185 else if (zs == 0) lz_contrib[0] = 2 * ze - 1;
186 else if (ze == mz) lz_contrib[proc_k] = user->
KM + 1 - (2 * zs - 1);
187 else lz_contrib[proc_k] = (ze - zs) * 2;
189 LOG_ALLOW_SYNC(
LOCAL,
LOG_VERBOSE,
"Rank %d: Calculated this rank's node contribution to fine grid: lx=%d, ly=%d, lz=%d\n", simCtx->
rank, lx_contrib[proc_i], ly_contrib[proc_j], lz_contrib[proc_k]);
192 ierr = PetscMalloc3(m, &lx, n, &ly, p, &lz); CHKERRQ(ierr);
193 ierr = MPI_Allreduce(lx_contrib, lx, m, MPIU_INT, MPI_MAX, PETSC_COMM_WORLD); CHKERRQ(ierr);
194 ierr = MPI_Allreduce(ly_contrib, ly, n, MPIU_INT, MPI_MAX, PETSC_COMM_WORLD); CHKERRQ(ierr);
195 ierr = MPI_Allreduce(lz_contrib, lz, p, MPIU_INT, MPI_MAX, PETSC_COMM_WORLD); CHKERRQ(ierr);
197 ierr = PetscFree3(lx_contrib, ly_contrib, lz_contrib); CHKERRQ(ierr);
210 ierr = DMDACreate3d(PETSC_COMM_WORLD, xperiod, yperiod, zperiod, DMDA_STENCIL_BOX,
211 user->
IM + 1, user->
JM + 1, user->
KM + 1,
213 1, stencil_width, lx, ly, lz, &user->
da); CHKERRQ(ierr);
216 ierr = PetscFree3(lx, ly, lz); CHKERRQ(ierr);
220 ierr = DMSetUp(user->
da); CHKERRQ(ierr);
221 ierr = DMGetCoordinateDM(user->
da, &user->
fda); CHKERRQ(ierr);
222 ierr = DMDASetUniformCoordinates(user->
da, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0); CHKERRQ(ierr);
223 ierr = DMDAGetLocalInfo(user->
da, &user->
info); CHKERRQ(ierr);
228 PetscFunctionReturn(0);
254 PetscFunctionBeginUser;
262 for (PetscInt level = usermg->
mglevels - 2; level >= 0; level--) {
263 for (PetscInt bi = 0; bi < nblk; bi++) {
267 user_coarse->
IM = user_fine->
isc ? user_fine->
IM : (user_fine->
IM + 1) / 2;
268 user_coarse->
JM = user_fine->
jsc ? user_fine->
JM : (user_fine->
JM + 1) / 2;
269 user_coarse->
KM = user_fine->
ksc ? user_fine->
KM : (user_fine->
KM + 1) / 2;
272 simCtx->
rank, bi, level, user_coarse->
IM, user_coarse->
JM, user_coarse->
KM);
275 PetscInt check_i = user_coarse->
IM * (2 - user_coarse->
isc) - (user_fine->
IM + 1 - user_coarse->
isc);
276 PetscInt check_j = user_coarse->
JM * (2 - user_coarse->
jsc) - (user_fine->
JM + 1 - user_coarse->
jsc);
277 PetscInt check_k = user_coarse->
KM * (2 - user_coarse->
ksc) - (user_fine->
KM + 1 - user_coarse->
ksc);
279 if (check_i + check_j + check_k != 0) {
283 LOG(
GLOBAL,
LOG_WARNING,
"WARNING: Grid at level %d, block %d can't be consistently coarsened further.\n", level, bi);
289 for (PetscInt bi = 0; bi < nblk; bi++) {
296 for (PetscInt level = 1; level < usermg->
mglevels; level++) {
304 ierr = DMView(mgctx[usermg->
mglevels - 1].
user[0].
da, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr);
311 PetscFunctionReturn(0);
409 PetscFunctionBeginUser;
419 FILE *grid_file_handle = NULL;
421 if (simCtx->
rank == 0) {
422 grid_file_handle = fopen(simCtx->
grid_file,
"r");
423 if (!grid_file_handle) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Cannot open file: %s", simCtx->
grid_file);
428 char dummy_buffer[2048];
429 for (PetscInt s = 0; s < headerLines; ++s) {
430 if (!fgets(dummy_buffer,
sizeof(dummy_buffer), grid_file_handle)) {
431 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
"Unexpected EOF while skipping grid header");
442 if (simCtx->
rank == 0) {
443 fclose(grid_file_handle);
451 ierr = DMGetCoordinates(user->
da, &gCoor); CHKERRQ(ierr);
452 ierr = DMGetCoordinatesLocal(user->
da, &lCoor); CHKERRQ(ierr);
455 ierr = DMLocalToGlobalBegin(user->
fda, lCoor, INSERT_VALUES, gCoor); CHKERRQ(ierr);
456 ierr = DMLocalToGlobalEnd(user->
fda, lCoor, INSERT_VALUES, gCoor); CHKERRQ(ierr);
458 ierr = DMGlobalToLocalBegin(user->
fda, gCoor, INSERT_VALUES, lCoor); CHKERRQ(ierr);
459 ierr = DMGlobalToLocalEnd(user->
fda, gCoor, INSERT_VALUES, lCoor); CHKERRQ(ierr);
463 PetscFunctionReturn(0);
524 PetscFunctionBeginUser;
530 ierr = DMDAGetLocalInfo(user->
da, &info); CHKERRQ(ierr);
531 ierr = DMGetCoordinatesLocal(user->
da, &lCoor); CHKERRQ(ierr);
533 PetscInt xs = info.xs, xe = info.xs + info.xm;
534 PetscInt ys = info.ys, ye = info.ys + info.ym;
535 PetscInt zs = info.zs, ze = info.zs + info.zm;
539 ierr = VecSet(lCoor, 0.0); CHKERRQ(ierr);
540 ierr = DMDAVecGetArray(user->
fda, lCoor, &coor); CHKERRQ(ierr);
547 for (PetscInt k = zs; k < ze; k++) {
548 for (PetscInt j = ys; j < ye; j++) {
549 for (PetscInt i = xs; i < xe; i++) {
550 if(k < user->KM && j < user->JM && i < user->IM){
571 ierr = DMDAVecRestoreArray(user->
fda, lCoor, &coor); CHKERRQ(ierr);
575 PetscFunctionReturn(0);
605 PetscMPIInt rank = simCtx->
rank;
606 PetscInt block_index = user->
_this;
607 PetscInt IM = user->
IM, JM = user->
JM, KM = user->
KM;
611 PetscReal *gc = NULL;
613 PetscFunctionBeginUser;
618 simCtx->
rank, block_index);
622 PetscInt n_nodes = (IM) * (JM) * (KM);
623 ierr = PetscMalloc1(3 * n_nodes, &gc); CHKERRQ(ierr);
627 if (!fd) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Recieved a NULL file handle.\n");
630 for (PetscInt k = 0; k < KM; k++) {
631 for (PetscInt j = 0; j < JM; j++) {
632 for (PetscInt i = 0; i < IM; i++) {
633 PetscInt base_index = 3 * ((k * (JM) + j) * (IM) + i);
634 if (fscanf(fd,
"%le %le %le\n", &gc[base_index], &gc[base_index + 1], &gc[base_index + 2]) != 3) {
635 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
"Error reading coordinates for node (i,j,k)=(%d,%d,%d) in block %d", i, j, k, block_index);
644 ierr = MPI_Bcast(gc, 3 * n_nodes, MPIU_REAL, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
647 ierr = DMDAGetLocalInfo(user->
da, &info); CHKERRQ(ierr);
648 ierr = DMGetCoordinatesLocal(user->
da, &lCoor); CHKERRQ(ierr);
649 ierr = DMDAVecGetArray(user->
fda, lCoor, &coor); CHKERRQ(ierr);
651 for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
652 for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
653 for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
654 if(k< KM && j < JM && i < IM){
655 PetscInt base_idx = 3 * ((k * (JM) + j) * (IM) + i);
656 coor[k][j][i].
x = gc[base_idx];
657 coor[k][j][i].
y = gc[base_idx + 1];
658 coor[k][j][i].
z = gc[base_idx + 2];
665 ierr = DMDAVecRestoreArray(user->
fda, lCoor, &coor); CHKERRQ(ierr);
666 ierr = PetscFree(gc); CHKERRQ(ierr);
670 PetscFunctionReturn(0);
691 Vec c_lCoor, f_lCoor;
694 DMDALocalInfo c_info;
697 PetscFunctionBeginUser;
704 ierr = DMDAGetLocalInfo(coarse_user->
da, &c_info); CHKERRQ(ierr);
706 ierr = DMGetCoordinatesLocal(coarse_user->
da, &c_lCoor); CHKERRQ(ierr);
707 ierr = DMGetCoordinatesLocal(fine_user->
da, &f_lCoor); CHKERRQ(ierr);
709 ierr = DMDAVecGetArray(coarse_user->
fda, c_lCoor, &c_coor); CHKERRQ(ierr);
710 ierr = DMDAVecGetArrayRead(fine_user->
fda, f_lCoor, &f_coor); CHKERRQ(ierr);
713 PetscInt xs = c_info.xs, xe = c_info.xs + c_info.xm;
714 PetscInt ys = c_info.ys, ye = c_info.ys + c_info.ym;
715 PetscInt zs = c_info.zs, ze = c_info.zs + c_info.zm;
718 PetscInt mx = c_info.mx, my = c_info.my, mz = c_info.mz;
727 for (PetscInt k = zs; k < ze; k++) {
728 for (PetscInt j = ys; j < ye; j++) {
729 for (PetscInt i = xs; i < xe; i++) {
732 ih = coarse_user->
isc ? i : 2 * i;
733 jh = coarse_user->
jsc ? j : 2 * j;
734 kh = coarse_user->
ksc ? k : 2 * k;
738 c_coor[k][j][i] = f_coor[kh][jh][ih];
743 ierr = DMDAVecRestoreArray(coarse_user->
fda, c_lCoor, &c_coor); CHKERRQ(ierr);
744 ierr = DMDAVecRestoreArrayRead(fine_user->
fda, f_lCoor, &f_coor); CHKERRQ(ierr);
748 ierr = DMGetCoordinates(coarse_user->
da, &c_gCoor); CHKERRQ(ierr);
749 ierr = DMLocalToGlobalBegin(coarse_user->
fda, c_lCoor, INSERT_VALUES, c_gCoor); CHKERRQ(ierr);
750 ierr = DMLocalToGlobalEnd(coarse_user->
fda, c_lCoor, INSERT_VALUES, c_gCoor); CHKERRQ(ierr);
751 ierr = DMGlobalToLocalBegin(coarse_user->
fda, c_gCoor, INSERT_VALUES, c_lCoor); CHKERRQ(ierr);
752 ierr = DMGlobalToLocalEnd(coarse_user->
fda, c_gCoor, INSERT_VALUES, c_lCoor); CHKERRQ(ierr);
756 PetscFunctionReturn(0);
785 PetscInt xs, ys, zs, xe, ye, ze;
789 Cmpnts minCoords, maxCoords;
791 PetscFunctionBeginUser;
801 return PETSC_ERR_ARG_NULL;
805 return PETSC_ERR_ARG_NULL;
809 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
812 ierr = DMGetCoordinatesLocal(user->
da, &coordinates);
820 return PETSC_ERR_ARG_NULL;
824 ierr = DMDAVecGetArrayRead(user->
fda, coordinates, &coordArray);
831 ierr = DMDAGetLocalInfo(user->
da, &info);
838 xs = info.gxs; xe = xs + info.gxm;
839 ys = info.gys; ye = ys + info.gym;
840 zs = info.gzs; ze = zs + info.gzm;
849 minCoords.
x = minCoords.
y = minCoords.
z = PETSC_MAX_REAL;
850 maxCoords.
x = maxCoords.
y = maxCoords.
z = PETSC_MIN_REAL;
852 LOG_ALLOW(
LOCAL,
LOG_TRACE,
"[Rank %d] Grid indices (Including Ghosts): xs=%d, xe=%d, ys=%d, ye=%d, zs=%d, ze=%d.\n",rank, xs, xe, ys, ye, zs, ze);
855 for (k = zs; k < ze; k++) {
856 for (j = ys; j < ye; j++) {
857 for (i = xs; i < xe; i++) {
859 if(i < user->IM && j < user->JM && k < user->KM){
860 Cmpnts coord = coordArray[k][j][i];
863 if (coord.
x < minCoords.
x) minCoords.
x = coord.
x;
864 if (coord.
y < minCoords.
y) minCoords.
y = coord.
y;
865 if (coord.
z < minCoords.
z) minCoords.
z = coord.
z;
867 if (coord.
x > maxCoords.
x) maxCoords.
x = coord.
x;
868 if (coord.
y > maxCoords.
y) maxCoords.
y = coord.
y;
869 if (coord.
z > maxCoords.
z) maxCoords.
z = coord.
z;
888 LOG_ALLOW(
LOCAL,
LOG_INFO,
"[Rank %d] Bounding Box Ranges = X[%.6f, %.6f], Y[%.6f,%.6f], Z[%.6f, %.6f].\n",rank,minCoords.
x, maxCoords.
x,minCoords.
y, maxCoords.
y, minCoords.
z, maxCoords.
z);
893 ierr = DMDAVecRestoreArrayRead(user->
fda, coordinates, &coordArray);
904 user->
bbox = *localBBox;
910 PetscFunctionReturn(0);
1056 PetscErrorCode ierr;
1057 BCFace inlet_face_id = -1;
1058 PetscBool inlet_found = PETSC_FALSE;
1060 PetscReal local_sum[3] = {0.0, 0.0, 0.0};
1061 PetscReal global_sum[3] = {0.0, 0.0, 0.0};
1062 PetscCount local_n_points = 0;
1063 PetscCount global_n_points = 0;
1066 DMDALocalInfo info = user->
info;
1067 PetscInt xs = info.xs, xe = info.xs + info.xm;
1068 PetscInt ys = info.ys, ye = info.ys + info.ym;
1069 PetscInt zs = info.zs, ze = info.zs + info.zm;
1070 PetscInt mx = info.mx, my = info.my, mz = info.mz;
1075 PetscFunctionBeginUser;
1080 for (
int i = 0; i < 6; i++) {
1083 inlet_found = PETSC_TRUE;
1090 PetscFunctionReturn(0);
1094 ierr = DMGetCoordinatesLocal(user->
da,&lCoor);
1095 ierr = DMDAVecGetArrayRead(user->
fda, lCoor, &coor); CHKERRQ(ierr);
1098 switch (inlet_face_id) {
1101 for (PetscInt k = zs; k < ze; k++)
for (PetscInt j = ys; j < ye; j++) {
1102 if(j < user->JM && k < user->KM){
1103 local_sum[0] += coor[k][j][0].
x;
1104 local_sum[1] += coor[k][j][0].
y;
1105 local_sum[2] += coor[k][j][0].
z;
1113 for (PetscInt k = zs; k < ze; k++)
for (PetscInt j = ys; j < ye; j++) {
1114 if(j < user->JM && k < user->KM){
1115 local_sum[0] += coor[k][j][mx-2].
x;
1116 local_sum[1] += coor[k][j][mx-2].
y;
1117 local_sum[2] += coor[k][j][mx-2].
z;
1125 for (PetscInt k = zs; k < ze; k++)
for (PetscInt i = xs; i < xe; i++) {
1126 if(i < user->IM && k < user->KM){
1127 local_sum[0] += coor[k][0][i].
x;
1128 local_sum[1] += coor[k][0][i].
y;
1129 local_sum[2] += coor[k][0][i].
z;
1137 for (PetscInt k = zs; k < ze; k++)
for (PetscInt i = xs; i < xe; i++) {
1138 local_sum[0] += coor[k][my-2][i].
x;
1139 local_sum[1] += coor[k][my-2][i].
y;
1140 local_sum[2] += coor[k][my-2][i].
z;
1147 for (PetscInt j = ys; j < ye; j++)
for (PetscInt i = xs; i < xe; i++) {
1148 if(i < user->IM && j < user->JM){
1149 local_sum[0] += coor[0][j][i].
x;
1150 local_sum[1] += coor[0][j][i].
y;
1151 local_sum[2] += coor[0][j][i].
z;
1159 for (PetscInt j = ys; j < ye; j++)
for (PetscInt i = xs; i < xe; i++) {
1160 if(i < user->IM && j < user->JM){
1161 local_sum[0] += coor[mz-2][j][i].
x;
1162 local_sum[1] += coor[mz-2][j][i].
y;
1163 local_sum[2] += coor[mz-2][j][i].
z;
1171 ierr = DMDAVecRestoreArrayRead(user->
fda, lCoor, &coor); CHKERRQ(ierr);
1174 ierr = MPI_Allreduce(local_sum, global_sum, 3, MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD); CHKERRQ(ierr);
1175 ierr = MPI_Allreduce(&local_n_points, &global_n_points, 1, MPI_INT, MPI_SUM, PETSC_COMM_WORLD); CHKERRQ(ierr);
1178 if (global_n_points > 0) {
1179 user->
simCtx->
CMx_c = global_sum[0] / global_n_points;
1180 user->
simCtx->
CMy_c = global_sum[1] / global_n_points;
1181 user->
simCtx->
CMz_c = global_sum[2] / global_n_points;
1185 LOG_ALLOW(
GLOBAL,
LOG_WARNING,
"WARNING: Inlet face was identified but no grid points found on it. Center not calculated.\n");
1190 PetscFunctionReturn(0);