Bayesian Analysis of Conventional and Ultrafast Spectroscopy Data for Investigating Detachment in the MAST-Upgrade Super-X
Xander Pope Kevin Verhaegh Chris Bowman Bruce Lipschultz














	
0.4 
    
 
    University of York
 
    School of Physics, Engineering and Technology
 
    2022/23
 
    Fusion Energy
 
    Supervisors:
 Prof. B, Lipschultz, 
Dr. K, Verhaegh, 
Dr. C, Bowman 
    


	

 
	





	
BAYESIAN ANALYSIS OF CONVENTIONAL AND ULTRAFAST SPECTROSCOPY DATA FOR INVESTIGATING DETACHMENT IN THE MAST-UPGRADE SUPER-X
 

XANDER POPE 
	





















	

	



Flexible Field Sizes in Secure Distributed Matrix Multiplication via Efficient Interference Cancellation
    
    Okko Makkonen

    Department of Mathematics and Systems Analysis

    Aalto University

    Espoo, Finland

    Email: okko.makkonen@aalto.fi

    Received / accepted 
========================================================================================================================================================




 
    


 








This paper presents the application, testing and first results of a new adaptive Bayesian inference analysis which utilises conventional and ultrafast spectroscopic measurements made in the divertor chamber to investigate the divertor physics during detachment.
Validation of this software is performed prior and during analyses of results, demonstrated by compelling reproductions of ideal test cases and synthetic spectroscopic measurements.
Application on real diagnostic data shows strong agreement with results from previous analysis methods.
We identify unprecedented success in significant advances in time and computational efficiencies.
We demonstrate a ≲1000× reduction in analysis time for spectroscopic measurements from simulated and real Super-X configurations, with the analysis technique presented in this report completing in <3 minutes.
Analysis of synthetic and real diagnostic measurements identifies detachment physics in agreement with previous literature.

Keywords: Super-X divertor, divertor spectroscopy, divertor physics, detachment, Bayesian inference, adaptive grid


 



§ INTRODUCTION












Many challenges line the road to commercial fusion energy, a particularly potent obstacle is that of exhausting heat from the main chamber.

Fusing deuterium (D) and tritium (T) into ^4He (α-particle) forms the most viable fusion reaction (Eq. <ref>).
Our concern is for the 20% of the reaction energy that remains in the core plasma as α-particles.
Radial diffusion causes this power to ultimately cross the last closed flux surface and enter the scrape-off-layer (SOL).

    D + T → n (14.1MeV) + α (3.5MeV)


Generally, the radial movement of plasma across flux surfaces is very slow compared to movement along flux surfaces.
The SOL travels parallel to flux surfaces.
Hence, the SOL spatially reduces the gradually diffused power into an intense heat flux, directed towards a solid target in the divertor.
Such flux would need to be significantly reduced to meet current material limits for the divertor <cit.>.
Commercial viability, in terms of exhausting residual heat, is hence hindered until this heat flux can be mitigated.

Detachment results in a simultaneous reduction of the target ion flux and electron temperature, which can reduce the target heat flux by orders of magnitude.
This is achieved by simultaneous power, momentum and particle (i.e., ion) losses as a result of plasma-atom/molecule interactions <cit.>.
To access detachment, the divertor region must exist at a temperature below ≈5eV <cit.>.
These conditions can be accomplished by a number of methods, including: inducing radiative losses in the plasma by seeding impurities, increasing the collisionality of the plasma by increasing the particle density in the core or divertor and investigating alternative divertor configurations (ADC).



ADCs, such as the Super-X divertor, developed for use on the MAST-U tokamak (Fig. <ref>), utilise magnetic shaping of the divertor topology to improve power exhaust and improve access to divertor detachment.
By shifting the outer target to a larger major radius, the Super-X increases the surface area of the target.
Furthermore, the plasma is moved towards a lower poloidal flux density which in turn causes the plasma to spread and the heat flux to lower.
The Super-X divertor is also installed on both the top and bottom of the tokamak allowing a second outer divertor.
Additionally, to ensure the confinement of neutral atoms/molecules that cannot be confined electromagnetically within the divertor region, the Super-X implements a baffle.
This facilitates further detachment-related interactions within the divertor.



 §.§ Detachment




  §.§.§ Particle and Power Balance


Plasma in the SOL enters a region of recycling conditions once in the divertor, shown in Fig. <ref>.
Within these conditions ions continuously neutralise/recombine and re-ionise.
It is the neutralisation on the target that transfers power and momentum, initially from the core plasma, onto the material.
When the bulk of this recycling is occurring on the target, the system is `attached'.
Thus the flux of ions reaching the target must be mitigated by `detaching' this high recycling region from the target and into the divertor chamber.



Total ion flux incident on the target (I_t) is promoted by ion sources within the divertor (I_i) and any net influx of ions from the SOL (I_SOL) and mitigated by ion sinks within the divertor (I_r) (Eq. <ref>) <cit.>.

    I_t = I_i - I_r + I_SOL


Shown in <cit.>, combining Eq. <ref> with power balance results in Eq. <ref>, assuming I_t ≫ I_SOL.
P_recl is the power entering the recycling region, E_ion is the energy required to ionise hydrogen (inclusive of radiative losses due to preliminary excitation of the neutral before ionisation), γ is the sheath transmission factor and T_t the target temperature.

    I_t = (P_recl/E_ion - I_r) ×1/1 + γ T_t/E_ion


During deep detachment γ T_t/E_ion≪ 1 <cit.>, we can approximate Eq. 3 into Eq. 4, giving further insight into what drives the ion target flux.

    I_t = P_recl/E_ion - I_r


`Starving' the ion flux to the target <cit.> can be done by seeding large amounts of heavy impurities into the main chamber to facilitate power loss in the core plasma via radiation, resulting in reduced P_recl <cit.>.
This is not optimal for keeping the core plasma in fusion conditions, therefore, we must look to increase the ion sinks in the divertor to facilitate the detachment of the high recycling region.
It is important to note that a parallel line of understanding for facilitating detachment finds that pressure reductions at the divertor target (rather than power/particle losses), caused by volumetric momentum losses, are required <cit.>.
Whilst differing in approach, it is shown that both concepts are valid and equivalent in the requirement for simultaneous power, momentum and particle loss <cit.>.



  §.§.§ Plasma-Atom and Plasma-Molecule Interactions


A large number of plasma-atom and plasma-molecule interactions, occurring within the divertor region, provide the microscopic origin for detachment.
MAST-U is not equipped for use with tritium, henceforth interactions are discussed in terms of deuterium with note that these interactions apply for all hydrogen isotopes.

Interactions between the plasma and atoms/molecules in the divertor can directly or indirectly contribute to the ion flux reaching the target.
Interactions that provide ion sources (ionisation) and sinks (recombination) directly influence the balance of ions reaching the target, whilst interactions that produce a source of neutral atoms (dissociation) provide an indirect impact by inducing increased momentum losses through more plasma-neutral collisions.

Initially, the ion sources/sinks and power losses were studied spectroscopically through plasma-atom interactions (ionisation and electron-ion recombination (EIR)) <cit.>. 
However, this could not explain the observed increase in Balmer-α emission during detachment. 
This required accounting for excited hydrogen atoms after molecular break-up when molecular ions (D_2^+, D_2^- → D^- + D) interact with the plasma <cit.>.



Whilst less understood than their atomic counterparts, plasma-molecule interactions (PMI) are known to be split into two stages: the creation of molecular ions (D_2^+ and/or D_2^-) followed by the destruction of these ions (Tab. <ref>).
Only D_2^+ is considered in Tab. <ref> as it is likely the dominant molecular ion <cit.>.
The combination of creation and destruction events determines whether there is a net; increase in atomic ions (Molecular Activated Ionisation - MAI); decrease in atomic ions (Molecular Activated Recombination - MAR); or increase in neutral atoms (Molecular Activated Dissociation - MAD) (Tab. <ref>).



  §.§.§ Diagnosing Detachment





Detailed analysis of the emission from excited hydrogen atoms facilitates studying the microscopic origins of detachment, for instance by analysing the visible Balmer emission spectrum.
Excited atoms, directly produced from EIR, MAR and dissociation events, as well as from electron-impact-excitation (EIE) (a known predecessor for atomic ionisation) can de-excite to the second energy level, releasing emissions of the Balmer series.
Additionally, Fulcher emissions produced from the electronic excitation of deuterium molecules is also visible.
Fulcher emissions give information for detachment via inference of temperature <cit.>.

A summary of visible detachment-related interactions is given in Fig. <ref>.



Utilising the intensity of these emissions (denoted by B throughout this report) forms the basis for detachment diagnostics.
Conventional and ultrafast spectrometers within MAST-U deploy line-of-sight (LoS) spectroscopy through the divertor chamber, resulting in a line integrated intensity (in ph/m^2/s) for the individual Balmer lines with an uncertainty of 12.5%.
The conventional spectrometer, spread over 40 lines-of-sight, can detect B_α, B_γ and B_δ, whilst the ultrafast spectrometer, spread over 10 lines-of-sight, finds B_α and B_β.



There is great difficulty associated with the diagnosis of detachment using Balmer emissions.
All Balmer emissions come from excited atoms, however these same atoms can originate from both atomic and molecular plasma-interactions, with no experimental method to distinguish between contributions from individual interactions.
Additionally, there are no emissive precursors for MAI so its inference relies on the knowledge of the ratio of molecular reactions given in Tab. <ref>.
Our analysis of detachment is therefore tasked with deducing the many potential interactive origins for the detected Balmer emissions, using only the intensity of these emissions.



  §.§.§ Detachment in the Super-X




Experimentation and pre-existing analyses have found that detachment within the Super-X can be split into four distinguishable phases (Fig. <ref>) <cit.>:

    
  * The ionisation (both atomic and molecular-activated) region detaches from the target.
    Subsequently, increased molecular densities occur in the space left by ionisation near the target, resulting in increases in MAD and more importantly MAR.
    Significant Balmer line emissions are associated with these increases in plasma-molecule interactions.
    
  * Plasma-molecule interactions shift upstream, indicated by the movement of the notable Balmer emissions.
    The increased ion sinks begin to decrease ion, and hence heat, flux downstream.
    
  * Temperatures drop below 0.5eV near the target, providing necessary conditions for EIR to become significant.
    
  * Balmer emissions associated with EIR detach from the target, suggesting the movement of the bulk of the electron density upstream.




 §.§ This Work








The inference of the detachment stages in <cit.> was performed using BaSPMI <cit.>, a complicated and lengthy iterative process, altering model parameters until a combination of emissive processes and thus plasma interactions have converged.
The BaSPMI algorithm is summarised in Fig. <ref>, with note that it is displayed to illustrate the complexity of such an analysis.

Steps have been taken to disentangle this process by utilising Bayesian inference techniques combined with a simplified emission model to replace the section enclosed in the dashed red line in Fig. <ref>.
This update, referred to as BaySPMI, uses the emission model to infer the most likely quantities for the free parameters, which can then be used to calculate rates of ion sources and sinks.

Both BaSPMI and BaySPMI require extensive computational resources (≲120GB of memory) and time (≲48 hours) to complete, making the analysis of detachment largely inaccessible.
With detachment expected to play an essential role for target heat deposition reduction, it is crucial to have a practical analysis method that can be used to evaluate a wide number of detachment scenarios to better understand the physics involved.

Importantly, BaSPMI must have three Balmer emission lines to work.
Therefore, the observations from two Balmer lines diagnosed by the ultrafast spectrometer is insufficient for use of BaSPMI.
The analysis of ultrafast data is essential for investigation into specific sections of shots to study the effect of transient events, such as ELMs, on detachment and divertor physics.

We therefore build on and improve the analysis methods used thus far, specifically BaySPMI, to create a more practical and efficient detachment analysis software.
Success for such a software will be measured by; 

    
  * Practicality: ability to run between MAST-U pulses (20 minutes) on a scientific workstation.
    
  * Validity: successful reproduction of ideal test case and simulated data and agreement with BaySPMI results.
    
  * Ability: able to analyse conventional and ultrafast observations.


Once validated, the software is used to analyse a simulated N_2 gas puffing experiment, in which N_2 is seeded into the divertor near its entrance at rates varying from 1×10^21 to 1.1×10^22 mol./s <cit.>.
Discharge 46860 (Tab. <ref>), a density ramp experiment from the second MAST-U campaign, is also analysed.





§ METHODOLOGY






 §.§ Balmer Emission Model
 

The emission model used in BaySPMI forms the foundation of our analysis.
Splitting the Balmer emission intensity for each emission band into plasma-atom and plasma-molecule interactions structures the diagnostic model:

    B_n→2   =B_n → 2^atom + B_n → 2^mol


Current understanding derives Eq. <ref> from Eq. <ref> <cit.>, recognising the individual contributions to Balmer emissions and identifying the six model parameters required to ultimately reconstruct the ion sources and sinks.


    B_n→2=   B_n → 2^EIE + B_n → 2^EIR + B_n → 2^PMI
    
            =   Δ L n_e^2 n_H/n_ePEC^EIE_n → 2(n_e, T_e^E) + Δ L n_e^2 PEC^EIR_n → 2(n_e, T_e^R)
       + Δ L n_e n_H_2^+PEC^PMI_n → 2(n_e, T_e^E, T_e^R)
.

There is a requirement for databases of photon emissivity coefficients (PEC) to compute the model.
Such coefficients identify the amount of photons we expect to see for the respective interactions at a given electron temperature and density.
PEC^EIE_n → 2 and PEC^EIR_n → 2 were obtained using ADAS <cit.> and PEC^PMI_n → 2 using Yacora <cit.>.
All PEC databases were created for temperatures ranging from 0.02 - 50eV and electron densities of 1×10^18 - 5×10^20m^-3. 

Before stating the free parameters involved in Eq. <ref>, it should first be noted that the density of molecular ions (n_H_2^+) presents a large unknown that would cause unacceptable uncertainties if included.
Therefore, we use the ratio for plasma-molecular to plasma-atomic emissions for the Balmer-α series (known as Q_mol) as a scaling factor to bypass the use of such an uncertain parameter.
Accommodating for Q_mol is shown below.


    B_n → 2 =    B_n → 2^atom + B_n → 2^mol
    
            =    B_n → 2^atom + B_n → 2^mol[B_3 → 2^atom/B_3 → 2^atom][B_3 → 2^mol/B_3 → 2^mol]
    
            =    B_n → 2^atom + B_3 → 2^atom[B_3 → 2^mol/B_3 → 2^atom][B_n→2^mol/B_3→2^mol]
    
            =    B_n→2^atom + B_3→2^atom Q_molB_n→2^mol/B_3→2^mol
    
            =    B_n → 2^EIE + B_n → 2^EIR + Q_mol[B_3 → 2^EIE + B_3 → 2^EIR][B_n → 2^PMI/B_3 → 2^PMI]


Taking a ratio of the PMI's allows the cancellation of n_H_2^+ making our final model for Balmer line emissions less sensitive to this uncertainty.


    B_n → 2 = Δ L n_e^2    [n_H/n_ePEC^EIE_n → 2(n_e, T_e^E) + PEC^EIR_n → 2(n_e, T_e^R)
       + Q_mol[n_H/n_ePEC^EIE_3 → 2(n_e, T_e^E) + PEC^EIR_3 → 2(n_e, T_e^R)]
       [PEC^PMI_n → 2(n_e, T_e^E, T_e^R)/PEC^PMI_3 → 2(n_e, T_e^E, T_e^R)] ]


Our free parameters required to diagnose the sources and sinks of ions are as follows: path length (Δ L), density of electrons (n_e), ratio of neutral hydrogen (n_H/n_e, referred to as n_ratio throughout the rest of this report), the temperature for the population of electrons along the LoS that could experience EIE and EIR (T_e^E and T_e^R respectively) and Q_mol.

The path length parameter identifies the extent of plasma that the LoS intersects and is therefore the length in which the spectrometer integrates to produce the measured line-integrated intensity.
n_ratio is used rather than n_H, as it introduces the understanding of the relationship between n_e and n_H into the model.
During our analysis Q_mol is interchanged with a similar parameter: f_mol.
The relation between the two parameters is given in Eq. <ref>.
f_mol is more practical during analysis as it can only have values between 0 and 1.


    f_mol   = B_3 → 2^mol/B_3 → 2^mol + B_3 → 2^atom,      
        Q_mol   = B_3 → 2^mol/B_3 → 2^atom,      
        Q_mol   = f_mol/1 - f_mol



By utilising an emission model that only requires the Balmer-α and one additional line emission intensity, we are able to analyse measurements from the ultrafast spectrometer, providing an important precursor for the success of our analysis.

Ultimately, the analysis of interest is how the various plasma-atom/molecule interactions impact power and particle balance in the divertor. 
Using the post-processing developed in <cit.>, the various emission fractions of the Balmer line emission (EIE, EIR, PMI) can be used to estimate the ion sources/sinks and power losses from each of these processes using the six free parameters introduced in the Balmer line emission model.



 §.§ Bayesian Analysis


Much of the difficulties of experimental data analyses can be reduced to the inherent reversal of deductive logic.
Deductive logic follows that: given a cause, an outcome can be deduced, the reverse of this being inductive logic: given an outcome, what is the original cause?
The complexities of inducing causes are indicated in Fig. <ref>, with there being many possible causes for any one given observation.
Indeed, this is the case for our analysis of detachment in Eq. <ref>: given the observation of the intensity of Balmer emissions, what combination of parameter values would have caused this?



Often, the value of the parameters within the best matching model are taken to be fixed with some level of uncertainty, this is the approach used in BaSPMI.
Moving away from convention, Bayesian probability theory provides a powerful alternative approach towards data analysis, replacing fixed parameter natures with a probability distribution of all possible parameter values.
In doing so, the distribution of all possible causes can be inferred <cit.>, where fixing parameters in other analysis techniques may fail to identify plausible causes.



  §.§.§ Bayes' Theorem


The notation used in probability theory is as follows. 
The probability (P) that X is true: P(X).
The probability that both X and Y are true can be denoted using a comma: P(X, Y).
Conditional probabilities are represented using `|', for example, the probability that X is true given that Y is true: P(X|Y).

