MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
52 #include <Xpetra_VectorFactory.hpp>
53 #include <Xpetra_MapFactory.hpp>
54 
56 
57 #include "MueLu_Level.hpp"
58 #include "MueLu_GraphBase.hpp"
59 #include "MueLu_Aggregates.hpp"
60 #include "MueLu_Monitor.hpp"
61 #include "MueLu_Utilities.hpp"
62 
63 namespace MueLu {
64 
65  template <class LocalOrdinal, class GlobalOrdinal, class Node>
67  RCP<ParameterList> validParamList = rcp(new ParameterList());
68 
69  // input parameters
70  validParamList->set<std::string>("filePrefix", "", "The data is read from files of this name: <filePrefix><level>_<PID>.<fileExt>");
71  validParamList->set<std::string>("fileExt", "", "The data is read from files of this name: <filePrefix><level>_<PID>.<fileExt>");
72 
73  return validParamList;
74  }
75 
76  template <class LocalOrdinal, class GlobalOrdinal, class Node>
78 
85  template <class LocalOrdinal, class GlobalOrdinal, class Node>
87  FactoryMonitor m(*this, "Build", currentLevel);
88 
89  const ParameterList& pL = GetParameterList();
90 
92  const int myRank = comm->getRank();
93 
94  std::string fileName = pL.get<std::string>("filePrefix") + toString(currentLevel.GetLevelID()) + "_" + toString(myRank) + "." + pL.get<std::string>("fileExt");
95  std::ifstream ifs(fileName.c_str());
96  TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), Exceptions::RuntimeError, "Cannot read data from \"" << fileName << "\"");
97 
98  LO numVertices, numAggregates;
99  ifs >> numVertices;
100  TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), Exceptions::RuntimeError, "Cannot read data from \"" << fileName << "\"");
101  ifs >> numAggregates;
102  TEUCHOS_TEST_FOR_EXCEPTION(numVertices <= 0, Exceptions::InvalidArgument, "Number of vertices must be > 0");
103  TEUCHOS_TEST_FOR_EXCEPTION(numAggregates <= 0, Exceptions::InvalidArgument, "Number of aggregates must be > 0");
104 
105  Xpetra::UnderlyingLib lib = currentLevel.lib();
106  const int indexBase = 0;
107  RCP<Map> map = MapFactory::Build(lib, numVertices, indexBase, comm);
108 
109  RCP<Aggregates> aggregates = rcp(new Aggregates(map));
110  aggregates->setObjectLabel("User");
111 
112  aggregates->SetNumAggregates(numAggregates);
113 
114  Teuchos::ArrayRCP<LO> vertex2AggId = aggregates->GetVertex2AggId()->getDataNonConst(0);
115  Teuchos::ArrayRCP<LO> procWinner = aggregates->GetProcWinner() ->getDataNonConst(0);
116 
117  for (LO i = 0; i < numAggregates; i++) {
118  int aggSize = 0;
119  ifs >> aggSize;
120 
121  std::vector<LO> list(aggSize);
122  for (int k = 0; k < aggSize; k++) {
123  // FIXME: File contains GIDs, we need LIDs
124  // for now, works on a single processor
125  ifs >> list[k];
126  }
127 
128  // Mark first node as root node for the aggregate
129  aggregates->SetIsRoot(list[0]);
130 
131  // Fill vertex2AggId and procWinner structure with information
132  for (int k = 0; k < aggSize; k++) {
133  vertex2AggId[list[k]] = i;
134  procWinner [list[k]] = myRank;
135  }
136  }
137 
138  // FIXME: do the proper check whether aggregates cross interprocessor boundary
139  aggregates->AggregatesCrossProcessors(false);
140 
141  Set(currentLevel, "Aggregates", aggregates);
142 
143  GetOStream(Statistics0) << aggregates->description() << std::endl;
144  }
145 
146 } //namespace MueLu
147 
148 #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.
void DeclareInput(Level &currentLevel) const
Input.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
LocalOrdinal LO
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
void Build(Level &currentLevel) const
Build aggregates.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
Exception throws to report errors in the internal logical of the program.