-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostprocess.py
More file actions
163 lines (134 loc) · 4.25 KB
/
Copy pathpostprocess.py
File metadata and controls
163 lines (134 loc) · 4.25 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
import sys
import xarray as xr
import numpy as np
from h5py import File
from os.path import basename, dirname, splitext
# def convert_exo_to_h5(moose_file_base, h5_file):
# print(f"Reading from {moose_file_base} and writing to {h5_file}")
# def gd(name):
# # 1-based indexing
# ind = field_names.index(name) + 1
# # get elem var field to read
# print(name, ind, f"vals_elem_var{ind}eb1")
# data = ds.get(f"vals_elem_var{ind}eb1").astype(np.float32)[:]
# # print("datashape", data.shape)
# # take data from last timestep
# data = data[-1]
# # print(data[1])
# return data
# def get_tensor_data(base):
# # array of size (6, x, y, z)
# return np.stack(
# [
# gd(f"{base}_00"),
# gd(f"{base}_11"),
# gd(f"{base}_22"),
# gd(f"{base}_12"),
# gd(f"{base}_02"),
# gd(f"{base}_12"),
# ],
# axis=0,
# )
# strain = get_tensor_data("total_strain")
# stress = get_tensor_data("small_stress")
# # total num voxels
# # TODO just read/write this manually with moose
# VX = round(strain.shape[-1] ** (1 / 3))
# print(VX)
# print(strain.shape, stress.shape)
# # make sure we reshape space in advance
# strain = strain.reshape(6, VX, VX, VX)
# stress = stress.reshape(6, VX, VX, VX)
# print(strain.shape, stress.shape)
# # each channel is one chunk (for all x, y, z)
# chunk_size = (1,) + strain[0].shape
# print("chunk size is", chunk_size)
# print(strain.dtype, strain.shape)
# print(stress.dtype, stress.shape)
# # write to hdf5 file
# output_f = File(h5_file, "w")
# # now make the actual datasets
# output_f.create_dataset(
# "strain",
# data=strain,
# dtype=strain.dtype,
# compression="gzip",
# compression_opts=4,
# shuffle=True,
# chunks=chunk_size,
# )
# output_f.create_dataset(
# "stress",
# data=stress,
# dtype=stress.dtype,
# compression="gzip",
# compression_opts=4,
# shuffle=True,
# chunks=chunk_size,
# )
def convert_csv_to_h5(input_file, output_file):
def get_field(np_file, base):
# get all components of a field from a given file
# (e.g. "strain" collects strain_xx, strain_xy, ....) in voigt order
return np.stack(
[
np_file[f"{base}_xx"],
np_file[f"{base}_yy"],
np_file[f"{base}_zz"],
np_file[f"{base}_yz"],
np_file[f"{base}_xz"],
np_file[f"{base}_xy"],
],
axis=0,
)
f = np.genfromtxt(
input_file,
names=True,
delimiter=",",
)
print(f)
strain = get_field(f, "strain")
stress = get_field(f, "stress")
# total num voxels
# TODO just read/write this manually with moose
VX = round(strain.shape[-1] ** (1 / 3))
print(VX)
print(strain.shape, stress.shape)
# make sure we reshape space in advance
strain = strain.reshape(1, 6, VX, VX, VX)
stress = stress.reshape(1, 6, VX, VX, VX)
print(strain.shape, stress.shape)
# one chunk is one instance (strain/stress)
chunk_size = (1, 6) + strain.shape[-3:]
print("chunk size is", chunk_size)
print(strain.dtype, strain.shape)
print(stress.dtype, stress.shape)
# write to hdf5 file
output_f = File(output_file, "w")
# now make the actual datasets
output_f.create_dataset(
"strain",
data=strain,
dtype=strain.dtype,
compression="gzip",
compression_opts=4,
shuffle=True,
chunks=chunk_size,
)
output_f.create_dataset(
"stress",
data=stress,
dtype=stress.dtype,
compression="gzip",
compression_opts=4,
shuffle=True,
chunks=chunk_size,
)
if __name__ == "__main__":
input_file = sys.argv[1]
if len(sys.argv) > 2:
output_file = sys.argv[2]
else:
basedir = dirname(input_file)
output_file = f"{basedir}/{splitext(basename(input_file))[0]}.h5"
convert_csv_to_h5(input_file, output_file)