Bayes' theory provides logical reasoning for data analysis by stating the following formula:

    P(X|Y) = P(Y|X) × P(X)/P(Y)


Whilst fairly unassuming in theory, applying it in the context of science showcases its significance for data analysis <cit.>:

    P(hypothesis|data) = P(data|hypothesis) × P(hypothesis)/P(data)


    P(hypothesis|data) ∝ P(data|hypothesis) × P(hypothesis)


This project omits the need of P(data), formally named `evidence', throughout.
By representing the probability that the data is true within a single experiment makes such a term a normalised constant which can therefore be excluded whilst still producing comparable results <cit.>.

For the remainder of this report the other components of Bayes' theorem will also be referred to with their formal names.
From right to left in Eq. <ref>; the prior probability; likelihood function; and posterior probability.
The prior accumulates previous knowledge of the experiment to form a probability relating to the current believed truth of the hypothesis.
The likelihood function utilises an aforementioned model to compose a probability that the data is correct in the context of the hypothesis.
Lastly, the posterior combines the prior and likelihood to produce the probability that the hypothesis is true in light of the experimental data.

Thus, the nature of Bayesian statistical analysis is to methodically test many hypotheses to generate an evolving probability density function (PDF) of the posterior outcome for said hypotheses, with areas of high probability density signifying likely hypotheses.



  §.§.§ Bayesian Analysis for Detachment


Given our experimental data of emission intensities for Balmer lines (B^exp_n→2), the hypotheses proposed are the combinations of free parameters that contribute to the emission model in Eq. <ref> (T_e^E, T_e^R, n_e, n_ratio, f_mol, Δ L).
Therefore, the application for Bayes' theorem in regards for this project is the following:

    P(T_e^E, T_e^R, n_e, n_ratio, f_mol, Δ L|B^exp)
        =     ∏^n P(B^exp_n→2|T_e^E, T_e^R, n_e, n_ratio, f_mol, Δ L → B^pre_n→2)
       × P(T_e^E, T_e^R, n_e, n_ratio, f_mol, Δ L)

        
Prior Probability:
Our priors, (P(T_e^E, T_e^R, n_e, n_ratio, f_mol, Δ L)), assemble knowledge of the values for these parameters.
For example, if a parameter hypothesis is proposed with T_e^E < T_e^R, then the prior would return an exceptionally low probability, reflecting that we would not expect the T_e^R to ever be greater than T_e^E, since EIE occurs at higher temperatures than EIR.
Since the same values for our free parameters are used within one evaluation of numerous Balmer emission bands, they need only be applied once.

Likelihood Function: 
Assuming such hypothesised parameter values are true, computing the emission model then returns `predicted' emission intensities for each Balmer line, P(B^exp_n→2|T_e^E, T_e^R, n_e, n_ratio, f_mol, Δ L → B^pre_n→2).
Lower likelihood functions are subsequently computed when the experimental results are further in value to the predicted emissions.
The product of the likelihood probabilities across all emission bands produce a final likelihood.
Greater confidence in the resultant posterior is associated with increased Balmer lines introduced in the analysis, since more individual likelihood functions must agree on any given combination of parameters, with the exception for if our emission model was imperfect.

Posterior Probability:
Knowing that the experimental values are always true, low likelihoods infer low posterior probabilities (P(T_e^E, T_e^R, n_e, n_ratio, f_mol, Δ L|B^exp)) for the current combination of parameters due to their proportionality in Bayes' theorem.
Therefore, our posterior forms a function which tells us how likely it is that a given set of parameter values were the cause of the observed data.

