Skip to content

Commit 4d71dee

Browse files
authored
Merge pull request #217 from angelPappas/models_documentation
Models documentation
2 parents 1754be0 + 181a344 commit 4d71dee

File tree

7 files changed

+176
-24
lines changed

7 files changed

+176
-24
lines changed

.github/write_models.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Write model documentation."""
2+
3+
from jinja2 import Environment, FileSystemLoader
4+
5+
import qpdk
6+
from qpdk.config import PATH
7+
8+
filepath_models = PATH.docs / "models.rst"
9+
template_dir = PATH.docs / "templates"
10+
11+
# Models that should NOT be plotted
12+
skip_plots = {
13+
"gamma_0_load",
14+
"launcher",
15+
"resonator_frequency",
16+
"single_admittance_element",
17+
"single_impedance_element",
18+
}
19+
20+
# Collect all public functions/classes in qpdk.models
21+
models = {
22+
name: obj
23+
for name, obj in qpdk.models.__dict__.items()
24+
if callable(obj) and not name.startswith("_")
25+
}
26+
27+
# Prepare items for the template
28+
items = [
29+
{
30+
"title": "Models",
31+
"automodule": "qpdk.models",
32+
"synopsis": "Code for S-parameter and other modelling",
33+
"members": True,
34+
"undoc_members": True,
35+
"show_inheritance": True,
36+
"functions": sorted(models.keys()), # preserves order
37+
"skip_plots": skip_plots,
38+
},
39+
{
40+
"title": "References",
41+
"bibliography_filter": "docname in docnames",
42+
},
43+
]
44+
45+
# Setup Jinja2
46+
env = Environment(loader=FileSystemLoader(template_dir), autoescape=False)
47+
template = env.get_template("models_static.rst.j2")
48+
49+
rendered = template.render(items=items)
50+
51+
with filepath_models.open("w") as f:
52+
f.write(rendered)

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ cython_debug/
156156

157157
# Documentation
158158
docs/cells.rst
159+
docs/models.rst
159160
docs/samples.rst
160161
docs/notebooks/
161162
docs/makefile_help.txt

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: all build clean convert-notebooks copy-sample-notebooks docs docs-latex docs-pdf git-rm-merged help install rm-samples run-pre setup-ipython-config test test-fail-fast test-force test-gds test-gds-fail-fast test-gds-force update-pre write-cells write-makefile-help
1+
.PHONY: all build clean convert-notebooks copy-sample-notebooks docs docs-latex docs-pdf git-rm-merged help install rm-samples run-pre setup-ipython-config test test-fail-fast test-force test-gds test-gds-fail-fast test-gds-force update-pre write-cells write-makefile-help write-models
22

33
# Based on https://gist.github.com/prwhite/8168133?permalink_comment_id=4718682#gistcomment-4718682
44
help: ##@ (Default) Print listing of key targets with their descriptions
@@ -61,6 +61,9 @@ build: ##@ Build the Python package (install build tool and create dist)
6161
write-cells: ##@ Write cell outputs into documentation notebooks (used when building docs)
6262
uv run --group docs .github/write_cells.py
6363

64+
write-models: ##@ Write model outputs into documentation notebooks (used when building docs)
65+
uv run --group docs .github/write_models.py
66+
6467
write-makefile-help: ##@ Write Makefile help output to documentation
6568
uv run --group docs .github/write_makefile_help.py
6669

@@ -77,10 +80,10 @@ setup-ipython-config: ##@ Setup IPython configuration for documentation build
7780
mkdir -p ~/.config/matplotlib/stylelib/
7881
cp docs/qpdk.mplstyle ~/.config/matplotlib/stylelib/qpdk.mplstyle
7982

80-
docs: write-cells write-makefile-help copy-sample-notebooks ##@ Build the HTML documentation
83+
docs: write-cells write-models write-makefile-help copy-sample-notebooks ##@ Build the HTML documentation
8184
uv run --group docs jb build docs
8285

83-
docs-latex: write-cells write-makefile-help copy-sample-notebooks ##@ Setup LaTeX for PDF documentation
86+
docs-latex: write-cells write-models write-makefile-help copy-sample-notebooks ##@ Setup LaTeX for PDF documentation
8487
uv run --group docs jb build docs --builder latex
8588

8689
docs-pdf: docs-latex ##@ Build PDF documentation (requires a TeXLive installation)

docs/models.rst

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{{ items[0].title }}
2+
{{ "=" * items[0].title | length }}
3+
4+
{% for func_name in items[0].functions %}
5+
6+
{{ func_name }}
7+
{{ "-" * func_name | length }}
8+
9+
.. autofunction:: qpdk.models.{{ func_name }}
10+
11+
{% if func_name not in items[0].skip_plots %}
12+
.. plot::
13+
:include-source:
14+
15+
import jax.numpy as jnp
16+
import matplotlib.pyplot as plt
17+
from qpdk.models import {{ func_name }}
18+
from qpdk import PDK
19+
20+
PDK.activate()
21+
22+
freq = jnp.linspace(2e9, 8e9, 501)
23+
freq_ghz = freq / 1e9
24+
s_model = {{ func_name }}(f=freq)
25+
26+
plt.figure()
27+
plt.title("{{ func_name }}", fontsize=14)
28+
29+
for (pout, pin), sij in s_model.items():
30+
i = int(pout[-1]) # "o2" -> 2
31+
j = int(pin[-1]) # "o1" -> 1
32+
33+
if i >= j:
34+
plt.plot(
35+
freq_ghz,
36+
20 * jnp.log10(jnp.abs(sij)),
37+
label = "$S_{" + str(i) + str(j) + "}$"
38+
)
39+
plt.xlabel("Frequency [GHz]", fontsize=12)
40+
plt.ylabel("Magnitude [dB]", fontsize=12)
41+
plt.grid(True)
42+
plt.legend()
43+
plt.show()
44+
{% endif %}
45+
46+
{% endfor %}

qpdk/models/generic.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ def short(
5656

5757

5858
def short_2_port(f: ArrayLike = jnp.array([5e9])) -> sax.SType:
59-
"""Electrical short 2-port connection Sax model."""
59+
"""Electrical short 2-port connection Sax model.
60+
61+
Args:
62+
f: Array of frequency points in Hz
63+
64+
Returns:
65+
sax.SType: S-parameters dictionary
66+
"""
6067
return short(f=f, n_ports=2)
6168

6269

qpdk/models/waveguides.py

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ def straight_shorted(
6666
Note:
6767
The port ``o2`` is internally shorted and should not be used.
6868
It seems to be a Sax limitation that we need to define at least two ports.
69+
70+
Args:
71+
**kwargs
72+
Keyword arguments forwarded to :func:`qpdk.models.straight`.
73+
See :class:`StraightModelKwargs` for the supported parameters.
74+
75+
Returns:
76+
sax.SType: S-parameters dictionary
6977
"""
7078
circuit, _ = sax.circuit(
7179
netlist={
@@ -97,23 +105,59 @@ def bend_circular(
97105
*args: Any,
98106
**kwargs: Unpack[StraightModelKwargs],
99107
) -> sax.SType:
100-
"""S-parameter model for a circular bend, wrapped to to :func:`~straight`."""
108+
"""S-parameter model for a circular bend, wrapped to to :func:`~straight`.
109+
110+
Args:
111+
*args:
112+
Positional arguments forwarded directly to :func:`straight`.
113+
114+
**kwargs:
115+
Keyword arguments forwarded directly to :func:`straight`.
116+
See :class:`StraightModelKwargs` for the supported parameters.
117+
118+
Returns:
119+
sax.SType: S-parameters dictionary
120+
"""
101121
return straight(*args, **kwargs) # pyrefly: ignore[bad-keyword-argument]
102122

103123

104124
def bend_euler(
105125
*args: Any,
106126
**kwargs: Unpack[StraightModelKwargs],
107127
) -> sax.SType:
108-
"""S-parameter model for an Euler bend, wrapped to to :func:`~straight`."""
128+
"""S-parameter model for an Euler bend, wrapped to to :func:`~straight`.
129+
130+
Args:
131+
*args:
132+
Positional arguments forwarded directly to :func:`straight`.
133+
134+
**kwargs:
135+
Keyword arguments forwarded directly to :func:`straight`.
136+
See :class:`StraightModelKwargs` for the supported parameters.
137+
138+
Returns:
139+
sax.SType: S-parameters dictionary
140+
"""
109141
return straight(*args, **kwargs) # pyrefly: ignore[bad-keyword-argument]
110142

111143

112144
def bend_s(
113145
*args: Any,
114146
**kwargs: Unpack[StraightModelKwargs],
115147
) -> sax.SType:
116-
"""S-parameter model for an S-bend, wrapped to to :func:`~straight`."""
148+
"""S-parameter model for an S-bend, wrapped to to :func:`~straight`.
149+
150+
Args:
151+
*args:
152+
Positional arguments forwarded directly to :func:`straight`.
153+
154+
**kwargs:
155+
Keyword arguments forwarded directly to :func:`straight`.
156+
See :class:`StraightModelKwargs` for the supported parameters.
157+
158+
Returns:
159+
sax.SType: S-parameters dictionary
160+
"""
117161
return straight(*args, **kwargs) # pyrefly: ignore[bad-keyword-argument]
118162

119163

@@ -135,6 +179,9 @@ def taper_cross_section(
135179
cross_section_1: Cross-section for the start of the taper.
136180
cross_section_2: Cross-section for the end of the taper.
137181
n_points: Number of segments to divide the taper into for simulation.
182+
183+
Returns:
184+
sax.SType: S-parameters dictionary
138185
"""
139186
# Ensure n_points is a concrete Python int
140187

@@ -212,7 +259,19 @@ def rectangle(
212259
*args: Any,
213260
**kwargs: Unpack[StraightModelKwargs],
214261
) -> sax.SType:
215-
"""S-parameter model for a rectangular section, wrapped to to :func:`~straight`."""
262+
"""S-parameter model for a rectangular section, wrapped to to :func:`~straight`.
263+
264+
Args:
265+
*args:
266+
Positional arguments forwarded directly to :func:`straight`.
267+
268+
**kwargs:
269+
Keyword arguments forwarded directly to :func:`straight`.
270+
See :class:`StraightModelKwargs` for the supported parameters.
271+
272+
Returns:
273+
sax.SType: S-parameters dictionary
274+
"""
216275
return straight(*args, **kwargs) # pyrefly: ignore[bad-keyword-argument]
217276

218277

0 commit comments

Comments
 (0)