MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_MapTransferFactory_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 
47 #ifndef MUELU_MAPTRANSFERFACTORY_DEF_HPP_
48 #define MUELU_MAPTRANSFERFACTORY_DEF_HPP_
49 
51 
52 #include <Xpetra_Map.hpp>
53 #include <Xpetra_MapFactory.hpp>
54 #include <Xpetra_Matrix.hpp>
55 
56 #include "MueLu_Level.hpp"
58 #include "MueLu_Monitor.hpp"
59 
60 namespace MueLu {
61 
62  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
64  {
65  }
66 
67 
68  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
70  RCP<ParameterList> validParamList = rcp(new ParameterList());
71 
72  //#define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
73  //SET_VALID_ENTRY("sa: damping factor");
74  //SET_VALID_ENTRY("sa: calculate eigenvalue estimate");
75  //SET_VALID_ENTRY("sa: eigenvalue estimate num iterations");
76  //#undef SET_VALID_ENTRY
77 
78  validParamList->setEntry("map: name", Teuchos::ParameterEntry(std::string("")));
79  validParamList->setEntry("map: factory", Teuchos::ParameterEntry(std::string("null")));
80  validParamList->set< RCP<const FactoryBase> >("P", Teuchos::null, "Tentative prolongator factory");
81 
82  return validParamList;
83  }
84 
85  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
87  const ParameterList & pL = GetParameterList();
88  std::string mapFactName = pL.get<std::string>("map: factory");
89  std::string mapName = pL.get<std::string>("map: name");
90 
91  // check whether user has provided a specific name for the MapFactory
92  if (mapFactName == "" || mapFactName == "NoFactory") {
93  mapFact_ = MueLu::NoFactory::getRCP();
94  }
95  else if (mapFactName != "null") {
96  mapFact_ = coarseLevel.GetFactoryManager()->GetFactory(mapFactName);
97  }
98 
99  // request map generated by mapFact_
100  fineLevel.DeclareInput(mapName, mapFact_.get(), this);
101 
102  // request Ptent
103  // note that "P" provided by the user (through XML file) is supposed to be of type TentativePFactory
104  Teuchos::RCP<const FactoryBase> tentPFact = GetFactory("P");
105  if (tentPFact == Teuchos::null)
106  tentPFact = coarseLevel.GetFactoryManager()->GetFactory("Ptent");
107  coarseLevel.DeclareInput("P", tentPFact.get(), this);
108  }
109 
110  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
112  Monitor m(*this, "Contact Map transfer factory");
113 
114  const ParameterList & pL = GetParameterList();
115  std::string mapName = pL.get<std::string>("map: name");
116 
117  if (fineLevel.IsAvailable(mapName, mapFact_.get())==false)
118  GetOStream(Runtime0) << "MapTransferFactory::Build: User provided map " << mapName << " not found in Level class." << std::endl;
119 
120  // fetch map extractor from level
121  RCP<Map> transferMap = fineLevel.Get<RCP<Map> >(mapName,mapFact_.get());
122 
123  // Get default tentative prolongator factory
124  // Getting it that way ensure that the same factory instance will be used for both SaPFactory and NullspaceFactory.
125  // -- Warning: Do not use directly initialPFact_. Use initialPFact instead everywhere!
126  RCP<const FactoryBase> tentPFact = GetFactory("P");
127  if (tentPFact == Teuchos::null)
128  tentPFact = coarseLevel.GetFactoryManager()->GetFactory("Ptent");
129  TEUCHOS_TEST_FOR_EXCEPTION(!coarseLevel.IsAvailable("P", tentPFact.get()), Exceptions::RuntimeError,
130  "MueLu::MapTransferFactory::Build(): P (generated by TentativePFactory) not available.");
131  RCP<Matrix> Ptent = coarseLevel.Get<RCP<Matrix> >("P", tentPFact.get());
132 
133  std::vector<GO > coarseMapGids;
134 
135  // loop over local rows of Ptent
136  for (size_t row=0; row<Ptent->getNodeNumRows(); row++) {
137  GO grid = Ptent->getRowMap()->getGlobalElement(row);
138 
139  if (transferMap->isNodeGlobalElement(grid)) {
142  Ptent->getLocalRowView(row, indices, vals);
143 
144  for (size_t i = 0; i < as<size_t>(indices.size()); i++) {
145  // mark all columns in Ptent(grid,*) to be coarse Dofs of next level transferMap
146  GO gcid = Ptent->getColMap()->getGlobalElement(indices[i]);
147  coarseMapGids.push_back(gcid);
148  }
149  } // end if isNodeGlobalElement(grid)
150  }
151 
152  // build column maps
154  std::sort(coarseMapGids.begin(), coarseMapGids.end());
155  coarseMapGids.erase(std::unique(coarseMapGids.begin(), coarseMapGids.end()), coarseMapGids.end());
156  Teuchos::ArrayView<GO> coarseMapGidsView(&coarseMapGids[0], coarseMapGids.size());
157  Teuchos::RCP<Map> coarseTransferMap = MapFactory::Build(Ptent->getColMap()->lib(), INVALID, coarseMapGidsView, Ptent->getColMap()->getIndexBase(), Ptent->getColMap()->getComm());
158 
159  // store map extractor in coarse level
160  coarseLevel.Set(mapName, coarseTransferMap, mapFact_.get());
161  }
162 
163 } // namespace MueLu
164 
165 #endif /* MUELU_MAPTRANSFERFACTORY_DEF_HPP_ */
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access). Usage: Level-&gt;Get&lt; RCP&lt;Matrix&gt; &gt;(&quot;A&quot;, factory) if factory == NULL =&gt; use default factory.
GlobalOrdinal GO
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)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_type size() const
One-liner description of what is happening.
T * get() const
ParameterList & setEntry(const std::string &name, const ParameterEntry &entry)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
Timer to be used in non-factories.
const RCP< const FactoryManagerBase > GetFactoryManager()
returns the current factory manager
Definition: MueLu_Level.cpp:96
Exception throws to report errors in the internal logical of the program.
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need&#39;s value has been saved.
static const RCP< const NoFactory > getRCP()
Static Get() functions.