Debian Codegraph @

packages/shylu/shylu_dd/frosch/test/Thyra_Xpetra_Elasticity/main.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // ShyLU: Scalable Hybrid LU Preconditioner and Solver
4 //
5 // Copyright 2011 NTESS and the ShyLU contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include <ShyLU_DDFROSch_config.h>
11
12 #include <mpi.h>
13
14 #include <Teuchos_GlobalMPISession.hpp>
15 #include <Teuchos_CommandLineProcessor.hpp>
16 #include <Teuchos_XMLParameterListCoreHelpers.hpp>
17 #include <Teuchos_StackedTimer.hpp>
18
19 // Galeri::Xpetra
20 #include "Galeri_XpetraProblemFactory.hpp"
21 #include "Galeri_XpetraMatrixTypes.hpp"
22 #include "Galeri_XpetraParameters.hpp"
23 #include "Galeri_XpetraUtils.hpp"
24 #include "Galeri_XpetraMaps.hpp"
25
26 // Thyra includes
27 #include <Thyra_LinearOpWithSolveBase.hpp>
28 #include <Thyra_VectorBase.hpp>
29 #include <Thyra_SolveSupportTypes.hpp>
30 #include <Thyra_LinearOpWithSolveBase.hpp>
31 #include <Thyra_LinearOpWithSolveFactoryHelpers.hpp>
32 #include <Thyra_TpetraLinearOp.hpp>
33 #include <Thyra_TpetraMultiVector.hpp>
34 #include <Thyra_TpetraVector.hpp>
35 #include <Thyra_TpetraThyraWrappers.hpp>
36 #include <Thyra_VectorBase.hpp>
37 #include <Thyra_VectorStdOps.hpp>
38 #ifdef HAVE_SHYLU_DDFROSCH_EPETRA
39 #include <Thyra_EpetraLinearOp.hpp>
40 #endif
41 #include <Thyra_VectorSpaceBase_def.hpp>
42 #include <Thyra_VectorSpaceBase_decl.hpp>
43
44 // Stratimikos includes
45 #include <Stratimikos_FROSch_def.hpp>
46
47 #include <Tpetra_Core.hpp>
48
49 // Xpetra include
50 #include <Xpetra_CrsMatrixWrap.hpp>
51 #include <Xpetra_DefaultPlatform.hpp>
52 #ifdef HAVE_SHYLU_DDFROSCH_EPETRA
53 #include <Xpetra_EpetraCrsMatrix.hpp>
54 #endif
55 #include <Xpetra_Parameters.hpp>
56
57 // FROSCH thyra includes
58 #include "Thyra_FROSchLinearOp_def.hpp"
59 #include "Thyra_FROSchFactory_def.hpp"
60 #include <FROSch_Tools_def.hpp>
61
62
63 using UN = unsigned;
64 using SC = double;
65 using LO = int;
66 using GO = FROSch::DefaultGlobalOrdinal;
67 using NO = Tpetra::KokkosClassic::DefaultNode::DefaultNodeType;
68
69 using namespace std;
70 using namespace Teuchos;
71 using namespace Xpetra;
72 using namespace FROSch;
73 using namespace Thyra;
74
75 int main(int argc, char *argv[])
76 {
77 oblackholestream blackhole;
78 GlobalMPISession mpiSession(&argc,&argv,&blackhole);
79
80 RCP<const Comm<int> > CommWorld = DefaultPlatform::getDefaultPlatform().getComm();
81
82 CommandLineProcessor My_CLP;
83
84 RCP<FancyOStream> out = VerboseObjectBase::getDefaultOStream();
85
86 int M = 3;
87 My_CLP.setOption("M",&M,"H / h.");
88 int Dimension = 2;
89 My_CLP.setOption("DIM",&Dimension,"Dimension.");
90 int Overlap = 0;
91 My_CLP.setOption("O",&Overlap,"Overlap.");
92 string xmlFile = "ParameterList.xml";
93 My_CLP.setOption("PLIST",&xmlFile,"File name of the parameter list.");
94 bool useepetra = false;
95 My_CLP.setOption("USEEPETRA","USETPETRA",&useepetra,"Use Epetra infrastructure for the linear algebra.");
96 bool useGeoMap = false;
97 My_CLP.setOption("useGeoMap","useAlgMap",&useGeoMap,"Use Geometric Map");
98 My_CLP.recogniseAllOptions(true);
99 My_CLP.throwExceptions(false);
100 CommandLineProcessor::EParseCommandLineReturn parseReturn = My_CLP.parse(argc,argv);
101 if (parseReturn == CommandLineProcessor::PARSE_HELP_PRINTED) {
102 return(EXIT_SUCCESS);
103 }
104
105 CommWorld->barrier();
106 RCP<StackedTimer> stackedTimer = rcp(new StackedTimer("Thyra Elasticity Test"));
107 TimeMonitor::setStackedTimer(stackedTimer);
108
109 int N = 0;
110 int color=1;
111 if (Dimension == 2) {
112 N = (int) (pow(CommWorld->getSize(),1/2.) + 100*numeric_limits<double>::epsilon()); // 1/H
113 if (CommWorld->getRank()<N*N) {
114 color=0;
115 }
116 } else if (Dimension == 3) {
117 N = (int) (pow(CommWorld->getSize(),1/3.) + 100*numeric_limits<double>::epsilon()); // 1/H
118 if (CommWorld->getRank()<N*N*N) {
119 color=0;
120 }
121 } else {
122 assert(false);
123 }
124
125 UnderlyingLib xpetraLib = UseTpetra;
126 if (useepetra) {
127 xpetraLib = UseEpetra;
128 } else {
129 xpetraLib = UseTpetra;
130 }
131
132 RCP<const Comm<int> > Comm = CommWorld->split(color,CommWorld->getRank());
133
134 if (color==0) {
135
136 RCP<ParameterList> parameterList = getParametersFromXmlFile(xmlFile);
137
138 Comm->barrier();
139 if (Comm->getRank()==0) {
140 cout << "##################\n# Parameter List #\n##################" << endl;
141 parameterList->print(cout);
142 cout << endl;
143 }
144
145 Comm->barrier(); if (Comm->getRank()==0) cout << "##############################\n# Assembly Laplacian #\n##############################\n" << endl;
146
147 ParameterList GaleriList;
148 GaleriList.set("nx", GO(N*M));
149 GaleriList.set("ny", GO(N*M));
150 GaleriList.set("nz", GO(N*M));
151 GaleriList.set("mx", GO(N));
152 GaleriList.set("my", GO(N));
153 GaleriList.set("mz", GO(N));
154
155 RCP<const Map<LO,GO,NO> > UniqueNodeMap;
156 RCP<const Map<LO,GO,NO> > UniqueMap;
157 RCP<MultiVector<SC,LO,GO,NO> > Coordinates;
158 RCP<Matrix<SC,LO,GO,NO> > K;
159 if (Dimension==2) {
160 UniqueNodeMap = Galeri::Xpetra::CreateMap<LO,GO,NO>(xpetraLib,"Cartesian2D",Comm,GaleriList); // RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); nodeMap->describe(*fancy,VERB_EXTREME);
161 UniqueMap = Xpetra::MapFactory<LO,GO,NO>::Build(UniqueNodeMap,2);
162 Coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<SC,LO,GO,Map<LO,GO,NO>,MultiVector<SC,LO,GO,NO> >("2D",UniqueMap,GaleriList);
163 RCP<Galeri::Xpetra::Problem<Map<LO,GO,NO>,CrsMatrixWrap<SC,LO,GO,NO>,MultiVector<SC,LO,GO,NO> > > Problem = Galeri::Xpetra::BuildProblem<SC,LO,GO,Map<LO,GO,NO>,CrsMatrixWrap<SC,LO,GO,NO>,MultiVector<SC,LO,GO,NO> >("Elasticity2D",UniqueMap,GaleriList);
164 K = Problem->BuildMatrix();
165 } else if (Dimension==3) {
166 UniqueNodeMap = Galeri::Xpetra::CreateMap<LO,GO,NO>(xpetraLib,"Cartesian3D",Comm,GaleriList); // RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); nodeMap->describe(*fancy,VERB_EXTREME);
167 UniqueMap = Xpetra::MapFactory<LO,GO,NO>::Build(UniqueNodeMap,3);
168 Coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<SC,LO,GO,Map<LO,GO,NO>,MultiVector<SC,LO,GO,NO> >("3D",UniqueMap,GaleriList);
169 RCP<Galeri::Xpetra::Problem<Map<LO,GO,NO>,CrsMatrixWrap<SC,LO,GO,NO>,MultiVector<SC,LO,GO,NO> > > Problem = Galeri::Xpetra::BuildProblem<SC,LO,GO,Map<LO,GO,NO>,CrsMatrixWrap<SC,LO,GO,NO>,MultiVector<SC,LO,GO,NO> >("Elasticity3D",UniqueMap,GaleriList);
170 K = Problem->BuildMatrix();
171 }
172
173
174 RCP<Map<LO,GO,NO> > FullRepeatedMap;
175 RCP<Map<LO,GO,NO> > RepeatedMap;
176 RCP<const Map<LO,GO,NO> > FullRepeatedMapNode;
177 if (useGeoMap) {
178 if (Dimension == 2) {
179 FullRepeatedMap = BuildRepeatedMapGaleriStruct2D<SC,LO,GO,NO>(K,M,Dimension);
180 RepeatedMap = FullRepeatedMap;
181 } else if (Dimension == 3) {
182 FullRepeatedMapNode = BuildRepeatedMapGaleriStruct3D<SC,LO,GO,NO>(K->getMap(),M,Dimension);
183 FullRepeatedMap = BuildMapFromNodeMap(FullRepeatedMapNode,Dimension,NodeWise);
184 //FullRepeatedMapNode->describe(*fancy,Teuchos::VERB_EXTREME);
185 RepeatedMap = FullRepeatedMap;
186 }
187 } else {
188 RepeatedMap = BuildRepeatedMapNonConst<LO,GO,NO>(K->getCrsGraph());
189 }
190
191
192 RCP<MultiVector<SC,LO,GO,NO> > xSolution = MultiVectorFactory<SC,LO,GO,NO>::Build(UniqueMap,1);
193 RCP<MultiVector<SC,LO,GO,NO> > xRightHandSide = MultiVectorFactory<SC,LO,GO,NO>::Build(UniqueMap,1);
194
195 xSolution->putScalar(ScalarTraits<SC>::zero());
196 xRightHandSide->putScalar(ScalarTraits<SC>::one());
197
198 CrsMatrixWrap<SC,LO,GO,NO>& crsWrapK = dynamic_cast<CrsMatrixWrap<SC,LO,GO,NO>&>(*K);
199 RCP<const LinearOpBase<SC> > K_thyra = ThyraUtils<SC,LO,GO,NO>::toThyra(crsWrapK.getCrsMatrix());
200 RCP<MultiVectorBase<SC> >thyraX = rcp_const_cast<MultiVectorBase<SC> >(ThyraUtils<SC,LO,GO,NO>::toThyraMultiVector(xSolution));
201 RCP<const MultiVectorBase<SC> >thyraB = ThyraUtils<SC,LO,GO,NO>::toThyraMultiVector(xRightHandSide);
202
203 //-----------Set Coordinates and RepMap in ParameterList--------------------------
204 RCP<ParameterList> plList = sublist(parameterList,"Preconditioner Types");
205 sublist(plList,"FROSch")->set("Dimension",Dimension);
206 sublist(plList,"FROSch")->set("Overlap",Overlap);
207 sublist(plList,"FROSch")->set("DofOrdering","NodeWise");
208 sublist(plList,"FROSch")->set("DofsPerNode",Dimension);
209
210 sublist(plList,"FROSch")->set("Repeated Map",RepeatedMap);
211 sublist(plList,"FROSch")->set("Coordinates List",Coordinates);
212
213 Comm->barrier();
214 if (Comm->getRank()==0) {
215 cout << "##################\n# Parameter List #\n##################" << endl;
216 parameterList->print(cout);
217 cout << endl;
218 }
219
220 Comm->barrier(); if (Comm->getRank()==0) cout << "###################################\n# Stratimikos LinearSolverBuilder #\n###################################\n" << endl;
221 Stratimikos::LinearSolverBuilder<SC> linearSolverBuilder;
222 Stratimikos::enableFROSch<SC,LO,GO,NO>(linearSolverBuilder);
223 linearSolverBuilder.setParameterList(parameterList);
224
225 Comm->barrier(); if (Comm->getRank()==0) cout << "######################\n# Thyra PrepForSolve #\n######################\n" << endl;
226
227 RCP<LinearOpWithSolveFactoryBase<SC> > lowsFactory =
228 linearSolverBuilder.createLinearSolveStrategy("");
229
230 lowsFactory->setOStream(out);
231 lowsFactory->setVerbLevel(VERB_HIGH);
232
233 Comm->barrier(); if (Comm->getRank()==0) cout << "###########################\n# Thyra LinearOpWithSolve #\n###########################" << endl;
234
235 RCP<LinearOpWithSolveBase<SC> > lows =
236 linearOpWithSolve(*lowsFactory, K_thyra);
237
238 Comm->barrier(); if (Comm->getRank()==0) cout << "\n#########\n# Solve #\n#########" << endl;
239 SolveStatus<SC> status =
240 solve<SC>(*lows, Thyra::NOTRANS, *thyraB, thyraX.ptr());
241
242 Comm->barrier(); if (Comm->getRank()==0) cout << "\n#############\n# Finished! #\n#############" << endl;
243 }
244
245 CommWorld->barrier();
246 stackedTimer->stop("Thyra Elasticity Test");
247 StackedTimer::OutputOptions options;
248 options.output_fraction = options.output_histogram = options.output_minmax = true;
249 stackedTimer->report(*out,CommWorld,options);
250 std::string watchrProblemName = std::string("FROSch Elasticity ") + std::to_string(Comm->getSize()) + " ranks";
251 auto xmlOut = stackedTimer->reportWatchrXML(watchrProblemName, Comm);
252 if (xmlOut.length())
253 std::cout << "\nAlso created Watchr performance report " << xmlOut << '\n';
254
255 return(EXIT_SUCCESS);
256
257 }