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
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
/** \file ScdInterface.hpp
 */
#ifndef SCD_INTERFACE
#define SCD_INTERFACE

#include "moab/Interface.hpp"
#include "moab/HomXform.hpp"

#include <iostream>
#include <vector>
#include <cassert>

#include "moab/win32_config.h"

namespace moab
{

class StructuredElementSeq;
class EntitySequence;
class ScdVertexData;
class EntitySequence;
class ScdBox;
class ParallelComm;

/** \class ScdInterface ScdInterface.hpp "moab/ScdInterface.hpp"
 * \brief A structured mesh interface for MOAB-based data
 *
 * Structured mesh in MOAB is created and accessed through the ScdInterface and ScdBox classes.
 *
 * \section Construction Construction and Representation
 * Structured mesh can be constructed in one of two ways.  First, a rectangular block of mesh,
 * both vertices and edges/quads/hexes, can be created in one shot, using the construct_box method.
 * In this case, there are single sequences of vertices/entities.  The second method for creating
 * structured mesh is to create the structured blocks of vertices and elements separately.  In
 * this case, different blocks of elements can share blocks of vertices, and each block of
 * elements has its own independent parametric space.  The algorithms behind this representation
 * are described in T. Tautges, "MOAB-SD: Integrated structured and unstructured mesh
 * representation", Eng. w Comp, vol 20 no. 3.
 *
 * Structured mesh is represented in MOAB down at the element sequence level, which is something
 * applications don't see.  In addition, when structured mesh is created, entity sets are also
 * created and tagged with information about the parametric space.  In particular, the BOX_DIMS
 * tag is used to indicate the lower and upper corners in parametric space (this
 * tag is integer size 6).  Structured mesh blocks are also available through ScdBox class objects
 * returned by ScdInterface.  These class objects should be treated only as references to the
 * structured mesh blocks; that is, the structured mesh referenced by these objects is not deleted
 * when the ScdBox instance is destroyed.  Functions for destroying the actual mesh are available
 * on this class, though.
 *
 * Structured mesh blocks are returned in the form of ScdBox class objects.  Each ScdBox instance
 * represents a rectangular block of vertices and possibly elements (edges, quads, or hexes).  The
 * edge/quad/hex entity handles for a ScdBox are guaranteed to be contiguous, starting at a starting
 * value which is also available through the ScdBox class.  However, vertex handles may or may not
 * be contiguous, depending on the construction method.  The start vertex handle is also available
 * from the ScdBox class.
 *
 * \section Parameters Parametric Space
 *
 * Each structured box has a parametric (ijk) space, which can be queried through the ScdBox
 * interface. For non-periodic boxes, the edge/quad/hex parameter bounds are one less in each
 * dimension than that of the vertices, otherwise they are the same as the vertex parameter bounds.
 * In a parallel representation, boxes are locally non-periodic by default, but global ids are
 * assigned such that the last set of vertices in a periodic direction match those of the first set
 * of vertices in that direction.
 *
 * Entity handles are allocated with the i parameter varying fastest, then j, then k.
 *
 * \section Per Periodic Meshes
 * Boxes can be periodic in i, or j, or both i and j.  If only i or j is periodic, the corresponding
 * mesh is a strip or an annular cylinder; if both i and j are periodic, the corresponding mesh is
 * an annular torus.  A box cannot be periodic in all three parameters.  If i and/or j is periodic,
 * and assuming IMIN/JMIN is zero, the parameter extents in the/each periodic direction (IMAX/JMAX)
 * for vertices and edges/faces/hexes are the same, and the vertices on the "top" end in the
 * periodic direction are at parameter value IMIN/JMIN.
 *
 * \section Par Parallel Representation
 *
 * For parallel structured meshes, each local mesh (the mesh on a given process) looks like a
 * non-periodic structured mesh, and there are both local and global parameters of the structured
 * mesh.  If the mesh is periodic in a given direction, the last process in the periodic direction
 * has local IMAX/JMAX that is one greater than the global IMAX/JMAX.
 *
 *
 * In parallel, the periodicity described in the previous paragraph is "local periodicity"; there is
 * also the notion of global periodicity.  For serial meshes, those concepts are the same.  In
 * parallel, a mesh can be locally non-periodic but globally periodic in a given direction.  In that
 * case, the local mesh is still non-periodic, i.e. the parametric extents for edges is one fewer
 * than that of vertices in that direction. However, vertices are given global ids such that they
 * match those of the parametric minimum in that direction. Geometric positions of the vertices at
 * the high end should still be greater than the ones just below.
 *
 * \section Adjs Adjacent Entities
 * This interface supports parametric access to intermediate-dimension entities, e.g. adjacent faces
 * and edges in a 3d mesh.  In this case, a direction parameter is added, to identify the parametric
 * direction of the entities being requested.  For example, to retrieve the faces adjacent to a hex
 * with parameters ijk, in the i parametric direction, you would use the parameters ijk0.  These
 * intermediate entities are not stored in a structured representation, but their parametric
 * positions can be evaluated based on their adjacencies to higher-dimensional entities.  Thanks to
 * Milad Fatenejad for the thinking behind this.
 *
 * \section Evaluation Evaluation
 * The ScdBox class provides functions for evaluating the mesh based on the ijk parameter space.
 * These functions are inlined where possible, for efficiency.
 */

//! struct for keeping parallel data in one place
class ScdParData
{
  public:
    ScdParData() : partMethod( NOPART ), pComm( NULL )
    {
        gDims[0] = gDims[1] = gDims[2] = gDims[3] = gDims[4] = gDims[5] = 0;
        gPeriodic[0] = gPeriodic[1] = gPeriodic[2] = 0;
        pDims[0] = pDims[1] = pDims[2] = 0;
    }

    //! Partition method enumeration; these strategies are described in comments for
    //! compute_partition_alljorkori, compute_partition_alljkbal, compute_partition_sqij,
    //! compute_partition_sqjk, and compute_partition_sqijk
    enum PartitionMethod
    {
        ALLJORKORI = 0,
        ALLJKBAL,
        SQIJ,
        SQJK,
        SQIJK,
        TRIVIAL,
        RCBZOLTAN,
        NOPART
    };

    //! Partition method names
    static MOAB_EXPORT const char* PartitionMethodNames[NOPART + 1];

    //! partition method used to partition global parametric space
    int partMethod;

    //! lower and upper corners of global box
    int gDims[6];

    //! is globally periodic in i or j or k
    int gPeriodic[3];

    //! number of procs in each direction
    int pDims[3];

    //! parallel communicator object for this par scd mesh
    ParallelComm* pComm;
};

class MOAB_EXPORT ScdInterface
{
  public:
    friend class ScdBox;

    //! Constructor
    /** Constructor; if find_boxes is true, this will search for entity sets marked as
     * structured blocks, based on the BOX_DIMS tag.  Structured mesh blocks will be stored
     * in this interface class for future retrieval.  Structured mesh blocks created through
     * this interface will also be stored here.
     * \param impl MOAB instance
     * \param find_boxes If true, search all the entity sets, caching the structured mesh blocks
     */
    ScdInterface( Interface* impl, bool find_boxes = false );

    // Destructor
    ~ScdInterface();

    //! Return the MOAB Interface instance *
    Interface* impl() const;

