MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
18 
20 
21 #include "MueLu_Level.hpp"
22 #include "MueLu_Monitor.hpp"
23 
24 namespace MueLu {
25 
26 template <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 
38 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
40  Input(currentLevel, "A");
41 }
42 
43 template <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
63  Ain->getLocalRowView(row, indices, vals);
64 
65  // just copy all values in output
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);
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
Timer to be used in factories. Similar to Monitor but with additional timers.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void resize(const size_type n, const T &val=T())
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Print statistics that do not involve significant additional computation.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
void Build(Level &currentLevel) const
Build method.
static magnitudeType magnitude(T a)
void DeclareInput(Level &currentLevel) const
Input.