def loadData(field, extension): global data # check if data has already been loaded if (field+extension in data.loaded) == False: data.slices[field+extension], data.t = pc.read_slices(datadir = 'data', field = field, extension = extension, proc = -1) data.dim[field+extension] = pc.read_dim() # system dimensions data.param[field+extension] = pc.read_param(quiet=True) # system parameters data.loaded.add(field+extension)
def slices2vtk( variables=["rho"], extensions=["xy", "xy2", "xz", "yz"], datadir="data/", destination="slices", proc=-1, format="native", ): """ Convert slices from PencilCode format to vtk. call signature:: slices2vtk(variables = ['rho'], extensions = ['xy', 'xy2', 'xz', 'yz'], datadir = 'data/', destination = 'slices', proc = -1, format = 'native'): Read slice files specified by *variables* and convert them into vtk format for the specified extensions. Write the result in *destination*. NB: You need to have called src/read_videofiles.x before using this script. Keyword arguments: *variables*: All allowed fields which can be written as slice files, e.g. b2, uu1, lnrho, ... See the pencil code manual for more (chapter: "List of parameters for `video.in'"). *extensions*: List of slice positions. *datadir*: Directory where the data is stored. *destination*: Destination files. *proc*: Processor which should be read. Set to -1 for all processors. *format*: Endian, one of little, big, or native (default) """ # this should correct for the case the user types only one variable if len(variables) > 0: if len(variables[0]) == 1: variables = [variables] # this should correct for the case the user types only one extension if len(extensions) > 0: if len(extensions[0]) == 1: extensions = [extensions] # read the grid dimensions grid = pc.read_grid(datadir=datadir, proc=proc, trim=True, quiet=True) # read the user given parameters for the slice positions params = pc.read_param(param2=True, quiet=True) # run through all specified variables for field in variables: # run through all specified extensions for ext in extensions: print("read ", field, " ", ext) slices, t = pc.read_slices(field=field, datadir=datadir, proc=proc, extension=ext, format=format) dim_p = slices.shape[2] dim_q = slices.shape[1] if ext[0] == "x": d_p = (np.max(grid.x) - np.min(grid.x)) / (dim_p) else: d_p = (np.max(grid.y) - np.min(grid.y)) / (dim_p) if ext[1] == "y": d_q = (np.max(grid.y) - np.min(grid.y)) / (dim_q) else: d_q = (np.max(grid.z) - np.min(grid.z)) / (dim_q) if params.ix != -1: x0 = grid.x[params.ix] elif params.slice_position == "m": x0 = grid.x[len(grid.x) / 2] if params.iy != -1: y0 = grid.y[params.iy] elif params.slice_position == "m": y0 = grid.y[len(grid.y) / 2] if params.iz != -1: z0 = grid.z[params.iz] elif params.slice_position == "m": z0 = grid.z[len(grid.z) / 2] for i in range(slices.shape[0]): # open the destination file for writing fd = open(destination + "_" + field + "_" + ext + "_" + str(i) + ".vtk", "wb") # write the header fd.write("# vtk DataFile Version 2.0\n") fd.write(field + "_" + ext + "\n") fd.write("BINARY\n") fd.write("DATASET STRUCTURED_POINTS\n") if ext[0:2] == "xy": x0 = grid.x[0] y0 = grid.y[0] fd.write("DIMENSIONS {0:9} {1:9} {2:9}\n".format(dim_p, dim_q, 1)) fd.write("ORIGIN {0:8.12} {1:8.12} {2:8.12}\n".format(x0, y0, z0)) fd.write("SPACING {0:8.12} {1:8.12} {2:8.12}\n".format(grid.dx, grid.dy, 1.0)) elif ext[0:2] == "xz": x0 = grid.x[0] z0 = grid.z[0] fd.write("DIMENSIONS {0:9} {1:9} {2:9}\n".format(dim_p, 1, dim_q)) fd.write("ORIGIN {0:8.12} {1:8.12} {2:8.12}\n".format(x0, y0, z0)) fd.write("SPACING {0:8.12} {1:8.12} {2:8.12}\n".format(grid.dx, 1.0, grid.dy)) elif ext[0:2] == "yz": y0 = grid.y[0] z0 = grid.z[0] fd.write("DIMENSIONS {0:9} {1:9} {2:9}\n".format(1, dim_p, dim_q)) fd.write("ORIGIN {0:8.12} {1:8.12} {2:8.12}\n".format(x0, y0, z0)) fd.write("SPACING {0:8.12} {1:8.12} {2:8.12}\n".format(1.0, grid.dy, grid.dy)) fd.write("POINT_DATA {0:9}\n".format(dim_p * dim_q)) fd.write("SCALARS " + field + "_" + ext + " float\n") fd.write("LOOKUP_TABLE default\n") for j in range(dim_q): for k in range(dim_p): fd.write(struct.pack(">f", slices[i, j, k])) fd.close()
#!/usr/bin/env python # # $Id: fourier.py,v 1.3 2009-04-27 10:29:47 dintrans Exp $ # Fourier diagram of the vertical velocity # import numpy as N import pylab as P import pencil as pc from scipy.integrate import simps from theory import * uz, t = pc.read_slices(field='uu3', proc=0) dim = pc.read_dim() par = pc.read_param(quiet=True) par2 = pc.read_param(quiet=True, param2=True) grid = pc.read_grid(param=par, quiet=True, trim=True) nt = len(t) uz = uz.reshape(nt, dim.nz, dim.nx) w1 = N.empty((nt, dim.nz, dim.nx), dtype='Complex64') for i in range(dim.nz): w1[:, i, :] = N.fft.fft2(uz[:, i, :]) / nt / dim.nx w2 = N.abs(w1[1:nt / 2 + 1, ...]) dw = 2 * N.pi / (t[-1] - t[0]) w = dw * N.arange(nt) w = w[1:nt / 2 + 1] kmax = 5
def slices2vtk(variables=['rho'], extensions=['xy', 'xy2', 'xz', 'yz'], datadir='data/', destination='slices', proc=-1, format='native'): """ Convert slices from PencilCode format to vtk. call signature:: slices2vtk(variables = ['rho'], extensions = ['xy', 'xy2', 'xz', 'yz'], datadir = 'data/', destination = 'slices', proc = -1, format = 'native'): Read slice files specified by *variables* and convert them into vtk format for the specified extensions. Write the result in *destination*. NB: You need to have called src/read_videofiles.x before using this script. Keyword arguments: *variables*: All allowed fields which can be written as slice files, e.g. b2, uu1, lnrho, ... See the pencil code manual for more (chapter: "List of parameters for `video.in'"). *extensions*: List of slice positions. *datadir*: Directory where the data is stored. *destination*: Destination files. *proc*: Processor which should be read. Set to -1 for all processors. *format*: Endian, one of little, big, or native (default) """ # this should correct for the case the user types only one variable if (len(variables) > 0): if (len(variables[0]) == 1): variables = [variables] # this should correct for the case the user types only one extension if (len(extensions) > 0): if (len(extensions[0]) == 1): extensions = [extensions] # read the grid dimensions grid = pc.read_grid(datadir=datadir, proc=proc, trim=True, quiet=True) # read the user given parameters for the slice positions params = pc.read_param(param2=True, quiet=True) # run through all specified variables for field in variables: # run through all specified extensions for ext in extensions: print("read " + field + ' ' + ext) slices, t = pc.read_slices(field=field, datadir=datadir, proc=proc, extension=ext, format=format) dim_p = slices.shape[2] dim_q = slices.shape[1] if ext[0] == 'x': d_p = (np.max(grid.x) - np.min(grid.x)) / (dim_p) else: d_p = (np.max(grid.y) - np.min(grid.y)) / (dim_p) if ext[1] == 'y': d_q = (np.max(grid.y) - np.min(grid.y)) / (dim_q) else: d_q = (np.max(grid.z) - np.min(grid.z)) / (dim_q) if params.ix != -1: x0 = grid.x[params.ix] elif params.slice_position == 'm': x0 = grid.x[int(len(grid.x) / 2)] if params.iy != -1: y0 = grid.y[params.iy] elif params.slice_position == 'm': y0 = grid.y[int(len(grid.y) / 2)] if params.iz != -1: z0 = grid.z[params.iz] elif params.slice_position == 'm': z0 = grid.z[int(len(grid.z) / 2)] for i in range(slices.shape[0]): # open the destination file for writing fd = open( destination + '_' + field + '_' + ext + '_' + str(i) + '.vtk', 'wb') # write the header fd.write('# vtk DataFile Version 2.0\n') fd.write(field + '_' + ext + '\n') fd.write('BINARY\n') fd.write('DATASET STRUCTURED_POINTS\n') if ext[0:2] == 'xy': x0 = grid.x[0] y0 = grid.y[0] fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format( dim_p, dim_q, 1)) fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format( x0, y0, z0)) fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format( grid.dx, grid.dy, 1.)) elif ext[0:2] == 'xz': x0 = grid.x[0] z0 = grid.z[0] fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format( dim_p, 1, dim_q)) fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format( x0, y0, z0)) fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format( grid.dx, 1., grid.dy)) elif ext[0:2] == 'yz': y0 = grid.y[0] z0 = grid.z[0] fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format( 1, dim_p, dim_q)) fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format( x0, y0, z0)) fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format( 1., grid.dy, grid.dy)) fd.write('POINT_DATA {0:9}\n'.format(dim_p * dim_q)) fd.write('SCALARS ' + field + '_' + ext + ' float\n') fd.write('LOOKUP_TABLE default\n') for j in range(dim_q): for k in range(dim_p): fd.write(struct.pack(">f", slices[i, j, k])) fd.close()
#!/usr/bin/python # $Id$ import numpy as N import pylab as P import pencil as pc from os import system system('cat video.in') field=raw_input('which field? ') f, t = pc.read_slices(field=field, proc=0, extension='xy') dim=pc.read_dim() grid=pc.read_grid(trim=True) nt=len(t) ff=f.reshape(nt, dim.nx) P.ion() P.subplot(211) line, = P.plot(grid.x, ff[0, :]) P.xlim(grid.x[0], grid.x[-1]) P.ylim(ymin=ff.min(), ymax=ff.max()) P.title(field) st=P.figtext(0.2, 0.85, 't=%.1f'%t[0]) for i in range(1, nt): line.set_ydata(ff[i, :]) st.set_text('t=%.1f'%t[i]) P.draw()
#!/usr/bin/python # $Id: pvid2D.py,v 1.1.1.1 2009-12-16 17:37:16 dintrans Exp $ import numpy as N import pylab as P import pencil as pc from os import system system('cat video.in') field=raw_input('which field? ') f, t = pc.read_slices(field=field, proc=0, extension='xz') ux, t = pc.read_slices(field='uu1', proc=0, extension='xz') uz, t = pc.read_slices(field='uu3', proc=0, extension='xz') dim=pc.read_dim() grid=pc.read_grid(trim=True) param=pc.read_param(quiet=True) nt=len(t) f=f.reshape(nt, dim.nz, dim.nx) ux=ux.reshape(nt, dim.nz, dim.nx) uz=uz.reshape(nt, dim.nz, dim.nx) P.ion() frame=param.xyz0[0],param.xyz1[0],param.xyz0[2],param.xyz1[2] qs1=N.random.random_integers(0,dim.nx-1, 1000) qs2=N.random.random_integers(0,dim.nz-1, 1000) xx,zz=P.meshgrid(grid.x, grid.z) im=P.imshow(f[0,...], extent=frame, origin='lower', aspect='auto') a=ux[0, qs2, qs1]**2+uz[0, qs2, qs1]**2 ; norm=N.sqrt(a.max())
#!/usr/bin/python # $Id: pvid2D.py,v 1.1.1.1 2009-12-16 17:37:16 dintrans Exp $ import numpy as N import pylab as P import pencil as pc from os import system system('cat video.in') field = raw_input('which field? ') f, t = pc.read_slices(field=field, proc=0, extension='xz') ux, t = pc.read_slices(field='uu1', proc=0, extension='xz') uz, t = pc.read_slices(field='uu3', proc=0, extension='xz') dim = pc.read_dim() grid = pc.read_grid(trim=True) param = pc.read_param(quiet=True) nt = len(t) f = f.reshape(nt, dim.nz, dim.nx) ux = ux.reshape(nt, dim.nz, dim.nx) uz = uz.reshape(nt, dim.nz, dim.nx) P.ion() frame = param.xyz0[0], param.xyz1[0], param.xyz0[2], param.xyz1[2] qs1 = N.random.random_integers(0, dim.nx - 1, 1000) qs2 = N.random.random_integers(0, dim.nz - 1, 1000) xx, zz = P.meshgrid(grid.x, grid.z) im = P.imshow(f[0, ...], extent=frame, origin='lower', aspect='auto') a = ux[0, qs2, qs1]**2 + uz[0, qs2, qs1]**2
#!/usr/bin/env python # # $Id: pvid.py,v 1.5 2009-08-27 08:11:08 dintrans Exp $ # Animate the entropy snapshots with the velocity field superimposed # import numpy as N import pylab as P import pencil as pc ss, t = pc.read_slices(field='ss', proc=0) ux, t = pc.read_slices(field='uu1', proc=0) uz, t = pc.read_slices(field='uu3', proc=0) dim=pc.read_dim() par=pc.read_param(quiet=True) par2=pc.read_param(quiet=True, param2=True) grid=pc.read_grid(param=par, quiet=True, trim=True) nt=len(t) ss=ss.reshape(nt, dim.nz, dim.nx) ux=ux.reshape(nt, dim.nz, dim.nx) uz=uz.reshape(nt, dim.nz, dim.nx) qs1=N.random.random_integers(0,dim.nx-1, 1000) qs2=N.random.random_integers(0,dim.nz-1, 1000) xx,zz=P.meshgrid(grid.x, grid.z) frame=par.xyz0[0],par.xyz1[0],par.xyz0[2],par.xyz1[2] P.ion() im=P.imshow(ss[0,...], extent=frame, origin='lower', aspect='auto')
#!/usr/bin/python # $Id$ import numpy as N import pylab as P import pencil as pc from os import system system('cat video.in') field = raw_input('which field? ') f, t = pc.read_slices(field=field, proc=0, extension='xy') dim = pc.read_dim() grid = pc.read_grid(trim=True) nt = len(t) ff = f.reshape(nt, dim.nx) P.ion() P.subplot(211) line, = P.plot(grid.x, ff[0, :]) P.xlim(grid.x[0], grid.x[-1]) P.ylim(ymin=ff.min(), ymax=ff.max()) P.title(field) st = P.figtext(0.2, 0.85, 't=%.1f' % t[0]) for i in range(1, nt): line.set_ydata(ff[i, :]) st.set_text('t=%.1f' % t[i]) P.draw()