Skip to content

Commit d7ad484

Browse files
committed
processing.*_prof return Rprof instances
Similar to stream_function returning a Field. Varr and Rprof are moved to the datatypes module.
1 parent 3502448 commit d7ad484

File tree

10 files changed

+119
-117
lines changed

10 files changed

+119
-117
lines changed

docs/sources/apiref/datatypes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ datatypes
55

66
.. autoclass:: Varf
77
.. autoclass:: Field
8+
.. autoclass:: Varr
9+
.. autoclass:: Rprof

docs/sources/apiref/phyvars.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ phyvars
2121
Dictionary of surface scalar fields output by StagYY. Keys are the
2222
variable names, values are :class:`Varf` instances.
2323

24-
.. autoclass:: Varr
25-
2624
.. data:: RPROF
2725
:annotation: = {rprofvar: Varr()}
2826

docs/sources/apiref/step.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ _step
55
:members:
66
:private-members:
77
:exclude-members: _init_shape, _get_raw_data, _scale_radius_mo,
8-
Rprof, _present_fields
9-
10-
.. autoclass:: Rprof
8+
_present_fields

stagpy/_step.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@
99
from __future__ import annotations
1010
from collections import abc
1111
from itertools import chain
12-
from typing import NamedTuple, TYPE_CHECKING
12+
import typing
1313

1414
import numpy as np
1515

16-
from . import error, phyvars, stagyyparsers, _helpers
16+
from . import error, phyvars, stagyyparsers
1717
from ._helpers import CachedReadOnlyProperty as crop
18-
from .datatypes import Field
18+
from .datatypes import Field, Rprof, Varr
1919

20-
if TYPE_CHECKING:
20+
if typing.TYPE_CHECKING:
2121
from typing import (Dict, Any, Mapping, List, Iterator, Tuple, Optional,
2222
Callable)
2323
from numpy import ndarray, signedinteger
2424
from pandas import DataFrame, Series
2525
from .datatypes import Varf
26-
from .phyvars import Varr
2726
from .stagyydata import StagyyData
2827

2928

@@ -407,30 +406,14 @@ def __iter__(self):
407406
raise TypeError('tracers collection is not iterable')
408407

409408

