MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_DropNegativeEntriesFactory_def.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MueLu: A package for multigrid based preconditioning
4//
5// Copyright 2012 NTESS and the MueLu contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP
11#define MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP
12
13#include <Xpetra_CrsGraph.hpp>
14#include <Xpetra_Matrix.hpp>
15#include <Xpetra_Vector.hpp>
16#include <Xpetra_MatrixFactory.hpp>
17#include <Xpetra_CrsGraphFactory.hpp>
18
20
21#include "MueLu_Level.hpp"
22#include "MueLu_Monitor.hpp"
23
24namespace MueLu {
25
26template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
28 RCP<ParameterList> validParamList = rcp(new ParameterList());
29
30#define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
31#undef SET_VALID_ENTRY
32
33 validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the matrix A used for filtering");
34
35 return validParamList;
36}
37
38template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
40 Input(currentLevel, "A");
41}
42
43template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
45 FactoryMonitor m(*this, "Matrix filtering (springs)", currentLevel);
46
47 RCP<Matrix> Ain = Get<RCP<Matrix> >(currentLevel, "A");
48
49 LocalOrdinal nDofsPerNode = Ain->GetFixedBlockSize();
50
51 // create new empty Operator
52 Teuchos::RCP<Matrix> Aout = MatrixFactory::Build(Ain->getRowMap(), Ain->getGlobalMaxNumRowEntries());
53
54 size_t numLocalRows = Ain->getLocalNumRows();
55 for (size_t row = 0; row < numLocalRows; row++) {
56 GlobalOrdinal grid = Ain->getRowMap()->getGlobalElement(row);
57
58 int rDofID = Teuchos::as<int>(grid % nDofsPerNode);
59
60 // extract row information from input matrix
61 Teuchos::ArrayView<const LocalOrdinal> indices;
62 Teuchos::ArrayView<const Scalar> vals;
63 Ain->getLocalRowView(row, indices, vals);
64
65 // just copy all values in output
66 Teuchos::ArrayRCP<GlobalOrdinal> indout(indices.size(), Teuchos::ScalarTraits<GlobalOrdinal>::zero());
67 Teuchos::ArrayRCP<Scalar> valout(indices.size(), Teuchos::ScalarTraits<Scalar>::zero());
68
69 size_t nNonzeros = 0;
70 for (size_t i = 0; i < (size_t)indices.size(); i++) {
71 GlobalOrdinal gcid = Ain->getColMap()->getGlobalElement(indices[i]); // global column id
72
73 int cDofID = Teuchos::as<int>(gcid % nDofsPerNode);
74 if (rDofID == cDofID && Teuchos::ScalarTraits<Scalar>::magnitude(vals[i]) >= Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::zero())) {
75 indout[nNonzeros] = gcid;
76 valout[nNonzeros] = vals[i];
77 nNonzeros++;
78 }
79 }
80 indout.resize(nNonzeros);
81 valout.resize(nNonzeros);
82
83 Aout->insertGlobalValues(Ain->getRowMap()->getGlobalElement(row), indout.view(0, indout.size()), valout.view(0, valout.size()));
84 }
85
86 Aout->fillComplete(Ain->getDomainMap(), Ain->getRangeMap());
87
88 // copy block size information
89 Aout->SetFixedBlockSize(nDofsPerNode);
90
91 GetOStream(Statistics0, 0) << "Nonzeros in A (input): " << Ain->getGlobalNumEntries() << ", Nonzeros after filtering A: " << Aout->getGlobalNumEntries() << std::endl;
92
93 Set(currentLevel, "A", Aout);
94}
95
96} // namespace MueLu
97
98#endif // MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultGlobalOrdinal GlobalOrdinal
void DeclareInput(Level &currentLevel) const
Input.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void Build(Level &currentLevel) const
Build method.
Timer to be used in factories. Similar to Monitor but with additional timers.
Class that holds all level-specific information.
Namespace for MueLu classes and methods.
@ Statistics0
Print statistics that do not involve significant additional computation.