17 KiB
17 KiB
In [45]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.optimize as optimization
import glob
import h5py
%matplotlib inline
plt.style.use('ggplot')
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)In [2]:
df_Ex = pd.read_csv("data/cylinder_Ex.txt", delim_whitespace=True, skiprows=9, header=None, names=['x','y','z','Ex'])
df_Ey = pd.read_csv("data/cylinder_Ey.txt", delim_whitespace=True, skiprows=9, header=None, names=['x','y','z','Ey'])
df_Ez = pd.read_csv("data/cylinder_Ez.txt", delim_whitespace=True, skiprows=9, header=None, names=['x','y','z','Ez'])In [3]:
df = pd.merge(df_Ex, df_Ey, on=['x','y','z'], how='outer')
df = pd.merge(df, df_Ez, on=['x','y','z'], how='outer')In [4]:
df = df.fillna(0)In [5]:
df['r'] = np.sqrt(df.x**2 + df.y**2)In [6]:
df['phi'] = np.arccos(df.x/df.r)
df.phi[df.y<0] = 2*np.pi-df.phiIn [7]:
df = df.loc[:,['z','r','phi','Ex','Ey','Ez']]In [54]:
index = ['z','r','phi']
n = [1000, 50, 20]
maxs = [16, 0.45, np.pi*2]
bins = [np.linspace(0,maxs[i],n[i]) for i in np.arange(3)]
mids = [(x[:-1] + x[1:])/2 for x in bins]
In [ ]:
In [30]:
for i in np.arange(3):
df[index[i] + '_binned'] = pd.cut(df[index[i]], bins = list(bins[i]), labels = mids[i], include_lowest=True)In [31]:
df_mean = df.groupby(['z_binned', 'r_binned','phi_binned'], as_index=False).mean()In [32]:
news = [[],[],[]]
for z in mids[0]:
for r in mids[1]:
for phi in mids[2]:
news[0].append(z)
news[1].append(r)
news[2].append(phi)
In [39]:
for i in np.arange(3):
df_mean[index[i]] = news[i]In [41]:
df_mean = df_mean.drop(['z_binned','r_binned','phi_binned'],axis = 1)In [42]:
df_mean.loc[:,['Ex','Ey','Ez']] = df_mean.loc[:,['Ex','Ey','Ez']].fillna(0)
df_mean['E'] = np.sqrt(df_mean['Ex']**2 + df_mean['Ey']**2 + df_mean['Ez']**2)In [52]:
ns = [x.shape[0] for x in mids]
def calc_bin(z, r, phi):
z_bin = np.ceil(z/z_bin_width)-1
r_bin = np.ceil(r/r_bin_width)-1
phi_bin = np.ceil(phi/phi_bin_width)-1
return z_bin * n_r_bins + r_binIn [ ]:
In [ ]:
bins = []
for i in np.arange(df_mean.shape[0]):
bins.append(int (calc_bin(df_mean.z[i], df_mean.r[i])))
df_mean['bin'] = binsIn [ ]:
df_export = df_mean[['bin', 'v_z', 'v_r']]In [50]:
df_export.to_csv('e_field.dat', sep='\t', index=False, float_format = "%.5e")[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) [0;32m<ipython-input-50-86ae9f896800>[0m in [0;36m<module>[0;34m[0m [0;32m----> 1[0;31m [0mdf_export[0m[0;34m.[0m[0mto_csv[0m[0;34m([0m[0;34m'e_field.dat'[0m[0;34m,[0m [0msep[0m[0;34m=[0m[0;34m'\t'[0m[0;34m,[0m [0mindex[0m[0;34m=[0m[0;32mFalse[0m[0;34m,[0m [0mfloat_format[0m [0;34m=[0m [0;34m"%.5e"[0m[0;34m)[0m[0;34m[0m[0m [0m [0;31mNameError[0m: name 'df_export' is not defined
In [49]:
df_z_mean = df_mean.groupby('z', as_index=False).mean()
df_plot = df_z_mean[df_z_mean.E>0.1]
plt.plot(df_plot.z, df_plot.E)
Out [49]:
[<matplotlib.lines.Line2D at 0x132885dd8>]
In [ ]: