Skip to content

Commit 60ac597

Browse files
author
alexander
committed
generation of beams correction
1 parent 73bd664 commit 60ac597

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
from ..interface import *
2+
from .config import *
3+
from ..backend_gorn.beam_gen import Gauss, rGauss
4+
import os
5+
from shutil import copyfile
6+
import matplotlib.pyplot as plt
7+
8+
9+
def printmd(string):
10+
try:
11+
from IPython.display import Markdown, display
12+
display(Markdown(string))
13+
except:
14+
print('### ', string, ' ###')
15+
16+
#-------------------------
17+
# Making directory module
18+
#-------------------------
19+
def makedir(path, name, cfg=config):
20+
path = os.path.join(path, name)
21+
print('Making directory... ', end='')
22+
try:
23+
os.makedirs(path)
24+
print('done.')
25+
except:
26+
print('seems like the directory already exists.')
27+
28+
with open(os.path.join(path, 'lcode.cfg'), "w") as text_file:
29+
print('Writing lcode.cfg... ', end='')
30+
text_file.write(cfg)
31+
print('done')
32+
return path
33+
34+
#------------------------------
35+
# Simulation setting up module
36+
#------------------------------
37+
def setup_simulation(lcode, time_step, window_width, window_length):
38+
lcode.set_parameter('time-step', time_step)
39+
print(type(window_width))
40+
window_width = round(window_width, 2)
41+
lcode.set_parameter('window-width', window_width)
42+
window_length = float(window_length)
43+
lcode.set_parameter('window-length', window_length)
44+
print('')
45+
46+
#----------------------
47+
# Making plasma module
48+
#----------------------
49+
def plasma_zshape(x, n, form='L'):
50+
zshape = ''
51+
dxs = x[1:] - x[:-1]
52+
arr = np.array([dxs, n[:-1], n[1:]]).T
53+
zshape += 'plasma-zshape = """\n'
54+
for dx, n_start, n_end in arr:
55+
zshape += '%f %f %s %f\n' % (dx, n_start, form, n_end)
56+
zshape +='"""'
57+
return zshape
58+
59+
def make_plasma(lcode, n_plasma, plasma_length, plasma_radius, n_profile):
60+
print("# PLASMA")
61+
plasma_length = round(plasma_length)
62+
lcode.set_parameter('time-limit', plasma_length + 0.1)
63+
64+
plasma_radius = round(plasma_radius, 2)
65+
lcode.set_parameter('plasma-width', plasma_radius)
66+
67+
# z-shape
68+
n0 = n_plasma
69+
z = np.arange(0, plasma_length, lcode.get_parameter('time-step'))
70+
nz = np.array([n0*n_profile(x) for x in z])
71+
plt.title('density profile')
72+
plt.plot(z*lcode.cwp/100, lcode.n*nz)
73+
plt.xlabel(r'$z$ [m]')
74+
plt.ylabel(r'$n$ [cm-3]')
75+
print('Writing pzshape.txt... ', end='')
76+
with open(os.path.join(lcode.path, 'pzshape.txt'), 'w') as f:
77+
f.write(plasma_zshape(z, nz))
78+
print('done\n')
79+
80+
#------------------------------
81+
# Beamfile construction module
82+
#------------------------------
83+
def Q2I(Q, sigma_z): # SI (returns Amps)
84+
return c/100. * Q / np.sqrt(2*np.pi) / sigma_z
85+
def make_beam(lcode, sigma_z, pos_xi, sigma_r, p0, enspread, emittance, N_particles, q_m=m_MeV/M_MeV, q_e=1):
86+
print("# BEAM")
87+
# xi
88+
xi_shape = Gauss(pos_xi, sigma_z)
89+
# r
90+
r_shape = rGauss(sigma_r)
91+
# pz
92+
pz_shape = Gauss(p0, enspread)
93+
# angle
94+
angspread = emittance / (p0 * q_m / abs(q_e) * sigma_r)
95+
#print('%e' % angspread)
96+
ang_shape = Gauss(0, angspread)
97+
# current
98+
Ipeak_A = Q2I(N_particles*e*q_e/3e9, sigma_z*lcode.cwp/100)
99+
Ipeak_kA = Ipeak_A/1000
100+
print('Peak current {:.2f} A'.format(Ipeak_A))
101+
lcode.beam = lcode.make_beam(xi_shape, r_shape, pz_shape, ang_shape, Ipeak_kA, q_m, saveto=lcode.path)
102+
print('Writing beamfile.bit...', end='')
103+
with open(os.path.join(lcode.path, 'beamfile.bit'), 'w') as f:
104+
f.write('0.0')
105+
print('done.\n')
106+
107+
#--------------------
108+
# Making task module
109+
#--------------------
110+
def make_task(lcode, name, N_nodes, cluster='matrosov'):
111+
if cluster == 'matrosov':
112+
task = """#!/bin/bash
113+
#
114+
#PBS -N {}
115+
#PBS -l nodes={}:intel:ppn=36,pvmem=30000mb,walltime=200:00:00
116+
cd $PBS_O_WORKDIR
117+
/share/apps/bin/mpiexec -perhost 36 lcode lcode.cfg pzshape.txt""".format(name, N_nodes)
118+
print('Making task.sh... ', end='')
119+
with open(os.path.join(lcode.path, 'task.sh'), 'w') as f:
120+
f.write(task)
121+
print('done.\n')
122+
elif cluster == 'matrosov2':
123+
task = """#!/bin/bash
124+
#
125+
#PBS -N {}
126+
#PBS -l nodes={}:amd:ppn=32,pvmem=30000mb,walltime=200:00:00
127+
cd $PBS_O_WORKDIR
128+
/share/apps/bin/mpiexec -perhost 32 lcode2 lcode.cfg pzshape.txt""".format(name, N_nodes)
129+
print('Making task.sh... ', end='')
130+
with open(os.path.join(lcode.path, 'task.sh'), 'w') as f:
131+
f.write(task)
132+
print('done.\n')
133+
elif cluster == 'condor':
134+
print('Making condor directory... ', end='')
135+
try:
136+
os.makedirs(os.path.join(lcode.path, 'condor'))
137+
print('done.')
138+
except:
139+
print('seems like the directory already exists.')
140+
print('Copying condor files... ', end='')
141+
copyfile('condor/job.py', os.path.join(lcode.path, 'condor'))
142+
copyfile('condor/job_start.sh', os.path.join(lcode.path, 'condor/job_%s.sh' % name))
143+
print('done.\n')

setsim/setsim.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def make_beam(lcode, sigma_z, pos_xi, sigma_r, p0, enspread, emittance, N_partic
9191
# pz
9292
pz_shape = Gauss(p0, enspread)
9393
# angle
94-
angspread = emittance / (p0 * q_m * sigma_r)
94+
angspread = emittance / (p0 * q_m / abs(q_e) * sigma_r)
9595
#print('%e' % angspread)
9696
ang_shape = Gauss(0, angspread)
9797
# current

0 commit comments

Comments
 (0)