Actual source code: ex39.c
petsc-3.12.2 2019-11-22
1: /*
2: mpiexec -n 8 ./ex39 -ksp_type fbcgs -ksp_rtol 1.e-6 -sub_ksp_type bcgs -sub_ksp_rtol 1.e-3 -pc_type bjacobi -ksp_converged_reason -ksp_monitor -n1 32 -n2 32 -n3 32
4: Contributed by Jie Chen for testing flexible BiCGStab algorithm
5: */
7: static char help[] = "Solves the PDE (in 3D) - laplacian(u) + gamma x dot grad(u) + beta u = 1\n\
8: with zero Dirichlet condition. The discretization is standard centered\n\
9: difference. Input parameters include:\n\
10: -n1 : number of mesh points in 1st dimension (default 32)\n\
11: -n2 : number of mesh points in 2nd dimension (default 32)\n\
12: -n3 : number of mesh points in 3nd dimension (default 32)\n\
13: -h : spacing between mesh points (default 1/n1)\n\
14: -gamma : gamma (default 4/h)\n\
15: -beta : beta (default 0.01/h^2)\n\n";
17: #include <petscksp.h>
18: int main(int argc,char **args)
19: {
20: Vec x,b,u; /* approx solution, RHS, working vector */
21: Mat A; /* linear system matrix */
22: KSP ksp; /* linear solver context */
23: PetscInt n1, n2, n3; /* parameters */
24: PetscReal h, gamma, beta; /* parameters */
25: PetscInt i,j,k,Ii,J,Istart,Iend;
27: PetscScalar v, co1, co2;
29: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
30: n1 = 32;
31: n2 = 32;
32: n3 = 32;
33: PetscOptionsGetInt(NULL,NULL,"-n1",&n1,NULL);
34: PetscOptionsGetInt(NULL,NULL,"-n2",&n2,NULL);
35: PetscOptionsGetInt(NULL,NULL,"-n3",&n3,NULL);
37: h = 1.0/n1;
38: gamma = 4.0/h;
39: beta = 0.01/(h*h);
40: PetscOptionsGetReal(NULL,NULL,"-h",&h,NULL);
41: PetscOptionsGetReal(NULL,NULL,"-gamma",&gamma,NULL);
42: PetscOptionsGetReal(NULL,NULL,"-beta",&beta,NULL);
44: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45: Compute the matrix and set right-hand-side vector.
46: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
47: MatCreate(PETSC_COMM_WORLD,&A);
48: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n1*n2*n3,n1*n2*n3);
49: MatSetFromOptions(A);
50: MatMPIAIJSetPreallocation(A,7,NULL,7,NULL);
51: MatSeqAIJSetPreallocation(A,7,NULL);
52: MatSetUp(A);
53: MatGetOwnershipRange(A,&Istart,&Iend);
55: /*
56: Set matrix elements for the 3-D, seven-point stencil in parallel.
57: - Each processor needs to insert only elements that it owns
58: locally (but any non-local elements will be sent to the
59: appropriate processor during matrix assembly).
60: - Always specify global rows and columns of matrix entries.
61: */
62: co1 = gamma * h * h / 2.0;
63: co2 = beta * h * h;
64: for (Ii=Istart; Ii<Iend; Ii++) {
65: i = Ii/(n2*n3); j = (Ii - i*n2*n3)/n3; k = Ii - i*n2*n3 - j*n3;
66: if (i>0) {
67: J = Ii - n2*n3; v = -1.0 + co1*(PetscScalar)i;
68: MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);
69: }
70: if (i<n1-1) {
71: J = Ii + n2*n3; v = -1.0 + co1*(PetscScalar)i;
72: MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);
73: }
74: if (j>0) {
75: J = Ii - n3; v = -1.0 + co1*(PetscScalar)j;
76: MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);
77: }
78: if (j<n2-1) {
79: J = Ii + n3; v = -1.0 + co1*(PetscScalar)j;
80: MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);
81: }
82: if (k>0) {
83: J = Ii - 1; v = -1.0 + co1*(PetscScalar)k;
84: MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);
85: }
86: if (k<n3-1) {
87: J = Ii + 1; v = -1.0 + co1*(PetscScalar)k;
88: MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);
89: }
90: v = 6.0 + co2;
91: MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);
92: }
93: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
94: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
96: /* Create parallel vectors and Set right-hand side. */
97: VecCreate(PETSC_COMM_WORLD,&b);
98: VecSetSizes(b,PETSC_DECIDE,n1*n2*n3);
99: VecSetFromOptions(b);
100: VecDuplicate(b,&x);
101: VecDuplicate(b,&u);
102: VecSet(b,1.0);
104: /* Create linear solver context */
105: KSPCreate(PETSC_COMM_WORLD,&ksp);
106: KSPSetOperators(ksp,A,A);
107: KSPSetTolerances(ksp,1.e-6,1.e-50,PETSC_DEFAULT,200);
108: KSPSetFromOptions(ksp);
110: /* Solve the linear system */
111: KSPSolve(ksp,b,x);
113: /* Free work space. */
114: KSPDestroy(&ksp);
115: VecDestroy(&u); VecDestroy(&x);
116: VecDestroy(&b); MatDestroy(&A);
117: PetscFinalize();
118: return ierr;
119: }
122: /*TEST
124: test:
125: nsize: 8
126: args: -ksp_type fbcgs -ksp_rtol 1.e-6 -sub_ksp_type bcgs -sub_ksp_rtol 1.e-3 -pc_type bjacobi -ksp_converged_reason -n1 32 -n2 32 -n3 32
128: test:
129: suffix: 2
130: nsize: 8
131: args: -ksp_type fbcgsr -ksp_rtol 1.e-6 -sub_ksp_type bcgs -sub_ksp_rtol 1.e-3 -pc_type bjacobi -ksp_converged_reason -n1 32 -n2 32 -n3 32
132: output_file: output/ex39_1.out
134: TEST*/