Skip to content
Snippets Groups Projects
Commit 7a67eefe authored by Yngve Levinsen's avatar Yngve Levinsen
Browse files

cleaning the repository

- more consistent module names
- removing a duplicate
- fixing new flake8 warnings
parent 4129aad7
No related branches found
No related tags found
No related merge requests found
...@@ -78,13 +78,13 @@ for f in os.listdir("."): ...@@ -78,13 +78,13 @@ for f in os.listdir("."):
has_weekday = False has_weekday = False
for l in open(f, "r", encoding="utf8"): for line in open(f, "r", encoding="utf8"):
if l.strip()[0] == "#": if line.strip()[0] == "#":
if "Weekday" in l: if "Weekday" in line:
has_weekday = True has_weekday = True
continue continue
# encoding issues with swedish: # encoding issues with swedish:
lsp = l.split(" ") lsp = line.split(" ")
date = lsp[0].strip() date = lsp[0].strip()
if has_weekday: if has_weekday:
i_desc = 2 i_desc = 2
......
...@@ -5,13 +5,13 @@ year = 2020 ...@@ -5,13 +5,13 @@ year = 2020
fout = open(f"list_{year}.txt", "w") fout = open(f"list_{year}.txt", "w")
count = 0 count = 0
for l in open("orig.txt", "r"): for line in open("orig.txt", "r"):
if len(l.strip()): if len(line.strip()):
if "Date" in l: if "Date" in line:
fout.write("# ") fout.write("# ")
fout.write(l[:-1]) fout.write(line[:-1])
count += 1 count += 1
elif len(l) == 1: elif len(line) == 1:
continue continue
else: else:
fout.write(" ") fout.write(" ")
......
...@@ -8,9 +8,9 @@ __all__ = [ ...@@ -8,9 +8,9 @@ __all__ = [
"fieldmap", "fieldmap",
"installed", "installed",
"lib_tw", "lib_tw",
"SP_Relativity", "relativity",
"TraceWin", "TraceWin",
"TTF", "ttf.py",
"nextcloud", "nextcloud",
] ]
__version__ = "2.9.1" __version__ = "2.9.1"
...@@ -972,7 +972,7 @@ def plt2x(path_file): ...@@ -972,7 +972,7 @@ def plt2x(path_file):
s.append(unpack("d", file.read(8))[0]) s.append(unpack("d", file.read(8))[0])
phs.append(unpack("d", file.read(8))[0]) phs.append(unpack("d", file.read(8))[0])
ken.append(unpack("d", file.read(8))[0]) ken.append(unpack("d", file.read(8))[0])
x.append([[unpack("f", file.read(4))[0] for l in range(7)] for k in range(Nptcl)]) x.append([[unpack("f", file.read(4))[0] for line in range(7)] for k in range(Nptcl)])
except IndexError: except IndexError:
break break
......
...@@ -182,7 +182,6 @@ def emit_norm(u_up_array, gamma): ...@@ -182,7 +182,6 @@ def emit_norm(u_up_array, gamma):
------- -------
normalized emittance of the distribution/array in units of pi.mm.mrad normalized emittance of the distribution/array in units of pi.mm.mrad
""" """
# from ess import SP_Relativity as spr
bt = beta_from_gamma(gamma) bt = beta_from_gamma(gamma)
geo_emit = emit_geo(u_up_array) geo_emit = emit_geo(u_up_array)
emit = bt * gamma * geo_emit emit = bt * gamma * geo_emit
......
...@@ -3,7 +3,7 @@ set of functions for calculating the transit time factor and the input phase res ...@@ -3,7 +3,7 @@ set of functions for calculating the transit time factor and the input phase res
-- M. Eshraqi 2018 July 11 -- M. Eshraqi 2018 July 11
""" """
import SP_Relativity as myrel from . import relativity
import numpy as np import numpy as np
...@@ -57,7 +57,7 @@ def Field_TTF(field_1d, step_dz, freq, E_in, mass, RetMaxPhase): ...@@ -57,7 +57,7 @@ def Field_TTF(field_1d, step_dz, freq, E_in, mass, RetMaxPhase):
dE += fval[0] * step_dz * np.cos(omega * t + d_phase * np.radians(phase)) dE += fval[0] * step_dz * np.cos(omega * t + d_phase * np.radians(phase))
if dE < 0: if dE < 0:
print("Please adjust the field level, particle decelrated to zero energy!") print("Please adjust the field level, particle decelrated to zero energy!")
t += step_dz / (myrel.c * myrel.beta(dE, mass)) t += step_dz / (relativity.c * relativity.beta(dE, mass))
if dE > Emax: if dE > Emax:
Emax = dE Emax = dE
phase_max = phase * d_phase phase_max = phase * d_phase
...@@ -105,7 +105,7 @@ def TTF_beta(field_1d, step_dz, freq, mass): ...@@ -105,7 +105,7 @@ def TTF_beta(field_1d, step_dz, freq, mass):
beta_out = 1 beta_out = 1
TTFb[0, :] = np.arange(beta_in, beta_out, (beta_out - beta_in) / 50) TTFb[0, :] = np.arange(beta_in, beta_out, (beta_out - beta_in) / 50)
for bt in TTFb[0, :]: for bt in TTFb[0, :]:
dummy = Field_TTF(field_1d, step_dz, freq, myrel.energy(bt, mass), mass, False) dummy = Field_TTF(field_1d, step_dz, freq, relativity.energy(bt, mass), mass, False)
TTFb[1, i] = dummy[0] TTFb[1, i] = dummy[0]
i += 1 i += 1
return TTFb return TTFb
"""
set of functions for calculating the transit time factor and the input phase resulting in max energy gain in a field map
-- M. Eshraqi 2018 July 11
"""
import Batch_Convert_and_Cut_Binary_Fieldmap as bccb
import SP_Relativity as myrel
import numpy as np
def field_on_axis(Field, fieldmap_dim, Nz, Nrx, Ny):
"""
Take a 2D or 3D fieldmap as a 1D numpy array (as from TraceWin) in MV/m,
dimension of the fieldmap,
number of steps (No points - 1) in z direction,
number of steps in x/r direction, and
number of steps in the y direction (for 3D fieldmap) and
returns the field on-axis in the z direction
example:
field_on_axis(Field, 3, 100, 10, 10)
"""
if fieldmap_dim == 2:
Fieldmapmatrix = np.reshape(Field, (int(Nz + 1), int(Nrx + 1)))
midpoint = int(Nrx / 2)
elif fieldmap_dim == 3:
Fieldmapmatrix = np.reshape(Field, (int(Nz + 1), int((Nrx + 1) * (Ny + 1))))
midpoint = int(Nrx * Ny / 2)
# print(midpoint)
return Fieldmapmatrix[:, midpoint]
def Field_TTF(field_1d, step_dz, freq, E_in, mass, RetMaxPhase):
"""
takes a 1D field on axis along the acceleration axis in MV/m,
the mesh size (length of each step),
frequency in MHz,
input energy in MeV,
mass in MeV and
RetMaxPhase as boolean
returns the TTF and at the given energy and
if RetMaxPhase==True returns also input phase resulting in max energy gain
"""
omega = freq * 1e6 * 2 * np.pi
TTF_Phase = [0] * 2
dE = E_in
Emax = E_in
phase_max = 0
counter = 0
phase_start = 0
phase_end = 360
d_phase = 90
for iteration in np.arange(1, 10):
for phase in np.arange(int(phase_start / d_phase), int(phase_end / d_phase)):
counter += 1
t = 0
dE = E_in
for fval in field_1d:
dE += fval[0] * step_dz * np.cos(omega * t + d_phase * np.radians(phase))
# print(dE)
if dE < 0:
print("Please adjust the field level, particle decelrated to zero energy!")
t += dz / (myrel.c() * myrel.beta(dE, mass))
if dE > Emax:
Emax = dE
# print('Emax', Emax)
phase_max = phase * d_phase
phase_start = phase_max - d_phase
phase_end = phase_max + d_phase
d_phase = d_phase / 4
if d_phase * 4 < 0.1:
break
# d_phase*=4
# print(counter, d_phase, phase_max, (Emax-E_in)/Field_integral(field_1d, dz))
TTF_Phase[0] = (Emax - E_in) / Field_integral(field_1d, dz)
if RetMaxPhase:
TTF_Phase[1] = phase_max
return TTF_Phase
def Field_integral(field_1d, dz):
"""
takes a 1D field on axis along the acceleration axis in MV/m,
the mesh size (length of each step) and
returns the rectangular integral of field
"""
field_int = 0
for fval in field_1d:
# print(abs(fval[0]))
field_int += abs(fval[0])
return dz * field_int
def TTF_beta(field_1d, step_dz, freq, mass):
"""
takes a 1D field on axis along the acceleration axis in MV/m,
the mesh size (length of each step),
freq in MHz, and mass of particle in MeV and
returns a 2D array containig the TTF vs. beta. [Not very fast]
example
TTFarray = TTF_beta(one_d_field_on_axis, step_z, 704.42, 938)
beta_list = TTFarray[0]
TTF_list = TTFarray[1]
"""
TTFb = np.zeros((2, 50))
i = 0
beta_in = 0.2
beta_out = 1
TTFb[0, :] = np.arange(beta_in, beta_out, (beta_out - beta_in) / 50)
for bt in TTFb[0, :]:
dummy = Field_TTF(field_1d, step_dz, freq, myrel.energy(bt, mass), mass, False)
TTFb[1, i] = dummy[0]
i += 1
return TTFb
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# #
# E X A M P L E S #
# #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
Header, Field = bccb.read_binary_fieldmap("Data/FM/HB_W_coupler.edz", 3)
fz = field_on_axis(Field, 3, Header[0], Header[2], Header[5])
z = np.arange(0, int(Header[0] + 1))
dz = Header[1] / Header[0]
z_points = dz * z
print(
"Int_E:", Field_integral(fz, dz), "[TTF, Phase_Max_Energy]: ", Field_TTF(fz, dz, 704.42, 700, 938, True),
)
TTFb = TTF_beta(fz, dz, 704.42, 938)
# plot(z_points, fz);
# plot(TTFb[0,:], 50*TTFb[1,:]);
source diff could not be displayed: it is too large. Options to address this: view the blob.
...@@ -55,7 +55,7 @@ err_comm_name = "MY_ERROR_COMMAND" ...@@ -55,7 +55,7 @@ err_comm_name = "MY_ERROR_COMMAND"
err_comm_typ = "ERROR_CAV_NCPL_STAT" # I know it's redundant but we also need this for now... err_comm_typ = "ERROR_CAV_NCPL_STAT" # I know it's redundant but we also need this for now...
err_comm_para = [0, 0, 0, 0, 0, 0, 0, 0, 0] # This also works: err_comm_para=[0]*100 err_comm_para = [0, 0, 0, 0, 0, 0, 0, 0, 0] # This also works: err_comm_para=[0]*100
err_comm = lib_tw.ERROR_CAV_NCPL_STAT(err_comm_name, err_comm_typ, err_comm_para) err_comm = lib_tw.lib_tw_elem.ERROR_CAV_NCPL_STAT(err_comm_name, err_comm_typ, err_comm_para)
print("This is how it looks in the TraceWin syntax:") print("This is how it looks in the TraceWin syntax:")
print(err_comm.get_tw(), "\n") print(err_comm.get_tw(), "\n")
......
from ess import TTF from ess import ttf, fieldmap
import numpy as np import numpy as np
Header, Field = TTF.bccb.read_binary_fieldmap("Data/FM/HB_W_coupler.edz", 3) Header, Field = fieldmap.read_fieldmap("Data/FM/HB_W_coupler.edz", 3, "b")
fz = TTF.field_on_axis(Field, 3, Header[0], Header[2], Header[5]) fz = ttf.field_on_axis(Field, 3, Header[0], Header[2], Header[5])
z = np.arange(0, int(Header[0] + 1)) z = np.arange(0, int(Header[0] + 1))
dz = Header[1] / Header[0] dz = Header[1] / Header[0]
z_points = dz * z z_points = dz * z
print( print(
"Int_E:", TTF.Field_integral(fz, dz), "[TTF, Phase_Max_Energy]: ", TTF.Field_TTF(fz, dz, 704.42, 700, 938, True), "Int_E:", ttf.Field_integral(fz, dz), "[TTF, Phase_Max_Energy]: ", ttf.Field_TTF(fz, dz, 704.42, 700, 938, True),
) )
TTFb = TTF.TTF_beta(fz, dz, 704.42, 938) TTFb = ttf.TTF_beta(fz, dz, 704.42, 938)
# plot(z_points, fz); # plot(z_points, fz);
# plot(TTFb[0,:], 50*TTFb[1,:]); # plot(TTFb[0,:], 50*TTFb[1,:]);
...@@ -39,7 +39,7 @@ args = parser.parse_args() ...@@ -39,7 +39,7 @@ args = parser.parse_args()
# A list to be filled depending on arguments given... # A list to be filled depending on arguments given...
binary = None binary = None
for c in ["TraceWin64_noX11", "TraceWin_cli"]: for c in ["TraceWin_noX11", "TraceWin_cli"]:
if shutil.which(c): if shutil.which(c):
binary = c binary = c
break break
......
...@@ -167,9 +167,9 @@ def setup_multi(args): ...@@ -167,9 +167,9 @@ def setup_multi(args):
# Get random seeds from previously run jobs: # Get random seeds from previously run jobs:
seeds = [] seeds = []
for l in open(os.path.join(args.calc_dir, "submit.job"), "r"): for line in open(os.path.join(args.calc_dir, "submit.job"), "r"):
if len(l.strip()) and l.split()[0] == "arguments": if len(line.strip()) and line.split()[0] == "arguments":
seeds.append(l.split()[-1]) seeds.append(line.split()[-1])
head_template = __queue_templates__.get_head_multi_template() head_template = __queue_templates__.get_head_multi_template()
queue_template = __queue_templates__.get_queue_multi_template() queue_template = __queue_templates__.get_queue_multi_template()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment