MueLu  Version of the Day
MueLu_DropNegativeEntriesFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 // Tobias Wiesner (tawiesn@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP
48 #define MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP
49 
50 #include <Xpetra_CrsGraph.hpp>
51 #include <Xpetra_Matrix.hpp>
52 #include <Xpetra_Vector.hpp>
53 #include <Xpetra_Import.hpp>
54 #include <Xpetra_MatrixFactory.hpp>
55 #include <Xpetra_VectorFactory.hpp>
56 #include <Xpetra_ImportFactory.hpp>
57 #include <Xpetra_CrsGraphFactory.hpp>
58 
60 
61 #include "MueLu_FactoryManager.hpp"
62 #include "MueLu_Level.hpp"
63 #include "MueLu_GraphBase.hpp"
64 #include "MueLu_Graph.hpp"
65 #include "MueLu_Monitor.hpp"
66 
67 namespace MueLu {
68 
69  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
71  RCP<ParameterList> validParamList = rcp(new ParameterList());
72 
73 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
74 #undef SET_VALID_ENTRY
75 
76  validParamList->set< RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the matrix A used for filtering");
77 
78  return validParamList;
79  }
80 
81  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83  Input(currentLevel, "A");
84  }
85 
86  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
88  FactoryMonitor m(*this, "Matrix filtering (springs)", currentLevel);
89 
90  RCP<Matrix> Ain = Get< RCP<Matrix> >(currentLevel, "A");
91 
92  LocalOrdinal nDofsPerNode = Ain->GetFixedBlockSize();
93 
94  // create new empty Operator
95  Teuchos::RCP<Matrix> Aout = MatrixFactory::Build(Ain->getRowMap(), Ain->getGlobalMaxNumRowEntries(), Xpetra::StaticProfile);
96 
97  size_t numLocalRows = Ain->getNodeNumRows();
98  for(size_t row=0; row<numLocalRows; row++) {
99  GlobalOrdinal grid = Ain->getRowMap()->getGlobalElement(row);
100 
101  int rDofID = Teuchos::as<int>(grid % nDofsPerNode);
102 
103  // extract row information from input matrix
104  Teuchos::ArrayView<const LocalOrdinal> indices;
105  Teuchos::ArrayView<const Scalar> vals;
106  Ain->getLocalRowView(row, indices, vals);
107 
108  // just copy all values in output
109  Teuchos::ArrayRCP<GlobalOrdinal> indout(indices.size(),Teuchos::ScalarTraits<GlobalOrdinal>::zero());
110  Teuchos::ArrayRCP<Scalar> valout(indices.size(),Teuchos::ScalarTraits<Scalar>::zero());
111 
112  size_t nNonzeros = 0;
113  for(size_t i=0; i<(size_t)indices.size(); i++) {
114  GlobalOrdinal gcid = Ain->getColMap()->getGlobalElement(indices[i]); // global column id
115 
116  int cDofID = Teuchos::as<int>(gcid % nDofsPerNode);
117  if(rDofID == cDofID && Teuchos::ScalarTraits<Scalar>::magnitude(vals[i]) >= Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::zero())) {
118  indout [nNonzeros] = gcid;
119  valout [nNonzeros] = vals[i];
120  nNonzeros++;
121  }
122  }
123  indout.resize(nNonzeros);
124  valout.resize(nNonzeros);
125 
126  Aout->insertGlobalValues(Ain->getRowMap()->getGlobalElement(row), indout.view(0,indout.size()), valout.view(0,valout.size()));
127  }
128 
129  Aout->fillComplete(Ain->getDomainMap(), Ain->getRangeMap());
130 
131  // copy block size information
132  Aout->SetFixedBlockSize(nDofsPerNode);
133 
134  GetOStream(Statistics0, 0) << "Nonzeros in A (input): " << Ain->getGlobalNumEntries() << ", Nonzeros after filtering A: " << Aout->getGlobalNumEntries() << std::endl;
135 
136  Set(currentLevel, "A", Aout);
137  }
138 
139 } //namespace MueLu
140 
141 #endif // MUELU_DROPNEGATIVEENTRIESFACTORY_DEF_HPP
Timer to be used in factories. Similar to Monitor but with additional timers.
Namespace for MueLu classes and methods.
Print statistics that do not involve significant additional computation.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
void DeclareInput(Level &currentLevel) const
Input.
void Build(Level &currentLevel) const
Build method.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.