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
#include "moab/Core.hpp"
#include "moab/ProgOptions.hpp"
#include "moab/ReorderTool.hpp"

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

#ifdef MOAB_HAVE_ZOLTAN
#include "moab/ZoltanPartitioner.hpp"

#ifdef MOAB_HAVE_CGM
#include "InitCGMA.hpp"
#include "CubitCompat.hpp"
#endif

#endif

#ifdef MOAB_HAVE_METIS
#include "moab/MetisPartitioner.hpp"
typedef idx_t PartType;
#else
typedef int PartType;
#endif

#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <list>
#include <ctime>

#include "moab/IntxMesh/IntxUtils.hpp"

using namespace moab;

std::string DEFAULT_TAGGEDSETS_TAG = "PARALLEL_PARTITION";

const char DEFAULT_ZOLTAN_METHOD[] = "RCB";
#ifdef MOAB_HAVE_ZOLTAN
const char ZOLTAN_PARMETIS_METHOD[] = "PARMETIS";
const char ZOLTAN_OCTPART_METHOD[]  = "OCTPART";
#endif

const char METIS_DEFAULT_METHOD[] = "ML_KWAY";
/* const char METIS_ALTERNATIVE_METHOD[] = "ML_RB"; */

const char BRIEF_DESC[] = "Use Zoltan or Metis to partition MOAB meshes for use on parallel computers";
std::ostringstream LONG_DESC;

int main( int argc, char* argv[] )
{
#ifdef MOAB_HAVE_MPI
    int err = MPI_Init( &argc, &argv );
    if( err )
    {
        std::cerr << "MPI_Init failed.  Aborting." << std::endl;
        return 3;
    }
#endif
    Core moab;
    Interface& mb = moab;
    std::vector< int > set_l;

#ifdef MOAB_HAVE_ZOLTAN
    bool moab_use_zoltan = false;
#endif
#ifdef MOAB_HAVE_METIS
    bool moab_use_metis = false;
#endif

    LONG_DESC << "This utility invokes the ZoltanPartitioner or MetisPartitioner component of MOAB/CGM "
                 "to partition a mesh/geometry."
              << std::endl
              << "If no partitioning method is specified, the defaults are: "
              << "for Zoltan=\"" << DEFAULT_ZOLTAN_METHOD << "\" and Metis=\"" << METIS_DEFAULT_METHOD << " method"
              << std::endl;

    ProgOptions opts( LONG_DESC.str(), BRIEF_DESC );

    int part_dim = 3;
    opts.addOpt< int >( "dimension",
                        "Specify dimension of entities to partition."
                        "  Default is  largest in file.",
                        &part_dim, ProgOptions::int_flag );

    std::string zoltan_method, parm_method, oct_method, metis_method;<--- Unused variable: zoltan_method<--- Unused variable: parm_method<--- Unused variable: oct_method<--- Unused variable: metis_method
#ifdef MOAB_HAVE_ZOLTAN
    opts.addOpt< std::string >( "zoltan,z",
                                "(Zoltan) Specify Zoltan partition method.  "
                                "One of RR, RCB, RIB, HFSC, PHG, or Hypergraph (PHG and Hypergraph "
                                "are synonymous).",
                                &zoltan_method );
#ifdef MOAB_HAVE_PARMETIS
    opts.addOpt< std::string >( "parmetis,p", "(Zoltan+PARMetis) Specify PARMetis partition method.", &parm_method );
#endif  // MOAB_HAVE_PARMETIS
    opts.addOpt< std::string >( "octpart,o", "(Zoltan) Specify OctPart partition method.", &oct_method );

    bool incl_closure = false;
    opts.addOpt< void >( "include_closure,c", "Include element closure for part sets.", &incl_closure );

    bool recompute_box_rcb = false;
    opts.addOpt< void >( "recompute_rcb_box,b", "recompute box in rcb cuts", &recompute_box_rcb );
#endif  // MOAB_HAVE_ZOLTAN

    double imbal_tol = 1.03;
    opts.addOpt< double >( "imbalance,i", "Imbalance tolerance (used in PHG/Hypergraph method)", &imbal_tol );

#ifdef MOAB_HAVE_METIS
    opts.addOpt< std::string >( "metis,m", "(Metis) Specify Metis partition method. One of ML_RB or ML_KWAY.",
                                &metis_method );
#endif  // MOAB_HAVE_METIS

    bool write_sets = true, write_tags = false;
    opts.addOpt< void >( "sets,s", "Write partition as tagged sets (Default)", &write_sets );
    opts.addOpt< void >( "tags,t", "Write partition by tagging entities", &write_tags );

    int power = -1;
    opts.addOpt< int >( "power,M", "Generate multiple partitions, in powers of 2, up to 2^(pow)", &power );

    bool reorder = false;
    opts.addOpt< void >( "reorder,R", "Reorder mesh to group entities by partition", &reorder );

    double part_geom_mesh_size = -1.0;<--- Assignment 'part_geom_mesh_size=-1.0', assigned value is -1.0
#ifdef MOAB_HAVE_ZOLTAN
    bool part_surf = false;
#ifdef MOAB_HAVE_CGM
    opts.addOpt< double >( "geom,g", "(CGM) If partition geometry, specify mesh size.", &part_geom_mesh_size );
    opts.addOpt< void >( "surf,f", "(CGM) Specify if partition geometry surface.", &part_surf );
#endif  // MOAB_HAVE_CGM

    bool ghost = false;
    opts.addOpt< void >( "ghost,H", "(Zoltan) Specify if partition ghost geometry body." );

    int obj_weight = 0;
    opts.addOpt< int >( "vertex_w,v", "(Zoltan) Number of weights associated with a graph vertex." );

    int edge_weight = 0;
    opts.addOpt< int >( "edge_w,e", "(Zoltan) Number of weights associated with an edge." );

    bool moab_partition_slave   = false;
    std::string slave_file_name = "";
    opts.addOpt< std::string >( "inferred",
                                "(Zoltan) Specify inferred slave mesh file name to impose "
                                "partition based on cuts computed for original master mesh.",
                                &slave_file_name );

    bool rescale_spherical_radius = false;
    opts.addOpt< void >( "scale_sphere",
                         "(Zoltan) If the meshes are defined on a sphere, rescale radius as needed "
                         "(in combination with --inferred)",
                         &rescale_spherical_radius );

#endif  // MOAB_HAVE_ZOLTAN

    long num_parts;
    opts.addOpt< std::vector< int > >( "set_l,l",
                                       "Load material set(s) with specified ids (comma separated) for partition" );

    opts.addRequiredArg< int >( "#parts", "Number of parts in partition" );

    std::string input_file, output_file;
    opts.addRequiredArg< std::string >( "input_file", "Mesh/geometry to partition", &input_file );
    opts.addRequiredArg< std::string >( "output_file", "File to which to write partitioned mesh/geometry",
                                        &output_file );

    bool print_time = false;
    opts.addOpt< void >( ",T", "Print CPU time for each phase.", &print_time );

    int projection_type = 0;
    opts.addOpt< int >( "project_on_sphere,p",
                        "use spherical coordinates (1) or gnomonic projection (2) for partitioning ",
                        &projection_type );
#ifdef MOAB_HAVE_METIS
    bool partition_tagged_sets = false;
    opts.addOpt< void >( "taggedsets,x", "(Metis) Partition tagged sets.", &partition_tagged_sets );

    bool partition_tagged_ents = false;
    opts.addOpt< void >( "taggedents,y", "(Metis) Partition tagged ents.", &partition_tagged_ents );

    std::string aggregating_tag;
    opts.addOpt< std::string >( "aggregatingtag,a",
                                "(Metis) Specify aggregating tag to partion tagged sets or tagged entities.",
                                &aggregating_tag );

    std::string aggregating_bc_tag;
    opts.addOpt< std::string >( "aggregatingBCtag,B",
                                "(Metis) Specify boundary id tag name used to group cells with same boundary ids.",
                                &aggregating_bc_tag );

    std::string boundaryIds;
    std::vector< int > BCids;
    opts.addOpt< std::string >( "aggregatingBCids,I",
                                " (Metis) Specify id or ids of boundaries to be aggregated before "
                                "partitioning (all elements with same boundary id will be in the "
                                "same partition). Comma separated e.g. -I 1,2,5 ",
                                &boundaryIds );
#endif  // MOAB_HAVE_METIS

    bool assign_global_ids = false;
    opts.addOpt< void >( "globalIds,j", "Assign GLOBAL_ID tag to entities", &assign_global_ids );

    opts.parseCommandLine( argc, argv );

#ifdef MOAB_HAVE_ZOLTAN
    if( !zoltan_method.empty() )
        moab_use_zoltan = true;
    else
#endif
#ifdef MOAB_HAVE_METIS
        if( !metis_method.empty() )
        moab_use_metis = true;
    else
#endif
        MB_SET_ERR( MB_FAILURE, "Specify either Zoltan or Metis partitioner type" );

#ifdef MOAB_HAVE_ZOLTAN
    ZoltanPartitioner* zoltan_tool = NULL;
    // check if partition geometry, if it is, should get mesh size for the geometry
    if( part_geom_mesh_size != -1.0 && part_geom_mesh_size <= 0.0 )<--- Condition 'part_geom_mesh_size<=0.0' is always true
    {
        std::cerr << part_geom_mesh_size << ": invalid geometry partition mesh size." << std::endl << std::endl;
        opts.printHelp();
        return EXIT_FAILURE;
    }

    if( slave_file_name.size() ) moab_partition_slave = true;

    if( moab_use_zoltan )
    {
        if( part_geom_mesh_size < 0. )
        {
            // partition mesh
            zoltan_tool = new ZoltanPartitioner( &mb, false, argc, argv );
        }
        else
        {
            // partition geometry
#ifdef MOAB_HAVE_CGM
            CubitStatus status = InitCGMA::initialize_cgma();
            if( CUBIT_SUCCESS != status )
            {
                std::cerr << "CGM couldn't be initialized." << std::endl << std::endl;
                opts.printHelp();
                return EXIT_FAILURE;
            }
            GeometryQueryTool* gti = GeometryQueryTool::instance();
            zoltan_tool            = new ZoltanPartitioner( &mb, false, argc, argv, gti );
#else
            std::cerr << "CGM should be configured to partition geometry." << std::endl << std::endl;
            opts.printHelp();
            return EXIT_FAILURE;
#endif  // MOAB_HAVE_CGM
        }
        zoltan_tool->set_global_id_option( assign_global_ids );
    }

    if( zoltan_method.empty() && parm_method.empty() && oct_method.empty() ) zoltan_method = DEFAULT_ZOLTAN_METHOD;
    if( !parm_method.empty() ) zoltan_method = ZOLTAN_PARMETIS_METHOD;
    if( !oct_method.empty() ) zoltan_method = ZOLTAN_OCTPART_METHOD;
#endif  // MOAB_HAVE_ZOLTAN

#ifdef MOAB_HAVE_METIS
    MetisPartitioner* metis_tool = NULL;<--- Assignment 'metis_tool=NULL', assigned value is 0
    if( moab_use_metis && !metis_tool )<--- Condition '!metis_tool' is always true
    {
        metis_tool = new MetisPartitioner( &mb, false );
        metis_tool->set_global_id_option( assign_global_ids );
    }

    if( ( aggregating_tag.empty() && partition_tagged_sets ) || ( aggregating_tag.empty() && partition_tagged_ents ) )
        aggregating_tag = DEFAULT_TAGGEDSETS_TAG;
    if( !write_sets && !write_tags ) write_sets = true;

    if( !boundaryIds.empty() )
    {
        std::vector< std::string > ids;
        std::stringstream ss( boundaryIds );
        std::string item;
        while( std::getline( ss, item, ',' ) )
        {
            ids.push_back( item );
        }
        for( unsigned int i = 0; i < ids.size(); i++ )
            BCids.push_back( std::atoi( ids[i].c_str() ) );
    }

    if( metis_method.empty() )
    {
        metis_method = METIS_DEFAULT_METHOD;
    }

#endif  // MOAB_HAVE_METIS

    if( !write_sets && !write_tags ) write_sets = true;

    if( -1 == power )
    {
        num_parts = opts.getReqArg< int >( "#parts" );
        power     = 1;
    }
    else if( power < 1 || power > 18 )
    {
        std::cerr << power << ": invalid power for multiple partitions. Expected value in [1,18]" << std::endl
                  << std::endl;
        opts.printHelp();
        return EXIT_FAILURE;
    }
    else
    {
        num_parts = 2;
    }

    if( part_dim < 0 || part_dim > 3 )
    {
        std::cerr << part_dim << " : invalid dimension" << std::endl << std::endl;
        opts.printHelp();
        return EXIT_FAILURE;
    }

    if( imbal_tol < 0.0 )
    {
        std::cerr << imbal_tol << ": invalid imbalance tolerance" << std::endl << std::endl;
        opts.printHelp();
        return EXIT_FAILURE;
    }

    bool load_msets = false;
    if( opts.getOpt( "set_l,l", &set_l ) )
    {
        load_msets = true;
        if( set_l.size() <= 0 )<--- Unsigned less than zero
        {
            std::cerr << " No material set id's to load" << std::endl << std::endl;
            opts.printHelp();
            return EXIT_FAILURE;
        }
    }

    if( num_parts <= 1 )
    {
        std::cerr << "** Please specify #parts = " << num_parts << " to be greater than 1." << std::endl << std::endl;
        opts.printHelp();
        return EXIT_FAILURE;
    }

    clock_t t = clock();

    const char* options = NULL;
    ErrorCode rval;
#ifdef MOAB_HAVE_ZOLTAN
    if( part_geom_mesh_size > 0. ) options = "FACET_DISTANCE_TOLERANCE=0.1";
#endif  // MOAB_HAVE_ZOLTAN

    std::cout << "Loading file " << input_file << "..." << std::endl;
    if( load_msets == false )
    {
        rval = mb.load_file( input_file.c_str(), 0, options );MB_CHK_SET_ERR( rval, "Failed to load input file: " + input_file );
    }
    else  // load the material set(s)
    {
        rval = mb.load_mesh( input_file.c_str(), &set_l[0], (int)set_l.size() );MB_CHK_SET_ERR( rval, "Failed to load input mesh: " + input_file );
    }
    if( print_time )
        std::cout << "Read input file in " << ( clock() - t ) / (double)CLOCKS_PER_SEC << " seconds" << std::endl;

    for( int dim = part_dim; dim >= 0; --dim )
    {
        int n;
        rval = mb.get_number_entities_by_dimension( 0, dim, n );
        if( MB_SUCCESS == rval && 0 != n )
        {
            part_dim = dim;
            break;
        }
    }
    if( part_dim < 0 )
    {
        std::cerr << input_file << " : file does not contain any mesh entities" << std::endl;
        return 2;
    }

    ReorderTool reorder_tool( &moab );

    for( int p = 0; p < power; p++ )
    {
        t = clock();
#ifdef MOAB_HAVE_ZOLTAN
        if( moab_use_zoltan )
        {
            rval = zoltan_tool->partition_mesh_and_geometry(
                part_geom_mesh_size, num_parts, zoltan_method.c_str(),
                ( !parm_method.empty() ? parm_method.c_str() : oct_method.c_str() ), imbal_tol, part_dim, write_sets,
                write_tags, obj_weight, edge_weight, part_surf, ghost, projection_type, recompute_box_rcb, print_time );MB_CHK_SET_ERR( rval, "Zoltan partitioner failed." );
        }
#endif
#ifdef MOAB_HAVE_METIS
        if( moab_use_metis )
        {
            rval = metis_tool->partition_mesh( num_parts, metis_method.c_str(), part_dim, write_sets, write_tags,
                                               partition_tagged_sets, partition_tagged_ents, aggregating_tag.c_str(),
                                               print_time );MB_CHK_SET_ERR( rval, "Metis partitioner failed." );
        }
#endif

        if( print_time )
            std::cout << "Generated " << num_parts << " part partitioning in "
                      << ( clock() - t ) / (double)CLOCKS_PER_SEC << " seconds" << std::endl;

        if( reorder && part_geom_mesh_size < 0. )
        {
            std::cout << "Reordering mesh for partition..." << std::endl;

            Tag tag, order;
            rval = mb.tag_get_handle( DEFAULT_TAGGEDSETS_TAG.c_str(), 1, MB_TYPE_INTEGER, tag );MB_CHK_SET_ERR( rval, "Partitioner did not create " + DEFAULT_TAGGEDSETS_TAG + " tag" );

            t = clock();
            if( write_sets )
            {
                Range sets;
                mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &tag, 0, 1, sets );
                rval = reorder_tool.handle_order_from_sets_and_adj( sets, order );MB_CHK_SET_ERR( rval, "Failed to calculate reordering." );
            }
            else
            {
                rval = reorder_tool.handle_order_from_int_tag( tag, -1, order );MB_CHK_SET_ERR( rval, "Failed to calculate reordering." );
            }

            rval = reorder_tool.reorder_entities( order );MB_CHK_SET_ERR( rval, "Failed to perform reordering." );

            rval = mb.tag_delete( order );MB_CHK_SET_ERR( rval, "Failed to delete tag." );
            if( print_time )
                std::cout << "Reordered mesh in " << ( clock() - t ) / (double)CLOCKS_PER_SEC << " seconds"
                          << std::endl;
        }

#ifdef MOAB_HAVE_ZOLTAN
        if( incl_closure )
        {
            rval = zoltan_tool->include_closure();MB_CHK_SET_ERR( rval, "Closure inclusion failed." );
        }
#endif

        std::ostringstream tmp_output_file;

        if( power > 1 )
        {
            // append num_parts to output filename
            std::string::size_type idx = output_file.find_last_of( "." );
            if( idx == std::string::npos )
            {
                tmp_output_file << output_file << "_" << num_parts;
                if( part_geom_mesh_size < 0. )
                    tmp_output_file << ".h5m";
                else
                {
                    std::cerr << "output file type is not specified." << std::endl;
                    return 1;
                }
            }
            else
            {
                tmp_output_file << output_file.substr( 0, idx ) << "_" << num_parts << output_file.substr( idx );
            }
        }
        else
            tmp_output_file << output_file;

        t = clock();
        std::cout << "Saving file to " << output_file << "..." << std::endl;
        if( part_geom_mesh_size < 0. )
        {
            rval = mb.write_file( tmp_output_file.str().c_str() );MB_CHK_SET_ERR( rval, tmp_output_file.str() << " : failed to write file." << std::endl );
        }
#ifdef MOAB_HAVE_ZOLTAN
#ifdef MOAB_HAVE_CGM
        else
        {
            std::string::size_type idx = output_file.find_last_of( "." );
            int c_size                 = output_file.length() - idx;
            const char* file_type      = NULL;
            if( output_file.compare( idx, c_size, ".occ" ) == 0 || output_file.compare( idx, c_size, ".OCC" ) == 0 )
                file_type = "OCC";
            else if( output_file.compare( idx, c_size, ".sab" ) == 0 )
                file_type = "ACIS_SAB";
            else if( output_file.compare( idx, c_size, ".sat" ) == 0 )
                file_type = "ACIS_SAT";
            else
            {
                std::cerr << "File type for " << output_file.c_str() << " not supported." << std::endl;
                return 1;
            }

            int num_ents_exported = 0;
            DLIList< RefEntity* > ref_entity_list;
            CubitStatus status =
                CubitCompat_export_solid_model( ref_entity_list, tmp_output_file.str().c_str(), file_type,
                                                num_ents_exported, CubitString( __FILE__ ) );
            if( CUBIT_SUCCESS != status )
            {
                std::cerr << "CGM couldn't export models." << std::endl;
                return 1;
            }
        }
#endif
#endif

        if( print_time )
            std::cout << "Wrote \"" << tmp_output_file.str() << "\" in " << ( clock() - t ) / (double)CLOCKS_PER_SEC
                      << " seconds" << std::endl;

#ifdef MOAB_HAVE_ZOLTAN

        if( moab_use_zoltan && moab_partition_slave && p == 0 )
        {
            t = clock();
            double master_radius, slave_radius;
            if( rescale_spherical_radius )
            {
                EntityHandle rootset = 0;
                Range masterverts;
                rval = mb.get_entities_by_dimension( rootset, 0, masterverts );MB_CHK_SET_ERR( rval, "Can't create vertices on master set" );
                double points[6];
                EntityHandle mfrontback[2] = { masterverts[0], masterverts[masterverts.size() - 1] };
                rval                       = mb.get_coords( &mfrontback[0], 2, points );MB_CHK_ERR( rval );
                const double mr1 = std::sqrt( points[0] * points[0] + points[1] * points[1] + points[2] * points[2] );
                const double mr2 = std::sqrt( points[3] * points[3] + points[4] * points[4] + points[5] * points[5] );
                master_radius    = 0.5 * ( mr1 + mr2 );
            }
            EntityHandle slaveset;
            rval = mb.create_meshset( moab::MESHSET_SET, slaveset );MB_CHK_SET_ERR( rval, "Can't create new set" );
            rval = mb.load_file( slave_file_name.c_str(), &slaveset, options );MB_CHK_SET_ERR( rval, "Can't load slave mesh" );
            if( rescale_spherical_radius )
            {
                double points[6];
                Range slaveverts;
                rval = mb.get_entities_by_dimension( slaveset, 0, slaveverts );MB_CHK_SET_ERR( rval, "Can't create vertices on master set" );
                EntityHandle sfrontback[2] = { slaveverts[0], slaveverts[slaveverts.size() - 1] };
                rval                       = mb.get_coords( &sfrontback[0], 2, points );MB_CHK_ERR( rval );
                const double sr1 = std::sqrt( points[0] * points[0] + points[1] * points[1] + points[2] * points[2] );
                const double sr2 = std::sqrt( points[3] * points[3] + points[4] * points[4] + points[5] * points[5] );
                slave_radius     = 0.5 * ( sr1 + sr2 );
                // Let us rescale both master and slave meshes to a unit sphere
                rval = moab::IntxUtils::ScaleToRadius( &mb, slaveset, master_radius );MB_CHK_ERR( rval );
            }

            rval = zoltan_tool->partition_inferred_mesh( slaveset, num_parts, part_dim, write_sets, projection_type );MB_CHK_ERR( rval );

            if( rescale_spherical_radius )
            {
                // rescale the slave mesh back to its original radius
                rval = moab::IntxUtils::ScaleToRadius( &mb, slaveset, slave_radius );MB_CHK_ERR( rval );
            }

            if( print_time )
            {
                std::cout << "Time taken to infer slave mesh partitions = " << ( clock() - t ) / (double)CLOCKS_PER_SEC
                          << " seconds" << std::endl;
            }

            size_t lastindex                 = slave_file_name.find_last_of( "." );
            std::string inferred_output_file = slave_file_name.substr( 0, lastindex ) + "_inferred" +
                                               slave_file_name.substr( lastindex, slave_file_name.size() );

            // Save the resulting mesh
            std::cout << "Saving inferred file to " << inferred_output_file << "..." << std::endl;
            rval = mb.write_file( inferred_output_file.c_str(), 0, 0, &slaveset, 1 );MB_CHK_SET_ERR( rval, inferred_output_file << " : failed to write file." << std::endl );
        }
#endif

        num_parts *= 2;
    }

#ifdef MOAB_HAVE_ZOLTAN
    delete zoltan_tool;
#endif
#ifdef MOAB_HAVE_METIS
    delete metis_tool;
#endif

#ifdef MOAB_HAVE_MPI
    err = MPI_Finalize();
    assert( MPI_SUCCESS == err );
#endif
    return 0;
}