410-
class Rprof(NamedTuple):
411-
"""Radial profile with associated radius and metadata.
412-
413-
Attributes:
414-
values: the profile itself.
415-
rad: the radial position.
416-
meta: the metadata of the profile.
417-
"""
418-
419-
values: ndarray
420-
rad: ndarray
421-
meta: Varr
422-
423-
424409
class _Rprofs:
425410
"""Radial profiles data structure.
426411
427412
The :attr:`Step.rprofs` attribute is an instance of this class.
428413
429414
:class:`_Rprofs` implements the getitem mechanism. Keys are profile names
430-
defined in :data:`stagpy.phyvars.RPROF[_EXTRA]`. An item is a named tuple
431-
('values', 'rad', 'meta'), respectively the profile itself, the radial
432-
position at which it is evaluated, and meta is a
433-
:class:`stagpy.phyvars.Varr` instance with relevant metadata. Note that
415+
defined in :data:`stagpy.phyvars.RPROF[_EXTRA]`. Items are
416+
:class:`stagpy.datatypes.Rprof` instances. Note that
434417
profiles are automatically scaled if conf.scaling.dimensional is True.
435418
436419
Attributes:
@@ -462,15 +445,12 @@ def __getitem__(self, name: str) -> Rprof:
462445
if name in phyvars.RPROF:
463446
meta = phyvars.RPROF[name]
464447
else:
465-
meta = phyvars.Varr(name, '', '1')
448+
meta = Varr(name, '', '1')
466449
elif name in self._cached_extra:
467450
rprof, rad, meta = self._cached_extra[name]
468451
elif name in phyvars.RPROF_EXTRA:
469-
meta = phyvars.RPROF_EXTRA[name]
470-
rprof, rad = meta.description(step)
471-
meta = phyvars.Varr(_helpers.baredoc(meta.description),
472-
meta.kind, meta.dim)
473-
self._cached_extra[name] = Rprof(rprof, rad, meta)
452+
self._cached_extra[name] = phyvars.RPROF_EXTRA[name](step)
453+
rprof, rad, meta = self._cached_extra[name]
474454
else:
475455
raise error.UnknownRprofVarError(name)
476456
rprof, _ = step.sdat.scale(rprof, meta.dim)

stagpy/datatypes.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,35 @@ class Field(NamedTuple):
3030

3131
values: ndarray
3232
meta: Varf
33+
34+
35+
class Varr(NamedTuple):
36+
"""Metadata of radial profiles.
37+
38+
Attributes:
39+
description: short description of the variable if it is output by
40+
StagYY, function to compute it otherwise.
41+
kind: shorter description to group similar variables under the same
42+
label.
43+
dim: dimension used to :func:`~stagpy.stagyydata.StagyyData.scale` to
44+
dimensional values.
45+
"""
46+
47+
description: str
48+
# Callable[[Step], Tuple[ndarray, ndarray]]]
49+
kind: str
50+
dim: str
51+
52+
53+
class Rprof(NamedTuple):
54+
"""Radial profile with associated radius and metadata.
55+
56+
Attributes:
57+
values: the profile itself.
58+
rad: the radial position.
59+
meta: the metadata of the profile.
60+
"""
61+
62+
values: ndarray
63+
rad: ndarray
64+
meta: Varr

stagpy/phyvars.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
from typing import NamedTuple, TYPE_CHECKING
1212

1313
from . import processing
14-
from .datatypes import Varf
14+
from .datatypes import Varf, Varr
1515

1616
if TYPE_CHECKING:
1717
from typing import Union, Callable, Tuple
1818
from numpy import ndarray
19-
from ._step import Step
2019
from .stagyydata import StagyyData
2120

2221

@@ -138,23 +137,6 @@
138137
})
139138

140139

141-
class Varr(NamedTuple):
142-
"""Metadata of radial profiles.
143-
144-
Attributes:
145-
description: short description of the variable if it is output by
146-
StagYY, function to compute it otherwise.
147-
kind: shorter description to group similar variables under the same
148-
label.
149-
dim: dimension used to :func:`~stagpy.stagyydata.StagyyData.scale` to
150-
dimensional values.
151-
"""
152-
153-
description: Union[str, Callable[[Step], Tuple[ndarray, ndarray]]]
154-
kind: str
155-
dim: str
156-
157-
158140
RPROF = MappingProxyType({
159141
'r': Varr('Radial coordinate', 'Radius', 'm'),
160142
'Tmean': Varr('Temperature', 'Temperature', 'K'),
@@ -225,16 +207,16 @@ class Varr(NamedTuple):
225207
})
226208

227209
RPROF_EXTRA = MappingProxyType({
228-
'dr': Varr(processing.delta_r, 'dr', 'm'),
229-
'diff': Varr(processing.diff_prof, 'Heat flux', 'W/m2'),
230-
'diffs': Varr(processing.diffs_prof, 'Heat flux', 'W/m2'),
231-
'advts': Varr(processing.advts_prof, 'Heat flux', 'W/m2'),
232-
'advds': Varr(processing.advds_prof, 'Heat flux', 'W/m2'),
233-
'advas': Varr(processing.advas_prof, 'Heat flux', 'W/m2'),
234-
'energy': Varr(processing.energy_prof, 'Heat flux', 'W/m2'),
235-
'ciover': Varr(processing.init_c_overturn, 'Concentration', '1'),
236-
'cfover': Varr(processing.c_overturned, 'Concentration', '1'),
237-
'advth': Varr(processing.advth, 'Heat Flux', 'W/m2'),
210+
'dr': processing.delta_r,
211+
'diff': processing.diff_prof,
212+
'diffs': processing.diffs_prof,
213+
'advts': processing.advts_prof,
214+
'advds': processing.advds_prof,
215+
'advas': processing.advas_prof,
216+
'energy': processing.energy_prof,
217+
'ciover': processing.init_c_overturn,
218+
'cfover': processing.c_overturned,
219+
'advth': processing.advth,
238220
})
239221

240222

stagpy/processing.py

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from scipy import integrate
1313

1414
from .error import NotAvailableError
15-
from .datatypes import Field, Varf
15+
from .datatypes import Field, Varf, Rprof, Varr
1616

1717
if typing.TYPE_CHECKING:
1818
from typing import Tuple
@@ -94,16 +94,17 @@ def mobility(sdat: StagyyData) -> Tuple[ndarray, ndarray]:
9494
return np.array(mob), np.array(time)
9595

9696

97-
def delta_r(step: Step) -> Tuple[ndarray, ndarray]:
98-
"""Cells thickness.
97+
def delta_r(step: Step) -> Rprof:
98+
"""Compute cell thicknesses.
9999
100100
Args:
101101
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
102102
Returns:
103103
the thickness of the cells and radius.
104104
"""
105105
edges = step.rprofs.walls
106-
return (edges[1:] - edges[:-1]), step.rprofs.centers
106+
meta = Varr("Cell thickness", 'dr', 'm')
107+
return Rprof((edges[1:] - edges[:-1]), step.rprofs.centers, meta)
107108

