// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP #define MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP #include #include #include #include #include #include "MueLu_DropNegativeEntriesFactory_decl.hpp" #include "MueLu_Level.hpp" #include "MueLu_Monitor.hpp" namespace MueLu { template RCP DropNegativeEntriesFactory::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name)) #undef SET_VALID_ENTRY validParamList->set >("A", Teuchos::null, "Generating factory of the matrix A used for filtering"); return validParamList; } template void DropNegativeEntriesFactory::DeclareInput(Level& currentLevel) const { Input(currentLevel, "A"); } template void DropNegativeEntriesFactory::Build(Level& currentLevel) const { FactoryMonitor m(*this, "Matrix filtering (springs)", currentLevel); RCP Ain = Get >(currentLevel, "A"); LocalOrdinal nDofsPerNode = Ain->GetFixedBlockSize(); // create new empty Operator Teuchos::RCP Aout = MatrixFactory::Build(Ain->getRowMap(), Ain->getGlobalMaxNumRowEntries()); size_t numLocalRows = Ain->getLocalNumRows(); for (size_t row = 0; row < numLocalRows; row++) { GlobalOrdinal grid = Ain->getRowMap()->getGlobalElement(row); int rDofID = Teuchos::as(grid % nDofsPerNode); // extract row information from input matrix Teuchos::ArrayView indices; Teuchos::ArrayView vals; Ain->getLocalRowView(row, indices, vals); // just copy all values in output Teuchos::ArrayRCP indout(indices.size(), Teuchos::ScalarTraits::zero()); Teuchos::ArrayRCP valout(indices.size(), Teuchos::ScalarTraits::zero()); size_t nNonzeros = 0; for (size_t i = 0; i < (size_t)indices.size(); i++) { GlobalOrdinal gcid = Ain->getColMap()->getGlobalElement(indices[i]); // global column id int cDofID = Teuchos::as(gcid % nDofsPerNode); if (rDofID == cDofID && Teuchos::ScalarTraits::magnitude(vals[i]) >= Teuchos::ScalarTraits::magnitude(Teuchos::ScalarTraits::zero())) { indout[nNonzeros] = gcid; valout[nNonzeros] = vals[i]; nNonzeros++; } } indout.resize(nNonzeros); valout.resize(nNonzeros); Aout->insertGlobalValues(Ain->getRowMap()->getGlobalElement(row), indout.view(0, indout.size()), valout.view(0, valout.size())); } Aout->fillComplete(Ain->getDomainMap(), Ain->getRangeMap()); // copy block size information Aout->SetFixedBlockSize(nDofsPerNode); GetOStream(Statistics0, 0) << "Nonzeros in A (input): " << Ain->getGlobalNumEntries() << ", Nonzeros after filtering A: " << Aout->getGlobalNumEntries() << std::endl; Set(currentLevel, "A", Aout); } } // namespace MueLu #endif // MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP