Skip to content

Commit ddbeb52

Browse files
authored
better API styling (#32)
this uses the numpydoc standard
1 parent 55fa6ff commit ddbeb52

7 files changed

Lines changed: 359 additions & 43 deletions

File tree

docs/_config.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ repository:
2525
# Add GitHub buttons to your book
2626
# See https://jupyterbook.org/customize/config.html#add-a-link-to-your-repository
2727
html:
28-
use_issues_button: true
29-
use_repository_button: true
28+
use_issues_button: True
29+
use_repository_button: True
3030
extra_footer: |
3131
<p>
3232
&copy; 2024; CC-BY-NC-SA 4.0
@@ -35,13 +35,12 @@ html:
3535
sphinx:
3636
extra_extensions:
3737
- 'sphinx.ext.autodoc'
38-
- 'sphinx.ext.autosummary'
38+
- 'sphinx.ext.napoleon'
39+
- 'sphinx.ext.viewcode'
3940
config:
40-
html_show_copyright: false
41-
nbsphinx_timeout: 300
41+
add_module_names: False
42+
html_show_copyright: False
4243
nb_execution_timeout: 300
43-
linkcheck_ignore: "https://ieeexplore.ieee.org/document/9994215"
44-
autosummary_generate: True
4544

4645
launch_buttons:
4746
binderhub_url: "https://mybinder.org"

docs/api.rst

Lines changed: 0 additions & 8 deletions
This file was deleted.

ppmpy/eigen.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,29 @@
88

99
def eigen(rho, u, p, gamma):
1010
"""Compute the left and right eigenvectors and the eigenvalues for
11-
the Euler equations. q is a single zone primitive variable state,
12-
v is a FluidVars object that identifies the components of q
11+
the Euler equations.
1312
13+
Parameters
14+
----------
15+
rho : ndarray
16+
density
17+
u : ndarray
18+
velocity
19+
p : ndarray
20+
pressure
21+
gamma : float
22+
ratio of specific heats
23+
24+
Returns
25+
-------
26+
ev : ndarray
27+
array of eigenvalues
28+
lvec : ndarray
29+
matrix of left eigenvectors, `lvec(iwave, :)` is
30+
the eigenvector for wave iwave
31+
rvec : ndarray
32+
matrix of right eigenvectors, `rvec(iwave, :)` is
33+
the eigenvector for wave iwave
1434
"""
1535

1636
# The Jacobian matrix for the primitive variable formulation of the

ppmpy/euler.py

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,44 @@ class Euler:
3333
"""A 1D compressible Euler solver using the piecewise parabolic method
3434
(PPM), following the original Colella & Woodward ideas
3535
36+
Parameters
37+
----------
38+
nx : int
39+
the number of zones
40+
C : float
41+
the CFL number
42+
fixed_dt : float, optional
43+
a fixed timestep to use for every step. In this case we
44+
do not estimate the timestep using the CFL criteria.
45+
bc_left_type : str
46+
boundary condition type at the left edge. Allowed values
47+
are: "reflect", "outflow", "periodic"
48+
bc_left_type : str
49+
boundary condition type at the right edge. Allowed values
50+
are: "reflect", "outflow", "periodic"
51+
gamma : float
52+
the ratio of specific heats
53+
init_cond : function
54+
the function to call to initialize the conserved state.
55+
This has the signature `init_cond(grid, v, gamma, U, params)`
56+
where `grid` is a `FVGrid`, `v` is a `FluidVars`, and `params`
57+
is a `dict` with any optional parameters needed for
58+
initialization.
59+
grav_func : function
60+
the function to call to compute the gravitational acceleration.
61+
This has the signature: `g = grav_func(grid, rho, params)`, where
62+
`grid` is a FVGrid`, `rho` is the density (array), and `params`
63+
is a `dict` of option parameters needed to interpret gravity.
64+
params : dict, optional
65+
a dictionary of parameters that is passed to the initial condition
66+
and gravity functions.
67+
use_hse_reconstruction : bool, optional
68+
do we subtract off HSE from pressure before doing the parabolic
69+
reconstruction?
70+
use_limiting : bool, optional
71+
do we limit the parabola coefficients?
72+
use_flattening : bool, optional
73+
do we apply flattening to the shock to smear them out?
3674
"""
3775

3876
def __init__(self, nx, C, *,
@@ -111,7 +149,13 @@ def estimate_dt(self):
111149
self.dt = self.C * self.grid.dx / np.max(np.abs(q[:, self.v.qu]) + cs)
112150

113151
def cons_to_prim(self):
114-
"""Convert the conserved variable state to primitive variables"""
152+
"""Convert the conserved variable state to primitive variables
153+
154+
Returns
155+
-------
156+
q : ndarray
157+
the primitive variable array.
158+
"""
115159

116160
q = self.grid.scratch_array(nc=self.v.nvar)
117161

@@ -163,6 +207,13 @@ def construct_parabola(self):
163207
def interface_states(self):
164208
"""Trace the primitive variables to the interfaces by integrating
165209
under the parabola and doing a characteristic projection
210+
211+
Returns
212+
-------
213+
q_left : ndarray
214+
the left primitive variable state on the interface.
215+
q_right : ndarray
216+
the right primitive variable state on the interface.
166217
"""
167218

168219
# convert to primitive variables
@@ -266,7 +317,18 @@ def interface_states(self):
266317
return q_left, q_right
267318

268319
def cons_flux(self, state):
269-
""" given an interface state, return the conservative flux"""
320+
""" given an interface state, return the conservative flux
321+
322+
Parameters
323+
----------
324+
state : RiemannState
325+
the interface state from the Riemann solver.
326+
327+
Returns
328+
-------
329+
flux : ndarray
330+
the conserved flux through the interface for the input state.
331+
"""
270332

271333
flux = np.zeros((self.v.nvar), dtype=np.float64)
272334

@@ -278,7 +340,20 @@ def cons_flux(self, state):
278340

279341
def compute_fluxes(self, q_left, q_right):
280342
"""given the left and right states, solve the Riemann
281-
problem to get the interface state and return the fluxes"""
343+
problem to get the interface state and return the fluxes
344+
345+
Parameters
346+
----------
347+
q_left : ndarray
348+
the left primitive variable state on the interface.
349+
q_right : ndarray
350+
the right primitive variable state on the interface.
351+
352+
Returns
353+
-------
354+
flux : ndarray
355+
the conserved flux through each interface.
356+
"""
282357

283358
flux = self.grid.scratch_array(nc=self.v.nvar)
284359

@@ -327,7 +402,15 @@ def advance_step(self):
327402
self.U[:, self.v.umx] * g_new)
328403

329404
def evolve(self, tmax, *, verbose=True):
330-
"""The main evolution driver to advance the state to time tmax"""
405+
"""The main evolution driver to advance the state to time tmax
406+
407+
Parameters
408+
----------
409+
tmax : float
410+
maximum simulation time to evolve to
411+
verbose : bool, optional
412+
enable / disable verbosity
413+
"""
331414

332415
while self.t < tmax:
333416

@@ -360,7 +443,15 @@ def evolve(self, tmax, *, verbose=True):
360443
bc_right_type=self.bcs_right[n])
361444

362445
def draw_prim(self, gp, ivar):
363-
"""Draw the parabola for a primitive variable (ivar) on a GridPlot object"""
446+
"""Draw the parabola for a primitive variable (ivar) on a GridPlot object
447+
448+
Parameters
449+
----------
450+
gp : GridPlot
451+
the grid plot object for the figure
452+
ivar : int
453+
the index of the primitive variable to plot
454+
"""
364455

365456
self.construct_parabola()
366457
self.q_parabola[ivar].draw_parabola(gp)

ppmpy/grid.py

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,50 @@
88

99

1010
class GridPlot:
11-
"""a container to hold info about the grid figure"""
11+
"""a container to hold info about the grid figure
12+
13+
Parameters
14+
----------
15+
fig : matplotlib.pyplot.Figure
16+
the figure object.
17+
ax : matplotlib.pyplot.Axes
18+
the axis object.
19+
lo_index : int, optional
20+
the 0-based zone index for the left edge of the plot.
21+
hi_index : int, optional
22+
the 0-based zone index for the right edge of the plot.
23+
"""
24+
1225
def __init__(self, *, fig=None, ax=None, lo_index=None, hi_index=None):
1326
self.fig = fig
1427
self.ax = ax
1528
self.lo_index = lo_index
1629
self.hi_index = hi_index
1730

1831
def show_fig(self):
19-
"""return the Figure object"""
32+
"""return the Figure object
33+
34+
Returns
35+
-------
36+
matplotlib.pyplot.Figure
37+
"""
2038
return self.fig
2139

2240

2341
class FVGrid:
24-
"""The main finite-volume grid class for holding our fluid state."""
42+
"""The main finite-volume grid class for holding our fluid state.
43+
44+
Parameters
45+
----------
46+
nx : int
47+
number of zones on the grid.
48+
ng : int
49+
number of ghost cells on each end of the grid.
50+
xmin : float, optional
51+
minimum x-coordinate.
52+
xmax : float, optional
53+
maximum x-coordinate.
54+
"""
2555

2656
def __init__(self, nx, ng, *,
2757
xmin=0.0, xmax=1.0):
@@ -43,13 +73,33 @@ def __init__(self, nx, ng, *,
4373
self.x = xmin + (np.arange(nx+2*ng)-ng+0.5)*self.dx
4474

4575
def scratch_array(self, nc=1):
46-
""" return a scratch array dimensioned for our grid """
76+
""" return a scratch array dimensioned for our grid
77+
78+
Parameters
79+
----------
80+
nc : int
81+
number of components.
82+
83+
Returns
84+
-------
85+
ndarray
86+
"""
4787
return np.squeeze(np.zeros((self.nq, nc), dtype=np.float64))
4888

4989
def ghost_fill(self, atmp, *,
5090
bc_left_type="outflow", bc_right_type="outflow"):
5191
"""fill all ghost cells with zero-gradient boundary
52-
conditions"""
92+
conditions.
93+
94+
Parameters
95+
----------
96+
atmp : ndarray
97+
the array of data defined on the `FVGrid` to fill ghost cells in
98+
bc_left_type : string
99+
boundary condition type on the left edge of the domain.
100+
bc_right_type : string
101+
boundary condition type on the left edge of the domain.
102+
"""
53103

54104
# left
55105

@@ -83,15 +133,38 @@ def ghost_fill(self, atmp, *,
83133
raise ValueError("invalid boundary condition")
84134

85135
def norm(self, e):
86-
"""compute the L2 norm of array e defined on this grid"""
136+
"""compute the L2 norm of array e defined on this grid
137+
138+
Parameters
139+
----------
140+
e : ndarray
141+
array of data defined on the grid whose norm we want to
142+
compute
143+
144+
Returns
145+
-------
146+
float
147+
"""
87148

88149
assert len(e) == self.nq
89150
return np.sqrt(self.dx * np.sum(e[self.lo:self.hi+1]**2))
90151

91152
def coarsen(self, fdata):
92153
"""coarsen an array fine defined on this grid down to a grid
93154
with 1/2 the number of zones (but the same number of ghost
94-
cells
155+
cells.
156+
157+
Parameters
158+
----------
159+
fdata : ndarray
160+
The data defined on this `FVGrid` object.
161+
162+
Returns
163+
-------
164+
FVGrid
165+
The coarse grid object.
166+
ndarray
167+
The coarsened data on the coarse grid.
95168
96169
"""
97170

@@ -106,7 +179,22 @@ def coarsen(self, fdata):
106179

107180
def draw(self, *, lo_index=None, hi_index=None, stretch=1):
108181
"""Draw a finite volume representation of the grid and return the
109-
figure and axis objects"""
182+
figure and axis objects
183+
184+
185+
Parameters
186+
----------
187+
lo_index : int
188+
0-based zone index to start the grid plot.
189+
hi_index : int
190+
0-based zone index to end the grid plot.
191+
stretch : float
192+
factor by which to stretch the vertical axis
193+
194+
Returns
195+
-------
196+
GridPlot
197+
"""
110198

111199
fig, ax = plt.subplots()
112200

@@ -120,6 +208,8 @@ def draw(self, *, lo_index=None, hi_index=None, stretch=1):
120208
else:
121209
nstop = hi_index
122210

211+
assert nstop > nstart
212+
123213
ax.plot([self.xl[nstart], self.xr[nstop]], [0, 0], color="0.25", lw=2)
124214

125215
# draw edges

0 commit comments

Comments
 (0)