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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "TestUtil.hpp"
#include "moab/Core.hpp"
#include "moab/ReadUtilIface.hpp"
#include "TagInfo.hpp"
#include "MBTagConventions.hpp"

using namespace moab;

std::string example    = TestDir + "unittest/io/homme3x3458.t.3.nc";
std::string conn_fname = TestDir + "unittest/io/HommeMapping.nc";

#ifdef MOAB_HAVE_MPI
#include "moab_mpi.h"
#include "moab/ParallelComm.hpp"
#endif

void test_read_all();
void test_read_onevar();
void test_read_onetimestep();
void test_read_nomesh();
void test_read_novars();
void test_read_coord_vars();  // Test reading coordinate variables
void test_gather_onevar();    // Test gather set with one variable
void test_read_conn();        // Test reading connectivity file only

void get_options( std::string& opts );

const int levels = 3;

int main( int argc, char* argv[] )
{
    int result = 0;

#ifdef MOAB_HAVE_MPI
    int fail = MPI_Init( &argc, &argv );
    if( fail ) return 1;
#else
    argv[0]   = argv[argc - argc];  // To remove the warnings in serial mode about unused variables
#endif

    result += RUN_TEST( test_read_all );
    result += RUN_TEST( test_read_onevar );
    result += RUN_TEST( test_read_onetimestep );
    result += RUN_TEST( test_read_nomesh );
    result += RUN_TEST( test_read_novars );
    result += RUN_TEST( test_read_coord_vars );
    result += RUN_TEST( test_gather_onevar );
    result += RUN_TEST( test_read_conn );

#ifdef MOAB_HAVE_MPI
    fail = MPI_Finalize();
    if( fail ) return 1;
#endif

    return result;
}

void test_read_all()
{
    Core moab;
    Interface& mb = moab;

    std::string opts;
    get_options( opts );

    ErrorCode rval = mb.load_file( example.c_str(), NULL, opts.c_str() );CHECK_ERR( rval );

    // Check for proper tags
    Tag Ttag0, Ttag1;
    rval = mb.tag_get_handle( "T0", levels, MB_TYPE_DOUBLE, Ttag0 );CHECK_ERR( rval );

    rval = mb.tag_get_handle( "T1", levels, MB_TYPE_DOUBLE, Ttag1 );CHECK_ERR( rval );
}

void test_read_onevar()
{
    Core moab;
    Interface& mb = moab;

    std::string opts;
    get_options( opts );

    // Read mesh and read vertex variable T at all timesteps
    opts += std::string( ";VARIABLE=T" );
    ErrorCode rval = mb.load_file( example.c_str(), NULL, opts.c_str() );CHECK_ERR( rval );

#ifdef MOAB_HAVE_MPI
    ParallelComm* pcomm = ParallelComm::get_pcomm( &mb, 0 );
    int procs           = pcomm->proc_config().proc_size();
#else
    int procs = 1;<--- 'procs' is assigned value '1' here.
#endif

    // Make check runs this test on one processor
    if( 1 == procs )
    {
        // Check for proper tags
        Tag Ttag0, Ttag1;
        rval = mb.tag_get_handle( "T0", levels, MB_TYPE_DOUBLE, Ttag0 );CHECK_ERR( rval );
        rval = mb.tag_get_handle( "T1", levels, MB_TYPE_DOUBLE, Ttag1 );CHECK_ERR( rval );

        // Get vertices
        Range verts;
        rval = mb.get_entities_by_type( 0, MBVERTEX, verts );CHECK_ERR( rval );
        CHECK_EQUAL( (size_t)3458, verts.size() );

        // Get all values of tag T0
        int count;
        void* Tbuf;
        rval = mb.tag_iterate( Ttag0, verts.begin(), verts.end(), count, Tbuf );CHECK_ERR( rval );
        CHECK_EQUAL( (size_t)count, verts.size() );

        const double eps = 0.0001;
        double* data     = (double*)Tbuf;

        // Check first level values on 4 strategically selected vertices
        CHECK_REAL_EQUAL( 233.1136, data[0 * levels], eps );     // First vert
        CHECK_REAL_EQUAL( 236.1505, data[1728 * levels], eps );  // Median vert
        CHECK_REAL_EQUAL( 235.7722, data[1729 * levels], eps );  // Median vert
        CHECK_REAL_EQUAL( 234.0416, data[3457 * levels], eps );  // Last vert
    }
}

void test_read_onetimestep()
{
    Core moab;
    Interface& mb = moab;

    std::string opts;
    get_options( opts );

    opts += std::string( ";TIMESTEP=1" );
    ErrorCode rval = mb.load_file( example.c_str(), NULL, opts.c_str() );CHECK_ERR( rval );

    // Check for proper tags
    Tag Ttag0, Ttag1;
    rval = mb.tag_get_handle( "T0", levels, MB_TYPE_DOUBLE, Ttag0 );
    CHECK_EQUAL( rval, MB_TAG_NOT_FOUND );

    rval = mb.tag_get_handle( "T1", levels, MB_TYPE_DOUBLE, Ttag1 );CHECK_ERR( rval );
}

void test_read_nomesh()
{
    Core moab;
    Interface& mb = moab;

    // Need a set for nomesh to work right
    EntityHandle file_set;
    ErrorCode rval = mb.create_meshset( MESHSET_SET, file_set );CHECK_ERR( rval );

    std::string orig, opts;
    get_options( orig );

    opts = orig + std::string( ";TIMESTEP=0" );
    rval = mb.load_file( example.c_str(), &file_set, opts.c_str() );CHECK_ERR( rval );

    // Check for proper tag
    Tag Ttag0, Ttag1;
    rval = mb.tag_get_handle( "T0", levels, MB_TYPE_DOUBLE, Ttag0 );CHECK_ERR( rval );

    rval = mb.tag_get_handle( "T1", levels, MB_TYPE_DOUBLE, Ttag1 );
    CHECK_EQUAL( rval, MB_TAG_NOT_FOUND );

    // Now read 2nd timestep with nomesh option
    opts = orig + std::string( ";TIMESTEP=1;NOMESH" );
    rval = mb.load_file( example.c_str(), &file_set, opts.c_str() );CHECK_ERR( rval );

    // Check for proper tag
    rval = mb.tag_get_handle( "T1", levels, MB_TYPE_DOUBLE, Ttag1 );CHECK_ERR( rval );
}

void test_read_novars()
{
    Core moab;
    Interface& mb = moab;

    // Need a set for nomesh to work right
    EntityHandle set;
    ErrorCode rval = mb.create_meshset( MESHSET_SET, set );CHECK_ERR( rval );

    std::string orig, opts;
    get_options( orig );

    opts = orig + std::string( ";NOMESH;VARIABLE=" );
    rval = mb.load_file( example.c_str(), &set, opts.c_str() );CHECK_ERR( rval );

    opts = orig + std::string( ";VARIABLE=;TIMESTEP=0" );
    rval = mb.load_file( example.c_str(), &set, opts.c_str() );CHECK_ERR( rval );

    // Check for proper tag
    Tag Ttag0, Ttag1;
    rval = mb.tag_get_handle( "T0", levels, MB_TYPE_DOUBLE, Ttag0 );
    CHECK_EQUAL( rval, MB_TAG_NOT_FOUND );

    opts = orig + std::string( ";VARIABLE=T;TIMESTEP=0;NOMESH" );
    rval = mb.load_file( example.c_str(), &set, opts.c_str() );CHECK_ERR( rval );

    rval = mb.tag_get_handle( "T0", levels, MB_TYPE_DOUBLE, Ttag0 );CHECK_ERR( rval );

    rval = mb.tag_get_handle( "T1", levels, MB_TYPE_DOUBLE, Ttag1 );
    CHECK_EQUAL( rval, MB_TAG_NOT_FOUND );

    // Now read 2nd timestep with nomesh option
    opts = orig + std::string( ";TIMESTEP=1;NOMESH" );
    rval = mb.load_file( example.c_str(), &set, opts.c_str() );CHECK_ERR( rval );

    // Check for proper tag
    rval = mb.tag_get_handle( "T1", levels, MB_TYPE_DOUBLE, Ttag1 );CHECK_ERR( rval );
}

