-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembly.py
336 lines (272 loc) · 9.57 KB
/
assembly.py
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
"""
Created by Patrik Rác on the 19.04.2023
Assembly file that contains code for the assembly of the matrices and the right-hand side
"""
import numpy as np
from scipy.sparse import coo_matrix, csr_matrix
from utilities import Boundary
import time
def Mloc(vtx, e):
"""
Compute the local mass matrix
:param vtx: Vertex array
:param e: Current element
:return: M_loc: Local mass matrix
"""
n = len(e)
if n == 2:
# Get the vertices of the current element
v1 = vtx[e[0]]
v2 = vtx[e[1]]
# Compute the length of the edge
L = np.sqrt((v1[0] - v2[0]) ** 2 + (v1[1] - v2[1]) ** 2)
# Compute the local mass matrix
M = L / 6. * np.array([[2, 1],
[1, 2]])
elif n == 3:
# Get the vertices of the current element
v1 = vtx[e[0]]
v2 = vtx[e[1]]
v3 = vtx[e[2]]
# Compute the area of the triangle
A = np.abs((v1[0] - v3[0]) * (v2[1] - v1[1]) - (v1[0] - v2[0]) * (v3[1] - v1[1])) / 2.
# Compute the local mass matrix
M = A / 12. * np.array([[2, 1, 1],
[1, 2, 1],
[1, 1, 2]])
else:
print("Error: element not supported")
return None
return M
def Mass(vtx, elt, bnd_vtx=None) -> coo_matrix:
"""
Compute the global mass matrix
:param vtx: Vertex array
:param elt: Element array
:param bnd_vtx: Set of boundary vertices
:return: coo_matrix: Global mass matrix
"""
if bnd_vtx is None:
bnd_vtx = set()
# Compute the global mass matrix
nbr_vtx = len(vtx)
nbr_elt = len(elt)
d = len(elt[0])
V = np.zeros((d, d, nbr_elt))
I = np.zeros((d, d, nbr_elt))
J = np.zeros((d, d, nbr_elt))
for i, e in enumerate(elt):
# Compute the local mass matrix
M_loc = Mloc(vtx, e)
# Add the local mass matrix to the global mass matrix
for j in range(d):
for k in range(d):
# Check if the current vertex is not on the boundary
if (e[j] not in bnd_vtx) and (e[k] not in bnd_vtx):
I[j, k, i] = e[j]
J[j, k, i] = e[k]
V[j, k, i] = M_loc[j, k]
elif e[j] == e[k]:
I[j, k, i] = e[j]
J[j, k, i] = e[k]
V[j, k, i] = 1.
return coo_matrix((V.flat, (I.flat, J.flat)), shape=(nbr_vtx, nbr_vtx))
def Kloc(vtx, e):
"""
Compute the local stiffness matrix
:param vtx: Vertex array
:param e: Current element
:return: Local stiffness matrix
"""
# Get the vertices of the current element
v1 = vtx[e[0]]
v2 = vtx[e[1]]
v3 = vtx[e[2]]
# Compute the area of the triangle
A = np.abs((v1[0] - v3[0]) * (v2[1] - v1[1]) - (v1[0] - v2[0]) * (v3[1] - v1[1])) / 2.
# Compute the local stiffness matrix
K = np.zeros((3, 3))
for i in range(3):
for j in range(3):
K[i, j] = np.dot(vtx[e[(i + 1) % 3]] - vtx[e[(i + 2) % 3]], vtx[e[(j + 1) % 3]] - vtx[e[(j + 2) % 3]])
return K / (4. * A)
def Rig(vtx, elt, bnd_vtx=None) -> coo_matrix:
"""
Compute the global stiffness matrix
:param vtx: Vertex array
:param elt: Element array
:param bnd_vtx: Set of boundary vertices
:return: Global stiffness matrix
"""
if bnd_vtx is None:
bnd_vtx = set()
# Compute the global stiffness matrix
nbr_vtx = len(vtx)
nbr_elt = len(elt)
d = len(elt[0])
V = np.zeros((d, d, nbr_elt))
I = np.zeros((d, d, nbr_elt))
J = np.zeros((d, d, nbr_elt))
for i, e in enumerate(elt):
K_loc = Kloc(vtx, e)
for j in range(d):
for k in range(d):
# Check if the current vertex is not on the boundary
if (e[j] not in bnd_vtx) and (e[k] not in bnd_vtx):
I[j, k, i] = e[j]
J[j, k, i] = e[k]
V[j, k, i] = K_loc[j, k]
elif e[j] == e[k]:
I[j, k, i] = e[j]
J[j, k, i] = e[k]
V[j, k, i] = 1.
return coo_matrix((V.flat, (I.flat, J.flat)), shape=(nbr_vtx, nbr_vtx))
def Cloc(vtx, e, b=np.array([1, 1])):
"""
Compute the local center matrix
:param vtx: Vertex array
:param e: Current element
:param b: Vector b
:return: Local center matrix
"""
# Get the vertices of the current element
v1 = vtx[e[0]]
v2 = vtx[e[1]]
v3 = vtx[e[2]]
# Compute the area of the triangle
A = np.abs((v1[0] - v3[0]) * (v2[1] - v1[1]) - (v1[0] - v2[0]) * (v3[1] - v1[1])) / 2.
# Compute the local stiffness matrix
C = np.zeros((3, 3))
for k in range(3):
# Compute the associate normal vector
nk = np.cross(np.array([0, 0, 1]), vtx[e[(k + 1) % 3]] - vtx[e[(k + 2) % 3]])[:2]
nk = nk / np.linalg.norm(nk)
C[:, k] = np.dot(b, nk) / np.dot((vtx[e[k]] - vtx[e[(k + 1) % 3]]), nk)
return A / 3. * C
def Cmat(vtx, elt, bnd_vtx=None, b=np.array([1, 1])) -> coo_matrix:
"""
Compute the global center matrix
:param vtx: Vertex array
:param elt: Element array
:param bnd_vtx: Set of boundary vertices
:param b: Vector b
:return: Global center matrix
"""
if bnd_vtx is None:
bnd_vtx = set()
# Compute the global divergence matrix
nbr_vtx = len(vtx)
nbr_elt = len(elt)
d = len(elt[0])
V = np.zeros((d, d, nbr_elt))
I = np.zeros((d, d, nbr_elt))
J = np.zeros((d, d, nbr_elt))
for i, e in enumerate(elt):
C_loc = Cloc(vtx, e, b)
for j in range(d):
for k in range(d):
# Check if the current vertex is on the boundary
if (e[j] not in bnd_vtx) and (e[k] not in bnd_vtx):
I[j, k, i] = e[j]
J[j, k, i] = e[k]
V[j, k, i] = C_loc[j, k]
elif e[j] == e[k]:
I[j, k, i] = e[j]
J[j, k, i] = e[k]
V[j, k, i] = 1.
return coo_matrix((V.flat, (I.flat, J.flat)), shape=(nbr_vtx, nbr_vtx))
def Floc(vtx, e, f):
# Get the vertices of the current element
v1 = vtx[e[0]]
v2 = vtx[e[1]]
v3 = vtx[e[2]]
# Compute the area of the triangle
A = np.abs((v1[0] - v3[0]) * (v2[1] - v1[1]) - (v1[0] - v2[0]) * (v3[1] - v1[1])) / 2.
F_loc = np.zeros(3)
for i in range(3):
ni = np.cross(np.array([0, 0, 1]), vtx[e[(i + 1) % 3]] - vtx[e[(i + 2) % 3]])[:2]
def g(x): return f(x) * np.dot(x - vtx[e[(i + 1) % 3]], ni) / np.dot(vtx[e[i]] - vtx[e[(i + 1) % 3]], ni)
F_loc[i] = g((v1 + v2) / 2.) + g((v2 + v3) / 2.) + g((v3 + v1) / 2.)
# Compute the local stiffness matrix
return A / 3. * F_loc
def assemble_rhs(vtx, elt, f, bnd_vtx=None):
"""
Assemble the right hand side
:param vtx: Vertex array
:param elt: Element array
:param f: Right hand side function f
:param bnd_vtx: Set of boundary vertices
:return: Right hand side vector F
"""
if bnd_vtx is None:
bnd_vtx = set()
F = np.zeros(len(vtx))
for e in elt:
F_loc = Floc(vtx, e, f)
for i in range(3):
if e[i] not in bnd_vtx:
F[e[i]] += F_loc[i]
return F
def Assemble(vtx, elt, f, b=np.array([1, 1]), c=1.):
"""
Assemble the global matrix A (in parts of K, C, and M) and the right hand side F
:param vtx: Vertex arrray
:param elt: Element array
:param f: Right hand side function f
:param b: Vector b
:param c: Parameter c
:return: Global matrices K, C and M and the right hand side vector F
"""
# Prepare treatment of the boundary values
bnd, _ = Boundary(elt)
# Create a set that stores all the boundary vertices
bnd_vtx = {v for e in bnd for v in e}
# Assemble the global matrices
M = Mass(vtx, elt, bnd_vtx)
K = Rig(vtx, elt, bnd_vtx)
C = Cmat(vtx, elt, bnd_vtx, b)
# A = K + C + c * M
# Assemble the right hand side
F = assemble_rhs(vtx, elt, f, bnd_vtx)
return K, C, M, F.T
def Assemble_optimized(vtx, elt, f, b=np.array([1, 1]), c=1.):
"""
Directly Assemble the global matrix A (if K, C and M are not needed) and the right hand side F
:param vtx: Vertex arrray
:param elt: Element array
:param f: Right hand side function f
:param b: Vector b
:param c: Parameter c
:return: Global matrix A and the right hand side vector F
"""
# Prepare treatment of the boundary values
bnd, _ = Boundary(elt)
# Create a set that stores all the boundary vertices
bnd_vtx = {v for e in bnd for v in e}
# Compute the global matrix
nbr_vtx = len(vtx)
nbr_elt = len(elt)
d = len(elt[0])
V = np.zeros((d, d, nbr_elt))
I = np.zeros((d, d, nbr_elt))
J = np.zeros((d, d, nbr_elt))
for i, e in enumerate(elt):
M_loc = Mloc(vtx, e)
K_loc = Kloc(vtx, e)
C_loc = Cloc(vtx, e, b)
for j in range(d):
for k in range(d):
# Check if the current vertex is on the boundary
if (e[j] not in bnd_vtx) and (e[k] not in bnd_vtx):
I[j, k, i] = e[j]
J[j, k, i] = e[k]
V[j, k, i] = K_loc[j, k] + C_loc[j, k] + c * M_loc[j, k]
elif e[j] == e[k]:
I[j, k, i] = e[j]
J[j, k, i] = e[k]
V[j, k, i] = 1.
A = coo_matrix((V.flat, (I.flat, J.flat)), shape=(nbr_vtx, nbr_vtx))
A.sum_duplicates()
# Assemble the right hand side
F = assemble_rhs(vtx, elt, f)
return A, F.T