Trilinos based (stochastic) FEM solvers
main.cpp
Go to the documentation of this file.
1 #include "Epetra_ConfigDefs.h"
2 #ifdef HAVE_MPI
3 #include "mpi.h"
4 #include "Epetra_MpiComm.h"
5 #else
6 #include "Epetra_SerialComm.h"
7 #endif
8 
9 #include <fstream>
10 #include "meshpp.hpp"
11 #include "laplacepp.hpp"
12 #include "Teuchos_CommandLineProcessor.hpp"
13 #include "Teuchos_StandardCatchMacros.hpp"
14 #include "Teuchos_ParameterList.hpp"
15 #include "Teuchos_XMLParameterListCoreHelpers.hpp"
16 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
17 
18 int main(int argc, char *argv[]){
19 
20  std::string xmlInFileName = "";
21  std::string extraXmlFile = "";
22  std::string xmlOutFileName = "paramList.out";
23 
24  Teuchos::CommandLineProcessor clp(false);
25  clp.setOption("xml-in-file",&xmlInFileName,"The XML file to read into a parameter list");
26  clp.setOption("xml-out-file",&xmlOutFileName,"The XML file to write the final parameter list to");
27  clp.setDocString(
28  "This example program shows how to read in a parameter list from an"
29  " XML file (given by --xml-in-file=xmlInFileName)."
30  " The final parameter list is then written back to an XML file."
31  " (given by --xml-out-file=xmlOutFileName)."
32  "This program also shows how to use Stratikimos for a"
33  " sequence of two linear systems."
34  );
35  Teuchos::CommandLineProcessor::EParseCommandLineReturn
36  parse_return = clp.parse(argc,argv);
37  if( parse_return != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL ) {
38  std::cout << "\nEnd Result: TEST FAILED" << std::endl;
39  return parse_return;
40  }
41 
42 #ifdef HAVE_MPI
43  MPI_Init(&argc, &argv);
44  Epetra_MpiComm Comm(MPI_COMM_WORLD);
45 #else
46  Epetra_SerialComm Comm;
47 #endif
48 
49  Teuchos::RCP<Teuchos::ParameterList> paramList = Teuchos::rcp(new Teuchos::ParameterList);
50  if(xmlInFileName.length()) {
51  Teuchos::updateParametersFromXmlFile(xmlInFileName, inoutArg(*paramList));
52  }
53 
54  Teuchos::RCP<Teuchos::ParameterList> solverBuilderSL = Teuchos::sublist(paramList,"Linear Solver Builder",true);
55 
56  Teuchos::RCP<laplace> Laplace = Teuchos::rcp(new laplace(Comm,paramList->sublist("Mesh")));
57 
58  Teuchos::RCP<Epetra_FECrsMatrix> matrix = Teuchos::rcp(new Epetra_FECrsMatrix(Copy,*Laplace->FEGraph));
59  Teuchos::RCP<Epetra_Vector> lhs = Teuchos::rcp(new Epetra_Vector(*Laplace->StandardMap));
60  Teuchos::RCP<Epetra_FEVector> rhs = Teuchos::rcp(new Epetra_FEVector(*Laplace->StandardMap));
61  Teuchos::RCP<Epetra_MultiVector> rhsv = rhs;
62 
63  Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
64  linearSolverBuilder.setParameterList(solverBuilderSL);
65 
66  Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> > lowsFactory =
67  linearSolverBuilder.createLinearSolveStrategy("");
68  Teuchos::RCP<Thyra::LinearOpWithSolveBase<double> > lows = lowsFactory->createOp();
69 
70  Thyra::SolveStatus<double> status;
71 
72  Teuchos::RCP<const Thyra::LinearOpBase<double>> A = Thyra::epetraLinearOp(matrix);
73  Teuchos::RCP<Thyra::VectorBase<double>> x = Thyra::create_Vector(lhs,A->domain());
74  Teuchos::RCP<const Thyra::MultiVectorBase<double>> b = Thyra::create_MultiVector(rhsv,A->range());
75 
76  int bc_indx[2];
77  double bc_val[2];
78  bc_indx[0] = 2; bc_indx[1] = 3;
79  bc_val[0] = 0.0; bc_val[1] = 1.0;
80 
81  for (unsigned iter=0; iter<5; ++iter){
82  Laplace->assembling_OAZ(*matrix, *rhs, &bc_indx[0], &bc_val[0]);
83  rhsv = rhs;
84  lhs->PutScalar(0.0);
85 
86  Thyra::initializeOp<double>(*lowsFactory, A, lows.ptr());
87  status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *b, x.ptr());
88  if (status.solveStatus){
89  if (Comm.MyPID()==0){
90  std::cout << "STRATIMIKOS FAILED TO CONVERGE.\n";
91  std::cout << status.message << "\n";
92  }
93  }
94  std::string name = "belos_test_" + std::to_string(iter) + ".mtx";
95  int error = Laplace->print_solution(*lhs,name);
96  }
97 
98  /*//if minor changes and reuse factorization information
99  Thyra::uninitializeOp<double>(*lowsFactory, lows.ptr());
100  Laplace->assembling_OAZ(*matrix, *rhs, &bc_indx[0], &bc_val[0]);
101  rhsv = rhs;
102  lhs->PutScalar(0.0);
103 
104  Thyra::initializeAndReuseOp<double>(*lowsFactory, A, lows.ptr());
105  status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *b, x.ptr());
106  if (status.solveStatus){
107  if (Comm.MyPID()==0){
108  std::cout << "STRATIMIKOS FAILED TO CONVERGE.\n";
109  std::cout << status.message << "\n";
110  }
111  }
112  error = Laplace->print_solution(*lhs,"belos_test_2.mtx");
113 
114  //if major changes
115  bc_indx[0] = 0; bc_indx[1] = 1;
116  Thyra::uninitializeOp<double>(*lowsFactory, lows.ptr());
117  Laplace->assembling_OAZ(*matrix, *rhs, &bc_indx[0], &bc_val[0]);
118  rhsv = rhs;
119  Thyra::initializeOp<double>(*lowsFactory, A, lows.ptr());
120  lhs->PutScalar(0.0);
121  status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *b, x.ptr());
122 
123  if (status.solveStatus){
124  if (Comm.MyPID()==0){
125  std::cout << "STRATIMIKOS FAILED TO CONVERGE.\n";
126  std::cout << status.message << "\n";
127  }
128  }
129  error = Laplace->print_solution(*lhs,"belos_test_3.mtx");*/
130 
131 #ifdef HAVE_MPI
132  MPI_Finalize();
133 #endif
134 
135  return 0;
136 }
int main(int argc, char *argv[])
Definition: main.cpp:23