foamlib 0.4.3__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. {foamlib-0.4.3 → foamlib-0.5.0}/PKG-INFO +13 -5
  2. {foamlib-0.4.3 → foamlib-0.5.0}/README.md +11 -4
  3. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/__init__.py +1 -3
  4. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/_cases/__init__.py +1 -2
  5. foamlib-0.5.0/foamlib/_cases/_async.py +233 -0
  6. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/_cases/_base.py +3 -184
  7. foamlib-0.5.0/foamlib/_cases/_recipes.py +299 -0
  8. foamlib-0.5.0/foamlib/_cases/_subprocess.py +86 -0
  9. foamlib-0.5.0/foamlib/_cases/_sync.py +183 -0
  10. foamlib-0.5.0/foamlib/_cases/_util.py +27 -0
  11. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/_files/_files.py +4 -8
  12. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/_files/_parsing.py +53 -46
  13. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/_files/_serialization.py +1 -1
  14. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib.egg-info/PKG-INFO +13 -5
  15. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib.egg-info/SOURCES.txt +4 -2
  16. {foamlib-0.4.3 → foamlib-0.5.0}/pyproject.toml +1 -0
  17. foamlib-0.4.3/foamlib/_cases/_async.py +0 -187
  18. foamlib-0.4.3/foamlib/_cases/_sync.py +0 -142
  19. foamlib-0.4.3/foamlib/_cases/_util.py +0 -35
  20. {foamlib-0.4.3 → foamlib-0.5.0}/LICENSE.txt +0 -0
  21. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/_files/__init__.py +0 -0
  22. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/_files/_base.py +0 -0
  23. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/_files/_io.py +0 -0
  24. {foamlib-0.4.3/foamlib → foamlib-0.5.0/foamlib/_files}/_util.py +0 -0
  25. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib/py.typed +0 -0
  26. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib.egg-info/dependency_links.txt +0 -0
  27. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib.egg-info/requires.txt +0 -0
  28. {foamlib-0.4.3 → foamlib-0.5.0}/foamlib.egg-info/top_level.txt +0 -0
  29. {foamlib-0.4.3 → foamlib-0.5.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: foamlib
3
- Version: 0.4.3
3
+ Version: 0.5.0
4
4
  Summary: A Python interface for interacting with OpenFOAM
5
5
  Author-email: "Gabriel S. Gerlero" <ggerlero@cimec.unl.edu.ar>
6
6
  Project-URL: Homepage, https://github.com/gerlero/foamlib
@@ -19,6 +19,7 @@ Classifier: Programming Language :: Python :: 3.9
19
19
  Classifier: Programming Language :: Python :: 3.10
20
20
  Classifier: Programming Language :: Python :: 3.11
21
21
  Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
22
23
  Classifier: Topic :: Scientific/Engineering
23
24
  Classifier: Topic :: Software Development
24
25
  Classifier: Typing :: Typed
@@ -67,9 +68,9 @@ Requires-Dist: foamlib[docs]; extra == "dev"
67
68
 
68
69
  It offers the following classes:
69
70
 
70
- * [`FoamFile`](https://foamlib.readthedocs.io/en/stable/#foamlib.FoamFile) (and [`FoamFieldFile`](https://foamlib.readthedocs.io/en/stable/#foamlib.FoamFieldFile)): read-write access to OpenFOAM configuration and field files as if they were Python `dict`s, using `foamlib`'s own parser. Supports both ASCII and binary field formats.
71
- * [`FoamCase`](https://foamlib.readthedocs.io/en/stable/#foamlib.FoamCase): a class for manipulating, executing and accessing the results of OpenFOAM cases.
72
- * [`AsyncFoamCase`](https://foamlib.readthedocs.io/en/stable/#foamlib.AsyncFoamCase): variant of `FoamCase` with asynchronous methods for running multiple cases at once.
71
+ * [`FoamFile`](https://foamlib.readthedocs.io/en/stable/files.html#foamlib.FoamFile) (and [`FoamFieldFile`](https://foamlib.readthedocs.io/en/stable/files.html#foamlib.FoamFieldFile)): read-write access to OpenFOAM configuration and field files as if they were Python `dict`s, using `foamlib`'s own parser. Supports both ASCII and binary field formats.
72
+ * [`FoamCase`](https://foamlib.readthedocs.io/en/stable/cases.html#foamlib.FoamCase): a class for manipulating, executing and accessing the results of OpenFOAM cases.
73
+ * [`AsyncFoamCase`](https://foamlib.readthedocs.io/en/stable/cases.html#foamlib.AsyncFoamCase): variant of `FoamCase` with asynchronous methods for running multiple cases at once.
73
74
 
74
75
  ## Get started
75
76
 
@@ -123,6 +124,14 @@ my_pitz.clean()
123
124
  my_pitz.control_dict["writeInterval"] = 10
124
125
  ```
125
126
 
127
+ ### Make multiple file reads and writes in a single go
128
+
129
+ ```python
130
+ with my_pitz.fv_schemes as f:
131
+ f["gradSchemes"]["default"] = f["divSchemes"]["default"]
132
+ f["snGradSchemes"]["default"] = "uncorrected"
133
+ ```
134
+
126
135
  ### Run a case asynchronously
127
136
 
128
137
  ```python
@@ -131,7 +140,6 @@ from foamlib import AsyncFoamCase
131
140
 
132
141
  async def run_case():
133
142
  my_pitz_async = AsyncFoamCase(my_pitz)
134
-
135
143
  await my_pitz_async.run()
136
144
 
137
145
  asyncio.run(run_case())
@@ -14,9 +14,9 @@
14
14
 
15
15
  It offers the following classes:
16
16
 
17
- * [`FoamFile`](https://foamlib.readthedocs.io/en/stable/#foamlib.FoamFile) (and [`FoamFieldFile`](https://foamlib.readthedocs.io/en/stable/#foamlib.FoamFieldFile)): read-write access to OpenFOAM configuration and field files as if they were Python `dict`s, using `foamlib`'s own parser. Supports both ASCII and binary field formats.
18
- * [`FoamCase`](https://foamlib.readthedocs.io/en/stable/#foamlib.FoamCase): a class for manipulating, executing and accessing the results of OpenFOAM cases.
19
- * [`AsyncFoamCase`](https://foamlib.readthedocs.io/en/stable/#foamlib.AsyncFoamCase): variant of `FoamCase` with asynchronous methods for running multiple cases at once.
17
+ * [`FoamFile`](https://foamlib.readthedocs.io/en/stable/files.html#foamlib.FoamFile) (and [`FoamFieldFile`](https://foamlib.readthedocs.io/en/stable/files.html#foamlib.FoamFieldFile)): read-write access to OpenFOAM configuration and field files as if they were Python `dict`s, using `foamlib`'s own parser. Supports both ASCII and binary field formats.
18
+ * [`FoamCase`](https://foamlib.readthedocs.io/en/stable/cases.html#foamlib.FoamCase): a class for manipulating, executing and accessing the results of OpenFOAM cases.
19
+ * [`AsyncFoamCase`](https://foamlib.readthedocs.io/en/stable/cases.html#foamlib.AsyncFoamCase): variant of `FoamCase` with asynchronous methods for running multiple cases at once.
20
20
 
21
21
  ## Get started
22
22
 
@@ -70,6 +70,14 @@ my_pitz.clean()
70
70
  my_pitz.control_dict["writeInterval"] = 10
71
71
  ```
72
72
 
73
+ ### Make multiple file reads and writes in a single go
74
+
75
+ ```python
76
+ with my_pitz.fv_schemes as f:
77
+ f["gradSchemes"]["default"] = f["divSchemes"]["default"]
78
+ f["snGradSchemes"]["default"] = "uncorrected"
79
+ ```
80
+
73
81
  ### Run a case asynchronously
74
82
 
75
83
  ```python
@@ -78,7 +86,6 @@ from foamlib import AsyncFoamCase
78
86
 
79
87
  async def run_case():
80
88
  my_pitz_async = AsyncFoamCase(my_pitz)
81
-
82
89
  await my_pitz_async.run()
83
90
 
84
91
  asyncio.run(run_case())
@@ -1,11 +1,10 @@
1
1
  """A Python interface for interacting with OpenFOAM."""
2
2
 
3
- __version__ = "0.4.3"
3
+ __version__ = "0.5.0"
4
4
 
5
5
  from ._cases import (
6
6
  AsyncFoamCase,
7
7
  CalledProcessError,
8
- CalledProcessWarning,
9
8
  FoamCase,
10
9
  FoamCaseBase,
11
10
  )
@@ -19,5 +18,4 @@ __all__ = [
19
18
  "FoamFieldFile",
20
19
  "FoamFileBase",
21
20
  "CalledProcessError",
22
- "CalledProcessWarning",
23
21
  ]
@@ -1,12 +1,11 @@
1
1
  from ._async import AsyncFoamCase
2
2
  from ._base import FoamCaseBase
3
+ from ._subprocess import CalledProcessError
3
4
  from ._sync import FoamCase
4
- from ._util import CalledProcessError, CalledProcessWarning
5
5
 
6
6
  __all__ = [
7
7
  "FoamCaseBase",
8
8
  "FoamCase",
9
9
  "AsyncFoamCase",
10
10
  "CalledProcessError",
11
- "CalledProcessWarning",
12
11
  ]
@@ -0,0 +1,233 @@
1
+ import asyncio
2
+ import multiprocessing
3
+ import os
4
+ import sys
5
+ import tempfile
6
+ from contextlib import asynccontextmanager
7
+ from pathlib import Path
8
+ from typing import (
9
+ Callable,
10
+ Optional,
11
+ Union,
12
+ )
13
+
14
+ if sys.version_info >= (3, 9):
15
+ from collections.abc import AsyncGenerator, Collection, Sequence
16
+ else:
17
+ from typing import AsyncGenerator, Collection, Sequence
18
+
19
+ if sys.version_info >= (3, 11):
20
+ from typing import Self
21
+ else:
22
+ from typing_extensions import Self
23
+
24
+ import aioshutil
25
+
26
+ from ._recipes import _FoamCaseRecipes
27
+ from ._subprocess import run_async
28
+ from ._util import awaitableasynccontextmanager
29
+
30
+
31
+ class AsyncFoamCase(_FoamCaseRecipes):
32
+ """
33
+ An OpenFOAM case with asynchronous support.
34
+
35
+ Provides methods for running and cleaning cases, as well as accessing files.
36
+
37
+ Access the time directories of the case as a sequence, e.g. `case[0]` or `case[-1]`.
38
+
39
+ :param path: The path to the case directory.
40
+ """
41
+
42
+ max_cpus = multiprocessing.cpu_count()
43
+ """
44
+ Maximum number of CPUs to use for running `AsyncFoamCase`s concurrently. Defaults to the number of CPUs on the system.
45
+ """
46
+
47
+ _reserved_cpus = 0
48
+ _cpus_cond = None # Cannot be initialized here yet
49
+
50
+ @staticmethod
51
+ @asynccontextmanager
52
+ async def _cpus(cpus: int) -> AsyncGenerator[None, None]:
53
+ if AsyncFoamCase._cpus_cond is None:
54
+ AsyncFoamCase._cpus_cond = asyncio.Condition()
55
+
56
+ cpus = min(cpus, AsyncFoamCase.max_cpus)
57
+ if cpus > 0:
58
+ async with AsyncFoamCase._cpus_cond:
59
+ await AsyncFoamCase._cpus_cond.wait_for(
60
+ lambda: AsyncFoamCase.max_cpus - AsyncFoamCase._reserved_cpus
61
+ >= cpus
62
+ )
63
+ AsyncFoamCase._reserved_cpus += cpus
64
+ try:
65
+ yield
66
+ finally:
67
+ if cpus > 0:
68
+ async with AsyncFoamCase._cpus_cond:
69
+ AsyncFoamCase._reserved_cpus -= cpus
70
+ AsyncFoamCase._cpus_cond.notify(cpus)
71
+
72
+ @staticmethod
73
+ async def _rmtree(
74
+ path: Union["os.PathLike[str]", str], ignore_errors: bool = False
75
+ ) -> None:
76
+ await aioshutil.rmtree(path, ignore_errors=ignore_errors) # type: ignore [call-arg]
77
+
78
+ @staticmethod
79
+ async def _copytree(
80
+ src: Union["os.PathLike[str]", str],
81
+ dest: Union["os.PathLike[str]", str],
82
+ *,
83
+ symlinks: bool = False,
84
+ ignore: Optional[
85
+ Callable[[Union["os.PathLike[str]", str], Collection[str]], Collection[str]]
86
+ ] = None,
87
+ ) -> None:
88
+ await aioshutil.copytree(src, dest, symlinks=symlinks, ignore=ignore)
89
+
90
+ async def clean(
91
+ self,
92
+ *,
93
+ script: bool = True,
94
+ check: bool = False,
95
+ ) -> None:
96
+ """
97
+ Clean this case.
98
+
99
+ :param script: If True, use an (All)clean script if it exists. If False, ignore any clean scripts.
100
+ :param check: If True, raise a CalledProcessError if the clean script returns a non-zero exit code.
101
+ """
102
+ for name, args, kwargs in self._clean_cmds(script=script, check=check):
103
+ await getattr(self, name)(*args, **kwargs)
104
+
105
+ async def _run(
106
+ self,
107
+ cmd: Union[Sequence[Union[str, "os.PathLike[str]"]], str],
108
+ *,
109
+ parallel: bool = False,
110
+ cpus: int = 1,
111
+ check: bool = True,
112
+ log: bool = True,
113
+ ) -> None:
114
+ with self._output(cmd, log=log) as (stdout, stderr):
115
+ if parallel:
116
+ if isinstance(cmd, str):
117
+ cmd = [
118
+ "mpiexec",
119
+ "-n",
120
+ str(cpus),
121
+ "/bin/sh",
122
+ "-c",
123
+ f"{cmd} -parallel",
124
+ ]
125
+ else:
126
+ cmd = ["mpiexec", "-n", str(cpus), *cmd, "-parallel"]
127
+
128
+ async with self._cpus(cpus):
129
+ await run_async(
130
+ cmd,
131
+ check=check,
132
+ cwd=self.path,
133
+ env=self._env(shell=isinstance(cmd, str)),
134
+ stdout=stdout,
135
+ stderr=stderr,
136
+ )
137
+
138
+ async def run(
139
+ self,
140
+ cmd: Optional[Union[Sequence[Union[str, "os.PathLike[str]"]], str]] = None,
141
+ *,
142
+ script: bool = True,
143
+ parallel: Optional[bool] = None,
144
+ cpus: Optional[int] = None,
145
+ check: bool = True,
146
+ ) -> None:
147
+ """
148
+ Run this case, or a specified command in the context of this case.
149
+
150
+ :param cmd: The command to run. If None, run the case. If a sequence, the first element is the command and the rest are arguments. If a string, `cmd` is executed in a shell.
151
+ :param script: If True and `cmd` is None, use an (All)run(-parallel) script if it exists for running the case. If False or no run script is found, autodetermine the command(s) needed to run the case.
152
+ :param parallel: If True, run in parallel using MPI. If None, autodetect whether to run in parallel.
153
+ :param check: If True, raise a CalledProcessError if any command returns a non-zero exit code.
154
+ """
155
+ for name, args, kwargs in self._run_cmds(
156
+ cmd=cmd, script=script, parallel=parallel, cpus=cpus, check=check
157
+ ):
158
+ await getattr(self, name)(*args, **kwargs)
159
+
160
+ async def block_mesh(self, *, check: bool = True) -> None:
161
+ """Run blockMesh on this case."""
162
+ await self.run(["blockMesh"], check=check)
163
+
164
+ async def decompose_par(self, *, check: bool = True) -> None:
165
+ """Decompose this case for parallel running."""
166
+ await self.run(["decomposePar"], check=check)
167
+
168
+ async def reconstruct_par(self, *, check: bool = True) -> None:
169
+ """Reconstruct this case after parallel running."""
170
+ await self.run(["reconstructPar"], check=check)
171
+
172
+ async def restore_0_dir(self) -> None:
173
+ """Restore the 0 directory from the 0.orig directory."""
174
+ for name, args, kwargs in self._restore_0_dir_cmds():
175
+ await getattr(self, name)(*args, **kwargs)
176
+
177
+ @awaitableasynccontextmanager
178
+ @asynccontextmanager
179
+ async def copy(
180
+ self, dst: Optional[Union["os.PathLike[str]", str]] = None
181
+ ) -> "AsyncGenerator[Self]":
182
+ """
183
+ Make a copy of this case.
184
+
185
+ Use as an async context manager to automatically delete the copy when done.
186
+
187
+ :param dst: The destination path. If None, copy to a temporary directory.
188
+ """
189
+ if dst is None:
190
+ dst = Path(tempfile.mkdtemp(), self.name)
191
+ tmp = True
192
+ else:
193
+ tmp = False
194
+
195
+ for name, args, kwargs in self._copy_cmds(dst):
196
+ await getattr(self, name)(*args, **kwargs)
197
+
198
+ yield type(self)(dst)
199
+
200
+ if tmp:
201
+ assert isinstance(dst, Path)
202
+ await self._rmtree(dst.parent)
203
+ else:
204
+ await self._rmtree(dst)
205
+
206
+ @awaitableasynccontextmanager
207
+ @asynccontextmanager
208
+ async def clone(
209
+ self, dst: Optional[Union["os.PathLike[str]", str]] = None
210
+ ) -> "AsyncGenerator[Self]":
211
+ """
212
+ Clone this case (make a clean copy).
213
+
214
+ Use as an async context manager to automatically delete the clone when done.
215
+
216
+ :param dst: The destination path. If None, clone to a temporary directory.
217
+ """
218
+ if dst is None:
219
+ dst = Path(tempfile.mkdtemp(), self.name)
220
+ tmp = True
221
+ else:
222
+ tmp = False
223
+
224
+ for name, args, kwargs in self._clone_cmds(dst):
225
+ await getattr(self, name)(*args, **kwargs)
226
+
227
+ yield type(self)(dst)
228
+
229
+ if tmp:
230
+ assert isinstance(dst, Path)
231
+ await self._rmtree(dst.parent)
232
+ else:
233
+ await self._rmtree(dst)
@@ -3,41 +3,29 @@ import shutil
3
3
  import sys
4
4
  from pathlib import Path
5
5
  from typing import (
6
- Any,
7
6
  Optional,
8
- Tuple,
9
7
  Union,
10
8
  overload,
11
9
  )
12
10
 
13
11
  if sys.version_info >= (3, 9):
14
12
  from collections.abc import (
15
- Callable,
16
- Collection,
17
- Generator,
18
13
  Iterator,
19
- Mapping,
20
14
  Sequence,
21
15
  Set,
22
16
  )
23
17
  else:
24
18
  from typing import AbstractSet as Set
25
19
  from typing import (
26
- Callable,
27
- Collection,
28
- Generator,
29
20
  Iterator,
30
- Mapping,
31
21
  Sequence,
32
22
  )
33
23
 
34
-
35
24
  from .._files import FoamFieldFile, FoamFile
36
- from .._util import is_sequence
37
25
 
38
26
 
39
27
  class FoamCaseBase(Sequence["FoamCaseBase.TimeDirectory"]):
40
- def __init__(self, path: Union[Path, str] = Path()):
28
+ def __init__(self, path: Union["os.PathLike[str]", str] = Path()):
41
29
  self.path = Path(path).absolute()
42
30
 
43
31
  class TimeDirectory(Set[FoamFieldFile]):
@@ -49,7 +37,7 @@ class FoamCaseBase(Sequence["FoamCaseBase.TimeDirectory"]):
49
37
  :param path: The path to the time directory.
50
38
  """
51
39
 
52
- def __init__(self, path: Union[Path, str]):
40
+ def __init__(self, path: Union["os.PathLike[str]", str]):
53
41
  self.path = Path(path).absolute()
54
42
 
55
43
  @property
@@ -142,184 +130,15 @@ class FoamCaseBase(Sequence["FoamCaseBase.TimeDirectory"]):
142
130
  def __len__(self) -> int:
143
131
  return len(self._times)
144
132
 
145
- def _clean_paths(self) -> Set[Path]:
146
- has_decompose_par_dict = (self.path / "system" / "decomposeParDict").is_file()
147
- has_block_mesh_dict = (self.path / "system" / "blockMeshDict").is_file()
148
-
149
- paths = set()
150
-
151
- for p in self.path.iterdir():
152
- if p.is_dir():
153
- try:
154
- t = float(p.name)
155
- except ValueError:
156
- pass
157
- else:
158
- if t != 0:
159
- paths.add(p)
160
-
161
- if has_decompose_par_dict and p.name.startswith("processor"):
162
- paths.add(p)
163
-
164
- if (self.path / "0.orig").is_dir() and (self.path / "0").is_dir():
165
- paths.add(self.path / "0")
166
-
167
- if has_block_mesh_dict and (self.path / "constant" / "polyMesh").exists():
168
- paths.add(self.path / "constant" / "polyMesh")
169
-
170
- if self._run_script() is not None:
171
- paths.update(self.path.glob("log.*"))
172
-
173
- return paths
174
-
175
133
  def __delitem__(self, key: Union[int, float, str]) -> None:
176
134
  shutil.rmtree(self[key].path)
177
135
 
178
- def _clone_ignore(
179
- self,
180
- ) -> Callable[[Union[Path, str], Collection[str]], Collection[str]]:
181
- clean_paths = self._clean_paths()
182
-
183
- def ignore(path: Union[Path, str], names: Collection[str]) -> Collection[str]:
184
- paths = {Path(path) / name for name in names}
185
- return {p.name for p in paths.intersection(clean_paths)}
186
-
187
- return ignore
188
-
189
- def _clean_script(self) -> Optional[Path]:
190
- """Return the path to the (All)clean script, or None if no clean script is found."""
191
- clean = self.path / "clean"
192
- all_clean = self.path / "Allclean"
193
-
194
- if clean.is_file():
195
- script = clean
196
- elif all_clean.is_file():
197
- script = all_clean
198
- else:
199
- return None
200
-
201
- if sys.argv and Path(sys.argv[0]).absolute() == script.absolute():
202
- return None
203
-
204
- return script if Path(sys.argv[0]).absolute() != script.absolute() else None
205
-
206
- def _run_script(self, *, parallel: Optional[bool] = None) -> Optional[Path]:
207
- """Return the path to the (All)run script, or None if no run script is found."""
208
- run = self.path / "run"
209
- run_parallel = self.path / "run-parallel"
210
- all_run = self.path / "Allrun"
211
- all_run_parallel = self.path / "Allrun-parallel"
212
-
213
- if run.is_file() or all_run.is_file():
214
- if run_parallel.is_file() or all_run_parallel.is_file():
215
- if parallel:
216
- script = (
217
- run_parallel if run_parallel.is_file() else all_run_parallel
218
- )
219
- elif parallel is False:
220
- script = run if run.is_file() else all_run
221
- else:
222
- raise ValueError(
223
- "Both (All)run and (All)run-parallel scripts are present. Please specify parallel argument."
224
- )
225
- else:
226
- script = run if run.is_file() else all_run
227
- elif parallel is not False and (
228
- run_parallel.is_file() or all_run_parallel.is_file()
229
- ):
230
- script = run_parallel if run_parallel.is_file() else all_run_parallel
231
- else:
232
- return None
233
-
234
- if sys.argv and Path(sys.argv[0]).absolute() == script.absolute():
235
- return None
236
-
237
- return script
238
-
239
- def _env(self, *, shell: bool) -> Optional[Mapping[str, str]]:
240
- sip_workaround = os.environ.get(
241
- "FOAM_LD_LIBRARY_PATH", ""
242
- ) and not os.environ.get("DYLD_LIBRARY_PATH", "")
243
-
244
- if not shell or sip_workaround:
245
- env = os.environ.copy()
246
-
247
- if not shell:
248
- env["PWD"] = str(self.path)
249
-
250
- if sip_workaround:
251
- env["DYLD_LIBRARY_PATH"] = env["FOAM_LD_LIBRARY_PATH"]
252
-
253
- return env
254
- else:
255
- return None
256
-
257
- def _run_cmds(
258
- self,
259
- cmd: Optional[Union[Sequence[Union[str, Path]], str, Path]] = None,
260
- *,
261
- script: bool = True,
262
- parallel: Optional[bool] = None,
263
- check: bool = True,
264
- ) -> Generator[Tuple[str, Sequence[Any], Mapping[str, Any]], None, None]:
265
- if cmd is not None:
266
- if parallel:
267
- cmd = self._parallel_cmd(cmd)
268
-
269
- yield ("_run", (cmd,), {"check": check})
270
- else:
271
- script_path = self._run_script(parallel=parallel) if script else None
272
-
273
- if script_path is not None:
274
- yield ("_run", ([script_path],), {"check": check})
275
-
276
- else:
277
- if not self and (self.path / "0.orig").is_dir():
278
- yield ("restore_0_dir", (), {})
279
-
280
- if (self.path / "system" / "blockMeshDict").is_file():
281
- yield ("block_mesh", (), {"check": check})
282
-
283
- if parallel is None:
284
- parallel = (
285
- self._nprocessors > 0
286
- or (self.path / "system" / "decomposeParDict").is_file()
287
- )
288
-
289
- if parallel:
290
- if (
291
- self._nprocessors == 0
292
- and (self.path / "system" / "decomposeParDict").is_file()
293
- ):
294
- yield ("decompose_par", (), {"check": check})
295
-
296
- yield (
297
- "run",
298
- ([self.application],),
299
- {"parallel": parallel, "check": check},
300
- )
301
-
302
- def _parallel_cmd(
303
- self, cmd: Union[Sequence[Union[str, Path]], str, Path]
304
- ) -> Union[Sequence[Union[str, Path]], str]:
305
- if not is_sequence(cmd):
306
- return f"mpiexec -np {self._nprocessors} {cmd} -parallel"
307
- else:
308
- return [
309
- "mpiexec",
310
- "-np",
311
- str(self._nprocessors),
312
- cmd[0],
313
- "-parallel",
314
- *cmd[1:],
315
- ]
316
-
317
136
  @property
318
137
  def name(self) -> str:
319
138
  """The name of the case."""
320
139
  return self.path.name
321
140
 
322
- def file(self, path: Union[Path, str]) -> FoamFile:
141
+ def file(self, path: Union["os.PathLike[str]", str]) -> FoamFile:
323
142
  """Return a FoamFile object for the given path in the case."""
324
143
  return FoamFile(self.path / path)
325
144