diff --git a/src/karl/katrin_fields.cpp b/src/karl/katrin_fields.cpp index f0d8257..b916860 100644 --- a/src/karl/katrin_fields.cpp +++ b/src/karl/katrin_fields.cpp @@ -28,7 +28,7 @@ blitz::TinyVector Katrin_Fields::getElField_Vm( E_cyl, cylinder); - return E_field_ptr->value(cylinder); + return E_cart; //return blitz::TinyVector(0., 0., 0.01); } \ No newline at end of file diff --git a/src/tools/linkToAcronym/createElectricFieldFromH5.py b/src/tools/linkToAcronym/createElectricFieldFromH5.py new file mode 100644 index 0000000..07cf6cf --- /dev/null +++ b/src/tools/linkToAcronym/createElectricFieldFromH5.py @@ -0,0 +1,166 @@ +import numpy as np +import matplotlib.pyplot as plt +from scipy import signal +from scipy.ndimage import gaussian_filter +from matplotlib import ticker, cm +import h5py as h5py + +import import_dings as impding + + +timestep = 761600 +filename_derived = "/home/jonas/new_workspace/Coding/Output/LRZ/HugeClassChanges/triangleDensityLonger/reflecting/derived.h5" +filename_output = "/home/jonas/new_workspace/Coding/Output/LRZ/HugeClassChanges/triangleDensityLonger/reflecting/karl_input.h5" + +dx = 0.13801796042392211 +L_wgts = 1300 + +cylinder_origin = [35,35,11] +radius = 32 +height = 1434 + +z_start = 350 +z_end = 1050 + +num_cells = int((L_wgts - height*dx)/dx) + + +def radialAverage(electricField, cylinder_origin, radius, height): + data_2d_x = electricField[cylinder_origin[0]:cylinder_origin[0]+radius, + cylinder_origin[1], + cylinder_origin[2]:cylinder_origin[2]+height] + + data_2d_xneg = electricField[cylinder_origin[0]-radius:cylinder_origin[0], + cylinder_origin[1], + cylinder_origin[2]:cylinder_origin[2]+height] + data_2d_xneg = np.flip(data_2d_xneg, axis=0) + + data_2d_y = electricField[cylinder_origin[0], + cylinder_origin[1]:cylinder_origin[1]+radius, + cylinder_origin[2]:cylinder_origin[2]+height] + + data_2d_yneg = electricField[cylinder_origin[0], + cylinder_origin[1]-radius:cylinder_origin[1], + cylinder_origin[2]:cylinder_origin[2]+height] + data_2d_yneg = np.flip(data_2d_yneg, axis=0) + + + data_mean = (data_2d_x + data_2d_xneg + data_2d_y + data_2d_yneg)/4 + + return data_mean + + +def extendValues(data_2d, z_start, z_end, num_cells): + (size_x, size_y) = np.shape(data_2d) + data_ext = np.zeros((size_x, size_y+num_cells)) + data_ext[:,:z_start] = data_2d[:,:z_start] + data_ext[:,-z_end:] = data_2d[:,-z_end:] + + d = z_end - z_start + d_ext = d + num_cells + d_ratio = d/d_ext + + for i_ext in np.arange(z_start,d_ext,1): + + i = int(np.floor(i_ext*d_ratio)) + data_ext[:,i_ext] = data_2d[:,i] + + return data_ext + +def copyValues(data_2d, z_start, z_end, num_cells): + (size_x, size_y) = np.shape(data_2d) + data_copy = np.zeros((size_x, size_y+num_cells)) + data_copy[:,:z_start] = data_2d[:,:z_start] + data_copy[:,-z_end:] = data_2d[:,-z_end:] + + d = z_end - z_start + num_copies = num_cells/d + + for i_copy in np.arange(0,int(num_copies),1): + i_start = z_start + d*i_copy + i_end = z_start + d*(i_copy+1) + data_copy[:,i_start:i_end] = data_2d[:,z_start:z_end] + + + i_start = z_start + int(num_copies)*d + i_end = size_y + num_cells - z_end + data_copy[:,i_start:i_end] = data_2d[:,z_start:z_start + i_end-i_start] + + + return data_copy + +data_Er = impding.getNumpyArray(filename_derived, timestep, "E_r") +data_Er_2d = radialAverage(data_Er[:,:,:-10], cylinder_origin, radius, height) + +data_Er_2d_ext = copyValues(data_Er_2d, z_start, z_end, num_cells) + +data_Ez = impding.getNumpyArray(filename_derived, timestep, "E_z") +data_Ez_2d = radialAverage(data_Ez[:,:,:-10], cylinder_origin, radius, height) + +data_Ez_2d_ext = copyValues(data_Ez_2d, z_start, z_end, num_cells) + + +fig, (ax0,ax1) = plt.subplots(2,1) +pos = ax0.contourf(data_Er_2d_ext, locator=ticker.LogLocator(), cmap=cm.Reds) +neg = ax0.contourf(data_Er_2d_ext*-1, locator=ticker.LogLocator(), cmap = cm.Purples) + +pos = ax1.contourf(data_Er_2d, locator=ticker.LogLocator(), cmap=cm.Reds) +neg = ax1.contourf(data_Er_2d*-1, locator=ticker.LogLocator(), cmap = cm.Purples) + + +cbar_pos = fig.colorbar(pos) +cbar_neg = fig.colorbar(neg) +plt.show() + + +fig, (ax0,ax1) = plt.subplots(2,1) +pos = ax0.contourf(data_Ez_2d_ext, locator=ticker.LogLocator(), cmap=cm.Reds) +neg = ax0.contourf(data_Ez_2d_ext*-1, locator=ticker.LogLocator(), cmap = cm.Purples) + +pos = ax1.contourf(data_Ez_2d, locator=ticker.LogLocator(), cmap=cm.Reds) +neg = ax1.contourf(data_Ez_2d*-1, locator=ticker.LogLocator(), cmap = cm.Purples) + + +cbar_pos = fig.colorbar(pos) +cbar_neg = fig.colorbar(neg) +plt.show() + +def saveToKarlH5File(filename, Er, Ez): + file = h5py.File(filename, 'w') + file.create_dataset('Er', data=Er) + file.create_dataset('Ez', data=Ez) + file.close() + +saveToKarlH5File(filename_output, data_Er_2d_ext, data_Ez_2d_ext) + +""" +win = signal.windows.hann(150) +fildered =signal.convolve(data_Er_2d[0,:], win, mode='same')/sum(win) +plt.plot(data_Er_2d[0,:]) +plt.plot(fildered) +plt.show() + +plt.contourf(data_Er_2d) +plt.show() + + + +filtered_2d = gaussian_filter(data_Er_2d,sigma=(2,10)) + +fig, ax = plt.subplots() +pos = plt.contourf(filtered_2d, locator=ticker.LogLocator(), cmap=cm.Reds) +neg = plt.contourf(filtered_2d*-1, locator=ticker.LogLocator(), cmap = cm.Purples) + +cbar_pos = fig.colorbar(pos) +cbar_neg = fig.colorbar(neg) +plt.show() + + +plt.plot(filtered_2d[0,:],label="0") +plt.plot(filtered_2d[5,:],label="5") +plt.plot(filtered_2d[16,:],label="16") +plt.plot(filtered_2d[20,:],label="20") +plt.legend() +plt.show() + +""" \ No newline at end of file diff --git a/src/tools/linkToAcronym/import_dings.py b/src/tools/linkToAcronym/import_dings.py new file mode 100644 index 0000000..fb99090 --- /dev/null +++ b/src/tools/linkToAcronym/import_dings.py @@ -0,0 +1,108 @@ +import numpy as np +import pandas as pd + +import h5py as h5py + +pd.options.mode.chained_assignment = None + +h5_path = {\ +"B0" : "felder/B/B[0]",\ +"B1" : "felder/B/B[1]",\ +"B2" : "felder/B/B[2]",\ +"E0" : "felder/E/E[0]",\ +"E1" : "felder/E/E[1]",\ +"E2" : "felder/E/E[2]",\ +"rhoL0" : "rho/rhoL[0]",\ +"rhoL1" : "rho/rhoL[1]",\ +"rhoL2" : "rho/rhoL[2]",\ +"rhoL3" : "rho/rhoL[3]",\ +"rhoM0" : "rho/rhoM[0]",\ +"rhoM1" : "rho/rhoM[1]",\ +"rhoM2" : "rho/rhoM[2]",\ +"rhoM3" : "rho/rhoM[3]",\ +"rhoN0" : "rho/rhoN[0]",\ +"El_x" : "longitudinalFields/Ex_l",\ +"El_y" : "longitudinalFields/Ey_l",\ +"El_z" : "longitudinalFields/Ez_l",\ +"potential" : "potential/potential",\ +"E_r" : "cylinderFields/E_r",\ +"E_theta" : "cylinderFields/E_theta", +"E_z" : "cylinderFields/E_z",\ +"E_total" : "cylinderFields/E_total"} + +def openH5file(filename): + f = h5py.File(filename, 'r') + return f + +def getTimestepKey(file, timestep): + try: + timestep_key = 'Timestep_' + str(timestep) + '/' + grp = file[timestep_key] + return timestep_key + except: + print("You can use this timesteps:") + print(list(file.keys())) + +def getSimulationSize(file, timestep_key): + grp = file[timestep_key] + size_str = str(grp['felder/B/B[0]']).replace('"','') + size_str = size_str.strip('') + size_str = size_str.replace('(','') + size_str = size_str.replace(')','') + size_str = size_str.replace(' ','') + size_array = size_str.split(",") + for i in np.arange(0,3): + size_array[i] = int(size_array[i]) + nx = size_array[0] + ny = size_array[1] + nz = size_array[2] + return nx, ny, nz + +def getNumpyArray(filename, timestep, key): + file = openH5file(filename) + timestep_key = getTimestepKey(file, timestep) + h5_key = timestep_key + h5_path[key] + ar = np.array(file[h5_key]) + file.close() + return ar + +def getNumpyArray2D(filename, timestep, key, cut_axis, cut_coordinate): + array_3D = getNumpyArray(filename, timestep, key) + if cut_axis == "x": + return np.transpose(array_3D[cut_coordinate, :, :]) + elif cut_axis == "y": + return np.transpose(array_3D[:, cut_coordinate, :]) + elif cut_axis == "z": + return np.transpose(array_3D[:, :, cut_coordinate]) + else: + print("The direction you specified is not valid!") + return None + +def getParameters(filename): + file = openH5file(filename) + dx = np.array(file["config/dx"]) + dt = np.array(file["config/dt"]) + file.close() + return dx[0], dt[0] + +def getNumpyArray1D(filename, timestep, key, axis, base_coordinate): + array_3D = getNumpyArray(filename, timestep, key) + if axis == "x": + return array_3D[:, base_coordinate[1], base_coordinate[2]] + elif axis == "y": + return array_3D[base_coordinate[0], :, base_coordinate[2]] + elif axis == "z": + return array_3D[base_coordinate[0], base_coordinate[1], :] + else: + print("The direction you specified is not valid!") + return None + +def extractSinglePoint(filename, point, key, last_timestep, step_timestep, first_timestep=0): + timesteps = np.arange(first_timestep, last_timestep, step_timestep) + data = np.zeros_like(timesteps,dtype=float) + + for i in np.arange(0,len(timesteps)): + data[i] = getNumpyArray(filename, timesteps[i], key)[point[0],point[1],point[2]] + + return data \ No newline at end of file