108109

109110
def _scale_prof(step: Step, rprof: ndarray, rad: ndarray = None) -> ndarray:
@@ -116,8 +117,8 @@ def _scale_prof(step: Step, rprof: ndarray, rad: ndarray = None) -> ndarray:
116117
return rprof * (2 * rad / (rtop + rbot))**2
117118

118119

119-
def diff_prof(step: Step) -> Tuple[ndarray, ndarray]:
120-
"""Diffusion.
120+
def diff_prof(step: Step) -> Rprof:
121+
"""Compute diffusion flux.
121122
122123
Args:
123124
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
@@ -132,11 +133,12 @@ def diff_prof(step: Step) -> Tuple[ndarray, ndarray]:
132133
diff = np.insert(diff, 0, (1 - tprof[0]) / (rad[0] - rbot))
133134
# assume ttop = 0
134135
diff = np.append(diff, tprof[-1] / (rtop - rad[-1]))
135-
return diff, step.rprofs.walls
136+
meta = Varr("Diffusion", 'Heat flux', 'W/m2')
137+
return Rprof(diff, step.rprofs.walls, meta)
136138

137139

138-
def diffs_prof(step: Step) -> Tuple[ndarray, ndarray]:
139-
"""Scaled diffusion.
140+
def diffs_prof(step: Step) -> Rprof:
141+
"""Compute scaled diffusion flux.
140142
141143
This computation takes sphericity into account if necessary.
142144
@@ -145,12 +147,13 @@ def diffs_prof(step: Step) -> Tuple[ndarray, ndarray]:
145147
Returns:
146148
the diffusion and radius.
147149
"""
148-
diff, rad = diff_prof(step)
149-
return _scale_prof(step, diff, rad), rad
150+
diff, rad, _ = diff_prof(step)
151+
meta = Varr("Scaled diffusion", 'Heat flux', 'W/m2')
152+
return Rprof(_scale_prof(step, diff, rad), rad, meta)
150153

151154

152-
def advts_prof(step: Step) -> Tuple[ndarray, ndarray]:
153-
"""Scaled advection.
155+
def advts_prof(step: Step) -> Rprof:
156+
"""Compute scaled advection flux.
154157
155158
This computation takes sphericity into account if necessary.
156159
@@ -159,11 +162,13 @@ def advts_prof(step: Step) -> Tuple[ndarray, ndarray]:
159162
Returns:
160163
the scaled advection and radius.
161164
"""
162-
return _scale_prof(step, step.rprofs['advtot'].values), step.rprofs.centers
165+
return Rprof(_scale_prof(step, step.rprofs['advtot'].values),
166+
step.rprofs.centers,
167+
Varr("Scaled advection", 'Heat flux', 'W/m2'))
163168

164169

