-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFiniteElements.cpp
1443 lines (1190 loc) · 47.2 KB
/
FiniteElements.cpp
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
/*
BSD 2-Clause License
Copyright (c) 2020, Andrey Kudryavtsev ([email protected])
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Vector.h"
#include "Conforming3D.h"
#include "Matrix.h"
#include "FEMVirtMatrix.h"
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <limits>
#include <chrono>
/**
Full finite-element C++ code with SIMD-accelerated equation solver based on conforming linear
finite elements with tests and example solution to Laplace equation. From scratch.
*/
/**
Conforming finite elements
--------------------------
This is an example code (VS 2019 without Windows specifics) on how to write approximation and
solution to a partial differential equation with conforming linear finite elements. Everything from
scratch, no third-party code.
The element with all associated code is here in <I>Conforming3D.h</I>. "Conforming" means that you
do not have to use a conformal mesh but build approximation on such constructions like balanced
octrees. The element provides continuous approximation across element faces, but its shape functions
are not trivial.
Conforming linear finite element
--------------------------------
<I>K.-W. Cheng, T.-P. Fries. XFEM with Hanging Nodes for Two-phase
Incompressible Flow</I>.
(7)----------18-----------(6)
/| /|
/ | / |
19 | 25 17 |
/ | / |
/ 15 23 / 14
(4)-----------16----------(5) |
| | | |
| 24 | | 22 |
| | | |
| (3)----------10------|----(2)
12 / 21 13 /
| / | /
| 11 20 | 9
W ^ /V | /
|/ |/
(0)->---------8-----------(1)
U
Any node from 8 to 25 (18 nodes in all) may not exist. That is, 8 standard and
18 hanging. The element provides C0-continuous approximation across faces of
neighbour elements.
Parameters U,V,W change from 0 to 1 from the node 0.
T - class for real numbers
Tvector - class for 3D vector with up to 4 components
Derivatives
-----------
Derivatives are not continuous across parametric value of 0.5 due to e.g.
T c0a = T1 - std::abs(c0); in shapeFunctionsConforming() - abs value
function expressions, derivatives at and near hanging nodes with a parameter 0.5
are not well defined as abs() is discontinuous, therefore the two functions
shapeDerivativesConformingSimple() based on finite differences and shapeDerivativesExact() -
exact expression MAY GIVE VERY DIFFERENT results. Because the derivative does not exist at x = 0
for function y = |x|.
In general, first derivatives for a linear approximation must be constant, so the use
of shapeDerivativesConformingFromSubOctant() which calculates derivatives at centres
of sub-octants looks most consistent.
Matrix
------
This class is for operations with not very large matrices but mainly for manipulations with
finite element shape functions. The class does not contain any special features for speedup,
it is a regular code for matrix ariphmetics and inversion for 2 x 2, 3 x 3 and 4 x 4 matrices.
These formulae are 100% reliable.
Solution to a system of equations
---------------------------------
A FEM system is banded and the code stores matrix by rows taking into account its banded nature with the
solution by Gauss elemination. The built-in SIMD makes the code sufficiently faster. But multithreading does not
make any good. I do not know why, did not try to understand why. The maximum reasonable size of the
system is something up to 70..100 thousands with 1/10 band width; the bigger solution will be destroyed by
roundup errors, storage probems and time of solution. A better parallelisation and a more sophisticated
algorithm is necessary for a good commercial code.
Tests
-----
A group of tests, define/undefine TEST_MATRIX, TEST_ELEMENT, TEST_SOLUTION below.
You can select various types for templates, all variants are possible :
#define T float - float variables are 4-byte floats
#define T double - floats are 8-bytes long
#define Tvector Vector4 - SIMD-accelerated 4-component vector based on 4-byte floats
#define Tvector TVector<float> - plain 4-component vector on floats
#define Tvector TVector<double> - plain 4-component vector on doubles
When testing solution to the system of equations, the matrix is not quite well positive definite,
so it would be much better to use doubles, like this :
#define T double
#define T TVector<double>
If T and Tvector have different basic types, the code produces some type-conversion warnings, I left them
as they are.
List of tests : disabled/enable by #define TEST_... :
- Tests of Matrix class : ariphmetics, multiplies, inversion etc.
- Tests of finite element shape functions : sum must be 1, for derivatives 0 etc. Some tests may fail
due to random nature of data when inconsistent with tolerance value currently set.
- Tests of parallel solution to a banded system of equations. The matrix is not well positive definite,
so residuals maybe not very good with 4-byte float types. Another thing is that the increase in the
number of threads does not help or even makes the code slower.
- ... and finally finite element test. Mesh is not generated here; it is a matter of further publications.
It is as follows :
(3)-----------------------(12)---------(21)--------(30) Z=1.0
/| /| /| /|
/ | / | / | / |
/ | (11)---------(20)--------(29)|
/ | /| | /| | /| |
/ | / | (9)------/-|-(18)--- /-| (27)
(2)-----------------------(10)---------(19)--------(28)| /|
| | | |/ | | |/ | | |/ |
| | | (8)------- |-(17)----- | (26)|
| | | /| | | /| | | /| |
| (1)------------------|/-|-(6)-----|/---(15)---|/-|-(24) Y=1.0
| / (7)----------(16)--------(25)| /
| / | |/ | |/ | |/
| / | (5)--------|-(14)------|-(23)
Z ^ /Y | / | / | /
|/ |/ |/ |/
(0)->---------------------(4)----------(13)--------(22)
X X=1.0 X=2.0
- one big conforming finite element with five hanging nodes (5,7,8,9,11) and eight
non-conforming small finite elements each with 8 nodes.
The "non-conforming" plane is X = 0.5 to achieve continuous approximation
across it with our conforming finite element Confroming3D.
The problem is formulated as follows : solve Laplace equation with essential boundary
conditions at plane X = 0 (potential is 0.0) and at plane X = 2.0 (specify potential 1.0).
On all other planes natural boundary conditions will be automatically satisfied. Read
<I>J.J.Connor, C.A.Brebbia Finite Element Techniques for Fluid Flow</I>. Very good about weak
formulations, stiffness and mass matrices etc.
So we expect that potential value will grow linearly from 0.0 at X = 0 to 1.0 at X = 2 with
the derivative of potential (velocity) along X equal everywhere to 1.0 / 2.0 = 0.5 and zero velocity
components in Y and Z.
A test printout
--------------
Seed 1609068822
===== Tests of matrix =====
Transpose
Multiply (AB)T = BT AT
Ariphmetic
Inversion
===== Tests of 3D conforming finite element =====
Testing incorrectly defined elements (arbitrary hanging nodes), shape function value at a node must be 1, at all other nodes 0
Testing correctly defined elements (whole faces with hanging nodes), shape function value at a node must be 1, at all other nodes 0
Calculation of shape function derivatives by four methods must yield close results
Calculation of values at some specific cases (correct hanging nodes on two faces)
Derivatives on real coordinates, element 2 x 0.5 x 0.75
Derivatives on parameters and real coordinates, random tests :
(1) element derivatives on X,Y,Z must be units (identity matrix)
(2) element derivatives on parameters must be equal to box sizes
(3) element volume must be equal to Jacobi matrix determinant
===== Solution to banded system with SIMD and multiple threads =====
Allocator stored file : FEMVirtMatrix.bin, size 40000000
Solving system of size 5000 by 500
1. solving with simple C solver...
4900 of 5000
2. solving with parallel solver...
4900
Time : simple solver 2.75633 sec, SIMD parallel solver 0.374472, acceleration 7.36059
Tesing results
Comparing solutions...
Max difference b/w solutions is 9.19168e-13, max solution value 0.109975, min value -0.110092, residual 8.99503e-13, parallel residual 1.33116e-12
Allocator stored file deleted : FEMVirtMatrix.bin
===== Solving Laplace equation with conforming finite elements =====
Allocator stored file : FEMVirtMatrix.bin, size 6944
0
Max residual 1.4988e-15
X Y Z potential gradient
0.5 0.5 0.5 0.25 0.5 5.55112e-17 -5.55112e-17
1.25 0.25 0.25 0.625 0.5 -5.55112e-17 5.55112e-17
1.25 0.75 0.25 0.625 0.5 -5.55112e-17 -1.66533e-16
1.25 0.25 0.75 0.625 0.5 -1.11022e-16 0
1.25 0.75 0.75 0.625 0.5 -2.77556e-17 -2.77556e-17
1.75 0.25 0.25 0.875 0.5 0 0
1.75 0.75 0.25 0.875 0.5 -1.11022e-16 -1.11022e-16
1.75 0.25 0.75 0.875 0.5 -1.11022e-16 -1.11022e-16
1.75 0.75 0.75 0.875 0.5 -1.11022e-16 -1.11022e-16
Allocator stored file deleted : FEMVirtMatrix.bin
What is good and what is bad
----------------------------
- Ready 100% reliable code for conforming finite elemnet. Remember that derivatives of shape functions are
discontinuous within the element.
- Matrix class for small matrices with 100% reliable inversion formulae. If you run into a problem,
you might have forgotten to transpose a matrix somewhere. Matrix operations are slow, no special attention
to code speed.
- Vector class for 4-component vectors Vector4 is accelerated by SIMD, but it cannot help much without
reorganisation of the whole code.
- FEMVirtMatrix is SIMD accelerated with normal speedup 4 times or more, but multithreading does not help
very much, maybe vecause of my computer is with just a single hardware set of XMM registers.
- The last test - solution to a Laplace equation is very very simple but produces correct results. We need
a mesh generation code for proper testing (to come).
References
----------
- J.J.Connor, C.A.Brebbia. Finite Element Techniques for Fluid Flow (about finite elements).
- K.-W. Cheng, T.-P. Fries. XFEM with Hanging Nodes for Two-phase Incompressible Flow (shape
functions for conforming finite element).
*/
using namespace std;
// Random integer
static int random(const int max)
{
return (rand() - 1) * max / RAND_MAX;
}
// Random real
template <typename T> T random()
{
return static_cast<T>(rand()) / static_cast<T>(RAND_MAX);
}
// Compute difference between two arrays of vectors
template <typename T, typename Tvector, size_t N> T diff(const std::array<Tvector,N> &a0,
const std::array<Tvector,N> &a1, const bool print = false)
{
#if 0
T max = -std::numeric_limits<T>::max();
int index = -1;
Tvector sum0(0,0,0), sum1(0,0,0);
for (int i = 0; i < N; i++)
{
T l = !(a0[i] - a1[i]);
if (l > max)
{
max = l;
index = i;
}
sum0 += a0[i];
sum1 += a1[i];
}
if (print)
cout << "max " << max << " index " << index <<
" sum0 = " << sum0.X << " " << sum0.Y << " " << sum0.Z <<
" sum1 = " << sum1.X << " " << sum1.Y << " " << sum1.Z << endl;
return max;
#else
std::array<T,N> l;
std::transform(a0.begin(),a0.end(),a1.begin(),l.begin(),[](auto v0, auto v1) { return !(v0 - v1); });
T sum = std::accumulate(l.begin(),l.end(),static_cast<T>(0.0),std::plus<T>());
sum /= static_cast<T>(N);
return sum;
#endif
}
// Create random matrix N * M
template <typename T, size_t N, size_t M> Matrix<T,N,M> randomMatrix(const T max)
{
Matrix<T,N,M> m;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
#if 1
m[i][j] = T(random(int(max)));
#else
m[i][j] = random<T>() * max;
#endif
}
}
return m;
}
template <typename T, typename Tvector> void addElementCoordinates(const Tvector& min, const Tvector& max,
std::vector<Tvector> &totalCoordinates)
{
// fill all coordinates including all hanging
std::array<Tvector,Conforming3D<T,Tvector>::NUM_NODES> basicCoordinates = {
Tvector(min.X,min.Y,min.Z),
Tvector(max.X,min.Y,min.Z),
Tvector(max.X,max.Y,min.Z),
Tvector(min.X,max.Y,min.Z),
Tvector(min.X,min.Y,max.Z),
Tvector(max.X,min.Y,max.Z),
Tvector(max.X,max.Y,max.Z),
Tvector(min.X,max.Y,max.Z),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0)),
Tvector(T(0.0),T(0.0),T(0.0))
};
for (int i = 0; i < Conforming3D<T,Tvector>::NUM_NODES; i++)
{
std::array<T,Conforming3D<T,Tvector>::NUM_NODES> func;
Conforming3D<T,Tvector>::shapeFunctions(Conforming3D<T,Tvector>::nodeParms()[i],func);
Tvector c = shapeFuncValue(func,basicCoordinates,Tvector(T(0.0),T(0.0),T(0.0)));
totalCoordinates.push_back(c);
}
}
#define TEST_MATRIX
#define TEST_ELEMENT
#define TEST_SOLUTION
int main()
{
// can change it here into float/Vector4
#define T double
#define Tvector TVector<double>
typedef Conforming3D<T,Tvector> Element;
// start random generator
auto seed = static_cast<unsigned int>(time(NULL));
srand(seed);
cout << " Seed " << seed << std::endl << std::endl;
#ifdef TEST_MATRIX
cout << " ===== Tests of matrix =====" << endl << endl;
cout << " Transpose" << endl << endl;
{
T max = static_cast<T>(8.0);
for (int i = 0; i < 100; i++)
{
Matrix<T,4,2> m = randomMatrix<T,4,2>(max);
Matrix<T,2,4> mT = m.transpose();
Matrix<T,4,2> mold = mT.transpose();
assert(m == mold);
}
for (int i = 0; i < 100; i++)
{
Matrix<T,4,4> m = randomMatrix<T,4,4>(max);
Matrix<T,4,4> mold = m;
m.transposeSelf();
assert(m != mold);
m.transposeSelf();
assert(m == mold);
}
int gsggsgs = 0;
}
cout << " Multiply (AB)T = BT AT" << endl << endl;
{
T max = static_cast<T>(18.0);
for (int i = 0; i < 100; i++)
{
Matrix<T,13,7> A = randomMatrix<T,13,7>(max);
Matrix<T,7,13> B = randomMatrix<T,7,13>(max);
Matrix<T,13,13> AB = A * B;
AB.transposeSelf();
Matrix<T,13,7> BT = B.transpose();
Matrix<T,7,13> AT = A.transpose();
Matrix<T,13,13> BTAT = BT * AT;
assert(AB == BTAT);
}
}
cout << " Ariphmetic" << endl << endl;
{
T max = static_cast<T>(3.0);
for (int i = 0; i < 100; i++)
{
Matrix<T,13,7> A = randomMatrix<T,13,7>(max);
Matrix<T,13,7> B = randomMatrix<T,13,7>(max);
Matrix<T,13,7> AB = A + B * max;
Matrix<T,13,7> A1 = AB - B * max;
assert(A == A1);
}
for (int i = 0; i < 100; i++)
{
Matrix<T,4,4> A = randomMatrix<T,4,4>(max);
Matrix<T,4,4> B = randomMatrix<T,4,4>(max);
Matrix<T,4,4> AB = A + B * max;
Matrix<T,4,4> A1 = AB - B * max;
assert(A == A1);
}
for (int i = 0; i < 100; i++)
{
Matrix<T,3,3> A = randomMatrix<T,3,3>(max);
Matrix<T,3,3> B = randomMatrix<T,3,3>(max);
Matrix<T,3,3> AB = A + B * max;
Matrix<T,3,3> A1 = AB - B * max;
assert(A == A1);
}
for (int i = 0; i < 100; i++)
{
Matrix<T,2,2> A = randomMatrix<T,2,2>(max);
Matrix<T,2,2> B = randomMatrix<T,2,2>(max);
Matrix<T,2,2> AB = A + B * max;
Matrix<T,2,2> A1 = AB - B * max;
assert(A == A1);
}
}
cout << " Inversion" << endl << endl;
{
T max = static_cast<T>(13.0);
Matrix<T,4,4>::setTolerance(std::numeric_limits<T>::epsilon() * static_cast<T>(100.0) * max * max);
Matrix<T,3,3>::setTolerance(std::numeric_limits<T>::epsilon() * static_cast<T>(100.0) * max * max);
for (int i = 0; i < 100; i++)
{
Matrix<T,4,4> A = randomMatrix<T,4,4>(max);
A.setDiag(max);
Matrix<T,4,4> A1 = +A;
Matrix<T,4,4> I = A * A1;
assert(I.isIdentity());
}
for (int i = 0; i < 100; i++)
{
Matrix<T,3,3> A = randomMatrix<T,3,3>(max);
A.setDiag(max);
Matrix<T,3,3> A1 = +A;
Matrix<T,3,3> I = A * A1;
assert(I.isIdentity());
}
for (int i = 0; i < 100; i++)
{
Matrix<T,2,2> A = randomMatrix<T,2,2>(max);
A.setDiag(max);
Matrix<T,2,2> A1 = +A;
Matrix<T,2,2> I = A * A1;
assert(I.isIdentity());
}
Matrix<T,4,4>::setDefaultTolerance();
}
#endif
#ifdef TEST_ELEMENT
cout << " ===== Tests of 3D conforming finite element =====" << endl << endl;
cout << " Testing incorrectly defined elements (arbitrary hanging nodes), shape function value at a node must be 1, at all other nodes 0" << endl << endl;
{
T tolerance = static_cast<T>(0.000001);
for (int i = 0; i < 100; i++)
{
std::array<LINT,Element::NUM_BASICNODES> nodes = {0,1,2,3,4,5,6,7};
std::array<LINT,Element::NUM_HANGINGNODES> hangingNodes = {
-1, // 8 0
-1, // 9 1
-1, // 10 2
-1, // 11 3
-1, // 12 4
-1, // 13 5
-1, // 14 6
-1, // 15 7
-1, // 16 8
-1, // 17 9
-1, // 18 10
-1, // 19 11
-1, // 20 12
-1, // 21 13
-1, // 22 14
-1, // 23 15
-1, // 24 16
-1 // 25 17
};
// random conforming nodes
int n = random(Element::NUM_HANGINGNODES);
for (int j = 0; j < n; j++)
{
hangingNodes[random(Element::NUM_HANGINGNODES - 1)] = 1;
}
// create element
Element e(nodes,hangingNodes);
// Standard shape functions : check 1.0 is only value among all shape functions
for (int j = 0; j < Element::NUM_BASICNODES; j++)
{
Tvector coords = e.nodeParms()[j];
std::array<T,Element::NUM_NODES> func;
e.shapeFunctions(coords,func);
for (int k = 0; k < Element::NUM_BASICNODES; k++)
{
if (k == j)
{
if (std::abs(func[k] - T1) > tolerance)
{
assert(false);
}
} else
{
if (std::abs(func[k]) > tolerance)
{
assert(false);
}
}
}
}
// Sum of all shape functions must be 1.0
for (int j = 0; j < Element::NUM_NODES; j++)
{
Tvector coords = e.nodeParms()[j];
std::array<T,Element::NUM_NODES> func;
e.shapeFunctionsConforming(coords,func);
T sum = std::accumulate(func.begin(),func.end(),T0,std::plus<T>());
assert(std::abs(sum - T1) < tolerance);
}
// Conforming functions : check 1.0
for (int j = 0; j < Element::NUM_NODES; j++)
{
Tvector coords = e.nodeParms()[j];
std::array<T,Element::NUM_NODES> func;
e.shapeFunctionsConforming(coords,func);
if (j < Element::NUM_BASICNODES)
{
for (int k = 0; k < Element::NUM_BASICNODES; k++)
{
if (k == j)
{
if (std::abs(func[k] - T1) > tolerance)
{
assert(false);
}
} else
{
if (std::abs(func[k]) > tolerance)
{
assert(false);
}
}
}
for (int k = Element::NUM_BASICNODES; k < Element::NUM_NODES; k++)
{
if (std::abs(func[k]) > tolerance)
{
assert(false);
}
}
} else
{
if (!e.nodeExists(j))
continue;
// This test is incorrect : the element maybe wrong with arbitrary
// selection of hanging nodes; i.e. all nodes on a face must be hanging
// to produce zeroes at [0..7] and single unit in [8..25]
//for (int k = 0; k < Element::NUM_BASICNODES; k++)
//{
// if (std::abs(func[k]) > tolerance)
// {
// assert(false);
// }
//}
for (int k = Element::NUM_BASICNODES; k < Element::NUM_NODES; k++)
{
if (k == j)
{
if (std::abs(func[k] - T1) > tolerance)
{
assert(false);
}
} else
{
// see comment above
//if (std::abs(func[k]) > tolerance)
//{
// assert(false);
//}
}
}
}
}
}
}
cout << " Testing correctly defined elements (whole faces with hanging nodes), shape function value at a node must be 1, at all other nodes 0" << endl << endl;
{
T tolerance = static_cast<T>(0.000001);
for (int i = 0; i < 100; i++)
{
std::array<LINT,Element::NUM_BASICNODES> nodes = {0,1,2,3,4,5,6,7};
std::array<LINT,Element::NUM_HANGINGNODES> hangingNodes = {
-1, // 8 0
-1, // 9 1
-1, // 10 2
-1, // 11 3
-1, // 12 4
-1, // 13 5
-1, // 14 6
-1, // 15 7
-1, // 16 8
-1, // 17 9
-1, // 18 10
-1, // 19 11
-1, // 20 12
-1, // 21 13
-1, // 22 14
-1, // 23 15
-1, // 24 16
-1 // 25 17
};
// random conforming nodes
int n = random(6);
for (int j = 0; j < n; j++)
{
int face = random(6);
for (int k = 4; k < 9; k++)
hangingNodes[Element::faces()[face][k] - 8] = 1;
}
// create element
Element e(nodes,hangingNodes);
// Standard shape functions : check 1.0 is only value among all shape functions
for (int j = 0; j < Element::NUM_BASICNODES; j++)
{
Tvector coords = e.nodeParms()[j];
std::array<T,Element::NUM_NODES> func;
e.shapeFunctions(coords,func);
for (int k = 0; k < Element::NUM_BASICNODES; k++)
{
if (k == j)
{
if (std::abs(func[k] - T1) > tolerance)
{
assert(false);
}
} else
{
if (std::abs(func[k]) > tolerance)
{
assert(false);
}
}
}
}
// Sum of all shape functions must be 1.0
for (int j = 0; j < Element::NUM_NODES; j++)
{
Tvector coords = e.nodeParms()[j];
std::array<T,Element::NUM_NODES> func;
e.shapeFunctionsConforming(coords,func);
T sum = std::accumulate(func.begin(),func.end(),T0,std::plus<T>());
assert(std::abs(sum - T1) < tolerance);
}
// Conforming functions : check 1.0
for (int j = 0; j < Element::NUM_NODES; j++)
{
Tvector coords = e.nodeParms()[j];
std::array<T,Element::NUM_NODES> func;
e.shapeFunctionsConforming(coords,func);
if (j < Element::NUM_BASICNODES)
{
for (int k = 0; k < Element::NUM_BASICNODES; k++)
{
if (k == j)
{
if (std::abs(func[k] - T1) > tolerance)
{
assert(false);
}
} else
{
if (std::abs(func[k]) > tolerance)
{
assert(false);
}
}
}
for (int k = Element::NUM_BASICNODES; k < Element::NUM_NODES; k++)
{
if (std::abs(func[k]) > tolerance)
{
assert(false);
}
}
} else
{
if (!e.nodeExists(j))
continue;
for (int k = 0; k < Element::NUM_BASICNODES; k++)
{
if (std::abs(func[k]) > tolerance)
{
assert(false);
}
}
for (int k = Element::NUM_BASICNODES; k < Element::NUM_NODES; k++)
{
if (k == j)
{
if (std::abs(func[k] - T1) > tolerance)
{
assert(false);
}
} else
{
// see comment above
if (std::abs(func[k]) > tolerance)
{
assert(false);
}
}
}
}
}
}
}
cout << " Calculation of shape function derivatives by four methods must yield close results" << endl << endl;
{
T tolerance = static_cast<T>(0.0002);
for (int i = 0; i < 10000; i++) //!!!!!!
{
std::array<LINT,Element::NUM_BASICNODES> nodes = {0,1,2,3,4,5,6,7};
std::array<LINT,Element::NUM_HANGINGNODES> hangingNodes = {
-1, // 8 0
-1, // 9 1
-1, // 10 2
-1, // 11 3
-1, // 12 4
-1, // 13 5
-1, // 14 6
-1, // 15 7
-1, // 16 8
-1, // 17 9
-1, // 18 10
-1, // 19 11
-1, // 20 12
-1, // 21 13
-1, // 22 14
-1, // 23 15
-1, // 24 16
-1 // 25 17
};
// random conforming nodes
int n = random(6);
for (int j = 0; j < n; j++)
{
int face = random(6);
for (int k = 4; k < 9; k++)
hangingNodes[Element::faces()[face][k] - 8] = 1;
}
// create element
Element e(nodes,hangingNodes);
std::array<Tvector,Element::NUM_NODES> dfunc0,dfunc1,dfunc2,dfunc3;
// coordinates must not be close to 0.0,0.5,1.0; otherwise
// difference between analytic and finite difference calculations
// may be large (derivatives are discontinuous at hanging nodes due to
// absolute value expression in shape functions (see explanation in
// function decsriptions)
Tvector coords(random<T>(),random<T>(),random<T>());
for (int k = 0; k < 3; k++)
{
// must be far from 0.0,0.5,1.0 at a distance larger than
// finite difference step (0.001)
T d = coords[k] - T(0.5);
if (std::abs(d < T(0.01)))
{
if (coords[k] > T(0.5))
{
coords[k] = T(0.51);
} else
{
coords[k] = T(0.49);
}
}
}
e.shapeDerivativesConformingExact(coords,dfunc0);
e.shapeDerivativesConformingSimple(coords,dfunc1);
e.shapeDerivativesNonConforming(coords,dfunc2);
e.shapeDerivativesConformingFromSubOctant(coords,dfunc3);
// check differences
T diff01 = diff<T,Tvector,Element::NUM_NODES>(dfunc0,dfunc1,true);
T diff02 = diff<T,Tvector,Element::NUM_NODES>(dfunc0,dfunc2);
T diff03 = diff<T,Tvector,Element::NUM_NODES>(dfunc0,dfunc3);
// compare only two close methods : exact and finite difference approximation;
// they must be close
if (diff01 >= 0.02)
{
cout << diff01 << endl;
assert(false);
}
// check sum
Tvector sum0, sum1, sum2, sum3;
sum0 = sum1 = sum2 = sum3 = Tvector(T0,T0,T0);
for (int i = 0; i < Element::NUM_NODES; i++)
{
sum0 += dfunc0[i];
sum1 += dfunc1[i];
sum2 += dfunc2[i];
sum3 += dfunc3[i];
}
T len0 = !sum0, len1 = !sum1, len2 = !sum2, len3 = !sum3;
assert(len0 < tolerance);
assert(len1 < tolerance);
assert(len2 < tolerance);
assert(len3 < tolerance);
}
}
cout << " Calculation of values at some specific cases (correct hanging nodes on two faces)" << endl << endl;
{
T tolerance = static_cast<T>(0.000001);
Tvector coords(T0,T05,T05);
std::array<LINT,Element::NUM_BASICNODES> nodes = {0,1,2,3,4,5,6,7};
std::array<LINT,Element::NUM_HANGINGNODES> hangingNodes = {
-1, // 8 0
-1, // 9 1
1, // 10 2
1, // 11 3
1, // 12 4
-1, // 13 5
1, // 14 6
1, // 15 7
-1, // 16 8
-1, // 17 9
1, // 18 10
1, // 19 11
-1, // 20 12
-1, // 21 13
-1, // 22 14
1, // 23 15
1, // 24 16
-1 // 25 17
};
// create element
Element e(nodes,hangingNodes);
std::array<T,Element::NUM_NODES> simplefunc,func;
e.shapeFunctions(coords,simplefunc);
e.shapeFunctionsConforming(coords,func);
for (int i = 0; i < 100; i++)
{
std::array<T,26> values =
{5,5,5,5,5, 5,5,5,5,5, 5,5,5,5,5, 5,5,5,5,5, 5,5,5,5,50, 5};
values[random(26)] = random<T>() * T(10.0);