MueLu  Version of the Day
MueLu_UserAggregationFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_USERAGGREGATIONFACTORY_DEF_HPP_
47 #define MUELU_USERAGGREGATIONFACTORY_DEF_HPP_
48 
49 #include <Xpetra_Map.hpp>
50 #include <Xpetra_Vector.hpp>
51 #include <Xpetra_VectorFactory.hpp>
52 #include <Xpetra_MapFactory.hpp>
53 
55 
56 #include "MueLu_Level.hpp"
57 #include "MueLu_GraphBase.hpp"
58 #include "MueLu_Aggregates.hpp"
59 #include "MueLu_Monitor.hpp"
60 #include "MueLu_Utilities.hpp"
61 
62 namespace MueLu {
63 
64  template <class LocalOrdinal, class GlobalOrdinal, class Node>
66  RCP<ParameterList> validParamList = rcp(new ParameterList());
67 
68  // input parameters
69  validParamList->set<std::string>("filePrefix", "", "The data is read from files of this name: <filePrefix><level>_<PID>.<fileExt>");
70  validParamList->set<std::string>("fileExt", "", "The data is read from files of this name: <filePrefix><level>_<PID>.<fileExt>");
71 
72  return validParamList;
73  }
74 
75  template <class LocalOrdinal, class GlobalOrdinal, class Node>
77 
84  template <class LocalOrdinal, class GlobalOrdinal, class Node>
86  FactoryMonitor m(*this, "Build", currentLevel);
87 
88  const ParameterList& pL = GetParameterList();
89 
90  RCP< const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
91  const int myRank = comm->getRank();
92 
93  std::string fileName = pL.get<std::string>("filePrefix") + toString(currentLevel.GetLevelID()) + "_" + toString(myRank) + "." + pL.get<std::string>("fileExt");
94  std::ifstream ifs(fileName.c_str());
95  TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), Exceptions::RuntimeError, "Cannot read data from \"" << fileName << "\"");
96 
97  LO numVertices, numAggregates;
98  ifs >> numVertices;
99  TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), Exceptions::RuntimeError, "Cannot read data from \"" << fileName << "\"");
100  ifs >> numAggregates;
101  TEUCHOS_TEST_FOR_EXCEPTION(numVertices <= 0, Exceptions::InvalidArgument, "Number of vertices must be > 0");
102  TEUCHOS_TEST_FOR_EXCEPTION(numAggregates <= 0, Exceptions::InvalidArgument, "Number of aggregates must be > 0");
103 
104  Xpetra::UnderlyingLib lib = currentLevel.lib();
105  const int indexBase = 0;
106  RCP<Map> map = MapFactory::Build(lib, numVertices, indexBase, comm);
107 
108  RCP<Aggregates> aggregates = rcp(new Aggregates(map));
109  aggregates->setObjectLabel("User");
110 
111  aggregates->SetNumAggregates(numAggregates);
112 
113  Teuchos::ArrayRCP<LO> vertex2AggId = aggregates->GetVertex2AggId()->getDataNonConst(0);
114  Teuchos::ArrayRCP<LO> procWinner = aggregates->GetProcWinner() ->getDataNonConst(0);
115 
116  for (LO i = 0; i < numAggregates; i++) {
117  int aggSize = 0;
118  ifs >> aggSize;
119 
120  std::vector<LO> list(aggSize);
121  for (int k = 0; k < aggSize; k++) {
122  // FIXME: File contains GIDs, we need LIDs
123  // for now, works on a single processor
124  ifs >> list[k];
125  }
126 
127  // Mark first node as root node for the aggregate
128  aggregates->SetIsRoot(list[0]);
129 
130  // Fill vertex2AggId and procWinner structure with information
131  for (int k = 0; k < aggSize; k++) {
132  vertex2AggId[list[k]] = i;
133  procWinner [list[k]] = myRank;
134  }
135  }
136 
137  // FIXME: do the proper check whether aggregates cross interprocessor boundary
138  aggregates->AggregatesCrossProcessors(false);
139 
140  Set(currentLevel, "Aggregates", aggregates);
141 
142  GetOStream(Statistics0) << aggregates->description() << std::endl;
143  }
144 
145 } //namespace MueLu
146 
147 #endif /* MUELU_USERAGGREGATIONFACTORY_DEF_HPP_ */
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
Container class for aggregation information.
Timer to be used in factories. Similar to Monitor but with additional timers.
LocalOrdinal LO
Namespace for MueLu classes and methods.
void DeclareInput(Level &currentLevel) const
Input.
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
void Build(Level &currentLevel) const
Build aggregates.
Exception throws to report errors in the internal logical of the program.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.