Actual source code: ex10.c
2: static char help[] = "Tests repeated use of assembly for matrices.\n\n";
4: #include <petscmat.h>
6: int main(int argc, char **args)
7: {
8: Mat C;
9: PetscInt i, j, m = 5, n = 2, Ii, J;
10: PetscMPIInt rank, size;
11: PetscScalar v;
14: PetscInitialize(&argc, &args, (char *)0, help);
15: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
16: MPI_Comm_size(PETSC_COMM_WORLD, &size);
17: n = 2 * size;
19: /* create the matrix for the five point stencil, YET AGAIN*/
20: MatCreate(PETSC_COMM_WORLD, &C);
21: MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n);
22: MatSetFromOptions(C);
23: MatSetUp(C);
24: for (i = 0; i < m; i++) {
25: for (j = 2 * rank; j < 2 * rank + 2; j++) {
26: v = -1.0;
27: Ii = j + n * i;
28: if (i > 0) {
29: J = Ii - n;
30: MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
31: }
32: if (i < m - 1) {
33: J = Ii + n;
34: MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
35: }
36: if (j > 0) {
37: J = Ii - 1;
38: MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
39: }
40: if (j < n - 1) {
41: J = Ii + 1;
42: MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
43: }
44: v = 4.0;
45: MatSetValues(C, 1, &Ii, 1, &Ii, &v, INSERT_VALUES);
46: }
47: }
48: MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY);
49: MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY);
50: for (i = 0; i < m; i++) {
51: for (j = 2 * rank; j < 2 * rank + 2; j++) {
52: v = 1.0;
53: Ii = j + n * i;
54: if (i > 0) {
55: J = Ii - n;
56: MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
57: }
58: if (i < m - 1) {
59: J = Ii + n;
60: MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
61: }
62: if (j > 0) {
63: J = Ii - 1;
64: MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
65: }
66: if (j < n - 1) {
67: J = Ii + 1;
68: MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
69: }
70: v = -4.0;
71: MatSetValues(C, 1, &Ii, 1, &Ii, &v, INSERT_VALUES);
72: }
73: }
74: MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY);
75: MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY);
77: MatView(C, PETSC_VIEWER_STDOUT_WORLD);
79: MatDestroy(&C);
80: PetscFinalize();
81: return 0;
82: }
84: /*TEST
86: test:
88: TEST*/