|
| 1 | +import atexit |
| 2 | +import glob |
| 3 | +import os |
| 4 | +import multiprocessing |
| 5 | +import re |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from scipy import interpolate, optimize, stats |
| 9 | + |
| 10 | +import tecplot as tp |
| 11 | +from tecplot.constant import * |
| 12 | + |
| 13 | +def init(): |
| 14 | + # !!! IMPORTANT !!! |
| 15 | + # Must register stop at exit to ensure Tecplot cleans |
| 16 | + # up all temporary files and does not create a core dump |
| 17 | + atexit.register(tp.session.stop) |
| 18 | + |
| 19 | +def work(datafile): |
| 20 | + # file name includes input hot water velocity and temperature |
| 21 | + match = re.search(r'Z(\d+)ZT(\d+)', datafile) |
| 22 | + Z, ZT = int(match.group(1)), int(match.group(2)) |
| 23 | + |
| 24 | + # load data and create a slice downstream of the tee of the pipe |
| 25 | + tp.new_layout() |
| 26 | + tp.data.load_tecplot(datafile) |
| 27 | + pipe_exit = tp.data.extract.extract_slice((0, 0.2, 0), (0, 1, 0)) |
| 28 | + |
| 29 | + # get temperature on this slice |
| 30 | + t = pipe_exit.values('Temperature')[:] |
| 31 | + |
| 32 | + # return input velocity, intput temperature |
| 33 | + # and the stddev of the output temperature |
| 34 | + return Z, ZT, np.std(t) |
| 35 | + |
| 36 | +if __name__ == '__main__': |
| 37 | + # !!! IMPORTANT !!! |
| 38 | + # On Linux systems, Python's multiprocessing start method |
| 39 | + # defaults to "fork" which is incompatible with PyTecplot |
| 40 | + # and must be set to "spawn" |
| 41 | + multiprocessing.set_start_method('spawn') |
| 42 | + |
| 43 | + # Get the datafiles |
| 44 | + files = glob.glob('hotwatermixing/HotWaterMixing_Y05YT10*.plt') |
| 45 | + |
| 46 | + # Set up the pool with initializing function and associated arguments |
| 47 | + num_workers = min(multiprocessing.cpu_count(), len(files)) |
| 48 | + pool = multiprocessing.Pool(num_workers, initializer=init) |
| 49 | + |
| 50 | + try: |
| 51 | + if not os.path.exists('images'): |
| 52 | + os.makedirs('images') |
| 53 | + |
| 54 | + # Map the work function to each of the job arguments |
| 55 | + results = pool.map(work, files) |
| 56 | + finally: |
| 57 | + # !!! IMPORTANT !!! |
| 58 | + # Must join the process pool before parent script exits |
| 59 | + # to ensure Tecplot cleans up all temporary files |
| 60 | + # and does not create a core dump |
| 61 | + pool.close() |
| 62 | + pool.join() |
| 63 | + |
| 64 | + # unpack results |
| 65 | + v_in, t_in, std_t_out = zip(*results) |
| 66 | + |
| 67 | + # create a grid onto which we will interpolate the results |
| 68 | + vv, tt = np.meshgrid(v_in, t_in) |
| 69 | + std_t_out_interp = interpolate.griddata((v_in, t_in), std_t_out, (vv, tt)) |
| 70 | + |
| 71 | + # create a new frame and dataset to hold the interpolated data in Tecplot |
| 72 | + frame = tp.active_page().add_frame() |
| 73 | + dataset = frame.create_dataset('Data', ['Input Velocity', |
| 74 | + 'Input Temperature', |
| 75 | + 'stddev(Output Temperature)']) |
| 76 | + zone = dataset.add_ordered_zone('Zone', std_t_out_interp.shape[::-1]) |
| 77 | + |
| 78 | + # push the actual data into Tecplot |
| 79 | + zone.values('Input Velocity')[:] = vv |
| 80 | + zone.values('Input Temperature')[:] = tt |
| 81 | + zone.values('stddev(Output Temperature)')[:] = std_t_out_interp |
| 82 | + |
| 83 | + # plot results |
| 84 | + plot = frame.plot(PlotType.Cartesian2D) |
| 85 | + plot.activate() |
| 86 | + plot.show_contour = True |
| 87 | + |
| 88 | + plot.axes.axis_mode = AxisMode.Independent |
| 89 | + plot.view.fit_data() |
| 90 | + |
| 91 | + plot.contour(0).levels.reset_to_nice(20) |
| 92 | + plot.contour(0).legend.auto_resize = True |
| 93 | + |
| 94 | + tp.save_png('hotwater_std_t.png') |
| 95 | + |
0 commit comments