Sierra Toolkit  Version of the Day
UnitTestFieldDataInitVal.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010, 2011 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #include <sstream>
10 #include <stdexcept>
11 #include <iostream>
12 
13 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
14 
15 #include <stk_util/parallel/Parallel.hpp>
16 
17 #include <stk_mesh/base/BulkData.hpp>
18 #include <stk_mesh/base/Field.hpp>
19 #include <stk_mesh/base/FieldData.hpp>
20 #include <stk_mesh/base/Comm.hpp>
21 #include <stk_mesh/base/EntityComm.hpp>
22 
23 #include <stk_mesh/fem/FEMMetaData.hpp>
24 #include <stk_mesh/fem/CoordinateSystems.hpp>
25 
27 using stk_classic::mesh::EntityRank;
31 using stk_classic::mesh::EntityId;
33 
34 namespace {
35 
36 const EntityRank NODE_RANK = FEMMetaData::NODE_RANK;
37 
38 STKUNIT_UNIT_TEST(UnitTestFieldDataInitVal, test_scalar_field)
39 {
40  // Test that if an initial-value is set on a scalar field, that value is
41  // present the first time field-data is referenced for that field.
42  //
43 
44  stk_classic::ParallelMachine pm = MPI_COMM_WORLD;
45  MPI_Barrier( MPI_COMM_WORLD );
46 
47  // Set up meta and bulk data
48  const unsigned spatial_dim = 2;
49  FEMMetaData meta_data(spatial_dim);
50 
51  const unsigned num_states = 1;
52  Field<double>& dfield = meta_data.declare_field<Field<double> >("double_scalar", num_states);
53 
54  const double initial_value = 99.9;
55 
56  stk_classic::mesh::put_field(dfield, NODE_RANK, meta_data.universal_part(), &initial_value);
57 
58  meta_data.commit();
59 
60  BulkData mesh(FEMMetaData::get_meta_data(meta_data), pm);
61  unsigned p_rank = mesh.parallel_rank();
62 
63  // Begin modification cycle so we can create stuff
64  mesh.modification_begin();
65 
66  // Node will be automatically added to the universal part
68 
69  EntityId node_id = p_rank+1;
70  // Create node
71  Entity & node = mesh.declare_entity(NODE_RANK, node_id, empty_parts);
72 
73  mesh.modification_end();
74 
75  //now insist that data for dfield on node is equal to the initial-value specified above:
76 
77  double* data_ptr = stk_classic::mesh::field_data( dfield, node);
78 
79  STKUNIT_ASSERT_EQUAL( *data_ptr, initial_value );
80 }
81 
82 STKUNIT_UNIT_TEST(UnitTestFieldDataInitVal, test_vector_field)
83 {
84  // Test that if an initial-value is set on a vector field, that value is
85  // present the first time field-data is referenced for that field.
86  //
87 
89 
90  stk_classic::ParallelMachine pm = MPI_COMM_WORLD;
91  MPI_Barrier( MPI_COMM_WORLD );
92 
93  // Set up meta and bulk data
94  const unsigned spatial_dim = 2;
95  FEMMetaData meta_data(spatial_dim);
96 
97  const unsigned num_states = 1;
98  VectorField& vfield = meta_data.declare_field<VectorField>("double_vector", num_states);
99 
100  const double initial_value[stk_classic::mesh::Cartesian2d::Size] = { 50.0, 99.0 };
101 
102  stk_classic::mesh::put_field(vfield, NODE_RANK, meta_data.universal_part(), stk_classic::mesh::Cartesian2d::Size, initial_value);
103 
104  meta_data.commit();
105 
106  BulkData mesh(FEMMetaData::get_meta_data(meta_data), pm);
107  unsigned p_rank = mesh.parallel_rank();
108 
109  // Begin modification cycle so we can create stuff
110  mesh.modification_begin();
111 
112  // Node will be automatically added to the universal part
113  stk_classic::mesh::PartVector empty_parts;
114 
115  EntityId node_id = p_rank+1;
116  // Create node
117  Entity & node = mesh.declare_entity(NODE_RANK, node_id, empty_parts);
118 
119  mesh.modification_end();
120 
121  //now insist that data for vfield on node is equal to the initial-value specified above:
122 
123  double* data_ptr = stk_classic::mesh::field_data( vfield, node);
124 
125  STKUNIT_ASSERT_EQUAL( data_ptr[0], initial_value[0] );
126  STKUNIT_ASSERT_EQUAL( data_ptr[1], initial_value[1] );
127 }
128 
129 STKUNIT_UNIT_TEST(UnitTestFieldDataInitVal, test_vector_field_move_bucket)
130 {
131  // Test that if an initial-value is set on a vector field, that value is
132  // present the first time field-data is referenced for that field, and
133  // that the value is present for a node that is moved from a bucket without
134  // the field to a new bucket that does have the field.
135  //
136 
138 
139  stk_classic::ParallelMachine pm = MPI_COMM_WORLD;
140  MPI_Barrier( MPI_COMM_WORLD );
141 
142  // Set up meta and bulk data
143  const unsigned spatial_dim = 2;
144  FEMMetaData meta_data(spatial_dim);
145 
146  const unsigned num_states = 1;
147  VectorField& vfield = meta_data.declare_field<VectorField>("double_vector", num_states);
148 
149  const double initial_value[stk_classic::mesh::Cartesian2d::Size] = { 50.0, 99.0 };
150 
151  Part& node_part = meta_data.declare_part<shards::Node>("node_part");
152 
153  stk_classic::mesh::put_field(vfield, NODE_RANK, node_part, stk_classic::mesh::Cartesian2d::Size, initial_value);
154 
155  meta_data.commit();
156 
157  BulkData mesh(FEMMetaData::get_meta_data(meta_data), pm);
158  unsigned p_rank = mesh.parallel_rank();
159 
160  // Begin modification cycle so we can create stuff
161  mesh.modification_begin();
162 
163  // Node will be automatically added to the universal part
164  stk_classic::mesh::PartVector empty_parts;
165 
166  EntityId node_id = p_rank+1;
167  // Create node
168  Entity & node = mesh.declare_entity(NODE_RANK, node_id, empty_parts);
169 
170  stk_classic::mesh::Bucket& old_bucket = node.bucket();
171 
172  //Now move the node to the "node_part":
173  stk_classic::mesh::PartVector node_part_vec;
174  node_part_vec.push_back(&node_part);
175  mesh.change_entity_parts(node, node_part_vec);
176 
177  mesh.modification_end();
178 
179  //Insist that the node is now in a different bucket:
180  stk_classic::mesh::Bucket& new_bucket = node.bucket();
181  STKUNIT_ASSERT_NE(&old_bucket, &new_bucket);
182 
183  //now insist that data for vfield on node is equal to the initial-value specified above:
184 
185  double* data_ptr = stk_classic::mesh::field_data( vfield, node);
186 
187  STKUNIT_ASSERT_EQUAL( data_ptr[0], initial_value[0] );
188  STKUNIT_ASSERT_EQUAL( data_ptr[1], initial_value[1] );
189 }
190 
191 STKUNIT_UNIT_TEST(UnitTestFieldDataInitVal, test_multi_state_vector_field)
192 {
193  // Test that if an initial-value is set on a multi-state vector field, that value is
194  // present the first time field-data is referenced for that field.
195  //
196 
198 
199  stk_classic::ParallelMachine pm = MPI_COMM_WORLD;
200  MPI_Barrier( MPI_COMM_WORLD );
201 
202  // Set up meta and bulk data
203  const unsigned spatial_dim = 2;
204  FEMMetaData meta_data(spatial_dim);
205 
206  const unsigned num_states = 2;
207  VectorField& vfield = meta_data.declare_field<VectorField>("double_vector", num_states);
208 
209  const double initial_value[stk_classic::mesh::Cartesian2d::Size] = { 50.0, 99.0 };
210 
211  stk_classic::mesh::put_field(vfield, NODE_RANK, meta_data.universal_part(), stk_classic::mesh::Cartesian2d::Size, initial_value);
212 
213  meta_data.commit();
214 
215  BulkData mesh(FEMMetaData::get_meta_data(meta_data), pm);
216  unsigned p_rank = mesh.parallel_rank();
217 
218  // Begin modification cycle so we can create stuff
219  mesh.modification_begin();
220 
221  // Node will be automatically added to the universal part
222  stk_classic::mesh::PartVector empty_parts;
223 
224  EntityId node_id = p_rank+1;
225  // Create node
226  Entity & node = mesh.declare_entity(NODE_RANK, node_id, empty_parts);
227 
228  mesh.modification_end();
229 
230  //now insist that data for vfield on node is equal to the initial-value specified above:
231 
232  STKUNIT_ASSERT_EQUAL( vfield.number_of_states(), num_states);
233 
236 
237  double* data_ptr_new = stk_classic::mesh::field_data( vfield_new, node);
238  double* data_ptr_old = stk_classic::mesh::field_data( vfield_old, node);
239 
240  STKUNIT_ASSERT_EQUAL( data_ptr_new[0], initial_value[0] );
241  STKUNIT_ASSERT_EQUAL( data_ptr_new[1], initial_value[1] );
242 
243  STKUNIT_ASSERT_EQUAL( data_ptr_old[0], initial_value[0] );
244  STKUNIT_ASSERT_EQUAL( data_ptr_old[1], initial_value[1] );
245 }
246 
247 }
unsigned number_of_states() const
Number of states of this field.
Definition: FieldBase.hpp:78
FEMMetaData is a class that implements a Finite Element Method skin on top of the Sierra Tool Kit Met...
Definition: FEMMetaData.hpp:54
FieldTraits< field_type >::data_type * field_data(const field_type &f, const Bucket::iterator i)
Pointer to the field data array.
Definition: FieldData.hpp:116
Newest state of a field with two states.
Definition: FieldState.hpp:36
Bucket & bucket() const
The bucket which holds this mesh entity&#39;s field data.
Definition: Entity.hpp:141
field_type & put_field(field_type &field, EntityRank entity_rank, const Part &part, const void *init_value=NULL)
Declare a field to exist for a given entity type and Part.
Field with defined data type and multi-dimensions (if any)
Definition: Field.hpp:118
An application-defined subset of a problem domain.
Definition: Part.hpp:49
Manager for an integrated collection of entities, entity relations, and buckets of field data...
Definition: BulkData.hpp:49
A fundamental unit within the discretization of a problem domain, including but not limited to nodes...
Definition: Entity.hpp:120
MPI_Comm ParallelMachine
Definition: Parallel.hpp:32
Previous state of a field with two states.
Definition: FieldState.hpp:38
Field & field_of_state(FieldState input_state) const
Query this field for a given field state.
Definition: Field.hpp:122
std::vector< Part *> PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31
A container for the field data of a homogeneous collection of entities.
Definition: Bucket.hpp:94