void test_read_coord_vars()
{
    Core moab;
    Interface& mb = moab;

    EntityHandle file_set;
    ErrorCode rval = mb.create_meshset( MESHSET_SET, file_set );CHECK_ERR( rval );

    std::string orig, opts;
    get_options( orig );

    opts = orig + std::string( ";NOMESH;VARIABLE=" );
    rval = mb.load_file( example.c_str(), &file_set, opts.c_str() );CHECK_ERR( rval );

    std::string tag_name;
    int var_len;
    Tag var_tag;
    const void* var_data;

    // Check tag for regular coordinate variable lev
    tag_name = "lev";
    var_len  = 0;
    rval     = mb.tag_get_handle( tag_name.c_str(), var_len, MB_TYPE_OPAQUE, var_tag, MB_TAG_SPARSE | MB_TAG_VARLEN );CHECK_ERR( rval );
    CHECK_EQUAL( true, var_tag->variable_length() );
    CHECK_EQUAL( MB_TYPE_DOUBLE, var_tag->get_data_type() );

    // Check lev tag size and values on file_set
    rval = mb.tag_get_by_ptr( var_tag, &file_set, 1, &var_data, &var_len );CHECK_ERR( rval );
    CHECK_EQUAL( levels, var_len );
    double* lev_val  = (double*)var_data;
    const double eps = 1e-10;
    CHECK_REAL_EQUAL( 3.54463800000002, lev_val[0], eps );
    CHECK_REAL_EQUAL( 13.9672100000001, lev_val[levels - 1], eps );

    // Check tag for dummy coordinate variable ncol
    tag_name = "ncol";
    var_len  = 0;
    rval     = mb.tag_get_handle( tag_name.c_str(), var_len, MB_TYPE_OPAQUE, var_tag, MB_TAG_SPARSE | MB_TAG_VARLEN );CHECK_ERR( rval );
    CHECK_EQUAL( true, var_tag->variable_length() );
    CHECK_EQUAL( MB_TYPE_INTEGER, var_tag->get_data_type() );

    // Check ncol tag size and values on file_set
    rval = mb.tag_get_by_ptr( var_tag, &file_set, 1, &var_data, &var_len );CHECK_ERR( rval );
    CHECK_EQUAL( 1, var_len );
    int* ncol_val = (int*)var_data;
    CHECK_EQUAL( 3458, ncol_val[0] );

    // Create another file set
    EntityHandle file_set2;
    rval = mb.create_meshset( MESHSET_SET, file_set2 );CHECK_ERR( rval );

    // Read file again with file_set2
    rval = mb.load_file( example.c_str(), &file_set2, opts.c_str() );CHECK_ERR( rval );

    // Check tag for regular coordinate lev
    tag_name = "lev";
    var_len  = 0;
    rval     = mb.tag_get_handle( tag_name.c_str(), var_len, MB_TYPE_OPAQUE, var_tag, MB_TAG_SPARSE | MB_TAG_VARLEN );CHECK_ERR( rval );
    CHECK_EQUAL( true, var_tag->variable_length() );
    CHECK_EQUAL( MB_TYPE_DOUBLE, var_tag->get_data_type() );

    // Check lev tag size and values on file_set2
    rval = mb.tag_get_by_ptr( var_tag, &file_set2, 1, &var_data, &var_len );CHECK_ERR( rval );
    CHECK_EQUAL( levels, var_len );
    lev_val = (double*)var_data;
    CHECK_REAL_EQUAL( 3.54463800000002, lev_val[0], eps );
    CHECK_REAL_EQUAL( 13.9672100000001, lev_val[levels - 1], eps );

    // Check tag for dummy coordinate variable ncol
    tag_name = "ncol";
    var_len  = 0;
    rval     = mb.tag_get_handle( tag_name.c_str(), var_len, MB_TYPE_OPAQUE, var_tag, MB_TAG_SPARSE | MB_TAG_VARLEN );CHECK_ERR( rval );
    CHECK_EQUAL( true, var_tag->variable_length() );
    CHECK_EQUAL( MB_TYPE_INTEGER, var_tag->get_data_type() );

    // Check ncol tag size and values on file_set2
    rval = mb.tag_get_by_ptr( var_tag, &file_set2, 1, &var_data, &var_len );CHECK_ERR( rval );
    CHECK_EQUAL( 1, var_len );
    ncol_val = (int*)var_data;
    CHECK_EQUAL( 3458, ncol_val[0] );
}

