def sample_vtk(file, p1=None, p2=None, x_c=0, N_points=101, solver=None, verbose=True): """ Samples velocity field in a given VTK file, along a line defined with p1 and p2, or x_c. :param file: Path to the VTK file :type file: str :param p1: Coordinates of the starting point of the sample line. Can be either 2D or 3D [x, y] or [x, y, z] :type p1: list :param p2: Coordinates of the ending point of the sample line. Can be either 2D or 3D [x, y] or [x, y, z] :type p2: list :param x_c: If p1 and p2 are not provided, this parameter serves the x coordinate of the vertical line. :type x_c: float :param N_points: The number of points to sample the velocity in. :type N_points: int """ if verbose: print 'Sampling a VTK file: {}'.format(file) solver = solver or postprocess.determineSolver(file) if type(file) == str: vtkFile = vtkextract.vtkFile() vtkFile.solver = solver vtkFile.readFile(file, verbose=False) else: vtkFile = file centers = vtkFile.getCenters() vtkFile.getVolumes() bounds = vtkFile.bounds p1 = p1 or [x_c, 0, -1] p2 = p2 or [x_c, 0, np.max(bounds[:, 4:])] if len(p1) == 2: p1.insert(1, 0) #if 2D data is provided if len(p2) == 2: p2.insert(1, 0) line = vtk.vtkLineSource() line.SetResolution(N_points - 1) line.SetPoint1(p1) line.SetPoint2(p2) probe = vtk.vtkProbeFilter() probe.SetInputConnection(line.GetOutputPort()) probe.SetSourceConnection(vtkFile.outputPort) probe.Update() probe = probe.GetOutput() vel = probe.GetPointData().GetArray(vtkFile.velArray[solver]) vel = np.array([vel.GetTuple(j) for j in range(probe.GetNumberOfPoints())]) line = np.array( [probe.GetPoint(j) for j in range(probe.GetNumberOfPoints())]) return line, vel
def sample_vtk(file, p1=None, p2=None, x_c=0, N_points=101, solver=None, verbose=True): """ Samples velocity field in a given VTK file, along a line defined with p1 and p2, or x_c. :param file: Path to the VTK file :type file: str :param p1: Coordinates of the starting point of the sample line. Can be either 2D or 3D [x, y] or [x, y, z] :type p1: list :param p2: Coordinates of the ending point of the sample line. Can be either 2D or 3D [x, y] or [x, y, z] :type p2: list :param x_c: If p1 and p2 are not provided, this parameter serves the x coordinate of the vertical line. :type x_c: float :param N_points: The number of points to sample the velocity in. :type N_points: int """ if verbose: print 'Sampling a VTK file: {}'.format(file) solver=solver or postprocess.determineSolver(file) if type(file) == str: vtkFile = vtkextract.vtkFile() vtkFile.solver = solver vtkFile.readFile(file, verbose=False) else: vtkFile = file centers = vtkFile.getCenters() vtkFile.getVolumes() bounds = vtkFile.bounds p1 = p1 or [x_c, 0, -1] p2 = p2 or [x_c, 0, np.max(bounds[:, 4:])] if len(p1) == 2: p1.insert(1, 0) #if 2D data is provided if len(p2) == 2: p2.insert(1, 0) line = vtk.vtkLineSource() line.SetResolution(N_points - 1) line.SetPoint1(p1) line.SetPoint2(p2) probe = vtk.vtkProbeFilter() probe.SetInputConnection(line.GetOutputPort()) probe.SetSourceConnection(vtkFile.outputPort) probe.Update() probe = probe.GetOutput() vel=probe.GetPointData().GetArray(vtkFile.velArray[solver]) vel=np.array([vel.GetTuple(j) for j in range(probe.GetNumberOfPoints())]) line=np.array([probe.GetPoint(j) for j in range(probe.GetNumberOfPoints())]) return line, vel
def determineMesh(): solver = pproc.determineSolver() if solver == 'Gerris': lines = open(glob.glob('*.gfs')[0]).readlines() sx = 1 #FINISHED, BUT I COULD MOVE l = re.split(...) TO THE BEGINNING, TO MAKE IT CLEANER for l in lines: if 'GfsSimulation' in l: NBoxes = int(l.split()[0]) if 'PhysicalParams' in l: H = float(re.split('[ =,{}]+',l.strip()) [2]) if 'MetricStretch' in l and not '#' in l: sx = float(re.split('[ =,{}]+',l.strip()) [2]) l = l.split() if len(l)==2 and l[0] in ['GfsRefine', 'Refine'] and l[1].isdigit(): refine = int(l[1]) if len(l)==3 and l[0] == 'Define' and l[1]== 'REFLEVEL': refine = int(l[2]) Nx = NBoxes * 2**refine Ny = 2**refine L = H*sx*NBoxes elif solver == 'Truchas': lines = open(glob.glob('*.inp')[0]).readlines() for l in lines: l = filter(None, re.split('[ =,{}\n]+',l)) if 'Ncell' in l: Nx = int(l[1]) Ny = int(l[2]) if 'Coord' in l: L = float(l[4]) - float(l[1]) H = float(l[5]) - float(l[2]) elif solver == 'Thetis': H = 0 L = 0 lines = open(glob.glob('*[0-9]*.don')[0]).readlines() for l in lines: l = re.split('[ ]+',l.strip()) if len(l)==3 and l[0] == 'MAILLAGE': Nx = int(l[1]) Ny = int(l[2]) if 'DIM_MAX' in l: L = L + float(l[1].replace('D0','')) H = H + float(l[2].replace('D0','')) if 'DIM_MIN' in l: L = L - float(l[1].replace('D0','')) H = H - float(l[2].replace('D0','')) return L,H,Nx,Ny
def initialize(a=None, solitonPosition=15): print 'Initialization of a solitary wave from numerical data' sys.stdout.flush() solver = pproc.determineSolver() a = a or pproc.determineAmplitude() refDir = os.path.join(referenceDirectoryMain,str(a)) if not os.path.exists(refDir): print 'No data for given amplitude found. Amplitude provided:',a,', available data:' for d in sorted(glob.glob(os.path.join(refDir,'../0.*'))): print d sys.exit() surf = np.loadtxt(os.path.join(refDir,'surf')) surf[:,0] = surf[:,0] + solitonPosition surf = np.vstack((np.array([-200,0.]),surf,np.array([200,0]))) #if the left side is inside the domain, vofOfManyPolygons has problems np.savetxt('interface.ini',surf,fmt='%10.6f') if solver == 'Gerris': initializeGerris(refDir) elif solver == 'OpenFOAM': initializeOpenFOAM(refDir) elif solver == 'Truchas': initializeTruchas(refDir) elif solver == 'Thetis': initializeThetis(refDir)
def cylinder(path = './', solver=None, cs='cfr'): if len(cs) > 1: for cs0 in cs: cylinder(path, solver, cs0) return path = os.path.abspath(path) solver = solver or postprocess.determineSolver(path) if solver == 'TruchasEnSight': solver = 'Truchas' U=1. R=0.05 if solver == 'Gerris': gfsFile=glob.glob(os.path.join(path, '*.gfs'))[0] for l in open(gfsFile).readlines(): if 'SurfaceBc U' in l: U = -float(l.split()[-1]) if 'SolidMoving' in l: for i, w in enumerate(l.split()): if w == 'scale': R = 0.25 * float(l.split()[i+2]) if 'Define R' in l: R = float(l.split()[-1]) elif solver == 'Truchas': inpFile=glob.glob(os.path.join(path, '*.inp'))[0] for l in open(inpFile).readlines(): l = l.split() if len(l) ==3 and l[0] == 'R' and l[1] == '=': R = float(l[2]) elif solver == 'OpenFOAM': inpFile=glob.glob(os.path.join(path, 'system/snappyHexMeshDict'))[0] for l in open(inpFile).readlines(): l = (l.replace(';', '')).split() if len(l) == 2 and l[0] == 'radius': R = float(l[1]) position = { 'c':0, 'f' : -2*R, 'r' : 2*R } y0 = { 'c':R, 'f' : 0, 'r' : 0 } cs_name={ 'c':'center', 'f' : 'front', 'r' : 'rear'} fig = plt.figure() fig.subplots_adjust(0.15, 0.15, 0.95, None) if 'static' in path.lower(): state = 'static' else: state = 'moving' if 'slip' in path: BC = 'slip' else: BC = 'outflow' plt.title('Cylinder - velocity profiles (' + ', '.join([solver, state, BC, cs_name[cs]]) + ')') r = np.linspace(y0[cs], 0.5, 100) ls=['-.', '--', '-', '-', '-'] for i, caseDir in enumerate(sorted(glob.glob(os.path.join(path, '*[1-9]')))[:5]): vtks = postprocess.globFiles(caseDir, ext='.vtk', exit=0) if vtks: print 'Profiles:', caseDir, '*.vtk' line, u = sample_vtk(vtks[-1], [position[cs], y0[cs]], [position[cs], 0.5]) if 'moving' in path: u = u+U #we want relative speed plt.plot(u[:, 0], line[:, 2], label='Resolution '+caseDir[-1], ls=ls[i]) files = glob.glob(os.path.join(caseDir, '*.ensight.CASE')) if files: file = files[0] print 'Profiles:', file ts = enSightTimeSteps(file) if len(glob.glob(os.path.join(caseDir, '*.ensight/P.*'))) == ts: #check if computations are finished line, u = sample_ensight(file, [position[cs], y0[cs]], [position[cs], 0.5]) if 'moving' in path: u = u+U #we want relative speed plt.plot(u[:, 0], line[:, 2], label='Resolution '+caseDir[-1], ls=ls[i]) Vx, Vy = cylinderReference(position[cs], r, R) #plt.plot(U*(1+R**2/r**2), r, label='Reference', lw=2) plt.plot(Vx, r, label='Reference', c='k', lw=2.0) legend = { 'c':'upper right', 'f' : 'upper left', 'r' : 'upper left' } plt.legend(loc=legend[cs]) #plt.ylim([0, line[:, 2].max()]) #plt.ylim([0, 0.15]) if cs == 'c':plt.xlim([1, 2]) plt.ylabel('z $[m]$') plt.xlabel('Velocity $u_x$ $[\\frac{m}{s}]$') #name = os.path.split(path)[1] #ex. cylinder-static, cylinder-moving plt.savefig('-'.join(['cylinder', solver, 'profiles', state, BC, cs])) plt.savefig('-'.join(['cylinder', solver, 'profiles', state, BC, cs])+'.pdf')
def plotCrossSections(path, time=None, a=None, solver=None, x_c=25, bw=0, suffix='', difference=False): """ Plots the velocity profiles along vertical crossections of a solitary wave, for a single time step. In previous versions multiple timesteps were supported, but is not any more. :param difference: Take a difference between the reference solution and numerical results :type difference: bool """ solver = solver or postprocess.determineSolver(path) a = a or postprocess.determineAmplitude(path) path=os.path.abspath(path) dirs = glob.glob(path + '/case[1-4]/*postprocessed.dat') + glob.glob(path + '/*-[1-4]/*postprocessed.dat') dirs = [os.path.split(dir)[0] for dir in dirs] dirs.sort() if not (dirs): print 'Empty directory:', path return 1 else: print dirs #correct time: dt = postprocess.findDt(path) time_step = int(round(time/dt)) time = time_step*dt ref_line, u_ref = get_reference(a, time, x_c) y_ref = ref_line[:, 1] ux_ref = u_ref[:, 0] ux_num_list, sample_line_z_list = [], [] for path in dirs: vtk_file = postprocess.globFiles(path, ext='.vtk', verbose=False)[time_step] if difference: p1 = [x_c, np.min(y_ref)] p2 = [x_c, np.max(y_ref)] line, vel = sample_vtk(vtk_file, p1=p1, p2=p2, N_points=len(y_ref)) vel[:, 0] = vel[:, 0] - ux_ref else: line, vel = sample_vtk(vtk_file, x_c=x_c) ux_num_list.append(vel[:, 0]) #x component sample_line_z_list.append(line[:, 2]) #vertical component print "Plotting..." gravity = 'sourceGrav' if 'sourceGrav' in path else '' results = ['VelCross', "A0{0:g}".format(a*10), solver, gravity, suffix] results = [p for p in results if p] #remove empty strings results = '_'.join(results) xlim = {0.1: [-0.03, 0.45], 0.3: [-0.3, 1.3]} plt.clf() fig = plt.figure() fig.subplots_adjust(0.15, 0.15, 0.95, None) #plt.title('Propagation of a {2:.1f} soliton, t={0:.2f} s, $\Delta x$={1:.2f}m, {3}'.format(t, x_c-15, a, solver)) ls = ['-.', '--', '-'] for j in range(len(ux_num_list)): case = dirs[j][-1:] u = ux_num_list[j] sample_line_z = sample_line_z_list[j] if bw: plt.plot(u, sample_line_z, label='Resolution ' + case, ls=ls[j], color='k') else: plt.plot(u, sample_line_z, label='Resolution ' + case, ls=ls[j]) if not difference: plt.plot(ux_ref, y_ref, label='Reference', color='k', ls='-', lw=2) plt.legend(loc='best') plt.ylabel('z $[m]$') plt.xlabel('Velocity $[\\frac{m}{s}]$') plt.ylim(-1, np.max(sample_line_z)) plt.xlim(xlim[a]) if bw: results = results+'_bw' plt.savefig(results+'.png'.format(time)) plt.savefig(results+'.pdf'.format(time))
def plotCrossSections(path, time=None, a=None, solver=None, x_c=25, bw=0, suffix='', difference=False): """ Plots the velocity profiles along vertical crossections of a solitary wave, for a single time step. In previous versions multiple timesteps were supported, but is not any more. :param difference: Take a difference between the reference solution and numerical results :type difference: bool """ solver = solver or postprocess.determineSolver(path) a = a or postprocess.determineAmplitude(path) path = os.path.abspath(path) dirs = glob.glob(path + '/case[1-4]/*postprocessed.dat') + glob.glob( path + '/*-[1-4]/*postprocessed.dat') dirs = [os.path.split(dir)[0] for dir in dirs] dirs.sort() if not (dirs): print 'Empty directory:', path return 1 else: print dirs #correct time: dt = postprocess.findDt(path) time_step = int(round(time / dt)) time = time_step * dt ref_line, u_ref = get_reference(a, time, x_c) y_ref = ref_line[:, 1] ux_ref = u_ref[:, 0] ux_num_list, sample_line_z_list = [], [] for path in dirs: vtk_file = postprocess.globFiles(path, ext='.vtk', verbose=False)[time_step] if difference: p1 = [x_c, np.min(y_ref)] p2 = [x_c, np.max(y_ref)] line, vel = sample_vtk(vtk_file, p1=p1, p2=p2, N_points=len(y_ref)) vel[:, 0] = vel[:, 0] - ux_ref else: line, vel = sample_vtk(vtk_file, x_c=x_c) ux_num_list.append(vel[:, 0]) #x component sample_line_z_list.append(line[:, 2]) #vertical component print "Plotting..." gravity = 'sourceGrav' if 'sourceGrav' in path else '' results = ['VelCross', "A0{0:g}".format(a * 10), solver, gravity, suffix] results = [p for p in results if p] #remove empty strings results = '_'.join(results) xlim = {0.1: [-0.03, 0.45], 0.3: [-0.3, 1.3]} plt.clf() fig = plt.figure() fig.subplots_adjust(0.15, 0.15, 0.95, None) #plt.title('Propagation of a {2:.1f} soliton, t={0:.2f} s, $\Delta x$={1:.2f}m, {3}'.format(t, x_c-15, a, solver)) ls = ['-.', '--', '-'] for j in range(len(ux_num_list)): case = dirs[j][-1:] u = ux_num_list[j] sample_line_z = sample_line_z_list[j] if bw: plt.plot(u, sample_line_z, label='Resolution ' + case, ls=ls[j], color='k') else: plt.plot(u, sample_line_z, label='Resolution ' + case, ls=ls[j])
def cylinder(path='./', solver=None, cs='cfr'): if len(cs) > 1: for cs0 in cs: cylinder(path, solver, cs0) return path = os.path.abspath(path) solver = solver or postprocess.determineSolver(path) if solver == 'TruchasEnSight': solver = 'Truchas' U = 1. R = 0.05 if solver == 'Gerris': gfsFile = glob.glob(os.path.join(path, '*.gfs'))[0] for l in open(gfsFile).readlines(): if 'SurfaceBc U' in l: U = -float(l.split()[-1]) if 'SolidMoving' in l: for i, w in enumerate(l.split()): if w == 'scale': R = 0.25 * float(l.split()[i + 2]) if 'Define R' in l: R = float(l.split()[-1]) elif solver == 'Truchas': inpFile = glob.glob(os.path.join(path, '*.inp'))[0] for l in open(inpFile).readlines(): l = l.split() if len(l) == 3 and l[0] == 'R' and l[1] == '=': R = float(l[2]) elif solver == 'OpenFOAM': inpFile = glob.glob(os.path.join(path, 'system/snappyHexMeshDict'))[0] for l in open(inpFile).readlines(): l = (l.replace(';', '')).split() if len(l) == 2 and l[0] == 'radius': R = float(l[1]) position = {'c': 0, 'f': -2 * R, 'r': 2 * R} y0 = {'c': R, 'f': 0, 'r': 0} cs_name = {'c': 'center', 'f': 'front', 'r': 'rear'} fig = plt.figure() fig.subplots_adjust(0.15, 0.15, 0.95, None) if 'static' in path.lower(): state = 'static' else: state = 'moving' if 'slip' in path: BC = 'slip' else: BC = 'outflow' plt.title('Cylinder - velocity profiles (' + ', '.join([solver, state, BC, cs_name[cs]]) + ')') r = np.linspace(y0[cs], 0.5, 100) ls = ['-.', '--', '-', '-', '-'] for i, caseDir in enumerate( sorted(glob.glob(os.path.join(path, '*[1-9]')))[:5]): vtks = postprocess.globFiles(caseDir, ext='.vtk', exit=0) if vtks: print 'Profiles:', caseDir, '*.vtk' line, u = sample_vtk(vtks[-1], [position[cs], y0[cs]], [position[cs], 0.5]) if 'moving' in path: u = u + U #we want relative speed plt.plot(u[:, 0], line[:, 2], label='Resolution ' + caseDir[-1], ls=ls[i]) files = glob.glob(os.path.join(caseDir, '*.ensight.CASE')) if files: file = files[0] print 'Profiles:', file ts = enSightTimeSteps(file) if len(glob.glob(os.path.join(caseDir, '*.ensight/P.*')) ) == ts: #check if computations are finished line, u = sample_ensight(file, [position[cs], y0[cs]], [position[cs], 0.5]) if 'moving' in path: u = u + U #we want relative speed plt.plot(u[:, 0], line[:, 2], label='Resolution ' + caseDir[-1], ls=ls[i]) Vx, Vy = cylinderReference(position[cs], r, R) #plt.plot(U*(1+R**2/r**2), r, label='Reference', lw=2) plt.plot(Vx, r, label='Reference', c='k', lw=2.0) legend = {'c': 'upper right', 'f': 'upper left', 'r': 'upper left'} plt.legend(loc=legend[cs]) #plt.ylim([0, line[:, 2].max()]) #plt.ylim([0, 0.15]) if cs == 'c': plt.xlim([1, 2]) plt.ylabel('z $[m]$') plt.xlabel('Velocity $u_x$ $[\\frac{m}{s}]$') #name = os.path.split(path)[1] #ex. cylinder-static, cylinder-moving plt.savefig('-'.join(['cylinder', solver, 'profiles', state, BC, cs])) plt.savefig('-'.join(['cylinder', solver, 'profiles', state, BC, cs]) + '.pdf')