A successful way of systematically investigating varying compositions of free parameters is to set up a grid, of dimensions equal to that of the number of variables, with each `cell' on this grid composed of specific combinations of values for these parameters.
This `cell' is then evaluated to compute the posterior probability for that parameter combination.
The probability of each cell directly contributes to the total PDF for the combined parameter space.
BaySPMI creates such a grid, of 144,000,000 cells spread over the space of realistic values, to perform its analysis of detachment.
Whilst the parameter values in their individuality are all plausible, within six parameter space, the majority of their combinations are not.
Thus many evaluations are wasted on `cells' that provide no realistic probability, making the process very inefficient.
Additionally, all 144,000,000 evaluations are stored in memory during application causing large computational strain.
Overcoming this inefficiency is where our analysis differs from BaySPMI.

For information on the six output parameters to be used in the post-processing calculations to infer ion sources/sinks, the calculations need to be computed on a set of sample parameters, sampled from the multidimensional PDF to account for all the various correlations.
Therefore, sampling is performed with bias based upon where the combination of parameters for each cell lies probability-wise within the PDF.
Parameter combinations with higher probability density are more likely to be sampled, whilst combinations with lower probability densities are less likely.
The representation of probability within the sampling is a powerful method of uncertainty propagation within Bayesian analyses.
Because these weighted samples are carried forward in processing, probabilities for the original parameters continue to hold weight throughout computation.
Thus, a sample of new computed parameters is created (for example, a sample for the ionisation source), which can be mapped back to a PDF using kernel density estimation.



 §.§ Adaptive Grid




The grid technique in BaySPMI can be referred to as `full grid', since it evaluates the full parameter space.
We apply a new algorithm that evaluates only the areas of parameter space with high probability, preventing the computation of unrealistic parameter combinations.
Such a technique is referred to as an `adaptive grid' approach.
A conceptual overview of the adaptive grid algorithm, initially created in <cit.> and updated for use in this project, is given in Fig. <ref>.

Adaptive grid analyses pairs very well with Bayesian inference techniques, as the computed probability can inform the script whether an area in parameter space is worth exploring.
Additionally, working in probability space allows confidence that all useful parameter space has been explored by examining the convergence in total probability found thus far.
For example, the convergence threshold in Fig. <ref> is set to be sufficiently low, that when continuations of the adaptive grid algorithm fail to add probability greater than this small value, we can be sure all important parameter space has been explored.
To ensure we do not converge in probability by exploring only one local maxima, we expand our number of starting positions to be sufficiently large such that we find and explore the majority of maxima.

In addition to the algorithm inputs shown in Fig. <ref>, the adaptive grid script requires the definition for the desired intervals between parameter cells and a centre of the grid.
This centre is not used as a starting position, but as a method of defining an offset such that each cell can be given real parameter values.

The decision to use an adaptive grid approach for more efficient analysis of parameter space over more established statistical techniques such as Markov Chain Monte Carlo (MCMC) <cit.> was made due to the random nature of MCMC.
Analyses with reduced amounts of randomness, such as our adaptive grid technique, produce more deterministic results.

Successful application of an adaptive grid technique, differing in approach to ours, has been demonstrated for up to four dimensions in parameter space <cit.>.
Whilst shown to be significantly faster than full grid approaches, notably coarse cell spacing and reduced exploration of parameter space was required to make such an analysis feasible in four dimensions without the requirement of a high performance computer (HPC).
We will use our alternative technique to evaluate an increased number of dimensions (six), with finer cell spacing and as much accuracy as its full grid counterpart, in significantly less time, without the requirement of a HPC.



§ SOFTWARE DEVELOPMENT




 §.§ Application for Bayesian Analysis in Script




Given in Fig. <ref> is the relation between components of Bayes' theorem within our analysis.
Our hypothesised parameters are the values for our free parameters within the current cell.
The prior module is discussed in the following section.
We use the program MIDAS (used in <cit.>) to compute the Balmer line emissions within our model.
Our incorporation of Bayes' utilises the log-probability environment.
Working in log-probability space: simplifies computation via addition of probabilities rather than multiplying, ensures probabilities are always positive and enforces unrealistic probability space via the addition of large negative numbers.



Within our parameter space created for the Bayesian analysis, we are using the log values for T_e^E, T_e^R, n_e and n_ratio, then converting these back to their actual values during sampling.
This is done because it allows equally weighted interval's in parameter spaces spanning across multiple magnitudes.



  §.§.§ Implication of Priors


The utilisation of prior probabilities is what allows an analysis of so many parameters from such little input data.
Without these priors, the parameter space would be left unrestricted, making the completion of the adaptive grid algorithm and the production of valid results largely impossible.
Our prior implementation can be split into two approaches: hard parameter boundary limits and model parameter value influence.

Hard parameter boundaries define the limits of realistic parameter values seen in our application of the analysis software.
Hence, any hypotheses proposed with a parameter value outside that of the limits shown in Tab. <ref> will have a large, negative log-probability enforced.
This ensures the resultant posterior is highly improbable.
The aforementioned T_e^E < T_e^R prior probability is the other hard limit prior utilised in our analysis.



Limits for T_e^E, T_e^R and n_ratio are defined via knowledge of plasma chemistry within the divertor.
Detachment is accessed below ≈5eV, EIR below ≈1eV and a n_ratio of 1 states that half the plasma is neutrals, more neutrals than this would fall into unrealistic plasma conditions.
Temperatures below 0.2eV fall outside of EIE conditions, 0.02eV represent the absolute minimum temperature plausible in a divertor and a n_ratio of 1×10^-3 is enough to imply a fully ionised plasma.

n_e, Δ L and a more specific n_ratio limit is dependent on the individual tokamak and divertor system.
n_e and Δ L are pre-defined with the values in Tab. <ref> as these are known to be realistic for MAST-U.
However, Stark broadening techniques and magnetic mapping of the divertor can provide better estimates for the limits of n_e and Δ L respectively for individual shots across different tokamaks/divertors.
These processes can be performed prior to the use of our analysis, thus, if defined in the input data dictionary, our script can swap the pre-defined limits for n_e and Δ L, as well as n_ratio, to more appropriate limits.

f_mol is set between 0.01 and 0.95 to prevent exceptionally small or large values for Q_mol.
All parameter boundaries are the same as those used for the edge of parameter space in BaySPMI.

Where possible, we use models for the observed parameters that are constructed before our analysis.
The parameter models influence the prior probability given to our free parameters.
Model priors are utilised for T_e^H, n_e and Δ L within our script.
The same diagnostics used to estimate the parameter limits for n_e and Δ L also provide statistical averages of the parameters, used to construct models.
Measured intensity for the Fulcher band emissions across the divertor can be modelled to provide a prior for T_e^H.



 §.§ Sampling Method


BaySPMI utilises rejection sampling, in which a wider and geometrically simpler sample region is established around the complex PDF.
The samples found to be within the PDF are accepted, whilst samples between the wider sample region and PDF are rejected <cit.>.
Although functional, this technique can prove inefficient.

Therefore, we use an alternative sampling method.
We perform bias sampling on parameter cells rather than sampling parameter values themselves, resulting in far quicker computation.
Reflecting our original PDF within our sample is very important for uncertainty propagation, as the distribution in probability infers uncertainty.
Hence, bias is given by the normalised probability for each of the cells.
Consequently, a single cell may be sampled many times if it is of high probability, producing the same parameter values each time.

Repeated parameter values do not imitate real samples, therefore, we perform nearest neighbour interpolation by adding/subtracting a randomly generated quantity to each parameter value of the sampled cell.
The values of these added/subtracted quantities do not exceed half the defined cell spacing to ensure we stay within the same cell in parameter space.
We can therefore assume the probability of this randomly generated nearest neighbour to be the same as that of the originally sampled cell.
However, if a cell is sampled on the edge of parameter space the inclusion of randomness can cause the sampled value to exit our parameter bounds.
Subsequently, re-sampling is introduced to the script until all sampled values are within our defined limits.



 §.§ Adaptive Grid Optimisation







Application for the adaptive grid algorithm was tailored for optimal balance of computational resource use and validity of results.

The spaces defined between parameter values is of high importance for such optimisation.
Spacings used in our software were validated against an adaptive grid analysis utilising far finer cells.
Comparison to parameter spaces in BaySPMI is given in Tab. <ref>.
Our analysis investigates parameter space at a higher resolution than BaySPMI, putting favour towards the adaptive grid technique.
Cell spacings that are too coarse were seen to prevent the correct convergence of the adaptive grid algorithm.



We define the convergence limit for the script to be 1×10^-1, as it is shown to be sufficient to investigate all useful parameter space.
Running the script past this limit therefore adds no significant importance to our analysis.

The efficiency of our script means that substantially fewer evaluations need to be performed and stored in memory at any one time, resulting in the ability to distribute our evaluations of the divertor at different lines-of-sight and times across different processes, without risk of overloading the memory, further increasing the speed of analysis.

Our script for the analysis software is given in App. A.


§ RESULTS




 §.§ Validation of Software




  §.§.§ Ideal Test Case


Our initial test of validity was a simple reproduction of manually created observations (creation discussed in App. B.1).
The first ideal test scenario uses model parameters to produce emission intensities for three Balmer lines: B_α, B_γ and B_δ, simulating data from conventional spectroscopic measurements.
Of the two types of spectroscopic measurements, this is the better restricted scenario for the adaptive algorithm, since we have three Balmer line emission intensity inputs.
We would therefore expect better agreement with the original parameters within this scenario compared to when we have less Balmer lines.



Accordance of the adaptive grid analysis output with the ideal test case data is seen across all free parameters via an area of high probability corresponding with the input value (Fig. <ref>).
Agreement is best for Δ L, due to the incorporated prior model strongly influencing the result of the parameter.
Neglecting prior models and specific boundaries could explain the slightly weaker correspondence between peaks in the PDF and the input value for the density parameters, however the agreement remains good.
A peak in probability is not found for T_e^R, this would imply temperatures are too high for recombination to occur, a very likely physical outcome given the relatively high value chosen for T_e^E.
Therefore, the inability to produce a peak in probability for T_e^R provides little concern.

Across all parameters mentioned thus far, affinity between adaptive and full grid approaches is very strong.
Such correspondence between the two approaches provides strong confirmation in the ability of the adaptive grid algorithm to analyse the majority of the relevant parameter space.
Differences are seen for f_mol as the parameter increases in value, however, both approaches are agreeable in probability value for the parameter input.
Overall, the PDF for f_mol produced by the adaptive algorithm is better biased towards the input value.



With consistent agreement across parameter and emissions (App. B.2) seen in the ideal test case for conventional spectroscopic data, the same analysis is performed with modelled emissions for B_α and B_β, simulating ultrafast spectroscopic observations.
With only two Balmer lines needing to agree on any given parameter combination the parameter space is less restricted.
We have used the same input parameters as in our previous case, so we expect to see the same overall shapes and locations for our parameter PDFs.
Difficulties seen reproducing these PDFs could infer that less probabilistic restrictions do hinder our adaptive grid analysis.

Fig. <ref> is significant because it demonstrates the ability for our analysis software to analyse ultrafast spectroscopy data, surpassing the limitations of BaSPMI.
Additionally, much of the same of Fig. <ref> is seen in Fig. <ref>, so a reduced number of input emissions does not greatly impact the accuracy of our analysis at an ideal test case level.
A better peak in T_e^R is now seen.
f_mol provides a noticeable difference between the two plots, with the adaptive grid providing a similar peak in probability seen in the full grid analysis towards our parameter limits.
Whilst this peak is not correct, the bulk of our probability density remains towards the centre near the input parameter.

Our analysis demonstrates its functionality in reproducing compelling model parameters with minimal inputs, using only knowledge of the hard boundary priors and Balmer emission intensities.



  §.§.§ Simulated Ultrafast Spectroscopic Data


SOLPS-ITER <cit.> was used to simulate a scenario within a Super-X-like divertor in which detachment is facilitated by an increasing gas puffing rate of N_2, as diagnosed by the ultrafast spectrometer with only B_α and B_β measurements.
A constant fuelling rate within the main chamber is chosen such that the density increase caused by fuelling does not facilitate detachment itself <cit.>, thus the synthetic data only explores the effects of N_2 seeding.
The simulated diagnostic result is obtained through lines-of-sight going through realistic 2D maps of density and temperature.
Therefore, unlike our ideal test case, our simplified model is no longer identical to the analysis input by definition.
However, similar PEC data and assumptions for emission processes are used to create the synthetic data.
With less generalised inputs into our analysis, we utilise all priors discussed in Section <ref>.

We performed the adaptive grid analysis, as well as BaySPMI, to produce free parameter samples, which used the same post-processing techniques to provide inferred measurements for the ion sources and sinks.
Comparison of key free parameters were performed prior to post-processing to verify our analysis was similar to BaySPMI (App. C).
Our adaptive grid analysis is therefore compared against both the simulation inputs and BaySPMI analysis results in Fig. <ref>.


The post-processing takes our sampled combination of free parameters to compute a sample, of equal size, for the ion sources and sinks.
Thus the final observations in Fig. <ref> remain reflective of our original PDF of parameter combinations.
We use the median result of this sample to plot ion sources and sinks, with uncertainties covering the 16-84% quantiles, representing ±1 standard deviations.
This quantile method has been historically used for BaSPMI and BaySPMI results.

Ionisation is plotted with contribution from both atomic-ionisation and MAI, since we care about the size and location of the ion sources.
MAR and EIR are kept separate due to the information they provide about detachment individually.
MAR is plotted using only the simulated and inferred contributions from D_2^+, since it is inferred to be the dominant ion sink <cit.>.
MAD is not included in our results because it does not directly impact the ion sources and sinks related to our power and particle analysis of detachment.
This inclusion and exclusion in plotting is also reflective to that historically done for BaSPMI and BaySPMI results.

Overall, the adaptive grid algorithm was able to correctly reproduce simulated ion sources and sinks within an uncertainty of one standard deviation.
For the most part, our analysis not only agrees with but provides greater certainty in ion sources and sinks than BaySPMI, reflected in the simulated inputs seen within narrower uncertainty bands.
However, this reduced quantile spread can be shown to give false certainty in specific cases, such as the large simulated spike of ionisation seen in the first case of Fig. <ref>.

A noteworthy difference between the two analyses is that of ionisation inferred when the puffing rate is 2×10^21mol./s.
Full inclusion of simulated ionisation is seen in our adaptive grid approach whilst BaySPMI does not capture the simulation in the centre of the divertor chamber.
Since the Fulcher emissions were not behaving as expected, we set the maximum Fulcher emission we expect to see based upon a manual viewing of a map of Fulcher emission intensity within the divertor, resulting in different constraints for our T_e^H parameter in the adaptive grid analysis.
This was not done for BaySPMI.

Whilst both analyses very slightly over-predict EIR near the target as gas puffing increases, essentially, the analyses indicates that EIR is negligible. 
This is coherent with early phases of detachment <cit.>.



  §.§.§ Experimental Spectroscopic Data


After having tested our analysis, we apply it to experimental data from discharge 46860 where the level of detachment is increased throughout the discharge as the core density is increased by additional core fuelling.

We utilise conventional spectroscopy measurements of the B_α, B_γ and B_δ emissions as well as the D_2 Fulcher emission to construct a T_e^E prior, manual adjustment of the Fulcher prior was not necessary.
Again, we run the analysis with all priors.
Model parameter comparisons were also made before post-processing for this analysis (App. C).
Comparative results for ion sources and sinks are given in Fig. <ref>.



Very good agreement of inferred sources and sinks between the two different analyses is seen.
This is more impressive than that seen in Fig. <ref> because both analyses display significantly thinner uncertainty bands whilst still agreeing.
The increased correspondence and narrower uncertainties are likely products of the inclusion of further Balmer lines resulting in better performance of the Bayesian analysis.
Most importantly, the adaptive grid analysis is able to infer the exact same stages of detachment, discussed further in Section <ref>, seen in BaySPMI at the same points in time.

In the region where the ion source is insignificant, the adaptive grid shows smaller uncertainties than BaySPMI. 
However, both analyses indicate that the ionisation source is insignificant at these points.



Whilst important to verify our analysis against an already accepted method, this cannot be done if our analysis was to be used going forward as we would run into the same computational and time issues we are attempting to resolve.
Instead, we can reproduce the measured emission intensities, although this test cannot tell us about the whole validity of our results, it can tell us whether our model can correctly fit the data with our combinations of inferred parameters.

We move into a regime in which our experimental measurements significantly strays from our simplified emission model, thus the ability to reproduce observed intensities with such certainty (Fig. <ref>) is very impressive.
A very small gap between uncertainties is seen at time = 0.783s for B_γ, nevertheless, the majority of cases demonstrate that our analysis reproduces the emissions very well, giving confidence in both our model used and free parameters inferred.



 §.§ Accessibility of Software


With the software meeting and exceeding expectations in validity, the accessibility of our approach is now considered.
We define a fair comparison between analysis methods as: when running the workstation at its limits, how fast does the analysis complete?
The results of this comparison are given in Tab. <ref>.



The time taken for our adaptive script is that of discharge 46860 because this was a larger input (30 lines-of-sight across 33 time points, whilst the simulated data was 20 lines-of-sight across 35 time points).
Analysing the smaller-scaled simulated data took 2.5 minutes.
Where the input data shows divergence from MAST-U specific data, BaySPMI begins to find difficulty, hence the range in time values.
The 46860 discharge took 4 hours, whilst the simulated discharge took up to 48 hours.

An important result for our analysis is the considerably smaller memory usage than BaySPMI, due to significantly less parameter space being evaluated.
This ensures no risk of computational overload and the ability to run with high performance on a personal computer, unlike the adaptive grid approach in <cit.>.
However, with access to the workstation used to run BaSPMI and BaySPMI, it allows the wide-scale multi-processing for our software to produce such fast results.

It should be noted that the time for BaSPMI includes the post-processing aspects of BaySPMI and our analysis.
However, to produce ion sources and sinks adds ≲ 0.5 minutes, and to reproduce emissions another ≲ 0.5 minutes.
Therefore, our analysis can easily complete within the 20 minutes between MAST-U pulses, allowing previously inaccessible insights into detachment processes before preceding discharges.



 §.§ Analysis of Detachment
 



First, we will discuss our observations during the SOLPS-ITER N_2 gas puffing experiment (Fig. <ref>) <cit.> in terms of the detachment physics involved. 
In this, a shift and detachment of the ionisation front is diagnosed as N_2 injection is increased and thus larger power losses occur (and thus more power limitation/ starvation, Eq. <ref>), inferring the divertor is currently in phase one detachment <cit.>. 
The comparative significance for ionisation against the ion sinks, could be explained by the higher temperatures in the divertor during N_2 gas puffing compared to a density ramp, which is not yet fully understood <cit.>. 
The lack of EIR and increase/movement of the MAR front is consistent with TCV experiments where no significant MAR or EIR is observed during N_2 seeding.

Moving from the N_2 seeded simulations to the MAST-U Super-X density ramp experiment 46860, excellent demonstration of the first three detachment phases is seen.
Our adaptive grid analysis is compared to the detachment phase infographic in Fig. <ref>.

Because the Super-X is so efficient at reducing heat flux and plasma temperatures in the divertor, discharge 46860 begins midway through phase 1 at time = 0.436s, as seen by the bulk of the ionisation being far from the target and a region of MAR already being established at the target.
The ion sources and sinks are shown at 0.569s to demonstrate the point of complete ionisation detachment from the target.

Our third time point in Fig. <ref> displays the plasma's movement into phase two.
Although the uncertainty on MAR does not yet infer a detachment of the front of the recombination region, our knowledge of detachment means that the onset of EIR at the target implies the shift in phase 2 and thus the movement of the MAR front.
Our last point in time demonstrates a clear detachment of the MAR front from the target and a large increase in EIR at the target.
This provides a clear implication that our system is now in phase three detachment.

If we explore the ion sources and sinks past 0.783s we do not see phase four detachment.
This is because further increasing the density would invite disruptions into the experimental set up of discharge 46860.
Lastly, it is implied that the ionisation front exits the divertor as identified in <cit.>.


§ DISCUSSION




 §.§ Results of Adaptive Grid Analysis


Both full and adaptive grid Bayesian analyses demonstrate difficulty in inferring f_mol unless the actual value is near the parameter boundaries (i.e. majority atomic or molecular interactions).
This is because f_mol cannot be restricted by a prior probability due to minimal diagnostic measurements for the separation of molecular and atomic contributions to emitted Balmer lines.

However in terms of ion source/sink estimation and emission reproduction, the specifically uncertain f_mol causes no concern.
It is important to reiterate here that our analysis samples a six-dimensional parameter space, not six parameters individually.
Therefore, the varied results we infer for f_mol may correspond more specifically with certain combinations of our other free parameters.
Once these combinations are amalgamated in our emission model we are then given more certain outcomes.

The miss in ionisation peak in Fig. <ref> at a puffing rate of 1×10^21mol./s is another result of interest, because this peak is included within the BaySPMI analysis.
A likely reason is the T_e^E prior using a manually estimated maximum in Fulcher emission intensity.
It may be that this estimation requires further refining before better reproduction of simulated inputs.
Nevertheless, it would be ideal to avoid this manual effort of tweaking the analysis to allow smoother use of such a software.
Therefore, other methods of estimating a prior probability for T_e^E would be welcomed into the script.
For example, knowledge that prolonged time during a density ramp experiment drives lower temperatures in the divertor could be used <cit.>.

Even so, we can currently maintain confidence in our uncertainties of ±1 standard deviation, without needing to expand to ±2, due to the good agreement across other lines-of-sight and puff rates.



 §.§ Balmer Emission Model


The Balmer emission model was originally simplified to six free parameters due to computational concerns for performing a full grid Bayesian analysis.
Our analysis demonstrates computational ease and efficiency for computing such a model over six-dimensional parameter space.
Therefore, there is potential to make this model more realistic with the introduction of further free parameters.

Although neglected throughout this report, negative molecular ions do play some role in the power and particle balance within the divertor <cit.>.
Hence, an additional ratio/fractional variable could be introduced within our B_n → 2^mol aspect of our emission model to consider the amount of contributions between negative and positive molecular ions.

Nevertheless, such improvements to the model may not ensure complete reproduction of emissions at high density (such as that seen in B_γ at 0.783s in Fig. <ref>) due to the increased photon opacity associated with large densities <cit.>.
For example, if there is a large population of neutral atoms surrounding our Balmer emitting interactions, the emitted photons could be absorbed and re-emitted at differing wavelengths, altering both the reaction rates found by our analysis as well as the PEC coefficients.



 §.§ Adaptive Grid Algorithm


Our use of the adaptive grid algorithm has been specifically specialised for optimised use on MAST-U Super-X data, providing concerns that the current state of the software may not be applicable to other tokamaks yet.
However, the script was able to accurately reproduce input values based upon Super-X data, as well as ion sources/sinks within Super-X simulations.
Neither scenarios are completely identical to the real Super-X measurements, with the latter causing great difficulty for the BaySPMI software.
Therefore, our analysis shows good signs for adaptability for use in differing divertor systems.
Since our analysis is built upon BaSPMI which has been tested on TCV (using density ramp and N_2 seeded simulations and experiments) as well as DIII-D, there is a high probability that it would work.
Even so, testing on data from other tokamak systems is essential before we can have confidence in extended use of such an analysis.

The adaptive grid analysis shows outstanding increases in efficiency over its predecessors.
However it is yet to be tested on real ultrafast spectroscopic data, which can measure Balmer emission intensities at 800kHz across 10 lines-of-sight.
The maximum amount of time points from these 10 lines-of-sight that could be analysed by our script within 20 minutes is ≈700 (App D.).

Whilst such an analysis would be of better time resolution than that possible for conventional spectroscopic measurements, we cannot neglect the potential for further increases in efficiency.
This increased efficiency could be achieved in a couple of ways.

    
  * Our analysis approach does not correctly converge for cell spacings that are of size equivalent to that of BaySPMI.
    This is because the algorithm only looks at the nearest neighbours directly adjacent to our current cell when initially climbing and filling the grid in probability.
    If cells are too large and we have complex six-dimensional shapes of probability, there is the possibility that the algorithm could miss areas of high probability that may have been `diagonal' to the current cell where finer cells would have gradually climbed to this area.
    Therefore, we could investigate a method of expanding the nearest neighbour search in order to utilise coarser cell spacing (equal to that of BaySPMI) for faster computation.
    
  * The adaptive grid algorithm is currently set up in such a way that it must first evaluate a cell with a parameter outside of our hard limits before it knows this is an unrealistic hypotheses.
    It is able to identify that it must search no further around this cell after this evaluation.
    A method of acceptance/rejection of hypotheses based upon the parameter values before any evaluations in the first place could overcome these inefficiencies.


