xref: /petsc/src/mat/tests/ex168.c (revision 6a98f8dc3f2c9149905a87dc2e9d0fedaf64e09a)
1 
2 static char help[] = "Tests external Clique direct solvers. Simplified from ex130.c\n\
3 Example: mpiexec -n <np> ./ex168 -f <matrix binary file> \n\n";
4 
5 #include <petscmat.h>
6 
7 int main(int argc,char **args)
8 {
9   Mat            A,F;
10   Vec            u,x,b;
11   PetscErrorCode ierr;
12   PetscMPIInt    rank,size;
13   PetscInt       m,n,nfact;
14   PetscReal      norm,tol=1.e-12,Anorm;
15   IS             perm,iperm;
16   MatFactorInfo  info;
17   PetscBool      flg,testMatSolve=PETSC_TRUE;
18   PetscViewer    fd;              /* viewer */
19   char           file[PETSC_MAX_PATH_LEN]; /* input file name */
20 
21   ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
22   ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr);
23   ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size);CHKERRQ(ierr);
24 
25   /* Determine file from which we read the matrix A */
26   ierr = PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
27   if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f option");
28 
29   /* Load matrix A */
30   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr);
31   ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
32   ierr = MatLoad(A,fd);CHKERRQ(ierr);
33   ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr);
34   ierr = VecLoad(b,fd);CHKERRQ(ierr);
35   ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr);
36   ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr);
37   if (m != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ, "This example is not intended for rectangular matrices (%d, %d)", m, n);
38   ierr = MatNorm(A,NORM_INFINITY,&Anorm);CHKERRQ(ierr);
39 
40   /* Create vectors */
41   ierr = VecDuplicate(b,&x);CHKERRQ(ierr);
42   ierr = VecDuplicate(x,&u);CHKERRQ(ierr); /* save the true solution */
43 
44   /* Test Cholesky Factorization */
45   ierr = MatGetOrdering(A,MATORDERINGNATURAL,&perm,&iperm);CHKERRQ(ierr);
46 
47   if (!rank) printf(" Clique Cholesky:\n");
48   ierr = MatGetFactor(A,MATSOLVERCLIQUE,MAT_FACTOR_CHOLESKY,&F);CHKERRQ(ierr);
49 
50   info.fill = 5.0;
51   ierr      = MatCholeskyFactorSymbolic(F,A,perm,&info);CHKERRQ(ierr);
52 
53   for (nfact = 0; nfact < 1; nfact++) {
54     if (!rank) printf(" %d-the Cholesky numfactorization \n",nfact);
55     ierr = MatCholeskyFactorNumeric(F,A,&info);CHKERRQ(ierr);
56 
57     /* Test MatSolve() */
58     if (testMatSolve && nfact == 2) {
59       ierr = MatSolve(F,b,x);CHKERRQ(ierr);
60 
61       /* Check the residual */
62       ierr = MatMult(A,x,u);CHKERRQ(ierr);
63       ierr = VecAXPY(u,-1.0,b);CHKERRQ(ierr);
64       ierr = VecNorm(u,NORM_INFINITY,&norm);CHKERRQ(ierr);
65       /* if (norm > tol) { */
66       if (!rank) {
67         ierr = PetscPrintf(PETSC_COMM_SELF,"MatSolve: rel residual %g/%g = %g, LU numfact %d\n",norm,Anorm,norm/Anorm,nfact);CHKERRQ(ierr);
68       }
69       /*} */
70     }
71   }
72 
73   /* Free data structures */
74   ierr = MatDestroy(&A);CHKERRQ(ierr);
75   ierr = MatDestroy(&F);CHKERRQ(ierr);
76   ierr = ISDestroy(&perm);CHKERRQ(ierr);
77   ierr = ISDestroy(&iperm);CHKERRQ(ierr);
78   ierr = VecDestroy(&x);CHKERRQ(ierr);
79   ierr = VecDestroy(&b);CHKERRQ(ierr);
80   ierr = VecDestroy(&u);CHKERRQ(ierr);
81   ierr = PetscFinalize();
82   return ierr;
83 }
84