Actual source code: ex205.c
petsc-3.13.6 2020-09-29
1: static char help[] = "Tests MatCopy() for SHELL matrices\n\n";
3: #include <petscmat.h>
5: typedef struct _n_User *User;
6: struct _n_User {
7: Mat A;
8: };
10: static PetscErrorCode MatMult_User(Mat A,Vec X,Vec Y)
11: {
12: User user;
16: MatShellGetContext(A,&user);
17: MatMult(user->A,X,Y);
18: return(0);
19: }
21: static PetscErrorCode MatCopy_User(Mat A,Mat B,MatStructure str)
22: {
23: User userA,userB;
27: MatShellGetContext(A,&userA);
28: if (userA) {
29: PetscNew(&userB);
30: MatDuplicate(userA->A,MAT_COPY_VALUES,&userB->A);
31: MatShellSetContext(B, userB);
32: }
33: return(0);
34: }
36: static PetscErrorCode MatDestroy_User(Mat A)
37: {
38: User user;
42: MatShellGetContext(A, &user);
43: if (user) {
44: MatDestroy(&user->A);
45: PetscFree(user);
46: }
47: return(0);
48: }
50: int main(int argc,char **args)
51: {
52: const PetscScalar xvals[] = {11,13},yvals[] = {17,19};
53: const PetscInt inds[] = {0,1};
54: PetscScalar avals[] = {2,3,5,7};
55: Mat S1,S2;
56: Vec X,Y;
57: User user;
58: PetscErrorCode ierr;
60: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
62: PetscNew(&user);
63: MatCreateSeqAIJ(PETSC_COMM_WORLD,2,2,2,NULL,&user->A);
64: MatSetUp(user->A);
65: MatSetValues(user->A,2,inds,2,inds,avals,INSERT_VALUES);
66: MatAssemblyBegin(user->A,MAT_FINAL_ASSEMBLY);
67: MatAssemblyEnd(user->A,MAT_FINAL_ASSEMBLY);
68: VecCreateSeq(PETSC_COMM_WORLD,2,&X);
69: VecSetValues(X,2,inds,xvals,INSERT_VALUES);
70: VecAssemblyBegin(X);
71: VecAssemblyEnd(X);
72: VecDuplicate(X,&Y);
73: VecSetValues(Y,2,inds,yvals,INSERT_VALUES);
74: VecAssemblyBegin(Y);
75: VecAssemblyEnd(Y);
77: MatCreateShell(PETSC_COMM_WORLD,2,2,2,2,user,&S1);
78: MatSetUp(S1);
79: MatShellSetOperation(S1,MATOP_MULT,(void (*)(void))MatMult_User);
80: MatShellSetOperation(S1,MATOP_COPY,(void (*)(void))MatCopy_User);
81: MatShellSetOperation(S1,MATOP_DESTROY,(void (*)(void))MatDestroy_User);
82: MatCreateShell(PETSC_COMM_WORLD,2,2,2,2,NULL,&S2);
83: MatSetUp(S2);
84: MatShellSetOperation(S2,MATOP_MULT,(void (*)(void))MatMult_User);
85: MatShellSetOperation(S2,MATOP_COPY,(void (*)(void))MatCopy_User);
86: MatShellSetOperation(S2,MATOP_DESTROY,(void (*)(void))MatDestroy_User);
88: MatScale(S1,31);
89: MatShift(S1,37);
90: MatDiagonalScale(S1,X,Y);
91: MatCopy(S1,S2,SAME_NONZERO_PATTERN);
92: MatMult(S1,X,Y);
93: VecView(Y,PETSC_VIEWER_STDOUT_WORLD);
94: MatMult(S2,X,Y);
95: VecView(Y,PETSC_VIEWER_STDOUT_WORLD);
97: MatDestroy(&S1);
98: MatDestroy(&S2);
99: VecDestroy(&X);
100: VecDestroy(&Y);
101: PetscFinalize();
102: return ierr;
103: }
105: /*TEST
107: test:
108: args: -malloc_dump
110: TEST*/