xref: /petsc/src/mat/tests/ex222.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 static char help[] = "Tests MatComputeOperator() and MatComputeOperatorTranspose()\n\n";
2 
3 #include <petscmat.h>
4 
5 int main(int argc,char **argv)
6 {
7   Mat            A,Ae,Aet;
8   char           filename[PETSC_MAX_PATH_LEN];
9   char           expltype[128],*etype = NULL;
10   PetscInt       bs = 1;
11   PetscBool      flg, check = PETSC_TRUE;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc,&argv,(char*) 0,help));
15 
16   PetscCall(PetscOptionsGetString(NULL,NULL,"-expl_type",expltype,sizeof(expltype),&flg));
17   if (flg) {
18     PetscCall(PetscStrallocpy(expltype,&etype));
19   }
20   PetscCall(PetscOptionsGetString(NULL,NULL,"-f",filename,sizeof(filename),&flg));
21   PetscCall(PetscOptionsGetInt(NULL,NULL,"-bs",&bs,NULL));
22   if (!flg) {
23     PetscInt M = 13,N = 6;
24 
25     PetscCall(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL));
26     PetscCall(PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL));
27     PetscCall(MatCreateDense(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,M,N,NULL,&A));
28     PetscCall(MatSetBlockSize(A,bs));
29     PetscCall(MatSetRandom(A,NULL));
30   } else {
31     PetscViewer viewer;
32 
33     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
34     PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
35     PetscCall(MatSetBlockSize(A,bs));
36     PetscCall(MatSetFromOptions(A));
37     PetscCall(MatLoad(A,viewer));
38     PetscCall(PetscViewerDestroy(&viewer));
39   }
40   PetscCall(PetscObjectSetName((PetscObject)A,"Matrix"));
41   PetscCall(MatViewFromOptions(A,NULL,"-view_expl"));
42 
43   PetscCall(MatComputeOperator(A,etype,&Ae));
44   PetscCall(PetscObjectSetName((PetscObject)Ae,"Explicit matrix"));
45   PetscCall(MatViewFromOptions(Ae,NULL,"-view_expl"));
46 
47   PetscCall(PetscOptionsGetBool(NULL,NULL,"-check",&check,NULL));
48   if (check) {
49     Mat A2;
50     PetscReal err,tol = PETSC_SMALL;
51 
52     PetscCall(PetscOptionsGetReal(NULL,NULL,"-tol",&tol,NULL));
53     PetscCall(MatConvert(A,etype,MAT_INITIAL_MATRIX,&A2));
54     PetscCall(MatAXPY(A2,-1.0,Ae,DIFFERENT_NONZERO_PATTERN));
55     PetscCall(MatNorm(A2,NORM_FROBENIUS,&err));
56     if (err > tol) {
57       PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Error %g > %g (type %s)\n",(double)err,(double)tol,etype));
58     }
59     PetscCall(MatDestroy(&A2));
60   }
61 
62   PetscCall(MatComputeOperatorTranspose(A,etype,&Aet));
63   PetscCall(PetscObjectSetName((PetscObject)Aet,"Explicit matrix transpose"));
64   PetscCall(MatViewFromOptions(Aet,NULL,"-view_expl"));
65 
66   PetscCall(PetscFree(etype));
67   PetscCall(MatDestroy(&Ae));
68   PetscCall(MatDestroy(&Aet));
69   PetscCall(MatDestroy(&A));
70   PetscCall(PetscFinalize());
71   return 0;
72 }
73 
74 /*TEST
75 
76    test:
77      output_file: output/ex222_null.out
78 
79    testset:
80      suffix: matexpl_rect
81      output_file: output/ex222_null.out
82      nsize: {{1 3}}
83      args: -expl_type {{dense aij baij}}
84 
85    testset:
86      suffix: matexpl_square
87      output_file: output/ex222_null.out
88      nsize: {{1 3}}
89      args: -bs {{1 2 3}} -M 36 -N 36 -expl_type {{dense aij baij sbaij}}
90 
91 TEST*/
92