Skip to content
Snippets Groups Projects
Commit b34cae89 authored by Naoto Yamamoto's avatar Naoto Yamamoto
Browse files

Add print_time and check_alive functions to util.

print_time : output the date and time with messages to strandard output.
check_alive : detect the charge loss after aperture function is called.
parent 4e5eec7b
No related branches found
No related tags found
No related merge requests found
......@@ -2,3 +2,4 @@
*.ipynb_checkpoints*
test_*.py
*.hdf5
bk_*.py
\ No newline at end of file
###################################################################################
### from nytool/utilities to mbtrack2/utilities
### 2023/02/06 written by Naoto Yamamoto, naotoy@post.kek.jp
###
### These functions are made to keep the similar usage as the original mbtrack.
###
###################################################################################
def check_alive(turn,beam,filename=None):
"""
turn : int
turn nuber
beam ; Beam or Bunch_object
beam or bunch object
I0 : float
total beam current
"""
if check_alive.enable:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
if size == 1:
for idx in beam.bunch_index:
if False in beam[idx].alive:
print("!!!!! Beam loss at turn %d of Bunch %d !!!!!" % (turn,idx),flush=True)
if filename is not None:
with open("%s.loss.txt" %(filename),'a') as f:
print("!!!!! Beam loss at turn %d of Bunch %d !!!!!" % (turn,idx),file=f)
check_alive.enable = False
else: #MPI
#bunch = beam[beam.mpi.rank_to_bunch(size)]
bunch = beam[beam.mpi.bunch_num]
if False in bunch.alive:
print("!!!!! Beam loss at turn %d of Bunch %d !!!!!" % (turn,beam.mpi.bunch_num),flush=True)
if filename is not None:
with open("%s.loss.txt" %(filename),'a') as f:
print("!!!!! Beam loss at turn %d of Bunch %d !!!!!" % (turn,beam.mpi.bunch_num),file=f)
check_alive.enable = False
check_alive.enable = True
###################################################################################
### from nytool/utilities to mbtrack2/utilities
### 2023/02/06 written by Naoto Yamamoto, naotoy@post.kek.jp
###
### These functions are made to keep the similar usage as the original mbtrack.
###
###################################################################################
def print_time(message = "",flush=False):
"""
Print a message only once even if mpi is used.
"""
from time import strftime,localtime
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
mess = strftime(" [%d %b %Y %H:%M:%S] ", localtime()) + message
print(mess,flush=flush)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment