Skip to content

Commit 9565d0d

Browse files
committed
Add type annotations to processing module
1 parent 12c564d commit 9565d0d

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

stagpy/processing.py

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,60 @@
55
which the variables are evaluated.
66
"""
77

8+
from __future__ import annotations
9+
import typing
10+
811
import numpy as np
912
from scipy import integrate
1013

1114
from .error import NotAvailableError
1215

16+
if typing.TYPE_CHECKING:
17+
from typing import Tuple
18+
from numpy import ndarray
19+
from .stagyydata import StagyyData
20+
from ._step import Step
21+
1322

14-
def dtime(sdat):
23+
def dtime(sdat: StagyyData) -> Tuple[ndarray, ndarray]:
1524
"""Time increment dt.
1625
1726
Compute dt as a function of time.
1827
1928
Args:
20-
sdat (:class:`~stagpy.stagyydata.StagyyData`): a StagyyData instance.
29+
sdat: a :class:`~stagpy.stagyydata.StagyyData` instance.
2130
Returns:
22-
tuple of :class:`numpy.array`: dt and time arrays.
31+
dt and time arrays.
2332
"""
2433
time = sdat.tseries.time
2534
return time[1:] - time[:-1], time[:-1]
2635

2736

28-
def dt_dt(sdat):
37+
def dt_dt(sdat: StagyyData) -> Tuple[ndarray, ndarray]:
2938
"""Derivative of temperature.
3039
3140
Compute dT/dt as a function of time using an explicit Euler scheme.
3241
3342
Args:
34-
sdat (:class:`~stagpy.stagyydata.StagyyData`): a StagyyData instance.
43+
sdat: a :class:`~stagpy.stagyydata.StagyyData` instance.
3544
Returns:
36-
tuple of :class:`numpy.array`: derivative of temperature and time
37-
arrays.
45+
derivative of temperature and time arrays.
3846
"""
3947
temp, time, _ = sdat.tseries['Tmean']
4048
dtdt = (temp[1:] - temp[:-1]) / (time[1:] - time[:-1])
4149
return dtdt, time[:-1]
4250

4351

44-
def ebalance(sdat):
52+
def ebalance(sdat: StagyyData) -> Tuple[ndarray, ndarray]:
4553
"""Energy balance.
4654
4755
Compute Nu_t - Nu_b + V*dT/dt as a function of time using an explicit
4856
Euler scheme. This should be zero if energy is conserved.
4957
5058
Args:
51-
sdat (:class:`~stagpy.stagyydata.StagyyData`): a StagyyData instance.
59+
sdat: a :class:`~stagpy.stagyydata.StagyyData` instance.
5260
Returns:
53-
tuple of :class:`numpy.array`: energy balance and time arrays.
61+
energy balance and time arrays.
5462
"""
5563
rbot, rtop = sdat.steps[-1].rprofs.bounds
5664
if rbot != 0: # spherical
@@ -67,15 +75,15 @@ def ebalance(sdat):
6775
return ebal, time
6876

6977

70-
def mobility(sdat):
78+
def mobility(sdat: StagyyData) -> Tuple[ndarray, ndarray]:
7179
"""Plates mobility.
7280
7381
Compute the ratio vsurf / vrms.
7482
7583
Args:
76-
sdat (:class:`~stagpy.stagyydata.StagyyData`): a StagyyData instance.
84+
sdat: a :class:`~stagpy.stagyydata.StagyyData` instance.
7785
Returns:
78-
tuple of :class:`numpy.array`: mobility and time arrays.
86+
mobility and time arrays.
7987
"""
8088
time = []
8189
mob = []
@@ -85,19 +93,19 @@ def mobility(sdat):
8593
return np.array(mob), np.array(time)
8694

8795

88-
def delta_r(step):
96+
def delta_r(step: Step) -> Tuple[ndarray, ndarray]:
8997
"""Cells thickness.
9098
9199
Args:
92-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
100+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
93101
Returns:
94-
tuple of :class:`numpy.array`: the thickness of the cells and radius.
102+
the thickness of the cells and radius.
95103
"""
96104
edges = step.rprofs.walls
97105
return (edges[1:] - edges[:-1]), step.rprofs.centers
98106

99107

100-
def _scale_prof(step, rprof, rad=None):
108+
def _scale_prof(step: Step, rprof: ndarray, rad: ndarray = None) -> ndarray:
101109
"""Scale profile to take sphericity into account."""
102110
rbot, rtop = step.rprofs.bounds
103111
if rbot == 0: # not spherical
@@ -107,13 +115,13 @@ def _scale_prof(step, rprof, rad=None):
107115
return rprof * (2 * rad / (rtop + rbot))**2
108116

109117

110-
def diff_prof(step):
118+
def diff_prof(step: Step) -> Tuple[ndarray, ndarray]:
111119
"""Diffusion.
112120
113121
Args:
114-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
122+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
115123
Returns:
116-
tuple of :class:`numpy.array`: the diffusion and radius.
124+
the diffusion and radius.
117125
"""
118126
rbot, rtop = step.rprofs.bounds
119127
rad = step.rprofs.centers
@@ -126,86 +134,85 @@ def diff_prof(step):
126134
return diff, step.rprofs.walls
127135

128136

129-
def diffs_prof(step):
137+
def diffs_prof(step: Step) -> Tuple[ndarray, ndarray]:
130138
"""Scaled diffusion.
131139
132140
This computation takes sphericity into account if necessary.
133141
134142
Args:
135-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
143+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
136144
Returns:
137-
tuple of :class:`numpy.array`: the diffusion and radius.
145+
the diffusion and radius.
138146
"""
139147
diff, rad = diff_prof(step)
140148
return _scale_prof(step, diff, rad), rad
141149

142150

143-
def advts_prof(step):
151+
def advts_prof(step: Step) -> Tuple[ndarray, ndarray]:
144152
"""Scaled advection.
145153
146154
This computation takes sphericity into account if necessary.
147155
148156
Args:
149-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
157+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
150158
Returns:
151-
tuple of :class:`numpy.array`: the scaled advection and radius.
159+
the scaled advection and radius.
152160
"""
153161
return _scale_prof(step, step.rprofs['advtot'].values), step.rprofs.centers
154162

155163

156-
def advds_prof(step):
164+
def advds_prof(step: Step) -> Tuple[ndarray, ndarray]:
157165
"""Scaled downward advection.
158166
159167
This computation takes sphericity into account if necessary.
160168
161169
Args:
162-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
170+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
163171
Returns:
164-
tuple of :class:`numpy.array`: the scaled downward advection and
165-
radius.
172+
the scaled downward advection and radius.
166173
"""
167174
return (_scale_prof(step, step.rprofs['advdesc'].values),
168175
step.rprofs.centers)
169176

170177

171-
def advas_prof(step):
178+
def advas_prof(step: Step) -> Tuple[ndarray, ndarray]:
172179
"""Scaled upward advection.
173180
174181
This computation takes sphericity into account if necessary.
175182
176183
Args:
177-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
184+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
178185
Returns:
179-
tuple of :class:`numpy.array`: the scaled upward advection and radius.
186+
the scaled upward advection and radius.
180187
"""
181188
return _scale_prof(step, step.rprofs['advasc'].values), step.rprofs.centers
182189

183190

184-
def energy_prof(step):
191+
def energy_prof(step: Step) -> Tuple[ndarray, ndarray]:
185192
"""Energy flux.
186193
187194
This computation takes sphericity into account if necessary.
188195
189196
Args:
190-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
197+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
191198
Returns:
192-
tuple of :class:`numpy.array`: the energy flux and radius.
199+
the energy flux and radius.
193200
"""
194201
diff, rad = diffs_prof(step)
195202
adv, _ = advts_prof(step)
196203
return (diff + np.append(adv, 0)), rad
197204

198205

199-
def advth(step):
206+
def advth(step: Step) -> Tuple[ndarray, ndarray]:
200207
"""Theoretical advection.
201208
202209
This compute the theoretical profile of total advection as function of
203210
radius.
204211
205212
Args:
206-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
213+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
207214
Returns:
208-
tuple of :class:`numpy.array`: the theoretical advection and radius.
215+
the theoretical advection and radius.
209216
"""
210217
rbot, rtop = step.rprofs.bounds
211218
rmean = 0.5 * (rbot + rtop)
@@ -220,16 +227,16 @@ def advth(step):
220227
return th_adv, rad
221228

222229

223-
def init_c_overturn(step):
230+
def init_c_overturn(step: Step) -> Tuple[ndarray, ndarray]:
224231
"""Initial concentration.
225232
226233
This compute the resulting composition profile if fractional
227234
crystallization of a SMO is assumed.
228235
229236
Args:
230-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
237+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
231238
Returns:
232-
tuple of :class:`numpy.array`: the composition and radius.
239+
the composition and radius.
233240
"""
234241
rbot, rtop = step.rprofs.bounds
235242
xieut = step.sdat.par['tracersin']['fe_eut']
@@ -252,32 +259,31 @@ def initprof(rpos):
252259
return initprof(rad), rad
253260

254261

255-
def c_overturned(step):
262+
def c_overturned(step: Step) -> Tuple[ndarray, ndarray]:
256263
"""Theoretical overturned concentration.
257264
258265
This compute the resulting composition profile if fractional
259266
crystallization of a SMO is assumed and then a purely radial
260267
overturn happens.
261268
262269
Args:
263-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
270+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
264271
Returns:
265-
tuple of :class:`numpy.array`: the composition and radius.
272+
the composition and radius.
266273
"""
267274
rbot, rtop = step.rprofs.bounds
268275
cinit, rad = init_c_overturn(step)
269276
radf = (rtop**3 + rbot**3 - rad**3)**(1 / 3)
270277
return cinit, radf
271278

272279

273-
def stream_function(step):
280+
def stream_function(step: Step) -> ndarray:
274281
"""Stream function.
275282
276283
Args:
277-
step (:class:`~stagpy._step.Step`): a step of a StagyyData instance.
284+
step: a :class:`~stagpy._step.Step` of a StagyyData instance.
278285
Returns:
279-
:class:`numpy.array`: the stream function field, with four dimensions:
280-
x-direction, y-direction, z-direction and block.
286+
the stream function field, with four dimensions (x, y, z and block).
281287
"""
282288
if step.geom.twod_yz:
283289
x_coord = step.geom.y_walls

0 commit comments

Comments
 (0)