diff --git a/README.md b/README.md index c1dcb97..608526e 100644 --- a/README.md +++ b/README.md @@ -126,12 +126,11 @@ result = differential_evolution(cost, bounds=[(-1, 1)], workers=AsyncFoamCase.ma ```python #!/usr/bin/env python3 -from pathlib import Path from foamlib import FoamCase -case = FoamCase(Path(__file__).parent) +this_case = FoamCase() # Any additional configuration here -case.run() +this_case.run() ``` ## Documentation diff --git a/foamlib/_cases/_async.py b/foamlib/_cases/_async.py index 9a359a9..b666bc5 100644 --- a/foamlib/_cases/_async.py +++ b/foamlib/_cases/_async.py @@ -41,7 +41,7 @@ class AsyncFoamCase(FoamCaseRunBase): Access the time directories of the case as a sequence, e.g. `case[0]` or `case[-1]`. - :param path: The path to the case directory. + :param path: The path to the case directory. If None, use the current script's directory (if applicable), or the current working directory. """ class TimeDirectory(FoamCaseRunBase.TimeDirectory): diff --git a/foamlib/_cases/_base.py b/foamlib/_cases/_base.py index bec3307..fbfe0a6 100644 --- a/foamlib/_cases/_base.py +++ b/foamlib/_cases/_base.py @@ -25,9 +25,6 @@ class FoamCaseBase(Sequence["FoamCaseBase.TimeDirectory"]): - def __init__(self, path: Union["os.PathLike[str]", str] = Path()): - self.path = Path(path).absolute() - class TimeDirectory(Set[FoamFieldFile]): """ An OpenFOAM time directory in a case. @@ -95,6 +92,15 @@ def __repr__(self) -> str: def __str__(self) -> str: return str(self.path) + def __init__(self, path: Optional[Union["os.PathLike[str]", str]] = None): + if path is None: + if sys.argv[0]: + self.path = Path(sys.argv[0]).absolute().parent + else: + self.path = Path.cwd() + else: + self.path = Path(path).absolute() + @property def _times(self) -> Sequence["FoamCaseBase.TimeDirectory"]: times = [] diff --git a/foamlib/_cases/_sync.py b/foamlib/_cases/_sync.py index c77c9c2..b1013e1 100644 --- a/foamlib/_cases/_sync.py +++ b/foamlib/_cases/_sync.py @@ -29,7 +29,7 @@ class FoamCase(FoamCaseRunBase): Access the time directories of the case as a sequence, e.g. `case[0]` or `case[-1]`. - :param path: The path to the case directory. + :param path: The path to the case directory. If None, use the current script's directory (if applicable), or the current working directory. """ class TimeDirectory(FoamCaseRunBase.TimeDirectory): diff --git a/tests/test_cases/test_cavity.py b/tests/test_cases/test_cavity.py index ec62ecf..e007a3a 100644 --- a/tests/test_cases/test_cavity.py +++ b/tests/test_cases/test_cavity.py @@ -27,7 +27,7 @@ def cavity(request: pytest.FixtureRequest) -> "Generator[FoamCase]": assert not run.exists() assert not (clone.path / "Allrun").exists() run.write_text( - "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).run(parallel=False)" + "#!/usr/bin/env python3\nfrom foamlib import FoamCase\nFoamCase().run(parallel=False)" ) run.chmod(run.stat().st_mode | stat.S_IEXEC) @@ -35,7 +35,7 @@ def cavity(request: pytest.FixtureRequest) -> "Generator[FoamCase]": assert not clean.exists() assert not (clone.path / "Allclean").exists() clean.write_text( - "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).clean()" + "#!/usr/bin/env python3\nfrom foamlib import FoamCase\nFoamCase().clean()" ) clean.chmod(clean.stat().st_mode | stat.S_IEXEC) diff --git a/tests/test_cases/test_cavity_async.py b/tests/test_cases/test_cavity_async.py index 151d74f..d5a1f31 100644 --- a/tests/test_cases/test_cavity_async.py +++ b/tests/test_cases/test_cavity_async.py @@ -28,7 +28,7 @@ async def cavity(request: pytest.FixtureRequest) -> "AsyncGenerator[AsyncFoamCas assert not run.exists() assert not (clone.path / "Allrun").exists() run.write_text( - "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).run(parallel=False)" + "#!/usr/bin/env python3\nfrom foamlib import FoamCase\nFoamCase().run(parallel=False)" ) run.chmod(run.stat().st_mode | stat.S_IEXEC) @@ -36,7 +36,7 @@ async def cavity(request: pytest.FixtureRequest) -> "AsyncGenerator[AsyncFoamCas assert not clean.exists() assert not (clone.path / "Allclean").exists() clean.write_text( - "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).clean()" + "#!/usr/bin/env python3\nfrom foamlib import FoamCase\nFoamCase().clean()" ) clean.chmod(clean.stat().st_mode | stat.S_IEXEC)