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

    Copyright 2006 Lawrence Livermore National Laboratory.  Under
    the terms of Contract B545069 with the University of Wisconsin --
    Madison, Lawrence Livermore National Laboratory 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

    (2006) [email protected]

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

/** \file LinearMappingFunctionTest.cpp
 *  \brief
 *  \author Jason Kraftcheck
 */

#include "Mesquite.hpp"
#include "MappingFunction.hpp"
#include "LinearHexahedron.hpp"
#include "LinearQuadrilateral.hpp"
#include "LinearTetrahedron.hpp"
#include "LinearTriangle.hpp"
#include "LinearPrism.hpp"
#include "LinearPyramid.hpp"
#include "TopologyInfo.hpp"
#include "MsqError.hpp"
#include "JacobianCalculator.hpp"
#include "IdealElements.hpp"

#include "UnitUtil.hpp"

#include <vector>
#include <algorithm>

using namespace MBMesquite;
using namespace std;

class LinearMappingFunctionTest : public CppUnit::TestFixture
{
  private:
    CPPUNIT_TEST_SUITE( LinearMappingFunctionTest );

    CPPUNIT_TEST( test_linear_hex_coeff_corners );
    CPPUNIT_TEST( test_linear_hex_coeff_edges );
    CPPUNIT_TEST( test_linear_hex_coeff_faces );
    CPPUNIT_TEST( test_linear_hex_coeff_center );

    CPPUNIT_TEST( test_linear_quad_coeff_corners );
    CPPUNIT_TEST( test_linear_quad_coeff_edges );
    //    CPPUNIT_TEST(test_linear_quad_coeff_faces);
    CPPUNIT_TEST( test_linear_quad_coeff_center );

    CPPUNIT_TEST( test_linear_tet_coeff_corners );
    CPPUNIT_TEST( test_linear_tet_coeff_edges );
    CPPUNIT_TEST( test_linear_tet_coeff_faces );
    CPPUNIT_TEST( test_linear_tet_coeff_center );

    CPPUNIT_TEST( test_linear_tri_coeff_corners );
    CPPUNIT_TEST( test_linear_tri_coeff_edges );
    CPPUNIT_TEST( test_linear_tri_coeff_faces );
    CPPUNIT_TEST( test_linear_tri_coeff_center );

    CPPUNIT_TEST( test_linear_prism_coeff_corners );
    CPPUNIT_TEST( test_linear_prism_coeff_edges );
    CPPUNIT_TEST( test_linear_prism_coeff_faces );
    CPPUNIT_TEST( test_linear_prism_coeff_center );

    CPPUNIT_TEST( test_linear_pyr_coeff_corners );
    CPPUNIT_TEST( test_linear_pyr_coeff_edges );
    CPPUNIT_TEST( test_linear_pyr_coeff_faces );
    CPPUNIT_TEST( test_linear_pyr_coeff_center );

    CPPUNIT_TEST( test_linear_hex_deriv_corners );
    CPPUNIT_TEST( test_linear_hex_deriv_edges );
    CPPUNIT_TEST( test_linear_hex_deriv_faces );
    CPPUNIT_TEST( test_linear_hex_deriv_center );

    CPPUNIT_TEST( test_linear_quad_deriv_corners );
    CPPUNIT_TEST( test_linear_quad_deriv_edges );
    //    CPPUNIT_TEST(test_linear_quad_deriv_faces);
    CPPUNIT_TEST( test_linear_quad_deriv_center );

    CPPUNIT_TEST( test_linear_tet_deriv_corners );
    CPPUNIT_TEST( test_linear_tet_deriv_edges );
    CPPUNIT_TEST( test_linear_tet_deriv_faces );
    CPPUNIT_TEST( test_linear_tet_deriv_center );

    CPPUNIT_TEST( test_linear_tri_deriv_corners );
    CPPUNIT_TEST( test_linear_tri_deriv_edges );
    //    CPPUNIT_TEST(test_linear_tri_deriv_faces);
    CPPUNIT_TEST( test_linear_tri_deriv_center );

    CPPUNIT_TEST( test_linear_prism_deriv_corners );
    CPPUNIT_TEST( test_linear_prism_deriv_edges );
    CPPUNIT_TEST( test_linear_prism_deriv_faces );
    CPPUNIT_TEST( test_linear_prism_deriv_center );

    CPPUNIT_TEST( test_linear_pyr_deriv_corners );
    CPPUNIT_TEST( test_linear_pyr_deriv_edges );
    CPPUNIT_TEST( test_linear_pyr_deriv_faces );
    CPPUNIT_TEST( test_linear_pyr_deriv_center );

    CPPUNIT_TEST( test_linear_hex_ideal );
    CPPUNIT_TEST( test_linear_quad_ideal );
    CPPUNIT_TEST( test_linear_tet_ideal );
    CPPUNIT_TEST( test_linear_tri_ideal );
    CPPUNIT_TEST( test_linear_prism_ideal );

    CPPUNIT_TEST_SUITE_END();

    LinearHexahedron hex;
    LinearQuadrilateral quad;
    LinearTetrahedron tet;
    LinearTriangle tri;
    LinearPrism prism;
    LinearPyramid pyr;

    static void hex_coeff( double xi[3], double coeff[8] );
    static void tet_coeff( double xi[3], double coeff[4] );
    static void quad_coeff( double xi[2], double coeff[4] );
    static void tri_coeff( double xi[2], double coeff[3] );
    static void prism_coeff( double xi[3], double coeff[6] );
    static void pyr_coeff( double xi[3], double coeff[5] );

    static void hex_deriv( double xi[3], double coeff_deriv[24] );
    static void tet_deriv( double xi[3], double coeff_deriv[12] );
    static void quad_deriv( double xi[2], double coeff_deriv[8] );
    static void tri_deriv( double xi[2], double coeff_deriv[6] );
    static void prism_deriv( double xi[3], double coeff_deriv[18] );
    static void pyr_deriv( double xi[3], double coeff_deriv[15] );

