MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_CoarseMapFactory_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  * MueLu_CoarseMapFactory_def.hpp
48  *
49  * Created on: Oct 12, 2012
50  * Author: wiesner
51  */
52 
53 #ifndef MUELU_COARSEMAPFACTORY_DEF_HPP_
54 #define MUELU_COARSEMAPFACTORY_DEF_HPP_
55 
56 #include <Teuchos_Array.hpp>
57 
58 #include <Xpetra_MultiVector.hpp>
60 
62 #include "MueLu_Level.hpp"
63 #include "MueLu_Aggregates.hpp"
64 #include "MueLu_Monitor.hpp"
65 
66 namespace MueLu {
67 
68  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
70  {
71  stridingInfo_.clear();
72  }
73 
74  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
76 
77  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
79  RCP<ParameterList> validParamList = rcp(new ParameterList());
80 
81  validParamList->set< RCP<const FactoryBase> >("Aggregates", Teuchos::null, "Generating factory for aggregates.");
82  validParamList->set< RCP<const FactoryBase> >("Nullspace", Teuchos::null, "Generating factory for null space.");
83 
84  validParamList->set< std::string >("Striding info", "{}", "Striding information");
85  validParamList->set< LocalOrdinal >("Strided block id", -1, "Strided block id");
86 
87  // Domain GID offsets: list of offset values for domain gids (coarse gids) of tentative prolongator (default = 0).
88  // For each multigrid level we can define a different offset value, ie for the prolongator between
89  // level 0 and level 1 we use the offset entry domainGidOffsets_[0], for the prolongator between
90  // level 1 and level 2 we use domainGidOffsets_[1]...
91  // If the vector domainGidOffsets_ is too short and does not contain a sufficient number of gid offset
92  // values for all levels, a gid offset of 0 is used for all the remaining levels!
93  // The GIDs for the domain dofs of Ptent start with domainGidOffset, are contiguous and distributed
94  // equally over the procs (unless some reordering is done).
95  validParamList->set< std::string > ("Domain GID offsets", "{0}", "vector with offsets for GIDs for each level. If no offset GID value is given for the level we use 0 as default.");
96 
97  return validParamList;
98  }
99 
100  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
102  Input(currentLevel, "Aggregates");
103  Input(currentLevel, "Nullspace");
104  }
105 
106  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
108  // store striding map in internal variable
109  stridingInfo_ = stridingInfo;
110 
111  // try to remove string "Striding info" from parameter list to make sure,
112  // that stridingInfo_ is not replaced in the Build call.
113  std::string strStridingInfo; strStridingInfo.clear();
114  SetParameter("Striding info", ParameterEntry(strStridingInfo));
115  }
116 
117  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
119  FactoryMonitor m(*this, "Build", currentLevel);
120 
121  RCP<Aggregates> aggregates = Get< RCP<Aggregates> >(currentLevel, "Aggregates");
122  RCP<MultiVector> nullspace = Get< RCP<MultiVector> >(currentLevel, "Nullspace");
123 
124  GlobalOrdinal numAggs = aggregates->GetNumAggregates();
125  const size_t NSDim = nullspace->getNumVectors();
126  RCP<const Teuchos::Comm<int> > comm = aggregates->GetMap()->getComm();
127 
128  // read in offset information from parameter list and fill the internal member variable
129  GlobalOrdinal domainGidOffset = 0;
130  std::vector<GlobalOrdinal> domainGidOffsets;
131  domainGidOffsets.clear();
132  const ParameterList & pL = GetParameterList();
133  if(pL.isParameter("Domain GID offsets")) {
134  std::string strDomainGIDs = pL.get<std::string>("Domain GID offsets");
135  if(strDomainGIDs.empty() == false) {
136  Teuchos::Array<GlobalOrdinal> arrayVal = Teuchos::fromStringToArray<GlobalOrdinal>(strDomainGIDs);
137  domainGidOffsets = Teuchos::createVector(arrayVal);
138  if(currentLevel.GetLevelID() < Teuchos::as<int>(domainGidOffsets.size()) ) {
139  domainGidOffset = domainGidOffsets[currentLevel.GetLevelID()];
140  }
141  }
142  }
143 
144  LocalOrdinal stridedBlockId = pL.get<LocalOrdinal>("Strided block id");
145 
146  // read in stridingInfo from parameter list and fill the internal member variable
147  // read the data only if the parameter "Striding info" exists and is non-empty
148  //const ParameterList & pL = GetParameterList();
149  if(pL.isParameter("Striding info")) {
150  std::string strStridingInfo = pL.get<std::string>("Striding info");
151  if(strStridingInfo.empty() == false) {
152  Teuchos::Array<size_t> arrayVal = Teuchos::fromStringToArray<size_t>(strStridingInfo);
153  stridingInfo_ = Teuchos::createVector(arrayVal);
154  }
155  }
156 
157 
158  // check for consistency of striding information with NSDim and nCoarseDofs
159  if (stridedBlockId== -1) {
160  // this means we have no real strided map but only a block map with constant blockSize "NSDim"
161  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo_.size() > 1, Exceptions::RuntimeError, "MueLu::CoarseMapFactory::Build(): stridingInfo_.size() but must be one");
162  stridingInfo_.clear();
163  stridingInfo_.push_back(NSDim);
164  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo_.size() != 1, Exceptions::RuntimeError, "MueLu::CoarseMapFactory::Build(): stridingInfo_.size() but must be one");
165 
166  } else {
167  // stridedBlockId > -1, set by user
168  TEUCHOS_TEST_FOR_EXCEPTION(stridedBlockId > Teuchos::as<LO>(stridingInfo_.size() - 1) , Exceptions::RuntimeError, "MueLu::CoarseMapFactory::Build(): it is stridingInfo_.size() <= stridedBlockId_. error.");
169  size_t stridedBlockSize = stridingInfo_[stridedBlockId];
170  TEUCHOS_TEST_FOR_EXCEPTION(stridedBlockSize != NSDim , Exceptions::RuntimeError, "MueLu::CoarseMapFactory::Build(): dimension of strided block != NSDim. error.");
171  }
172 
173  GetOStream(Statistics2) << "domainGIDOffset: " << domainGidOffset << " block size: " << getFixedBlockSize() << " stridedBlockId: " << stridedBlockId << std::endl;
174 
175  // number of coarse level dofs (fixed by number of aggregates and blocksize data)
176  GlobalOrdinal nCoarseDofs = numAggs * getFixedBlockSize();
177  GlobalOrdinal indexBase = aggregates->GetMap()->getIndexBase();
178 
179  RCP<const Map> coarseMap = StridedMapFactory::Build(aggregates->GetMap()->lib(),
181  nCoarseDofs,
182  indexBase,
183  stridingInfo_,
184  comm,
185  stridedBlockId,
186  domainGidOffset);
187 
188  Set(currentLevel, "CoarseMap", coarseMap);
189  } // Build
190 
191 } //namespace MueLu
192 
193 #endif /* MUELU_COARSEMAPFACTORY_DEF_HPP_ */
T & get(const std::string &name, T def_value)
void Build(Level &currentLevel) const
Build an object with this factory.
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)
const RCP< const Map > GetMap() const
returns (overlapping) map of aggregate/node distribution
virtual ~CoarseMapFactory()
Destructor.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Print even more statistics.
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void DeclareInput(Level &currentLevel) const
Specifies the data that this class needs, and the factories that generate that data.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
LO GetNumAggregates() const
returns the number of aggregates of the current processor. Note: could/should be renamed to GetNumLoc...
virtual void setStridingData(std::vector< size_t > stridingInfo)
setStridingData set striding vector for the domain DOF map (= coarse map), e.g. (2,1) for 2 velocity dofs and 1 pressure dof
Exception throws to report errors in the internal logical of the program.