// @HEADER // ***************************************************************************** // Trilinos: An Object-Oriented Solver Framework // // Copyright 2001-2024 NTESS and the Trilinos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include "ml_config.h" #include "EpetraExt_config.h" #if defined(HAVE_PETSC) && defined(HAVE_ML_EPETRA) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_AZTECOO) && defined(HAVE_MPI) #include "petscksp.h" #include "EpetraExt_PETScAIJMatrix.h" #include "Epetra_Vector.h" #include "Epetra_Map.h" #include "ml_MultiLevelPreconditioner.h" #include "AztecOO.h" #include "Epetra_LinearProblem.h" /* This example demonstrates how to apply a Trilinos preconditioner to a PETSc linear system. For information on configuring and building Trilinos with the PETSc aij interface enabled, please see EpetraExt's doxygen documentation at http://trilinos.sandia.gov/packages/epetraext, development version or release 9.0 or later. The PETSc matrix is a 2D 5-point Laplace operator stored in AIJ format. This matrix is wrapped as an Epetra_PETScAIJMatrix, and an ML AMG preconditioner is created for it. The associated linear system is solved twice, the first time using AztecOO's preconditioned CG, the second time using PETSc's. To invoke this example, use something like: mpirun -np 5 ./TrilinosCouplings_petsc.exe -m 150 -n 150 -petsc_smoother -ksp_monitor_true_residual */ static char help[] = "Demonstrates how to solve a PETSc linear system with KSP\ and a Trilinos AMG preconditioner. In particular, it shows how to wrap a PETSc\ AIJ matrix as an Epetra matrix, create the AMG preconditioner, and wrap it as a\ shell preconditioner for a PETSc Krylov method.\ Input parameters include:\n\ -random_exact_sol : use a random exact solution vector\n\ -view_exact_sol : write exact solution vector to stdout\n\ -m : number of mesh points in x-direction\n\ -n : number of mesh points in y-direction\n\n"; #if (PETSC_VERSION_MAJOR < 3) || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 1) extern PetscErrorCode ShellApplyML(void*,Vec,Vec); #else extern PetscErrorCode ShellApplyML(PC,Vec,Vec); #endif #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **args) { Vec x,b,u; /* approx solution, RHS, exact solution */ Mat A; /* linear system matrix */ KSP ksp; /* linear solver context */ KSP kspSmoother=0; /* solver context for PETSc fine grid smoother */ PC pc; PetscRandom rctx; /* random number generator context */ PetscReal norm; /* norm of solution error */ PetscInt i,j,Ii,J,Istart,Iend,its; PetscInt m = 50,n = 50; /* #mesh points in x & y directions, resp. */ PetscErrorCode ierr; PetscBool flg; PetscScalar v,one = 1.0,neg_one = -1.0; PetscInt rank=0; MPI_Comm comm; PetscInitialize(&argc,&args,(char *)0,help); ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);CHKERRQ(ierr); ierr = MatSetType(A, MATAIJ);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(A,5,PETSC_NULL,5,PETSC_NULL);CHKERRQ(ierr); ierr = MatSetUp(A);CHKERRQ(ierr); PetscObjectGetComm( (PetscObject)A, &comm); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) printf("Matrix has %d (%dx%d) rows\n",m*n,m,n); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii0) {J = Ii - n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i0) {J = Ii - 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j(xvals); Epetra_Vector epx(View,mlp->OperatorDomainMap(),nonconst_xvals); ierr = VecGetArray(y,&yvals);CHKERRQ(ierr); Epetra_Vector epy(View,mlp->OperatorRangeMap(),yvals); /* Apply ML. */ mlp->ApplyInverse(epx,epy); /* Clean up and return. */ ierr = VecRestoreArrayRead(x,&xvals);CHKERRQ(ierr); ierr = VecRestoreArray(y,&yvals);CHKERRQ(ierr); return 0; } /*ShellApplyML*/ #else #include #include #ifdef HAVE_MPI #include "mpi.h" #endif int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc,&argv); #endif printf("Please configure EpetraExt with:"); #if !defined(HAVE_PETSC) printf("--enable-petsc"); #endif #if !defined(HAVE_ML_EPETRA) printf("--enable-epetra"); #endif #if !defined(HAVE_ML_TEUCHOS) printf("--enable-teuchos"); #endif #if !defined(HAVE_ML_AZTECOO) printf("--enable-aztecoo"); #endif #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); } #endif