    typedef void ( *map_func )( double*, double* );

    void do_coeff_test( MappingFunction& mf, unsigned subdim, map_func mf2, unsigned count, double* xi );
    void do_deriv_test( MappingFunction2D& mf, unsigned subdim, map_func mf2, unsigned count, double* xi );
    void do_deriv_test( MappingFunction3D& mf, unsigned subdim, map_func mf2, unsigned count, double* xi );
    void do_ideal_test( MappingFunction2D& mf );
    void do_ideal_test( MappingFunction3D& mf );

    void test_coeff_fail( MappingFunction& mf, unsigned subdim );
    void test_deriv_fail( MappingFunction2D& mf, unsigned subdim );
    void test_deriv_fail( MappingFunction3D& mf, unsigned subdim );

    void xi_at_corners( EntityTopology type, double* xi, const int* corners );
    void xi_at_edges( EntityTopology type, double* xi, const int* corners );
    void xi_at_faces( EntityTopology type, double* xi, const int* corners );

  public:
    void setUp();
    void tearDown();

    void test_linear_hex_coeff_corners();
    void test_linear_hex_coeff_edges();
    void test_linear_hex_coeff_faces();
    void test_linear_hex_coeff_center();

    void test_linear_quad_coeff_corners();
    void test_linear_quad_coeff_edges();
    //    void test_linear_quad_coeff_faces();
    void test_linear_quad_coeff_center();

    void test_linear_tet_coeff_corners();
    void test_linear_tet_coeff_edges();
    void test_linear_tet_coeff_faces();
    void test_linear_tet_coeff_center();

    void test_linear_tri_coeff_corners();
    void test_linear_tri_coeff_edges();
    void test_linear_tri_coeff_faces();
    void test_linear_tri_coeff_center();

    void test_linear_prism_coeff_corners();
    void test_linear_prism_coeff_edges();
    void test_linear_prism_coeff_faces();
    void test_linear_prism_coeff_center();

    void test_linear_pyr_coeff_corners();
    void test_linear_pyr_coeff_edges();
    void test_linear_pyr_coeff_faces();
    void test_linear_pyr_coeff_center();

    void test_linear_hex_deriv_corners();
    void test_linear_hex_deriv_edges();
    void test_linear_hex_deriv_faces();
    void test_linear_hex_deriv_center();

    void test_linear_quad_deriv_corners();
    void test_linear_quad_deriv_edges();
    //    void test_linear_quad_deriv_faces();
    void test_linear_quad_deriv_center();

    void test_linear_tet_deriv_corners();
    void test_linear_tet_deriv_edges();
    void test_linear_tet_deriv_faces();
    void test_linear_tet_deriv_center();

    void test_linear_tri_deriv_corners();
    void test_linear_tri_deriv_edges();
    //    void test_linear_tri_deriv_faces();
    void test_linear_tri_deriv_center();

    void test_linear_prism_deriv_corners();
    void test_linear_prism_deriv_edges();
    void test_linear_prism_deriv_faces();
    void test_linear_prism_deriv_center();

    void test_linear_pyr_deriv_corners();
    void test_linear_pyr_deriv_edges();
    void test_linear_pyr_deriv_faces();
    void test_linear_pyr_deriv_center();

    void test_linear_hex_ideal()
    {
        do_ideal_test( hex );
    }
    void test_linear_quad_ideal()
    {
        do_ideal_test( quad );
    }
    void test_linear_tet_ideal()
    {
        do_ideal_test( tet );
    }
    void test_linear_tri_ideal()
    {
        do_ideal_test( tri );
    }
    void test_linear_prism_ideal()
    {
        do_ideal_test( prism );
    }
};

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LinearMappingFunctionTest, "LinearMappingFunctionTest" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LinearMappingFunctionTest, "Unit" );

void LinearMappingFunctionTest::setUp() {}
void LinearMappingFunctionTest::tearDown() {}

/*******************************************************************************
 *                             Xi values at element corners
 *******************************************************************************/

const int HexCorners[8][3] = { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 },
                               { 0, 0, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 0, 1, 1 } };

const int QuadCorners[4][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };

const int TetCorners[12] = { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 };

const int TriCorners[6] = { 0, 0, 1, 0, 0, 1 };

const int PrismCorners[18] = { 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1 };

/*******************************************************************************
 * Functions to calculate element xi at different locations, given the xi
 * at the corners.
 *******************************************************************************/

void LinearMappingFunctionTest::xi_at_corners( EntityTopology type, double* xi, const int* corners )
{
    unsigned d = TopologyInfo::dimension( type );
    unsigned c = TopologyInfo::corners( type );
    for( unsigned i = 0; i < c * d; ++i )
        xi[i] = corners[i];
}

void LinearMappingFunctionTest::xi_at_edges( EntityTopology type, double* xi, const int* corners )
{
    MsqError err;
    unsigned d = TopologyInfo::dimension( type );
    unsigned e = TopologyInfo::edges( type );
    for( unsigned i = 0; i < e; ++i )
    {
        const unsigned* vtx = TopologyInfo::edge_vertices( type, i, err );
        CPPUNIT_ASSERT( !err );
        for( unsigned j = 0; j < d; ++j )
            xi[d * i + j] = ( corners[d * vtx[0] + j] + corners[d * vtx[1] + j] ) / 2.0;
    }
}

void LinearMappingFunctionTest::xi_at_faces( EntityTopology type, double* xi, const int* corners )
{
    MsqError err;
    unsigned d = TopologyInfo::dimension( type );
    unsigned f = TopologyInfo::faces( type );
    for( unsigned i = 0; i < f; ++i )
    {
        unsigned c;
        const unsigned* vtx = TopologyInfo::face_vertices( type, i, c, err );
        CPPUNIT_ASSERT( !err );
        for( unsigned j = 0; j < d; ++j )
        {
            int sum = 0;
            for( unsigned k = 0; k < c; ++k )
                sum += corners[d * vtx[k] + j];
            xi[d * i + j] = (double)sum / c;
        }
    }
}

/*******************************************************************************
 *                 Test functions for mapping function coefficients
 *******************************************************************************/

void LinearMappingFunctionTest::test_linear_hex_coeff_corners()
{
    double xi[24];
    xi_at_corners( HEXAHEDRON, xi, &HexCorners[0][0] );
    do_coeff_test( hex, 0, hex_coeff, 8, xi );
}

void LinearMappingFunctionTest::test_linear_hex_coeff_edges()
{
    double xi[36];
    xi_at_edges( HEXAHEDRON, xi, &HexCorners[0][0] );
    do_coeff_test( hex, 1, hex_coeff, 12, xi );
}

void LinearMappingFunctionTest::test_linear_hex_coeff_faces()
{
    double xi[18];
    xi_at_faces( HEXAHEDRON, xi, &HexCorners[0][0] );
    do_coeff_test( hex, 2, hex_coeff, 6, xi );
}

void LinearMappingFunctionTest::test_linear_hex_coeff_center()
{
    double xi[3] = { 0.5, 0.5, 0.5 };
    do_coeff_test( hex, 3, hex_coeff, 1, xi );
}

void LinearMappingFunctionTest::test_linear_quad_coeff_corners()
{
    double xi[8];
    xi_at_corners( QUADRILATERAL, xi, &QuadCorners[0][0] );
    do_coeff_test( quad, 0, quad_coeff, 4, xi );
}

void LinearMappingFunctionTest::test_linear_quad_coeff_edges()
{
    double xi[8];
    xi_at_edges( QUADRILATERAL, xi, &QuadCorners[0][0] );
    do_coeff_test( quad, 1, quad_coeff, 4, xi );
}

// void LinearMappingFunctionTest::test_linear_quad_coeff_faces()
//{
//  test_coeff_fail( quad, 3 );
//}

void LinearMappingFunctionTest::test_linear_quad_coeff_center()
{
    double xi[2] = { 0.5, 0.5 };
    do_coeff_test( quad, 2, quad_coeff, 1, xi );
}

void LinearMappingFunctionTest::test_linear_tet_coeff_corners()
{
    double xi[12];
    xi_at_corners( TETRAHEDRON, xi, TetCorners );
    do_coeff_test( tet, 0, tet_coeff, 4, xi );
}

void LinearMappingFunctionTest::test_linear_tet_coeff_edges()
{
    double xi[18];
    xi_at_edges( TETRAHEDRON, xi, TetCorners );
    do_coeff_test( tet, 1, tet_coeff, 6, xi );
}

void LinearMappingFunctionTest::test_linear_tet_coeff_faces()
{
    double xi[12];
    xi_at_faces( TETRAHEDRON, xi, TetCorners );
    do_coeff_test( tet, 2, tet_coeff, 4, xi );
}

void LinearMappingFunctionTest::test_linear_tet_coeff_center()
{
    double xi[3] = { 0.25, 0.25, 0.25 };
    do_coeff_test( tet, 3, tet_coeff, 1, xi );
}

void LinearMappingFunctionTest::test_linear_tri_coeff_corners()
{
    double xi[12];
    xi_at_corners( TRIANGLE, xi, TriCorners );
    do_coeff_test( tri, 0, tri_coeff, 3, xi );
}

void LinearMappingFunctionTest::test_linear_tri_coeff_edges()
{
    double xi[18];
    xi_at_edges( TRIANGLE, xi, TriCorners );
    do_coeff_test( tri, 1, tri_coeff, 3, xi );
}

void LinearMappingFunctionTest::test_linear_tri_coeff_faces()
{
    test_coeff_fail( tri, 3 );
}

void LinearMappingFunctionTest::test_linear_tri_coeff_center()
{
    double xi[2] = { 1. / 3, 1. / 3 };
    do_coeff_test( tri, 2, tri_coeff, 1, xi );
}

void LinearMappingFunctionTest::test_linear_prism_coeff_corners()
{
    double xi[18];
    xi_at_corners( PRISM, xi, PrismCorners );
    do_coeff_test( prism, 0, prism_coeff, 6, xi );
}

void LinearMappingFunctionTest::test_linear_prism_coeff_edges()
{
    double xi[27];
    xi_at_edges( PRISM, xi, PrismCorners );
    do_coeff_test( prism, 1, prism_coeff, 9, xi );
}

void LinearMappingFunctionTest::test_linear_prism_coeff_faces()
{
    double xi[15];
    xi_at_faces( PRISM, xi, PrismCorners );
    do_coeff_test( prism, 2, prism_coeff, 5, xi );
}

void LinearMappingFunctionTest::test_linear_prism_coeff_center()
{
    double xi[3] = { 0.5, 1. / 3, 1. / 3 };
    do_coeff_test( prism, 3, prism_coeff, 1, xi );
}

void LinearMappingFunctionTest::test_linear_pyr_coeff_corners()
{
    double xi[15] = { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1 };
    do_coeff_test( pyr, 0, pyr_coeff, 5, xi );
}

void LinearMappingFunctionTest::test_linear_pyr_coeff_edges()
{
    double xi[24] = { 0.5, 0.0, 0.0, 1.0, 0.5, 0.0, 0.5, 1.0, 0.0, 0.0, 0.5, 0.0,
                      0.0, 0.0, 0.5, 1.0, 0.0, 0.5, 1.0, 1.0, 0.5, 0.0, 1.0, 0.5 };
    do_coeff_test( pyr, 1, pyr_coeff, 8, xi );
}

void LinearMappingFunctionTest::test_linear_pyr_coeff_faces()
{
    double xi[15] = { 0.5, 0.0, 0.5, 1.0, 0.5, 0.5, 0.5, 1.0, 0.5, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0 };
    do_coeff_test( pyr, 2, pyr_coeff, 5, xi );
}

void LinearMappingFunctionTest::test_linear_pyr_coeff_center()
{
    double xi[3] = { 0.5, 0.5, 0.5 };
    do_coeff_test( pyr, 3, pyr_coeff, 1, xi );
}

/*******************************************************************************
 *                 Test functions for mapping function derivatives
 *******************************************************************************/

void LinearMappingFunctionTest::test_linear_hex_deriv_corners()
{
    double xi[24];
    xi_at_corners( HEXAHEDRON, xi, &HexCorners[0][0] );
    do_deriv_test( hex, 0, hex_deriv, 8, xi );
}

void LinearMappingFunctionTest::test_linear_hex_deriv_edges()
{
    double xi[36];
    xi_at_edges( HEXAHEDRON, xi, &HexCorners[0][0] );
    do_deriv_test( hex, 1, hex_deriv, 12, xi );
}

void LinearMappingFunctionTest::test_linear_hex_deriv_faces()
{
    double xi[18];
    xi_at_faces( HEXAHEDRON, xi, &HexCorners[0][0] );
    do_deriv_test( hex, 2, hex_deriv, 6, xi );
}

void LinearMappingFunctionTest::test_linear_hex_deriv_center()
{
    double xi[3] = { 0.5, 0.5, 0.5 };
    do_deriv_test( hex, 3, hex_deriv, 1, xi );
}

void LinearMappingFunctionTest::test_linear_quad_deriv_corners()
{
    double xi[8];
    xi_at_corners( QUADRILATERAL, xi, &QuadCorners[0][0] );
    do_deriv_test( quad, 0, quad_deriv, 4, xi );
}

void LinearMappingFunctionTest::test_linear_quad_deriv_edges()
{
    double xi[8];
    xi_at_edges( QUADRILATERAL, xi, &QuadCorners[0][0] );
    do_deriv_test( quad, 1, quad_deriv, 4, xi );
}

// void LinearMappingFunctionTest::test_linear_quad_deriv_faces()
//{
//  test_deriv_fail( quad, 3 );
//}

void LinearMappingFunctionTest::test_linear_quad_deriv_center()
{
    double xi[2] = { 0.5, 0.5 };
    do_deriv_test( quad, 2, quad_deriv, 1, xi );
}

void LinearMappingFunctionTest::test_linear_tet_deriv_corners()
{
    double xi[12];
    xi_at_corners( TETRAHEDRON, xi, TetCorners );
    do_deriv_test( tet, 0, tet_deriv, 4, xi );
}

void LinearMappingFunctionTest::test_linear_tet_deriv_edges()
{
    double xi[18];
    xi_at_edges( TETRAHEDRON, xi, TetCorners );
    do_deriv_test( tet, 1, tet_deriv, 6, xi );
}

void LinearMappingFunctionTest::test_linear_tet_deriv_faces()
{
    double xi[12];
    xi_at_faces( TETRAHEDRON, xi, TetCorners );
    do_deriv_test( tet, 2, tet_deriv, 4, xi );
}

void LinearMappingFunctionTest::test_linear_tet_deriv_center()
{
    double xi[3] = { 0.25, 0.25, 0.25 };
    do_deriv_test( tet, 3, tet_deriv, 1, xi );
}

void LinearMappingFunctionTest::test_linear_tri_deriv_corners()
{
    double xi[12];
    xi_at_corners( TRIANGLE, xi, TriCorners );
    do_deriv_test( tri, 0, tri_deriv, 3, xi );
}

void LinearMappingFunctionTest::test_linear_tri_deriv_edges()
{
    double xi[18];
    xi_at_edges( TRIANGLE, xi, TriCorners );
    do_deriv_test( tri, 1, tri_deriv, 3, xi );
}

// void LinearMappingFunctionTest::test_linear_tri_deriv_faces()
//{
//  test_deriv_fail( tri, 3 );
//}

void LinearMappingFunctionTest::test_linear_tri_deriv_center()
{
    double xi[2] = { 1. / 3, 1. / 3 };
    do_deriv_test( tri, 2, tri_deriv, 1, xi );
}

void LinearMappingFunctionTest::test_linear_prism_deriv_corners()
{
    double xi[18];
    xi_at_corners( PRISM, xi, PrismCorners );
    do_deriv_test( prism, 0, prism_deriv, 6, xi );
}

void LinearMappingFunctionTest::test_linear_prism_deriv_edges()
{
    double xi[27];
    xi_at_edges( PRISM, xi, PrismCorners );
    do_deriv_test( prism, 1, prism_deriv, 9, xi );
}

void LinearMappingFunctionTest::test_linear_prism_deriv_faces()
{
    double xi[15];
    xi_at_faces( PRISM, xi, PrismCorners );
    do_deriv_test( prism, 2, prism_deriv, 5, xi );
}

void LinearMappingFunctionTest::test_linear_prism_deriv_center()
{
    double xi[3] = { 0.5, 1. / 3, 1. / 3 };
    do_deriv_test( prism, 3, prism_deriv, 1, xi );
}

void LinearMappingFunctionTest::test_linear_pyr_deriv_corners()
{
    double xi[15] = { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0.5, 0.5, 1 };
    do_deriv_test( pyr, 0, pyr_deriv, 5, xi );
}

void LinearMappingFunctionTest::test_linear_pyr_deriv_edges()
{
    double xi[24] = { 0.5, 0.0, 0.0, 1.0, 0.5, 0.0, 0.5, 1.0, 0.0, 0.0, 0.5, 0.0,
                      0.0, 0.0, 0.5, 1.0, 0.0, 0.5, 1.0, 1.0, 0.5, 0.0, 1.0, 0.5 };
    do_deriv_test( pyr, 1, pyr_deriv, 8, xi );
}

void LinearMappingFunctionTest::test_linear_pyr_deriv_faces()
{
    double xi[15] = { 0.5, 0.0, 0.5, 1.0, 0.5, 0.5, 0.5, 1.0, 0.5, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0 };
    do_deriv_test( pyr, 2, pyr_deriv, 5, xi );
}

void LinearMappingFunctionTest::test_linear_pyr_deriv_center()
{
    double xi[3] = { 0.5, 0.5, 0.5 };
    do_deriv_test( pyr, 3, pyr_deriv, 1, xi );
}

/*******************************************************************************
 *        Mapping function implementations to compare with
 *******************************************************************************/

void LinearMappingFunctionTest::hex_coeff( double xi[3], double coeff[8] )
{
    coeff[0] = ( 1 - xi[0] ) * ( 1 - xi[1] ) * ( 1 - xi[2] );
    coeff[1] = xi[0] * ( 1 - xi[1] ) * ( 1 - xi[2] );
    coeff[2] = xi[0] * xi[1] * ( 1 - xi[2] );
    coeff[3] = ( 1 - xi[0] ) * xi[1] * ( 1 - xi[2] );
    coeff[4] = ( 1 - xi[0] ) * ( 1 - xi[1] ) * xi[2];
    coeff[5] = xi[0] * ( 1 - xi[1] ) * xi[2];
    coeff[6] = xi[0] * xi[1] * xi[2];
    coeff[7] = ( 1 - xi[0] ) * xi[1] * xi[2];
}

void LinearMappingFunctionTest::tet_coeff( double xi[3], double coeff[4] )
{
    coeff[0] = 1 - xi[0] - xi[1] - xi[2];
    coeff[1] = xi[0];
    coeff[2] = xi[1];
    coeff[3] = xi[2];
}

void LinearMappingFunctionTest::quad_coeff( double xi[2], double coeff[4] )
{
    coeff[0] = ( 1 - xi[0] ) * ( 1 - xi[1] );
    coeff[1] = xi[0] * ( 1 - xi[1] );
    coeff[2] = xi[0] * xi[1];
    coeff[3] = ( 1 - xi[0] ) * xi[1];
}

void LinearMappingFunctionTest::tri_coeff( double xi[2], double coeff[3] )
{
    coeff[0] = 1 - xi[0] - xi[1];
    coeff[1] = xi[0];
    coeff[2] = xi[1];
}

void LinearMappingFunctionTest::prism_coeff( double xi[3], double coeff[6] )
{
    coeff[0] = ( 1 - xi[0] ) * ( 1 - xi[1] - xi[2] );
    coeff[1] = ( 1 - xi[0] ) * xi[1];
    coeff[2] = ( 1 - xi[0] ) * xi[2];
    coeff[3] = xi[0] * ( 1 - xi[1] - xi[2] );
    coeff[4] = xi[0] * xi[1];
    coeff[5] = xi[0] * xi[2];
}

void LinearMappingFunctionTest::pyr_coeff( double xi[3], double coeff[5] )
{
    coeff[0] = ( 1 - xi[0] ) * ( 1 - xi[1] ) * ( 1 - xi[2] );
    coeff[1] = xi[0] * ( 1 - xi[1] ) * ( 1 - xi[2] );
    coeff[2] = xi[0] * xi[1] * ( 1 - xi[2] );
    coeff[3] = ( 1 - xi[0] ) * xi[1] * ( 1 - xi[2] );
    coeff[4] = xi[2];
}

/*******************************************************************************
 *        Mapping function derivatives to compare with
 *******************************************************************************/

void LinearMappingFunctionTest::hex_deriv( double xi[3], double coeff[24] )
{
    coeff[3 * 0 + 0] = -( 1 - xi[1] ) * ( 1 - xi[2] );
    coeff[3 * 0 + 1] = -( 1 - xi[0] ) * ( 1 - xi[2] );
    coeff[3 * 0 + 2] = -( 1 - xi[0] ) * ( 1 - xi[1] );

    coeff[3 * 1 + 0] = ( 1 - xi[1] ) * ( 1 - xi[2] );
    coeff[3 * 1 + 1] = -xi[0] * ( 1 - xi[2] );
    coeff[3 * 1 + 2] = -xi[0] * ( 1 - xi[1] );

    coeff[3 * 2 + 0] = xi[1] * ( 1 - xi[2] );
    coeff[3 * 2 + 1] = xi[0] * ( 1 - xi[2] );
    coeff[3 * 2 + 2] = -xi[0] * xi[1];

    coeff[3 * 3 + 0] = -xi[1] * ( 1 - xi[2] );
    coeff[3 * 3 + 1] = ( 1 - xi[0] ) * ( 1 - xi[2] );
    coeff[3 * 3 + 2] = -( 1 - xi[0] ) * xi[1];

    coeff[3 * 4 + 0] = -( 1 - xi[1] ) * xi[2];
    coeff[3 * 4 + 1] = -( 1 - xi[0] ) * xi[2];
    coeff[3 * 4 + 2] = ( 1 - xi[0] ) * ( 1 - xi[1] );

    coeff[3 * 5 + 0] = ( 1 - xi[1] ) * xi[2];
    coeff[3 * 5 + 1] = -xi[0] * xi[2];
    coeff[3 * 5 + 2] = xi[0] * ( 1 - xi[1] );

    coeff[3 * 6 + 0] = xi[1] * xi[2];
    coeff[3 * 6 + 1] = xi[0] * xi[2];
    coeff[3 * 6 + 2] = xi[0] * xi[1];

    coeff[3 * 7 + 0] = -xi[1] * xi[2];
    coeff[3 * 7 + 1] = ( 1 - xi[0] ) * xi[2];
    coeff[3 * 7 + 2] = ( 1 - xi[0] ) * xi[1];
}

void LinearMappingFunctionTest::tet_deriv( double*, double coeff[12] )
{
    static const double derivs[] = { -1, -1, -1, 1, 0, 0, 0, 1, 0, 0, 0, 1 };
    memcpy( coeff, derivs, sizeof( derivs ) );
}

void LinearMappingFunctionTest::quad_deriv( double xi[2], double coeff[8] )
{
    coeff[2 * 0 + 0] = xi[1] - 1;
    coeff[2 * 0 + 1] = xi[0] - 1;

    coeff[2 * 1 + 0] = 1 - xi[1];
    coeff[2 * 1 + 1] = -xi[0];

    coeff[2 * 2 + 0] = xi[1];
    coeff[2 * 2 + 1] = xi[0];

    coeff[2 * 3 + 0] = -xi[1];
    coeff[2 * 3 + 1] = 1 - xi[0];
}

void LinearMappingFunctionTest::tri_deriv( double*, double coeff[6] )
{
    static const double derivs[] = { -1, -1, 1, 0, 0, 1 };
    memcpy( coeff, derivs, sizeof( derivs ) );
}

void LinearMappingFunctionTest::prism_deriv( double xi[3], double coeff[18] )
{
    coeff[0] = xi[1] + xi[2] - 1.0;
    ;
    coeff[1] = xi[0] - 1.0;
    coeff[2] = xi[0] - 1.0;

    coeff[3] = -xi[1];
    coeff[4] = 1.0 - xi[0];
    coeff[5] = 0.0;

    coeff[6] = -xi[2];
    coeff[7] = 0.0;
    coeff[8] = 1.0 - xi[0];

    coeff[9]  = 1.0 - xi[1] - xi[2];
    coeff[10] = -xi[0];
    coeff[11] = -xi[0];

    coeff[12] = xi[1];
    coeff[13] = xi[0];
    coeff[14] = 0.0;

    coeff[15] = xi[2];
    coeff[16] = 0.0;
    coeff[17] = xi[0];
}

void LinearMappingFunctionTest::pyr_deriv( double xi[3], double coeff[15] )
{
    coeff[3 * 0 + 0] = -( 1 - xi[1] ) * ( 1 - xi[2] );
    coeff[3 * 0 + 1] = -( 1 - xi[0] ) * ( 1 - xi[2] );
    coeff[3 * 0 + 2] = -( 1 - xi[0] ) * ( 1 - xi[1] );

    coeff[3 * 1 + 0] = ( 1 - xi[1] ) * ( 1 - xi[2] );
    coeff[3 * 1 + 1] = -xi[0] * ( 1 - xi[2] );
    coeff[3 * 1 + 2] = -xi[0] * ( 1 - xi[1] );

    coeff[3 * 2 + 0] = xi[1] * ( 1 - xi[2] );
    coeff[3 * 2 + 1] = xi[0] * ( 1 - xi[2] );
    coeff[3 * 2 + 2] = -xi[0] * xi[1];

    coeff[3 * 3 + 0] = -xi[1] * ( 1 - xi[2] );
    coeff[3 * 3 + 1] = ( 1 - xi[0] ) * ( 1 - xi[2] );
    coeff[3 * 3 + 2] = -xi[1] * ( 1 - xi[0] );

    coeff[3 * 4 + 0] = 0.0;
    coeff[3 * 4 + 1] = 0.0;
    coeff[3 * 4 + 2] = 1.0;
}

/*******************************************************************************
 *         Some utlity functions for creating CppUnit error messages
 *******************************************************************************/

static string itostr( int i )
{
    char buffer[32];
    sprintf( buffer, "%d", i );
    return buffer;
}

static string dtostr( double i )
{
    char buffer[32];
    sprintf( buffer, "%g", i );
    return buffer;
}

/*******************************************************************************
 *         Actual test imlplementation (common code for many tests)
 *******************************************************************************/

void LinearMappingFunctionTest::do_coeff_test( MappingFunction& mf,
                                               unsigned subdim,
                                               map_func mf2,
                                               unsigned count,
                                               double* xi )
{
    // make sure it fails if passed a nonlinear element
    MsqError err;
    double coeff[100];
    size_t indices[100];
    size_t num_coeff = 100;
    NodeSet tmp_set;
    tmp_set.set_mid_edge_node( 1 );
    mf.coefficients( Sample( 0, 1 ), tmp_set, coeff, indices, num_coeff, err );
    CPPUNIT_ASSERT( err );
    err.clear();

    // get number of vertices in element
    const unsigned n = TopologyInfo::corners( mf.element_topology() );
    const unsigned d = TopologyInfo::dimension( mf.element_topology() );

    // compare coefficients at each location
    vector< double > comp( n );
    for( unsigned i = 0; i < count; ++i )
    {
        num_coeff = 101;
        mf.coefficients( Sample( subdim, i ), NodeSet(), coeff, indices, num_coeff, err );
        CPPUNIT_ASSERT( !err );

        mf2( xi, &comp[0] );
        string xi_str;
        for( unsigned j = 0; j < d; ++j )
        {
            xi_str += !j ? "(" : ", ";
            xi_str += dtostr( xi[j] );
        }
        xi_str += ")";
        xi += d;

        for( unsigned j = 0; j < n; ++j )
        {
            double mf_val = 0.0;
            size_t idx    = std::find( indices, indices + num_coeff, j ) - indices;
            if( idx < num_coeff ) mf_val = coeff[idx];

            CppUnit::Message message( "Coefficients do not match." );
            message.addDetail( string( "Entity:             " ) + itostr( i ) );
            message.addDetail( string( "Coefficient number: " ) + itostr( j ) );
            message.addDetail( string( "Xi:             " ) + xi_str );
            message.addDetail( string( "Expected value: " ) + dtostr( comp[j] ) );
            message.addDetail( string( "Actual value:   " ) + dtostr( mf_val ) );
            ASSERT_MESSAGE( message, fabs( comp[j] - mf_val ) < DBL_EPSILON );
        }
    }
}

void LinearMappingFunctionTest::do_deriv_test( MappingFunction2D& mf,
                                               unsigned subdim,
                                               map_func mf2,
                                               unsigned count,
                                               double* xi )
{
    // make sure it fails if passed a nonlinear element
    MsqError err;
    MsqVector< 2 > derivs[100];
    size_t verts[100], num_vtx = 37;
    NodeSet tmp_set;
    tmp_set.set_mid_edge_node( 1 );
    mf.derivatives( Sample( subdim, 0 ), tmp_set, verts, derivs, num_vtx, err );
    CPPUNIT_ASSERT( err );
    err.clear();

    // get number of vertices in element
    const unsigned n = TopologyInfo::corners( mf.element_topology() );

    // compare coefficients at each location
    vector< double > comp( 2 * n );
    for( unsigned i = 0; i < count; ++i )
    {
        num_vtx = 33;
        mf.derivatives( Sample( subdim, i ), NodeSet(), verts, derivs, num_vtx, err );
        CPPUNIT_ASSERT( !err );
        CPPUNIT_ASSERT( num_vtx > 0 );
        CPPUNIT_ASSERT( num_vtx <= n );

        mf2( xi, &comp[0] );
        string xi_str;
        for( unsigned j = 0; j < 2; ++j )
        {
            xi_str += !j ? "(" : ", ";
            xi_str += dtostr( xi[j] );
        }
        xi_str += ")";
        xi += 2;

        for( unsigned j = 0; j < num_vtx; ++j )
        {
            bool all_zero = true;
            for( unsigned k = 0; k < 2; ++k )
            {
                CppUnit::Message message( "Coefficient derivatives do not match." );
                message.addDetail( string( "Entity:             " ) + itostr( i ) );
                message.addDetail( string( "Coefficient number: " ) + itostr( j ) );
                message.addDetail( string( "Xi:             " ) + xi_str );
                message.addDetail( string( "Axis:           " ) + itostr( k ) );
                message.addDetail( string( "Expected value: " ) + dtostr( comp[2 * verts[j] + k] ) );
                message.addDetail( string( "Actual value:   " ) + dtostr( derivs[j][k] ) );
                ASSERT_MESSAGE( message, fabs( comp[2 * verts[j] + k] - derivs[j][k] ) < DBL_EPSILON );
                if( fabs( derivs[j][k] ) > DBL_EPSILON ) all_zero = false;
            }

            // if vertex has all zero values, it shouldn't have been in the
            // vertex list at all, as the Jacobian will not depend on that vertex.
            CPPUNIT_ASSERT( !all_zero );
        }

        // If any vertex is not in the list, then its values must be zero.
        sort( verts, verts + num_vtx );
        for( unsigned j = 0; j < num_vtx; ++j )
        {
            if( !binary_search( verts, verts + num_vtx, j ) )
            {
                for( unsigned k = 0; k < 2; ++k )
                {
                    CppUnit::Message message( "Missing coefficient derivatives." );
                    message.addDetail( string( "Entity:              " ) + itostr( i ) );
                    message.addDetail( string( "Coefficient number:  " ) + itostr( j ) );
                    message.addDetail( string( "Axis:                " ) + itostr( k ) );
                    message.addDetail( string( "Expected derivative: " ) + dtostr( comp[2 * j + k] ) );
                    ASSERT_MESSAGE( message, fabs( comp[2 * j + k] ) < DBL_EPSILON );
                }
            }
        }
    }
}

void LinearMappingFunctionTest::do_deriv_test( MappingFunction3D& mf,
                                               unsigned subdim,
                                               map_func mf2,
                                               unsigned count,
                                               double* xi )
{
    // make sure it fails if passed a nonlinear element
    MsqError err;
    MsqVector< 3 > derivs[100];
    size_t verts[100], num_vtx = 37;
    NodeSet tmp_set;
    tmp_set.set_mid_edge_node( 1 );
    mf.derivatives( Sample( subdim, 0 ), tmp_set, verts, derivs, num_vtx, err );
    CPPUNIT_ASSERT( err );
    err.clear();

    // get number of vertices in element
    const unsigned n = TopologyInfo::corners( mf.element_topology() );

    // compare coefficients at each location
    vector< double > comp( 3 * n );
    for( unsigned i = 0; i < count; ++i )
    {
        num_vtx = 33;
        mf.derivatives( Sample( subdim, i ), NodeSet(), verts, derivs, num_vtx, err );
        CPPUNIT_ASSERT( !err );
        CPPUNIT_ASSERT( num_vtx > 0 );
        CPPUNIT_ASSERT( num_vtx <= n );

        mf2( xi, &comp[0] );
        string xi_str;
        for( unsigned j = 0; j < 3; ++j )
        {
            xi_str += !j ? "(" : ", ";
            xi_str += dtostr( xi[j] );
        }
        xi_str += ")";
        xi += 3;

        for( unsigned j = 0; j < num_vtx; ++j )
        {
            bool all_zero = true;
            for( unsigned k = 0; k < 3; ++k )
            {
                CppUnit::Message message( "Coefficient derivatives do not match." );
                message.addDetail( string( "Entity:             " ) + itostr( i ) );
                message.addDetail( string( "Coefficient number: " ) + itostr( j ) );
                message.addDetail( string( "Xi:             " ) + xi_str );
                message.addDetail( string( "Axis:           " ) + itostr( k ) );
                message.addDetail( string( "Expected value: " ) + dtostr( comp[3 * verts[j] + k] ) );
                message.addDetail( string( "Actual value:   " ) + dtostr( derivs[j][k] ) );
                ASSERT_MESSAGE( message, fabs( comp[3 * verts[j] + k] - derivs[j][k] ) < DBL_EPSILON );
                if( fabs( derivs[j][k] ) > DBL_EPSILON ) all_zero = false;
            }

            // if vertex has all zero values, it shouldn't have been in the
            // vertex list at all, as the Jacobian will not depend on that vertex.
            CPPUNIT_ASSERT( !all_zero );
        }

        // If any vertex is not in the list, then its values must be zero.
        sort( verts, verts + num_vtx );
        for( unsigned j = 0; j < num_vtx; ++j )
        {
            if( !binary_search( verts, verts + num_vtx, j ) )
            {
                for( unsigned k = 0; k < 3; ++k )
                {
                    CppUnit::Message message( "Missing coefficient derivatives." );
                    message.addDetail( string( "Entity:              " ) + itostr( i ) );
                    message.addDetail( string( "Coefficient number:  " ) + itostr( j ) );
                    message.addDetail( string( "Axis:                " ) + itostr( k ) );
                    message.addDetail( string( "Expected derivative: " ) + dtostr( comp[3 * j + k] ) );
                    ASSERT_MESSAGE( message, fabs( comp[3 * j + k] ) < DBL_EPSILON );
                }
            }
        }
    }
}

void LinearMappingFunctionTest::do_ideal_test( MappingFunction2D& mf )
{
    MsqError err;
    MsqMatrix< 3, 2 > W_prime;
    mf.ideal( Sample( 2, 0 ), W_prime, err );
    ASSERT_NO_ERROR( err );

    // for this test that everything is in the xy-plane
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, W_prime( 2, 0 ), 1e-12 );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, W_prime( 2, 1 ), 1e-12 );
    MsqMatrix< 2, 2 > W( W_prime.data() );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, det( W ), 1e-6 );

    const Vector3D* verts = unit_edge_element( mf.element_topology() );
    CPPUNIT_ASSERT( verts );

    JacobianCalculator jc;
    jc.get_Jacobian_2D( &mf, NodeSet(), Sample( 2, 0 ), verts, TopologyInfo::corners( mf.element_topology() ), W_prime,
                        err );
    ASSERT_NO_ERROR( err );

    // for this test that everything is in the xy-plane
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, W_prime( 2, 0 ), 1e-12 );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, W_prime( 2, 1 ), 1e-12 );
    MsqMatrix< 2, 2 > W_exp( W_prime.data() );
    W_exp /= sqrt( det( W_exp ) );

    // Matrices should be a rotation of each other.
    // First, calculate tentative rotation matrix
    MsqMatrix< 2, 2 > R = inverse( W_exp ) * W;
    // next check that it is a rotation
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, det( R ), 1e-6 );          // no scaling
    ASSERT_MATRICES_EQUAL( transpose( R ), inverse( R ), 1e-6 );  // orthogonal
}

void LinearMappingFunctionTest::do_ideal_test( MappingFunction3D& mf )
{
    MsqError err;
    MsqMatrix< 3, 3 > W, I( 1.0 );
    mf.ideal( Sample( 3, 0 ), W, err );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, det( W ), 1e-6 );

    const Vector3D* verts = unit_edge_element( mf.element_topology() );
    CPPUNIT_ASSERT( verts );

    JacobianCalculator jc;
    MsqMatrix< 3, 3 > W_exp;
    jc.get_Jacobian_3D( &mf, NodeSet(), Sample( 3, 0 ), verts, TopologyInfo::corners( mf.element_topology() ), W_exp,
                        err );
    ASSERT_NO_ERROR( err );
    W_exp /= MBMesquite::cbrt( det( W_exp ) );

    // Matrices should be a rotation of each other.
    // First, calculate tentative rotation matrix
    MsqMatrix< 3, 3 > R = W * inverse( W_exp );
    // next check that it is a rotation
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, det( R ), 1e-6 );   // no scaling
    ASSERT_MATRICES_EQUAL( I, transpose( R ) * R, 1e-6 );  // orthogonal
}

void LinearMappingFunctionTest::test_coeff_fail( MappingFunction& mf, unsigned subdim )
{
    // make sure it fails if called
    MsqError err;
    double coeff[100];
    size_t num_coeff, indices[100];
    mf.coefficients( Sample( subdim, 0 ), NodeSet(), coeff, indices, num_coeff, err );
    CPPUNIT_ASSERT( err );
    err.clear();
}

void LinearMappingFunctionTest::test_deriv_fail( MappingFunction2D& mf, unsigned subdim )<--- The function 'test_deriv_fail' is never used.
{
    // make sure it fails if called
    MsqError err;
    MsqVector< 2 > coeff[100];
    size_t verts[100], num_coeff;
    mf.derivatives( Sample( subdim, 0 ), NodeSet(), verts, coeff, num_coeff, err );
    CPPUNIT_ASSERT( err );
    err.clear();
}

void LinearMappingFunctionTest::test_deriv_fail( MappingFunction3D& mf, unsigned subdim )
{
    // make sure it fails if called
    MsqError err;
    MsqVector< 3 > coeff[100];
    size_t verts[100], num_coeff;
    mf.derivatives( Sample( subdim, 0 ), NodeSet(), verts, coeff, num_coeff, err );
    CPPUNIT_ASSERT( err );
    err.clear();
}