MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_UserPFactory_def.hpp
Go to the documentation of this file.
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 #ifndef MUELU_USERPFACTORY_DEF_HPP
11 #define MUELU_USERPFACTORY_DEF_HPP
12 
13 #include <Xpetra_MultiVectorFactory.hpp>
14 #include <Xpetra_Matrix.hpp>
15 #include <Xpetra_IO.hpp>
16 
17 #include "MueLu_Monitor.hpp"
18 #include "MueLu_PerfUtils.hpp"
20 
21 namespace MueLu {
22 
23 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
25  RCP<ParameterList> validParamList = rcp(new ParameterList());
26 
27  validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the matrix A");
28  validParamList->set<RCP<const FactoryBase> >("Nullspace", Teuchos::null, "Generating factory of the nullspace");
29  validParamList->set<std::string>("matrixFileName", "", "File with matrix data");
30  validParamList->set<std::string>("mapFileName", "", "File with coarse map data");
31 
32  return validParamList;
33 }
34 
35 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
37  Input(fineLevel, "A");
38  Input(fineLevel, "Nullspace");
39 }
40 
41 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
43  return BuildP(fineLevel, coarseLevel);
44 }
45 
46 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
48  FactoryMonitor m(*this, "Build", coarseLevel);
49 
50  RCP<Matrix> A = Get<RCP<Matrix> >(fineLevel, "A");
51  RCP<MultiVector> fineNullspace = Get<RCP<MultiVector> >(fineLevel, "Nullspace");
52 
53  TEUCHOS_TEST_FOR_EXCEPTION(A->GetFixedBlockSize() != 1, Exceptions::RuntimeError, "Block size > 1 has not been implemented");
54 
55  const Teuchos::ParameterList& pL = GetParameterList();
56 
57  std::string mapFile = pL.get<std::string>("mapFileName");
58  RCP<const Map> rowMap = A->getRowMap();
59  RCP<const Map> coarseMap = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::ReadMap(mapFile, rowMap->lib(), rowMap->getComm());
60  Set(coarseLevel, "CoarseMap", coarseMap);
61 
62  std::string matrixFile = pL.get<std::string>("matrixFileName");
63  RCP<Matrix> P = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Read(matrixFile, rowMap, coarseMap, coarseMap, rowMap);
64 #if 1
65  Set(coarseLevel, "P", P);
66 #else
67  // Expand column map by 1
69  P = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Read(matrixFile, rowMap, P1->getColMap(), coarseMap, rowMap);
70  Set(coarseLevel, "P", P);
71 #endif
72 
73  RCP<MultiVector> coarseNullspace = MultiVectorFactory::Build(coarseMap, fineNullspace->getNumVectors());
74  P->apply(*fineNullspace, *coarseNullspace, Teuchos::TRANS, Teuchos::ScalarTraits<SC>::one(), Teuchos::ScalarTraits<SC>::zero());
75  Set(coarseLevel, "Nullspace", coarseNullspace);
76 
77  // Coordinates transfer
78  size_t n = Teuchos::as<size_t>(sqrt(coarseMap->getGlobalNumElements()));
79  TEUCHOS_TEST_FOR_EXCEPTION(n * n != coarseMap->getGlobalNumElements(), Exceptions::RuntimeError, "Unfortunately, this is not the case, don't know what to do");
80 
81  RCP<MultiVector> coarseCoords = MultiVectorFactory::Build(coarseMap, 2);
82  ArrayRCP<Scalar> x = coarseCoords->getDataNonConst(0), y = coarseCoords->getDataNonConst(1);
83  for (size_t LID = 0; LID < coarseMap->getLocalNumElements(); ++LID) {
84  GlobalOrdinal GID = coarseMap->getGlobalElement(LID) - coarseMap->getIndexBase();
85  GlobalOrdinal i = GID % n, j = GID / n;
86  x[LID] = i;
87  y[LID] = j;
88  }
89  Set(coarseLevel, "Coordinates", coarseCoords);
90 
91  if (IsPrint(Statistics1)) {
92  RCP<ParameterList> params = rcp(new ParameterList());
93  params->set("printLoadBalancingInfo", true);
94  GetOStream(Statistics1) << PerfUtils::PrintMatrixInfo(*P, "P", params);
95  }
96 }
97 
98 } // namespace MueLu
99 
100 #define MUELU_USERPFACTORY_SHORT
101 #endif // MUELU_USERPFACTORY_DEF_HPP
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print more statistics.
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
static RCP< const Map > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
static void Multiply(const Matrix &A, bool transposeA, const Matrix &B, bool transposeB, Matrix &C, bool call_FillComplete_on_result=true, bool doOptimizeStorage=true, const std::string &label=std::string(), const RCP< ParameterList > &params=null)
static Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Read(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm, bool binary=false)
Exception throws to report errors in the internal logical of the program.