Sierra Toolkit  Version of the Day
UnitTestIOFixture.cpp
1 #include <stk_io/util/IO_Fixture.hpp>
2 
3 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
4 #include <stk_mesh/base/GetEntities.hpp>
5 #include <stk_mesh/base/BulkData.hpp>
6 
7 #include <string>
8 #include <stdlib.h>
9 
10 namespace {
11 
12 void activate_entities(stk_classic::io::util::IO_Fixture &fixture,
13  stk_classic::mesh::Part &active_part)
14 {
15  // Seed generator so multiple calls produce same result
16  srand(999999u);
17  stk_classic::mesh::fem::FEMMetaData & meta = fixture.meta_data();
18  stk_classic::mesh::BulkData &bulk = fixture.bulk_data();
19 
20  stk_classic::mesh::EntityRank elem_rank = meta.element_rank();
21 
22  stk_classic::mesh::PartVector add_parts(1, &active_part);
23 
24  bulk.modification_begin();
25  const stk_classic::mesh::PartVector & all_parts = meta.get_parts();
26  for ( stk_classic::mesh::PartVector::const_iterator
27  ip = all_parts.begin(); ip != all_parts.end(); ++ip ) {
28 
29  stk_classic::mesh::Part * const part = *ip;
30  if (stk_classic::io::is_part_io_part(*part) && part->primary_entity_rank() == elem_rank) {
31  // Get all entities (elements) on this part...
32  std::vector<stk_classic::mesh::Entity*> entities;
33  stk_classic::mesh::Selector select = meta.locally_owned_part() & *part;
34  stk_classic::mesh::get_selected_entities(select, bulk.buckets(elem_rank), entities);
35  for (size_t i=0; i < entities.size(); i++) {
36  if (rand() > (RAND_MAX/4)*3)
37  bulk.change_entity_parts(*entities[i], add_parts);
38  }
39  }
40  }
41  bulk.modification_end();
42 }
43 
44 }
45 
46 STKUNIT_UNIT_TEST( IOFixture, iofixture )
47 {
48  // A simple test for reading and writing an exodus file using the IOFixture.
49 
50  stk_classic::ParallelMachine pm = MPI_COMM_WORLD;
51 
53 
54  std::string input_base_filename = "unit_test.g";
55 
56  // Initialize meta data from exodus file
57  fixture.initialize_meta_data( input_base_filename, "exodusii" );
58 
59  stk_classic::mesh::fem::FEMMetaData & meta_data = fixture.meta_data();
60 
61  // Commit meta_data
62  meta_data.commit();
63 
64  // bulk_data initialize (from exodus file)
65  fixture.initialize_bulk_data();
66 
67  // exodus file creation
68  std::string output_base_filename = "unit_test_output.e";
69  fixture.create_output_mesh( output_base_filename, "exodusii" );
70 
71  // process output
72  const double time_step = 0;
73  fixture.add_timestep_to_output_mesh( time_step );
74 
75  // Since correctness can only be established by running SEACAS tools, correctness
76  // checking is left to the test XML.
77 }
78 
79 STKUNIT_UNIT_TEST( IOFixture, active_only )
80 {
81  // A simple test for reading and writing an exodus file using the IOFixture.
82 
83  stk_classic::ParallelMachine pm = MPI_COMM_WORLD;
85 
86  std::string input_base_filename = "unit_test.g";
87 
88  // Initialize meta data from exodus file
89  fixture.initialize_meta_data( input_base_filename, "exodusii" );
90  stk_classic::mesh::fem::FEMMetaData & meta_data = fixture.meta_data();
91 
92  // Add an "active" part...
93  stk_classic::mesh::Part &active = meta_data.declare_part("active", meta_data.element_rank());
94  meta_data.commit();
95 
96  // bulk_data initialize (from exodus file)
97  fixture.initialize_bulk_data();
98 
99  // Put some entities into the "active" part...
100  // This will be used to test the I/O filtering via a selector...
101  activate_entities(fixture, active);
102 
103  // Set the output filter on the mesh_data...
104  stk_classic::mesh::Selector active_selector(active);
105  fixture.mesh_data().m_anded_selector = &active_selector;
106 
107  // exodus file creation
108  std::string output_base_filename = "unit_test_output_filtered.e";
109  fixture.create_output_mesh( output_base_filename, "exodusii" );
110 
111  // process output
112  const double time_step = 0;
113  fixture.add_timestep_to_output_mesh( time_step );
114 
115 
116  // Since correctness can only be established by running SEACAS tools, correctness
117  // checking is left to the test XML.
118 }
119 
120 STKUNIT_UNIT_TEST( IOFixture, active_and_all )
121 {
122  // A simple test for reading and writing two exodus files using the IOFixture.
123  stk_classic::ParallelMachine pm = MPI_COMM_WORLD;
125 
126  std::string input_base_filename = "unit_test.g";
127 
128  // Initialize meta data from exodus file
129  fixture.initialize_meta_data( input_base_filename, "exodusii" );
130  stk_classic::mesh::fem::FEMMetaData & meta_data = fixture.meta_data();
131 
132  // Add an "active" part...
133  stk_classic::mesh::Part &active = meta_data.declare_part("active", meta_data.element_rank());
134  meta_data.commit();
135 
136  // bulk_data initialize (from exodus file)
137  fixture.initialize_bulk_data();
138 
139  // Put some entities into the "active" part...
140  // This will be used to test the I/O filtering via a selector...
141  activate_entities(fixture, active);
142 
143  // Set the output filter on the mesh_data...
144  stk_classic::mesh::Selector active_selector(active);
145  fixture.mesh_data().m_anded_selector = &active_selector;
146 
147  // exodus file creation
148  std::string filtered_output_base_filename = "unit_test_output_first_of_two.e";
149  fixture.create_output_mesh( filtered_output_base_filename, "exodusii" );
150 
151  // process output
152  double time_step = 0;
153  fixture.add_timestep_to_output_mesh( time_step );
154 
155 
156  Teuchos::RCP<Ioss::Region> active_output_ioss_region = fixture.output_ioss_region();
157 
158  // Set the output filter on the mesh_data...
159  stk_classic::mesh::Selector universal_selector(meta_data.universal_part());
160  fixture.mesh_data().m_anded_selector = &universal_selector;
161 
162  // exodus file creation
163  std::string unfiltered_output_base_filename = "unit_test_output_second_of_two.e";
164  fixture.create_output_mesh( unfiltered_output_base_filename, "exodusii" );
165  fixture.add_timestep_to_output_mesh( time_step );
166 
167  Teuchos::RCP<Ioss::Region> universal_output_ioss_region = fixture.output_ioss_region();
168 
169  ++time_step;
170 
171  fixture.output_ioss_region(active_output_ioss_region);
172  fixture.mesh_data().m_anded_selector = &active_selector;
173  fixture.add_timestep_to_output_mesh( time_step );
174 
175  fixture.output_ioss_region(universal_output_ioss_region);
176  fixture.mesh_data().m_anded_selector = &universal_selector;
177  fixture.add_timestep_to_output_mesh( time_step );
178 
179  ++time_step;
180 
181  fixture.output_ioss_region(active_output_ioss_region);
182  fixture.mesh_data().m_anded_selector = &active_selector;
183  fixture.add_timestep_to_output_mesh( time_step );
184 
185  fixture.output_ioss_region(universal_output_ioss_region);
186  fixture.mesh_data().m_anded_selector = &universal_selector;
187  fixture.add_timestep_to_output_mesh( time_step );
188  // Since correctness can only be established by running SEACAS tools, correctness
189  // checking is left to the test XML.
190 }
191 
192 STKUNIT_UNIT_TEST( IOFixture, large_mesh_test )
193 {
194  // A simple test for reading and writing two exodus files using the IOFixture.
195  stk_classic::ParallelMachine pm = MPI_COMM_WORLD;
197 
198  std::string input_base_filename = "1mCube_20x20x20.g";
199 
200  // Initialize meta data from exodus file
201  fixture.initialize_meta_data( input_base_filename, "exodusii" );
202  stk_classic::mesh::fem::FEMMetaData & meta_data = fixture.meta_data();
203 
204  // Commit
205  meta_data.commit();
206 
207  // bulk_data initialize (from exodus file)
208  fixture.initialize_bulk_data();
209  stk_classic::mesh::BulkData &bulk_data = fixture.bulk_data();
210 
211  const std::vector< stk_classic::mesh::Bucket * > & element_buckets
212  = bulk_data.buckets( meta_data.element_rank());
213 
214  // iterate elements and check num nodal relations
215  for ( std::vector<stk_classic::mesh::Bucket*>::const_iterator ib = element_buckets.begin() ;
216  ib != element_buckets.end() ; ++ib ) {
217  stk_classic::mesh::Bucket & b = **ib ;
218  const int length = b.size();
219  for ( int k = 0 ; k < length ; ++k ) {
220  // get element
221  stk_classic::mesh::Entity &elem = b[k];
222  stk_classic::mesh::PairIterRelation elem_node_rels = elem.relations(stk_classic::mesh::fem::FEMMetaData::NODE_RANK);
223  STKUNIT_EXPECT_EQ( 8u, elem_node_rels.size());
224  }
225  }
226 }
void add_timestep_to_output_mesh(double time)
Definition: IO_Fixture.cpp:129
Part & locally_owned_part() const
Subset for the problem domain that is owned by the local process. Ghost entities are not members of t...
FEMMetaData is a class that implements a Finite Element Method skin on top of the Sierra Tool Kit Met...
Definition: FEMMetaData.hpp:54
EntityRank element_rank() const
Returns the element rank which is always equal to spatial dimension.
void initialize_meta_data(const std::string &base_filename, const std::string &type="exodusii")
Definition: IO_Fixture.cpp:150
Part & universal_part() const
Universal subset for the problem domain. All other parts are a subset of the universal part...
This is a class for selecting buckets based on a set of meshparts and set logic.
Definition: Selector.hpp:112
void create_output_mesh(const std::string &base_exodus_filename, bool add_transient=true, bool add_all_fields=false)
Definition: IO_Fixture.cpp:34
const PartVector & get_parts() const
Query all parts of the mesh ordered by the parts&#39; ordinal.
const std::vector< Bucket * > & buckets(EntityRank rank) const
Query all buckets of a given entity rank.
Definition: BulkData.hpp:195
unsigned primary_entity_rank() const
The primary entity type for this part.
Definition: Part.hpp:64
An application-defined subset of a problem domain.
Definition: Part.hpp:49
void get_selected_entities(const Selector &selector, const std::vector< Bucket * > &input_buckets, std::vector< Entity * > &entities)
Get entities in selected buckets (selected by the given selector instance), and sorted by ID...
Definition: GetEntities.cpp:77
void change_entity_parts(Entity &entity, const PartVector &add_parts, const PartVector &remove_parts=PartVector())
Change the parallel-locally-owned entity&#39;s part membership by adding and/or removing parts...
Definition: BulkData.hpp:249
size_t size() const
Number of entities associated with this bucket.
Definition: Bucket.hpp:119
Part & declare_part(const std::string &name, fem::CellTopology cell_topology)
Declare a part with a given cell topology.
bool modification_end()
Parallel synchronization of modifications and transition to the guaranteed parallel consistent state...
bool modification_begin()
Begin a modification phase during which the mesh bulk data could become parallel inconsistent. This is a parallel synchronous call. The first time this method is called the mesh meta data is verified to be committed and parallel consistent. An exception is thrown if this verification fails.
Definition: BulkData.cpp:172
bool is_part_io_part(stk_classic::mesh::Part &part)
PairIterRelation relations() const
All Entity relations for which this entity is a member. The relations are ordered from lowest entity-...
Definition: Entity.hpp:161
Manager for an integrated collection of entities, entity relations, and buckets of field data...
Definition: BulkData.hpp:49
void commit()
Commit the part and field declarations so that the meta data manager can be used to create mesh bulk ...
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
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