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

    Copyright 2006 Sandia National Laboratories.  Developed at the
    University of Wisconsin--Madison under SNL contract number
    624796.  The U.S. Government and the University of Wisconsin
    retian certain rights to 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

    (2006) [email protected]
    (2010) [email protected]

  ***************************************************************** */

/*! \file TMPQualityMetricTest.cpp

Unit testing for the TMPQualityMetric class
\author Jasno Kraftcheck
*/

#include "IdealShapeTarget.hpp"
#include "MsqMatrix.hpp"
#include "QualityMetricTester.hpp"
#include "Settings.hpp"
#include "UnitUtil.hpp"
#include "PlanarDomain.hpp"
#include "PatchData.hpp"
#include "WeightCalculator.hpp"
#include "ElementPMeanP.hpp"
#include "ElemSampleQM.hpp"

#include <iostream>

using namespace MBMesquite;
using std::cerr;
using std::cout;
using std::endl;

/** Target metric (templatized by dimension) for use in misc. tests.
 *  'evaluate' method records input values and returns a constant.
 */
template < typename B >
class FauxMetric : public B
{
  public:
    int count;
    double value;
    bool rval;
    MsqMatrix< 2, 2 > last_A_2D;
    MsqMatrix< 3, 3 > last_A_3D;

    FauxMetric( double v ) : count( 0 ), value( v ), rval( true ) {}

    std::string get_name() const
    {
        return "Faux";
    }

    bool evaluate( const MsqMatrix< 2, 2 >& A, const MsqMatrix< 2, 2 >& W, double& result, MsqError& )
    {
        last_A_2D = A;
        result    = value;
        ++count;
        return rval;
    }

    bool evaluate( const MsqMatrix< 3, 3 >& A, const MsqMatrix< 3, 3 >& W, double& result, MsqError& )
    {
        last_A_3D = A;
        result    = value;
        ++count;
        return rval;
    }

    bool evaluate( const MsqMatrix< 2, 2 >& T, double& result, MsqError& )
    {
        last_A_2D = T;
        result    = value;
        ++count;
        return rval;
    }

    bool evaluate( const MsqMatrix< 3, 3 >& T, double& result, MsqError& )
    {
        last_A_3D = T;
        result    = value;
        ++count;
        return rval;
    }
};

/** Weight calculator used for testing.  Returns constant weight. */
class ScaleWeight : public WeightCalculator
{
  public:
    ScaleWeight( double s ) : value( s ) {}
    double get_weight( PatchData&, size_t, Sample, MsqError& )
    {
        return value;
    }
    double value;
};

/** wrapper class to force numeric approximation of derivatives */
template < class Base >
class NumericalMetric : public Base
{
  public:
    NumericalMetric( Base* real_metric ) : mMetric( real_metric ) {}

    ~NumericalMetric() {}

    std::string get_name() const
    {
        return "Numerical " + mMetric->get_name();
    }

    bool evaluate( const MsqMatrix< 2, 2 >& A, const MsqMatrix< 2, 2 >& W, double& result, MsqError& err )
    {
        return mMetric->evaluate( A, W, result, err );
    }

    bool evaluate( const MsqMatrix< 3, 3 >& A, const MsqMatrix< 3, 3 >& W, double& result, MsqError& err )
    {
        return mMetric->evaluate( A, W, result, err );
    }

    bool evaluate( const MsqMatrix< 2, 2 >& T, double& result, MsqError& err )
    {
        return mMetric->evaluate( T, result, err );
    }

    bool evaluate( const MsqMatrix< 3, 3 >& T, double& result, MsqError& err )
    {
        return mMetric->evaluate( T, result, err );
    }

  private:
    Base* mMetric;
};

/** Simple target metric for testing first partial derivatives.
 *  \f$\mu(A,W) = |A|^2\f$
 *  \f$\frac{\partial\mu}{\partial \A} = 2 A \f$
 */
template < class Base >
class TestGradTargetMetric : public Base
{
  public:
    std::string get_name() const
    {
        return "TestGrad";
    }

    bool evaluate( const MsqMatrix< 2, 2 >& T, double& result, MsqError& err )
    {
        result = sqr_Frobenius( T );
        return true;
    }

    bool evaluate( const MsqMatrix< 2, 2 >& A, const MsqMatrix< 2, 2 >&, double& result, MsqError& err )
    {
        return evaluate( A, result, err );
    }

    bool evaluate_with_grad( const MsqMatrix< 2, 2 >& T, double& result, MsqMatrix< 2, 2 >& d, MsqError& err )
    {
        result = sqr_Frobenius( T );
        d      = 2 * T;
        return true;
    }

    bool evaluate_with_grad( const MsqMatrix< 2, 2 >& A,
                             const MsqMatrix< 2, 2 >&,
                             double& result,
                             MsqMatrix< 2, 2 >& d,
                             MsqError& err )
    {
        return evaluate_with_grad( A, result, d, err );
    }

    bool evaluate( const MsqMatrix< 3, 3 >& T, double& result, MsqError& err )
    {
        result = sqr_Frobenius( T );
        return true;
    }

    bool evaluate( const MsqMatrix< 3, 3 >& A, const MsqMatrix< 3, 3 >&, double& result, MsqError& err )
    {
        return evaluate( A, result, err );
    }

    bool evaluate_with_grad( const MsqMatrix< 3, 3 >& T, double& result, MsqMatrix< 3, 3 >& d, MsqError& err )
    {
        result = sqr_Frobenius( T );
        d      = 2 * T;
        return true;
    }

    bool evaluate_with_grad( const MsqMatrix< 3, 3 >& A,
                             const MsqMatrix< 3, 3 >&,
                             double& result,
                             MsqMatrix< 3, 3 >& d,
                             MsqError& err )
    {
        return evaluate_with_grad( A, result, d, err );
    }
};

/* class to force evaluation of mapping function only at element center
 * so that we can re-use tests from QualityMetricTester that work only
 * for element-based metrics (TMP metric is "element based" if only one
 * sample in each element.)
 */
class CenterMF2D : public MappingFunction2D
{
  public:
    CenterMF2D( const MappingFunction2D* real_mf ) : myFunc( real_mf ){};
    EntityTopology element_topology() const
    {
        return myFunc->element_topology();
    }
    int num_nodes() const
    {
        return myFunc->num_nodes();
    }
    NodeSet sample_points( NodeSet ) const
    {
        NodeSet s;
        s.set_mid_face_node( 0 );
        return s;
    }
    void coefficients( Sample l, NodeSet s, double* c, size_t* i, size_t& n, MsqError& e ) const
    {
        myFunc->coefficients( l, s, c, i, n, e );
    }
    void derivatives( Sample l, NodeSet s, size_t* i, MsqVector< 2 >* c, size_t& n, MsqError& e ) const
    {
        myFunc->derivatives( l, s, i, c, n, e );
    }

  private:
    const MappingFunction2D* myFunc;
};
class CenterMF3D : public MappingFunction3D
{
  public:
    CenterMF3D( const MappingFunction3D* real_mf ) : myFunc( real_mf ){};
    EntityTopology element_topology() const
    {
        return myFunc->element_topology();
    }
    int num_nodes() const
    {
        return myFunc->num_nodes();
    }
    NodeSet sample_points( NodeSet ) const
    {
        NodeSet s;
        s.set_mid_region_node();
        return s;
    }
    void coefficients( Sample l, NodeSet s, double* c, size_t* i, size_t& n, MsqError& e ) const
    {
        myFunc->coefficients( l, s, c, i, n, e );
    }
    void derivatives( Sample l, NodeSet s, size_t* i, MsqVector< 3 >* c, size_t& n, MsqError& e ) const
    {
        myFunc->derivatives( l, s, i, c, n, e );
    }

  private:
    const MappingFunction3D* myFunc;
};

// Define a target calculator that returns targets for
// ideally shaped elements, but also includes orientation
// information (aligning surface elements to the xy plane
// with the first column of the jacobian in the x direction).
class IdealShapeXY : public IdealShapeTarget
{
  public:
    bool have_surface_orient() const
    {
        return true;
    }
    bool get_surface_target( PatchData& pd, size_t element, Sample sample, MsqMatrix< 3, 2 >& W_out, MsqError& err )
    {
        MsqMatrix< 2, 2 > W;
        bool rval = get_2D_target( pd, element, sample, W, err );
        W_out.set_row( 0, W.row( 0 ) );
        W_out.set_row( 1, W.row( 1 ) );
        W_out.set_row( 2, MsqMatrix< 1, 2 >( 0.0 ) );
        return rval;
    }
};

#define REGISTER_TMP_TESTS                                      \
                                                                \
    CPPUNIT_TEST( test_negate_flag );                           \
    CPPUNIT_TEST( test_supported_types );                       \
    CPPUNIT_TEST( test_get_evaluations );                       \
    CPPUNIT_TEST( test_get_element_evaluations );               \
                                                                \
    CPPUNIT_TEST( test_evaluate_2D );                           \
    CPPUNIT_TEST( test_evaluate_surface );                      \
    CPPUNIT_TEST( test_evaluate_3D );                           \
    CPPUNIT_TEST( test_evaluate_2D_weight );                    \
    CPPUNIT_TEST( test_evaluate_surface_weight );               \
    CPPUNIT_TEST( test_evaluate_3D_weight );                    \
    CPPUNIT_TEST( test_2d_eval_ortho_quad );                    \
    CPPUNIT_TEST( test_surf_eval_ortho_quad );                  \
    CPPUNIT_TEST( test_3d_eval_ortho_hex );                     \
                                                                \
    CPPUNIT_TEST( test_sample_indices );                        \
    CPPUNIT_TEST( test_evaluate_with_indices );                 \
    CPPUNIT_TEST( test_evaluate_fixed_indices );                \
                                                                \
    CPPUNIT_TEST( test_gradient_2D );                           \
    CPPUNIT_TEST( test_gradient_surface );                      \
    CPPUNIT_TEST( test_gradient_3D );                           \
    CPPUNIT_TEST( compare_indices_and_gradient );               \
    CPPUNIT_TEST( test_ideal_element_gradient );                \
    CPPUNIT_TEST( compare_analytical_and_numerical_gradient );  \
    CPPUNIT_TEST( test_weighted_gradients );                    \
    CPPUNIT_TEST( test_gradient_with_fixed_vertices );          \
                                                                \
    CPPUNIT_TEST( compare_indices_and_hessian );                \
    CPPUNIT_TEST( compare_gradient_and_hessian );               \
    CPPUNIT_TEST( compare_analytical_and_numerical_hessians );  \
    CPPUNIT_TEST( test_symmetric_hessian_diagonal );            \
    CPPUNIT_TEST( test_weighted_hessians );                     \
    CPPUNIT_TEST( test_hessian_with_fixed_vertices );           \
                                                                \
    CPPUNIT_TEST( compare_indices_and_diagonal );               \
    CPPUNIT_TEST( compare_gradient_and_diagonal );              \
    CPPUNIT_TEST( compare_analytical_and_numerical_diagonals ); \
    CPPUNIT_TEST( test_weighted_diagonals );                    \
    CPPUNIT_TEST( test_diagonal_with_fixed_vertices );

static double col_dot_prod( MsqMatrix< 2, 2 >& m )<--- Parameter 'm' can be declared with const<--- Parameter 'm' can be declared with const
{
    return m( 0, 0 ) * m( 0, 1 ) + m( 1, 0 ) * m( 1, 1 );
}

template < class QMType >
class TMPTypes
{
};

template < class QMType >
class TMPQualityMetricTest : public CppUnit::TestFixture
{
  protected:
    QualityMetricTester tester;

    Settings settings;
    IdealShapeTarget ideal;
    IdealShapeXY surf_target;
    ScaleWeight e_weight;

    FauxMetric< typename TMPTypes< QMType >::MetricType > faux_pi, faux_zero, faux_two;
    typename TMPTypes< QMType >::TestType test_metric;
    NumericalMetric< typename QMType::MetricType > num_metric;
    QMType test_qm, test_qm_surf, zero_qm, weight_qm, center_qm;
    Settings centerOnly;
    CenterMF2D triCenter, quadCenter;
    CenterMF3D tetCenter, pyrCenter, priCenter, hexCenter;

  public:
    TMPQualityMetricTest()
        : tester( QualityMetricTester::ALL_FE_EXCEPT_SEPTAHEDRON, &settings ), e_weight( 2.7182818284590451 ),
          faux_pi( 3.14159 ), faux_zero( 0.0 ), faux_two( 2.0 ), num_metric( &test_metric ),
          test_qm( &ideal, &num_metric ), test_qm_surf( &surf_target, &num_metric ), zero_qm( &ideal, &faux_zero ),
          weight_qm( &ideal, &e_weight, &test_metric ), center_qm( &ideal, &test_metric ),
          triCenter( centerOnly.get_mapping_function_2D( TRIANGLE ) ),
          quadCenter( centerOnly.get_mapping_function_2D( QUADRILATERAL ) ),
          tetCenter( centerOnly.get_mapping_function_3D( TETRAHEDRON ) ),
          pyrCenter( centerOnly.get_mapping_function_3D( PYRAMID ) ),
          priCenter( centerOnly.get_mapping_function_3D( PRISM ) ),
          hexCenter( centerOnly.get_mapping_function_3D( HEXAHEDRON ) )
    {
        centerOnly.set_mapping_function( &triCenter );
        centerOnly.set_mapping_function( &quadCenter );
        centerOnly.set_mapping_function( &tetCenter );
        centerOnly.set_mapping_function( &pyrCenter );
        centerOnly.set_mapping_function( &priCenter );
        centerOnly.set_mapping_function( &hexCenter );
        tester.ideal_pyramid_base_equals_height( true );
    }

    void test_negate_flag()
    {
        CPPUNIT_ASSERT_EQUAL( 1, zero_qm.get_negate_flag() );
    }
    void test_supported_types()
    {
        tester.test_supported_element_types( &zero_qm );
    }
    void test_get_evaluations()
    {
        QMType edge_metric( &ideal, &faux_zero );
        tester.test_get_sample_evaluations( &zero_qm );
        tester.test_get_sample_evaluations( &edge_metric );
    }
    void test_get_element_evaluations()
    {
        QMType edge_metric( &ideal, &faux_zero );
        tester.test_get_in_element_evaluations( &zero_qm );
        tester.test_get_in_element_evaluations( &edge_metric );
    }

    void test_evaluate_2D();
    void test_evaluate_surface();
    void test_evaluate_3D();

    void test_evaluate_2D_weight()
    {
        MsqPrintError err( cout );
        PatchData pd;
        bool rval;
        double value;

        QMType m( &ideal, &e_weight, &faux_pi );
        tester.get_ideal_element( TRIANGLE, true, pd );
        rval = m.evaluate( pd, 0, value, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( rval );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_pi.value * e_weight.value, value, DBL_EPSILON );
    }

    void test_evaluate_surface_weight()
    {
        MsqPrintError err( cout );
        PatchData pd;
        bool rval;
        double value;

        QMType m( &surf_target, &e_weight, &faux_pi );

        tester.get_ideal_element( TRIANGLE, true, pd );
        rval = m.evaluate( pd, 0, value, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( rval );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_pi.value * e_weight.value, value, DBL_EPSILON );
    }

    void test_evaluate_3D_weight()
    {
        MsqPrintError err( cout );
        PatchData pd;
        bool rval;
        double value;

        QMType m( &ideal, &e_weight, &faux_two );

        tester.get_ideal_element( PRISM, true, pd );
        rval = m.evaluate( pd, 0, value, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( rval );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_two.value * e_weight.value, value, DBL_EPSILON );
    }

    void test_2d_eval_ortho_quad()
    {
        MsqPrintError err( cout );
        PatchData pd;
        bool rval;
        double value;

        QMType m( &ideal, &faux_zero );
        faux_zero.count = 0;

        tester.get_ideal_element( QUADRILATERAL, true, pd );
        rval = m.evaluate( pd, 0, value, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( rval );
        CPPUNIT_ASSERT_EQUAL( 1, faux_zero.count );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, col_dot_prod( faux_zero.last_A_2D ), DBL_EPSILON );
    }

    void test_surf_eval_ortho_quad()
    {
        MsqPrintError err( cout );
        PatchData pd;
        bool rval;
        double value;

        QMType m( &surf_target, &faux_zero );
        faux_zero.count = 0;

        tester.get_ideal_element( QUADRILATERAL, true, pd );
        rval = m.evaluate( pd, 0, value, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( rval );
        CPPUNIT_ASSERT_EQUAL( 1, faux_zero.count );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, col_dot_prod( faux_zero.last_A_2D ), DBL_EPSILON );
    }

    void test_3d_eval_ortho_hex()
    {
        MsqPrintError err( cout );
        PatchData pd;
        bool rval;
        double value;

        QMType m( &ideal, &faux_zero );
        faux_zero.count = 0;

        tester.get_ideal_element( HEXAHEDRON, true, pd );
        rval = m.evaluate( pd, 0, value, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( rval );
        CPPUNIT_ASSERT_EQUAL( 1, faux_zero.count );

        // test that columns are orthogonal for ideal hex element
        MsqMatrix< 3, 3 > A = faux_zero.last_A_3D;
        CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A.column( 0 ) % A.column( 1 ), 1e-6 );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A.column( 0 ) % A.column( 2 ), 1e-6 );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A.column( 1 ) % A.column( 2 ), 1e-6 );
    }

    void test_gradient_common( TargetCalculator* tc );
    void test_gradient_2D()
    {
        test_gradient_common( &ideal );
    }
    void test_gradient_surface()
    {
        test_gradient_common( &surf_target );
    }
    void test_gradient_3D();

    void test_sample_indices()
    {
        tester.test_get_sample_indices( &zero_qm );
    }
    void test_evaluate_with_indices()
    {
        tester.compare_eval_and_eval_with_indices( &zero_qm );
    }
    void test_evaluate_fixed_indices()
    {
        tester.test_get_indices_fixed( &zero_qm );
    }

    void compare_indices_and_gradient()
    {
        tester.compare_eval_with_indices_and_eval_with_gradient( &test_qm );
        tester.compare_eval_with_indices_and_eval_with_gradient( &test_qm_surf );
    }
    void test_ideal_element_gradient()
    {
        tester.test_ideal_element_zero_gradient( &test_qm, false );
        tester.test_ideal_element_zero_gradient( &test_qm_surf, false );
    }
    void compare_analytical_and_numerical_gradient()
    {
        compare_analytical_and_numerical_gradients( &test_qm );
        compare_analytical_and_numerical_gradients( &test_qm_surf );
    }
    void test_weighted_gradients()
    {
        compare_analytical_and_numerical_gradients( &weight_qm );
    }
    void test_gradient_with_fixed_vertices()
    {
        tester.test_gradient_with_fixed_vertex( &center_qm, &centerOnly );
    }

    void compare_indices_and_hessian()
    {
        tester.compare_eval_with_indices_and_eval_with_hessian( &test_qm );
        tester.compare_eval_with_indices_and_eval_with_hessian( &test_qm_surf );
    }
    void compare_gradient_and_hessian()
    {
        tester.compare_eval_with_grad_and_eval_with_hessian( &test_qm );
        tester.compare_eval_with_grad_and_eval_with_hessian( &test_qm_surf );
    }
    void compare_analytical_and_numerical_hessians()
    {
        compare_analytical_and_numerical_hessians( &test_qm );
        compare_analytical_and_numerical_hessians( &test_qm_surf );
    }
    void test_symmetric_hessian_diagonal()
    {
        tester.test_symmetric_Hessian_diagonal_blocks( &test_qm );
        tester.test_symmetric_Hessian_diagonal_blocks( &test_qm_surf );
    }
    void test_weighted_hessians()
    {
        compare_analytical_and_numerical_hessians( &weight_qm );
    }
    void test_hessian_with_fixed_vertices()
    {
        tester.test_hessian_with_fixed_vertex( &center_qm, &centerOnly );
    }

    void compare_indices_and_diagonal()
    {
        tester.compare_eval_with_indices_and_eval_with_diagonal( &test_qm );
        tester.compare_eval_with_indices_and_eval_with_diagonal( &test_qm_surf );
    }
    void compare_gradient_and_diagonal()
    {
        tester.compare_eval_with_grad_and_eval_with_diagonal( &test_qm );
        tester.compare_eval_with_grad_and_eval_with_diagonal( &test_qm_surf );
    }
    void compare_analytical_and_numerical_diagonals()
    {
        compare_analytical_and_numerical_diagonals( &test_qm );
        compare_analytical_and_numerical_diagonals( &test_qm_surf );
    }
    void test_weighted_diagonals()
    {
        compare_analytical_and_numerical_diagonals( &weight_qm );
    }
    void test_diagonal_with_fixed_vertices()
    {
        tester.test_diagonal_with_fixed_vertex( &center_qm, &centerOnly );
    }

    // Delcare specialized versions of the functions from
    // QualityMetricTester because we surface elements must
    // be handled differently.  For a surface element in the XY plane,
    // the finite difference approximations of the derivatives will
    // have non-zero values for derivatives wrt Z coordinates while the
    // analytical derivative calculations will return all derivatives
    // wrt Z coordiantes as zero.

    void get_nonideal_element( EntityTopology type, PatchData& pd )
    {
        tester.get_nonideal_element( type, pd, true );
        // Callers assume surface elements are in XY plane.
        // Verify this assumption.
        if( TopologyInfo::dimension( type ) == 2 )
        {
            for( size_t i = 0; i < pd.num_nodes(); ++i )
            {
                CPPUNIT_ASSERT_DOUBLES_EQUAL( pd.vertex_by_index( i )[2], 0.0, 1e-6 );
            }
        }
    }

    void compare_analytical_and_numerical_gradients( QualityMetric* qm )
    {
        PatchData pd;
        const EntityTopology types[] = { TRIANGLE, QUADRILATERAL, TETRAHEDRON, PYRAMID, PRISM, HEXAHEDRON };
        const int num_types          = sizeof( types ) / sizeof( types[0] );
        for( int i = 0; i < num_types; ++i )
        {
            get_nonideal_element( types[i], pd );
            compare_analytical_and_numerical_gradients( qm, pd, TopologyInfo::dimension( types[i] ) );
        }
    }

    void compare_analytical_and_numerical_hessians( QualityMetric* qm );
    void compare_analytical_and_numerical_diagonals( QualityMetric* qm );
    void compare_analytical_and_numerical_gradients( QualityMetric* qm, PatchData&, int dim );
};

// CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(TMPQualityMetricTest, "TMPQualityMetricTest");
// CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(TMPQualityMetricTest, "Unit");

template < class QMType >
inline void TMPQualityMetricTest< QMType >::test_evaluate_2D()
{
    MsqPrintError err( cout );
    PatchData pd;
    bool rval;
    double value;

    QMType m( &ideal, &faux_pi );

    // test with aligned elements
    faux_pi.count = 0;
    tester.get_ideal_element( QUADRILATERAL, true, pd );
    rval = m.evaluate( pd, 0, value, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( rval );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_pi.value, value, DBL_EPSILON );
    CPPUNIT_ASSERT_EQUAL( 1, faux_pi.count );

    // test that columns are orthogonal for ideal quad element
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, col_dot_prod( faux_pi.last_A_2D ), 1e-6 );

    // test with an element rotated about X-axis
    faux_pi.count = 0;
    tester.get_ideal_element( QUADRILATERAL, true, pd );
    // rotate by 90 degrees about X axis
    for( size_t i = 0; i < pd.num_nodes(); ++i )
    {
        Vector3D orig = pd.vertex_by_index( i );
        Vector3D newp( orig[0], -orig[2], orig[1] );
        pd.set_vertex_coordinates( newp, i, err );
    }
    rval = m.evaluate( pd, 0, value, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( rval );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_pi.value, value, DBL_EPSILON );
    CPPUNIT_ASSERT_EQUAL( 1, faux_pi.count );

    // test with an element rotated about Y-axis
    faux_pi.count = 0;
    tester.get_ideal_element( TRIANGLE, true, pd );
    // rotate by -90 degrees about Y axis
    for( size_t i = 0; i < pd.num_nodes(); ++i )
    {
        Vector3D orig = pd.vertex_by_index( i );
        Vector3D newp( orig[2], orig[1], -orig[0] );
        pd.set_vertex_coordinates( newp, i, err );
    }
    rval = m.evaluate( pd, 0, value, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( rval );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_pi.value, value, DBL_EPSILON );
    CPPUNIT_ASSERT_EQUAL( 1, faux_pi.count );
}

template < class QMType >
inline void TMPQualityMetricTest< QMType >::test_evaluate_surface()
{
    MsqPrintError err( cout );
    PatchData pd;
    bool rval;
    double value;

    QMType m( &surf_target, &faux_pi );

    // test with aligned elements
    faux_pi.count = 0;
    tester.get_ideal_element( QUADRILATERAL, true, pd );
    rval = m.evaluate( pd, 0, value, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( rval );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_pi.value, value, DBL_EPSILON );
    CPPUNIT_ASSERT_EQUAL( 1, faux_pi.count );

    // test that columns are orthogonal for ideal quad element
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, col_dot_prod( faux_pi.last_A_2D ), 1e-6 );

    // test with an element rotated about X-axis
    faux_pi.count = 0;
    tester.get_ideal_element( QUADRILATERAL, true, pd );
    // rotate by 90 degrees about X axis
    for( size_t i = 0; i < pd.num_nodes(); ++i )
    {
        Vector3D orig = pd.vertex_by_index( i );
        Vector3D newp( orig[0], -orig[2], orig[1] );
        pd.set_vertex_coordinates( newp, i, err );
    }
    rval = m.evaluate( pd, 0, value, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( rval );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_pi.value, value, DBL_EPSILON );
    CPPUNIT_ASSERT_EQUAL( 1, faux_pi.count );

    // test with an element rotated about Y-axis
    faux_pi.count = 0;
    tester.get_ideal_element( TRIANGLE, true, pd );
    // rotate by -90 degrees about Y axis
    for( size_t i = 0; i < pd.num_nodes(); ++i )
    {
        Vector3D orig = pd.vertex_by_index( i );
        Vector3D newp( orig[2], orig[1], -orig[0] );
        pd.set_vertex_coordinates( newp, i, err );
    }
    rval = m.evaluate( pd, 0, value, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( rval );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_pi.value, value, DBL_EPSILON );
    CPPUNIT_ASSERT_EQUAL( 1, faux_pi.count );
}

template < class QMType >
inline void TMPQualityMetricTest< QMType >::test_evaluate_3D()
{
    MsqPrintError err( cout );
    PatchData pd;
    bool rval;
    double value;

    QMType m( &ideal, &faux_two );

    // test with aligned elements
    faux_two.count = 0;
    tester.get_ideal_element( HEXAHEDRON, true, pd );
    rval = m.evaluate( pd, 0, value, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( rval );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_two.value, value, DBL_EPSILON );
    CPPUNIT_ASSERT_EQUAL( 1, faux_two.count );

    // test that columns are orthogonal for ideal hex element
    MsqMatrix< 3, 3 > A = faux_two.last_A_3D;
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A.column( 0 ) % A.column( 1 ), 1e-6 );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A.column( 0 ) % A.column( 2 ), 1e-6 );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A.column( 1 ) % A.column( 2 ), 1e-6 );

    // test with rotated element
    faux_two.count = 0;
    tester.get_ideal_element( TETRAHEDRON, true, pd );
    // rotate by 90-degrees about X axis
    for( size_t i = 0; i < pd.num_nodes(); ++i )
    {
        Vector3D orig = pd.vertex_by_index( i );
        Vector3D newp( orig[0], -orig[2], orig[1] );
        pd.set_vertex_coordinates( newp, i, err );
    }
    rval = m.evaluate( pd, 0, value, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( rval );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( faux_two.value, value, DBL_EPSILON );
    CPPUNIT_ASSERT_EQUAL( 1, faux_two.count );
}

template < class QMType >
inline void TMPQualityMetricTest< QMType >::test_gradient_common( TargetCalculator* tc )
{
    MsqPrintError err( std::cout );

    // check for expected value at center of flattened hex

    // construct flattened hex
    const double y          = 0.5;
    const double vertices[] = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, y, 0.0, 0.0, y, 0.0 };
    size_t conn[8]          = { 0, 1, 2, 3 };
    PatchData pd;
    pd.fill( 4, vertices, 1, QUADRILATERAL, conn, 0, err );
    ASSERT_NO_ERROR( err );

    // calculate Jacobian matrix at element center

    // derivatives of bilinear map at quad center
    const double deriv_vals[] = { -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5 };
    MsqMatrix< 4, 2 > coeff_derivs( deriv_vals );
    MsqMatrix< 4, 3 > coords( vertices );
    MsqMatrix< 3, 2 > J = transpose( coords ) * coeff_derivs;
    // calculate expected metric value
    const double expt_val = sqr_Frobenius( J );
    // calculate derivative for each element vertex
    MsqVector< 3 > expt_grad[4];
    for( int v = 0; v < 4; ++v )
        expt_grad[v] = 2 * J * transpose( coeff_derivs.row( v ) );

    // construct metric
    pd.attach_settings( &settings );
    TestGradTargetMetric< typename TMPTypes< QMType >::MetricType > tm;
    // IdealShapeTarget tc;
    QMType m( tc, &tm );
    PlanarDomain plane( PlanarDomain::XY );
    pd.set_domain( &plane );

    // evaluate metric
    double act_val;
    std::vector< size_t > indices;
    std::vector< Vector3D > act_grad;
    size_t h = ElemSampleQM::handle( Sample( 2, 0 ), 0 );
    m.evaluate_with_gradient( pd, h, act_val, indices, act_grad, err );
    ASSERT_NO_ERROR( err );

    // compare values
    CPPUNIT_ASSERT_DOUBLES_EQUAL( expt_val, act_val, 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[0]].data() ), act_grad[0], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[1]].data() ), act_grad[1], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[2]].data() ), act_grad[2], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[3]].data() ), act_grad[3], 1e-10 );

    // check numerical approx of gradient
    m.QualityMetric::evaluate_with_gradient( pd, h, act_val, indices, act_grad, err );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( expt_val, act_val, 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[0]].data() ), act_grad[0], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[1]].data() ), act_grad[1], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[2]].data() ), act_grad[2], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[3]].data() ), act_grad[3], 1e-5 );
}

