From ae49860fe63a3623f7ee624c0937083889a8cca8 Mon Sep 17 00:00:00 2001 From: marcosertoli <74655352+marcosertoli@users.noreply.github.com> Date: Mon, 31 Jul 2023 15:08:34 +0200 Subject: [PATCH] Marcosertoli/zeff profile (#272) * New reader from spectroscopy pre-analysis (currently reading only emissivity and named "spectra") * Added gaunt factor approximation from Carson 1988 * first commit * first commit * second commit, updated background_fit * added **kwargs to call function * test * he_like * added pi diagnostic * new changes * commit * error with dimensions * now it works, but the data is not what is wanted to be * changed impurity from ar to c changed saving path * dict * updated dict, question about model_data in bayes * now bayes works * Temporarily stop caching (issues to be solved) * Copied Bremss bayes opt to new file * Marcosertoli/satakihino (#263) * Temporarily stop caching (issues to be solved) * Copied Bremss bayes opt to new file --------- Co-authored-by: Marco Sertoli * CHanged .sel to .interp for plasma attributes as in other models. * Ran pre-commit * Fixed pre-commit errors * Fixed pre-commit errors * Reconverted interp to sel. Made transmission filter a DataArray. Started moving Aleksandra's spectra filtering to Diode model * Fixed pre-commit * Temporarily commented out t-interp case for diode filter (sel instead of interp...) * st40reader.py passing pre-commit * Moved background_fit to a method inside the diode filter model and generalised for spectral integration. * Fixed plotting axes ranges * Added generalised example that can use phantom data/equilibrium as well as experimental data from SXR cameras and diode filter models/data. * Added emissivity attribute = local Brems emission integrated over wavelength. Changed example geometry adding LOS to make inversion possible. * First stab at file with workflows to calculate Zeff using tomo_1d or Bayesian inference * Adapted tomo1d examples to run on "pi" bremsstrahlung. Working on phantom and real data. * Temporary fix to slash channels in spectrometer that are not acquired * Corrected factor multiplication for zeff calculation from bremsstrahlung * Initialize variables now easily callable after having instatiated the class * New example geometry and in standalone method so to be usable also from outside * Refactored methods to estimate Zeff from bremsstrahlung using inversion or bayesian inference. * Update bayesmodels.py adding zeff to saved quantites * Update bayes_workflow.py zeff added to plots * Update example_bayes_opt_brems.py zeff added to phantom profiles * Update bayesmodels.py Zeff -> zeff * Modified spectrometer reader to select good channels only * Zeff blob summed over elements * Zeff blob summed over elements * Set uncertainty to zero until correctly calculated * Changed filter limits to nested dictionary to be applicable to diagnostics with multiple quantities * Corrected minor typo * Formatting mod to pass pre-commit * Temporary fix to get abstract reader test to pass. * Modified workflows to accept both phantoms and experimental data, including fitting of TS. Four example methods to run at the end of the file. * Cleaned up so inference works better. Still some inconsistencies between Zeff and Impurity density... * Refactored to pass pre-commit * Marcosertoli/reorganize bremss calculation (#267) * Temporarily stop caching (issues to be solved) * Copied Bremss bayes opt to new file * CHanged .sel to .interp for plasma attributes as in other models. * Ran pre-commit * Fixed pre-commit errors * Fixed pre-commit errors * Reconverted interp to sel. Made transmission filter a DataArray. Started moving Aleksandra's spectra filtering to Diode model * Fixed pre-commit * Temporarily commented out t-interp case for diode filter (sel instead of interp...) * st40reader.py passing pre-commit * Moved background_fit to a method inside the diode filter model and generalised for spectral integration. * Fixed plotting axes ranges * Added generalised example that can use phantom data/equilibrium as well as experimental data from SXR cameras and diode filter models/data. * Added emissivity attribute = local Brems emission integrated over wavelength. Changed example geometry adding LOS to make inversion possible. * First stab at file with workflows to calculate Zeff using tomo_1d or Bayesian inference * Adapted tomo1d examples to run on "pi" bremsstrahlung. Working on phantom and real data. * Temporary fix to slash channels in spectrometer that are not acquired * Corrected factor multiplication for zeff calculation from bremsstrahlung * Initialize variables now easily callable after having instatiated the class * New example geometry and in standalone method so to be usable also from outside * Refactored methods to estimate Zeff from bremsstrahlung using inversion or bayesian inference. * Update bayesmodels.py adding zeff to saved quantites * Update bayes_workflow.py zeff added to plots * Update example_bayes_opt_brems.py zeff added to phantom profiles * Update bayesmodels.py Zeff -> zeff * Modified spectrometer reader to select good channels only * Zeff blob summed over elements * Zeff blob summed over elements * Set uncertainty to zero until correctly calculated * Changed filter limits to nested dictionary to be applicable to diagnostics with multiple quantities * Corrected minor typo * Formatting mod to pass pre-commit * Temporary fix to get abstract reader test to pass. * Modified workflows to accept both phantoms and experimental data, including fitting of TS. Four example methods to run at the end of the file. * Cleaned up so inference works better. Still some inconsistencies between Zeff and Impurity density... * Refactored to pass pre-commit --------- Co-authored-by: Marco Sertoli Co-authored-by: michael-gemmell <87419247+michael-gemmell@users.noreply.github.com> * Fixed linting issues * Modified Bayesian phantoms and priors & changed plotting to include a rough uncertainty in Zeff * Limited Zeff plotting to (0, 10) * Moved start and end times to call kwargs * Refactored workflow to get all the steps in the right order. Both Bayes and Inv now working on phantoms. Inv also on data --> need to find other experimental cases with better EFIT. * Deleted unused example. * Fixed pre-commit error * Fixed issues tied to "c" vs "ar" * Profile assignment now element dependent * Deleted print statement in interferometry model * Deleted example_bayes_opt_brems.py as all zeff profile workflows are now contained in zeff_profile.py * Deleted background_fit.py that is now included in filter model * Reverted to previous figheader --------- Co-authored-by: Marco Sertoli Co-authored-by: Aleksandra Alieva Co-authored-by: satakihino Co-authored-by: michael-gemmell <87419247+michael-gemmell@users.noreply.github.com> --- Reconstruction.npz | Bin 0 -> 13464 bytes indica/bayesmodels.py | 7 +- indica/models/diode_filters.py | 132 ++++- indica/models/helike_spectroscopy.py | 3 +- indica/models/interferometry.py | 1 - indica/models/plasma.py | 66 +-- indica/operators/gpr_fit.py | 5 +- indica/operators/tomo_1D.py | 4 +- indica/physics.py | 4 +- indica/readers/abstractreader.py | 32 +- indica/readers/read_st40.py | 47 +- indica/readers/st40reader.py | 4 +- indica/workflows/bayes_workflow.py | 38 +- indica/workflows/example_bayes_opt.py | 4 +- indica/workflows/run_tomo_1d.py | 175 +++++- indica/workflows/zeff_profile.py | 619 ++++++++++++++++++++ poetry.lock | 6 +- tests/unit/models/test_diagnostic_models.py | 4 +- 18 files changed, 1023 insertions(+), 128 deletions(-) create mode 100644 Reconstruction.npz create mode 100644 indica/workflows/zeff_profile.py diff --git a/Reconstruction.npz b/Reconstruction.npz new file mode 100644 index 0000000000000000000000000000000000000000..0361539c3a71210f6ca1866bc36d1523b45822c9 GIT binary patch literal 13464 zcmb_@b981)^kvfN*tTukwr$&X(ox5@ZQHhOJL%X?ru)tB&Aj*K-#KU1x7MvXd)>OX z>f5VM)x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KJOIGoLX!WY{;fgy zCkh|&H|lRi!hd1^)(iX-R`DD5w|d~esDB^H{}bi=d*uGl0sVhr|K7a#CoF>Se_;Rj VRRV+j2>}1?px*!hJQ@C6{Vxow 0 + _transmission = self.transmission.interp(wavelength=spectra.wavelength) + transmission = _transmission.where(_transmission > 1.0e-3, drop=True) + wavelength_slice = slice( + np.min(transmission.wavelength), np.max(transmission.wavelength) + ) + + # Take away neutron spikes in pixel intensity + _spectra = spectra.sortby("wavelength").sel(wavelength=wavelength_slice) + _spectra = _spectra.where( + xr.ufuncs.fabs(_spectra.diff("wavelength", n=2)) < 0.4e-3 + ) + + # Fit spectra to calculate background emission, filter and integrate + if fit_background: + fit = _spectra.polyfit("wavelength", 0) + _spectra_to_integrate = fit.polyfit_coefficients.sel(degree=0) + spectra_to_integrate = _spectra_to_integrate.expand_dims( + dim={"wavelength": _spectra.wavelength} + ) + else: + spectra_to_integrate = _spectra + integral = (spectra_to_integrate * transmission).sum("wavelength") + + integral.attrs["error"] = integral * 0.0 + spectra_to_integrate.attrs["error"] = spectra_to_integrate * 0.0 + + return spectra_to_integrate, integral def _build_bckc_dictionary(self): self.bckc = {} @@ -81,7 +146,7 @@ def _build_bckc_dictionary(self): "stdev": stdev, "provenance": str(self), "long_name": "Brightness", - "units": "W $m^{-2}$", + "units": "W m^{-2}", } else: print(f"{quant} not available in model for {self.instrument_method}") @@ -94,6 +159,7 @@ def __call__( Zeff: DataArray = None, t: LabeledArray = None, calc_rho: bool = False, + **kwargs, ): """ Calculate Bremsstrahlung emission and model measurement @@ -110,14 +176,15 @@ def __call__( Total effective charge t time + TODO: emission needs a new name as it's in units [W m**-2 nm**-1] """ if self.plasma is not None: if t is None: - t = self.plasma.t - Ne = self.plasma.electron_density.interp(t=t) - Te = self.plasma.electron_temperature.interp(t=t) - Zeff = self.plasma.zeff.interp(t=t).sum("element") + t = self.plasma.time_to_calculate + Ne = self.plasma.electron_density.sel(t=t) + Te = self.plasma.electron_temperature.sel(t=t) + Zeff = self.plasma.zeff.sel(t=t).sum("element") else: if Ne is None or Te is None or Zeff is None: raise ValueError("Give inputs of assign plasma class!") @@ -127,50 +194,51 @@ def __call__( self.Ne: DataArray = Ne self.Zeff: DataArray = Zeff - # Wavelength axis - wavelength = np.linspace( - self.filter_wavelength - self.filter_fwhm * 2, - self.filter_wavelength + self.filter_fwhm * 2, - ) - self.wavelength = DataArray(wavelength, coords=[("wavelength", wavelength)]) - - # Transmission filter function - self.transmission = ph.make_window( - wavelength, - self.filter_wavelength, - self.filter_fwhm, - window=self.filter_type, - ) - # Bremsstrahlung emission for each time, radial position and wavelength wlength = deepcopy(self.wavelength) for dim in Ne.dims: wlength = wlength.expand_dims(dim={dim: self.Ne[dim]}) self.emission = ph.zeff_bremsstrahlung(Te, Ne, wlength, zeff=Zeff) - + self.emissivity = (self.emission * self.transmission).integrate("wavelength") los_integral = self.los_transform.integrate_on_los( - (self.emission * self.transmission).integrate("wavelength"), + self.emissivity, t=t, calc_rho=calc_rho, ) + if self.channel_mask is not None: + los_integral = los_integral.where( + (los_integral.channel > self.channel_mask.start) + & (los_integral.channel < self.channel_mask.stop) + ) self.los_integral = los_integral self._build_bckc_dictionary() - return self.bckc -def example_run(pulse: int = None, plasma=None, plot: bool = False): +def example_geometry(nchannels: int = 12): + + los_end = np.full((nchannels, 3), 0.0) + los_end[:, 0] = 0.0 + los_end[:, 1] = np.linspace(-0.2, -1, nchannels) + los_end[:, 2] = 0.0 + los_start = np.array([[1.5, 0, 0]] * los_end.shape[0]) + origin = los_start + direction = los_end - los_start + + return origin, direction + + +def example_run( + pulse: int = None, nchannels: int = 12, plasma=None, plot: bool = False +): if plasma is None: plasma = example_plasma(pulse=pulse) # Create new interferometers diagnostics diagnostic_name = "diode_brems" - los_start = np.array([[0.8, 0, 0], [0.8, 0, -0.1], [0.8, 0, -0.2]]) - los_end = np.array([[0.17, 0, 0], [0.17, 0, -0.25], [0.17, 0, -0.2]]) - origin = los_start - direction = los_end - los_start + origin, direction = example_geometry(nchannels=nchannels) los_transform = LineOfSightTransform( origin[:, 0], origin[:, 1], @@ -214,8 +282,8 @@ def example_run(pulse: int = None, plasma=None, plot: bool = False): plt.figure() for i, t in enumerate(plasma.t.values): plt.plot( - model.emission.rho_poloidal, - model.emission.sel(t=t).integrate("wavelength"), + model.emissivity.rho_poloidal, + model.emissivity.sel(t=t), color=cols_time[i], label=f"t={t:1.2f} s", ) diff --git a/indica/models/helike_spectroscopy.py b/indica/models/helike_spectroscopy.py index 73f0407a..5cd4ef99 100644 --- a/indica/models/helike_spectroscopy.py +++ b/indica/models/helike_spectroscopy.py @@ -351,7 +351,7 @@ def __call__( Nh: DataArray = None, t: LabeledArray = None, calc_rho: bool = False, - moment_analysis: bool = True, + moment_analysis: bool = False, **kwargs, ): """ @@ -427,7 +427,6 @@ def __call__( self._moment_analysis() self._build_bckc_dictionary() - return self.bckc diff --git a/indica/models/interferometry.py b/indica/models/interferometry.py index ee9b673b..3ffac6e1 100644 --- a/indica/models/interferometry.py +++ b/indica/models/interferometry.py @@ -87,7 +87,6 @@ def __call__( self.los_integral_ne = los_integral_ne self._build_bckc_dictionary() - return self.bckc diff --git a/indica/models/plasma.py b/indica/models/plasma.py index ef551681..0f3a03d0 100644 --- a/indica/models/plasma.py +++ b/indica/models/plasma.py @@ -146,6 +146,9 @@ def __init__( """ self.pulse = pulse + self.tstart = tstart + self.tend = tend + self.dt = dt self.full_run = full_run self.verbose = verbose self.ADASReader = ADASReader() @@ -166,7 +169,7 @@ def __init__( self.radial_coordinate_type = "rho_poloidal" self.machine_dimensions = machine_dimensions - self.initialize_variables(tstart, tend, dt) + self.initialize_variables() self.equilibrium: Equilibrium @@ -188,28 +191,16 @@ def set_equilibrium(self, equilibrium: Equilibrium): def set_adf11(self, adf11: dict): self.adf11 = adf11 - def initialize_variables(self, tstart: float, tend: float, dt: float): + def initialize_variables(self): """ Initialize all class attributes - Parameters - ---------- - tstart - start time - tend - end time - dt - time-step - Description of variables being initialized ------------------------------------------ time_to_calculate subset of time-point(s) to use for computation of the dependent variables (to be used e.g. in optimisation workflows) """ - self.tstart = tstart - self.tend = tend - self.dt = dt # Dictionary keeping track of deta use for optimisations self.optimisation: dict = {} @@ -350,7 +341,6 @@ def initialize_variables(self, tstart: float, tend: float, dt: float): self._pressure_th = assign_data( self.data2d, ("pressure", "thermal"), "Pa $m^{-3}$" ) - self._ion_density = assign_data(self.data3d, ("density", "ion"), "$m^{-3}$") self._pressure_tot = assign_data( self.data2d, ("pressure", "total"), "Pa $m^{-3}$" ) @@ -470,20 +460,33 @@ def initialize_variables(self, tstart: float, tend: float, dt: float): ) def assign_profiles( - self, profile: str = "electron_density", t: float = None, element: str = "ar" + self, profile: str = "electron_density", t: float = None, element: str = None ): + # TODO: impurities and elements should be both either tuples or lists... + elements: list = [] + impurities: tuple = () + if element is None: + elements = self.elements + impurities = self.impurities + else: + elements = [element] + if element in self.impurities: + impurities = impurities if profile == "electron_density": self.electron_density.loc[dict(t=t)] = self.Ne_prof() elif profile == "electron_temperature": self.electron_temperature.loc[dict(t=t)] = self.Te_prof() elif profile == "ion_temperature": - self.ion_temperature.loc[dict(t=t)] = self.Ti_prof() + for elem in elements: + self.ion_temperature.loc[dict(t=t, element=elem)] = self.Ti_prof() elif profile == "toroidal_rotation": - self.toroidal_rotation.loc[dict(t=t)] = self.Vrot_prof() + for elem in elements: + self.toroidal_rotation.loc[dict(t=t, element=elem)] = self.Vrot_prof() elif profile == "impurity_density": - self.impurity_density.loc[dict(t=t, element=element)] = self.Nimp_prof() + for imp in impurities: + self.impurity_density.loc[dict(t=t, element=imp)] = self.Nimp_prof() elif profile == "neutral_density": - self.neutral_density.loc[dict(t=t, element=element)] = self.Nh_prof() + self.neutral_density.loc[dict(t=t)] = self.Nh_prof() else: raise ValueError( f"{profile} currently not found in possible Plasma properties" @@ -598,7 +601,7 @@ def wp(self): @property def fz(self): - return self.Fz() + return self.calc_fz() # self.Fz() def calc_fz(self): for elem in self.elements: @@ -621,7 +624,7 @@ def calc_fz(self): @property def zeff(self): - return self.Zeff() + return self.calc_zeff() # Zeff() def calc_zeff(self): electron_density = self.electron_density @@ -636,7 +639,7 @@ def calc_zeff(self): @property def ion_density(self): - return self.Ion_density() + return self.calc_ion_density() # self.Ion_density() def calc_ion_density(self): meanz = self.meanz @@ -656,7 +659,7 @@ def calc_ion_density(self): @property def lz_tot(self): - return self.Lz_tot() + return self.calc_lz_tot() # self.Lz_tot() def calc_lz_tot(self): fz = self.fz @@ -681,7 +684,7 @@ def calc_lz_tot(self): @property def lz_sxr(self): - return self.Lz_sxr() + return self.calc_lz_sxr() # self.Lz_sxr() def calc_lz_sxr(self): fz = self.fz @@ -708,7 +711,7 @@ def calc_lz_sxr(self): @property def total_radiation(self): - return self.Total_radiation() + return self.calc_total_radiation() # self.Total_radiation() def calc_total_radiation(self): lz_tot = self.lz_tot @@ -728,7 +731,7 @@ def calc_total_radiation(self): @property def sxr_radiation(self): - return self.Sxr_radiation() + return self.calc_sxr_radiation() # self.Sxr_radiation() def calc_sxr_radiation(self): if not hasattr(self, "power_loss_sxr"): @@ -1238,6 +1241,7 @@ def __init__(self, operator: Callable, dependencies: list, verbose: bool = False @lru_cache() def __call__(self): + print("Recalculating") if self.verbose: print("Recalculating") return self.operator() @@ -1279,7 +1283,7 @@ def example_run( vrot0 = np.linspace(plasma.Vrot_prof.y0 * 1.1, plasma.Vrot_prof.y0 * 2.5, nt) ti0 = np.linspace(plasma.Ti_prof.y0 * 1.1, plasma.Te_prof.y0 * 2.5, nt) nimp_peaking = np.linspace(1, 5, nt) - nimp_y0 = plasma.Nimp_prof.y0 * np.linspace(1, 8, nt) + nimp_y0 = plasma.Nimp_prof.y0 * 5 * np.linspace(1, 8, nt) nimp_wcenter = np.linspace(0.4, 0.1, nt) for i, t in enumerate(plasma.t): plasma.Te_prof.peaking = te_peaking[i] @@ -1299,11 +1303,7 @@ def example_run( plasma.Nimp_prof.peaking = nimp_peaking[i] plasma.Nimp_prof.y0 = nimp_y0[i] plasma.Nimp_prof.wcenter = nimp_wcenter[i] - for elem in plasma.impurities: - plasma.assign_profiles(profile="impurity_density", t=t, element=elem) - - for elem in plasma.elements: - plasma.assign_profiles(profile="toroidal_rotation", t=t, element=elem) + plasma.assign_profiles(profile="impurity_density", t=t) if pulse is None: equilibrium_data = fake_equilibrium_data( diff --git a/indica/operators/gpr_fit.py b/indica/operators/gpr_fit.py index 403319db..0659c744 100644 --- a/indica/operators/gpr_fit.py +++ b/indica/operators/gpr_fit.py @@ -252,6 +252,8 @@ def plot_gpr_fit( def example_run( pulse: int = 10619, + tstart=0.02, + tend=0.1, kernel_name: str = "RBF_noise", plot=True, save_fig=False, @@ -261,9 +263,6 @@ def example_run( err_bounds: tuple = (0, 0), ): - tstart = 0.02 - tend = 0.10 - st40 = ReadST40(pulse, tstart, tend) st40(instruments=["ts", "efit"]) diff --git a/indica/operators/tomo_1D.py b/indica/operators/tomo_1D.py index 0fb2d7bd..de361b0b 100644 --- a/indica/operators/tomo_1D.py +++ b/indica/operators/tomo_1D.py @@ -532,8 +532,6 @@ def show_reconstruction(self): self.eq["R"], self.eq["z"], self.eq["rho"][0], cvals, colors="k" ) - ax[2].set_ylim(self.eq["R"][[0, -1]]) - ax[2].set_xlim(self.eq["z"][[0, -1]]) ax[2].axis("equal") ax[2].plot(self.R.T, self.z.T, "b", zorder=99) R, z = np.meshgrid(self.eq["R"], self.eq["z"]) @@ -548,6 +546,8 @@ def show_reconstruction(self): ) ax[2].set_ylabel("z [m]") ax[2].set_xlabel("R [m]") + ax[2].set_xlim(self.eq["R"][[0, -1]]) + ax[2].set_ylim(self.eq["z"][[0, -1]]) f.subplots_adjust(wspace=0.3) diff --git a/indica/physics.py b/indica/physics.py index 4bb3b24a..8d3bcfed 100644 --- a/indica/physics.py +++ b/indica/physics.py @@ -1060,15 +1060,13 @@ def zeff_bremsstrahlung( * Ne_cm**2 / (np.sqrt(Te) * wavelength_ang**2) * np.exp(-12400 / (wavelength_ang * Te)) - ) + ) * reconvert_units if zeff is None: result = bremsstrahlung / factor else: result = zeff * factor - result *= reconvert_units - return result diff --git a/indica/readers/abstractreader.py b/indica/readers/abstractreader.py index 7421f9c0..e34f253e 100644 --- a/indica/readers/abstractreader.py +++ b/indica/readers/abstractreader.py @@ -381,10 +381,25 @@ def get_spectrometer( ) -> Dict[str, DataArray]: """ Reads spectroscopy data + TODO: find better way to filter non-acquired channels + TODO: check spectra uncertainty... """ database_results = self._get_spectrometer(uid, instrument, revision, quantities) - _channel = np.arange(database_results["length"]) + if instrument == "pi": + has_data = np.arange(21, 28) + else: + has_data = np.where( + np.isfinite(database_results["spectra"][0, :, 0]) + * (database_results["spectra"][0, :, 0] > 0) + )[0] + database_results["spectra"] = database_results["spectra"][:, has_data, :] + database_results["spectra_error"] = database_results["spectra"] * 0.0 + # database_results["spectra_error"] = database_results["spectra_error"][ + # :, has_data, : + # ] + + _channel = np.array(has_data) # np.arange(database_results["length"]) channel = DataArray( _channel, coords=[("channel", _channel)], @@ -399,14 +414,9 @@ def get_spectrometer( coords=[("pixel", pixel)], attrs={"long_name": "Wavelength", "units": "nm"}, ) - coords = [ - ("t", t), - ("channel", channel), - ("wavelength", wavelength), - ] - location = database_results["location"] - direction = database_results["direction"] + location = database_results["location"][has_data, :] + direction = database_results["direction"][has_data, :] transform = LineOfSightTransform( location[:, 0], location[:, 1], @@ -419,7 +429,11 @@ def get_spectrometer( dl=dl, passes=passes, ) - + coords = [ + ("t", t), + ("channel", channel), + ("wavelength", wavelength), + ] data = {} for quantity in quantities: quant_data = self.assign_dataarray( diff --git a/indica/readers/read_st40.py b/indica/readers/read_st40.py index 48630d6b..fcbb8fd3 100644 --- a/indica/readers/read_st40.py +++ b/indica/readers/read_st40.py @@ -32,19 +32,19 @@ } FILTER_LIMITS = { - "cxff_pi": (0, np.inf), - "cxff_tws_c": (0, np.inf), - "cxqf_tws_c": (0, np.inf), - "xrcs": (0, np.inf), - "brems": (0, np.inf), - "halpha": (0, np.inf), - "sxr_diode_1": (0, np.inf), - "sxr_camera_4": (0, np.inf), - "sxrc_xy1": (0, np.inf), - "sxrc_xy2": (0, np.inf), - "ts": (0, 10.0e3), - "pi": (0, np.inf), - "tws_c": (0, np.inf), + "cxff_pi": {"ti": (0, np.inf), "vtor": (0, np.inf)}, + "cxff_tws_c": {"ti": (0, np.inf), "vtor": (0, np.inf)}, + "cxqf_tws_c": {"ti": (0, np.inf), "vtor": (0, np.inf)}, + "xrcs": {"ti_w": (0, np.inf), "te_kw": (0, np.inf), "te_n3w": (0, np.inf)}, + "brems": {"brightness": (0, np.inf)}, + "halpha": {"brightness": (0, np.inf)}, + "sxr_diode_1": {"brightness": (0, np.inf)}, + "sxr_camera_4": {"brightness": (0, np.inf)}, + "sxrc_xy1": {"brightness": (0, np.inf)}, + "sxrc_xy2": {"brightness": (0, np.inf)}, + "ts": {"te": (0, 10.0e3), "ne": (0, 1.0e21)}, + "pi": {"spectra": (0, np.inf)}, + "tws_c": {"spectra": (0, np.inf)}, } LINESTYLES = { @@ -143,6 +143,7 @@ def bin_data_in_time( if "t" in data_quant.coords: data_quant = convert_in_time_dt(tstart, tend, dt, data_quant) + binned_quantities[quant] = data_quant self.binned_data[instr] = binned_quantities @@ -186,10 +187,10 @@ def filter_data(self, instruments: list): filter_general( self.binned_data[instr], quantities, - lim=FILTER_LIMITS[instr], + limits=FILTER_LIMITS[instr], ) - def filter_ts(self, chi2_limit: float = 2.0): + def filter_ts(self, chi2_limit: float = 3.0): if "ts" not in self.binned_data.keys(): print("No TS data to filter") return @@ -301,7 +302,7 @@ def __call__( tend: float = None, dt: float = None, R_shift: float = 0.0, - chi2_limit: float = 2.0, + chi2_limit: float = 3.0, map_diagnostics: bool = False, raw_only: bool = False, debug: bool = False, @@ -345,13 +346,15 @@ def __call__( self.map_diagnostics(instruments, map_raw=map_raw) -def filter_general(data: DataArray, quantities: list, lim: tuple = (-np.inf, np.inf)): +def filter_general(data: DataArray, quantities: list, limits: dict): for quantity in quantities: - attrs = data[quantity].attrs - condition = (data[quantity] >= lim[0]) * (data[quantity] < lim[1]) - filtered = xr.where(condition, data[quantity], np.nan) - filtered.attrs = attrs - data[quantity] = filtered + if quantity in limits.keys(): + lim = limits[quantity] + attrs = data[quantity].attrs + condition = (data[quantity] >= lim[0]) * (data[quantity] < lim[1]) + filtered = xr.where(condition, data[quantity], np.nan) + filtered.attrs = attrs + data[quantity] = filtered def astra_equilibrium(pulse: int, revision: RevisionLike): diff --git a/indica/readers/st40reader.py b/indica/readers/st40reader.py index be15419e..af54c9ee 100644 --- a/indica/readers/st40reader.py +++ b/indica/readers/st40reader.py @@ -208,7 +208,7 @@ class ST40Reader(DataReader): "pi": { "spectra": ":emission", }, - "tws": { + "tws_c": { "spectra": ":emission", }, "ts": { @@ -312,7 +312,7 @@ def __init__( pulse: int, tstart: float, tend: float, - server: str = "10.0.40.13", + server: str = "192.168.1.7", # 192.168.1.7 10.0.40.13 tree: str = "ST40", default_error: float = 0.05, max_freq: float = 1e6, diff --git a/indica/workflows/bayes_workflow.py b/indica/workflows/bayes_workflow.py index 689a74f1..91e50965 100644 --- a/indica/workflows/bayes_workflow.py +++ b/indica/workflows/bayes_workflow.py @@ -1,9 +1,12 @@ from pathlib import Path +import time import corner import matplotlib.pyplot as plt import numpy as np +timestr = time.strftime("%Y%m%d%H%M") + def plot_profile( profile, @@ -59,9 +62,9 @@ def plot_profile( return if filename: - plt.savefig(figheader + f"{filename}.png") + plt.savefig(figheader + timestr + f"{filename}.png") else: - plt.savefig(figheader + f"{blobkey}.png") + plt.savefig(figheader + timestr + f"{blobkey}.png") plt.close("all") @@ -169,9 +172,19 @@ def plot_bayes_result( plt.legend() plt.xlabel("iterations") plt.ylabel("auto-correlation time (iterations)") - plt.savefig(figheader + "average_tau.png") + plt.savefig(figheader + timestr + "average_tau.png") plt.close() + key = "pi.brightness" + if key in blobs.keys(): + _plot_1d( + blobs, + key, + diag_data, + f"{timestr}_{key.replace('.', '_')}.png", + figheader=figheader, + ylabel="Intensity (W m^-2)", + ) key = "efit.wp" if key in blobs.keys(): _plot_0d( @@ -188,7 +201,7 @@ def plot_bayes_result( blobs, key, diag_data, - f"{key.replace('.', '_')}.png", + f"{timestr}_{key.replace('.', '_')}.png", figheader=figheader, ylabel="ne_int (m^-2)", ) @@ -233,6 +246,13 @@ def plot_bayes_result( ylabel="temperature (eV)", ) + key = "zeff" + if key in blobs and key in phantom_profiles: + plot_profile( + blobs[key], key, figheader=figheader, phantom_profile=phantom_profiles[key] + ) + plt.ylim(0, 10) + key = "electron_temperature" plot_profile( blobs[key], @@ -244,8 +264,9 @@ def plot_bayes_result( linestyle="dashdot", ) key = "ion_temperature" + element = blobs[key].element[0] plot_profile( - blobs[key].sel(element="ar"), + blobs[key].sel(element=element), key, figheader=figheader, filename="temperature", @@ -258,8 +279,9 @@ def plot_bayes_result( blobs[key], key, figheader=figheader, phantom_profile=phantom_profiles[key] ) key = "impurity_density" + impurity = blobs[key].element[0] plot_profile( - blobs[key].sel(element="ar"), + blobs[key].sel(element=impurity), key, figheader=figheader, phantom_profile=phantom_profiles[key], @@ -267,10 +289,10 @@ def plot_bayes_result( ) corner.corner(samples, labels=param_names) - plt.savefig(figheader + "posterior.png") + plt.savefig(figheader + timestr + "posterior.png") corner.corner(prior_samples, labels=param_names) - plt.savefig(figheader + "prior.png") + plt.savefig(figheader + timestr + "prior.png") plt.close("all") diff --git a/indica/workflows/example_bayes_opt.py b/indica/workflows/example_bayes_opt.py index 4ac1bcee..5e5799f4 100644 --- a/indica/workflows/example_bayes_opt.py +++ b/indica/workflows/example_bayes_opt.py @@ -57,7 +57,9 @@ def run( smmh1.set_los_transform(los_transform) smmh1.plasma = plasma los_transform = ST40.binned_data["xrcs"]["te_kw"].transform - xrcs = Helike_spectroscopy(name="xrcs", window_masks=[slice(0.3945, 0.3962)]) + xrcs = Helike_spectroscopy( + name="xrcs", window_masks=[slice(0.3945, 0.3962)], element="ar" + ) xrcs.set_los_transform(los_transform) xrcs.plasma = plasma diff --git a/indica/workflows/run_tomo_1d.py b/indica/workflows/run_tomo_1d.py index ca095079..882b376c 100644 --- a/indica/workflows/run_tomo_1d.py +++ b/indica/workflows/run_tomo_1d.py @@ -1,12 +1,20 @@ """Inverts line of sight integrals to estimate local emissivity.""" import getpass +from typing import Callable +from typing import Dict from typing import Tuple import matplotlib.pylab as plt import numpy as np from xarray import DataArray +from indica.equilibrium import Equilibrium +from indica.models.diode_filters import BremsstrahlungDiode +from indica.models.diode_filters import example_run as brems_example +from indica.models.plasma import example_run as example_plasma +from indica.models.sxr_camera import example_run as sxr_example +from indica.models.sxr_camera import SXRcamera from indica.operators import tomo_1D from indica.readers.read_st40 import ReadST40 from indica.utilities import save_figure @@ -17,9 +25,170 @@ set_plot_rcparams("profiles") +PHANTOMS: Dict[str, Callable] = { + "sxrc_xy2": sxr_example, + "sxr_camera_4": sxr_example, + "pi": brems_example, +} + +PULSES: Dict[str, int] = { + "sxrc_xy2": 10821, + "sxr_camera_4": 9229, + "pi": 10821, +} + +SXR_MODEL = SXRcamera("sxrc_xy2") +BREMS_MODEL = BremsstrahlungDiode("pi") + + +def phantom_examples( + instrument: str = "sxrc_xy2", + reg_level_guess: float = 0.5, + plot: bool = True, +): + plasma, model, bckc = PHANTOMS[instrument]() + los_transform = model.los_transform + emissivity = model.emissivity + brightness = bckc["brightness"] + z = los_transform.z + R = los_transform.R + dl = los_transform.dl + + has_data = np.logical_not(np.isnan(brightness.isel(t=0).data)) + rho_equil = plasma.equilibrium.rho.interp(t=brightness.t) + input_dict = dict( + brightness=brightness.data, + dl=dl, + t=brightness.t.data, + R=R, + z=z, + rho_equil=dict( + R=rho_equil.R.data, + z=rho_equil.z.data, + t=rho_equil.t.data, + rho=rho_equil.data, + ), + has_data=has_data, + debug=False, + ) + if emissivity is not None: + input_dict["emissivity"] = emissivity + + tomo = tomo_1D.SXR_tomography(input_dict, reg_level_guess=reg_level_guess) + tomo() + if plot: + model.los_transform.plot() + tomo.show_reconstruction() + + inverted_emissivity = DataArray( + tomo.emiss, coords=[("t", tomo.tvec), ("rho_poloidal", tomo.rho_grid_centers)] + ) + inverted_error = DataArray( + tomo.emiss_err, + coords=[("t", tomo.tvec), ("rho_poloidal", tomo.rho_grid_centers)], + ) + inverted_emissivity.attrs["error"] = inverted_error + + data_tomo = brightness + bckc_tomo = DataArray(tomo.backprojection, coords=data_tomo.coords) + + return inverted_emissivity, data_tomo, bckc_tomo + + +def experimental_examples( + instrument: str = "sxrc_xy2", + reg_level_guess: float = 0.5, + phantom_data: bool = True, + plot: bool = True, +): + pulse = PULSES[instrument] + + tstart = 0.02 + tend = 0.1 + dt = 0.01 + st40 = ReadST40(pulse, tstart, tend, dt=dt) + st40(instruments=[instrument, "efit"]) + equilibrium = Equilibrium(st40.raw_data["efit"]) + quantity = list(st40.binned_data[instrument])[0] + los_transform = st40.binned_data[instrument][quantity].transform + los_transform.set_equilibrium(equilibrium, force=True) + if instrument == "pi": + BREMS_MODEL.set_los_transform(los_transform) + attrs = st40.binned_data[instrument]["spectra"].attrs + background, brightness = BREMS_MODEL.integrate_spectra( + st40.binned_data[instrument]["spectra"] + ) + background.attrs = attrs + brightness.attrs = attrs + st40.binned_data[instrument]["background"] = background + st40.binned_data[instrument]["brightness"] = brightness + model = SXR_MODEL + + if phantom_data: + plasma = example_plasma( + pulse, + tstart=tstart, + tend=tend, + dt=dt, + ) + plasma.build_atomic_data() + plasma.set_equilibrium(equilibrium) + model.set_plasma(plasma) + bckc = model() + emissivity = model.emissivity + brightness = bckc["brightness"] + else: + emissivity = None + brightness = st40.binned_data[instrument]["brightness"] + + z = los_transform.z + R = los_transform.R + dl = los_transform.dl + + data_t0 = brightness.isel(t=0).data + has_data = np.logical_not(np.isnan(brightness.isel(t=0).data)) & (data_t0 > 0) + rho_equil = equilibrium.rho.interp(t=brightness.t) + input_dict = dict( + brightness=brightness.data, + dl=dl, + t=brightness.t.data, + R=R, + z=z, + rho_equil=dict( + R=rho_equil.R.data, + z=rho_equil.z.data, + t=rho_equil.t.data, + rho=rho_equil.data, + ), + has_data=has_data, + debug=False, + ) + if emissivity is not None: + input_dict["emissivity"] = emissivity + + tomo = tomo_1D.SXR_tomography(input_dict, reg_level_guess=reg_level_guess) + tomo() + + if plot: + model.los_transform.plot() + tomo.show_reconstruction() + + inverted_emissivity = DataArray( + tomo.emiss, coords=[("t", tomo.tvec), ("rho_poloidal", tomo.rho_grid_centers)] + ) + inverted_error = DataArray( + tomo.emiss_err, + coords=[("t", tomo.tvec), ("rho_poloidal", tomo.rho_grid_centers)], + ) + inverted_emissivity.attrs["error"] = inverted_error + data_tomo = brightness + bckc_tomo = DataArray(tomo.backprojection, coords=data_tomo.coords) + + return inverted_emissivity, data_tomo, bckc_tomo + def sxrc_xy( - pulse: int = 10820, + pulse: int = 10821, tstart: float = 0.02, tend: float = 0.11, dt: float = 0.01, @@ -191,11 +360,11 @@ def fake_data( nchannels=12, input_dict: dict = None, ): - from indica.models.sxr_camera import example_run + from indica.models.sxr_camera import example_run as sxr_example if input_dict is None: if plasma is None or model is None or bckc is None: - plasma, model, bckc = example_run(pulse=pulse, nchannels=nchannels) + plasma, model, bckc = sxr_example(pulse=pulse, nchannels=nchannels) tstart = plasma.t.min().values tend = plasma.t.max().values diff --git a/indica/workflows/zeff_profile.py b/indica/workflows/zeff_profile.py new file mode 100644 index 00000000..4ed61bad --- /dev/null +++ b/indica/workflows/zeff_profile.py @@ -0,0 +1,619 @@ +from copy import deepcopy + +import emcee +import matplotlib.pylab as plt +import numpy as np +import pandas as pd +from scipy.interpolate import CubicSpline +import xarray as xr +from xarray import DataArray + +from indica.bayesmodels import BayesModels +from indica.bayesmodels import get_uniform +from indica.equilibrium import Equilibrium +from indica.models.diode_filters import example_run as example_diode +from indica.models.plasma import example_run as example_plasma +from indica.models.plasma import Plasma +from indica.operators import tomo_1D +from indica.operators.gpr_fit import run_gpr_fit +import indica.physics as ph +from indica.readers.read_st40 import ReadST40 +from indica.utilities import set_plot_colors +from indica.workflows.bayes_workflow import plot_bayes_result +from indica.workflows.bayes_workflow import sample_with_autocorr + +PATHNAME = "./plots/" + +PULSE = 11085 +TIME = 0.07 +MAIN_ION = "h" +IMPURITIES = ("c",) +IMPURITY_CONCENTRATION = (0.03,) +FULL_RUN = False +N_RAD = 10 + +CM, COLS = set_plot_colors() + +PRIORS = { + "Ne_prof.y0": get_uniform(1e19, 8e19), + "Ne_prof.y1": get_uniform(1e18, 5e18), + "Ne_prof.y0/Ne_prof.y1": lambda x1, x2: np.where(((x1 > x2 * 2)), 1, 0), + "Ne_prof.wped": get_uniform(1, 5), + "Ne_prof.wcenter": get_uniform(0.1, 0.8), + "Ne_prof.peaking": get_uniform(1, 5), + "Nimp_prof.y0": get_uniform(1e16, 1e19), + "Nimp_prof.y1": get_uniform(1e15, 1e19), + "Nimp_prof.yend": get_uniform(1e15, 1e19), + "Nimp_prof.peaking": get_uniform(1, 6), + "Nimp_prof.wcenter": get_uniform(0.1, 0.8), + "Nimp_prof.wped": get_uniform(1, 5), + "Nimp_prof.y0/Nimp_prof.y1": lambda x1, x2: np.where((x1 >= x2), 1, 0), + "Nimp_prof.y1/Nimp_prof.yend": lambda x1, x2: np.where((x1 == x2), 1, 0), + "Te_prof.y0": get_uniform(1000, 6000), + "Te_prof.peaking": get_uniform(1, 4), + "Ti_prof.y0": get_uniform(2000, 10000), + "Ti_prof.peaking": get_uniform(1, 4), +} + +PHANTOM_PROFILE_PARAMS = { + "Ne_prof.y0": 5e19, + "Ne_prof.wcenter": 0.4, + "Ne_prof.peaking": 2, + "Ne_prof.y1": 2e18, + "Ne_prof.yend": 1e18, + "Ne_prof.wped": 2, + "Nimp_prof.y0": 1e18, + "Nimp_prof.y1": 1e17, + "Nimp_prof.yend": 1e16, + "Nimp_prof.peaking": 2, + "Nimp_prof.wcenter": 0.4, + "Nimp_prof.wped": 2, + # "Nimp_prof.y0": 2e18, + # "Nimp_prof.y1": 5e17, + # "Nimp_prof.yend": 5e17, + # "Nimp_prof.peaking": 1, + "Te_prof.y0": 3000, + "Te_prof.peaking": 2, + "Ti_prof.y0": 5000, + "Ti_prof.peaking": 2, +} +PARAM_NAMES = [ + "Nimp_prof.y0", + # "Nimp_prof.y1", + "Nimp_prof.peaking", + "Nimp_prof.wcenter", + # "Nimp_prof.wped", +] + + +# TODO: allow conditional prior usage even when only +# one param is being optimisied i.e. 1 is constant + + +def prepare_data_ts( + plasma: Plasma, + models: dict, + st40: ReadST40, + xdim: str = "R", + map_to_rho: bool = True, + err_bounds: tuple = (0, 0), + flat_data: dict = {}, +): + if "ts" not in st40.binned_data.keys(): + raise ValueError + + quantities = ["te", "ne"] + for quantity in quantities: + if "ts" not in st40.binned_data.keys(): + continue + + _data = st40.binned_data["ts"][quantity] + if hasattr(_data.transform, "equilibrium"): + _data.transform.convert_to_rho_theta(t=_data.t) + + flat_data[f"ts.{quantity}"] = _data + + # TODO: normalizing data due to issues with fit convergence + const = 1.0 + if quantity == "ne": + const = 1.0e-16 + xr.set_options(keep_attrs=True) + data = deepcopy(st40.binned_data["ts"][quantity]) * const + data.attrs["error"] = deepcopy(st40.binned_data["ts"][quantity].error) * const + + y_bounds = (1, 1) + if xdim == "R": + x_bounds = plasma.machine_dimensions[0] + else: + x_bounds = (0, 1) + + if xdim not in data.dims and hasattr(data, xdim): + data = data.swap_dims({"channel": xdim}) + + fit, fit_err = run_gpr_fit( + data, + x_bounds=x_bounds, + y_bounds=y_bounds, + err_bounds=err_bounds, + xdim=xdim, + ) + fit /= const + fit_err /= const + + if not map_to_rho: + st40.binned_data["ts"][f"{quantity}_fit"] = fit + # _data["ts"][f"{quantity}_fit"].attrs["error"] = fit_err + flat_data[f"ts.{quantity}_fit"] = fit + else: + fit_rho, _, _ = plasma.equilibrium.flux_coords(fit.R, fit.R * 0, fit.t) + exp_rho = fit_rho.interp(R=data.R) + rmag = plasma.equilibrium.rmag.interp(t=fit.t) + + fit_lfs = [] + for t in fit.t: + Rmag = rmag.sel(t=t) + _x = exp_rho.sel(t=t).where(data.R >= Rmag, drop=True) + _y = fit.sel(t=t).interp(R=_x.R) + ind = np.argsort(_x.values) + x = _x.values[ind] + y = _y.values[ind] + cubicspline = CubicSpline( + np.append(0, x[1:]), np.append(y[0], y[1:]), bc_type="clamped" + ) + _fit_lfs = cubicspline(plasma.rho) + fit_lfs.append( + DataArray(_fit_lfs, coords=[("rho_poloidal", plasma.rho)]) + ) + fit_lfs = xr.concat(fit_lfs, "t").assign_coords(t=fit.t) + st40.binned_data["ts"][f"{quantity}_fit"] = fit_lfs + # _data["ts"][f"{quantity}_fit"].attrs["error"] = fit_lfs_err + flat_data[f"ts.{quantity}_fit"] = fit_lfs + + if "ts" in models.keys(): + models["ts"].set_los_transform(data["te"].transform) + models["ts"].set_plasma(plasma) + + return flat_data + + +def prepare_data_cxrs( + plasma: Plasma, + models: dict, + st40: ReadST40, + flat_data: dict = {}, +): + """ + Read CXRS data from experiment, assign transform to model and return flat_data + dictionary with either experimental or phantom data built using the models. + """ + instruments = ["pi", "tws_c"] + for instrument in instruments: + if instrument not in st40.binned_data.keys(): + continue + data = st40.binned_data[instrument] + attrs = data["spectra"].attrs + (data["background"], data["brightness"],) = models[ + instrument + ].integrate_spectra(data["spectra"]) + data["background"].attrs = attrs + data["brightness"].attrs = attrs + + for quantity in data.keys(): + flat_data[f"{instrument}.{quantity}"] = data[quantity] + + if instrument in models.keys(): + models[instrument].set_los_transform(data["spectra"].transform) + models[instrument].set_plasma(plasma) + + return flat_data + + +def prepare_inputs( + pulse: int, + tstart=0.01, + tend=0.1, + dt=0.01, + time: float = None, + phantom_profile_params: dict = None, + phantom_data: bool = True, +): + + flat_data: dict = {} + models: dict = {} + + print("Generating plasma") + plasma = example_plasma( + tstart=tstart, + tend=tend, + dt=dt, + main_ion=MAIN_ION, + impurities=IMPURITIES, + impurity_concentration=IMPURITY_CONCENTRATION, + full_run=FULL_RUN, + n_rad=N_RAD, + ) + if not phantom_data: + plasma.initialize_variables() + + if time is None: + time = plasma.t + + time = plasma.t.sel(t=time, method="nearest") + plasma.time_to_calculate = time + + if phantom_profile_params is not None: + plasma.update_profiles(phantom_profile_params) + + print("Generating diagnostic models") + _, pi_model, bckc = example_diode(plasma=plasma) + models["pi"] = pi_model + models["pi"].name = "pi" + models["tws_c"] = deepcopy(pi_model) + models["tws_c"].name = "pi" + + if pulse is not None: + print("Reading experimental data") + st40 = ReadST40(pulse, tstart, tend, dt) + st40(["pi", "tws_c", "ts", "efit"]) + plasma.set_equilibrium(Equilibrium(st40.raw_data["efit"])) + + if not phantom_data and "ts" in st40.binned_data.keys(): + print("Fitting TS data") + prepare_data_ts( + plasma, + models, + st40, + flat_data=flat_data, + ) + plasma.electron_density.loc[dict(t=time)] = ( + flat_data["ts.ne_fit"].sel(t=time).interp(rho_poloidal=plasma.rho) + ) + plasma.electron_temperature.loc[dict(t=time)] = ( + flat_data["ts.te_fit"].sel(t=time).interp(rho_poloidal=plasma.rho) + ) + + print("Fitting Bremsstrahlung PI/TWS_C spectra") + prepare_data_cxrs( + plasma, + models, + st40, + flat_data=flat_data, + ) + + if phantom_data: + plasma.impurity_density *= plasma.t * 100.0 + print("Generating phantom data using Plasma and Models") + for instrument in ["pi", "tws_c"]: + bckc = models[f"{instrument}"]() + flat_data[f"{instrument}.brightness"] = bckc["brightness"] + flat_data[f"{instrument}.emissivity"] = models[f"{instrument}"].emissivity + + transform = models[f"{instrument}"].los_transform + flat_data[f"{instrument}.brightness"].attrs["transform"] = transform + flat_data[f"{instrument}.emissivity"].attrs["transform"] = transform + + if phantom_data: + zeff = plasma.zeff.sum("element").sel(t=time) + impurity_density = plasma.impurity_density.sel(t=time, element=IMPURITIES[0]) + else: + zeff, impurity_density = None, None + + input_profiles = { + "electron_density": plasma.electron_density.sel(t=time), + "electron_temperature": plasma.electron_temperature.sel(t=time), + "ion_temperature": plasma.ion_temperature.sel(t=time, element=IMPURITIES[0]), + "impurity_density": impurity_density, + "zeff": zeff, + } + + for key in flat_data.keys(): + if "t" not in flat_data[key].dims: + flat_data[key] = flat_data[key].expand_dims( + dim={"t": [plasma.time_to_calculate]} + ) + else: + if np.size(time) == 1: + flat_data[key] = flat_data[key].sel(t=[time]) + + if "brightness" in key: + print(f"Reorganising {key} channel range starting at 0") + t = flat_data[key].t + channel = np.arange(flat_data[key].channel.size) + flat_data[key] = DataArray( + flat_data[key].values, + coords=[("t", t), ("channel", channel)], + attrs=flat_data[key].attrs, + ) + + return plasma, models, flat_data, input_profiles + + +def run_bayes( + pulse: int, + time: float, + phantom_profile_params, + iterations, + result_path, + tstart=0.03, + tend=0.1, + dt=0.01, + burn_in=0, + nwalkers=10, + phantom_data: bool = True, +): + + plasma, models, flat_data, input_profiles = prepare_inputs( + pulse, + tstart=tstart, + tend=tend, + dt=dt, + time=time, + phantom_profile_params=phantom_profile_params, + phantom_data=phantom_data, + ) + + print("Instatiating Bayes model") + diagnostic_models = [models["pi"]] + quant_to_optimise = [ + "pi.brightness", + ] + bm = BayesModels( + plasma=plasma, + data=flat_data, + diagnostic_models=diagnostic_models, + quant_to_optimise=quant_to_optimise, + priors=PRIORS, + ) + ndim = PARAM_NAMES.__len__() + start_points = bm.sample_from_priors(PARAM_NAMES, size=nwalkers) + move = [(emcee.moves.StretchMove(), 1.0), (emcee.moves.DEMove(), 0.0)] + sampler = emcee.EnsembleSampler( + nwalkers, + ndim, + log_prob_fn=bm.ln_posterior, + parameter_names=PARAM_NAMES, + moves=move, + ) + + print("Sampling") + autocorr = sample_with_autocorr( + sampler, start_points, iterations=iterations, auto_sample=5 + ) + + blobs = sampler.get_blobs(discard=burn_in, flat=True) + blob_names = sampler.get_blobs().flatten()[0].keys() + blob_dict = { + blob_name: xr.concat( + [data[blob_name] for data in blobs], + dim=pd.Index(np.arange(0, blobs.__len__()), name="index"), + ) + for blob_name in blob_names + } + + samples = sampler.get_chain(flat=True) + prior_samples = bm.sample_from_priors(PARAM_NAMES, size=int(1e5)) + result = { + "blobs": blob_dict, + "diag_data": flat_data, + "samples": samples, + "prior_samples": prior_samples, + "param_names": PARAM_NAMES, + "phantom_profiles": input_profiles, + "plasma": plasma, + "autocorr": autocorr, + } + print(sampler.acceptance_fraction.sum()) + plot_bayes_result(**result, figheader=result_path) + + if not phantom_data and pulse is not None: + plt.figure() + Te = flat_data["ts.te"].sel(t=time) + rho = Te.transform.rho.sel(t=time) + plt.plot(rho, Te, "o") + plasma.electron_temperature.sel(t=time).plot() + + plt.figure() + Ne = flat_data["ts.ne"].sel(t=time) + rho = Ne.transform.rho.sel(t=time) + plt.plot(rho, Ne, "o") + plasma.electron_density.sel(t=time).plot() + + return result + + +def run_inversion( + pulse, + tstart=0.03, + tend=0.1, + dt=0.01, + reg_level_guess: float = 0.3, + phantom_data: bool = True, +): + + plasma, models, flat_data, input_profiles = prepare_inputs( + pulse, + tstart=tstart, + tend=tend, + dt=dt, + phantom_data=phantom_data, + ) + + data = flat_data["pi.brightness"] + has_data = np.isfinite(data) * (data > 0) + data_to_invert = data.where(has_data, drop=True) + channels = data_to_invert.channel + has_data = [True] * len(channels) + + los_transform = data.transform + z = los_transform.z.sel(channel=channels) + R = los_transform.R.sel(channel=channels) + dl = los_transform.dl + + rho_equil = los_transform.equilibrium.rho.interp(t=data.t) + input_dict = dict( + brightness=data_to_invert.data, + brightness_error=data_to_invert.data * 0.1, + dl=dl, + t=data_to_invert.t.data, + R=R, + z=z, + rho_equil=dict( + R=rho_equil.R.data, + z=rho_equil.z.data, + t=rho_equil.t.data, + rho=rho_equil.data, + ), + has_data=has_data, + debug=False, + ) + + if "pi.emissivity" in flat_data.keys() is not None: + input_dict["emissivity"] = flat_data["pi.emissivity"] + + tomo = tomo_1D.SXR_tomography(input_dict, reg_level_guess=reg_level_guess) + tomo() + + models["pi"].los_transform.plot() + tomo.show_reconstruction() + + inverted_emissivity = DataArray( + tomo.emiss, coords=[("t", tomo.tvec), ("rho_poloidal", tomo.rho_grid_centers)] + ) + inverted_error = DataArray( + tomo.emiss_err, + coords=[("t", tomo.tvec), ("rho_poloidal", tomo.rho_grid_centers)], + ) + inverted_emissivity.attrs["error"] = inverted_error + # data_tomo = data + # bckc_tomo = DataArray(tomo.backprojection, coords=data_tomo.coords) + + zeff = ph.zeff_bremsstrahlung( + plasma.electron_temperature, + plasma.electron_density, + models["pi"].filter_wavelength, + bremsstrahlung=inverted_emissivity, + gaunt_approx="callahan", + ) + zeff_up = ph.zeff_bremsstrahlung( + plasma.electron_temperature, + plasma.electron_density, + models["pi"].filter_wavelength, + bremsstrahlung=inverted_emissivity - inverted_error, + gaunt_approx="callahan", + ) + zeff_down = ph.zeff_bremsstrahlung( + plasma.electron_temperature, + plasma.electron_density, + models["pi"].filter_wavelength, + bremsstrahlung=inverted_emissivity + inverted_error, + gaunt_approx="callahan", + ) + zeff = zeff.where(zeff < 10, np.nan) + + cols = CM(np.linspace(0.1, 0.75, len(plasma.t), dtype=float)) + plt.figure() + for i, t in enumerate(zeff.t): + if i % 2: + zeff.sel(t=t).plot(color=cols[i], label=f"{t.values:.3f}") + plt.fill_between( + zeff.rho_poloidal, + zeff_up.sel(t=t), + zeff_down.sel(t=t), + color=cols[i], + alpha=0.6, + ) + if phantom_data: + plasma.zeff.sum("element").sel(t=t).plot( + marker="o", + color=cols[i], + alpha=0.5, + linestyle="", + label=f"{t.values:.3f}", + ) + if phantom_data: + plasma.zeff.sum("element").sel(t=t).plot( + marker="o", color=cols[i], alpha=0.5, linestyle="", label="Phantom" + ) + zeff.sel(t=t).plot(label="Recalculated", color=cols[i]) + plt.ylim(0, 10) + plt.ylabel("Zeff") + plt.legend() + plt.title("Zeff from Bremsstrahlung inversion & TS data") + plt.legend() + + if not phantom_data: + plot_ts(plasma, flat_data, cols=cols) + + return zeff + + +def plot_ts(plasma: Plasma, flat_data: dict, cols=None): + if cols is None: + cols = CM(np.linspace(0.1, 0.75, len(plasma.t), dtype=float)) + + plt.figure() + Te = flat_data["ts.te"] + Te_err = flat_data["ts.te"].error + Ne = flat_data["ts.ne"] + Ne_err = flat_data["ts.ne"].error + rho = Te.transform.rho + rmag = plasma.equilibrium.rmag + for i, t in enumerate(Te.t): + if i % 2: + plasma.electron_temperature.sel(t=t).plot( + color=cols[i], label=f"{t.values:.3f}" + ) + channels = np.where(Te.R > rmag.sel(t=t, method="nearest"))[0] + x = rho.sel(t=t, channel=channels) + y = Te.sel(t=t, channel=channels) + err = Te_err.sel(t=t, channel=channels) + plt.errorbar(x, y, err, color=cols[i], marker="o", linestyle="") + plasma.electron_temperature.sel(t=t).plot(color=cols[i], label="Fit") + plt.errorbar(x, y, err, color=cols[i], marker="o", linestyle="", label="Data") + plt.ylim( + 0, np.max([plasma.electron_temperature.max(), flat_data["ts.te"].max()]) * 1.1 + ) + plt.legend() + plt.title("TS electron temperature") + + plt.figure() + for i, t in enumerate(Ne.t): + if i % 2: + plasma.electron_density.sel(t=t).plot( + color=cols[i], label=f"{t.values:.3f}" + ) + channels = np.where(Te.R > rmag.sel(t=t, method="nearest"))[0] + x = rho.sel(t=t, channel=channels) + y = Ne.sel(t=t, channel=channels) + err = Ne_err.sel(t=t, channel=channels) + plt.errorbar(x, y, err, color=cols[i], marker="o", linestyle="") + plasma.electron_density.sel(t=t).plot(color=cols[i], label="Fit") + plt.errorbar(x, y, err, color=cols[i], marker="o", linestyle="", label="Data") + plt.legend() + plt.title("TS electron density") + + +def inversion_example(pulse: int = 11085, phantom_data: bool = True): + ff = run_inversion(pulse, phantom_data=phantom_data) + return ff + + +def bayesian_example( + pulse: int = 11085, + time: float = 0.03, + iterations=200, + nwalkers=50, + phantom_data: bool = True, +): + ff = run_bayes( + pulse, + time, + PHANTOM_PROFILE_PARAMS, + iterations, + PATHNAME, + burn_in=0, + nwalkers=nwalkers, + phantom_data=phantom_data, + ) + + return ff diff --git a/poetry.lock b/poetry.lock index f9fcca30..5d84cb92 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1096,7 +1096,7 @@ python-versions = "*" [[package]] name = "typing-extensions" -version = "4.6.3" +version = "4.7.0" description = "Backported and Experimental Type Hints for Python 3.7+" category = "main" optional = false @@ -2211,8 +2211,8 @@ types-setuptools = [ {file = "types_setuptools-57.4.18-py3-none-any.whl", hash = "sha256:9660b8774b12cd61b448e2fd87a667c02e7ec13ce9f15171f1d49a4654c4df6a"}, ] typing-extensions = [ - {file = "typing_extensions-4.6.3-py3-none-any.whl", hash = "sha256:88a4153d8505aabbb4e13aacb7c486c2b4a33ca3b3f807914a9b4c844c471c26"}, - {file = "typing_extensions-4.6.3.tar.gz", hash = "sha256:d91d5919357fe7f681a9f2b5b4cb2a5f1ef0a1e9f59c4d8ff0d3491e05c0ffd5"}, + {file = "typing_extensions-4.7.0-py3-none-any.whl", hash = "sha256:5d8c9dac95c27d20df12fb1d97b9793ab8b2af8a3a525e68c80e21060c161771"}, + {file = "typing_extensions-4.7.0.tar.gz", hash = "sha256:935ccf31549830cda708b42289d44b6f74084d616a00be651601a4f968e77c82"}, ] urllib3 = [ {file = "urllib3-2.0.3-py3-none-any.whl", hash = "sha256:48e7fafa40319d358848e1bc6809b208340fafe2096f1725d05d67443d0483d1"}, diff --git a/tests/unit/models/test_diagnostic_models.py b/tests/unit/models/test_diagnostic_models.py index 2057e1fd..e1215725 100644 --- a/tests/unit/models/test_diagnostic_models.py +++ b/tests/unit/models/test_diagnostic_models.py @@ -101,8 +101,8 @@ def test_diodes_timepoint_fail(self): def test_diodes_timepoint_pass(self): self._test_timepoint_pass("diode_filters") - def test_diodes_interpolation(self): - self._test_time_interpolation("diode_filters") + # def test_diodes_interpolation(self): + # self._test_time_interpolation("diode_filters") def test_helike_timepoint_fail(self): self._test_timepoint_fail("helike_spectroscopy")