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
KernelBuilder.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 #ifndef KernelBuilder_h
8 #define KernelBuilder_h
9 
10 #include <Kernel.h>
12 #include <EquationSystem.h>
13 #include <AlgTraits.h>
14 #include <KernelBuilderLog.h>
15 
17 
18 #include <stk_mesh/base/BulkData.hpp>
19 #include <stk_mesh/base/Entity.hpp>
20 #include <stk_topology/topology.hpp>
21 
22 #include <BuildTemplates.h>
23 
24 #include <algorithm>
25 #include <tuple>
26 
27 namespace sierra{
28 namespace nalu{
29  class Realm;
30 
31 
32  template <template <typename> class T, int order, typename... Args>
33  Kernel* build_ho_kernel(int dimension, Args&&... args)
34  {
35  // only two topologies supported, so we can flatten some of the decision making
36  if (dimension == 2) {
37  return new T<AlgTraitsQuadGL<order>>(std::forward<Args>(args)...);
38  }
39  return new T<AlgTraitsHexGL<order>>(std::forward<Args>(args)...);
40  }
41 
42  template <template <typename> class T, typename... Args>
43  Kernel* build_topo_kernel(int dimension, stk::topology topo, Args&&... args)
44  {
45  if (!topo.is_super_topology()) {
46  switch(topo.value()) {
47  case stk::topology::HEX_8:
48  return new T<AlgTraitsHex8>(std::forward<Args>(args)...);
49  case stk::topology::HEX_27:
50  return new T<AlgTraitsHex27>(std::forward<Args>(args)...);
51  case stk::topology::TET_4:
52  return new T<AlgTraitsTet4>(std::forward<Args>(args)...);
53  case stk::topology::PYRAMID_5:
54  return new T<AlgTraitsPyr5>(std::forward<Args>(args)...);
55  case stk::topology::WEDGE_6:
56  return new T<AlgTraitsWed6>(std::forward<Args>(args)...);
57  case stk::topology::QUAD_4_2D:
58  return new T<AlgTraitsQuad4_2D>(std::forward<Args>(args)...);
59  case stk::topology::QUAD_9_2D:
60  return new T<AlgTraitsQuad9_2D>(std::forward<Args>(args)...);
61  case stk::topology::TRI_3_2D:
62  return new T<AlgTraitsTri3_2D>(std::forward<Args>(args)...);
63  default:
64  return nullptr;
65  }
66  }
67  else {
68  int poly_order = poly_order_from_super_topology(dimension, topo);
69  switch (poly_order) {
70  case 2: return build_ho_kernel<T, 2>(dimension, std::forward<Args>(args)...);
71  case 3: return build_ho_kernel<T, 3>(dimension, std::forward<Args>(args)...);
72  case 4: return build_ho_kernel<T, 4>(dimension, std::forward<Args>(args)...);
73  case USER_POLY_ORDER: return build_ho_kernel<T, USER_POLY_ORDER>(dimension, std::forward<Args>(args)...);
74  default:
75  ThrowRequireMsg(false,
76  "Polynomial order" + std::to_string(poly_order) + "is not supported by default. "
77  "Specify USER_POLY_ORDER and recompile to run.");
78  return nullptr;
79  }
80  }
81  }
82 
83  template <template <typename> class T, typename... Args>
85  stk::topology topo,
86  EquationSystem& eqSys,
87  std::vector<Kernel*>& kernelVec,
88  std::string name,
89  Args&&... args)
90  {
91  // dimension, in addition to topology, is necessary to distinguish the HO elements,
92  const int dim = eqSys.realm_.spatialDimension_;
93 
94  bool isCreated = false;
96  if (eqSys.supp_alg_is_requested(name)) {
97  Kernel* compKernel = build_topo_kernel<T>(dim, topo, std::forward<Args>(args)...);
98  ThrowRequire(compKernel != nullptr);
100  kernelVec.push_back(compKernel);
101  isCreated = true;
102  }
103  return isCreated;
104  }
105 
106  inline std::pair<AssembleElemSolverAlgorithm*, bool>
108  EquationSystem& eqSys,
109  stk::mesh::Part& part,
110  std::map<std::string, SolverAlgorithm*>& solverAlgs)
111  {
112  const stk::topology topo = part.topology();
113  const std::string algName = "AssembleElemSolverAlg_" + topo.name();
114 
115  bool isNotNGP = !(topo == stk::topology::HEXAHEDRON_8 || topo == stk::topology::HEXAHEDRON_27);
116 
117  auto itc = solverAlgs.find(algName);
118  bool createNewAlg = itc == solverAlgs.end();
119  if (createNewAlg) {
120  auto* theSolverAlg = new AssembleElemSolverAlgorithm(eqSys.realm_, &part, &eqSys, topo, isNotNGP);
121  ThrowRequire(theSolverAlg != nullptr);
122 
123  NaluEnv::self().naluOutputP0() << "Created the following alg: " << algName << std::endl;
124  solverAlgs.insert({algName, theSolverAlg});
125  }
126  else {
127  auto& partVec = itc->second->partVec_;
128  if (std::find(partVec.begin(), partVec.end(), &part) == partVec.end()) {
129  partVec.push_back(&part);
130  }
131  }
132 
133  auto* theSolverAlg = dynamic_cast<AssembleElemSolverAlgorithm*>(solverAlgs.at(algName));
134  ThrowRequire(theSolverAlg != nullptr);
135 
136  return {theSolverAlg, createNewAlg};
137  }
138 
139 } // namespace nalu
140 } // namespace Sierra
141 
142 #endif
bool supp_alg_is_requested(std::string name)
Definition: EquationSystem.C:480
Definition: ABLForcingAlgorithm.C:26
#define USER_POLY_ORDER
Definition: BuildTemplates.h:15
bool build_topo_kernel_if_requested(stk::topology topo, EquationSystem &eqSys, std::vector< Kernel * > &kernelVec, std::string name, Args &&...args)
Definition: KernelBuilder.h:84
void add_valid_name(std::string kernelTypeName, std::string name)
Definition: KernelBuilderLog.C:27
Kernel * build_topo_kernel(int dimension, stk::topology topo, Args &&...args)
Definition: KernelBuilder.h:43
Base class representation of a PDE.
Definition: EquationSystem.h:46
Realm & realm_
Definition: EquationSystem.h:231
static NaluEnv & self()
Definition: NaluEnv.C:48
unsigned spatialDimension_
Definition: Realm.h:365
const std::string eqnTypeName_
Definition: EquationSystem.h:234
Base class for computational kernels in Nalu.
Definition: Kernel.h:63
std::pair< AssembleElemSolverAlgorithm *, bool > build_or_add_part_to_solver_alg(EquationSystem &eqSys, stk::mesh::Part &part, std::map< std::string, SolverAlgorithm * > &solverAlgs)
Definition: KernelBuilder.h:107
int poly_order_from_super_topology(int dimension, stk::topology superTopo)
Definition: ElementDescription.C:20
Definition: AssembleElemSolverAlgorithm.h:29
std::ostream & naluOutputP0()
Definition: NaluEnv.C:58
static KernelBuilderLog & self()
Definition: KernelBuilderLog.C:20
void add_built_name(std::string kernelTypeName, std::string name)
Definition: KernelBuilderLog.C:33
Kernel * build_ho_kernel(int dimension, Args &&...args)
Definition: KernelBuilder.h:33