165-
def advds_prof(step: Step) -> Tuple[ndarray, ndarray]:
166-
"""Scaled downward advection.
170+
def advds_prof(step: Step) -> Rprof:
171+
"""Compute scaled downward advection flux.
167172
168173
This computation takes sphericity into account if necessary.
169174
@@ -172,12 +177,13 @@ def advds_prof(step: Step) -> Tuple[ndarray, ndarray]:
172177
Returns:
173178
the scaled downward advection and radius.
174179
"""
175-
return (_scale_prof(step, step.rprofs['advdesc'].values),
176-
step.rprofs.centers)
180+
return Rprof(_scale_prof(step, step.rprofs['advdesc'].values),
181+
step.rprofs.centers,
182+
Varr("Scaled downward advection", 'Heat flux', 'W/m2'))
177183

178184

179-
def advas_prof(step: Step) -> Tuple[ndarray, ndarray]:
180-
"""Scaled upward advection.
185+
def advas_prof(step: Step) -> Rprof:
186+
"""Compute scaled upward advection flux.
181187
182188
This computation takes sphericity into account if necessary.
183189
@@ -186,11 +192,13 @@ def advas_prof(step: Step) -> Tuple[ndarray, ndarray]:
186192
Returns:
187193
the scaled upward advection and radius.
188194
"""
189-
return _scale_prof(step, step.rprofs['advasc'].values), step.rprofs.centers
195+
return Rprof(_scale_prof(step, step.rprofs['advasc'].values),
196+
step.rprofs.centers,
197+
Varr("Scaled upward advection", 'Heat flux', 'W/m2'))
190198

191199

192-
def energy_prof(step: Step) -> Tuple[ndarray, ndarray]:
193-
"""Energy flux.
200+
def energy_prof(step: Step) -> Rprof:
201+
"""Compute total heat flux.
194202
195203
This computation takes sphericity into account if necessary.
196204
@@ -199,13 +207,14 @@ def energy_prof(step: Step) -> Tuple[ndarray, ndarray]:
199207
Returns:
200208
the energy flux and radius.
201209
"""
202-
diff, rad = diffs_prof(step)
203-
adv, _ = advts_prof(step)
204-
return (diff + np.append(adv, 0)), rad
210+
diff, rad, _ = diffs_prof(step)
211+
adv, _, _ = advts_prof(step)
212+
return Rprof(diff + np.append(adv, 0), rad,
213+
Varr("Total heat flux", 'Heat flux', 'W/m2'))
205214

206215

207-
def advth(step: Step) -> Tuple[ndarray, ndarray]:
208-
"""Theoretical advection.
216+
def advth(step: Step) -> Rprof:
217+
"""Compute theoretical advection.
209218
210219
This compute the theoretical profile of total advection as function of
211220
radius.
@@ -225,11 +234,12 @@ def advth(step: Step) -> Tuple[ndarray, ndarray]:
225234
th_adv = rad - rtop
226235
th_adv *= radio
227236
th_adv += step.timeinfo['Nutop']
228-
return th_adv, rad
237+
return Rprof(th_adv, rad,
238+
Varr("Theoretical advection", 'Heat flux', 'W/m2'))
229239

230240

231-
def init_c_overturn(step: Step) -> Tuple[ndarray, ndarray]:
232-
"""Initial concentration.
241+
def init_c_overturn(step: Step) -> Rprof:
242+
"""Compute concentration before overturn.
233243
234244
This compute the resulting composition profile if fractional
235245
crystallization of a SMO is assumed.
@@ -257,11 +267,12 @@ def initprof(rpos):
257267

258268
rad = np.linspace(rbot, rtop, 500)
259269
initprof = np.vectorize(initprof)
260-
return initprof(rad), rad
270+
return Rprof(initprof(rad), rad,
271+
Varr("Concentration before overturn", 'Concentration', '1'))
261272

262273

263-
def c_overturned(step: Step) -> Tuple[ndarray, ndarray]:
264-
"""Theoretical overturned concentration.
274+
def c_overturned(step: Step) -> Rprof:
275+
"""Compute theoretical overturned concentration.
265276
266277
This compute the resulting composition profile if fractional
267278
crystallization of a SMO is assumed and then a purely radial
@@ -273,9 +284,10 @@ def c_overturned(step: Step) -> Tuple[ndarray, ndarray]:
273284
the composition and radius.
274285
"""
275286
rbot, rtop = step.rprofs.bounds
276-
cinit, rad = init_c_overturn(step)
277-
radf = (rtop**3 + rbot**3 - rad**3)**(1 / 3)
278-
return cinit, radf
287+
cinit = init_c_overturn(step)
288+
radf = (rtop**3 + rbot**3 - cinit.rad**3)**(1 / 3)
289+
return Rprof(cinit.values, radf,
290+
Varr("Overturned concentration", 'Concentration', '1'))
279291

280292

281293
def stream_function(step: Step) -> Field:

0 commit comments

Comments
 (0)