-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfem.um
170 lines (128 loc) · 5.16 KB
/
fem.um
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
// 2D finite element method solver
import (
"th.um"
"matrix.um"
)
type (
Material* = struct {
young, poisson, yield, thickness: real
}
Element* = struct {
tri: [3]int
geometry, elasticity, stiffness: matrix::Matrix
}
)
fn (mat: ^Material) isValid*(): bool {
return mat.young > 0 && mat.poisson > 0 && mat.poisson < 0.5 && mat.thickness > 0
}
fn createElement*(pts: []th::Vf2, tri: [3]int, material: Material): Element {
el := Element{tri: tri}
a := pts[tri[1]].sub(pts[tri[0]])
b := pts[tri[2]].sub(pts[tri[1]])
c := pts[tri[0]].sub(pts[tri[2]])
det := b.x * c.y - c.x * b.y
area := 0.5 * fabs(det)
el.geometry = matrix::Matrix{
{-b.y, 0, -c.y, 0, -a.y, 0},
{ 0, b.x, 0, c.x, 0, a.x},
{ b.x, -b.y, c.x, -c.y, a.x, -a.y}
}.mulf(1.0 / det)
el.elasticity = matrix::Matrix{
{ 1, material.poisson, 0},
{material.poisson, 1, 0},
{ 0, 0, (1 - material.poisson) / 2}
}.mulf(material.young / (1 - material.poisson * material.poisson))
el.stiffness = el.geometry.transpose().mul(el.elasticity).mul(el.geometry).mulf(material.thickness * area)
return el
}
fn (el: ^Element) convertDofToGlobal(dof: int): int {
return 2 * el.tri[dof / 2] + (dof % 2)
}
fn (el: ^Element) getEquivalentStress(displacements: []th::Vf2): real {
dofDisplacements := matrix::Matrix{
{displacements[el.tri[0]].x},
{displacements[el.tri[0]].y},
{displacements[el.tri[1]].x},
{displacements[el.tri[1]].y},
{displacements[el.tri[2]].x},
{displacements[el.tri[2]].y}
}
stresses := el.elasticity.mul(el.geometry).mul(dofDisplacements) // {X normal stress, Y normal stress, shear stress}
sqr := fn (x: real): real {return x * x}
// Von Mises stress
return sqrt(sqr(stresses[0][0]) - stresses[0][0] * stresses[1][0] + sqr(stresses[1][0]) + 3 * sqr(stresses[2][0]))
}
fn getGlobalStiffness(els: []Element, numPts: int): matrix::Matrix {
stiffness := matrix::zeros(2 * numPts, 2 * numPts)
for _, el in els {
for row in el.stiffness {
globalRow := el.convertDofToGlobal(row)
for col in el.stiffness[0] {
globalCol := el.convertDofToGlobal(col)
stiffness[globalRow][globalCol] += el.stiffness[row][col]
}
}
}
return stiffness
}
fn getDofForces(forces: map[int]th::Vf2, numPts: int): matrix::Matrix {
// Flatten force vectors into a single column vector
dofForces := matrix::zeros(2 * numPts, 1)
for pt, force in forces {
if force.mag() == 0 {continue}
dofForces[2 * pt][0] = force.x
dofForces[2 * pt + 1][0] = force.y
}
return dofForces
}
fn getDofConstraints(constraints: map[int]bool): map[int]bool {
// Each point constraint constrains both X and Y displacements
dofConstraints := map[int]bool{}
for pt, exists in constraints {
if !exists {continue}
dofConstraints[2 * pt] = true
dofConstraints[2 * pt + 1] = true
}
return dofConstraints
}
fn getPtDisplacements(unconstrainedDofDisplacements: matrix::Matrix, dofConstraints: map[int]bool, numPts: int): []th::Vf2 {
displacements := make([]th::Vf2, numPts)
dof := 0
for pt, displacement^ in displacements {
if dofConstraints[2 * pt] {continue}
displacement.x = unconstrainedDofDisplacements[dof][0]
displacement.y = unconstrainedDofDisplacements[dof + 1][0]
dof += 2
}
return displacements
}
fn getEquivalentStresses(els: []Element, displacements: []th::Vf2): []real {
stresses := make([]real, len(els))
for i, el in els {
stresses[i] = el.getEquivalentStress(displacements)
}
return stresses
}
fn solve*(pts: []th::Vf2, tris: [][3]int, forces: map[int]th::Vf2, constraints: map[int]bool, material: Material): ([]th::Vf2, []real) {
// The DOFs are point displacements: {displacements[0].x, displacements[0].y, displacements[1].x, displacements[1].y, ...}
// Create finite elements
els := make([]Element, len(tris))
for i in tris {
els[i] = createElement(pts, tris[i], material)
}
// Get global stiffness matrix
stiffness := getGlobalStiffness(els, len(pts))
// Get DOF-wise forces and constraints
dofForces := getDofForces(forces, len(pts))
dofConstraints := getDofConstraints(constraints)
// Remove constrained DOFs
unconstrainedStiffness := stiffness.remove(dofConstraints, dofConstraints)
unconstrainedDofForces := dofForces.remove(dofConstraints, {})
// Solve the equation: unconstrainedStiffness * unconstrainedDofDisplacements = unconstrainedDofForces
unconstrainedDofDisplacements := unconstrainedStiffness.solve(unconstrainedDofForces)
// Get point displacements from DOF displacements
displacements := getPtDisplacements(unconstrainedDofDisplacements, dofConstraints, len(pts))
// Get equivalent stresses for elements
stresses := getEquivalentStresses(els, displacements)
return displacements, stresses
}