pyro-kinetics / pyrokinetics

Python library to run and analyse gyrokinetics simulations
https://pyrokinetics.readthedocs.io/en/latest/#
GNU Lesser General Public License v3.0
25 stars 6 forks source link

Bugfix: unit conversions using simulation units #357

Closed LiamPattinson closed 5 months ago

LiamPattinson commented 5 months ago

I've been experimenting with ways to keep units consistent across many simulations (Issue https://github.com/pyro-kinetics/pyrokinetics/issues/337) while also removing the need to repeatedly rebuild the pint units cache, which is significantly slowing down our tests (Issue https://github.com/pyro-kinetics/pyrokinetics/issues/326). I should have something to show for it shortly, but it's definitely something that should wait until after our next release.

In the process of this work, I found a bug in the LocalGeometry normalisation function:

>>> import pyrokinetics as pk
>>> pyro = pk.Pyro(gk_file=pk.gk_templates["GS2"])
>>> pyro.local_geometry
<class 'pyrokinetics.local_geometry.miller.LocalGeometryMiller'>(
type  = Miller,
psi_n = 0.5 dimensionless
rho = 0.5 lref_minor_radius
Rmaj = 3.0 lref_minor_radius
Z0 = 0.0 lref_minor_radius
a_minor = 1.0 lref_minor_radius
Fpsi = 0.0 bref_B0 * lref_minor_radius
B0 = 14.142135623730951 bref_B0
q = 2.0 dimensionless
shat = 1.0 dimensionless
beta_prime = -0.04 bref_B0 ** 2 / lref_minor_radius
dpsidr = 0.253546276418555 bref_B0 * lref_minor_radius
bt_ccw = 1 dimensionless
ip_ccw = 1 dimensionless
kappa = 1.0 dimensionless
s_kappa = 0.0 dimensionless
delta = 0.0 dimensionless
s_delta = 0.0 dimensionless
shift = 0.0 dimensionless
dZ0dr = 0.0 dimensionless
bunit_over_b0 = 1.01418510567422 dimensionless
>>> pyro.local_geometry.normalise(pyro.norms.gene)
>>> pyro.local_geometry
<class 'pyrokinetics.local_geometry.miller.LocalGeometryMiller'>(
type  = Miller,
psi_n = 0.5 dimensionless
rho = nan lref_major_radius
Rmaj = nan lref_major_radius
Z0 = nan lref_major_radius
a_minor = nan lref_major_radius
Fpsi = nan bref_B0 * lref_major_radius
B0 = 14.142135623730951 bref_B0
q = 2.0 dimensionless
shat = 1.0 dimensionless
beta_prime = nan bref_B0 ** 2 / lref_major_radius
dpsidr = nan bref_B0 * lref_major_radius
bt_ccw = 1 dimensionless
ip_ccw = 1 dimensionless
kappa = 1.0 dimensionless
s_kappa = 0.0 dimensionless
delta = 0.0 dimensionless
s_delta = 0.0 dimensionless
shift = 0.0 dimensionless
dZ0dr = 0.0 dimensionless
bunit_over_b0 = 1.01418510567422 dimensionless

All quantities that depend on lref are set to nan after normalising to a convention that doesn't use lref_minor_radius. This PR fixes that so that the aspect ratio is taken into account during the conversion. I've implemented similar fixes for LocalGeometry and LocalSpecies.

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 83.18%. Comparing base (261a2a7) to head (ef9424c). Report is 2 commits behind head on unstable.

:exclamation: Current head ef9424c differs from pull request most recent head 462bf10

Please upload reports for the commit 462bf10 to get more accurate results.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## unstable #357 +/- ## ========================================= Coverage 83.18% 83.18% ========================================= Files 49 49 Lines 9121 9121 ========================================= Hits 7587 7587 Misses 1534 1534 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.