diff --git a/README.md b/README.md index 608526e..c1dcb97 100644 --- a/README.md +++ b/README.md @@ -126,11 +126,12 @@ result = differential_evolution(cost, bounds=[(-1, 1)], workers=AsyncFoamCase.ma ```python #!/usr/bin/env python3 +from pathlib import Path from foamlib import FoamCase -this_case = FoamCase() +case = FoamCase(Path(__file__).parent) # Any additional configuration here -this_case.run() +case.run() ``` ## Documentation diff --git a/foamlib/_cases/_async.py b/foamlib/_cases/_async.py index b666bc5..9a359a9 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. If None, use the current script's directory (if applicable), or the current working directory. + :param path: The path to the case directory. """ class TimeDirectory(FoamCaseRunBase.TimeDirectory): diff --git a/foamlib/_cases/_base.py b/foamlib/_cases/_base.py index fbfe0a6..bec3307 100644 --- a/foamlib/_cases/_base.py +++ b/foamlib/_cases/_base.py @@ -25,6 +25,9 @@ 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. @@ -92,15 +95,6 @@ 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 b1013e1..c77c9c2 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. If None, use the current script's directory (if applicable), or the current working directory. + :param path: The path to the case directory. """ class TimeDirectory(FoamCaseRunBase.TimeDirectory): diff --git a/tests/test_cases/test_cavity.py b/tests/test_cases/test_cavity.py index e007a3a..ec62ecf 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 foamlib import FoamCase\nFoamCase().run(parallel=False)" + "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).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 foamlib import FoamCase\nFoamCase().clean()" + "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).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 d5a1f31..151d74f 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 foamlib import FoamCase\nFoamCase().run(parallel=False)" + "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).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 foamlib import FoamCase\nFoamCase().clean()" + "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).clean()" ) clean.chmod(clean.stat().st_mode | stat.S_IEXEC)