Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_belos_epetra_gcrodr.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stratimikos: Thyra-based strategies for linear solvers
4 //
5 // Copyright 2006 NTESS and the Stratimikos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include <Teuchos_ConfigDefs.hpp>
12 #include <Teuchos_RCP.hpp>
15 
16 #include <string>
17 #include <iostream>
18 
19 #ifdef HAVE_MPI
20  #include "Epetra_MpiComm.h"
21 #else
22  #include "Epetra_SerialComm.h"
23 #endif
24 
25 #include "Epetra_Map.h"
26 #include "Epetra_CrsMatrix.h"
27 #include "Epetra_Vector.h"
28 // #include "EpetraExt_RowMatrixOut.h"
29 
30 #include "Thyra_LinearOpBase.hpp"
31 #include "Thyra_VectorBase.hpp"
33 #include "Thyra_EpetraLinearOp.hpp"
34 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
35 #include "Thyra_LinearOpWithSolveHelpers.hpp"
36 #include "Thyra_DefaultZeroLinearOp.hpp"
37 #include "Thyra_DefaultProductVector.hpp"
38 #include "Thyra_DefaultProductVectorSpace.hpp"
39 #include "Thyra_MultiVectorStdOps.hpp"
40 #include "Thyra_VectorStdOps.hpp"
41 #include "Thyra_DefaultBlockedLinearOp.hpp"
42 
44 
45 using Teuchos::RCP;
46 using Teuchos::rcp;
47 
48 Teuchos::RCP<Epetra_CrsMatrix> buildMatrix(int nx, Epetra_Comm & comm)
49 {
50  Epetra_Map map(nx*comm.NumProc(),0,comm);
51  Teuchos::RCP<Epetra_CrsMatrix> mat = Teuchos::rcp(new Epetra_CrsMatrix(Copy,map,3));
52 
53  int offsets[3] = {-1, 0, 1 };
54  double values[3] = { -1, 2, -1};
55  int maxGid = map.MaxAllGID();
56  for(int lid=0;lid<nx;lid++) {
57  int gid = mat->GRID(lid);
58  int numEntries = 3, offset = 0;
59  int indices[3] = { gid+offsets[0],
60  gid+offsets[1],
61  gid+offsets[2] };
62  if(gid==0) { // left end point
63  numEntries = 2;
64  offset = 1;
65  } // right end point
66  else if(gid==maxGid)
67  numEntries = 2;
68 
69  // insert rows
70  mat->InsertGlobalValues(gid,numEntries,values+offset,indices+offset);
71  }
72 
73  mat->FillComplete();
74  return mat;
75 }
76 
77 TEUCHOS_UNIT_TEST(belos_gcrodr, multiple_solves)
78 {
79  // build global (or serial communicator)
80  #ifdef HAVE_MPI
81  Epetra_MpiComm Comm(MPI_COMM_WORLD);
82  #else
83  Epetra_SerialComm Comm;
84  #endif
85 
86  // build and allocate linear system
88  Teuchos::RCP<Epetra_Vector> x0 = rcp(new Epetra_Vector(mat->OperatorDomainMap()));
89  Teuchos::RCP<Epetra_Vector> x1 = rcp(new Epetra_Vector(mat->OperatorDomainMap()));
90  Teuchos::RCP<Epetra_Vector> b = rcp(new Epetra_Vector(mat->OperatorRangeMap()));
91 
92  x0->Random();
93  x1->Random();
94  b->PutScalar(0.0);
95 
96  // sanity check
97  // EpetraExt::RowMatrixToMatrixMarketFile("mat_output.mm",*mat);
98 
99  // build Thyra wrappers
101  tA = Thyra::epetraLinearOp( mat );
103  tx0 = Thyra::create_Vector( x0, tA->domain() );
105  tx1 = Thyra::create_Vector( x1, tA->domain() );
107  tb = Thyra::create_Vector( b, tA->range() );
108 
109  // now comes Stratimikos
110  RCP<Teuchos::ParameterList> paramList = Teuchos::getParametersFromXmlFile("BelosGCRODRTest.xml");
111  Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
112  linearSolverBuilder.setParameterList(paramList);
113 
114  // Create a linear solver factory given information read from the
115  // parameter list.
117  linearSolverBuilder.createLinearSolveStrategy("");
118 
119  // Create a linear solver based on the forward operator A
121  Thyra::linearOpWithSolve(*lowsFactory, tA);
122 
123  // Solve the linear system
124  Thyra::SolveStatus<double> status;
125  status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *tb, tx0.ptr());
126  status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *tb, tx1.ptr());
127 }
128 
129 TEUCHOS_UNIT_TEST(belos_gcrodr, 2x2_multiple_solves)
130 {
131  // build global (or serial communicator)
132  #ifdef HAVE_MPI
133  Epetra_MpiComm Comm(MPI_COMM_WORLD);
134  #else
135  Epetra_SerialComm Comm;
136  #endif
137 
138  // build and allocate linear system
140  Teuchos::RCP<Epetra_Vector> b = rcp(new Epetra_Vector(mat->OperatorRangeMap()));
141 
142  b->PutScalar(0.0);
143 
144  // sanity check
145  // EpetraExt::RowMatrixToMatrixMarketFile("mat_output.mm",*mat);
146 
147  // build Thyra wrappers
150  {
151  // build blocked linear Op
153  = Thyra::epetraLinearOp( mat );
155  = Thyra::zero(tA_sub->range(),tA_sub->domain());
156 
157  tA = Thyra::block2x2(tA_sub,zero,zero,tA_sub);
158 
159  // build blocked vector
161  = Thyra::create_Vector( b, tA_sub->range() );
162 
163  RCP<Thyra::VectorBase<double> > tb_m = Thyra::createMember(tA->range());
164  Thyra::randomize(-1.0,1.0,tb_m.ptr());
165 
166  tb = tb_m;
167  }
170  {
171  tx0 = Thyra::createMember(tA->domain());
172  tx1 = Thyra::createMember(tA->domain());
173 
174  Thyra::randomize(-1.0,1.0,tx0.ptr());
175  Thyra::randomize(-1.0,1.0,tx1.ptr());
176  }
177 
178  // now comes Stratimikos
179  RCP<Teuchos::ParameterList> paramList = Teuchos::getParametersFromXmlFile("BelosGCRODRTest.xml");
180  Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
181  linearSolverBuilder.setParameterList(paramList);
182 
183  // Create a linear solver factory given information read from the
184  // parameter list.
186  linearSolverBuilder.createLinearSolveStrategy("");
187 
188  // Create a linear solver based on the forward operator A
190  Thyra::linearOpWithSolve(*lowsFactory, tA);
191 
192  // Solve the linear system
193  Thyra::SolveStatus<double> status;
194  status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *tb, tx0.ptr());
195  status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *tb, tx1.ptr());
196 }
TEUCHOS_UNIT_TEST(belos_gcrodr, multiple_solves)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setParameterList(RCP< ParameterList > const &paramList)
RCP< VectorBase< double > > create_Vector(const RCP< Epetra_Vector > &epetra_v, const RCP< const VectorSpaceBase< double > > &space=Teuchos::null)
Ptr< T > ptr() const
RCP< Thyra::LinearOpWithSolveFactoryBase< Scalar > > createLinearSolveStrategy(const std::string &linearSolveStrategyName) const
Teuchos::RCP< Epetra_CrsMatrix > buildMatrix(int nx, Epetra_Comm &comm)
Concrete subclass of Thyra::LinearSolverBuilderBase for creating Thyra::LinearOpWithSolveFactoryBase ...