-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfembase.py
More file actions
575 lines (441 loc) · 19.8 KB
/
fembase.py
File metadata and controls
575 lines (441 loc) · 19.8 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
'''Basic classes and functions for finite element (FEM) programming.
Written by Stefan Possanner
stefan.possanner@ma.tum.de
Contains:
LagrangeShape : class
The class for 1D Lagrange shape functions on the interval [-1,1].
lag_assemb : function
Computes the global mass and stiffness matrices from Lagrange basis functions.
lag_L2prod : function
Computes the L2 scalar product of a given function with each element
of a basis defined from shape functions.
lag_fun : function
Given a coefficient vector, returns a function in the space spanned by a Lagrange basis.
'''
import numpy as np
from scipy.integrate import fixed_quad as gauss_int
from scipy.linalg import block_diag
class LagrangeShape:
'''The class for 1D Lagrange shape functions on the interval [-1,1].
Parameters:
pts : ndarray
1D array of increasing values in [-1, 1] defining the Lagrange polynomials.
Returns:
self.kind : string
Is set to 'lagrange'.
self.d : int
Polynomial degree.
self.s : ndarray
The input array pts (knot sequence).
self.eta : list
List elements are the Lagrange polynomials (LPs) in 'poly1d' format.
self.Deta : list
List elements are the derivatives of the LPs in 'poly1d' format.
self.mass0 : ndarray
Local mass matrix of the LPs.
self.stiff0 : ndarray
Local stiffness matrix of the LPs.
self.chi : list
List elements are the Lagrange histopolation polynomials (LHPs) in 'poly1d' format.
self.Dchi : list
List elements are the derivatives of LHPs in 'poly1d' format.
self.mass1 : ndarray
Local mass matrix of the LHPs.
self.stiff1 : ndarray
Local stiffness matrix of the LHPs.
'''
kind = 'lagrange'
def __init__(self, pts):
self.d = len(pts) - 1
# polynomial degree
self.s = pts
# knot sequence
### Lagrange polynomials (LPs):
self.eta = []
for i in range(self.d + 1):
condition = self.s != self.s[i]
roots = np.compress(condition, self.s)
self.eta.append(np.poly1d(roots, r=True))
# Numerator of LP
for j in range(len(roots)):
self.eta[i] /= self.s[i] - roots[j]
# Denominator of LP
# derivatives of LPs
self.Deta = []
for i in range(self.d + 1):
self.Deta.append(np.polyder(self.eta[i]))
# mass and stiffness matrix:
self.mass0 = np.zeros((self.d + 1, self.d + 1))
self.stiff0 = np.zeros((self.d + 1, self.d + 1))
for i in range(self.d + 1):
for j in range(self.d + 1):
antider = np.polyint(self.eta[i]*self.eta[j])
self.mass0[i, j] = antider(1) - antider(-1)
antider_D = np.polyint(self.Deta[i]*self.Deta[j])
self.stiff0[i, j] = antider_D(1) - antider_D(-1)
### Lagrange histopolation polynomials (LHPs):
self.chi = []
for i in range(self.d):
chisum = 0
for j in range(i + 1, self.d + 1):
chisum += self.Deta[j]
# sum of derivatives of LPs
self.chi.append(chisum)
# derivatives of LHPs
self.Dchi = []
for i in range(self.d):
self.Dchi.append(np.polyder(self.chi[i]))
# mass and stiffness matrix:
self.mass1 = np.zeros((self.d, self.d))
self.stiff1 = np.zeros((self.d, self.d))
for i in range(self.d):
for j in range(self.d):
antider = np.polyint(self.chi[i]*self.chi[j])
self.mass1[i, j] = antider(1) - antider(-1)
antider_D = np.polyint(self.Dchi[i]*self.Dchi[j])
self.stiff1[i, j] = antider_D(1) - antider_D(-1)
def lag_assemb(el_b, mass_eta, stiff_eta, basis = 1, bcs = 2):
''' Computes the global mass and stiffness matrices from Lagrange basis functions.
Parameters:
el_b : ndarray
1D array of element interfaces from left to right including the boundaries.
mass_eta : ndarray
The mass matrix (2D) of the shape functions defined on [-1, 1].
stiff_eta : ndarray
The stiffness matrix (2D) of the shape functions defined on [-1, 1].
bcs : int
Specifies the boundary conditions. DEFAULT = 2 which stands for Dirichlet.
1 stands for periodic.
basis: int
The type of basis functions. DEFAULT = 1 which stands for Langrange interpolation polynomials. 2 stands for Lagrange histoplation polynomials.
Returns:
Nel : int
The number of elements, Nel = len(el_b) - 1.
Nbase : int
Number of basis functions, Nbase = np.size(fun_bar)
mass : ndarray
The mass matrix. If m = np.size(mass_eta[:, 0]) denotes the size of the local mass matrix,
then np.size(mass[:, 0]) = Nel*m - (Nel - 1) - bcs.
stiff : ndarray
The stiffness matrix. Same size as mass.
'''
if basis == 1:
Nel = len(el_b) - 1
# number of elements
m = mass_eta[:, 0].size
# size of local mass matrix (of the shape functions)
d = m - 1
# polynomial degree
Ntot = Nel*m
# number of degrees of freedom (including the boundary)
Ntot -= (Nel - 1)
# subtracted the number of shared degrees of freedom (interior interfaces)
Nbase = Ntot - bcs
# number of basis functions (for the respective boundary conditions)
mass = np.zeros((Nbase, Nbase))
stiff = np.zeros((Nbase, Nbase))
# initiate mass and stiffness matrix
if bcs == 2:
# left boundary:
mass[:d, :d] = (el_b[1] - el_b[0])/2*mass_eta[1:, 1:]
stiff[:d, :d] = 2/(el_b[1] - el_b[0])*stiff_eta[1:, 1:]
index = d - 1
# bulk:
for i in range(1, Nel - 1):
mass[index:index + d + 1, index:index + d + 1] += (el_b[i + 1] - el_b[i])/2*mass_eta[:, :]
stiff[index:index + d + 1, index:index + d + 1] += 2/(el_b[i + 1] - el_b[i])*stiff_eta[:, :]
index += d
# remark the '+=' in mass (stiff) for the cumulative sum for overlapping degrees of freedom
# right boundary
if bcs == 2:
mass[index:index + d, index:index + d] += (el_b[-1] - el_b[-2])/2*mass_eta[:-1, :-1]
stiff[index:index + d, index:index + d] += 2/(el_b[-1] - el_b[-2])*stiff_eta[:-1, :-1]
return Nel, Nbase, mass, stiff
elif bcs == 1:
# bulk
for ie in range(0, Nel):
for il in range(0, d + 1):
for jl in range(0, d + 1):
i = d*ie + il
j = d*ie + jl
mass[i%Nbase, j%Nbase] += (el_b[ie + 1] - el_b[ie])/2*mass_eta[il, jl]
stiff[i%Nbase, j%Nbase] += 2/(el_b[ie + 1] - el_b[ie])*stiff_eta[il, jl]
return Nel, Nbase, mass, stiff
else:
print('boundary conditions not yet implemented!')
return
elif basis == 2:
Nel = len(el_b) - 1
# number of elements
d = mass_eta[:, 0].size
# size of local mass matrix (of the shape functions)
Nbase = Nel*d
# number of basis functions (for the respective boundary conditions)
mass = np.zeros((Nbase, Nbase))
stiff = np.zeros((Nbase, Nbase))
# initiate mass and stiffness matrix
for ie in range(Nel):
i = ie*d
mass[i:(i + d), i:(i + d)] = (el_b[ie + 1] - el_b[ie])/2*mass_eta
stiff[i:(i + d), i:(i + d)] = (el_b[ie + 1] - el_b[ie])/2*stiff_eta
return Nel, Nbase, mass, stiff
def lag_L2prod(fun, eta, el_b, basis = 1, bcs = 2):
'''Computes the L2 scalar product of a function with each element of a Lagrange basis
defined from shape functions.
Parameters:
fun : function
The input function, for example a 'lambda'-function.
eta : list
List elements are the shape functions in 'poly1d' format.
el_b : ndarray
1D array of element interfaces from left to right including the boundaries.
bcs : int
Specifies the boundary conditions. DEFAULT = 2 which stands for Dirichlet.
1 stands for periodic.
basis : int
The type of basis functions. DEFAULT = 1 which stands for Langrange interpolation polynomials. 2 stands for Lagrange histoplation polynomials.
Returns:
Nel : int
The number of elements, Nel = len(el_b) - 1.
Nbase : int
Number of basis functions, Nbase = np.size(fun_bar)
funbar : ndarray
1D array of scalar products of fun with the basis functions.
'''
from scipy.integrate import fixed_quad
if basis == 1:
Nel = len(el_b) - 1
# number of elements
m = len(eta)
# number of shape functions
d = m - 1
# polynomial degree
Ntot = Nel*m
# number of degrees of freedom (including the boundary)
Ntot -= (Nel - 1)
# subtracted the number of shared degrees of freedom (interior interfaces)
Nbase = Ntot - bcs
# number of basis functions (for the respective boundary conditions)
funbar = np.zeros(Nbase)
# initiate output vector
index = 0
# index of the basis function
if bcs == 2:
# left boundary:
i = 0
for j in range(1, m):
fun1 = lambda s: fun( el_b[i] + (s + 1)/2*(el_b[i + 1] - el_b[i]) )
# function fun transformed to the reference element [-1, 1]
fun2 = lambda s: np.polyval(eta[j], s)
# shape function
fun12 = lambda s: fun1(s)*fun2(s)
intval, foo = fixed_quad(fun12, -1, 1)
funbar[index] += (el_b[i + 1] - el_b[i])/2*intval
# integral
if j != d:
index += 1
# If it is the last shape function (j = d), the index rests the same
# and the subsequent integral is added at the same position in funbar.
# bulk:
for i in range(1, Nel - 1):
for j in range(m):
fun1 = lambda s: fun( el_b[i] + (s + 1)/2*(el_b[i + 1] - el_b[i]) )
# function fun transformed to the reference element [-1, 1]
fun2 = lambda s: np.polyval(eta[j], s)
# shape function
fun12 = lambda s: fun1(s)*fun2(s)
intval, foo = fixed_quad(fun12, -1, 1)
funbar[index] += (el_b[i + 1] - el_b[i])/2*intval
# integral
if j != d:
index += 1
# right boundary:
i = Nel - 1
for j in range(d):
fun1 = lambda s: fun( el_b[i] + (s + 1)/2*(el_b[i + 1] - el_b[i]) )
# function fun transformed to the reference element [-1, 1]
fun2 = lambda s: np.polyval(eta[j], s)
# shape function
fun12 = lambda s: fun1(s)*fun2(s)
intval, foo = fixed_quad(fun12, -1, 1)
funbar[index] += (el_b[i + 1] - el_b[i])/2*intval
# integral
if j != d:
index += 1
return Nel, Nbase, funbar
elif bcs == 1:
for ie in range(0, Nel):
for il in range(0, d + 1):
fun1 = lambda s: fun( el_b[ie] + (s + 1)/2*(el_b[ie + 1] - el_b[ie]) )
# function fun transformed to the reference element [-1, 1]
fun2 = lambda s: np.polyval(eta[il], s)
# shape function
fun12 = lambda s: fun1(s)*fun2(s)
intval, foo = fixed_quad(fun12, -1, 1)
i = d*ie + il
funbar[i%Nbase] += (el_b[ie + 1] - el_b[ie])/2*intval
return Nel, Nbase, funbar
else:
print('boundary conditions not yet implemented')
elif basis == 2:
Nel = len(el_b) - 1
# number of elements
d = len(eta)
# polynomial degree
Nbase = Nel*d
# number of basis functions (for the respective boundary conditions)
funbar = np.zeros(Nbase)
# initiate output vector
for ie in range(0, Nel):
for il in range(0, d):
fun1 = lambda s: fun( el_b[ie] + (s + 1)/2*(el_b[ie + 1] - el_b[ie]) )
# function fun transformed to the reference element [-1, 1]
fun2 = lambda s: np.polyval(eta[il], s)
# shape function
fun12 = lambda s: fun1(s)*fun2(s)
intval, foo = fixed_quad(fun12, -1, 1)
i = d*ie + il
funbar[i] += (el_b[ie + 1] - el_b[ie])/2*intval
return Nel, Nbase, funbar
def lag_fun(cvec, eta, el_b, basis = 1, bcs = 2):
'''Given a coefficient vector, returns a function in the space spanned by a Lagrange basis.
Parameters:
cvec : ndarray
Coefficient vector.
eta : list
List elements are the shape functions in 'poly1d' format.
el_b : ndarray
1D array of element interfaces from left to right including the boundaries.
bcs : int
Specifies the boundary conditions. DEFAULT = 2 which stands for Dirichlet.
1 stands for periodic.
basis : int
The type of basis functions. DEFAULT = 1 which stands for Langrange interpolation polynomials. 2 stands for Lagrange histoplation polynomials.
Returns:
Nel : int
The number of elements, Nel = len(el_b) - 1.
Nbase : int
Number of basis functions, Nbase = np.size(fun_bar).
fun : function
Function defined on [el_b[0], el_b[-1]].
'''
if basis == 1:
Nel = len(el_b) - 1
# number of elements
m = len(eta)
# number of shape functions
d = m - 1
# polynomial degree
Ntot = Nel*m
# number of degrees of freedom (including the boundary)
Ntot -= (Nel - 1)
# subtracted the number of shared degrees of freedom (interior interfaces)
Nbase = Ntot - bcs
# number of basis functions (for the respective boundary conditions)
el_b[-1] += 1e-8
# important for binning, see np.digitize
def fun(x):
'''Function in a finite dimensional space spanned by Lagrange basis functions,
created with fembase.lag_fun.
Parameters:
x : ndarray
Array of arguments passed to the function f.
Returns:
funval : ndarray
f(x), same size as x.
'''
binnr = np.digitize(x, el_b)
# binning the input arguments
il = binnr == 1
ir = binnr == Nel
ii = np.logical_not(np.logical_or(il, ir))
# logicals to treat boundary conditions
funval = np.zeros(np.size(x))
index = np.int_(np.zeros(np.size(x)))
index[ii] = (binnr[ii] - 1)*d - 1
index[ir] = (binnr[ir] - 1)*d - 1
# the starting index for each bin to identify the coefficient of the basis function
if bcs == 2:
for i in range(len(x)):
# left boundary:
if il[i] == True:
for j in range(1, m):
funval[i] += ( cvec[index[i]]*np.polyval( eta[j],
2*(x[i] - el_b[binnr[i] - 1])
/(el_b[binnr[i]] - el_b[binnr[i] - 1]) - 1. ) )
index[i] += 1
# right boundary:
elif ir[i] == True:
for j in range(d):
funval[i] += ( cvec[index[i]]*np.polyval( eta[j],
2*(x[i] - el_b[binnr[i] - 1])
/(el_b[binnr[i]] - el_b[binnr[i] - 1]) - 1. ) )
index[i] += 1
# bulk:
else:
for j in range(m):
funval[i] += ( cvec[index[i]]*np.polyval( eta[j],
2*(x[i] - el_b[binnr[i] - 1])
/(el_b[binnr[i]] - el_b[binnr[i] - 1]) - 1. ) )
index[i] += 1
return funval
elif bcs == 1:
for i in range(len(x)):
for j in range(0,m):
index = (binnr[i] - 1)*d + j
funval[i] += ( cvec[index%Nbase]*np.polyval( eta[j],
2*(x[i] - el_b[binnr[i] - 1])
/(el_b[binnr[i]] - el_b[binnr[i] - 1]) - 1. ) )
return funval
return Nel, Nbase, fun
elif basis == 2:
Nel = len(el_b) - 1
# number of elements
d = len(eta)
# polynomial degree
Nbase = Nel*d
# number of basis functions (for the respective boundary conditions)
el_b[-1] += 1e-8
# important for binning, see np.digitize
def fun(x):
funval = np.zeros(np.size(x))
binnr = np.digitize(x, el_b)
for i in range(len(x)):
for j in range(d):
index = (binnr[i] - 1)*d + j
funval[i] += ( cvec[index]*np.polyval( eta[j],
2*(x[i] - el_b[binnr[i] - 1])
/(el_b[binnr[i]] - el_b[binnr[i] - 1]) - 1. ) )
return funval
return Nel, Nbase, fun
def lag_proj(s, el_b, fun):
'''Projects the given function fun on the Lagrange histopolation basis.
Parameters:
s : ndarray
local knot vector on reference element [-1,1] defining the Lagrange polynomials.
el_b : ndarray
The element boundaries.
fun : function
The function to be projected.
Returns:
xvec : ndarray
Global knot vector
funvec : ndarray
Coefficient vector of the projected function
'''
Nel = len(el_b) - 1
d = len(s) - 1
Nknots = Nel*d + 1
xvec = np.zeros(Nknots)
funvec = np.zeros(Nel*d)
for ie in range(Nel):
for il in range(d + 1):
i = ie*d + il
xvec[i] = el_b[ie] + (s[il] + 1)/2*(el_b[ie + 1] - el_b[ie])
# assemble global knot vector
for ie in range(Nel):
jac = (el_b[ie + 1] - el_b[ie])/2
for il in range(d):
i = ie*d + il
funvec[i] = 1/(2*jac)*(fun(xvec[i + 1]) + fun(xvec[i]))*(xvec[i + 1] - xvec[i])
return xvec, funvec