|
| 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)]) |
0 commit comments