| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include <stddef.h> // for NULL |
| 36 |
#include <iostream> // for ostream, operator<<, etc |
| 37 |
#include <stdexcept> // for runtime_error |
| 38 |
#include <stk_mesh/base/FindRestriction.hpp> // for find_restriction |
| 39 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 40 |
#include <gtest/gtest.h> |
| 41 |
#include <string> // for string |
| 42 |
#include <vector> // for vector |
| 43 |
#include "stk_mesh/base/Field.hpp" // for Field |
| 44 |
#include "stk_mesh/base/FieldBase.hpp" // for FieldBase, etc |
| 45 |
#include "stk_mesh/base/FieldRestriction.hpp" // for FieldRestriction |
| 46 |
#include "stk_mesh/base/FieldState.hpp" // for FieldState::StateOld, etc |
| 47 |
#include "stk_mesh/base/Part.hpp" // for Part |
| 48 |
#include "stk_mesh/base/Selector.hpp" // for Selector |
| 49 |
#include "stk_mesh/base/Types.hpp" // for FieldVector |
| 50 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 51 |
|
| 52 |
namespace { |
| 53 |
|
| 54 |
using namespace stk::mesh; |
| 55 |
|
| 56 |
class UnitTestFieldImpl : public ::testing::Test |
| 57 |
{ |
| 58 |
public: |
| 59 |
UnitTestFieldImpl() |
| 60 |
: dummy_names(4, "dummy"), |
| 61 |
meta_data(3 /*dim*/, dummy_names), |
| 62 |
pA(meta_data.declare_part( std::string("A") , stk::topology::NODE_RANK)), |
| 63 |
pB(meta_data.declare_part( std::string("B") , stk::topology::NODE_RANK)), |
| 64 |
pC(meta_data.declare_part( std::string("C") , stk::topology::NODE_RANK)), |
| 65 |
pD(meta_data.declare_part( std::string("D") , stk::topology::NODE_RANK)) |
| 66 |
{ |
| 67 |
} |
| 68 |
|
| 69 |
void testFieldRestriction(); |
| 70 |
void testRestrictions3parts(); |
| 71 |
void testRestrictions3parts_union(); |
| 72 |
void testRestrictions3parts_differentNumScalars(); |
| 73 |
void testRestrictions3parts_supersetSubset(); |
| 74 |
void testRestrictions4parts_1union_1intersection(); |
| 75 |
void testRestrictions4parts_2unions(); |
| 76 |
|
| 77 |
void print_restrictions(const FieldRestrictionVector& restrs) |
| 78 |
{ |
| 79 |
for(const FieldRestriction& restr : restrs) { |
| 80 |
std::cout << restr.selector() << " : " << restr.num_scalars_per_entity() << std::endl; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
private: |
| 85 |
std::vector<std::string> dummy_names; |
| 86 |
MetaData meta_data; |
| 87 |
|
| 88 |
Part& pA; |
| 89 |
Part& pB; |
| 90 |
Part& pC; |
| 91 |
Part& pD; |
| 92 |
}; |
| 93 |
|
| 94 |
void UnitTestFieldImpl::testFieldRestriction() |
| 95 |
{ |
| 96 |
unsigned stride[8] ; |
| 97 |
|
| 98 |
stride[0] = 10 ; |
| 99 |
for ( unsigned i = 1 ; i < 8 ; ++i ) { |
| 100 |
stride[i] = ( i + 1 ) * stride[i-1] ; |
| 101 |
} |
| 102 |
|
| 103 |
const FieldVector & allocated_fields = meta_data.get_fields(); |
| 104 |
|
| 105 |
//------------------------------ |
| 106 |
|
| 107 |
FieldBase * const f2 = &meta_data.declare_field<double>( stk::topology::NODE_RANK, std::string("F2"), 1/* # states */ ); |
| 108 |
|
| 109 |
//------------------------------ |
| 110 |
|
| 111 |
FieldBase * const nodeField = &meta_data.declare_field<double>( stk::topology::NODE_RANK, std::string("nodeField"), 2/* #states*/); |
| 112 |
FieldBase * const edgeField = &meta_data.declare_field<double>( stk::topology::EDGE_RANK, std::string("edgeField"), 2/* #states*/); |
| 113 |
FieldBase * const faceField = &meta_data.declare_field<double>( stk::topology::FACE_RANK, std::string("faceField"), 2/* #states*/); |
| 114 |
|
| 115 |
FieldBase * const f3_old = nodeField->field_state( StateOld ) ; |
| 116 |
|
| 117 |
//------------------------------ |
| 118 |
// Test for correctness of vector of declared fields. |
| 119 |
if (meta_data.is_field_sync_debugger_enabled()) { |
| 120 |
ASSERT_EQ(14u, allocated_fields.size()); |
| 121 |
ASSERT_TRUE( f2 == allocated_fields[0] ); |
| 122 |
ASSERT_TRUE( nodeField == allocated_fields[2] ); |
| 123 |
} |
| 124 |
else { |
| 125 |
ASSERT_EQ(7u, allocated_fields.size()); |
| 126 |
ASSERT_TRUE( f2 == allocated_fields[0] ); |
| 127 |
ASSERT_TRUE( nodeField == allocated_fields[1] ); |
| 128 |
} |
| 129 |
|
| 130 |
//------------------------------ |
| 131 |
// Test for correctness of field internal state access: |
| 132 |
|
| 133 |
ASSERT_TRUE( f2 == f2->field_state( StateNone ) ); |
| 134 |
ASSERT_TRUE( NULL == f2->field_state( StateOld ) ); |
| 135 |
ASSERT_TRUE( nodeField == nodeField->field_state( StateNew ) ); |
| 136 |
ASSERT_TRUE( f3_old == nodeField->field_state( StateOld ) ); |
| 137 |
ASSERT_TRUE( NULL == nodeField->field_state( StateNM1 ) ); |
| 138 |
ASSERT_TRUE( nodeField == f3_old->field_state( StateNew ) ); |
| 139 |
ASSERT_TRUE( f3_old == f3_old->field_state( StateOld ) ); |
| 140 |
ASSERT_TRUE( NULL == f3_old->field_state( StateNM1 ) ); |
| 141 |
|
| 142 |
// Declare three restrictions: |
| 143 |
|
| 144 |
meta_data.declare_field_restriction(*nodeField, pA, stride[0], stride[0]); |
| 145 |
meta_data.declare_field_restriction(*edgeField, pB, stride[1], stride[1]); |
| 146 |
meta_data.declare_field_restriction(*faceField, pC, stride[2], stride[2]); |
| 147 |
|
| 148 |
// Check for correctness of restrictions: |
| 149 |
|
| 150 |
ASSERT_TRUE( nodeField->restrictions().size() == 1 ); |
| 151 |
ASSERT_TRUE( nodeField->restrictions()[0] == FieldRestriction( pA ) ); |
| 152 |
ASSERT_TRUE( edgeField->restrictions()[0] == FieldRestriction( pB ) ); |
| 153 |
ASSERT_TRUE( faceField->restrictions()[0] == FieldRestriction( pC ) ); |
| 154 |
|
| 155 |
meta_data.declare_field_restriction(*nodeField, pB, stride[1], stride[1]); |
| 156 |
|
| 157 |
ASSERT_EQ( nodeField->max_size() , 20u ); |
| 158 |
|
| 159 |
//------------------------------ |
| 160 |
// Check for error detection of bad stride: |
| 161 |
{ |
| 162 |
unsigned bad_stride[4] = { 5 , 4 , 6 , 3 }; |
| 163 |
Selector selA(pA); |
| 164 |
ASSERT_THROW(meta_data.declare_field_restriction(*nodeField , selA, 5*4*6 , bad_stride[0] ), std::runtime_error); |
| 165 |
ASSERT_THROW(meta_data.declare_field_restriction(*nodeField , pA, 5*4*6 , bad_stride[0] ), std::runtime_error); |
| 166 |
ASSERT_EQ(2u, nodeField->restrictions().size()); |
| 167 |
} |
| 168 |
|
| 169 |
// Check for error detection in re-declaring an incompatible |
| 170 |
// field restriction. |
| 171 |
{ |
| 172 |
Selector selA(pA); |
| 173 |
ASSERT_THROW(meta_data.declare_field_restriction(*nodeField, selA, stride[1], stride[1]), std::runtime_error); |
| 174 |
ASSERT_THROW(meta_data.declare_field_restriction(*nodeField, pA, stride[1], stride[1]), std::runtime_error); |
| 175 |
ASSERT_EQ(2u, nodeField->restrictions().size()); |
| 176 |
} |
| 177 |
|
| 178 |
// Verify and clean out any redundant restructions: |
| 179 |
|
| 180 |
ASSERT_TRUE( nodeField->restrictions().size() == 2 ); |
| 181 |
|
| 182 |
#ifndef NDEBUG |
| 183 |
//The following checking/cleaning of restrictions can be expensive, so it |
| 184 |
//is only done in debug mode. |
| 185 |
//------------------------------ |
| 186 |
// Introduce a redundant restriction, clean it, and |
| 187 |
// check that it was cleaned. |
| 188 |
Part & pD = meta_data.declare_part( std::string("D") , stk::topology::NODE_RANK ); |
| 189 |
|
| 190 |
meta_data.declare_part_subset( pD, pA ); |
| 191 |
meta_data.declare_field_restriction(*f2, pA, stride[0], stride[0]); |
| 192 |
meta_data.declare_field_restriction(*f2, pD, stride[0], stride[0]); |
| 193 |
|
| 194 |
unsigned expected = 1; |
| 195 |
ASSERT_TRUE( f2->restrictions().size() == expected ); |
| 196 |
|
| 197 |
{ |
| 198 |
const FieldBase::Restriction & rA = stk::mesh::find_restriction(*f2, stk::topology::NODE_RANK, pA ); |
| 199 |
const FieldBase::Restriction & rD = stk::mesh::find_restriction(*f2, stk::topology::NODE_RANK, pD ); |
| 200 |
ASSERT_TRUE( & rA == & rD ); |
| 201 |
ASSERT_TRUE( rA.selector() == pD ); |
| 202 |
} |
| 203 |
|
| 204 |
//------------------------------ |
| 205 |
// Introduce a new restriction, then introduce a |
| 206 |
// subset-superset relationship that renders the new restriction |
| 207 |
// redundant and incompatible. |
| 208 |
// Check that the verify_and_clean_restrictions method detects |
| 209 |
// this error condition. |
| 210 |
{ |
| 211 |
meta_data.declare_field_restriction(*f2, pB, stride[1], stride[1]); |
| 212 |
ASSERT_THROW(meta_data.declare_part_subset(pD, pB), std::runtime_error); |
| 213 |
} |
| 214 |
|
| 215 |
//Coverage for error from print_restriction in FieldBaseImpl.cpp when there is no stride (!stride[i]) |
| 216 |
//Call print_restriction from insert_restriction |
| 217 |
{ |
| 218 |
unsigned arg_no_stride[2]; |
| 219 |
|
| 220 |
arg_no_stride[0] = 1; |
| 221 |
arg_no_stride[1] = 0; |
| 222 |
|
| 223 |
ASSERT_ANY_THROW(meta_data.declare_field_restriction(*f2, pA, 0, arg_no_stride[0])); |
| 224 |
} |
| 225 |
#endif |
| 226 |
} |
| 227 |
|
| 228 |
void UnitTestFieldImpl::testRestrictions3parts() |
| 229 |
{ |
| 230 |
FieldBase * const nodeField = &meta_data.declare_field<double>( stk::topology::NODE_RANK, std::string("nodeField"), 2/* #states*/); |
| 231 |
|
| 232 |
const unsigned numScalarsPerEntity = 3; |
| 233 |
|
| 234 |
meta_data.declare_field_restriction(*nodeField, pA, numScalarsPerEntity, 1); |
| 235 |
meta_data.declare_field_restriction(*nodeField, pB, numScalarsPerEntity, 1); |
| 236 |
meta_data.declare_field_restriction(*nodeField, pC, numScalarsPerEntity, 1); |
| 237 |
|
| 238 |
const unsigned expectedNumRestrictions = 1; |
| 239 |
EXPECT_EQ(expectedNumRestrictions, nodeField->restrictions().size()); |
| 240 |
print_restrictions(nodeField->restrictions()); |
| 241 |
} |
| 242 |
|
| 243 |
void UnitTestFieldImpl::testRestrictions3parts_union() |
| 244 |
{ |
| 245 |
FieldBase * const nodeField = &meta_data.declare_field<double>( stk::topology::NODE_RANK, std::string("nodeField"), 2/* #states*/); |
| 246 |
|
| 247 |
const unsigned numScalarsPerEntity = 3; |
| 248 |
|
| 249 |
Selector abcUnion = pA | pB | pC; |
| 250 |
meta_data.declare_field_restriction(*nodeField, abcUnion, numScalarsPerEntity, 1); |
| 251 |
meta_data.declare_field_restriction(*nodeField, pC, numScalarsPerEntity, 1); |
| 252 |
|
| 253 |
const unsigned expectedNumRestrictions = 1; |
| 254 |
EXPECT_EQ(expectedNumRestrictions, nodeField->restrictions().size()); |
| 255 |
print_restrictions(nodeField->restrictions()); |
| 256 |
} |
| 257 |
|
| 258 |
void UnitTestFieldImpl::testRestrictions3parts_differentNumScalars() |
| 259 |
{ |
| 260 |
FieldBase * const nodeField = &meta_data.declare_field<double>( stk::topology::NODE_RANK, std::string("nodeField"), 2/* #states*/); |
| 261 |
|
| 262 |
unsigned numScalarsPerEntity = 3; |
| 263 |
|
| 264 |
meta_data.declare_field_restriction(*nodeField, pA, numScalarsPerEntity, 1); |
| 265 |
meta_data.declare_field_restriction(*nodeField, pB, numScalarsPerEntity, 1); |
| 266 |
|
| 267 |
numScalarsPerEntity = 1; |
| 268 |
meta_data.declare_field_restriction(*nodeField, pC, numScalarsPerEntity, 1); |
| 269 |
|
| 270 |
const unsigned expectedNumRestrictions = 2; |
| 271 |
EXPECT_EQ(expectedNumRestrictions, nodeField->restrictions().size()); |
| 272 |
print_restrictions(nodeField->restrictions()); |
| 273 |
} |
| 274 |
|
| 275 |
void UnitTestFieldImpl::testRestrictions3parts_supersetSubset() |
| 276 |
{ |
| 277 |
FieldBase * const nodeField = &meta_data.declare_field<double>( stk::topology::NODE_RANK, std::string("nodeField"), 2/* #states*/); |
| 278 |
|
| 279 |
meta_data.declare_part_subset(pB, pC); |
| 280 |
|
| 281 |
const unsigned numScalarsPerEntity = 3; |
| 282 |
|
| 283 |
meta_data.declare_field_restriction(*nodeField, pA, numScalarsPerEntity, 1); |
| 284 |
meta_data.declare_field_restriction(*nodeField, pB, numScalarsPerEntity, 1); |
| 285 |
meta_data.declare_field_restriction(*nodeField, pC, numScalarsPerEntity, 1); |
| 286 |
|
| 287 |
const unsigned expectedNumRestrictions = 1; |
| 288 |
EXPECT_EQ(expectedNumRestrictions, nodeField->restrictions().size()); |
| 289 |
print_restrictions(nodeField->restrictions()); |
| 290 |
} |
| 291 |
|
| 292 |
void UnitTestFieldImpl::testRestrictions4parts_1union_1intersection() |
| 293 |
{ |
| 294 |
FieldBase * const nodeField = &meta_data.declare_field<double>( stk::topology::NODE_RANK, std::string("nodeField"), 2/* #states*/); |
| 295 |
|
| 296 |
const unsigned numScalarsPerEntity = 3; |
| 297 |
|
| 298 |
Selector unionAB = pA | pB; |
| 299 |
meta_data.declare_field_restriction(*nodeField, unionAB, numScalarsPerEntity, 1); |
| 300 |
|
| 301 |
Selector intersectionCD = pC & pD; |
| 302 |
meta_data.declare_field_restriction(*nodeField, intersectionCD, numScalarsPerEntity, 1); |
| 303 |
|
| 304 |
const unsigned expectedNumRestrictions = 1; |
| 305 |
EXPECT_EQ(expectedNumRestrictions, nodeField->restrictions().size()); |
| 306 |
print_restrictions(nodeField->restrictions()); |
| 307 |
} |
| 308 |
|
| 309 |
void UnitTestFieldImpl::testRestrictions4parts_2unions() |
| 310 |
{ |
| 311 |
FieldBase * const nodeField = &meta_data.declare_field<double>( stk::topology::NODE_RANK, std::string("nodeField"), 2/* #states*/); |
| 312 |
|
| 313 |
const unsigned numScalarsPerEntity = 3; |
| 314 |
|
| 315 |
Selector unionAB = pA | pB; |
| 316 |
meta_data.declare_field_restriction(*nodeField, unionAB, numScalarsPerEntity, 1); |
| 317 |
|
| 318 |
Selector unionCD = pC | pD; |
| 319 |
meta_data.declare_field_restriction(*nodeField, unionCD, numScalarsPerEntity, 1); |
| 320 |
|
| 321 |
const unsigned expectedNumRestrictions = 1; |
| 322 |
EXPECT_EQ(expectedNumRestrictions, nodeField->restrictions().size()); |
| 323 |
print_restrictions(nodeField->restrictions()); |
| 324 |
} |
| 325 |
|
| 326 |
TEST_F(UnitTestFieldImpl, testRestriction) |
| 327 |
{ |
| 328 |
testFieldRestriction(); |
| 329 |
} |
| 330 |
|
| 331 |
TEST_F(UnitTestFieldImpl, testRestrictions3parts) |
| 332 |
{ |
| 333 |
testRestrictions3parts(); |
| 334 |
} |
| 335 |
|
| 336 |
TEST_F(UnitTestFieldImpl, testRestrictions3parts_union) |
| 337 |
{ |
| 338 |
testRestrictions3parts_union(); |
| 339 |
} |
| 340 |
|
| 341 |
TEST_F(UnitTestFieldImpl, testRestrictions3parts_differentNumScalars) |
| 342 |
{ |
| 343 |
testRestrictions3parts_differentNumScalars(); |
| 344 |
} |
| 345 |
|
| 346 |
TEST_F(UnitTestFieldImpl, testRestrictions3parts_supersetSubset) |
| 347 |
{ |
| 348 |
testRestrictions3parts_supersetSubset(); |
| 349 |
} |
| 350 |
|
| 351 |
TEST_F(UnitTestFieldImpl, testRestrictions4parts_1union_1intersection) |
| 352 |
{ |
| 353 |
testRestrictions4parts_1union_1intersection(); |
| 354 |
} |
| 355 |
|
| 356 |
TEST_F(UnitTestFieldImpl, testRestrictions4parts_2unions) |
| 357 |
{ |
| 358 |
testRestrictions4parts_2unions(); |
| 359 |
} |
| 360 |
|
| 361 |
}//namespace <anonymous> |
| 362 |
|