Exemple #1
0
def make_plasma_from_variables_single_time(filename,
                                           shot,
                                           time,
                                           rhop_profiles,
                                           Te,
                                           ne,
                                           R,
                                           z,
                                           Br,
                                           Bt,
                                           Bz,
                                           rhop,
                                           vessel_data=None,
                                           vessel_bd_file=None):
    # Vessel data has to be a ndarray of points (shape = (n,2)) with R,z points of the machine wall
    # Alternatively a standard ECRad vessel file can be used like ASDEX_Upgrade_vessel.txt
    # rhop_profiles can be none for 2D profiles
    plasma_dict = {}
    plasma_dict["shot"] = shot
    plasma_dict["time"] = np.array([time])
    plasma_dict["prof_reference"] = "rhop_prof"
    plasma_dict["eq_dim"] = 2
    plasma_dict["2D_prof"] = len(Te.shape) > 2
    if (not plasma_dict["2D_prof"]):
        plasma_dict["prof_reference"] = "rhop_prof"
        plasma_dict["rhop_prof"] = np.array([rhop_profiles])
    else:
        plasma_dict["prof_reference"] = "2D"
        plasma_dict["rhop_prof"] = None
    plasma_dict["ne"] = np.array([ne])
    plasma_dict["Te"] = np.array([Te])
    EQObj = EQDataExt(shot, Ext_data=True)
    EQObj.insert_slices_from_ext(np.array([time]), \
                                 [EQDataSlice(time, R, z, rhop**2, Br, Bt, Bz, \
                                              Psi_ax=0.0, Psi_sep=1.0, rhop=rhop)], False)
    plasma_dict["eq_data_2D"] = EQObj
    if (vessel_data is not None):
        plasma_dict["vessel_bd"] = np.array(vessel_data).T
    elif (vessel_bd_file is not None):
        vessel_bd = np.loadtxt(vessel_bd_file, skiprows=1)
        plasma_dict["vessel_bd"] = []
        plasma_dict["vessel_bd"].append(vessel_bd.T[0])
        plasma_dict["vessel_bd"].append(vessel_bd.T[1])
        plasma_dict["vessel_bd"] = np.array(plasma_dict["vessel_bd"])
    else:
        raise ValueError("vessel_data and vessel_bd_file cannot both be None")
    make_netcdf_plasma(filename, plasma_dict)
Exemple #2
0
def make_plasma_mat_for_testing(filename, shot, times, eq_exp, eq_diag, eq_ed, \
                                IDA_exp="AUGD", IDA_ed=0):
    plasma_dict = load_IDA_data(shot, timepoints=times, exp=IDA_exp, ed=IDA_ed)
    EQ_obj = EQData(shot, EQ_exp=eq_exp, EQ_diag=eq_diag, EQ_ed=eq_ed)
    plasma_dict["eq_data_2D"] = EQDataExt(shot, Ext_data=True)
    for time in times:
        plasma_dict["eq_data_2D"].insert_slices_from_ext(
            [time], [EQ_obj.GetSlice(time)])
    make_plasma_mat(filename, plasma_dict)
Exemple #3
0
def make_ECRadScenario_from_TB_input(shot, time, path, mat_out_name):
    plasma_dict = {}
    plasma_dict["shot"] = shot
    plasma_dict["time"] = np.array([time])
    topfile_dict = make_mdict_from_TB_files(os.path.join(path, "topfile"),
                                            True)
    EQObj = EQDataExt(shot)
    EQObj.load_slices_from_mat(plasma_dict["time"],
                               topfile_dict,
                               eq_prefix=False)
    vessel_bd = np.loadtxt(os.path.join(path, "vessel_bd"), skiprows=1)
    plasma_dict["vessel_bd"] = []
    plasma_dict["vessel_bd"].append(vessel_bd.T[0])
    plasma_dict["vessel_bd"].append(vessel_bd.T[1])
    plasma_dict["vessel_bd"] = np.array(plasma_dict["vessel_bd"])
    plasma_dict["rhop_prof"], plasma_dict["Te"] = np.loadtxt(os.path.join(
        path, "Te.dat"),
                                                             skiprows=1,
                                                             unpack=True)
    rhop_temp, plasma_dict["ne"] = np.loadtxt(os.path.join(path, "ne.dat"),
                                              skiprows=1,
                                              unpack=True)
    interpolate = False
    if (len(rhop_temp) != len(plasma_dict["rhop_prof"])):
        interpolate = True
    elif (any(rhop_temp != plasma_dict["rhop_prof"])):
        interpolate = True
    if (interpolate):
        ne_spl = InterpolatedUnivariateSpline(rhop_temp,
                                              plasma_dict["ne"],
                                              k=1)
        plasma_dict["ne"] = ne_spl(plasma_dict["rhop_prof"])
    plasma_dict["ne"] *= 1.e19
    plasma_dict["Te"] *= 1.e3
    plasma_dict["eq_data_2D"] = EQObj
    plasma_dict["prof_reference"] = "rhop_prof"
    make_plasma_mat(os.path.join(path, mat_out_name), plasma_dict)
