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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <fstream>
#if defined( __MINGW32__ )
#include <sys/time.h>
#else
#include <ctime>
#endif

#include <ctime>
#include <cmath>
#include <cassert>
#include <cfloat>

#include "moab/Core.hpp"
#include "moab/Interface.hpp"
#include "moab/verdict/VerdictWrapper.hpp"
#include "moab/NestedRefine.hpp"

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

/* Exit values */
#define SUCCESS         0
#define USAGE_ERROR     1
#define NOT_IMPLEMENTED 2

using namespace moab;

static void print_usage( const char* name, std::ostream& stream )
{
    stream << "Usage: " << name << " <options> <input_file> [<input_file2> ...]" << std::endl
           << "Options: " << std::endl
           << "\t-h             - Print this help text and exit." << std::endl
           << "\t-d             - Dimension of the mesh." << std::endl
           << "\t-n             - Exact or a maximum number of levels for the hierarchy. Default 1." << std::endl
           << "\t-L             - Degree of refinement for each level. Pass an array or a number. It "
              "is mandatory to pass dimension and num_levels before to use this option. If this flag "
              "is not used, a default of deg 2 refinement is used. "
           << std::endl
           << "\t-V             - Pass a desired volume (absolute) . This will generate a hierarchy "
              "such that the maximum volume is reduced to the given amount approximately. The length "
              "of the hierarchy can be constrained further if a maximum number of levels is passed. "
              "It is mandatory to pass the dimension for this option. "
           << std::endl
           << "\t-q             - Prints out the maximum volume of the mesh and exits the program. "
              "This option can be used as a guide to volume constrainted mesh hierarchy later. "
           << std::endl
           << "\t-t             - Print out the time taken to generate hierarchy." << std::endl
           << "\t-s             - Print out the mesh sizes of each level of the generated hierarchy." << std::endl
           << "\t-o             - Specify true for output files for the mesh levels of the hierarchy." << std::endl
    //<< "\t-O option      - Specify read option." << std::endl
#ifdef MOAB_HAVE_MPI
           << "\t-p[0|1|2]      - Read in parallel[0], optionally also doing resolve_shared_ents (1) "
              "and exchange_ghosts (2)"
           << std::endl
#endif
        ;
    exit( USAGE_ERROR );
}

static void usage_error( const char* name )
{
    print_usage( name, std::cerr );
#ifdef MOAB_HAVE_MPI
    MPI_Finalize();
#endif
    exit( USAGE_ERROR );
}

bool parse_id_list( const char* string, int dim, int nval, std::vector< int >& results );

bool make_opts_string( std::vector< std::string > options, std::string& opts );

ErrorCode get_degree_seq( Core& mb,
                          EntityHandle fileset,
                          int dim,
                          double desired_vol,
                          int& num_levels,
                          std::vector< int >& level_degs );

ErrorCode get_max_volume( Core& mb, EntityHandle fileset, int dim, double& vmax );

int main( int argc, char* argv[] )
{
    int proc_id = 0, size = 1;<--- 'proc_id' is assigned value '0' here.<--- 'size' is assigned value '1' here.
#ifdef MOAB_HAVE_MPI
    MPI_Init( &argc, &argv );
    MPI_Comm_rank( MPI_COMM_WORLD, &proc_id );
    MPI_Comm_size( MPI_COMM_WORLD, &size );
#endif

    int num_levels = 0, dim = 0;
    std::vector< int > level_degrees;
    bool optimize    = false;
    bool do_flag     = true;
    bool print_times = false, print_size = false, output = false;
    bool parallel = false, resolve_shared = false, exchange_ghosts = false;<--- 'resolve_shared' is assigned value 'false' here.<--- 'parallel' is assigned value 'false' here.<--- 'exchange_ghosts' is assigned value 'false' here.<--- 'parallel' is assigned value 'false' here.
    bool printusage = false, parselevels = true;
    bool qc_vol = false, only_quality = false;
    double cvol = 0;
    std::string infile;

    int i = 1;
    while( i < argc )
    {
        if( !argv[i][0] && 0 == proc_id )
        {
            usage_error( argv[0] );
#ifdef MOAB_HAVE_MPI
            MPI_Finalize();
#endif
        }

        if( do_flag && argv[i][0] == '-' )
        {
            switch( argv[i][1] )
            {
                    // do flag arguments:
                case '-':
                    do_flag = false;
                    break;
                case 't':
                    print_times = true;
                    break;
                case 's':
                    print_size = true;
                    break;
                case 'h':
                case 'H':
                    print_usage( argv[0], std::cerr );
                    printusage = true;
                    break;
                case 'd':
                    dim = atol( argv[i + 1] );
                    ++i;
                    break;
                case 'n':
                    num_levels = atol( argv[i + 1] );
                    ++i;
                    break;
                case 'L':
                    if( dim != 0 && num_levels != 0 )
                    {
                        parselevels = parse_id_list( argv[i + 1], dim, num_levels, level_degrees );
                        ++i;
                    }
                    else
                    {
                        print_usage( argv[0], std::cerr );
                        printusage = true;
                    }
                    break;
                case 'V':
                    qc_vol = true;
                    cvol   = strtod( argv[i + 1], NULL );
                    ++i;
                    break;
                case 'q':
                    only_quality = true;
                    break;
                case 'o':
                    output = true;
                    break;
                case 'O':
                    optimize = true;
                    break;
#ifdef MOAB_HAVE_MPI
                case 'p':
                    parallel       = true;
                    resolve_shared = true;
                    if( argv[i][1] == '1' ) exchange_ghosts = true;
                    break;
#endif
                default:
                    ++i;
                    switch( argv[i - 1][1] )
                    {
                            // case 'O':  read_opts.push_back(argv[i]); break;
                        default:
                            std::cerr << "Invalid option: " << argv[i] << std::endl;
                    }
            }
            ++i;
        }
        // do file names
        else
        {
            infile = argv[i];
            ++i;
        }
    }

    if( infile.empty() && !printusage ) print_usage( argv[0], std::cerr );

    if( !parselevels ) exit( USAGE_ERROR );

#ifdef MOAB_HAVE_MPI
    parallel       = true;<--- Assignment 'parallel=true', assigned value is 1
    resolve_shared = true;<--- Assignment 'resolve_shared=true', assigned value is 1
#endif

    ErrorCode error;

    Core* moab    = new Core;
    Interface* mb = (Interface*)moab;
    EntityHandle fileset;

    // Create a fileset
    error = mb->create_meshset( MESHSET_SET, fileset );MB_CHK_ERR( error );

    // Set the read options for parallel file loading
    std::vector< std::string > read_opts, write_opts;
    std::string read_options, write_options;

    if( parallel && size > 1 )<--- outer condition: parallel<--- outer condition: parallel<--- Condition 'parallel' is always true
    {
        read_opts.push_back( "PARALLEL=READ_PART" );
        read_opts.push_back( "PARTITION=PARALLEL_PARTITION" );
        if( resolve_shared ) read_opts.push_back( "PARALLEL_RESOLVE_SHARED_ENTS" );<--- identical inner condition: resolve_shared<--- Condition 'resolve_shared' is always true
        if( exchange_ghosts ) read_opts.push_back( "PARALLEL_GHOSTS=3.0.1" );<--- identical inner condition: exchange_ghosts

        /*  if (output)
            {
              write_opts.push_back(";;PARALLEL=WRITE_PART");
              std::cout<<"Here"<<std::endl;
            }*/
    }

    if( !make_opts_string( read_opts, read_options ) || !make_opts_string( write_opts, write_options ) )
    {
#ifdef MOAB_HAVE_MPI
        MPI_Finalize();
#endif
        return USAGE_ERROR;
    }

    // Load file
    std::cout << "READ OPTS=" << (char*)read_options.c_str() << std::endl;
    error = mb->load_file( infile.c_str(), &fileset, read_options.c_str() );MB_CHK_ERR( error );

    // Create the nestedrefine instance

#ifdef MOAB_HAVE_MPI
    ParallelComm* pc   = new ParallelComm( mb, MPI_COMM_WORLD );
    NestedRefine* uref = new NestedRefine( moab, pc, fileset );
#else
    NestedRefine* uref = new NestedRefine( moab );
#endif

    std::vector< EntityHandle > lsets;

    // std::cout<<"Read input file"<<std::endl;

    if( only_quality )
    {
        double vmax;
        error = get_max_volume( *moab, fileset, dim, vmax );MB_CHK_ERR( error );
#ifdef MOAB_HAVE_MPI
        int rank = 0;
        MPI_Comm_rank( MPI_COMM_WORLD, &rank );
        if( rank == 0 ) std::cout << "Maximum global volume = " << vmax << std::endl;
#else
        std::cout << "Maximum volume = " << vmax << std::endl;
#endif
        exit( SUCCESS );
    }

    // If a volume constraint is given, find an optimal degree sequence to reach the desired volume
    // constraint.
    if( qc_vol )
    {
        error = get_degree_seq( *moab, fileset, dim, cvol, num_levels, level_degrees );MB_CHK_ERR( error );

        if( dim == 0 ) print_usage( argv[0], std::cerr );
    }

    if( num_levels == 0 ) num_levels = 1;

    int* ldeg = new int[num_levels];
    // std::cout<<"level_degrees.size = "<<level_degrees.size()<<std::endl;
    if( level_degrees.empty() )
    {
        for( int l = 0; l < num_levels; l++ )
            ldeg[l] = 2;
    }
    else
    {
        for( int l = 0; l < num_levels; l++ )
            ldeg[l] = level_degrees[l];
    }

    std::cout << "Starting hierarchy generation" << std::endl;

    std::cout << "opt = " << optimize << std::endl;

    error = uref->generate_mesh_hierarchy( num_levels, ldeg, lsets, optimize );MB_CHK_ERR( error );

    if( print_times )
    {
        std::cout << "Finished hierarchy generation in " << uref->timeall.tm_total << "  secs" << std::endl;
        if( parallel )
        {
            std::cout << "Time taken for refinement " << uref->timeall.tm_refine << "  secs" << std::endl;
            std::cout << "Time taken for resolving shared interface " << uref->timeall.tm_resolve << "  secs"
                      << std::endl;
        }
    }
    else
        std::cout << "Finished hierarchy generation " << std::endl;

    if( print_size )
    {
        Range all_ents, ents[4];
        error = mb->get_entities_by_handle( fileset, all_ents );MB_CHK_ERR( error );

        for( int k = 0; k < 4; k++ )
            ents[k] = all_ents.subset_by_dimension( k );

        std::cout << std::endl;
        if( qc_vol )
        {
            double volume;
            error = get_max_volume( *moab, fileset, dim, volume );MB_CHK_ERR( error );
            std::cout << "Mesh size for level 0"
                      << "  :: nverts = " << ents[0].size() << ", nedges = " << ents[1].size()
                      << ", nfaces = " << ents[2].size() << ", ncells = " << ents[3].size() << " :: Vmax = " << volume
                      << std::endl;
        }
        else
            std::cout << "Mesh size for level 0"
                      << "  :: nverts = " << ents[0].size() << ", nedges = " << ents[1].size()
                      << ", nfaces = " << ents[2].size() << ", ncells = " << ents[3].size() << std::endl;

        for( int l = 0; l < num_levels; l++ )
        {
            all_ents.clear();
            ents[0].clear();
            ents[1].clear();
            ents[2].clear();
            ents[3].clear();
            error = mb->get_entities_by_handle( lsets[l + 1], all_ents );MB_CHK_ERR( error );

            for( int k = 0; k < 4; k++ )
                ents[k] = all_ents.subset_by_dimension( k );

            // std::cout<<std::endl;

            if( qc_vol )
            {
                double volume;
                error = get_max_volume( *moab, lsets[l + 1], dim, volume );MB_CHK_ERR( error );
                std::cout << "Mesh size for level " << l + 1 << "  :: nverts = " << ents[0].size()
                          << ", nedges = " << ents[1].size() << ", nfaces = " << ents[2].size()
                          << ", ncells = " << ents[3].size() << " :: Vmax = " << volume << std::endl;
            }
            else
                std::cout << "Mesh size for level " << l + 1 << "  :: nverts = " << ents[0].size()
                          << ", nedges = " << ents[1].size() << ", nfaces = " << ents[2].size()
                          << ", ncells = " << ents[3].size() << std::endl;
        }
    }

    if( output )
    {
        for( int l = 0; l < num_levels; l++ )
        {
            std::string::size_type idx1 = infile.find_last_of( "\\/" );
            std::string::size_type idx2 = infile.find_last_of( "." );
            std::string file            = infile.substr( idx1 + 1, idx2 - idx1 - 1 );
            std::stringstream out;
            if( parallel )
                out << "_ML_" << l + 1 << ".h5m";
            else
                out << "_ML_" << l + 1 << ".vtk";
            file                    = file + out.str();
            const char* output_file = file.c_str();
#ifdef MOAB_HAVE_MPI
            error = mb->write_file( output_file, 0, ";;PARALLEL=WRITE_PART", &lsets[l + 1], 1 );MB_CHK_ERR( error );
#else
            error = mb->write_file( output_file, 0, NULL, &lsets[l + 1], 1 );MB_CHK_ERR( error );
#endif
            //   const char* output_file = file.c_str();
            //   error =  mb->write_file(output_file, 0, write_options.c_str(), &lsets[l+1],
            //   1);MB_CHK_ERR(error);
            //    mb->list_entity(lsets[l+1]);
            //   mb->write_file(output_file, 0, "PARALLEL=WRITE_PART", &lsets[l+1], 1);
        }
    }

    delete uref;
#ifdef MOAB_HAVE_MPI
    delete pc;
#endif

    delete[] ldeg;
    delete moab;

#ifdef MOAB_HAVE_MPI
    MPI_Finalize();
#endif

    exit( SUCCESS );
}

ErrorCode get_degree_seq( Core& mb,
                          EntityHandle fileset,
                          int dim,
                          double desired_vol,
                          int& num_levels,
                          std::vector< int >& level_degs )
{
    // Find max volume
    double vmax_global;
    ErrorCode error = get_max_volume( mb, fileset, dim, vmax_global );MB_CHK_ERR( error );

    int init_nl = num_levels;
    num_levels  = 0;
    level_degs.clear();

    // Find a sequence that reduces the maximum volume to desired.
    // desired_vol should be a fraction or absolute value ?

    // double remV = vmax_global*desired_vol/dim;
    double Vdesired = desired_vol;
    double try_x;
    double remV    = vmax_global;
    int degs[3][3] = { { 5, 3, 2 }, { 25, 9, 4 }, { 0, 27, 8 } };

    if( dim == 1 || dim == 2 )
    {
        while( remV - Vdesired >= 0 )
        {
            try_x = degs[dim - 1][0];
            if( ( remV / try_x - Vdesired ) >= 0 )
            {
                level_degs.push_back( 5 );
                num_levels += 1;
                remV /= try_x;
            }
            else
            {
                try_x = degs[dim - 1][1];
                if( ( remV / try_x - Vdesired ) >= 0 )
                {
                    level_degs.push_back( 3 );
                    num_levels += 1;
                    remV /= try_x;
                }
                else
                {
                    try_x = degs[dim - 1][2];
                    if( ( remV / try_x - Vdesired ) >= 0 )
                    {
                        level_degs.push_back( 2 );
                        num_levels += 1;
                        remV /= try_x;
                    }
                    else
                        break;
                }
            }
        }
    }
    else
    {
        while( remV - Vdesired >= 0 )
        {
            try_x = degs[dim - 1][1];
            if( ( remV / try_x - Vdesired ) >= 0 )
            {
                level_degs.push_back( 3 );
                num_levels += 1;
                remV /= try_x;
            }
            else
            {
                try_x = degs[dim - 1][2];
                if( ( remV / try_x - Vdesired ) >= 0 )
                {
                    level_degs.push_back( 2 );
                    num_levels += 1;
                    remV /= try_x;
                }
                else
                    break;
            }
        }
    }

    if( init_nl != 0 && init_nl < num_levels )
    {
        for( int i = level_degs.size(); i >= init_nl; i-- )
            level_degs.pop_back();
        num_levels = init_nl;
    }

    return MB_SUCCESS;
}

ErrorCode get_max_volume( Core& mb, EntityHandle fileset, int dim, double& vmax )
{
    ErrorCode error;
    VerdictWrapper vw( &mb );
    QualityType q;

    switch( dim )
    {
        case 1:
            q = MB_LENGTH;
            break;
        case 2:
            q = MB_AREA;
            break;
        case 3:
            q = MB_VOLUME;
            break;
        default:
            return MB_FAILURE;
            break;
    }

    // Get all entities of the highest dimension which is passed as a command line argument.
    Range allents, owned;
    error = mb.get_entities_by_handle( fileset, allents );MB_CHK_ERR( error );
    owned = allents.subset_by_dimension( dim );MB_CHK_ERR( error );

    // Get all owned entities
#ifdef MOAB_HAVE_MPI
    int size = 1;
    MPI_Comm_size( MPI_COMM_WORLD, &size );
    int mpi_err;
    if( size > 1 )
    {
        // filter the entities not owned, so we do not process them more than once
        ParallelComm* pcomm = moab::ParallelComm::get_pcomm( &mb, 0 );
        Range current       = owned;
        owned.clear();
        error = pcomm->filter_pstatus( current, PSTATUS_NOT_OWNED, PSTATUS_NOT, -1, &owned );
        if( error != MB_SUCCESS )
        {
            MPI_Finalize();
            return MB_FAILURE;
        }
    }
#endif

    double vmax_local = 0;
    // Find the maximum volume of an entity in the owned mesh
    for( Range::iterator it = owned.begin(); it != owned.end(); it++ )
    {
        double volume;
        error = vw.quality_measure( *it, q, volume );MB_CHK_ERR( error );
        if( volume > vmax_local ) vmax_local = volume;
    }

    // Get the global maximum
    double vmax_global = vmax_local;
#ifdef MOAB_HAVE_MPI
    mpi_err = MPI_Reduce( &vmax_local, &vmax_global, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD );
    if( mpi_err )
    {
        MPI_Finalize();
        return MB_FAILURE;
    }
#endif

    vmax = vmax_global;

    return MB_SUCCESS;
}

bool parse_id_list( const char* string, int dim, int nval, std::vector< int >& results )
{
    bool okay   = true;
    char* mystr = strdup( string );
    for( const char* ptr = strtok( mystr, "," ); ptr; ptr = strtok( 0, "," ) )
    {
        char* endptr;
        int val = strtol( ptr, &endptr, 0 );

        if( dim == 1 || dim == 2 )
        {
            if( val != 2 && val != 3 && val != 5 )
            {
                std::cerr << "Not a valid degree for the passed dimension" << std::endl;
                okay = false;
                break;
            }
        }
        else if( dim == 3 )
        {
            if( val != 2 && val != 3 )
            {
                std::cerr << "Not a valid degree for the passed dimension" << std::endl;
                okay = false;
                break;
            }
        }

        if( endptr == ptr || val <= 0 )
        {
            std::cerr << "Not a valid id: " << ptr << std::endl;
            okay = false;
            break;
        }

        results.push_back( val );
    }

    if( (int)results.size() < nval )
    {
        for( int i = results.size(); i <= nval - 1; i++ )
            results.push_back( results[0] );
    }
    else if( (int)results.size() > nval )
    {
        for( int i = results.size(); i > nval; i-- )
            results.pop_back();
    }

    free( mystr );
    return okay;
}

bool make_opts_string( std::vector< std::string > options, std::string& opts )
{
    opts.clear();
    if( options.empty() ) return true;

    // choose a separator character
    std::vector< std::string >::const_iterator i;
    char separator             = '\0';
    const char* alt_separators = ";+,:\t\n";
    for( const char* sep_ptr = alt_separators; *sep_ptr; ++sep_ptr )
    {
        bool seen = false;
        for( i = options.begin(); i != options.end(); ++i )
            if( i->find( *sep_ptr, 0 ) != std::string::npos )
            {
                seen = true;
                break;
            }
        if( !seen )
        {
            separator = *sep_ptr;
            break;
        }
    }
    if( !separator )
    {
        std::cerr << "Error: cannot find separator character for options string" << std::endl;
        return false;
    }
    if( separator != ';' )
    {
        opts = ";";
        opts += separator;
    }

    // concatenate options
    i = options.begin();
    opts += *i;
    for( ++i; i != options.end(); ++i )
    {
        opts += separator;
        opts += *i;
    }

    return true;
}