    //! Construct new structured mesh box, including both vertices and elements
    /** Parameter range
     * for vertex box is [low-high], for elements is [low-high).  Construct quads by passing
     * in low[2] == high[2], and edges by passing in low[1] == high[1] and low[2] == high[2].
     * The result is passed back in a ScdBox*, which is a *reference* to the box of structured mesh.
     * That is, the actual mesh is retained in MOAB when the ScdBox is destroyed.  To actually
     * destroy the mesh, call the destroy_mesh function on the ScdBox object first, before
     * destroying it. \param low Lower corner in parameter space \param high Higher corner in
     * parameter space \param coords Coordinates of vertices, interleaved (xyzxyz...); if NULL,
     * coords are set to parametric values \param num_coords Number of coordinate values \param
     * new_box Reference to box of structured mesh \param lperiodic[3] If lperiodic[s] != 0,
     * direction s is locally periodic \param par_data If non-NULL, this will get stored on the
     * ScdBox once created, contains info about global parallel nature of ScdBox across procs \param
     * assign_global_ids If true, assigns 1-based global ids to vertices using GLOBAL_ID_TAG_NAME
     * \param resolve_shared_ents If != -1, resolves shared entities up to and including dimension
     * equal to value
     */
    ErrorCode construct_box( HomCoord low,
                             HomCoord high,
                             const double* const coords,
                             unsigned int num_coords,
                             ScdBox*& new_box,
                             int* const lperiodic       = NULL,
                             ScdParData* const par_data = NULL,
                             bool assign_global_ids     = false,
                             int resolve_shared_ents    = -1 );

    //! Create a structured sequence of vertices, quads, or hexes
    /** Starting handle for the sequence is available from the returned ScdBox.
     * If creating a structured quad or hex box, subsequent calls must be made to ScdBox::add_vbox,
     * until all the vertices have been filled in for the box.
     * \param low Lower corner of structured box
     * \param high Higher corner of structured box
     * \param type EntityType, one of MBVERTEX, MBEDGE, MBQUAD, MBHEX
     * \param starting_id Requested start id of entities
     * \param new_box Reference to the newly created box of entities
     * \param is_periodic[3] If is_periodic[s] is non-zero, mesh should be periodic in direction s
     * (s=[0,1,2])
     */
    ErrorCode create_scd_sequence( const HomCoord& low,
                                   const HomCoord& high,
                                   EntityType type,
                                   int starting_id,
                                   ScdBox*& new_box,
                                   int* is_periodic = NULL );

    //! Return all the structured mesh blocks in this MOAB instance, as ScdBox objects
    /** Return the structured blocks in this MOAB instance.  If these were not searched for
     * at instantiation time, then the search is done now.
     * \param boxes Vector of ScdBox objects representing structured mesh blocks
     */
    ErrorCode find_boxes( std::vector< ScdBox* >& boxes );

    //! Return all the structured mesh blocks in this MOAB instance, as entity set handles
    /** Return the structured blocks in this MOAB instance.  If these were not searched for
     * at instantiation time, then the search is done now.
     * \param boxes Range of entity set objects representing structured mesh blocks
     */
    ErrorCode find_boxes( Range& boxes );

    //! Return all the structured mesh blocks known by ScdInterface (does not search)
    /** Return the structured blocks in this ScdInterface instance.  Does not search for new boxes,
     * just returns the contents of the list.
     * \param boxes Structured boxes
     */
    ErrorCode get_boxes( std::vector< ScdBox* >& boxes );

    //! Return the tag marking the lower and upper corners of boxes
    /**
     * \param create_if_missing If the tag does not yet exist, create it
     */
    Tag box_dims_tag( bool create_if_missing = true );

    //! Return the tag marking the global lower and upper corners of boxes
    /**
     * \param create_if_missing If the tag does not yet exist, create it
     */
    Tag global_box_dims_tag( bool create_if_missing = true );

    //! Return the tag marking the partitioning method used to partition the box in parallel
    /**
     * \param create_if_missing If the tag does not yet exist, create it
     */
    Tag part_method_tag( bool create_if_missing = true );

    //! Return the tag marking whether box is periodic in i and j
    /**
     * \param create_if_missing If the tag does not yet exist, create it
     */
    Tag box_periodic_tag( bool create_if_missing = true );

    //! Return the tag marking the ScdBox for a set
    /**
     * \param create_if_missing If the tag does not yet exist, create it
     */
    Tag box_set_tag( bool create_if_missing = true );

    //! Return the ScdBox corresponding to the entity set passed in
    /** If the entity isn't a structured box set, NULL is returned.
     * \param eh Entity whose box is being queried
     */
    ScdBox* get_scd_box( EntityHandle eh );

    //! Compute a partition of structured parameter space
    /** Compute a partition of structured parameter space, based on data in the ScdParData
     * passed in.  Results are passed back in arguments, which application can set back into
     * par_data argument if they so desire.
     * \param np Number of processors
     * \param nr Rank of this processor
     * \param par_data ScdParData object that contains input global parameter space, desired
     *           partitioning method, and information about global periodicity.
     * \param ldims Local parameters for grid
     * \param lperiodic Whether or not a given dimension is locally periodic
     * \param pdims Number of procs in i, j, k directions
     */
    static ErrorCode compute_partition( int np,
                                        int nr,
                                        const ScdParData& par_data,
                                        int* ldims,
                                        int* lperiodic = NULL,
                                        int* pdims     = NULL );

    //! Get information about the neighbor in the dijk[] direction, where dijk can be -1 or 1 for
    //! all 3 params
    /** Get information about the neighbor in the dijk[] direction, where dijk can be -1 or 1 for
     * all 3 params \param np (in) Total # procs \param nr Processor from which neighbor is
     * requested \param spd (in) ScdParData containing part method, gdims, gperiodic data \param
     * dijk(*) (in) Direction being queried, = +/-1 or 0 \param pto (out) Processor holding the
     * destination part \param rdims(6) (out) Parametric min/max of destination part \param
     * facedims(6) (out) Parametric min/max of interface between pfrom and pto; if at the max in a
     * periodic direction, set to global min of that direction \param across_bdy(3) (out) If
     * across_bdy[i] is -1(1), interface with pto is across periodic lower(upper) bdy in parameter
     * i, 0 otherwise
     */
    static ErrorCode get_neighbor( int np,
                                   int nr,
                                   const ScdParData& spd,
                                   const int* const dijk,
                                   int& pto,
                                   int* rdims,
                                   int* facedims,
                                   int* across_bdy );

    //! Tag vertices with sharing data for parallel representations
    /** Given the ParallelComm object to use, tag the vertices shared with other processors
     */
    ErrorCode tag_shared_vertices( ParallelComm* pcomm, EntityHandle seth );

    //! Tag vertices with sharing data for parallel representations
    /** Given the ParallelComm object to use, tag the vertices shared with other processors
     */
    ErrorCode tag_shared_vertices( ParallelComm* pcomm, ScdBox* box );

  protected:
    //! Remove the box from the list on ScdInterface
    ErrorCode remove_box( ScdBox* box );

    //! Add the box to the list on ScdInterface
    ErrorCode add_box( ScdBox* box );

  private:
    //! Create an entity set for a box, and tag with the parameters
    /** \param low Lower corner parameters for this box
     * \param high Upper corner parameters for this box
     * \param scd_set Entity set created
     * \param is_periodic[3] If is_periodic[s] is non-zero, mesh should be periodic in direction s
     * (s=[0,1,2])
     */
    ErrorCode create_box_set( const HomCoord& low,
                              const HomCoord& high,
                              EntityHandle& scd_set,
                              int* is_periodic = NULL );

    //! Compute a partition of structured parameter space
    /** Partitions the structured parametric space by partitioning j, k, or i only.
     * If j is greater than #procs, partition that, else k, else i.
     * For description of arguments, see ScdInterface::compute_partition.
     */
    inline static ErrorCode compute_partition_alljorkori( int np,
                                                          int nr,
                                                          const int gijk[6],
                                                          const int* const gperiodic,
                                                          int* lijk,
                                                          int* lperiodic,
                                                          int* pijk );

    //! Compute a partition of structured parameter space
    /** Partitions the structured parametric space by partitioning j, and possibly k,
     * seeking square regions of jk space
     * For description of arguments, see ScdInterface::compute_partition.
     */
    inline static ErrorCode compute_partition_alljkbal( int np,
                                                        int nr,
                                                        const int gijk[6],
                                                        const int* const gperiodic,
                                                        int* lijk,
                                                        int* lperiodic,
                                                        int* pijk );

    //! Compute a partition of structured parameter space
    /** Partitions the structured parametric space by seeking square ij partitions
     * For description of arguments, see ScdInterface::compute_partition.
     */
    inline static ErrorCode compute_partition_sqij( int np,
                                                    int nr,
                                                    const int gijk[6],
                                                    const int* const gperiodic,
                                                    int* lijk,
                                                    int* lperiodic,
                                                    int* pijk );

    //! Compute a partition of structured parameter space
    /** Partitions the structured parametric space by seeking square jk partitions
     * For description of arguments, see ScdInterface::compute_partition.
     */
    inline static ErrorCode compute_partition_sqjk( int np,
                                                    int nr,
                                                    const int gijk[6],
                                                    const int* const gperiodic,
                                                    int* lijk,
                                                    int* lperiodic,
                                                    int* pijk );

    //! Compute a partition of structured parameter space
    /** Partitions the structured parametric space by seeking square ijk partitions
     * For description of arguments, see ScdInterface::compute_partition.
     */
    inline static ErrorCode compute_partition_sqijk( int np,
                                                     int nr,
                                                     const int gijk[6],
                                                     const int* const gperiodic,
                                                     int* lijk,
                                                     int* lperiodic,
                                                     int* pijk );

    //! Get vertices shared with other processors
    /** Shared vertices returned as indices into each proc's handle space
     * \param box Box used to get parametric space info
     * \param procs Procs this proc shares vertices with
     * \param offsets Offsets into indices list for each proc
     * \param shared_indices local/remote indices of shared vertices
     */
    static ErrorCode get_shared_vertices( ParallelComm* pcomm,
                                          ScdBox* box,
                                          std::vector< int >& procs,
                                          std::vector< int >& offsets,
                                          std::vector< int >& shared_indices );

    static ErrorCode get_indices( const int* const ldims,
                                  const int* const rdims,
                                  const int* const across_bdy,
                                  int* face_dims,
                                  std::vector< int >& shared_indices );

    static ErrorCode get_neighbor_alljorkori( int np,
                                              int pfrom,
                                              const int* const gdims,
                                              const int* const gperiodic,
                                              const int* const dijk,
                                              int& pto,
                                              int* rdims,
                                              int* facedims,
                                              int* across_bdy );

    static ErrorCode get_neighbor_alljkbal( int np,
                                            int pfrom,
                                            const int* const gdims,
                                            const int* const gperiodic,
                                            const int* const dijk,
                                            int& pto,
                                            int* rdims,
                                            int* facedims,
                                            int* across_bdy );

    static ErrorCode get_neighbor_sqij( int np,
                                        int pfrom,
                                        const int* const gdims,
                                        const int* const gperiodic,
                                        const int* const dijk,
                                        int& pto,
                                        int* rdims,
                                        int* facedims,
                                        int* across_bdy );

    static ErrorCode get_neighbor_sqjk( int np,
                                        int pfrom,
                                        const int* const gdims,
                                        const int* const gperiodic,
                                        const int* const dijk,
                                        int& pto,
                                        int* rdims,
                                        int* facedims,
                                        int* across_bdy );

    static ErrorCode get_neighbor_sqijk( int np,
                                         int pfrom,
                                         const int* const gdims,
                                         const int* const gperiodic,
                                         const int* const dijk,
                                         int& pto,
                                         int* rdims,
                                         int* facedims,
                                         int* across_bdy );

    static int gtol( const int* gijk, int i, int j, int k );

    //! assign global ids to vertices in this box
    ErrorCode assign_global_ids( ScdBox* box );

    //! interface instance
    Interface* mbImpl;

    //! whether we've searched the database for boxes yet
    bool searchedBoxes;

    //! structured mesh blocks; stored as ScdBox objects, can get sets from those
    std::vector< ScdBox* > scdBoxes;

    //! tag representing whether box is periodic in i and j
    Tag boxPeriodicTag;

    //! tag representing box lower and upper corners
    Tag boxDimsTag;

    //! tag representing global lower and upper corners
    Tag globalBoxDimsTag;

    //! tag representing partition method
    Tag partMethodTag;

    //! tag pointing from set to ScdBox
    Tag boxSetTag;
};

class MOAB_EXPORT ScdBox
{
    friend class ScdInterface;

  public:
    //! Destructor
    ~ScdBox();

    //! Return the ScdInterface responsible for this box
    inline ScdInterface* sc_impl() const;

    //! Add a vertex box to this box
    /* Add a vertex box to the element sequence referenced by this box.  The passed in vbox must
     * be a vertex box, with parametric extents no larger than that of this box.  This vbox is
     * oriented to this box by matching parameters from1-from3 in vbox to to1-to3 in this box.
     * If bb_input is true, only the part of the vertex sequence between bb_min and bb_max is
     * referenced \param vbox The vertex box being added to this box \param from1 1st reference
     * point on vbox \param to1 1st reference point on this box \param from2 2nd reference point on
     * vbox \param to2 2nd reference point on this box \param from3 3rd reference point on vbox
     * \param to3 3rd reference point on this box
     * \param bb_input If true, subsequent parameters list extents of vbox referenced
     * \param bb_min Lower corner of rectangle referenced
     * \param bb_max Upper corner of rectangle referenced
     */
    ErrorCode add_vbox( ScdBox* vbox,
                        HomCoord from1,
                        HomCoord to1,
                        HomCoord from2,
                        HomCoord to2,
                        HomCoord from3,
                        HomCoord to3,
                        bool bb_input          = false,
                        const HomCoord& bb_min = HomCoord::getUnitv( 0 ),
                        const HomCoord& bb_max = HomCoord::getUnitv( 0 ) );
    // const HomCoord &bb_min = HomCoord::unitv[0],
    // const HomCoord &bb_max = HomCoord::unitv[0]);

    //! Return whether this box has all its vertices defined
    /** Tests whether vertex boxs added with add_vbox have completely defined the vertex parametric
     * space for this box.
     *
     */
    bool boundary_complete() const;

    //! Return highest topological dimension of box
    inline int box_dimension() const;

    //! Starting vertex handle for this box
    inline EntityHandle start_vertex() const;

    //! Starting entity handle for this box
    /** If this is a vertex box, the start vertex handle is returned.
     */
    inline EntityHandle start_element() const;

    //! Return the number of elements in the box
    /* Number of elements is (boxSize[0]-1)(boxSize[1]-1)(boxSize[2]-1)
     */
    inline int num_elements() const;

    //! Return the number of vertices in the box
    /* Number of vertices is boxSize[0] * boxSize[1] * boxSize[2]
     */
    inline int num_vertices() const;

    //! Return the parametric coordinates for this box
    /**
     * \return IJK parameters of lower and upper corners
     */
    inline const int* box_dims() const;

    //! Return the lower corner parametric coordinates for this box
    inline HomCoord box_min() const;

    //! Return the upper corner parametric coordinates for this box
    inline HomCoord box_max() const;

    //! Return the parameter extents for this box
    inline HomCoord box_size() const;

    //! Return the parametric extents for this box
    /**
     * \param ijk IJK extents of this box
     */
    inline void box_size( int* ijk ) const;

    //! Return the parametric extents for this box
    /**
     * \param i I extent of this box
     * \param j J extent of this box
     * \param k K extent of this box
     */
    void box_size( int& i, int& j, int& k ) const;

    //! Get the element at the specified coordinates
    /**
     * \param ijk Parametric coordinates being evaluated
     */
    EntityHandle get_element( const HomCoord& ijk ) const;

    //! Get the element at the specified coordinates
    /**
     * \param i Parametric coordinates being evaluated
     * \param j Parametric coordinates being evaluated
     * \param k Parametric coordinates being evaluated
     */
    EntityHandle get_element( int i, int j = 0, int k = 0 ) const;

    //! Get the vertex at the specified coordinates
    /**
     * \param ijk Parametric coordinates being evaluated
     */
    EntityHandle get_vertex( const HomCoord& ijk ) const;

    //! Get the vertex at the specified coordinates
    /**
     * \param i Parametric coordinates being evaluated
     * \param j Parametric coordinates being evaluated
     * \param k Parametric coordinates being evaluated
     */
    EntityHandle get_vertex( int i, int j = 0, int k = 0 ) const;

    //! Get parametric coordinates of the specified entity
    /** This function returns MB_ENTITY_NOT_FOUND if the entity is not
     * in this ScdBox.
     * \param ent Entity being queried
     * \param i Parametric coordinates returned
     * \param j Parametric coordinates returned
     * \param k Parametric coordinates returned
     * \param dir Parametric coordinate direction returned (in case of getting adjacent
     *            edges (2d, 3d) or faces (3d); not modified otherwise
     */
    ErrorCode get_params( EntityHandle ent, int& i, int& j, int& k, int& dir ) const;

    //! Get parametric coordinates of the specified entity, intermediate entities not allowed (no
    //! dir parameter)
    /** This function returns MB_ENTITY_NOT_FOUND if the entity is not
     * in this ScdBox, or MB_FAILURE if the entity is an intermediate-dimension entity.
     * \param ent Entity being queried
     * \param i Parametric coordinates returned
     * \param j Parametric coordinates returned
     * \param k Parametric coordinates returned
     */
    ErrorCode get_params( EntityHandle ent, int& i, int& j, int& k ) const;

    //! Get parametric coordinates of the specified entity
    /** This function returns MB_ENTITY_NOT_FOUND if the entity is not
     * in this ScdBox.
     * \param ent Entity being queried
     * \param ijkd Parametric coordinates returned (including direction, in case of
     *            getting adjacent edges (2d, 3d) or faces (3d))
     */
    ErrorCode get_params( EntityHandle ent, HomCoord& ijkd ) const;

    /** \brief Get the adjacent edge or face at a parametric location
     * This function gets the left (i=0), front (j=0), or bottom (k=0) edge or face for a parametric
     * element. Left, front, or bottom is indicated by dir = 0, 1, or 2, resp.  All edges and faces
     * in a structured mesh block can be accessed using these parameters. \param dim Dimension of
     * adjacent entity being requested \param i Parametric coordinates of cell being evaluated
     * \param j Parametric coordinates of cell being evaluated
     * \param k Parametric coordinates of cell being evaluated
     * \param dir Direction (0, 1, or 2), for getting adjacent edges (2d, 3d) or faces (3d)
     * \param ent Entity returned from this function
     * \param create_if_missing If true, creates the entity if it doesn't already exist
     */
    ErrorCode get_adj_edge_or_face( int dim,
                                    int i,
                                    int j,
                                    int k,
                                    int dir,
                                    EntityHandle& ent,
                                    bool create_if_missing = true ) const;

    //! Return whether the box contains the parameters passed in
    /**
     * \param i Parametric coordinates being evaluated
     * \param j Parametric coordinates being evaluated
     * \param k Parametric coordinates being evaluated
     */
    bool contains( int i, int j, int k ) const;

    //! Return whether the box contains the parameters passed in
    /**
     * \param i Parametric coordinates being evaluated
     * \param j Parametric coordinates being evaluated
     * \param k Parametric coordinates being evaluated
     */
    bool contains( const HomCoord& ijk ) const;

    //! Set/Get the entity set representing the box
    void box_set( EntityHandle this_set );
    EntityHandle box_set();

    //! Get coordinate arrays for vertex coordinates for a structured block
    /** Returns error if there isn't a single vertex sequence associated with this structured block
     * \param xc X coordinate array pointer returned
     * \param yc Y coordinate array pointer returned
     * \param zc Z coordinate array pointer returned
     */
    ErrorCode get_coordinate_arrays( double*& xc, double*& yc, double*& zc );

    //! Get read-only coordinate arrays for vertex coordinates for a structured block
    /** Returns error if there isn't a single vertex sequence associated with this structured block
     * \param xc X coordinate array pointer returned
     * \param yc Y coordinate array pointer returned
     * \param zc Z coordinate array pointer returned
     */
    ErrorCode get_coordinate_arrays( const double*& xc, const double*& yc, const double*& zc ) const;

    //! Return whether box is locally periodic in i
    /** Return whether box is locally periodic in i
     * \return True if box is locally periodic in i direction
     */
    bool locally_periodic_i() const;

    //! Return whether box is locally periodic in j
    /** Return whether box is locally periodic in j
     * \return True if box is locally periodic in j direction
     */
    bool locally_periodic_j() const;

    //! Return whether box is locally periodic in k
    /** Return whether box is locally periodic in k
     * \return True if box is locally periodic in k direction
     */
    bool locally_periodic_k() const;

    //! Set local periodicity
    /**
     * \param lperiodic Vector of ijk periodicities to set this box to
     */
    void locally_periodic( bool lperiodic[3] );

    //! Get local periodicity
    /**
     * \return Vector of ijk periodicities for this box
     */
    const int* locally_periodic() const;

    //! Return parallel data
    /** Return parallel data, if there is any
     * \return par_data Parallel data set on this box
     */
    ScdParData& par_data()
    {
        return parData;
    }

    //! Return parallel data
    /** Return parallel data, if there is any
     * \return par_data Parallel data set on this box
     */
    const ScdParData& par_data() const
    {
        return parData;
    }

    //! set parallel data
    /** Set parallel data for this box
     * \param par_data Parallel data to be set on this box
     */
    void par_data( const ScdParData& par_datap )
    {
        parData = par_datap;
    }

  private:
    //! Constructor
    /** Create a structured box instance; this constructor is private because it should only be
     * called from ScdInterface, a friend class.  This constructor takes two sequences, one of which
     * can be NULL.  If both sequences come in non-NULL, the first should be a VertexSequence*
     * corresponding to a structured vertex sequence and the second should be a
     * StructuredElementSeq*.  If the 2nd is NULL, the first can be either of those types.  The
     * other members of this class are taken from the sequences (e.g. parametric space) or the box
     * set argument.  Tags on the box set should be set from the caller. \param sc_impl A
     * ScdInterface instance \param box_set Entity set representing this rectangle \param seq1 An
     * EntitySequence (see ScdBox description) \param seq2 An EntitySequence (see ScdBox
     * description), or NULL
     */
    ScdBox( ScdInterface* sc_impl, EntityHandle box_set, EntitySequence* seq1, EntitySequence* seq2 = NULL );

    //! function to get vertex handle directly from sequence
    /** \param i Parameter being queried
     * \param j Parameter being queried
     * \param k Parameter being queried
     */
    EntityHandle get_vertex_from_seq( int i, int j, int k ) const;

    //! set the vertex sequence
    ErrorCode vert_dat( ScdVertexData* vert_dat );

    //! get the vertex sequence
    ScdVertexData* vert_dat() const;

    //! set the element sequence
    ErrorCode elem_seq( EntitySequence* elem_seq );

    //! get the element sequence
    StructuredElementSeq* elem_seq() const;

    //! Set the starting vertex handle for this box
    void start_vertex( EntityHandle startv );

    //! Set the starting entity handle for this box
    void start_element( EntityHandle starte );

    //! interface instance
    ScdInterface* scImpl;

    //! entity set representing this box
    EntityHandle boxSet;

    //! vertex sequence this box represents, if there's only one, otherwise they're
    //! retrieved from the element sequence
    ScdVertexData* vertDat;

    //! element sequence this box represents
    StructuredElementSeq* elemSeq;

    //! starting vertex handle for this box
    EntityHandle startVertex;

    //! starting element handle for this box
    EntityHandle startElem;

    //! lower and upper corners
    int boxDims[6];

    //! is locally periodic in i or j or k
    int locallyPeriodic[3];

    //! parallel data associated with this box, if any
    ScdParData parData;

    //! parameter extents
    HomCoord boxSize;

    //! convenience parameters, (boxSize[1]-1)*(boxSize[0]-1) and boxSize[0]-1
    int boxSizeIJ;
    int boxSizeIJM1;
    int boxSizeIM1;
};

inline ErrorCode ScdInterface::compute_partition( int np,
                                                  int nr,
                                                  const ScdParData& par_data,
                                                  int* ldims,
                                                  int* lperiodic,
                                                  int* pdims )
{
    ErrorCode rval = MB_SUCCESS;
    switch( par_data.partMethod )
    {
        case ScdParData::ALLJORKORI:
        case -1:
            rval = compute_partition_alljorkori( np, nr, par_data.gDims, par_data.gPeriodic, ldims, lperiodic, pdims );
            break;
        case ScdParData::ALLJKBAL:
            rval = compute_partition_alljkbal( np, nr, par_data.gDims, par_data.gPeriodic, ldims, lperiodic, pdims );
            break;
        case ScdParData::SQIJ:
            rval = compute_partition_sqij( np, nr, par_data.gDims, par_data.gPeriodic, ldims, lperiodic, pdims );
            break;
        case ScdParData::SQJK:
            rval = compute_partition_sqjk( np, nr, par_data.gDims, par_data.gPeriodic, ldims, lperiodic, pdims );
            break;
        case ScdParData::SQIJK:
            rval = compute_partition_sqijk( np, nr, par_data.gDims, par_data.gPeriodic, ldims, lperiodic, pdims );
            break;
        default:
            rval = MB_FAILURE;
            break;
    }

    return rval;
}

inline ErrorCode ScdInterface::compute_partition_alljorkori( int np,
                                                             int nr,
                                                             const int gijk[6],
                                                             const int* const gperiodic,
                                                             int* ldims,
                                                             int* lperiodic,
                                                             int* pijk )
{
    // partition *the elements* over the parametric space; 1d partition for now, in the j, k, or i
    // parameters
    int tmp_lp[3], tmp_pijk[3];
    if( !lperiodic ) lperiodic = tmp_lp;
    if( !pijk ) pijk = tmp_pijk;

    for( int i = 0; i < 3; i++ )
        lperiodic[i] = gperiodic[i];

    if( np == 1 )
    {
        if( ldims )
        {
            ldims[0] = gijk[0];
            ldims[3] = gijk[3];
            ldims[1] = gijk[1];
            ldims[4] = gijk[4];
            ldims[2] = gijk[2];
            ldims[5] = gijk[5];
        }
        pijk[0] = pijk[1] = pijk[2] = 1;
    }
    else
    {
        if( gijk[4] - gijk[1] > np )
        {
            // partition j over procs
            int dj    = ( gijk[4] - gijk[1] ) / np;
            int extra = ( gijk[4] - gijk[1] ) % np;
            ldims[1]  = gijk[1] + nr * dj + std::min( nr, extra );
            ldims[4]  = ldims[1] + dj + ( nr < extra ? 1 : 0 );

            if( gperiodic[1] && np > 1 )
            {
                lperiodic[1] = 0;
                ldims[4]++;
            }

            ldims[2] = gijk[2];
            ldims[5] = gijk[5];
            ldims[0] = gijk[0];
            ldims[3] = gijk[3];
            pijk[0] = pijk[2] = 1;
            pijk[1]           = np;
        }
        else if( gijk[5] - gijk[2] > np )
        {
            // partition k over procs
            int dk    = ( gijk[5] - gijk[2] ) / np;
            int extra = ( gijk[5] - gijk[2] ) % np;
            ldims[2]  = gijk[2] + nr * dk + std::min( nr, extra );
            ldims[5]  = ldims[2] + dk + ( nr < extra ? 1 : 0 );

            ldims[1] = gijk[1];
            ldims[4] = gijk[4];
            ldims[0] = gijk[0];
            ldims[3] = gijk[3];
            pijk[0] = pijk[1] = 1;
            pijk[2]           = np;
        }
        else if( gijk[3] - gijk[0] > np )
        {
            // partition i over procs
            int di    = ( gijk[3] - gijk[0] ) / np;
            int extra = ( gijk[3] - gijk[0] ) % np;
            ldims[0]  = gijk[0] + nr * di + std::min( nr, extra );
            ldims[3]  = ldims[0] + di + ( nr < extra ? 1 : 0 );

            if( gperiodic[0] && np > 1 )
            {
                lperiodic[0] = 0;
                ldims[3]++;
            }

            ldims[2] = gijk[2];
            ldims[5] = gijk[5];
            ldims[1] = gijk[1];
            ldims[4] = gijk[4];

            pijk[1] = pijk[2] = 1;
            pijk[0]           = np;
        }
        else
        {
            // Couldn't find a suitable partition...
            return MB_FAILURE;
        }
    }

    return MB_SUCCESS;
}

inline ErrorCode ScdInterface::compute_partition_alljkbal( int np,
                                                           int nr,
                                                           const int gijk[6],
                                                           const int* const gperiodic,
                                                           int* ldims,
                                                           int* lperiodic,
                                                           int* pijk )
{
    int tmp_lp[3], tmp_pijk[3];
    if( !lperiodic ) lperiodic = tmp_lp;
    if( !pijk ) pijk = tmp_pijk;

    for( int i = 0; i < 3; i++ )
        lperiodic[i] = gperiodic[i];

    if( np == 1 )
    {
        if( ldims )
        {
            ldims[0] = gijk[0];
            ldims[3] = gijk[3];
            ldims[1] = gijk[1];
            ldims[4] = gijk[4];
            ldims[2] = gijk[2];
            ldims[5] = gijk[5];
        }
        pijk[0] = pijk[1] = pijk[2] = 1;
    }
    else
    {
        // improved, possibly 2-d partition
        std::vector< double > kfactors;
        kfactors.push_back( 1 );
        int K = gijk[5] - gijk[2];
        for( int i = 2; i < K; i++ )
            if( !( K % i ) && !( np % i ) ) kfactors.push_back( i );
        kfactors.push_back( K );

        // compute the ideal nj and nk
        int J          = gijk[4] - gijk[1];
        double njideal = sqrt( ( (double)( np * J ) ) / ( (double)K ) );
        double nkideal = ( njideal * K ) / J;

        int nk, nj;
        if( nkideal < 1.0 )
        {
            nk = 1;
            nj = np;
        }
        else
        {
            std::vector< double >::iterator vit = std::lower_bound( kfactors.begin(), kfactors.end(), nkideal );
            if( vit == kfactors.begin() )
                nk = 1;
            else
                nk = (int)*( --vit );
            nj = np / nk;
        }

        int dk = K / nk;
        int dj = J / nj;

        ldims[2] = gijk[2] + ( nr % nk ) * dk;
        ldims[5] = ldims[2] + dk;

        int extra = J % nj;

        ldims[1] = gijk[1] + ( nr / nk ) * dj + std::min( nr / nk, extra );
        ldims[4] = ldims[1] + dj + ( nr / nk < extra ? 1 : 0 );

        ldims[0] = gijk[0];
        ldims[3] = gijk[3];

        if( gperiodic[1] && np > 1 )
        {
            lperiodic[1] = 0;
            if( nr / nk == nj - 1 )
            {
                ldims[1]++;
            }
        }

        pijk[0] = 1;
        pijk[1] = nj;
        pijk[2] = nk;
    }

    return MB_SUCCESS;
}

inline ErrorCode ScdInterface::compute_partition_sqij( int np,
                                                       int nr,
                                                       const int gijk[6],
                                                       const int* const gperiodic,
                                                       int* ldims,
                                                       int* lperiodic,
                                                       int* pijk )
{
    int tmp_lp[3], tmp_pijk[3];
    if( !lperiodic ) lperiodic = tmp_lp;
    if( !pijk ) pijk = tmp_pijk;

    // square IxJ partition

    for( int i = 0; i < 3; i++ )
        lperiodic[i] = gperiodic[i];

    if( np == 1 )
    {
        if( ldims )
        {
            ldims[0] = gijk[0];
            ldims[3] = gijk[3];
            ldims[1] = gijk[1];
            ldims[4] = gijk[4];
            ldims[2] = gijk[2];
            ldims[5] = gijk[5];
        }
        pijk[0] = pijk[1] = pijk[2] = 1;
    }
    else
    {
        std::vector< double > pfactors, ppfactors;
        for( int i = 2; i <= np / 2; i++ )
            if( !( np % i ) )
            {
                pfactors.push_back( i );
                ppfactors.push_back( ( (double)( i * i ) ) / np );
            }
        pfactors.push_back( np );
        ppfactors.push_back( (double)np );

        // ideally, Px/Py = I/J
        double ijratio = ( (double)( gijk[3] - gijk[0] ) ) / ( (double)( gijk[4] - gijk[1] ) );

        unsigned int ind                        = 0;
        std::vector< double >::iterator optimal = std::lower_bound( ppfactors.begin(), ppfactors.end(), ijratio );
        if( optimal == ppfactors.end() )
        {
            ind = ppfactors.size() - 1;
        }
        else
        {
            ind = optimal - ppfactors.begin();
            if( ind && fabs( ppfactors[ind - 1] - ijratio ) < fabs( ppfactors[ind] - ijratio ) ) ind--;
        }

        // VARIABLES DESCRIBING THE MESH:
        // pi, pj = # procs in i and j directions
        // nri, nrj = my proc's position in i, j directions
        // I, J = # edges/elements in i, j directions
        // iextra, jextra = # procs having extra edge in i/j direction
        // top_i, top_j = if true, I'm the last proc in the i/j direction
        // i, j = # edges locally in i/j direction, *not* including one for iextra/jextra
        int pi = pfactors[ind];
        int pj = np / pi;

        int I = ( gijk[3] - gijk[0] ), J = ( gijk[4] - gijk[1] );
        int iextra = I % pi, jextra = J % pj, i = I / pi, j = J / pj;
        int nri = nr % pi, nrj = nr / pi;

        if( ldims )
        {
            ldims[0] = gijk[0] + i * nri + std::min( iextra, nri );
            ldims[3] = ldims[0] + i + ( nri < iextra ? 1 : 0 );
            ldims[1] = gijk[1] + j * nrj + std::min( jextra, nrj );
            ldims[4] = ldims[1] + j + ( nrj < jextra ? 1 : 0 );

            ldims[2] = gijk[2];
            ldims[5] = gijk[5];

            if( gperiodic[0] && pi > 1 )
            {
                lperiodic[0] = 0;
                if( nri == pi - 1 ) ldims[3]++;
            }
            if( gperiodic[1] && pj > 1 )
            {
                lperiodic[1] = 0;
                if( nrj == pj - 1 ) ldims[4]++;
            }
        }

        pijk[0] = pi;
        pijk[1] = pj;
        pijk[2] = 1;
    }

    return MB_SUCCESS;
}

inline ErrorCode ScdInterface::compute_partition_sqjk( int np,
                                                       int nr,
                                                       const int gijk[6],
                                                       const int* const gperiodic,
                                                       int* ldims,
                                                       int* lperiodic,
                                                       int* pijk )
{
    int tmp_lp[3], tmp_pijk[3];
    if( !lperiodic ) lperiodic = tmp_lp;
    if( !pijk ) pijk = tmp_pijk;

    // square JxK partition
    for( int i = 0; i < 3; i++ )
        lperiodic[i] = gperiodic[i];

    if( np == 1 )
    {
        if( ldims )
        {
            ldims[0] = gijk[0];
            ldims[3] = gijk[3];
            ldims[1] = gijk[1];
            ldims[4] = gijk[4];
            ldims[2] = gijk[2];
            ldims[5] = gijk[5];
        }
        pijk[0] = pijk[1] = pijk[2] = 1;
    }
    else
    {
        std::vector< double > pfactors, ppfactors;
        for( int p = 2; p <= np; p++ )
            if( !( np % p ) )
            {
                pfactors.push_back( p );
                ppfactors.push_back( ( (double)( p * p ) ) / np );
            }

        // ideally, Pj/Pk = J/K
        int pj, pk;
        if( gijk[5] == gijk[2] )
        {
            pk = 1;
            pj = np;
        }
        else
        {
            double jkratio = ( (double)( gijk[4] - gijk[1] ) ) / ( (double)( gijk[5] - gijk[2] ) );

            std::vector< double >::iterator vit = std::lower_bound( ppfactors.begin(), ppfactors.end(), jkratio );
            if( vit == ppfactors.end() )
                --vit;
            else if( vit != ppfactors.begin() && fabs( *( vit - 1 ) - jkratio ) < fabs( ( *vit ) - jkratio ) )
                --vit;
            int ind = vit - ppfactors.begin();

            pj = 1;
            if( ind >= 0 && !pfactors.empty() ) pfactors[ind];
            pk = np / pj;
        }

        int K = ( gijk[5] - gijk[2] ), J = ( gijk[4] - gijk[1] );
        int jextra = J % pj, kextra = K % pk, j = J / pj, k = K / pk;
        int nrj = nr % pj, nrk = nr / pj;
        ldims[1] = gijk[1] + j * nrj + std::min( jextra, nrj );
        ldims[4] = ldims[1] + j + ( nrj < jextra ? 1 : 0 );
        ldims[2] = gijk[2] + k * nrk + std::min( kextra, nrk );
        ldims[5] = ldims[2] + k + ( nrk < kextra ? 1 : 0 );

        ldims[0] = gijk[0];
        ldims[3] = gijk[3];

        if( gperiodic[1] && pj > 1 )
        {
            lperiodic[1] = 0;
            if( nrj == pj - 1 ) ldims[4]++;
        }

        pijk[0] = 1;
        pijk[1] = pj;
        pijk[2] = pk;
    }

    return MB_SUCCESS;
}

inline ErrorCode ScdInterface::compute_partition_sqijk( int np,
                                                        int nr,
                                                        const int* const gijk,
                                                        const int* const gperiodic,
                                                        int* ldims,
                                                        int* lperiodic,
                                                        int* pijk )
{
    if( gperiodic[0] || gperiodic[1] || gperiodic[2] ) return MB_FAILURE;

    int tmp_lp[3], tmp_pijk[3];
    if( !lperiodic ) lperiodic = tmp_lp;
    if( !pijk ) pijk = tmp_pijk;

    // square IxJxK partition

    for( int i = 0; i < 3; i++ )
        lperiodic[i] = gperiodic[i];

    if( np == 1 )
    {
        if( ldims )
            for( int i = 0; i < 6; i++ )
                ldims[i] = gijk[i];
        pijk[0] = pijk[1] = pijk[2] = 1;
        return MB_SUCCESS;
    }

    std::vector< int > pfactors;
    pfactors.push_back( 1 );
    for( int i = 2; i <= np / 2; i++ )
        if( !( np % i ) ) pfactors.push_back( i );
    pfactors.push_back( np );

    // test for IJ, JK, IK
    int IJK[3], dIJK[3];
    for( int i = 0; i < 3; i++ )
        IJK[i] = std::max( gijk[3 + i] - gijk[i], 1 );
    // order IJK from lo to hi
    int lo = 0, hi = 0;
    for( int i = 1; i < 3; i++ )
    {
        if( IJK[i] < IJK[lo] ) lo = i;
        if( IJK[i] > IJK[hi] ) hi = i;
    }
    if( lo == hi ) hi = ( lo + 1 ) % 3;
    int mid = 3 - lo - hi;
    // search for perfect subdivision of np that balances #cells
    int perfa_best = -1, perfb_best = -1;
    double ratio = 0.0;
    for( int po = 0; po < (int)pfactors.size(); po++ )
    {
        for( int pi = po; pi < (int)pfactors.size() && np / ( pfactors[po] * pfactors[pi] ) >= pfactors[pi]; pi++ )
        {
            int p3 =
                std::find( pfactors.begin(), pfactors.end(), np / ( pfactors[po] * pfactors[pi] ) ) - pfactors.begin();
            if( p3 == (int)pfactors.size() || pfactors[po] * pfactors[pi] * pfactors[p3] != np )
                continue;  // po*pi should exactly factor np
            assert( po <= pi && pi <= p3 );
            // by definition, po <= pi <= p3
            double minl =
                       std::min( std::min( IJK[lo] / pfactors[po], IJK[mid] / pfactors[pi] ), IJK[hi] / pfactors[p3] ),
                   maxl =
                       std::max( std::max( IJK[lo] / pfactors[po], IJK[mid] / pfactors[pi] ), IJK[hi] / pfactors[p3] );
            if( minl / maxl > ratio )
            {
                ratio      = minl / maxl;
                perfa_best = po;
                perfb_best = pi;
            }
        }
    }
    if( perfa_best == -1 || perfb_best == -1 ) return MB_FAILURE;

    // VARIABLES DESCRIBING THE MESH:
    // pijk[i] = # procs in direction i
    // numr[i] = my proc's position in direction i
    // dIJK[i] = # edges/elements in direction i
    // extra[i]= # procs having extra edge in direction i
    // top[i] = if true, I'm the last proc in direction i

    pijk[lo]     = pfactors[perfa_best];
    pijk[mid]    = pfactors[perfb_best];
    pijk[hi]     = ( np / ( pfactors[perfa_best] * pfactors[perfb_best] ) );
    int extra[3] = { 0, 0, 0 }, numr[3];
    for( int i = 0; i < 3; i++ )
    {
        dIJK[i]  = IJK[i] / pijk[i];
        extra[i] = IJK[i] % pijk[i];
    }
    numr[2] = nr / ( pijk[0] * pijk[1] );
    int rem = nr % ( pijk[0] * pijk[1] );
    numr[1] = rem / pijk[0];
    numr[0] = rem % pijk[0];
    for( int i = 0; i < 3; i++ )
    {
        extra[i]     = IJK[i] % dIJK[i];
        ldims[i]     = gijk[i] + numr[i] * dIJK[i] + std::min( extra[i], numr[i] );
        ldims[3 + i] = ldims[i] + dIJK[i] + ( numr[i] < extra[i] ? 1 : 0 );
    }

    return MB_SUCCESS;
}

inline int ScdInterface::gtol( const int* gijk, int i, int j, int k )
{
    return ( ( k - gijk[2] ) * ( gijk[3] - gijk[0] + 1 ) * ( gijk[4] - gijk[1] + 1 ) +
             ( j - gijk[1] ) * ( gijk[3] - gijk[0] + 1 ) + i - gijk[0] );
}

inline ErrorCode ScdInterface::get_indices( const int* const ldims,
                                            const int* const rdims,
                                            const int* const across_bdy,
                                            int* face_dims,
                                            std::vector< int >& shared_indices )
{
    // check for going across periodic bdy and face_dims not in my ldims (I'll always be on top in
    // that case)...
    if( across_bdy[0] > 0 && face_dims[0] != ldims[3] )
        face_dims[0] = face_dims[3] = ldims[3];
    else if( across_bdy[0] < 0 && face_dims[0] != ldims[0] )
        face_dims[0] = face_dims[3] = ldims[0];
    if( across_bdy[1] > 0 && face_dims[1] != ldims[4] )
        face_dims[1] = face_dims[4] = ldims[4];
    else if( across_bdy[1] < 0 && face_dims[1] != ldims[1] )
        face_dims[0] = face_dims[3] = ldims[1];

    for( int k = face_dims[2]; k <= face_dims[5]; k++ )
        for( int j = face_dims[1]; j <= face_dims[4]; j++ )
            for( int i = face_dims[0]; i <= face_dims[3]; i++ )
                shared_indices.push_back( gtol( ldims, i, j, k ) );

    if( across_bdy[0] > 0 && face_dims[0] != rdims[0] )
        face_dims[0] = face_dims[3] = rdims[0];
    else if( across_bdy[0] < 0 && face_dims[0] != rdims[3] )
        face_dims[0] = face_dims[3] = rdims[3];
    if( across_bdy[1] > 0 && face_dims[1] != rdims[1] )
        face_dims[1] = face_dims[4] = rdims[1];
    else if( across_bdy[1] < 0 && face_dims[1] != rdims[4] )
        face_dims[0] = face_dims[3] = rdims[4];

    for( int k = face_dims[2]; k <= face_dims[5]; k++ )
        for( int j = face_dims[1]; j <= face_dims[4]; j++ )
            for( int i = face_dims[0]; i <= face_dims[3]; i++ )
                shared_indices.push_back( gtol( rdims, i, j, k ) );

    return MB_SUCCESS;
}

inline ErrorCode ScdInterface::get_neighbor( int np,
                                             int pfrom,
                                             const ScdParData& spd,
                                             const int* const dijk,
                                             int& pto,
                                             int* rdims,
                                             int* facedims,
                                             int* across_bdy )
{
    if( !dijk[0] && !dijk[1] && !dijk[2] )
    {
        // not going anywhere, return
        pto = -1;
        return MB_SUCCESS;
    }

    switch( spd.partMethod )
    {
        case ScdParData::ALLJORKORI:
        case -1:
            return get_neighbor_alljorkori( np, pfrom, spd.gDims, spd.gPeriodic, dijk, pto, rdims, facedims,
                                            across_bdy );
        case ScdParData::ALLJKBAL:
            return get_neighbor_alljkbal( np, pfrom, spd.gDims, spd.gPeriodic, dijk, pto, rdims, facedims, across_bdy );
        case ScdParData::SQIJ:
            return get_neighbor_sqij( np, pfrom, spd.gDims, spd.gPeriodic, dijk, pto, rdims, facedims, across_bdy );
        case ScdParData::SQJK:
            return get_neighbor_sqjk( np, pfrom, spd.gDims, spd.gPeriodic, dijk, pto, rdims, facedims, across_bdy );
        case ScdParData::SQIJK:
            return get_neighbor_sqijk( np, pfrom, spd.gDims, spd.gPeriodic, dijk, pto, rdims, facedims, across_bdy );
        default:
            break;
    }

    return MB_FAILURE;
}

inline ErrorCode ScdInterface::tag_shared_vertices( ParallelComm* pcomm, EntityHandle seth )
{
    ScdBox* box = get_scd_box( seth );
    if( !box )
    {
        // look for contained boxes
        Range tmp_range;
        ErrorCode rval = mbImpl->get_entities_by_type( seth, MBENTITYSET, tmp_range );
        if( MB_SUCCESS != rval ) return rval;
        for( Range::iterator rit = tmp_range.begin(); rit != tmp_range.end(); ++rit )
        {
            box = get_scd_box( *rit );
            if( box ) break;
        }
    }

    if( !box ) return MB_FAILURE;

    return tag_shared_vertices( pcomm, box );
}

inline ScdInterface* ScdBox::sc_impl() const
{
    return scImpl;
}

inline EntityHandle ScdBox::start_vertex() const
{
    return startVertex;
}

inline void ScdBox::start_vertex( EntityHandle startv )
{
    startVertex = startv;
}

inline EntityHandle ScdBox::start_element() const
{
    return startElem;
}

inline void ScdBox::start_element( EntityHandle starte )
{
    startElem = starte;
}

inline int ScdBox::num_elements() const
{
    if( !startElem ) return 0;  // not initialized yet

    /* for a structured mesh, total number of elements is obtained by multiplying
        number of elements in each direction
      number of elements in each direction is given by number of vertices in that direction minus 1
      if periodic in that direction, the last vertex is the same as first one, count one more
      element
      */
    int num_e_i = ( -1 == boxSize[0] || 1 == boxSize[0] ) ? 1 : boxSize[0] - 1;
    if( locallyPeriodic[0] ) ++num_e_i;

    int num_e_j = ( -1 == boxSize[1] || 1 == boxSize[1] ) ? 1 : boxSize[1] - 1;
    if( locallyPeriodic[1] ) ++num_e_j;

    int num_e_k = ( -1 == boxSize[2] || 1 == boxSize[2] ) ? 1 : boxSize[2] - 1;
    if( locallyPeriodic[2] ) ++num_e_k;

    return num_e_i * num_e_j * num_e_k;
}

inline int ScdBox::num_vertices() const
{
    return boxSize[0] * ( !boxSize[1] ? 1 : boxSize[1] ) * ( !boxSize[2] ? 1 : boxSize[2] );
}

inline const int* ScdBox::box_dims() const
{
    return boxDims;
}

inline HomCoord ScdBox::box_min() const
{
    return HomCoord( boxDims, 3 );
}

inline HomCoord ScdBox::box_max() const
{
    return HomCoord( boxDims + 3, 3 );
}

inline HomCoord ScdBox::box_size() const
{
    return boxSize;
}

inline void ScdBox::box_size( int* ijk ) const
{
    ijk[0] = boxSize[0];
    ijk[1] = boxSize[1];
    ijk[2] = boxSize[2];
}

inline void ScdBox::box_size( int& i, int& j, int& k ) const
{
    i = boxSize[0];
    j = boxSize[1];
    k = boxSize[2];
}

inline EntityHandle ScdBox::get_element( int i, int j, int k ) const
{
    return ( !startElem
                 ? 0
                 : startElem + ( k - boxDims[2] ) * boxSizeIJM1 + ( j - boxDims[1] ) * boxSizeIM1 + i - boxDims[0] );
}

inline EntityHandle ScdBox::get_element( const HomCoord& ijk ) const
{
    return get_element( ijk[0], ijk[1], ijk[2] );
}

inline EntityHandle ScdBox::get_vertex( int i, int j, int k ) const
{
    return ( vertDat
                 ? startVertex + ( boxDims[2] == -1 && boxDims[5] == -1 ? 0 : ( k - boxDims[2] ) ) * boxSizeIJ +
                       ( boxDims[1] == -1 && boxDims[4] == -1 ? 0 : ( j - boxDims[1] ) ) * boxSize[0] + i - boxDims[0]
                 : get_vertex_from_seq( i, j, k ) );
}

inline EntityHandle ScdBox::get_vertex( const HomCoord& ijk ) const
{
    return get_vertex( ijk[0], ijk[1], ijk[2] );
}

inline bool ScdBox::contains( const HomCoord& ijk ) const
{
    return ( ijk >= HomCoord( boxDims, 3 ) && ijk <= HomCoord( boxDims + 3, 3 ) );
}

inline bool ScdBox::contains( int i, int j, int k ) const
{
    return contains( HomCoord( i, j, k ) );
}

inline void ScdBox::box_set( EntityHandle this_set )
{
    boxSet = this_set;
}

inline EntityHandle ScdBox::box_set()
{
    return boxSet;
}

inline ScdVertexData* ScdBox::vert_dat() const
{
    return vertDat;
}

inline StructuredElementSeq* ScdBox::elem_seq() const
{
    return elemSeq;
}

inline ErrorCode ScdBox::get_params( EntityHandle ent, int& i, int& j, int& k, int& dir ) const
{
    HomCoord hc;
    ErrorCode rval = get_params( ent, hc );
    if( MB_SUCCESS == rval )
    {
        i   = hc[0];
        j   = hc[1];
        k   = hc[2];
        dir = hc[3];
    }

    return rval;
}

inline ErrorCode ScdBox::get_params( EntityHandle ent, int& i, int& j, int& k ) const
{
    HomCoord hc;
    ErrorCode rval = get_params( ent, hc );
    if( MB_SUCCESS == rval )
    {
        i = hc[0];
        j = hc[1];
        k = hc[2];
    }

    return rval;
}

inline bool ScdBox::locally_periodic_i() const
{
    return locallyPeriodic[0];
}

inline bool ScdBox::locally_periodic_j() const
{
    return locallyPeriodic[1];
}

inline bool ScdBox::locally_periodic_k() const
{
    return locallyPeriodic[2];
}

inline void ScdBox::locally_periodic( bool lperiodic[3] )
{
    for( int i = 0; i < 3; i++ )
        locallyPeriodic[i] = lperiodic[i];
}

inline const int* ScdBox::locally_periodic() const
{
    return locallyPeriodic;
}

std::ostream& operator<<( std::ostream& str, const ScdParData& pd );

}  // namespace moab
#endif