template < class QMType >
inline void TMPQualityMetricTest< QMType >::test_gradient_3D()
{
    MsqPrintError err( std::cout );

    // check for expected value at center of flattened hex

    // construct flattened hex
    const double z          = 0.5;
    const double vertices[] = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0,
                                0.0, 0.0, z,   1.0, 0.0, z,   1.0, 1.0, z,   0.0, 1.0, z };
    size_t conn[8]          = { 0, 1, 2, 3, 4, 5, 6, 7 };
    PatchData pd;
    pd.fill( 8, vertices, 1, HEXAHEDRON, conn, 0, err );
    ASSERT_NO_ERROR( err );

    // calculate Jacobian matrix at element center

    // derivatives of trilinear map at hex center
    const double deriv_vals[8][3] = { { -0.25, -0.25, -0.25 }, { 0.25, -0.25, -0.25 }, { 0.25, 0.25, -0.25 },
                                      { -0.25, 0.25, -0.25 },  { -0.25, -0.25, 0.25 }, { 0.25, -0.25, 0.25 },
                                      { 0.25, 0.25, 0.25 },    { -0.25, 0.25, 0.25 } };
    MsqMatrix< 8, 3 > coeff_derivs( deriv_vals );
    MsqMatrix< 8, 3 > coords( vertices );
    MsqMatrix< 3, 3 > J = transpose( coords ) * coeff_derivs;
    // calculate expected metric value
    const double expt_val = sqr_Frobenius( J );
    // calculate derivative for each element vertex
    MsqVector< 3 > expt_grad[8];
    for( int v = 0; v < 8; ++v )
        expt_grad[v] = 2 * J * transpose( coeff_derivs.row( v ) );

    // construct metric
    pd.attach_settings( &settings );
    TestGradTargetMetric< typename TMPTypes< QMType >::MetricType > tm;
    IdealShapeTarget tc;
    QMType m( &tc, 0, &tm );

    // evaluate metric
    double act_val;
    std::vector< size_t > indices;
    std::vector< Vector3D > act_grad;
    size_t h = ElemSampleQM::handle( Sample( 3, 0 ), 0 );
    m.evaluate_with_gradient( pd, h, act_val, indices, act_grad, err );
    ASSERT_NO_ERROR( err );

    // compare values
    CPPUNIT_ASSERT_DOUBLES_EQUAL( expt_val, act_val, 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[0]].data() ), act_grad[0], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[1]].data() ), act_grad[1], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[2]].data() ), act_grad[2], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[3]].data() ), act_grad[3], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[4]].data() ), act_grad[4], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[5]].data() ), act_grad[5], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[6]].data() ), act_grad[6], 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[7]].data() ), act_grad[7], 1e-10 );

    // check numerical approx of gradient
    m.QualityMetric::evaluate_with_gradient( pd, h, act_val, indices, act_grad, err );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( expt_val, act_val, 1e-10 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[0]].data() ), act_grad[0], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[1]].data() ), act_grad[1], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[2]].data() ), act_grad[2], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[3]].data() ), act_grad[3], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[4]].data() ), act_grad[4], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[5]].data() ), act_grad[5], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[6]].data() ), act_grad[6], 1e-5 );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( expt_grad[indices[7]].data() ), act_grad[7], 1e-5 );
}

