Skip to content

Add IMAS↔InputGACODE round-trip conversion (dd! / IMAS.dd)#7

Open
bclyons12 wants to merge 1 commit intomasterfrom
gacode_roundtrip
Open

Add IMAS↔InputGACODE round-trip conversion (dd! / IMAS.dd)#7
bclyons12 wants to merge 1 commit intomasterfrom
gacode_roundtrip

Conversation

@bclyons12
Copy link
Member

Summary

This PR adds full round-trip conversion between InputGACODE structs and IMAS dd objects, enabling GACODE input files to be used seamlessly within IMAS-based workflows.

New API

  • dd!(dd, ig::InputGACODE) — populates an existing IMAS dd from an InputGACODE
  • IMAS.dd(ig::InputGACODE) — convenience constructor returning a new dd
  • Both are the inverse of the existing InputGACODE(dd::IMAS.dd) constructor

What's covered in the round-trip

  • Equilibrium: vacuum field, Ip, psi/phi/q/f, geometry (r_inboard/outboard, kappa, delta, zeta, zmag), fpol, MXH shape harmonics via profiles_2d grid_type 57
  • Core profiles: electrons (ne, Te), ions with two-pass thermal+fast merge matching OMFIT to_omas, vtor via rotation_frequency_tor, vpol, zeff, w0, ptot
  • Current profiles: johm, jbs, jbstor, sigmapar (stored directly to override dynamic expressions)
  • Core sources: NBI, RF (EC/IC/LH), fusion, ohmic, bremsstrahlung, synchrotron, line radiation, collisional equipartition, gas puff, recombination, charge exchange, plus RF/NB current densities

Other improvements

  • COCOS 2 sign conventions handled via transform_cocos / @cocos2 (replaces hard-coded -1.0 *)
  • update_hcd rewritten with lazy accumulation (missing instead of pre-zeroed arrays); adds qione/qioni/qcxi; guards empty profiles_1d
  • Type fields narrowed from Union{Real/AbstractString} to Union{Float64/String} for type stability
  • Ion type tokens canonicalized: [therm]/[thermal]/thermal"therm"
  • load() handles missing optional fields gracefully; legacy "qpar" alias for "qpar_beam" supported
  • New dependencies: CoordinateConventions, Statistics

Test plan

  • GACODE round-trip: file -> dd -> InputGACODE — loads the sample input.gacode, converts to dd and back, asserts field-level equality
  • GACODE round-trip: synthetic full-field coverage — constructs a fully-populated InputGACODE, round-trips through IMAS.dd and InputGACODE(dd), verifies all fields survive losslessly
  • GACODE round-trip: file-level save/load after conversion — converts file → dd → ig → saved file → reloaded, checks equality

🤖 Generated with Claude Code

Adds `dd!(dd, ig)` and `IMAS.dd(ig)` — the inverse of the existing
`InputGACODE(dd)` constructor — enabling lossless round-trips between
GACODE input files and IMAS data structures.

Key changes:
- New `dd!` function populates equilibrium, core_profiles, and
  core_sources from an InputGACODE, with correct COCOS 2 sign
  conventions via CoordinateConventions.transform_cocos / @cocos2
- Two-pass ion merge: thermal+fast rows of the same species share one
  IMAS ion entry, matching OMFIT to_omas behavior
- `InputGACODE(dd)` extended with fpol, MXH shape harmonics
  (profiles_2d grid_type 57), vtor via rotation_frequency_tor,
  current profiles (johm, jbs, jbstor, sigmapar), and RF/NB currents
- `update_hcd` rewritten with lazy accumulation (missing instead of
  zeros); adds qione/qioni/qcxi sources; guards empty profiles_1d
- ptot uses directly stored cp1d.pressure when available, falls back
  to species reconstruction
- Type fields narrowed from Union{Real/AbstractString} to
  Union{Float64/String} for type stability
- Ion type tokens canonicalized: [therm]/[thermal]/thermal → "therm"
- load() handles missing optional fields gracefully; legacy "qpar"
  alias for "qpar_beam" supported
- New deps: CoordinateConventions, Statistics
- Comprehensive round-trip tests added (file→dd→ig, synthetic
  full-field, file-level save/load)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bclyons12 bclyons12 added the enhancement New feature or request label Feb 27, 2026
@bclyons12 bclyons12 self-assigned this Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant