File: FE_Interpolate.cpp

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (993 lines) | stat: -rw-r--r-- 35,646 bytes parent folder | download | duplicates (2)
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
// ATC header files
#include "ATC_Error.h"
#include "FE_Element.h"
#include "FE_Interpolate.h"
#include "FE_Quadrature.h"

// Other headers
#include <cmath>

using std::map;
using std::vector;

namespace ATC {

  FE_Interpolate::FE_Interpolate(FE_Element *feElement)
    : feElement_(feElement),
      nSD_(feElement->num_dims())
  {
    // Nothing to do here
  }

  FE_Interpolate::~FE_Interpolate()
  {
    if (!feQuadList_.empty()) {
      map<FeIntQuadrature,FE_Quadrature *>::iterator qit;
      for (qit  = feQuadList_.begin();
           qit != feQuadList_.end(); ++qit) {
        delete (qit->second);
      }
    }
  }

  void FE_Interpolate::set_quadrature(FeEltGeometry geo,
                                      FeIntQuadrature quad)
  {
    if (feQuadList_.count(quad) == 0) {
      feQuad_ = new FE_Quadrature(geo,quad);
      feQuadList_[quad] = feQuad_;
    } else {
      feQuad_ = feQuadList_[quad];
    }
    precalculate_shape_functions();
  }

  void FE_Interpolate::precalculate_shape_functions()
  {
    int numEltNodes = feElement_->num_elt_nodes();
    int numFaces = feElement_->num_faces();
    int numFaceNodes = feElement_->num_face_nodes();

    int numIPs = feQuad_->numIPs;
    DENS_MAT &ipCoords = feQuad_->ipCoords;
    int numFaceIPs = feQuad_->numFaceIPs;
    vector<DENS_MAT> &ipFaceCoords = feQuad_->ipFaceCoords;
    DENS_MAT &ipFace2DCoords = feQuad_->ipFace2DCoords;

    // Compute elemental shape functions at ips
    N_.reset(numIPs,numEltNodes);
    dNdr_.assign(numIPs,DENS_MAT(nSD_,numEltNodes));
    for (int ip = 0; ip < numIPs; ip++) {
      CLON_VEC thisIP = column(ipCoords,ip);
      CLON_VEC thisN = row(N_,ip);
      DENS_MAT &thisdNdr = dNdr_[ip];
      compute_N(thisIP,thisN);
      compute_N_dNdr(thisIP,thisN,thisdNdr);
    }

    // Compute face shape functions at ip's
    NFace_.assign(numFaces,DENS_MAT(numFaceIPs,numEltNodes));
    dNdrFace_.resize(numFaces);
    for (int f = 0; f < numFaces; f++) {
      dNdrFace_[f].assign(numIPs,DENS_MAT(nSD_,numEltNodes));
    }
    for (int f = 0; f < numFaces; f++) {
      for (int ip = 0; ip < numFaceIPs; ip++) {
        CLON_VEC thisIP = column(ipFaceCoords[f],ip);
        CLON_VEC thisN = row(NFace_[f],ip);
        DENS_MAT &thisdNdr = dNdrFace_[f][ip];
        compute_N_dNdr(thisIP,thisN,thisdNdr);
      }
    }

    // Compute 2D face shape function derivatives
    dNdrFace2D_.assign(numFaceIPs,DENS_MAT(nSD_-1,numFaceNodes));
    for (int ip = 0; ip < numFaceIPs; ip++) {
      CLON_VEC thisIP = column(ipFace2DCoords,ip);
      DENS_MAT &thisdNdr = dNdrFace2D_[ip];
      compute_dNdr(thisIP,
                   numFaceNodes,nSD_-1,feElement_->face_area(),
                   thisdNdr);
    }
  }

  //-----------------------------------------------------------------
  // shape function value at a particular point given local coordinates
  //-----------------------------------------------------------------
  void FE_Interpolate::shape_function(const VECTOR &xi,
                                      DENS_VEC &N)
  {
    int numEltNodes = feElement_->num_elt_nodes();
    N.resize(numEltNodes);
    compute_N(xi,N);
  }

  void FE_Interpolate::shape_function(const DENS_MAT &eltCoords,
                                      const VECTOR &xi,
                                      DENS_VEC &N,
                                      DENS_MAT &dNdx)
  {
    int numEltNodes = feElement_->num_elt_nodes();
    N.resize(numEltNodes);
    DENS_MAT dNdr(nSD_,numEltNodes,false);
    compute_N_dNdr(xi,N,dNdr);

    DENS_MAT eltCoordsT = transpose(eltCoords);
    DENS_MAT dxdr, drdx;

    dxdr = dNdr*eltCoordsT;
    drdx = inv(dxdr);
    dNdx = drdx*dNdr;
  }

