Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/ngp/NgpFieldTestUtils.hpp

raw
1 #ifndef NgpFieldTestUtils_hpp
2 #define NgpFieldTestUtils_hpp
3
4 #include <gtest/gtest.h>
5 #include <Kokkos_Core.hpp>
6 #include <stk_util/ngp/NgpSpaces.hpp>
7 #include <stk_mesh/base/Ngp.hpp>
8 #include <stk_mesh/base/NgpMesh.hpp>
9 #include <stk_mesh/base/BulkData.hpp>
10 #include "stk_mesh/base/NgpForEachEntity.hpp"
11
12 namespace ngp_field_test_utils {
13
14 template<typename T>
15 void check_field_data_on_device(stk::mesh::NgpMesh& ngpMesh,
16 stk::mesh::NgpField<T>& ngpField,
17 const stk::mesh::Selector& selector,
18 T expectedValue,
19 int component = -1,
20 T componentValue = 0)
21 {
22 stk::mesh::for_each_entity_run(ngpMesh, ngpField.get_rank(), selector,
23 KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& entity) {
24 const int numComponents = ngpField.get_num_components_per_entity(entity);
25 for(int i=0; i<numComponents; ++i) {
26 if (i == component) {
27 STK_NGP_ThrowRequire(ngpField(entity, i) == componentValue);
28 }
29 else {
30 STK_NGP_ThrowRequire(ngpField(entity, i) == expectedValue);
31 }
32 }
33 }
34 );
35 }
36
37 template<typename T>
38 void check_field_data_on_host(const stk::mesh::HostMesh& stkMesh,
39 const stk::mesh::FieldBase& stkField,
40 const stk::mesh::Selector& selector,
41 T expectedValue,
42 int component = -1,
43 T componentValue = 0)
44 {
45 stk::mesh::for_each_entity_run(stkMesh, stkField.entity_rank(), selector,
46 [&](const stk::mesh::FastMeshIndex& fastMeshIndex) {
47 stk::mesh::Entity entity = stkMesh.get_entity(stkField.entity_rank(), fastMeshIndex);
48 const int numComponents = stk::mesh::field_scalars_per_entity(stkField, entity);
49 const T* fieldData = reinterpret_cast<const T*>(stk::mesh::field_data(stkField, entity));
50 for(int i=0; i<numComponents; ++i) {
51 if (i == component) {
52 STK_ThrowRequireMsg(fieldData[i] == componentValue, "expected "<<componentValue<<" but actual value is "<<fieldData[i]<<"; i=="<<i<<", component=="<<component);
53 }
54 else {
55 STK_ThrowRequireMsg(fieldData[i] == expectedValue, "expected "<<expectedValue<<" but actual value is "<<fieldData[i]<<"; i=="<<i<<", entity="<<stkField.entity_rank()<<","<<stkMesh.identifier(entity));
56 }
57 }
58 }
59 );
60 }
61
62 template<typename T>
63 void check_field_data_on_host(const stk::mesh::BulkData& stkMesh,
64 const stk::mesh::FieldBase& stkField,
65 const stk::mesh::Selector& selector,
66 T expectedValue,
67 int component = -1,
68 T componentValue = 0)
69 {
70 stk::mesh::for_each_entity_run(stkMesh, stkField.entity_rank(), selector,
71 [&](const stk::mesh::BulkData& bulk, const stk::mesh::Entity entity) {
72 const int numComponents = stk::mesh::field_scalars_per_entity(stkField, entity);
73 const T* fieldData = reinterpret_cast<const T*>(stk::mesh::field_data(stkField, entity));
74 for(int i=0; i<numComponents; ++i) {
75 if (i == component) {
76 STK_ThrowRequireMsg(fieldData[i] == componentValue, "expected "<<componentValue<<" but actual value is "<<fieldData[i]<<"; i=="<<i<<", component=="<<component);
77 }
78 else {
79 STK_ThrowRequireMsg(fieldData[i] == expectedValue, "expected "<<expectedValue<<" but actual value is "<<fieldData[i]<<"; i=="<<i<<", entity="<<bulk.entity_key(entity));
80 }
81 }
82 }
83 );
84 }
85
86 inline void set_field_data_on_host(const stk::mesh::BulkData& stkMesh,
87 const stk::mesh::FieldBase& stkField,
88 const stk::mesh::Selector& selector,
89 std::function<std::vector<double>(const double*)> func)
90 {
91 const stk::mesh::FieldBase* coordField = stkMesh.mesh_meta_data().coordinate_field();
92 stk::mesh::for_each_entity_run(stkMesh, stkField.entity_rank(), selector,
93 [&](const stk::mesh::BulkData& /*bulk*/, const stk::mesh::Entity entity) {
94 double* entityCoords = static_cast<double*>(stk::mesh::field_data(*coordField, entity));
95 auto expectedValues = func(entityCoords);
96 const int numComponents = stk::mesh::field_scalars_per_entity(stkField, entity);
97 double* fieldData = static_cast<double*>(stk::mesh::field_data(stkField, entity));
98 for(int i=0; i<numComponents; ++i) {
99 fieldData[i] = expectedValues[i];
100 }
101 }
102 );
103 }
104
105 inline void check_field_data_on_host_func(const stk::mesh::BulkData& stkMesh,
106 const stk::mesh::FieldBase& stkField,
107 const stk::mesh::Selector& selector,
108 const std::vector<const stk::mesh::FieldBase*>& otherFields,
109 std::function<std::vector<double>(const double*)> func)
110 {
111 const stk::mesh::FieldBase* coordField = stkMesh.mesh_meta_data().coordinate_field();
112 stk::mesh::for_each_entity_run(stkMesh, stkField.entity_rank(), selector,
113 [&](const stk::mesh::BulkData& /*bulk*/, const stk::mesh::Entity entity) {
114 double* entityCoords = static_cast<double*>(stk::mesh::field_data(*coordField, entity));
115 auto expectedValues = func(entityCoords);
116
117 unsigned int numComponents = stk::mesh::field_scalars_per_entity(stkField, entity);
118 for (const stk::mesh::FieldBase* otherField : otherFields)
119 {
120 numComponents = std::min(numComponents, stk::mesh::field_scalars_per_entity(*otherField, entity));
121 }
122 const double* fieldData = reinterpret_cast<const double*>(stk::mesh::field_data(stkField, entity));
123 for(unsigned int i=0; i<numComponents; ++i) {
124 EXPECT_FLOAT_EQ(fieldData[i], expectedValues[i]);
125 }
126 }
127 );
128 }
129
130 } // ngp_field_test_utils
131
132 #endif
133