def get_sims(path='.', depth=1, nuhide=False): """Returns all found simulations as object list from all subdirs, not following symbolic links. Args: depth depth of searching for simulations, default is only one level higher directories will be searched unhide unhides all simulation found if True, if False hidden sim will stay hidden """ import os import numpy as np from pencilnew.io import load from pencilnew.io import save from pencilnew.sim import Simulation #from pen.intern.class_simdict import Simdict #from intern import get_simdict from pencilnew.io import walklevel #import intern.debug_breakpoint as debug_breakpoint new_sim_list = [] old_sim_list = False print '~ A list of pencil code simulations is generated from this dir downwards, this may take some time..' print '~ Symbolic links will not be followed, since this can lead to infinit recursion' # get overview of simulations in all lower dirs sim_paths = [] for path, dirs, files in walklevel('.', depth): if 'start.in' in files and 'run.in' in files: print '# Found Simulation in ' + path sim_paths.append(path) # take care for each simulation found # generate new simulation object for all found simulations and append on new_sim_list for path in sim_paths: sim = Simulation(path) # check if sim.name is already existing as name for a different simulation for s in new_sim_list: # check for double names if sim.name == s.name: sim.name = sim.name + '#' # add # to dublicate print "?? Warning: Found two simulatoins with the same name: " + sim.path + ' and ' + s.path print "?? Changed name of " + sim.path + ' to ' + sim.name if old_sim_list: if not unhide: # take over hidden status from previouse simDICT if existing and unhide = False (which is default!) for old_sim in old_sim_list: if (sim.path == old_sim.path) and (old_sim.hidden == True): sim.hide() ## ADD: auto hide simulations which are not runned yet, i.e. without param.nml new_sim_list.append(sim) # add new sim object to new_sim_list sim.export() # is new_sim_list empty? if new_sim_list == []: print '?? WARNING: no simulations found!' save(new_sim_list, 'sim_list', folder='.pen') return new_sim_list
def export(self): """Export simulation object to its root/.pc-dir""" from pencilnew.io import save if self == False: print('! ERROR: Simulation object is bool object and False!') # clean self.tmp_dict tmp_dict = self.tmp_dict; self.tmp_dict = {} save(self, name='sim', folder=self.pc_dir) # restore self.tmp_dict self.tmp_dict = tmp_dict
def export(self): """Export simulation object to its root/.pc-dir""" from pencilnew.io import save if self == False: print('! ERROR: Simulation object is bool object and False!') save(self, name='sim', folder=self.pc_dir)
def __init__(self, direction='x', trange=[0, -1], sim='.', OVERWRITE=False, quiet=True, jump_distance=0.5): from os.path import join from os.path import exists as path_exists import numpy as np from scipy.stats import linregress from pencilnew.io import load, save, exists from pencilnew.read import pstalk as read_pstalk from pencilnew.math.derivatives import simple_centered as derivatives_centered from pencilnew.backpack import printProgressBar # .. no pstalk file is found if not path_exists(join(sim.datadir, 'proc0', 'particles_stalker.dat')): print( '?? WARNING: No particles_stalker.dat found for simulation ' + sim.name + '!! Skipping this run!') return False # check if already existing. if, then load it out_path = join(sim.pc_datadir, 'particle', 'diffusion') out_name = direction + '_' + str(trange[0]) + '_' + str(trange[1]) # skip if diffusion already exists if not OVERWRITE and exists(name=out_name, folder=out_path): self_tmp = load(out_name, folder=out_path) for key in [a for a in dir(self_tmp) if not a.startswith('__')]: setattr(self, key, getattr(self_tmp, key)) else: #### start calculations #### print('##') print('## Calculating particle diffusion for "' + sim.name + '" in "' + sim.path + '"') print('## reading particle stalker file..') pstalk = read_pstalk(sim=sim) grid = sim.grid dim = sim.dim # get time range as index for pstalk dataset argmin = np.abs(pstalk.t - trange[0]).argmin() if trange[1] < 0.: argmax = pstalk.t.argmax() else: argmax = np.abs(pstalk.t - trange[1]).argmin() time_range = pstalk.t[argmin:argmax + 1] # modify time range with time_range[0] == 0 by substracting first time entry from time list #time_offset = pstalk.t[argmin:argmax][0] #time_range = pstalk.t[argmin:argmax]-time_offset print('\n## doing the calculation for ' + direction) L = getattr(grid, 'L' + direction) # domain size in direction N = getattr(dim, 'n' + direction) # number grid cells in direction pos_series = getattr(pstalk, direction + 'p').T[ argmin:argmax + 1] # get position timeseries series for direction for all particles N_dt = pos_series.shape[0] # get number of time steps available N_par = pos_series.shape[1] # get number of stalked particles travel_distance = 0. * pos_series # prepare 'travel distance' array for all particles mean_travel_dist = np.array(0) # prepare mean, var and sigma arrays variance = np.array(0) sigma = np.array(0) ## calulate travel distances for each particle with corrections for jumps at the boundary for i_t, pos in enumerate(pos_series): if i_t == 0: continue # skip first time_step printProgressBar(i_t, N_dt) # calculate the distance dx made in dt for all particles at once dx = pos - pos_series[i_t - 1] travel_distance[i_t] = travel_distance[i_t - 1] + dx # correct for negative jumps jumps = np.where(dx > jump_distance * L) travel_distance[i_t][jumps] = travel_distance[i_t][jumps] - L # correct for positive jumps jumps = np.where(dx < -jump_distance * L) travel_distance[i_t][jumps] = travel_distance[i_t][jumps] + L # calculate mean, variance and sigma for at 'time' i mean_travel_dist = np.mean(travel_distance, axis=1) # 1. estimate variance as time series variance = np.var(travel_distance, axis=1) sigma = np.sqrt(variance) # 2. estimate diffusion by using centered derivatives method !!! diff_dvar = 0.5 * derivatives_centered(time_range, variance) diffusion_mean = np.mean(diff_dvar) diffusion_std = np.std(diff_dvar) # create diffusion object self.diffusion = diffusion_mean self.diffusion_error = diffusion_std self.travel_distance = travel_distance self.mean_travel_distance = mean_travel_dist self.timerange = time_range self.variance = variance self.sigma = sigma self.direction = direction print('~ diffusion = ' + str(self.diffusion)) print('~ diffusion std = ' + str(self.diffusion_error)) print('~ direction = ' + str(self.direction)) try: print('## saving results in' + join(out_path, out_name)) save(obj=self, name=out_name, folder=out_path) except: print("!! Unexpected error:", sys.exc_info()[0]) print("!! Check if you have writing rights.") raise
def __init__(self, direction='x', trange=[0, -1], sim='.', OVERWRITE=False, quiet=True, jump_distance=0.5, use_existing_pstalk_sav=False): from os.path import join from os.path import exists as path_exists import numpy as np from scipy.stats import linregress from pencilnew.io import load, save, exists from pencilnew.read import pstalk as read_pstalk from pencilnew.math.derivatives import simple_centered as derivatives_centered from pencilnew.backpack import printProgressBar # .. no pstalk file is found if not path_exists(join(sim.datadir, 'proc0', 'particles_stalker.dat')): print('?? WARNING: No particles_stalker.dat found for simulation '+sim.name+'!! Skipping this run!') return False # check if already existing. if, then load it out_path = join(sim.pc_datadir, 'particle', 'diffusion') out_name = direction+'_'+str(trange[0])+'_'+str(trange[1]) # skip if diffusion already exists if not OVERWRITE and exists(name=out_name, folder=out_path): self_tmp = load(out_name, folder=out_path) for key in [a for a in dir(self_tmp) if not a.startswith('__')]: setattr(self, key, getattr(self_tmp, key)) else: #### start calculations #### print('##') print('## Calculating particle diffusion for "'+sim.name+'" in "'+sim.path+'"') print('## reading particle stalker file..') pstalk = read_pstalk(sim=sim, use_existing_pstalk_sav=use_existing_pstalk_sav, tmin=trange[0], tmax=trange[1]) grid = sim.grid dim = sim.dim # get time range as index for pstalk dataset argmin = np.abs(pstalk.t-trange[0]).argmin() if trange[1] < 0.: argmax = pstalk.t.argmax() else: argmax = np.abs(pstalk.t-trange[1]).argmin() time_range = pstalk.t[argmin:argmax+1] # modify time range with time_range[0] == 0 by substracting first time entry from time list #time_offset = pstalk.t[argmin:argmax][0] #time_range = pstalk.t[argmin:argmax]-time_offset print('\n## doing the calculation for '+direction) L = getattr(grid, 'L'+direction) # domain size in direction N = getattr(dim, 'n'+direction) # number grid cells in direction pos_series = getattr(pstalk,direction+'p').T[argmin:argmax+1] # get position timeseries series for direction for all particles N_dt = pos_series.shape[0] # get number of time steps available N_par = pos_series.shape[1] # get number of stalked particles travel_distance = 0. * pos_series # prepare 'travel distance' array for all particles mean_travel_dist = np.array(0); # prepare mean, var and sigma arrays variance = np.array(0); sigma = np.array(0) ## calulate travel distances for each particle with corrections for jumps at the boundary pbar = False; Nt = np.size(pos_series) for i_t,pos in enumerate(pos_series): if i_t == 0: continue # skip first time_step pbar = printProgressBar(i_t, Nt, pbar=pbar) # calculate the distance dx made in dt for all particles at once dx = pos - pos_series[i_t-1] travel_distance[i_t] = travel_distance[i_t-1] + dx # correct for negative jumps jumps = np.where(dx > jump_distance*L) travel_distance[i_t][jumps] = travel_distance[i_t][jumps] - L # correct for positive jumps jumps = np.where(dx < -jump_distance*L) travel_distance[i_t][jumps] = travel_distance[i_t][jumps] + L # calculate mean, variance and sigma for at 'time' i mean_travel_dist = np.mean(travel_distance, axis=1) # 1. estimate variance as time series variance = np.var(travel_distance, axis=1) sigma = np.sqrt(variance) # 2. estimate diffusion by using centered derivatives method !!! diff_dvar = 0.5*derivatives_centered(time_range, variance) diffusion_mean = np.mean(diff_dvar) diffusion_std = np.std(diff_dvar) # create diffusion object self.diffusion = diffusion_mean self.diffusion_error = diffusion_std self.travel_distance = travel_distance self.mean_travel_distance = mean_travel_dist self.timerange = time_range self.variance = variance self.sigma = sigma self.direction = direction print('~ diffusion = '+str(self.diffusion)) print('~ diffusion std = '+str(self.diffusion_error)) print('~ direction = '+str(self.direction)) try: print('## saving results in' + join(out_path,out_name)) save(obj=self, name=out_name, folder=out_path) except: print("!! Unexpected error:", sys.exc_info()[0]) print("!! Check if you have writing rights.") raise