Skip to content

Commit 081f138

Browse files
committed
Add basic demo back in
1 parent 2d95437 commit 081f138

7 files changed

Lines changed: 108 additions & 1 deletion

File tree

docs/NAVIGATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ See https://oprypin.github.io/mkdocs-literate-nav/
66
- [Home](index.md)
77
- [Installation](installation.md)
88
- [How-to guides](how-to-guides/index.md)
9+
- [Basic demo](how-to-guides/basic-demo.py)
910
- [Tutorials](tutorials/index.md)
1011
- [Further background](further-background/index.md)
1112
- [Dependency pinning and testing](further-background/dependency-pinning-and-testing.md)

docs/how-to-guides/basic-demo.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.17.2
9+
# kernelspec:
10+
# display_name: Python 3 (ipykernel)
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %% [markdown]
16+
# # Basic demo
17+
#
18+
# Here we show a very basic demo of how to use the package.
19+
# The actual behaviour isn't so interesting,
20+
# but it demonstrates that we can wrap code
21+
# that is ultimately written in Fortran.
22+
23+
# %% [markdown]
24+
# ## Imports
25+
26+
# %%
27+
import pint
28+
29+
from example_fgen_basic.error_v import ErrorV
30+
from example_fgen_basic.error_v.creation import create_error, create_errors
31+
from example_fgen_basic.error_v.passing import pass_error, pass_errors
32+
from example_fgen_basic.get_wavelength import get_wavelength, get_wavelength_plain
33+
34+
# %% [markdown]
35+
# ## Calculation with basic types
36+
#
37+
# Here we show how we can use a basic wrapped function.
38+
# This functionality isn't actually specific to our wrappers
39+
# (you can do the same with f2py),
40+
# but it's a useful starting demonstration.
41+
42+
# %%
43+
# `_plain` because this works on plain floats,
44+
# not quantities with units (see below for this demonstration)
45+
get_wavelength_plain(400.0e12)
46+
47+
# %% [markdown]
48+
# With these python wrappers,
49+
# we can also do nice things like support interfaces that use units
50+
# (this would be much more work to implement directly in Fortran).
51+
52+
# %%
53+
ur = pint.get_application_registry()
54+
55+
# %%
56+
get_wavelength(ur.Quantity(400.0, "THz")).to("nm")
57+
58+
# %% [markdown]
59+
# ## Receiving and passing derived types
60+
#
61+
# TODO: more docs and cross-references on how this actually works
62+
63+
# %% [markdown]
64+
# We can receive a Python-equivalent of a Fortran derived type.
65+
66+
# %%
67+
create_error(3)
68+
69+
# %% [markdown]
70+
# Or multiple derived types.
71+
72+
# %%
73+
create_errors([1, 2, 1, 5])
74+
75+
# %% [markdown]
76+
# We can also pass Python-equivalent of Fortran derived types back into Fortran.
77+
78+
# %%
79+
pass_error(ErrorV(code=0))
80+
81+
# %%
82+
pass_errors([ErrorV(code=0), ErrorV(code=3), ErrorV(code=5), ErrorV(code=-2)])

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ docs = [
9494
"jupyterlab>=4.4.5",
9595
"jupytext>=1.17.2",
9696
"mkdocs-jupyter>=0.25.1",
97+
"pint>=0.24.4",
9798
]
9899
# For minimum test dependencies.
99100
# These are used when running our minimum PyPI install tests.

requirements-docs-locked.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ defusedxml==0.7.1
2828
exceptiongroup==1.3.0 ; python_full_version < '3.11'
2929
executing==2.1.0
3030
fastjsonschema==2.21.1
31+
flexcache==0.3
32+
flexparser==0.4
3133
fonttools==4.59.0
3234
ford==6.1.13
3335
fqdn==1.5.1
@@ -103,6 +105,7 @@ parso==0.8.4
103105
pathspec==0.12.1
104106
pexpect==4.9.0 ; (python_full_version < '3.10' and sys_platform == 'emscripten') or (sys_platform != 'emscripten' and sys_platform != 'win32')
105107
pillow==11.3.0
108+
pint==0.24.4
106109
platformdirs==4.3.6
107110
prometheus-client==0.21.1
108111
prompt-toolkit==3.0.48

src/example_fgen_basic/error_v/error_v.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,21 @@ def from_instance_index(cls, instance_index: int) -> ErrorV:
6060
res = cls(code=code, message=message)
6161

6262
return res
63+
64+
def build_fortran_instance(self) -> int:
65+
"""
66+
Build an instance equivalent to `self` on the Fortran side
67+
68+
Intended for use mainly by wrapping functions.
69+
Most users should not need to use this method directly.
70+
71+
Returns
72+
-------
73+
:
74+
Instance index of the object which has been created on the Fortran side
75+
"""
76+
instance_index: int = m_error_v_w.build_instance(
77+
code=self.code, message=self.message
78+
)
79+
80+
return instance_index

src/example_fgen_basic/error_v/passing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def pass_error(inv: ErrorV) -> bool:
4848
If `inv` is an error, `True`, otherwise `False`.
4949
"""
5050
# Tell Fortran to build the object on the Fortran side
51-
instance_index: int = m_error_v_w.build_instance(code=inv.code, message=inv.message)
51+
instance_index = inv.build_fortran_instance()
5252

5353
# Call the Fortran function
5454
# Boolean wrapping strategy, have to cast to bool

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)