Impact-T fieldmap reconstruction¶
In [1]:
Copied!
from impact import Impact, fieldmaps
import numpy as np
from impact import Impact, fieldmaps
import numpy as np
In [2]:
Copied!
ifile = 'templates/lcls_injector/ImpactT.in'
I = Impact(input_file=ifile)
ifile = 'templates/lcls_injector/ImpactT.in'
I = Impact(input_file=ifile)
In [3]:
Copied!
# Fieldmaps are stored here
I.input['fieldmaps'].keys()
# Fieldmaps are stored here
I.input['fieldmaps'].keys()
Out[3]:
dict_keys(['rfdata201', 'rfdata102', 'rfdata4', 'rfdata5', 'rfdata6', 'rfdata7'])
In [4]:
Copied!
# Look at a solrf element.
I.ele['SOL1']
# Look at a solrf element.
I.ele['SOL1']
Out[4]:
{'description': 'name:SOL1', 'original': '0.49308 0 0 105 0.0 0.0 0.0 0.0 102 0.15 0.0 0.0 0.0 0.0 0.0 0.2457 /!name:SOL1', 'L': 0.49308, 'type': 'solrf', 'zedge': 0.0, 'rf_field_scale': 0.0, 'rf_frequency': 0.0, 'theta0_deg': 0.0, 'filename': 'rfdata102', 'radius': 0.15, 'x_offset': 0.0, 'y_offset': 0.0, 'x_rotation': 0.0, 'y_rotation': 0.0, 'z_rotation': 0.0, 'solenoid_field_scale': 0.2457, 's': 0.49308, 'name': 'SOL1'}
In [5]:
Copied!
# This is its fieldmap filename
I.ele['SOL1']['filename']
# This is its fieldmap filename
I.ele['SOL1']['filename']
Out[5]:
'rfdata102'
In [6]:
Copied!
# That data is here.
fmap = I.input['fieldmaps']['rfdata102']
fmap
# That data is here.
fmap = I.input['fieldmaps']['rfdata102']
fmap
Out[6]:
{'info': {'format': 'solrf', 'filePath': '/Users/chrisonian/Code/GitHub/lume-impact/docs/examples/templates/lcls_injector/rfdata102'}, 'field': {'Ez': {'z0': 0.0, 'z1': 0.0, 'L': 0.0, 'fourier_coefficients': array([0.])}, 'Bz': {'z0': -0.53308, 'z1': 0.49308, 'L': 1.02616, 'fourier_coefficients': array([ 7.59104159e-01, 1.86860998e-01, 2.68209187e-09, -4.75732117e-01, 5.68488796e-09, -2.63303014e-01, -2.00685489e-10, 8.31385362e-02, -1.07061180e-09, 3.32147505e-02, 3.34109023e-09, -4.03166145e-03, 2.36977653e-09, 7.55695585e-02, 7.03628508e-09, 4.05472421e-02, -7.93679210e-09, -2.78630279e-02, -2.80674359e-08, -1.23309101e-02, -1.21750197e-08, -1.11917490e-03, 2.72183881e-08, -1.76941899e-02, 2.07775590e-08, -6.82928971e-03, -4.64611502e-09, 9.04327137e-03, -2.74644209e-08, 3.46208848e-03, 1.22819960e-08, 4.56318358e-04, 2.11520498e-08, 4.27450120e-03, -2.88644504e-08, 7.52482987e-04, -3.59752217e-08, -2.87393685e-03, 1.26542743e-08, -8.38592686e-04, 3.01815044e-08, -1.30737597e-04, -9.86177028e-09, -1.04357300e-03, -6.63856936e-09, -2.53914168e-06, 2.18126093e-08, 8.16316559e-04, -9.00342349e-09, 1.78532652e-04, -3.75346464e-08, -1.85656369e-05, 1.66112357e-08, 2.70906299e-04, 4.30436466e-08, -6.91061736e-05, 1.79889178e-08, -3.21395974e-04, -3.28634884e-08, -6.26834704e-05, 4.78721227e-08, 4.53788783e-05, -2.40687516e-08, -7.00121686e-05, -1.09529996e-08, 5.88552784e-06, -3.18410200e-08, 9.16404141e-05, 2.20705961e-08, -1.40213052e-05, 8.62505486e-08, -7.90063994e-05, 1.80561930e-08, 9.73707320e-06, -3.55099131e-08, -3.24265398e-05, 8.04201228e-09, -2.52641442e-05, 2.39895303e-08, 7.72642702e-07, -1.86279094e-08, 3.37057351e-05, -5.60221048e-08, 8.99206468e-06, 3.60553118e-08, -1.69794491e-05, 4.76241852e-08, -8.20592718e-06, -3.75317080e-08, -1.46785073e-05, -1.35667390e-08, -8.04441471e-06, 9.17261718e-08, -3.13719566e-05, 5.84058221e-08, -4.49735425e-06, -1.30484352e-07, -1.47358475e-05, -1.10775372e-07, 1.89208186e-05, 5.67838231e-08, -3.75811096e-05, 6.90257455e-08, -2.31210131e-05, -2.80681931e-08, 4.15569850e-07, -3.99391984e-08, 2.35273720e-05, 1.13469662e-07, -2.13950099e-05, 1.17155810e-08, -2.05874941e-05, -6.31687124e-08, 2.11433614e-05, -9.36452667e-08, -3.11181571e-05, -6.42682642e-08, -1.59322792e-05, -1.78752181e-08])}}}
In [7]:
Copied!
# Reconstruction function
fieldmaps.fieldmap_reconstruction_solrf(fmap['field']['Bz'], 0)
# Reconstruction function
fieldmaps.fieldmap_reconstruction_solrf(fmap['field']['Bz'], 0)
Out[7]:
0.006497827018877966
Basic plot¶
In [8]:
Copied!
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format='retina'
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format='retina'
In [9]:
Copied!
zlist = np.linspace(0, 0.49308, 1000)
fieldlist = [fieldmaps.fieldmap_reconstruction_solrf(fmap['field']['Bz'], z) for z in zlist]
zlist = np.linspace(0, 0.49308, 1000)
fieldlist = [fieldmaps.fieldmap_reconstruction_solrf(fmap['field']['Bz'], z) for z in zlist]
In [10]:
Copied!
# z at max field
zlist[np.argmax(np.array(fieldlist))]
# z at max field
zlist[np.argmax(np.array(fieldlist))]
Out[10]:
0.19496156156156158
In [11]:
Copied!
plt.plot(zlist, fieldlist);
plt.plot(zlist, fieldlist);
In [12]:
Copied!
# Integrated field (approximate)
field_scale = 0.243 # from imput file
BL = np.sum(fieldlist)*0.49308/1000 # T*m
BL * field_scale * 10 # T*m -> kG*m
# Integrated field (approximate)
field_scale = 0.243 # from imput file
BL = np.sum(fieldlist)*0.49308/1000 # T*m
BL * field_scale * 10 # T*m -> kG*m
Out[12]:
0.47251221486003464
In [13]:
Copied!
1/BL
1/BL
Out[13]:
5.142724195436521
Create Fieldmap¶
In [14]:
Copied!
fmap2 = fmap.copy()
fmap2['field']['Bz']['z0'] = min(zlist)
fmap2['field']['Bz']['z1'] = max(zlist)
fmap2['field']['Bz']['L'] = zlist.ptp()
fmap2['field']['Bz']['fourier_coefficients'] = fieldmaps.create_fourier_coefficients(zlist, fieldlist, n=20)
fmap2 = fmap.copy()
fmap2['field']['Bz']['z0'] = min(zlist)
fmap2['field']['Bz']['z1'] = max(zlist)
fmap2['field']['Bz']['L'] = zlist.ptp()
fmap2['field']['Bz']['fourier_coefficients'] = fieldmaps.create_fourier_coefficients(zlist, fieldlist, n=20)
In [15]:
Copied!
fieldlist2 = [fieldmaps.fieldmap_reconstruction_solrf(fmap2['field']['Bz'], z) for z in zlist]
plt.plot(zlist, fieldlist, label='original')
plt.plot(zlist, fieldlist2, '--', label='created')
plt.legend()
fieldlist2 = [fieldmaps.fieldmap_reconstruction_solrf(fmap2['field']['Bz'], z) for z in zlist]
plt.plot(zlist, fieldlist, label='original')
plt.plot(zlist, fieldlist2, '--', label='created')
plt.legend()
Out[15]:
<matplotlib.legend.Legend at 0x14128e1b0>
In [16]:
Copied!
fmap2
fmap2
Out[16]:
{'info': {'format': 'solrf', 'filePath': '/Users/chrisonian/Code/GitHub/lume-impact/docs/examples/templates/lcls_injector/rfdata102'}, 'field': {'Ez': {'z0': 0.0, 'z1': 0.0, 'L': 0.0, 'fourier_coefficients': array([0.])}, 'Bz': {'z0': 0.0, 'z1': 0.49308, 'L': 0.49308, 'fourier_coefficients': array([ 7.89495139e-01, 4.33867017e-01, -3.57732944e-01, 2.94026907e-02, -1.53404612e-01, 3.25260823e-02, 5.58082653e-02, 6.13403407e-02, 2.39379983e-02, 3.57981962e-03, -1.24552364e-03, -1.02459657e-02, 1.68094954e-02, 1.23265799e-03, 7.41061917e-03, -2.88377447e-03, -2.34475616e-03, -3.69833230e-03, -3.94627426e-04, -5.82441696e-05, 1.26215240e-04, 3.02765112e-04, -1.36723089e-03, -2.26429980e-04, -2.39596500e-04, 3.45355578e-04, -4.64071534e-05, 2.62624018e-04, 2.21612127e-05, 6.48490182e-05, -1.18646462e-04, 1.19955063e-05, 2.07495250e-04, 6.46051459e-05, -2.35246836e-05, -3.25265093e-05, 1.09808260e-04, -1.75593316e-05, -9.23404592e-05])}}}
In [17]:
Copied!
from numpy import sin, cos, pi, arange
from numpy import sin, cos, pi, arange
In [18]:
Copied!
# Raw data from
coefs = fmap2['field']['Bz']['fourier_coefficients']
coefs
# Raw data from
coefs = fmap2['field']['Bz']['fourier_coefficients']
coefs
Out[18]:
array([ 7.89495139e-01, 4.33867017e-01, -3.57732944e-01, 2.94026907e-02, -1.53404612e-01, 3.25260823e-02, 5.58082653e-02, 6.13403407e-02, 2.39379983e-02, 3.57981962e-03, -1.24552364e-03, -1.02459657e-02, 1.68094954e-02, 1.23265799e-03, 7.41061917e-03, -2.88377447e-03, -2.34475616e-03, -3.69833230e-03, -3.94627426e-04, -5.82441696e-05, 1.26215240e-04, 3.02765112e-04, -1.36723089e-03, -2.26429980e-04, -2.39596500e-04, 3.45355578e-04, -4.64071534e-05, 2.62624018e-04, 2.21612127e-05, 6.48490182e-05, -1.18646462e-04, 1.19955063e-05, 2.07495250e-04, 6.46051459e-05, -2.35246836e-05, -3.25265093e-05, 1.09808260e-04, -1.75593316e-05, -9.23404592e-05])
In [19]:
Copied!
A0 = coefs[0] # constant factor
A = coefs[1::2] # cos parts
B = coefs[2::2] # sin parts
A0 = coefs[0] # constant factor
A = coefs[1::2] # cos parts
B = coefs[2::2] # sin parts
In [20]:
Copied!
L = 0.6
@np.vectorize
def f(z):
phase = 2*pi*(z/L-1/2)
return A0/2 + sum([ A[n-1] * cos(n*phase) + B[n-1] * sin(n*phase) for n in arange(1, len(A)+1)])
z0 = np.linspace(0, L, 100)
plt.plot(z0, f(z0))
L = 0.6
@np.vectorize
def f(z):
phase = 2*pi*(z/L-1/2)
return A0/2 + sum([ A[n-1] * cos(n*phase) + B[n-1] * sin(n*phase) for n in arange(1, len(A)+1)])
z0 = np.linspace(0, L, 100)
plt.plot(z0, f(z0))
Out[20]:
[<matplotlib.lines.Line2D at 0x141478380>]