10 #ifndef PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_
11 #define PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_
19 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
23 validParamList->
set<
double>(
"Advanced Dirichlet: threshold", 1e-5,
"Drop tolerance for Dirichlet detection");
24 validParamList->
set<
double>(
"Variable DOF amalgamation: threshold", 1.8e-9,
"Drop tolerance for amalgamation process");
25 validParamList->
set<
int>(
"maxDofPerNode", 1,
"Maximum number of DOFs per node");
30 return validParamList;
33 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
36 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
38 Input(currentLevel,
"A");
39 Input(currentLevel,
"Coordinates");
47 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
54 RCP<Matrix> A = Get<RCP<Matrix> >(currentLevel,
"A");
60 RCP<dxMV> Coords = Get<RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO,
NO> > >(currentLevel,
"Coordinates");
62 int maxDofPerNode = pL.
get<
int>(
"maxDofPerNode");
63 Scalar dirDropTol = Teuchos::as<Scalar>(pL.
get<
double>(
"Advanced Dirichlet: threshold"));
64 Scalar amalgDropTol = Teuchos::as<Scalar>(pL.
get<
double>(
"Variable DOF amalgamation: threshold"));
66 bool bHasZeroDiagonal =
false;
84 std::vector<LocalOrdinal> map(A->getLocalNumRows());
85 this->buildPaddedMap(dofPresent, map, A->getLocalNumRows());
88 std::vector<LocalOrdinal> myLocalNodeIds(A->getColMap()->getLocalNumElements());
91 size_t nLocalNodes, nLocalPlusGhostNodes;
92 this->assignGhostLocalNodeIds(A->getRowMap(), A->getColMap(), myLocalNodeIds, map, maxDofPerNode, nLocalNodes, nLocalPlusGhostNodes, comm);
96 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.");
106 size_t nLocalDofs = A->getRowMap()->getLocalNumElements();
107 size_t nLocalPlusGhostDofs = A->getColMap()->getLocalNumElements();
116 if (nLocalDofs > 0) {
117 amalgRowMapGIDs[count] = myGids[0];
118 amalgColMapGIDs[count] = myGids[0];
122 for (
size_t i = 1; i < nLocalDofs; i++) {
123 if (myLocalNodeIds[i] != myLocalNodeIds[i - 1]) {
124 amalgRowMapGIDs[count] = myGids[i];
125 amalgColMapGIDs[count] = myGids[i];
130 RCP<GOVector> tempAmalgColVec = GOVectorFactory::Build(A->getDomainMap());
133 for (
size_t i = 0; i < A->getDomainMap()->getLocalNumElements(); i++)
134 tempAmalgColVecData[i] = amalgColMapGIDs[myLocalNodeIds[i]];
137 RCP<GOVector> tempAmalgColVecTarget = GOVectorFactory::Build(A->getColMap());
139 tempAmalgColVecTarget->doImport(*tempAmalgColVec, *dofImporter,
Xpetra::INSERT);
144 for (
size_t i = 0; i < myLocalNodeIds.size(); i++)
145 amalgColMapGIDs[myLocalNodeIds[i]] = tempAmalgColVecBData[i];
151 A->getRowMap()->getIndexBase(),
157 A->getRangeMap()->getIndexBase(),
168 size_t nNonZeros = 0;
169 std::vector<bool> isNonZero(nLocalPlusGhostDofs,
false);
170 std::vector<size_t> nonZeroList(nLocalPlusGhostDofs);
175 A->getLocalDiagCopy(*diagVecUnique);
182 Acrs->getAllValues(rowptr, colind, values);
193 amalgRowPtr[0] = newNzs;
195 bool doNotDrop =
false;
197 if (values.size() == 0) doNotDrop =
true;
199 for (decltype(rowptr.size()) i = 0; i < rowptr.size() - 1; i++) {
200 blockRow = std::floor<LocalOrdinal>(map[i] / maxDofPerNode);
201 if (blockRow != oldBlockRow) {
203 for (
size_t j = 0; j < nNonZeros; j++) isNonZero[nonZeroList[j]] =
false;
205 amalgRowPtr[blockRow] = newNzs;
207 for (
size_t j = rowptr[i]; j < rowptr[i + 1]; j++) {
208 if (doNotDrop ==
true ||
209 (STS::magnitude(values[j] / STS::magnitude(sqrt(STS::magnitude(diagVecData[i]) * STS::magnitude(diagVecData[colind[j]])))) >= STS::magnitude(amalgDropTol))) {
210 blockColumn = myLocalNodeIds[colind[j]];
211 if (isNonZero[blockColumn] ==
false) {
212 isNonZero[blockColumn] =
true;
213 nonZeroList[nNonZeros++] = blockColumn;
214 amalgCols[newNzs++] = blockColumn;
218 oldBlockRow = blockRow;
220 amalgRowPtr[blockRow + 1] = newNzs;
222 TEUCHOS_TEST_FOR_EXCEPTION((blockRow + 1 != Teuchos::as<LO>(nLocalNodes)) && (nLocalNodes != 0), MueLu::Exceptions::RuntimeError,
"VariableDofsPerNodeAmalgamation: error, computed # block rows (" << blockRow + 1 <<
") != nLocalNodes (" << nLocalNodes <<
")");
224 amalgCols.resize(amalgRowPtr[nLocalNodes]);
244 std::vector<bool> keep(amalgRowPtr[amalgRowPtr.
size() - 1],
true);
247 for (decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size() - 1; i++) {
250 for (decltype(maxDofPerNode) j = 0; j < maxDofPerNode; j++) {
251 if (dofPresent[ii++]) uniqueId[i] += temp;
262 for (decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size() - 1; i++) {
263 nodeIdSrcData[i] = uniqueId[i];
266 nodeIdTarget->doImport(*nodeIdSrc, *nodeImporter,
Xpetra::INSERT);
269 for (decltype(uniqueId.
size()) i = 0; i < uniqueId.
size(); i++) {
270 uniqueId[i] = nodeIdTargetData[i];
277 for (decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size() - 1; i++) {
278 for (
size_t j = amalgRowPtr[i]; j < amalgRowPtr[i + 1]; j++) {
279 if (uniqueId[i] != uniqueId[amalgCols[j]]) keep[j] =
false;
285 this->squeezeOutNnzs(amalgRowPtr, amalgCols, amalgVals, keep);
288 RCP<dxMV> ghostedCoords = dxMVf::Build(amalgColMap, Coords->getNumVectors());
290 TEUCHOS_TEST_FOR_EXCEPTION(amalgRowMap->getLocalNumElements() != Coords->getMap()->getLocalNumElements(), MueLu::Exceptions::RuntimeError,
"MueLu::VariableDofLaplacianFactory: the number of Coordinates and amalgamated nodes is inconsistent.");
298 Coords->replaceMap(amalgRowMap);
302 this->buildLaplacian(amalgRowPtr, amalgCols, lapVals, Coords->getNumVectors(), ghostedCoords);
305 for (decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size() - 1; i++) {
306 size_t j = amalgRowPtr[i];
307 this->MueLu_az_sort<LocalOrdinal>(&(amalgCols[j]), amalgRowPtr[i + 1] - j, NULL, &(lapVals[j]));
314 for (decltype(status.
size()) i = 0; i < status.
size(); i++) status[i] =
's';
315 for (decltype(status.
size()) i = 0; i < status.
size(); i++) {
316 if (dofPresent[i] ==
false) status[i] =
'p';
318 if (dirOrNot.
size() > 0) {
319 for (decltype(map.size()) i = 0; i < map.size(); i++) {
320 if (dirOrNot[i] ==
true) {
321 status[map[i]] =
'd';
325 Set(currentLevel,
"DofStatus", status);
331 for (
size_t i = 0; i < nLocalNodes; i++) {
332 lapCrsMat->insertLocalValues(i, amalgCols.view(amalgRowPtr[i], amalgRowPtr[i + 1] - amalgRowPtr[i]),
333 lapVals.
view(amalgRowPtr[i], amalgRowPtr[i + 1] - amalgRowPtr[i]));
335 lapCrsMat->fillComplete(amalgRowMap, amalgRowMap);
340 Set(currentLevel,
"A", lapMat);
343 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
344 void VariableDofLaplacianFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::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 {
351 for (decltype(rowPtr.
size()) i = 0; i < rowPtr.
size() - 1; i++) {
354 for (
size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
355 if (cols[j] != Teuchos::as<LO>(i)) {
356 vals[j] = std::sqrt((x[i] - x[cols[j]]) * (x[i] - x[cols[j]]) +
357 (y[i] - y[cols[j]]) * (y[i] - y[cols[j]]));
374 for (decltype(rowPtr.
size()) i = 0; i < rowPtr.
size() - 1; i++) {
377 for (
size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
378 if (cols[j] != Teuchos::as<LO>(i)) {
379 vals[j] = std::sqrt((x[i] - x[cols[j]]) * (x[i] - x[cols[j]]) +
380 (y[i] - y[cols[j]]) * (y[i] - y[cols[j]]) +
381 (z[i] - z[cols[j]]) * (z[i] - z[cols[j]]));
398 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
406 size_t nRows = rowPtr.
size() - 1;
407 if (vals.
size() > 0) {
408 for (
size_t i = 0; i < nRows; i++) {
409 size_t newStart = count;
410 for (
size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
412 cols[count] = cols[j];
413 vals[count++] = vals[j];
416 rowPtr[i] = newStart;
419 for (
size_t i = 0; i < nRows; i++) {
420 size_t newStart = count;
421 for (
size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
422 if (keep[j] ==
true) {
423 cols[count++] = cols[j];
426 rowPtr[i] = newStart;
429 rowPtr[nRows] = count;
432 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
435 for (decltype(dofPresent.
size()) i = 0; i < dofPresent.
size(); i++)
436 if (dofPresent[i] == 1) map[count++] = Teuchos::as<LocalOrdinal>(i);
440 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
441 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 {
442 size_t nLocalDofs = rowDofMap->getLocalNumElements();
443 size_t nLocalPlusGhostDofs = colDofMap->getLocalNumElements();
455 for (
size_t i = 0; i < localNodeIdsTemp->getLocalLength(); i++)
456 localNodeIdsTempData[i] = std::floor<LocalOrdinal>(dofMap[i] / maxDofPerNode);
459 localNodeIds->doImport(*localNodeIdsTemp, *importer,
Xpetra::INSERT);
471 for (
size_t i = 0; i < myProcTemp->getLocalLength(); i++)
472 myProcTempData[i] = Teuchos::as<LocalOrdinal>(comm->getRank());
489 std::vector<size_t> location(nLocalPlusGhostDofs - nLocalDofs + 1);
490 std::vector<size_t> tempId(nLocalPlusGhostDofs - nLocalDofs + 1);
491 std::vector<size_t> tempProc(nLocalPlusGhostDofs - nLocalDofs + 1);
493 size_t notProcessed = nLocalDofs;
494 size_t tempIndex = 0;
495 size_t first = tempIndex;
498 while (notProcessed < nLocalPlusGhostDofs) {
499 neighbor = myProcData[notProcessed];
501 location[tempIndex] = notProcessed;
502 tempId[tempIndex++] = localNodeIdsData[notProcessed];
503 myProcData[notProcessed] = -1 - neighbor;
505 for (
size_t i = notProcessed + 1; i < nLocalPlusGhostDofs; i++) {
506 if (myProcData[i] == neighbor) {
507 location[tempIndex] = i;
508 tempId[tempIndex++] = localNodeIdsData[i];
512 this->MueLu_az_sort<size_t>(&(tempId[first]), tempIndex - first, &(location[first]), NULL);
513 for (
size_t i = first; i < tempIndex; i++) tempProc[i] = neighbor;
517 while ((notProcessed < nLocalPlusGhostDofs) && (myProcData[notProcessed] < 0))
527 if (nLocalDofs > 0) nLocalNodes = localNodeIdsData[nLocalDofs - 1] + 1;
529 nLocalPlusGhostNodes = nLocalNodes;
530 if (nLocalDofs < nLocalPlusGhostDofs) nLocalPlusGhostNodes++;
537 for (
size_t i = nLocalDofs + 1; i < nLocalPlusGhostDofs; i++) {
538 size_t lagged = nLocalPlusGhostNodes - 1;
541 if ((tempId[i - nLocalDofs] != tempId[i - 1 - nLocalDofs]) ||
542 (tempProc[i - nLocalDofs] != tempProc[i - 1 - nLocalDofs]))
543 nLocalPlusGhostNodes++;
544 tempId[i - 1 - nLocalDofs] = lagged;
546 if (nLocalPlusGhostDofs > nLocalDofs)
547 tempId[nLocalPlusGhostDofs - 1 - nLocalDofs] = nLocalPlusGhostNodes - 1;
550 for (
size_t i = 0; i < nLocalDofs; i++)
551 myLocalNodeIds[i] = std::floor<LocalOrdinal>(dofMap[i] / maxDofPerNode);
554 for (
size_t i = nLocalDofs; i < nLocalPlusGhostDofs; i++)
555 myLocalNodeIds[location[i - nLocalDofs]] = tempId[i - nLocalDofs];
VariableDofLaplacianFactory()
Constructor.
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
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void DeclareInput(Level ¤tLevel) const
Input.
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.
void Build(Level ¤tLevel) 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)
void squeezeOutNnzs(Teuchos::ArrayRCP< size_t > &rowPtr, Teuchos::ArrayRCP< LocalOrdinal > &cols, Teuchos::ArrayRCP< Scalar > &vals, const std::vector< bool > &keep) const
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's value has been saved.
ArrayView< T > view(size_type lowerOffset, size_type size) const