template < class QMType >
inline void TMPQualityMetricTest< QMType >::compare_analytical_and_numerical_gradients( QualityMetric* qm,
                                                                                        PatchData& pd,
                                                                                        int dim )
{
    MsqPrintError err( std::cout );

    std::vector< size_t > handles, indices1, indices2;
    std::vector< Vector3D > grad1, grad2;
    double qm_val1, qm_val2;
    bool rval;

    qm->get_evaluations( pd, handles, false, err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
    CPPUNIT_ASSERT( !handles.empty() );
    for( size_t j = 0; j < handles.size(); ++j )
    {
        rval = qm->QualityMetric::evaluate_with_gradient( pd, handles[j], qm_val1, indices1, grad1, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( rval );

        // For analytical gradient of a 2D element in the XY plane,
        // we expect all Z terms to be zero.
        if( dim == 2 )
            for( size_t k = 0; k < grad1.size(); ++k )
                grad1[k][2] = 0.0;

        rval = qm->evaluate_with_gradient( pd, handles[j], qm_val2, indices2, grad2, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( rval );

        CPPUNIT_ASSERT_DOUBLES_EQUAL( qm_val1, qm_val2, 1e-6 );
        CPPUNIT_ASSERT_EQUAL( indices1.size(), indices2.size() );
        CPPUNIT_ASSERT( !indices1.empty() );

        std::vector< size_t >::iterator it1, it2;
        for( it1 = indices1.begin(); it1 != indices1.end(); ++it1 )
        {
            it2 = std::find( indices2.begin(), indices2.end(), *it1 );
            CPPUNIT_ASSERT( it2 != indices2.end() );

            size_t idx1 = it1 - indices1.begin();
            size_t idx2 = it2 - indices2.begin();
            CPPUNIT_ASSERT_VECTORS_EQUAL( grad1[idx1], grad2[idx2], 0.01 );
        }
    }
}

// Delcare specialized versions of the functions from
// QualityMetricTester because we surface elements must
// be handled differently.  For a surface element in the XY plane,
// the finite difference approximations of the derivatives will
// have non-zero values for derivatives wrt Z coordinates while the
// analytical derivative calculations will return all derivatives
// wrt Z coordiantes as zero.
template < class QMType >
inline void TMPQualityMetricTest< QMType >::compare_analytical_and_numerical_hessians( QualityMetric* qm )
{
    MsqPrintError err( std::cout );
    PatchData pd;
    const EntityTopology types[] = { TRIANGLE, QUADRILATERAL, TETRAHEDRON, PYRAMID, PRISM, HEXAHEDRON };
    const int num_types          = sizeof( types ) / sizeof( types[0] );
    for( int i = 0; i < num_types; ++i )
    {
        get_nonideal_element( types[i], pd );

        std::vector< size_t > handles, indices1, indices2;
        std::vector< Vector3D > grad1, grad2;
        std::vector< Matrix3D > Hess1, Hess2;
        double qm_val1, qm_val2;
        bool rval;

        qm->get_evaluations( pd, handles, false, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( !handles.empty() );
        for( size_t j = 0; j < handles.size(); ++j )
        {
            rval = qm->QualityMetric::evaluate_with_Hessian( pd, handles[j], qm_val1, indices1, grad1, Hess1, err );
            CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
            CPPUNIT_ASSERT( rval );

            // For analytical gradient of a 2D element in the XY plane,
            // we expect all Z terms to be zero.
#ifdef PLANAR_HESSIAN
            if( TopologyInfo::dimension( types[i] ) == 2 )
                for( size_t k = 0; k < Hess1.size(); ++k )
                    Hess1[k]( 0, 2 ) = Hess1[k]( 1, 2 ) = Hess1[k]( 2, 0 ) = Hess1[k]( 2, 1 ) = Hess1[k]( 2, 2 ) = 0.0;
#endif

            rval = qm->evaluate_with_Hessian( pd, handles[j], qm_val2, indices2, grad2, Hess2, err );
            CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
            CPPUNIT_ASSERT( rval );

            CPPUNIT_ASSERT_DOUBLES_EQUAL( qm_val1, qm_val2, 1e-6 );
            CPPUNIT_ASSERT_EQUAL( indices1.size(), indices2.size() );
            CPPUNIT_ASSERT( !indices1.empty() );

            std::vector< size_t >::iterator it;
            unsigned h = 0;
            for( unsigned r = 0; r < indices1.size(); ++r )
            {
                it = std::find( indices2.begin(), indices2.end(), indices1[r] );
                CPPUNIT_ASSERT( it != indices2.end() );
                unsigned r2 = it - indices2.begin();

                for( unsigned c = r; c < indices1.size(); ++c, ++h )
                {
                    it = std::find( indices2.begin(), indices2.end(), indices1[c] );
                    CPPUNIT_ASSERT( it != indices2.end() );
                    unsigned c2 = it - indices2.begin();

                    unsigned h2;
                    if( r2 <= c2 )
                        h2 = indices2.size() * r - r * ( r + 1 ) / 2 + c;
                    else
                        h2 = indices2.size() * c - c * ( c + 1 ) / 2 + r;

                    // if (!utest_mat_equal(Hess1[h],Hess2[h2],0.001))
                    //  assert(false);
                    CPPUNIT_ASSERT_MATRICES_EQUAL( Hess1[h], Hess2[h2], 0.05 );
                }
            }
        }
    }
}

// Delcare specialized versions of the functions from
// QualityMetricTester because we surface elements must
// be handled differently.  For a surface element in the XY plane,
// the finite difference approximations of the derivatives will
// have non-zero values for derivatives wrt Z coordinates while the
// analytical derivative calculations will return all derivatives
// wrt Z coordiantes as zero.
template < class QMType >
inline void TMPQualityMetricTest< QMType >::compare_analytical_and_numerical_diagonals( QualityMetric* qm )
{
    MsqPrintError err( std::cout );
    PatchData pd;
    const EntityTopology types[] = { TRIANGLE, QUADRILATERAL, TETRAHEDRON, PYRAMID, PRISM, HEXAHEDRON };
    const int num_types          = sizeof( types ) / sizeof( types[0] );
    for( int i = 0; i < num_types; ++i )
    {
        get_nonideal_element( types[i], pd );

        std::vector< size_t > handles, indices1, indices2;
        std::vector< Vector3D > grad1, grad2;
        std::vector< Matrix3D > Hess1;
        std::vector< SymMatrix3D > Hess2;
        double qm_val1, qm_val2;
        bool rval;

        qm->get_evaluations( pd, handles, false, err );
        CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
        CPPUNIT_ASSERT( !handles.empty() );
        for( size_t j = 0; j < handles.size(); ++j )
        {
            rval = qm->QualityMetric::evaluate_with_Hessian( pd, handles[j], qm_val1, indices1, grad1, Hess1, err );
            CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
            CPPUNIT_ASSERT( rval );

            // For analytical gradient of a 2D element in the XY plane,
            // we expect all Z terms to be zero.
#ifdef PLANAR_HESSIAN
            if( TopologyInfo::dimension( types[i] ) == 2 )
                for( size_t k = 0; k < Hess1.size(); ++k )
                    Hess1[k]( 0, 2 ) = Hess1[k]( 1, 2 ) = Hess1[k]( 2, 0 ) = Hess1[k]( 2, 1 ) = Hess1[k]( 2, 2 ) = 0.0;
#endif

            rval = qm->evaluate_with_Hessian_diagonal( pd, handles[j], qm_val2, indices2, grad2, Hess2, err );
            CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
            CPPUNIT_ASSERT( rval );

            CPPUNIT_ASSERT_DOUBLES_EQUAL( qm_val1, qm_val2, 1e-6 );
            CPPUNIT_ASSERT_EQUAL( indices1.size(), indices2.size() );
            CPPUNIT_ASSERT( !indices1.empty() );
            CPPUNIT_ASSERT_EQUAL( indices1.size() * ( indices1.size() + 1 ) / 2, Hess1.size() );
            CPPUNIT_ASSERT_EQUAL( indices2.size(), Hess2.size() );

            size_t h = 0;
            std::vector< size_t >::iterator it;
            for( unsigned r = 0; r < indices1.size(); ++r )
            {
                it = std::find( indices2.begin(), indices2.end(), indices1[r] );
                CPPUNIT_ASSERT( it != indices2.end() );
                unsigned r2 = it - indices2.begin();
                // if (!utest_mat_equal(Hess1[h],Hess2[r2],0.001))
                //  assert(false);
                CPPUNIT_ASSERT_MATRICES_EQUAL( Hess1[h], Hess2[r2], 0.05 );
                h += indices1.size() - r;
            }
        }
    }
}