From c64eefd1655ccb97db0da6ed4ba5d58f60140f09 Mon Sep 17 00:00:00 2001 From: MintoDA1 <51412913+MintoDA1@users.noreply.github.com> Date: Tue, 15 Aug 2023 14:44:24 -0400 Subject: [PATCH] Removed old cruft from when the executable was called --- .gitignore | 1 + buildscripts/build_swiftest.sh | 1 + swiftest/__init__.py | 3 +- swiftest/simulation_class.py | 76 +++++++++++----------------------- 4 files changed, 27 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index 62d904a21..b283d2285 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ dump* *.egg* swiftest-* __pycache__* +_cmake* #Documentation diff --git a/buildscripts/build_swiftest.sh b/buildscripts/build_swiftest.sh index 8ba96a4ee..294a10d50 100755 --- a/buildscripts/build_swiftest.sh +++ b/buildscripts/build_swiftest.sh @@ -107,5 +107,6 @@ printf "SKBUILD_CONFIGURE_OPTIONS: ${SKBUILD_CONFIGURE_OPTIONS}\n" printf "*********************************************************\n" python3 -m pip install build pip +python3 -m pip build python3 -m pip install . -v diff --git a/swiftest/__init__.py b/swiftest/__init__.py index 86f1b79c0..3b8bf6179 100644 --- a/swiftest/__init__.py +++ b/swiftest/__init__.py @@ -10,5 +10,4 @@ """ from .constants import * -from .simulation_class import Simulation -from ._bindings import driver \ No newline at end of file +from .simulation_class import Simulation \ No newline at end of file diff --git a/swiftest/simulation_class.py b/swiftest/simulation_class.py index ba1d411de..8264cab00 100644 --- a/swiftest/simulation_class.py +++ b/swiftest/simulation_class.py @@ -14,6 +14,7 @@ from swiftest import init_cond from swiftest import tool from swiftest import constants +from swiftest._bindings import driver from swiftest import __file__ as _pyfile import json import os @@ -25,7 +26,6 @@ from functools import partial import numpy.typing as npt import shutil -import shlex import warnings import sys from py.io import StdCaptureFD @@ -37,7 +37,16 @@ Tuple, Any ) +import contextlib +@contextlib.contextmanager +def _cwd(newdir): + olddir = os.getcwd() + os.chdir(newdir) + try: + yield + finally: + os.chdir(olddir) class Simulation(object): """ @@ -63,7 +72,7 @@ def __init__(self,read_param: bool = False, file, arguments to Simulation, the general `set_parameter` method, or the specific setters for groups of similar parameters (e.g. set_init_cond_files, set_simulation_time, etc.). Each parameter has a default value that can be overridden by an argument to Simulation(). Some argument parameters have equivalent values that - are passed to the `swiftest_driver` Fortran program via a parameter input file. When declaring a new + are passed to the Swiftest driver Fortran function via a parameter input file. When declaring a new Simulation object, parameters are chosen in the following way, from highest to lowest priority" 1. Arguments to Simulation() 2. The parameter input file given by `param_file` under the following conditions: @@ -424,14 +433,9 @@ def _type_scrub(output_data): post_message += f" dL/L0: {0.0:.5e} dE/|E0|: {0.0:+.5e}" post_message += f" Wall time / step: {0.0:.5e} s" pbar = tqdm(total=noutput, desc=pre_message, postfix=post_message, bar_format='{l_bar}{bar}{postfix}') - try: + with _cwd(self.simdir): capture = StdCaptureFD(out=False, in_=False) out,err = capture.reset() - # with subprocess.Popen(shlex.split(cmd), - # stdout=subprocess.PIPE, - # stderr=subprocess.PIPE, - # env=env, - # universal_newlines=True) as p: driver(self.integrator,str(self.param_file), "compact") for line in out: if "SWIFTEST STOP" in line: @@ -459,18 +463,18 @@ def _type_scrub(output_data): if "SWIFTEST START" in line: process_output = True - res = p.communicate() - if p.returncode != 0: - for line in res[1]: - print(line, end='') - warnings.warn("Failure in swiftest_driver", stacklevel=2) - sys.exit() - except: - warnings.warn(f"Error executing main swiftest_driver program", stacklevel=2) - res = p.communicate() - for line in res[1]: - print(line, end='') - sys.exit() + # res = p.communicate() + # if p.returncode != 0: + # for line in res[1]: + # print(line, end='') + # warnings.warn("Failure in swiftest_driver", stacklevel=2) + # sys.exit() + # except: + # warnings.warn(f"Error executing main swiftest_driver program", stacklevel=2) + # res = p.communicate() + # for line in res[1]: + # print(line, end='') + # sys.exit() pbar.close() return @@ -500,12 +504,6 @@ def run(self,dask: bool = False, **kwargs): warnings.warn(f"Running an integration is not yet supported for {self.codename}",stacklevel=2) return - if not self.binary_source.exists(): - msg = "Path to swiftest_driver has not been set!" - msg += f"\nMake sure swiftest_driver is compiled and the executable is in {str(self.binary_source.parent)}" - warnings.warn(msg,stacklevel=2) - return - if not self.restart: self.clean() @@ -943,19 +941,6 @@ def set_integrator(self, self.param['! VERSION'] = f"{self.codename} input file" update_list.append("codename") - if self.codename == "Swiftest": - self.binary_source = Path(_pyfile).parent.parent.parent.parent / "bin" / "swiftest_driver" - self.driver_executable = self.binary_source - if not self.binary_source.exists(): - warnings.warn(f"Cannot find the Swiftest driver at {str(self.binary_source)}",stacklevel=2) - self.driver_executable = None - else: - if self.binary_source.exists(): - self.driver_executable.resolve() - else: - self.binary_source = "NOT IMPLEMENTED FOR THIS CODE" - self.driver_executable = None - update_list.append("driver_executable") if self.codename == "Swifter": J2 = self.param.pop("J2",0.0) @@ -1018,7 +1003,6 @@ def get_integrator(self,arg_list: str | List[str] | None = None, verbose: bool | valid_instance_vars = {"codename": self.codename, "integrator": self.integrator, "param_file": str(self.param_file), - "driver_executable": str(self.driver_executable) } try: @@ -2770,13 +2754,6 @@ def write_param(self, else: warnings.warn('Cannot process unknown code type. Call the read_param method with a valid code name. Valid options are "Swiftest", "Swifter", or "Swift".',stacklevel=2) - # # Generate executable script - # self.driver_script = os.path.join(self.simdir, "swiftest_driver.sh") - # with open(self.driver_script, 'w') as f: - # f.write(f"#{self._shell_full}\n") - # f.write(f"cd {self.simdir}\n") - # f.write(f"{str(self.driver_executable)} {self.integrator} {str(self.param_file)} compact\n") - return def convert(self, param_file, newcodename="Swiftest", plname="pl.swiftest.in", tpname="tp.swiftest.in", @@ -3008,11 +2985,6 @@ def save(self, infile_name = Path(self.simdir) / param['NC_IN'] io.swiftest_xr2infile(ds=self.data, param=param, in_type=self.param['IN_TYPE'], infile_name=infile_name, framenum=framenum, verbose=verbose) self.write_param(param_file=param_file,**kwargs) - if not self.binary_source.exists(): - msg = "Path to swiftest_driver has not been set!" - msg += f"\nMake sure swiftest_driver is compiled and the executable is in {str(self.binary_source.parent)}" - warnings.warn(msg,stacklevel=2) - return elif codename == "Swifter": swifter_param = io.swiftest2swifter_param(param) if "rhill" in self.data: