Skip to content

Add loading a source from a .gkyl file - #1087

Open
Maxwell-Rosen wants to merge 1 commit into
mainfrom
source-from-file
Open

Add loading a source from a .gkyl file#1087
Maxwell-Rosen wants to merge 1 commit into
mainfrom
source-from-file

Conversation

@Maxwell-Rosen

@Maxwell-Rosen Maxwell-Rosen commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Adds GKYL_SOURCE_FROMFILE, a new gkyl_source_id that lets a gyrokinetic species (or kinetic neutral species) source term be loaded directly from a .gkyl file instead of being built from a projection, mirroring the existing init_from_file / GKYL_IC_IMPORT_F mechanism used for initial conditions.

Verification

  • A new regression test (rt_gk_sheath_1x2v_p1_source_import.c) runs a sheath sim once with GKYL_PROJ_SOURCE to write out -elc_source files, then a second run reloads those same files via GKYL_SOURCE_FROMFILE; the reloaded source arrays are confirmed bit-for-bit identical to the originals.
  • Valgrind reports zero errors and zero leaks.

Bug fix caught along the way

Valgrind caught a real crash in gk_species_source_release / gk_neut_species_source_release, which unconditionally released proj_source[k] even when it was never initialized for a from-file source; both release paths are now guarded on source_id != GKYL_SOURCE_FROMFILE.

Implementation

Claude Sonnet 5 one-shot this using the prompt:
"Similar to how a distribution function can be loaded from a file
.init_from_file = {
.type = GKYL_IC_IMPORT_F,
.file_name = "gk_wham_1x2v_p1-elc_0.gkyl",
// .jacobtot_inv_file_name = "gk_wham_1x2v_p1-jacobtot_inv.gkyl",
},,
Enable sources to be loaded from files. This is specified inside as a GKYL_SOURCE_FROMFILE in the source_id of the source table"

GKYL_SOURCE_FROMFILE is implemented and verified. Summary:

What changed:
- New GKYL_SOURCE_FROMFILE value in enum gkyl_source_id (core/zero/gkyl_eqn_type.h)
- New source_import[GKYL_MAX_SOURCES] field on gkyl_gyrokinetic_source, reusing the same gkyl_gyrokinetic_ic_import struct you already know from init_from_file (file_name, jacobtot_inv_file_name, etc.)
- Extracted the read/rescale logic out of gk_species_file_import_init and gk_neut_species_kinetic_file_import_init into reusable gk_species_read_distf_from_file/gk_neut_species_kinetic_read_distf_from_file functions that write into any caller-supplied array
- Wired GKYL_SOURCE_FROMFILE into gk_species_source.c/gk_neut_species_source.c: the source shape is read once at init (asserting it's static — no evolve, no adaptive sources, no enforce_positivity for now) and _calc skips recomputation for it

Bug caught during verification: valgrind on the new regression test hit a segfault in gk_species_source_release — it unconditionally called gk_species_projection_release on proj_source[k], which is never initialized for a from-file source. Fixed by guarding that release loop (both charged and neutral species) on source_id != GKYL_SOURCE_FROMFILE.

Verification:
- gyrokinetic and gyrokinetic-unit build clean
- New regression test gyrokinetic/creg/rt_gk_sheath_1x2v_p1_source_import.c: runs a sheath sim once with GKYL_PROJ_SOURCE (writing -elc_source_0.gkyl/-ion_source_0.gkyl), then a second run with GKYL_SOURCE_FROMFILE pointing at those files — the reloaded source arrays are bit-for-bit identical to the originals
- Valgrind: 0 errors, 0 leaks
- Existing rt_gk_sheath_1x2v_p1_ic_import regression test still passes (confirms the extraction didn't break the IC-import path)
- Full ctest_gk* unit suite passes except ctest_gk_geometry_tok, which fails identically regardless of my changes (confirmed via git stash) — it's an unrelated EFIT/tokamak-geometry orthonormality issue with no file overlap with this work

Example usage now works exactly as you described, just on source instead of init_from_file:
.source = {
  .source_id = GKYL_SOURCE_FROMFILE,
  .num_sources = 1,
  .source_import[0] = {
    .type = GKYL_IC_IMPORT_F,
    .file_name = "gk_wham_1x2v_p1-elc_source_0.gkyl",
  },
},
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.

1 participant