Newer
Older
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
function FEM(data_FEM,ρ)
# FEM solver for 2D elements
# Author: JV Carstensen, CEE, MIT (JK Guest, Civil Eng, JHU)
# Revised: Oct 26 2017, JVC
# Revised: Amira Abdel-Rahman
# =======================================================================
# ---- READ IN DATA -----------------------------------------------------
# read in external file
E,f,dis,idb,ien,iplane,ndf,nel,nen,nnp,nsd,snu,t,xn,Ke0 = data_FEM;
g=zeros(size(dis))
g.=dis
# ---- NUMBER THE EQUATIONS ---------------------------------------------
id,neq = number_eq(idb,ndf,nnp);
# ---- FORM THE ELEMENT STIFFNESS MATRICES ------------------------------
nee = ndf*nen; # number of element equations
Ke = zeros(nee,nee,nel);
Imx_nsd = Matrix(1I, nsd, nsd);
zero_nsd = zeros(nsd,nsd);
for i = 1:nel
Ke[:,:,i] .= (ρ[i]).*Ke_quad4(iplane,E,snu,t,nee,nen,nsd,ndf,ien[:,i],xn,Imx_nsd,zero_nsd);
end
# ---- PERFORM GLOBAL TO LOCAL MAPPING ----------------------------------
LM = zeros(nee,nel);
for i = 1:nel
LM[:,i] = get_local_id(id,ien[:,i],ndf,nee,nen);
end
# ---- IF THERE IS FREE DEGREES OF FREEDOM, THEN SOLVE THE EQUILIBRIUM --
if (neq > 0)
# get global force vector (line 275)
F = globalF(f,g,id,ien,Ke,LM,ndf,nee,nel,nen,neq,nnp);
# solve Kd - F = 0 (line 521)
d,K = solveEQ(F,LM,Ke,nee,nel,neq);
end
# display(Ke)
de,Fe=post_processing(d,E,g,id,ien,Ke,ndf,nee,nel,nen,nnp)
return K,F,d,de,Fe
end
# =======================================================================
# =======================================================================
function add_d2dcomp(dcomp,d,id,ndf,nnp)
# function that adds the displacements of the free degrees of freedom to
# the nodal displacements
# -----------------------------------------------------------------------
# dcomp(ndf,nnp) = nodal displacements
# d(neq,1) = displacement at free degrees of freedom
# id(ndf,nnp) = equation numbers of degrees of freedom
# ndf = number of degrees of freedom per node
# nnp = number of nodal points
#------------------------------------------------------------------------
# loop over nodes and degrees of freedom
for n=1:nnp
for i=1:ndf
# if it is a free dof then add the global displacement
if (id[i,n]>0)
dcomp[i,n] = dcomp[i,n]+d[Int(id[i,n])];
end
end
end
return dcomp
end
# =======================================================================
# =======================================================================
function add_loads_to_force(F,f,id,ndf,nnp)
# function that adds nodal forces to the global force vector
# -----------------------------------------------------------------------
# F(neq,1) = global force vector
# f(ndf,nnp) = prescribed nodal forces
# id(ndf,nnp) = equation numbers of degrees of freedom
# ndf = number of degrees of freedom per node
# nnp = number of nodal points
#------------------------------------------------------------------------
# loop over nodes and degrees of freedom
for n = 1:nnp
for i = 1:ndf
# get the global equation number
M = Int(id[i,n]);
# if free degree of freedom, then add nodal load to global force
# vector
if (M > 0)
F[M] = F[M]+f[i,n];
end
end
end
return F
end
# =======================================================================
# =======================================================================
function addforce(F,Fe,LM,nee)
# function that adds element forces to the global force vector
# -----------------------------------------------------------------------
# F(neq,1) = global force vector
# Fe(nee,1) = element force vector
# LM(nee,nel) = global to local map for the element
# nee = number of element equations
# =======================================================================
# loop over rows of Fe
for i = 1:nee
# get the global equation number for local equation i
M = Int(LM[i]);
# if free dof (eqn number > 0) add to F vector
if (M > 0)
F[M]=F[M]+Fe[i];
end
end
return F
end
# =======================================================================
# =======================================================================
function addstiff(K,Ke,LM,nee)
# function that adds the element stiffness matrices to the global
# stiffness matrix
# -----------------------------------------------------------------------
# K(neq,neq) = global stiffness matrix
# Ke(nee,nee,1) = element stiffness matrix
# LM(nee,nel) = global to local map for the element
# nee = number of element equations
# =======================================================================
# loop over rows of Ke
for i = 1:nee
# loop over columns of Ke
for j = 1:nee
Mr = Int(LM[i]);
Mc = Int(LM[j]);
if (Mr > 0 && Mc > 0)
# if equation #'s are non-zero add element contribution to the
# stiffness matrix
K[Mr,Mc] = K[Mr,Mc]+Ke[i,j];
end
end
end
return K
end
# =======================================================================
# =======================================================================
function B_2d_elastic(dNdx,dNdy,nen,ndf)
# Computes the strain-displacement matrix for an elastic 2d problem
# -----------------------------------------------------------------------
# B(3,nen*ndf) = strain displacement matrix at current gauss point
# dNdx = derivative of shape function at current gauss point
# dNdy = derivative of shape function at current gauss point
# nen = number of nodes per element
# ndf = number of degrees of freedom per node
#
# =======================================================================
# Initialize
B = zeros(3,nen*ndf);
# Compute B
for i = 1:nen
B[:,(i-1)*ndf+1:ndf*i] = [ dNdx[i] 0
0 dNdy[i]
dNdy[i] dNdx[i] ];
end
# =======================================================================
return B
end
# =======================================================================
# =======================================================================
function D_2d(E,snu,iplane,nstr)
# Computes the elastic constitutive matrix for a 2d problem
# -----------------------------------------------------------------------
# E = Young's modulus
# snu = Poisson's ratio
# iplane = 1 - plane strain, 2 - plane stress
# nstr = number of independent stress components
#
# D(nstr,nstr) = elastic constitutive matrix for element e
#
# =======================================================================
# initialize and define D
D = zeros(Int(nstr),Int(nstr));
if (iplane == 2) # plane stress
coeff = E/(1-(snu*snu));
D[1,1] = coeff;
D[2,2] = D[1,1];
D[1,2] = coeff*snu;
D[2,1] = D[1,2];
D[3,3] = coeff*(1-snu)/2;
elseif (iplane == 1) # plane strain
coeff = E/((1+snu)*(1-2*snu));
D[1,1] = coeff*(1-snu);
D[2,2] = D[1,1];
D[1,2] = coeff*snu;
D[2,1] = D[1,2];
D[3,3] = coeff*(1-2*snu)/2;
end
return D
end
# =======================================================================
# =======================================================================
function get_de_from_dcomp(dcomp,ien,ndf,nen)
# extracts element displacement vector from complete displacement vector
#------------------------------------------------------------------------
# dcomp(ndf,nnp) = nodal displacements
# ien(nen,1) = element connectivity
# ndf = number of degrees of freedom per node
# nen = number of element equations
#
# de(nen,1) = element displacements
#
#------------------------------------------------------------------------
de = zeros((nen-1)*ndf+ndf,1);
# loop over number of element nodes
for i = 1:nen
# loop over number of degrees of freedom per node
for j = 1:ndf
# get the local element number and place displacement in de
leq = (i-1)*ndf+j;
de[leq] = dcomp[j,ien[i]];
end
end
return de
end
# =======================================================================
# =======================================================================
function gausspoints_quad4(ndf,nsd)
# function that defines integration points and weigths for quad4 elements
#------------------------------------------------------------------------
# ndf = number of degrees of freedom per node
# nsd = number of spacial dimensions
#
# Nint = number of gauss points in x and y
# point = location of gauss points in x and y
# weight = weigthing of gauss points for numerical integration
# nstre = number of independent stress components
# nstr1 = total number of stress components
# ngp = number of gauss points per element
#
#------------------------------------------------------------------------
Nint=zeros(Int,2)
Nint[1] = 2; # number of gauss points in x
Nint[2] = 2; # number of gauss points in y
# total number of gauss points
ngp = Nint[1]*Nint[2];
# Initialize
point = zeros(Nint[2],Nint[1]);
weight = zeros(Nint[2],Nint[1]);
weight .= point;
# get gauss point locations and weights for integration
point[1,1] = (-0.577350269189626);
point[1,2] = (-0.577350269189626);
point[2,1] = ( 0.577350269189626);
point[2,2] = ( 0.577350269189626);
weight .= weight .+1;
# define number of individual stress/strain components
nstre = nsd*(ndf+1)/2;
# define number of stress components in each element (4 in 2d)
nstr1 = 4;
return Nint,point,weight,nstre,nstr1,ngp
end
# =======================================================================
# =======================================================================
function get_local_id(id,ien,ndf,nee,nen)
# functions that performs the global to local mapping of equation numbers
# -----------------------------------------------------------------------
# id(ndf,nnp) = equation numbers of degrees of freedom
# ien(nen,1) = element connectivity
# ndf = number of degrees of freedom per node
# nee = number of element equations
# nen = number of element equations
#
# LM(nee,1) = global to local map for element
# =======================================================================
# initialize global-local mapping matrix
LM = zeros(nee,1);
# initialize local equation number counter
k = 0;
# loop over element nodes
for i = 1:nen
# loop over degrees of freedom at each node
for j = 1:ndf
# update counter and prescribe global equation number
k = k+1;
LM[k] = id[j,ien[i]];
end
end
return LM
end
# =======================================================================
# =======================================================================
function globalF(f,g,id,ien,Ke,LM,ndf,nee,nel,nen,neq,nnp)
# function that assembles the global load vector
# -----------------------------------------------------------------------
# id(ndf,nnp) = equation numbers of degrees of freedom
# f(ndf,nnp) = prescribed nodal forces
# g(ndf,nnp) = prescribed nodal displacements
# ien(nen,nel) = element connectivities
# Ke(nee,nee,nel) = element stiffness matrices
# ndf = number of degrees of freedom per node
# nee = number of element equations
# nel = number of elements
# nen = number of element equations
# neq = number of equations
# nnp = number of nodal points
#
# F(neq,1) = global force vector
# =======================================================================
# initialize
F = zeros(neq,1);
# Insert applied loads into F
F = add_loads_to_force(F,f,id,ndf,nnp);
# Compute forces from applied displacements (ds~=0) and insert into F
Fse = zeros(nee,nel);
# loop over elements
for i = 1:nel
# get dse for current element
dse = get_de_from_dcomp(g,ien[:,i],ndf,nen);
# compute element force
Fse[:,i] = -Ke[:,:,i]*dse;
# assemble elem force into global force vector
F = addforce(F,Fse[:,i],LM[:,i],nee);
end
return F
end
# =======================================================================
# =======================================================================
function jacobian_2d(dNdr,dNds,xn,nen,Imx,Jaco)
# computes the Jacobian, its determinate and inverse at current gauss
# point in element e
# -----------------------------------------------------------------------
# dNdr = derivative of shape function at current gauss point
# dNds = derivative of shape function at current gauss point
# xn(nsd,nen) = nodal coordinates for element e
# nen = number of nodes per element
# Imx = eye(nsd)
# Jaco = zeros(nsd)
#
# Jaco = Jacobian matrix for current gauss point
# detJ = determinant of the Jacobian for current gauss point
# InvJ = inverse of the Jacobian matrix for current gauss point
# =======================================================================
# Add to Jacobian
for i = 1:nen
#for j = 1:nsd
Jaco[1,1] = Jaco[1,1]+dNdr[i]*xn[1,i];
Jaco[1,2] = Jaco[1,2]+dNdr[i]*xn[2,i];
Jaco[2,1] = Jaco[2,1]+dNds[i]*xn[1,i];
Jaco[2,2] = Jaco[2,2]+dNds[i]*xn[2,i];
#end
end
# Find its determinant
detJ = det(Jaco);
# Find its inverse
invJ = Imx/Jaco;
return Jaco,detJ,invJ
end
# =======================================================================
# =======================================================================
function ke_elem_quad4(Nint,point,weight,ien, xn,ndf,nen,nee,t,D,Imx_nsd,zero_nsd)
# loops over element gauss points and computes element stiffness matrix
# -----------------------------------------------------------------------
# Nint = number of gauss points in x and y
# point = location of gauss points
# weight = weigthing of gauss points in numerical integration
# ien(nen,nel) = element connectivity
# xn(nsd,nnp) = nodal coordinates
# nen = number of nodes per element
# nee = number of element equations
# t = element thickness
# D = elastic constititive matrix for the element
# Imx = eye(nsd)
# Jaco = zeros(nsd)
#
# kee(nee,nee,1) = element stiffness matrix for quad 4 element
# =======================================================================
# =======================================================================
# Initialize
kee = zeros(nee,nee);
coeff = 0;
for i = 1:Nint[1]
ri = point[i,1];
wi = weight[i,1];
for j = 1:Nint[2]
sj = point[j,2];
wj = weight[j,2];
sN,dNdx,dNdy,detJ,invJ = shape_quad4(ri,sj,xn,ien,nen,Imx_nsd,zero_nsd);
B = B_2d_elastic(dNdx,dNdy,nen,ndf);
coeff = t*wi*wj*detJ;
# Compute kee = kee + transpose(B)*D*B*coeff
kee .= kee .+(B')*D*B*coeff;
end
end
return kee
end
# =======================================================================
# =======================================================================
function Ke_quad4(iplane,E,snu,t,nee,nen,nsd,ndf,ien,xn, Imx_nsd,zero_nsd)
# computes element stiffness matrix
# -----------------------------------------------------------------------
# iplane = 1 - plane strain, 2 - plane stress
# E = Young's modulus
# snu = Poisson's ratio
# t = element thickness
# nee = number of element equations
# nen = number of nodes per element
# nsd = number of spacial dimmensions
# ndf = number of degrees of freedom
# weight = weigthing of gauss points in numerical integration
# ien(nen,nel) = element connectivity
# xn(nsd,nnp) = nodal coordinates
# Imx = eye(nsd)
# Jaco = zeros(nsd)
#
# kee(nee,nee,1) = element stiffness matrix for quad 4 element
# =======================================================================
# Define location and weights of integration points
Nint,point,weight,nstre,nstr1,ngp = gausspoints_quad4(ndf,nsd);
# get constitutive matrix
nstr = nsd*(nsd+1)/2;
D = D_2d(E,snu,iplane,nstr);
# Compute element matrix
kee = ke_elem_quad4(Nint,point,weight,ien,xn,ndf,nen,nee,t,D,Imx_nsd,zero_nsd);
return kee
end
# =======================================================================
# =======================================================================
function number_eq(idb,ndf,nnp)
# function that numbers the unknown degrees of freedom (equations)
# -----------------------------------------------------------------------
# idb(ndf,nnp) = 1 if the degree of freedom is prescribed, 0 otherwise
# ndf = number of degrees of freedom per node
# nnp = number of nodal points
#
# id(ndf,nnp) = equation numbers of degrees of freedom
# neq = number of equations (tot number of degrees of freedom)
# =======================================================================
# initialize id and neq
id = zeros(ndf,nnp);
neq = 0;
# loop over nodes
for n = 1:nnp
# loop over degrees of freedom
for i = 1:ndf
if idb[i,n] == 0
# udate # of equations
neq = neq + 1;
# if no prescribed displacement at dof i of node n
# => give an equation # different from 0
id[i,n] = neq;
end
end
end
return id,neq
end
# =======================================================================
# =======================================================================
function solveEQ(F,LM,Ke,nee,nel,neq)
# function that solves the equilibrium condition
# -----------------------------------------------------------------------
# F(neq,1) = global force vector
# LM(nee,nel) = global to local maps
# Ke(nee,nee,nel) = element stiffness matrices
# nee = number of element equations
# nel = number of elements
# neq = number of equations
#
# d(neq,1) = displacements at free degrees of freedom
# =======================================================================
# assemble global stiffness matrix
# K = zeros(neq,neq); # Use 'sparse' for more efficient memory usage
K=spzeros(neq,neq)
for i = 1:nel
K = addstiff(K,Ke[:,:,i],LM[:,i],nee);
end
# solve the equlibrium
d = K\F;
return d,K
end
# =======================================================================
# =======================================================================
function shape_quad4(r,s,xn,ien,nen, Imx_nsd,zero_nsd)
# computes the shape functions for quad4 element e at gauss point (r,s)
# -----------------------------------------------------------------------
# r = gauss point coordinate in parent domain
# s = gauss point coordinate in parent domain
# xn(nsd,nnp) = nodal coordinates
# ien(nen,1) = element connectivity for element e
# nen = number of nodes per element
# Imx = eye(nsd)
# Jaco = zeros(nsd)
#
# sN(nen,1) = shape functions at current gauss point
# dNdx(nen,1) = shape function derivatives at current gauss point in
# (x,y)
# dNdy(nen,1) = shape function derivatives at current gauss point in
# (x,y)
# detJ = determinant of the Jacobian for current gauss point
# InvJ = inverse of the Jacobian matrix for current gauss point
#
# =======================================================================
# Initialize
dNdx = zeros(nen,1);
dNdy = zeros(nen,1);
dNdy .= dNdx;
# compute shape functions at(r,s)
sN,dNdr,dNds = shapefunct_quad4(r,s);
# compute Jacobian, its deterninant and inverse
Jaco,detJ,invJ = jacobian_2d(dNdr,dNds,xn[:,ien],nen,Imx_nsd,zero_nsd);
# compute the derivatives of the shape functions
for i = 1:nen
dNdx[i] = invJ[1,1]*dNdr[i]+invJ[1,2]*dNds[i];
dNdy[i] = invJ[2,1]*dNdr[i]+invJ[2,2]*dNds[i];
end
return sN,dNdx,dNdy,detJ,invJ
end
# =======================================================================
# =======================================================================
function shapefunct_quad4(r,s)
# evaluates the shape functions and their derivatives at point (s,r)
# -----------------------------------------------------------------------
# r = gauss point coordinate in parent domain
# s = gauss point coordinate in parent domain
#
# sN(nen,1) = shape functions at current gauss point
# dNdr(nen,1) = shape function derivatives at current gauss point in
# (r,s)
# dNds(nen,1) = shape function derivatives at current gauss point in
# (r,s)
#
# =======================================================================
# Initialize
sN = zeros(4,1);
dNdr = zeros(4,1);
dNds = zeros(4,1);
dNdr .= sN;
dNds .= sN;
# Shape functions
sN[1] = (1-r)*(1-s);
sN[2] = (1+r)*(1-s);
sN[3] = (1+r)*(1+s);
sN[4] = (1-r)*(1+s);
sN .= 0.25*sN;
# Derivatives
dNdr[1] = -(1-s);
dNdr[2] = (1-s);
dNdr[3] = (1+s);
dNdr[4] = -(1+s);
dNdr .= 0.25*dNdr;
dNds[1] = -(1-r);
dNds[2] = -(1+r);
dNds[3] = (1+r);
dNds[4] = (1-r);
dNds .= 0.25*dNds;
return sN,dNdr,dNds
end
# =======================================================================
# =======================================================================
function post_processing(d,E,g,id,ien,Ke,ndf,nee,nel,nen,nnp)
# function that performs post processing for truss elements
# -----------------------------------------------------------------------
# A(nel,1) = cross-sectional area of elements
# d(neq,1) = displacements at free degrees of freedom
# E(nel,1) = Young's modulus of elements
# g(ndf,nnp) = prescribed nodal displacements
# id(ndf,nnp) = equation numbers of degrees of freedom
# ien(nen,nel) = element connectivities
# Ke(nee,nee,nel) = element stiffness matrices
# ndf = number of degrees of freedom per node
# nee = number of element equations
# nel = number of elements
# nen = number of element equations
# nnp = number of nodes
# Te(nee,nee,nel) = element transformation matrices
#
# dcomp(ndf,nnp) = nodal displacements
# axial(nel,1) = axial element forces
# stress(nel,1) = element stresses
# strain(nel,1) = element strains
# Fe(nee,nel) = element forces
# =======================================================================
# get the total displacement of the structure in matrix form dcomp(nsd,nnp)
dcomp = add_d2dcomp(g,d,id,ndf,nnp);
# initalize evaluation of global element forces Fe, local element forces
# fe, axial forces, element stresses and strains
Fe = zeros(nee,nel);
de = zeros(nee,nel);
fe = zeros(1*nen,nel); # element local force vector
axial = zeros(nel,1);
stress = zeros(nel,1);
strain = zeros(nel,1); # element axial, stress, strain
# loop over elements
for i=1:nel
# get the element displacaments
de[:,i] = get_de_from_dcomp(dcomp,ien[:,i],ndf,nen);
# compute the element forces
Fe[:,i] = Ke[:,:,i]*de[:,i];
end
# display("Fe")
# display(Fe)
return de,Fe
end