Nalu
Nalu: a generalized unstructured massively parallel low Mach flow code designed to support a variety of energy applications of interest (most notably Wind ECP) built on the Sierra Toolkit and Trilinos solver Tpetra/Epetra stack. The open source BSD, clause 3 license model has been chosen for the code base. See LICENSE for more information. http://NaluCFD.org
TpetraLinearSystem.h
Go to the documentation of this file.
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2014 Sandia Corporation. */
3 /* This software is released under the license detailed */
4 /* in the file, LICENSE, which is located in the top-level Nalu */
5 /* directory structure */
6 /*------------------------------------------------------------------------*/
7 
8 
9 #ifndef TpetraLinearSystem_h
10 #define TpetraLinearSystem_h
11 
12 #include <LinearSystem.h>
13 
14 #include <KokkosInterface.h>
15 
16 #include <Tpetra_DefaultPlatform.hpp>
17 #include <Kokkos_DefaultNode.hpp>
18 #include <Tpetra_Vector.hpp>
19 #include <Tpetra_DefaultPlatform.hpp>
20 #include <Tpetra_CrsMatrix.hpp>
21 
22 #include <stk_mesh/base/Types.hpp>
23 #include <stk_mesh/base/Entity.hpp>
24 #include <stk_mesh/base/FieldBase.hpp>
25 
26 #include <vector>
27 #include <string>
28 #include <boost/unordered_map.hpp>
29 
30 #include <Kokkos_UnorderedMap.hpp>
31 
32 namespace sierra {
33 namespace nalu {
34 
35 class Realm;
36 class EquationSystem;
38 
39 typedef boost::unordered_map<stk::mesh::EntityId, size_t> MyLIDMapType;
40 
41 typedef std::pair<stk::mesh::Entity, stk::mesh::Entity> Connection;
42 typedef Kokkos::UnorderedMap<Connection,void> ConnectionSetKK;
43 typedef std::vector< Connection > ConnectionVec;
44 
46 {
47 public:
50 
52  Realm &realm,
53  const unsigned numDof,
54  EquationSystem *eqSys,
55  LinearSolver * linearSolver);
57 
58  // Graph/Matrix Construction
59  void buildNodeGraph(const stk::mesh::PartVector & parts); // for nodal assembly (e.g., lumped mass and source)
60  void buildFaceToNodeGraph(const stk::mesh::PartVector & parts); // face->node assembly
61  void buildEdgeToNodeGraph(const stk::mesh::PartVector & parts); // edge->node assembly
62  void buildElemToNodeGraph(const stk::mesh::PartVector & parts); // elem->node assembly
63  void buildReducedElemToNodeGraph(const stk::mesh::PartVector & parts); // elem (nearest nodes only)->node assembly
64  void buildFaceElemToNodeGraph(const stk::mesh::PartVector & parts); // elem:face->node assembly
65  void buildNonConformalNodeGraph(const stk::mesh::PartVector & parts); // nonConformal->node assembly
66  void buildOversetNodeGraph(const stk::mesh::PartVector & parts); // overset->elem_node assembly
67  void finalizeLinearSystem();
68 
69  // Matrix Assembly
70  void zeroSystem();
71 
72  void sumInto(
73  unsigned numEntities,
74  const stk::mesh::Entity* entities,
77  const SharedMemView<int*> & localIds,
78  const SharedMemView<int*> & sortPermutation,
79  const char * trace_tag);
80 
81  void sumInto(
82  const std::vector<stk::mesh::Entity> & entities,
83  std::vector<int> &scratchIds,
84  std::vector<double> &scratchVals,
85  const std::vector<double> & rhs,
86  const std::vector<double> & lhs,
87  const char *trace_tag=0
88  );
89 
90  void applyDirichletBCs(
91  stk::mesh::FieldBase * solutionField,
92  stk::mesh::FieldBase * bcValuesField,
93  const stk::mesh::PartVector & parts,
94  const unsigned beginPos,
95  const unsigned endPos);
96 
97  void prepareConstraints(
98  const unsigned beginPos,
99  const unsigned endPos);
100 
107  virtual void resetRows(
108  const std::vector<stk::mesh::Entity> nodeList,
109  const unsigned beginPos,
110  const unsigned endPos);
111 
112  // Solve
113  int solve(stk::mesh::FieldBase * linearSolutionField);
114  void loadComplete();
115  void writeToFile(const char * filename, bool useOwned=true);
116  void printInfo(bool useOwned=true);
117  void writeSolutionToFile(const char * filename, bool useOwned=true);
118  size_t lookup_myLID(MyLIDMapType& myLIDs, stk::mesh::EntityId entityId, const char* msg=nullptr, stk::mesh::Entity entity = stk::mesh::Entity())
119  {
120  return myLIDs[entityId];
121  }
122 
123 
124  enum DOFStatus {
126  DS_SkippedDOF = 1 << 1,
127  DS_OwnedDOF = 1 << 2,
129  DS_GhostedDOF = 1 << 4
130  };
131 
132  int getDofStatus(stk::mesh::Entity node);
133 
134 private:
136 
138  const int err_code,
139  const char * msg) {}
140 
141  void copy_kokkos_unordered_map_to_sorted_vector(const ConnectionSetKK& connectionSetKK,
142  ConnectionVec& connectionVec);
143 
144  void compute_graph_row_lengths(const ConnectionVec& connectionVec,
145  LinSys::RowLengths& globallyOwnedRowLengths,
146  LinSys::RowLengths& locallyOwnedRowLengths);
147 
148  void insert_graph_connections(const ConnectionVec& connectionVec,
149  LinSys::Graph& ownedGraph,
150  int ownedOrSharedMask);
151 
153 
154  void copy_tpetra_to_stk(
155  const Teuchos::RCP<LinSys::Vector> tpetraVector,
156  stk::mesh::FieldBase * stkField);
157 
158  // This method copies a stk::mesh::field to a tpetra multivector. Each dof/node is written into a different
159  // vector in the multivector.
160  void copy_stk_to_tpetra(stk::mesh::FieldBase * stkField,
161  const Teuchos::RCP<LinSys::MultiVector> tpetraVector);
162 
163  int addConnections(const stk::mesh::Entity* entities,const size_t&);
164  void expand_unordered_map(unsigned newCapacityNeeded);
165  void checkForNaN(bool useOwned);
166  bool checkForZeroRow(bool useOwned, bool doThrow, bool doPrint=false);
167 
168  ConnectionSetKK connectionSetKK_ ;
169  std::vector<GlobalOrdinal> totalGids_;
170 
171  Teuchos::RCP<LinSys::Node> node_;
172 
173  // all rows, otherwise known as col map
174  Teuchos::RCP<LinSys::Map> totalColsMap_;
175 
176  // Map of rows my proc owns (locally owned)
177  Teuchos::RCP<LinSys::Map> ownedRowsMap_;
178 
179  // Map of all rows my proc references
180  Teuchos::RCP<LinSys::Map> ownedPlusGloballyOwnedRowsMap_;
181 
182  // Only nodes that share with other procs that I don't own = Global = !locally owned
183  Teuchos::RCP<LinSys::Map> globallyOwnedRowsMap_;
184 
185  Teuchos::RCP<LinSys::Graph> ownedGraph_;
186  Teuchos::RCP<LinSys::Graph> globallyOwnedGraph_;
187 
188  Teuchos::RCP<LinSys::Matrix> ownedMatrix_;
189  Teuchos::RCP<LinSys::Vector> ownedRhs_;
190 
191  Teuchos::RCP<LinSys::Matrix> globallyOwnedMatrix_;
192  Teuchos::RCP<LinSys::Vector> globallyOwnedRhs_;
193 
194  Teuchos::RCP<LinSys::Vector> sln_;
195  Teuchos::RCP<LinSys::Vector> globalSln_;
196  Teuchos::RCP<LinSys::Export> exporter_;
197  Teuchos::RCP<LinSys::Import> importer_;
198 
199  MyLIDMapType myLIDs_;
200  std::vector<LocalOrdinal> entityToLID_;
201  LocalOrdinal maxOwnedRowId_; // = num_owned_nodes * numDof_
202  LocalOrdinal maxGloballyOwnedRowId_; // = (num_owned_nodes + num_globallyOwned_nodes) * numDof_
203 
204  std::vector<int> sortPermutation_;
205 };
206 
207 template<typename T1, typename T2>
208 void copy_kokkos_unordered_map(const Kokkos::UnorderedMap<T1,T2>& src,
209  Kokkos::UnorderedMap<T1,T2>& dest)
210 {
211  if (src.capacity() > dest.capacity()) {
212  dest = Kokkos::UnorderedMap<T1,T2>(src.capacity());
213  }
214 
215  unsigned capacity = src.capacity();
216  unsigned fail_count = 0;
217  for(unsigned i=0; i<capacity; ++i) {
218  if (src.valid_at(i)) {
219  auto insert_result = dest.insert(src.key_at(i));
220  fail_count += insert_result.failed() ? 1 : 0;
221  }
222  }
223  ThrowRequire(fail_count == 0);
224 }
225 
226 } // namespace nalu
227 } // namespace Sierra
228 
229 #endif
Teuchos::RCP< LinSys::Export > exporter_
Definition: TpetraLinearSystem.h:196
void copy_kokkos_unordered_map(const Kokkos::UnorderedMap< T1, T2 > &src, Kokkos::UnorderedMap< T1, T2 > &dest)
Definition: TpetraLinearSystem.h:208
LinSys::GlobalOrdinal GlobalOrdinal
Definition: TpetraLinearSystem.h:48
std::vector< GlobalOrdinal > totalGids_
Definition: TpetraLinearSystem.h:169
DOFStatus
Definition: TpetraLinearSystem.h:124
Definition: TpetraLinearSystem.h:45
std::vector< Part * > PartVector
Definition: Algorithm.h:16
void compute_graph_row_lengths(const ConnectionVec &connectionVec, LinSys::RowLengths &globallyOwnedRowLengths, LinSys::RowLengths &locallyOwnedRowLengths)
Definition: TpetraLinearSystem.C:847
Definition: ABLForcingAlgorithm.C:26
std::vector< Connection > ConnectionVec
Definition: TpetraLinearSystem.h:43
void prepareConstraints(const unsigned beginPos, const unsigned endPos)
Definition: TpetraLinearSystem.C:1331
boost::unordered_map< stk::mesh::EntityId, size_t > MyLIDMapType
Definition: TpetraLinearSystem.h:37
void checkForNaN(bool useOwned)
Definition: TpetraLinearSystem.C:1528
void copy_kokkos_unordered_map_to_sorted_vector(const ConnectionSetKK &connectionSetKK, ConnectionVec &connectionVec)
Definition: TpetraLinearSystem.C:823
Kokkos::UnorderedMap< Connection, void > ConnectionSetKK
Definition: TpetraLinearSystem.h:42
Teuchos::RCP< LinSys::Matrix > globallyOwnedMatrix_
Definition: TpetraLinearSystem.h:191
size_t lookup_myLID(MyLIDMapType &myLIDs, stk::mesh::EntityId entityId, const char *msg=nullptr, stk::mesh::Entity entity=stk::mesh::Entity())
Definition: TpetraLinearSystem.h:118
long GlobalOrdinal
Definition: LinearSolverTypes.h:80
Teuchos::RCP< LinSys::Import > importer_
Definition: TpetraLinearSystem.h:197
TpetraLinearSystem(Realm &realm, const unsigned numDof, EquationSystem *eqSys, LinearSolver *linearSolver)
=====================================================================================================...
Definition: TpetraLinearSystem.C:84
std::pair< stk::mesh::Entity, stk::mesh::Entity > Connection
Definition: TpetraLinearSystem.h:41
Teuchos::RCP< LinSys::Vector > globallyOwnedRhs_
Definition: TpetraLinearSystem.h:192
MyLIDMapType myLIDs_
Definition: TpetraLinearSystem.h:199
void copy_tpetra_to_stk(const Teuchos::RCP< LinSys::Vector > tpetraVector, stk::mesh::FieldBase *stkField)
Definition: TpetraLinearSystem.C:1797
ConnectionSetKK connectionSetKK_
Definition: TpetraLinearSystem.h:168
void buildEdgeToNodeGraph(const stk::mesh::PartVector &parts)
Definition: TpetraLinearSystem.C:460
std::vector< int > sortPermutation_
Definition: TpetraLinearSystem.h:204
unsigned numDof() const
Definition: LinearSystem.h:117
void loadComplete()
Definition: TpetraLinearSystem.C:1433
Definition: TpetraLinearSystem.h:126
~TpetraLinearSystem()
Definition: TpetraLinearSystem.C:95
static constexpr double lhs[8][8]
Definition: UnitTestContinuityAdvElem.C:25
Teuchos::RCP< LinSys::Vector > globalSln_
Definition: TpetraLinearSystem.h:195
void buildNonConformalNodeGraph(const stk::mesh::PartVector &parts)
Definition: TpetraLinearSystem.C:661
Base class representation of a PDE.
Definition: EquationSystem.h:46
LinSys::LocalOrdinal LocalOrdinal
Definition: TpetraLinearSystem.h:49
Definition: TpetraLinearSystem.h:127
void buildNodeGraph(const stk::mesh::PartVector &parts)
Definition: TpetraLinearSystem.C:429
void fill_entity_to_LID_mapping()
Definition: TpetraLinearSystem.C:960
Definition: TpetraLinearSystem.h:129
void writeToFile(const char *filename, bool useOwned=true)
Definition: TpetraLinearSystem.C:1647
void finalizeLinearSystem()
Definition: TpetraLinearSystem.C:977
void buildFaceElemToNodeGraph(const stk::mesh::PartVector &parts)
Definition: TpetraLinearSystem.C:613
Teuchos::RCP< LinSys::Map > ownedPlusGloballyOwnedRowsMap_
Definition: TpetraLinearSystem.h:180
static constexpr double rhs[8]
Definition: UnitTestContinuityAdvElem.C:18
int getDofStatus(stk::mesh::Entity node)
Definition: TpetraLinearSystem.C:135
Teuchos::RCP< LinSys::Map > ownedRowsMap_
Definition: TpetraLinearSystem.h:177
Teuchos::RCP< LinSys::Vector > ownedRhs_
Definition: TpetraLinearSystem.h:189
LocalOrdinal maxOwnedRowId_
Definition: TpetraLinearSystem.h:201
Teuchos::RCP< LinSys::Map > totalColsMap_
Definition: TpetraLinearSystem.h:174
void zeroSystem()
Definition: TpetraLinearSystem.C:1068
void copy_stk_to_tpetra(stk::mesh::FieldBase *stkField, const Teuchos::RCP< LinSys::MultiVector > tpetraVector)
Definition: TpetraLinearSystem.C:775
LocalOrdinal maxGloballyOwnedRowId_
Definition: TpetraLinearSystem.h:202
Definition: TpetraLinearSystem.h:125
Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > Graph
Definition: LinearSolverTypes.h:90
Teuchos::RCP< LinSys::Graph > ownedGraph_
Definition: TpetraLinearSystem.h:185
Teuchos::RCP< LinSys::Map > globallyOwnedRowsMap_
Definition: TpetraLinearSystem.h:183
std::vector< LocalOrdinal > entityToLID_
Definition: TpetraLinearSystem.h:200
void buildElemToNodeGraph(const stk::mesh::PartVector &parts)
Definition: TpetraLinearSystem.C:530
Kokkos::View< T, Kokkos::LayoutRight, DeviceShmem, Kokkos::MemoryUnmanaged > SharedMemView
Definition: KokkosInterface.h:25
void buildFaceToNodeGraph(const stk::mesh::PartVector &parts)
Definition: TpetraLinearSystem.C:498
void applyDirichletBCs(stk::mesh::FieldBase *solutionField, stk::mesh::FieldBase *bcValuesField, const stk::mesh::PartVector &parts, const unsigned beginPos, const unsigned endPos)
Definition: TpetraLinearSystem.C:1252
Teuchos::RCP< LinSys::Matrix > ownedMatrix_
Definition: TpetraLinearSystem.h:188
Teuchos::RCP< LinSys::Node > node_
Definition: TpetraLinearSystem.h:171
void checkError(const int err_code, const char *msg)
Definition: TpetraLinearSystem.h:137
void expand_unordered_map(unsigned newCapacityNeeded)
Definition: TpetraLinearSystem.C:421
Definition: LinearSolver.h:55
int solve(stk::mesh::FieldBase *linearSolutionField)
Definition: TpetraLinearSystem.C:1456
Teuchos::RCP< LinSys::Graph > globallyOwnedGraph_
Definition: TpetraLinearSystem.h:186
void sumInto(unsigned numEntities, const stk::mesh::Entity *entities, const SharedMemView< const double * > &rhs, const SharedMemView< const double ** > &lhs, const SharedMemView< int * > &localIds, const SharedMemView< int * > &sortPermutation, const char *trace_tag)
Definition: TpetraLinearSystem.C:1124
int addConnections(const stk::mesh::Entity *entities, const size_t &)
Definition: TpetraLinearSystem.C:395
Definition: Realm.h:82
virtual void resetRows(const std::vector< stk::mesh::Entity > nodeList, const unsigned beginPos, const unsigned endPos)
Reset LHS and RHS for the given set of nodes to 0.
Definition: TpetraLinearSystem.C:1384
bool checkForZeroRow(bool useOwned, bool doThrow, bool doPrint=false)
Definition: TpetraLinearSystem.C:1560
Kokkos::DualView< size_t *, DeviceSpace > RowLengths
Definition: LinearSolverTypes.h:84
void buildOversetNodeGraph(const stk::mesh::PartVector &parts)
Definition: TpetraLinearSystem.C:727
Teuchos::RCP< LinSys::Vector > sln_
Definition: TpetraLinearSystem.h:194
void beginLinearSystemConstruction()
Definition: TpetraLinearSystem.C:246
void buildReducedElemToNodeGraph(const stk::mesh::PartVector &parts)
Definition: TpetraLinearSystem.C:563
Definition: LinearSystem.h:40
int LocalOrdinal
Definition: LinearSolverTypes.h:81
void writeSolutionToFile(const char *filename, bool useOwned=true)
Definition: TpetraLinearSystem.C:1748
void insert_graph_connections(const ConnectionVec &connectionVec, LinSys::Graph &ownedGraph, int ownedOrSharedMask)
Definition: TpetraLinearSystem.C:895
void printInfo(bool useOwned=true)
Definition: TpetraLinearSystem.C:1723
Definition: TpetraLinearSystem.h:128