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
/*=========================================================================

  Module:    $RCSfile: V_GaussIntegration.cpp,v $

  Copyright (c) 2006 Sandia Corporation.
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

/*
 *
 * GaussIntegration.cpp performs Gauss Integrations
 *
 * This file is part of VERDICT
 *
 */

#define VERDICT_EXPORTS

#include "moab/verdict.h"
#include "V_GaussIntegration.hpp"

#include <cmath>

double verdictSqrt2 = sqrt( (double)2.0 );

int numberGaussPoints;
int numberNodes;
int numberDims;
double gaussPointY[maxNumberGaussPoints];
double gaussWeight[maxNumberGaussPoints];
double shapeFunction[maxTotalNumberGaussPoints][maxNumberNodes];
double dndy1GaussPts[maxTotalNumberGaussPoints][maxNumberNodes];
double dndy2GaussPts[maxTotalNumberGaussPoints][maxNumberNodes];
double dndy3GaussPts[maxTotalNumberGaussPoints][maxNumberNodes];
double totalGaussWeight[maxTotalNumberGaussPoints];
int totalNumberGaussPts;
double y1Area[maxNumberGaussPointsTri];
double y2Area[maxNumberGaussPointsTri];
double y1Volume[maxNumberGaussPointsTet];
double y2Volume[maxNumberGaussPointsTet];
double y3Volume[maxNumberGaussPointsTet];
double y4Volume[maxNumberGaussPointsTet];

void GaussIntegration::initialize( int n, int m, int dim, int tri )
{
    numberGaussPoints = n;
    numberNodes       = m;
    numberDims        = dim;

    if( tri == 1 )
    // triangular element
    {
        if( numberDims == 2 )
            totalNumberGaussPts = numberGaussPoints;
        else if( numberDims == 3 )
            totalNumberGaussPts = numberGaussPoints;
    }
    else if( tri == 0 )
    {
        if( numberDims == 2 )
            totalNumberGaussPts = numberGaussPoints * numberGaussPoints;
        else if( numberDims == 3 )
            totalNumberGaussPts = numberGaussPoints * numberGaussPoints * numberGaussPoints;
    }
}

void GaussIntegration::get_shape_func( double shape_function[],
                                       double dndy1_at_gauss_pts[],
                                       double dndy2_at_gauss_pts[],
                                       double gauss_weight[] )
{
    int i, j;
    for( i = 0; i < totalNumberGaussPts; i++ )
    {
        for( j = 0; j < numberNodes; j++ )
        {
            shape_function[i * maxNumberNodes + j]     = shapeFunction[i][j];
            dndy1_at_gauss_pts[i * maxNumberNodes + j] = dndy1GaussPts[i][j];
            dndy2_at_gauss_pts[i * maxNumberNodes + j] = dndy2GaussPts[i][j];
        }
    }

    for( i = 0; i < totalNumberGaussPts; i++ )
        gauss_weight[i] = totalGaussWeight[i];
}

void GaussIntegration::get_shape_func( double shape_function[],
                                       double dndy1_at_gauss_pts[],
                                       double dndy2_at_gauss_pts[],
                                       double dndy3_at_gauss_pts[],
                                       double gauss_weight[] )
{
    int i, j;
    for( i = 0; i < totalNumberGaussPts; i++ )
    {
        for( j = 0; j < numberNodes; j++ )
        {
            shape_function[i * maxNumberNodes + j]     = shapeFunction[i][j];
            dndy1_at_gauss_pts[i * maxNumberNodes + j] = dndy1GaussPts[i][j];
            dndy2_at_gauss_pts[i * maxNumberNodes + j] = dndy2GaussPts[i][j];
            dndy3_at_gauss_pts[i * maxNumberNodes + j] = dndy3GaussPts[i][j];
        }
    }

    for( i = 0; i < totalNumberGaussPts; i++ )
        gauss_weight[i] = totalGaussWeight[i];
}

void GaussIntegration::get_gauss_pts_and_weight()
{

    switch( numberGaussPoints )
    {
        case 1:
            gaussPointY[0] = 0.0;
            gaussWeight[0] = 2.0;
            break;
        case 2:
            gaussPointY[0] = -0.577350269189626;
            gaussPointY[1] = 0.577350269189626;
            gaussWeight[0] = 1.0;
            gaussWeight[1] = 1.0;
            break;
        case 3:
            gaussPointY[0] = -0.774596669241483;
            gaussPointY[1] = 0.0;
            gaussPointY[2] = 0.774596669241483;
            gaussWeight[0] = 0.555555555555555;
            gaussWeight[1] = 0.888888888888889;
            gaussWeight[2] = 0.555555555555555;
            break;
    }
}

void GaussIntegration::calculate_shape_function_2d_quad()
{
    int ife = 0, i, j;
    double y1, y2;
    get_gauss_pts_and_weight();

    switch( numberNodes )
    {
        case 4:
            for( i = 0; i < numberGaussPoints; i++ )
            {
                for( j = 0; j < numberGaussPoints; j++ )
                {
                    y1                    = gaussPointY[i];
                    y2                    = gaussPointY[j];
                    shapeFunction[ife][0] = 0.25 * ( 1 - y1 ) * ( 1 - y2 );
                    shapeFunction[ife][1] = 0.25 * ( 1 + y1 ) * ( 1 - y2 );
                    shapeFunction[ife][2] = 0.25 * ( 1 + y1 ) * ( 1 + y2 );
                    shapeFunction[ife][3] = 0.25 * ( 1 - y1 ) * ( 1 + y2 );

                    dndy1GaussPts[ife][0] = -0.25 * ( 1 - y2 );
                    dndy1GaussPts[ife][1] = 0.25 * ( 1 - y2 );
                    dndy1GaussPts[ife][2] = 0.25 * ( 1 + y2 );
                    dndy1GaussPts[ife][3] = -0.25 * ( 1 + y2 );

                    dndy2GaussPts[ife][0] = -0.25 * ( 1 - y1 );
                    dndy2GaussPts[ife][1] = -0.25 * ( 1 + y1 );
                    dndy2GaussPts[ife][2] = 0.25 * ( 1 + y1 );
                    dndy2GaussPts[ife][3] = 0.25 * ( 1 - y1 );

                    totalGaussWeight[ife] = gaussWeight[i] * gaussWeight[j];
                    ife++;
                }
            }
            break;
        case 8:
            for( i = 0; i < numberGaussPoints; i++ )
            {
                for( j = 0; j < numberGaussPoints; j++ )
                {
                    y1                    = gaussPointY[i];
                    y2                    = gaussPointY[j];
                    shapeFunction[ife][0] = 0.25 * ( 1 - y1 ) * ( 1 - y2 ) * ( -y1 - y2 - 1 );
                    shapeFunction[ife][1] = 0.25 * ( 1 + y1 ) * ( 1 - y2 ) * ( y1 - y2 - 1 );
                    shapeFunction[ife][2] = 0.25 * ( 1 + y1 ) * ( 1 + y2 ) * ( y1 + y2 - 1 );
                    shapeFunction[ife][3] = 0.25 * ( 1 - y1 ) * ( 1 + y2 ) * ( -y1 + y2 - 1 );
                    shapeFunction[ife][4] = 0.5 * ( 1 - y1 * y1 ) * ( 1 - y2 );
                    shapeFunction[ife][5] = 0.5 * ( 1 - y2 * y2 ) * ( 1 + y1 );
                    shapeFunction[ife][6] = 0.5 * ( 1 - y1 * y1 ) * ( 1 + y2 );
                    shapeFunction[ife][7] = 0.5 * ( 1 - y2 * y2 ) * ( 1 - y1 );

                    dndy1GaussPts[ife][0] = 0.25 * ( 1 - y2 ) * ( 2.0 * y1 + y2 );
                    dndy1GaussPts[ife][1] = 0.25 * ( 1 - y2 ) * ( 2.0 * y1 - y2 );
                    dndy1GaussPts[ife][2] = 0.25 * ( 1 + y2 ) * ( 2.0 * y1 + y2 );
                    dndy1GaussPts[ife][3] = 0.25 * ( 1 + y2 ) * ( 2.0 * y1 - y2 );

                    dndy1GaussPts[ife][4] = -y1 * ( 1 - y2 );
                    dndy1GaussPts[ife][5] = 0.5 * ( 1 - y2 * y2 );
                    dndy1GaussPts[ife][6] = -y1 * ( 1 + y2 );
                    dndy1GaussPts[ife][7] = -0.5 * ( 1 - y2 * y2 );

                    dndy2GaussPts[ife][0] = 0.25 * ( 1 - y1 ) * ( 2.0 * y2 + y1 );
                    dndy2GaussPts[ife][1] = 0.25 * ( 1 + y1 ) * ( 2.0 * y2 - y1 );
                    dndy2GaussPts[ife][2] = 0.25 * ( 1 + y1 ) * ( 2.0 * y2 + y1 );
                    dndy2GaussPts[ife][3] = 0.25 * ( 1 - y1 ) * ( 2.0 * y2 - y1 );

                    dndy2GaussPts[ife][4] = -0.5 * ( 1 - y1 * y1 );
                    dndy2GaussPts[ife][5] = -y2 * ( 1 + y1 );
                    dndy2GaussPts[ife][6] = 0.5 * ( 1 - y1 * y1 );
                    dndy2GaussPts[ife][7] = -y2 * ( 1 - y1 );

                    totalGaussWeight[ife] = gaussWeight[i] * gaussWeight[j];
                    ife++;
                }
            }
            break;
    }
}

void GaussIntegration::calculate_shape_function_3d_hex()
{
    int ife = 0, i, j, k, node_id;
    double y1, y2, y3, sign_node_y1, sign_node_y2, sign_node_y3;
    double y1_term, y2_term, y3_term, y123_temp;

    get_gauss_pts_and_weight();

    switch( numberNodes )
    {
        case 8:
            for( i = 0; i < numberGaussPoints; i++ )
            {
                for( j = 0; j < numberGaussPoints; j++ )
                {
                    for( k = 0; k < numberGaussPoints; k++ )
                    {
                        y1 = gaussPointY[i];
                        y2 = gaussPointY[j];
                        y3 = gaussPointY[k];

                        for( node_id = 0; node_id < numberNodes; node_id++ )
                        {
                            get_signs_for_node_local_coord_hex( node_id, sign_node_y1, sign_node_y2, sign_node_y3 );

                            y1_term = 1 + sign_node_y1 * y1;
                            y2_term = 1 + sign_node_y2 * y2;
                            y3_term = 1 + sign_node_y3 * y3;

                            shapeFunction[ife][node_id] = 0.125 * y1_term * y2_term * y3_term;
                            dndy1GaussPts[ife][node_id] = 0.125 * sign_node_y1 * y2_term * y3_term;
                            dndy2GaussPts[ife][node_id] = 0.125 * sign_node_y2 * y1_term * y3_term;
                            dndy3GaussPts[ife][node_id] = 0.125 * sign_node_y3 * y1_term * y2_term;
                        }
                        totalGaussWeight[ife] = gaussWeight[i] * gaussWeight[j] * gaussWeight[k];
                        ife++;
                    }
                }
            }
            break;
        case 20:
            for( i = 0; i < numberGaussPoints; i++ )
            {
                for( j = 0; j < numberGaussPoints; j++ )
                {
                    for( k = 0; k < numberGaussPoints; k++ )
                    {
                        y1 = gaussPointY[i];
                        y2 = gaussPointY[j];
                        y3 = gaussPointY[k];

                        for( node_id = 0; node_id < numberNodes; node_id++ )
                        {
                            get_signs_for_node_local_coord_hex( node_id, sign_node_y1, sign_node_y2, sign_node_y3 );

                            y1_term   = 1 + sign_node_y1 * y1;
                            y2_term   = 1 + sign_node_y2 * y2;
                            y3_term   = 1 + sign_node_y3 * y3;
                            y123_temp = sign_node_y1 * y1 + sign_node_y2 * y2 + sign_node_y3 * y3 - 2.;

                            switch( node_id )
                            {
                                case 0:
                                case 1:
                                case 2:
                                case 3:
                                case 4:
                                case 5:
                                case 6:
                                case 7: {
                                    shapeFunction[ife][node_id] = 0.125 * y1_term * y2_term * y3_term * y123_temp;
                                    dndy1GaussPts[ife][node_id] = 0.125 * sign_node_y1 * y123_temp * y2_term * y3_term +
                                                                  0.125 * y1_term * y2_term * y3_term * sign_node_y1;
                                    dndy2GaussPts[ife][node_id] = 0.125 * sign_node_y2 * y1_term * y3_term * y123_temp +
                                                                  0.125 * y1_term * y2_term * y3_term * sign_node_y2;
                                    dndy3GaussPts[ife][node_id] = 0.125 * sign_node_y3 * y1_term * y2_term * y123_temp +
                                                                  0.125 * y1_term * y2_term * y3_term * sign_node_y3;
                                    break;
                                }
                                case 8:
                                case 10:
                                case 16:
                                case 18: {
                                    shapeFunction[ife][node_id] = 0.25 * ( 1 - y1 * y1 ) * y2_term * y3_term;
                                    dndy1GaussPts[ife][node_id] = -0.5 * y1 * y2_term * y3_term;
                                    dndy2GaussPts[ife][node_id] = 0.25 * ( 1 - y1 * y1 ) * sign_node_y2 * y3_term;
                                    dndy3GaussPts[ife][node_id] = 0.25 * ( 1 - y1 * y1 ) * y2_term * sign_node_y3;
                                    break;
                                }
                                case 9:
                                case 11:
                                case 17:
                                case 19: {
                                    shapeFunction[ife][node_id] = 0.25 * ( 1 - y2 * y2 ) * y1_term * y3_term;
                                    dndy1GaussPts[ife][node_id] = 0.25 * ( 1 - y2 * y2 ) * sign_node_y1 * y3_term;
                                    dndy2GaussPts[ife][node_id] = -0.5 * y2 * y1_term * y3_term;
                                    dndy3GaussPts[ife][node_id] = 0.25 * ( 1 - y2 * y2 ) * y1_term * sign_node_y3;
                                    break;
                                }
                                case 12:
                                case 13:
                                case 14:
                                case 15: {
                                    shapeFunction[ife][node_id] = 0.25 * ( 1 - y3 * y3 ) * y1_term * y2_term;
                                    dndy1GaussPts[ife][node_id] = 0.25 * ( 1 - y3 * y3 ) * sign_node_y1 * y2_term;
                                    dndy2GaussPts[ife][node_id] = 0.25 * ( 1 - y3 * y3 ) * y1_term * sign_node_y2;
                                    dndy3GaussPts[ife][node_id] = -0.5 * y3 * y1_term * y2_term;
                                    break;
                                }
                            }
                        }
                        totalGaussWeight[ife] = gaussWeight[i] * gaussWeight[j] * gaussWeight[k];
                        ife++;
                    }
                }
            }
            break;
    }
}

void GaussIntegration::calculate_derivative_at_nodes( double dndy1_at_nodes[][maxNumberNodes],
                                                      double dndy2_at_nodes[][maxNumberNodes] )
{
    double y1 = 0., y2 = 0.;
    int i;
    for( i = 0; i < numberNodes; i++ )
    {
        switch( i )
        {
            case 0:
                y1 = -1.;
                y2 = -1.;
                break;
            case 1:
                y1 = 1.;
                y2 = -1.;
                break;
            case 2:
                y1 = 1.;
                y2 = 1.;
                break;
            case 3:
                y1 = -1.;
                y2 = 1.;
                break;

                // midside nodes if there is any

            case 4:
                y1 = 0.;
                y2 = -1.;
                break;

            case 5:
                y1 = 1.;
                y2 = 0.;
                break;

            case 6:
                y1 = 0.;
                y2 = 1.;
                break;

            case 7:
                y1 = -1.;
                y2 = 0.;
                break;
        }

        switch( numberNodes )
        {
            case 4:
                // dn_i/dy1 evaluated at node i
                dndy1_at_nodes[i][0] = -0.25 * ( 1 - y2 );
                dndy1_at_nodes[i][1] = 0.25 * ( 1 - y2 );
                dndy1_at_nodes[i][2] = 0.25 * ( 1 + y2 );
                dndy1_at_nodes[i][3] = -0.25 * ( 1 + y2 );

                // dn_i/dy2 evaluated at node i
                dndy2_at_nodes[i][0] = -0.25 * ( 1 - y1 );
                dndy2_at_nodes[i][1] = -0.25 * ( 1 + y1 );
                dndy2_at_nodes[i][2] = 0.25 * ( 1 + y1 );
                dndy2_at_nodes[i][3] = 0.25 * ( 1 - y1 );
                break;

            case 8:

                dndy1_at_nodes[i][0] = 0.25 * ( 1 - y2 ) * ( 2.0 * y1 + y2 );
                dndy1_at_nodes[i][1] = 0.25 * ( 1 - y2 ) * ( 2.0 * y1 - y2 );
                dndy1_at_nodes[i][2] = 0.25 * ( 1 + y2 ) * ( 2.0 * y1 + y2 );
                dndy1_at_nodes[i][3] = 0.25 * ( 1 + y2 ) * ( 2.0 * y1 - y2 );

                dndy1_at_nodes[i][4] = -y1 * ( 1 - y2 );
                dndy1_at_nodes[i][5] = 0.5 * ( 1 - y2 * y2 );
                dndy1_at_nodes[i][6] = -y1 * ( 1 + y2 );
                dndy1_at_nodes[i][7] = -0.5 * ( 1 - y2 * y2 );

                dndy2_at_nodes[i][0] = 0.25 * ( 1 - y1 ) * ( 2.0 * y2 + y1 );
                dndy2_at_nodes[i][1] = 0.25 * ( 1 + y1 ) * ( 2.0 * y2 - y1 );
                dndy2_at_nodes[i][2] = 0.25 * ( 1 + y1 ) * ( 2.0 * y2 + y1 );
                dndy2_at_nodes[i][3] = 0.25 * ( 1 - y1 ) * ( 2.0 * y2 - y1 );

                dndy2_at_nodes[i][4] = -0.5 * ( 1 - y1 * y1 );
                dndy2_at_nodes[i][5] = -y2 * ( 1 + y1 );
                dndy2_at_nodes[i][6] = 0.5 * ( 1 - y1 * y1 );
                dndy2_at_nodes[i][7] = -y2 * ( 1 - y1 );
                break;
        }
    }
}

void GaussIntegration::calculate_derivative_at_nodes_3d( double dndy1_at_nodes[][maxNumberNodes],
                                                         double dndy2_at_nodes[][maxNumberNodes],
                                                         double dndy3_at_nodes[][maxNumberNodes] )
{
    double y1, y2, y3, sign_node_y1, sign_node_y2, sign_node_y3;
    double y1_term, y2_term, y3_term, y123_temp;
    int node_id, node_id_2;
    for( node_id = 0; node_id < numberNodes; node_id++ )
    {
        get_signs_for_node_local_coord_hex( node_id, y1, y2, y3 );

        switch( numberNodes )
        {
            case 8:
                for( node_id_2 = 0; node_id_2 < numberNodes; node_id_2++ )
                {
                    get_signs_for_node_local_coord_hex( node_id_2, sign_node_y1, sign_node_y2, sign_node_y3 );
                    y1_term = 1 + sign_node_y1 * y1;
                    y2_term = 1 + sign_node_y2 * y2;
                    y3_term = 1 + sign_node_y3 * y3;

                    dndy1_at_nodes[node_id][node_id_2] = 0.125 * sign_node_y1 * y2_term * y3_term;

                    dndy2_at_nodes[node_id][node_id_2] = 0.125 * sign_node_y2 * y1_term * y3_term;

                    dndy3_at_nodes[node_id][node_id_2] = 0.125 * sign_node_y3 * y1_term * y2_term;
                }
                break;
            case 20:
                for( node_id_2 = 0; node_id_2 < numberNodes; node_id_2++ )
                {
                    get_signs_for_node_local_coord_hex( node_id_2, sign_node_y1, sign_node_y2, sign_node_y3 );

                    y1_term   = 1 + sign_node_y1 * y1;
                    y2_term   = 1 + sign_node_y2 * y2;
                    y3_term   = 1 + sign_node_y3 * y3;
                    y123_temp = sign_node_y1 * y1 + sign_node_y2 * y2 + sign_node_y3 * y3 - 2.;
                    switch( node_id_2 )
                    {
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7: {
                            dndy1_at_nodes[node_id][node_id_2] = 0.125 * sign_node_y1 * y2_term * y3_term * y123_temp +
                                                                 0.125 * y1_term * y2_term * y3_term * sign_node_y1;
                            dndy2_at_nodes[node_id][node_id_2] = 0.125 * sign_node_y2 * y1_term * y3_term * y123_temp +
                                                                 0.125 * y1_term * y2_term * y3_term * sign_node_y2;
                            dndy3_at_nodes[node_id][node_id_2] = 0.125 * sign_node_y3 * y1_term * y2_term * y123_temp +
                                                                 0.125 * y1_term * y2_term * y3_term * sign_node_y3;
                            break;
                        }
                        case 8:
                        case 10:
                        case 16:
                        case 18: {
                            dndy1_at_nodes[node_id][node_id_2] = -0.5 * y1 * y2_term * y3_term;
                            dndy2_at_nodes[node_id][node_id_2] = 0.25 * ( 1 - y1 * y1 ) * sign_node_y2 * y3_term;
                            dndy3_at_nodes[node_id][node_id_2] = 0.25 * ( 1 - y1 * y1 ) * y2_term * sign_node_y3;
                            break;
                        }
                        case 9:
                        case 11:
                        case 17:
                        case 19: {
                            dndy1_at_nodes[node_id][node_id_2] = 0.25 * ( 1 - y2 * y2 ) * sign_node_y1 * y3_term;
                            dndy2_at_nodes[node_id][node_id_2] = -0.5 * y2 * y1_term * y3_term;
                            dndy3_at_nodes[node_id][node_id_2] = 0.25 * ( 1 - y2 * y2 ) * y1_term * sign_node_y3;
                            break;
                        }
                        case 12:
                        case 13:
                        case 14:
                        case 15: {
                            dndy1_at_nodes[node_id][node_id_2] = 0.25 * ( 1 - y3 * y3 ) * sign_node_y1 * y2_term;
                            dndy2_at_nodes[node_id][node_id_2] = 0.25 * ( 1 - y3 * y3 ) * y1_term * sign_node_y2;
                            dndy3_at_nodes[node_id][node_id_2] = -0.5 * y3 * y1_term * y2_term;
                            break;
                        }
                    }
                }
                break;
        }
    }
}

void GaussIntegration::get_signs_for_node_local_coord_hex( int node_id,
                                                           double& sign_node_y1,
                                                           double& sign_node_y2,
                                                           double& sign_node_y3 )
{
    switch( node_id )
    {
        case 0:
            sign_node_y1 = -1.;
            sign_node_y2 = -1.;
            sign_node_y3 = -1.;
            break;
        case 1:
            sign_node_y1 = 1.;
            sign_node_y2 = -1.;
            sign_node_y3 = -1.;
            break;
        case 2:
            sign_node_y1 = 1.;
            sign_node_y2 = 1.;
            sign_node_y3 = -1.;
            break;
        case 3:
            sign_node_y1 = -1.;
            sign_node_y2 = 1.;
            sign_node_y3 = -1.;
            break;
        case 4:
            sign_node_y1 = -1.;
            sign_node_y2 = -1.;
            sign_node_y3 = 1.;
            break;
        case 5:
            sign_node_y1 = 1.;
            sign_node_y2 = -1.;
            sign_node_y3 = 1.;
            break;
        case 6:
            sign_node_y1 = 1.;
            sign_node_y2 = 1.;
            sign_node_y3 = 1.;
            break;
        case 7:
            sign_node_y1 = -1.;
            sign_node_y2 = 1.;
            sign_node_y3 = 1.;
            break;
        case 8:
            sign_node_y1 = 0;
            sign_node_y2 = -1.;
            sign_node_y3 = -1.;
            break;
        case 9:
            sign_node_y1 = 1.;
            sign_node_y2 = 0;
            sign_node_y3 = -1.;
            break;
        case 10:
            sign_node_y1 = 0;
            sign_node_y2 = 1.;
            sign_node_y3 = -1.;
            break;
        case 11:
            sign_node_y1 = -1.;
            sign_node_y2 = 0.;
            sign_node_y3 = -1.;
            break;
        case 12:
            sign_node_y1 = -1.;
            sign_node_y2 = -1.;
            sign_node_y3 = 0.;
            break;
        case 13:
            sign_node_y1 = 1.;
            sign_node_y2 = -1.;
            sign_node_y3 = 0.;
            break;
        case 14:
            sign_node_y1 = 1.;
            sign_node_y2 = 1.;
            sign_node_y3 = 0.;
            break;
        case 15:
            sign_node_y1 = -1.;
            sign_node_y2 = 1.;
            sign_node_y3 = 0.;
            break;
        case 16:
            sign_node_y1 = 0;
            sign_node_y2 = -1.;
            sign_node_y3 = 1.;
            break;
        case 17:
            sign_node_y1 = 1.;
            sign_node_y2 = 0;
            sign_node_y3 = 1.;
            break;
        case 18:
            sign_node_y1 = 0;
            sign_node_y2 = 1.;
            sign_node_y3 = 1.;
            break;
        case 19:
            sign_node_y1 = -1.;
            sign_node_y2 = 0.;
            sign_node_y3 = 1.;
            break;
    }
}

void GaussIntegration::get_tri_rule_pts_and_weight()
{
    // get triangular rule integration points and weight

    switch( numberGaussPoints )
    {
        case 6:
            y1Area[0] = 0.09157621;
            y2Area[0] = 0.09157621;

            y1Area[1] = 0.09157621;
            y2Area[1] = 0.8168476;

            y1Area[2] = 0.8168476;
            y2Area[2] = 0.09157621;

            y1Area[3] = 0.4459485;
            y2Area[3] = 0.4459485;

            y1Area[4] = 0.4459485;
            y2Area[4] = 0.1081030;

            y1Area[5] = 0.1081030;
            y2Area[5] = 0.4459485;

            int i;
            for( i = 0; i < 3; i++ )
            {
                totalGaussWeight[i] = 0.06348067;
            }
            for( i = 3; i < 6; i++ )
            {
                totalGaussWeight[i] = 0.1289694;
            }
            break;
    }
}

void GaussIntegration::calculate_shape_function_2d_tri()
{
    int ife;
    double y1, y2, y3;
    get_tri_rule_pts_and_weight();

    for( ife = 0; ife < totalNumberGaussPts; ife++ )
    {
        y1 = y1Area[ife];
        y2 = y2Area[ife];
        y3 = 1.0 - y1 - y2;

        shapeFunction[ife][0] = y1 * ( 2. * y1 - 1. );
        shapeFunction[ife][1] = y2 * ( 2. * y2 - 1. );
        shapeFunction[ife][2] = y3 * ( 2. * y3 - 1. );

        shapeFunction[ife][3] = 4. * y1 * y2;
        shapeFunction[ife][4] = 4. * y2 * y3;
        shapeFunction[ife][5] = 4. * y1 * y3;

        dndy1GaussPts[ife][0] = 4 * y1 - 1.;
        dndy1GaussPts[ife][1] = 0;
        dndy1GaussPts[ife][2] = 1 - 4. * y3;

        dndy1GaussPts[ife][3] = 4. * y2;
        dndy1GaussPts[ife][4] = -4. * y2;
        dndy1GaussPts[ife][5] = 4. * ( 1 - 2 * y1 - y2 );

        dndy2GaussPts[ife][0] = 0.0;
        dndy2GaussPts[ife][1] = 4. * y2 - 1.;
        dndy2GaussPts[ife][2] = 1 - 4. * y3;

        dndy2GaussPts[ife][3] = 4. * y1;
        dndy2GaussPts[ife][4] = 4. * ( 1 - y1 - 2. * y2 );
        dndy2GaussPts[ife][5] = -4. * y1;
    }
}

void GaussIntegration::calculate_derivative_at_nodes_2d_tri( double dndy1_at_nodes[][maxNumberNodes],
                                                             double dndy2_at_nodes[][maxNumberNodes] )
{
    double y1 = 0., y2 = 0., y3;
    int i;
    for( i = 0; i < numberNodes; i++ )
    {
        switch( i )
        {
            case 0:
                y1 = 1.;
                y2 = 0.;
                break;
            case 1:
                y1 = 0.;
                y2 = 1.;
                break;
            case 2:
                y1 = 0.;
                y2 = 0.;
                break;
            case 3:
                y1 = 0.5;
                y2 = 0.5;
                break;
            case 4:
                y1 = 0.;
                y2 = 0.5;
                break;
            case 5:
                y1 = 0.5;
                y2 = 0.0;
                break;
        }

        y3 = 1. - y1 - y2;

        dndy1_at_nodes[i][0] = 4 * y1 - 1.;
        dndy1_at_nodes[i][1] = 0;
        dndy1_at_nodes[i][2] = 1 - 4. * y3;

        dndy1_at_nodes[i][3] = 4. * y2;
        dndy1_at_nodes[i][4] = -4. * y2;
        dndy1_at_nodes[i][5] = 4. * ( 1 - 2 * y1 - y2 );

        dndy2_at_nodes[i][0] = 0.0;
        dndy2_at_nodes[i][1] = 4. * y2 - 1.;
        dndy2_at_nodes[i][2] = 1 - 4. * y3;

        dndy2_at_nodes[i][3] = 4. * y1;
        dndy2_at_nodes[i][4] = 4. * ( 1 - y1 - 2. * y2 );
        dndy2_at_nodes[i][5] = -4. * y1;
    }
}
void GaussIntegration::get_tet_rule_pts_and_weight()
{
    // get tetrahedron rule integration points and weight

    double a, b;
    switch( numberGaussPoints )
    {
        case 1:
            // 1 integration point formula, degree of precision 1
            y1Volume[0]         = 0.25;
            y2Volume[0]         = 0.25;
            y3Volume[0]         = 0.25;
            y4Volume[0]         = 0.25;
            totalGaussWeight[0] = 1.;
            break;
        case 4:
            // 4 integration points formula, degree of precision 2
            a = 0.58541020;
            b = 0.13819660;

            y1Volume[0] = a;
            y2Volume[0] = b;
            y3Volume[0] = b;
            y4Volume[0] = b;

            y1Volume[1] = b;
            y2Volume[1] = a;
            y3Volume[1] = b;
            y4Volume[1] = b;

            y1Volume[2] = b;
            y2Volume[2] = b;
            y3Volume[2] = a;
            y4Volume[2] = b;

            y1Volume[3] = b;
            y2Volume[3] = b;
            y3Volume[3] = b;
            y4Volume[3] = a;

            int i;
            for( i = 0; i < 4; i++ )
            {
                totalGaussWeight[i] = 0.25;
            }
            break;
    }
}

void GaussIntegration::calculate_shape_function_3d_tet()
{
    int ife;
    double y1, y2, y3, y4;
    get_tet_rule_pts_and_weight();

    switch( numberNodes )
    {
        case 10:  // 10 nodes quadratic tet
        {
            for( ife = 0; ife < totalNumberGaussPts; ife++ )
            {
                // y1,y2,y3,y4 are the volume coordinates
                y1 = y1Volume[ife];
                y2 = y2Volume[ife];
                y3 = y3Volume[ife];
                y4 = y4Volume[ife];

                // shape function is the same as in ABAQUS
                // it is different from that in all the FEA book
                // in which node is the first node
                // here at node 1 y4=1
                shapeFunction[ife][0] = y4 * ( 2. * y4 - 1. );
                shapeFunction[ife][1] = y1 * ( 2. * y1 - 1. );
                shapeFunction[ife][2] = y2 * ( 2. * y2 - 1. );
                shapeFunction[ife][3] = y3 * ( 2. * y3 - 1. );

                shapeFunction[ife][4] = 4. * y1 * y4;
                shapeFunction[ife][5] = 4. * y1 * y2;
                shapeFunction[ife][6] = 4. * y2 * y4;
                shapeFunction[ife][7] = 4. * y3 * y4;
                shapeFunction[ife][8] = 4. * y1 * y3;
                shapeFunction[ife][9] = 4. * y2 * y3;

                dndy1GaussPts[ife][0] = 1 - 4 * y4;
                dndy1GaussPts[ife][1] = 4 * y1 - 1.;
                dndy1GaussPts[ife][2] = 0;
                dndy1GaussPts[ife][3] = 0;

                dndy1GaussPts[ife][4] = 4. * ( y4 - y1 );
                dndy1GaussPts[ife][5] = 4. * y2;
                dndy1GaussPts[ife][6] = -4. * y2;
                dndy1GaussPts[ife][7] = -4. * y3;
                dndy1GaussPts[ife][8] = 4. * y3;
                dndy1GaussPts[ife][9] = 0;

                dndy2GaussPts[ife][0] = 1 - 4 * y4;
                dndy2GaussPts[ife][1] = 0;
                dndy2GaussPts[ife][2] = 4. * y2 - 1.;
                dndy2GaussPts[ife][3] = 0;

                dndy2GaussPts[ife][4] = -4. * y1;
                dndy2GaussPts[ife][5] = 4. * y1;
                dndy2GaussPts[ife][6] = 4. * ( y4 - y2 );
                dndy2GaussPts[ife][7] = -4. * y3;
                dndy2GaussPts[ife][8] = 0.;
                dndy2GaussPts[ife][9] = 4. * y3;

                dndy3GaussPts[ife][0] = 1 - 4 * y4;
                dndy3GaussPts[ife][1] = 0;
                dndy3GaussPts[ife][2] = 0;
                dndy3GaussPts[ife][3] = 4. * y3 - 1.;

                dndy3GaussPts[ife][4] = -4. * y1;
                dndy3GaussPts[ife][5] = 0;
                dndy3GaussPts[ife][6] = -4. * y2;
                dndy3GaussPts[ife][7] = 4. * ( y4 - y3 );
                dndy3GaussPts[ife][8] = 4. * y1;
                dndy3GaussPts[ife][9] = 4. * y2;
            }
            break;
        }
        case 4:  // four node linear tet for debug purpose
        {
            for( ife = 0; ife < totalNumberGaussPts; ife++ )
            {
                y1 = y1Volume[ife];
                y2 = y2Volume[ife];
                y3 = y3Volume[ife];
                y4 = y4Volume[ife];

                shapeFunction[ife][0] = y4;
                shapeFunction[ife][1] = y1;
                shapeFunction[ife][2] = y2;
                shapeFunction[ife][3] = y3;

                dndy1GaussPts[ife][0] = -1.;
                dndy1GaussPts[ife][1] = 1;
                dndy1GaussPts[ife][2] = 0;
                dndy1GaussPts[ife][3] = 0;

                dndy2GaussPts[ife][0] = -1.;
                dndy2GaussPts[ife][1] = 0;
                dndy2GaussPts[ife][2] = 1;
                dndy2GaussPts[ife][3] = 0;

                dndy3GaussPts[ife][0] = -1.;
                dndy3GaussPts[ife][1] = 0;
                dndy3GaussPts[ife][2] = 0;
                dndy3GaussPts[ife][3] = 1;
            }
            break;
        }
    }
}

