| 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 <map> |
| 12 |
#include <Teuchos_UnitTestRepository.hpp> |
| 13 |
#include <Teuchos_DefaultComm.hpp> |
| 14 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 15 |
#include <Teuchos_UnitTestHarness.hpp> |
| 16 |
#include <Teuchos_ScalarTraits.hpp> |
| 17 |
#include <Teuchos_RCP.hpp> |
| 18 |
|
| 19 |
// Xpetra |
| 20 |
#include <Xpetra_Map.hpp> |
| 21 |
#include <Xpetra_MapFactory.hpp> |
| 22 |
#include <Xpetra_MultiVector.hpp> |
| 23 |
#include <Xpetra_Vector.hpp> |
| 24 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 25 |
#include <Xpetra_Matrix.hpp> |
| 26 |
#include <Xpetra_CrsMatrix.hpp> |
| 27 |
#include <Xpetra_CrsMatrixFactory.hpp> |
| 28 |
#include <Xpetra_Parameters.hpp> |
| 29 |
#include <Xpetra_IO.hpp> |
| 30 |
|
| 31 |
// MueLu |
| 32 |
#include <MueLu_config.hpp> |
| 33 |
#include <MueLu_TestHelpers.hpp> |
| 34 |
#include <MueLu_Version.hpp> |
| 35 |
#include <MueLu_ReitzingerPFactory.hpp> |
| 36 |
#include <MueLu_TentativePFactory.hpp> |
| 37 |
#include <MueLu_Exceptions.hpp> |
| 38 |
#include <MueLu_Hierarchy.hpp> |
| 39 |
#include <MueLu_RAPFactory.hpp> |
| 40 |
#include <MueLu_SaPFactory.hpp> |
| 41 |
|
| 42 |
using Teuchos::RCP; |
| 43 |
using Teuchos::rcp; |
| 44 |
|
| 45 |
// Copy & paste from MueLu_TestHelpers.cpp |
| 46 |
namespace MueLuTests { |
| 47 |
|
| 48 |
// static members initialization of the class TestHelpers::Parameters |
| 49 |
Xpetra::Parameters TestHelpers::Parameters::xpetraParameters = Xpetra::Parameters(Teuchos::UnitTestRepository::getCLP()); |
| 50 |
|
| 51 |
} // namespace MueLuTests |
| 52 |
|
| 53 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 54 |
typename Teuchos::ScalarTraits<Scalar>::magnitudeType |
| 55 |
CheckCommutingProperty(MueLu::Level& nodeLevel_coarse, MueLu::Level& edgeLevel_fine, MueLu::Level& edgeLevel_coarse) { |
| 56 |
#include <MueLu_UseShortNames.hpp> |
| 57 |
RCP<Matrix> Pn = nodeLevel_coarse.Get<RCP<Matrix> >("P"); |
| 58 |
RCP<Matrix> Pe = edgeLevel_coarse.Get<RCP<Matrix> >("P"); |
| 59 |
RCP<Matrix> D0_f = edgeLevel_fine.Get<RCP<Matrix> >("D0"); |
| 60 |
RCP<Matrix> D0_c = edgeLevel_coarse.Get<RCP<Matrix> >("D0"); |
| 61 |
|
| 62 |
using XMM = Xpetra::MatrixMatrix<SC, LO, GO, NO>; |
| 63 |
using MT = typename Teuchos::ScalarTraits<SC>::magnitudeType; |
| 64 |
SC one = Teuchos::ScalarTraits<SC>::one(); |
| 65 |
SC zero = Teuchos::ScalarTraits<SC>::zero(); |
| 66 |
|
| 67 |
RCP<Matrix> dummy; |
| 68 |
RCP<Teuchos::FancyOStream> out0 = Teuchos::getFancyOStream(rcp(new Teuchos::oblackholestream())); |
| 69 |
|
| 70 |
RCP<Matrix> left = XMM::Multiply(*Pe, false, *D0_c, false, dummy, *out0); |
| 71 |
RCP<Matrix> right = XMM::Multiply(*D0_f, false, *Pn, false, dummy, *out0); |
| 72 |
|
| 73 |
// We need a non-FC matrix for the add, sadly |
| 74 |
RCP<CrsMatrix> sum_c = CrsMatrixFactory::Build(left->getRowMap(), left->getLocalMaxNumRowEntries() + right->getLocalMaxNumRowEntries()); |
| 75 |
RCP<Matrix> summation = rcp(new CrsMatrixWrap(sum_c)); |
| 76 |
XMM::TwoMatrixAdd(*left, false, one, *summation, zero); |
| 77 |
XMM::TwoMatrixAdd(*right, false, -one, *summation, one); |
| 78 |
|
| 79 |
MT norm = summation->getFrobeniusNorm(); |
| 80 |
return norm; |
| 81 |
} |
| 82 |
|
| 83 |
template <typename Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 84 |
void read_matrix(Xpetra::UnderlyingLib& lib, RCP<const Teuchos::Comm<int> >& comm, |
| 85 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& SM_Matrix, |
| 86 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& D0_Matrix, |
| 87 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& Kn_Matrix, |
| 88 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LocalOrdinal, GlobalOrdinal, Node> >& coords) { |
| 89 |
#include <MueLu_UseShortNames.hpp> |
| 90 |
RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 91 |
|
| 92 |
std::string S_file, M1_file, D0_file; |
| 93 |
if (!Teuchos::ScalarTraits<Scalar>::isComplex) { |
| 94 |
S_file = "S.mat"; |
| 95 |
D0_file = "D0.mat"; |
| 96 |
M1_file = "M1.mat"; |
| 97 |
} else { |
| 98 |
S_file = "S_complex.mat"; |
| 99 |
D0_file = "D0_complex.mat"; |
| 100 |
M1_file = "M1_complex.mat"; |
| 101 |
} |
| 102 |
std::string coords_file = "coords.mat"; |
| 103 |
|
| 104 |
// maps for nodal and edge matrices |
| 105 |
RCP<const Map> node_map; |
| 106 |
RCP<const Map> edge_map; |
| 107 |
|
| 108 |
// gradient matrix |
| 109 |
try { |
| 110 |
std::string base = D0_file.substr(0, D0_file.find_last_of('/') + 1); |
| 111 |
std::string D0_filename = D0_file.substr(D0_file.find_last_of('/') + 1, std::string::npos); |
| 112 |
std::string edgeMap_file = base + "rowmap_" + D0_filename; |
| 113 |
std::string nodeMap_file = base + "domainmap_" + D0_filename; |
| 114 |
std::string colMap_file = base + "colmap_" + D0_filename; |
| 115 |
node_map = Xpetra::IO<SC, LO, GO, NO>::ReadMap(nodeMap_file, lib, comm); |
| 116 |
edge_map = Xpetra::IO<SC, LO, GO, NO>::ReadMap(edgeMap_file, lib, comm); |
| 117 |
RCP<const Map> colMap; |
| 118 |
if (comm->getSize() > 1) |
| 119 |
colMap = Xpetra::IO<SC, LO, GO, NO>::ReadMap(colMap_file, lib, comm); |
| 120 |
D0_Matrix = Xpetra::IO<SC, LO, GO, NO>::Read(D0_file, edge_map, colMap, node_map, edge_map); |
| 121 |
} catch (const std::exception&) { |
| 122 |
// *out << "Skipping D0 maps, because: " << e.what() << std::endl; |
| 123 |
D0_Matrix = Xpetra::IO<SC, LO, GO, NO>::Read(D0_file, lib, comm); |
| 124 |
node_map = D0_Matrix->getDomainMap(); |
| 125 |
edge_map = D0_Matrix->getRangeMap(); |
| 126 |
} |
| 127 |
|
| 128 |
// build stiffness plus mass matrix (SM_Matrix) |
| 129 |
// edge stiffness matrix |
| 130 |
RCP<Matrix> S_Matrix = Xpetra::IO<SC, LO, GO, NO>::Read(S_file, edge_map); |
| 131 |
RCP<Matrix> M1_Matrix = Xpetra::IO<SC, LO, GO, NO>::Read(M1_file, edge_map); |
| 132 |
Scalar one = Teuchos::ScalarTraits<SC>::one(); |
| 133 |
Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*S_Matrix, false, one, *M1_Matrix, false, one, SM_Matrix, *out); |
| 134 |
SM_Matrix->fillComplete(); |
| 135 |
|
| 136 |
// coordinates |
| 137 |
coords = Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>::ReadMultiVector(coords_file, node_map); |
| 138 |
|
| 139 |
// Generate Kn matrix |
| 140 |
{ |
| 141 |
RCP<Matrix> temp = Xpetra::MatrixFactory<SC, LO, GO, NO>::Build(SM_Matrix->getRangeMap()); |
| 142 |
Xpetra::MatrixMatrix<SC, LO, GO, NO>::Multiply(*SM_Matrix, false, *D0_Matrix, false, *temp, true, true); |
| 143 |
Kn_Matrix = Xpetra::MatrixFactory<SC, LO, GO, NO>::Build(D0_Matrix->getDomainMap()); |
| 144 |
Xpetra::MatrixMatrix<SC, LO, GO, NO>::Multiply(*D0_Matrix, true, *temp, false, *Kn_Matrix, true, true); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
namespace MueLuTests { |
| 149 |
|
| 150 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(ReitzingerPFactory, Setup2Level_Unsmoothed, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 151 |
#include <MueLu_UseShortNames.hpp> |
| 152 |
MUELU_TESTING_SET_OSTREAM; |
| 153 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 154 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 155 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 156 |
|
| 157 |
RCP<Matrix> SM_Matrix, D0_Matrix, Kn_Matrix; |
| 158 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO> > coords; |
| 159 |
read_matrix<SC, LO, GO, NO>(lib, comm, SM_Matrix, D0_Matrix, Kn_Matrix, coords); |
| 160 |
|
| 161 |
int NumLevels = 2; |
| 162 |
// This guy works by generating a nodal hierarchy, copying the relevant data to the edge hierarchy |
| 163 |
// and then generating the edge hierarchy |
| 164 |
|
| 165 |
Hierarchy NodeH, EdgeH; |
| 166 |
// NodeH.EnableGraphDumping("node_graph_0",0); |
| 167 |
// EdgeH.EnableGraphDumping("edge_graph_0",0); |
| 168 |
NodeH.SetMaxCoarseSize(10); |
| 169 |
|
| 170 |
// Generate Node Hierarchy |
| 171 |
out << "*** Setting Up Node Hierarchy *** " << std::endl; |
| 172 |
{ |
| 173 |
RCP<MueLu::Level> Finest = NodeH.GetLevel(); |
| 174 |
Finest->setDefaultVerbLevel(Teuchos::VERB_HIGH); |
| 175 |
Finest->Set("A", Kn_Matrix); |
| 176 |
|
| 177 |
// Level 0 |
| 178 |
FactoryManager M0; // how to build aggregates and smoother of the first level |
| 179 |
M0.SetKokkosRefactor(false); |
| 180 |
|
| 181 |
// Level 1 (Plain aggregation) |
| 182 |
FactoryManager M1; |
| 183 |
M1.SetKokkosRefactor(false); |
| 184 |
Teuchos::ParameterList tp_list; |
| 185 |
tp_list.set("tentative: constant column sums", false); |
| 186 |
tp_list.set("tentative: calculate qr", false); |
| 187 |
RCP<TentativePFactory> PnodalFact = rcp(new TentativePFactory()); |
| 188 |
PnodalFact->SetParameterList(tp_list); |
| 189 |
M1.SetFactory("P", PnodalFact); |
| 190 |
M1.SetFactory("CoarseSolver", Teuchos::null); |
| 191 |
|
| 192 |
bool r; |
| 193 |
r = NodeH.Setup(0, Teuchos::null, rcpFromRef(M0), rcpFromRef(M1)); |
| 194 |
TEST_EQUALITY(r, false); |
| 195 |
r = NodeH.Setup(1, rcpFromRef(M0), rcpFromRef(M1), Teuchos::null); |
| 196 |
TEST_EQUALITY(r, true); |
| 197 |
RCP<Level> l0 = NodeH.GetLevel(0); |
| 198 |
RCP<Level> l1 = NodeH.GetLevel(1); |
| 199 |
} |
| 200 |
|
| 201 |
out << "*** Copy Node->Edge Data *** " << std::endl; |
| 202 |
// Copy Data to Edge Hierarchy |
| 203 |
for (int i = 0; i < NumLevels; i++) { |
| 204 |
EdgeH.AddNewLevel(); |
| 205 |
RCP<Level> NodeL = NodeH.GetLevel(i); |
| 206 |
RCP<Level> EdgeL = EdgeH.GetLevel(i); |
| 207 |
|
| 208 |
EdgeL->Set("NodeAggMatrix", NodeL->Get<RCP<Matrix> >("A")); |
| 209 |
EdgeL->Set("NodeMatrix", NodeL->Get<RCP<Matrix> >("A")); |
| 210 |
if (i != 0) { |
| 211 |
EdgeL->Set("Pnodal", NodeL->Get<RCP<Matrix> >("P")); |
| 212 |
|
| 213 |
RCP<const Matrix> P = NodeL->Get<RCP<Matrix> >("P"); |
| 214 |
// Xpetra::IO<SC,LO,GO,NO>::Write("Pn.mat",*P); |
| 215 |
} |
| 216 |
|
| 217 |
if (i == 0) { |
| 218 |
EdgeL->Set("A", SM_Matrix); |
| 219 |
EdgeL->Set("D0", D0_Matrix); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// Generate the Edge Hierarchy |
| 224 |
out << "*** Setting Up Edge Hierarchy *** " << std::endl; |
| 225 |
{ |
| 226 |
FactoryManager M0; // how to build aggregates and smoother of the first level |
| 227 |
M0.SetKokkosRefactor(false); |
| 228 |
|
| 229 |
// Level 1 (Plain aggregation) |
| 230 |
FactoryManager M1; |
| 231 |
M1.SetKokkosRefactor(false); |
| 232 |
RCP<ReitzingerPFactory> PedgeFact = rcp(new ReitzingerPFactory()); |
| 233 |
M1.SetFactory("P", PedgeFact); |
| 234 |
M1.SetFactory("CoarseSolver", Teuchos::null); |
| 235 |
|
| 236 |
// Do the setup |
| 237 |
bool r; |
| 238 |
r = EdgeH.Setup(0, Teuchos::null, rcpFromRef(M0), rcpFromRef(M1)); |
| 239 |
TEST_EQUALITY(r, false); |
| 240 |
r = EdgeH.Setup(1, rcpFromRef(M0), rcpFromRef(M1), Teuchos::null); |
| 241 |
TEST_EQUALITY(r, true); |
| 242 |
} |
| 243 |
|
| 244 |
RCP<Level> l0 = EdgeH.GetLevel(0); |
| 245 |
RCP<Level> l1 = EdgeH.GetLevel(1); |
| 246 |
|
| 247 |
RCP<Level> node_l1 = NodeH.GetLevel(1); |
| 248 |
|
| 249 |
TEST_EQUALITY(l0->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 250 |
TEST_EQUALITY(l1->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 251 |
TEST_EQUALITY(l1->IsAvailable("P", MueLu::NoFactory::get()), true); |
| 252 |
|
| 253 |
// Check the commuting property |
| 254 |
using MT = typename Teuchos::ScalarTraits<SC>::magnitudeType; |
| 255 |
Teuchos::Array<MT> norm(1), norm0(1); |
| 256 |
norm0[0] = Teuchos::ScalarTraits<MT>::zero(); |
| 257 |
|
| 258 |
norm[0] = CheckCommutingProperty<SC, LO, GO, NO>(*node_l1, *l0, *l1); |
| 259 |
TEST_COMPARE_FLOATING_ARRAYS(norm, norm0, Teuchos::ScalarTraits<MT>::eps() * 100) |
| 260 |
} |
| 261 |
|
| 262 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(ReitzingerPFactory, Setup2Level_AlphaSmoothed, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 263 |
#include <MueLu_UseShortNames.hpp> |
| 264 |
MUELU_TESTING_SET_OSTREAM; |
| 265 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 266 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 267 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 268 |
|
| 269 |
RCP<Matrix> SM_Matrix, D0_Matrix, Kn_Matrix; |
| 270 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO> > coords; |
| 271 |
read_matrix<SC, LO, GO, NO>(lib, comm, SM_Matrix, D0_Matrix, Kn_Matrix, coords); |
| 272 |
|
| 273 |
int NumLevels = 2; |
| 274 |
// This guy works by generating a nodal hierarchy, copying the relevant data to the edge hierarchy |
| 275 |
// and then generating the edge hierarchy |
| 276 |
Hierarchy NodeH, EdgeH; |
| 277 |
NodeH.SetMaxCoarseSize(10); |
| 278 |
// NodeH.EnableGraphDumping("node_graph_0",0); |
| 279 |
// EdgeH.EnableGraphDumping("edge_graph_0",0); |
| 280 |
|
| 281 |
// Generate Node Hierarchy |
| 282 |
out << "*** Setting Up Node Hierarchy *** " << std::endl; |
| 283 |
{ |
| 284 |
RCP<MueLu::Level> Finest = NodeH.GetLevel(); |
| 285 |
Finest->setDefaultVerbLevel(Teuchos::VERB_HIGH); |
| 286 |
Finest->Set("A", Kn_Matrix); |
| 287 |
|
| 288 |
// Level 0 |
| 289 |
FactoryManager M0; // how to build aggregates and smoother of the first level |
| 290 |
M0.SetKokkosRefactor(false); |
| 291 |
|
| 292 |
// Level 1 (Plain aggregation) |
| 293 |
FactoryManager M1; |
| 294 |
M1.SetKokkosRefactor(false); |
| 295 |
Teuchos::ParameterList tp_list; |
| 296 |
tp_list.set("tentative: constant column sums", false); |
| 297 |
tp_list.set("tentative: calculate qr", false); |
| 298 |
RCP<TentativePFactory> PnodalFact = rcp(new TentativePFactory()); |
| 299 |
PnodalFact->SetParameterList(tp_list); |
| 300 |
M1.SetFactory("P", PnodalFact); |
| 301 |
M1.SetFactory("CoarseSolver", Teuchos::null); |
| 302 |
|
| 303 |
bool r; |
| 304 |
r = NodeH.Setup(0, Teuchos::null, rcpFromRef(M0), rcpFromRef(M1)); |
| 305 |
TEST_EQUALITY(r, false); |
| 306 |
r = NodeH.Setup(1, rcpFromRef(M0), rcpFromRef(M1), Teuchos::null); |
| 307 |
TEST_EQUALITY(r, true); |
| 308 |
RCP<Level> l0 = NodeH.GetLevel(0); |
| 309 |
RCP<Level> l1 = NodeH.GetLevel(1); |
| 310 |
} |
| 311 |
|
| 312 |
out << "*** Copy Node->Edge Data *** " << std::endl; |
| 313 |
// Copy Data to Edge Hierarchy |
| 314 |
for (int i = 0; i < NumLevels; i++) { |
| 315 |
EdgeH.AddNewLevel(); |
| 316 |
RCP<Level> NodeL = NodeH.GetLevel(i); |
| 317 |
RCP<Level> EdgeL = EdgeH.GetLevel(i); |
| 318 |
|
| 319 |
EdgeL->Set("NodeAggMatrix", NodeL->Get<RCP<Matrix> >("A")); |
| 320 |
EdgeL->Set("NodeMatrix", NodeL->Get<RCP<Matrix> >("A")); |
| 321 |
if (i != 0) { |
| 322 |
EdgeL->Set("Pnodal", NodeL->Get<RCP<Matrix> >("P")); |
| 323 |
|
| 324 |
RCP<const Matrix> P = NodeL->Get<RCP<Matrix> >("P"); |
| 325 |
// Xpetra::IO<SC,LO,GO,NO>::Write("Pn.mat",*P); |
| 326 |
} |
| 327 |
|
| 328 |
if (i == 0) { |
| 329 |
EdgeL->Set("A", SM_Matrix); |
| 330 |
EdgeL->Set("D0", D0_Matrix); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
// Generate the Edge Hierarchy |
| 335 |
out << "*** Setting Up Edge Hierarchy *** " << std::endl; |
| 336 |
{ |
| 337 |
FactoryManager M0; // how to build aggregates and smoother of the first level |
| 338 |
M0.SetKokkosRefactor(false); |
| 339 |
|
| 340 |
// Level 1 (Smoothed Reitzinger) |
| 341 |
FactoryManager M1; |
| 342 |
M1.SetKokkosRefactor(false); |
| 343 |
RCP<ReitzingerPFactory> PedgeFact = rcp(new ReitzingerPFactory()); |
| 344 |
RCP<SaPFactory> PFact = rcp(new SaPFactory()); |
| 345 |
PFact->SetFactory("P", PedgeFact); |
| 346 |
M1.SetFactory("Ptent", PedgeFact); |
| 347 |
M1.SetFactory("P", PFact); |
| 348 |
M1.SetFactory("CoarseSolver", Teuchos::null); |
| 349 |
|
| 350 |
// Do the setup |
| 351 |
bool r; |
| 352 |
r = EdgeH.Setup(0, Teuchos::null, rcpFromRef(M0), rcpFromRef(M1)); |
| 353 |
TEST_EQUALITY(r, false); |
| 354 |
r = EdgeH.Setup(1, rcpFromRef(M0), rcpFromRef(M1), Teuchos::null); |
| 355 |
TEST_EQUALITY(r, true); |
| 356 |
} |
| 357 |
|
| 358 |
RCP<Level> l0 = EdgeH.GetLevel(0); |
| 359 |
RCP<Level> l1 = EdgeH.GetLevel(1); |
| 360 |
|
| 361 |
{ |
| 362 |
RCP<const Matrix> Pe = l1->Get<RCP<Matrix> >("P"); |
| 363 |
// Xpetra::IO<SC,LO,GO,NO>::Write("Pe.mat",*Pe); |
| 364 |
} |
| 365 |
TEST_EQUALITY(l0->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 366 |
TEST_EQUALITY(l1->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 367 |
TEST_EQUALITY(l1->IsAvailable("P", MueLu::NoFactory::get()), true); |
| 368 |
} |
| 369 |
|
| 370 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(ReitzingerPFactory, Setup3Level_AlphaSmoothed, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 371 |
#include <MueLu_UseShortNames.hpp> |
| 372 |
MUELU_TESTING_SET_OSTREAM; |
| 373 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 374 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 375 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 376 |
|
| 377 |
RCP<Matrix> SM_Matrix, D0_Matrix, Kn_Matrix; |
| 378 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO> > coords; |
| 379 |
read_matrix<SC, LO, GO, NO>(lib, comm, SM_Matrix, D0_Matrix, Kn_Matrix, coords); |
| 380 |
|
| 381 |
int NumLevels = 3; |
| 382 |
// This guy works by generating a nodal hierarchy, copying the relevant data to the edge hierarchy |
| 383 |
// and then generating the edge hierarchy |
| 384 |
Hierarchy NodeH, EdgeH; |
| 385 |
NodeH.SetMaxCoarseSize(1); |
| 386 |
EdgeH.SetMaxCoarseSize(1); |
| 387 |
// NodeH.EnableGraphDumping("node_graph_0",0); |
| 388 |
// EdgeH.EnableGraphDumping("edge_graph_0",0); |
| 389 |
|
| 390 |
// Generate Node Hierarchy |
| 391 |
out << "*** Setting Up Node Hierarchy *** " << std::endl; |
| 392 |
{ |
| 393 |
RCP<MueLu::Level> Finest = NodeH.GetLevel(); |
| 394 |
Finest->setDefaultVerbLevel(Teuchos::VERB_HIGH); |
| 395 |
Finest->Set("A", Kn_Matrix); |
| 396 |
Finest->Set("Coordinates", coords); |
| 397 |
|
| 398 |
// Level 0 |
| 399 |
FactoryManager M0; // how to build aggregates and smoother of the first level |
| 400 |
M0.SetKokkosRefactor(false); |
| 401 |
|
| 402 |
// Level 1 (Plain aggregation) |
| 403 |
FactoryManager M1; |
| 404 |
{ |
| 405 |
M1.SetKokkosRefactor(false); |
| 406 |
Teuchos::ParameterList tp_list; |
| 407 |
tp_list.set("tentative: constant column sums", false); |
| 408 |
tp_list.set("tentative: calculate qr", false); |
| 409 |
RCP<TentativePFactory> PnodalFact = rcp(new TentativePFactory()); |
| 410 |
PnodalFact->SetParameterList(tp_list); |
| 411 |
M1.SetFactory("P", PnodalFact); |
| 412 |
M1.SetFactory("Ptent", PnodalFact); // You actually need this |
| 413 |
} |
| 414 |
|
| 415 |
// Level 2 (Plain aggregation) |
| 416 |
FactoryManager M2; |
| 417 |
{ |
| 418 |
M2.SetKokkosRefactor(false); |
| 419 |
Teuchos::ParameterList tp_list; |
| 420 |
tp_list.set("tentative: constant column sums", false); |
| 421 |
tp_list.set("tentative: calculate qr", false); |
| 422 |
RCP<TentativePFactory> PnodalFact = rcp(new TentativePFactory()); |
| 423 |
PnodalFact->SetParameterList(tp_list); |
| 424 |
M2.SetFactory("P", PnodalFact); |
| 425 |
M2.SetFactory("CoarseSolver", Teuchos::null); |
| 426 |
} |
| 427 |
|
| 428 |
bool r; |
| 429 |
r = NodeH.Setup(0, Teuchos::null, rcpFromRef(M0), rcpFromRef(M1)); |
| 430 |
TEST_EQUALITY(r, false); |
| 431 |
r = NodeH.Setup(1, rcpFromRef(M0), rcpFromRef(M1), rcpFromRef(M2)); |
| 432 |
TEST_EQUALITY(r, false); |
| 433 |
r = NodeH.Setup(2, rcpFromRef(M1), rcpFromRef(M2), Teuchos::null); |
| 434 |
TEST_EQUALITY(r, true); |
| 435 |
|
| 436 |
RCP<Level> l0 = NodeH.GetLevel(0); |
| 437 |
RCP<Level> l1 = NodeH.GetLevel(1); |
| 438 |
RCP<Level> l2 = NodeH.GetLevel(2); |
| 439 |
} |
| 440 |
|
| 441 |
out << "*** Copy Node->Edge Data *** " << std::endl; |
| 442 |
// Copy Data to Edge Hierarchy |
| 443 |
for (int i = 0; i < NumLevels; i++) { |
| 444 |
EdgeH.AddNewLevel(); |
| 445 |
RCP<Level> NodeL = NodeH.GetLevel(i); |
| 446 |
RCP<Level> EdgeL = EdgeH.GetLevel(i); |
| 447 |
|
| 448 |
EdgeL->Set("NodeAggMatrix", NodeL->Get<RCP<Matrix> >("A")); |
| 449 |
EdgeL->Set("NodeMatrix", NodeL->Get<RCP<Matrix> >("A")); |
| 450 |
if (i != 0) { |
| 451 |
EdgeL->Set("Pnodal", NodeL->Get<RCP<Matrix> >("P")); |
| 452 |
|
| 453 |
RCP<const Matrix> P = NodeL->Get<RCP<Matrix> >("P"); |
| 454 |
// Xpetra::IO<SC,LO,GO,NO>::Write("Pn.mat",*P); |
| 455 |
} |
| 456 |
|
| 457 |
if (i == 0) { |
| 458 |
EdgeL->Set("A", SM_Matrix); |
| 459 |
EdgeL->Set("D0", D0_Matrix); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
// Generate the Edge Hierarchy |
| 464 |
out << "*** Setting Up Edge Hierarchy *** " << std::endl; |
| 465 |
{ |
| 466 |
FactoryManager M0; // how to build aggregates and smoother of the first level |
| 467 |
M0.SetKokkosRefactor(false); |
| 468 |
|
| 469 |
// Level 1 (Smoothed Reitzinger) |
| 470 |
FactoryManager M1; |
| 471 |
{ |
| 472 |
M1.SetKokkosRefactor(false); |
| 473 |
RCP<ReitzingerPFactory> PedgeFact1 = rcp(new ReitzingerPFactory()); |
| 474 |
RCP<SaPFactory> PFact = rcp(new SaPFactory()); |
| 475 |
PFact->SetFactory("P", PedgeFact1); |
| 476 |
M1.SetFactory("Ptent", PedgeFact1); |
| 477 |
M1.SetFactory("D0", PedgeFact1); |
| 478 |
M1.SetFactory("P", PFact); |
| 479 |
} |
| 480 |
|
| 481 |
// Level 2 (just like level 1) |
| 482 |
FactoryManager M2; |
| 483 |
{ |
| 484 |
M2.SetKokkosRefactor(false); |
| 485 |
RCP<ReitzingerPFactory> PedgeFact2 = rcp(new ReitzingerPFactory()); |
| 486 |
RCP<SaPFactory> PFact = rcp(new SaPFactory()); |
| 487 |
PFact->SetFactory("P", PedgeFact2); |
| 488 |
M2.SetFactory("Ptent", PedgeFact2); |
| 489 |
M2.SetFactory("D0", PedgeFact2); |
| 490 |
M2.SetFactory("P", PFact); |
| 491 |
M2.SetFactory("CoarseSolver", Teuchos::null); |
| 492 |
} |
| 493 |
|
| 494 |
// Do the setup |
| 495 |
bool r; |
| 496 |
r = EdgeH.Setup(0, Teuchos::null, rcpFromRef(M0), rcpFromRef(M1)); |
| 497 |
TEST_EQUALITY(r, false); |
| 498 |
r = EdgeH.Setup(1, rcpFromRef(M0), rcpFromRef(M1), rcpFromRef(M2)); |
| 499 |
TEST_EQUALITY(r, false); |
| 500 |
r = EdgeH.Setup(2, rcpFromRef(M1), rcpFromRef(M2), Teuchos::null); |
| 501 |
TEST_EQUALITY(r, true); |
| 502 |
} |
| 503 |
|
| 504 |
RCP<Level> node_l1 = NodeH.GetLevel(1); |
| 505 |
RCP<Level> node_l2 = NodeH.GetLevel(2); |
| 506 |
|
| 507 |
RCP<Level> l0 = EdgeH.GetLevel(0); |
| 508 |
RCP<Level> l1 = EdgeH.GetLevel(1); |
| 509 |
RCP<Level> l2 = EdgeH.GetLevel(2); |
| 510 |
|
| 511 |
TEST_EQUALITY(l0->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 512 |
TEST_EQUALITY(l1->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 513 |
TEST_EQUALITY(l1->IsAvailable("P", MueLu::NoFactory::get()), true); |
| 514 |
TEST_EQUALITY(l2->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 515 |
TEST_EQUALITY(l2->IsAvailable("P", MueLu::NoFactory::get()), true); |
| 516 |
} |
| 517 |
|
| 518 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(ReitzingerPFactory, Setup3Level_Unsmoothed, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 519 |
#include <MueLu_UseShortNames.hpp> |
| 520 |
MUELU_TESTING_SET_OSTREAM; |
| 521 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 522 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 523 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 524 |
|
| 525 |
RCP<Matrix> SM_Matrix, D0_Matrix, Kn_Matrix; |
| 526 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO> > coords; |
| 527 |
read_matrix<SC, LO, GO, NO>(lib, comm, SM_Matrix, D0_Matrix, Kn_Matrix, coords); |
| 528 |
|
| 529 |
int NumLevels = 3; |
| 530 |
// This guy works by generating a nodal hierarchy, copying the relevant data to the edge hierarchy |
| 531 |
// and then generating the edge hierarchy |
| 532 |
Hierarchy NodeH, EdgeH; |
| 533 |
NodeH.SetMaxCoarseSize(1); |
| 534 |
EdgeH.SetMaxCoarseSize(1); |
| 535 |
// NodeH.EnableGraphDumping("node_graph_0",0); |
| 536 |
// EdgeH.EnableGraphDumping("edge_graph_0",0); |
| 537 |
|
| 538 |
// Generate Node Hierarchy |
| 539 |
out << "*** Setting Up Node Hierarchy *** " << std::endl; |
| 540 |
{ |
| 541 |
RCP<MueLu::Level> Finest = NodeH.GetLevel(); |
| 542 |
Finest->setDefaultVerbLevel(Teuchos::VERB_HIGH); |
| 543 |
Finest->Set("A", Kn_Matrix); |
| 544 |
Finest->Set("Coordinates", coords); |
| 545 |
|
| 546 |
// Level 0 |
| 547 |
FactoryManager M0; // how to build aggregates and smoother of the first level |
| 548 |
M0.SetKokkosRefactor(false); |
| 549 |
|
| 550 |
// Level 1 (Plain aggregation) |
| 551 |
FactoryManager M1; |
| 552 |
{ |
| 553 |
M1.SetKokkosRefactor(false); |
| 554 |
Teuchos::ParameterList tp_list; |
| 555 |
tp_list.set("tentative: constant column sums", false); |
| 556 |
tp_list.set("tentative: calculate qr", false); |
| 557 |
RCP<TentativePFactory> PnodalFact = rcp(new TentativePFactory()); |
| 558 |
PnodalFact->SetParameterList(tp_list); |
| 559 |
M1.SetFactory("P", PnodalFact); |
| 560 |
M1.SetFactory("Ptent", PnodalFact); // You actually need this |
| 561 |
} |
| 562 |
|
| 563 |
// Level 2 (Plain aggregation) |
| 564 |
FactoryManager M2; |
| 565 |
{ |
| 566 |
M2.SetKokkosRefactor(false); |
| 567 |
Teuchos::ParameterList tp_list; |
| 568 |
tp_list.set("tentative: constant column sums", false); |
| 569 |
tp_list.set("tentative: calculate qr", false); |
| 570 |
RCP<TentativePFactory> PnodalFact = rcp(new TentativePFactory()); |
| 571 |
PnodalFact->SetParameterList(tp_list); |
| 572 |
M2.SetFactory("P", PnodalFact); |
| 573 |
M2.SetFactory("CoarseSolver", Teuchos::null); |
| 574 |
} |
| 575 |
|
| 576 |
bool r; |
| 577 |
r = NodeH.Setup(0, Teuchos::null, rcpFromRef(M0), rcpFromRef(M1)); |
| 578 |
TEST_EQUALITY(r, false); |
| 579 |
r = NodeH.Setup(1, rcpFromRef(M0), rcpFromRef(M1), rcpFromRef(M2)); |
| 580 |
TEST_EQUALITY(r, false); |
| 581 |
r = NodeH.Setup(2, rcpFromRef(M1), rcpFromRef(M2), Teuchos::null); |
| 582 |
TEST_EQUALITY(r, true); |
| 583 |
|
| 584 |
RCP<Level> l0 = NodeH.GetLevel(0); |
| 585 |
RCP<Level> l1 = NodeH.GetLevel(1); |
| 586 |
RCP<Level> l2 = NodeH.GetLevel(2); |
| 587 |
} |
| 588 |
|
| 589 |
out << "*** Copy Node->Edge Data *** " << std::endl; |
| 590 |
// Copy Data to Edge Hierarchy |
| 591 |
for (int i = 0; i < NumLevels; i++) { |
| 592 |
EdgeH.AddNewLevel(); |
| 593 |
RCP<Level> NodeL = NodeH.GetLevel(i); |
| 594 |
RCP<Level> EdgeL = EdgeH.GetLevel(i); |
| 595 |
|
| 596 |
EdgeL->Set("NodeAggMatrix", NodeL->Get<RCP<Matrix> >("A")); |
| 597 |
EdgeL->Set("NodeMatrix", NodeL->Get<RCP<Matrix> >("A")); |
| 598 |
if (i != 0) { |
| 599 |
EdgeL->Set("Pnodal", NodeL->Get<RCP<Matrix> >("P")); |
| 600 |
|
| 601 |
RCP<const Matrix> P = NodeL->Get<RCP<Matrix> >("P"); |
| 602 |
// Xpetra::IO<SC,LO,GO,NO>::Write("Pn.mat",*P); |
| 603 |
} |
| 604 |
|
| 605 |
if (i == 0) { |
| 606 |
EdgeL->Set("A", SM_Matrix); |
| 607 |
EdgeL->Set("D0", D0_Matrix); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
// Generate the Edge Hierarchy |
| 612 |
out << "*** Setting Up Edge Hierarchy *** " << std::endl; |
| 613 |
{ |
| 614 |
FactoryManager M0; // how to build aggregates and smoother of the first level |
| 615 |
M0.SetKokkosRefactor(false); |
| 616 |
|
| 617 |
// Level 1 (Plain aggregation) |
| 618 |
FactoryManager M1; |
| 619 |
{ |
| 620 |
M1.SetKokkosRefactor(false); |
| 621 |
RCP<ReitzingerPFactory> PFact = rcp(new ReitzingerPFactory()); |
| 622 |
M1.SetFactory("Ptent", PFact); |
| 623 |
M1.SetFactory("D0", PFact); |
| 624 |
M1.SetFactory("P", PFact); |
| 625 |
} |
| 626 |
|
| 627 |
// Level 2 (just like level 1) |
| 628 |
FactoryManager M2; |
| 629 |
{ |
| 630 |
M2.SetKokkosRefactor(false); |
| 631 |
RCP<ReitzingerPFactory> PFact = rcp(new ReitzingerPFactory()); |
| 632 |
M2.SetFactory("Ptent", PFact); |
| 633 |
M2.SetFactory("D0", PFact); |
| 634 |
M2.SetFactory("P", PFact); |
| 635 |
M2.SetFactory("CoarseSolver", Teuchos::null); |
| 636 |
} |
| 637 |
|
| 638 |
// Do the setup |
| 639 |
bool r; |
| 640 |
r = EdgeH.Setup(0, Teuchos::null, rcpFromRef(M0), rcpFromRef(M1)); |
| 641 |
TEST_EQUALITY(r, false); |
| 642 |
r = EdgeH.Setup(1, rcpFromRef(M0), rcpFromRef(M1), rcpFromRef(M2)); |
| 643 |
TEST_EQUALITY(r, false); |
| 644 |
r = EdgeH.Setup(2, rcpFromRef(M1), rcpFromRef(M2), Teuchos::null); |
| 645 |
TEST_EQUALITY(r, true); |
| 646 |
} |
| 647 |
|
| 648 |
RCP<Level> node_l1 = NodeH.GetLevel(1); |
| 649 |
RCP<Level> node_l2 = NodeH.GetLevel(2); |
| 650 |
|
| 651 |
RCP<Level> l0 = EdgeH.GetLevel(0); |
| 652 |
RCP<Level> l1 = EdgeH.GetLevel(1); |
| 653 |
RCP<Level> l2 = EdgeH.GetLevel(2); |
| 654 |
|
| 655 |
// l0->print(std::cout,MueLu::Debug); |
| 656 |
// l1->print(std::cout,MueLu::Debug); |
| 657 |
// l2->print(std::cout,MueLu::Debug); |
| 658 |
|
| 659 |
TEST_EQUALITY(l0->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 660 |
TEST_EQUALITY(l1->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 661 |
TEST_EQUALITY(l1->IsAvailable("P", MueLu::NoFactory::get()), true); |
| 662 |
TEST_EQUALITY(l2->IsAvailable("A", MueLu::NoFactory::get()), true); |
| 663 |
TEST_EQUALITY(l2->IsAvailable("P", MueLu::NoFactory::get()), true); |
| 664 |
|
| 665 |
// Check the commuting property |
| 666 |
using MT = typename Teuchos::ScalarTraits<SC>::magnitudeType; |
| 667 |
Teuchos::Array<MT> norm(1), norm0(1); |
| 668 |
norm0[0] = Teuchos::ScalarTraits<MT>::zero(); |
| 669 |
|
| 670 |
norm[0] = CheckCommutingProperty<SC, LO, GO, NO>(*node_l1, *l0, *l1); |
| 671 |
TEST_COMPARE_FLOATING_ARRAYS(norm, norm0, Teuchos::ScalarTraits<MT>::eps() * 100); |
| 672 |
norm[0] = CheckCommutingProperty<SC, LO, GO, NO>(*node_l2, *l1, *l2); |
| 673 |
TEST_COMPARE_FLOATING_ARRAYS(norm, norm0, Teuchos::ScalarTraits<MT>::eps() * 100); |
| 674 |
} |
| 675 |
|
| 676 |
#define MUELU_ETI_GROUP(Scalar, LO, GO, Node) \ |
| 677 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(ReitzingerPFactory, Setup2Level_Unsmoothed, Scalar, LO, GO, Node) \ |
| 678 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(ReitzingerPFactory, Setup2Level_AlphaSmoothed, Scalar, LO, GO, Node) \ |
| 679 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(ReitzingerPFactory, Setup3Level_Unsmoothed, Scalar, LO, GO, Node) \ |
| 680 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(ReitzingerPFactory, Setup3Level_AlphaSmoothed, Scalar, LO, GO, Node) |
| 681 |
|
| 682 |
#include <MueLu_ETI_4arg.hpp> |
| 683 |
|
| 684 |
} // namespace MueLuTests |
| 685 |
|
| 686 |
int main(int argc, char* argv[]) { |
| 687 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 688 |
|
| 689 |
#ifdef HAVE_MUELU_KOKKOS |
| 690 |
Kokkos::initialize(argc, argv); |
| 691 |
#endif |
| 692 |
|
| 693 |
bool success = false; |
| 694 |
bool verbose = true; |
| 695 |
int ierr = -1; |
| 696 |
try { |
| 697 |
// Note: the command line parameter --linAlgebra= is take into account. |
| 698 |
// Xpetra parameters are added to the Teuchos::CommandLineProcessor of Teuchos::UnitTestRepository in MueLu_TestHelpers.cpp |
| 699 |
|
| 700 |
#ifdef ParallelDebug |
| 701 |
RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); |
| 702 |
|
| 703 |
int mypid = comm->getRank(); |
| 704 |
|
| 705 |
if (mypid == 0) std::cout << "Host and Process Ids for tasks" << std::endl; |
| 706 |
for (int i = 0; i < comm->getSize(); i++) { |
| 707 |
if (i == mypid) { |
| 708 |
char buf[80]; |
| 709 |
char hostname[80]; |
| 710 |
gethostname(hostname, sizeof(hostname)); |
| 711 |
int pid = getpid(); |
| 712 |
sprintf(buf, "Host: %s\tMPI rank: %d,\tPID: %d\n\tattach %d\n\tcontinue\n", |
| 713 |
hostname, mypid, pid, pid); |
| 714 |
printf("%s\n", buf); |
| 715 |
fflush(stdout); |
| 716 |
sleep(1); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
if (mypid == 0) { |
| 721 |
printf("** Enter a character to continue > "); |
| 722 |
fflush(stdout); |
| 723 |
char go = ' '; |
| 724 |
scanf("%c", &go); |
| 725 |
} |
| 726 |
comm->barrier(); |
| 727 |
#endif |
| 728 |
|
| 729 |
ierr = Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv); |
| 730 |
|
| 731 |
success = true; |
| 732 |
} |
| 733 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 734 |
|
| 735 |
#ifdef HAVE_MUELU_KOKKOS |
| 736 |
Kokkos::finalize(); |
| 737 |
#endif |
| 738 |
|
| 739 |
return (success ? ierr : EXIT_FAILURE); |
| 740 |
} |