Skip to content

Create plasma current class#4086

Open
chris-ashe wants to merge 16 commits intomainfrom
create_plasma_current_class
Open

Create plasma current class#4086
chris-ashe wants to merge 16 commits intomainfrom
create_plasma_current_class

Conversation

@chris-ashe
Copy link
Collaborator

@chris-ashe chris-ashe commented Feb 8, 2026

This pull request introduces significant improvements to how plasma current is modeled, calculated, and visualized throughout the codebase and documentation. It refactors the plasma current logic into a dedicated PlasmaCurrent class, updates the documentation for clarity and accuracy, expands the set of tracked plasma current variables, and adapts plotting and testing to use the new structure.

Key changes include:

Refactoring and Integration of Plasma Current Calculations:

  • Introduced the new PlasmaCurrent class, centralizing all plasma current calculations, and integrated it throughout the codebase, including in the main application logic (process/main.py), plotting (process/core/io/plot_proc.py), and unit tests (tests/unit/test_physics.py).

Enhancements to Data Structure:

  • Expanded physics_variables to include a comprehensive set of plasma current calculation results from different empirical and analytic models, with initialization and documentation for each new variable.

Documentation Improvements:

  • Updated and clarified the plasma current documentation, including correcting equations to use consistent physical constants, refining explanations, and adding explicit references to calculation functions.

Plotting Updates:

  • Modified the main plotting routine to use the new plasma current comparison plot from PlasmaCurrent, adjusted subplot indexing to accommodate new plots, and increased the number of figure pages accordingly.

Testing Adaptation:

  • Refactored plasma current-related unit tests to use the new PlasmaCurrent class and removed outdated test parameters and checks, ensuring alignment with the updated calculation interface.

Checklist

I confirm that I have completed the following checks:

  • My changes follow the PROCESS style guide
  • I have justified any large differences in the regression tests caused by this pull request in the comments.
  • I have added new tests where appropriate for the changes I have made.
  • If I have had to change any existing unit or integration tests, I have justified this change in the pull request comments.
  • If I have made documentation changes, I have checked they render correctly.
  • I have added documentation for my change, if appropriate.

@chris-ashe chris-ashe added Physics Relating to the physics models Refactor labels Feb 8, 2026
@codecov-commenter
Copy link

codecov-commenter commented Feb 8, 2026

Codecov Report

❌ Patch coverage is 53.69650% with 119 lines in your changes missing coverage. Please review.
✅ Project coverage is 47.97%. Comparing base (c9a2074) to head (92b344d).

Files with missing lines Patch % Lines
process/models/physics/plasma_current.py 56.68% 81 Missing ⚠️
process/core/io/plot_proc.py 2.94% 33 Missing ⚠️
process/models/physics/physics.py 28.57% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4086      +/-   ##
==========================================
+ Coverage   47.84%   47.97%   +0.12%     
==========================================
  Files         141      142       +1     
  Lines       29754    29883     +129     
==========================================
+ Hits        14236    14335      +99     
- Misses      15518    15548      +30     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

@chris-ashe chris-ashe force-pushed the create_plasma_current_class branch from 935e8f0 to 660cc7c Compare February 26, 2026 15:00
@chris-ashe chris-ashe force-pushed the create_plasma_current_class branch from 248115d to 632de11 Compare March 6, 2026 14:33
@chris-ashe chris-ashe marked this pull request as ready for review March 16, 2026 11:46
@chris-ashe chris-ashe requested a review from a team as a code owner March 16, 2026 11:46
@timothy-nunn timothy-nunn self-assigned this Mar 16, 2026
- Updated test_physics.py to instantiate PlasmaCurrent for calculating plasma current and related methods.
- Replaced direct calls to calculate_plasma_current_peng and calculate_current_coefficient_hastie with PlasmaCurrent class methods.
- Modified test_stellarator.py to include PlasmaCurrent in the stellarator setup.
…lot_proc and update data structure in PlasmaCurrent class
@chris-ashe chris-ashe force-pushed the create_plasma_current_class branch from 539add7 to 92b344d Compare March 19, 2026 15:42
Copy link
Collaborator

@timothy-nunn timothy-nunn left a comment

Choose a reason for hiding this comment

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

There are an awful many changes in the conventional aspect ratio cases, I suspect there is a big somewhere

2026-03-19T15:48:41.5813756Z FAILED tests/regression/test_process_input_files.py::test_input_file[large_tokamak_nof] - AssertionError: �[0;32m 8593 differences: the reference MFile contains different values for some of the variables. See the warnings for a breakdown of the differences.�[0m
2026-03-19T15:48:41.5813858Z assert 8593 == 0
2026-03-19T15:48:41.5815860Z  +  where 8593 = len([MFileVariableDifference(name='pfc1t4', ref=287728.4430610426, new=-2272946.4289503414, percentage_change=-889.962370341026), MFileVariableDifference(name='i_plasma_current', ref=4.0, new=9.0, percentage_change=125.0), MFileVariableDifference(name='f_c_plasma_hcd_primary', ref=0.027082630195024323, new=0.0, percentage_change=-100.0), MFileVariableDifference(name='f_c_plasma_auxiliary', ref=0.027082630195024326, new=0.0, percentage_change=-100.0), MFileVariableDifference(name='c_hcd_primary_driven', ref=453313.9488936291, new=0.0, percentage_change=-100.0), MFileVariableDifference(name='p_hcd_injected_current_total_mw', ref=9.81398334505902, new=0.0, percentage_change=-100.0), ...])
2026-03-19T15:48:41.5816768Z FAILED tests/regression/test_process_input_files.py::test_input_file[large_tokamak_eval] - AssertionError: �[0;32m 8614 differences: the reference MFile contains different values for some of the variables. See the warnings for a breakdown of the differences.�[0m
2026-03-19T15:48:41.5816850Z assert 8614 == 0
2026-03-19T15:48:41.5818785Z  +  where 8614 = len([MFileVariableDifference(name='pfc0t4', ref=-543745.9921847769, new=-2804442.4944127165, percentage_change=-415.7633407364417), MFileVariableDifference(name='i_plasma_current', ref=4.0, new=9.0, percentage_change=125.0), MFileVariableDifference(name='f_c_plasma_hcd_primary', ref=0.010312341172393658, new=0.0, percentage_change=-100.0), MFileVariableDifference(name='f_c_plasma_auxiliary', ref=0.010312341172393658, new=0.0, percentage_change=-100.0), MFileVariableDifference(name='c_hcd_primary_driven', ref=165936.8656852688, new=0.0, percentage_change=-100.0), MFileVariableDifference(name='p_hcd_injected_current_total_mw', ref=3.413518735361287, new=0.0, percentage_change=-100.0), ...])
2026-03-19T15:48:41.5819795Z FAILED tests/regression/test_process_input_files.py::test_input_file[low_aspect_ratio_DEMO] - AssertionError: �[0;32m 7988 differences: the reference MFile contains different values for some of the variables. See the warnings for a breakdown of the differences.�[0m
2026-03-19T15:48:41.5819868Z assert 7988 == 0
2026-03-19T15:48:41.5822069Z  +  where 7988 = len([MFileVariableDifference(name='pfc1t4', ref=-66468.16490802169, new=-2160158.9627523157, percentage_change=-3149.915152225931), MFileVariableDifference(name='cs_t3', ref=8023669.20057866, new=37198927.57899087, percentage_change=363.6149204195521), MFileVariableDifference(name='cs_t2', ref=8023669.20057866, new=37198927.57899087, percentage_change=363.6149204195521), MFileVariableDifference(name='f_j_cs_start_end_flat_top', ref=-0.04323663015144943, new=-0.20045146846873732, percentage_change=-363.6149204195497), MFileVariableDifference(name='big_q_plasma', ref=33.05921861558781, new=95.42180776169131, percentage_change=188.63902946786166), MFileVariableDifference(name='i_plasma_current', ref=4.0, new=9.0, percentage_change=125.0), ...])

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you get someone to check the formula changes here

Comment on lines -1184 to -1185
expected_bp=0.96008591022564971,
expected_qstar=2.900802902105021,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any way of making another test that checks these calculations are still correct?

Comment on lines +57 to +60
Triangularity at 95% of the plasma boundary.
Returns
-------
float
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Triangularity at 95% of the plasma boundary.
Returns
-------
float
Triangularity at 95% of the plasma boundary.
Returns
-------
float

Comment on lines +61 to +62
Cylindrical safety factor (dimensionless).
Notes
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Cylindrical safety factor (dimensionless).
Notes
Cylindrical safety factor (dimensionless).
Notes

po.ovarrf(
self.outfile,
"Current density profile factor",
"\nCurrent density profile factor",
Copy link
Collaborator

Choose a reason for hiding this comment

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

This strikes me as something that will cause problems in the MFile. What is the purpose anyway?

logger = logging.getLogger(__name__)


def calculate_cylindrical_safety_factor(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could probably be jit'ed

Comment on lines +186 to +190
except ValueError:
raise ProcessValueError(
"Illegal value of i_plasma_current",
i_plasma_current=physics_variables.i_plasma_current,
) from None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
except ValueError:
raise ProcessValueError(
"Illegal value of i_plasma_current",
i_plasma_current=physics_variables.i_plasma_current,
) from None
except ValueError as e:
raise ProcessValueError(
"Illegal value of i_plasma_current",
i_plasma_current=physics_variables.i_plasma_current,
) from e

Comment on lines +301 to +327
physics_variables.c_plasma_peng_analytic = plasma_currents.get(
PlasmaCurrentModel.PENG_ANALYTIC_FIT
)
physics_variables.c_plasma_peng_double_null = plasma_currents.get(
PlasmaCurrentModel.PENG_DIVERTOR_SCALING
)
physics_variables.c_plasma_cyclindrical = plasma_currents.get(
PlasmaCurrentModel.ITER_SCALING
)
physics_variables.c_plasma_ipdg89 = plasma_currents.get(
PlasmaCurrentModel.IPDG89_SCALING
)
physics_variables.c_plasma_todd_empirical_i = plasma_currents.get(
PlasmaCurrentModel.TODD_EMPIRICAL_SCALING_I
)
physics_variables.c_plasma_todd_empirical_ii = plasma_currents.get(
PlasmaCurrentModel.TODD_EMPIRICAL_SCALING_II
)
physics_variables.c_plasma_connor_hastie = plasma_currents.get(
PlasmaCurrentModel.CONNOR_HASTIE_MODEL
)
physics_variables.c_plasma_sauter = plasma_currents.get(
PlasmaCurrentModel.SAUTER_SCALING
)
physics_variables.c_plasma_fiesta_st = plasma_currents.get(
PlasmaCurrentModel.FIESTA_ST_SCALING
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If these are only being calculated in output_plasma_current_models is it required to create new variables to hold their values? Surely these new variables won't be used anywhere else in the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Physics Relating to the physics models Refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants