Skip to content

Commit f7c6dc4

Browse files
Merge pull request #107 from PFLAREProject/adv_2d_scaling
Adv 2d scaling
2 parents a47b0d8 + f9a6f5f commit f7c6dc4

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

tests/adv_diff_2d.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static char help[] = "Solves 2D steady advection-diffusion on a structured grid.
3131

3232
#include "pflare.h"
3333

34-
extern PetscErrorCode ComputeMat(DM,Mat,PetscScalar,PetscScalar, PetscScalar, PetscBool);
34+
extern PetscErrorCode ComputeMat(DM,Mat,PetscScalar,PetscScalar,PetscScalar,PetscScalar, PetscScalar, PetscBool);
3535

3636
int main(int argc,char **argv)
3737
{
@@ -40,7 +40,7 @@ int main(int argc,char **argv)
4040
DM da;
4141
PetscErrorCode ierr;
4242
PetscInt its, M, N;
43-
PetscScalar theta, alpha, u, v, u_test, v_test;
43+
PetscScalar theta, alpha, u, v, u_test, v_test, L_x, L_y, L_x_test, L_y_test;
4444
PetscBool option_found_u, option_found_v, adv_nondim, check_nondim, diag_scale;
4545
Vec x, b, diag_vec;
4646
Mat A, A_temp;
@@ -59,6 +59,22 @@ int main(int argc,char **argv)
5959
PetscLogStageRegister("Setup", &setup);
6060
PetscLogStageRegister("GPU copy stage - triggered by a prelim KSPSolve", &gpu_copy);
6161

62+
// Dimensions of box, L_y x L_x - default to [0, 1]^2
63+
L_x = 1.0;
64+
L_y = 1.0;
65+
PetscOptionsGetReal(NULL, NULL, "-L_x", &L_x_test, &option_found_u);
66+
PetscOptionsGetReal(NULL, NULL, "-L_y", &L_y_test, &option_found_v);
67+
68+
if (option_found_u) PetscCheck(L_x_test >= 0.0, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE, "L_x must be positive");
69+
if (option_found_v) PetscCheck(L_y_test >= 0.0, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE, "L_y must be positive");
70+
71+
if (option_found_u) {
72+
L_x = L_x_test;
73+
}
74+
if (option_found_v) {
75+
L_y = L_y_test;
76+
}
77+
6278
ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
6379
ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,11,11,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);CHKERRQ(ierr);
6480
ierr = DMSetFromOptions(da);CHKERRQ(ierr);
@@ -67,6 +83,7 @@ int main(int argc,char **argv)
6783
// We do this instead of calling MatFilter as there is no Kokkos implementation so its very slow
6884
ierr = DMSetMatrixPreallocateOnly(da,PETSC_TRUE);CHKERRQ(ierr);
6985
ierr = DMSetUp(da);CHKERRQ(ierr);
86+
ierr = DMDASetUniformCoordinates(da, 0.0, L_x, 0.0, L_y, 0.0, 0.0);CHKERRQ(ierr);
7087
ierr = KSPSetDM(ksp,(DM)da);CHKERRQ(ierr);
7188
// We generate the matrix ourselves
7289
ierr = KSPSetDMActive(ksp, PETSC_FALSE);CHKERRQ(ierr);
@@ -146,7 +163,7 @@ int main(int argc,char **argv)
146163
// ~~~~~~~~~~~~~~
147164

148165
// Compute our matrix
149-
ComputeMat(da, A, u, v, alpha, adv_nondim);
166+
ComputeMat(da, A, u, v, L_x, L_y, alpha, adv_nondim);
150167
// This will compress out the extra memory
151168
MatDuplicate(A, MAT_COPY_VALUES, &A_temp);
152169
MatDestroy(&A);
@@ -215,24 +232,24 @@ int main(int argc,char **argv)
215232
return 0;
216233
}
217234

218-
PetscErrorCode ComputeMat(DM da, Mat A, PetscScalar u, PetscScalar v, PetscScalar alpha, PetscBool adv_nondim)
235+
PetscErrorCode ComputeMat(DM da, Mat A, PetscScalar u, PetscScalar v, PetscScalar L_x, PetscScalar L_y, PetscScalar alpha, PetscBool adv_nondim)
219236
{
220237
PetscErrorCode ierr;
221238
PetscInt i, j, M, N, xm, ym, xs, ys;
222239
PetscScalar val[5], Hx, Hy, HydHx, HxdHy, adv_x_scale, adv_y_scale;
223240
MatStencil row, col[5];
224241

225242
ierr = DMDAGetInfo(da,0,&M,&N,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
226-
Hx = 1.0 / (PetscReal)(M);
227-
Hy = 1.0 / (PetscReal)(N);
243+
Hx = L_x / (PetscReal)(M);
244+
Hy = L_y / (PetscReal)(N);
228245
HxdHy = Hx/Hy;
229246
HydHx = Hy/Hx;
230247
adv_x_scale = Hx;
231248
adv_y_scale = Hy;
232-
// Don't need to scale the advection terms if dimensionless
249+
// If dimensionless
233250
if (adv_nondim) {
234251
adv_x_scale = 1;
235-
adv_y_scale = 1;
252+
adv_y_scale = HydHx;
236253
}
237254

238255
ierr = DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);CHKERRQ(ierr);

0 commit comments

Comments
 (0)