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
UnitTestKernelUtils.h
Go to the documentation of this file.
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2014 National Renewable Energy Laboratory. */
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 #ifndef UNITTESTKERNELUTILS_H
9 #define UNITTESTKERNELUTILS_H
10 
11 #include <gtest/gtest.h>
12 #include "UnitTestUtils.h"
13 
14 #include "SolutionOptions.h"
15 #include "Kernel.h"
16 #include "ElemDataRequests.h"
17 #include "ScratchViews.h"
18 #include "CopyAndInterleave.h"
19 #include "AlgTraits.h"
20 #include "KokkosInterface.h"
21 #include "TimeIntegrator.h"
22 
23 #include <gtest/gtest.h>
24 
25 #include <vector>
26 #include <memory>
27 #include <iostream>
28 #include <iomanip>
29 #include <cmath>
30 
31 namespace unit_test_kernel_utils {
32 
34  const stk::mesh::BulkData&,
35  const VectorFieldType& coordinates,
36  VectorFieldType& velocity);
37 
39  const stk::mesh::BulkData& bulk,
40  const VectorFieldType& coordinates,
41  GenericFieldType& dudx);
42 
44  const stk::mesh::BulkData&,
45  const VectorFieldType& coordinates,
46  ScalarFieldType& pressure);
47 
49  const stk::mesh::BulkData&,
50  const VectorFieldType& coordinates,
51  VectorFieldType& dpdx);
52 
54  const stk::mesh::BulkData& bulk,
55  const VectorFieldType& coordinates,
56  ScalarFieldType& temperature);
57 
59  const stk::mesh::BulkData& bulk,
60  const VectorFieldType& coordinates,
61  ScalarFieldType& density);
62 
64  const stk::mesh::BulkData& bulk,
65  const VectorFieldType& coordinates,
66  ScalarFieldType& tke);
67 
69  const stk::mesh::BulkData& bulk,
70  const VectorFieldType& coordinates,
71  VectorFieldType& dkdx);
72 
74  const stk::mesh::BulkData& bulk,
75  const VectorFieldType& coordinates,
76  ScalarFieldType& sdr);
77 
79  const stk::mesh::BulkData& bulk,
80  const VectorFieldType& coordinates,
81  VectorFieldType& dwdx);
82 
84  const stk::mesh::BulkData& bulk,
85  const VectorFieldType& coordinates,
86  ScalarFieldType& turbulent_viscosity);
87 
89  const stk::mesh::BulkData& bulk,
90  const VectorFieldType& coordinates,
91  ScalarFieldType& sst_f_one_blending);
92 
94  const stk::mesh::BulkData& bulk,
95  const VectorFieldType& coordinates,
96  ScalarFieldType& minimum_distance_to_wall);
97 
99  const stk::mesh::BulkData&,
100  const ScalarFieldType& mixFraction,
101  ScalarFieldType& property,
102  const double primary,
103  const double secondary);
104 
106  const stk::mesh::BulkData&,
107  const ScalarFieldType& mixFraction,
108  ScalarFieldType& property,
109  const double primary,
110  const double secondary);
111 
113  const stk::mesh::BulkData& bulk,
114  const VectorFieldType& coordinates,
115  const ScalarFieldType& mixFrac,
116  const double znot,
117  const double amf);
118 
119 void dhdx_test_function(
120  const stk::mesh::BulkData& bulk,
121  const VectorFieldType& coordinates,
122  VectorFieldType& dhdx);
123 
125  const stk::mesh::BulkData&,
126  const stk::topology&,
127  const VectorFieldType&,
128  const ScalarFieldType&,
129  const VectorFieldType&,
130  const GenericFieldType&);
131 
133  const stk::mesh::BulkData& bulk,
134  const stk::topology& topo,
135  const VectorFieldType& coordinates,
136  ScalarFieldType& dualNodalVolume,
137  const ScalarFieldType& scalarField,
138  VectorFieldType& vectorField);
139 
141  const stk::mesh::BulkData& bulk,
142  const stk::topology& topo,
143  const VectorFieldType& coordinates,
144  ScalarFieldType& dualNodalVolume,
145  const VectorFieldType& vectorField,
146  GenericFieldType& tensorField);
147 
148 void expect_all_near(
149  const Kokkos::View<double*>& calcValue,
150  const double* exactValue,
151  const double tol = 1.0e-15);
152 
153 void expect_all_near(
154  const Kokkos::View<double*>& calcValue,
155  const double exactValue,
156  const double tol = 1.0e-15);
157 
158 void expect_all_near(
159  const Kokkos::View<double**>& calcValue,
160  const double* exactValue,
161  const double tol = 1.0e-15);
162 
163 template<int N>
165  const Kokkos::View<double**>& calcValue,
166  const double (*exactValue)[N],
167  const double tol = 1.0e-15)
168 {
169  const int dim1 = calcValue.dimension(0);
170  const int dim2 = calcValue.dimension(1);
171  EXPECT_EQ(dim2, N);
172 
173  for (int i=0; i < dim1; ++i) {
174  for (int j=0; j < dim2; ++j) {
175  EXPECT_NEAR(calcValue(i,j), exactValue[i][j], tol);
176  }
177  }
178 }
179 
180 template<int N>
182  const Kokkos::View<double**>& calcValue,
183  const double exactValue,
184  const double tol = 1.0e-15)
185 {
186  const int dim1 = calcValue.dimension(0);
187  const int dim2 = calcValue.dimension(1);
188  EXPECT_EQ(dim2, N);
189 
190  for (int i=0; i < dim1; ++i) {
191  for (int j=0; j < dim2; ++j) {
192  EXPECT_NEAR(calcValue(i,j), exactValue, tol);
193  }
194  }
195 }
196 
197 }
198 
199 #ifndef KOKKOS_HAVE_CUDA
200 
207 class TestKernelHex8Mesh : public ::testing::Test
208 {
209 public:
211  : comm_(MPI_COMM_WORLD),
212  spatialDim_(3),
213  meta_(spatialDim_),
214  bulk_(meta_, comm_),
215  solnOpts_()
216  {}
217 
218  virtual ~TestKernelHex8Mesh() {}
219 
220  void fill_mesh(bool doPerturb = false)
221  {
222 
224  if (doPerturb) {
226  }
227 
228  partVec_ = {meta_.get_part("block_1")};
229 
230  coordinates_ = static_cast<const VectorFieldType*>(meta_.coordinate_field());
231 
232  EXPECT_TRUE(coordinates_ != nullptr);
233  }
234 
235  stk::ParallelMachine comm_;
236  unsigned spatialDim_;
237  stk::mesh::MetaData meta_;
238  stk::mesh::BulkData bulk_;
240 
242 
243  const VectorFieldType* coordinates_{nullptr};
244 };
245 
255 {
256 public:
258  : TestKernelHex8Mesh(),
259  velocity_(
260  &meta_.declare_field<VectorFieldType>(
261  stk::topology::NODE_RANK, "velocity",2)),
262  dpdx_(
263  &meta_.declare_field<VectorFieldType>(
264  stk::topology::NODE_RANK, "dpdx",2)),
265  density_(
266  &meta_.declare_field<ScalarFieldType>(
267  stk::topology::NODE_RANK, "density",2)),
268  pressure_(
269  &meta_.declare_field<ScalarFieldType>(
270  stk::topology::NODE_RANK, "pressure",2))
271  {
272  stk::mesh::put_field(*velocity_, meta_.universal_part(), spatialDim_);
273  stk::mesh::put_field(*dpdx_, meta_.universal_part(), spatialDim_);
274  stk::mesh::put_field(*density_, meta_.universal_part(), 1);
275  stk::mesh::put_field(*pressure_, meta_.universal_part(), 1);
276  }
277 
279 
280  virtual void fill_mesh_and_init_fields(bool doPerturb = false)
281  {
282  fill_mesh(doPerturb);
283 
284  unit_test_kernel_utils::velocity_test_function(bulk_, *coordinates_, *velocity_);
285  unit_test_kernel_utils::pressure_test_function(bulk_, *coordinates_, *pressure_);
286  unit_test_kernel_utils::dpdx_test_function(bulk_, *coordinates_, *dpdx_);
287  stk::mesh::field_fill(1.0, *density_);
288  }
289 
290  VectorFieldType* velocity_{nullptr};
291  VectorFieldType* dpdx_{nullptr};
292  ScalarFieldType* density_{nullptr};
293  ScalarFieldType* pressure_{nullptr};
294 };
295 
297 {
298 public:
300 };
301 
303 {
304 public:
307  massFlowRate_(
308  &meta_.declare_field<GenericFieldType>(
309  stk::topology::ELEM_RANK, "mass_flow_rate_scs")),
310  viscosity_(
311  &meta_.declare_field<ScalarFieldType>(
312  stk::topology::NODE_RANK, "viscosity")),
313  dudx_(
314  &meta_.declare_field<GenericFieldType>(
315  stk::topology::NODE_RANK, "dudx")),
316  temperature_(
317  &meta_.declare_field<ScalarFieldType>(
318  stk::topology::NODE_RANK, "temperature"))
319  {
320  const auto& meSCS = sierra::nalu::MasterElementRepo::get_surface_master_element(stk::topology::HEX_8);
321  stk::mesh::put_field(*massFlowRate_, meta_.universal_part(), meSCS->numIntPoints_);
322  stk::mesh::put_field(*viscosity_, meta_.universal_part(), 1);
323  stk::mesh::put_field(*dudx_, meta_.universal_part(), spatialDim_ * spatialDim_);
324  stk::mesh::put_field(*temperature_, meta_.universal_part(), 1);
325  }
326 
328 
329  virtual void fill_mesh_and_init_fields(bool doPerturb = false)
330  {
333  bulk_, stk::topology::HEX_8, *coordinates_, *density_, *velocity_, *massFlowRate_);
334  unit_test_kernel_utils::dudx_test_function(bulk_, *coordinates_, *dudx_);
335  stk::mesh::field_fill(0.1, *viscosity_);
336  stk::mesh::field_fill(300.0, *temperature_);
337  }
338 
339  GenericFieldType* massFlowRate_{nullptr};
340  ScalarFieldType* viscosity_{nullptr};
341  GenericFieldType* dudx_{nullptr};
342  ScalarFieldType* temperature_{nullptr};
343 };
344 
354 {
355 public:
357  : TestKernelHex8Mesh(),
358  temperature_(
359  &meta_.declare_field<ScalarFieldType>(
360  stk::topology::NODE_RANK, "temperature",2)),
361  thermalCond_(
362  &meta_.declare_field<ScalarFieldType>(
363  stk::topology::NODE_RANK, "thermal_conductivity",2))
364  {
365  stk::mesh::put_field(*temperature_, meta_.universal_part(), 1);
366  stk::mesh::put_field(*thermalCond_, meta_.universal_part(), 1);
367  }
368 
369  void fill_mesh_and_init_fields(bool doPerturb = false)
370  {
371  fill_mesh(doPerturb);
372 
373  unit_test_kernel_utils::temperature_test_function(bulk_, *coordinates_, *temperature_);
374  stk::mesh::field_fill(1.0, *thermalCond_);
375  }
376 
377  ScalarFieldType* temperature_{nullptr};
378  ScalarFieldType* thermalCond_{nullptr};
379 };
380 
389 {
390 public:
392  : TestKernelHex8Mesh(),
393  mixFraction_(&meta_.declare_field<ScalarFieldType>(stk::topology::NODE_RANK,
394  "mixture_fraction")),
395  velocity_(&meta_.declare_field<VectorFieldType>(stk::topology::NODE_RANK,
396  "velocity")),
397  density_(&meta_.declare_field<ScalarFieldType>(stk::topology::NODE_RANK,
398  "density")),
399  viscosity_(&meta_.declare_field<ScalarFieldType>(stk::topology::NODE_RANK,
400  "viscosity")),
401  effectiveViscosity_(&meta_.declare_field<ScalarFieldType>(stk::topology::NODE_RANK,
402  "effective_viscosity")),
403  massFlowRate_(&meta_.declare_field<GenericFieldType>(stk::topology::ELEM_RANK,
404  "mass_flow_rate_scs")),
405 
406  znot_(1.0),
407  amf_(2.0),
408  lamSc_(0.9),
409  trbSc_(1.1),
410  rhoPrimary_(0.163),
411  rhoSecondary_(1.18),
412  viscPrimary_(1.967e-5),
413  viscSecondary_(1.85e-5)
414  {
415  const auto& meSCS = sierra::nalu::MasterElementRepo::get_surface_master_element(stk::topology::HEX_8);
416  stk::mesh::put_field(*mixFraction_, meta_.universal_part(), 1);
417  stk::mesh::put_field(*velocity_, meta_.universal_part(), spatialDim_);
418  stk::mesh::put_field(*density_, meta_.universal_part(), 1);
419  stk::mesh::put_field(*viscosity_, meta_.universal_part(), 1);
420  stk::mesh::put_field(*massFlowRate_, meta_.universal_part(), meSCS->numIntPoints_);
421  }
423 
424  virtual void fill_mesh_and_init_fields(bool doPerturb = false)
425  {
426  fill_mesh(doPerturb);
427 
428  unit_test_kernel_utils::mixture_fraction_test_function(bulk_, *coordinates_, *mixFraction_, amf_, znot_);
429  unit_test_kernel_utils::velocity_test_function(bulk_, *coordinates_, *velocity_);
431  rhoPrimary_, rhoSecondary_);
433  viscPrimary_, viscSecondary_);
435  bulk_, stk::topology::HEX_8, *coordinates_, *density_, *velocity_, *massFlowRate_);
436  }
437 
438  ScalarFieldType* mixFraction_{nullptr};
439  VectorFieldType* velocity_{nullptr};
440  ScalarFieldType* density_{nullptr};
441  ScalarFieldType* viscosity_{nullptr};
442  ScalarFieldType* effectiveViscosity_{nullptr};
443  GenericFieldType* massFlowRate_{nullptr};
444 
445  const double znot_;
446  const double amf_;
447  const double lamSc_;
448  const double trbSc_;
449  const double rhoPrimary_;
450  const double rhoSecondary_;
451  const double viscPrimary_;
452  const double viscSecondary_;
453 };
454 
455 #endif /* KOKKOS_HAVE_CUDA */
456 
457 #endif /* UNITTESTKERNELUTILS_H */
stk::ParallelMachine comm_
Definition: UnitTestKernelUtils.h:235
std::vector< Part * > PartVector
Definition: Algorithm.h:16
void density_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, ScalarFieldType &density)
Definition: UnitTestKernelUtils.C:371
void turbulent_viscosity_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, ScalarFieldType &turbulent_viscosity)
Definition: UnitTestKernelUtils.C:411
virtual ~MomentumKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:327
void sdr_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, ScalarFieldType &sdr)
Definition: UnitTestKernelUtils.C:395
void pressure_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, ScalarFieldType &pressure)
Definition: UnitTestKernelUtils.C:347
void mixture_fraction_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, const ScalarFieldType &mixtureFrac, const double znot, const double amf)
Definition: UnitTestKernelUtils.C:467
Definition: UnitTestKernelUtils.C:327
const double viscPrimary_
Definition: UnitTestKernelUtils.h:451
Text fixture for mixture fraction equation kernels.
Definition: UnitTestKernelUtils.h:388
void dhdx_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, VectorFieldType &dhdx)
Definition: UnitTestKernelUtils.C:487
stk::mesh::BulkData bulk_
Definition: UnitTestKernelUtils.h:238
virtual void fill_mesh_and_init_fields(bool doPerturb=false)
Definition: UnitTestKernelUtils.h:424
void minimum_distance_to_wall_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, ScalarFieldType &minimum_distance_to_wall)
Definition: UnitTestKernelUtils.C:427
static MasterElement * get_surface_master_element(const stk::topology &theTopo, int dimension=0, std::string quadType="GaussLegendre")
Definition: MasterElementFactory.C:208
const double viscSecondary_
Definition: UnitTestKernelUtils.h:452
virtual void fill_mesh_and_init_fields(bool doPerturb=false)
Definition: UnitTestKernelUtils.h:280
void calc_mass_flow_rate_scs(const stk::mesh::BulkData &bulk, const stk::topology &topo, const VectorFieldType &coordinates, const ScalarFieldType &density, const VectorFieldType &velocity, const GenericFieldType &massFlowRate)
Definition: UnitTestKernelUtils.C:495
stk::mesh::Field< double > ScalarFieldType
Definition: PromotedElementIO.h:38
virtual ~ContinuityKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:299
MomentumKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:305
LowMachKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:257
Definition: UnitTestKernelUtils.h:296
void property_from_mixture_fraction_test_function(const stk::mesh::BulkData &bulk, const ScalarFieldType &mixFraction, ScalarFieldType &property, const double primary, const double secondary)
Definition: UnitTestKernelUtils.C:435
virtual ~MixtureFractionKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:422
Definition: SolutionOptions.h:38
Definition: Algorithm.h:14
virtual ~TestKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:218
void fill_mesh_1_elem_per_proc_hex8(stk::mesh::BulkData &bulk)
Definition: UnitTestUtils.C:32
Definition: UnitTestKernelUtils.h:302
void dwdx_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, VectorFieldType &dwdx)
Definition: UnitTestKernelUtils.C:403
void fill_mesh(bool doPerturb=false)
Definition: UnitTestKernelUtils.h:220
void dpdx_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, VectorFieldType &dpdx)
Definition: UnitTestKernelUtils.C:355
void expect_all_near(const Kokkos::View< double * > &calcValue, const double *exactValue, const double tol)
Definition: UnitTestKernelUtils.C:990
void perturb_coord_hex_8(stk::mesh::BulkData &bulk, double perturbSize)
Definition: UnitTestUtils.C:39
MixtureFractionKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:391
void fill_mesh_and_init_fields(bool doPerturb=false)
Definition: UnitTestKernelUtils.h:369
virtual void fill_mesh_and_init_fields(bool doPerturb=false)
Definition: UnitTestKernelUtils.h:329
const double lamSc_
Definition: UnitTestKernelUtils.h:447
HeatCondKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:356
stk::mesh::Field< double, stk::mesh::Cartesian > VectorFieldType
Definition: PromotedElementIO.h:40
const double amf_
Definition: UnitTestKernelUtils.h:446
Base class for all computational kernel testing setups.
Definition: UnitTestKernelUtils.h:207
Text fixture for heat conduction equation kernels.
Definition: UnitTestKernelUtils.h:353
void dkdx_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, VectorFieldType &dkdx)
Definition: UnitTestKernelUtils.C:387
void field_fill(const stk::mesh::MetaData &metaData, const stk::mesh::BulkData &bulkData, const double alpha, const stk::mesh::FieldBase &xField, const bool auraIsActive, const stk::topology::rank_t entityRankValue)
Definition: FieldFunctions.C:59
virtual ~LowMachKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:278
stk::mesh::MetaData meta_
Definition: UnitTestKernelUtils.h:237
const double rhoSecondary_
Definition: UnitTestKernelUtils.h:450
void inverse_property_from_mixture_fraction_test_function(const stk::mesh::BulkData &bulk, const ScalarFieldType &mixFraction, ScalarFieldType &property, const double primary, const double secondary)
Definition: UnitTestKernelUtils.C:451
void velocity_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, VectorFieldType &velocity)
Definition: UnitTestKernelUtils.C:329
void temperature_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, ScalarFieldType &temperature)
Definition: UnitTestKernelUtils.C:363
void tke_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, ScalarFieldType &tke)
Definition: UnitTestKernelUtils.C:379
const double trbSc_
Definition: UnitTestKernelUtils.h:448
stk::mesh::Field< double, stk::mesh::SimpleArrayTag > GenericFieldType
Definition: PromotedElementIO.h:39
unsigned spatialDim_
Definition: UnitTestKernelUtils.h:236
sierra::nalu::SolutionOptions solnOpts_
Definition: UnitTestKernelUtils.h:241
void dudx_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, GenericFieldType &dudx)
Definition: UnitTestKernelUtils.C:338
const double tol
Definition: UnitTestUtils.h:56
const double znot_
Definition: UnitTestKernelUtils.h:445
TestKernelHex8Mesh()
Definition: UnitTestKernelUtils.h:210
const double rhoPrimary_
Definition: UnitTestKernelUtils.h:449
stk::mesh::PartVector partVec_
Definition: UnitTestKernelUtils.h:239
void sst_f_one_blending_test_function(const stk::mesh::BulkData &bulk, const VectorFieldType &coordinates, ScalarFieldType &sst_f_one_blending)
Definition: UnitTestKernelUtils.C:419
void calc_projected_nodal_gradient(const stk::mesh::BulkData &bulk, const stk::topology &topo, const VectorFieldType &coordinates, ScalarFieldType &dnv, const ScalarFieldType &scalarField, VectorFieldType &gradField)
Definition: UnitTestKernelUtils.C:939
Test Fixture for Low-Mach Kernels.
Definition: UnitTestKernelUtils.h:254