1 #include "TestUtil.hpp"
2 #include "moab/Core.hpp"
3 #include "moab/Range.hpp"
4 #include "MBTagConventions.hpp"
5 #include <math.h>
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();
30 void test_read_material_set();
31 void test_read_material_on_nodes();
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 
main()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);
46   result += RUN_TEST(test_read_material_on_nodes);
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 
read_file(Interface & moab,const char * input_file)56 void read_file( Interface& moab, const char* input_file )
57 {
58   ErrorCode rval;
59   rval = moab.load_file( input_file );
60   CHECK_ERR(rval);
61 }
62 
test_read_nodes()63 void test_read_nodes()
64 {
65   const double eps = 1e-100;
66   ErrorCode rval;
67   Core moab;
68   Interface& mb = moab;
69   read_file( moab, example );
70 
71   std::vector<EntityHandle> nodes;
72   rval = mb.get_entities_by_type( 0, MBVERTEX, nodes );
73   CHECK_ERR(rval);
74   CHECK_EQUAL( (size_t)6, nodes.size() );
75 
76   Tag id_tag;
77   rval = mb.tag_get_handle( GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, id_tag );
78   CHECK_ERR(rval);
79 
80   std::vector<int> ids(nodes.size());
81   rval = mb.tag_get_data( id_tag, &nodes[0], nodes.size(), &ids[0] );
82   CHECK_ERR(rval);
83 
84   std::vector<int> sorted_ids( ids );
85   std::sort( sorted_ids.begin(), sorted_ids.end() );
86 
87   std::vector<double> coords(3*nodes.size());
88   rval = mb.get_coords( &nodes[0], nodes.size(), &coords[0] );
89   CHECK_ERR(rval);
90 
91   int idx, pos = 0;
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], 0.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], 0.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], 1.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], 0.0, eps );
116   CHECK_REAL_EQUAL( coords[3*idx+1], 1.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], 0.0, eps );
124   CHECK_REAL_EQUAL( coords[3*idx+2], 0.0, eps );
125 
126   ++pos;
127   CHECK_EQUAL( pos+1, sorted_ids[pos] );
128   idx = std::find( ids.begin(), ids.end(),pos+1 ) - ids.begin();
129   CHECK_REAL_EQUAL( coords[3*idx+0], 2.0, eps );
130   CHECK_REAL_EQUAL( coords[3*idx+1], 1.0, eps );
131   CHECK_REAL_EQUAL( coords[3*idx+2], 0.0, eps );
132 }
133 
134 
test_read_quads()135 void test_read_quads()
136 {
137   ErrorCode rval;
138   Core moab;
139   Interface& mb = moab;
140   read_file( moab, example );
141 
142   std::vector<EntityHandle> quads;
143   rval = mb.get_entities_by_type( 0, MBQUAD, quads );
144   CHECK_ERR(rval);
145   CHECK_EQUAL( (size_t)2, quads.size() );
146 
147   Tag id_tag;
148   rval = mb.tag_get_handle( GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, id_tag );
149   CHECK_ERR(rval);
150 
151   std::vector<int> ids(quads.size());
152   rval = mb.tag_get_data( id_tag, &quads[0], quads.size(), &ids[0] );
153   CHECK_ERR(rval);
154 
155   if (ids[0] != 1) {
156     std::swap( ids[0], ids[1] );
157     std::swap( quads[0], quads[1] );
158   }
159 
160   int vtx_ids[4];
161   const EntityHandle* conn;
162   int len;
163 
164   const int conn1[] = { 1, 2, 3, 4 };
165   int pos = 0;
166   CHECK_EQUAL( pos+1, ids[pos] );
167   rval = mb.get_connectivity( quads[pos], conn, len );
168   CHECK_ERR(rval);
169   CHECK_EQUAL( 4, len );
170   rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );
171   CHECK_ERR(rval);
172   CHECK_ARRAYS_EQUAL( conn1, 4, vtx_ids, len );
173 
174   const int conn2[] = { 2, 5, 6, 3 };
175   ++pos;
176   CHECK_EQUAL( pos+1, ids[pos] );
177   rval = mb.get_connectivity( quads[pos], conn, len );
178   CHECK_ERR(rval);
179   CHECK_EQUAL( 4, len );
180   rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );
181   CHECK_ERR(rval);
182   CHECK_ARRAYS_EQUAL( conn2, 4, vtx_ids, len );
183 }
184 
test_read_material_set()185 void test_read_material_set()
186 {
187   ErrorCode rval;
188   Core moab;
189   Interface& mb = moab;
190   read_file( moab, example );
191 
192   Tag mat_tag;
193   rval = mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mat_tag );
194   CHECK_ERR(rval);
195 
196   Range sets;
197   rval = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, 0, 1, sets );
198   CHECK_ERR(rval);
199   CHECK_EQUAL( 1, (int)sets.size() );
200   EntityHandle set = sets.front();
201 
202   int id;
203   rval = mb.tag_get_data( mat_tag, &set, 1, &id );
204   CHECK_ERR(rval);
205   CHECK_EQUAL( 99, id );
206 
207   std::vector<EntityHandle> quads, contents;
208   rval = mb.get_entities_by_type( 0, MBQUAD, quads );
209   CHECK_ERR(rval);
210   rval = mb.get_entities_by_handle( set, contents );
211   CHECK_ERR(rval);
212   std::sort( quads.begin(), quads.end() );
213   std::sort( contents.begin(), contents.end() );
214   CHECK_EQUAL( quads, contents );
215 }
216 
test_read_material_on_nodes()217 void test_read_material_on_nodes()
218 {
219   ErrorCode rval;
220   Core moab;
221   Interface& mb = moab;
222   read_file( moab, example5 );
223 
224   Tag mat_tag;
225   rval = mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mat_tag );
226   CHECK_ERR(rval);
227 
228   Range sets;
229   rval = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, 0, 1, sets );
230   CHECK_ERR(rval);
231   CHECK_EQUAL( 2, (int)sets.size() );
232 
233   EntityHandle set_101 = sets.front();
234   EntityHandle set_102 = sets.back();
235 
236   int id;
237   rval = mb.tag_get_data( mat_tag, &set_101, 1, &id );
238   CHECK_ERR(rval);
239   CHECK_EQUAL( 101, id );
240 
241   rval = mb.tag_get_data( mat_tag, &set_102, 1, &id );
242   CHECK_ERR(rval);
243   CHECK_EQUAL( 102, id );
244 }
245 
test_read_geom_set()246 void test_read_geom_set()
247 {
248   ErrorCode rval;
249   Core moab;
250   Interface& mb = moab;
251   read_file( moab, example );
252 
253   Tag dim_tag, id_tag;
254   rval = mb.tag_get_handle( GEOM_DIMENSION_TAG_NAME, 1, MB_TYPE_INTEGER, dim_tag );
255   CHECK_ERR(rval);
256   rval = mb.tag_get_handle( GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, id_tag );
257   CHECK_ERR(rval);
258 
259   Range sets;
260   rval = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &dim_tag, 0, 1, sets );
261   CHECK_ERR(rval);
262   CHECK_EQUAL( 1, (int)sets.size() );
263   EntityHandle set = sets.front();
264 
265   int dim;
266   rval = mb.tag_get_data( dim_tag, &set, 1, &dim );
267   CHECK_ERR(rval);
268   CHECK_EQUAL( 2, dim );
269 
270   int id;
271   rval = mb.tag_get_data( id_tag, &set, 1, &id );
272   CHECK_ERR(rval);
273   CHECK_EQUAL( 3, id );
274 
275   std::vector<EntityHandle> quads, contents;
276   rval = mb.get_entities_by_type( 0, MBQUAD, quads );
277   CHECK_ERR(rval);
278   rval = mb.get_entities_by_handle( set, contents );
279   CHECK_ERR(rval);
280   std::sort( quads.begin(), quads.end() );
281   std::sort( contents.begin(), contents.end() );
282   CHECK_EQUAL( quads, contents );
283 }
284 
test_read_airfoil()285 void test_read_airfoil()
286 {
287   Core moab;
288   read_file( moab, example2 );
289 }
290 
test_read_t1()291 void test_read_t1()
292 {
293   Core moab;
294   read_file( moab, example3 );
295 }
296 
test_read_fgh()297 void test_read_fgh()
298 {
299   Core moab;
300   read_file( moab, example4 ); // it should find 3 partitions
301   // read partition sets
302   Tag  ptag;
303   ErrorCode  rval = moab.tag_get_handle("PARALLEL_PARTITION", ptag ); CHECK_ERR(rval);
304   Range psets;
305   rval = moab.get_entities_by_type_and_tag(0, MBENTITYSET, &ptag, 0, 1, psets); CHECK_ERR(rval);
306   CHECK_EQUAL( 3, (int)psets.size() );
307   Range ents_first_set;
308   rval = moab.get_entities_by_handle(psets[0], ents_first_set); CHECK_ERR(rval);
309   CHECK_EQUAL( (int)ents_first_set.size(), 98 );
310 }
311