It is worth noting that the ultrafast measurements are not intended for inter-shot analysis, but for specific analysis of shots after the fact, to study transient events.
Thus there are decreased time constraints for our analysis of ultrafast spectroscopic data.


§ CONCLUSIONS


We have presented the first application of a Bayesian inference technique for conventional and ultrafast spectroscopic measurements, utilising a new adaptive grid algorithm for probability analysis, to infer the physics within a divertor chamber during detachment.

A simplified emission model with six free parameters describing the Balmer line emissions due to plasma-atom (EIE and EIR) and plasma-molecular interactions was discussed and shown to successfully reproduce observed emission intensities.
The adaptive grid algorithm was established for use on observations from the Super-X divertor and used to infer the probabilities of these six free parameters, with minimal analysis inputs, which can be post-processed to obtain compelling estimates of ion sources/sinks and power loss mechanisms.
Methods for establishing the algorithm are accessible in the analysis script for ease of change across divertor configurations.

Initial testing within this work was performed on: ideal test cases to reproduce known parameter inputs; SOLPS-ITER simulations to reproduce known ion source/sink inputs; and MAST-U discharge 46860 to infer the same phases of detachment seen in BaySPMI analyses.
These tests have demonstrated the successful application for the analysis across all cases.

This analysis software also proves significant reductions in run-time and computational strain compared to previous analysis methods.
Thus, our foundational use of Bayesian techniques with adaptive grid algorithms for divertor diagnoses provides powerful understanding for plasma chemistry during detachment, previously unattainable between experimental discharges.
The flexibility of analyses associated with Bayesian approaches allows us to easily update our software for use with increased Balmer lines and prior conditions as they become available.