void test_gather_onevar()
{
    Core moab;
    Interface& mb = moab;

    EntityHandle file_set;
    ErrorCode rval = mb.create_meshset( MESHSET_SET, file_set );CHECK_ERR( rval );

    std::string opts;
    get_options( opts );

    // Read vertex variable T and create gather set on processor 0
    opts += ";VARIABLE=T;GATHER_SET=0";
#ifdef MOAB_HAVE_MPI
    opts += ";PARALLEL_RESOLVE_SHARED_ENTS";
#endif
    rval = mb.load_file( example.c_str(), &file_set, opts.c_str() );CHECK_ERR( rval );

#ifdef MOAB_HAVE_MPI
    ParallelComm* pcomm = ParallelComm::get_pcomm( &mb, 0 );
    int rank            = pcomm->proc_config().proc_rank();

    Range verts, verts_owned;
    rval = mb.get_entities_by_type( file_set, MBVERTEX, verts );CHECK_ERR( rval );

    // Get local owned vertices
    rval = pcomm->filter_pstatus( verts, PSTATUS_NOT_OWNED, PSTATUS_NOT, -1, &verts_owned );CHECK_ERR( rval );

    EntityHandle gather_set = 0;
    if( 0 == rank )
    {
        // Get gather set
        ReadUtilIface* readUtilIface;
        mb.query_interface( readUtilIface );
        rval = readUtilIface->get_gather_set( gather_set );CHECK_ERR( rval );
        assert( gather_set != 0 );
    }

    Tag Ttag0, gid_tag;
    rval = mb.tag_get_handle( "T0", levels, MB_TYPE_DOUBLE, Ttag0, MB_TAG_DENSE );CHECK_ERR( rval );

    gid_tag = mb.globalId_tag();

    pcomm->gather_data( verts_owned, Ttag0, gid_tag, gather_set, 0 );

    if( 0 == rank )
    {
        // Get gather set vertices
        Range gather_set_verts;
        rval = mb.get_entities_by_type( gather_set, MBVERTEX, gather_set_verts );CHECK_ERR( rval );
        CHECK_EQUAL( (size_t)3458, gather_set_verts.size() );

        // Get T0 tag values on 4 strategically selected gather set vertices
        double T0_val[4 * levels];
        EntityHandle vert_ents[] = { gather_set_verts[0], gather_set_verts[1728], gather_set_verts[1729],
                                     gather_set_verts[3457] };
        rval                     = mb.tag_get_data( Ttag0, vert_ents, 4, T0_val );CHECK_ERR( rval );

        const double eps = 0.001;

        // Check first level values
        CHECK_REAL_EQUAL( 233.1136, T0_val[0 * levels], eps );  // First vert
        CHECK_REAL_EQUAL( 236.1505, T0_val[1 * levels], eps );  // Median vert
        CHECK_REAL_EQUAL( 235.7722, T0_val[2 * levels], eps );  // Median vert
        CHECK_REAL_EQUAL( 234.0416, T0_val[3 * levels], eps );  // Last vert
    }
#endif
}

void test_read_conn()
{
    Core moab;
    Interface& mb = moab;

    std::string opts;
    get_options( opts );

    ErrorCode rval = mb.load_file( conn_fname.c_str(), NULL, opts.c_str() );CHECK_ERR( rval );

#ifdef MOAB_HAVE_MPI
    ParallelComm* pcomm = ParallelComm::get_pcomm( &mb, 0 );
    int procs           = pcomm->proc_config().proc_size();
#else
    int procs = 1;<--- 'procs' is assigned value '1' here.
#endif

    // Make check runs this test on one processor
    if( 1 == procs )
    {
        // Get vertices
        Range verts;
        rval = mb.get_entities_by_type( 0, MBVERTEX, verts );CHECK_ERR( rval );
        CHECK_EQUAL( (size_t)3458, verts.size() );

        // Get cells
        Range cells;
        rval = mb.get_entities_by_type( 0, MBQUAD, cells );CHECK_ERR( rval );
        CHECK_EQUAL( (size_t)3456, cells.size() );
    }
}

void get_options( std::string& opts )
{
#ifdef MOAB_HAVE_MPI
    // Use parallel options
    opts = std::string( ";;PARALLEL=READ_PART;PARTITION_METHOD=TRIVIAL" );
#else
    opts      = std::string( ";;" );
#endif
}