def power2vtk(powerfiles=["power_mag.dat"], datadir="data/", destination="power", thickness=1): """ Convert power spectra from PencilCode format to vtk. call signature:: power2vtk(powerfiles = ['power_mag.dat'], datadir = 'data/', destination = 'power.vtk', thickness = 1): Read the power spectra stored in the power*.dat files and convert them into vtk format. Write the result in *destination*. Keyword arguments: *powerfiles*: The files containing the power spectra. *datadir*: Directory where the data is stored. *destination*: Destination file. *thickness*: Dimension in z-direction. Setting it 2 will create n*m*2 dimensional array of data. This is useful in Paraview for visualizing the spectrum in 3 dimensions. Note that this will simply double the amount of data. """ # this should correct for the case the user types only one variable if len(powerfiles) > 0: if len(powerfiles[0]) == 1: powerfiles = [powerfiles] # read the grid dimensions grid = pc.read_grid(datadir=datadir, trim=True, quiet=True) # leave k0 to 1 now, will fix this later k0 = 1.0 # leave dk to 1 now, will fix this later dk = 1.0 # open the destination file fd = open(destination + ".vtk", "wb") # read the first power spectrum t, power = pc.read_power(datadir + powerfiles[0]) fd.write("# vtk DataFile Version 2.0\n") fd.write("power spectra\n") fd.write("BINARY\n") fd.write("DATASET STRUCTURED_POINTS\n") if thickness == 1: fd.write("DIMENSIONS {0:9} {1:9} {2:9}\n".format(len(t), power.shape[1], 1)) else: fd.write("DIMENSIONS {0:9} {1:9} {2:9}\n".format(len(t), power.shape[1], 2)) fd.write("ORIGIN {0:8.12} {1:8.12} {2:8.12}\n".format(float(t[0]), k0, 0.0)) fd.write("SPACING {0:8.12} {1:8.12} {2:8.12}\n".format(t[1] - t[0], dk, 1.0)) if thickness == 1: fd.write("POINT_DATA {0:9}\n".format(power.shape[0] * power.shape[1])) else: fd.write("POINT_DATA {0:9}\n".format(power.shape[0] * power.shape[1] * 2)) for powfile in powerfiles: # read the power spectrum t, power = pc.read_power(datadir + powfile) fd.write("SCALARS " + powfile[:-4] + " float\n") fd.write("LOOKUP_TABLE default\n") if thickness == 1: for j in range(power.shape[1]): for i in range(len(t)): fd.write(struct.pack(">f", power[i, j])) else: for k in [1, 2]: for j in range(power.shape[1]): for i in range(len(t)): fd.write(struct.pack(">f", power[i, j])) fd.close()
def lo_pass(self, kthresh): pass def filter_field(self, k, dk=1.): shell = self.get_shell(k) hat = (self.fft).copy() hat[~shell] = 0 return (self._ifftn(hat, **self._extra_fft_args)).real if __name__ == "__main__": import pencil as pc import pylab as P import os import pickle from .utils import pc_en_spec datadir = os.path.expanduser( '~/vc/pencil-code/samples/helical-MHDturb/data/') t, pcpow = pc.read_power('power_kin.dat', datadir=datadir) var = pc.read_var(datadir=datadir) power2 = 0.5 * abs( fpack.fftn(pc.dot2(var.f[0:3, 3:-3, 3:-3, 3:-3])) / var.f[0, 3:-3, 3:-3, 3:-3].size) spec = pc_en_spec(var) q = FourierFilter(power2) spec2 = na.array([power2[q.get_shell(bin)].sum() for bin in range(q.nx)]) P.loglog(pcpow[-1, :], 'rx', markersize=10) P.loglog(spec) P.loglog(spec2) P.show()
def power2vtk(powerfiles=['power_mag.dat'], datadir='data/', destination='power', thickness=1): """ Convert power spectra from PencilCode format to vtk. call signature:: power2vtk(powerfiles = ['power_mag.dat'], datadir = 'data/', destination = 'power.vtk', thickness = 1): Read the power spectra stored in the power*.dat files and convert them into vtk format. Write the result in *destination*. Keyword arguments: *powerfiles*: The files containing the power spectra. *datadir*: Directory where the data is stored. *destination*: Destination file. *thickness*: Dimension in z-direction. Setting it 2 will create n*m*2 dimensional array of data. This is useful in Paraview for visualizing the spectrum in 3 dimensions. Note that this will simply double the amount of data. """ # this should correct for the case the user types only one variable if (len(powerfiles) > 0): if (len(powerfiles[0]) == 1): powerfiles = [powerfiles] # read the grid dimensions grid = pc.read_grid(datadir=datadir, trim=True, quiet=True) # leave k0 to 1 now, will fix this later k0 = 1. # leave dk to 1 now, will fix this later dk = 1. # open the destination file fd = open(destination + '.vtk', 'wb') # read the first power spectrum t, power = pc.read_power(datadir + powerfiles[0]) fd.write('# vtk DataFile Version 2.0\n'.encode('utf-8')) fd.write('power spectra\n'.encode('utf-8')) fd.write('BINARY\n'.encode('utf-8')) fd.write('DATASET STRUCTURED_POINTS\n'.encode('utf-8')) if (thickness == 1): fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format( len(t), power.shape[1], 1).encode('utf-8')) else: fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format( len(t), power.shape[1], 2).encode('utf-8')) fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format(float(t[0]), k0, 0.).encode('utf-8')) fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format( t[1] - t[0], dk, 1.).encode('utf-8')) if (thickness == 1): fd.write('POINT_DATA {0:9}\n'.format(power.shape[0] * power.shape[1]).encode('utf-8')) else: fd.write('POINT_DATA {0:9}\n'.format(power.shape[0] * power.shape[1] * 2).encode('utf-8')) for powfile in powerfiles: # read the power spectrum t, power = pc.read_power(datadir + powfile) fd.write(('SCALARS ' + powfile[:-4] + ' float\n').encode('utf-8')) fd.write('LOOKUP_TABLE default\n'.encode('utf-8')) if (thickness == 1): for j in range(power.shape[1]): for i in range(len(t)): fd.write(struct.pack(">f", power[i, j])) else: for k in [1, 2]: for j in range(power.shape[1]): for i in range(len(t)): fd.write(struct.pack(">f", power[i, j])) fd.close()
def power2vtk(powerFiles = ['mag_spec.dat'], destination = 'spectra.vtk', mulz = 2): """ Convert slices from PencilCode format to vtk. call signature:: power2vtk(specFiles = ['mag_spec'], destination = 'spectra.vtk') Read the power files and convert their content into vtk format. Write the result in *destination*. NB: The z-dimension is used for the time. Keyword arguments: *powerFiles*: The files containing the spectra. The files have to be in the 'data' directory. *destination*: Destination file. *mulz*: Multiplication of the data in z-direction. Use mulz=2 if you want to do fancy animations in Paraview. """ # open the destination file for writing fd = open(destination, 'wb') # write the header fd.write('# vtk DataFile Version 2.0\n') fd.write('power spectra\n') fd.write('BINARY\n') # rad the first power spectrum if (len(powerFiles[0]) > 1): pfile = powerFiles[0] else: pfile = powerFiles t, power = pc.read_power('data/'+pfile) dimk = len(power[0,:]) dimt = len(t) dt = t[1]-t[0] fd.write('DATASET STRUCTURED_POINTS\n') fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format(dimk, dimt, mulz)) fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format(0.0, 0.0, 0.0)) fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format(1.0, dt, 1.0)) fd.write('POINT_DATA {0:9}\n'.format(np.size(power)*mulz)) print 'writing ', pfile[:-4] fd.write('SCALARS '+pfile[:-4]+' float\n') fd.write('LOOKUP_TABLE default\n') for k in range(mulz): for j in range(dimt): for i in range(dimk): fd.write(struct.pack(">f", power[j,i])) # run through all power files if (len(powerFiles[0]) > 1): for pfile in powerFiles[1:]: t, power = pc.read_power('data/'+pfile) print 'writing ', pfile[:-4] fd.write('SCALARS '+pfile[:-4]+' float\n') fd.write('LOOKUP_TABLE default\n') for k in range(mulz): for j in range(dimt): for i in range(dimk): fd.write(struct.pack(">f", power[j,i])) fd.close()
for p, pb in enumerate(pb_ges[i]): if p in plottimes: #print('plotting',dd, p) ax.loglog(krms, pb) ax.set_xlim(1,dim.nxgrid//2) ax.set_ylim(pb_ges[i][0,1], 1.5*pb_ges[i][0,:].max()) dirs = [d for d in listdir('.') if d.startswith('prandtl') and path.isdir(d)] dirs = sorted(dirs, key=lambda a: float(a.split('_')[-1])) print(dirs) pb_ges = [] tb_ges = [] pr_numbers = {} for dd in dirs: tb, powerb = pc.read_power('power_mag.dat', datadir=dd) pr_numeric = float(dd.split('_')[-1]) #print(nu_numeric) pr_numbers[dd] = pr_numeric pb_ges.append(powerb) tb_ges.append(tb) #print('tb_ges has : %i indices' % len(tb_ges)) dim = pc.read_dim(datadir=dirs[0]) krms = np.loadtxt(path.join(dirs[0],'power_krms.dat')).flatten()[:dim.nxgrid//2] fig = plt.figure(figsize=fsize(.95))#figsize(width) grid = Grid(fig, rect=[.08,.1,.9,.85], nrows_ncols=(2,2), axes_pad=0., label_mode='L',
from helperfuncs import search_indx from os import listdir, path from scipy.optimize import curve_fit try: dim = pc.read_dim(datadir=dstart) krms = np.loadtxt(path.join(dstart,'power_krms.dat')).flatten()[:dim.nxgrid//2] print('krms shape: ', krms.shape) p0 = (-12.92758524, 1.94666781, 3.4643292) #(-15.56, 2.20, 4.26) except FileNotFoundError: sys.exit(1) #read in simulation parameters dim = pc.read_dim(datadir=dstart) pars = pc.read_param(datadir=dstart, quiet=True, param2=True) xi = dim.nxgrid//2 t, powerb = pc.read_power('power_mag.dat', datadir=dstart) print('t: ',t.shape, 'powerb: ',powerb.shape) print('plotting at t = %g' % t[tfit]) # Simple plot setup plotted = False fig, ax = newfig(0.45, ratio=0.75) #ax.set_xscale('log') ax.set_yscale('log') #ax.set_xlim(1,dim.nxgrid//2) ax.set_xlim(10,50) ax.set_ylim(round_exp(powerb[tfit,10]),1.5*max(powerb[tfit,:])) #ax.set_ylim(1e-7, 1e-3) clr = (0.9, 0.4, 0.4, 1.0) for p,pb in enumerate(powerb): try: