def make_continue_sub(simdir='.', paramname='snapshot.param', \ newparam='continue.param', t=None, t_extra=None, oldsub='subber.sh', \ newsub='cont_subber.sh'): """ Makes a submission script for continuing a simulation from a previous output. Also makes a .param file for the continued simulation. The simulation will be continued in the same directory, with snapshot numbering scheme for outputs being the same. Parameters for the original simulation cannot be altered (except the number of steps you want to continue the simulation by). PBS runtime parameters also cannot be changed (number of nodes, walltime, etc...) Any checkpoints will be deleted. Requires a submission script be present for the original simulation NOTE: if nSteps, nSteps_extra are not set, the total number of steps to simulate is not changed. **ARGUMENTS** simdir : str The simulation directory paramname : str Filename of the .param file for the simulation newparam : str filename for the .param file for the continued simulation t : float or SimArray Total simulation time to run. If no units are specified, it is in simulation units t_extra : float or SimArray Extra simulation time to run If no units are specified, it is in simulation units OVERIDES t!!! oldsub : str Filename for the original submission script newsub : str Filename for the new submission script **RETURNS** sub_path : str Full path to the PBS submission script """ # Lazy man's way of dealing with files in another directory cwd = os.getcwd() os.chdir(simdir) # Load param file param = isaac.configparser(paramname, 'param') fprefix = param['achOutName'] # Find all the outputs. They should be of the format fprefix.000000 search_exp = '^' + fprefix + '.(?:(?<!\d)\d{6}(?!\d))$' flist = [] for fname in glob.glob(fprefix + '*'): if re.search(search_exp, fname) is not None: flist.append(fname) # Find the number of the last output (the last 6 chars should be an int) flist.sort() iStartStep = int(flist[-1][-6:]) param['iStartStep'] = iStartStep param['achInFile'] = flist[-1] dDelta = param['dDelta'] # Set the number of steps to run if t_extra is not None: # Convert to simulation units if needed if pynbody.units.has_units(t_extra): t_unit = isaac.units_from_param(param)['t_unit'] t_extra.convert_units(t_unit) # Assign output param['nSteps'] = iStartStep + int(round(t_extra / dDelta)) elif t is not None: # Convert to simulation units if needed if pynbody.units.has_units(t): t_unit = isaac.units_from_param(param)['t_unit'] t.convert_units(t_unit) # Assign output param['nSteps'] = int(round(t / dDelta)) # Save new param file isaac.configsave(param, newparam, ftype='param') # Delete old checkpoints for checkpoint in glob.glob(fprefix + '.chk*'): print 'removing ' + checkpoint os.system('rm -rf ' + checkpoint) if os.path.exists('lastcheckpoint'): print 'removing lastcheckpoint' os.remove('lastcheckpoint') # Create a submission script for the simulation continuation oldsubfile = open(oldsub, 'r') newsubfile = open(newsub, 'w') for line in oldsubfile: newsubfile.write(line.replace(paramname, newparam)) oldsubfile.close() newsubfile.close() # Make the submission script executable os.chmod(newsub, 0777) sub_path = os.path.abspath(newsub) # Change back to original working directory os.chdir(cwd) return sub_path
def find_clumps(f, n_smooth=32, param=None, arg_string=None, seed=None, verbose=True): """ Uses skid (https://github.com/N-BodyShop/skid) to find clumps in a gaseous protoplanetary disk. The linking length used is equal to the gravitational softening length of the gas particles. The density cut-off comes from the criterion that there are n_smooth particles within the Hill sphere of a particle. This is formulated mathematically as: rho_min = 3*n_smooth*Mstar/R^3 where R is the distance from the star. The trick used here is to multiply all particle masses by R^3 before running skid so the density cut-off is: rho_min = 3*n_smooth*Mstar **ARGUMENTS** *f* : TipsySnap, or str A tipsy snapshot loaded/created by pynbody -OR- a filename pointing to a snapshot. *n_smooth* : int (optional) Number of particles used in SPH calculations. Should be the same as used in the simulation. Default = 32 *param* : str (optional) filename for a .param file for the simulation *arg_string* : str (optional) Additional arguments to be passed to skid. Cannot use -tau, -d, -m, -s, -o *seed* : int An integer used to seed the random filename generation for temporary files. Necessary for multiprocessing and should be unique for each thread. *verbose* : bool Verbosity flag. Default is True **RETURNS** *clumps* : array, int-like Array containing the group number each particle belongs to, with star particles coming after gas particles. A zero means the particle belongs to no groups """ # Parse areguments if isinstance(f, str): f = pynbody.load(f, paramfile=param) if seed is not None: np.random.seed(seed) # Estimate the linking length as the gravitational softening length tau = f.g['eps'][0] # Calculate minimum density rho_min = 3*n_smooth*f.s['mass'][0] # Center on star. This is done because R used in hill-sphere calculations # is relative to the star star_pos = f.s['pos'].copy() f['pos'] -= star_pos # Scale mass by R^3 R = isaac.strip_units(f['rxy']) m0 = f['mass'].copy() f['mass'] *= (R+tau)**3 # Save temporary snapshot f_prefix = str(np.random.randint(np.iinfo(int).max)) f_name = f_prefix + '.std' # Save temporary .param file if param is not None: param_name = f_prefix + '.param' param_dict = isaac.configparser(param, 'param') isaac.configsave(param_dict, param_name) f.write(filename=f_name, fmt=pynbody.tipsy.TipsySnap) f['pos'] += star_pos f['mass'] = m0 command = 'totipnat < {} | skid -tau {:.2e} -d {:.2e} -m {:d} -s {:d} -o {}'\ .format(f_name, tau, rho_min, n_smooth, n_smooth, f_prefix) p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if verbose: for line in iter(p.stdout.readline, ''): print line, p.wait() # Load clumps clumps = isaac.loadhalos(f_prefix + '.grp') # Cleanup for name in glob.glob(f_prefix + '*'): os.remove(name) return clumps
# Save Output # ------------------------------------------------- # Save snapshot snapshot.write(filename=snapshotName, fmt=pynbody.tipsy.TipsySnap) print 'Tipsy snapshot saved to {0}'.format(snapshotName) # Start saving log file logFile = open(logFileName, 'w') logFile.write('Generated from settings file: {0}\n'.format( os.path.abspath(settings_file))) m_amu = float((m * 0.992733 / pynbody.units.m_p).in_units('1')) # Save director file if paramName is not None: try: import isaac defPar = os.path.join(ICgenDir, 'default.param') pars = isaac.configparser(defPar, ftype='param') pars['achInFile'] = snapshotName pars['achOutName'] = os.path.splitext(snapshotName)[0] # Convert molecular mass from mass of proton to amu pars['dMeanMolWeight'] = m_amu isaac.configsave(pars, paramName, ftype='param') print 'ChaNGa parameter file saved to {0}'.format(paramName) logFile.write( 'ChaNGa parameter file saved to {0}\n'.format(paramName)) except: print 'Could not save .param file. Check that the module isaac\ was properly imported' #Continue writing to log file logFile.write('Tipsy snapshot saved to {0}\n'.format(snapshotName)) logFile.write('Surface density profile saved to {0}\n\
def make_continue_sub(simdir='.', paramname='snapshot.param', \ newparam='continue.param', t=None, t_extra=None, oldsub='subber.sh', \ newsub='cont_subber.sh'): """ Makes a submission script for continuing a simulation from a previous output. Also makes a .param file for the continued simulation. The simulation will be continued in the same directory, with snapshot numbering scheme for outputs being the same. Parameters for the original simulation cannot be altered (except the number of steps you want to continue the simulation by). PBS runtime parameters also cannot be changed (number of nodes, walltime, etc...) Any checkpoints will be deleted. Requires a submission script be present for the original simulation NOTE: if nSteps, nSteps_extra are not set, the total number of steps to simulate is not changed. **ARGUMENTS** simdir : str The simulation directory paramname : str Filename of the .param file for the simulation newparam : str filename for the .param file for the continued simulation t : float or SimArray Total simulation time to run. If no units are specified, it is in simulation units t_extra : float or SimArray Extra simulation time to run If no units are specified, it is in simulation units OVERIDES t!!! oldsub : str Filename for the original submission script newsub : str Filename for the new submission script **RETURNS** sub_path : str Full path to the PBS submission script """ # Lazy man's way of dealing with files in another directory cwd = os.getcwd() os.chdir(simdir) # Load param file param = isaac.configparser(paramname, 'param') fprefix = param['achOutName'] # Find all the outputs. They should be of the format fprefix.000000 search_exp = '^' + fprefix + '.(?:(?<!\d)\d{6}(?!\d))$' flist = [] for fname in glob.glob(fprefix + '*'): if re.search(search_exp, fname) is not None: flist.append(fname) # Find the number of the last output (the last 6 chars should be an int) flist.sort() iStartStep = int(flist[-1][-6:]) param['iStartStep'] = iStartStep param['achInFile'] = flist[-1] dDelta = param['dDelta'] # Set the number of steps to run if t_extra is not None: # Convert to simulation units if needed if pynbody.units.has_units(t_extra): t_unit = isaac.units_from_param(param)['t_unit'] t_extra.convert_units(t_unit) # Assign output param['nSteps'] = iStartStep + int(round(t_extra/dDelta)) elif t is not None: # Convert to simulation units if needed if pynbody.units.has_units(t): t_unit = isaac.units_from_param(param)['t_unit'] t.convert_units(t_unit) # Assign output param['nSteps'] = int(round(t/dDelta)) # Save new param file isaac.configsave(param, newparam, ftype='param') # Delete old checkpoints for checkpoint in glob.glob(fprefix + '.chk*'): print 'removing ' + checkpoint os.system('rm -rf ' + checkpoint) if os.path.exists('lastcheckpoint'): print 'removing lastcheckpoint' os.remove('lastcheckpoint') # Create a submission script for the simulation continuation oldsubfile = open(oldsub, 'r') newsubfile = open(newsub, 'w') for line in oldsubfile: newsubfile.write(line.replace(paramname, newparam)) oldsubfile.close() newsubfile.close() # Make the submission script executable os.chmod(newsub, 0777) sub_path = os.path.abspath(newsub) # Change back to original working directory os.chdir(cwd) return sub_path
# Save Output # ------------------------------------------------- # Save snapshot snapshot.write(filename=snapshotName, fmt=pynbody.tipsy.TipsySnap) print "Tipsy snapshot saved to {0}".format(snapshotName) # Start saving log file logFile = open(logFileName, "w") logFile.write("Generated from settings file: {0}\n".format(os.path.abspath(settings_file))) m_amu = float((m * 0.992733 / pynbody.units.m_p).in_units("1")) # Save director file if paramName is not None: try: import isaac defPar = os.path.join(ICgenDir, "default.param") pars = isaac.configparser(defPar, ftype="param") pars["achInFile"] = snapshotName pars["achOutName"] = os.path.splitext(snapshotName)[0] # Convert molecular mass from mass of proton to amu pars["dMeanMolWeight"] = m_amu isaac.configsave(pars, paramName, ftype="param") print "ChaNGa parameter file saved to {0}".format(paramName) logFile.write("ChaNGa parameter file saved to {0}\n".format(paramName)) except: print "Could not save .param file. Check that the module isaac\ was properly imported" # Continue writing to log file logFile.write("Tipsy snapshot saved to {0}\n".format(snapshotName)) logFile.write( "Surface density profile saved to {0}\n\ rho(z,r) saved to {1}\n\
def find_clumps(f, n_smooth=32, param=None, arg_string=None, seed=None, verbose=True): """ Uses skid (https://github.com/N-BodyShop/skid) to find clumps in a gaseous protoplanetary disk. The linking length used is equal to the gravitational softening length of the gas particles. The density cut-off comes from the criterion that there are n_smooth particles within the Hill sphere of a particle. This is formulated mathematically as: rho_min = 3*n_smooth*Mstar/R^3 where R is the distance from the star. The trick used here is to multiply all particle masses by R^3 before running skid so the density cut-off is: rho_min = 3*n_smooth*Mstar **ARGUMENTS** *f* : TipsySnap, or str A tipsy snapshot loaded/created by pynbody -OR- a filename pointing to a snapshot. *n_smooth* : int (optional) Number of particles used in SPH calculations. Should be the same as used in the simulation. Default = 32 *param* : str (optional) filename for a .param file for the simulation *arg_string* : str (optional) Additional arguments to be passed to skid. Cannot use -tau, -d, -m, -s, -o *seed* : int An integer used to seed the random filename generation for temporary files. Necessary for multiprocessing and should be unique for each thread. *verbose* : bool Verbosity flag. Default is True **RETURNS** *clumps* : array, int-like Array containing the group number each particle belongs to, with star particles coming after gas particles. A zero means the particle belongs to no groups """ # Parse areguments if isinstance(f, str): f = pynbody.load(f, paramfile=param) if seed is not None: np.random.seed(seed) # Estimate the linking length as the gravitational softening length tau = f.g['eps'][0] # Calculate minimum density rho_min = 3 * n_smooth * f.s['mass'][0] # Center on star. This is done because R used in hill-sphere calculations # is relative to the star star_pos = f.s['pos'].copy() f['pos'] -= star_pos # Scale mass by R^3 R = isaac.strip_units(f['rxy']) m0 = f['mass'].copy() f['mass'] *= (R + tau)**3 # Save temporary snapshot f_prefix = str(np.random.randint(np.iinfo(int).max)) f_name = f_prefix + '.std' # Save temporary .param file if param is not None: param_name = f_prefix + '.param' param_dict = isaac.configparser(param, 'param') isaac.configsave(param_dict, param_name) f.write(filename=f_name, fmt=pynbody.tipsy.TipsySnap) f['pos'] += star_pos f['mass'] = m0 command = 'totipnat < {} | skid -tau {:.2e} -d {:.2e} -m {:d} -s {:d} -o {}'\ .format(f_name, tau, rho_min, n_smooth, n_smooth, f_prefix) p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if verbose: for line in iter(p.stdout.readline, ''): print line, p.wait() # Load clumps clumps = isaac.loadhalos(f_prefix + '.grp') # Cleanup for name in glob.glob(f_prefix + '*'): os.remove(name) return clumps
# ICgen modules import calc_rho_zr import calc_rho import pos_gen_grid as pos_gen import calc_temp import calc_sigma import pos_class import make_snapshot import isaac import ICgen_settings import make_sigma ICgenDir = os.path.dirname(os.path.realpath(__file__)) param_filepath = os.path.join(ICgenDir, 'default.param') param_default = isaac.configparser(param_filepath, ftype='param') class IC: def __init__(self): # Initialize # Load up default settings self.settings = ICgen_settings.settings() # Add modules/attributes self.T = calc_temp.T(self) self.maker = maker(self) self.add = add(self) def saver(filename = None): """
# ICgen modules import calc_rho_zr import calc_rho import pos_gen_grid as pos_gen import calc_temp import calc_sigma import pos_class import make_snapshot import isaac import ICgen_settings import make_sigma ICgenDir = os.path.dirname(os.path.realpath(__file__)) param_filepath = os.path.join(ICgenDir, 'default.param') param_default = isaac.configparser(param_filepath, ftype='param') class IC: def __init__(self): # Initialize # Load up default settings self.settings = ICgen_settings.settings() # Add modules/attributes self.T = calc_temp.T(self) self.maker = maker(self) self.add = add(self) def saver(filename=None): """