1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "TestUtil.hpp"
#include "moab/Core.hpp"
#include "MBTagConventions.hpp"
#define IS_BUILDING_MB
#include "moab/Range.hpp"
#include <cmath>
#include <algorithm>

using namespace moab;

#ifdef MESHDIR
static const char example[] = STRINGIFY( MESHDIR ) "/io/test.nas";<--- Skipping configuration 'MESHDIR' since the value of 'MESHDIR' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#else
static const char example[] = "test.nas";
#endif

void read_file( Interface& moab, const char* input_file );
void test_read_nodes();
void test_read_tets();
void test_read_prisms();
void test_read_hexes();
void test_read_material_set1();
void test_read_material_set2();

int main()
{
    int result = 0;

    result += RUN_TEST( test_read_nodes );
    result += RUN_TEST( test_read_tets );
    result += RUN_TEST( test_read_prisms );
    result += RUN_TEST( test_read_hexes );
    result += RUN_TEST( test_read_material_set1 );
    result += RUN_TEST( test_read_material_set2 );

    return result;
}

void read_file( Interface& moab, const char* input_file )
{
    ErrorCode rval = moab.load_file( input_file );CHECK_ERR( rval );
}

void test_read_nodes()
{
    const double eps = 1e-100;
    ErrorCode rval;
    Core moab;
    Interface& mb = moab;
    read_file( moab, example );

    std::vector< EntityHandle > nodes;
    rval = mb.get_entities_by_type( 0, MBVERTEX, nodes );CHECK_ERR( rval );
    CHECK_EQUAL( (size_t)19, nodes.size() );

    Tag id_tag = mb.globalId_tag();

    std::vector< int > ids( nodes.size() );
    rval = mb.tag_get_data( id_tag, &nodes[0], nodes.size(), &ids[0] );CHECK_ERR( rval );

    std::vector< int > sorted_ids( ids );
    std::sort( sorted_ids.begin(), sorted_ids.end() );

    std::vector< double > coords( 3 * nodes.size() );
    rval = mb.get_coords( &nodes[0], nodes.size(), &coords[0] );CHECK_ERR( rval );

    int idx, pos = 0;
    // shared between 2 tets and 2 prisms
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], -2.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], -1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], -1.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], -1.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], -1.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], -1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], -1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 1.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 1.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 1.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 2.0, eps );
    // hex element
    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 5.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 5.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 5.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 10.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 5.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 5.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 10.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 10.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 5.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 5.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 10.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 5.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 5.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 5.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 10.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 10.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 5.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 10.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 10.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 10.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 10.0, eps );

    ++pos;
    CHECK_EQUAL( pos + 1, sorted_ids[pos] );
    idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
    CHECK_REAL_EQUAL( coords[3 * idx + 0], 5.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 1], 10.0, eps );
    CHECK_REAL_EQUAL( coords[3 * idx + 2], 10.0, eps );
}

void test_read_tets()
{
    ErrorCode rval;
    Core moab;
    Interface& mb = moab;
    read_file( moab, example );

    std::vector< EntityHandle > tets;
    rval = mb.get_entities_by_type( 0, MBTET, tets );CHECK_ERR( rval );
    CHECK_EQUAL( (size_t)2, tets.size() );

    Tag id_tag = mb.globalId_tag();

    std::vector< int > ids( tets.size() );
    rval = mb.tag_get_data( id_tag, &tets[0], tets.size(), &ids[0] );CHECK_ERR( rval );

    if( ids[0] != 1 )
    {
        std::swap( ids[0], ids[1] );
        std::swap( tets[0], tets[1] );
    }

    int vtx_ids[4];
    const EntityHandle* conn;
    int len;

    const int conn1[] = { 8, 9, 10, 11 };
    int pos           = 0;
    CHECK_EQUAL( pos + 1, ids[pos] );
    rval = mb.get_connectivity( tets[pos], conn, len );CHECK_ERR( rval );
    CHECK_EQUAL( 4, len );
    rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
    CHECK_ARRAYS_EQUAL( conn1, 4, vtx_ids, len );

    const int conn2[] = { 4, 3, 2, 1 };
    ++pos;
    CHECK_EQUAL( pos + 1, ids[pos] );
    rval = mb.get_connectivity( tets[pos], conn, len );CHECK_ERR( rval );
    CHECK_EQUAL( 4, len );
    rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
    CHECK_ARRAYS_EQUAL( conn2, 4, vtx_ids, len );
}

void test_read_prisms()
{
    ErrorCode rval;
    Core moab;
    Interface& mb = moab;
    read_file( moab, example );

    std::vector< EntityHandle > prisms;
    rval = mb.get_entities_by_type( 0, MBPRISM, prisms );CHECK_ERR( rval );
    CHECK_EQUAL( (size_t)2, prisms.size() );

    Tag id_tag = mb.globalId_tag();

    std::vector< int > ids( prisms.size() );
    rval = mb.tag_get_data( id_tag, &prisms[0], prisms.size(), &ids[0] );CHECK_ERR( rval );

    if( ids[0] != 3 )
    {
        std::swap( ids[0], ids[1] );
        std::swap( prisms[0], prisms[1] );
    }

    int vtx_ids[6];
    const EntityHandle* conn;
    int len;

    const int conn1[] = { 2, 3, 4, 5, 6, 7 };
    int pos           = 0;
    // Element ids 1 and 2 are the two tet elements.
    // Element ids 3 and 4 are the two prism elements.
    CHECK_EQUAL( pos + 3, ids[pos] );
    rval = mb.get_connectivity( prisms[pos], conn, len );CHECK_ERR( rval );
    CHECK_EQUAL( 6, len );
    rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
    CHECK_ARRAYS_EQUAL( conn1, 6, vtx_ids, len );

    const int conn2[] = { 5, 6, 7, 8, 9, 10 };
    ++pos;
    CHECK_EQUAL( pos + 3, ids[pos] );
    rval = mb.get_connectivity( prisms[pos], conn, len );CHECK_ERR( rval );
    CHECK_EQUAL( 6, len );
    rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
    CHECK_ARRAYS_EQUAL( conn2, 6, vtx_ids, len );
}

void test_read_hexes()
{
    ErrorCode rval;
    Core moab;
    Interface& mb = moab;
    read_file( moab, example );

    std::vector< EntityHandle > hexes;
    rval = mb.get_entities_by_type( 0, MBHEX, hexes );CHECK_ERR( rval );
    CHECK_EQUAL( (size_t)1, hexes.size() );

    Tag id_tag = mb.globalId_tag();

    std::vector< int > ids( hexes.size() );
    rval = mb.tag_get_data( id_tag, &hexes[0], hexes.size(), &ids[0] );CHECK_ERR( rval );

    int vtx_ids[8];
    const EntityHandle* conn;
    int len;

    const int conn1[] = { 12, 13, 14, 15, 16, 17, 18, 19 };
    int pos           = 0;
    // Element id 5 is the hex
    CHECK_EQUAL( pos + 5, ids[pos] );
    rval = mb.get_connectivity( hexes[pos], conn, len );CHECK_ERR( rval );
    CHECK_EQUAL( 8, len );
    rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
    CHECK_ARRAYS_EQUAL( conn1, 8, vtx_ids, len );
}

// The tets are in material set 1.
void test_read_material_set1()
{
    ErrorCode rval;
    Core moab;
    Interface& mb = moab;
    read_file( moab, example );

    Tag mat_tag;
    rval = mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mat_tag );CHECK_ERR( rval );

    Range mat_set_one;
    const int one               = 1;
    const void* const one_val[] = { &one };
    rval                        = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, one_val, 1, mat_set_one );CHECK_ERR( rval );
    CHECK_EQUAL( 1, (int)mat_set_one.size() );

    std::vector< EntityHandle > tets, contents;
    rval = mb.get_entities_by_type( 0, MBTET, tets );CHECK_ERR( rval );
    rval = mb.get_entities_by_handle( mat_set_one.front(), contents );CHECK_ERR( rval );
    std::sort( tets.begin(), tets.end() );
    std::sort( contents.begin(), contents.end() );
    CHECK_EQUAL( tets, contents );
}

// The prisms are in material set 2.
void test_read_material_set2()
{
    ErrorCode rval;
    Core moab;
    Interface& mb = moab;
    read_file( moab, example );

    Tag mat_tag;
    rval = mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mat_tag );CHECK_ERR( rval );

    Range mat_set_two;
    const int two               = 2;
    const void* const two_val[] = { &two };
    rval                        = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, two_val, 1, mat_set_two );CHECK_ERR( rval );
    CHECK_EQUAL( 1, (int)mat_set_two.size() );

    std::vector< EntityHandle > prisms, contents;
    rval = mb.get_entities_by_type( 0, MBPRISM, prisms );CHECK_ERR( rval );
    rval = mb.get_entities_by_handle( mat_set_two.front(), contents );CHECK_ERR( rval );
    std::sort( prisms.begin(), prisms.end() );
    std::sort( contents.begin(), contents.end() );
    CHECK_EQUAL( prisms, contents );
}