MOAB: Mesh Oriented datABase  (version 5.5.0)
gmsh_test.cpp
Go to the documentation of this file.
1 #include "TestUtil.hpp"
2 #include "moab/Core.hpp"
3 #include "moab/Range.hpp"
4 #include "MBTagConventions.hpp"
5 #include <cmath>
6 #include <algorithm>
7 
8 using namespace moab;
9 
10 /* Input test file: gmsh2.msh
11  *
12  * Example version 2.0 ASCII input file from Gmsh 2.4 manual.
13  */
14 #ifdef MESHDIR
15 static const char example[] = STRINGIFY( MESHDIR ) "/io/gmsh2.msh";
16 static const char example2[] = STRINGIFY( MESHDIR ) "/io/airfoil_exterior.msh";
17 static const char example3[] = STRINGIFY( MESHDIR ) "/io/t1.msh";
18 static const char example4[] = STRINGIFY( MESHDIR ) "/io/ghosts.msh";
19 static const char example5[] = STRINGIFY( MESHDIR ) "/io/gmsh3.msh";
20 #else
21 static const char example[] = "gmsh2.msh";
22 static const char example2[] = "airfoil_exterior.msh";
23 static const char example3[] = "t1.msh";
24 static const char example4[] = "ghosts.msh";
25 static const char example5[] = "gmsh3.msh";
26 #endif
27 
28 void test_read_nodes();
29 void test_read_quads();
32 void test_read_geom_set();
33 void test_read_airfoil();
34 void test_read_t1();
35 void test_read_fgh(); // file with ghosts id for partition
36 
37 void read_file( Interface& moab, const char* input_file );
38 
39 int main()
40 {
41  int result = 0;
42 
43  result += RUN_TEST( test_read_nodes );
44  result += RUN_TEST( test_read_quads );
45  result += RUN_TEST( test_read_material_set );
47  result += RUN_TEST( test_read_geom_set );
48 
49  result += RUN_TEST( test_read_airfoil );
50  result += RUN_TEST( test_read_t1 );
51  result += RUN_TEST( test_read_fgh );
52  return result;
53 }
54 
55 void read_file( Interface& moab, const char* input_file )
56 {
57  ErrorCode rval;
58  rval = moab.load_file( input_file );CHECK_ERR( rval );
59 }
60 
62 {
63  const double eps = 1e-100;
64  ErrorCode rval;
65  Core moab;
66  Interface& mb = moab;
68 
69  std::vector< EntityHandle > nodes;
70  rval = mb.get_entities_by_type( 0, MBVERTEX, nodes );CHECK_ERR( rval );
71  CHECK_EQUAL( (size_t)6, nodes.size() );
72 
74 
75  std::vector< int > ids( nodes.size() );
76  rval = mb.tag_get_data( id_tag, &nodes[0], nodes.size(), &ids[0] );CHECK_ERR( rval );
77 
78  std::vector< int > sorted_ids( ids );
79  std::sort( sorted_ids.begin(), sorted_ids.end() );
80 
81  std::vector< double > coords( 3 * nodes.size() );
82  rval = mb.get_coords( &nodes[0], nodes.size(), &coords[0] );CHECK_ERR( rval );
83 
84  int idx, pos = 0;
85  CHECK_EQUAL( pos + 1, sorted_ids[pos] );
86  idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
87  CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
88  CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
89  CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
90 
91  ++pos;
92  CHECK_EQUAL( pos + 1, sorted_ids[pos] );
93  idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
94  CHECK_REAL_EQUAL( coords[3 * idx + 0], 1.0, eps );
95  CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
96  CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
97 
98  ++pos;
99  CHECK_EQUAL( pos + 1, sorted_ids[pos] );
100  idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
101  CHECK_REAL_EQUAL( coords[3 * idx + 0], 1.0, eps );
102  CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
103  CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
104 
105  ++pos;
106  CHECK_EQUAL( pos + 1, sorted_ids[pos] );
107  idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
108  CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
109  CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
110  CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
111 
112  ++pos;
113  CHECK_EQUAL( pos + 1, sorted_ids[pos] );
114  idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
115  CHECK_REAL_EQUAL( coords[3 * idx + 0], 2.0, eps );
116  CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
117  CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
118 
119  ++pos;
120  CHECK_EQUAL( pos + 1, sorted_ids[pos] );
121  idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
122  CHECK_REAL_EQUAL( coords[3 * idx + 0], 2.0, eps );
123  CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
124  CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
125 }
126 
128 {
129  ErrorCode rval;
130  Core moab;
131  Interface& mb = moab;
132  read_file( moab, example );
133 
134  std::vector< EntityHandle > quads;
135  rval = mb.get_entities_by_type( 0, MBQUAD, quads );CHECK_ERR( rval );
136  CHECK_EQUAL( (size_t)2, quads.size() );
137 
139 
140  std::vector< int > ids( quads.size() );
141  rval = mb.tag_get_data( id_tag, &quads[0], quads.size(), &ids[0] );CHECK_ERR( rval );
142 
143  if( ids[0] != 1 )
144  {
145  std::swap( ids[0], ids[1] );
146  std::swap( quads[0], quads[1] );
147  }
148 
149  int vtx_ids[4];
150  const EntityHandle* conn;
151  int len;
152 
153  const int conn1[] = { 1, 2, 3, 4 };
154  int pos = 0;
155  CHECK_EQUAL( pos + 1, ids[pos] );
156  rval = mb.get_connectivity( quads[pos], conn, len );CHECK_ERR( rval );
157  CHECK_EQUAL( 4, len );
158  rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
159  CHECK_ARRAYS_EQUAL( conn1, 4, vtx_ids, len );
160 
161  const int conn2[] = { 2, 5, 6, 3 };
162  ++pos;
163  CHECK_EQUAL( pos + 1, ids[pos] );
164  rval = mb.get_connectivity( quads[pos], conn, len );CHECK_ERR( rval );
165  CHECK_EQUAL( 4, len );
166  rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
167  CHECK_ARRAYS_EQUAL( conn2, 4, vtx_ids, len );
168 }
169 
171 {
172  ErrorCode rval;
173  Core moab;
174  Interface& mb = moab;
175  read_file( moab, example );
176 
177  Tag mat_tag;
178  rval = mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mat_tag );CHECK_ERR( rval );
179 
180  Range sets;
181  rval = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, 0, 1, sets );CHECK_ERR( rval );
182  CHECK_EQUAL( 1, (int)sets.size() );
183  EntityHandle set = sets.front();
184 
185  int id;
186  rval = mb.tag_get_data( mat_tag, &set, 1, &id );CHECK_ERR( rval );
187  CHECK_EQUAL( 99, id );
188 
189  std::vector< EntityHandle > quads, contents;
190  rval = mb.get_entities_by_type( 0, MBQUAD, quads );CHECK_ERR( rval );
191  rval = mb.get_entities_by_handle( set, contents );CHECK_ERR( rval );
192  std::sort( quads.begin(), quads.end() );
193  std::sort( contents.begin(), contents.end() );
194  CHECK_EQUAL( quads, contents );
195 }
196 
198 {
199  ErrorCode rval;
200  Core moab;
201  Interface& mb = moab;
202  read_file( moab, example5 );
203 
204  Tag mat_tag;
205  rval = mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mat_tag );CHECK_ERR( rval );
206 
207  Range sets;
208  rval = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, 0, 1, sets );CHECK_ERR( rval );
209  CHECK_EQUAL( 2, (int)sets.size() );
210 
211  EntityHandle set_101 = sets.front();
212  EntityHandle set_102 = sets.back();
213 
214  int id;
215  rval = mb.tag_get_data( mat_tag, &set_101, 1, &id );CHECK_ERR( rval );
216  CHECK_EQUAL( 101, id );
217 
218  rval = mb.tag_get_data( mat_tag, &set_102, 1, &id );CHECK_ERR( rval );
219  CHECK_EQUAL( 102, id );
220 }
221 
223 {
224  ErrorCode rval;
225  Core moab;
226  Interface& mb = moab;
227  read_file( moab, example );
228 
229  Tag dim_tag, id_tag;
231  id_tag = mb.globalId_tag();
232 
233  Range sets;
234  rval = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &dim_tag, 0, 1, sets );CHECK_ERR( rval );
235  CHECK_EQUAL( 1, (int)sets.size() );
236  EntityHandle set = sets.front();
237 
238  int dim;
239  rval = mb.tag_get_data( dim_tag, &set, 1, &dim );CHECK_ERR( rval );
240  CHECK_EQUAL( 2, dim );
241 
242  int id;
243  rval = mb.tag_get_data( id_tag, &set, 1, &id );CHECK_ERR( rval );
244  CHECK_EQUAL( 3, id );
245 
246  std::vector< EntityHandle > quads, contents;
247  rval = mb.get_entities_by_type( 0, MBQUAD, quads );CHECK_ERR( rval );
248  rval = mb.get_entities_by_handle( set, contents );CHECK_ERR( rval );
249  std::sort( quads.begin(), quads.end() );
250  std::sort( contents.begin(), contents.end() );
251  CHECK_EQUAL( quads, contents );
252 }
253 
255 {
256  Core moab;
257  read_file( moab, example2 );
258 }
259 
261 {
262  Core moab;
263  read_file( moab, example3 );
264 }
265 
267 {
268  Core moab;
269  read_file( moab, example4 ); // it should find 3 partitions
270  // read partition sets
271  Tag ptag;
272  ErrorCode rval = moab.tag_get_handle( "PARALLEL_PARTITION", ptag );CHECK_ERR( rval );
273  Range psets;
274  rval = moab.get_entities_by_type_and_tag( 0, MBENTITYSET, &ptag, 0, 1, psets );CHECK_ERR( rval );
275  CHECK_EQUAL( 3, (int)psets.size() );
276  Range ents_first_set;
277  rval = moab.get_entities_by_handle( psets[0], ents_first_set );CHECK_ERR( rval );
278  CHECK_EQUAL( (int)ents_first_set.size(), 98 );
279 }