@@ -88,7 +88,8 @@ def save_csv_file(
88
88
if isinstance (data , paddle .Tensor ):
89
89
data = data .numpy () # [num_of_samples, ]
90
90
91
- data = data .flatten ()
91
+ if isinstance (data , np .ndarray ):
92
+ data = data .flatten ()
92
93
data_fields .append (data )
93
94
94
95
header .append (key )
@@ -111,58 +112,62 @@ def save_tecplot_file(
111
112
filename : str ,
112
113
data_dict : Dict [str , Union [np .ndarray , "paddle.Tensor" ]],
113
114
keys : Tuple [str , ...],
115
+ num_x : int ,
116
+ num_y : int ,
114
117
alias_dict : Optional [Dict [str , str ]] = None ,
115
118
delimiter : str = " " ,
116
119
encoding : str = "utf-8" ,
117
120
num_timestamps : int = 1 ,
118
121
):
119
- """Write numpy data to tecplot file.
122
+ """Write numpy or tensor data to tecplot file.
120
123
121
124
Args:
122
125
filename (str): Tecplot file path.
123
126
data_dict (Dict[str, Union[np.ndarray, paddle.Tensor]]): Numpy or Tensor data in dict.
124
127
keys (Tuple[str, ...]): Target keys to be dumped.
128
+ num_x (int): The number of discrete points of the grid in the X-axis. Assuming
129
+ the discrete grid size is 20 x 30, then num_x=20.
130
+ num_y (int): The number of discrete points of the grid in the Y-axis. Assuming
131
+ the discrete grid size is 20 x 30, then num_y=30.
125
132
alias_dict (Optional[Dict[str, str]], optional): Alias dict for keys,
126
133
i.e. {dump_key: dict_key}. Defaults to None.
127
- delimiter (str, optional): Delemiter for splitting different data field. Defaults to ", ".
134
+ delimiter (str, optional): Delemiter for splitting different data field. Defaults to " ".
128
135
encoding (str, optional): Encoding. Defaults to "utf-8".
129
136
num_timestamps (int, optional): Number of timestamp over coord and value. Defaults to 1.
130
137
131
138
Examples:
132
139
>>> import numpy as np
133
140
>>> from ppsci.utils import save_tecplot_file
134
141
>>> data_dict = {
135
- ... "x": np.array([[1.0], [2.0], [10.0], [20.0], [100.0], [200.0]]), # [6, 1]
136
- ... "y": np.array([[-1.0], [-2.0], [-10.0], [-20.0], [-100.0], [-200.0]]) # [6, 1]
142
+ ... "x": np.array([[-1.0], [-1.0], [-1.0], [-1.0], [-1.0], [-1.0]]), # [6, 1]
143
+ ... "y": np.array([[1.0], [2.0], [3.0], [1.0], [2.0], [3.0]]), # [6, 1]
144
+ ... "value": np.array([[3], [33], [333], [3333], [33333], [333333]]), # [6, 1]
137
145
... }
138
146
>>> save_tecplot_file(
139
- ... "./test.tec ",
147
+ ... "./test.dat ",
140
148
... data_dict,
141
149
... ("X", "Y"),
150
+ ... num_x=1,
151
+ ... num_y=3,
142
152
... alias_dict={"X": "x", "Y": "y"},
143
- ... num_timestamps=3 ,
153
+ ... num_timestamps=2 ,
144
154
... )
145
-
146
- >>> # == test_t-0.tec ==
147
- >>> # title="./test_t-0.tec"
155
+ >>> # == test_t-0.dat ==
156
+ >>> # title = "./test_t-0.dat"
148
157
>>> # variables = "X", "Y"
149
- >>> # Zone I = 2 , J = 1, F = POINT
158
+ >>> # Zone I = 3 , J = 1, F = POINT
150
159
>>> # -1.0 1.0
151
- >>> # -2.0 2.0
160
+ >>> # -1.0 2.0
161
+ >>> # -1.0 3.0
152
162
153
- >>> # == test_t-1.tec ==
154
- >>> # title="./test_t-1.tec"
155
- >>> # variables = "X", "Y"
156
- >>> # Zone I = 2, J = 1, F = POINT
157
- >>> # -10.0 10.0
158
- >>> # -20.0 20.0
159
163
160
- >>> # == test_t-2.tec ==
161
- >>> # title= "./test_t-2.tec "
164
+ >>> # == test_t-1.dat ==
165
+ >>> # title = "./test_t-1.dat "
162
166
>>> # variables = "X", "Y"
163
- >>> # Zone I = 2, J = 1, F = POINT
164
- >>> # -100.0 100.0
165
- >>> # -200.0 200.0
167
+ >>> # Zone I = 3, J = 1, F = POINT
168
+ >>> # -1.0 1.0
169
+ >>> # -1.0 2.0
170
+ >>> # -1.0 3.0
166
171
"""
167
172
ntxy = len (next (iter (data_dict .values ())))
168
173
if ntxy % num_timestamps != 0 :
@@ -172,25 +177,31 @@ def save_tecplot_file(
172
177
)
173
178
nxy = ntxy // num_timestamps
174
179
180
+ nx , ny = num_x , num_y
181
+ assert nx * ny == nxy , f"nx({ nx } ) * ny({ ny } ) != nxy({ nxy } )"
182
+
175
183
os .makedirs (os .path .dirname (filename ), exist_ok = True )
176
184
177
- if filename .endswith (".tec " ):
185
+ if filename .endswith (".dat " ):
178
186
filename = filename [:- 4 ]
179
187
180
188
for t in range (num_timestamps ):
181
189
# write 1 tecplot file for each timestep
182
190
if num_timestamps > 1 :
183
- dump_filename = f"{ filename } _t-{ t } .tec "
191
+ dump_filename = f"{ filename } _t-{ t } .dat "
184
192
else :
185
- dump_filename = f"{ filename } .tec "
193
+ dump_filename = f"{ filename } .dat "
186
194
195
+ fetch_keys = [(alias_dict [key ] if alias_dict else key ) for key in keys ]
187
196
with open (dump_filename , "w" , encoding = encoding ) as f :
188
197
# write meta information of tec
189
- f .write (f'title="{ dump_filename } "\n ' )
190
- fetch_keys = {(alias_dict [key ] if alias_dict else key ) for key in keys }
198
+ f .write (f'title = "{ dump_filename } "\n ' )
191
199
header = ", " .join ([f'"{ key } "' for key in keys ])
192
200
f .write (f"variables = { header } \n " )
193
- f .write (f"Zone I = { nxy } , J = 1, F = POINT\n " )
201
+
202
+ # NOTE: Tecplot is column-major, so we need to specify I = ny, J = nx,
203
+ # which is in contrast to our habits.
204
+ f .write (f"Zone I = { ny } , J = { nx } , F = POINT\n " )
194
205
195
206
# write points data into file
196
207
data_cur_time_step = [
@@ -200,4 +211,9 @@ def save_tecplot_file(
200
211
for items in zip (* data_cur_time_step ):
201
212
f .write (delimiter .join ([str (float (x )) for x in items ]) + "\n " )
202
213
203
- logger .message (f"csv file has been dumped to { dump_filename } " )
214
+ if num_timestamps > 1 :
215
+ logger .message (
216
+ f"tecplot files are saved to: { filename } _t-0.dat ~ { filename } _t-{ num_timestamps - 1 } .dat"
217
+ )
218
+ else :
219
+ logger .message (f"tecplot file is saved to: { filename } .dat" )
0 commit comments