-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpetsc_io.py
More file actions
324 lines (278 loc) · 8.87 KB
/
petsc_io.py
File metadata and controls
324 lines (278 loc) · 8.87 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
import argparse
import math
import sys
def read_PETSc_vec(file):
import numpy
dsource = numpy.DataSource()
# open file
# omit header
# read length
# read values
# close file
try:
f = open(file, "rb")
except:
print("Unexpected error:", sys.exc_info()[0],file)
numpy.fromfile(f, dtype=">i4", count=1)
nvec = numpy.fromfile(f, dtype=">i4", count=1)
#load data and change it to little endian, importend for np.dot
v = numpy.fromfile(f, dtype=">f8", count=nvec[0]).astype('<f8')
f.close()
return v
def write_PETSc_vec(v,file):
import numpy
f = open(file, "wb")
header = numpy.array([1211214])
nx = numpy.array(v.shape[0])
header.astype('>i4').tofile(f)
nx.astype('>i4').tofile(f)
v.astype('>f8').tofile(f)
f.close()
return 0
def write_PETSc_mat(A,file):
#does transpose the matrix samehow fixed with A.tocsr() because of row major/colum major
import struct
import numpy
header = numpy.array([1211216])
dims = A.shape
nx = numpy.array(dims[0])
ny = numpy.array(dims[1])
nnz = numpy.array([A.nnz])
rowidx,colidx = A.nonzero()
nrow,k = numpy.histogram(rowidx,range(0,dims[0]+1))
# print('header')
# print(header)
# print("dims")
# print(dims)
# print("nnz")
# print (nnz)
# print ("nrow")
# print (nrow,nrow.shape)
# print ("colidx")
# print (colidx,colidx.shape)
# print('val')
# print(A.data)
f = open(file, "wb")
header.astype('>i4').tofile(f)
nx.astype('>i4').tofile(f)
ny.astype('>i4').tofile(f)
nnz.astype('>i4').tofile(f)
nrow.astype('>i4').tofile(f)
colidx.astype('>i4').tofile(f)
A.data.astype('>f8').tofile(f)
f.close()
return 0
def write_PETSc_mat_dense(A,file):
#does transpose the matrix samehow fixed with A.tocsr() because of row major/colum major
import struct
import numpy
header = numpy.array([1211216])
dims = A.shape
nx = numpy.array(dims[0])
ny = numpy.array(dims[1])
matrixFormat = numpy.array([-1])
# print('header')
# print(header)
# print("dims")
# print(dims)
# print("nnz")
# print (nnz)
# print ("nrow")
# print (nrow,nrow.shape)
# print ("colidx")
# print (colidx,colidx.shape)
# print('val')
# print(A.data)
f = open(file, "wb")
header.astype('>i4').tofile(f)
nx.astype('>i4').tofile(f)
ny.astype('>i4').tofile(f)
matrixFormat.astype('>i4').tofile(f)
A.astype('>f8').tofile(f)
f.close()
return 0
def read_PETSc_mat_dense(file):
import numpy
# open file
try:
f = open(file, "rb")
except:
print("Unexpected error:", sys.exc_info()[0],file)
# omit header
numpy.fromfile(f, dtype=">i4", count=1)
# read dims
nx = numpy.fromfile(f, dtype=">i4", count=1)
ny = numpy.fromfile(f, dtype=">i4", count=1)
format = numpy.fromfile(f, dtype=">i4", count=1)
val = numpy.fromfile(f, dtype=">f8", count=(ny[0]*nx[0]))
# print("dims")
# print( nx, ny)
# print("nnz")
# print (nnz)
# print ("nrow")
# print (nrow,nrow.shape)
# print ("colidx")
# print (colidx,colidx.shape)
# print ("val")
# print (val)
# close file
f.close()
#create full matrix
lsmfull = numpy.zeros(shape=(nx[0], ny[0]), dtype=numpy.float_)
offset = 0
for i in range(nx[0]):
for j in range(ny[0]):
lsmfull[i, j] = val[offset]
offset = offset + 1
#print (numpy.nonzero(lsmfull),i,j,offset,val[offset] )
return lsmfull
def read_PETSc_mat(file):
import numpy
from scipy.sparse import csr_matrix
# open file
try:
f = open(file, "rb")
except:
print("Unexpected error:", sys.exc_info()[0],file)
# omit header
numpy.fromfile(f, dtype=">i4", count=1)
# read dims
nx = numpy.fromfile(f, dtype=">i4", count=1)
ny = numpy.fromfile(f, dtype=">i4", count=1)
nnz = numpy.fromfile(f, dtype=">i4", count=1)
nrow = numpy.fromfile(f, dtype=">i4", count=nx[0])
colidx = numpy.fromfile(f, dtype=">i4", count=nnz[0])
val = numpy.fromfile(f, dtype=">f8", count=nnz[0])
# print("dims")
# print( nx, ny)
# print("nnz")
# print (nnz)
# print ("nrow")
# print (nrow,nrow.shape)
# print ("colidx")
# print (colidx,colidx.shape)
# print ("val")
# print (val)
# close file
f.close()
# create sparse matrix
spdata = numpy.zeros((nnz[0],3))
offset = 0
for i in range(nx[0]):
if not nrow[i] == 0.0:
spdata[offset:offset+nrow[i],0]= i
spdata[offset:offset+nrow[i],1]= colidx[offset:offset+nrow[i]]
spdata[offset:offset+nrow[i],2]= val[offset:offset+nrow[i]]
offset = offset+nrow[i]
#print(spdata[:,0])
return csr_matrix((spdata[:,2], (spdata[:,0],spdata[:,1])), shape=(nx, ny),dtype=numpy.float_)
# create full matrix
# lsmfull = numpy.zeros(shape=(nx[0], ny[0]), dtype=int)
# offset = 0
# for i in range(nx[0]):
# if not nrow[i] == 0.0:
# for j in range(nrow[i]):
# lsmfull[i, colidx[offset]] = int(val[offset])
# offset = offset + 1
#return lsmfull
def read_data(tracer):
''' reads vector and returns it wiht the right geometry
'''
import numpy as np
# arrays
# v1d, z, dz, lsm (land sea mask)
v1d = read_PETSc_vec("simulation/" + tracer)
z = read_PETSc_vec("data/TMM/2.8/Forcing/DomainCondition/z.petsc")
dz = read_PETSc_vec("data/TMM/2.8/Forcing/DomainCondition/dz.petsc")
lsm = read_PETSc_mat("data/TMM/2.8/Geometry/landSeaMask.petsc")
# dims
nx, ny = lsm.shape
nz = 15
# v3d
v3d = np.zeros(shape=(3, nx, ny, nz), dtype=float)
v3d[:,:,:,:] = np.nan
# v1d -> (v3d, z, dz)
offset = 0
for ix in range(nx):
for iy in range(ny):
length = lsm[ix, iy]
if not length == 0:
v3d[0, ix, iy, 0:length] = v1d[offset:offset+length]
v3d[1, ix, iy, 0:length] = z[offset:offset+length]
v3d[2, ix, iy, 0:length] = dz[offset:offset+length]
offset = offset + length
return v3d
def vectogeometry(v1d):
''' creates right vectorgeometry for ploting on world map
'''
import numpy as np
# arrays
# v1d, z, dz, lsm (land sea mask)
z = read_PETSc_vec("data/TMM/2.8/Forcing/DomainCondition/z.petsc")
dz = read_PETSc_vec("data/TMM/2.8/Forcing/DomainCondition/dz.petsc")
lsm = read_PETSc_mat("data/TMM/2.8/Geometry/landSeaMask.petsc")
# dims
nx, ny = lsm.shape
nz = 15
# v3d
v3d = np.zeros(shape=(3, nx, ny, nz), dtype=float)
v3d[:,:,:,:] = np.nan
# v1d -> (v3d, z, dz)
offset = 0
for ix in range(nx):
for iy in range(ny):
length = int(lsm[ix, iy])
if not length == 0:
v3d[0, ix, iy, 0:length] = v1d[offset:offset+length]
v3d[1, ix, iy, 0:length] = z[offset:offset+length]
v3d[2, ix, iy, 0:length] = dz[offset:offset+length]
offset = offset + length
return v3d
def create_figure_surface(figid, aspect, xx, yy, cmin, cmax, levels, slices, v3d):
# prepare matplotlib
import matplotlib
matplotlib.rc("font",**{"family":"sans-serif"})
matplotlib.rc("text", usetex=True)
#matplotlib.use("PDF")
import matplotlib.pyplot as plt
# basemap
from mpl_toolkits.basemap import Basemap
# numpy
import numpy as np
# data
vv = v3d[0,:,:,4]
# shift
vv = np.roll(vv, 64, axis=1)
# plot surface
plt.figure(figid)
# colormap
cmap = plt.cm.bone_r
# contour fill
p1 = plt.contourf(xx, yy, vv, cmap=cmap, levels=levels, origin="lower")#, hold="on")
plt.clim(cmin, cmax)
# contour lines
p2 = plt.contour(xx, yy, vv, levels=levels, linewidths = (1,), colors="k")#, hold="on")
plt.clabel(p2, fmt = "%2.3f", colors = "k", fontsize = 14)
#plt.colorbar(p2,shrink=0.8, extend='both')
# slices
#s1 = xx[np.mod(slices[0]+64, 128)]
#s2 = xx[np.mod(slices[1]+64, 128)]
#s3 = xx[np.mod(slices[2]+64, 128)]
# print s1, s2, s3
#plt.vlines([s1, s2, s3], -90, 90, color='k', linestyles='--')
# set aspect ratio of axes
plt.gca().set_aspect(aspect)
# basemap
m = Basemap(projection="cyl")
m.drawcoastlines(linewidth = 0.5)
# xticks
plt.xticks(range(-180, 181, 45), range(-180, 181, 45))
plt.xlim([-180, 180])
plt.xlabel("Longitude [degrees]", labelpad=8)
# yticks
plt.yticks(range(-90, 91, 30), range(-90, 91, 30))
plt.ylim([-90, 90])
plt.ylabel("Latitude [degrees]")
# write to file
#plt.savefig("solution-surface", bbox_inches="tight")
plt.show()