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
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2004 Sandia Corporation and Argonne National
    Laboratory.  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.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    (lgpl.txt) along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    [email protected], [email protected], [email protected],
    [email protected], [email protected], [email protected]

  ***************************************************************** */
//
//    AUTHOR: Thomas Leurent <[email protected]>
//       ORG: Argonne National Laboratory
//    E-MAIL: [email protected]
//
// ORIG-DATE: 13-Nov-02 at 18:05:56
//  LAST-MOD:  9-Jun-04 at 14:50:51 by Thomas Leurent
//
// DESCRIPTION:
// ============
/*! \file PatchDataTest.cpp

Unit testing of various functions in the PatchData class.

 */
// DESCRIP-END.
//

#include "Mesquite.hpp"
#include "PatchData.hpp"
#include "PatchDataInstances.hpp"
#include "UnitUtil.hpp"
#include "Settings.hpp"
#include "Instruction.hpp"

#include "ArrayMesh.hpp"
#include "DomainClassifier.hpp"
#include "PlanarDomain.hpp"
#include "MeshDomain1D.hpp"
#include "MeshDecorator.hpp"

//#include "TriLagrangeShape.hpp"

#include "cppunit/extensions/HelperMacros.h"

#include <algorithm>
#include <set>
#include <map>

using namespace MBMesquite;

using std::cerr;
using std::cout;
using std::endl;

class PatchDataTest : public CppUnit::TestFixture
{
  private:
    CPPUNIT_TEST_SUITE( PatchDataTest );
    CPPUNIT_TEST( test_num_corners );
    CPPUNIT_TEST( test_get_element_vertex_indices );
    CPPUNIT_TEST( test_get_vertex_element_indices );
    CPPUNIT_TEST( test_get_element_vertex_coordinates );
    CPPUNIT_TEST( test_move_free_vertices_constrained );
    CPPUNIT_TEST( test_movement_function );
    CPPUNIT_TEST( test_get_adj_elems_2d );
    CPPUNIT_TEST( test_get_minmax_element_area );
    CPPUNIT_TEST( test_sub_patch );
    CPPUNIT_TEST( test_fill );
    CPPUNIT_TEST( test_reorder );
    CPPUNIT_TEST( test_update_slave_node_coords );
    CPPUNIT_TEST( test_patch_data_fill_slaved_ho_nodes );
    CPPUNIT_TEST( test_patch_reorder_ho_nodes );
    CPPUNIT_TEST( test_patch_data_fill_free_ho_nodes );
    CPPUNIT_TEST( test_patch_data_mesh_slaved_ho_nodes );
    CPPUNIT_TEST( test_patch_data_mesh_free_ho_nodes );
    CPPUNIT_TEST( test_patch_data_mesh_calcualted_ho_nodes );
    CPPUNIT_TEST( test_patch_data_mesh_flagged_ho_nodes );
    CPPUNIT_TEST( test_vertex_verts_fixed );
    CPPUNIT_TEST( test_curve_verts_fixed );
    CPPUNIT_TEST( test_surf_verts_fixed );
    CPPUNIT_TEST_SUITE_END();

  private:
    MsqVertex vtx_0_0;
    MsqVertex vtx_0_1;
    MsqVertex vtx_1_0;
    MsqVertex vtx_1_1;
    MsqVertex vtx_2_0;
    MsqVertex vtx_2_1;

    MsqMeshEntity tri1;
    MsqMeshEntity tri2;
    MsqMeshEntity quad1;

    PatchData mPatch2D;

    void test_quad8_patch( bool reorder, bool ho_nodes_slaved );
    void get_quad8_mesh( Mesh*& mesh_out );
    void get_quad8_mesh_and_domain( Mesh*& mesh_out, MeshDomain*& domain_out );
    void get_higher_order_vertices( Mesh* mesh,
                                    std::map< Mesh::VertexHandle, bool >& ho_verts,
                                    bool initial_value  = false,
                                    bool non_fixed_only = true );
    void check_higher_order_vertices_slaved( Mesh* mesh,
                                             Settings::HigherOrderSlaveMode mode,
                                             const std::map< Mesh::VertexHandle, bool >& expected );

  public:
    void setUp()
    {
        MsqPrintError err( cout );

        /* our 2D set up: 2 triangles and one quad are available
          1___3___5
           |\1|   |
           |0\| 2 |
          0---2---4
        */
        vtx_0_0.set( 0, 0, 0 );
        vtx_0_1.set( 0, 1, 0 );
        vtx_1_0.set( 1, 0, 0 );
        vtx_1_1.set( 1, 1, 0 );
        vtx_2_0.set( 2, 0, 0 );
        vtx_2_1.set( 2, 1, 0 );

        double coords[] = { 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 2, 0, 0, 2, 1, 0 };

        EntityTopology types[] = { TRIANGLE, TRIANGLE, QUADRILATERAL };

        size_t connectivity[] = { 0, 2, 1, 1, 2, 3, 3, 2, 4, 5 };

        size_t counts[] = { 3, 3, 4 };

        mPatch2D.fill( 6, coords, 3, types, counts, connectivity, 0, err );
    }

    void tearDown() {}

  public:
    PatchDataTest() {}

    void test_num_corners()
    {
        MsqPrintError err( cout );
        size_t n = mPatch2D.num_corners();
        CPPUNIT_ASSERT( n == 10 );
    }

    void test_get_element_vertex_indices()
    {

        MsqPrintError err( cout );

        std::vector< size_t > vtx_ind;
        std::vector< size_t > res;

        // test we get the right vertices for element 1 (tri)
        mPatch2D.get_element_vertex_indices( 1, vtx_ind, err );
        CPPUNIT_ASSERT( !err );
        res.push_back( 1 );
        res.push_back( 2 );
        res.push_back( 3 );
        CPPUNIT_ASSERT( vtx_ind == res );

        // test we get the right vertices for element 2 (quad)
        vtx_ind.clear();
        res.clear();
        mPatch2D.get_element_vertex_indices( 2, vtx_ind, err );
        CPPUNIT_ASSERT( !err );
        res.push_back( 3 );
        res.push_back( 2 );
        res.push_back( 4 );
        res.push_back( 5 );
        CPPUNIT_ASSERT( vtx_ind == res );
    }

    void test_get_vertex_element_indices()
    {
        /*  1___3___5
            |\1|   |
            |0\| 2 |
            0---2---4   */
        MsqPrintError err( cout );

        std::vector< size_t > elem_ind;
        std::vector< size_t > res;

        mPatch2D.generate_vertex_to_element_data();

        // test we get the elements contiguous to vertex 3
        mPatch2D.get_vertex_element_indices( 3, elem_ind, err );
        CPPUNIT_ASSERT( !err );
        res.push_back( 2 );
        res.push_back( 1 );
        CPPUNIT_ASSERT( res == elem_ind );

        // test we get the elements contiguous to vertex 2
        elem_ind.clear();
        res.clear();
        mPatch2D.get_vertex_element_indices( 2, elem_ind, err );
        CPPUNIT_ASSERT( !err );
        res.push_back( 2 );
        res.push_back( 1 );
        res.push_back( 0 );
        CPPUNIT_ASSERT( res == elem_ind );
    }

    void test_get_element_vertex_coordinates()
    {
        MsqPrintError err( cout );

        std::vector< Vector3D > coords;
        mPatch2D.get_element_vertex_coordinates( 1, coords, err );
        CPPUNIT_ASSERT( !err );

        CPPUNIT_ASSERT( coords[0] == vtx_0_1 );
        CPPUNIT_ASSERT( coords[1] == vtx_1_0 );
        CPPUNIT_ASSERT( coords[2] == vtx_1_1 );
    }

    /* This tests the move_vertices() function as well as the
       PatchDataCoordsMemento functionality
       */
    void test_move_free_vertices_constrained()
    {
        MsqPrintError err( cout );

        // gets a memento of the patch coordinates.
        PatchDataVerticesMemento* coords_mem = mPatch2D.create_vertices_memento( err );
        CPPUNIT_ASSERT( !err );

        // Move the two first vertices in direction dk by step size s;
        Vector3D dk[6];
        dk[0].set( -1, -2, 0 );
        dk[1].set( -1, 2, 0 );
        double s = 0.3;
        mPatch2D.move_free_vertices_constrained( dk, 6, s, err );
        CPPUNIT_ASSERT( !err );

        // gets the new coordinates and  checks the vertices were displaced as expected.
        std::vector< Vector3D > coords;
        mPatch2D.get_element_vertex_coordinates( 0, coords, err );
        Vector3D new_vtx_0_0 = vtx_0_0 + s * dk[0];
        Vector3D new_vtx_0_1 = vtx_0_1 + s * dk[1];
        CPPUNIT_ASSERT( coords[0] == new_vtx_0_0 );
        CPPUNIT_ASSERT( coords[2] == new_vtx_0_1 );

        // restore the PatchData to previous coords.
        mPatch2D.set_to_vertices_memento( coords_mem, err );
        CPPUNIT_ASSERT( !err );

        // gets the new coordinates and  checks the vertices are back to original.
        coords.clear();
        mPatch2D.get_element_vertex_coordinates( 0, coords, err );
        CPPUNIT_ASSERT( coords[0] == vtx_0_0 );
        CPPUNIT_ASSERT( coords[2] == vtx_0_1 );

        delete coords_mem;
    }

    void test_movement_function()
    {
        MsqPrintError err( cout );
        // gets a memento of the patch coordinates.
        PatchDataVerticesMemento* coords_mem = mPatch2D.create_vertices_memento( err );
        CPPUNIT_ASSERT( !err );

        // Move the two first vertices in direction dk by step size s;
        Vector3D dk[6];
        dk[0].set( 0, -2, 0 );
        dk[1].set( -1, 0, 0 );
        double s = 1;
        mPatch2D.move_free_vertices_constrained( dk, 6, 1, err );
        CPPUNIT_ASSERT( !err );
        // gets the new coordinates and  checks the vertices were displaced as expected.
        std::vector< Vector3D > coords;
        mPatch2D.get_element_vertex_coordinates( 0, coords, err );
        Vector3D new_vtx_0_0 = vtx_0_0 + s * dk[0];
        Vector3D new_vtx_0_1 = vtx_0_1 + s * dk[1];
        CPPUNIT_ASSERT( coords[0] == new_vtx_0_0 );
        CPPUNIT_ASSERT( coords[2] == new_vtx_0_1 );
        double m_dist = mPatch2D.get_max_vertex_movement_squared( coords_mem, err );
        CPPUNIT_ASSERT( m_dist == 4.0 );
        // restore the PatchData to previous coords.
        mPatch2D.set_to_vertices_memento( coords_mem, err );
        CPPUNIT_ASSERT( !err );
        // gets the new coordinates and  checks the vertices are back to original.
        coords.clear();
        mPatch2D.get_element_vertex_coordinates( 0, coords, err );
        CPPUNIT_ASSERT( coords[0] == vtx_0_0 );
        CPPUNIT_ASSERT( coords[2] == vtx_0_1 );

        delete coords_mem;
    }

    /*Tests the function PatchData::get_adjacent_entities_via_n_dim()
      which finds the elements adjacent to a given element.  If 'n'
      equals 0 the elements must share a vertex; if 'n' equals 1 the
      elements must share an edge; and if 'n' equals 2 the elements
      must share a face.*/
    void test_get_adj_elems_2d()
    {
        MsqPrintError err( cout );
        std::vector< size_t > elems_0;
        // find elements sharing an edge with oth elem (should be 1)
        mPatch2D.get_adjacent_entities_via_n_dim( 1, 0, elems_0, err );
        CPPUNIT_ASSERT( !err );
        CPPUNIT_ASSERT( elems_0.back() == 1 );
        std::vector< size_t > elems_1;
        // find elements sharing an edge with 1st elem (should be 0 and 2)
        mPatch2D.get_adjacent_entities_via_n_dim( 1, 1, elems_1, err );
        CPPUNIT_ASSERT( !err );
        CPPUNIT_ASSERT( elems_1.size() == 2 );
        std::vector< size_t > elems_2;
        // find elements sharing an vert with 0th elem (should be 1 and 2).
        mPatch2D.get_adjacent_entities_via_n_dim( 0, 0, elems_2, err );
        CPPUNIT_ASSERT( !err );
        CPPUNIT_ASSERT( elems_2.size() == 2 );
        std::vector< size_t > elems_3;
        // find elements sharing an face with 0th elem (should be empty).
        mPatch2D.get_adjacent_entities_via_n_dim( 2, 0, elems_3, err );
        CPPUNIT_ASSERT( !err );
        CPPUNIT_ASSERT( elems_3.size() == 0 );
    }

    void test_get_minmax_element_area()
    {
        MsqPrintError err( cout );
        double min, max;
        mPatch2D.get_minmax_element_unsigned_area( min, max, err );
        CPPUNIT_ASSERT( !err );

        CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.5, min, 0.0001 );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, max, 0.0001 );
    }

    void check_sub_patch( unsigned vtx, unsigned layers, PatchData& pd, PatchData& sub );

    void test_sub_patch();

    void test_patch_contents( bool reorder );

    void test_fill()
    {
        test_patch_contents( false );
    }
    void test_reorder()
    {
        test_patch_contents( true );
    }

    void test_update_slave_node_coords();
    void test_patch_data_fill_slaved_ho_nodes()
    {
        test_quad8_patch( false, true );
    }
    void test_patch_reorder_ho_nodes()
    {
        test_quad8_patch( true, true );
    }
    void test_patch_data_fill_free_ho_nodes()
    {
        test_quad8_patch( false, false );
    }

    void test_patch_data_mesh_slaved_ho_nodes();
    void test_patch_data_mesh_free_ho_nodes();
    void test_patch_data_mesh_calcualted_ho_nodes();
    void test_patch_data_mesh_flagged_ho_nodes();

    void test_fixed_by_geom_dim( unsigned dim );
    void test_vertex_verts_fixed()
    {
        test_fixed_by_geom_dim( 0 );
    }
    void test_curve_verts_fixed()
    {
        test_fixed_by_geom_dim( 1 );
    }
    void test_surf_verts_fixed()
    {
        test_fixed_by_geom_dim( 2 );
    }
};

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PatchDataTest, "PatchDataTest" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PatchDataTest, "Unit" );

void PatchDataTest::check_sub_patch( unsigned vtx, unsigned layers, PatchData& pd, PatchData& sub )
{
    unsigned i, j;
    std::set< size_t > seen;
    std::vector< size_t > vtx_map, elem_map;

    // test vertex list consistency
    vtx_map.resize( sub.num_nodes() );
    for( i = 0; i < sub.num_nodes(); ++i )
    {
        // get index in old patch for this vertex
        Mesh::VertexHandle h    = sub.get_vertex_handles_array()[i];
        Mesh::VertexHandle* end = pd.get_vertex_handles_array() + pd.num_nodes();
        Mesh::VertexHandle* ptr = std::find( pd.get_vertex_handles_array(), end, h );
        CPPUNIT_ASSERT( ptr != end );
        size_t idx = ptr - pd.get_vertex_handles_array();
        CPPUNIT_ASSERT( idx < pd.num_nodes() );
        // put handle in map
        vtx_map[i] = idx;
        // make sure we don't have duplicates of vertices
        CPPUNIT_ASSERT( seen.insert( idx ).second );
        // make sure vertices have same coords
        CPPUNIT_ASSERT_VECTORS_EQUAL( pd.vertex_by_index( idx ), sub.vertex_by_index( i ), 1e-12 );
    }

    // test element list consistency
    seen.clear();
    elem_map.resize( sub.num_elements() );
    for( i = 0; i < sub.num_elements(); ++i )
    {
        // get index in old patch for element
        Mesh::ElementHandle h    = sub.get_element_handles_array()[i];
        Mesh::ElementHandle* end = pd.get_element_handles_array() + pd.num_nodes();
        Mesh::ElementHandle* ptr = std::find( pd.get_element_handles_array(), end, h );
        CPPUNIT_ASSERT( ptr != end );
        size_t idx = ptr - pd.get_element_handles_array();
        CPPUNIT_ASSERT( idx < pd.num_elements() );
        // put handle in map
        elem_map[i] = idx;
        // make sure we don't have duplicate elements
        CPPUNIT_ASSERT( seen.insert( idx ).second );
        // get elements
        MsqMeshEntity& elem1 = pd.element_by_index( idx );
        MsqMeshEntity& elem2 = sub.element_by_index( i );
        // compare element data
        CPPUNIT_ASSERT_EQUAL( elem1.get_element_type(), elem2.get_element_type() );
        CPPUNIT_ASSERT_EQUAL( elem1.node_count(), elem2.node_count() );
        // get connectivity for elements
        std::vector< size_t > vtx1, vtx2;
        elem1.get_node_indices( vtx1 );
        elem2.get_node_indices( vtx2 );
        CPPUNIT_ASSERT_EQUAL( vtx1.size(), vtx2.size() );
        // compare connectivity
        for( j = 0; j < vtx1.size(); ++j )
        {
            CPPUNIT_ASSERT( vtx1[j] < pd.num_nodes() );
            CPPUNIT_ASSERT( vtx2[j] < sub.num_nodes() );
            CPPUNIT_ASSERT_EQUAL( vtx1[j], vtx_map[vtx2[j]] );
        }
    }

    // test that the subpatch has the elements adjacent to the specified
    // vertex.

    // first get list of adjacent elements in original patch
    seen.clear();
    for( i = 0; i < pd.num_elements(); ++i )
    {
        std::vector< size_t > vtx_list;
        pd.element_by_index( i ).get_node_indices( vtx_list );
        if( std::find( vtx_list.begin(), vtx_list.end(), vtx ) != vtx_list.end() ) seen.insert( i );
    }

    // if 1 layer, then should match element count
    if( layers == 1 )
    {
        CPPUNIT_ASSERT_EQUAL( seen.size(), sub.num_elements() );
    }

    // remove from the set each element in the subpatch
    for( i = 0; i < sub.num_elements(); ++i )
    {
        size_t idx                      = elem_map[i];
        std::set< size_t >::iterator it = seen.find( idx );
        if( it != seen.end() )
        {
            seen.erase( it );
        }
        else
        {
            CPPUNIT_ASSERT( layers > 1 );
        }
    }
    CPPUNIT_ASSERT( seen.empty() );
}

void PatchDataTest::test_sub_patch()
{
    MsqPrintError err( std::cout );
    PatchData pd, sub;
    create_twelve_hex_patch( pd, err );
    CPPUNIT_ASSERT( !err );

    for( unsigned i = 0; i < pd.num_free_vertices(); ++i )
    {
        unsigned layers = i % 2 ? 2 : 1;
        pd.get_subpatch( i, layers, sub, err );
        CPPUNIT_ASSERT( !err );

        check_sub_patch( i, layers, pd, sub );
    }
}

void PatchDataTest::test_patch_contents( bool reorder )
{
    const unsigned NUM_VERTEX  = 15;
    const unsigned NUM_ELEMENT = 9;

    // Mesh data used to populate patch
    // Use a relatively randomized order for verices
    // so patch reordering will result in a changed
    // vertex ordering.
    double coords[3 * NUM_VERTEX]     = { 6, 6, 3,                                      // 0
                                      0, 0, 0, 0, 6, 3, 4, 2, 2, 2, 4, 2, 4, 4, 2,  // 5
                                      0, 6, 3, 2, 2, 1, 2, 6, 3, 4, 0, 2, 6, 3, 3,  // 10
                                      0, 4, 2, 2, 0, 1, 6, 2, 2, 0, 2, 1 };         // 14
    size_t conn[]                     = { 3, 5,  4, 7, 7,  4,  11, 14, 5, 0,  8, 11, 5, 8,  13, 3, 9,
                      6, 10, 5, 3, 13, 12, 7,  14, 1, 10, 0, 5,  7, 12, 9,  3 };
    size_t conn_len[NUM_ELEMENT]      = { 4, 4, 3, 3, 4, 4, 4, 3, 4 };
    EntityTopology types[NUM_ELEMENT] = { QUADRILATERAL, QUADRILATERAL, TRIANGLE, TRIANGLE,     QUADRILATERAL,
                                          QUADRILATERAL, QUADRILATERAL, TRIANGLE, QUADRILATERAL };
    // mark vertices along X and Y axis as fixed
    bool fixed[NUM_VERTEX] = { false, true, true,  false, false, false, true, false,<--- Shadow variable
                               false, true, false, true,  true,  false, false };

    // populate patch data
    PatchData pd;
    MsqPrintError err( std::cout );
    pd.fill( NUM_VERTEX, coords, NUM_ELEMENT, types, conn_len, conn, fixed, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );

    if( reorder ) pd.reorder();

    // count free vertices
    unsigned i, j;<--- Shadowed declaration
    size_t num_free = 0;
    for( i = 0; i < NUM_VERTEX; ++i )
        if( !fixed[i] ) ++num_free;
    CPPUNIT_ASSERT_EQUAL( num_free, pd.num_free_vertices() );

    // NOTE: PatchData will reorder contents either because reorder()
    //       was called or to group vertices by fixed/free status.
    //       We will assume that the handles arrays for vertices and
    //       elements contain the initial positions in the input
    //       arrays used to populate the patch data.

    // Test vertex handles
    std::vector< bool > seen( NUM_VERTEX, false );
    for( i = 0; i < pd.num_nodes(); ++i )
    {
        size_t h = (size_t)( pd.get_vertex_handles_array()[i] );
        CPPUNIT_ASSERT( h < NUM_VERTEX );
        CPPUNIT_ASSERT( !seen[h] );
        seen[h] = true;
    }

    // Test vertex coordinates
    for( i = 0; i < pd.num_nodes(); ++i )
    {
        size_t h      = (size_t)( pd.get_vertex_handles_array()[i] );
        MsqVertex vtx = pd.vertex_by_index( i );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( vtx[0], coords[3 * h], DBL_EPSILON );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( vtx[1], coords[3 * h + 1], DBL_EPSILON );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( vtx[2], coords[3 * h + 2], DBL_EPSILON );
    }

    // Test vertex fixed flags
    for( i = 0; i < pd.num_nodes(); ++i )
    {
        size_t h = (size_t)( pd.get_vertex_handles_array()[i] );
        if( fixed[h] )
        {
            CPPUNIT_ASSERT( i >= pd.num_free_vertices() );
            CPPUNIT_ASSERT( !pd.vertex_by_index( i ).is_free_vertex() );
        }
        else
        {
            CPPUNIT_ASSERT( i < pd.num_free_vertices() );
            CPPUNIT_ASSERT( pd.vertex_by_index( i ).is_free_vertex() );
        }
    }

    // Test element handles
    seen.clear();
    seen.resize( NUM_ELEMENT, false );
    for( i = 0; i < pd.num_elements(); ++i )
    {
        size_t h = (size_t)( pd.get_element_handles_array()[i] );
        CPPUNIT_ASSERT( h < NUM_ELEMENT );
        CPPUNIT_ASSERT( !seen[h] );
        seen[h] = true;
    }

    // Test element types
    for( i = 0; i < pd.num_elements(); ++i )
    {
        size_t h = (size_t)( pd.get_element_handles_array()[i] );
        CPPUNIT_ASSERT_EQUAL( types[h], pd.element_by_index( i ).get_element_type() );
    }

    // Test element connectivity
    for( i = 0; i < pd.num_elements(); ++i )
    {
        size_t h            = (size_t)( pd.get_element_handles_array()[i] );
        MsqMeshEntity& elem = pd.element_by_index( i );
        CPPUNIT_ASSERT_EQUAL( conn_len[h], elem.vertex_count() );
        CPPUNIT_ASSERT_EQUAL( conn_len[h], elem.node_count() );

        // calculate offset in input list for element connectivity
        unsigned conn_pos = 0;
        for( j = 0; j < h; ++j )
            conn_pos += conn_len[j];

        const size_t* elem_conn = elem.get_vertex_index_array();
        for( unsigned j = 0; j < elem.vertex_count(); ++j )<--- Shadow variable
        {
            size_t vh = (size_t)( pd.get_vertex_handles_array()[elem_conn[j]] );
            CPPUNIT_ASSERT_EQUAL( vh, conn[conn_pos] );
            ++conn_pos;
        }
    }
}

void PatchDataTest::test_update_slave_node_coords()
{
    MsqPrintError err( std::cerr );

    // create a patch containing a single 6-node triangle
    // with a) two mid-edge nodes marked as slave vertices and
    // b) with all mid-edge nodes moved away from the center
    // of their corresponding edge.
    const double coords[]    = { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, -0.1, -0.1, 0.6, 0.6, 0.1, -0.1, 0.5, 0.0 };
    const size_t init_conn[] = { 0, 1, 2, 3, 4, 5 };
    const bool fixed[]       = { false, false, false, false, true, false };<--- Shadow variable
    PatchData pd;
    EntityTopology type = TRIANGLE;
    size_t node_per_tri = 6;
    pd.fill( 6, coords, 1, &type, &node_per_tri, init_conn, fixed, err );
    ASSERT_NO_ERROR( err );

    // update_slave_node_coords requires a mapping function
    Settings settings;
    //  TriLagrangeShape tri_func;
    //  settings.set_mapping_function( &tri_func );
    pd.attach_settings( &settings );

    // call the function we're trying to test.
    pd.update_slave_node_coordinates( err );
    ASSERT_NO_ERROR( err );

    // get vertex coordinates in the same order that we passed them in
    MsqMeshEntity elem = pd.element_by_index( 0 );
    const size_t* conn = elem.get_vertex_index_array();
    Vector3D vtx_coords[6];
    for( size_t i = 0; i < 6; ++i )
        vtx_coords[i] = pd.vertex_by_index( conn[i] );

    // check that corner vertex coordinates are unchanged
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( coords ), vtx_coords[0], 1e-12 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( coords + 3 ), vtx_coords[1], 1e-12 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( coords + 6 ), vtx_coords[2], 1e-12 );
    // check that fixed HO node is unchanged
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( coords + 12 ), vtx_coords[4], 1e-12 );
    // check that slave HO nodes were updated
    const Vector3D mid1 = 0.5 * ( Vector3D( coords ) + Vector3D( coords + 3 ) );
    const Vector3D mid2 = 0.5 * ( Vector3D( coords ) + Vector3D( coords + 6 ) );
    CPPUNIT_ASSERT_VECTORS_EQUAL( mid1, vtx_coords[3], 1e-6 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( mid2, vtx_coords[5], 1e-6 );
}

/* This is the input mesh topology
     (0)------(16)-----(1)------(17)-----(2)------(18)-----(3)
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
     (19)      0       (20)      1       (21)      2       (22)
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
     (4)------(23)-----(5)------(24)-----(6)------(25)-----(7)
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
     (26)      3       (27)      4       (28)      5       (29)
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
     (8)------(30)-----(9)------(31)-----(10)-----(32)-----(11)
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
     (33)      6       (34)      7       (35)      8       (36)
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
      |                 |                 |                 |
     (12)-----(37)-----(13)-----(38)-----(14)-----(39)-----(15)
*/
// input mesh definition
const int NUM_VTX = 40, NUM_ELEM = 9, NUM_CORNER = 16;
const double input_coords[3 * NUM_VTX] = { -3.0, 3.0,  0, -1.0, 3.0,  0, 1.0,  3.0,  0, 3.0,  3.0,  0, -3.0, 1.0,  0,
                                           -1.0, 1.0,  0, 1.0,  1.0,  0, 3.0,  1.0,  0, -3.0, -1.0, 0, -1.0, -1.0, 0,
                                           1.0,  -1.0, 0, 3.0,  -1.0, 0, -3.0, -3.0, 0, -1.0, -3.0, 0, 1.0,  -3.0, 0,
                                           3.0,  -3.0, 0, -2.0, 3.0,  0, 0.0,  3.0,  0, 2.0,  3.0,  0, -3.0, 2.0,  0,
                                           -1.0, 2.0,  0, 1.0,  2.0,  0, 3.0,  2.0,  0, -2.0, 1.0,  0, 0.0,  1.0,  0,
                                           2.0,  1.0,  0, -3.0, 0.0,  0, -1.0, 0.0,  0, 1.0,  0.0,  0, 3.0,  0.0,  0,
                                           -2.0, -1.0, 0, 0.0,  -1.0, 0, 2.0,  -1.0, 0, -3.0, -2.0, 0, -1.0, -2.0, 0,
                                           1.0,  -2.0, 0, 3.0,  -2.0, 0, -2.0, -3.0, 0, 0.0,  -3.0, 0, 2.0,  -3.0, 0 };
const bool fixed[NUM_VTX] = { true, true,  true,  true, true,  false, false, true,  true,  false, false, true,  true,<--- Shadowed declaration<--- Shadowed declaration<--- Shadowed declaration
                              true, true,  true,  true, true,  true,  true,  false, false, true,  false, false, false,
                              true, false, false, true, false, false, false, true,  false, false, true };
const size_t input_conn[8 * NUM_ELEM] = { 1,  0,  4,  5,  16, 19, 23, 20, 2,  1,  5,  6,  17, 20, 24, 21, 3,  2,
                                          6,  7,  18, 21, 25, 22, 5,  4,  8,  9,  23, 26, 30, 27, 6,  5,  9,  10,
                                          24, 27, 31, 28, 7,  6,  10, 11, 25, 28, 32, 29, 9,  8,  12, 13, 30, 33,
                                          37, 34, 10, 9,  13, 14, 31, 34, 38, 35, 11, 10, 14, 15, 32, 35, 39, 36 };

const size_t node_per_elem[NUM_ELEM]      = { 8, 8, 8, 8, 8, 8, 8, 8, 8 };
const EntityTopology elem_types[NUM_ELEM] = { QUADRILATERAL, QUADRILATERAL, QUADRILATERAL, QUADRILATERAL, QUADRILATERAL,
                                              QUADRILATERAL, QUADRILATERAL, QUADRILATERAL, QUADRILATERAL };

void PatchDataTest::test_quad8_patch( bool reorder, bool slaved )
{
    // create PatchData
    MsqPrintError err( std::cerr );
    PatchData pd;
    Settings settings;
    settings.set_slaved_ho_node_mode( Settings::SLAVE_NONE );
    if( !slaved )  // default is slaved, so only change settings if no slaved
        pd.attach_settings( &settings );
    pd.fill( NUM_VTX, input_coords, NUM_ELEM, elem_types, node_per_elem, input_conn, fixed, err );
    ASSERT_NO_ERROR( err );

    // reorder if testing that
    if( reorder ) pd.reorder();

    // Check sizes.  Assume that all non-fixed HO nodes are slave vertices
    // unless 'slaved' is false.

    CPPUNIT_ASSERT_EQUAL( NUM_VTX, (int)pd.num_nodes() );
    CPPUNIT_ASSERT_EQUAL( NUM_ELEM, (int)pd.num_elements() );
    int num_free = 0, num_slave = 0, num_fixed = 0;
    for( int i = 0; i < NUM_VTX; ++i )
    {
        if( fixed[i] )
            ++num_fixed;
        else if( i < NUM_CORNER )
            ++num_free;
        else if( slaved )
            ++num_slave;
        else
            ++num_free;
    }
    CPPUNIT_ASSERT_EQUAL( num_free, (int)pd.num_free_vertices() );
    CPPUNIT_ASSERT_EQUAL( num_slave, (int)pd.num_slave_vertices() );
    CPPUNIT_ASSERT_EQUAL( num_fixed, (int)pd.num_fixed_vertices() );

    // Check that vertex handles and vertex coords are correct.
    // Assume that handles array contains input vertex indices.

    for( int i = 0; i < NUM_VTX; ++i )
    {
        const MsqVertex& vtx   = pd.vertex_by_index( i );
        Mesh::VertexHandle hdl = pd.get_vertex_handles_array()[i];
        size_t idx             = (size_t)hdl;
        Vector3D exp_coords( input_coords + 3 * idx );
        if( ( exp_coords - vtx ).length_squared() > 1e-16 )
        {
            std::cerr << "Input Index: " << idx << std::endl;
            std::cerr << "Patch Index: " << i << std::endl;
        }
        CPPUNIT_ASSERT_VECTORS_EQUAL( exp_coords, vtx, 1e-16 );
    }

    // Check that vertex flags are correct.
    // Assume all non-fixed HO noes are slave vertices unless 'slaved' is false.
    // Assume that handles array contains input vertex indices.

    for( int i = 0; i < NUM_VTX; ++i )
    {
        const MsqVertex& vtx   = pd.vertex_by_index( i );
        Mesh::VertexHandle hdl = pd.get_vertex_handles_array()[i];
        size_t idx             = (size_t)hdl;
        if( fixed[idx] )
        {
            CPPUNIT_ASSERT( vtx.is_flag_set( MsqVertex::MSQ_HARD_FIXED ) );
        }
        else if( slaved && idx >= (size_t)NUM_CORNER )
        {
            CPPUNIT_ASSERT( vtx.is_flag_set( MsqVertex::MSQ_DEPENDENT ) );
        }
        else
        {
            CPPUNIT_ASSERT( !vtx.is_flag_set( MsqVertex::MSQ_HARD_FIXED ) );
            CPPUNIT_ASSERT( !vtx.is_flag_set( MsqVertex::MSQ_DEPENDENT ) );
        }
    }

    // Check that element connectivity is correct.
    // Assume that handles array contains input vertex and element indices.

    for( int i = 0; i < NUM_ELEM; ++i )
    {
        MsqMeshEntity& elem = pd.element_by_index( i );
        CPPUNIT_ASSERT_EQUAL( QUADRILATERAL, elem.get_element_type() );
        CPPUNIT_ASSERT_EQUAL( (size_t)4, elem.vertex_count() );
        CPPUNIT_ASSERT_EQUAL( (size_t)8, elem.node_count() );
        CPPUNIT_ASSERT_EQUAL( (size_t)4, elem.corner_count() );
        std::vector< std::size_t > conn;
        elem.get_node_indices( conn );
        CPPUNIT_ASSERT_EQUAL( elem.node_count(), conn.size() );
        for( int j = 0; j < 8; ++j )
            conn[j] = (size_t)pd.get_vertex_handles_array()[conn[j]];
        size_t idx = (size_t)pd.get_element_handles_array()[i];
        ASSERT_ARRAYS_EQUAL( input_conn + 8 * idx, arrptr( conn ), 8 );
    }
}

void PatchDataTest::get_quad8_mesh( Mesh*& mesh_out )
{
    static std::vector< int > fixed_flags( fixed, fixed + NUM_VTX );
    static std::vector< double > coords( input_coords, input_coords + 3 * NUM_VTX );
    static std::vector< unsigned long > conn( input_conn, input_conn + 8 * NUM_ELEM );
    mesh_out = new ArrayMesh( 3, NUM_VTX, arrptr( coords ), arrptr( fixed_flags ), NUM_ELEM, QUADRILATERAL,
                              arrptr( conn ), false, 8 );
}

void PatchDataTest::get_quad8_mesh_and_domain( Mesh*& mesh_out, MeshDomain*& domain_out )
{
    MsqPrintError err( std::cerr );

    get_quad8_mesh( mesh_out );
    DomainClassifier geom;
    Vector3D corners[4]   = { Vector3D( -3, 3, 0 ), Vector3D( 3, 3, 0 ), Vector3D( -3, -3, 0 ), Vector3D( 3, -3, 0 ) };
    MeshDomain* geomarr[] = { new PointDomain( corners[0] ),
                              new PointDomain( corners[1] ),
                              new PointDomain( corners[2] ),
                              new PointDomain( corners[3] ),
                              new LineDomain( corners[0], corners[1] - corners[0] ),
                              new LineDomain( corners[1], corners[2] - corners[1] ),
                              new LineDomain( corners[2], corners[3] - corners[2] ),
                              new LineDomain( corners[3], corners[0] - corners[3] ),
                              new PlanarDomain( PlanarDomain::XY ) };
    int dimarr[]          = { 0, 0, 0, 0, 1, 1, 1, 1, 2 };

    DomainClassifier* domain;
    domain_out = domain = new DomainClassifier;
    DomainClassifier::classify_geometrically( *domain, mesh_out, 1e-6, geomarr, dimarr, 9, err );
    domain->delete_sub_domains( true );
    ASSERT_NO_ERROR( err );
}

void PatchDataTest::test_fixed_by_geom_dim( unsigned dim )
{
    MsqPrintError err( std::cerr );

    Settings settings;
    switch( dim )
    {
        case 0:
            settings.set_fixed_vertex_mode( Settings::FIXED_VERTEX );
            break;
        case 1:
            settings.set_fixed_vertex_mode( Settings::FIXED_CURVE );
            break;
        case 2:
            settings.set_fixed_vertex_mode( Settings::FIXED_SURFACE );
            break;
        default:
            CPPUNIT_ASSERT( false );
    }

    Mesh* mesh         = 0;
    MeshDomain* domain = 0;
    get_quad8_mesh_and_domain( mesh, domain );
    MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( mesh, domain );
    Instruction::initialize_vertex_byte( &mesh_and_domain, &settings, err );
    ASSERT_NO_ERROR( err );

    PatchData pd;
    pd.attach_settings( &settings );
    pd.set_mesh( mesh );
    pd.set_domain( domain );

    std::vector< Mesh::ElementHandle > elems;
    std::vector< Mesh::VertexHandle > verts;
    mesh->get_all_elements( elems, err );
    ASSERT_NO_ERROR( err );
    mesh->get_all_vertices( verts, err );
    ASSERT_NO_ERROR( err );
    pd.set_mesh_entities( elems, verts, err );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT( !elems.empty() );

    std::vector< unsigned short > dims( verts.size() );
    domain->domain_DoF( arrptr( verts ), arrptr( dims ), verts.size(), err );
    ASSERT_NO_ERROR( err );

    for( size_t i = 0; i < pd.num_free_vertices(); ++i )
    {
        Mesh::VertexHandle handle = pd.get_vertex_handles_array()[i];
        unsigned short d;
        domain->domain_DoF( &handle, &d, 1, err );
        ASSERT_NO_ERROR( err );
        CPPUNIT_ASSERT( d > dim );
    }
    for( size_t i = 0; i < pd.num_fixed_vertices(); ++i )
    {
        size_t j                  = i + pd.num_free_vertices() + pd.num_slave_vertices();
        Mesh::VertexHandle handle = pd.get_vertex_handles_array()[j];
        unsigned short d;
        domain->domain_DoF( &handle, &d, 1, err );
        ASSERT_NO_ERROR( err );
        CPPUNIT_ASSERT( d <= dim );
    }

    delete mesh;
    delete domain;
}

void PatchDataTest::get_higher_order_vertices( Mesh* mesh,
                                               std::map< Mesh::VertexHandle, bool >& ho_verts,
                                               bool initial_value,
                                               bool non_fixed_only )
{
    // get mesh data
    MsqPrintError err( std::cerr );
    std::vector< Mesh::ElementHandle > elems;
    std::vector< Mesh::VertexHandle > verts;
    std::vector< size_t > offsets;
    mesh->get_all_elements( elems, err );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT( !elems.empty() );
    mesh->elements_get_attached_vertices( arrptr( elems ), elems.size(), verts, offsets, err );
    CPPUNIT_ASSERT_EQUAL( elems.size() + 1, offsets.size() );
    ASSERT_NO_ERROR( err );
    std::vector< EntityTopology > types( elems.size() );
    mesh->elements_get_topologies( arrptr( elems ), arrptr( types ), elems.size(), err );
    ASSERT_NO_ERROR( err );

    // clear initial state
    ho_verts.clear();

    // for each element, add ho nodes
    for( size_t i = 0; i < elems.size(); ++i )
        for( size_t j = offsets[i] + TopologyInfo::corners( types[i] ); j < offsets[i + 1]; ++j )
            ho_verts[verts[j]] = initial_value;

    if( non_fixed_only )
    {
        std::map< Mesh::VertexHandle, bool >::iterator p;
        std::sort( verts.begin(), verts.end() );
        verts.erase( std::unique( verts.begin(), verts.end() ), verts.end() );
        std::vector< bool > fixed;<--- Shadow variable
        mesh->vertices_get_fixed_flag( arrptr( verts ), fixed, verts.size(), err );
        ASSERT_NO_ERROR( err );
        for( size_t i = 0; i < verts.size(); ++i )
        {
            if( fixed[i] )
            {
                p = ho_verts.find( verts[i] );
                if( p != ho_verts.end() ) ho_verts.erase( p );
            }
        }
    }
}

