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>
51 #include <Xpetra_MultiVectorFactory.hpp>
52 #include <Xpetra_VectorFactory.hpp>
53 #include <Xpetra_MapFactory.hpp>
54 
56 
57 #include "MueLu_Level.hpp"
58 #include "MueLu_Aggregates.hpp"
59 #include "MueLu_Monitor.hpp"
60 
61 namespace MueLu {
62 
63 template <class LocalOrdinal, class GlobalOrdinal, class Node>
65  RCP<ParameterList> validParamList = rcp(new ParameterList());
66 
67  // input parameters
68  validParamList->set<std::string>("filePrefix", "", "The data is read from files of this name: <filePrefix><level>_<PID>.<fileExt>");
69  validParamList->set<std::string>("fileExt", "", "The data is read from files of this name: <filePrefix><level>_<PID>.<fileExt>");
70 
71  return validParamList;
72 }
73 
74 template <class LocalOrdinal, class GlobalOrdinal, class Node>
76 
83 template <class LocalOrdinal, class GlobalOrdinal, class Node>
85  FactoryMonitor m(*this, "Build", currentLevel);
86 
87  const ParameterList& pL = GetParameterList();
88 
90  const int myRank = comm->getRank();
91 
92  std::string fileName = pL.get<std::string>("filePrefix") + toString(currentLevel.GetLevelID()) + "_" + toString(myRank) + "." + pL.get<std::string>("fileExt");
93  std::ifstream ifs(fileName.c_str());
94  TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), Exceptions::RuntimeError, "Cannot read data from \"" << fileName << "\"");
95 
96  LO numVertices, numAggregates;
97  ifs >> numVertices;
98  TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), Exceptions::RuntimeError, "Cannot read data from \"" << fileName << "\"");
99  ifs >> numAggregates;
100  TEUCHOS_TEST_FOR_EXCEPTION(numVertices <= 0, Exceptions::InvalidArgument, "Number of vertices must be > 0");
101  TEUCHOS_TEST_FOR_EXCEPTION(numAggregates <= 0, Exceptions::InvalidArgument, "Number of aggregates must be > 0");
102 
103  Xpetra::UnderlyingLib lib = currentLevel.lib();
104  const int indexBase = 0;
105  RCP<Map> map = MapFactory::Build(lib, numVertices, indexBase, comm);
106 
107  RCP<Aggregates> aggregates = rcp(new Aggregates(map));
108  aggregates->setObjectLabel("User");
109 
110  aggregates->SetNumAggregates(numAggregates);
111 
112  Teuchos::ArrayRCP<LO> vertex2AggId = aggregates->GetVertex2AggId()->getDataNonConst(0);
113  Teuchos::ArrayRCP<LO> procWinner = aggregates->GetProcWinner()->getDataNonConst(0);
114 
115  for (LO i = 0; i < numAggregates; i++) {
116  int aggSize = 0;
117  ifs >> aggSize;
118 
119  std::vector<LO> list(aggSize);
120  for (int k = 0; k < aggSize; k++) {
121  // FIXME: File contains GIDs, we need LIDs
122  // for now, works on a single processor
123  ifs >> list[k];
124  }
125 
126  // Mark first node as root node for the aggregate
127  aggregates->SetIsRoot(list[0]);
128 
129  // Fill vertex2AggId and procWinner structure with information
130  for (int k = 0; k < aggSize; k++) {
131  vertex2AggId[list[k]] = i;
132  procWinner[list[k]] = myRank;
133  }
134  }
135 
136  // FIXME: do the proper check whether aggregates cross interprocessor boundary
137  aggregates->AggregatesCrossProcessors(false);
138 
139  Set(currentLevel, "Aggregates", aggregates);
140 
141  GetOStream(Statistics0) << aggregates->description() << std::endl;
142 }
143 
144 } // namespace MueLu
145 
146 #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.