Skip to content

Commit d509dd1

Browse files
authored
Merge pull request #12 from PyCOMPLETE/develop
v2.4.0
2 parents 4ede264 + e4990dc commit d509dd1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+108
-3337
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# PyPARIS
2+
3+
This package should be tested using the examples in:
4+
5+
https://github.com/PyCOMPLETE/PyPARIS_CoupledBunch_sim_class
6+
7+
https://github.com/PyCOMPLETE/PyPARIS_sim_class

communication_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ def split_float_buffers(megabuffer):
3131

3232

3333
def list_of_strings_2_buffer(strlist):
34-
data = ''.join(map(lambda s:s+';', strlist))+'\n'
34+
data = ''.join(map(lambda s:s+';', strlist))+'\nendbuf\n'
3535
buf_to_send = np.atleast_1d(np.int_(np.array(map(ord, list(data)))))
3636
return buf_to_send
3737

3838
def buffer_2_list_of_strings(buf):
3939
str_received = ''.join(map(unichr, list(buf)))
40-
strlist = list(map(str, str_received.split('\n')[0].split(';')))[:-1]
40+
strlist = list(map(str, str_received.split('\nendbuf\n')[0].split(';')))[:-1]
4141
return strlist
4242

4343

gen_multibunch_beam.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from scipy.constants import c as clight, e as qe
24
import numpy as np
35

@@ -55,3 +57,40 @@ def gen_matched_multibunch_beam(machine, n_macroparticles_per_bunch, filling_pat
5557
bb.slice_info['i_turn'] = 0
5658

5759
return list_bunches
60+
61+
62+
def load_multibunch_beam(dirname, reset_i_turns=True):
63+
import PyPARIS.myfilemanager as mfm
64+
import PyPARIS.communication_helpers as ch
65+
66+
print('Loading the beam from %s'%dirname)
67+
68+
bzero = ch.buffer_2_beam(mfm.dict_of_arrays_and_scalar_from_h5(
69+
dirname+'/bunch0.h5')['bunchbuffer'])
70+
N_bunches_tot_beam = bzero.slice_info['N_bunches_tot_beam']
71+
list_bunches = [bzero]
72+
for ibun in xrange(1, N_bunches_tot_beam):
73+
list_bunches.append(ch.buffer_2_beam(
74+
mfm.dict_of_arrays_and_scalar_from_h5(
75+
dirname+'/bunch%d.h5'%ibun)['bunchbuffer']))
76+
77+
list_bunches = list_bunches[::-1] # We want the last bunch to be in pos 0
78+
if reset_i_turns:
79+
for bb in list_bunches:
80+
bb.slice_info['i_turn'] = 0
81+
82+
return list_bunches
83+
84+
85+
def save_bunch_to_folder(bunch, dirname):
86+
import PyPARIS.myfilemanager as mfm
87+
import PyPARIS.communication_helpers as ch
88+
if not os.path.exists(dirname):
89+
os.makedirs(dirname)
90+
buf = ch.beam_2_buffer(bunch)
91+
bpath = dirname+'/bunch%d.h5'%bunch.slice_info['i_bunch']
92+
print('Saving: ' + bpath)
93+
mfm.dict_to_h5(dict_save={'bunchbuffer':buf},
94+
filename=bpath)
95+
96+

myfilemanager.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,36 @@ def monitorh5_to_dict(filename, key= 'Bunch'):
5959
def monitorh5_to_obj(filename, key= 'Bunch'):
6060
return obj_from_dict(monitorh5_to_dict(filename, key))
6161

62-
def monitorh5list_to_dict(filename_list, key='Bunch', permissive=False):
62+
def monitorh5list_to_dict(filename_list, key='Bunch', flag_transpose=False, permissive=False):
6363
monitor_dict = monitorh5_to_dict(filename_list[0], key=key)
64-
for i_file in xrange(1,len(filename_list)):
64+
for i_file in xrange(1, len(filename_list)):
6565
print('Loading '+filename_list[i_file])
6666
try:
67-
monitor_dict_curr = monitorh5_to_dict(filename_list[i_file])
68-
for kk in monitor_dict.keys():
69-
monitor_dict[kk] = np.array(list(monitor_dict[kk])+list(monitor_dict_curr[kk]))
67+
monitor_dict_curr = monitorh5_to_dict(filename_list[i_file], key=key)
68+
if flag_transpose:
69+
for kk in monitor_dict.keys():
70+
monitor_dict[kk] = np.array(list(monitor_dict[kk].T)+list(monitor_dict_curr[kk].T)).T
71+
else:
72+
for kk in monitor_dict.keys():
73+
monitor_dict[kk] = np.array(list(monitor_dict[kk])+list(monitor_dict_curr[kk]))
7074
except IOError as err:
7175
print('Got:')
7276
print(err)
7377
if not permissive:
7478
raise err
7579

76-
return monitor_dict
80+
return monitor_dict
7781

78-
def monitorh5list_to_obj(filename_list, key= 'Bunch', permissive=False):
79-
return obj_from_dict(monitorh5list_to_dict(filename_list, key, permissive))
82+
def monitorh5list_to_obj(filename_list, key= 'Bunch', flag_transpose=False, permissive=False):
83+
return obj_from_dict(monitorh5list_to_dict(filename_list, key, flag_transpose, permissive))
8084

8185

82-
def dict_to_h5(dict_save, filename):
86+
def dict_to_h5(dict_save, filename, compression=None, compression_opts=None):
8387
import h5py
8488
with h5py.File(filename, 'w') as fid:
8589
for kk in dict_save.keys():
86-
fid[kk] = dict_save[kk]
90+
fid.create_dataset(kk, data=dict_save[kk],
91+
compression=compression, compression_opts=compression_opts)
8792

8893

8994

ring_of_CPUs_multiturn.py

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def __init__(self, sim_content, N_pieces_per_transfer=1, force_serial = False, c
3030
N_buffer_float_size = 1000000, N_buffer_int_size = 100,
3131
verbose = False, mpi_verbose = False, enable_barriers = False,
3232
enable_orders_from_master = True):
33-
3433

34+
35+
self.iteration = -1
3536
self.sim_content = sim_content
3637
self.N_turns = sim_content.N_turns
3738

@@ -145,7 +146,15 @@ def __init__(self, sim_content, N_pieces_per_transfer=1, force_serial = False, c
145146
self.comm.Barrier()
146147
self.verbose_mpi_out('After barrier 2 (cpu %d)'%self.comm.Get_rank())
147148

148-
self.sim_content.init_all()
149+
if hasattr(self.sim_content, 'pre_init_master'):
150+
if self.I_am_the_master:
151+
from_master = self.sim_content.pre_init_master()
152+
else:
153+
from_master = None
154+
from_master = self._broadcast_from_master(from_master)
155+
self.sim_content.init_all(from_master)
156+
else:
157+
self.sim_content.init_all()
149158

150159
if self.enable_barriers:
151160
self.verbose_mpi_out('At barrier 3 (cpu %d)'%self.comm.Get_rank())
@@ -195,7 +204,7 @@ def __init__(self, sim_content, N_pieces_per_transfer=1, force_serial = False, c
195204
def run(self):
196205

197206

198-
iteration = 0
207+
self.iteration = 0
199208
list_received_buffers = [self.sim_content.piece_to_buffer(None)]
200209
while True:
201210

@@ -219,7 +228,7 @@ def run(self):
219228
if self.myring==0 and self.myid_in_ring == 0:
220229
t_now = time.mktime(time.localtime())
221230
print2logandstdo('%s, iter%03d - cpu %d.%d startin bunch %d/%d turn=%d'%(time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(t_now)),
222-
iteration, self.myring, self.myid_in_ring,
231+
self.iteration, self.myring, self.myid_in_ring,
223232
next_bunch.slice_info['i_bunch'], next_bunch.slice_info['N_bunches_tot_beam'], next_bunch.slice_info['i_turn']))
224233

225234

@@ -255,7 +264,7 @@ def run(self):
255264
if thisslice is not None:
256265
self.sim_content.treat_piece(thisslice)
257266
t_end = time.mktime(time.localtime())
258-
self._print_some_info_on_comm(thisslice, iteration, t_start, t_end)
267+
self._print_some_info_on_comm(thisslice, t_start, t_end)
259268

260269

261270
#########################
@@ -301,10 +310,10 @@ def run(self):
301310
self.comm.Barrier()
302311
self.verbose_mpi_out('After barrier L1 (cpu %d)'%self.comm.Get_rank())
303312

304-
self.verbose_mpi_out('At Sendrecv, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, iteration))
313+
self.verbose_mpi_out('At Sendrecv, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, self.iteration))
305314
self.comm.Sendrecv(sendbuf, dest=self.right, sendtag=self.right,
306315
recvbuf=self.buf_float, source=self.left, recvtag=self.myid)
307-
self.verbose_mpi_out('After Sendrecv, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, iteration))
316+
self.verbose_mpi_out('After Sendrecv, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, self.iteration))
308317

309318
if self.enable_barriers:
310319
self.verbose_mpi_out('At barrier L2 (cpu %d)'%self.comm.Get_rank())
@@ -324,26 +333,7 @@ def run(self):
324333

325334

326335
if self.enable_orders_from_master:
327-
if self.I_am_the_master:
328-
# send orders
329-
buforders = ch.list_of_strings_2_buffer(orders_from_master)
330-
if len(buforders) > self.N_buffer_int_size:
331-
raise ValueError('Int buffer is too small!')
332-
self.buf_int = 0*self.buf_int
333-
self.buf_int[:len(buforders)]=buforders
334-
335-
self.verbose_mpi_out('At Bcast, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, iteration))
336-
self.comm.Bcast(self.buf_int, self.master_id)
337-
self.verbose_mpi_out('After Bcast, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, iteration))
338-
339-
else:
340-
# receive orders from the master
341-
self.verbose_mpi_out('At Bcast, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, iteration))
342-
self.comm.Bcast(self.buf_int, self.master_id)
343-
self.verbose_mpi_out('After Bcast, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, iteration))
344-
345-
orders_from_master = ch.buffer_2_list_of_strings(self.buf_int)
346-
336+
orders_from_master = self._broadcast_from_master(orders_from_master)
347337
# check if simulation has to be ended
348338
if 'stop' in orders_from_master:
349339
break
@@ -353,23 +343,47 @@ def run(self):
353343
self.comm.Barrier()
354344
self.verbose_mpi_out('After barrier L4 (cpu %d)'%self.comm.Get_rank())
355345

356-
iteration+=1
346+
self.iteration+=1
357347

358348
# (TEMPORARY!) To stop
359-
# if iteration==10000:
349+
# if self.iteration==10000:
360350
# break
361351
# (TEMPORARY!)
362352

363-
def _print_some_info_on_comm(self, thisslice, iteration, t_start, t_end):
353+
def _print_some_info_on_comm(self, thisslice, t_start, t_end):
364354
if self.verbose:
365355
if thisslice is not None:
366-
print2logandstdo('Iter start on %s, iter%05d - I am %02d.%02d and I treated slice %d/%d of bunch %d/%d, lasts %ds'%(time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(t_start)), iteration,
356+
print2logandstdo('Iter start on %s, iter%05d - I am %02d.%02d and I treated slice %d/%d of bunch %d/%d, lasts %ds'%(time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(t_start)), self.iteration,
367357
self.myring, self.myid_in_ring,
368358
thisslice.slice_info['i_slice'], thisslice.slice_info['N_slices_tot_bunch'],
369359
thisslice.slice_info['info_parent_bunch']['i_bunch'],
370360
thisslice.slice_info['info_parent_bunch']['N_bunches_tot_beam'],
371361
t_end-t_start))
372362
else:
373-
print2logandstdo('Iter start on %s, iter%05d - I am %02d.%02d and I treated None, lasts %ds'%(time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(t_start)), iteration,
363+
print2logandstdo('Iter start on %s, iter%05d - I am %02d.%02d and I treated None, lasts %ds'%(time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(t_start)), self.iteration,
374364
self.myring, self.myid_in_ring,
375365
t_end-t_start))
366+
367+
368+
def _broadcast_from_master(self, list_of_strings):
369+
if self.I_am_the_master:
370+
# send orders
371+
buforders = ch.list_of_strings_2_buffer(list_of_strings)
372+
if len(buforders) > self.N_buffer_int_size:
373+
raise ValueError('Int buffer is too small!')
374+
self.buf_int = 0*self.buf_int
375+
self.buf_int[:len(buforders)] = buforders
376+
377+
self.verbose_mpi_out('At Bcast, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, self.iteration))
378+
self.comm.Bcast(self.buf_int, self.master_id)
379+
self.verbose_mpi_out('After Bcast, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, self.iteration))
380+
381+
else:
382+
# receive orders from the master
383+
self.verbose_mpi_out('At Bcast, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, self.iteration))
384+
self.comm.Bcast(self.buf_int, self.master_id)
385+
self.verbose_mpi_out('After Bcast, cpu %d/%d, iter %d'%(self.myid, self.N_nodes, self.iteration))
386+
387+
list_of_strings = ch.buffer_2_list_of_strings(self.buf_int)
388+
389+
return list_of_strings

test_instability/000_instability_simulation/000_run_sim_with_eclouds.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

test_instability/000_instability_simulation/001_run_sim_with_dummy_eclouds.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)