Actual source code: ex64.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Saves 4by4 block matrix.\n\n";
4: #include <petscmat.h>
8: int main(int argc,char **args)
9: {
10: Mat A;
11: PetscInt i,j;
13: PetscMPIInt size;
14: PetscViewer fd;
15: PetscScalar values[16],one = 1.0;
16: Vec x;
18: PetscInitialize(&argc,&args,(char*)0,help);
19: MPI_Comm_size(PETSC_COMM_WORLD,&size);
20: if (size > 1) SETERRQ(PETSC_COMM_WORLD,1,"Can only run on one processor");
22: /*
23: Open binary file. Note that we use FILE_MODE_WRITE to indicate
24: writing to this file.
25: */
26: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"4by4",FILE_MODE_WRITE,&fd);
28: MatCreateSeqBAIJ(PETSC_COMM_WORLD,4,12,12,0,0,&A);
30: for (i=0; i<16; i++) values[i] = i;
31: for (i=0; i<4; i++) values[4*i+i] += 5;
32: i = 0; j = 0;
33: MatSetValuesBlocked(A,1,&i,1,&j,values,INSERT_VALUES);
35: for (i=0; i<16; i++) values[i] = i;
36: i = 0; j = 2;
37: MatSetValuesBlocked(A,1,&i,1,&j,values,INSERT_VALUES);
39: for (i=0; i<16; i++) values[i] = i;
40: i = 1; j = 0;
41: MatSetValuesBlocked(A,1,&i,1,&j,values,INSERT_VALUES);
43: for (i=0; i<16; i++) values[i] = i;for (i=0; i<4; i++) values[4*i+i] += 6;
44: i = 1; j = 1;
45: MatSetValuesBlocked(A,1,&i,1,&j,values,INSERT_VALUES);
47: for (i=0; i<16; i++) values[i] = i;
48: i = 2; j = 0;
49: MatSetValuesBlocked(A,1,&i,1,&j,values,INSERT_VALUES);
51: for (i=0; i<16; i++) values[i] = i;for (i=0; i<4; i++) values[4*i+i] += 7;
52: i = 2; j = 2;
53: MatSetValuesBlocked(A,1,&i,1,&j,values,INSERT_VALUES);
55: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
56: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
57: MatView(A,fd);
58: MatDestroy(&A);
60: VecCreateSeq(PETSC_COMM_WORLD,12,&x);
61: VecSet(x,one);
62: VecView(x,fd);
63: VecDestroy(&x);
65: PetscViewerDestroy(&fd);
66: PetscFinalize();
67: return 0;
68: }