class ECRadRayManipulator(object): ''' classdocs ''' def __init__(self, ECRad_result_file): ''' Constructor ''' self.org_results_file = ECRad_result_file self.ECRad_results = ECRadResults() self.ECRad_results.from_mat_file(ECRad_result_file) self.N_ch = self.ECRad_results.Scenario.ray_launch[0]["f"] self.N_ray = self.ECRad_results.Config["Physics"]["N_ray"] if (self.ECRad_results.Config["Physics"]["considered_modes"] == 1): self.modes = ["X"] elif (self.ECRad_results.Config["Physics"]["considered_modes"] == 2): self.modes = ["O"] elif (self.ECRad_results.Config["Physics"]["considered_modes"] == 3): self.modes = ["X", "O"] else: raise ValueError("Invalid value in considered modes") def get_Rz_single_ray(self, it, ich, mode="X", iray=0): if (self.N_ray == 1): R = np.sqrt(self.ECRad_results.ray["x"+mode][it][ich]**2 + \ self.ECRad_results.ray["y"+mode][it][ich]**2) return R, self.ECRad_results.ray["z" + mode][it][ich] else: R = np.sqrt(self.ECRad_results.ray["x"+mode][it][ich][iray]**2 + \ self.ECRad_results.ray["y"+mode][it][ich][iray]**2) return R, self.ECRad_results.ray["z" + mode][it][ich][iray] def get_field_single_ray(self, field, it, ich, mode="X", iray=0): if (self.N_ray == 1): return self.ECRad_results.ray[field + mode][it][ich] else: return self.ECRad_results.ray[field + mode][it][ich][iray] def set_field_single_ray(self, field, values, it, ich, mode="X", iray=0): if (self.N_ray == 1): if (self.ECRad_results.ray[field + mode][it][ich].shape != values.shape): raise ValueError( "The new values must have the same shape as the original values" ) self.ECRad_results.ray[field + mode][it][ich] = values else: if (self.ECRad_results.ray[field + mode][it][ich][iray].shape != values.shape): raise ValueError( "The new values must have the same shape as the original values" ) self.ECRad_results.ray[field + mode][it][ich][iray] = values def save(self, new_filename, comment=""): if (new_filename == self.org_results_file): print("This routine does not allow overwriting of the old file!") return self.ECRad_results.to_mat_file(new_filename, comment)
def manipiulate_rays(result_file_in, result_file_out): res = ECRadResults() res.from_mat_file(result_file_in) for index in range(len(res.time)): for ich in range(len(res.Scenario.ray_launch[index]["f"])): for mode in res.modes: if (res.Config["Physics"]["N_ray"] > 1): for iray in range(len(res.ray["s" + mode][index][ich])): res.ray["Te" + mode][index][ich][iray][:] /= 2.0 else: res.ray["Te" + mode][index][ich][:] /= 2.0 res.to_mat_file(result_file_out)
def diag_weight_stand_alone(fig, ax, Result_file, time_point, ch, DistWaveFile=None): Results = ECRadResults() Results.from_mat_file(Result_file) fig = diag_weight(fig, Results, time_point, ch, DistWaveFile=DistWaveFile, ax=ax)
def repair_ECRad_results(folder_in, folder_out=None): # Allows to make bulk modification of result files using glob # If folder_out is True it overwrites! filelist = glob.glob(os.path.join(folder_in, "*.mat")) # filelist = ['/tokp/work/sdenk/DRELAX_Results/ECRad_35662_ECECTACTC_run0208.mat'] cur_result = ECRadResults() for filename in filelist: cur_result.reset() cur_result.from_mat_file(filename) # Enter bulk modifcations here cur_result.Scenario.used_diags_dict["CTC"].diag = "CTC" if(folder_out is None): cur_result.to_mat_file(filename) else: cur_result.to_mat_file(os.path.join(folder_out, os.path.basename(filename)))
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