-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.cpp
More file actions
2107 lines (1824 loc) · 90.3 KB
/
physics.cpp
File metadata and controls
2107 lines (1824 loc) · 90.3 KB
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
#include "main.h"
#include "geometry.h"
#include "physics.h"
#include <algorithm>
class RigidBody;
struct StructureDefinition;
FElement::FElement(double verticesLoc[][3], double matE, double matV, int inds[], int numNodesGiven){
numNodes = numNodesGiven;
for (int i = 0; i < numNodes; i++){
verts.push_back(glm::vec3());
for (int j = 0; j < 3; j++){
verts[i][j] = verticesLoc[i][j];
}
}
for (int i = 0; i < numNodes; i++){
vertIndex.push_back(inds[i]);
}
E = matE;
v = matV;
switch (numNodes){
case 4:
calcElementStiffnessMatrixTet();
break;
case 8:
calcElementStiffnessMatrixHex();
break;
default:
break;
}
}
FElement::~FElement(){
delete elementStiffnessMatrix;
}
void FElement::calcElementStiffnessMatrixTet(){
// idea for how to do it is based of https://www.unm.edu/~bgreen/ME360/2D%20Triangular%20Elements.pdf a method for finding stiffness matrix of a triangle in 2d
// i expanded it into 3d and 3d element and implemented
// calculate the element stiffness matrix for a solid tetrahedron in 3d
// i is a direction j and k are other directions all perpendiular
// calculate stress matrix from generalised hookes law
// ex = sx/E -v*sy/E -v*sz/E
// ey = -v*sx/E +sy/E -v*sz/E
// ez = -v*sx/E -v*sy/E +sz/E
// ei (for i in x,y,z) usually shown epsilon subscript i is strain in the direction of i
// si (for i in x,y,z) usually shown sigma subscript i is stress in the direction of i
// E is youngs modulus a constant in a material equal to stress/strain in 1d eg stretch a wire
// v is poisson ratio usually shown as nu is a constant in a material equal to the ratio a cube decreases in size perpendiular to force to size increase parallel to force
// sx = E*ex + v*sy + v*sz
// sy = E*ey + v*sz + v*sx
// sz = E*ez + v*sx + v*sy
// sx = E*ex + v*E*ez + (v^2)*sx + (v^2)*sy + v*sy
// sy = E*ey + v*(E*ez + v*sx + v*sy) + v*sx
// sy - (v^2)*sy = E*ey +v*E*ez + (v^2)*sx + v*sx
// sy = (1/(1-(v^2))*(E*ey + v*E*ez + (v^2 + v)*sx)
// sx = E*ex + v*E*ez + (v^2)*sx + (v^2 + v)/(1-(v^2))*(E*ey + v*E*ez + (v^2 + v)*sx)
// sx = E*ex + v*E*ez + (v^2)*sx + (v*(1+v))/((1-v)*(1+v))*(E*ey + v*E*ez + (v^2 + v)*sx)
// sx = E*ex + v*E*ez + (v^2)*sx + (v/(1-v))*(E*ey + v*E*ez + (v^2 + v)*sx)
// sx - v*sx = E*ex - v*E*ex + v*E*ez - (v^2)*E*ez + (v^2)*sx - (v^3)*sx + v*E*ey + (v^2)*E*ez + (v^3)*sx + (v^2)*sx
// sx - v*sx = E*ex - v*E*ex + v*E*ez + v*E*ey + 2*(v^2)*sx
// sx - v*sx - 2*(v^2)*sx = E*ex - v*E*ex + v*E*ez + v*E*ey
// sx*(1 - v - 2*(v^2)) = E*((1-v)*ex + v*ey + v*ez)
// sx = (E/ ((1+v)*(1-2v)) )*( (1-v)*ex + v*ey + v*ez )
// as symetric system can swap x y and z to get other equations
// sx = (E/ ((1+v)*(1-2v)) )*( (1-v)*ex + v*ey + v*ez )
// sy = (E/ ((1+v)*(1-2v)) )*( v*ex + (1-v)*ey + v*ez )
// sz = (E/ ((1+v)*(1-2v)) )*( v*ex + v*ey + (1-v)*ez )
// shear stress formulae
// tij (for i,j in x,y,z i != j) usually denoted tau subscript ij is shear strain in the direction combination ij, tij == tji so can ignore half of them
// gij (for i,j in x,y,z i != j) usually denoted gamma subscript ij is shear stress in the direction combination ij, gij == gji so can ignore half of them
// G is shear modulus = E/(2*(1+v))
// gij = tij/G
// tij = G*gij
// tij = E/(2*(1+v))*gij
// s = vec6[sx,sy,sz,txy,tyz,tzx]
// e = vec6[ex,ey,ez,gxy,gyz,gzx]
// D is a 6x6 matrix which turns e into s
// s = De
// have formulae for s in linear terms of e so can just plug in values
// first factor out (E/ ((1+v)*(1-2v))) as it makes it simpler
/* D = ( E/((1+v)*(1-2v)) )*[[1-v,v , v,0 ,0 ,0 ]
[v ,1-v, v,0 ,0 ,0 ]
[v ,v ,1-v,0 ,0 ,0 ]
[0 ,0 ,0 ,(1-2v)/2,0 ,0 ]
[0 ,0 ,0 ,0 ,(1-2v)/2,0 ]
[0 ,0 ,0 ,0 ,0 ,(1-2v)/2]]*/
// now need to find strain matrix, assume all d(something)/d(something else) is not a derivitive but is a partial derivitive
// x,y,z are the positions in coorinates
// u,v,w are the changes in displacement from start in directions x,y,z
// e = jacobian vec6[du/dx, dv/dy, dw/dz, du/dy + dv/dx, dv/dz + dw/dy, dw/dx + du/dz] = vec6[ex,ey,ez,gxy,gyz,gzx]
// there are 3 degrees of freedom per vertex and 4 vertexs so 12 dof
// these are displacements of vertices from starting location of vertex defined as, q = vec12[q0,q1,q2,...,q10,q11]
// q(3i+j) = vertex i displacement in direction j (0 = x, 1 = y, 2 = z)
// there exists a matrix such that e = Bq
// must be able to define e at every point in the tetrahedron so need a way to interpolate between vertices
// a similar method to used for 2d triangle can be used spliting the volume into 4 tetrahedrons V(0 to 3)
// split by this point being the point interpolating for
// 4 shape functions N(0 to 3) = V(0 to 3)/ total V
// u = N0*q0 + N1*q3 + N2*q6 + N3*q9
// v = N0*q1 + N1*q4 + N2*q7 + N3*q10
// w = N0*q2 + N1*q5 + N2*q8 + N3*q11
// as N0 + N1 + N2 + N3 = 1
// N3 = 1 -N0 -N1 -N2
// let a = N0,b = N1,c = N2
// N3 = 1-a-b-c
// substitute in
// u = a*(q0 - q9) + b*(q3 - q9) + c*(q6 - q9) + q9
// v = a*(q4 - q10) + b*(q4 - q10) + c*(q7 - q10) + q10
// w = a*(q5 - q11) + b*(q5 - q11) + c*(q8 - q11) + q11
// x = a*(x0 - x3) + b*(x1 - x3) + c*(x2 - x3) + x3
// y = a*(y0 - y3) + b*(y1 - y3) + c*(y2 - y3) + y3
// z = a*(z0 - z3) + b*(z1 - z3) + c*(z2 - z3) + z3
// dx/da = (x0 - x3)
// ect for all d(i in x,y,z)/d(j in a,b,c)
// finding for all du/d(i in x,y,z)
// could repeat for all u v and w but as symetric after one is found can work out others easy
// chain rule du/da = (du/dx * dx/da) + (du/dy * dy/da) + (du/dz * dz/da)
// chain rule du/db = (du/dx * dx/db) + (du/dy * dy/db) + (du/dz * dz/db)
// chain rule du/dc = (du/dx * dx/dc) + (du/dy * dy/dc) + (du/dz * dz/dc)
/*vec3[du/da [[dx/da, dy/da, dz/da] [du/dx
,du/db = [dx/db, dy/db, dz/db] * ,du/dy
,du/dc] [dx/dc, dy/dc, dz/dc] ,du/dz]*/
// known vec on left and values in matrix (A) so can work out right by premultiply by A^-1 both sides to get, vec on right = (A^-1)*vec on left
// let ijk (for i in x,y,z and for j,k in 0,1,2,3) be (vertex j pos direction i) - (vertex k pos direction i) e.g., x12 = vert1 x val - vert2 x val
// can input values obtained from functions of x and a,b,c by differentiating x with respect to a,b,c
/* [[x03,y03,z03]
A = [x13,y13,z13]
[x23,y23,z23]]*/
// find A^-1
/* [[y13*z23-y23*z13 , -(y03*z23-y23*z03), y03*z13-y13*z03 ]
A^-1 = (1/detA) * [-(x13*z23-x23*z13), x03*z23-x23*z03 , -(x03*z13-x13*z03)]
[x13*y23-x23*y13 , -(x03*y23-x23*y03), x03*y13-x13*y03 ]]*/
// volume = 1/6 * detA so we can ignore the 1/detA and hope it cancels latter
// get rid of -ve brackets
/* [[y13*z23-y23*z13 , z03*y23-z23*y03 , y03*z13-y13*z03 ]
A^-1 = (1/detA) * [z13*x23-z23*x13 , x03*z23-x23*z03 , z03*x13-z13*x03 ]
[x13*y23-x23*y13 , y03*x23-y23*x03 , x03*y13-x13*y03 ]]*/
// multiply A^-1 by left vec3 to find du/dx
// du/dx = (y13*z23-y23*z13)*du/da + (z03*y23-z23*y03)*du/db , (y03*z13-y13*z03)*du/dc
// substute in du/d(a b or c)
// du/dx = (y13*z23-y23*z13)*(q0 - q9) + (z03*y23-z23*y03)*(q3 - q9) , (y03*z13-y13*z03)*(q6 - q9)
// expand out first bracket in each term eg bracket one is
// ((y1-y3)*(z2-z3)-(y2-y3)*(z1-z3)) = ((y1*z2 - y1*z3 - y3*z2 + y3*z3) - (y2*z1 - y2*z3 - y3*z1 + y3*z3))
// prev line = (y1*z2 - y2*z1 + y2*z3 - y3*z2 + y3*z1 - y1*z3)
// this looks like a cross product of y and z then add up all terms but does not matter if getting a computer to do it as same result
// if you expand all first brackets you get
/* du/dx = 1/det(A) * ((y1*z2 - y2*z1 + y2*z3 - y3*z2 + y3*z1 - y1*z3)(q0 - q9)+
(y2*z0 - y0*z2 + y0*z3 - y3*z0 + y3*z2 - y2*z3)(q3 - q9)+
(y0*z1 - y1*z0 + y1*z3 - y3*z1 + y3*z0 - y0*z3)(q6 - q9))*/
// terms 3 and 4 in each line are the same but negitive of terms 5 and 6 in a different line so when multiplying out qs only first line remains
/* du/dx = 1/det(A) * ((y1*z2 - y2*z1 + y2*z3 - y3*z2 + y3*z1 - y1*z3)q0 +
(y2*z0 - y0*z2 + y0*z3 - y3*z0 + y3*z2 - y2*z3)q3 +
(y0*z1 - y1*z0 + y1*z3 - y3*z1 + y3*z0 - y0*z3)q6 -
(y1*z2 - y2*z1 + y2*z0 - y0*z2 + y0*z1 - y1*z0)q9) */
// get rid of negitve sign at end of line 3
/* du/dx = 1/det(A) * ((y1*z2 - y2*z1 + y2*z3 - y3*z2 + y3*z1 - y1*z3)q0 +
(y2*z0 - y0*z2 + y0*z3 - y3*z0 + y3*z2 - y2*z3)q3 +
(y0*z1 - y1*z0 + y1*z3 - y3*z1 + y3*z0 - y0*z3)q6 +
(y2*z1 - y1*z2 + y0*z2 - y2*z0 + y1*z0 - y0*z1)q9) */
// pattern to make easy to iterate just moving around terms
/* du/dx = 1/det(A) * (( y2*z3 - y3*z2 + y1*z2 - y2*z1 + y3*z1 - y1*z3)q0 +
(- y3*z0 + y0*z3 - y2*z3 + y3*z2 - y0*z2 + y2*z0)q3 +
( y0*z1 - y1*z0 + y3*z0 - y0*z3 + y1*z3 - y3*z1)q6 +
(- y1*z2 + y2*z1 - y0*z1 + y1*z0 - y2*z0 + y0*z2)q9) */
// bring out -1 from line 2 and 4
/* du/dx = 1/det(A) * ((y2*z3 - y3*z2 + y1*z2 - y2*z1 + y3*z1 - y1*z3)q0 -
(y3*z0 - y0*z3 + y2*z3 - y3*z2 + y0*z2 - y2*z0)q3 +
(y0*z1 - y1*z0 + y3*z0 - y0*z3 + y1*z3 - y3*z1)q6 -
(y1*z2 - y2*z1 + y0*z1 - y1*z0 + y2*z0 - y0*z2)q9) */
// now have du/dx in linear terms of q
// now need d(i in u,v,w)/d(j in x,y,z) except for combination du/dx
// could use same method but as it is symetric problem to get u,v,w just have to swap out qs for q(s+ 1 or 2)
// also as symetric problem to get x,y or z just have to swap out all ys and zs for xs (and either y or z which isnt being calculated)
// now got all d(i in u,v,w)/d(j in x,y,z)
// e = jacobian vec6[du/dx, dv/dy, dw/dz, du/dy + dv/dx, dv/dz + dw/dy, dw/dx + du/dz] = vec6[ex,ey,ez,gxy,gyz,gzx]
// there exists a 6x12 matrix such that e = Bq
// can just input coefficients
// we have a strain displacment matrix (however it is 6x12 and very messy coefficents so i will not write it out here as the program will assemble it)
// after this all the integration (put in quote marks) is very similar to how they found for triangle in 2d except there is no thickness t but as is constant could ignore already
// "potential energy due to external forces
// U = 1/2 * inetgral over V of ((transpose of strain matrix) * stress matrix dV)
// stress matrix = D*e where we calculated D and e is strain matrix
// U = 1/2 * inetgral over V of ((transpose of e) * D*e dV)
// e = Bq
// U = 1/2 * inetgral over V of ((transpose of B*q) * D*B*q dV)
// U = 1/2 * inetgral over V of ((transpose of q)*(transpose of B) * D*B*q dV)
// D is constant (for same material) and B is constant for the same tetrahedron so can take out of integral
// U = 1/2 *(transpose of q)*(transpose of B) * D*B * inetgral over V of ( dV)*q
// integral of V over dv is the volume
// U = 1/2 *(transpose of q)*(transpose of B) * D*B * Volume *q
// therefore stiffness matrix of tetrahedron k = V*((transpose of B)*D*B)
// so stiffness matrix of whole thing is sum of k for each element"
// detA must be computed i think
/* [[x03,y03,z03]
A = [x13,y13,z13]
[x23,y23,z23]]*/
// detA = x03*(y13*z23-z13*y23) +y03*(z13*x23-x13*z23) +z03*(x13*y23-y13*x23)
// detA = [x03,y03,z03].[ y13*z23-z13*y23, z13*x23-x13*z23, x13*y23-y13*x23]
// detA = v03.(v13 X v23) with dot and cross products (triple scaler product)
// actual program
elementStiffnessMatrix = new matNxN(12);
double D[6][6] = {{1-v,v,v,0,0,0}
,{v,1-v,v,0,0,0}
,{v,v,1-v,0,0,0}
,{0,0,0,(1-2*v)/2.0,0,0}
,{0,0,0,0,(1-2*v)/2.0,0}
,{0,0,0,0,0,(1-2*v)/2.0}};// constant for a material and space
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
D[i][j] *= E/((1+v)*(1-2*v));
}
}
//std::cout << "got D matrix\n";
//std::cout << E << "\n";
//std::cout << D[0][0] << "\n";
double B[6][12] = {};// = {} should mean dont need to initailise anymore
/*for (int i = 0; i < 6; i++){for (int j = 0; j < 12; j++){B[i][j] = 0;}}*/
double v0[3] = {verts[0][0]-verts[3][0],verts[0][1]-verts[3][1],verts[0][2]-verts[3][2]};
double v1[3] = {verts[1][0]-verts[3][0],verts[1][1]-verts[3][1],verts[1][2]-verts[3][2]};
double v2[3] = {verts[2][0]-verts[3][0],verts[2][1]-verts[3][1],verts[2][2]-verts[3][2]};
double detA = 0;
for (int i = 0; i < 3; i++){
detA += v0[i] * (v1[(i+1)%3]*v2[(i+2)%3] - v1[(i+2)%3]*v2[(i+1)%3]);
}
detA = fabs(detA);// dont know if got right way round so may be negitive
for (int i = 0; i < 3; i++){// for i in u,v,w
for (int j = 0; j < 3; j++){// for j in x,y,z
int row = 0;
int offset = i;
if (i == j){// du/dx, dv/dy, dw/dz
row = i;
}
else{
// if i + j == 1 (du/dy or dv/dx) row = 3
// if i + j == 2 (du/dz or dw/dx (cant be dv/dy as already checked i != j)) row = 5
// if i + j == 3 (dv/dz or dw/dy) row = 4
switch (i+j){
case 1:row = 3;break;
case 2:row = 5;break;
case 3:row = 4;break;
default:break;}
}
for (int k = 0; k < 4; k++){// for k in some q (every third with an offset)
int a = (j+1)%3; int b = (j+2)%3;
double val = verts[(2+k)%4][a]*verts[(3+k)%4][b] - verts[(3+k)%4][a]*verts[(2+k)%4][b]+
verts[(1+k)%4][a]*verts[(2+k)%4][b] - verts[(2+k)%4][a]*verts[(1+k)%4][b]+
verts[(3+k)%4][a]*verts[(1+k)%4][b] - verts[(1+k)%4][a]*verts[(3+k)%4][b];//(y2*z3 - y3*z2 + y1*z2 - y2*z1 + y3*z1 - y1*z3)
val *= (((k+1)%2)*2)-1;// times 1 if k is even; times -1 if k is odd
B[row][offset + 3*k] = val/detA;
}
}
}
//std::cout << "got B matrix\n";
//std::cout << B[0][0] << "\n";
// k = transpose(B)*D*B
double DB[6][12];
for (int i = 0; i < 6; i++){
for (int j = 0; j < 12; j++){
DB[i][j] = 0;
for (int k = 0; k < 6; k++){
DB[i][j] += D[i][k]*B[k][j];
}
}
}
//double elementK[12][12];
for (int i = 0; i < 12 ; i++){
for (int j = 0; j < 12; j++){
elementStiffnessMatrix->val[i][j] = 0;
for (int k = 0; k < 6; k++){
//k[i][j] += transpose(B[i][k])*DB[k][j]; // transpose reverses index order
elementStiffnessMatrix->val[i][j] += B[k][i]*DB[k][j];
}
}
}
for (int i = 0; i < 12; i++){
for (int j = 0; j < 12; j++){
//std::cout << elementStiffnessMatrix.val[i][j] << "\t";
}
//std::cout << "\n";
}
//std::cout << "got K matrix\n";
}
void FElement::calcElementStiffnessMatrixHex(){
// used for hexahedral elements
// D matrix is the same for all 3d solid elements so use same as the tetrahedral version
// http://nliebeaux.free.fr/ressources/introfem.pdf used to help find strain matrix
// now need to find strain matrix, assume all d(something)/d(something else) is not a derivitive but is a partial derivitive
// x,y,z are the positions in coorinates
// u,v,w are the changes in displacement from start in directions x,y,z
// e = jacobian vec6[du/dx, dv/dy, dw/dz, du/dy + dv/dx, dv/dz + dw/dy, dw/dx + du/dz] = vec6[ex,ey,ez,gxy,gyz,gzx]
// there are 3 degrees of freedom per vertex and 8 vertexs so 24 dof
// these are displacements of vertices from starting location of vertex defined as, q = vec24[q0,q1,q2,...,q10,q11]
// q(3i+j) = vertex i displacement in direction j (0 = x, 1 = y, 2 = z)
// there exists a matrix such that e = Bq
// must be able to define e at every point in the hexahedron so need a way to interpolate between vertices
// need shape functions which shape function is 1 for 1 vertex and 0 for all others
// then get new pos using transformed pos = sum for all shape functions(Nx(original pos))
// chosen function Ni(ξ,η,ζ) = 0.125 * (1 + ξi * ξ) (1 + ηi * η) (1 + ζi * ζ)
// each corner will have ξηζ values = plus or minus 1
// so each point has transformed pos ξηζ in range [-1,1]^3 where point i is position (ξi,ηi,ζi)
// B = [B0 B1 B2 ... B7] the strain matrix
// Bi = [dNi/dx, 0 , 0]
// [0 ,dNi/dy, 0]
// [0 , 0 ,dNi/dz]
// [dNi/dy,dNi/dx, 0]
// [0 ,dNi/dz,dNi/dy]
// [dNi/dz, 0 ,dNi/dx]
// dx/dξ = sum(0 to 7) (dNi/dξ * xi)
// can be used to find jacobian below
// [dx/dξ,dy/dξ,dz/dξ]
// let J = [dx/dη,dy/dη,dz/dζ]
// [dx/dζ,dy/dη,dz/dζ]
// jacobian *vector = this by standard matrix multiplication = final vector through chain rule
// [dNi/dx] [(dNi/dx)*(dx/dξ) + (dNi/dy)*(dy/dξ) + (dNi/dz)*(dz/dξ)] [dNi/dξ]
// J * [dNi/dy] = [(dNi/dx)*(dx/dη) + (dNi/dy)*(dy/dη) + (dNi/dz)*(dz/dη)] = [dNi/dη]
// [dNi/dz] [(dNi/dx)*(dx/dζ) + (dNi/dy)*(dy/dζ) + (dNi/dz)*(dz/dζ)] [dNi/dζ]
// premultiply by inverse of J
// [dNi/dx] [dNi/dξ]
// [dNi/dy] = [J]^-1 * [dNi/dη]
// [dNi/dz] [dNi/dζ]
// this gives vars needed to make strain matrix
// dNi/dξ = d(0.125 * (1 + ξi * ξ) (1 + ηi * η) (1 + ζi * ζ))/dξ = (0.125 * ξi * (1 + ηi * η) (1 + ζi * ζ))
// dNi/d(ξ,η,ζ) can be calculated at any point in element given (ξ,η,ζ)
// stiffness matrix = volume integral of ( transpose of (B(ξ,η,ζ)) * elastistity matrix * B(ξ,η,ζ))dv
// dv = dxdydz = |J| * dξdηdζ
// then change ranges to local coordinates (ξ,η,ζ) and take the definite triple integral in range [-1,1] for all of (ξ,η,ζ)
// definite integrals in the range [-1,1] can be solved using gaussian quadrature which turns it into the sum of values at specific points times by weights
// turns integral [-1,1] of (f(x))dx into sum (from 1 to n) of (f(point n)*(weight n))
// for linear polynomials use of 2 points (per integral which as 3d means 2^3 points) is recommended by the paper
// the points for 2point gaussian quatrature (using lagrange polynomials) is (1/root(3) and -1/root(3)) with weights (1 and 1)
// so for the 3d one points are x = +or- 1/root(3) y = +or- 1/root(3) z = +or- 1/root(3)
// all with weight 1 (as have a *0.125 term in shape function will end up with total weight 1)
// dNi/dξ at point(ξ0,η0,ζ0) = 0.125 * (ξi * ξ0) (1 + ηi * η0) (1 + ζi * ζ0)
elementStiffnessMatrix = new matNxN(24);
double D[6][6] = {{1-v,v,v,0,0,0}
,{v,1-v,v,0,0,0}
,{v,v,1-v,0,0,0}
,{0,0,0,(1-2*v)/2.0,0,0}
,{0,0,0,0,(1-2*v)/2.0,0}
,{0,0,0,0,0,(1-2*v)/2.0}};// constant for a material and space
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
D[i][j] *= E/((1+v)*(1-2*v));
}
}
const double points[2] = {-sqrt(1.0/3.0),sqrt(1.0/3.0)};
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
for (int k = 0; k < 2; k++){
double point[3] = {points[i],points[j],points[k]};// location of test point in transformed space
double B[24][6];
double dNiBYdj[8][3];// rate of change of shape function at point given
for (int l = 0; l < 8; l++){
int NPoint[3] = {2*(l/4)-1 , 2*((l/2) %2) -1, 2*(l%2) -1};// location of vertex in transformed space
dNiBYdj[l][0] = 0.125 * (NPoint[0] * point[0]) * (1 + NPoint[1] * point[1]) * (1 + NPoint[2] * point[2]);
dNiBYdj[l][1] = 0.125 * (1 + NPoint[0] * point[0]) * (NPoint[1] * point[1]) * (1 + NPoint[2] * point[2]);
dNiBYdj[l][2] = 0.125 * (1 + NPoint[0] * point[0]) * (1 + NPoint[1] * point[1]) * (NPoint[2] * point[2]);
}
glm::mat3x3 diBYdj;// dx/d(local space) the jacobi matrix
for (int l = 0; l < 3; l++){
for (int m = 0; m < 3; m++){
diBYdj[l][m] = 0;
for (int n = 0; n < 8; n++){
diBYdj[l][m] += (dNiBYdj[n][m]) * verts[n][l];
}
}
}
glm::mat3x3 invJacobi = glm::inverse(diBYdj);
double detJacobi = glm::determinant(diBYdj);
for (int l = 0; l < 8; l++){
glm::vec3 dNiBydGlobal = invJacobi * glm::vec3(dNiBYdj[l][0],dNiBYdj[l][1],dNiBYdj[l][2]);// dNi/dx and dNi/dy and dNi/dz
B[3*l + 0][0] = dNiBydGlobal[0];
B[3*l + 1][1] = dNiBydGlobal[1];
B[3*l + 2][2] = dNiBydGlobal[2];
B[3*l + 0][3] = dNiBydGlobal[1];
B[3*l + 0][5] = dNiBydGlobal[2];
B[3*l + 1][3] = dNiBydGlobal[0];
B[3*l + 1][4] = dNiBydGlobal[2];
B[3*l + 2][4] = dNiBydGlobal[1];
B[3*l + 2][5] = dNiBydGlobal[0];
B[3*l + 0][4] = 0;
B[3*l + 1][5] = 0;
B[3*l + 2][3] = 0;
B[3*l + 0][1] = 0;
B[3*l + 0][2] = 0;
B[3*l + 1][0] = 0;
B[3*l + 1][2] = 0;
B[3*l + 2][0] = 0;
B[3*l + 2][1] = 0;
//std::cout << dNiBydGlobal[0] << " " << dNiBydGlobal[1] << " " << dNiBydGlobal[2] << "\n";
}// assemble the B matrix
/*for (int l = 0; l < 6; l++){
for (int m = 0; m < 24; m++){
std::cout << std::round(B[m][l]*1000)/1000 << "\t";
}
std::cout << "\n";
}
std::cout << "\n";*/
// k = transpose(B)*D*B
double DB[6][24];
for (int l = 0; l < 6; l++){
for (int m = 0; m < 24; m++){
DB[l][m] = 0;
for (int n = 0; n < 6; n++){
DB[l][m] += D[l][n]*B[m][n];// B indexing was backward
}
//std::cout << std::round(DB[l][m]*1000)/1000 << "\t";
}
//std::cout << "\n";
}
//std::cout << "\n";
//double elementK[24][24];
for (int l = 0; l < 24 ; l++){
for (int m = 0; m < 24; m++){
double delta = 0;
for (int n = 0; n < 6; n++){
//k[l][m] += transpose(B[l][n])*DB[n][m]; // transpose reverses index order except reversing happens twice as data structure must also be reversed
delta += B[l][n]*DB[n][m];
}
elementStiffnessMatrix->val[l][m] += delta * fabs(detJacobi);// as dv = |J| * dξdηdζ need to times by det of the jacobi matrix
//std::cout << std::round(delta) << "\t";
}
//std::cout << "\n";
}
//std::cout << "\n";
//std::cout << detJacobi << "\n";
}
}
}
/*for (int l = 0; l < 24; l++){
for (int m = 0; m < 24; m++){
std::cout << std::round(elementStiffnessMatrix->val[m][l]*1000)/1000 << "\t";
}
std::cout << "\n";
}
std::cout << "\n";*/
}
std::vector<double> FElement::calcNodalSressFromDisplacement(std::vector<double> *globalDisplacements){
// how to find stress from https://quickfem.com/wp-content/ups/IFEM.Ch28.pdf
// stress = D (material matrix of element) * ε (strain field)
// ε = B (strain displacement matrix) * u (displacement known)
// so stress = DBu
// for tetrahedral nodes no guass points needed so I can just plug in values and get out displacements
// for hexahedral nodes it is a bit more complicated
// stress to be evaluated at each gauss point then extrapolated
// I assume that the displacment is assumed to be the same ??? or is it interpolated to find val at gauss point
// so evaluate BD at each gauss point (this was done in calculating the stiffness matrix but then discarded)
// then just times by displacement to get stress at the gauss point
// then extrapolate
switch (numNodes)
{
case 4:// tetrahedron
return calcNodalSressFromDisplacementTet(globalDisplacements);
break;
case 8:// hexahedron
return calcNodalSressFromDisplacementHex(globalDisplacements);
break;
default:
break;
}
}
std::vector<double> FElement::calcNodalSressFromDisplacementTet(std::vector<double> *globalDisplacements){
// get DB matrix then times by displacements to get stress
std::vector<double> stressValues;
double D[6][6] = {{1-v,v,v,0,0,0}
,{v,1-v,v,0,0,0}
,{v,v,1-v,0,0,0}
,{0,0,0,(1-2*v)/2.0,0,0}
,{0,0,0,0,(1-2*v)/2.0,0}
,{0,0,0,0,0,(1-2*v)/2.0}};// constant for a material and space
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
D[i][j] *= E/((1+v)*(1-2*v));
}
}
double B[6][12] = {};// = {} should mean dont need to initailise anymore
/*for (int i = 0; i < 6; i++){for (int j = 0; j < 12; j++){B[i][j] = 0;}}*/
double v0[3] = {verts[0][0]-verts[3][0],verts[0][1]-verts[3][1],verts[0][2]-verts[3][2]};
double v1[3] = {verts[1][0]-verts[3][0],verts[1][1]-verts[3][1],verts[1][2]-verts[3][2]};
double v2[3] = {verts[2][0]-verts[3][0],verts[2][1]-verts[3][1],verts[2][2]-verts[3][2]};
double detA = 0;
for (int i = 0; i < 3; i++){
detA += v0[i] * (v1[(i+1)%3]*v2[(i+2)%3] - v1[(i+2)%3]*v2[(i+1)%3]);
}
detA = fabs(detA);// dont know if got right way round so may be negitive
for (int i = 0; i < 3; i++){// for i in u,v,w
for (int j = 0; j < 3; j++){// for j in x,y,z
int row = 0;
int offset = i;
if (i == j){// du/dx, dv/dy, dw/dz
row = i;
}
else{
// if i + j == 1 (du/dy or dv/dx) row = 3
// if i + j == 2 (du/dz or dw/dx (cant be dv/dy as already checked i != j)) row = 5
// if i + j == 3 (dv/dz or dw/dy) row = 4
switch (i+j){
case 1:row = 3;break;
case 2:row = 5;break;
case 3:row = 4;break;
default:break;}
}
for (int k = 0; k < 4; k++){// for k in some q (every third with an offset)
int a = (j+1)%3; int b = (j+2)%3;
double val = verts[(2+k)%4][a]*verts[(3+k)%4][b] - verts[(3+k)%4][a]*verts[(2+k)%4][b]+
verts[(1+k)%4][a]*verts[(2+k)%4][b] - verts[(2+k)%4][a]*verts[(1+k)%4][b]+
verts[(3+k)%4][a]*verts[(1+k)%4][b] - verts[(1+k)%4][a]*verts[(3+k)%4][b];//(y2*z3 - y3*z2 + y1*z2 - y2*z1 + y3*z1 - y1*z3)
val *= (((k+1)%2)*2)-1;// times 1 if k is even; times -1 if k is odd
B[row][offset + 3*k] = val/detA;
}
}
}
//std::cout << "got B matrix\n";
// stress = D*B * u
double DB[6][12];
for (int i = 0; i < 6; i++){
for (int j = 0; j < 12; j++){
DB[i][j] = 0;
for (int k = 0; k < 6; k++){
DB[i][j] += D[i][k]*B[k][j];
}
}
}
for (int i = 0; i < 6; i++){
stressValues.push_back(0);
for (int j = 0; j < 12; j++){
stressValues[i] += DB[i][j]*(*globalDisplacements)[vertIndex[i/3]*3 + i%3];
}
}
for (int i = 0; i < 18; i++){
stressValues.push_back(stressValues[i%6]);// because stress is all the same in a linear tetrahedral element just copy result for other 3 vertices
}
return stressValues;
}
std::vector<double> FElement::calcNodalSressFromDisplacementHex(std::vector<double> *globalDisplacements){
// get DB matrix per gauss point
// then times each of them by displacements to get stress at guass points
// then extrapolate to get nodal stress
std::vector<double> stressValues;
std::vector<double> gaussStressValues;
double D[6][6] = {{1-v,v,v,0,0,0}
,{v,1-v,v,0,0,0}
,{v,v,1-v,0,0,0}
,{0,0,0,(1-2*v)/2.0,0,0}
,{0,0,0,0,(1-2*v)/2.0,0}
,{0,0,0,0,0,(1-2*v)/2.0}};// constant for a material and space
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
D[i][j] *= E/((1+v)*(1-2*v));
}
}
const double points[2] = {-sqrt(1.0/3.0),sqrt(1.0/3.0)};
const double invPoints[2] = {-sqrt(3.0),sqrt(3.0)};// the inverse of values in points
for (int i = 0; i < 8; i++){
double point[3] = {points[i/4],points[(i/2)%2],points[i%2]};// location of test point in transformed space
double B[24][6];
double dNiBYdj[8][3];// rate of change of shape function at point given
for (int l = 0; l < 8; l++){
int NPoint[3] = {2*(l/4)-1 , 2*((l/2) %2) -1, 2*(l%2) -1};// location of vertex in transformed space
dNiBYdj[l][0] = 0.125 * (NPoint[0] * point[0]) * (1 + NPoint[1] * point[1]) * (1 + NPoint[2] * point[2]);
dNiBYdj[l][1] = 0.125 * (1 + NPoint[0] * point[0]) * (NPoint[1] * point[1]) * (1 + NPoint[2] * point[2]);
dNiBYdj[l][2] = 0.125 * (1 + NPoint[0] * point[0]) * (1 + NPoint[1] * point[1]) * (NPoint[2] * point[2]);
}
glm::mat3x3 diBYdj;// dx/d(local space) the jacobi matrix
for (int l = 0; l < 3; l++){
for (int m = 0; m < 3; m++){
diBYdj[l][m] = 0;
for (int n = 0; n < 8; n++){
diBYdj[l][m] += (dNiBYdj[n][m]) * verts[n][l];
}
}
}
glm::mat3x3 invJacobi = glm::inverse(diBYdj);
double detJacobi = glm::determinant(diBYdj);
for (int l = 0; l < 8; l++){
glm::vec3 dNiBydGlobal = invJacobi * glm::vec3(dNiBYdj[l][0],dNiBYdj[l][1],dNiBYdj[l][2]);// dNi/dx and dNi/dy and dNi/dz
B[3*l + 0][0] = dNiBydGlobal[0];
B[3*l + 1][1] = dNiBydGlobal[1];
B[3*l + 2][2] = dNiBydGlobal[2];
B[3*l + 0][3] = dNiBydGlobal[1];
B[3*l + 0][5] = dNiBydGlobal[2];
B[3*l + 1][3] = dNiBydGlobal[0];
B[3*l + 1][4] = dNiBydGlobal[2];
B[3*l + 2][4] = dNiBydGlobal[1];
B[3*l + 2][5] = dNiBydGlobal[0];
B[3*l + 0][4] = 0;
B[3*l + 1][5] = 0;
B[3*l + 2][3] = 0;
B[3*l + 0][1] = 0;
B[3*l + 0][2] = 0;
B[3*l + 1][0] = 0;
B[3*l + 1][2] = 0;
B[3*l + 2][0] = 0;
B[3*l + 2][1] = 0;
//std::cout << dNiBydGlobal[0] << " " << dNiBydGlobal[1] << " " << dNiBydGlobal[2] << "\n";
}// assemble the B matrix
// k = transpose(B)*D*B
double DB[6][24];
for (int l = 0; l < 6; l++){
for (int m = 0; m < 24; m++){
DB[l][m] = 0;
for (int n = 0; n < 6; n++){
DB[l][m] += D[l][n]*B[m][n];
}
}
}
for (int j = 0; j < 6; j++){
gaussStressValues.push_back(0);
for (int k = 0; k < 24; k++){
gaussStressValues[j] += DB[j][k]*(*globalDisplacements)[vertIndex[k/3]*3 + k%3];
}
}
}
for (int i = 0; i < 48; i++){
stressValues.push_back(0);
}
for (int i = 0; i < 6; i++){// for each component
for (int j = 0; j < 8; j++){// for each node
for (int k = 0; k < 8; k++){// for each gauss point
// if i XORed j and k that would give me which bits are different so I could use that to check which invPoint to add
int jXORk = j ^ k; // ^ does XOR op in c++
stressValues[j*6+i] += 0.125*(1 + invPoints[(jXORk & 4) >> 2])*(1 + invPoints[(jXORk & 2) >> 1])*(1 + invPoints[(jXORk & 1)]) * gaussStressValues[k*6+i];
}
}
}
return stressValues;
}
FEStructure::FEStructure(Program* program, ModelAndForces &design){
p = program;
splitIntoElements(design);
}
FEStructure::~FEStructure(){
}
struct BigArray{
float space[128][128][128];
};
void FEStructure::splitIntoElements(ModelAndForces &design){
// for each cell a point must be found
// the points in all cells surrounding a corner which is inside the region make up an element
// if all corners in a cell are inside then the point is in the middle of the cell
nodes.clear();
BigArray* s = new BigArray();
for (int i = 0; i < 128; i++){
for (int j = 0; j < 128; j++){
for (int k = 0; k < 128; k++){
double x = i-64;
double y = j-64;
double z = k-64;
s->space[i][j][k] = design.model.getSDF(glm::vec3(x,y,z));
}
}
}
std::unordered_map<int, uint32_t> nodeInds;
uint32_t currentNode = 0;
for (int i = 0; i < 127; i++){
for (int j = 0; j < 127; j++){
for (int k = 0; k < 127; k++){
double x = i-64;
double y = j-64;
double z = k-64;
int key = (i)*(128*128) + (j)*(128) + (k);
int usedNodeX = ((s->space[i][j][k] <= 0.0) + (s->space[i][j][k+1] <= 0.0) + (s->space[i][j+1][k] <= 0.0) + (s->space[i][j+1][k+1] <= 0.0) +
(s->space[i+1][j][k] <= 0.0) + (s->space[i+1][j][k+1] <= 0.0) + (s->space[i+1][j+1][k] <= 0.0) + (s->space[i+1][j+1][k+1] <= 0.0));
if (usedNodeX == 0){// not used in the middle of nowhere not in the model
}
else if (usedNodeX == 8){// in the middle of the model not a surface node
nodes.push_back(x+0.5);
nodes.push_back(y+0.5);
nodes.push_back(z+0.5);
nodeInds[key] = currentNode;
currentNode++;
}
else{// a surface node
std::vector<glm::vec3> midpoints;
std::vector<glm::vec3> mpNormals;
for (int l = 0; l < 12; l++){// for each edge
int a = l/4;
int b = l%4;
glm::ivec3 v = glm::ivec3(0,b%2,(b/2)%2);
glm::ivec3 v1 = glm::ivec3(v[a],v[(2+a)%3],v[(1+a)%3]);
glm::ivec3 v2 = glm::ivec3(v1.x+(a == 0),v1.y+(a == 1),v1.z+(a == 2));
if ((s->space[i+ v1.x][j+ v1.y][k+ v1.z] <= 0.0) != (s->space[i+ v2.x][j+ v2.y][k+ v2.z] <= 0.0)){// if the end points dont match
//midpoints.push_back(glm::vec3(v1+v2)/glm::vec3(2.0));
midpoints.push_back((glm::vec3(v2)*glm::vec3(s->space[i+ v1.x][j+ v1.y][k+ v1.z]) -
glm::vec3(v1)*glm::vec3(s->space[i+ v2.x][j+ v2.y][k+ v2.z]))
/glm::vec3(s->space[i+ v1.x][j+ v1.y][k+ v1.z] - s->space[i+ v2.x][j+ v2.y][k+ v2.z]));
//std::cout << v1[0] << " " << v1[1] << " " << v1[2] << "\n";
//std::cout << v2[0] << " " << v2[1] << " " << v2[2] << "\n";
//std::cout << midpoints[midpoints.size()-1][0] << " " << midpoints[midpoints.size()-1][1] << " " << midpoints[midpoints.size()-1][2] << "\n";
//std::cout << s->space[i+ v1.x][j+ v1.y][k+ v1.z] << " " << s->space[i+ v2.x][j+ v2.y][k+ v2.z] << "\n";
//std::cout << design.getSDF(glm::vec3(x,y,z)+glm::vec3(v1+v2)/glm::vec3(2.0)) << "\n";
//std::cout << design.getSDF(glm::vec3(x,y,z)+midpoints[midpoints.size()-1]) << "\n\n";
mpNormals.push_back(design.model.getNormal(glm::vec3(x,y,z)+midpoints[midpoints.size()-1]));
//std::cout << midpoints[midpoints.size()-1][0] << " " << midpoints[midpoints.size()-1][1] << " " << midpoints[midpoints.size()-1][2] << "\n";
//std::cout << s->space[i+ v1.x][j+ v1.y][k+ v1.z] << " " << s->space[i+ v2.x][j+ v2.y][k+ v2.z] << "\n";
}
}
glm::vec3 result = glm::vec3(0.0);
if (midpoints.size() != 0){
for (int l = 0; l < midpoints.size(); l++){
result += midpoints[l];
}
result /= midpoints.size();
/*glm::vec3 step = glm::vec3(0.0);// not great at optimising so ignored for now
for (int l = 0; l < midpoints.size(); l++){
float dist = glm::dot(result,mpNormals[l])-glm::dot(midpoints[l],mpNormals[l]);
// assess how close to the plane which a line through the result and midpoint would give the correct normal
result -= glm::vec3(dist*0.7)*mpNormals[l];
}
*/float error = 0;// assess how good at matching normals it is
for (int l = 0; l < midpoints.size(); l++){
error += fabs(glm::dot(result,mpNormals[l])-glm::dot(midpoints[l],mpNormals[l]));
// assess how close to the plane which a line through the result and midpoint would give the correct normal
}
//std::cout << error << "\n";
}
nodes.push_back(x+result[0]);// only slightly optimised
nodes.push_back(y+result[1]);
nodes.push_back(z+result[2]);
surfaceNodes[currentNode] = static_cast<uint16_t>(surfaceNodes.size());
//std::cout << currentNode << "\n";
nodeInds[key] = currentNode;
currentNode++;
}
}
}
}
std::cout << currentNode << " num nodes\n";
std::cout << surfaceNodes.size() << " num surface nodes\n";
numNodes = nodes.size()/3;
for (int i = 1; i < 127; i++){
for (int j = 1; j < 127; j++){
for (int k = 1; k < 127; k++){
if (s->space[i][j][k] > 0.0){
continue;
}
int index[8];
for (int l = 0; l < 8; l++){
int key = (i+(l/4)-1)*(128*128) + (j+((l/2)%2)-1)*(128) + (k+(l%2)-1);
index[l] = nodeInds[key];
}
// the vector below is just to quickly change the order of vertices in an element if there was an error
const std::vector<int> elementNodes = {0,1,2,3,4,5,6,7};
double verts[8][3];
for (int l = 0; l < 8; l++){
for (int m = 0; m < 3; m++){
verts[l][m] = nodes[index[elementNodes[l]]*3 + m];
}
}
int inds[8];
for (int l = 0; l < 8; l++){
inds[l] = index[elementNodes[l]];
}
elements.push_back(new FElement(verts, 10000, 0.4, inds, 8));
}
}
}
delete s;
}
void FEStructure::getGlobalStiffnessMatrix(){
globalStiffnessMatrix = new double*[numNodes*3];
for (int i = 0; i < numNodes*3; i++){
globalStiffnessMatrix[i] = new double[numNodes*3];
}
for (int i = 0; i < numNodes*3; i++){
for (int j = 0; j < numNodes*3; j++){
globalStiffnessMatrix[i][j] = 0;
}
}
for (FElement* e: elements){
matNxN* elementStressMat = e->getElementStiffnessMatrix();
std::vector<uint32_t> indices = e->getVertIndices();
for (int i = 0; i < elementStressMat->size; i++){
for (int j = 0; j < elementStressMat->size; j++){
globalStiffnessMatrix[(indices[i/3])*3 + i%3][(indices[j/3])*3 + j%3] += elementStressMat->val[i][j];
}
}
}
}
void FEStructure::getSparseGlobalStiffnessMatrix(){// for when space becomes an issue
sparseGlobalStiffnessMatrix.valInds.clear();
sparseGlobalStiffnessMatrix.values.clear();
for (int i = 0; i < 3*numNodes; i++){
std::vector<int> newInds;
std::vector<double> newVals;
sparseGlobalStiffnessMatrix.valInds.push_back(newInds);
sparseGlobalStiffnessMatrix.values.push_back(newVals);
}
for (FElement* e: elements){
matNxN* elementStressMat = e->getElementStiffnessMatrix();
std::vector<uint32_t> indices = e->getVertIndices();
for (int i = 0; i < elementStressMat->size; i++){
for (int j = 0; j < elementStressMat->size; j++){
// because it is small (0-20) linear search is probably fast enough also means dont need to sort
int x = (indices[i/3])*3 + i%3;
int y = (indices[j/3])*3 + j%3;
int ind = -1;
for (int k = 0; k < sparseGlobalStiffnessMatrix.valInds[y].size(); k++){
if (sparseGlobalStiffnessMatrix.valInds[y][k] == x){
ind = k;
}
}
if (ind == -1){
sparseGlobalStiffnessMatrix.values[y].push_back(0);
sparseGlobalStiffnessMatrix.valInds[y].push_back(x);
ind = sparseGlobalStiffnessMatrix.valInds[y].size()-1;
}
sparseGlobalStiffnessMatrix.values[y][ind] += elementStressMat->val[i][j];
//globalStiffnessMatrix[(indices[i/3])*3 + i%3][(indices[j/3])*3 + j%3] += elementStressMat.val[i][j];
}
}
}
// the fact it is symetric makes removing nodes easier as know where things which need to be removed are
}
void FEStructure::makeSystemOfLinearEquations(std::vector<bool> dDefined, std::vector<double> forces, std::vector<double> displacements){
linearEquations.values.clear();
linearEquations.index.clear();
for (int i = 0; i < 3*numNodes; i++){
if (!dDefined[i]){// unknown displacement
std::vector<double> newRow;
double val = forces[i];
for (int j = 0; j < 3*numNodes; j++){
if (dDefined[j]){
val -= globalStiffnessMatrix[i][j]*displacements[j];
}
else{
newRow.push_back(globalStiffnessMatrix[i][j]);
}
}
newRow.push_back(val);
linearEquations.values.push_back(newRow);
//index.push_back(index.size());// for printing in order not used any more
linearEquations.index.push_back(i);
}
}
linearEquations.width = linearEquations.values[0].size();