Future work will include further testing of this analysis software, across varying detachment scenarios and divertors, to build a more robust analysis script for wider-scale use.
Additionally, subsequent work on the efficiency of the adaptive script algorithm could lead to further applications across the tokamak diagnosis environment.



§ ACKNOWLEDGEMENTS


This work utilises PEC databases made using ADAS <cit.> and Yacora <cit.>, as well as simulations produced by Myatra, O, et al., <cit.> using SOLPS-ITER as the simulation software <cit.>.
Thanks go to the whole diagnostic team at CCFE working on MAST-U, to produce experimental spectroscopic data for detachment.
Lastly, my thanks go to my supervisors at UKAEA, Verhaegh, K and Bowman, C, for providing continued knowledge and support throughout the completion of this project.





Roman


 §.§ Appendix A. Analysis Script
 



  §.§.§ Appendix A.1 User Script


[language=Python]
from agsi.ReportAnalysis import FullAnalysis
import numpy as np

output = FullAnalysis('46860_BaySPMI_low_Te_150623_input.npy', specific_bound=True)

np.save('output_name.npy', output)





  §.§.§ Appendix A.2 Analysis Script


[language=Python]
from numpy import array, log, load, exp, linspace, ones, zeros, shape, nan, unravel_index, hstack
from pdfgrid import PdfGrid
from agsi.priors import *
from agsi.likelihood import *
from numpy.random import default_rng
import dms.analysis.emission.Balmer_analysis as BA
import matplotlib.pyplot as plt
import scipy.stats as scis
from multiprocessing import Pool
import numpy as np


