def postprocess1phase(path, solver=None): """ Postprocesses output directory of a solver. Use it for 1 phase simulations: moving cylinder etc. Looks for all the timesteps, computes and plots kinetic energy, maximum velocity, timesteps. """ path = os.path.abspath(path) if os.path.isdir(path): out_dir = os.path.abspath(path) else: out_dir = os.path.split(path)[0] solver = solver or determineSolver(path) t, Ek, Vmax, Vx_max, P = [], [], [], [], [] rho = 1000 if solver == "TruchasEnSight": file = globFiles(path, ext=".CASE")[0] basename = findBasename(file, solver) dt = findDt(out_dir, basename, solver) vtkFile = vtkextract.vtkFile() vtkFile.solver = solver vtkFile.sim = "3D" ts = 0 for l in open(file).readlines(): if "number of steps" in l: ts = int(l.split()[-1]) print "postprocess1phase: ", ts, " ensight CASE files in", out_dir, ": ", for i in range(ts): print i, sys.stdout.flush() vtkFile.readFile(file, ts=i) # centers = vtkFile.getCenters() vel = vtkFile.getVelocity() u = np.sqrt(vel[:, 0] ** 2 + vel[:, 1] ** 2 + vel[:, 2] ** 2) vol = vtkFile.getVolumes() Ek.append(np.sum(u ** 2 * rho * vol / 2)) Vmax.append(np.max(u)) Vx_max.append(np.max(vel[:, 0])) p = vtkFile.getPressure() P.append(p.max()) if vtkFile.time >= 0: t.append(vtkFile.time) else: t.append(i * dt) print else: files = globFiles(path, ext=".vtk", verbose=False) basename = findBasename(files[0], solver) dt = findDt(out_dir, basename, solver) vtkFile = vtkextract.vtkFile() vtkFile.solver = solver print "postprocess1phase: ", len(files), "files in", out_dir, ": ", basename + ".#.vtk", for i, file in enumerate(files): print i, sys.stdout.flush() vtkFile.readFile(file, verbose=False) # centers = vtkFile.getCenters() vel = vtkFile.getVelocity() u = np.sqrt(vel[:, 0] ** 2 + vel[:, 1] ** 2 + vel[:, 2] ** 2) vol = vtkFile.getVolumes() Ek.append(np.sum(u ** 2 * rho * vol / 2)) Vmax.append(np.max(u)) Vx_max.append(np.max(vel[:, 0])) # p = vtkFile.getPressure() # P.append(p.max()) if vtkFile.time >= 0: t.append(vtkFile.time) else: t.append(i * dt) print t = np.array(t) Ek = np.array(Ek) Vmax = np.array(Vmax) # P = np.array(P) Vx_max = np.array(Vx_max) saveData(out_dir, basename, var=[t, Vmax, Ek, Vx_max], labels=["t", "Vmax", "Ek", "Vx_max"])
def postprocess(path, solver=None): """ Postprocesses output directory of a simulation. Use it for 2 phase simulations: propagation of a solitary wave, run up of a solitary wave, still water test. Extracts data from all VTK files, computes and plots kinetic energy, maximum velocity etc. """ path = os.path.abspath(path) solver = solver or determineSolver(path) test = determineTestCase(path) amp = determineAmplitude(path) files = globFiles(path, ext='.vtk', verbose=False, exit=0) if not files: return 1 basename = findBasename(files[0], solver) out_dir = os.path.abspath(path) if os.path.isdir(path) else os.path.split( path)[0] dt = findDt(out_dir, basename, solver) vtkFile = vtkextract.vtkFile() vtkFile.solver = solver Ek, Ep, Vmax, max_x, max_z, max_z_largest, mass, mass_wave = [], [], [], [], [], [], [], [] variable_names = [ 't', 'center_full_width', 'full_width', 'center_max_height' ] data = {name: [] for name in variable_names} print 'postprocess: ', len( files), 'files in', out_dir, ': ', basename + '.#.vtk', for i, file in enumerate(files): print i, sys.stdout.flush() vtkFile.readFile(file, verbose=False) vel = vtkFile.getVelocity() u = np.sqrt(vel[:, 0]**2 + vel[:, 1]**2 + vel[:, 2]**2) vol = vtkFile.getVolumes() #ipdb.set_trace() water = vtkFile.getWaterFraction() rho = water * 1000 + (1 - water) * 1 Ekk = u**2 * rho * vol / 2 Ek.append(np.sum(Ekk[water > 0.005])) Vmax.append(np.max(u * water)) mass.append(np.sum(vol * rho)) mass_wave.append( (np.sum(vol * water) - np.max(vtkFile.bounds[:, 1])) * 1000) if vtkFile.time >= 0: data['t'].append(vtkFile.time) else: data['t'].append(i * dt) contour = vtkFile.getContour() #sorting the x values (will spoil the results for complicated shapes, but works fine for propagation and run up: arg = np.argsort(contour[:, 0], axis=0) contour = contour[arg] contour = contour[contour[:, 2] >= -0.05] maxWaterLevel = np.max(contour[:, 2]) centerX = np.mean(contour[contour[:, 2] == maxWaterLevel, 0]) max_x.append(centerX) max_z.append(maxWaterLevel) data['center_max_height'].append(centerX) contour = vtkFile.getContour(largestRegion=True) arg = np.argsort(contour[:, 0], axis=0) contour = contour[arg] contour = contour[contour[:, 2] >= -0.05] max_z_largest.append(np.max(contour[:, 2])) #calculation of potential energy - integration of the interface dx = contour[1:, 0] - contour[:-1, 0] height = (contour[:-1, 2] + contour[1:, 2]) / 2 Ep1 = height * height * dx * 1000 * g / 2 Ep1 = np.sum(Ep1) Ep.append(Ep1) if 'propagation' in path.lower(): #calculation of the full width at half maximum #making the contour 1D: indices = np.unique(contour[:, 0], return_index=True)[1] contour = contour[indices] contour = contour[:, [0, 2]] f = scipy.interpolate.interp1d(contour[:, 0], contour[:, 1] - float(amp) / 2, bounds_error=False, fill_value=0) a = scipy.optimize.brentq(f, np.min(contour[:, 0]), centerX) b = scipy.optimize.brentq(f, centerX, np.max(contour[:, 0])) data['full_width'].append(b - a) data['center_full_width'].append((a + b) / 2) print #for Gerris, the centers and bounds are moved by 1 in z direction by vtkextract.py saveData(out_dir, basename, var=[ data['t'], Ek, Ep, mass, mass_wave, Vmax, max_x, max_z_largest, max_z ], labels=[ 't', 'Ek', 'Ep', 'mass', 'massWave', 'maxVel', 'Xmax', 'Zmax_largest', 'Zmax' ], data_sep_files=data) print "Plotting..." title = '{test} - {{}} ({solver})'.format(test=test, solver=solver) maker = plotMaker(data['t'], basename + ".{name}", ext=['png', 'pdf'], xlabel='Time $[s]$', a=determineAmplitude(path), out_dir=out_dir, slope=determineSlope(path), title=title) maker.plotEvolution( Ek, 'EK', name_long='kinetic energy', ylabel='Kinetic energy $[J]$', ) maker.plotEvolution( Ep, 'EP', name_long='potential energy', ylabel='Potential energy $[J]$', ) maker.plotEvolution( Vmax, 'MaxVel', name_long='maximum velocity', ylabel='Velocity $[\\frac{m}{s}]$', ) runup = True if 'run up' in test.lower() else False maker.plotEvolution(max_z, 'Height-multi', name_long='maximum height', ylabel='Height $[m]$', reference=runup) maker.plotEvolution(max_z_largest, 'Height', name_long='maximum height of largest region', ylabel='Height $[m]$', reference=runup)
def postprocess(path, solver=None): """ Postprocesses output directory of a simulation. Use it for 2 phase simulations: propagation of a solitary wave, run up of a solitary wave, still water test. Extracts data from all VTK files, computes and plots kinetic energy, maximum velocity etc. """ path = os.path.abspath(path) solver = solver or determineSolver(path) test = determineTestCase(path) amp = determineAmplitude(path) files = globFiles(path, ext=".vtk", verbose=False, exit=0) if not files: return 1 basename = findBasename(files[0], solver) out_dir = os.path.abspath(path) if os.path.isdir(path) else os.path.split(path)[0] dt = findDt(out_dir, basename, solver) vtkFile = vtkextract.vtkFile() vtkFile.solver = solver Ek, Ep, Vmax, max_x, max_z, max_z_largest, mass, mass_wave = [], [], [], [], [], [], [], [] variable_names = ["t", "center_full_width", "full_width", "center_max_height"] data = {name: [] for name in variable_names} print "postprocess: ", len(files), "files in", out_dir, ": ", basename + ".#.vtk", for i, file in enumerate(files): print i, sys.stdout.flush() vtkFile.readFile(file, verbose=False) vel = vtkFile.getVelocity() u = np.sqrt(vel[:, 0] ** 2 + vel[:, 1] ** 2 + vel[:, 2] ** 2) vol = vtkFile.getVolumes() # ipdb.set_trace() water = vtkFile.getWaterFraction() rho = water * 1000 + (1 - water) * 1 Ekk = u ** 2 * rho * vol / 2 Ek.append(np.sum(Ekk[water > 0.005])) Vmax.append(np.max(u * water)) mass.append(np.sum(vol * rho)) mass_wave.append((np.sum(vol * water) - np.max(vtkFile.bounds[:, 1])) * 1000) if vtkFile.time >= 0: data["t"].append(vtkFile.time) else: data["t"].append(i * dt) contour = vtkFile.getContour() # sorting the x values (will spoil the results for complicated shapes, but works fine for propagation and run up: arg = np.argsort(contour[:, 0], axis=0) contour = contour[arg] contour = contour[contour[:, 2] >= -0.05] maxWaterLevel = np.max(contour[:, 2]) centerX = np.mean(contour[contour[:, 2] == maxWaterLevel, 0]) max_x.append(centerX) max_z.append(maxWaterLevel) data["center_max_height"].append(centerX) contour = vtkFile.getContour(largestRegion=True) arg = np.argsort(contour[:, 0], axis=0) contour = contour[arg] contour = contour[contour[:, 2] >= -0.05] max_z_largest.append(np.max(contour[:, 2])) # calculation of potential energy - integration of the interface dx = contour[1:, 0] - contour[:-1, 0] height = (contour[:-1, 2] + contour[1:, 2]) / 2 Ep1 = height * height * dx * 1000 * g / 2 Ep1 = np.sum(Ep1) Ep.append(Ep1) if "propagation" in path.lower(): # calculation of the full width at half maximum # making the contour 1D: indices = np.unique(contour[:, 0], return_index=True)[1] contour = contour[indices] contour = contour[:, [0, 2]] f = scipy.interpolate.interp1d( contour[:, 0], contour[:, 1] - float(amp) / 2, bounds_error=False, fill_value=0 ) a = scipy.optimize.brentq(f, np.min(contour[:, 0]), centerX) b = scipy.optimize.brentq(f, centerX, np.max(contour[:, 0])) data["full_width"].append(b - a) data["center_full_width"].append((a + b) / 2) print # for Gerris, the centers and bounds are moved by 1 in z direction by vtkextract.py saveData( out_dir, basename, var=[data["t"], Ek, Ep, mass, mass_wave, Vmax, max_x, max_z_largest, max_z], labels=["t", "Ek", "Ep", "mass", "massWave", "maxVel", "Xmax", "Zmax_largest", "Zmax"], data_sep_files=data, ) print "Plotting..." title = "{test} - {{}} ({solver})".format(test=test, solver=solver) maker = plotMaker( data["t"], basename + ".{name}", ext=["png", "pdf"], xlabel="Time $[s]$", a=determineAmplitude(path), out_dir=out_dir, slope=determineSlope(path), title=title, ) maker.plotEvolution(Ek, "EK", name_long="kinetic energy", ylabel="Kinetic energy $[J]$") maker.plotEvolution(Ep, "EP", name_long="potential energy", ylabel="Potential energy $[J]$") maker.plotEvolution(Vmax, "MaxVel", name_long="maximum velocity", ylabel="Velocity $[\\frac{m}{s}]$") runup = True if "run up" in test.lower() else False maker.plotEvolution(max_z, "Height-multi", name_long="maximum height", ylabel="Height $[m]$", reference=runup) maker.plotEvolution( max_z_largest, "Height", name_long="maximum height of largest region", ylabel="Height $[m]$", reference=runup )
def postprocess1phase(path, solver=None): """ Postprocesses output directory of a solver. Use it for 1 phase simulations: moving cylinder etc. Looks for all the timesteps, computes and plots kinetic energy, maximum velocity, timesteps. """ path = os.path.abspath(path) if os.path.isdir(path): out_dir = os.path.abspath(path) else: out_dir = os.path.split(path)[0] solver = solver or determineSolver(path) t, Ek, Vmax, Vx_max, P = [], [], [], [], [] rho = 1000 if solver == 'TruchasEnSight': file = globFiles(path, ext='.CASE')[0] basename = findBasename(file, solver) dt = findDt(out_dir, basename, solver) vtkFile = vtkextract.vtkFile() vtkFile.solver = solver vtkFile.sim = '3D' ts = 0 for l in open(file).readlines(): if 'number of steps' in l: ts = int(l.split()[-1]) print 'postprocess1phase: ', ts, ' ensight CASE files in', out_dir, ': ', for i in range(ts): print i, sys.stdout.flush() vtkFile.readFile(file, ts=i) #centers = vtkFile.getCenters() vel = vtkFile.getVelocity() u = np.sqrt(vel[:, 0]**2 + vel[:, 1]**2 + vel[:, 2]**2) vol = vtkFile.getVolumes() Ek.append(np.sum(u**2 * rho * vol / 2)) Vmax.append(np.max(u)) Vx_max.append(np.max(vel[:, 0])) p = vtkFile.getPressure() P.append(p.max()) if vtkFile.time >= 0: t.append(vtkFile.time) else: t.append(i * dt) print else: files = globFiles(path, ext='.vtk', verbose=False) basename = findBasename(files[0], solver) dt = findDt(out_dir, basename, solver) vtkFile = vtkextract.vtkFile() vtkFile.solver = solver print 'postprocess1phase: ', len( files), 'files in', out_dir, ': ', basename + '.#.vtk', for i, file in enumerate(files): print i, sys.stdout.flush() vtkFile.readFile(file, verbose=False) #centers = vtkFile.getCenters() vel = vtkFile.getVelocity() u = np.sqrt(vel[:, 0]**2 + vel[:, 1]**2 + vel[:, 2]**2) vol = vtkFile.getVolumes() Ek.append(np.sum(u**2 * rho * vol / 2)) Vmax.append(np.max(u)) Vx_max.append(np.max(vel[:, 0])) #p = vtkFile.getPressure() #P.append(p.max()) if vtkFile.time >= 0: t.append(vtkFile.time) else: t.append(i * dt) print t = np.array(t) Ek = np.array(Ek) Vmax = np.array(Vmax) #P = np.array(P) Vx_max = np.array(Vx_max) saveData(out_dir, basename, var=[t, Vmax, Ek, Vx_max], labels=['t', 'Vmax', 'Ek', 'Vx_max'])