Skip to content

Commit 172e304

Browse files
committed
leverage cached properties to simplify Geometry._shape
1 parent 81193ca commit 172e304

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/stagpy/step.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ class Geometry:
4040
def __init__(self, header: dict[str, Any], step: Step):
4141
self._header = header
4242
self._step = step
43-
self._shape: dict[str, Any] = {
44-
"sph": False,
45-
"cyl": False,
46-
"axi": False,
47-
}
48-
self._init_shape()
4943

5044
def _scale_radius_mo(self, radius: NDArray) -> NDArray:
5145
"""Rescale radius for evolving MO runs."""
@@ -193,42 +187,44 @@ def y_centers(self) -> NDArray:
193187
"""Same as p_centers."""
194188
return self.p_centers
195189

196-
def _init_shape(self) -> None:
197-
"""Determine shape of geometry."""
198-
shape = self._step.sdat.par.nml["geometry"]["shape"].lower()
199-
aspect = self._header["aspect"]
200-
if self._header["rcmb"] >= 0:
201-
# curvilinear
202-
self._shape["cyl"] = self.twod_xz and (
203-
shape == "cylindrical" or aspect[0] >= np.pi
204-
)
205-
self._shape["sph"] = not self._shape["cyl"]
206-
self._shape["axi"] = self.cartesian and self.twod_xz and shape == "axisymmetric"
207-
208190
@cached_property
209191
def rcmb(self) -> float:
210192
"""Radius of CMB, 0 in cartesian geometry."""
211193
return max(self._header["rcmb"], 0)
212194

213-
@property
214-
def cartesian(self) -> bool:
215-
"""Whether the grid is in cartesian geometry."""
216-
return not self.curvilinear
195+
@cached_property
196+
def _shape(self) -> str:
197+
return self._step.sdat.par.nml["geometry"]["shape"].lower()
217198

218-
@property
199+
@cached_property
219200
def curvilinear(self) -> bool:
220201
"""Whether the grid is in curvilinear geometry."""
221-
return self.spherical or self.cylindrical
202+
return self._header["rcmb"] >= 0
222203

223204
@property
205+
def cartesian(self) -> bool:
206+
"""Whether the grid is in cartesian geometry."""
207+
return not self.curvilinear
208+
209+
@cached_property
224210
def cylindrical(self) -> bool:
225211
"""Whether the grid is in cylindrical geometry (2D spherical)."""
226-
return self._shape["cyl"]
212+
aspect = self._header["aspect"]
213+
return (
214+
self.curvilinear
215+
and self.twod_xz
216+
and (self._shape == "cylindrical" or aspect[0] >= np.pi)
217+
)
227218

228-
@property
219+
@cached_property
229220
def spherical(self) -> bool:
230221
"""Whether the grid is in spherical geometry."""
231-
return self._shape["sph"]
222+
return self.curvilinear and not self.cylindrical
223+
224+
@cached_property
225+
def axisymmetric(self) -> bool:
226+
"""Whether the grid is in cartesian axisymmetric geometry."""
227+
return self.cartesian and self.twod_xz and self._shape == "axisymmetric"
232228

233229
@property
234230
def yinyang(self) -> bool:

0 commit comments

Comments
 (0)