MueLu  Version of the Day
MueLu_CoordinatesTransferFactory_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_COORDINATESTRANSFER_FACTORY_DEF_HPP
47 #define MUELU_COORDINATESTRANSFER_FACTORY_DEF_HPP
48 
49 #include "Xpetra_ImportFactory.hpp"
50 #include "Xpetra_MultiVectorFactory.hpp"
51 #include "Xpetra_MapFactory.hpp"
52 #include "Xpetra_IO.hpp"
53 
54 #include "MueLu_CoarseMapFactory.hpp"
55 #include "MueLu_Aggregates.hpp"
57 //#include "MueLu_Utilities.hpp"
58 
59 #include "MueLu_Level.hpp"
60 #include "MueLu_Monitor.hpp"
61 
62 namespace MueLu {
63 
64  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
66  RCP<ParameterList> validParamList = rcp(new ParameterList());
67 
68  validParamList->set<RCP<const FactoryBase> >("Coordinates", Teuchos::null, "Factory for coordinates generation");
69  validParamList->set<RCP<const FactoryBase> >("Aggregates", Teuchos::null, "Factory for coordinates generation");
70  validParamList->set<RCP<const FactoryBase> >("CoarseMap", Teuchos::null, "Generating factory of the coarse map");
71  validParamList->set<bool> ("structured aggregation", false, "Flag specifying that the geometric data is transferred for StructuredAggregationFactory");
72  validParamList->set<bool> ("aggregation coupled", false, "Flag specifying if the aggregation algorithm was used in coupled mode.");
73  validParamList->set<bool> ("Geometric", false, "Flag specifying that the coordinates are transferred for GeneralGeometricPFactory");
74  validParamList->set<RCP<const FactoryBase> >("coarseCoordinates", Teuchos::null, "Factory for coarse coordinates generation");
75  validParamList->set<RCP<const FactoryBase> >("gCoarseNodesPerDim", Teuchos::null, "Factory providing the global number of nodes per spatial dimensions of the mesh");
76  validParamList->set<RCP<const FactoryBase> >("lCoarseNodesPerDim", Teuchos::null, "Factory providing the local number of nodes per spatial dimensions of the mesh");
77  validParamList->set<int> ("write start", -1, "first level at which coordinates should be written to file");
78  validParamList->set<int> ("write end", -1, "last level at which coordinates should be written to file");
79  validParamList->set<RCP<const FactoryBase> >("aggregationRegionTypeCoarse", Teuchos::null, "Factory indicating what aggregation type is to be used on the coarse level of the region");
80  validParamList->set<bool> ("hybrid aggregation", false, "Flag specifying that hybrid aggregation data is transfered for HybridAggregationFactory");
81 
82 
83  return validParamList;
84  }
85 
86  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
88  static bool isAvailableCoords = false;
89 
90  const ParameterList& pL = GetParameterList();
91  if(pL.get<bool>("structured aggregation") == true) {
92  if(pL.get<bool>("aggregation coupled") == true) {
93  Input(fineLevel, "gCoarseNodesPerDim");
94  }
95  Input(fineLevel, "lCoarseNodesPerDim");
96  } else if(pL.get<bool>("Geometric") == true) {
97  Input(coarseLevel, "coarseCoordinates");
98  Input(coarseLevel, "gCoarseNodesPerDim");
99  Input(coarseLevel, "lCoarseNodesPerDim");
100  } else {
101  if (coarseLevel.GetRequestMode() == Level::REQUEST)
102  isAvailableCoords = coarseLevel.IsAvailable("Coordinates", this);
103 
104  if (isAvailableCoords == false) {
105  Input(fineLevel, "Coordinates");
106  Input(fineLevel, "Aggregates");
107  Input(fineLevel, "CoarseMap");
108  }
109  }
110  if(pL.get<bool>("hybrid aggregation") == true) {
111  Input(fineLevel,"aggregationRegionTypeCoarse");
112  Input(fineLevel, "lCoarseNodesPerDim");
113  }
114  }
115 
116  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
118  FactoryMonitor m(*this, "Build", coarseLevel);
119 
120  using xdMV = Xpetra::MultiVector<double,LO,GO,NO>;
121 
122  GetOStream(Runtime0) << "Transferring coordinates" << std::endl;
123 
124  RCP<xdMV> coarseCoords;
125  RCP<xdMV> fineCoords;
126  Array<GO> gCoarseNodesPerDir;
127  Array<LO> lCoarseNodesPerDir;
128 
129  const ParameterList& pL = GetParameterList();
130 
131  if(pL.get<bool>("hybrid aggregation") == true) {
132  std::string regionType = Get<std::string>(fineLevel,"aggregationRegionTypeCoarse");
133  lCoarseNodesPerDir = Get<Array<LO> >(fineLevel, "lCoarseNodesPerDim");
134  Set<std::string>(coarseLevel, "aggregationRegionType", regionType);
135  Set< Array<LO> >(coarseLevel, "lNodesPerDim", lCoarseNodesPerDir);
136  }
137 
138  if(pL.get<bool>("structured aggregation") == true) {
139  if(pL.get<bool>("aggregation coupled") == true) {
140  gCoarseNodesPerDir = Get<Array<GO> >(fineLevel, "gCoarseNodesPerDim");
141  Set<Array<GO> >(coarseLevel, "gNodesPerDim", gCoarseNodesPerDir);
142  }
143  lCoarseNodesPerDir = Get<Array<LO> >(fineLevel, "lCoarseNodesPerDim");
144  Set<Array<LO> >(coarseLevel, "lNodesPerDim", lCoarseNodesPerDir);
145  } else if(pL.get<bool>("Geometric") == true) {
146  coarseCoords = Get<RCP<xdMV> >(coarseLevel, "coarseCoordinates");
147  gCoarseNodesPerDir = Get<Array<GO> >(coarseLevel, "gCoarseNodesPerDim");
148  lCoarseNodesPerDir = Get<Array<LO> >(coarseLevel, "lCoarseNodesPerDim");
149  Set<Array<GO> >(coarseLevel, "gNodesPerDim", gCoarseNodesPerDir);
150  Set<Array<LO> >(coarseLevel, "lNodesPerDim", lCoarseNodesPerDir);
151 
152  Set<RCP<xdMV> >(coarseLevel, "Coordinates", coarseCoords);
153 
154  } else {
155  if (coarseLevel.IsAvailable("Coordinates", this)) {
156  GetOStream(Runtime0) << "Reusing coordinates" << std::endl;
157  return;
158  }
159 
160  RCP<Aggregates> aggregates = Get< RCP<Aggregates> > (fineLevel, "Aggregates");
161  fineCoords = Get< RCP<xdMV> >(fineLevel, "Coordinates");
162  RCP<const Map> coarseMap = Get< RCP<const Map> > (fineLevel, "CoarseMap");
163 
164  // coarseMap is being used to set up the domain map of tentative P, and therefore, the row map of Ac
165  // Therefore, if we amalgamate coarseMap, logical nodes in the coordinates vector would correspond to
166  // logical blocks in the matrix
167 
168  ArrayView<const GO> elementAList = coarseMap->getNodeElementList();
169 
170  LO blkSize = 1;
171  if (rcp_dynamic_cast<const StridedMap>(coarseMap) != Teuchos::null)
172  blkSize = rcp_dynamic_cast<const StridedMap>(coarseMap)->getFixedBlockSize();
173 
174  GO indexBase = coarseMap->getIndexBase();
175  size_t numElements = elementAList.size() / blkSize;
176  Array<GO> elementList(numElements);
177 
178  // Amalgamate the map
179  for (LO i = 0; i < Teuchos::as<LO>(numElements); i++)
180  elementList[i] = (elementAList[i*blkSize]-indexBase)/blkSize + indexBase;
181 
182  RCP<const Map> uniqueMap = fineCoords->getMap();
183  RCP<const Map> coarseCoordMap = MapFactory ::Build(coarseMap->lib(), Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(), elementList, indexBase, coarseMap->getComm());
184  coarseCoords = Xpetra::MultiVectorFactory<double,LO,GO,NO>::Build(coarseCoordMap, fineCoords->getNumVectors());
185 
186  // Create overlapped fine coordinates to reduce global communication
187  RCP<xdMV> ghostedCoords = fineCoords;
188  if (aggregates->AggregatesCrossProcessors()) {
189  RCP<const Map> nonUniqueMap = aggregates->GetMap();
190  RCP<const Import> importer = ImportFactory::Build(uniqueMap, nonUniqueMap);
191 
192  ghostedCoords = Xpetra::MultiVectorFactory<double,LO,GO,NO>::Build(nonUniqueMap, fineCoords->getNumVectors());
193  ghostedCoords->doImport(*fineCoords, *importer, Xpetra::INSERT);
194  }
195 
196  // Get some info about aggregates
197  int myPID = uniqueMap->getComm()->getRank();
198  LO numAggs = aggregates->GetNumAggregates();
199  ArrayRCP<LO> aggSizes = aggregates->ComputeAggregateSizes();
200  const ArrayRCP<const LO> vertex2AggID = aggregates->GetVertex2AggId()->getData(0);
201  const ArrayRCP<const LO> procWinner = aggregates->GetProcWinner()->getData(0);
202 
203  // Fill in coarse coordinates
204  for (size_t j = 0; j < fineCoords->getNumVectors(); j++) {
205  ArrayRCP<const double> fineCoordsData = ghostedCoords->getData(j);
206  ArrayRCP<double> coarseCoordsData = coarseCoords->getDataNonConst(j);
207 
208  for (LO lnode = 0; lnode < vertex2AggID.size(); lnode++) {
209  if (procWinner[lnode] == myPID &&
210  lnode < vertex2AggID.size() &&
211  lnode < fineCoordsData.size() && // TAW do not access off-processor coordinates
212  vertex2AggID[lnode] < coarseCoordsData.size() &&
213  Teuchos::ScalarTraits<double>::isnaninf(fineCoordsData[lnode]) == false) {
214  coarseCoordsData[vertex2AggID[lnode]] += fineCoordsData[lnode];
215  }
216  }
217  for (LO agg = 0; agg < numAggs; agg++) {
218  coarseCoordsData[agg] /= aggSizes[agg];
219  }
220  }
221 
222  Set<RCP<xdMV> >(coarseLevel, "Coordinates", coarseCoords);
223  } // if pL.get<bool>("Geometric") == true
224 
225  int writeStart = pL.get<int>("write start"), writeEnd = pL.get<int>("write end");
226  if (writeStart == 0 && fineLevel.GetLevelID() == 0 && writeStart <= writeEnd) {
227  std::ostringstream buf;
228  buf << fineLevel.GetLevelID();
229  std::string fileName = "coordinates_before_rebalance_level_" + buf.str() + ".m";
230  Xpetra::IO<double,LO,GO,NO>::Write(fileName,*fineCoords);
231  }
232  if (writeStart <= coarseLevel.GetLevelID() && coarseLevel.GetLevelID() <= writeEnd) {
233  std::ostringstream buf;
234  buf << coarseLevel.GetLevelID();
235  std::string fileName = "coordinates_before_rebalance_level_" + buf.str() + ".m";
236  Xpetra::IO<double,LO,GO,NO>::Write(fileName,*coarseCoords);
237  }
238  }
239 
240 } // namespace MueLu
241 
242 #endif // MUELU_COORDINATESTRANSFER_FACTORY_DEF_HPP
size_type size() const
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)
RequestMode GetRequestMode() const
Timer to be used in factories. Similar to Monitor but with additional timers.
One-liner description of what is happening.
Namespace for MueLu classes and methods.
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
void DeclareInput(Level &finelevel, Level &coarseLevel) const
Specifies the data that this class needs, and the factories that generate that data.
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
static bool isnaninf(const T &x)
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
const RCP< const Map > GetMap() const
returns (overlapping) map of aggregate/node distribution
LO GetNumAggregates() const
returns the number of aggregates of the current processor. Note: could/should be renamed to GetNumLoc...
const RCP< LOMultiVector > & GetVertex2AggId() const
Returns constant vector that maps local node IDs to local aggregates IDs.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need&#39;s value has been saved.
const RCP< LOVector > & GetProcWinner() const
Returns constant vector that maps local node IDs to owning processor IDs.
void AggregatesCrossProcessors(const bool &flag)
Record whether aggregates include DOFs from other processes.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Teuchos::ArrayRCP< LO > ComputeAggregateSizes(bool forceRecompute=false) const
Compute sizes of aggregates.