-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflacmarker2vtk.py
More file actions
executable file
·149 lines (118 loc) · 3.85 KB
/
flacmarker2vtk.py
File metadata and controls
executable file
·149 lines (118 loc) · 3.85 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
#!/usr/bin/env python
'''Convert the binary marker output of flac to VTK (vtp) files.
'''
from __future__ import print_function
import sys, os, glob
import numpy as np
import flac
from flac2vtk import vts_dataarray
# filtering markers to only those within the domain bounds (in km)
filtering = False
xmin = 300
xmax = 700
zmin = -50
zmax = 100
def filter_marker(x, z, age, phase, ID, a1, a2, ntriag):
# bool * bool is element-wise logical AND
ind = (xmin <= x) * (x <= xmax) * (zmin <= z) * (z <= zmax)
x = x[ind]
z = z[ind]
age = age[ind]
phase = phase[ind]
ID = ID[ind]
a1 = a1[ind]
a2 = a2[ind]
ntriag = ntriag[ind]
return x, z, age, phase, ID, a1, a2, ntriag
def main(path, start=1, end=-1):
# changing directory
os.chdir(path)
fl = flac.Flac()
if end == -1:
end = fl.nrec
if start == -1:
vtplist = sorted(glob.glob('flacmarker.*.vtp'))
lastframe = int(vtplist[-1][11:-4]) if vtplist else 0
start = lastframe + 1
for i in range(start, end+1):
x, z, age, phase, ID, a1, a2, ntriag = fl.read_markers(i)
if filtering:
x, z, age, phase, ID, a1, a2, ntriag = filter_marker(x, z, age, phase, ID, a1, a2, ntriag)
nmarkers = len(x)
print('Writing record #%d, model time=%.3e, %d markers' % (i, fl.time[i-1], nmarkers), end='\r')
sys.stdout.flush()
fvtp = open('flacmarker.%06d.vtp' % i, 'w')
vtp_header(fvtp, nmarkers, fl.time[i-1], fl.steps[i-1])
# point-based data
fvtp.write(' <PointData>\n')
vts_dataarray(fvtp, age, 'age', 1)
vts_dataarray(fvtp, phase.astype(np.int32), 'phase', 1)
vts_dataarray(fvtp, ID.astype(np.int32), 'ID', 1)
vts_dataarray(fvtp, a1, 'a1', 1)
vts_dataarray(fvtp, a2, 'a2', 1)
vts_dataarray(fvtp, ntriag.astype(np.int32), 'ntriag', 1)
fvtp.write(' </PointData>\n')
# point coordinates
# VTK requires vector field (velocity, coordinate) has 3 components.
# Allocating a 3-vector tmp array for VTK data output.
tmp = np.zeros((nmarkers, 3), dtype=x.dtype)
tmp[:,0] = x
tmp[:,1] = z
fvtp.write(' <Points>\n')
vts_dataarray(fvtp, tmp, '', 3)
fvtp.write(' </Points>\n')
vtp_footer(fvtp)
fvtp.close()
print()
return
def vtp_header(f, npoints, time, step):
f.write(
'''<?xml version="1.0"?>
<VTKFile type="PolyData" version="0.1" byte_order="LittleEndian" compressor="vtkZLibDataCompressor">
<PolyData>
<FieldData>
<DataArray type="Float32" Name="TIME" NumberOfTuples="1" format="ascii">
{1}
</DataArray>
<DataArray type="Float32" Name="CYCLE" NumberOfTuples="1" format="ascii">
{2}
</DataArray>
</FieldData>
<Piece NumberOfPoints="{0}">
'''.format(npoints, time, step))
return
def vtp_footer(f):
f.write(
'''</Piece>
</PolyData>
</VTKFile>
''')
return
if __name__ == '__main__':
doc = '''usage: flacmarker2vtk.py [-f xmin,xmax,zmin,zmax] path [frame_min [frame_max]]
Processing flac marker output to VTK format.
If frame_min is -1, start from the latest vtp file.
If frame_max is not given, processing to latest frames
If both frame_min and frame_max are not given, processing all frames
-f xmin,xmax,zmin,zmax: if provided, only output markers within the domain range (in km)
'''
if len(sys.argv) < 2:
print(doc)
sys.exit(1)
try:
n = 0
if sys.argv[1] == '-f':
filtering = True
xmin, xmax, zmin, zmax = (float(f) for f in sys.argv[2].split(','))
n = 2
path = sys.argv[n+1]
start = 1
end = -1
if len(sys.argv) >= n+3:
start = int(sys.argv[n+2])
if len(sys.argv) >= n+4:
end = int(sys.argv[n+3])
except:
print(doc, '\n')
raise
main(path, start, end)