68 PetscInt block_index = user->
_this;
70 PetscFunctionBeginUser;
78 if (block_index == 0) {
81 user->
Min_X = 0.0; user->
Max_X = 2.0 * PETSC_PI;
82 user->
Min_Y = 0.0; user->
Max_Y = 2.0 * PETSC_PI;
83 user->
Min_Z = 0.0; user->
Max_Z = 0.2 * PETSC_PI;
86 PetscReal s = sqrt((PetscReal)nblk);
89 if (fabs(s - floor(s)) > 1e-9) {
90 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_ARG_INCOMP,
91 "\n\n*** CONFIGURATION ERROR FOR TGV3D ***\n"
92 "For multi-block TGV3D cases, the number of blocks must be a perfect square (e.g., 4, 9, 16).\n"
93 "You have specified %d blocks. Please adjust `-block_number`.\n", nblk);
95 PetscInt blocks_per_dim = (PetscInt)s;
97 if (block_index == 0) {
98 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"%d blocks detected. Decomposing domain into a %d x %d grid in the X-Y plane.\n", nblk, blocks_per_dim, blocks_per_dim);
102 PetscInt row = block_index / blocks_per_dim;
103 PetscInt col = block_index % blocks_per_dim;
106 PetscReal block_width = (2.0 * PETSC_PI) / (PetscReal)blocks_per_dim;
107 PetscReal block_height = (2.0 * PETSC_PI) / (PetscReal)blocks_per_dim;
110 user->
Min_X = col * block_width;
111 user->
Max_X = (col + 1) * block_width;
112 user->
Min_Y = row * block_height;
113 user->
Max_Y = (row + 1) * block_height;
115 user->
Max_Z = 2.0 * PETSC_PI;
130 PetscFunctionReturn(0);
134 PetscInt *IMs, *JMs, *KMs;
138 ierr = PetscMalloc3(nblk, &IMs, nblk, &JMs, nblk, &KMs); CHKERRQ(ierr);
141 for(PetscInt i=0; i<nblk; ++i) { IMs[i] = 10; JMs[i] = 10; KMs[i] = 10; }
143 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-im", IMs, &count, &found); CHKERRQ(ierr);
144 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-jm", JMs, &count, &found); CHKERRQ(ierr);
145 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-km", KMs, &count, &found); CHKERRQ(ierr);
147 user->
IM = IMs[block_index];
148 user->
JM = JMs[block_index];
149 user->
KM = KMs[block_index];
153 user->
rx = 1.0; user->
ry = 1.0; user->
rz = 1.0;
156 simCtx->
rank, block_index, user->
IM, user->
JM, user->
KM);
157 LOG_ALLOW(
LOCAL,
LOG_DEBUG,
"Rank %d: Block %d final bounds: X=[%.4f, %.4f], Y=[%.4f, %.4f], Z=[%.4f, %.4f]\n",
160 ierr = PetscFree3(IMs, JMs, KMs); CHKERRQ(ierr);
163 PetscFunctionReturn(0);
257 const PetscReal V0 = 1.0;
258 const PetscReal rho = 1.0;
259 const PetscReal p0 = 0.0;
262 const PetscReal nu = (simCtx->
ren > 0) ? (1.0 / simCtx->
ren) : 0.0;
264 const PetscReal k = 1.0;
265 const PetscReal t = simCtx->
ti;
267 LOG_ALLOW(
GLOBAL,
LOG_TRACE,
"TGV Setup: t = %.4f, V0* = %.4f, rho* = %.4f, k = %.4f, p0* = %4.f, nu = %.6f.\n",simCtx->
ti,V0,rho,k,p0,nu);
269 const PetscReal vel_decay = exp(-2.0 * nu * k * k * t);
270 const PetscReal prs_decay = exp(-4.0 * nu * k * k * t);
272 PetscFunctionBeginUser;
274 for (PetscInt bi = 0; bi < simCtx->
block_number; bi++) {
275 UserCtx* user = &user_finest[bi];
276 DMDALocalInfo info = user->
info;
277 PetscInt xs = info.xs, xe = info.xs + info.xm;
278 PetscInt ys = info.ys, ye = info.ys + info.ym;
279 PetscInt zs = info.zs, ze = info.zs + info.zm;
280 PetscInt mx = info.mx, my = info.my, mz = info.mz;
283 const Cmpnts ***cent, ***cent_x, ***cent_y, ***cent_z;
287 PetscInt lxs = (xs == 0) ? xs + 1 : xs, lxe = (xe == mx) ? xe - 1 : xe;
288 PetscInt lys = (ys == 0) ? ys + 1 : ys, lye = (ye == my) ? ye - 1 : ye;
289 PetscInt lzs = (zs == 0) ? zs + 1 : zs, lze = (ze == mz) ? ze - 1 : ze;
292 ierr = DMDAVecGetArray(user->
fda, user->
Ucat, &ucat); CHKERRQ(ierr);
293 ierr = DMDAVecGetArray(user->
da, user->
P, &p); CHKERRQ(ierr);
294 ierr = DMDAVecGetArray(user->
fda, user->
Bcs.
Ubcs, &ubcs); CHKERRQ(ierr);
295 ierr = DMDAVecGetArrayRead(user->
fda, user->
Cent, ¢); CHKERRQ(ierr);
296 ierr = DMDAVecGetArrayRead(user->
fda, user->
Centx, ¢_x); CHKERRQ(ierr);
297 ierr = DMDAVecGetArrayRead(user->
fda, user->
Centy, ¢_y); CHKERRQ(ierr);
298 ierr = DMDAVecGetArrayRead(user->
fda, user->
Centz, ¢_z); CHKERRQ(ierr);
301 for (PetscInt k_cell = lzs; k_cell < lze; k_cell++) {
302 for (PetscInt j_cell = lys; j_cell < lye; j_cell++) {
303 for (PetscInt i_cell = lxs; i_cell < lxe; i_cell++) {
304 const PetscReal cx = cent[k_cell][j_cell][i_cell].
x, cy = cent[k_cell][j_cell][i_cell].
y, cz = cent[k_cell][j_cell][i_cell].
z;
305 ucat[k_cell][j_cell][i_cell].
x = V0 * sin(k*cx) * cos(k*cy) * cos(k*cz) * vel_decay;
306 ucat[k_cell][j_cell][i_cell].
y = -V0 * cos(k*cx) * sin(k*cy) * cos(k*cz) * vel_decay;
307 ucat[k_cell][j_cell][i_cell].
z = 0.0;
314 for (PetscInt k_cell = lzs; k_cell < lze; k_cell++) {
315 for (PetscInt j_cell = lys; j_cell < lye; j_cell++) {
316 for (PetscInt i_cell = lxs; i_cell < lxe; i_cell++) {
317 const PetscReal cx = cent[k_cell][j_cell][i_cell].
x, cy = cent[k_cell][j_cell][i_cell].
y;
318 p[k_cell][j_cell][i_cell] = p0 + (rho * V0 * V0 / 4.0) * (cos(2*k*cx) + cos(2*k*cy)) * prs_decay;
325 if (xs == 0)
for (PetscInt k=zs; k<ze; k++)
for (PetscInt j=ys; j<ye; j++) {
326 const PetscReal fcx=cent_x[k][j][xs].
x, fcy=cent_x[k][j][xs].
y, fcz=cent_x[k][j][xs].
z;
327 ubcs[k][j][xs].
x = V0*sin(k*fcx)*cos(k*fcy)*cos(k*fcz)*vel_decay; ubcs[k][j][xs].
y = -V0*cos(k*fcx)*sin(k*fcy)*cos(k*fcz)*vel_decay; ubcs[k][j][xs].
z = 0.0;
329 if (xe == mx)
for (PetscInt k=zs; k<ze; k++)
for (PetscInt j=ys; j<ye; j++) {
330 const PetscReal fcx=cent_x[k][j][xe-1].
x, fcy=cent_x[k][j][xe-1].
y, fcz=cent_x[k][j][xe-1].
z;
331 ubcs[k][j][xe-1].
x = V0*sin(k*fcx)*cos(k*fcy)*cos(k*fcz)*vel_decay; ubcs[k][j][xe-1].
y = -V0*cos(k*fcx)*sin(k*fcy)*cos(k*fcz)*vel_decay; ubcs[k][j][xe-1].
z = 0.0;
333 if (ys == 0)
for (PetscInt k=zs; k<ze; k++)
for (PetscInt i=xs; i<xe; i++) {
334 const PetscReal fcx=cent_y[k][ys][i].
x, fcy=cent_y[k][ys][i].
y, fcz=cent_y[k][ys][i].
z;
335 ubcs[k][ys][i].
x = V0*sin(k*fcx)*cos(k*fcy)*cos(k*fcz)*vel_decay; ubcs[k][ys][i].
y = -V0*cos(k*fcx)*sin(k*fcy)*cos(k*fcz)*vel_decay; ubcs[k][ys][i].
z = 0.0;
337 if (ye == my)
for (PetscInt k=zs; k<ze; k++)
for (PetscInt i=xs; i<xe; i++) {
338 const PetscReal fcx=cent_y[k][ye-1][i].
x, fcy=cent_y[k][ye-1][i].
y, fcz=cent_y[k][ye-1][i].
z;
339 ubcs[k][ye-1][i].
x = V0*sin(k*fcx)*cos(k*fcy)*cos(k*fcz)*vel_decay; ubcs[k][ye-1][i].
y = -V0*cos(k*fcx)*sin(k*fcy)*cos(k*fcz)*vel_decay; ubcs[k][ye-1][i].
z = 0.0;
341 if (zs == 0)
for (PetscInt j=ys; j<ye; j++)
for (PetscInt i=xs; i<xe; i++) {
342 const PetscReal fcx=cent_z[zs][j][i].
x, fcy=cent_z[zs][j][i].
y, fcz=cent_z[zs][j][i].
z;
343 ubcs[zs][j][i].
x = V0*sin(k*fcx)*cos(k*fcy)*cos(k*fcz)*vel_decay; ubcs[zs][j][i].
y = -V0*cos(k*fcx)*sin(k*fcy)*cos(k*fcz)*vel_decay; ubcs[zs][j][i].
z = 0.0;
345 if (ze == mz)
for (PetscInt j=ys; j<ye; j++)
for (PetscInt i=xs; i<xe; i++) {
346 const PetscReal fcx=cent_z[ze-1][j][i].
x, fcy=cent_z[ze-1][j][i].
y, fcz=cent_z[ze-1][j][i].
z;
347 ubcs[ze-1][j][i].
x = V0*sin(k*fcx)*cos(k*fcy)*cos(k*fcz)*vel_decay; ubcs[ze-1][j][i].
y = -V0*cos(k*fcx)*sin(k*fcy)*cos(k*fcz)*vel_decay; ubcs[ze-1][j][i].
z = 0.0;
351 if (xs == 0)
for (PetscInt k=lzs; k<lze; k++)
for (PetscInt j=lys; j<lye; j++) p[k][j][xs] = p[k][j][xs+1];
352 if (xe == mx)
for (PetscInt k=lzs; k<lze; k++)
for (PetscInt j=lys; j<lye; j++) p[k][j][xe-1] = p[k][j][xe-2];
354 if (ys == 0)
for (PetscInt k=lzs; k<lze; k++)
for (PetscInt i=lxs; i<lxe; i++) p[k][ys][i] = p[k][ys+1][i];
355 if (ye == my)
for (PetscInt k=lzs; k<lze; k++)
for (PetscInt i=lxs; i<lxe; i++) p[k][ye-1][i] = p[k][ye-2][i];
357 if (zs == 0)
for (PetscInt j=lys; j<lye; j++)
for (PetscInt i=lxs; i<lxe; i++) p[zs][j][i] = p[zs+1][j][i];
358 if (ze == mz)
for (PetscInt j=lys; j<lye; j++)
for (PetscInt i=lxs; i<lxe; i++) p[ze-1][j][i] = p[ze-2][j][i];
361 ierr = DMDAVecRestoreArray(user->
fda, user->
Ucat, &ucat); CHKERRQ(ierr);
362 ierr = DMDAVecRestoreArray(user->
da, user->
P, &p); CHKERRQ(ierr);
363 ierr = DMDAVecRestoreArray(user->
fda, user->
Bcs.
Ubcs, &ubcs); CHKERRQ(ierr);
364 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
Cent, ¢); CHKERRQ(ierr);
365 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
Centx, ¢_x); CHKERRQ(ierr);
366 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
Centy, ¢_y); CHKERRQ(ierr);
367 ierr = DMDAVecRestoreArrayRead(user->
fda, user->
Centz, ¢_z); CHKERRQ(ierr);
383 PetscFunctionReturn(0);