-
Notifications
You must be signed in to change notification settings - Fork 2
/
multimodel.py
149 lines (113 loc) · 4.4 KB
/
multimodel.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
from __future__ import print_function, division
import numpy as np
import scipy.sparse as sparse
import scipy.sparse.linalg as linalg
class MultiModelROM:
def __init__(self, M, C, K, f, damp_func):
self.M = M
self.C = C
self.K = K
self.f = f
self.damp_func = damp_func
self.Q = None
self.Mr = None
self.Cr = None
self.Kr = None
self.fr = None
self.is_reduced = False
def linearise_symm(self, M, C, K, **kwargs):
csc = kwargs.get('csc', False)
A = sparse.block_diag((-K, M))
B = sparse.bmat([[C, M], [M, None]])
if csc:
return (A.tocsc(), B.tocsc())
return (A, B)
def linearise(self, M, C, K, **kwargs):
csc = kwargs.get('csc', False)
n = M.shape[0]
A = sparse.bmat([[-K, None],
[None, sparse.identity(n)]])
B = sparse.bmat([[C, M],
[sparse.identity(n), None]])
if csc:
return (A.tocsc(), B.tocsc())
return (A, B)
def eigen_solve(self, omega0, **kwargs):
A, B = self.linearise_symm(self.M,
self.damp_func(omega0)*self.C,
self.K, csc=True)
k = kwargs.get('k', 4)
which = kwargs.get('which', 'SM')
w, v = linalg.eigs(A, M=B, k=k, which=which, sigma=1j*omega0)
return (w, v)
def pod_orthogonalise(self, Q, **kwargs):
tol = kwargs.get('tol', 0.)
# Eigenproblem of covariance matrix
Cqq = np.dot(Q.conj().T, Q)
lmbda, psi = np.linalg.eig(Cqq)
# Filter out eigenvalues by tolerance
lmbda = np.real(lmbda)
mask = lmbda/np.max(lmbda) > tol
lmbda = lmbda[mask]
psi = psi[:, mask]
# Normalise eigenvectors
psi = np.dot(psi, np.diag(1/np.sqrt(lmbda)))
# Return orthogonal basis
return np.dot(Q, psi)
def get_static_correction(self, lmbda, phi):
N = self.K.shape[0]
print('Computing static correction ...')
# static deflection
Ust = linalg.spsolve(self.K, self.f)
# residual
Ust_res = np.zeros((N, N), dtype='complex')
for i in range(0, len(lmbda)):
Ust_res += (1./lmbda[i]) * np.outer(phi[:N, i], phi[:N, i])
Ust_res = Ust_res.dot(self.f)
# correction
return Ust - Ust_res
def reduce(self, omega, n_ip, n_eig, **kwargs):
ind_ip_tmp = np.linspace(0, len(omega)-1, n_ip+1)
ind_ip = np.int_(np.around((ind_ip_tmp[:-1] + ind_ip_tmp[1:])/2.))
N = self.M.shape[0]
# indlude static correction?
stat_corr = kwargs.get('stat_corr', False)
# Solve eigenproblem at interpolation points
print('Solving eigenproblem at interpolation point #1 ...')
lmbda_i, Q_i = self.eigen_solve(omega[ind_ip[0]], k=n_eig)
if stat_corr:
Qst = self.get_static_correction(lmbda_i, Q_i)
Q = np.hstack((Qst[:, None], Q_i[:N, :]))
else:
Q = Q_i[:N, :]
if n_ip > 1:
for i, w in enumerate(omega[ind_ip[1:]]):
print('Solving eigenproblem at interpolation point #{} ...'.format(i+2))
lmbda_i, Q_i = self.eigen_solve(w, k=n_eig)
if stat_corr:
Qst = self.get_static_correction(lmbda_i, Q_i)
Q = np.hstack((Q, Qst[:, None]))
Q = np.hstack((Q, Q_i[:N, :]))
# Orthogonalise union of eigenvectors
print('Performing POD orthogonalisation ...')
Q = self.pod_orthogonalise(Q)
self.Q = Q
# Reduce system matrices and load vector
print('Reducing system matrices ...')
self.Mr = np.dot(Q.conj().T, self.M.dot(Q))
self.Cr = np.dot(Q.conj().T, self.C.dot(Q))
self.Kr = np.dot(Q.conj().T, self.K.dot(Q))
self.fr = np.dot(Q.conj().T, self.f)
self.is_reduced=True
def get_frf(self, omega, ndof, **kwargs):
if self.is_reduced:
print('Computing FRF ...')
sol = np.empty((self.Mr.shape[0], len(omega)), dtype=complex)
g = self.damp_func(omega)
for i, w in enumerate(omega):
A = -w**2*self.Mr + 1j*w*g[i]*self.Cr + self.Kr
sol[:, i] = np.linalg.solve(A, self.fr);
u = np.dot(self.Q, sol)
return u[ndof, :]
else:
print('Call reduce() before this function!')