forked from tan2/DynEarthSol-old
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dynearthsol.py
297 lines (237 loc) · 10.4 KB
/
Dynearthsol.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
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
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
import sys
import numpy as np
# 2D or 3D data?
ndims = 2
class Dynearthsol:
'''Read output file of 2D/3D DynEarthSol'''
def __init__(self, modelname):
self.suffix = 'save'
self.modelname = modelname
self.read_info()
self.read_header(self.frames[0])
return
def read_info(self):
tmp = np.fromfile(self.modelname + '.info', dtype=float, sep=' ')
tmp.shape = (-1, 8)
self.frames = list(tmp[:,0].astype(int))
self.steps = list(tmp[:,1].astype(int))
self.time = list(tmp[:,2].astype(float))
self.nnode_list = tmp[:,5].astype(int)
self.nelem_list = tmp[:,6].astype(int)
return
def get_fn(self, frame):
return '{0}.{1}.{2:0=6}'.format(self.modelname, self.suffix, frame)
def read_header(self, frame):
self._header_frame = frame
headerlen = 4096
fname = self.get_fn(frame)
with open(fname, 'rb') as f:
header = f.read(headerlen).splitlines()
#print(header)
# parsing 1st line
first = header[0].split(b' ')
if (first[0] != b'#' or
first[1] != b'DynEarthSol' or
first[2].split(b'=')[0] != b'ndims' or
first[3].split(b'=')[0] != b'revision'):
print('Error:', fname, 'is not a valid DynEarthSol output file!')
sys.exit(1)
self.ndims = int(first[2].split(b'=')[1])
self.revision = int(first[3].split(b'=')[1])
if self.ndims == 2:
self.nstr = 3
self.component_names = ('XX', 'ZZ', 'XZ')
else:
self.nstr = 6
self.component_names = ('XX', 'YY', 'ZZ', 'XY', 'XZ', 'YZ')
# parsing other lines
self.field_pos = {}
for line in header[1:]:
# test for null in python3 bytes and python2 str
if line[0] in (0, '\x00'): break # end of record
name, pos = line.split(b'\t')
self.field_pos[name.decode('ascii')] = int(pos)
#print(self.field_pos)
return
def _get_dtype_count_shape(self, frame, name):
i = self.frames.index(frame)
nnode = self.nnode_list[i]
nelem = self.nelem_list[i]
dtype = np.float64 if name not in ('connectivity', 'bcflag') else np.int32
if name in set(['strain', 'strain-rate', 'stress', 'stress averaged']):
count = self.nstr * nelem
shape = (nelem, self.nstr)
elif name in set(['density', 'material', 'mesh quality',
'plastic strain', 'plastic strain-rate',
'viscosity', 'edvoldt', 'volume','radiogenic source']):
count = nelem
shape = (nelem, )
elif name in set(['connectivity']):
count = (self.ndims + 1) * nelem
shape = (nelem, self.ndims+1)
elif name in set(['coordinate', 'velocity', 'velocity averaged', 'force', 'coord0']):
count = self.ndims * nnode
shape = (nnode, self.ndims)
elif name in set(['bcflag', 'temperature', 'mass', 'tmass', 'volume_n']):
count = nnode
shape = (nnode, )
else:
raise NameError('uknown field name: ' + name)
return dtype, count, shape
def read_field(self, frame, name):
if frame != self._header_frame: self.read_header(frame)
dtype, count, shape = self._get_dtype_count_shape(frame, name)
pos = self.field_pos[name]
fname = self.get_fn(frame)
with open(fname,'r') as f:
f.seek(pos)
field = np.fromfile(f, dtype=dtype, count=count).reshape(shape)
return field
def load_calculation(self, frame, name):
def second_invariant(t):
'''The second invariant of the deviatoric part of a symmetric tensor t,
where t[:,0:ndims] are the diagonal components;
and t[:,ndims:] are the off-diagonal components.'''
nstr = t.shape[1]
# second invariant: sqrt(0.5 * t_ij**2)
if nstr == 3: # 2D
return np.sqrt(0.25 * (t[:,0] - t[:,1])**2 + t[:,2]**2)
else: # 3D
a = (t[:,0] + t[:,1] + t[:,2]) / 3
return np.sqrt( 0.5 * ((t[:,0] - a)**2 + (t[:,1] - a)**2 + (t[:,2] - a)**2) +
t[:,3]**2 + t[:,4]**2 + t[:,5]**2)
if name == 'heat flux':
conductivity = 3.3
coord = self.read_field(frame, 'coordinate')
temperature = self.read_field(frame, 'temperature')
connectivity = self.read_field(frame, 'connectivity')
p = np.transpose(coord[connectivity], (1,2,0))
t = np.transpose(temperature[connectivity], (1,0))
v_arr = np.zeros((self.ndims,self.ndims+1,connectivity.shape[0]))
v_arr[:,:-1] = p[:-1] - p[-1]
v_arr[:,-1] = t[:-1] - t[-1]
nv = np.cross(v_arr[0].T,v_arr[1].T)
v_slope = np.cross(nv, np.cross(nv, [0,0,1])).T
norm = np.linalg.norm(v_slope[:-1], axis=0)
flux_val = -1.e3 * conductivity * v_slope[-1] / norm
flux = v_slope[:-1] * flux_val / norm
return flux, flux_val
elif name == 'strain II':
strain = self.read_field(frame, 'strain')
sII = second_invariant(strain)
return sII
elif name == 'ecoordinate':
coord = self.read_field(frame, 'coordinate')
connectivity = self.read_field(frame, 'connectivity')
ecoord = coord[connectivity]
return ecoord
else:
raise NameError('uknown field name: ' + name)
def overwrite_field(self, frame, name, data):
if frame != self._header_frame: self.read_header(frame)
if frame != self._header_frame: read_header(frame)
if name.startswith(('markerset.', 'hydrous-markerset.')):
dtype = data.dtype
count = len(data)
shape = (count,)
else:
dtype, count, shape = self._get_dtype_count_shape(frame, name)
if data.shape != shape:
raise Error('Shape of {0} field is changed! Expecting {1}, got {2}.'.format(name, shape, data.shape))
pos = self.field_pos[name]
fname = self.get_fn(frame)
with open(fname, 'r+') as f:
f.seek(pos)
f.write(data.tostring())
return
def read_markers(self, frame, markername):
'Read and return marker data'
if frame != self._header_frame: self.read_header(frame)
fname = self.get_fn(frame)
with open(fname) as f:
pos = self.field_pos[markername+' size']
f.seek(pos)
nmarkers = np.fromfile(f, dtype=np.int32, count=1)[0]
marker_data = {'size': nmarkers}
# floating point
for name in (markername+'.coord',):
pos = self.field_pos[name]
f.seek(pos)
tmp = np.fromfile(f, dtype=np.float64, count=nmarkers*self.ndims)
marker_data[name] = tmp.reshape(-1, self.ndims)
#print(marker_data[name].shape, marker_data[name])
try:
for name in (markername+'.eta',):
pos = self.field_pos[name]
f.seek(pos)
tmp = np.fromfile(f, dtype=np.float64, count=nmarkers*(self.ndims+1))
marker_data[name] = tmp.reshape(-1, (self.ndims+1))
except:
pass
# int
for name in (markername+'.elem', markername+'.mattype', markername+'.id'):
pos = self.field_pos[name]
f.seek(pos)
marker_data[name] = np.fromfile(f, dtype=np.int32, count=nmarkers)
#print(marker_data[name].shape, marker_data[name])
# float
for name in (markername+'.time',markername+'.z',markername+'.distance',markername+'.slope'):
try:
pos = self.field_pos[name]
f.seek(pos)
marker_data[name] = np.fromfile(f, dtype=np.float64, count=nmarkers)
except:
pass
return marker_data
class DynearthsolCheckpoint(Dynearthsol):
'''Read chkpt file of 2D/3D DynEarthSol'''
def __init__(self, modelname, frame):
self.suffix = 'chkpt'
self.modelname = modelname
self.read_info()
self.read_header(frame)
return
def _get_dtype_count_shape(self, frame, name):
i = self.frames.index(frame)
nnode = self.nnode_list[i]
nelem = self.nelem_list[i]
dtype = np.float64 if name != 'connectivity' else np.int32
if name in set(['volume_old']):
count = nelem
shape = (nelem, )
else:
raise NameError('uknown field name: ' + name)
return dtype, count, shape
def read_markers(self, frame, markername):
'Read and return marker data'
if frame != self._header_frame: read_header(frame)
fname = self.get_fn(frame)
with open(fname) as f:
pos = self.field_pos[markername+' size']
f.seek(pos)
nmarkers = np.fromfile(f, dtype=np.int32, count=1)[0]
marker_data = {'size': nmarkers}
# floating point
for name in (markername+'.eta',):
pos = self.field_pos[name]
f.seek(pos)
tmp = np.fromfile(f, dtype=np.float64, count=nmarkers*(self.ndims+1))
marker_data[name] = tmp.reshape(-1, self.ndims+1)
#print(marker_data[name].shape, marker_data[name])
# int
for name in (markername+'.elem', markername+'.mattype', markername+'.id'):
pos = self.field_pos[name]
f.seek(pos)
marker_data[name] = np.fromfile(f, dtype=np.int32, count=nmarkers)
#print(marker_data[name].shape, marker_data[name])
# float
for name in (markername+'.time',markername+'.z',markername+'.distance',markername+'.slope'):
try:
pos = self.field_pos[name]
f.seek(pos)
marker_data[name] = np.fromfile(f, dtype=np.float64, count=nmarkers)
except:
pass
return marker_data