Debian Codegraph @

packages/muelu/test/helmholtz/Helmholtz2D.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include <iostream>
11 #include <complex>
12
13 // Teuchos
14 #include <Teuchos_StandardCatchMacros.hpp>
15
16 // Xpetra and Galeri
17 #include <Xpetra_MultiVectorFactory.hpp>
18 #include <Galeri_XpetraParameters.hpp>
19 #include <Galeri_XpetraProblemFactory_Helmholtz.hpp>
20 #include <Galeri_XpetraUtils.hpp>
21 #include <Galeri_XpetraMaps.hpp>
22
23 // MueLu
24 #include <MueLu_ShiftedLaplacian.hpp>
25 #include <MueLu_UseDefaultTypesComplex.hpp>
26
27 int main(int argc, char *argv[]) {
28 #include <MueLu_UseShortNames.hpp>
29
30 typedef Tpetra::Vector<SC, LO, GO, NO> TVEC;
31 typedef Tpetra::MultiVector<SC, LO, GO, NO> TMV;
32 typedef Tpetra::CrsMatrix<SC, LO, GO, NO> TCRS;
33 typedef Xpetra::CrsMatrix<SC, LO, GO, NO> XCRS;
34 typedef Xpetra::TpetraCrsMatrix<SC, LO, GO, NO> XTCRS;
35 typedef Xpetra::Matrix<SC, LO, GO, NO> XMAT;
36 typedef Xpetra::CrsMatrixWrap<SC, LO, GO, NO> XWRAP;
37 typedef typename Teuchos::ScalarTraits<SC>::magnitudeType real_type;
38 typedef Xpetra::MultiVector<real_type, LO, GO, NO> RealValuedMultiVector;
39
40 using Teuchos::RCP;
41 using Teuchos::rcp;
42
43 Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL);
44
45 bool success = false;
46 bool verbose = true;
47 try {
48 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
49 Teuchos::CommandLineProcessor clp(false);
50
51 //***********************//
52 // Galeri Parameters //
53 //***********************//
54
55 GO nx, ny, nz;
56 GO mx, my, mz;
57 double stretchx, stretchy, stretchz, h, delta;
58 int PMLXL, PMLXR, PMLYL, PMLYR, PMLZL, PMLZR;
59 double omega, shift;
60 int model;
61
62 std::ifstream inputfile;
63 inputfile.open("helm2D.inp");
64 inputfile >> nx >> ny >> nz;
65 if (comm->getRank() == 0)
66 std::cout << "nx: " << nx << " ny: " << ny << " nz: " << nz << std::endl;
67 inputfile >> stretchx >> stretchy >> stretchz;
68 if (comm->getRank() == 0)
69 std::cout << "stretchx: " << stretchx << " stretchy: " << stretchy << " stretchz: " << stretchz << std::endl;
70 inputfile >> h >> delta;
71 if (comm->getRank() == 0)
72 std::cout << "h: " << h << " delta: " << delta << std::endl;
73 inputfile >> PMLXL >> PMLXR;
74 if (comm->getRank() == 0)
75 std::cout << "PMLXL: " << PMLXL << " PMLXR: " << PMLXR << std::endl;
76 inputfile >> PMLYL >> PMLYR;
77 if (comm->getRank() == 0)
78 std::cout << "PMLYL: " << PMLYL << " PMLYR: " << PMLYR << std::endl;
79 inputfile >> PMLZL >> PMLZR;
80 if (comm->getRank() == 0)
81 std::cout << "PMLZL: " << PMLZL << " PMLZR: " << PMLZR << std::endl;
82 inputfile >> omega >> shift;
83 if (comm->getRank() == 0)
84 std::cout << "omega: " << omega << " shift: " << shift << std::endl;
85 inputfile >> mx >> my >> mz;
86 if (comm->getRank() == 0)
87 std::cout << "mx: " << mx << " my: " << my << " mz: " << mz << std::endl;
88 inputfile >> model;
89 if (comm->getRank() == 0)
90 std::cout << "velocity model: " << model << std::endl;
91
92 Galeri::Xpetra::Parameters<GO> matrixParameters_aux(clp, nx, ny, nz, "Helmholtz2D", 0, stretchx, stretchy, stretchz,
93 h, delta, PMLXL, PMLXR, PMLYL, PMLYR, PMLZL, PMLZR, omega, 0.0, mx, my, mz, model);
94 Xpetra::Parameters xpetraParameters(clp);
95
96 //****************************************//
97 // Setup Galeri Problems and Matrices //
98 //****************************************//
99
100 Teuchos::ParameterList pl = matrixParameters_aux.GetParameterList();
101 RCP<RealValuedMultiVector> coordinates;
102 Teuchos::ParameterList galeriList;
103 galeriList.set("nx", pl.get("nx", nx));
104 galeriList.set("ny", pl.get("ny", ny));
105 galeriList.set("nz", pl.get("nz", nz));
106 RCP<const Map> map;
107
108 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian2D", comm, galeriList);
109 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<SC, LO, GO, Map, RealValuedMultiVector>("2D", map, matrixParameters_aux.GetParameterList());
110
111 RCP<const Tpetra::Map<LO, GO, NO> > tmap = Xpetra::toTpetra(map);
112
113 Teuchos::ParameterList matrixParams_aux = matrixParameters_aux.GetParameterList();
114
115 RCP<Galeri::Xpetra::Problem_Helmholtz<Map, CrsMatrixWrap, MultiVector> > Pr_aux =
116 Galeri::Xpetra::BuildProblem_Helmholtz<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(matrixParameters_aux.GetMatrixType(), map, matrixParams_aux);
117 RCP<Matrix> Kmat, Mmat;
118 std::pair<RCP<Matrix>, RCP<Matrix> > system = Pr_aux->BuildMatrices();
119 Kmat = system.first;
120 Mmat = system.second;
121
122 //******************************************************************//
123 // Initialize Shifted Laplacian Preconditioner and Belos Solver //
124 //******************************************************************//
125
126 // Initialize Shifted Laplacian with the stiffness matrix
127 RCP<ShiftedLaplacian> SLSolver = rcp(new ShiftedLaplacian);
128 SLSolver->setstiff(Kmat);
129 SLSolver->initialize();
130
131 // vector of frequencies
132 std::vector<double> omegas;
133 omegas.push_back(omega);
134 omegas.push_back(omega + 10);
135 omegas.push_back(omega + 20);
136 int total_iters = 0;
137
138 // loop over frequencies
139 for (size_t i = 0; i < omegas.size(); i++) {
140 Galeri::Xpetra::Parameters<GO> matrixParameters_helmholtz(clp, nx, ny, nz, "Helmholtz2D", 0, stretchx, stretchy, stretchz,
141 h, delta, PMLXL, PMLXR, PMLYL, PMLYR, PMLZL, PMLZR, omegas[i], 0.0, mx, my, mz, model);
142 Galeri::Xpetra::Parameters<GO> matrixParameters_shifted(clp, nx, ny, nz, "Helmholtz2D", 0, stretchx, stretchy, stretchz,
143 h, delta, PMLXL, PMLXR, PMLYL, PMLYR, PMLZL, PMLZR, omegas[i], shift, mx, my, mz, model);
144
145 Teuchos::ParameterList matrixParams_helmholtz = matrixParameters_helmholtz.GetParameterList();
146 Teuchos::ParameterList matrixParams_shifted = matrixParameters_shifted.GetParameterList();
147
148 RCP<Galeri::Xpetra::Problem_Helmholtz<Map, CrsMatrixWrap, MultiVector> > Pr_helmholtz =
149 Galeri::Xpetra::BuildProblem_Helmholtz<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(matrixParameters_helmholtz.GetMatrixType(), map, matrixParams_helmholtz);
150 RCP<Matrix> Amat = Pr_helmholtz->BuildMatrix();
151
152 RCP<Galeri::Xpetra::Problem_Helmholtz<Map, CrsMatrixWrap, MultiVector> > Pr_shifted =
153 Galeri::Xpetra::BuildProblem_Helmholtz<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(matrixParameters_shifted.GetMatrixType(), map, matrixParams_shifted);
154 RCP<Matrix> Pmat = Pr_shifted->BuildMatrix();
155
156 // set Helmholtz operator (Amat), Shifted Laplacian operator (Pmat),
157 // and mass matrix (Mmat)
158 SLSolver->setmass(Mmat);
159 SLSolver->setProblemMatrix(Amat);
160 SLSolver->setPreconditioningMatrix(Pmat);
161 // determine shifts for RAPShiftFactory
162 std::vector<SC> shifts;
163 int maxLevels = 5;
164 for (int j = 0; j < maxLevels; j++) {
165 double alpha = 1.0;
166 double beta = shift + ((double)j) * 0.2;
167 SC curshift(alpha, beta);
168 shifts.push_back(-curshift);
169 }
170 SLSolver->setLevelShifts(shifts);
171 // setup hierarchy with variable complex shifts
172 SLSolver->setupSlowRAP();
173
174 // solve for RHS
175 RCP<TMV> X = Tpetra::createMultiVector<SC, LO, GO, NO>(tmap, 1);
176 RCP<TMV> B = Tpetra::createMultiVector<SC, LO, GO, NO>(tmap, 1);
177 X->putScalar((SC)0.0);
178 B->putScalar((SC)0.0);
179 int pointsourceid = nx * ny / 2 + nx / 2;
180 if (map->isNodeGlobalElement(pointsourceid) == true) {
181 B->replaceGlobalValue(pointsourceid, 0, (SC)1.0);
182 }
183 SLSolver->solve(B, X);
184
185 // sum up number of iterations
186 total_iters += SLSolver->GetIterations();
187
188 #ifdef HAVE_MUELU_DEBUG
189 SLSolver->Manager_->ResetDebugData();
190 #endif
191 }
192
193 if (total_iters <= 110)
194 success = true;
195 else
196 success = false;
197 }
198
199 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
200
201 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
202 } // main