Add loading a source from a .gkyl file - #1087
Open
Maxwell-Rosen wants to merge 1 commit into
Open
Conversation
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",
},
},
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
GKYL_SOURCE_FROMFILE, a newgkyl_source_idthat lets a gyrokinetic species (or kinetic neutral species) source term be loaded directly from a.gkylfile instead of being built from a projection, mirroring the existinginit_from_file/GKYL_IC_IMPORT_Fmechanism used for initial conditions.Verification
rt_gk_sheath_1x2v_p1_source_import.c) runs a sheath sim once withGKYL_PROJ_SOURCEto write out-elc_sourcefiles, then a second run reloads those same files viaGKYL_SOURCE_FROMFILE; the reloaded source arrays are confirmed bit-for-bit identical to the originals.Bug fix caught along the way
Valgrind caught a real crash in
gk_species_source_release/gk_neut_species_source_release, which unconditionally releasedproj_source[k]even when it was never initialized for a from-file source; both release paths are now guarded onsource_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"