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
/**
 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
 * storing and accessing finite element mesh data.
 *
 * Copyright 2004 Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
 * retains certain rights in this software.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 */

// MOAB performance tests building mapped mesh with nodes and
// hexes created one at a time.  This also creates the node to hex adjacencies.

// Different platforms follow different conventions for usage
#if !defined( _MSC_VER ) && !defined( __MINGW32__ )
#include <sys/resource.h>
#else
#include <time.h>
#endif
#ifdef SOLARIS
extern "C" int getrusage( int, struct rusage* );
#ifndef RUSAGE_SELF
#include </usr/ucbinclude/sys/rusage.h>
#endif
#endif

#ifndef IS_BUILDING_MB
#define IS_BUILDING_MB
#endif

#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <iostream>
#include "moab/Core.hpp"
#include "moab/ReadUtilIface.hpp"
#include "VertexSequence.hpp"
#include "StructuredElementSeq.hpp"
#include "EntitySequence.hpp"
#include "SequenceManager.hpp"
#include "moab/HomXform.hpp"
#include "moab/SetIterator.hpp"

using namespace moab;

double LENGTH = 1.0;

void testA( const int nelem, const double* coords );
void testB( const int nelem, const double* coords, int* connect );
void testC( const int nelem, const double* coords );
void testD( const int nelem, const double* coords, int ver );
void testE( const int nelem, const double* coords, int* connect );
void print_time( const bool print_em, double& tot_time, double& utime, double& stime, long& imem, long& rmem );
void query_vert_to_elem();
void query_elem_to_vert();
void query_struct_elem_to_vert();
void query_elem_to_vert_direct();
void query_vert_to_elem_direct();
ErrorCode normalize_elems( double* coords );
void check_answers( const char* );

#define RC( msg )                                            \
    if( MB_SUCCESS != result ) do                            \
        {                                                    \
            std::cout << "FAIL in " << ( msg ) << std::endl; \
            return;                                          \
    } while( true )
#define RR( msg )                                            \
    if( MB_SUCCESS != result ) do                            \
        {                                                    \
            std::cout << "FAIL in " << ( msg ) << std::endl; \
            return result;                                   \
    } while( true )

void compute_edge( double* start, const int nelem, const double xint, const int stride )
{
    for( int i = 1; i < nelem; i++ )
    {
        start[i * stride]                     = start[0] + i * xint;
        start[nelem + 1 + i * stride]         = start[nelem + 1] + i * xint;
        start[2 * ( nelem + 1 ) + i * stride] = start[2 * ( nelem + 1 )] + i * xint;
    }
}

void compute_face( double* a, const int nelem, const double xint, const int stride1, const int stride2 )
{
    // 2D TFI on a face starting at a, with strides stride1 in ada and stride2 in tse
    for( int j = 1; j < nelem; j++ )
    {
        double tse = j * xint;
        for( int i = 1; i < nelem; i++ )
        {
            double ada = i * xint;

            a[i * stride1 + j * stride2] =
                ( 1.0 - ada ) * a[i * stride1] + ada * a[i * stride1 + nelem * stride2] +
                ( 1.0 - tse ) * a[j * stride2] + tse * a[j * stride2 + nelem * stride1] -
                ( 1.0 - tse ) * ( 1.0 - ada ) * a[0] - ( 1.0 - tse ) * ada * a[nelem * stride1] -
                tse * ( 1.0 - ada ) * a[nelem * stride2] - tse * ada * a[nelem * ( stride1 + stride2 )];
            a[nelem + 1 + i * stride1 + j * stride2] =
                ( 1.0 - ada ) * a[nelem + 1 + i * stride1] + ada * a[nelem + 1 + i * stride1 + nelem * stride2] +
                ( 1.0 - tse ) * a[nelem + 1 + j * stride2] + tse * a[nelem + 1 + j * stride2 + nelem * stride1] -
                ( 1.0 - tse ) * ( 1.0 - ada ) * a[nelem + 1 + 0] -
                ( 1.0 - tse ) * ada * a[nelem + 1 + nelem * stride1] -
                tse * ( 1.0 - ada ) * a[nelem + 1 + nelem * stride2] -
                tse * ada * a[nelem + 1 + nelem * ( stride1 + stride2 )];
            a[2 * ( nelem + 1 ) + i * stride1 + j * stride2] =
                ( 1.0 - ada ) * a[2 * ( nelem + 1 ) + i * stride1] +
                ada * a[2 * ( nelem + 1 ) + i * stride1 + nelem * stride2] +
                ( 1.0 - tse ) * a[2 * ( nelem + 1 ) + j * stride2] +
                tse * a[2 * ( nelem + 1 ) + j * stride2 + nelem * stride1] -
                ( 1.0 - tse ) * ( 1.0 - ada ) * a[2 * ( nelem + 1 ) + 0] -
                ( 1.0 - tse ) * ada * a[2 * ( nelem + 1 ) + nelem * stride1] -
                tse * ( 1.0 - ada ) * a[2 * ( nelem + 1 ) + nelem * stride2] -
                tse * ada * a[2 * ( nelem + 1 ) + nelem * ( stride1 + stride2 )];
        }
    }
}

void build_coords( const int nelem, double*& coords )
{
    double ttime0 = 0.0, ttime1 = 0.0, utime1 = 0.0, stime1 = 0.0;
    long imem = 0, rmem = 0;
    print_time( false, ttime0, utime1, stime1, imem, rmem );
    // allocate the memory
    int numv     = nelem + 1;
    int numv_sq  = numv * numv;
    int tot_numv = numv * numv * numv;
    coords       = new double[3 * tot_numv];

// use FORTRAN-like indexing
#define VINDEX( i, j, k ) ( ( i ) + ( (j)*numv ) + ( (k)*numv_sq ) )
    int idx;<--- Unused variable: idx
    double scale1, scale2, scale3;
    // use these to prevent optimization on 1-scale, etc (real map wouldn't have
    // all these equal)
    scale1 = LENGTH / nelem;
    scale2 = LENGTH / nelem;
    scale3 = LENGTH / nelem;

#ifdef REALTFI
    // use a real TFI xform to compute coordinates
    // compute edges
    // i (stride=1)
    compute_edge( &coords[VINDEX( 0, 0, 0 )], nelem, scale1, 1 );
    compute_edge( &coords[VINDEX( 0, nelem, 0 )], nelem, scale1, 1 );
    compute_edge( &coords[VINDEX( 0, 0, nelem )], nelem, scale1, 1 );
    compute_edge( &coords[VINDEX( 0, nelem, nelem )], nelem, scale1, 1 );
    // j (stride=numv)
    compute_edge( &coords[VINDEX( 0, 0, 0 )], nelem, scale1, numv );
    compute_edge( &coords[VINDEX( nelem, 0, 0 )], nelem, scale1, numv );
    compute_edge( &coords[VINDEX( 0, 0, nelem )], nelem, scale1, numv );
    compute_edge( &coords[VINDEX( nelem, 0, nelem )], nelem, scale1, numv );
    // k (stride=numv^2)
    compute_edge( &coords[VINDEX( 0, 0, 0 )], nelem, scale1, numv_sq );
    compute_edge( &coords[VINDEX( nelem, 0, 0 )], nelem, scale1, numv_sq );
    compute_edge( &coords[VINDEX( 0, nelem, 0 )], nelem, scale1, numv_sq );
    compute_edge( &coords[VINDEX( nelem, nelem, 0 )], nelem, scale1, numv_sq );

    // compute faces
    // i=0, nelem
    compute_face( &coords[VINDEX( 0, 0, 0 )], nelem, scale1, numv, numv_sq );
    compute_face( &coords[VINDEX( nelem, 0, 0 )], nelem, scale1, numv, numv_sq );
    // j=0, nelem
    compute_face( &coords[VINDEX( 0, 0, 0 )], nelem, scale1, 1, numv_sq );
    compute_face( &coords[VINDEX( 0, nelem, 0 )], nelem, scale1, 1, numv_sq );
    // k=0, nelem
    compute_face( &coords[VINDEX( 0, 0, 0 )], nelem, scale1, 1, numv );
    compute_face( &coords[VINDEX( 0, 0, nelem )], nelem, scale1, 1, numv );

    // initialize corner indices
    int i000 = VINDEX( 0, 0, 0 );
    int ia00 = VINDEX( nelem, 0, 0 );
    int i0t0 = VINDEX( 0, nelem, 0 );
    int iat0 = VINDEX( nelem, nelem, 0 );
    int i00g = VINDEX( 0, 0, nelem );
    int ia0g = VINDEX( nelem, 0, nelem );
    int i0tg = VINDEX( 0, nelem, nelem );
    int iatg = VINDEX( nelem, nelem, nelem );
    double cX, cY, cZ;
    int adaInts   = nelem;
    int tseInts   = nelem;
    int gammaInts = nelem;

    for( int i = 1; i < nelem; i++ )
    {
        for( int j = 1; j < nelem; j++ )
        {
            for( int k = 1; k < nelem; k++ )
            {
                // idx = VINDEX(i,j,k);
                double tse   = i * scale1;
                double ada   = j * scale2;
                double gamma = k * scale3;
                double tm1   = 1.0 - tse;
                double am1   = 1.0 - ada;
                double gm1   = 1.0 - gamma;

                cX = gm1 * ( am1 * ( tm1 * coords[i000] + tse * coords[i0t0] ) +
                             ada * ( tm1 * coords[ia00] + tse * coords[iat0] ) ) +
                     gamma * ( am1 * ( tm1 * coords[i00g] + tse * coords[i0tg] ) +
                               ada * ( tm1 * coords[ia0g] + tse * coords[iatg] ) );

                cY = gm1 * ( am1 * ( tm1 * coords[i000] + tse * coords[i0t0] ) +
                             ada * ( tm1 * coords[ia00] + tse * coords[iat0] ) ) +
                     gamma * ( am1 * ( tm1 * coords[i00g] + tse * coords[i0tg] ) +
                               ada * ( tm1 * coords[ia0g] + tse * coords[iatg] ) );

                cZ = gm1 * ( am1 * ( tm1 * coords[i000] + tse * coords[i0t0] ) +
                             ada * ( tm1 * coords[ia00] + tse * coords[iat0] ) ) +
                     gamma * ( am1 * ( tm1 * coords[i00g] + tse * coords[i0tg] ) +
                               ada * ( tm1 * coords[ia0g] + tse * coords[iatg] ) );

                double* ai0k = &coords[VINDEX( k, 0, i )];
                double* aiak = &coords[VINDEX( k, adaInts, i )];
                double* a0jk = &coords[VINDEX( k, j, 0 )];
                double* atjk = &coords[VINDEX( k, j, tseInts )];
                double* aij0 = &coords[VINDEX( 0, j, i )];
                double* aijg = &coords[VINDEX( gammaInts, j, i )];

                coords[VINDEX( i, j, k )] = ( am1 * ai0k[0] + ada * aiak[0] + tm1 * a0jk[0] + tse * atjk[0] +
                                              gm1 * aij0[0] + gamma * aijg[0] ) /
                                                2.0 -
                                            cX / 2.0;

                coords[nelem + 1 + VINDEX( i, j, k )] =
                    ( am1 * ai0k[nelem + 1] + ada * aiak[nelem + 1] + tm1 * a0jk[nelem + 1] + tse * atjk[nelem + 1] +
                      gm1 * aij0[nelem + 1] + gamma * aijg[nelem + 1] ) /
                        2.0 -
                    cY / 2.0;

                coords[2 * ( nelem + 1 ) + VINDEX( i, j, k )] =
                    ( am1 * ai0k[2 * ( nelem + 1 )] + ada * aiak[2 * ( nelem + 1 )] + tm1 * a0jk[2 * ( nelem + 1 )] +
                      tse * atjk[2 * ( nelem + 1 )] + gm1 * aij0[2 * ( nelem + 1 )] +
                      gamma * aijg[2 * ( nelem + 1 )] ) /
                        2.0 -
                    cZ / 2.0;
            }
        }
    }

#else
    for( int i = 0; i < numv; i++ )
    {
        for( int j = 0; j < numv; j++ )
        {
            for( int k = 0; k < numv; k++ )
            {
                idx = VINDEX( i, j, k );
                // blocked coordinate ordering
                coords[idx]                = i * scale1;
                coords[tot_numv + idx]     = j * scale2;
                coords[2 * tot_numv + idx] = k * scale3;
            }
        }
    }
#endif
    print_time( false, ttime1, utime1, stime1, imem, rmem );
    //  std::cout << "MOAB: TFI time = " << ttime1-ttime0 << " sec"
    //            << std::endl;
}

void build_connect( const int nelem, const EntityHandle /*vstart*/, int*& connect )
{
    // allocate the memory
    int nume_tot = nelem * nelem * nelem;
    connect      = new int[8 * nume_tot];

    EntityHandle vijk;
    int numv    = nelem + 1;
    int numv_sq = numv * numv;
    int idx     = 0;
    for( int i = 0; i < nelem; i++ )
    {
        for( int j = 0; j < nelem; j++ )
        {
            for( int k = 0; k < nelem; k++ )
            {
                vijk           = VINDEX( i, j, k );
                connect[idx++] = vijk;
                connect[idx++] = vijk + 1;
                connect[idx++] = vijk + 1 + numv;
                connect[idx++] = vijk + numv;
                connect[idx++] = vijk + numv * numv;
                connect[idx++] = vijk + 1 + numv * numv;
                connect[idx++] = vijk + 1 + numv + numv * numv;
                connect[idx++] = vijk + numv + numv * numv;
                assert( i <= numv * numv * numv );
            }
        }
    }
}

Interface* gMB;
Tag pos_tag, pos2_tag;

