@@ -411,11 +411,17 @@ class CutRFBucket6D(ParticleGenerator):
411411 bi-gaussian with given sigma_z and sigma_dp which is then cut along
412412 the funciotn given by is_accepted which is typically the function
413413 is_in_separatrix of the rfbucket instance of the longitudinal map.
414+ To avoid bucket leakage and particle losses as a consequence of the
415+ unmatched initialisation, a margin / tolerance can be specified by
416+ the argument margin (in % of RFBucket.Hmax, 5% by default), so that
417+ particles will not be initialised too close to the separatrix. The
418+ boundary will in fact be given by the equihamiltonian with a value
419+ of margin*Hmax.
414420 '''
415421 def __init__ (self , macroparticlenumber , intensity , charge , mass ,
416422 circumference , gamma_reference ,
417423 transverse_map , epsn_x , epsn_y ,
418- sigma_z , sigma_dp , is_accepted = None ,
424+ sigma_z , sigma_dp , is_accepted , margin = 0.05 ,
419425 * args , ** kwargs ):
420426 '''Uses the transverse_map to extract the optics parameters
421427 and the rf_bucket to match the longitudinal distribution.
@@ -427,7 +433,7 @@ def __init__(self, macroparticlenumber, intensity, charge, mass,
427433 self ._rf_bucket_matcher = CutRFBucket2D (
428434 macroparticlenumber , intensity , charge , mass ,
429435 circumference , gamma_reference ,
430- sigma_z , sigma_dp , is_accepted , * args , ** kwargs )
436+ sigma_z , sigma_dp , is_accepted , margin , * args , ** kwargs )
431437 super (CutRFBucket6D , self ).__init__ (
432438 macroparticlenumber , intensity , charge , mass , circumference ,
433439 gamma_reference , HEADTAILcoords .coordinates , * args , ** kwargs )
@@ -474,39 +480,46 @@ def distribute(self):
474480class CutRFBucket2D (ParticleGenerator ):
475481 '''
476482 For HEADTAIL style matching into RF bucket.
483+ The argument is_accepted takes a function (usually
484+ RFBucket.is_in_separatrix) defining the separatrix of the rf bucket.
485+ To avoid bucket leakage and particle losses as a consequence of the
486+ unmatched initialisation, a margin / tolerance can be specified by
487+ the argument margin (in % of RFBucket.Hmax, 5% by default), so that
488+ particles will not be initialised too close to the separatrix. The
489+ boundary will in fact be given by the equihamiltonian with a value
490+ of margin*Hmax.
491+
477492 BY KEVIN: NEEDS TO BE CLEANED UP BY ADRIAN!
478493 '''
479494 def __init__ (self , macroparticlenumber , intensity , charge , mass ,
480495 circumference , gamma_reference , sigma_z , sigma_dp ,
481- is_accepted = None , generator_seed = None , * args , ** kwargs ):
496+ is_accepted , margin = 0.05 , * args , ** kwargs ):
482497
483498 self .sigma_z = sigma_z
484499 self .sigma_dp = sigma_dp
485500 self .is_accepted = is_accepted
486- self .random_state = RandomState ()
487- self .random_state .seed (generator_seed )
501+ self .margin = margin
488502
489503 super (CutRFBucket2D , self ).__init__ (
490504 macroparticlenumber , intensity , charge , mass , circumference ,
491505 gamma_reference , HEADTAILcoords .longitudinal , * args , ** kwargs )
492506
493507 def distribute (self ):
494508
495- z = self .sigma_z * self .random_state .randn (self .macroparticlenumber )
496- dp = self .sigma_dp * self .random_state .randn (self .macroparticlenumber )
497- if self .is_accepted :
498- self ._redistribute (z , dp )
509+ z = normal (0 , self .sigma_z , self .macroparticlenumber )
510+ dp = normal (0 , self .sigma_dp , self .macroparticlenumber )
511+ self ._redistribute (z , dp )
499512
500513 return {'z' : z , 'dp' : dp }
501514
502515 def _redistribute (self , z , dp ):
503516
504- mask_out = ~ self .is_accepted (z , dp )
517+ mask_out = ~ self .is_accepted (z , dp , self . margin )
505518 while mask_out .any ():
506519 n_gen = np .sum (mask_out )
507- z [mask_out ] = self .sigma_z * self . random_state . randn ( n_gen )
508- dp [mask_out ] = self .sigma_dp * self . random_state . randn ( n_gen )
509- mask_out = ~ self .is_accepted (z , dp )
520+ z [mask_out ] = normal ( 0 , self .sigma_z , n_gen )
521+ dp [mask_out ] = normal ( 0 , self .sigma_dp , n_gen )
522+ mask_out = ~ self .is_accepted (z , dp , self . margin )
510523 self .prints ('Reiterate on non-accepted particles' )
511524
512525
0 commit comments