def ImportData(input_data, band_check):
    Uncertainty = input_data['AbsErr']
    length_mode = input_data['DL']
    length_lower = input_data['DLL']
    length_upper = input_data['DLH']
    fulcher = input_data['Fulcher']
    fulcherLimits, te_lim_low, te_lim_high = BA.get_fulcher_constraint_spline(fulcher, telim=True, cummax=True)
    fulcherLimits = fulcherLimits
    DenMean = input_data['Den']
    DenErr = input_data['DenErr']
    n1 = int(input_data['n1'])
    n2 = int(input_data['n2'])

    if band_check == 3:
        D_alpha = input_data['n1Int']
        n2_line = input_data['n2Int']

        return D_alpha, n2_line, Uncertainty, length_mode, length_lower, length_upper, DenMean, DenErr, fulcherLimits, n1, n2

    else:
        D_alpha = input_data['DaMea']
        n1_line = input_data['n1Int']
        n2_line = input_data['n2Int']

        return D_alpha, n1_line, n2_line, Uncertainty, length_mode, length_lower, length_upper, DenMean, DenErr, fulcherLimits, n1, n2


def SpecificBounds(input_data):
    DenMax = input_data['DenMax']
    DenMin = input_data['DenMin']
    NeutralMax = input_data['noneH']
    NeutralMin = input_data['noneL']

    return DenMax, DenMin, NeutralMax, NeutralMin


def EmissionLines(n1, n2):
    n_lines = ["D_alpha", "D_beta", "D_gamma", "D_delta", "D_epsilon", "D_zeta", "D_eta"]
    if n1 == 3:
        line = n_lines[n1 - 3], n_lines[n2 - 3]
    else:
        line = n_lines[0], n_lines[n1 - 3], n_lines[n2 - 3]

    return line


def TwoBands(D_alpha, n2_line, uncertainty, length_mode, length_lower, length_upper, DenMean, DenErr, fulcherLimits, n1, n2, DenMax=4e19, DenMin=5e18, NeutralMax=1, NeutralMin=1e-3):

    if np.isnan(n2_line):
        adaptive_sample = np.full((500, 6), np.nan)

    else:
        lines = EmissionLines(n1, n2)
        simulated_data = array([D_alpha, n2_line])
        EmissionData = SpecData(
            lines=[lines[0], lines[1]],
            brightness=simulated_data,
            uncertainty=simulated_data*uncertainty
        )

        """
        Build posterior
        """
        likelihood = SpecAnalysisLikelihood(
            measurements=EmissionData
        )

        HardLimit = BoundaryPrior(
            upper_limits=array([log(5), log(1), log(DenMax), log(NeutralMax), 0.95, 0.4]),
            lower_limits=array([log(0.2), log(0.02), log(DenMin), log(NeutralMin), 0.01, 0.02])
        )

        ColdHotPrior = ColdTempPrior()

        length_prior = PathLengthPrior(mode=length_mode, lower_limit=length_lower, upper_limit=length_upper)

        FulcherPrior = HotTempPrior(fulcherLimits)

        DensityPrior = ElectronDensityPrior(mean=DenMean, sigma=DenErr)

        posterior = Posterior(
            components=[likelihood, ColdHotPrior, length_prior, FulcherPrior, DensityPrior],
            boundary_prior=HardLimit
        )

        """
        search for a good starting point
        """
        rng = default_rng()
        hypercube_samples = rng.uniform(low=HardLimit.lwr, high=HardLimit.upr, size=[100000, 6])
        hypercube_probs = posterior(hypercube_samples)
        grid_centre = hypercube_samples[hypercube_probs.argmax(), :]


        """
        Run the algorithm
        """
        grid_spacing = array([0.06, 0.09, 0.09, 0.15, 0.06, 0.02])
        grid_bounds = array([[log(0.2), log(0.02), log(DenMin), log(NeutralMin), 0.01, 0.02], [log(5), log(1), log(DenMax), log(NeutralMax), 0.95, 0.4]]).T
        grid = PdfGrid(spacing=grid_spacing, offset=grid_centre, bounds=grid_bounds, convergence=1e-1, n_samples=10000, n_climbs=200)

        while grid.state != "end":
            params = grid.get_parameters()
            P = posterior(params)
            grid.give_probabilities(P)

        adaptive_sample = grid.generate_sample(HardLimit, 500)

    return adaptive_sample


def ThreeBands(D_alpha, n1_line, n2_line, uncertainty, length_mode, length_lower, length_upper, DenMean, DenErr, fulcherLimits, n1, n2, DenMax=4e19, DenMin=5e18, NeutralMax=1, NeutralMin=1e-3):

    if np.isnan(n1_line) or np.isnan(n2_line):
        adaptive_sample = np.full((500, 6), np.nan)

    else:
        """
        Construct emission model
        """
        lines = EmissionLines(n1, n2)
        emission_data = array([D_alpha, n1_line, n2_line])
        EmissionData = SpecData(
            lines=[lines[0], lines[1], lines[2]],
            brightness=emission_data,
            uncertainty=emission_data*uncertainty
        )

        """
        Build posterior
        """
        likelihood = SpecAnalysisLikelihood(
            measurements=EmissionData
        )

        HardLimit = BoundaryPrior(
            upper_limits=array([log(5), log(1), log(DenMax), log(NeutralMax), 0.95, 0.4]),
            lower_limits=array([log(0.2), log(0.02), log(DenMin), log(NeutralMin), 0.01, 0.02])
        )

        ColdHotPrior = ColdTempPrior()

        length_prior = PathLengthPrior(mode=length_mode, lower_limit=length_lower, upper_limit=length_upper)

        FulcherPrior = HotTempPrior(fulcherLimits)

        DensityPrior = ElectronDensityPrior(mean=DenMean, sigma=DenErr)

        posterior = Posterior(
            components=[likelihood, ColdHotPrior, length_prior, FulcherPrior, DensityPrior],
            boundary_prior=HardLimit
        )

        """
        Run adaptive grid algorithm
        """
        grid_spacing = array([0.06, 0.09, 0.09, 0.15, 0.06, 0.02])
        grid_bounds = array([[log(0.2), log(0.02), log(DenMin), log(NeutralMin), 0.01, 0.02], [log(5), log(1), log(DenMax), log(NeutralMax), 0.95, 0.4]]).T
        grid_centre = (grid_bounds[:, 1] - grid_bounds[:, 0]) / 2
        grid = PdfGrid(spacing=grid_spacing, offset=grid_centre, bounds=grid_bounds, convergence=1e-1, n_samples=10000, n_climbs=200)

        while grid.state != "end":
            params = grid.get_parameters()
            P = posterior(params)
            grid.give_probabilities(P)

        adaptive_sample = grid.generate_sample(HardLimit, 500)

    return adaptive_sample


