def update(self, quiet=True): """Update simulation object: - read param.nml - read grid and ghost grid """ from os.path import exists from os.path import join from pencilnew.read import param from pencilnew.read import grid if self.param == False or self.param != param(quiet=quiet, datadir=self.datadir): try: if exists(join(self.datadir, 'param.nml')): param = param(quiet=quiet, datadir=self.datadir) self.param = {} # read params into Simulation object for key in dir(param): if key.startswith('__'): continue self.param[key] = getattr(param, key) else: if not quiet: print( '? WARNING: for ' + self.path + '\n? Simulation has not run yet! Meaning: No param.nml found!' ) except: print('! ERROR: while reading param.nml for ' + self.path) self.param = False if self.param != False and (self.grid == False or self.ghost_grid == False): try: # read grid only if param is not False #import pencilnew as pcn; pcn.io.debug_breakpoint() self.grid = grid(datadir=self.datadir, trim=True, quiet=True) self.ghost_grid = grid(datadir=self.datadir, trim=False, quiet=True) if not quiet: print('# Updating grid and ghost_grid succesfull') except: if not quiet: print( '? WARNING: Updating grid and ghost_grid was not succesfull, since reading grid had an error' ) if self.started() or (not quiet): print('? WARNING: Couldnt load grid for ' + self.path) self.grid = False self.ghost_grid = False elif self.param == False: if not quiet: print( '? WARNING: Updating grid and ghost_grid was not succesfull, since run did is not started yet.' ) self.grid = False self.ghost_grid = False self.export() #import pencilnew as pcn; pcn.io.debug_breakpoint() return self
def read(self, data_dir='data', param=None, dim=None): """ Read Pencil Code index data from index.pro. call signature: read(self, data_dir='data/', param=None, dim=None) Keyword arguments: *data_dir*: Directory where the data is stored. *param* Parameter object. *dim* Dimension object. """ import os import pencilnew.read as read if param is None: param = read.param(data_dir=data_dir, quiet=True) if dim is None: dim = read.dim(data_dir=data_dir) if param.lwrite_aux: totalvars = dim.mvar + dim.maux else: totalvars = dim.mvar index_file = open(os.path.join(data_dir, 'index.pro')) for line in index_file.readlines(): clean = line.strip() name = clean.split('=')[0].strip().replace('[', '').replace(']', '') if (clean.split('=')[1].strip().startswith('intarr(370)')): continue val = int(clean.split('=')[1].strip()) if (val != 0 and val <= totalvars \ and not name.startswith('i_') and name.startswith('i')): name = name.lstrip('i') if (name == 'lnTT' and param.ltemperature_nolog): name = 'tt' setattr(self, name, val)
def update(self, hard=False, quiet=True): """Update simulation object: if not read in: - read param.nml - read grid and ghost grid Set hard=True to force update. """ from os.path import exists from os.path import join from pencilnew.read import param, grid, dim REEXPORT = False if hard == True: self.param = False self.grid = False self.ghost_grid = False self.dim = False REEXPORT = True if self.param == False: try: if exists(join(self.datadir, 'param.nml')): print('~ Reading param.nml.. ') param = param(quiet=quiet, datadir=self.datadir) self.param = {} # read params into Simulation object for key in dir(param): if key.startswith('_') or key == 'read': continue self.param[key] = getattr(param, key) REEXPORT = True else: if not quiet: print( '? WARNING: for ' + self.path + '\n? Simulation has not run yet! Meaning: No param.nml found!' ) REEXPORT = True except: print('! ERROR: while reading param.nml for ' + self.path) self.param = False REEXPORT = True if self.param != False and (self.grid == False or self.ghost_grid == False): try: # read grid only if param is not False #import pencilnew as pcn; pcn.io.debug_breakpoint() print('~ Reading grid.. ') self.grid = grid(datadir=self.datadir, trim=True, quiet=True) print('~ Reading ghost_grid.. ') self.ghost_grid = grid(datadir=self.datadir, trim=False, quiet=True) print('~ Reading dim.. ') self.dim = dim(datadir=self.datadir) if not quiet: print('# Updating grid and ghost_grid succesfull') REEXPORT = True # adding lx, dx etc to params self.param['Lx'] = self.grid.Lx self.param['Ly'] = self.grid.Ly self.param['Lz'] = self.grid.Lz self.param['lx'] = self.grid.Lx self.param['ly'] = self.grid.Ly self.param['lz'] = self.grid.Lz self.param['dx'] = self.grid.dx self.param['dy'] = self.grid.dy self.param['dz'] = self.grid.dz except: if not quiet: print( '? WARNING: Updating grid and ghost_grid was not succesfull, since reading grid had an error' ) if self.started() or (not quiet): print('? WARNING: Couldnt load grid for ' + self.path) self.grid = False self.ghost_grid = False self.dim = False REEXPORT = True elif self.param == False: if not quiet: print( '? WARNING: Updating grid and ghost_grid was not succesfull, since run did is not started yet.' ) self.grid = False self.ghost_grid = False self.dim = False REEXPORT = True if REEXPORT == True: self.export() #import pencilnew as pcn; pcn.io.debug_breakpoint() return self
def read(self, var_file='', sim=None, datadir='data', proc=-1, ivar=-1, quiet=True, trim_all=False, trimall=False, dim=None, magic=None): """ Read VAR files from pencil code. If proc < 0, then load all data and assemble. otherwise, load VAR file from specified processor. The file format written by output() (and used, e.g. in var.dat) consists of the followinig Fortran records: 1. data(mx, my, mz, nvar) 2. t(1), x(mx), y(my), z(mz), dx(1), dy(1), dz(1), deltay(1) Here nvar denotes the number of slots, i.e. 1 for one scalar field, 3 for one vector field, 8 for var.dat in the case of MHD with entropy. but, deltay(1) is only there if lshear is on! need to know parameters. call signature: read(var_file='', datadir='data/', proc=-1, ivar=-1, quiet=True, trimall=False, magic=None, sim=None) Keyword arguments: var_file: Name of the VAR file. sim: Simulation sim object. magic: Values to be computed from the data, e.g. B = curl(A). trimall: Trim the data cube to exclude ghost zones. quiet: Flag for switching off output. datadir: Directory where the data is stored. proc: Processor to be read. If -1 read all and assemble to one array. ivar: Index of the VAR file, if var_file is not specified. """ import numpy as np import os from scipy.io import FortranFile from pencilnew.math.derivatives import curl, curl2 from pencilnew import read if sim is not None: datadir = os.path.expanduser(sim.datadir) dim = sim.dim param = read.param(datadir=sim.datadir, quiet=True) index = read.index(datadir=sim.datadir) else: datadir = os.path.expanduser(datadir) if dim is None: dim = read.dim(datadir, proc) if param is None: param = read.param(datadir=datadir, quiet=quiet) if index is None: index = read.index(datadir=datadir) run2D = param.lwrite_2d if dim.precision == 'D': precision = 'd' else: precision = 'f' if param.lwrite_aux: total_vars = dim.mvar + dim.maux else: total_vars = dim.mvar if not var_file: if ivar < 0: var_file = 'var.dat' else: var_file = 'VAR' + str(ivar) if proc < 0: proc_dirs = self.__natural_sort( filter(lambda s: s.startswith('proc'), os.listdir(datadir))) else: proc_dirs = ['proc' + str(proc)] if trimall != False: trim_all = trimall # Set up the global array. if not run2D: f = np.zeros((total_vars, dim.mz, dim.my, dim.mx), dtype=precision) else: if dim.ny == 1: f = np.zeros((total_vars, dim.mz, dim.mx), dtype=precision) else: f = np.zeros((total_vars, dim.my, dim.mx), dtype=precision) x = np.zeros(dim.mx, dtype=precision) y = np.zeros(dim.my, dtype=precision) z = np.zeros(dim.mz, dtype=precision) for directory in proc_dirs: proc = int(directory[4:]) procdim = read.dim(datadir, proc) if not quiet: print("Reading data from processor {0} of {1} ...".format( \ proc, len(proc_dirs))) mxloc = procdim.mx myloc = procdim.my mzloc = procdim.mz # Read the data. file_name = os.path.join(datadir, directory, var_file) infile = FortranFile(file_name) if not run2D: f_loc = infile.read_record(dtype=precision) f_loc = f_loc.reshape((-1, mzloc, myloc, mxloc)) else: if dim.ny == 1: f_loc = infile.read_record(dtype=precision) f_loc = f_loc.reshape((-1, mzloc, mxloc)) else: f_loc = infile.read_record(dtype=precision) f_loc = f_loc.reshape((-1, myloc, mxloc)) raw_etc = infile.read_record(precision) infile.close() t = raw_etc[0] x_loc = raw_etc[1:mxloc + 1] y_loc = raw_etc[mxloc + 1:mxloc + myloc + 1] z_loc = raw_etc[mxloc + myloc + 1:mxloc + myloc + mzloc + 1] if param.lshear: shear_offset = 1 deltay = raw_etc[-1] else: shear_offset = 0 dx = raw_etc[-3 - shear_offset] dy = raw_etc[-2 - shear_offset] dz = raw_etc[-1 - shear_offset] if len(proc_dirs) > 1: # Calculate where the local processor will go in # the global array. # # Don't overwrite ghost zones of processor to the left (and # accordingly in y and z direction -- makes a difference on the # diagonals) # # Recall that in NumPy, slicing is NON-INCLUSIVE on the right end # ie, x[0:4] will slice all of a 4-digit array, not produce # an error like in idl. if procdim.ipx == 0: i0x = 0 i1x = i0x + procdim.mx i0xloc = 0 i1xloc = procdim.mx else: i0x = procdim.ipx * procdim.nx + procdim.nghostx i1x = i0x + procdim.mx - procdim.nghostx i0xloc = procdim.nghostx i1xloc = procdim.mx if procdim.ipy == 0: i0y = 0 i1y = i0y + procdim.my i0yloc = 0 i1yloc = procdim.my else: i0y = procdim.ipy * procdim.ny + procdim.nghosty i1y = i0y + procdim.my - procdim.nghosty i0yloc = procdim.nghosty i1yloc = procdim.my if procdim.ipz == 0: i0z = 0 i1z = i0z + procdim.mz i0zloc = 0 i1zloc = procdim.mz else: i0z = procdim.ipz * procdim.nz + procdim.nghostz i1z = i0z + procdim.mz - procdim.nghostz i0zloc = procdim.nghostz i1zloc = procdim.mz x[i0x:i1x] = x_loc[i0xloc:i1xloc] y[i0y:i1y] = y_loc[i0yloc:i1yloc] z[i0z:i1z] = z_loc[i0zloc:i1zloc] if not run2D: f[:, i0z:i1z, i0y:i1y, i0x:i1x] = \ f_loc[:, i0zloc:i1zloc, i0yloc:i1yloc, i0xloc:i1xloc] else: if dim.ny == 1: f[:, i0z:i1z, i0x:i1x] = \ f_loc[:, i0zloc:i1zloc, i0xloc:i1xloc] else: f[:, i0y:i1y, i0x:i1x] = \ f_loc[:, i0yloc:i1yloc, i0xloc:i1xloc] else: f = f_loc x = x_loc y = y_loc z = z_loc if magic is not None: if 'bb' in magic: # Compute the magnetic field before doing trim_all. aa = f[index.ax - 1:index.az, ...] self.bb = curl(aa, dx, dy, dz, run2D=run2D) if trim_all: self.bb = self.bb[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1, dim.l1:dim.l2 + 1] if 'jj' in magic: # Compute the electric current field before doing trim_all. aa = f[index.ax - 1:index.az, ...] self.jj = curl2(aa, dx, dy, dz) if trim_all: self.jj = self.jj[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1, dim.l1:dim.l2 + 1] if 'vort' in magic: # Compute the vorticity field before doing trim_all. uu = f[index.ux - 1:index.uz, ...] self.vort = curl(uu, dx, dy, dz, run2D=run2D) if trim_all: if run2D: if dim.nz == 1: self.vort = self.vort[:, dim.m1:dim.m2 + 1, dim.l1:dim.l2 + 1] else: self.vort = self.vort[:, dim.n1:dim.n2 + 1, dim.l1:dim.l2 + 1] else: self.vort = self.vort[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1, dim.l1:dim.l2 + 1] # Trim the ghost zones of the global f-array if asked. if trim_all: self.x = x[dim.l1:dim.l2 + 1] self.y = y[dim.m1:dim.m2 + 1] self.z = z[dim.n1:dim.n2 + 1] if not run2D: self.f = f[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1, dim.l1:dim.l2 + 1] else: if dim.ny == 1: self.f = f[:, dim.n1:dim.n2 + 1, dim.l1:dim.l2 + 1] else: self.f = f[:, dim.m1:dim.m2 + 1, dim.l1:dim.l2 + 1] else: self.x = x self.y = y self.z = z self.f = f self.l1 = dim.l1 self.l2 = dim.l2 + 1 self.m1 = dim.m1 self.m2 = dim.m2 + 1 self.n1 = dim.n1 self.n2 = dim.n2 + 1 # Assign an attribute to self for each variable defined in # 'data/index.pro' so that e.g. self.ux is the x-velocity for key in index.__dict__.keys(): if key != 'global_gg' and key != 'keys': value = index.__dict__[key] setattr(self, key, self.f[value - 1, ...]) # Special treatment for vector quantities. if hasattr(index, 'uu'): self.uu = self.f[index.ux - 1:index.uz, ...] if hasattr(index, 'aa'): self.aa = self.f[index.ax - 1:index.az, ...] self.t = t self.dx = dx self.dy = dy self.dz = dz if param.lshear: self.deltay = deltay # Do the rest of magic after the trim_all (i.e. no additional curl...). self.magic = magic if self.magic is not None: self.magic_attributes(param)
def update(self, hard=False, quiet=True): """Update simulation object: if not read in: - read param.nml - read grid and ghost grid Set hard=True to force update. """ from os.path import exists from os.path import join from pencilnew.read import param, grid, dim REEXPORT = False if hard == True: self.param = False self.grid = False self.ghost_grid = False self.dim = False REEXPORT = True if self.param == False: try: if exists(join(self.datadir,'param.nml')): print('~ Reading param.nml.. ') param = param(quiet=quiet, datadir=self.datadir) self.param = {} # read params into Simulation object for key in dir(param): if key.startswith('_') or key == 'read': continue self.param[key] = getattr(param, key) REEXPORT = True else: if not quiet: print('? WARNING: for '+self.path+'\n? Simulation has not run yet! Meaning: No param.nml found!') REEXPORT = True except: print('! ERROR: while reading param.nml for '+self.path) self.param = False REEXPORT = True if self.param != False and (self.grid == False or self.ghost_grid == False): try: # read grid only if param is not False #import pencilnew as pcn; pcn.io.debug_breakpoint() print('~ Reading grid.. ') self.grid = grid(datadir=self.datadir, trim=True, quiet=True) print('~ Reading ghost_grid.. ') self.ghost_grid = grid(datadir=self.datadir, trim=False, quiet=True) print('~ Reading dim.. ') self.dim = dim(datadir=self.datadir) if not quiet: print('# Updating grid and ghost_grid succesfull') REEXPORT = True # adding lx, dx etc to params self.param['Lx'] = self.grid.Lx; self.param['Ly'] = self.grid.Ly; self.param['Lz'] = self.grid.Lz self.param['lx'] = self.grid.Lx; self.param['ly'] = self.grid.Ly; self.param['lz'] = self.grid.Lz self.param['dx'] = self.grid.dx; self.param['dy'] = self.grid.dy; self.param['dz'] = self.grid.dz except: if not quiet: print('? WARNING: Updating grid and ghost_grid was not succesfull, since reading grid had an error') if self.started() or (not quiet): print('? WARNING: Couldnt load grid for '+self.path) self.grid = False self.ghost_grid = False self.dim = False REEXPORT = True elif self.param == False: if not quiet: print('? WARNING: Updating grid and ghost_grid was not succesfull, since run did is not started yet.') self.grid = False self.ghost_grid = False self.dim = False REEXPORT = True if REEXPORT == True: self.export() #import pencilnew as pcn; pcn.io.debug_breakpoint() return self