  void FE_Interpolate::shape_function_derivatives(const DENS_MAT &eltCoords,
                                      const VECTOR &xi,
                                      DENS_MAT &dNdx)
  {
    int numEltNodes = feElement_->num_elt_nodes();
    DENS_MAT dNdr(nSD_,numEltNodes,false);

    DENS_VEC N(numEltNodes);
    compute_N_dNdr(xi,N,dNdr);
    DENS_MAT eltCoordsT = transpose(eltCoords);
    DENS_MAT dxdr, drdx;
    dxdr = dNdr*eltCoordsT; // tangents or Jacobian matrix

    drdx = inv(dxdr);
    dNdx = drdx*dNdr; // dN/dx = dN/dxi (dx/dxi)^-1
  }

  void FE_Interpolate::tangents(const DENS_MAT &eltCoords,
                                const VECTOR &xi,
                                DENS_MAT &dxdr) const
  {
    int numEltNodes = feElement_->num_elt_nodes();
    DENS_MAT dNdr(nSD_,numEltNodes,false);

    DENS_VEC N(numEltNodes);
    compute_N_dNdr(xi,N,dNdr);
//dNdr.print("dNdr");
    DENS_MAT eltCoordsT = transpose(eltCoords);
//eltCoordsT.print("elt coords");
    dxdr = dNdr*eltCoordsT;
//dxdr.print("dxdr");
  }

  void FE_Interpolate::tangents(const DENS_MAT &eltCoords,
                                const VECTOR &xi,
                                vector<DENS_VEC> & dxdxi,
                                const bool normalize) const
  {
     DENS_MAT dxdr;
     tangents(eltCoords,xi,dxdr);
//dxdr.print("dxdr-post");
     dxdxi.resize(nSD_);
     //for (int i = 0; i < nSD_; ++i) dxdxi[i] = CLON_VEC(dxdr,CLONE_COL,i);
     for (int i = 0; i < nSD_; ++i) {
       dxdxi[i].resize(nSD_);
       for (int j = 0; j < nSD_; ++j) {
         dxdxi[i](j) = dxdr(i,j);
       }
     }
//dxdxi[0].print("t1");
//dxdxi[1].print("t2");
//dxdxi[2].print("t3");
     if (normalize) {
       for (int j = 0; j < nSD_; ++j) {
         double norm = 0;
         VECTOR & t = dxdxi[j];
         for (int i = 0; i < nSD_; ++i) norm += t(i)*t(i);
         norm = 1./sqrt(norm);
         for (int i = 0; i < nSD_; ++i) t(i) *= norm;
       }
     }
  }

  // -------------------------------------------------------------
  //   shape_function values at nodes
  // -------------------------------------------------------------
  void FE_Interpolate::shape_function(const DENS_MAT &eltCoords,
                                      DENS_MAT &N,
                                      vector<DENS_MAT> &dN,
                                      DIAG_MAT &weights)
  {
    int numEltNodes = feElement_->num_elt_nodes();

    // Transpose eltCoords
    DENS_MAT eltCoordsT(transpose(eltCoords));

    // Shape functions are simply the canonical element values
    N = N_;

    // Set sizes of matrices and vectors
    if ((int)dN.size() != nSD_) dN.resize(nSD_);
    for (int isd = 0; isd < nSD_; isd++)
      dN[isd].resize(feQuad_->numIPs,numEltNodes);
    weights.resize(feQuad_->numIPs,feQuad_->numIPs);

    // Create some temporary matrices:
    // Jacobian matrix: [dx/dr dy/ds dz/dt | dx/ds ... ]
    DENS_MAT dxdr, drdx, dNdx;

    // Loop over integration points
    for (int ip = 0; ip < feQuad_->numIPs; ip++) {
      // Compute dx/dxi matrix
      dxdr = dNdr_[ip]*eltCoordsT;
      drdx = inv(dxdr);

      // Compute dNdx and fill dN matrix
      dNdx = drdx * dNdr_[ip];
      for (int isd = 0; isd < nSD_; isd++)
        for (int inode = 0; inode < numEltNodes; inode++)
          dN[isd](ip,inode) = dNdx(isd,inode);

      // Compute jacobian determinant of dxdr at this ip
      double J = dxdr(0,0) * (dxdr(1,1)*dxdr(2,2) - dxdr(2,1)*dxdr(1,2))
               - dxdr(0,1) * (dxdr(1,0)*dxdr(2,2) - dxdr(1,2)*dxdr(2,0))
               + dxdr(0,2) * (dxdr(1,0)*dxdr(2,1) - dxdr(1,1)*dxdr(2,0));

      // Compute ip weight
      weights(ip,ip) = feQuad_->ipWeights(ip)*J;
    }

  }

  //-----------------------------------------------------------------
  // shape functions on a given face
  //-----------------------------------------------------------------
  void FE_Interpolate::face_shape_function(const DENS_MAT &eltCoords,
                                           const DENS_MAT &faceCoords,
                                           const int faceID,
                                           DENS_MAT &N,
                                           DENS_MAT &n,
                                           DIAG_MAT &weights)
  {
    int numFaceIPs = feQuad_->numFaceIPs;

    // Transpose eltCoords
    DENS_MAT eltCoordsT = transpose(eltCoords);

    // Shape functions are simply the canonical element values
    N = NFace_[faceID];

    // Create some temporary matrices:
    // Jacobian matrix: [dx/dr dy/ds dz/dt | dx/ds ... ]
    DENS_MAT dxdr, drdx, dNdx;

    // Loop over integration points
    DENS_VEC normal(nSD_);
    n.resize(nSD_,numFaceIPs);
    weights.resize(numFaceIPs,numFaceIPs);
    for (int ip = 0; ip < numFaceIPs; ip++) {
      // Compute 2d jacobian determinant of dxdr at this ip
      double J = face_normal(faceCoords,ip,normal);

      // Copy normal at integration point
      for (int isd = 0; isd < nSD_; isd++) {
        n(isd,ip) = normal(isd);
      }

      // Compute ip weight
      weights(ip,ip) = feQuad_->ipFaceWeights(ip)*J;
    }

  }

  void FE_Interpolate::face_shape_function(const DENS_MAT &eltCoords,
                                           const DENS_MAT &faceCoords,
                                           const int faceID,
                                           DENS_MAT &N,
                                           vector<DENS_MAT> &dN,
                                           vector<DENS_MAT> &Nn,
                                           DIAG_MAT &weights)
  {
    int numEltNodes = feElement_->num_elt_nodes();
    int numFaceIPs = feQuad_->numFaceIPs;

    // Transpose eltCoords
    DENS_MAT eltCoordsT = transpose(eltCoords);

    // Shape functions are simply the canonical element values
    N = NFace_[faceID];

    // Set sizes of matrices and vectors
    if ((int)dN.size() != nSD_) dN.resize(nSD_);
    if ((int)Nn.size() != nSD_) Nn.resize(nSD_);
    for (int isd = 0; isd < nSD_; isd++) {
      dN[isd].resize(numFaceIPs,numEltNodes);
      Nn[isd].resize(numFaceIPs,numEltNodes);
    }
    weights.resize(numFaceIPs,numFaceIPs);

    // Create some temporary matrices:
    // Jacobian matrix: [dx/dr dy/ds dz/dt | dx/ds ... ]
    DENS_MAT dxdr, drdx, dNdx;
    DENS_VEC normal(nSD_);

    // Loop over integration points
    for (int ip = 0; ip < numFaceIPs; ip++) {
      // Compute dx/dxi matrix
      dxdr = dNdrFace_[faceID][ip] * eltCoordsT;
      drdx = inv(dxdr);

      // Compute 2d jacobian determinant of dxdr at this ip
      double J = face_normal(faceCoords,ip,normal);

      // Compute dNdx and fill dN matrix
      dNdx = drdx * dNdrFace_[faceID][ip];
      for (int isd = 0; isd < nSD_; isd++) {
        for (int inode = 0; inode < numEltNodes; inode++) {
          dN[isd](ip,inode) = dNdx(isd,inode);
          Nn[isd](ip,inode) = N(ip,inode)*normal(isd);
        }
      }

      // Compute ip weight
      weights(ip,ip) = feQuad_->ipFaceWeights(ip)*J;
    }

  }

  // -------------------------------------------------------------
  //   face normal
  // -------------------------------------------------------------
  double FE_Interpolate::face_normal(const DENS_MAT &faceCoords,
                                     int ip,
                                     DENS_VEC &normal)
  {
    // Compute dx/dr for deformed element
    DENS_MAT faceCoordsT = transpose(faceCoords);
    DENS_MAT dxdr = dNdrFace2D_[ip]*faceCoordsT;

    // Normal vector from cross product, hardcoded for 3D, sad
    normal(0) = dxdr(0,1)*dxdr(1,2) - dxdr(0,2)*dxdr(1,1);
    normal(1) = dxdr(0,2)*dxdr(1,0) - dxdr(0,0)*dxdr(1,2);
    normal(2) = dxdr(0,0)*dxdr(1,1) - dxdr(0,1)*dxdr(1,0);

    // Jacobian (3D)
    double J = sqrt(normal(0)*normal(0) +
                    normal(1)*normal(1) +
                    normal(2)*normal(2));
    double invJ = 1.0/J;
    normal(0) *= invJ;
    normal(1) *= invJ;
    normal(2) *= invJ;
    return J;
  }

  int FE_Interpolate::num_ips() const
  {
    return feQuad_->numIPs;
  }

  int FE_Interpolate::num_face_ips() const
  {
    return feQuad_->numFaceIPs;
  }


  /*********************************************************
   * Class FE_InterpolateCartLagrange
   *
   * For computing Lagrange shape functions using Cartesian
   * coordinate systems (all quads/hexes fall under this
   * category, and any elements derived by degenerating
   * them). Not to be used for serendipity elements, which
   * should be implemented for SPEED.
   *
   *********************************************************/
  FE_InterpolateCartLagrange::FE_InterpolateCartLagrange(
                                          FE_Element *feElement)
    : FE_Interpolate(feElement)
  {
    set_quadrature(HEXA,GAUSS2);
  }

  FE_InterpolateCartLagrange::~FE_InterpolateCartLagrange()
  {
    // Handled by base class
  }

  void FE_InterpolateCartLagrange::compute_N(const VECTOR &point,
                                             VECTOR &N)
  {
    // *** see comments for compute_N_dNdr ***
    const DENS_VEC &localCoords1d = feElement_->local_coords_1d();
    int numEltNodes = feElement_->num_elt_nodes();
    int numEltNodes1d = feElement_->num_elt_nodes_1d();

    DENS_MAT lagrangeTerms(nSD_,numEltNodes1d);
    DENS_MAT lagrangeDenom(nSD_,numEltNodes1d);
    lagrangeTerms = 1.0;
    lagrangeDenom = 1.0;
    for (int iSD = 0; iSD < nSD_; ++iSD) {
      for (int inode = 0; inode < numEltNodes1d; ++inode) {
        for (int icont = 0; icont < numEltNodes1d; ++icont) {
          if (inode != icont) {
            lagrangeDenom(iSD,inode) *= (localCoords1d(inode) -
                                         localCoords1d(icont));
            lagrangeTerms(iSD,inode) *= (point(iSD)-localCoords1d(icont));
          }
        }
      }
    }
    for (int iSD=0; iSD<nSD_; ++iSD) {
      for (int inode=0; inode<numEltNodes1d; ++inode) {
        lagrangeTerms(iSD,inode) /= lagrangeDenom(iSD,inode);
      }
    }

    N = 1.0;
    vector<int> mapping(nSD_);
    for (int inode=0; inode<numEltNodes; ++inode) {
      feElement_->mapping(inode,mapping);
      for (int iSD=0; iSD<nSD_; ++iSD) {
        N(inode) *= lagrangeTerms(iSD,mapping[iSD]);
      }
    }
  }

  // Sort of a test-ride for a generic version that can be used for
  // faces too. The only thing that's not "generic" is localCoords,
  // which very magically works in both cases.
  void FE_InterpolateCartLagrange::compute_dNdr(const VECTOR &point,
                                                const int numNodes,
                                                const int nD,
                                                const double,
                                                DENS_MAT &dNdr)
  {
    // *** see comments for compute_N_dNdr ***
    const DENS_VEC &localCoords1d = feElement_->local_coords_1d();
    int numEltNodes1d = feElement_->num_elt_nodes_1d();

    DENS_MAT lagrangeTerms(nD,numEltNodes1d);
    DENS_MAT lagrangeDenom(nD,numEltNodes1d);
    DENS_MAT lagrangeDeriv(nD,numEltNodes1d);
    lagrangeDenom = 1.0;
    lagrangeTerms = 1.0;
    lagrangeDeriv = 0.0;
    DENS_VEC productRuleVec(numEltNodes1d);
    productRuleVec = 1.0;
    for (int iSD = 0; iSD < nD; ++iSD) {
      for (int inode = 0; inode < numEltNodes1d; ++inode) {
        for (int icont = 0; icont < numEltNodes1d; ++icont) {
          if (inode != icont) {
            lagrangeTerms(iSD,inode) *= (point(iSD)-localCoords1d(icont));
            lagrangeDenom(iSD,inode) *= (localCoords1d(inode) -
                                         localCoords1d(icont));
            for (int dcont=0; dcont<numEltNodes1d; ++dcont) {
              if (inode == dcont) {
                productRuleVec(dcont) = 0.0;
              } else if (icont == dcont) {
              } else {
                productRuleVec(dcont) *= (point(iSD)-localCoords1d(icont));
              }
            }
          }
        }
        for (int dcont=0; dcont<numEltNodes1d; ++dcont) {
          lagrangeDeriv(iSD,inode) += productRuleVec(dcont);
        }
        productRuleVec = 1.0;
      }
    }
    for (int iSD=0; iSD<nD; ++iSD) {
      for (int inode=0; inode<numEltNodes1d; ++inode) {
        lagrangeTerms(iSD,inode) /= lagrangeDenom(iSD,inode);
        lagrangeDeriv(iSD,inode) /= lagrangeDenom(iSD,inode);
      }
    }

    dNdr = 1.0;
    vector<int> mapping(nD);
    for (int inode=0; inode<numNodes; ++inode) {
      feElement_->mapping(inode,mapping);
      for (int iSD=0; iSD<nD; ++iSD) {
        for (int dSD=0; dSD<nD; ++dSD) {
          if (iSD == dSD) {
            dNdr(dSD,inode) *= lagrangeDeriv(iSD,mapping[iSD]);
          } else {
            dNdr(dSD,inode) *= lagrangeTerms(iSD,mapping[iSD]);
          }
        }
      }
    }
  }

  void FE_InterpolateCartLagrange::compute_N_dNdr(const VECTOR &point,
                                                  VECTOR &N,
                                                  DENS_MAT &dNdr) const
  {
    // Required data from element class
    const DENS_VEC &localCoords1d = feElement_->local_coords_1d();
    int numEltNodes = feElement_->num_elt_nodes();
    int numEltNodes1d = feElement_->num_elt_nodes_1d();

    // lagrangeTerms stores the numerator for the various Lagrange polynomials
    // in one dimension, that will be used to produce the three dimensional
    // shape functions
    DENS_MAT lagrangeTerms(nSD_,numEltNodes1d);
    // lagrangeDenom stores the denominator. Stored separately to reduce
    // redundancy, because it will be used for the shape functions and derivs
    DENS_MAT lagrangeDenom(nSD_,numEltNodes1d);
    // lagrangeDeriv stores the numerator for the derivative of the Lagrange
    // polynomials
    DENS_MAT lagrangeDeriv(nSD_,numEltNodes1d);
    // Terms/Denom are products, Deriv will be a sum, so initialize as such:
    lagrangeTerms = 1.0;
    lagrangeDenom = 1.0;
    lagrangeDeriv = 0.0;
    // the derivative requires use of the product rule; to store the prodcuts
    // which make up the terms produced by the product rule, we'll use this
    // vector
    DENS_VEC productRuleVec(numEltNodes1d);
    productRuleVec = 1.0;
    for (int iSD = 0; iSD < nSD_; ++iSD) {
      for (int inode = 0; inode < numEltNodes1d; ++inode) {
        for (int icont = 0; icont < numEltNodes1d; ++icont) {
          if (inode != icont) {
            // each dimension and each 1d node per dimension has a
            // contribution from all nodes besides the current node
            lagrangeTerms(iSD,inode) *= (point(iSD)-localCoords1d(icont));
            lagrangeDenom(iSD,inode) *= (localCoords1d(inode) -
                                         localCoords1d(icont));
            // complciated; each sum produced by the product rule has one
            // "derivative", and the rest are just identical to the terms
            // above
            for (int dcont=0; dcont<numEltNodes1d; ++dcont) {
              if (inode == dcont) {
                // skip this term, derivative is 0
                productRuleVec(dcont) = 0.0;
              } else if (icont == dcont) {
                // no numerator contribution, derivative is 1
              } else {
                // part of the "constant"
                productRuleVec(dcont) *= (point(iSD)-localCoords1d(icont));
              }
            }
          }
        }
        // sum the terms produced by the product rule and store in Deriv
        for (int dcont=0; dcont<numEltNodes1d; ++dcont) {
          lagrangeDeriv(iSD,inode) += productRuleVec(dcont);
        }
        productRuleVec = 1.0;
      }
    }
    // divide by denom
    for (int iSD=0; iSD<nSD_; ++iSD) {
      for (int inode=0; inode<numEltNodes1d; ++inode) {
        lagrangeTerms(iSD,inode) /= lagrangeDenom(iSD,inode);
        lagrangeDeriv(iSD,inode) /= lagrangeDenom(iSD,inode);
      }
    }

    N = 1.0;
    dNdr = 1.0;
    // mapping returns the 1d nodes in each dimension that should be multiplied
    // to achieve the shape functions in 3d
    vector<int> mapping(nSD_);
    for (int inode=0; inode<numEltNodes; ++inode) {
      feElement_->mapping(inode,mapping);
      for (int iSD=0; iSD<nSD_; ++iSD) {
        N(inode) *= lagrangeTerms(iSD,mapping[iSD]);
        for (int dSD=0; dSD<nSD_; ++dSD) {
          // only use Deriv for the dimension in which we're taking the
          // derivative, because the rest is essentially a "constant"
          if (iSD == dSD) {
            dNdr(dSD,inode) *= lagrangeDeriv(iSD,mapping[iSD]);
          } else {
            dNdr(dSD,inode) *= lagrangeTerms(iSD,mapping[iSD]);
          }
        }
      }
    }
  }

  /*********************************************************
   * Class FE_InterpolateCartLin
   *
   * For computing linear shape functions using Cartesian
   * coordinate systems (all quads/hexes fall under this
   * category, and any elements derived by degenerating
   * them).
   *
   *********************************************************/
  FE_InterpolateCartLin::FE_InterpolateCartLin(
                                          FE_Element *feElement)
    : FE_Interpolate(feElement)
  {
    set_quadrature(HEXA,GAUSS2);
  }

  FE_InterpolateCartLin::~FE_InterpolateCartLin()
  {
    // Handled by base class
  }

  void FE_InterpolateCartLin::compute_N(const VECTOR &point,
                                        VECTOR &N)
  {
    // *** see comments for compute_N_dNdr ***
    const DENS_MAT &localCoords = feElement_->local_coords();
    double invVol = 1.0/(feElement_->vol());
    int numEltNodes = feElement_->num_elt_nodes();

    for (int inode = 0; inode < numEltNodes; ++inode) {
      N(inode) = invVol;
      for (int isd = 0; isd < nSD_; ++isd) {
        N(inode) *= (1.0 + point(isd)*localCoords(isd,inode));
      }
    }
  }

  // Sort of a test-ride for a generic version that can be used for
  // faces too. The only thing that's not "generic" is localCoords,
  // which very magically works in both cases.
  void FE_InterpolateCartLin::compute_dNdr(const VECTOR &point,
                                           const int numNodes,
                                           const int nD,
                                           const double vol,
                                           DENS_MAT &dNdr)
  {
    // *** see comments for compute_N_dNdr ***
    const DENS_MAT &localCoords = feElement_->local_coords();
    double invVol = 1.0/vol;

    for (int inode = 0; inode < numNodes; ++inode) {
      for (int idr = 0; idr < nD; ++idr) {
        dNdr(idr,inode) = invVol;
      }
      for (int id = 0; id < nD; ++id) {
        for (int idr = 0; idr < nD; ++idr) {
          if (id == idr) dNdr(idr,inode) *= localCoords(id,inode);
          else dNdr(idr,inode) *= 1.0 +
                                  point(id)*localCoords(id,inode);
        }
      }
    }
  }

  void FE_InterpolateCartLin::compute_N_dNdr(const VECTOR &point,
                                             VECTOR &N,
                                             DENS_MAT &dNdr) const
  {
    // Required data from element class
    const DENS_MAT &localCoords = feElement_->local_coords();
    double invVol = 1.0/(feElement_->vol());
    int numEltNodes = feElement_->num_elt_nodes();

    // Fill in for each node
    for (int inode = 0; inode < numEltNodes; ++inode) {
      // Intiialize shape function and derivatives
      N(inode) = invVol;
      for (int idr = 0; idr < nSD_; ++idr) {
        dNdr(idr,inode) = invVol;
      }
      for (int isd = 0; isd < nSD_; ++isd) {
        // One term for each dimension
        N(inode) *= (1.0 + point(isd)*localCoords(isd,inode));
        // One term for each dimension, only deriv in deriv's dimension
        for (int idr = 0; idr < nSD_; ++idr) {
          if (isd == idr) dNdr(idr,inode) *= localCoords(isd,inode);
          else dNdr(idr,inode) *= 1.0 +
                                  point(isd)*localCoords(isd,inode);
        }
      }
    }
  }


  /*********************************************************
   * Class FE_InterpolateCartSerendipity
   *
   * For computing shape functions for quadratic serendipity
   * elements, implemented for SPEED.
   *
   *********************************************************/
  FE_InterpolateCartSerendipity::FE_InterpolateCartSerendipity(
                                   FE_Element *feElement)
    : FE_Interpolate(feElement)
  {
    set_quadrature(HEXA,GAUSS2);
  }

  FE_InterpolateCartSerendipity::~FE_InterpolateCartSerendipity()
  {
    // Handled by base class
  }

  void FE_InterpolateCartSerendipity::compute_N(const VECTOR &point,
                                                VECTOR &N)
  {
    // *** see comments for compute_N_dNdr ***
    const DENS_MAT &localCoords = feElement_->local_coords();
    double invVol = 1.0/(feElement_->vol());
    int numEltNodes = feElement_->num_elt_nodes();

    for (int inode = 0; inode < numEltNodes; ++inode) {
      N(inode) = invVol;
      for (int isd = 0; isd < nSD_; ++isd) {
        if (((inode == 8  || inode == 10 || inode == 16 || inode == 18) &&
             (isd == 0))                                                   ||
            ((inode == 9  || inode == 11 || inode == 17 || inode == 19) &&
             (isd == 1))                                                   ||
            ((inode == 12 || inode == 13 || inode == 14 || inode == 15) &&
             (isd == 2))) {
          N(inode) *= (1.0 - pow(point(isd),2))*2;
        } else {
          N(inode) *= (1.0 + point(isd)*localCoords(isd,inode));
        }
      }
      if (inode < 8) {
        N(inode) *= (point(0)*localCoords(0,inode) +
                     point(1)*localCoords(1,inode) +
                     point(2)*localCoords(2,inode) - 2);
      }
    }
  }

  // Sort of a test-ride for a generic version that can be used for
  // faces too. The only thing that's not "generic" is localCoords,
  // which very magically works in both cases.
  void FE_InterpolateCartSerendipity::compute_dNdr(const VECTOR &point,
                                                   const int numNodes,
                                                   const int nD,
                                                   const double vol,
                                                   DENS_MAT &dNdr)
  {
    // *** see comments for compute_N_dNdr ***
    const DENS_MAT &localCoords = feElement_->local_coords();
    double invVol = 1.0/vol;
    bool serendipityNode = false;
    double productRule1 = 0.0;
    double productRule2 = 0.0;

    if (nD != 3 && nD != 2) {
      ATC_Error("Serendipity dNdr calculations are too hard-wired to do "
                "what you want them to. Only 2D and 3D currently work.");
    }

    for (int inode = 0; inode < numNodes; ++inode) {
      for (int idr = 0; idr < nD; ++idr) {
        dNdr(idr,inode) = invVol;
      }
      for (int id = 0; id < nD; ++id) {
        for (int idr = 0; idr < nD; ++idr) {
          // identify nodes/dims differently if 3d or 2d case
          if (nD == 3) {
            serendipityNode =
             (((inode == 8  || inode == 10 || inode == 16 || inode == 18) &&
               (id == 0))                                                    ||
              ((inode == 9  || inode == 11 || inode == 17 || inode == 19) &&
               (id == 1))                                                    ||
              ((inode == 12 || inode == 13 || inode == 14 || inode == 15) &&
               (id == 2)));
          } else if (nD == 2) {
            serendipityNode =
             (((inode == 4 || inode == 6) && (id == 0))  ||
              ((inode == 5 || inode == 7) && (id == 1)));
          }
          if (serendipityNode) {
            if (id == idr) {
              dNdr(idr,inode) *= point(id)*(-4);
            } else {
              dNdr(idr,inode) *= (1.0 - pow(point(id),2))*2;
            }
          } else {
            if (id == idr) {
              dNdr(idr,inode) *= localCoords(id,inode);
            } else {
              dNdr(idr,inode) *= (1.0 + point(id)*localCoords(id,inode));
            }
          }
        }
      }
      for (int idr = 0; idr < nD; ++idr) {
        if (inode < 8) {
          // final corner contribution slightly different for 3d and 2d cases
          if (nD == 3) {
            productRule2 = (point(0)*localCoords(0,inode) +
                            point(1)*localCoords(1,inode) +
                            point(2)*localCoords(2,inode) - 2);
          } else if (nD == 2) {
            productRule2 = (point(0)*localCoords(0,inode) +
                            point(1)*localCoords(1,inode) - 1);
          }
          productRule1 = dNdr(idr,inode) *
                         (1 + point(idr)*localCoords(idr,inode));
          productRule2 *= dNdr(idr,inode);
          dNdr(idr,inode) = productRule1 + productRule2;
        }
      }
    }
  }

  void FE_InterpolateCartSerendipity::compute_N_dNdr(const VECTOR &point,
                                                     VECTOR &N,
                                                     DENS_MAT &dNdr) const
  {
    // Required data from element class
    const DENS_MAT &localCoords = feElement_->local_coords();
    double invVol = 1.0/(feElement_->vol());
    int numEltNodes = feElement_->num_elt_nodes();

    // Will store terms for product rule derivative for dNdr
    double productRule1;
    double productRule2;

    // Fill in for each node
    for (int inode = 0; inode < numEltNodes; ++inode) {
      // Initialize shape functions and derivatives
      N(inode) = invVol;
      for (int idr = 0; idr < nSD_; ++idr) {
        dNdr(idr,inode) = invVol;
      }
      // Add components from each dimension
      for (int isd = 0; isd < nSD_; ++isd) {
        for (int idr = 0; idr < nSD_; ++idr) {
          // Check to see if the node is NOT a corner node, and if its
          // "0-coordinate" is in the same dimension as the one we're currently
          // iterating over. If that's the case, we want to contribute to its
          // shape functions and derivatives in a modified way:
          if (((inode == 8  || inode == 10 || inode == 16 || inode == 18) &&
               (isd == 0))                                                   ||
              ((inode == 9  || inode == 11 || inode == 17 || inode == 19) &&
               (isd == 1))                                                   ||
              ((inode == 12 || inode == 13 || inode == 14 || inode == 15) &&
               (isd == 2))) {
            // If the 1d shape function dimension matches the derivative
            // dimension...
            if (isd == idr) {
              // contribute to N; sloppy, but this is the easiest way to get
              // N to work right without adding extra, arguably unnecessary
              // loops, while also computing the shape functions
              N(inode) *= (1.0 - pow(point(isd),2))*2;
              // contribute to dNdr with the derivative of this shape function
              // contribution
              dNdr(idr,inode) *= point(isd)*(-4);
            } else {
              // otherwise, just use the "constant" contribution to the deriv
              dNdr(idr,inode) *= (1.0 - pow(point(isd),2))*2;
            }
          } else {
            // non-serendipity style contributions
            if (isd == idr) {
              N(inode) *= (1.0 + point(isd)*localCoords(isd,inode));
              dNdr(idr,inode) *= localCoords(isd,inode);
            } else {
              dNdr(idr,inode) *= (1.0 + point(isd)*localCoords(isd,inode));
            }
          }
        }
      }
      // serendipity corner nodes require more extra handling
      if (inode < 8) {
        N(inode) *= (point(0)*localCoords(0,inode) +
                     point(1)*localCoords(1,inode) +
                     point(2)*localCoords(2,inode) - 2);
      }
      for (int idr = 0; idr < nSD_; ++idr) {
        if (inode < 8) {
          productRule1 = dNdr(idr,inode) *
                         (1 + point(idr)*localCoords(idr,inode));
          productRule2 = dNdr(idr,inode) * (point(0)*localCoords(0,inode) +
                                            point(1)*localCoords(1,inode) +
                                            point(2)*localCoords(2,inode) - 2);
          dNdr(idr,inode) = productRule1 + productRule2;
        }
      }
    }
  }


