int main(int argc, char *argv[]) { #ifdef EPETRA_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // Creates the linear problem using the Galeri package. // Various matrix examples are supported; please refer to the // Galeri documentation for more details. // This matrix is a simple VBR matrix, constructed by replicating // a point-matrix on each unknown. This example is // useful to test the vector capabilities of ML, or to debug // code for vector problems. The number of equations is here // hardwired as 5, but any positive number (including 1) can be // specified. // // NOTE: The epetra interface of ML has only limited capabilites // for matrices with variable block size (that is, // best is if the number of equations for // each block row is constant). If you are interested in variable // block capabilites, please contact the ML developers. int NumPDEEqns = 5; // build up a 9-point Laplacian in 2D. This stencil will lead to // "perfect" aggregates, of square shape, using almost all the ML // aggregation schemes. // The problem size (900) must be a square number. Otherwise, the user // can specify the number of points in the x- and y-direction, and the // length of the x- and y-side of the computational domain. Please // refer to the Trilinos tutorial for more details. // // Note also that this gallery matrix have no boundary nodes. int nx; if (argc > 1) nx = (int) strtol(argv[1],NULL,10); else nx = 16; Teuchos::ParameterList GaleriList; GaleriList.set("nx", nx); GaleriList.set("ny", nx * Comm.NumProc()); GaleriList.set("mx", 1); GaleriList.set("my", Comm.NumProc()); Epetra_Map* Map = CreateMap("Cartesian2D", Comm, GaleriList); Epetra_CrsMatrix* CrsA = CreateCrsMatrix("Star2D", Map, GaleriList); Epetra_VbrMatrix* A = CreateVbrMatrix(CrsA, NumPDEEqns); Epetra_Vector LHS(A->Map()); LHS.Random(); Epetra_Vector RHS(A->Map()); RHS.PutScalar(0.0); Epetra_LinearProblem Problem(A, &LHS, &RHS); // Construct a solver object for this problem AztecOO solver(Problem); // =========================== begin of ML part =========================== // create a parameter list for ML options ParameterList MLList; // set defaults for classic smoothed aggregation ML_Epetra::SetDefaults("SA",MLList); // overwrite some parameters. Please refer to the user's guide // for more information // some of the parameters do not differ from their default value, // and they are here reported for the sake of clarity // maximum number of levels MLList.set("max levels",5); MLList.set("increasing or decreasing","increasing"); // set different aggregation schemes for each level. Depending on the // size of your problem, the hierarchy will contain different number // of levels. As `Uncoupled' and `METIS' are local aggregation // schemes, they should be used only for the finest level. `MIS' and // `ParMETIS' are global aggregation schemes (meaning that the // aggregates can span across processes), and should be reserved for // coarsest levels. // Note also that `Uncoupled' and `MIS' will always try to create // aggregates of diameter 3 (in the graph sense), while `METIS' and // `ParMETIS' can generate aggregates of any size. MLList.set("aggregation: type (level 0)", "Uncoupled"); MLList.set("aggregation: type (level 1)", "MIS"); // this is recognized by `METIS' and `ParMETIS' only MLList.set("aggregation: nodes per aggregate", 9); // smoother is Gauss-Seidel. Example file // ml_2level_DD.cpp shows how to use // AZTEC's preconditioners as smoothers MLList.set("smoother: type","Gauss-Seidel"); // use both pre and post smoothing. Non-symmetric problems may take // advantage of pre-smoothing or post-smoothing only. MLList.set("smoother: pre or post", "both"); // solve with serial direct solver KLU MLList.set("coarse: type","Amesos-KLU"); // create the preconditioner object and compute hierarchy ML_Epetra::MultiLevelPreconditioner * MLPrec = new ML_Epetra::MultiLevelPreconditioner(*A, MLList); // tell AztecOO to use this preconditioner, then solve solver.SetPrecOperator(MLPrec); // =========================== end of ML part ============================= solver.SetAztecOption(AZ_solver, AZ_cg_condnum); solver.SetAztecOption(AZ_output, 32); // solve with 500 iterations and 1e-5 tolerance solver.Iterate(500, 1e-7); delete MLPrec; // compute the real residual. double residual; LHS.Norm2(&residual); if (Comm.MyPID() == 0) { cout << "||b-Ax||_2 = " << residual << endl; } if (residual > 1e-3) exit(EXIT_FAILURE); delete A; delete CrsA; delete Map; #ifdef EPETRA_MPI MPI_Finalize() ; #endif return(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // Create the linear problem using the Galeri package. int NumPDEEqns = 5; Teuchos::ParameterList GaleriList; int nx = 32; GaleriList.set("nx", nx); GaleriList.set("ny", nx * Comm.NumProc()); GaleriList.set("mx", 1); GaleriList.set("my", Comm.NumProc()); Epetra_Map* Map = CreateMap("Cartesian2D", Comm, GaleriList); Epetra_CrsMatrix* CrsA = CreateCrsMatrix("Laplace2D", Map, GaleriList); Epetra_VbrMatrix* A = CreateVbrMatrix(CrsA, NumPDEEqns); Epetra_Vector LHS(A->Map()); LHS.Random(); Epetra_Vector RHS(A->Map()); RHS.PutScalar(0.0); Epetra_LinearProblem Problem(A, &LHS, &RHS); AztecOO solver(Problem); // =========================== definition of coordinates ================= // use the following Galeri function to get the // coordinates for a Cartesian grid. Note however that the // visualization capabilites of Trilinos accept non-structured grid as // well. Visualization and statistics occurs just after the ML // preconditioner has been build. Epetra_MultiVector* Coord = CreateCartesianCoordinates("2D", &(A->Map()), GaleriList); double* x_coord = (*Coord)[0]; double* y_coord = (*Coord)[1]; // =========================== begin of ML part =========================== // create a parameter list for ML options ParameterList MLList; int *options = new int[AZ_OPTIONS_SIZE]; double *params = new double[AZ_PARAMS_SIZE]; // set defaults ML_Epetra::SetDefaults("SA",MLList, options, params); // overwrite some parameters. Please refer to the user's guide // for more information // some of the parameters do not differ from their default value, // and they are here reported for the sake of clarity // maximum number of levels MLList.set("max levels",3); MLList.set("increasing or decreasing","increasing"); MLList.set("smoother: type", "symmetric Gauss-Seidel"); // aggregation scheme set to Uncoupled. Note that the aggregates // created by MIS can be visualized for serial runs only, while // Uncoupled, METIS for both serial and parallel runs. MLList.set("aggregation: type", "Uncoupled"); // ======================== // // visualization parameters // // ======================== // // // - set "viz: enable" to `false' to disable visualization and // statistics. // - set "x-coordinates" to the pointer of x-coor // - set "viz: equation to plot" to the number of equation to // be plotted (for vector problems only). Default is -1 (that is, // plot all the equations) // - set "viz: print starting solution" to print on file // the starting solution vector, that was used for pre- // and post-smoothing, and for the cycle. This may help to // understand whether the smoothed solution is "smooth" // or not. // // NOTE: visualization occurs *after* the creation of the ML preconditioner, // by calling VisualizeAggregates(), VisualizeSmoothers(), and // VisualizeCycle(). However, the user *must* enable visualization // *before* creating the ML object. This is because ML must store some // additional information about the aggregates. // // NOTE: the options above work only for "viz: output format" == "xyz" // (default value) or "viz: output format" == "vtk". // If "viz: output format" == "dx", the user // can only plot the aggregates. MLList.set("viz: output format", "vtk"); MLList.set("viz: enable", true); MLList.set("x-coordinates", x_coord); MLList.set("y-coordinates", y_coord); MLList.set("z-coordinates", (double *)0); MLList.set("viz: print starting solution", true); // =============================== // // end of visualization parameters // // =============================== // // create the preconditioner object and compute hierarchy ML_Epetra::MultiLevelPreconditioner * MLPrec = new ML_Epetra::MultiLevelPreconditioner(*A, MLList); // ============= // // visualization // // ============= // // 1.- print out the shape of the aggregates, plus some // statistics // 2.- print out the effect of presmoother and postsmoother // on a random vector. Input integer number represent // the number of applications of presmoother and postmsoother, // respectively // 3.- print out the effect of the ML cycle on a random vector. // The integer parameter represents the number of cycles. // Below, `5' and `1' refers to the number of pre-smoother and // post-smoother applications. `10' refers to the number of ML // cycle applications. In both cases, smoothers and ML cycle are // applied to a random vector. MLPrec->VisualizeAggregates(); MLPrec->VisualizeSmoothers(5,1); MLPrec->VisualizeCycle(10); // ==================== // // end of visualization // // ==================== // // destroy the preconditioner delete MLPrec; delete [] options; delete [] params; delete A; delete Coord; delete Map; #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); }