void PatchDataTest::check_higher_order_vertices_slaved( Mesh* mesh,
                                                        Settings::HigherOrderSlaveMode mode,
                                                        const std::map< Mesh::VertexHandle, bool >& expected )
{
    MsqPrintError err( std::cerr );

    Settings settings;
    settings.set_slaved_ho_node_mode( mode );
    MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( mesh, 0 );
    Instruction::initialize_vertex_byte( &mesh_and_domain, &settings, err );
    ASSERT_NO_ERROR( err );

    PatchData pd;
    pd.attach_settings( &settings );
    pd.set_mesh( mesh );

    std::vector< Mesh::ElementHandle > elements;
    std::vector< Mesh::VertexHandle > vertices;
    mesh->get_all_elements( elements, err );
    ASSERT_NO_ERROR( err );
    pd.set_mesh_entities( elements, vertices, err );
    ASSERT_NO_ERROR( err );

    std::map< Mesh::VertexHandle, bool >::const_iterator p;
    for( size_t i = 0; i < pd.num_nodes(); ++i )
    {
        p          = expected.find( pd.get_vertex_handles_array()[i] );
        bool found = ( p != expected.end() );
        bool exp   = found && p->second;
        bool act   = pd.vertex_by_index( i ).is_flag_set( MsqVertex::MSQ_DEPENDENT );
        CPPUNIT_ASSERT_EQUAL( exp, act );
    }
}

void PatchDataTest::test_patch_data_mesh_slaved_ho_nodes()
{
    Mesh* mesh = 0;
    get_quad8_mesh( mesh );

    std::map< Mesh::VertexHandle, bool > ho_verts;
    get_higher_order_vertices( mesh, ho_verts, true, false );

    check_higher_order_vertices_slaved( mesh, Settings::SLAVE_ALL, ho_verts );
    delete mesh;
}

void PatchDataTest::test_patch_data_mesh_free_ho_nodes()
{
    Mesh* mesh = 0;
    get_quad8_mesh( mesh );

    std::map< Mesh::VertexHandle, bool > ho_verts;
    get_higher_order_vertices( mesh, ho_verts, false );

    check_higher_order_vertices_slaved( mesh, Settings::SLAVE_NONE, ho_verts );
    delete mesh;
}

void PatchDataTest::test_patch_data_mesh_calcualted_ho_nodes()
{
    Mesh* mesh = 0;
    get_quad8_mesh( mesh );

    std::map< Mesh::VertexHandle, bool > ho_verts;
    get_higher_order_vertices( mesh, ho_verts, false );

    // set bit on every other higher-order vertex
    std::map< Mesh::VertexHandle, bool >::iterator i = ho_verts.end();
    std::vector< Mesh::VertexHandle > slaved;
    std::vector< unsigned char > bytes;
    while( i != ho_verts.end() )
    {
        slaved.push_back( i->first );
        bytes.push_back( MsqVertex::MSQ_DEPENDENT );
        i->second = true;
        if( ++i == ho_verts.end() )
            ;
        break;
        ++i;<--- Statements following return, break, continue, goto or throw will never be executed.
    }

    if( !slaved.empty() )
    {
        MsqPrintError err( std::cerr );
        mesh->vertices_set_byte( arrptr( slaved ), arrptr( bytes ), slaved.size(), err );
        ASSERT_NO_ERROR( err );
    }

    check_higher_order_vertices_slaved( mesh, Settings::SLAVE_CALCULATED, ho_verts );
    delete mesh;
}

//! create a wrapper around the real mesh that returns what we
//! want from vertices_get_slaved_flag.
class HoSlavedMesh : public MeshDecorator
{
  public:
    typedef std::map< Mesh::VertexHandle, bool > SMap;
    HoSlavedMesh( Mesh* real_mesh, SMap& slaved ) : MeshDecorator( real_mesh ), slavedVerts( slaved ) {}

    virtual void vertices_get_slaved_flag( const VertexHandle vert_array[],
                                           std::vector< bool >& slaved_flag_array,
                                           size_t num_vtx,
                                           MsqError& err );

  private:
    SMap slavedVerts;
};
void HoSlavedMesh::vertices_get_slaved_flag( const VertexHandle vert_array[],
                                             std::vector< bool >& slaved_flag_array,
                                             size_t num_vtx,
                                             MsqError& err )
{
    slaved_flag_array.resize( num_vtx );
    for( size_t i = 0; i < num_vtx; ++i )
    {
        SMap::iterator j     = slavedVerts.find( vert_array[i] );
        slaved_flag_array[i] = ( j != slavedVerts.end() ) && j->second;
    }
}

void PatchDataTest::test_patch_data_mesh_flagged_ho_nodes()
{
    Mesh* mesh = 0;
    get_quad8_mesh( mesh );

    std::map< Mesh::VertexHandle, bool > ho_verts;
    get_higher_order_vertices( mesh, ho_verts, false );

    // set every other higher-order vertex as slaved
    std::map< Mesh::VertexHandle, bool >::iterator i = ho_verts.end();
    while( i != ho_verts.end() )
    {
        if( ++i == ho_verts.end() )
            ;
        break;
        i->second = true;<--- Statements following return, break, continue, goto or throw will never be executed.
        ++i;
    }

    // create a wrapper mesh that returns what we want
    // from vertices_get_slaved_flag
    HoSlavedMesh wrapper( mesh, ho_verts );

    check_higher_order_vertices_slaved( &wrapper, Settings::SLAVE_FLAG, ho_verts );
    delete mesh;
}