  /*********************************************************
   * Class FE_InterpolateSimpLin
   *
   * For computing linear shape functions of simplices,
   * which are rather different from those computed
   * in Cartesian coordinates.
   *
   * Note: degenerating quads/hexes can yield simplices
   *       as well, but this class is for computing these
   *       shape functions _natively_, in their own
   *       triangular/tetrahedral coordinate systems.
   *
   *********************************************************/
  FE_InterpolateSimpLin::FE_InterpolateSimpLin(
                                        FE_Element *feElement)
    : FE_Interpolate(feElement)
  {
    set_quadrature(TETRA,GAUSS2);
  }

  FE_InterpolateSimpLin::~FE_InterpolateSimpLin()
  {
    // Handled by base class
  }

  void FE_InterpolateSimpLin::compute_N(const VECTOR &point,
                                        VECTOR &N)
  {
    int numEltNodes = feElement_->num_elt_nodes();

    // Fill in for each node
    for (int inode = 0; inode < numEltNodes; ++inode) {
      if (inode == 0) {
        // Fill N...the ips are serving as proxies for "dimensions"
        // since we're in tetrahedral coordinates, except that
        //   0th node = 3rd "dimension" (u or O_o)
        //   1st node = 0th "dimension" (x or r)
        //   2nd node = 1st "dimension" (y or s)
        //   3rd node = 3nd "dimension" (z or t)
        // and remember that u = 1 - r - s - t for tet coords
        N(inode) = 1;
        for (int icont = 0; icont < nSD_; ++icont) {
          N(inode) -= point(icont);
        }
      } else {
        N(inode) = point(inode-1);
      }
    }
  }

  void FE_InterpolateSimpLin::compute_dNdr(const VECTOR &,
                                           const int numNodes,
                                           const int nD,
                                           const double,
                                           DENS_MAT &dNdr)
  {
    // Fill in for each node
    for (int inode = 0; inode < numNodes; ++inode) {
      // Fill dNdr_; we want 1 if the dimension of derivative
      // and variable within N correspond. That is, if N == r,
      // we want the 0th dimension to contain (d/dr)r = 1. Of
      // course, (d/di)r = 0 forall i != r, so we need that as
      // well. This is a bit elusively complicated. Also, the 0th
      // integration point contains the term u = 1 - r - s - t.
      // (which map to x, y, and z). Therefore, the derivative in
      // each dimension are -1.
      //
      // The idea is similar for 2 dimensions, which this can
      // handle as well.
      for (int idr = 0; idr < nD; ++idr) {
        if (inode == 0) {
          dNdr(idr,inode) = -1;
        } else {
          dNdr(idr,inode) = (inode == (idr + 1)) ? 1 : 0;
        }
      }
    }
  }

  void FE_InterpolateSimpLin::compute_N_dNdr(const VECTOR &point,
                                             VECTOR &N,
                                             DENS_MAT &dNdr) const
  {
    int numEltNodes = feElement_->num_elt_nodes();

    // Fill in for each node
    for (int inode = 0; inode < numEltNodes; ++inode) {
      // Fill N...
      if (inode == 0) {
        N(inode) = 1;
        for (int icont = 0; icont < nSD_; ++icont) {
          N(inode) -= point(icont);
        }
      } else {
        N(inode) = point(inode-1);
      }
      // Fill dNdr...
      for (int idr = 0; idr < nSD_; ++idr) {
        if (inode == 0) {
          dNdr(idr,inode) = -1;
        } else {
          dNdr(idr,inode) = (inode == (idr + 1)) ? 1 : 0;
        }
      }
    }
  }


} // namespace ATC