Skip to content

Take physical constants from scipy.constants - #686

Merged
mishapadidar merged 7 commits into
masterfrom
constants-from-scipy
Dec 3, 2026
Merged

mishapadidar merged 7 commits into
masterfrom
constants-from-scipy

Conversation

@jurasic-pf

@jurasic-pf jurasic-pf commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Physical constants now follow new CODATA releases.

Since the 2019 SI redefinition, mu_0 differs from 4*pi*1e-7 by about 1.4e-10 (relative), so results shift at that level.

As agreed in todays developer meeting

util/constants.py and the hardcoded mu_0 = 4*pi*1e-7 in Python now come
from scipy.constants, so they follow new CODATA releases. Since the 2019
SI redefinition mu_0 differs from 4*pi*1e-7 by ~1.4e-10 (relative).

The C++ core hardcoded mu_0/(4 pi) = 1e-7; it now uses MU0_OVER_4PI from
the new simsoptpp/constants.h (CODATA 2022), exposed as simsoptpp.MU0.
C++ cannot read scipy at compile time, so tests/field/test_constants.py
checks it against scipy.constants.mu_0 and against analytic Biot-Savart
and dipole fields.

@mishapadidar mishapadidar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple things to resolve before we move on:

  • VMEC uses its own hard coded value of mu0.
  • The Boozer/surface-objective default G still uses 4π×10⁻⁷;
  • which CODATA is used, depends on the scipy version. We may want to bump the version requirement to be after 2022 CODATA.

Comment thread src/simsoptpp/constants.h Resolved
Comment thread src/simsoptpp/constants.h Resolved
Comment thread src/simsopt/util/constants.py Resolved
Comment thread src/simsopt/mhd/vmec.py Resolved
Comment thread src/simsopt/field/wireframefield.py Resolved
Comment thread tests/field/test_biotsavart.py Resolved
@jurasic-pf

Copy link
Copy Markdown
Contributor Author

A couple things to resolve before we move on:

* VMEC uses its own hard coded value of mu0.

* The Boozer/surface-objective default G still uses 4π×10⁻⁷;

* which CODATA is used, depends on the scipy version. We may want to bump the version requirement to be after 2022 CODATA.

vmecpp can be switched to CODATA

- Revert vmec.py and vmec_diagnostics.py to VMEC's own 4*pi*1e-7.
- Use constants.mu_0 for the Boozer/surface-objective default G.
- CircularCoil.I inverts Inorm with mu_0.
- Import constants explicitly in wireframefield.py.
- Require scipy>=1.15 (CODATA 2022) and Python>=3.10.

@jurasic-pf jurasic-pf left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try again

@mishapadidar mishapadidar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. It all looks good.

The last thing I will flag is that some examples still rely on manually set values. It is best to change those so users dont copy/paste bad behavior.

See:

  Boozer: initial guess for G only
  - examples/2_Intermediate/boozer.py:25: G0 = 2. * np.pi * current_sum * (4 * np.pi * 10**(-7) / (2 * np.pi))
  - examples/2_Intermediate/boozerQA.py:45: same line
  
  Permanent magnets: mu0 = 4 * np.pi * 1e-7
  - examples/1_Simple/permanent_magnet_simple.py:50
  - examples/2_Intermediate/permanent_magnet_MUSE.py:189
  - examples/2_Intermediate/permanent_magnet_PM4Stell.py:143
  - examples/2_Intermediate/permanent_magnet_QA.py:173 
  
  Wireframe: mu0 = 4.0 * np.pi * 1e-7
  - examples/2_Intermediate/wireframe_gsco_modular.py:87
  - examples/2_Intermediate/wireframe_gsco_sector_saddle.py:97
  - examples/2_Intermediate/wireframe_rcls_basic.py:62
  - examples/2_Intermediate/wireframe_rcls_with_ports.py:108
  - examples/3_Advanced/wireframe_gsco_multistep.py:220

and the documentation,

  Example walkthroughs (code copied from the example scripts)
  - docs/source/example_permanent_magnets.rst:112: mu0 = 4 * np.pi * 1e-7
  - docs/source/example_wireframe.rst:145: mu0 = 4.0 * np.pi * 1e-7
  - docs/source/example_wireframe.rst:505: mu0 = 4.0 * np.pi * 1e-7
  
  Prose
  - docs/source/fields.rst:39: "where μ₀ = 4π × 10⁻⁷ is the vacuum permitivity". This also has the wrong term: μ₀ is the
    vacuum permeability, and "permitivity" is misspelled.

Replace hand-written 4*pi*1e-7 in the Boozer, permanent-magnet and
wireframe examples and their documentation walkthroughs so users do not
copy a value that disagrees with the library. Fix 'permitivity' ->
'permeability' in fields.rst.
The C++ kernels use mu_0/(4 pi) from CODATA 2022, so the 1e-7 prefactor
and 4*pi*1e-7 initial G guess in these tests no longer match the code
under test. Tests that compare against VMEC output keep VMEC's value.
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.94%. Comparing base (6a53194) to head (c3fb6c1).
⚠️ Report is 49 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #686      +/-   ##
==========================================
- Coverage   90.95%   90.94%   -0.01%     
==========================================
  Files          86       86              
  Lines       18781    18606     -175     
==========================================
- Hits        17082    16921     -161     
+ Misses       1699     1685      -14     
Flag Coverage Δ
unittests 90.94% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/simsopt/field/force.py 95.95% <100.00%> (+0.10%) ⬆️
src/simsopt/field/magneticfieldclasses.py 97.37% <100.00%> (+<0.01%) ⬆️
src/simsopt/field/wireframefield.py 100.00% <100.00%> (ø)
src/simsopt/geo/boozersurface.py 97.75% <100.00%> (+<0.01%) ⬆️
src/simsopt/geo/permanent_magnet_grid.py 95.62% <100.00%> (+0.01%) ⬆️
src/simsopt/geo/surfaceobjectives.py 98.87% <100.00%> (+<0.01%) ⬆️
src/simsopt/util/constants.py 100.00% <100.00%> (ø)

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

These were written by a local run of tests/mhd/test_virtual_casing.py
(VirtualCasing.from_vmec saves vcasing_<ext>.nc next to the wout file)
and are not used by any test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@mishapadidar mishapadidar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @jurasic-pf!
I think the two new test files vcasing...nc are not used here so i made a commit removing them. I think everything else looks good

@mishapadidar
mishapadidar merged commit 05258a6 into master Dec 3, 2026
40 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants