MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_VariableDofLaplacianFactory_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 // Tobias Wiesner (tawiesn@sandia.gov)
42 // Ray Tuminaro (rstumin@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_
48 #define PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_
49 
50 #include "MueLu_Monitor.hpp"
51 
53 
54 namespace MueLu {
55 
56 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
58  RCP<ParameterList> validParamList = rcp(new ParameterList());
59 
60  validParamList->set<double>("Advanced Dirichlet: threshold", 1e-5, "Drop tolerance for Dirichlet detection");
61  validParamList->set<double>("Variable DOF amalgamation: threshold", 1.8e-9, "Drop tolerance for amalgamation process");
62  validParamList->set<int>("maxDofPerNode", 1, "Maximum number of DOFs per node");
63 
64  validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the matrix A");
65  validParamList->set<RCP<const FactoryBase> >("Coordinates", Teuchos::null, "Generating factory for Coordinates");
66 
67  return validParamList;
68 }
69 
70 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
72 
73 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
75  Input(currentLevel, "A");
76  Input(currentLevel, "Coordinates");
77 
78  // if (currentLevel.GetLevelID() == 0) // TODO check for finest level (special treatment)
79  if (currentLevel.IsAvailable("DofPresent", NoFactory::get())) {
80  currentLevel.DeclareInput("DofPresent", NoFactory::get(), this);
81  }
82 }
83 
84 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
86  FactoryMonitor m(*this, "Build", currentLevel);
87  typedef Teuchos::ScalarTraits<SC> STS;
88 
89  const ParameterList& pL = GetParameterList();
90 
91  RCP<Matrix> A = Get<RCP<Matrix> >(currentLevel, "A");
92 
93  Teuchos::RCP<const Teuchos::Comm<int> > comm = A->getRowMap()->getComm();
94  Xpetra::UnderlyingLib lib = A->getRowMap()->lib();
95 
97  RCP<dxMV> Coords = Get<RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO> > >(currentLevel, "Coordinates");
98 
99  int maxDofPerNode = pL.get<int>("maxDofPerNode");
100  Scalar dirDropTol = Teuchos::as<Scalar>(pL.get<double>("Advanced Dirichlet: threshold")); // "ML advnaced Dirichlet: threshold"
101  Scalar amalgDropTol = Teuchos::as<Scalar>(pL.get<double>("Variable DOF amalgamation: threshold")); //"variable DOF amalgamation: threshold")
102 
103  bool bHasZeroDiagonal = false;
105 
106  // check availability of DofPresent array
108  if (currentLevel.IsAvailable("DofPresent", NoFactory::get())) {
109  dofPresent = currentLevel.Get<Teuchos::ArrayRCP<LocalOrdinal> >("DofPresent", NoFactory::get());
110  } else {
111  // TAW: not sure about size of array. We cannot determine the expected size in the non-padded case correctly...
112  dofPresent = Teuchos::ArrayRCP<LocalOrdinal>(A->getRowMap()->getLocalNumElements(), 1);
113  }
114 
115  // map[k] indicates that the kth dof in the variable dof matrix A would
116  // correspond to the map[k]th dof in the padded system. If, i.e., it is
117  // map[35] = 39 then dof no 35 in the variable dof matrix A corresponds to
118  // row map id 39 in an imaginary padded matrix Apadded.
119  // The padded system is never built but would be the associated matrix if
120  // every node had maxDofPerNode dofs.
121  std::vector<LocalOrdinal> map(A->getLocalNumRows());
122  this->buildPaddedMap(dofPresent, map, A->getLocalNumRows());
123 
124  // map of size of number of DOFs containing local node id (dof id -> node id, inclusive ghosted dofs/nodes)
125  std::vector<LocalOrdinal> myLocalNodeIds(A->getColMap()->getLocalNumElements()); // possible maximum (we need the ghost nodes, too)
126 
127  // assign the local node ids for the ghosted nodes
128  size_t nLocalNodes, nLocalPlusGhostNodes;
129  this->assignGhostLocalNodeIds(A->getRowMap(), A->getColMap(), myLocalNodeIds, map, maxDofPerNode, nLocalNodes, nLocalPlusGhostNodes, comm);
130 
131  // RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)," ",0,false,10,false, true);
132 
133  TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(dofPresent.size()) != Teuchos::as<size_t>(nLocalNodes * maxDofPerNode), MueLu::Exceptions::RuntimeError, "VariableDofLaplacianFactory: size of provided DofPresent array is " << dofPresent.size() << " but should be " << nLocalNodes * maxDofPerNode << " on the current processor.");
134 
135  // put content of assignGhostLocalNodeIds here...
136 
137  // fill nodal maps
138 
139  Teuchos::ArrayView<const GlobalOrdinal> myGids = A->getColMap()->getLocalElementList();
140 
141  // vector containing row/col gids of amalgamated matrix (with holes)
142 
143  size_t nLocalDofs = A->getRowMap()->getLocalNumElements();
144  size_t nLocalPlusGhostDofs = A->getColMap()->getLocalNumElements();
145 
146  // myLocalNodeIds (dof -> node)
147 
148  Teuchos::Array<GlobalOrdinal> amalgRowMapGIDs(nLocalNodes);
149  Teuchos::Array<GlobalOrdinal> amalgColMapGIDs(nLocalPlusGhostNodes);
150 
151  // initialize
152  size_t count = 0;
153  if (nLocalDofs > 0) {
154  amalgRowMapGIDs[count] = myGids[0];
155  amalgColMapGIDs[count] = myGids[0];
156  count++;
157  }
158 
159  for (size_t i = 1; i < nLocalDofs; i++) {
160  if (myLocalNodeIds[i] != myLocalNodeIds[i - 1]) {
161  amalgRowMapGIDs[count] = myGids[i];
162  amalgColMapGIDs[count] = myGids[i];
163  count++;
164  }
165  }
166 
167  RCP<GOVector> tempAmalgColVec = GOVectorFactory::Build(A->getDomainMap());
168  {
169  Teuchos::ArrayRCP<GlobalOrdinal> tempAmalgColVecData = tempAmalgColVec->getDataNonConst(0);
170  for (size_t i = 0; i < A->getDomainMap()->getLocalNumElements(); i++)
171  tempAmalgColVecData[i] = amalgColMapGIDs[myLocalNodeIds[i]];
172  }
173 
174  RCP<GOVector> tempAmalgColVecTarget = GOVectorFactory::Build(A->getColMap());
175  Teuchos::RCP<Import> dofImporter = ImportFactory::Build(A->getDomainMap(), A->getColMap());
176  tempAmalgColVecTarget->doImport(*tempAmalgColVec, *dofImporter, Xpetra::INSERT);
177 
178  {
179  Teuchos::ArrayRCP<const GlobalOrdinal> tempAmalgColVecBData = tempAmalgColVecTarget->getData(0);
180  // copy from dof vector to nodal vector
181  for (size_t i = 0; i < myLocalNodeIds.size(); i++)
182  amalgColMapGIDs[myLocalNodeIds[i]] = tempAmalgColVecBData[i];
183  }
184 
185  Teuchos::RCP<Map> amalgRowMap = MapFactory::Build(lib,
187  amalgRowMapGIDs(), // View,
188  A->getRowMap()->getIndexBase(),
189  comm);
190 
191  Teuchos::RCP<Map> amalgColMap = MapFactory::Build(lib,
193  amalgColMapGIDs(), // View,
194  A->getRangeMap()->getIndexBase(),
195  comm);
196 
197  // end fill nodal maps
198 
199  // start variable dof amalgamation
200 
201  Teuchos::RCP<CrsMatrixWrap> Awrap = Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(A);
202  Teuchos::RCP<CrsMatrix> Acrs = Awrap->getCrsMatrix();
203  // Acrs->describe(*fancy, Teuchos::VERB_EXTREME);
204 
205  size_t nNonZeros = 0;
206  std::vector<bool> isNonZero(nLocalPlusGhostDofs, false);
207  std::vector<size_t> nonZeroList(nLocalPlusGhostDofs); // ???
208 
209  // also used in DetectDirichletExt
210  Teuchos::RCP<Vector> diagVecUnique = VectorFactory::Build(A->getRowMap());
211  Teuchos::RCP<Vector> diagVec = VectorFactory::Build(A->getColMap());
212  A->getLocalDiagCopy(*diagVecUnique);
213  diagVec->doImport(*diagVecUnique, *dofImporter, Xpetra::INSERT);
214  Teuchos::ArrayRCP<const Scalar> diagVecData = diagVec->getData(0);
215 
216  Teuchos::ArrayRCP<const size_t> rowptr(Acrs->getLocalNumRows());
217  Teuchos::ArrayRCP<const LocalOrdinal> colind(Acrs->getLocalNumEntries());
218  Teuchos::ArrayRCP<const Scalar> values(Acrs->getLocalNumEntries());
219  Acrs->getAllValues(rowptr, colind, values);
220 
221  // create arrays for amalgamated matrix
222  Teuchos::ArrayRCP<size_t> amalgRowPtr(nLocalNodes + 1);
223  Teuchos::ArrayRCP<LocalOrdinal> amalgCols(rowptr[rowptr.size() - 1]);
224 
225  LocalOrdinal oldBlockRow = 0;
226  LocalOrdinal blockRow = 0;
227  LocalOrdinal blockColumn = 0;
228 
229  size_t newNzs = 0;
230  amalgRowPtr[0] = newNzs;
231 
232  bool doNotDrop = false;
233  if (amalgDropTol == Teuchos::ScalarTraits<Scalar>::zero()) doNotDrop = true;
234  if (values.size() == 0) doNotDrop = true;
235 
236  for (decltype(rowptr.size()) i = 0; i < rowptr.size() - 1; i++) {
237  blockRow = std::floor<LocalOrdinal>(map[i] / maxDofPerNode);
238  if (blockRow != oldBlockRow) {
239  // zero out info recording nonzeros in oldBlockRow
240  for (size_t j = 0; j < nNonZeros; j++) isNonZero[nonZeroList[j]] = false;
241  nNonZeros = 0;
242  amalgRowPtr[blockRow] = newNzs; // record start of next row
243  }
244  for (size_t j = rowptr[i]; j < rowptr[i + 1]; j++) {
245  if (doNotDrop == true ||
246  (STS::magnitude(values[j] / STS::magnitude(sqrt(STS::magnitude(diagVecData[i]) * STS::magnitude(diagVecData[colind[j]])))) >= STS::magnitude(amalgDropTol))) {
247  blockColumn = myLocalNodeIds[colind[j]];
248  if (isNonZero[blockColumn] == false) {
249  isNonZero[blockColumn] = true;
250  nonZeroList[nNonZeros++] = blockColumn;
251  amalgCols[newNzs++] = blockColumn;
252  }
253  }
254  }
255  oldBlockRow = blockRow;
256  }
257  amalgRowPtr[blockRow + 1] = newNzs;
258 
259  TEUCHOS_TEST_FOR_EXCEPTION((blockRow + 1 != Teuchos::as<LO>(nLocalNodes)) && (nLocalNodes != 0), MueLu::Exceptions::RuntimeError, "VariableDofsPerNodeAmalgamation: error, computed # block rows (" << blockRow + 1 << ") != nLocalNodes (" << nLocalNodes << ")");
260 
261  amalgCols.resize(amalgRowPtr[nLocalNodes]);
262 
263  // end variableDofAmalg
264 
265  // begin rm differentDofsCrossings
266 
267  // Remove matrix entries (i,j) where the ith node and the jth node have
268  // different dofs that are 'present'
269  // Specifically, on input:
270  // dofPresent[i*maxDofPerNode+k] indicates whether or not the kth
271  // dof at the ith node is present in the
272  // variable dof matrix (e.g., the ith node
273  // has an air pressure dof). true means
274  // the dof is present while false means it
275  // is not.
276  // We create a unique id for the ith node (i.e. uniqueId[i]) via
277  // sum_{k=0 to maxDofPerNode-1} dofPresent[i*maxDofPerNode+k]*2^k
278  // and use this unique idea to remove entries (i,j) when uniqueId[i]!=uniqueId[j]
279 
280  Teuchos::ArrayRCP<LocalOrdinal> uniqueId(nLocalPlusGhostNodes); // unique id associated with DOF
281  std::vector<bool> keep(amalgRowPtr[amalgRowPtr.size() - 1], true); // keep connection associated with node
282 
283  size_t ii = 0; // iteration index for present dofs
284  for (decltype(amalgRowPtr.size()) i = 0; i < amalgRowPtr.size() - 1; i++) {
285  LocalOrdinal temp = 1; // basis for dof-id
286  uniqueId[i] = 0;
287  for (decltype(maxDofPerNode) j = 0; j < maxDofPerNode; j++) {
288  if (dofPresent[ii++]) uniqueId[i] += temp; // encode dof to be present
289  temp = temp * 2; // check next dof
290  }
291  }
292 
293  Teuchos::RCP<Import> nodeImporter = ImportFactory::Build(amalgRowMap, amalgColMap);
294 
297 
298  Teuchos::ArrayRCP<LocalOrdinal> nodeIdSrcData = nodeIdSrc->getDataNonConst(0);
299  for (decltype(amalgRowPtr.size()) i = 0; i < amalgRowPtr.size() - 1; i++) {
300  nodeIdSrcData[i] = uniqueId[i];
301  }
302 
303  nodeIdTarget->doImport(*nodeIdSrc, *nodeImporter, Xpetra::INSERT);
304 
305  Teuchos::ArrayRCP<const LocalOrdinal> nodeIdTargetData = nodeIdTarget->getData(0);
306  for (decltype(uniqueId.size()) i = 0; i < uniqueId.size(); i++) {
307  uniqueId[i] = nodeIdTargetData[i];
308  }
309 
310  // nodal comm uniqueId, myLocalNodeIds
311 
312  // uniqueId now should contain ghosted data
313 
314  for (decltype(amalgRowPtr.size()) i = 0; i < amalgRowPtr.size() - 1; i++) {
315  for (size_t j = amalgRowPtr[i]; j < amalgRowPtr[i + 1]; j++) {
316  if (uniqueId[i] != uniqueId[amalgCols[j]]) keep[j] = false;
317  }
318  }
319 
320  // squeeze out hard-coded zeros from CSR arrays
321  Teuchos::ArrayRCP<Scalar> amalgVals;
322  this->squeezeOutNnzs(amalgRowPtr, amalgCols, amalgVals, keep);
323 
325  RCP<dxMV> ghostedCoords = dxMVf::Build(amalgColMap, Coords->getNumVectors());
326 
327  TEUCHOS_TEST_FOR_EXCEPTION(amalgRowMap->getLocalNumElements() != Coords->getMap()->getLocalNumElements(), MueLu::Exceptions::RuntimeError, "MueLu::VariableDofLaplacianFactory: the number of Coordinates and amalgamated nodes is inconsistent.");
328 
329  // Coords might live on a special nodeMap with consecutive ids (the natural numbering)
330  // The amalgRowMap might have the same number of entries, but with holes in the ids.
331  // e.g. 0,3,6,9,... as GIDs.
332  // We need the ghosted Coordinates in the buildLaplacian routine. But we access the data
333  // through getData only, i.e., the global ids are not interesting as long as we do not change
334  // the ordering of the entries
335  Coords->replaceMap(amalgRowMap);
336  ghostedCoords->doImport(*Coords, *nodeImporter, Xpetra::INSERT);
337 
338  Teuchos::ArrayRCP<Scalar> lapVals(amalgRowPtr[nLocalNodes]);
339  this->buildLaplacian(amalgRowPtr, amalgCols, lapVals, Coords->getNumVectors(), ghostedCoords);
340 
341  // sort column GIDs
342  for (decltype(amalgRowPtr.size()) i = 0; i < amalgRowPtr.size() - 1; i++) {
343  size_t j = amalgRowPtr[i];
344  this->MueLu_az_sort<LocalOrdinal>(&(amalgCols[j]), amalgRowPtr[i + 1] - j, NULL, &(lapVals[j]));
345  }
346 
347  // Caluclate status array for next level
348  Teuchos::Array<char> status(nLocalNodes * maxDofPerNode);
349 
350  // dir or not Teuchos::ArrayRCP<const bool> dirOrNot
351  for (decltype(status.size()) i = 0; i < status.size(); i++) status[i] = 's';
352  for (decltype(status.size()) i = 0; i < status.size(); i++) {
353  if (dofPresent[i] == false) status[i] = 'p';
354  }
355  if (dirOrNot.size() > 0) {
356  for (decltype(map.size()) i = 0; i < map.size(); i++) {
357  if (dirOrNot[i] == true) {
358  status[map[i]] = 'd';
359  }
360  }
361  }
362  Set(currentLevel, "DofStatus", status);
363 
364  // end status array
365 
366  Teuchos::RCP<CrsMatrix> lapCrsMat = CrsMatrixFactory::Build(amalgRowMap, amalgColMap, 10); // TODO better approx for max nnz per row
367 
368  for (size_t i = 0; i < nLocalNodes; i++) {
369  lapCrsMat->insertLocalValues(i, amalgCols.view(amalgRowPtr[i], amalgRowPtr[i + 1] - amalgRowPtr[i]),
370  lapVals.view(amalgRowPtr[i], amalgRowPtr[i + 1] - amalgRowPtr[i]));
371  }
372  lapCrsMat->fillComplete(amalgRowMap, amalgRowMap);
373 
374  // lapCrsMat->describe(*fancy, Teuchos::VERB_EXTREME);
375 
376  Teuchos::RCP<Matrix> lapMat = Teuchos::rcp(new CrsMatrixWrap(lapCrsMat));
377  Set(currentLevel, "A", lapMat);
378 }
379 
380 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
382  TEUCHOS_TEST_FOR_EXCEPTION(numdim != 2 && numdim != 3, MueLu::Exceptions::RuntimeError, "buildLaplacian only works for 2d or 3d examples. numdim = " << numdim);
383 
384  if (numdim == 2) { // 2d
387 
388  for (decltype(rowPtr.size()) i = 0; i < rowPtr.size() - 1; i++) {
390  LocalOrdinal diag = -1;
391  for (size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
392  if (cols[j] != Teuchos::as<LO>(i)) {
393  vals[j] = std::sqrt((x[i] - x[cols[j]]) * (x[i] - x[cols[j]]) +
394  (y[i] - y[cols[j]]) * (y[i] - y[cols[j]]));
395  TEUCHOS_TEST_FOR_EXCEPTION(vals[j] == Teuchos::ScalarTraits<Scalar>::zero(), MueLu::Exceptions::RuntimeError, "buildLaplacian: error, " << i << " and " << cols[j] << " have same coordinates: " << x[i] << " and " << y[i]);
396  vals[j] = -Teuchos::ScalarTraits<SC>::one() / vals[j];
397  sum = sum - vals[j];
398  } else
399  diag = j;
400  }
402  TEUCHOS_TEST_FOR_EXCEPTION(diag == -1, MueLu::Exceptions::RuntimeError, "buildLaplacian: error, row " << i << " has zero diagonal!");
403 
404  vals[diag] = sum;
405  }
406  } else { // 3d
410 
411  for (decltype(rowPtr.size()) i = 0; i < rowPtr.size() - 1; i++) {
413  LocalOrdinal diag = -1;
414  for (size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
415  if (cols[j] != Teuchos::as<LO>(i)) {
416  vals[j] = std::sqrt((x[i] - x[cols[j]]) * (x[i] - x[cols[j]]) +
417  (y[i] - y[cols[j]]) * (y[i] - y[cols[j]]) +
418  (z[i] - z[cols[j]]) * (z[i] - z[cols[j]]));
419 
420  TEUCHOS_TEST_FOR_EXCEPTION(vals[j] == Teuchos::ScalarTraits<Scalar>::zero(), MueLu::Exceptions::RuntimeError, "buildLaplacian: error, " << i << " and " << cols[j] << " have same coordinates: " << x[i] << " and " << y[i] << " and " << z[i]);
421 
422  vals[j] = -Teuchos::ScalarTraits<SC>::one() / vals[j];
423  sum = sum - vals[j];
424  } else
425  diag = j;
426  }
428  TEUCHOS_TEST_FOR_EXCEPTION(diag == -1, MueLu::Exceptions::RuntimeError, "buildLaplacian: error, row " << i << " has zero diagonal!");
429 
430  vals[diag] = sum;
431  }
432  }
433 }
434 
435 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
437  // get rid of nonzero entries that have 0's in them and properly change
438  // the row ptr array to reflect this removal (either vals == NULL or vals != NULL)
439  // Note, the arrays are squeezed. No memory is freed.
440 
441  size_t count = 0;
442 
443  size_t nRows = rowPtr.size() - 1;
444  if (vals.size() > 0) {
445  for (size_t i = 0; i < nRows; i++) {
446  size_t newStart = count;
447  for (size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
448  if (vals[j] != Teuchos::ScalarTraits<Scalar>::zero()) {
449  cols[count] = cols[j];
450  vals[count++] = vals[j];
451  }
452  }
453  rowPtr[i] = newStart;
454  }
455  } else {
456  for (size_t i = 0; i < nRows; i++) {
457  size_t newStart = count;
458  for (size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
459  if (keep[j] == true) {
460  cols[count++] = cols[j];
461  }
462  }
463  rowPtr[i] = newStart;
464  }
465  }
466  rowPtr[nRows] = count;
467 }
468 
469 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
471  size_t count = 0;
472  for (decltype(dofPresent.size()) i = 0; i < dofPresent.size(); i++)
473  if (dofPresent[i] == 1) map[count++] = Teuchos::as<LocalOrdinal>(i);
474  TEUCHOS_TEST_FOR_EXCEPTION(nDofs != count, MueLu::Exceptions::RuntimeError, "VariableDofLaplacianFactory::buildPaddedMap: #dofs in dofPresent does not match the expected value (number of rows of A): " << nDofs << " vs. " << count);
475 }
476 
477 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
478 void VariableDofLaplacianFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::assignGhostLocalNodeIds(const Teuchos::RCP<const Map>& rowDofMap, const Teuchos::RCP<const Map>& colDofMap, std::vector<LocalOrdinal>& myLocalNodeIds, const std::vector<LocalOrdinal>& dofMap, size_t maxDofPerNode, size_t& nLocalNodes, size_t& nLocalPlusGhostNodes, Teuchos::RCP<const Teuchos::Comm<int> > comm) const {
479  size_t nLocalDofs = rowDofMap->getLocalNumElements();
480  size_t nLocalPlusGhostDofs = colDofMap->getLocalNumElements(); // TODO remove parameters
481 
482  // create importer for dof-based information
483  Teuchos::RCP<Import> importer = ImportFactory::Build(rowDofMap, colDofMap);
484 
485  // create a vector living on column map of A (dof based)
486  Teuchos::RCP<LOVector> localNodeIdsTemp = LOVectorFactory::Build(rowDofMap, true);
487  Teuchos::RCP<LOVector> localNodeIds = LOVectorFactory::Build(colDofMap, true);
488 
489  // fill local dofs (padded local ids)
490  {
491  Teuchos::ArrayRCP<LocalOrdinal> localNodeIdsTempData = localNodeIdsTemp->getDataNonConst(0);
492  for (size_t i = 0; i < localNodeIdsTemp->getLocalLength(); i++)
493  localNodeIdsTempData[i] = std::floor<LocalOrdinal>(dofMap[i] / maxDofPerNode);
494  }
495 
496  localNodeIds->doImport(*localNodeIdsTemp, *importer, Xpetra::INSERT);
497  Teuchos::ArrayRCP<const LocalOrdinal> localNodeIdsData = localNodeIds->getData(0);
498 
499  // Note: localNodeIds contains local ids for the padded version as vector values
500 
501  // we use Scalar instead of int as type
502  Teuchos::RCP<LOVector> myProcTemp = LOVectorFactory::Build(rowDofMap, true);
503  Teuchos::RCP<LOVector> myProc = LOVectorFactory::Build(colDofMap, true);
504 
505  // fill local dofs (padded local ids)
506  {
507  Teuchos::ArrayRCP<LocalOrdinal> myProcTempData = myProcTemp->getDataNonConst(0);
508  for (size_t i = 0; i < myProcTemp->getLocalLength(); i++)
509  myProcTempData[i] = Teuchos::as<LocalOrdinal>(comm->getRank());
510  }
511  myProc->doImport(*myProcTemp, *importer, Xpetra::INSERT);
512  Teuchos::ArrayRCP<LocalOrdinal> myProcData = myProc->getDataNonConst(0); // we have to modify the data (therefore the non-const version)
513 
514  // At this point, the ghost part of localNodeIds corresponds to the local ids
515  // associated with the current owning processor. We want to convert these to
516  // local ids associated with the processor on which these are ghosts.
517  // Thus we have to re-number them. In doing this re-numbering we must make sure
518  // that we find all ghosts with the same id & proc and assign a unique local
519  // id to this group (id&proc). To do this find, we sort all ghost entries in
520  // localNodeIds that are owned by the same processor. Then we can look for
521  // duplicates (i.e., several ghost entries corresponding to dofs with the same
522  // node id) easily and make sure these are all assigned to the same local id.
523  // To do the sorting we'll make a temporary copy of the ghosts via tempId and
524  // tempProc and sort this multiple times for each group owned by the same proc.
525 
526  std::vector<size_t> location(nLocalPlusGhostDofs - nLocalDofs + 1);
527  std::vector<size_t> tempId(nLocalPlusGhostDofs - nLocalDofs + 1);
528  std::vector<size_t> tempProc(nLocalPlusGhostDofs - nLocalDofs + 1);
529 
530  size_t notProcessed = nLocalDofs; // iteration index over all ghosted dofs
531  size_t tempIndex = 0;
532  size_t first = tempIndex;
533  LocalOrdinal neighbor;
534 
535  while (notProcessed < nLocalPlusGhostDofs) {
536  neighbor = myProcData[notProcessed]; // get processor id of not-processed element
537  first = tempIndex;
538  location[tempIndex] = notProcessed;
539  tempId[tempIndex++] = localNodeIdsData[notProcessed];
540  myProcData[notProcessed] = -1 - neighbor;
541 
542  for (size_t i = notProcessed + 1; i < nLocalPlusGhostDofs; i++) {
543  if (myProcData[i] == neighbor) {
544  location[tempIndex] = i;
545  tempId[tempIndex++] = localNodeIdsData[i];
546  myProcData[i] = -1; // mark as visited
547  }
548  }
549  this->MueLu_az_sort<size_t>(&(tempId[first]), tempIndex - first, &(location[first]), NULL);
550  for (size_t i = first; i < tempIndex; i++) tempProc[i] = neighbor;
551 
552  // increment index. Find next notProcessed dof index corresponding to first non-visited element
553  notProcessed++;
554  while ((notProcessed < nLocalPlusGhostDofs) && (myProcData[notProcessed] < 0))
555  notProcessed++;
556  }
557  TEUCHOS_TEST_FOR_EXCEPTION(tempIndex != nLocalPlusGhostDofs - nLocalDofs, MueLu::Exceptions::RuntimeError, "Number of nonzero ghosts is inconsistent.");
558 
559  // Now assign ids to all ghost nodes (giving the same id to those with the
560  // same myProc[] and the same local id on the proc that actually owns the
561  // variable associated with the ghost
562 
563  nLocalNodes = 0; // initialize return value
564  if (nLocalDofs > 0) nLocalNodes = localNodeIdsData[nLocalDofs - 1] + 1;
565 
566  nLocalPlusGhostNodes = nLocalNodes; // initialize return value
567  if (nLocalDofs < nLocalPlusGhostDofs) nLocalPlusGhostNodes++; // 1st ghost node is unique (not accounted for). number will be increased later, if there are more ghost nodes
568 
569  // check if two adjacent ghost dofs correspond to different nodes. To do this,
570  // check if they are from different processors or whether they have different
571  // local node ids
572 
573  // loop over all (remaining) ghost dofs
574  for (size_t i = nLocalDofs + 1; i < nLocalPlusGhostDofs; i++) {
575  size_t lagged = nLocalPlusGhostNodes - 1;
576 
577  // i is a new unique ghost node (not already accounted for)
578  if ((tempId[i - nLocalDofs] != tempId[i - 1 - nLocalDofs]) ||
579  (tempProc[i - nLocalDofs] != tempProc[i - 1 - nLocalDofs]))
580  nLocalPlusGhostNodes++; // update number of ghost nodes
581  tempId[i - 1 - nLocalDofs] = lagged;
582  }
583  if (nLocalPlusGhostDofs > nLocalDofs)
584  tempId[nLocalPlusGhostDofs - 1 - nLocalDofs] = nLocalPlusGhostNodes - 1;
585 
586  // fill myLocalNodeIds array. Start with local part (not ghosted)
587  for (size_t i = 0; i < nLocalDofs; i++)
588  myLocalNodeIds[i] = std::floor<LocalOrdinal>(dofMap[i] / maxDofPerNode);
589 
590  // copy ghosted nodal ids into myLocalNodeIds
591  for (size_t i = nLocalDofs; i < nLocalPlusGhostDofs; i++)
592  myLocalNodeIds[location[i - nLocalDofs]] = tempId[i - nLocalDofs];
593 }
594 
595 } // namespace MueLu
596 
597 #endif /* PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_ */
void buildPaddedMap(const Teuchos::ArrayRCP< const LocalOrdinal > &dofPresent, std::vector< LocalOrdinal > &map, size_t nDofs) const
MueLu::DefaultLocalOrdinal LocalOrdinal
void buildLaplacian(const Teuchos::ArrayRCP< size_t > &rowPtr, const Teuchos::ArrayRCP< LocalOrdinal > &cols, Teuchos::ArrayRCP< Scalar > &vals, const size_t &numdim, const RCP< Xpetra::MultiVector< typename Teuchos::ScalarTraits< Scalar >::magnitudeType, LocalOrdinal, GlobalOrdinal, Node > > &ghostedCoords) const
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)
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
size_type size() const
void DeclareInput(Level &currentLevel) const
Input.
MueLu::DefaultNode Node
void assignGhostLocalNodeIds(const Teuchos::RCP< const Map > &rowDofMap, const Teuchos::RCP< const Map > &colDofMap, std::vector< LocalOrdinal > &myLocalNodeIds, const std::vector< LocalOrdinal > &dofMap, size_t maxDofPerNode, size_t &nLocalNodes, size_t &nLocalPlusGhostNodes, Teuchos::RCP< const Teuchos::Comm< int > > comm) const
static const NoFactory * get()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
void Build(Level &currentLevel) const
Build an object with this factory.
static Teuchos::ArrayRCP< const bool > DetectDirichletRowsExt(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, bool &bHasZeroDiagonal, const Magnitude &tol=Teuchos::ScalarTraits< Scalar >::zero())
Detect Dirichlet rows (extended version)
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
size_type size() const
void squeezeOutNnzs(Teuchos::ArrayRCP< size_t > &rowPtr, Teuchos::ArrayRCP< LocalOrdinal > &cols, Teuchos::ArrayRCP< Scalar > &vals, const std::vector< bool > &keep) const
Node NO
Exception throws to report errors in the internal logical of the program.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need&#39;s value has been saved.
ArrayView< T > view(size_type lowerOffset, size_type size) const