Coupling with an AI model for sheath BC in GK simulations - #982
Open
Antoinehoff wants to merge 157 commits into
Open
Coupling with an AI model for sheath BC in GK simulations#982Antoinehoff wants to merge 157 commits into
Antoinehoff wants to merge 157 commits into
Conversation
…ce of vparcut. The kernels are updated accordingly, the sheath test looks ok when alpha=1. The kernels are not tested yet for non unity alpha.
… gk_sheath_vcut_mu_dep
…t by default, i.e. alpha_mu = 1.0
…ject because the vel_map is not using the gk hybrid basis.
…d checking cells that are crossed by the vcut because they may have 0 or non 0 features in an impredictive way.
…he only method is to set up an array outside using the get_basis routine and pass it using the set_alpha_mu routine.
… between the perpendicular conf. directions and mu. We can now evaluate vpar cut everywhere (with the surrogate) and store it before calling the kernels.
…ogate. This commit does not contain the surrogate itself.
…ected cells is back
- Pass q2Dm to convert the surrogate output to a factor of vcut const - add an interface to evaluate the surrogate directly - Force the surrogate to answer only positive number or 0 - refactoring of temperature and density variables - upgrade the unit test so that it also test the surrogate. - fix some indexation issue in the vcut_fact update - add an assert to forbid the use of surrogate if it is not an electron species.
…efine a new maxwellian moment updater to compute the density and temperature. These moments are computed in the entire app->local range for now, one could consider limiting it to the skin range.
… major tweak is to copy the dev species basis onto the host to define the necessary basis and arrays for the vcut. the surrogate is not working on gpu yet.
… helical turb constant source in steady state namely ~6e18
…m Noah Mandel's case where the density is increased by a factor ten compared to NSTX reference. This is in a comment now.
… density profile of 3D
…e' into aisheath-dg-binop
…y the NN as an extension of the output layer. This allows us to remove entirely the need of hardcoded quantity to indicate about the grid points used in the interpolation of the NN output onto the physical mu grid.
…lity does not allow for a good setup of adaptive sourcing. Instead we replace it by a constant source setup.
…he extrapolation to negative value is risky.
…ve an adaptive source that is ambipolar in 1x2v. Connect the 1x2v TCV regression test to the sheath surrogate.
manauref
requested changes
Jul 21, 2026
manauref
left a comment
Collaborator
There was a problem hiding this comment.
Very nice general description. It covers all the basis and offers useful plots. I also very much like that this is now using the kann code.
Here's some feedback on what I've had time to review. I'll try to look at the actual kann part later today/tomorrow. In addition to the minor things below, here are some higher level questions:
- One downside I see here is that another person (maybe even yourself) could not extend the surrogate because we don't have enough material to replicate the construction of the network. Would it be possible to create a directory (say in gyrokinetic/data/) with the GYRAZE input files and network training scripts? I see this as necessary, much in the same way that we have a folder for creating the parameters of the radiation operator (which depend on other external analysis, but which can be replicated especially with what's in PR #1060).
- I'd like to read your AI sheath paper draft before we merge this to better understand what this is contributing and how it's changing the physics (beyond what you've put in the description).
- The description mentions that this model doesn't work for ion reflection. As you know Gkeyll can produce transients with negative MPE potential. What does the surrogate do to electrons and ions in that case? Are the input files only giving this option to the electrons?
- We should add the assert that you mentioned for alpha=pi/2. Also, since the validity of using this outside of the training domain is unknown, I think we should add an
fprintf(stdout,with a warning like "sheath_surrogate: outside of region of convergence". In general we stand by what Gkeyll produces, meaning we say its results can be trusted (at least as far as we know). But this option is adding the ability to produce results whose validity is knowningly unknown. - Is the gkylcas PR up to date?
manauref
requested changes
Jul 22, 2026
…H_CONDUCTING and GKYL_BC_GK_SPECIES_SHEATH_SURROGATE
…ession test, moved around some structures and clean up the code.
… jacobian with the new moment updater and this asked me to remove the `const` in the signature of the apply bc routine of gk_species.
… model accordingly
…d runregression passes. I need to wait for the return of Perlmutter to check on GPU.
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.
This PR follows DR #957 and introduces$\mu$ -dependent sheath boundary conditions in the gyrokinetic solver using a neural network surrogate of the GYRAZE code.
It is now using the KANN Gkeyll interface developed in #1056. In particular, this branch demonstrates the use of KANN model inference on multi GPU in production simulations.
Motivation
The standard conducting sheath BC in Gkeyll uses a constant cutting velocity$v_{\parallel\text{cut}}$ determined by the potential drop $\Delta\phi = \phi_\text{mpe} - \phi_w$ between the magnetic presheath entrance (MPE) and the wall:
This ignores the effect of the particle magnetic moment$\mu$ on the absorption/reflection condition.
The main changes in this PR reside in a new$v_{\parallel\text{cut}}(\mu)$ curve in the
gkyl_array vcutsqwhich stores thegkyl_bc_sheath_gyrokineticstructure. The evaluation of the sheath BC is now done in two steps:vcutsq, if we don't use the NN model, it will just contain the conducting sheath constant value.vcutsq.The inference of the NN model in step 1. above is done in three stages:
gkyl_kann_net_applystoring the output inkann_out. The KANN Gkeyll interface (GPU Hackathon 2026 branch: porting the KANN implementation to GPUs #1056) makes this call on CPU or GPU.vcutsq. Note, this step requires to interpolate between the NN model hardcoded 20 pointsNew files
gyrokinetic/ker/bc_sheath_gyrokinetic/bc_sheath_gyrokinetic_gyraze_surrogate.cAuto-generated GYRAZE surrogate model (NN weights, SVM classifier, forward pass, interpolation, projection), from gkeyll_sheath_ai, the commit is indicated in the file.This is now replaced by the KANN Gkeyll interface.gyrokinetic/ker/bc_sheath_gyrokinetic/gkyl_bc_sheath_gyrokinetic_gyraze_surrogate.hPublic API for the surrogate.This is now replaced by the KANN Gkeyll interface.gyrokinetic/creg/rt_gk_tcv_sheath_surrogate_2x2v_p1.cRegression test, TCV clopen IWL GK simulation with surrogate sheath BC.The original 2x2v tcv reg test is using the surrogate model now.gyrokinetic/data/nn_model/nn_model_sheath_bc_conv_MPE.kannThis is the NN model in a KANN readable format, generate using a python routine to convert from pytorch. Nomenclature: "conv" indicates that the model was trained on converged region of GYRAZE, "MPE" means it is expecting values at the magnetic presheath entrance. One could think of simplifying this name...
Modified files
gyrokinetic/zero/gkyl_bc_sheath_gyrokinetic.hNew constructor parameters
use_surrogate,surrogate_model_path, andphase_grid/phase_global(the latter two for thevcutsqdiagnostic output). New public methodsgkyl_bc_sheath_gyrokinetic_update_vcutsq(computesvcutsq),gkyl_bc_sheath_gyrokinetic_set_vcutsq, andgkyl_bc_sheath_gyrokinetic_write_vcutsq(writesvcutsqas a diagnostic).gyrokinetic/zero/gkyl_bc_sheath_gyrokinetic_priv.hgkyl_bc_sheath_gyrokineticstruct now stores:vcutsqDG array over perpendicular config spaceuse_surrogateflag, aupdate_vcutsqfunction pointer for switching between conducting sheath and NN inference, and thegkyl_kn_vecinput/output buffers andgkyl_kann_netof the KANN interface.gyrokinetic/zero/bc_sheath_gyrokinetic.cSee git diff.
gyrokinetic/zero/bc_sheath_gyrokinetic_cu.cuSee git diff.
gyrokinetic/ker/bc_sheath_gyrokinetic/bc_sheath_gyrokinetic_ser_p1.cReflection kernels now take the
vcutsqDG expansion and do not compute it on the fly anymore. Newbc_sheath_gyrokinetic_vcutsq_const_{lower,upper}_{1x2v,2x2v,3x2v}_ser_p1kernels to compute the constant conducting sheath model. Newbc_sheath_gyrokinetic_vcutsq_surr_{lower,upper}_{1x2v,2x2v,3x2v}_ser_p1kernels to compute the surrogate vcut.gyrokinetic/apps/gkyl_gyrokinetic.hNew
use_sheath_surrogateboolean to activate surrogate. Newsurrogate_model_pathto store the path to the.kannfile.gyrokinetic/apps/gkyl_gyrokinetic_priv.hSpecies struct gains
alloc_surr_aux_var,maxwell_mom,maxmom,dens_sheath,temp_sheathfor surrogate moment computation.gyrokinetic/apps/gk_species.cAllocates the Maxwellian moment calculator when the surrogate is enabled, computes density and temperature before each BC application, and calls
gkyl_bc_sheath_gyrokinetic_update_vcutsqat both lower and upper sheath/IWL boundaries. Also writes thevcutsqdiagnostic alongside the phase-space data.gyrokinetic/unit/ctest_bc_sheath_gyrokinetic.cExpanded with surrogate tests: verifiesvcutsqevaluation and end-to-end BC application with the surrogate for 1x2v, 2x2v, and 3x2v on both CPU and GPU.Design choices
Correcting factor approach
Unlike DR #957, we gave up on the$\mu$ -dependent multiplying factor $\alpha(\mu)$ which would modify the cutting velocity:
because of computing it requires to divide by$\Delta\phi$ that can be 0.
Instead, the$\times,\mu$ .
vcutsqarray stores the full cutting velocity as a DG expansion over perpendicular configuration spaceSafety assertions
assertenforces that the surrogate is only used for electrons (q2Dm == -2e/m_e), since ion reflection is not expected.vdim > 1(i.e. aUser interface
Enabling the surrogate requires a single flag in the BC specification:
Demonstration
Unit test
The expanded unit test
gyrokinetic/unit/ctest_bc_sheath_gyrokinetic.cvalidates:vcutsqevaluation and end-to-end BC application with the surrogate.Below are outputs with$v_\text{cut}(\mu)$ curve from the surrogate:
write_field = true, showing the reflected distribution and theThe$v_\text{cut}(\mu)$ curve is now provided by the GYRAZE surrogate rather than the ad-hoc function in #960.
TCV 2x2v regression test
The new regression test$\times$ 16 $\times$ 12 $\times$ 8) we obtain the following results with and without the surrogate:
rt_gk_tcv_sheath_surrogate_2x2v_p1.cruns a TCV clopen IWL GK simulation. At higher resolution (24We can see that the electron distribution functions close to the magnetic presheath entrance ($z=\pm \pi$ ) is affected by the surrogate as expected. More electrons are absorbed with the surrogate which creates a large region of $f_e=0$ in the $v_\parallel > 0$ region for $z=-\pi$ and $v_\parallel < 0$ region for $z=\pi$ . This also show that the surrogate is active in the simulation and is not always reaching the non converged state which would bypass it. The plots are zoomed in the region of interest.
The SOL potential is significantly higher with the surrogate. This is expected: FLR effects allow electrons to be absorbed at a gyroradius distance from the wall, lowering the effective potential barrier. The increased negative charge depletion raises the potential.
TCV 3x2v production like turbulence simulation
We consider now my favorite 3x2v low cost production case (see input file).
We also check the distribution function at the magnetic presheath entrance for the 3x2v case. The effect of the surrogate is less visible on this snapshot. It is possible that the surrogate is often bypassed though an effect is observed in the potential and macroscopic quantities.
Performance comparison
For production like run as the 3x2v simulation presented above, the computational cost is limited. Averaged over a 6h restart, we have a cost of forward Euler evaluation of:
Known limitations