47 #ifndef PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_
48 #define PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_
57 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
61 validParamList->
set<
double > (
"Advanced Dirichlet: threshold", 1e-5,
"Drop tolerance for Dirichlet detection");
62 validParamList->
set<
double > (
"Variable DOF amalgamation: threshold", 1.8e-9,
"Drop tolerance for amalgamation process");
63 validParamList->
set<
int > (
"maxDofPerNode", 1,
"Maximum number of DOFs per node");
68 return validParamList;
71 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
74 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
76 Input(currentLevel,
"A");
77 Input(currentLevel,
"Coordinates");
85 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
92 RCP<Matrix> A = Get< RCP<Matrix> >(currentLevel,
"A");
95 Xpetra::UnderlyingLib lib = A->getRowMap()->lib();
97 typedef Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO> dxMV;
98 RCP<dxMV> Coords = Get< RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO> > >(currentLevel,
"Coordinates");
100 int maxDofPerNode = pL.
get<
int>(
"maxDofPerNode");
101 Scalar dirDropTol = Teuchos::as<Scalar>(pL.
get<
double>(
"Advanced Dirichlet: threshold"));
102 Scalar amalgDropTol = Teuchos::as<Scalar>(pL.
get<
double>(
"Variable DOF amalgamation: threshold"));
104 bool bHasZeroDiagonal =
false;
122 std::vector<LocalOrdinal> map(A->getNodeNumRows());
123 this->buildPaddedMap(dofPresent, map, A->getNodeNumRows());
126 std::vector<LocalOrdinal> myLocalNodeIds(A->getColMap()->getNodeNumElements());
129 size_t nLocalNodes, nLocalPlusGhostNodes;
130 this->assignGhostLocalNodeIds(A->getRowMap(), A->getColMap(), myLocalNodeIds, map, maxDofPerNode, nLocalNodes, nLocalPlusGhostNodes, comm);
134 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.");
144 size_t nLocalDofs = A->getRowMap()->getNodeNumElements();
145 size_t nLocalPlusGhostDofs = A->getColMap()->getNodeNumElements();
154 if (nLocalDofs > 0) {
155 amalgRowMapGIDs[count] = myGids[0];
156 amalgColMapGIDs[count] = myGids[0];
160 for(
size_t i = 1; i < nLocalDofs; i++) {
161 if (myLocalNodeIds[i] != myLocalNodeIds[i-1]) {
162 amalgRowMapGIDs[count] = myGids[i];
163 amalgColMapGIDs[count] = myGids[i];
168 RCP<GOVector> tempAmalgColVec = GOVectorFactory::Build(A->getDomainMap());
170 for (
size_t i = 0; i < A->getDomainMap()->getNodeNumElements(); i++)
171 tempAmalgColVecData[i] = amalgColMapGIDs[ myLocalNodeIds[i]];
173 RCP<GOVector> tempAmalgColVecTarget = GOVectorFactory::Build(A->getColMap());
175 tempAmalgColVecTarget->doImport(*tempAmalgColVec, *dofImporter, Xpetra::INSERT);
179 for (
size_t i = 0; i < myLocalNodeIds.size(); i++)
180 amalgColMapGIDs[ myLocalNodeIds[i]] = tempAmalgColVecBData[i];
185 A->getRowMap()->getIndexBase(),
191 A->getRangeMap()->getIndexBase(),
206 Acrs->getAllValues(rowptr, colind, values);
213 size_t nNonZeros = 0;
214 std::vector<bool> isNonZero(nLocalPlusGhostDofs,
false);
215 std::vector<size_t> nonZeroList(nLocalPlusGhostDofs);
220 A->getLocalDiagCopy(*diagVecUnique);
221 diagVec->doImport(*diagVecUnique, *dofImporter, Xpetra::INSERT);
229 amalgRowPtr[0] = newNzs;
231 bool doNotDrop =
false;
233 if (values.size() == 0) doNotDrop =
true;
235 for(decltype(rowptr.size()) i = 0; i < rowptr.size()-1; i++) {
236 blockRow = std::floor<LocalOrdinal>( map[i] / maxDofPerNode);
237 if (blockRow != oldBlockRow) {
239 for(
size_t j = 0; j < nNonZeros; j++) isNonZero[nonZeroList[j]] =
false;
241 amalgRowPtr[blockRow] = newNzs;
243 for (
size_t j = rowptr[i]; j < rowptr[i+1]; j++) {
244 if(doNotDrop ==
true ||
245 ( STS::magnitude(values[j] / STS::magnitude(sqrt(STS::magnitude(diagVecData[i]) * STS::magnitude(diagVecData[colind[j]]))) ) >= STS::magnitude(amalgDropTol) )) {
246 blockColumn = myLocalNodeIds[colind[j]];
247 if(isNonZero[blockColumn] ==
false) {
248 isNonZero[blockColumn] =
true;
249 nonZeroList[nNonZeros++] = blockColumn;
250 amalgCols[newNzs++] = blockColumn;
254 oldBlockRow = blockRow;
256 amalgRowPtr[blockRow+1] = newNzs;
258 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 amalgCols.resize(amalgRowPtr[nLocalNodes]);
280 std::vector<bool> keep(amalgRowPtr[amalgRowPtr.
size()-1],
true);
283 for(decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size()-1; i++) {
286 for (decltype(maxDofPerNode) j = 0; j < maxDofPerNode; j++) {
287 if (dofPresent[ii++]) uniqueId[i] += temp;
294 RCP<LOVector> nodeIdSrc = Xpetra::VectorFactory<LocalOrdinal,LocalOrdinal,GlobalOrdinal,Node>::Build(amalgRowMap,
true);
295 RCP<LOVector> nodeIdTarget = Xpetra::VectorFactory<LocalOrdinal,LocalOrdinal,GlobalOrdinal,Node>::Build(amalgColMap,
true);
298 for(decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size()-1; i++) {
299 nodeIdSrcData[i] = uniqueId[i];
302 nodeIdTarget->doImport(*nodeIdSrc, *nodeImporter, Xpetra::INSERT);
305 for(decltype(uniqueId.
size()) i = 0; i < uniqueId.
size(); i++) {
306 uniqueId[i] = nodeIdTargetData[i];
313 for(decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size()-1; i++) {
314 for(
size_t j = amalgRowPtr[i]; j < amalgRowPtr[i+1]; j++) {
315 if (uniqueId[i] != uniqueId[amalgCols[j]]) keep [j] =
false;
321 this->squeezeOutNnzs(amalgRowPtr,amalgCols,amalgVals,keep);
323 typedef Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO> dxMVf;
324 RCP<dxMV> ghostedCoords = dxMVf::Build(amalgColMap,Coords->getNumVectors());
326 TEUCHOS_TEST_FOR_EXCEPTION(amalgRowMap->getNodeNumElements() != Coords->getMap()->getNodeNumElements(), MueLu::Exceptions::RuntimeError,
"MueLu::VariableDofLaplacianFactory: the number of Coordinates and amalgamated nodes is inconsistent.");
334 Coords->replaceMap(amalgRowMap);
335 ghostedCoords->doImport(*Coords, *nodeImporter, Xpetra::INSERT);
338 this->buildLaplacian(amalgRowPtr, amalgCols, lapVals, Coords->getNumVectors(), ghostedCoords);
341 for(decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size()-1; i++) {
342 size_t j = amalgRowPtr[i];
343 this->MueLu_az_sort<LocalOrdinal>(&(amalgCols[j]), amalgRowPtr[i+1] - j, NULL, &(lapVals[j]));
350 for(decltype(status.
size()) i = 0; i < status.
size(); i++) status[i] =
's';
351 for(decltype(status.
size()) i = 0; i < status.
size(); i++) {
352 if(dofPresent[i] ==
false) status[i] =
'p';
354 if(dirOrNot.
size() > 0) {
355 for(decltype(map.size()) i = 0; i < map.size(); i++) {
356 if(dirOrNot[i] ==
true){
357 status[map[i]] =
'd';
361 Set(currentLevel,
"DofStatus",status);
367 for (
size_t i = 0; i < nLocalNodes; i++) {
368 lapCrsMat->insertLocalValues(i, amalgCols.view(amalgRowPtr[i],amalgRowPtr[i+1]-amalgRowPtr[i]),
369 lapVals.
view(amalgRowPtr[i],amalgRowPtr[i+1]-amalgRowPtr[i]));
371 lapCrsMat->fillComplete(amalgRowMap,amalgRowMap);
376 Set(currentLevel,
"A",lapMat);
379 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
380 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 {
387 for(decltype(rowPtr.
size()) i = 0; i < rowPtr.
size() - 1; i++) {
390 for(
size_t j = rowPtr[i]; j < rowPtr[i+1]; j++) {
391 if(cols[j] != Teuchos::as<LO>(i)){
392 vals[j] = std::sqrt( (x[i]-x[cols[j]]) * (x[i]-x[cols[j]]) +
393 (y[i]-y[cols[j]]) * (y[i]-y[cols[j]]) );
410 for(decltype(rowPtr.
size()) i = 0; i < rowPtr.
size() - 1; i++) {
413 for(
size_t j = rowPtr[i]; j < rowPtr[i+1]; j++) {
414 if(cols[j] != Teuchos::as<LO>(i)){
415 vals[j] = std::sqrt( (x[i]-x[cols[j]]) * (x[i]-x[cols[j]]) +
416 (y[i]-y[cols[j]]) * (y[i]-y[cols[j]]) +
417 (z[i]-z[cols[j]]) * (z[i]-z[cols[j]]) );
434 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
442 size_t nRows = rowPtr.
size()-1;
443 if(vals.
size() > 0) {
444 for(
size_t i = 0; i < nRows; i++) {
445 size_t newStart = count;
446 for(
size_t j = rowPtr[i]; j < rowPtr[i+1]; j++) {
448 cols[count ] = cols[j];
449 vals[count++] = vals[j];
452 rowPtr[i] = newStart;
455 for (
size_t i = 0; i < nRows; i++) {
456 size_t newStart = count;
457 for(
size_t j = rowPtr[i]; j < rowPtr[i+1]; j++) {
458 if (keep[j] ==
true) {
459 cols[count++] = cols[j];
462 rowPtr[i] = newStart;
465 rowPtr[nRows] = count;
468 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
471 for (decltype(dofPresent.
size()) i = 0; i < dofPresent.
size(); i++)
472 if(dofPresent[i] == 1) map[count++] = Teuchos::as<LocalOrdinal>(i);
476 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
477 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->getNodeNumElements();
480 size_t nLocalPlusGhostDofs = colDofMap->getNodeNumElements();
491 for(
size_t i = 0; i < localNodeIdsTemp->getLocalLength(); i++)
492 localNodeIdsTempData[i] = std::floor<LocalOrdinal>( dofMap[i] / maxDofPerNode );
494 localNodeIds->doImport(*localNodeIdsTemp, *importer, Xpetra::INSERT);
506 for(
size_t i = 0; i < myProcTemp->getLocalLength(); i++)
507 myProcTempData[i] = Teuchos::as<LocalOrdinal>(comm->getRank());
508 myProc->doImport(*myProcTemp, *importer, Xpetra::INSERT);
524 std::vector<size_t> location(nLocalPlusGhostDofs - nLocalDofs + 1);
525 std::vector<size_t> tempId (nLocalPlusGhostDofs - nLocalDofs + 1);
526 std::vector<size_t> tempProc(nLocalPlusGhostDofs - nLocalDofs + 1);
528 size_t notProcessed = nLocalDofs;
529 size_t tempIndex = 0;
530 size_t first = tempIndex;
533 while (notProcessed < nLocalPlusGhostDofs) {
534 neighbor = myProcData[notProcessed];
536 location[tempIndex] = notProcessed;
537 tempId[tempIndex++] = localNodeIdsData[notProcessed];
538 myProcData[notProcessed] = -1 - neighbor;
540 for(
size_t i = notProcessed + 1; i < nLocalPlusGhostDofs; i++) {
541 if(myProcData[i] == neighbor) {
542 location[tempIndex] = i;
543 tempId[tempIndex++] = localNodeIdsData[i];
547 this->MueLu_az_sort<size_t>(&(tempId[first]), tempIndex - first, &(location[first]), NULL);
548 for(
size_t i = first; i < tempIndex; i++) tempProc[i] = neighbor;
552 while ( (notProcessed < nLocalPlusGhostDofs) && (myProcData[notProcessed] < 0))
562 if(nLocalDofs > 0) nLocalNodes = localNodeIdsData[nLocalDofs-1] + 1;
564 nLocalPlusGhostNodes = nLocalNodes;
565 if(nLocalDofs < nLocalPlusGhostDofs) nLocalPlusGhostNodes++;
572 for (
size_t i = nLocalDofs+1; i < nLocalPlusGhostDofs; i++) {
573 size_t lagged = nLocalPlusGhostNodes-1;
576 if ((tempId[i-nLocalDofs] != tempId[i-1-nLocalDofs]) ||
577 (tempProc[i-nLocalDofs] != tempProc[i-1-nLocalDofs]))
578 nLocalPlusGhostNodes++;
579 tempId[i-1-nLocalDofs] = lagged;
581 if (nLocalPlusGhostDofs > nLocalDofs)
582 tempId[nLocalPlusGhostDofs-1-nLocalDofs] = nLocalPlusGhostNodes - 1;
585 for(
size_t i = 0; i < nLocalDofs; i++)
586 myLocalNodeIds[i] = std::floor<LocalOrdinal>( dofMap[i] / maxDofPerNode );
589 for(
size_t i = nLocalDofs; i < nLocalPlusGhostDofs; i++)
590 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)
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)
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())
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