def OutputStructure(input, TeEMC, TeRMC, DenMC, noneMC, fmolMC, DLMC, PresMC):
    input['n1'] = int(input['n1'])
    input['n2'] = int(input['n2'])
    output = dict()
    output['input'] = input
    output['ResultMC'] = dict()
    output['ResultMC']['DenMC'] = DenMC
    output['ResultMC']['TeEMC'] = TeEMC
    output['ResultMC']['TeRMC'] = TeRMC
    output['ResultMC']['fmolMC'] = fmolMC
    output['ResultMC']['DLMC'] = DLMC
    output['ResultMC']['noneMC'] = noneMC
    output['ResultMC']['PresMC'] = PresMC

    return output


def FullAnalysis(input_file, poolcount=48, specific_bound=False):
    input_data = load(input_file, allow_pickle=True)
    input_data = input_data[()]
    band_check = input_data['n1']

    Iter = 500

    p = []
    pool = Pool(poolcount)

    if band_check == 3:
        D_alpha, n2_line, Uncertainty, length_mode, length_lower, length_upper, DenMean, DenErr, fulcherLimits, n1, n2 = ImportData(input_data, band_check)

        DenMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        TeEMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        TeRMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        DLMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        noneMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        fmolMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        PresMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan

        S = shape(DenMean)

        print('appending operations to pool')

        if specific_bound:
            DenMax, DenMin, NeutralMax, NeutralMin = SpecificBounds(input_data)

            for l in range(0, S[0]*S[1]):
                i, j = unravel_index(l, S)

                p.append(pool.apply_async(TwoBands, (D_alpha[i, j], n2_line[i, j], Uncertainty, length_mode[i, j], length_lower[i, j], length_upper[i, j], DenMean[i, j], DenErr[i, j], fulcherLimits[i, j], n1, n2), dict(DenMax=DenMax, DenMin=DenMin, NeutralMax=NeutralMax[i, j], NeutralMin=NeutralMin[i, j])))

        else:
            for l in range(0, S[0] * S[1]):
                i, j = unravel_index(l, S)

                p.append(pool.apply_async(TwoBands, (D_alpha[i, j], n2_line[i, j], Uncertainty, length_mode[i, j], length_lower[i, j], length_upper[i, j], DenMean[i, j], DenErr[i, j], fulcherLimits[i, j], n1, n2)))

        print('executing pool')
        sample = [p[hh].get() for hh in range(len(p))]

        for l in range(0, S[0]*S[1]):
            i, j = unravel_index(l, S)

            TeEMC[i, j, :] = exp(sample[l][:, 0])
            TeRMC[i, j, :] = exp(sample[l][:, 1])
            DenMC[i, j, :] = exp(sample[l][:, 2])
            noneMC[i, j, :] = exp(sample[l][:, 3])
            fmolMC[i, j, :] = sample[l][:, 4]
            DLMC[i, j, :] = sample[l][:, 5]
            PresMC[i, j, :] = exp(sample[l][:, 0]+sample[l][:,2])

        output = OutputStructure(input_data, TeEMC, TeRMC, DenMC, noneMC, fmolMC, DLMC, PresMC)

    else:
        D_alpha, n1_line, n2_line, Uncertainty, length_mode, length_lower, length_upper, DenMean, DenErr, fulcherLimits, n1, n2 = ImportData(input_data, band_check)

        DenMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        TeEMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        TeRMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        DLMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        noneMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        fmolMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan
        PresMC = zeros([shape(DenMean)[0], shape(DenMean)[1], Iter]) + nan

        S = shape(DenMean)

        print('appending operations to pool')

        if specific_bound:
            DenMax, DenMin, NeutralMax, NeutralMin = SpecificBounds(input_data)

            for l in range(0, S[0] * S[1]):
                i, j = unravel_index(l, S)

                p.append(pool.apply_async(ThreeBands, (D_alpha[i, j], n1_line[i, j], n2_line[i, j], Uncertainty, length_mode[i, j], length_lower[i, j], length_upper[i, j], DenMean[i, j], DenErr[i, j], fulcherLimits[i, j], n1, n2), dict(DenMax=DenMax, DenMin=DenMin, NeutralMax=NeutralMax[i, j], NeutralMin=NeutralMin[i, j])))

        else:
            for l in range(0, S[0] * S[1]):
                i, j = unravel_index(l, S)

                p.append(pool.apply_async(ThreeBands, (D_alpha[i, j], n1_line[i, j], n2_line[i, j], Uncertainty, length_mode[i, j], length_lower[i, j], length_upper[i, j], DenMean[i, j], DenErr[i, j], fulcherLimits[i, j], n1, n2)))

        print('executing pool')
        sample = [p[hh].get() for hh in range(len(p))]

        for l in range(0, S[0] * S[1]):
            i, j = unravel_index(l, S)

            TeEMC[i, j, :] = exp(sample[l][:, 0])
            TeRMC[i, j, :] = exp(sample[l][:, 1])
            DenMC[i, j, :] = exp(sample[l][:, 2])
            noneMC[i, j, :] = exp(sample[l][:, 3])
            fmolMC[i, j, :] = sample[l][:, 4]
            DLMC[i, j, :] = sample[l][:, 5]
            PresMC[i, j, :] = exp(sample[l][:, 0]+sample[l][:, 2])

        output = OutputStructure(input_data, TeEMC, TeRMC, DenMC, noneMC, fmolMC, DLMC, PresMC)

    return output






 §.§ Appendix B. Ideal Test Cases




  §.§.§ Appendix B.1 Ideal Test Case Set-Up


Our ideal test case scenario was produced by feeding a realistic combination of parameters (Tab. <ref>) into our emission model (Eq. <ref>) to produce intensities of Balmer lines for use in our analysis.



A comparative full grid approach for the Bayesian analysis used in the adaptive grid script was produced to provide an additional test of validity.
Such a comparison can verify whether any failed reproductions of the ideal test data is due to the adaptive grid approach, or the Bayesian analysis and subsequent emission model itself.
The full grid script uses 25 equally spaced values within the limits of each parameter to produce the parameter space. 

Prior probability incorporation within the ideal test case has been reduced to prevent the potential for over-complication leading to unrealistic test scenarios.
Therefore, the utilisation of Fulcher emissions and Stark broadening for temperature and electron density priors respectively, have been neglected.
Hard limits for the parameters are those stated in Section <ref>.
Prior models for the comparative cold/hot temperatures and path length are kept, due to certainty of T_e^R < T_e^E and strong knowledge of the path length parameter in MAST-U.



  §.§.§ Appendix B.2 Ideal Test Case Emission Reproduction




We plot the PDFs of our reproduced emissions for the contributing Balmer lines in Fig. <ref>.
Although our ideal test case utilises the same model to create the emissions, such a plot can tell us whether our sampled output parameters can correctly fit the data.
Considering the consistency between parameter input and PDF in Fig. <ref>, good agreement is also seen between reproduced and model emissions across all Balmer lines.
Confidence for our adaptive analysis is given by the reproduction of all possible model emission inputs.
Whilst we do reproduce emissions outside of our inputs, these are of low probability density, and are also seen within the full grid approach.
Lastly, the bulk of our reproduced PDFs remain within the model Gaussian for Balmer emission intensities.



The same plot was made for the ultrafast spectrometer ideal test case, again, all model emission inputs are reproduced by our analysis (Fig. <ref>), inferring that the incorrect peak in f_mol in Fig. <ref> is not detrimental to the model in full.





 §.§ Appendix C. Free Parameter Validation
 

Before post-processing was performed on the sample parameter results from the adaptive grid and BaySPMI analyses, important parameters were compared for both simulated ultrafast (Fig. <ref>) and experimental conventional spectroscopic inputs (Fig. <ref>).
Agreement via overlapping uncertainties between analyses can be seen across all parameters for all times and LoS, giving confidence in the parameter inferrence of our analysis.









 §.§ Appendix D. Maximum Frequency Analysis


Our analysis script is shown to complete investigation of Balmer emission intensities for 30 lines-of-sight across 33 points in time within 180 seconds.
15 of these 180 seconds account for setting up the analysis.
Thus, our time for evaluations is 165 seconds, and our time per evaluation (t) is ≈0.17s.

To analyse ultrafast spectroscopic measurements across 10 lines-of-sight within 20 minutes (1200 seconds) we define the following equation, in which f is frequency of measurements.


    f × LoS × t    = 1200
    
        f    = 700


Thus we can only analyse 700 time points for ultrafast spectroscopic measurements within 20 minutes.





