forked from precice/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblemDefinition.py
68 lines (47 loc) · 1.61 KB
/
problemDefinition.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
import numpy as np
from numpy.linalg import eig
class SpringLeft:
k = 4 * np.pi**2
class SpringMiddle:
k = 16 * (np.pi**2)
class SpringRight:
k = 4 * np.pi**2
class MassLeft:
# mass
m = 1
# initial conditions (the way how we currently compute the analytical
# solution allows arbitrary u0, but requires v0 = 0)
u0 = 1.0
v0 = 0.0
u_analytical, v_analytical = None, None # will be defined below
class MassRight:
# mass
m = 1
# initial conditions (the way how we currently compute the analytical
# solution allows arbitrary u0, but requires v0 = 0)
u0 = 0.0
v0 = 0.0
u_analytical, v_analytical = None, None # will be defined below
# Mass matrix
M = np.array([
[MassLeft.m, 0],
[0, MassRight.m]
])
# Stiffness matrix
K = np.array([
[SpringLeft.k + SpringMiddle.k, -SpringMiddle.k],
[-SpringMiddle.k, SpringRight.k + SpringMiddle.k]
])
# system:
# m ddu + k u = f
# compute analytical solution from eigenvalue ansatz
eigenvalues, eigenvectors = eig(K)
omega = np.sqrt(eigenvalues)
A, B = eigenvectors
c = np.linalg.solve(eigenvectors, [MassLeft.u0, MassRight.u0])
MassLeft.u_analytical = lambda t: c[0] * A[0] * np.cos(omega[0] * t) + c[1] * A[1] * np.cos(omega[1] * t)
MassLeft.v_analytical = lambda t: -c[0] * A[0] * omega[0] * \
np.sin(omega[0] * t) - c[1] * A[1] * omega[1] * np.sin(omega[1] * t)
MassRight.u_analytical = lambda t: c[0] * B[0] * np.cos(omega[0] * t) + c[1] * B[1] * np.cos(omega[1] * t)
MassRight.v_analytical = lambda t: -c[0] * B[0] * omega[0] * \
np.sin(omega[0] * t) - c[1] * B[1] * omega[1] * np.sin(omega[1] * t)