void GaussIntegration::calculate_derivative_at_nodes_3d_tet( double dndy1_at_nodes[][maxNumberNodes],
                                                             double dndy2_at_nodes[][maxNumberNodes],
                                                             double dndy3_at_nodes[][maxNumberNodes] )
{
    double y1, y2, y3, y4;
    int i;

    switch( numberNodes )
    {
        case 10: {
            for( i = 0; i < numberNodes; i++ )
            {
                get_node_local_coord_tet( i, y1, y2, y3, y4 );

                dndy1_at_nodes[i][0] = 1 - 4 * y4;
                dndy1_at_nodes[i][1] = 4 * y1 - 1.;
                dndy1_at_nodes[i][2] = 0;
                dndy1_at_nodes[i][3] = 0;

                dndy1_at_nodes[i][4] = 4. * ( y4 - y1 );
                dndy1_at_nodes[i][5] = 4. * y2;
                dndy1_at_nodes[i][6] = -4. * y2;
                dndy1_at_nodes[i][7] = -4. * y3;
                dndy1_at_nodes[i][8] = 4. * y3;
                dndy1_at_nodes[i][9] = 0;

                dndy2_at_nodes[i][0] = 1 - 4 * y4;
                dndy2_at_nodes[i][1] = 0;
                dndy2_at_nodes[i][2] = 4. * y2 - 1.;
                dndy2_at_nodes[i][3] = 0;
                dndy2_at_nodes[i][4] = -4. * y1;
                dndy2_at_nodes[i][5] = 4. * y1;
                dndy2_at_nodes[i][6] = 4. * ( y4 - y2 );
                dndy2_at_nodes[i][7] = -4. * y3;
                dndy2_at_nodes[i][8] = 0.;
                dndy2_at_nodes[i][9] = 4. * y3;

                dndy3_at_nodes[i][0] = 1 - 4 * y4;
                dndy3_at_nodes[i][1] = 0;
                dndy3_at_nodes[i][2] = 0;
                dndy3_at_nodes[i][3] = 4. * y3 - 1.;

                dndy3_at_nodes[i][4] = -4. * y1;
                dndy3_at_nodes[i][5] = 0;
                dndy3_at_nodes[i][6] = -4. * y2;
                dndy3_at_nodes[i][7] = 4. * ( y4 - y3 );
                dndy3_at_nodes[i][8] = 4. * y1;
                dndy3_at_nodes[i][9] = 4. * y2;
            }
            break;
        }
        case 4: {
            for( i = 0; i < numberNodes; i++ )
            {
                get_node_local_coord_tet( i, y1, y2, y3, y4 );
                dndy1_at_nodes[i][0] = -1.;
                dndy1_at_nodes[i][1] = 1;
                dndy1_at_nodes[i][2] = 0;
                dndy1_at_nodes[i][3] = 0;

                dndy2_at_nodes[i][0] = -1.;
                dndy2_at_nodes[i][1] = 0;
                dndy2_at_nodes[i][2] = 1;
                dndy2_at_nodes[i][3] = 0;

                dndy3_at_nodes[i][0] = -1.;
                dndy3_at_nodes[i][1] = 0;
                dndy3_at_nodes[i][2] = 0;
                dndy3_at_nodes[i][3] = 1;
            }
            break;
        }
    }
}

void GaussIntegration::get_node_local_coord_tet( int node_id, double& y1, double& y2, double& y3, double& y4 )
{
    switch( node_id )
    {
        case 0:
            y1 = 0.;
            y2 = 0.;
            y3 = 0.;
            y4 = 1.;
            break;
        case 1:
            y1 = 1.;
            y2 = 0.;
            y3 = 0.;
            y4 = 0.;
            break;
        case 2:
            y1 = 0.;
            y2 = 1.;
            y3 = 0.;
            y4 = 0.;
            break;
        case 3:
            y1 = 0.;
            y2 = 0.;
            y3 = 1.;
            y4 = 0.;
            break;
        case 4:
            y1 = 0.5;
            y2 = 0.;
            y3 = 0.;
            y4 = 0.5;
            break;
        case 5:
            y1 = 0.5;
            y2 = 0.5;
            y3 = 0.;
            y4 = 0.;
            break;
        case 6:
            y1 = 0.;
            y2 = 0.5;
            y3 = 0.;
            y4 = 0.5;
            break;
        case 7:
            y1 = 0.;
            y2 = 0.0;
            y3 = 0.5;
            y4 = 0.5;
            break;
        case 8:
            y1 = 0.5;
            y2 = 0.;
            y3 = 0.5;
            y4 = 0.0;
            break;
        case 9:
            y1 = 0.;
            y2 = 0.5;
            y3 = 0.5;
            y4 = 0.;
            break;
    }
}