Skip to content

Commit

Permalink
Includes convergence study for h and p refinement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dark-Elektron committed Sep 20, 2024
1 parent 7e81cd6 commit 8ab9dec
Show file tree
Hide file tree
Showing 6 changed files with 273,519 additions and 74,740 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,12 @@ op_points = {
}
}
wakefield_config = {
'bunch_length': 25,
'wakelength': 50,
'beam_config': {
'bunch_length': 25
},
'wake_config': {
'wakelength': 50
},
'processes': 2,
'rerun': True,
'operating_points': op_points,
Expand Down Expand Up @@ -667,16 +671,24 @@ optimisation_config = {
}
},
},
'wakefield_config': {'n_cells': 1, 'n_modules': 1,
'MROT': 2, 'MT': 4, 'NFS': 10000, 'UBT': 50, 'bunch_length': 25,
'DDR_SIG': 0.1, 'DDZ_SIG': 0.1,
'WG_M': None, 'marker': '',
'uq_config': {
'wakefield_config': {'n_cells': 1, 'n_modules': 1,
'NFS': 10000,
'polarisation': 2,
'beam_config': {
'bunch_length': 25,
},
'wake_config': {
'wakelength': 50
},
'mesh_config': {
'DDR_SIG': 0.1,
'DDZ_SIG': 0.1
},
'uq_config': {
'variables': ['A'],
'objectives': [["ZL", [1, 2, 5]], ["ZT", [2, 3, 4]]],
'delta': [0.05],
'processes': 4,
'distribution': 'gaussian',
'method': ['Quadrature', 'Stroud3'],
'cell_type': 'mid-cell',
'cell complexity': 'simplecell'
Expand Down
51 changes: 34 additions & 17 deletions cavsim2d/analysis/wakefield/abci_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ def cavity(self, no_of_cells, no_of_modules,

# defaults
RDRIVE, ISIG = 5e-3, 5
LCRBW = 'F'
LCRBW = 'F' # counter-rotating beam
ZSEP = 0.0
BSEP = 0
NBUNCH = 1
BETA = 1
LMATPR = 'F'
LPRW, LPPW, LSVW, LSVWA, LSVWT, LSVWL, LSVF = 'T', 'T', 'T', 'F', 'T', 'T', 'F'
LSAV, LCPUTM = 'F', 'F'
LCBACK = 'T'
LPLE = 'F'
NSHOT = 7
NSHOT = 40

# unpack kwargs
for key, value in kwargs.items():
Expand Down Expand Up @@ -73,6 +76,20 @@ def cavity(self, no_of_cells, no_of_modules,
if 'nshot' in value['save_fields'].keys():
NSHOT = value['save_fields']['nshot']

if 'wake' in value.keys():
if 'counter_rotating'in value['wake'].keys():
LCRBW = 'T'
if 'separation' in value['wake']['counter_rotating'].keys():
ZSEP = value['wake']['counter_rotating']['separation']

if 'beam' in value.keys():
if 'beam_offset' in value['beam'].keys():
RDRIVE = value['beam']['beam_offset']
if 'nbunch' in value['beam'].keys():
NBUNCH = value['beam']['nbunch']
if 'separation' in value['beam'].keys():
BSEP = value['beam']['separation']

# Adding parameter arguments here for testing purposes # fid, fileID
self.fid = f'{fid}'

Expand Down Expand Up @@ -155,7 +172,7 @@ def cavity(self, no_of_cells, no_of_modules,
L_all_increment = 0
self.L_all = 0
with open(fname, 'w') as f:
f.write(f' &FILE LSAV = .{LSAV}., ITEST = 0, LREC = .F., LCPUTM = .{LCPUTM}. &END \n')
f.write(f' &FILE LSAV = {LSAV}, ITEST = 0, LREC = F, LCPUTM = {LCPUTM} &END \n')
f.write(' SAMPLE INPUT #1 A SIMPLE CAVITY STRUCTURE \n')
f.write(' &BOUN IZL = 3, IZR = 3 &END \n')
f.write(' &MESH DDR = {}, DDZ = {} &END \n'.format(mesh_DDR, mesh_DDZ))
Expand Down Expand Up @@ -285,16 +302,16 @@ def cavity(self, no_of_cells, no_of_modules,
f.write('0 0 \n')
f.write('9999. 9999. \n')

f.write(f' &BEAM SIG = {SIG}, ISIG = {ISIG}, RDRIVE = {RDRIVE}, MROT = {MROT} &END \n')
f.write(f' &BEAM SIG = {SIG}, ISIG = {ISIG}, RDRIVE = {RDRIVE}, MROT = {MROT}, NBUNCH = {NBUNCH}, BSEP = {BSEP} &END \n')
# f.write(' &BEAM SIG = {}, MROT = {}, RDRIVE = {} &END \n'.format(SIG, MROT, beam_offset))
f.write(f' &TIME MT = {int(MT)}, NSHOT={NSHOT} &END \n')
f.write(f' &WAKE UBT = {int(UBT)}, LCRBW = .{LCRBW}., LCBACK = .{LCBACK}. &END \n') # , NFS = {NFS}
# f.write(' &WAKE UBT = {}, LCHIN = .F., LNAPOLY = .F., LNONAP = .F. &END \n'.format(UBT, wake_offset))
f.write(f' &WAKE UBT = {int(UBT)}, LCRBW = {LCRBW}, LCBACK = {LCBACK}, LCRBW = {LCRBW}, ZSEP = {ZSEP} &END \n') # , NFS = {NFS}
# f.write(' &WAKE UBT = {}, LCHIN = F, LNAPOLY = F, LNONAP = F &END \n'.format(UBT, wake_offset))
# f.write(' &WAKE R = {} &END \n'.format(wake_offset))
f.write(f' &PLOT LCAVIN = .T., LCAVUS = .F., LPLW = .T., LFFT = .T., LSPEC = .T., '
f'LINTZ = .F., LPATH = .T., LPLE = .{LPLE}., LPLC=.F. &END \n')
f.write(f' &PRIN LMATPR = .{LMATPR}., LPRW = .{LPRW}., LPPW = .{LPPW}., LSVW = .{LSVW}., '
f'LSVWA = .{LSVWA}., LSVWT = .{LSVWT}., LSVWL = .{LSVWL}., LSVF = .{LSVF}. &END\n')
f.write(f' &PLOT LCAVIN = T, LCAVUS = F, LPLW = T, LFFT = T, LSPEC = T, '
f'LINTZ = F, LPATH = T, LPLE = {LPLE}, LPLC= F &END \n')
f.write(f' &PRIN LMATPR = {LMATPR}, LPRW = {LPRW}, LPPW = {LPPW}, LSVW = {LSVW}, '
f'LSVWA = {LSVWA}, LSVWT = {LSVWT}, LSVWL = {LSVWL}, LSVF = {LSVF} &END\n')
f.write('\nSTOP\n')

exe_path = os.path.join(parentDir / Path(fr'solvers/ABCI/ABCI.exe'))
Expand Down Expand Up @@ -453,7 +470,7 @@ def cavity_flattop(self, no_of_cells, no_of_modules,
self.L_all = 0
# print(fname)
with open(fname, 'w') as f:
f.write(f' &FILE LSAV = .{LSAV}., ITEST = 0, LREC = .F., LCPUTM = .{LCPUTM}. &END \n')
f.write(f' &FILE LSAV = {LSAV}, ITEST = 0, LREC = F, LCPUTM = {LCPUTM} &END \n')
f.write(' SAMPLE INPUT #1 A SIMPLE CAVITY STRUCTURE \n')
f.write(' &BOUN IZL = 3, IZR = 3 &END \n')
f.write(' &MESH DDR = {}, DDZ = {} &END \n'.format(mesh_DDR, mesh_DDZ))
Expand Down Expand Up @@ -613,13 +630,13 @@ def cavity_flattop(self, no_of_cells, no_of_modules,
f.write(f' &BEAM SIG = {SIG}, ISIG = {ISIG}, RDRIVE = {RDRIVE}, MROT = {MROT} &END \n')
# f.write(' &BEAM SIG = {}, MROT = {}, RDRIVE = {} &END \n'.format(SIG, MROT, beam_offset))
f.write(f' &TIME MT = {int(MT)}, NSHOT={NSHOT} &END \n')
f.write(f' &WAKE UBT = {int(UBT)}, LCRBW = .{LCRBW}., LCBACK = .{LCBACK}. &END \n') # , NFS = {NFS}
# f.write(' &WAKE UBT = {}, LCHIN = .F., LNAPOLY = .F., LNONAP = .F. &END \n'.format(UBT, wake_offset))
f.write(f' &WAKE UBT = {int(UBT)}, LCRBW = {LCRBW}, LCBACK = {LCBACK} &END \n') # , NFS = {NFS}
# f.write(' &WAKE UBT = {}, LCHIN = F, LNAPOLY = F, LNONAP = F &END \n'.format(UBT, wake_offset))
# f.write(' &WAKE R = {} &END \n'.format(wake_offset))
f.write(f' &PLOT LCAVIN = .T., LCAVUS = .F., LPLW = .T., LFFT = .T., LSPEC = .T., '
f'LINTZ = .F., LPATH = .T., LPLE = .{LPLE}., LPLC=.F. &END \n')
f.write(f' &PRIN LMATPR = .{LMATPR}., LPRW = .{LPRW}., LPPW = .{LPPW}., LSVW = .{LSVW}., '
f'LSVWA = .{LSVWA}., LSVWT = .{LSVWT}., LSVWL = .{LSVWL}., LSVF = .{LSVF}. &END\n')
f.write(f' &PLOT LCAVIN = T, LCAVUS = F, LPLW = T, LFFT = T, LSPEC = T, '
f'LINTZ = F, LPATH = T, LPLE = {LPLE}, LPLC=F &END \n')
f.write(f' &PRIN LMATPR = {LMATPR}, LPRW = {LPRW}, LPPW = {LPPW}, LSVW = {LSVW}, '
f'LSVWA = {LSVWA}, LSVWT = {LSVWT}, LSVWL = {LSVWL}, LSVF = {LSVF} &END\n')
f.write('\nSTOP\n')

exe_path = os.path.join(parentDir / Path(fr'solvers/ABCI/ABCI.exe'))
Expand Down
80 changes: 76 additions & 4 deletions cavsim2d/cavity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,12 @@ def __init__(self, n_cells, mid_cell, end_cell_left=None, end_cell_right=None, b
name
"""

if isinstance(mid_cell, dict):
# then mid cell parameter is a shape
end_cell_left = mid_cell['OC']
end_cell_right = mid_cell['OC_R']
mid_cell = mid_cell['IC']

self.convergence_df_data = None
self.convergence_df = None
self.uq_weights = None
Expand Down Expand Up @@ -2333,7 +2339,6 @@ def plot_contour_for_frame(data, frame_key, ax):
ax.set_xlabel('X-axis (m)')
ax.set_ylabel('Y-axis (m)')
ax.set_title(f'Contour Plot for {frame_key}')
ax.grid(True)

def animate_frames(data):
fig, ax = plt.subplots(figsize=(18, 4))
Expand Down Expand Up @@ -2536,6 +2541,32 @@ def plot(self, what, ax=None, scale_x=1, **kwargs):
ax.set_ylabel(r"$Z_{\perp} ~[\mathrm{k\Omega/m}]$")
return ax

if what.lower() == 'wpl':
if ax:
x, y, _ = self.abci_data['Long'].get_data('Wake Potentials')
ax.plot(x, y, lw=3, label=fr'{self.name} (Longitudinal wake potentials)', **kwargs)
else:
fig, ax = plt.subplots(figsize=(12, 4))
ax.margins(x=0)
x, y, _ = self.abci_data['Long'].get_data('Wake Potentials')
ax.plot(x, y, lw=3, label=fr'{self.name} (Longitudinal wake potentials)', **kwargs)

ax.set_xlabel('Distance from Bunch Head S [m]')
ax.set_ylabel(r"Scaled Wake Potentials $W (S)$ [V/pC]")
return ax
if what.lower() == 'wpt':
if ax:
x, y, _ = self.abci_data['Trans'].get_data('Wake Potentials')
ax.plot(x, y, label=fr'{self.name} (Transversal wake potentials)', lw=3, **kwargs)
else:
fig, ax = plt.subplots(figsize=(12, 4))
ax.margins(x=0)
x, y, _ = self.abci_data['Trans'].get_data('Wake Potentials')
ax.plot(x, y, label=fr'{self.name} (Transversal wake potentials)', lw=3, **kwargs)
ax.set_xlabel('Distance from Bunch Head S [m]')
ax.set_ylabel(r"Scaled Wake Potentials $W (S)$ [V/pC/m]")
return ax

if what.lower() == 'convergence':
try:
if ax:
Expand Down Expand Up @@ -2592,7 +2623,10 @@ def _plot_convergence(self, ax):
def define_operating_points(self, op):
self.operating_points = op

def inspect(self, cell_type='mid-cell', variation=0.2):
def inspect(self, cell_type='mid-cell', variation=0.2, tangent_check=True):

fig, ax = plt.subplots(figsize=(12, 6))
ax.set_aspect('equal')

if cell_type == 'mid-cell':
cell = self.shape['IC']
Expand Down Expand Up @@ -2657,9 +2691,10 @@ def plot_cavity_geometry_flattop(A, B, a, b, Ri, L, Req, l):
# Define the function that plots the graph
def plot_cavity_geometry(A, B, a, b, Ri, L, Req):
cell = np.array([A, B, a, b, Ri, L, Req])
ax.clear()
write_cavity_geometry_cli(cell, cell, cell, BP='none', n_cell=1,
tangent_check=True, lw=1,
plot=True,
tangent_check=tangent_check, lw=3,
plot=True, ax=ax,
ignore_degenerate=True)
# Update the sum display
sum_label.value = f'Sum of A + a: {A + a:.2f}, L: {L}, delta: {A + a - L}'
Expand Down Expand Up @@ -3626,8 +3661,45 @@ def plot(self, what, ax=None, scale_x=None, **kwargs):
ax.margins(x=0)
ax = cav.plot('zt', ax, scale_x=scale_x[ii])

if what.lower() == 'wpl':
if scale_x is None:
scale_x = [1 for _ in self.cavities_list]
else:
if isinstance(scale_x, list):
assert len(scale_x) == len(self.cavities_list), error(
'Length of scale_x must be same as number of Cavity objects.')
else:
scale_x = [scale_x for _ in self.cavities_list]

if ax:
ax = cav.plot('wpl', ax, scale_x=scale_x[ii])
else:
fig, ax = plt.subplots(figsize=(12, 4))
ax.margins(x=0)
ax = cav.plot('wpl', ax, scale_x=scale_x[ii])

if what.lower() == 'wpt':
if scale_x is None:
scale_x = [1 for _ in self.cavities_list]
else:
if isinstance(scale_x, list):
assert len(scale_x) == len(self.cavities_list), error(
'Length of scale_x must be same as number of Cavity objects.')
else:
scale_x = [scale_x for _ in self.cavities_list]

if ax:
ax = cav.plot('wpt', ax, scale_x=scale_x[ii])
else:
fig, ax = plt.subplots(figsize=(12, 4))
ax.margins(x=0)
ax = cav.plot('wpt', ax, scale_x=scale_x[ii])

if what.lower() == 'convergence':
ax = cav.plot('convergence', ax)
if 'logy' in kwargs:
if kwargs['logy']:
ax.set_yscale('log')
return ax

def set_cavities_field(self):
Expand Down
6 changes: 3 additions & 3 deletions cavsim2d/utils/shared_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ def write_cavity_geometry_cli(IC, OC, OC_R, BP, n_cell, scale=1, ax=None, bc=Non
L_bp_l = 0.000
L_bp_r = 0.000

step = 0.005
step = 0.0005

# calculate shift
shift = (L_bp_r + L_bp_l + L_el + (n_cell - 1) * 2 * L_m + L_er) / 2
Expand Down Expand Up @@ -1466,7 +1466,7 @@ def write_cavity_geometry_cli(IC, OC, OC_R, BP, n_cell, scale=1, ax=None, bc=Non
error('Check file path:: ', e)

# append start point
geo.append([start_point[1], start_point[0]])
# geo.append([start_point[1], start_point[0]])

if bc:
# draw right boundary condition
Expand All @@ -1491,7 +1491,7 @@ def write_cavity_geometry_cli(IC, OC, OC_R, BP, n_cell, scale=1, ax=None, bc=Non
shift_to_center = n_cell * L_m + L_bp_r

top = ax.plot(geo[:, 1] - shift_left + shift_to_center, geo[:, 0], **kwargs)
# bottom = ax.plot(geo[:, 1] - shift_left + shift_to_center, -geo[:, 0], c=top[0].get_color(), **kwargs)
bottom = ax.plot(geo[:, 1] - shift_left + shift_to_center, -geo[:, 0], c=top[0].get_color(), **kwargs)

# plot legend wthout duplicates
handles, labels = plt.gca().get_legend_handles_labels()
Expand Down
2,422 changes: 2,195 additions & 227 deletions notebooks/convergence_study.ipynb

Large diffs are not rendered by default.

345,672 changes: 271,191 additions & 74,481 deletions notebooks/misc.ipynb

Large diffs are not rendered by default.

0 comments on commit 8ab9dec

Please sign in to comment.