def diag_weight(fig, Results, time_point, ch, DistWaveFile=None, ax=None):
    # Currently only RELAX/LUKE distributions supported
    # Extension for GENE trivial though
    if (ax is None):
        ax = fig.add_subplot(111)
    harmonic_n = 2
    itime = np.argmin(np.abs(time_point -
                             Results.Scenario.plasma_dict["time"]))
    time_cor = Results.Scenario.plasma_dict["time"][itime]
    EQObj = EQDataExt(Results.Scenario.shot)
    EQObj.set_slices_from_ext(Results.Scenario.plasma_dict["time"],
                              Results.Scenario.plasma_dict["eq_data"])
    B_ax = EQObj.get_B_on_axis(time_cor)
    if (DistWaveFile is not None):
        dist_obj = load_f_from_mat(DistWaveFile, True)
        f_inter = make_f_inter(Results.Config["Physics"]["dstf"],
                               dist_obj=dist_obj,
                               EQObj=EQObj,
                               time=time_cor)[0]
    else:
        dist_obj = None
        f_inter = make_f_inter("Th", EQObj=EQObj, time=time_cor)[0]
    m = 40
    n = 80
    if (dist_obj is None):
        # Estimate grid for thermal distribution assuming X-mode
        rhop_Te_signed = np.concatenate([- Results.Scenario.plasma_dict[Results.Scenario.plasma_dict["prof_reference"]][itime][::-1][:-1], \
                                        Results.Scenario.plasma_dict[Results.Scenario.plasma_dict["prof_reference"]][itime]])
        Te_signed_rhop = np.concatenate([
            Results.Scenario.plasma_dict["Te"][itime][::-1][:-1],
            Results.Scenario.plasma_dict["Te"][itime]
        ])
        Te_spl = InterpolatedUnivariateSpline(rhop_Te_signed, Te_signed_rhop)
        Te_weighted_spl = InterpolatedUnivariateSpline(Results.BPD["rhopX"][itime][ch - 1], Results.BPD["BPDX"][itime][ch - 1] * \
                                                       Te_spl(Results.BPD["rhopX"][itime][ch - 1]))
        Te_av = Te_weighted_spl.integral(-1.0, 1.0)
        beta_th = rel_thermal_beta(cnst.c**2 * cnst.electron_mass / cnst.e /
                                   Te_av)
        u_th = beta_th / np.sqrt(1.0 - beta_th**2)
        u_perp_grid = np.linspace(0.0, 5.0 * u_th, m)
        u_par_grid = np.linspace(-5.0 * u_th, 5.0 * u_th,
                                 n)  # five sigma should be good enough!
    else:
        u_perp_grid = np.linspace(0.0, np.max(dist_obj.u), m)
        u_par_grid = np.linspace(-np.max(dist_obj.u), np.max(f_inter.x), n)
    diag_weight_f = np.zeros((m, n))
    diag_weight_rel = np.zeros((m, n))
    for ir in range(Results.Config["Physics"]["N_ray"]):
        cur_BDOP = make_3DBDOP_for_ray(Results,
                                       time_cor,
                                       ch,
                                       ir,
                                       harmonic_n,
                                       B_ax,
                                       f_inter=f_inter)
        for irhop, in range(len(cur_BDOP.rho)):
            print(irhop + 1, " / ", len(cur_BDOP.rho))
            intercep_points = find_cell_interceps(u_par_grid, u_perp_grid,
                                                  cur_BDOP, irhop)
            for i_intercep, intercep_point in enumerate(intercep_points[:-1]):
                i = np.argmin(np.abs(intercep_point[0] - u_par_grid))
                j = np.argmin(np.abs(intercep_point[1] - u_perp_grid))
                if (u_par_grid[i] > intercep_point[0]):
                    i -= 1
                if (u_perp_grid[j] > intercep_point[1]):
                    j -= 1
                if (i < 0 or j < 0):
                    continue  # only happens at the lower bounds, where u_perp is very small and, therefore, also j is very small
                # Compute arclength
                t = np.zeros(cur_BDOP.u_par[irhop].shape)
                for i_res_line in range(1, len(cur_BDOP.u_par[irhop])):
                    t[i_res_line] = t[i_res_line - 1] + np.sqrt((cur_BDOP.u_par[irhop][i_res_line] - cur_BDOP.u_par[irhop][i_res_line - 1])**2 + \
                                                                (cur_BDOP.u_perp[irhop][i_res_line] - cur_BDOP.u_perp[irhop][i_res_line - 1])**2)
                t /= np.max(t)  # Normalize this
                # Sort
                t_spl = InterpolatedUnivariateSpline(cur_BDOP.u_par[irhop], t)
                try:
                    BPD_val_spl = InterpolatedUnivariateSpline(
                        t, cur_BDOP.val[irhop])
                except Exception as e:
                    print(e)
                BPD_val_rel_spl = InterpolatedUnivariateSpline(
                    t, np.abs(cur_BDOP.val[irhop] - cur_BDOP.val_back[irhop]))
                t1 = t_spl(intercep_point[0])
                t2 = t_spl(intercep_points[i_intercep + 1][0])
                diag_weight_f[j,i] += Results.weights["ray"][itime][ch][ir] * \
                                    BPD_val_spl.integral(t1, t2)
                diag_weight_rel[j,i] += Results.weights["ray"][itime][ch][ir] * \
                                    BPD_val_rel_spl.integral(t1, t2)
    ax.contour(u_perp_grid, u_par_grid, diag_weight_f.T / np.max(diag_weight_f.flatten()), \
                 levels = np.linspace(0.01,1,10), cmap = plt.get_cmap("plasma"))
    m = cm.ScalarMappable(cmap=plt.cm.get_cmap("plasma"))
    m.set_array(np.linspace(0.01, 1.0, 10))
    cb_diag = fig.colorbar(m, pad=0.15, ticks=[0.0, 0.5, 1.0])
    cb_diag.set_label(r"$D_\omega [\si{{a.u.}}]$")
    ax.set_ylabel(r"$u_\parallel$")
    ax.set_xlabel(r"$u_\perp$")
    ax.set_aspect("equal")
    return fig
def ECRH_weight(fig,
                Result_file,
                time_point,
                ibeam,
                DistWaveFile,
                beam_freq=105.e9,
                ax=None):
    # Currently only RELAX/LUKE distributions supported
    # Extension for GENE trivial though
    if (ax is None):
        ax = fig.add_subplot(111)
    harmonic_n = 2
    Results = ECRadResults()
    Results.from_mat_file(Result_file)
    itime = np.argmin(np.abs(time_point -
                             Results.Scenario.plasma_dict["time"]))
    time_cor = Results.Scenario.plasma_dict["time"][itime]
    EQObj = EQDataExt(Results.Scenario.shot)
    EQObj.set_slices_from_ext(Results.Scenario.plasma_dict["time"],
                              Results.Scenario.plasma_dict["eq_data"])
    B_ax = EQObj.get_B_on_axis(time_cor)
    EqSlice = EQObj.GetSlice(time_point)
    dist_wave_mat = loadmat(DistWaveFile)
    dist_obj = load_f_from_mat(DistWaveFile, True)
    f_inter = make_f_inter(Results.Config["Physics"]["dstf"],
                           dist_obj=dist_obj,
                           EQObj=EQObj,
                           time=time_cor)[0]
    linear_beam = read_waves_mat_to_beam(dist_wave_mat,
                                         EqSlice,
                                         use_wave_prefix=None)
    itme = np.argmin(np.abs(Results.Scenario.plasma_dict["time"] - time_point))
    Te_spl = InterpolatedUnivariateSpline(Results.Scenario.plasma_dict[Results.Scenario.plasma_dict["prof_reference"]][itime], \
                                          np.log(Results.Scenario.plasma_dict["Te"][itme]))
    ne_spl = InterpolatedUnivariateSpline(Results.Scenario.plasma_dict[Results.Scenario.plasma_dict["prof_reference"]][itime], \
                                          np.log(Results.Scenario.plasma_dict["ne"][itme]))
    m = 40
    n = 80
    u_perp_grid = np.linspace(0.0, np.max(dist_obj.u), m)
    u_par_grid = np.linspace(-np.max(dist_obj.u), np.max(f_inter.x), n)
    diag_weight_f = np.zeros((m, n))
    for ray in linear_beam.rays[ibeam]:
        tot_pw_ray, cur_PDP = make_PowerDepo_3D_for_ray(ray, beam_freq, "Re", harmonic_n, \
                                                        B_ax, EqSlice, Te_spl, ne_spl, f_inter, \
                                                        N_pnts=100, fast= True)
        for irhop in range(len(cur_PDP.rho)):
            print(irhop + 1, " / ", len(cur_PDP.rho))
            intercep_points = find_cell_interceps(u_par_grid, u_perp_grid,
                                                  cur_PDP, irhop)
            for i_intercep, intercep_point in enumerate(intercep_points[:-1]):
                i = np.argmin(np.abs(intercep_point[0] - u_par_grid))
                j = np.argmin(np.abs(intercep_point[1] - u_perp_grid))
                if (u_par_grid[i] > intercep_point[0]):
                    i -= 1
                if (u_perp_grid[j] > intercep_point[1]):
                    j -= 1
                if (i < 0 or j < 0):
                    continue  # only happens at the lower bounds, where u_perp is very small and, therefore, also j is very small
                # Compute arclength
                t = np.zeros(cur_PDP.u_par[irhop].shape)
                for i_res_line in range(1, len(cur_PDP.u_par[irhop])):
                    t[i_res_line] = t[i_res_line - 1] + np.sqrt((cur_PDP.u_par[irhop][i_res_line] - cur_PDP.u_par[irhop][i_res_line - 1])**2 + \
                                                                (cur_PDP.u_perp[irhop][i_res_line] - cur_PDP.u_perp[irhop][i_res_line - 1])**2)
                t /= np.max(t)  # Normalize this
                # Sort
                t_spl = InterpolatedUnivariateSpline(cur_PDP.u_par[irhop], t)
                try:
                    PDP_val_spl = InterpolatedUnivariateSpline(
                        t, cur_PDP.val[irhop])
                except Exception as e:
                    print(e)
                t1 = t_spl(intercep_point[0])
                t2 = t_spl(intercep_points[i_intercep + 1][0])
                diag_weight_f[j,i] += tot_pw_ray * \
                                        PDP_val_spl.integral(t1, t2)
    ax.contourf(u_perp_grid, u_par_grid, diag_weight_f.T / np.max(diag_weight_f.flatten()), \
                 levels = np.linspace(0.01,1,10), cmap = plt.get_cmap("Greens"))
    m = cm.ScalarMappable(cmap=plt.cm.get_cmap("Greens"))
    m.set_array(np.linspace(0.01, 1.0, 10))
    cb_diag = fig.colorbar(m, pad=0.15, ticks=[0.0, 0.5, 1.0])
    cb_diag.set_label(r"$\mathrm{d}P/\mathrm{d}s [\si{{a.u.}}]$")
    ax.set_ylabel(r"$u_\parallel$")
    ax.set_xlabel(r"$u_\perp$")
    ax.set_aspect("equal")
    return fig
 def __init__(self, shot, external_folder='', EQ_exp="AUGD", EQ_diag="EQH", EQ_ed=0, Ext_data=False):
     EQDataExt.__init__(self, shot, external_folder, EQ_exp, EQ_diag, EQ_ed, Ext_data)