MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_UserAggregationFactory_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_USERAGGREGATIONFACTORY_DEF_HPP_
11#define MUELU_USERAGGREGATIONFACTORY_DEF_HPP_
12
13#include <Xpetra_Map.hpp>
14#include <Xpetra_Vector.hpp>
15#include <Xpetra_MultiVectorFactory.hpp>
16#include <Xpetra_VectorFactory.hpp>
17#include <Xpetra_MapFactory.hpp>
18
20
21#include "MueLu_Level.hpp"
22#include "MueLu_Aggregates.hpp"
23#include "MueLu_Monitor.hpp"
24
25namespace MueLu {
26
27template <class LocalOrdinal, class GlobalOrdinal, class Node>
29 RCP<ParameterList> validParamList = rcp(new ParameterList());
30
31 // input parameters
32 validParamList->set<std::string>("filePrefix", "", "The data is read from files of this name: <filePrefix><level>_<PID>.<fileExt>");
33 validParamList->set<std::string>("fileExt", "", "The data is read from files of this name: <filePrefix><level>_<PID>.<fileExt>");
34
35 return validParamList;
36}
37
38template <class LocalOrdinal, class GlobalOrdinal, class Node>
40
47template <class LocalOrdinal, class GlobalOrdinal, class Node>
49 FactoryMonitor m(*this, "Build", currentLevel);
50
51 const ParameterList& pL = GetParameterList();
52
53 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
54 const int myRank = comm->getRank();
55
56 std::string fileName = pL.get<std::string>("filePrefix") + toString(currentLevel.GetLevelID()) + "_" + toString(myRank) + "." + pL.get<std::string>("fileExt");
57 std::ifstream ifs(fileName.c_str());
58 TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), Exceptions::RuntimeError, "Cannot read data from \"" << fileName << "\"");
59
60 LO numVertices, numAggregates;
61 ifs >> numVertices;
62 TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), Exceptions::RuntimeError, "Cannot read data from \"" << fileName << "\"");
63 ifs >> numAggregates;
64 TEUCHOS_TEST_FOR_EXCEPTION(numVertices <= 0, Exceptions::InvalidArgument, "Number of vertices must be > 0");
65 TEUCHOS_TEST_FOR_EXCEPTION(numAggregates <= 0, Exceptions::InvalidArgument, "Number of aggregates must be > 0");
66
67 Xpetra::UnderlyingLib lib = currentLevel.lib();
68 const int indexBase = 0;
69 RCP<Map> map = MapFactory::Build(lib, numVertices, indexBase, comm);
70
71 RCP<Aggregates> aggregates = rcp(new Aggregates(map));
72 aggregates->setObjectLabel("User");
73
74 aggregates->SetNumAggregates(numAggregates);
75
76 Teuchos::ArrayRCP<LO> vertex2AggId = aggregates->GetVertex2AggId()->getDataNonConst(0);
77 Teuchos::ArrayRCP<LO> procWinner = aggregates->GetProcWinner()->getDataNonConst(0);
78
79 for (LO i = 0; i < numAggregates; i++) {
80 int aggSize = 0;
81 ifs >> aggSize;
82
83 std::vector<LO> list(aggSize);
84 for (int k = 0; k < aggSize; k++) {
85 // FIXME: File contains GIDs, we need LIDs
86 // for now, works on a single processor
87 ifs >> list[k];
88 }
89
90 // Mark first node as root node for the aggregate
91 aggregates->SetIsRoot(list[0]);
92
93 // Fill vertex2AggId and procWinner structure with information
94 for (int k = 0; k < aggSize; k++) {
95 vertex2AggId[list[k]] = i;
96 procWinner[list[k]] = myRank;
97 }
98 }
99
100 // FIXME: do the proper check whether aggregates cross interprocessor boundary
101 aggregates->AggregatesCrossProcessors(false);
102
103 Set(currentLevel, "Aggregates", aggregates);
104
105 GetOStream(Statistics0) << aggregates->description() << std::endl;
106}
107
108} // namespace MueLu
109
110#endif /* MUELU_USERAGGREGATIONFACTORY_DEF_HPP_ */
Container class for aggregation information.
Exception throws to report errors in the internal logical of the program.
Timer to be used in factories. Similar to Monitor but with additional timers.
Class that holds all level-specific information.
int GetLevelID() const
Return level number.
void DeclareInput(Level &currentLevel) const
Input.
void Build(Level &currentLevel) const
Build aggregates.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Namespace for MueLu classes and methods.
std::string toString(const T &what)
Little helper function to convert non-string types to strings.