Skip to content

Commit

Permalink
Merge remote-tracking branch 'kparasch/ecloud' into release/0.21.5
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlopaciuk committed Dec 9, 2024
2 parents 16abe38 + aad6a0f commit fe41c45
Show file tree
Hide file tree
Showing 15 changed files with 6,362 additions and 72 deletions.
4 changes: 4 additions & 0 deletions test_data/pyecloud_pinch/Pinch_creation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.h5
*.mat
MP_state*
*.txt
4 changes: 4 additions & 0 deletions test_data/pyecloud_pinch/Pinch_creation/000_buildup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from PyECLOUD.buildup_simulation import BuildupSimulation

sim = BuildupSimulation()
sim.run()
131 changes: 131 additions & 0 deletions test_data/pyecloud_pinch/Pinch_creation/001_pinch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import json
import scipy.io
from scipy.constants import c,e
import matplotlib.pyplot as plt
import numpy as np
# from cmcrameri import cm
from rich.progress import Progress
import glob
import os

import sys
import filemanager as exfm

from PyECLOUD.buildup_simulation import BuildupSimulation
import argparse

if os.path.exists("Pinch.h5"):
os.remove("Pinch.h5")

zeta_max = 0.2

parser = argparse.ArgumentParser()
parser.add_argument("--ecloud", nargs="?", type=str, default="ecloud.q3r1.ir1.0")
args = parser.parse_args()

ecloud = args.ecloud

mod_factor = 1

out_pinch = f"Pinch.h5"

mp_states = []
mp_state_files = ["MP_state_0"]
for name in mp_state_files:
mp_system = scipy.io.loadmat(name)
mp_system['nel_mp'] = mp_system['nel_mp']/len(mp_state_files)
mp_states.append(mp_system)

t_min = 2.5e-9 - zeta_max / c
t_max = 2.5e-9 + zeta_max / c
t_end_sim = t_max + 0.02 / c
total_N_particles = sum([mp_system["N_mp"][0][0] for mp_system in mp_states])
total_N_particles *= 2 #put more particles to max allowed
total_N_particles += 10000 #put more particles to max allowed

PyPICmode = 'ShortleyWeller_WithTelescopicGrids'
target_grid = {'x_min_target' : -12e-3, 'x_max_target' : 12e-3,
'y_min_target' : -5e-3, 'y_max_target' : 5e-3,
'Dh_target' : 1.e-4}
f_telescope = 0.5
N_nodes_discard = 10
N_min_Dh_main = 50

sim = BuildupSimulation(extract_sey=False, N_mp_max=total_N_particles, PyPICmode=PyPICmode,
target_grid=target_grid, f_telescope=f_telescope,
N_nodes_discard=N_nodes_discard, N_min_Dh_main=N_min_Dh_main,
init_unif_edens_flag=0, Dt_sc=None)

sim = BuildupSimulation(extract_sey=False, N_mp_max=total_N_particles,
init_unif_edens_flag=0, Dt_sc=None)


sim.cloud_list[0].MP_e.init_from_dict(mp_states[0])
sim.cloud_list[0].MP_e.nel_mp_ref *= len(mp_states)
prev_mp = sim.cloud_list[0].MP_e.N_mp
for mp_system in mp_states[1:]:
sim.cloud_list[0].MP_e.add_from_file(mp_system)
now_mp = sim.cloud_list[0].MP_e.N_mp
print(now_mp, mp_system["N_mp"][0][0], now_mp - prev_mp)
prev_mp = now_mp

N_mp = sim.cloud_list[0].MP_e.N_mp
max_nel_mp = np.max(sim.cloud_list[0].MP_e.nel_mp[:N_mp])
sim.cloud_list[0].MP_e.nel_mp_split = 3*(max_nel_mp)

print(sim.cloud_list[0].MP_e.nel_mp_split)

print("Start timestep iter")

## simulation
def time_step(sim, t_end_sim=None):
beamtim = sim.beamtim
if t_end_sim is not None and beamtim.tt_curr is not None:
if beamtim.tt_curr >= t_end_sim:
print("Reached user defined t_end_sim --> Ending simulation")
return 1

beamtim.next_time_step()

if sim.flag_presence_sec_beams:
for sec_beam in sim.sec_beams_list:
sec_beam.next_time_step()

sim.sim_time_step(force_reinterp_fields_at_substeps=True, skip_MP_cleaning=True, skip_MP_regen=True)

if beamtim.flag_new_bunch_pass:
print(
"**** Done pass_numb = %d/%d\n"
% (beamtim.pass_numb, beamtim.N_pass_tot)
)
return 0



xg = sim.spacech_ele.xg
yg = sim.spacech_ele.yg
zg = []

ii = 0
jj = 0

with Progress() as progress:
task = progress.add_task("PyECLOUD tracking", total=t_end_sim)

while not time_step(sim, t_end_sim=t_end_sim):
# print(sim.cloud_list[0].MP_e.N_mp/total_N_particles)
ii += 1
tt = sim.beamtim.tt_curr
progress.update(task, completed=tt)
if tt > t_min and tt < t_max:
if ii%mod_factor == 0.:
# print(jj, ii, -c*(tt-2.5e-9))
zg.append(-c*(tt-2.5e-9))
exfm.dict_to_h5({'phi' : sim.spacech_ele.phi, 'rho' : sim.spacech_ele.rho}, out_pinch, group='slices/slice%d'%jj, readwrite_opts='a')
jj += 1

grid_dict = {"xg" : np.array(xg),
"yg" : np.array(yg),
"zg" : np.array(zg),
}
exfm.dict_to_h5(grid_dict, out_pinch, group='grid', readwrite_opts='a')
33 changes: 33 additions & 0 deletions test_data/pyecloud_pinch/Pinch_creation/beam.beam
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# BEAM PARAMETERS
energy_eV = 0.450000e+12
nemittx = 2.00e-6
nemitty = 2.00e-6 #m
Dp_p = 0


# x_beam_pos = 0
# y_beam_pos = 0

beam_field_file = 'computeFD'
Dh_beam_field=1.0e-4
Nx = None
Ny = None
nimag = None
#save_beam_field_file_as = 'beam1field.mat'


b_spac = 25e-9 #s (to be specified also if you load the profile from file -
# it is used as period for clean and save)
fact_beam = 1.200000e+11



flag_bunched_beam = 1 # 1: bunched beam 0:load profile from file

# to be filled in case of bunched beam
sigmaz = 9.000000e-02
t_offs = 2.5e-9
filling_pattern_file = 1*(48*[1.]+7*[0.])
#filling_pattern_file = 2*(3*(48*[1.]+7*[0.])+32*[0.]) + [0.]

beam_long_prof_file = -1
54 changes: 54 additions & 0 deletions test_data/pyecloud_pinch/Pinch_creation/filemanager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
### Adapted from https://github.com/PyECLOUD/myfilemanager.py

import h5py
import numpy

class obj_from_dict:
def __init__(self, mydict):
for key in mydict.keys():
setattr(self, key, mydict[key])

def h5_to_dict(filename, group=None):
fid = h5py.File(filename, 'r')
if group == None :
grp = fid
else:
grp = fid[group]
mydict={}
for key in grp.keys():
mydict[key] = grp[key][()]
return mydict

def h5_to_obj(filename, group=None):
return obj_from_dict(h5_to_dict(filename, group=group))

def overwrite(dict_save, filename, group=None, verbose=False):
with h5py.File(filename, 'a') as fid:
if group == None :
grp = fid
else:
grp = fid[group]

for kk in dict_save.keys():
if verbose: print('Overwriting '+kk)
grp[kk][...] = dict_save[kk]

def dict_to_h5(dict_save, filename, compression_opts=4, group=None, readwrite_opts='w', verbose=False):
with h5py.File(filename, readwrite_opts) as fid:
if group == None :
grp = fid
else:
grp = fid.create_group(group)

for kk in dict_save.keys():
if verbose: print('Writing '+kk)
if isinstance(dict_save[kk], numpy.ndarray):
dset = grp.create_dataset(kk, shape=dict_save[kk].shape, dtype=dict_save[kk].dtype, compression='gzip', compression_opts=compression_opts)
dset[...] = dict_save[kk]
else:
grp[kk] = dict_save[kk]

def print_h5(filename):
def print_this(x,y): print(x,y)
with h5py.File(filename,'r') as fid:
fid.visititems(print_this)
51 changes: 51 additions & 0 deletions test_data/pyecloud_pinch/Pinch_creation/machine_parameters.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# MACHINE PARAMETERS

# Choose chamb_type='ellip'/ 'polyg'
chamb_type = 'polyg_cython'
# flag_assume_convex = False

x_aper = 2.300000e-02
y_aper = 1.800000e-02

#For polygonal chamber
filename_chm = 'LHC_chm_ver.mat'

# Choose track_method= 'StrongBdip' / 'StrongBgen'/ 'Boris'
track_method= 'BorisMultipole'
# N_sub_steps = 5

B_multip = [0., 12.1]
B_skew = [0., 0.]

betafx = 80.
betafy = 80.

# gas ionization parameters
gas_ion_flag = 0
P_nTorr=12.;
sigma_ion_MBarn=2.;
Temp_K=300.;
unif_frac=0.1
E_init_ion=0.1; #in eV

# photoemission parameters
photoem_flag = 0
inv_CDF_refl_photoem_file = 'unif_no_file'
k_pe_st=0.0001233
refl_frac=1.
alimit=10.*pi/180.
e_pe_sigma=5. #in eV
e_pe_max=7. #in eV
x0_refl=0. #no effect since uniform seeding
y0_refl=0. #no effect since uniform seeding
out_radius = .1
phem_resc_fac=0.995


# initial uniform electron density
init_unif_edens_flag=1
init_unif_edens=1e8
E_init_unif_edens = 1.
# init_unif_flag=1
# Nel_init_unif=1e8
# E_init_unif = 1.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# secondary emission model
Emax = 332.;
del_max = 1.50000
R0 = 0.7
switch_model=0

# hilleret model for sec. emission
E_th = 35.;
sigmafit =1.0828;
mufit = 1.6636;

switch_no_increase_energy = 0
thresh_low_energy = -1

scrub_en_th = 20.#eV
secondary_angle_distribution = 'cosine_3D'
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# SIMULATION PARAMETERS

machine_param_file='machine_parameters.input'
secondary_emission_parameters_file='secondary_emission_parameters.input'
beam_parameters_file='beam.beam'

#secondary_beams_file_list = ['beam2.beam']
#additional_clouds_file_list = ['electrons2.cloud']

logfile_path = 'logfile.txt'
progress_path = 'progress.txt'
stopfile = 'progress.stop'

Dt = 25.000000e-12
t_end=1e-9; #s (no effect if log. profile is imported from file)

import numpy as np
dec_fact_out = int(np.round(5 * 25e-12/Dt))
# save_mat_every = 6

lam_th=1.e2 #e-/m
Dx_hist=1.e-3 #m
r_center=2.e-3 #m


Dt_En_hist = 25e-9 #s
Nbin_En_hist= 100
En_hist_max= 1000. #eV

t_ion=100.; #s

N_mp_max=200000; #size of allocated vectors

#Regen parameters

N_mp_regen=400000;
N_mp_regen_low=5000;
N_mp_after_regen=10000;
t_ON_regen_low=0.
fact_split=1.5;
fact_clean=1e-4;
nel_mp_ref_0=1e8*3.14*0.055**2/10000/2.
regen_hist_cut = 1.e-4

N_mp_soft_regen = 100000
N_mp_after_soft_regen = 40000


# Number of bins
Nx_regen=51;#it must be odd!
Ny_regen=51;#it must be odd!
Nvx_regen=51;#it must be odd!
Nvy_regen=101;#it must be odd!
Nvz_regen=51;#it must be odd!

#Sp_ch params
Dt_sc = 0.3e-9
Dh_sc = 0.2e-3
# Dt_sc# = 0.5e-9
# Dh_sc# = 0.1e-3
t_sc_ON=0e-9; #s
sparse_solver = 'klu'

#save_mp_state_time_file = -1
#save_mp_state_time_file = np.arange(88,136,1)*25e-9 # 1x48 bunches
save_mp_state_time_file = np.arange(44,46,1)*25e-9 #

flag_hist_impact_seg = 1

flag_movie = 0 #1/0
flag_sc_movie = 0 #1/0

flag_verbose_file = False
flag_verbose_stdout = False
flag_detailed_MP_info = 0
dec_fac_secbeam_prof=dec_fact_out

el_density_probes = [{'x' : 3.578528e-07, 'y': 6.051342e-03, 'r_obs': 1e-3},{'x' : -1.186982e-06, 'y': -0.006264070337426034, 'r_obs': 1e-3}]
# [\
# {'x' : .007, 'y': .004, 'r_obs': 1e-3},\
# {'x' : .007, 'y': -.004, 'r_obs': 1e-3}]
Loading

0 comments on commit fe41c45

Please sign in to comment.