void init()
{
    gMB               = new Core();
    double def_val[3] = { 0.0, 0.0, 0.0 };
    ErrorCode rval =
        gMB->tag_get_handle( "position_tag", 3, MB_TYPE_DOUBLE, pos_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
    assert( MB_SUCCESS == rval );
    if( rval )
    {
    }  // empty line to remove compiler warning
    rval = gMB->tag_get_handle( "position2_tag", 3, MB_TYPE_DOUBLE, pos2_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
    assert( MB_SUCCESS == rval );
}

int main( int argc, char* argv[] )
{
    int nelem = 20;
    if( argc < 3 )<--- Assuming that condition 'argc<3' is not redundant<--- Assuming that condition 'argc<3' is not redundant
    {
        std::cout << "Usage: " << argv[0] << " <ints_per_side> [A|B|C|D [1|2|3|4]|E]" << std::endl;
        return 1;
    }

    char which_test = '\0';
    int ver         = 0;

    sscanf( argv[1], "%d", &nelem );
    if( argc >= 3 ) sscanf( argv[2], "%c", &which_test );<--- Condition 'argc>=3' is always true
    if( argc >= 4 ) sscanf( argv[3], "%d", &ver );

    if( 3 <= argc && which_test != 'A' && which_test != 'B' && which_test != 'C' && which_test != 'D' &&<--- Condition '3<=argc' is always true
        which_test != 'E' )
    {
        std::cout << "Must indicate A or B, C, D or E for test." << std::endl;
        return 1;
    }

    if( 4 <= argc && which_test == 'D' && ( ver < 1 || ver > 4 ) )
    {
        std::cout << "Must indicate version 1, 2, 3, or 4 for test D." << std::endl;
        return 1;
    }

    //  std::cout << "number of elements: " << nelem << "; test "
    //            << which_test << std::endl;

    // pre-build the coords array
    double* coords = NULL;
    build_coords( nelem, coords );
    assert( NULL != coords );

    int* connect = NULL;

    // test A: create structured mesh
    if( '\0' == which_test || 'A' == which_test ) testA( nelem, coords );

    build_connect( nelem, 1, connect );

    // test B: create mesh using bulk interface
    if( '\0' == which_test || 'B' == which_test ) testB( nelem, coords, connect );

    // test C: create mesh using individual interface
    if( '\0' == which_test || 'C' == which_test ) testC( nelem, coords );

    // test D: query mesh using iterators
    if( '\0' == which_test || 'D' == which_test ) testD( nelem, coords, ver );

    // test E: query mesh using direct access
    if( '\0' == which_test || 'E' == which_test ) testE( nelem, coords, connect );

    return 0;
}

void query_elem_to_vert()
{
    Range all_hexes;
    ErrorCode result = gMB->get_entities_by_type( 0, MBHEX, all_hexes );
    RC( "query_elem_to_vert" );
    const EntityHandle* connect;
    int num_connect;
    double dum_coords[24];
    for( Range::iterator eit = all_hexes.begin(); eit != all_hexes.end(); ++eit )
    {
        result = gMB->get_connectivity( *eit, connect, num_connect );
        RC( "query_elem_to_vert" );
        result = gMB->get_coords( connect, num_connect, dum_coords );
        RC( "query_elem_to_vert" );

        // compute the centroid
        double centroid[3] = { 0.0, 0.0, 0.0 };
        for( int j = 0; j < 8; j++ )
        {
            centroid[0] += dum_coords[3 * j + 0];
            centroid[1] += dum_coords[3 * j + 1];
            centroid[2] += dum_coords[3 * j + 2];
        }
        centroid[0] *= 0.125;
        centroid[1] *= 0.125;
        centroid[2] *= 0.125;
        result = gMB->tag_set_data( pos_tag, &( *eit ), 1, centroid );
        RC( "query_elem_to_vert" );
    }
}

void query_vert_to_elem()
{
    Range all_verts;
    std::vector< EntityHandle > neighbor_hexes;
    std::vector< double > neighbor_pos;
    double coords[3];
    neighbor_pos.resize( 3 * 8 );  // average vertex will have 8 adjacent hexes
    ErrorCode result = gMB->get_entities_by_type( 0, MBVERTEX, all_verts );
    RC( "query_vert_to_elem" );
    for( Range::iterator vit = all_verts.begin(); vit != all_verts.end(); ++vit )
    {
        neighbor_hexes.clear();
        result = gMB->get_coords( &( *vit ), 1, coords );
        RC( "query_vert_to_elem" );
        result = gMB->get_adjacencies( &( *vit ), 1, 3, false, neighbor_hexes );
        RC( "query_vert_to_elem" );
        assert( neighbor_pos.size() >= 3 * neighbor_hexes.size() );
        result = gMB->tag_get_data( pos2_tag, &neighbor_hexes[0], neighbor_hexes.size(), &neighbor_pos[0] );
        RC( "query_vert_to_elem" );
        for( unsigned int i = 0; i < neighbor_hexes.size(); i++ )
        {
            neighbor_pos[3 * i] += coords[0];
            neighbor_pos[3 * i + 1] += coords[1];
            neighbor_pos[3 * i + 2] += coords[2];
        }

        result = gMB->tag_set_data( pos2_tag, &neighbor_hexes[0], neighbor_hexes.size(), &neighbor_pos[0] );
        RC( "query_vert_to_elem" );
    }

    // get all hexes and divide pos_tag by 8; reuse all_verts
    result = normalize_elems( coords );
    RC( "query_vert_to_elem" );
}

void query_elem_to_vert_iters( int chunk_size,
                               bool check_valid,
                               std::vector< EntityHandle >& connect,
                               double* dum_coords,
                               double* dum_pos )
{
    std::vector< EntityHandle > hexes;
    SetIterator* iter;
    ErrorCode result = gMB->create_set_iterator( 0, MBHEX, -1, chunk_size, check_valid, iter );
    RC( "query_elem_to_vert_iters" );
    bool atend = false;
    while( !atend )
    {
        hexes.clear();
        result = iter->get_next_arr( hexes, atend );
        RC( "query_elem_to_vert_iters" );
        result = gMB->get_connectivity( &hexes[0], hexes.size(), connect );
        RC( "query_elem_to_vert_iters" );
        result = gMB->get_coords( &connect[0], connect.size(), dum_coords );
        RC( "query_elem_to_vert_iters" );
        result = gMB->tag_get_data( pos_tag, &hexes[0], hexes.size(), dum_pos );
        RC( "query_elem_to_vert_iters" );
        for( unsigned int i = 0; i < hexes.size(); i++ )
        {
            // compute the centroid
            for( int j = 0; j < 8; j++ )
            {
                dum_pos[3 * i + 0] += dum_coords[24 * i + 3 * j];
                dum_pos[3 * i + 1] += dum_coords[24 * i + 3 * j + 1];
                dum_pos[3 * i + 2] += dum_coords[24 * i + 3 * j + 2];
            }
            dum_pos[3 * i + 0] *= 0.125;
            dum_pos[3 * i + 1] *= 0.125;
            dum_pos[3 * i + 2] *= 0.125;
        }
        result = gMB->tag_set_data( pos_tag, &hexes[0], hexes.size(), dum_pos );
        RC( "query_elem_to_vert_iters" );
    }

    delete iter;
}

void query_vert_to_elem_iters( int chunk_size,
                               bool check_valid,
                               std::vector< EntityHandle >& /*connect*/,
                               double* dum_coords,
                               double* dum_pos )
{
    std::vector< EntityHandle > verts, neighbor_hexes;
    SetIterator* iter;
    ErrorCode result = gMB->create_set_iterator( 0, MBVERTEX, -1, chunk_size, check_valid, iter );
    RC( "query_vert_to_elem_iters" );
    assert( MB_SUCCESS == result );
    bool atend = false;
    while( !atend )
    {
        verts.clear();
        result = iter->get_next_arr( verts, atend );
        RC( "query_vert_to_elem_iters" );
        result = gMB->get_coords( &verts[0], verts.size(), dum_coords );
        RC( "query_vert_to_elem_iters" );
        chunk_size = std::min( (int)verts.size(), chunk_size );
        for( int i = 0; i < chunk_size; i++ )
        {
            neighbor_hexes.clear();
            result = gMB->get_adjacencies( &verts[i], 1, 3, false, neighbor_hexes );
            RC( "query_vert_to_elem_iters" );
            result = gMB->tag_get_data( pos2_tag, &neighbor_hexes[0], neighbor_hexes.size(), dum_pos );
            RC( "query_vert_to_elem_iters" );
            for( unsigned int j = 0; j < neighbor_hexes.size(); j++ )
            {
                dum_pos[3 * j + 0] += dum_coords[3 * i + 0];
                dum_pos[3 * j + 1] += dum_coords[3 * i + 1];
                dum_pos[3 * j + 2] += dum_coords[3 * i + 2];
            }
            result = gMB->tag_set_data( pos2_tag, &neighbor_hexes[0], neighbor_hexes.size(), dum_pos );
            RC( "query_vert_to_elem_iters" );
        }
    }

    result = normalize_elems( dum_coords );
    RC( "query_vert_to_elem_iters" );
}

// normalize pos_tag on all elems by 1/8; coords assumed large enough to hold 3 doubles
ErrorCode normalize_elems( double* coords )
{
    // get all hexes and divide pos_tag by 8
    Range elems;
    ErrorCode result = gMB->get_entities_by_type( 0, MBHEX, elems );
    RR( "normalize" );

    for( Range::iterator vit = elems.begin(); vit != elems.end(); ++vit )
    {
        result = gMB->tag_get_data( pos2_tag, &( *vit ), 1, coords );
        RR( "normalize" );
        coords[0] *= 0.125;
        coords[1] *= 0.125;
        coords[2] *= 0.125;
        result = gMB->tag_set_data( pos2_tag, &( *vit ), 1, coords );
        RR( "normalize" );
    }

    return result;
}

void query_struct_elem_to_vert()
{
    // assumes brick mapped mesh with handles starting at zero
    Range all_hexes;
    ErrorCode result = gMB->get_entities_by_type( 0, MBHEX, all_hexes );
    RC( "query_struct_elem_to_vert" );
    double dum_coords[24];
    std::vector< EntityHandle > connect;
    for( Range::iterator eit = all_hexes.begin(); eit != all_hexes.end(); ++eit )
    {
        result = gMB->get_connectivity( &( *eit ), 1, connect );
        RC( "query_struct_elem_to_vert" );
        result = gMB->get_coords( &connect[0], connect.size(), dum_coords );
        RC( "query_struct_elem_to_vert" );

        double centroid[3] = { 0.0, 0.0, 0.0 };
        for( int j = 0; j < 8; j++ )
        {
            centroid[0] += dum_coords[3 * j + 0];
            centroid[1] += dum_coords[3 * j + 1];
            centroid[2] += dum_coords[3 * j + 2];
        }
        centroid[0] *= 0.125;
        centroid[1] *= 0.125;
        centroid[2] *= 0.125;
        result = gMB->tag_set_data( pos_tag, &( *eit ), 1, centroid );
        RC( "query_struct_elem_to_vert" );
    }
}

#if defined( _MSC_VER ) || defined( __MINGW32__ )
void print_time( const bool print_em, double& tot_time, double& utime, double& stime, long& imem, long& rmem )
{
    utime = (double)clock() / CLOCKS_PER_SEC;
    if( print_em ) std::cout << "Total wall time = " << utime << std::endl;
    tot_time = stime = 0;
    imem = rmem = 0;
}
#else
void print_time( const bool print_em, double& tot_time, double& utime, double& stime, long& imem, long& rmem )
{
    struct rusage r_usage;
    getrusage( RUSAGE_SELF, &r_usage );
    utime = (double)r_usage.ru_utime.tv_sec + ( (double)r_usage.ru_utime.tv_usec / 1.e6 );
    stime = (double)r_usage.ru_stime.tv_sec + ( (double)r_usage.ru_stime.tv_usec / 1.e6 );
    tot_time = utime + stime;
    if( print_em )<--- First condition
        std::cout << "User, system, total time = " << utime << ", " << stime << ", " << tot_time << std::endl;
#ifndef LINUX
    if( print_em )<--- Second condition
    {
        std::cout << "Max resident set size = " << r_usage.ru_maxrss << " kbytes" << std::endl;
        std::cout << "Int resident set size = " << r_usage.ru_idrss << std::endl;
    }
    imem = r_usage.ru_idrss;
    rmem = r_usage.ru_maxrss;
#else
    system( "ps o args,drs,rss | grep perf | grep -v grep" );  // RedHat 9.0 doesnt fill in actual
                                                               // memory data
    imem = rmem = 0;
#endif
}
#endif

void testA( const int nelem, const double* coords )
{
    double ttime0 = 0.0, ttime1 = 0.0, ttime2 = 0.0, ttime3 = 0.0, ttime4 = 0.0, utime = 0.0, stime = 0.0;
    long imem0 = 0, rmem0 = 0, imem1 = 0, rmem1 = 0, imem2 = 0, rmem2 = 0, imem3 = 0, rmem3 = 0, imem4 = 0, rmem4 = 0;

    print_time( false, ttime0, utime, stime, imem0, rmem0 );

    // make a 3d block of vertices
    EntitySequence* dum_seq    = NULL;
    ScdVertexData* vseq        = NULL;
    StructuredElementSeq* eseq = NULL;
    init();
    Core* mbcore = dynamic_cast< Core* >( gMB );
    assert( mbcore != NULL );
    SequenceManager* seq_mgr = mbcore->sequence_manager();
    HomCoord vseq_minmax[2]  = { HomCoord( 0, 0, 0 ), HomCoord( nelem, nelem, nelem ) };
    EntityHandle vstart, estart;

    ErrorCode result = seq_mgr->create_scd_sequence( vseq_minmax[0], vseq_minmax[1], MBVERTEX, 1, vstart, dum_seq );
    RC( "testA" );
    if( NULL != dum_seq ) vseq = dynamic_cast< ScdVertexData* >( dum_seq->data() );
    assert( MB_FAILURE != result && vstart != 0 && dum_seq != NULL && vseq != NULL );
    // now the element sequence
    result = seq_mgr->create_scd_sequence( vseq_minmax[0], vseq_minmax[1], MBHEX, 1, estart, dum_seq );
    if( NULL != dum_seq ) eseq = dynamic_cast< StructuredElementSeq* >( dum_seq );
    assert( MB_FAILURE != result && estart != 0 && dum_seq != NULL && eseq != NULL );

    // only need to add one vseq to this, unity transform
    // trick: if I know it's going to be unity, just input 3 sets of equivalent points
    result = eseq->sdata()->add_vsequence( vseq, vseq_minmax[0], vseq_minmax[0], vseq_minmax[0], vseq_minmax[0],
                                           vseq_minmax[0], vseq_minmax[0] );
    assert( MB_SUCCESS == result );

    // set the coordinates of the vertices
    EntityHandle handle;
    int i;
    double dumv[3];
    int num_verts = ( nelem + 1 ) * ( nelem + 1 ) * ( nelem + 1 );
    for( i = 0, handle = vstart; i < num_verts; i++, handle++ )
    {
        dumv[0] = coords[i];
        dumv[1] = coords[num_verts + i];
        dumv[2] = coords[2 * num_verts + i];
        result  = gMB->set_coords( &handle, 1, dumv );
        assert( MB_SUCCESS == result );
    }

    print_time( false, ttime1, utime, stime, imem1, rmem1 );

    // query the mesh 2 ways
    query_struct_elem_to_vert();

    print_time( false, ttime2, utime, stime, imem2, rmem2 );

    query_vert_to_elem();

    print_time( false, ttime3, utime, stime, imem3, rmem3 );

#ifndef NDEBUG
    check_answers( "A" );
#endif

    delete gMB;

    print_time( false, ttime4, utime, stime, imem4, rmem4 );

    std::cout << "MOAB_scd:nelem,construct,e_to_v,v_to_e,after_dtor,total= " << nelem << " " << ttime1 - ttime0 << " "
              << ttime2 - ttime1 << " " << ttime3 - ttime2 << " " << ttime4 - ttime3 << " " << ttime4 - ttime0
              << " seconds" << std::endl;
    std::cout << "MOAB_scd_memory(rss):initial,after_construction,e-v,v-e,after_dtor= " << rmem0 << " " << rmem1 << " "
              << rmem2 << " " << rmem3 << " " << rmem4 << " kb" << std::endl;
}

void testB( const int nelem, const double* coords, int* connect )
{
    double ttime0 = 0.0, ttime1 = 0.0, ttime2 = 0.0, ttime3 = 0.0, ttime4 = 0.0, utime = 0.0, stime = 0.0;
    long imem0 = 0, rmem0 = 0, imem1 = 0, rmem1 = 0, imem2 = 0, rmem2 = 0, imem3 = 0, rmem3 = 0, imem4 = 0, rmem4 = 0;

    print_time( false, ttime0, utime, stime, imem0, rmem0 );

    int num_verts = ( nelem + 1 ) * ( nelem + 1 ) * ( nelem + 1 );
    int num_elems = nelem * nelem * nelem;
    EntityHandle vstart, estart;

    // get the read interface
    ReadUtilIface* readMeshIface;
    init();
    gMB->query_interface( readMeshIface );

    // create a sequence to hold the node coordinates
    // get the current number of entities and start at the next slot
    std::vector< double* > coord_arrays;
    ErrorCode result = readMeshIface->get_node_coords( 3, num_verts, 1, vstart, coord_arrays );
    RC( "testB" );
    assert( MB_SUCCESS == result && 1 == vstart && coord_arrays[0] && coord_arrays[1] && coord_arrays[2] );
    // memcpy the coordinate data into place
    memcpy( coord_arrays[0], coords, sizeof( double ) * num_verts );
    memcpy( coord_arrays[1], &coords[num_verts], sizeof( double ) * num_verts );
    memcpy( coord_arrays[2], &coords[2 * num_verts], sizeof( double ) * num_verts );

    EntityHandle* conn = 0;
    result             = readMeshIface->get_element_connect( num_elems, 8, MBHEX, 1, estart, conn );
    assert( MB_SUCCESS == result );
    for( int i = 0; i < num_elems * 8; i++ )
        conn[i] = vstart + connect[i];

    delete[] connect;

    result = readMeshIface->update_adjacencies( estart, num_elems, 8, conn );
    assert( MB_SUCCESS == result );

    print_time( false, ttime1, utime, stime, imem1, rmem1 );

    // query the mesh 2 ways
    query_elem_to_vert();

    print_time( false, ttime2, utime, stime, imem2, rmem2 );

    query_vert_to_elem();

    print_time( false, ttime3, utime, stime, imem3, rmem3 );

#ifndef NDEBUG
    check_answers( "B" );
#endif

    delete gMB;

    print_time( false, ttime4, utime, stime, imem4, rmem4 );

    std::cout << "MOAB_ucd_blocked:nelem,construct,e_to_v,v_to_e,after_dtor,total= " << nelem << " " << ttime1 - ttime0
              << " " << ttime2 - ttime1 << " " << ttime3 - ttime2 << " " << ttime4 - ttime3 << " " << ttime4 - ttime0
              << " seconds" << std::endl;
    std::cout << "MOAB_ucdblocked_memory_(rss):initial,after_construction,e-v,v-e,after_dtor= " << rmem0 << " " << rmem1
              << " " << rmem2 << " " << rmem3 << " " << rmem4 << " kb" << std::endl;
}

void testC( const int nelem, const double* coords )
{
    double ttime0 = 0.0, ttime1 = 0.0, ttime2 = 0.0, ttime3 = 0.0, ttime4 = 0.0, utime = 0.0, stime = 0.0;
    long imem0 = 0, rmem0 = 0, imem1 = 0, rmem1 = 0, imem2 = 0, rmem2 = 0, imem3 = 0, rmem3 = 0, imem4 = 0, rmem4 = 0;

    print_time( false, ttime0, utime, stime, imem0, rmem0 );

    // create the vertices; assume we don't need to keep a list of vertex handles, since they'll
    // be created in sequence
    int numv             = nelem + 1;
    int numv_sq          = numv * numv;
    int num_verts        = numv * numv * numv;
    double dum_coords[3] = { coords[0], coords[num_verts], coords[2 * num_verts] };
    EntityHandle vstart;

    init();
    ErrorCode result = gMB->create_vertex( dum_coords, vstart );
    RC( "testC" );
    assert( MB_SUCCESS == result && 1 == vstart );

    EntityHandle dum_vert, vijk;
    int i;
    for( i = 1; i < num_verts; i++ )
    {
        dum_coords[0] = coords[i];
        dum_coords[1] = coords[num_verts + i];
        dum_coords[2] = coords[2 * num_verts + i];
        result        = gMB->create_vertex( dum_coords, dum_vert );
        assert( MB_SUCCESS == result );
    }

    EntityHandle dum_conn[8];
    for( i = 0; i < nelem; i++ )
    {
        for( int j = 0; j < nelem; j++ )
        {
            for( int k = 0; k < nelem; k++ )
            {
                vijk        = vstart + VINDEX( i, j, k );
                dum_conn[0] = vijk;
                dum_conn[1] = vijk + 1;
                dum_conn[2] = vijk + 1 + numv;
                dum_conn[3] = vijk + numv;
                dum_conn[4] = vijk + numv * numv;
                dum_conn[5] = vijk + 1 + numv * numv;
                dum_conn[6] = vijk + 1 + numv + numv * numv;
                dum_conn[7] = vijk + numv + numv * numv;
                result      = gMB->create_element( MBHEX, dum_conn, 8, dum_vert );
                assert( MB_SUCCESS == result );
            }
        }
    }

    print_time( false, ttime1, utime, stime, imem1, rmem1 );

    // query the mesh 2 ways
    query_elem_to_vert();

    print_time( false, ttime2, utime, stime, imem2, rmem2 );

    query_vert_to_elem();

    print_time( false, ttime3, utime, stime, imem3, rmem3 );

#ifndef NDEBUG
    check_answers( "C" );
#endif

    delete gMB;

    print_time( false, ttime4, utime, stime, imem4, rmem4 );

    std::cout << "MOAB_ucd_indiv:nelem,construct,e_to_v,v_to_e,after_dtor,total= " << nelem << " " << ttime1 - ttime0
              << " " << ttime2 - ttime1 << " " << ttime3 - ttime2 << " " << ttime4 - ttime3 << " " << ttime4 - ttime0
              << " seconds" << std::endl;
    std::cout << "MOAB_ucd_indiv_memory_(rss):initial,after_construction,e-v,v-e,after_dtor= " << rmem0 << " " << rmem1
              << " " << rmem2 << " " << rmem3 << " " << rmem4 << " kb" << std::endl;
}

void testD( const int nelem, const double* coords, int ver )
{
    double ttime0 = 0.0, ttime1 = 0.0, ttime2 = 0.0, ttime3 = 0.0, ttime4 = 0.0, ttime5 = 0.0, ttime6 = 0.0,
           ttime7 = 0.0, ttime8 = 0.0, ttime9 = 0.0, ttime10 = 0.0, utime = 0.0, stime = 0.0;
    long imem0 = 0, rmem0 = 0, imem1 = 0, rmem1 = 0, imem2 = 0, rmem2 = 0, imem3 = 0, rmem3 = 0, imem4 = 0, rmem4 = 0,
         imem5 = 0, rmem5 = 0, imem6 = 0, rmem6 = 0, imem7 = 0, rmem7 = 0, imem8 = 0, rmem8 = 0, imem9 = 0, rmem9 = 0,
         imem10 = 0, rmem10 = 0;

    print_time( false, ttime0, utime, stime, imem0, rmem0 );

    // create the vertices; assume we don't need to keep a list of vertex handles, since they'll
    // be created in sequence
    int numv      = nelem + 1;
    int numv_sq   = numv * numv;
    int num_verts = numv * numv * numv;
    std::vector< double > dum_coords( 24 ), dum_pos( 24 );
    dum_coords[0] = coords[0];
    dum_coords[1] = coords[num_verts];
    dum_coords[2] = coords[2 * num_verts];
    EntityHandle vstart;

    init();
    ErrorCode result = gMB->create_vertex( &dum_coords[0], vstart );
    RC( "testD" );
    assert( MB_SUCCESS == result && 1 == vstart );

    EntityHandle dum_vert, vijk;
    int i;
    for( i = 1; i < num_verts; i++ )
    {
        dum_coords[0] = coords[i];
        dum_coords[1] = coords[num_verts + i];
        dum_coords[2] = coords[2 * num_verts + i];
        result        = gMB->create_vertex( &dum_coords[0], dum_vert );
        assert( MB_SUCCESS == result );
    }

    EntityHandle dum_conn[8];
    for( i = 0; i < nelem; i++ )
    {
        for( int j = 0; j < nelem; j++ )
        {
            for( int k = 0; k < nelem; k++ )
            {
                vijk        = vstart + VINDEX( i, j, k );
                dum_conn[0] = vijk;
                dum_conn[1] = vijk + 1;
                dum_conn[2] = vijk + 1 + numv;
                dum_conn[3] = vijk + numv;
                dum_conn[4] = vijk + numv * numv;
                dum_conn[5] = vijk + 1 + numv * numv;
                dum_conn[6] = vijk + 1 + numv + numv * numv;
                dum_conn[7] = vijk + numv + numv * numv;
                result      = gMB->create_element( MBHEX, dum_conn, 8, dum_vert );
                assert( MB_SUCCESS == result );
            }
        }
    }

    print_time( false, ttime1, utime, stime, imem1, rmem1 );

    // query the mesh 2 ways with !check_valid
    std::vector< EntityHandle > connect( 8 );
#ifndef NDEBUG
    // used only in debug mode
    double def_val[3] = { 0.0, 0.0, 0.0 };
#endif
    if( ver == 0 || ver == 1 )
    {
        query_elem_to_vert_iters( 1, false, connect, &dum_coords[0], &dum_pos[0] );
        print_time( false, ttime2, utime, stime, imem2, rmem2 );
        query_vert_to_elem_iters( 1, false, connect, &dum_coords[0], &dum_pos[0] );
        print_time( false, ttime3, utime, stime, imem3, rmem3 );
#ifndef NDEBUG
        check_answers( "D" );
        result = gMB->tag_delete( pos_tag );
        assert( MB_SUCCESS == result );
        result =
            gMB->tag_get_handle( "position_tag", 3, MB_TYPE_DOUBLE, pos_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
        assert( MB_SUCCESS == result );
        result = gMB->tag_delete( pos2_tag );
        assert( MB_SUCCESS == result );
        result =
            gMB->tag_get_handle( "position2_tag", 3, MB_TYPE_DOUBLE, pos2_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
        assert( MB_SUCCESS == result );
#endif
    }

    if( ver == 0 || ver == 2 )
    {
        if( ver != 0 ) print_time( false, ttime3, utime, stime, imem3, rmem3 );
        query_elem_to_vert_iters( 1, true, connect, &dum_coords[0], &dum_pos[0] );
        print_time( false, ttime4, utime, stime, imem4, rmem4 );
        query_vert_to_elem_iters( 1, true, connect, &dum_coords[0], &dum_pos[0] );
        print_time( false, ttime5, utime, stime, imem5, rmem5 );
#ifndef NDEBUG
        check_answers( "D" );
        result = gMB->tag_delete( pos_tag );
        assert( MB_SUCCESS == result );
        result =
            gMB->tag_get_handle( "position_tag", 3, MB_TYPE_DOUBLE, pos_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
        assert( MB_SUCCESS == result );
        result = gMB->tag_delete( pos2_tag );
        assert( MB_SUCCESS == result );
        result =
            gMB->tag_get_handle( "position2_tag", 3, MB_TYPE_DOUBLE, pos2_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
        assert( MB_SUCCESS == result );
#endif
    }

    if( ver == 0 || ver >= 3 )
    {
        dum_coords.resize( 2400 );
        dum_pos.resize( 300 );
    }
    if( ver == 0 || ver == 3 )
    {
        if( ver != 0 ) print_time( false, ttime5, utime, stime, imem3, rmem3 );
        query_elem_to_vert_iters( 100, false, connect, &dum_coords[0], &dum_pos[0] );
        print_time( false, ttime6, utime, stime, imem6, rmem6 );
        query_vert_to_elem_iters( 100, false, connect, &dum_coords[0], &dum_pos[0] );
        print_time( false, ttime7, utime, stime, imem7, rmem7 );
#ifndef NDEBUG
        check_answers( "D" );
        result = gMB->tag_delete( pos_tag );
        assert( MB_SUCCESS == result );
        result =
            gMB->tag_get_handle( "position_tag", 3, MB_TYPE_DOUBLE, pos_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
        assert( MB_SUCCESS == result );
        result = gMB->tag_delete( pos2_tag );
        assert( MB_SUCCESS == result );
        result =
            gMB->tag_get_handle( "position2_tag", 3, MB_TYPE_DOUBLE, pos2_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
        assert( MB_SUCCESS == result );
#endif
    }

    if( ver == 0 || ver == 4 )
    {
        if( ver != 0 ) print_time( false, ttime7, utime, stime, imem3, rmem3 );
        query_elem_to_vert_iters( 100, true, connect, &dum_coords[0], &dum_pos[0] );
        print_time( false, ttime8, utime, stime, imem8, rmem8 );
        query_vert_to_elem_iters( 100, true, connect, &dum_coords[0], &dum_pos[0] );
        print_time( false, ttime9, utime, stime, imem9, rmem9 );
#ifndef NDEBUG
        check_answers( "D" );
        result = gMB->tag_delete( pos_tag );
        assert( MB_SUCCESS == result );
        result =
            gMB->tag_get_handle( "position_tag", 3, MB_TYPE_DOUBLE, pos_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
        assert( MB_SUCCESS == result );
        result = gMB->tag_delete( pos2_tag );
        assert( MB_SUCCESS == result );
        result =
            gMB->tag_get_handle( "position2_tag", 3, MB_TYPE_DOUBLE, pos2_tag, MB_TAG_DENSE | MB_TAG_CREAT, def_val );
        assert( MB_SUCCESS == result );
#endif
    }

    if( ver > 0 && ver < 4 ) print_time( false, ttime9, utime, stime, imem9, rmem9 );
    delete gMB;

    print_time( false, ttime10, utime, stime, imem10, rmem10 );

    if( ver == 0 || ver == 1 )
    {
        std::cout << "MOAB_ucd_iters_!check_valid_1:nelem,construct,e-v,v-e,after_dtor,total= " << nelem << " "
                  << ttime1 - ttime0 << " " << ttime2 - ttime1 << " " << ttime3 - ttime2 << " " << ttime10 - ttime9
                  << " " << ttime3 - ttime0 + ttime10 - ttime9 << std::endl;
        std::cout << "MOAB_ucd_iters_memory_(rss)_!check_valid_1:initial,after_construction,e-v,v-"
                     "e,after_dtor= "
                  << rmem0 << " " << rmem1 << " " << rmem2 << " " << rmem3 << " " << rmem10 << " kb" << std::endl;
    }
    if( ver == 0 || ver == 2 )
    {
        std::cout << "MOAB_ucd_iters_check_valid_1:nelem,construct,e-v,v-e,after_dtor,total= " << nelem << " "
                  << ttime1 - ttime0 << " " << ttime4 - ttime3 << " " << ttime5 - ttime4 << " " << ttime10 - ttime9
                  << " " << ttime1 - ttime0 + ttime5 - ttime3 + ttime10 - ttime9 << std::endl;
        std::cout << "MOAB_ucd_iters_memory_(rss)_check_valid_1:initial,after_construction,e-v,v-e,"
                     "after_dtor= "
                  << rmem0 << " " << rmem1 << " " << rmem2 << " " << rmem3 << " " << rmem10 << " kb" << std::endl;
    }
    if( ver == 0 || ver == 3 )
    {
        std::cout << "MOAB_ucd_iters_!check_valid_100:nelem,construct,e-v,v-e,after_dtor,total= " << nelem << " "
                  << ttime1 - ttime0 << " " << ttime6 - ttime5 << " " << ttime7 - ttime6 << " " << ttime10 - ttime9
                  << " " << ttime1 - ttime0 + ttime7 - ttime5 + ttime10 - ttime9 << std::endl;
        std::cout << "MOAB_ucd_iters_memory_(rss)_!check_valid_100:initial,after_construction,e-v,"
                     "v-e,after_dtor= "
                  << rmem0 << " " << rmem1 << " " << rmem6 << " " << rmem7 << " " << rmem10 << " kb" << std::endl;
    }
    if( ver == 0 || ver == 4 )
    {
        std::cout << "MOAB_ucd_iters_check_valid_100:nelem,construct,e-v,v-e,after_dtor,total= " << nelem << " "
                  << ttime1 - ttime0 << " " << ttime8 - ttime7 << " " << ttime9 - ttime8 << " " << ttime10 - ttime9
                  << " " << ttime1 - ttime0 + ttime10 - ttime7 << std::endl;
        std::cout << "MOAB_ucd_iters_memory_(rss)_check_valid_100:initial,after_construction,e-v,v-"
                     "e,after_dtor= "
                  << rmem0 << " " << rmem1 << " " << rmem8 << " " << rmem9 << " " << rmem10 << " kb" << std::endl;
    }
}

void testE( const int nelem, const double* coords, int* connect )
{
    double ttime0 = 0.0, ttime1 = 0.0, ttime2 = 0.0, ttime3 = 0.0, ttime4 = 0.0, ttime5 = 0.0, ttime6 = 0.0,
           utime = 0.0, stime = 0.0;
    long imem0 = 0, rmem0 = 0, imem1 = 0, rmem1 = 0, imem2 = 0, rmem2 = 0, imem3 = 0, rmem3 = 0, imem4 = 0, rmem4 = 0,
         imem5 = 0, rmem5 = 0, imem6 = 0, rmem6 = 0;

    print_time( false, ttime0, utime, stime, imem0, rmem0 );

    int num_verts = ( nelem + 1 ) * ( nelem + 1 ) * ( nelem + 1 );
    int num_elems = nelem * nelem * nelem;
    EntityHandle vstart, estart;

    // get the read interface
    ReadUtilIface* readMeshIface;
    init();
    gMB->query_interface( readMeshIface );

    // create a sequence to hold the node coordinates
    // get the current number of entities and start at the next slot
    std::vector< double* > coord_arrays;
    ErrorCode result = readMeshIface->get_node_coords( 3, num_verts, 1, vstart, coord_arrays );
    RC( "testE" );
    assert( MB_SUCCESS == result && 1 == vstart && coord_arrays[0] && coord_arrays[1] && coord_arrays[2] );
    // memcpy the coordinate data into place
    memcpy( coord_arrays[0], coords, sizeof( double ) * num_verts );
    memcpy( coord_arrays[1], &coords[num_verts], sizeof( double ) * num_verts );
    memcpy( coord_arrays[2], &coords[2 * num_verts], sizeof( double ) * num_verts );

    EntityHandle* conn = 0;
    result             = readMeshIface->get_element_connect( num_elems, 8, MBHEX, 1, estart, conn );
    assert( MB_SUCCESS == result );
    for( int i = 0; i < num_elems * 8; i++ )
        conn[i] = vstart + connect[i];

    delete[] connect;

    Range verts( vstart, vstart + num_verts - 1 ), elems( estart, estart + num_elems - 1 );

    result = readMeshIface->update_adjacencies( estart, num_elems, 8, conn );
    assert( MB_SUCCESS == result );

    print_time( false, ttime1, utime, stime, imem1, rmem1 );

    // query the mesh 2 ways
    query_elem_to_vert_direct();

    print_time( false, ttime2, utime, stime, imem2, rmem2 );

    query_vert_to_elem_direct();

    print_time( false, ttime3, utime, stime, imem3, rmem3 );

#ifndef NDEBUG
    check_answers( "E" );
#endif

    query_elem_to_vert_direct();

    print_time( false, ttime4, utime, stime, imem4, rmem4 );

    query_vert_to_elem_direct();

    print_time( false, ttime5, utime, stime, imem5, rmem5 );

#ifndef NDEBUG
    check_answers( "E" );
#endif

    delete gMB;

    print_time( false, ttime6, utime, stime, imem6, rmem6 );

    std::cout << "MOAB_ucd_direct:nelem,construct,e_to_v,v_to_e,after_dtor,total= " << nelem << " " << ttime1 - ttime0
              << " " << ttime2 - ttime1 << " " << ttime3 - ttime2 << " " << ttime6 - ttime5 << " "
              << ttime3 - ttime0 + ttime6 - ttime5 << " seconds" << std::endl;
    std::cout << "MOAB_ucd_direct_memory_(rss):initial,after_construction,e-v,v-e,after_dtor= " << rmem0 << " " << rmem1
              << " " << rmem2 << " " << rmem3 << " " << rmem6 << " kb" << std::endl;

    std::cout << "MOAB_ucd_direct2:nelem,construct,e_to_v,v_to_e,after_dtor,total= " << nelem << " " << ttime1 - ttime0
              << " " << ttime4 - ttime3 << " " << ttime5 - ttime4 << " " << ttime6 - ttime5 << " "
              << ttime1 - ttime0 + ttime6 - ttime3 << " seconds" << std::endl;
    std::cout << "MOAB_ucd_direct2_memory_(rss):initial,after_construction,e-v,v-e,after_dtor= " << rmem0 << " "
              << rmem1 << " " << rmem4 << " " << rmem5 << " " << rmem6 << " kb" << std::endl;
}

void query_elem_to_vert_direct()
{
    Range all_hexes, all_verts;
    ErrorCode result = gMB->get_entities_by_type( 0, MBHEX, all_hexes );
    assert( MB_SUCCESS == result );
    result = gMB->get_entities_by_type( 0, MBVERTEX, all_verts );
    RC( "query_elem_to_vert_direct" );
    EntityHandle* connect;
    int ecount, vcount, vpere;
    double* coords[3];

    result = gMB->connect_iterate( all_hexes.begin(), all_hexes.end(), connect, vpere, ecount );
    if( MB_SUCCESS != result || ecount != (int)all_hexes.size() )
    {
        std::cout << "FAILED in connect_iterate!" << std::endl;
        return;
    }
    result = gMB->coords_iterate( all_verts.begin(), all_verts.end(), coords[0], coords[1], coords[2], vcount );
    if( MB_SUCCESS != result || vcount != (int)all_verts.size() )
    {
        std::cout << "FAILED in coords_iterate!" << std::endl;
        return;
    }
    double* centroid;
    result = gMB->tag_iterate( pos_tag, all_hexes.begin(), all_hexes.end(), ecount, (void*&)centroid );
    if( MB_SUCCESS != result || ecount != (int)all_hexes.size() )
    {
        std::cout << "FAILED in connect_iterate!" << std::endl;
        return;
    }

    EntityHandle vstart = *all_verts.begin();
    for( int i = 0; i < ecount; i++ )
    {
        // compute the centroid
        for( int j = 0; j < vpere; j++ )
        {
            int vind = *connect - vstart;
            connect++;
            centroid[3 * i + 0] += coords[0][vind];
            centroid[3 * i + 1] += coords[1][vind];
            centroid[3 * i + 2] += coords[2][vind];
        }

        // now normalize
        centroid[3 * i + 0] *= 0.125;
        centroid[3 * i + 1] *= 0.125;
        centroid[3 * i + 2] *= 0.125;
    }
}

void query_vert_to_elem_direct()
{
    Range all_verts, tmp_ents;
    ErrorCode result = gMB->get_entities_by_type( 0, MBVERTEX, all_verts );
    assert( MB_SUCCESS == result );

    // make sure vertex-element adjacencies are created
    result = gMB->get_adjacencies( &( *all_verts.begin() ), 1, 3, false, tmp_ents );
    assert( MB_SUCCESS == result );

    const std::vector< EntityHandle >** adjs;
    int count;
    result = gMB->adjacencies_iterate( all_verts.begin(), all_verts.end(), adjs, count );
    if( MB_SUCCESS != result && count != (int)all_verts.size() )
    {
        std::cout << "FAILED:adjacencies_iterate." << std::endl;
        return;
    }

    double* coords[3];
    result = gMB->coords_iterate( all_verts.begin(), all_verts.end(), coords[0], coords[1], coords[2], count );
    if( MB_SUCCESS != result || count != (int)all_verts.size() )
    {
        std::cout << "FAILED in coords_iterate!" << std::endl;
        return;
    }
    // get all hexes, then iterator over pos2_tag
    double* centroid;
    int ecount;
    tmp_ents.clear();
    result = gMB->get_entities_by_type( 0, MBHEX, tmp_ents );
    assert( MB_SUCCESS == result );
    result = gMB->tag_iterate( pos2_tag, tmp_ents.begin(), tmp_ents.end(), ecount, (void*&)centroid );
    if( MB_SUCCESS != result || ecount != (int)tmp_ents.size() )
    {
        std::cout << "FAILED in tag_iterate!" << std::endl;
        return;
    }

    int i;
    Range::iterator vit;
    EntityHandle estart = *tmp_ents.begin();
    for( vit = all_verts.begin(), i = 0; vit != all_verts.end(); ++vit, i++ )
    {
        assert( adjs[i] );
        for( std::vector< EntityHandle >::const_iterator vit2 = adjs[i]->begin(); vit2 != adjs[i]->end(); ++vit2 )
            if( *vit >= estart )
            {
                int eind = *vit2 - estart;
                centroid[3 * eind + 0] += coords[0][i];
                centroid[3 * eind + 1] += coords[1][i];
                centroid[3 * eind + 2] += coords[2][i];
            }
    }

    // now normalize
    for( i = 0; i < (int)tmp_ents.size(); i++ )
    {
        centroid[3 * i + 0] *= 0.125;
        centroid[3 * i + 1] *= 0.125;
        centroid[3 * i + 2] *= 0.125;
    }
}

void check_answers( const char* /*test_name*/ )
{
    Range elems;
    ErrorCode result = gMB->get_entities_by_type( 0, MBHEX, elems );
    RC( "check_answers" );

    double coords1[3], coords2[3], del[3];
    double diff = 0.0;
    for( Range::iterator vit = elems.begin(); vit != elems.end(); ++vit )
    {
        result = gMB->tag_get_data( pos_tag, &( *vit ), 1, coords1 );
        RC( "check_answers" );
        result = gMB->tag_get_data( pos2_tag, &( *vit ), 1, coords2 );
        RC( "check_answers" );
        for( int i = 0; i < 3; i++ )
            del[i] = fabs( coords1[i] - coords2[i] );
        if( del[0] || del[1] || del[2] )
        {
            double len2 = std::max( coords1[0] * coords1[0] + coords1[1] * coords1[1] + coords1[2] * coords1[2],
                                    coords2[0] * coords2[0] + coords2[1] * coords2[1] + coords2[2] * coords2[2] ),
                   num  = del[0] * del[0] + del[1] * del[1] + del[2] * del[2];
            if( len2 > 0.0 )
                diff = std::max( diff, num / sqrt( len2 ) );
            else if( num > 0.0 )
                diff = sqrt( num );
        }
    }
    if( diff > 0.0 ) std::cout << "Max relative difference = " << diff << std::endl;
}