-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
192 lines (160 loc) · 6.38 KB
/
data_utils.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
import json
import numpy as np
import scipy.sparse.linalg as spsla
import scipy.sparse as sps
def get_stokes_solution(mats, Re, control, palpha=1, ct0=0):
""""Compute Stokes solution."""
J = mats['J']
fp = mats['fp'] + mats['fp_div']
NV = J.shape[1]
NP = J.shape[0]
if control == 'bc':
fvstks = mats['fv'] + 1./Re*mats['fv_diff'] + ct0
Astks = 1./Re*mats['A'] + 1./palpha*mats['Arob']
else:
fvstks = mats['fv'] + 1./Re*mats['fv_diff']
Astks = 1./Re*mats['A']
stksrhs = np.vstack([fvstks, fp])
stksmat = sps.vstack([
sps.hstack([Astks, -J.T]),
sps.hstack([J, sps.csc_matrix((NP, NP))])
]).tocsc()
stksvp = spsla.spsolve(stksmat, stksrhs).reshape((NV+NP, 1))
stksv = stksvp[:NV].reshape((NV, 1))
stksp = stksvp[NV:].reshape((NP, 1))
return stksv, stksp
def solve_steadystate_nse(mats, Re, control, tol=1e-10, npicardstps=5, maxit=30,
palpha=1, uvec=np.array([[0], [0]]), v0=None):
"""Compute steady-state NSE solution."""
J = mats['J']
hmat = mats['H']
fp = mats['fp'] + mats['fp_div']
if control == 'bc':
A = 1./Re*mats['A'] + mats['L1'] + mats['L2'] + 1./palpha*mats['Arob']
Brob = mats['Brob']
fv = mats['fv'] + 1./Re*mats['fv_diff'] + mats['fv_conv'] \
+ 1. / palpha*np.dot(Brob, uvec)
else:
A = 1. / Re * mats['A'] + mats['L1'] + mats['L2']
fv = mats['fv'] + 1./Re*mats['fv_diff'] + mats['fv_conv']
updnorm = 1
NV, NP = fv.shape[0], fp.shape[0]
if v0 is None:
curv = np.zeros((NV, 1))
else:
curv = v0
stpcount = 0
while updnorm > tol:
picard = stpcount < npicardstps
H1k, H2k = linearized_convection(hmat, curv, retparts=True)
if picard:
currhs = np.vstack([fv, fp])
HL = H1k
else:
currhs = np.vstack([fv+eva_quadterm(hmat, curv), fp])
HL = H1k + H2k
cursysmat = sps.vstack([
sps.hstack([A+HL, -J.T]),
sps.hstack([J, sps.csc_matrix((NP, NP))])
]).tocsc()
nextvp = spsla.spsolve(cursysmat, currhs).reshape((NV+NP, 1))
print('Iteration step {0} ({1})'.format(stpcount, 'Picard' if picard else 'Newton'))
nextv = nextvp[:NV].reshape((NV, 1))
nextp = nextvp[NV:].reshape((NP, 1))
curnseres = A*nextv + eva_quadterm(hmat, nextv) - J.T*nextp - fv
print('Norm of nse residual: {0:e}'.format(np.linalg.norm(curnseres)))
updnorm = np.linalg.norm(nextv - curv) / np.linalg.norm(nextv)
print('Norm of current update: {0:e}'.format(updnorm))
print('\n')
curv = nextv
stpcount += 1
if stpcount > maxit:
raise RuntimeError('Could not compute steady-state NSE solution.')
return nextv, nextp
def linearized_convection(H, linv, retparts=False):
"""Compute linearized convection term."""
nv = linv.size
# H1 = np.zeros((nv, nv))
# H2 = np.zeros((nv, nv))
if retparts:
H1 = H * (sps.kron(sps.eye(nv), linv))
H2 = H * (sps.kron(linv, sps.eye(nv)))
# for k in range(nv):
# H1[:, k] = (H[:, k*nv:k*nv + nv] @ linv)[:, 0]
# H2[:, k] = (H[:, [x+k for x in range(0, nv*nv, nv)]] @ linv)[:, 0]
return H1, H2
else:
H1 = H * (sps.kron(sps.eye(nv), linv))
H2 = H * (sps.kron(linv, sps.eye(nv)))
# for k in range(nv):
# H[:, k] = ((H[:, k*nv:k*nv + nv]
# + H[:, [x+k for x in list(range(0, nv*nv, nv))]]) @ linv)[:, 0]
return H1 + H2
def writevp_paraview(velvec=None, pvec=None, strtojson=None, visudict=None,
vfile='vel__.vtu', pfile='p__.vtu'):
if visudict is None:
jsfile = open(strtojson)
visudict = json.load(jsfile)
vaux = np.zeros((visudict['vdim'], 1))
for bcdict in visudict['bclist']:
intbcidx = [int(bci) for bci in bcdict.keys()]
vaux[intbcidx, 0] = list(bcdict.values())
vaux[visudict['invinds']] = velvec
vxvtxdofs = visudict['vxvtxdofs']
vyvtxdofs = visudict['vyvtxdofs']
with open(vfile, 'w') as velfile:
velfile.write(visudict['vtuheader_v'])
for xvtx, yvtx in zip(vxvtxdofs, vyvtxdofs):
velfile.write(u'{0} {1} {2} '.format(vaux[xvtx][0], vaux[yvtx][0], 0.))
velfile.write(visudict['vtufooter_v'])
if pvec is not None:
pvtxdofs = visudict['pvtxdofs']
with open(pfile, 'w') as prefile:
prefile.write(visudict['vtuheader_p'])
for pval in pvec[pvtxdofs, 0]:
prefile.write(u'{0} '.format(pval))
prefile.write(visudict['vtufooter_p'])
def collect_vtu_files(filelist, pvdfilestr):
with open(pvdfilestr, 'w') as colfile:
colfile.write(u'<?xml version="1.0"?>\n<VTKFile type="Collection" version="0.1"> <Collection>\n')
for tsp, vtufile in enumerate(filelist):
dtst = u'<DataSet timestep="{0}" part="0" file="{1}"/>'.format(tsp, vtufile)
colfile.write(dtst)
colfile.write(u'</Collection> </VTKFile>')
def eva_quadterm(H, v):
''' function to evaluate `H*kron(v, v)` without forming `kron(v, v)`
Parameters:
---
H : (nv, nv*nv) sparse array
the tensor (as a matrix) that evaluates the convection term
'''
NV = v.size
hvv = np.zeros((NV, 1))
for k, vi in enumerate(v):
hviv = H[:, k*NV:(k+1)*NV]*(vi[0]*v)
hvv = hvv + hviv
return np.array(hvv)
def imaginary_to_csv(filename, ews):
with open(filename, 'w+') as f:
f.write("real,imag\n")
for ew in ews:
f.write(f"{np.real(ew)},{np.imag(ew)}\n")
f.closed
def generate_spectrum_csv(reynolds_numers, mats, control, conv_mat, palpha):
for rey in reynolds_numers:
M = mats['M']
J = mats['J']
if control == 'dist':
Amat = -1./rey * mats['A'] - conv_mat
elif control == 'bc':
Amat = -1./rey * mats['A'] - 1./palpha*mats['Arob'] - conv_mat
Eb = sps.bmat([
[M, sps.csc_matrix(J.T.shape)],
[sps.csc_matrix(J.shape), None]
])
Ab = sps.bmat([
[Amat, J.T],
[J, None]
])
ews, evs = spsla.eigs(Ab, k=50, M=Eb, sigma=0)
imaginary_to_csv("Spectrum_Re_" + str(rey), ews)