def __init__(self, path=None, pkg=None, import_legacy=False): """ Constructor. """ self.pkg = pkg if self.pkg == None: from code_saturne.cs_package import package pkg = package() self.run_conf = None self.path = path # Configuration-based information i_c = cs_run_conf.get_install_config_info(self.pkg) self.resource_name = cs_run_conf.get_resource_name(i_c) self.compute_builds = i_c['compute_builds'] self.batch = cs_batch.batch(self.pkg) # Convert from legacy runcase if not updated yet # (delaying application of file changes to save). # in this case, the run_conf object is pre-loaded so that # the "save" operation can apply the (conversion) changes # even when the configuration file has not been loaded. self.runcase_path = None if self.path: if import_legacy and not os.path.isfile(self.path): dirname = os.path.dirname(self.path) if os.path.basename(dirname) == 'DATA': dirname = os.path.join(os.path.basename(dirname), 'SCRIPTS') runcase_path = os.path.join(dirname, 'runcase') if os.path.isfile(runcase_path): self.runcase_path = runcase_path if self.runcase_path: from code_saturne import cs_runcase runcase = cs_runcase.runcase(runcase_path, package=self.pkg) sections = runcase.run_conf_sections(resource_name=self.resource_name, batch_template=i_c['batch']) self.run_conf = cs_run_conf.run_conf(self.path, package=self.pkg, create_if_missing=True) for sn in sections: if not sn in self.run_conf.sections: run_conf.sections[sn] = {} for kw in sections[sn]: self.run_conf.sections[sn][kw] = sections[sn][kw]
def __build_run_cfg__(self, distrep, casename, coupling=None, cathare_path=None): """ Retrieve batch file for the current system Update batch file for the study """ run_conf_path = os.path.join(distrep, 'run.cfg') if self.copy is not None: ref_run_conf_path = os.path.join(self.copy, 'DATA', 'run.cfg') try: shutil.copy(ref_run_conf_path, run_conf_path) except Exception: pass # Add info from parent in case of copy run_conf = cs_run_conf.run_conf(run_conf_path, package=self.package, create_if_missing=True) if coupling: run_conf.set('setup', 'coupling', coupling) # If a cathare LIBPATH is given, it is added to LD_LIBRARY_PATH. # This modification is needed for the dlopen of the cathare .so file if cathare_path: i_c = cs_run_conf.get_install_config_info(self.package) resource_name = cs_run_conf.get_resource_name(i_c) v25_3_line = "export v25_3=%s\n" % cathare_path new_line = "export LD_PATH_LIBRARY=$v25_3/%s/" + ":$LD_LIBRARY_PATH\n" add_lines = v25_3_line add_lines += new_line % ("lib") add_lines += new_line % ("ICoCo/lib") run_conf.set(resource_name, 'compute_prologue', add_lines) run_conf.save()
def update_case(options, pkg): topdir = os.getcwd() study_name = os.path.basename(os.getcwd()) i_c = cs_run_conf.get_install_config_info(pkg) resource_name = cs_run_conf.get_resource_name(i_c) for case in options.case_names: os.chdir(topdir) if case == ".": case, staging_dir = get_case_dir() if not case: sys.stderr.write(" o Skipping '%s', which does not seem " "to be a case directory\n" % topdir) continue casename = os.path.basename(case) else: casename = case if options.verbose > 0: sys.stdout.write(" o Updating case '%s' paths...\n" % casename) datadir = os.path.join(pkg.get_dir("pkgdatadir")) os.chdir(case) # Write a local wrapper to main command data = 'DATA' if not os.path.isdir(data): os.mkdir(data) dataref_distpath = os.path.join(datadir, 'user') user = os.path.join(data, 'REFERENCE') # Only update user_scripts reference, not data # (we should try to deprecate the copying of reference data # or use the GUI to align it with the active options) if os.path.exists(user): abs_f = os.path.join(datadir, 'data', 'user', 'cs_user_scripts.py') shutil.copy(abs_f, user) unset_executable(user) for s in ("SaturneGUI", "NeptuneGUI"): old_gui_script = os.path.join(data, s) if os.path.isfile(old_gui_script): os.remove(old_gui_script) # Rebuild launch script create_local_launcher(pkg, data) # User source files directory src = 'SRC' if not os.path.isdir(src): os.mkdir(src) user_ref_distpath = os.path.join(datadir, 'user_sources') for srcdir in ('REFERENCE', 'EXAMPLES', 'EXAMPLES_neptune_cfd'): if os.path.isdir(os.path.join(user_ref_distpath, srcdir)): copy_directory(os.path.join(user_ref_distpath, srcdir), os.path.join(src, srcdir), True) unset_executable(os.path.join(src, srcdir)) # Results directory (only one for all instances) resu = 'RESU' if not os.path.isdir(resu): os.mkdir(resu) # Script directory (only one for all instances) run_conf_file = os.path.join(topdir, case, 'DATA', 'run.cfg') batch_file = os.path.join(topdir, case, 'SCRIPTS', 'runcase') if sys.platform.startswith('win'): batch_file = batch_file + '.bat' run_conf = cs_run_conf.run_conf(run_conf_file, package=pkg, rebuild=True) if os.path.isfile(batch_file): runcase = cs_runcase.runcase(batch_file, package=pkg) sections = runcase.run_conf_sections(resource_name=resource_name, batch_template=i_c['batch']) for sn in sections: if not sn in run_conf.sections: run_conf.sections[sn] = {} for kw in sections[sn]: run_conf.sections[sn][kw] = sections[sn][kw] os.remove(batch_file) scripts_dir = os.path.join(topdir, case, 'SCRIPTS') try: os.rmdir(scripts_dir) except Exception: pass run_conf.save()
def create_case(self, casename): """ Create a case for a Code_Saturne study. """ casedirname = casename datadir = self.package.get_dir("pkgdatadir") data_distpath = os.path.join(datadir, 'data') if os.path.exists(casedirname): sys.stdout.write(" o Case '%s' already exists\n" % casename) return if self.verbose > 0: sys.stdout.write(" o Creating case '%s'...\n" % casename) os.mkdir(casedirname) os.chdir(casedirname) if self.copy is not None: if not (os.path.exists(os.path.join(self.copy, 'DATA', 'REFERENCE')) \ or os.path.exists(os.path.join(self.copy, 'SRC', 'REFERENCE'))): self.use_ref = False # Data directory data = 'DATA' os.mkdir(data) abs_setup_distpath = os.path.join(data_distpath, 'setup.xml') if os.path.isfile(abs_setup_distpath) and not self.copy: shutil.copy(abs_setup_distpath, data) unset_executable(data) if self.use_ref: thch_distpath = os.path.join(data_distpath, 'user') ref = os.path.join(data, 'REFERENCE') shutil.copytree(thch_distpath, ref) unset_executable(ref) # Write a wrapper for code and launching create_local_launcher(self.package, data) # Generate run.cfg file or copy one run_conf = None run_conf_path = os.path.join(data, 'run.cfg') if not self.copy: run_conf = cs_run_conf.run_conf(run_conf_path, package=self.package, rebuild=True) # User source files directory src = 'SRC' if self.use_ref: user_distpath = os.path.join(datadir, 'user_sources') shutil.copytree(user_distpath, src) unset_executable(src) else: os.mkdir(src) # Copy data and source files from another case if self.copy is not None: # Data files ref_data = os.path.join(self.copy, data) data_files = os.listdir(ref_data) for f in data_files: abs_f = os.path.join(ref_data, f) if os.path.isfile(abs_f) and \ f not in ["SaturneGUI", "NeptuneGUI", self.package.name]: shutil.copy(abs_f, data) unset_executable(os.path.join(data, f)) # Source files ref_src = os.path.join(self.copy, src) if os.path.exists(ref_src): src_files = os.listdir(ref_src) else: src_files = [] for f in src_files: abs_f = os.path.join(ref_src, f) if os.path.isfile(abs_f): shutil.copy(abs_f, src) unset_executable(os.path.join(src, f)) # If run.cfg was not present in initial case, generate it if not os.path.isfile(run_conf_path): run_conf = cs_run_conf.run_conf(run_conf_path, package=self.package, rebuild=True) # Runcase (for legacy structures) runcase_path = os.path.join(self.copy, 'SCRIPTS', 'runcase') if os.path.isfile(runcase_path): i_c = cs_run_conf.get_install_config_info(self.package) resource_name = cs_run_conf.get_resource_name(i_c) runcase = cs_runcase.runcase(runcase_path, package=self.package) sections = runcase.run_conf_sections(resource_name=resource_name, batch_template=i_c['batch']) for sn in sections: if not sn in run_conf.sections: run_conf.sections[sn] = {} for kw in sections[sn]: run_conf.sections[sn][kw] = sections[sn][kw] # Now write run.cfg if not copied if run_conf != None: run_conf.save() # Results directory resu = 'RESU' if not os.path.isdir(resu): os.mkdir(resu)
def create_case(self, casename): """ Create a case for a Code_Saturne study. """ casedirname = casename if self.verbose > 0: sys.stdout.write(" o Creating case '%s'...\n" % casename) datadir = self.package.get_dir("pkgdatadir") data_distpath = os.path.join(datadir, 'data') try: os.mkdir(casedirname) except: sys.exit(1) os.chdir(casedirname) if self.copy is not None: if not (os.path.exists(os.path.join(self.copy, 'DATA', 'REFERENCE')) \ or os.path.exists(os.path.join(self.copy, 'SRC', 'REFERENCE'))): self.use_ref = False # Data directory data = 'DATA' os.mkdir(data) abs_setup_distpath = os.path.join(data_distpath, 'setup.xml') if os.path.isfile(abs_setup_distpath) and not self.copy: shutil.copy(abs_setup_distpath, data) unset_executable(data) if self.use_ref: thch_distpath = os.path.join(data_distpath, 'thch') ref = os.path.join(data, 'REFERENCE') os.mkdir(ref) for f in [ 'dp_C3P', 'dp_C3PSJ', 'dp_C4P', 'dp_ELE', 'dp_FUE', 'dp_transformers', 'meteo' ]: abs_f = os.path.join(thch_distpath, f) if os.path.isfile(abs_f): shutil.copy(abs_f, ref) unset_executable(ref) abs_f = os.path.join(datadir, 'cs_user_scripts.py') shutil.copy(abs_f, ref) unset_executable(ref) # Write a wrapper for code and launching create_local_launcher(self.package, data) # Generate run.cfg file or copy one run_conf = None run_conf_path = os.path.join(data, 'run.cfg') if not self.copy: run_conf = cs_run_conf.run_conf(run_conf_path, package=self.package, rebuild=True) # User source files directory src = 'SRC' os.mkdir(src) if self.use_ref: user_distpath = os.path.join(datadir, 'user') user_examples_distpath = os.path.join(datadir, 'user_examples') user = os.path.join(src, 'REFERENCE') user_examples = os.path.join(src, 'EXAMPLES') shutil.copytree(user_distpath, user) shutil.copytree(user_examples_distpath, user_examples) add_datadirs = [] if self.package.name == 'neptune_cfd': add_datadirs.append( os.path.join(self.package.get_dir("datadir"), self.package.name)) for d in add_datadirs: user_distpath = os.path.join(d, 'user') user_examples_distpath = os.path.join(d, 'user_examples') if os.path.isdir(user_distpath): s_files = os.listdir(user_distpath) for f in s_files: shutil.copy(os.path.join(user_distpath, f), user) if os.path.isdir(user_examples_distpath): s_files = os.listdir(user_examples_distpath) for f in s_files: shutil.copy(os.path.join(user_examples_distpath, f), user_examples) unset_executable(user) unset_executable(user_examples) # Copy data and source files from another case if self.copy is not None: # Data files ref_data = os.path.join(self.copy, data) data_files = os.listdir(ref_data) for f in data_files: abs_f = os.path.join(ref_data, f) if os.path.isfile(abs_f) and \ f not in [self.package.guiname, self.package.name]: shutil.copy(abs_f, data) unset_executable(os.path.join(data, f)) # Source files ref_src = os.path.join(self.copy, src) if os.path.exists(ref_src): src_files = os.listdir(ref_src) else: src_files = [] for f in src_files: abs_f = os.path.join(ref_src, f) if os.path.isfile(abs_f): shutil.copy(abs_f, src) unset_executable(os.path.join(src, f)) # If run.cfg was not present in initial case, generate it if not os.path.isfile(run_conf_path): run_conf = cs_run_conf.run_conf(run_conf_path, package=self.package, rebuild=True) # Runcase (for legacy structures) runcase_path = os.path.join(self.copy, 'SCRIPTS', 'runcase') if os.path.isfile(runcase_path): i_c = cs_run_conf.get_install_config_info(self.package) resource_name = cs_run_conf.get_resource_name(i_c) runcase = cs_runcase.runcase(runcase_path, package=self.package) sections = runcase.run_conf_sections( resource_name=resource_name, batch_template=i_c['batch']) for sn in sections: if not sn in run_conf.sections: run_conf.sections[sn] = {} for kw in sections[sn]: run_conf.sections[sn][kw] = sections[sn][kw] # Now write run.cfg if not copied if run_conf != None: run_conf.save() # Results directory resu = 'RESU' if not os.path.isdir(resu): os.mkdir(resu)
def run(argv=[], pkg=None, run_args=None, submit_args=None): """ Run calculation; returns return code, run id, and results directory path when created. """ if not pkg: from code_saturne.cs_package import package pkg = package() if run_args == None: options = parse_cmd_line(argv, pkg) else: options = run_args i_c = cs_run_conf.get_install_config_info(pkg) r_c, s_c, run_conf = process_options(options, pkg) if not r_c['casedir'] and not r_c['staging_dir']: return 1, None, None # Read run configuration read_run_config_file(i_c, r_c, s_c, pkg, run_conf=run_conf) # Determine compute stages update_run_steps(s_c, None, final=True) stages = {'prepare_data': s_c['stage'], 'initialize': s_c['initialize'], 'run_solver': s_c['compute'], 'save_results': s_c['finalize']} # Use alternate compute (back-end) package if defined pkg_compute = None if not r_c['compute_build']: if i_c['compute_builds']: r_c['compute_build'] = i_c['compute_builds'][0] if r_c['compute_build']: pkg_compute = pkg.get_alternate_version(r_c['compute_build']) # Specific case for coupling if r_c['coupled_domains'] != []: from code_saturne import cs_case_coupling domains = r_c['coupled_domains'] verbose = True if r_c['suggest_id']: verbose = False c = cs_case_coupling.coupling(pkg, domains, r_c['casedir'], r_c['dest_dir'], staging_dir=r_c['staging_dir'], verbose=verbose, package_compute=pkg_compute) else: # Values in case and associated domain set from parameters d = cs_case_domain.domain(pkg, package_compute=pkg_compute, param=r_c['param']) # Now handle case for the corresponding calculation domain(s). c = cs_case.case(pkg, package_compute=pkg_compute, case_dir=r_c['casedir'], dest_dir=r_c['dest_dir'], staging_dir=r_c['staging_dir'], domains=d) # Determine run id if not forced if not r_c['run_id']: r_c['run_id'] = c.suggest_id(r_c['id_prefix'], r_c['id_suffix']) if r_c['suggest_id']: print(r_c['run_id']) return 0, r_c['run_id'], None if submit_args != None: submit_stages = {'prepare_data': False} for s in ('initialize', 'run_solver', 'save_results'): submit_stages[s] = stages[s] stages[s] = False c.run_prologue = r_c['run_prologue'] c.run_epilogue = r_c['run_epilogue'] c.compute_prologue = r_c['compute_prologue'] c.compute_epilogue = r_c['compute_epilogue'] # Now run case retval = c.run(n_procs=r_c['n_procs'], n_threads=r_c['n_threads'], time_limit=r_c['time_limit'], run_id=r_c['run_id'], force_id=r_c['force_id'], stages=stages) if submit_args != None: resource_name = cs_run_conf.get_resource_name(i_c) run_cfg_path = os.path.join(c.result_dir, 'run.cfg') if len(submit_args) > 1: job_parameters = cs_exec_environment.assemble_args(submit_args) r_c['job_parameters'] = job_parameters generate_run_config_file(run_cfg_path, resource_name, r_c, submit_stages, pkg) return retval, c.result_dir, r_c