def beam_decode(self, encodings, input_len=10, beam_size=1): # Add parameters to the graph self.dec.init(encodings, [[self.trg_sos]], self.usr.user_vector, test=self.test, update=self.update) # Initialize context context = dy.zeroes((self.enc.dim, )) # Process user token if necessary if self.user_token: _, _, _ = self.dec.next(self.usr.user_vector, context, test=self.test) # Get conditional log probability of lengths llp = np.log(self.lex.p_L[input_len]) # Initialize beam beams = [beam.Beam(self.dec.ds, context, [self.trg_sos], llp[0])] # Loop for i in range(int(min(self.max_len, input_len * 1.5))): new_beam = [] for b in beams: if b.words[-1] == self.trg_eos: new_beam.append( beam.Beam(b.state, b.context, b.words, b.logprob, b.align)) continue h, e, b.state = self.dec.next([b.words[-1]], b.context, state=b.state) # Compute next context b.context, att = self.attend(encodings, h) # Score s = self.dec.s(h, b.context, e, test=self.test) # Probabilities p = dy.softmax(s).npvalue() # Careful for floating errors p = p.flatten() / p.sum() # Store alignment for e.g. unk replacement align = np.argmax(att.npvalue()) kbest = np.argsort(p) for nw in kbest[-beam_size:]: new_beam.append( beam.Beam( b.state, b.context, b.words + [nw], b.logprob + np.log(p[nw]) + llp[i + 1] - llp[i], b.align + [align])) # Only keep the best beams = sorted(new_beam, key=lambda b: b.logprob)[-beam_size:] if beams[-1].words[-1] == self.trg_eos: break return beams[-1]
def test_make_one_primary_uniform(self): """Check function that throws a particle - for uniform time dist""" a_beam = beam.Beam() test_birth = { "weight": 0.5, "random_seed": 10, "random_seed_algorithm": "incrementing_random", "reference": TEST_PRIM_MU, "transverse": { "transverse_mode": "pencil" }, "longitudinal": TEST_UNIFORM_T, "coupling": { "coupling_mode": "none" }, # "beam_polarisation" : "Gaussian_Unit_Vectors" } a_beam.birth(test_birth, "binomial", 2) for i in range(1000): #pylint: disable = W0612 primary = a_beam.make_one_primary() hit = xboa.hit.factory.MausJsonHitFactory.\ hit_from_maus_object('primary', primary, 0) self.assertGreater(hit['t'], TEST_UNIFORM_T["t_start"]) self.assertLess(hit['t'], TEST_UNIFORM_T["t_end"]) self.assertGreater(hit['energy'], xboa.common.pdg_pid_to_mass[13]) self.assertTrue(hit.check())
def test_make_one_primary_pencil(self): """Check function that throws a particle - for pencil beam""" a_beam = beam.Beam() test_birth = { "weight": 0.5, "random_seed": 10, "random_seed_algorithm": "incrementing_random", "reference": TEST_PRIM_MU, "transverse": { "transverse_mode": "pencil" }, "longitudinal": { "longitudinal_mode": "pencil", "momentum_variable": "p" }, "coupling": { "coupling_mode": "none" }, } a_beam.birth(test_birth, "binomial", 2) for i in range(10): #pylint: disable = W0612 primary = a_beam.make_one_primary() self.assertEqual( a_beam.reference, xboa.hit.factory.MausJsonHitFactory.hit_from_maus_object( 'primary', primary, 0))
def test_make_one_primary_gaus(self): """Check function that throws a particle - for gaussian distribution""" a_beam = beam.Beam() a_beam.birth(TEST_BIRTH, "binomial", 2) for i in range(1000): # pylint: disable = W0612 primary = a_beam.make_one_primary() hit = xboa.hit.factory.MausJsonHitFactory.\ hit_from_maus_object('primary', primary, 0) self.assertTrue(hit.check())
def main(**options): """Main script for finding GALFACTS sources""" print("find_sources.py {0}".format(vers)) for b in options["beams"]: if options["verbose"]: print("Log: Starting beam {0} analysis.".format(b)) this_beam = beam.Beam(b, **options) this_beam.find_sources() if options["verbose"]: print("Log: Done!")
def update(self): # 押されているキーをチェック pressed_keys = pygame.key.get_pressed() # 押されているキーに応じてプレイヤーを移動 if pressed_keys[K_LEFT]: self.rect.move_ip(-self.speed, 0) elif pressed_keys[K_RIGHT]: self.rect.move_ip(self.speed, 0) self.rect.clamp_ip(SCR_RECT) #スペースキーが押されたらショット!!!! if pressed_keys[K_SPACE]: beam.Beam(self.rect.center)
def run_beam(packed_args): #unpack args bm, tid = packed_args #print("Initializing beam {0} for taskid {1}".format(bm,tid)) #skip check here; done with mapping instead B = beam.Beam(tid, bm, pbname=pbname, pbdir=pbdir, masklevel=0.1, outputdir=args.outputdir, workingdir=args.workingdir) try: B.go() except Exception as e: print(("Processing failed for beam {0}, " "taskid {1}").format(bm, tid))
def __init__(self, anchorX, anchorY, anchor_deg): self.beams = [] self.onehot = np.zeros(CONST.LIDAR_DATA_SIZE) self.closest_dist = CONST.LIDAR_RANGE self.start_ang_deg = 90 - (CONST.LIDAR_SWEEP / 2) self.x0 = anchorX self.y0 = anchorY self.color = CONST.COLOR_BLUE self.increments = [] self.reward = 0 for i in range(CONST.LIDAR_COUNT): b = beam.Beam(i) self.beams.append(b) # increments are used for colsions detection # between beams and obstacles. temp = 0 for i in range((CONST.LIDAR_RANGE // CONST.LIDAR_RES) + 1): self.increments.append(temp) temp += CONST.LIDAR_RES
def birth(self, json_configuration): """ Read in datacards from supplied cards file and configuration defaults. @param json_configuration unicode string containing the json configuration Several options available: - binomial uses binomial_n, binomial_p to generate a random number of particles according to the binomial distribution. Uses the weight field in each beam to randomly select from multiple beam distributions. - counter adds a fixed number of particles defined by each of the beam's n_particles_per_spill field. - overwrite_existing overwrites the primary branch of any existing particles in mc branch, regardless of existing values. Uses the weight field in each beam to randomly select from multiple beam distributions. - file reads beam particles from input beam file The number of beam particles in a spill is determined by nbm_particles_per_spill specified in the config. """ try: config_doc = json.loads(json_configuration) self.__birth_empty_particles(config_doc["beam"]) self.beams = [] # if use_beam_file, then verify the file can be opened # get the file handle, and skip over headers and comments if self.use_beam_file: return self.__check_beam_file() for beam_def in config_doc["beam"]["definitions"]: a_beam = beam.Beam() a_beam.birth(beam_def, self.particle_generator, self.seed) self.beams.append(a_beam) return True except Exception: #pylint: disable=W0703 ErrorHandler.HandleException({}, self) return False
def test_process_beam_polarisation(self): """tests polarisation""" a_beam = beam.Beam() array = [] self._beam._Beam__birth_beam_polarisation(TEST_FORWARD) array = self._beam._Beam__process_beam_polarisation() self.assertAlmostEqual( ((array[0]**2) + (array[1]**2) + (array[2]**2))**0.5, 1.0) self.assertTrue(array[0] < array[2] and array[1] < array[2]) flag_more_x = False flag_less_x = False flag_more_y = False flag_less_y = False flag_more_z = False flag_less_z = False for i in range(100): # pylint: disable=W0612 self._beam._Beam__birth_beam_polarisation(TEST_NO_POL) array = self._beam._Beam__process_beam_polarisation() if array[0] > 0.0: flag_more_x = True if array[0] < 0.0: flag_less_x = True if array[1] > 0.0: flag_more_y = True if array[1] < 0.0: flag_less_y = True if array[2] > 0.0: flag_more_z = True if array[2] < 0.0: flag_less_z = True self.assertEqual(flag_more_x or flag_less_x, True) self.assertEqual(flag_more_y or flag_less_y, True) self.assertEqual(flag_more_z or flag_less_z, True) a_beam.make_one_primary()
import beam as bm beam = bm.Beam() beam.initiate_fem() beam.symplectic_euler() beam.compute_energy()
#command line code for testing from __future__ import print_function import beam as beam print("Load Beam object; 190807041, 1; mask 10%") B = beam.Beam(190807041, 1, masklevel=0.1, workingdir='tmp', outputdir='test') #print("Load Beam object; 190823041, 9; mask 10%") #B = beam.Beam(190823041,9,masklevel=0.1,workingdir='tmp') #print("Load Beam object; 190823041, 9; mask 10%") #B = beam.Beam(190823041,9,masklevel=0.1,workingdir='tmp', # pbname='gpall',pbdir='/data/kutkin/cbeams') #print("Get cont image") #B.get_cont_image() #print("Smooth cont image") #B.convolve() #print("Get and regrid PB") #B.regrid_pb() #print("Do PB correction") #B.do_pb() B.go() print("Test done")
def shoot(self, beam_list, way, b_type=0): b = beam.Beam(self.game_display, way, self.position, b_type=b_type) beam_list.append(b)
def test_birth(self): """ Overall check birth works """ a_beam = beam.Beam() self.assertRaises(KeyError, a_beam.birth, TEST_BIRTH, "counter", 2)
class TestBeam(unittest.TestCase): #pylint: disable = R0904 """ Tests the Beam class """ @classmethod def setUp(cls): #pylint: disable = C0103 """Set up tests""" cls._beam._Beam__birth_reference_particle({"reference": TEST_PRIM_MU}) def test_birth_part_gen_1(self): """Test __birth_particle_generator for counter mode""" self._beam._Beam__birth_particle_generator( { "n_particles_per_spill": 1, "random_seed_algorithm": "incrementing_random" }, "counter") self.assertRaises( ValueError, self._beam._Beam__birth_particle_generator, { "n_particles_per_spill": 0, "random_seed_algorithm": "incrementing_random" }, "counter") self._beam._Beam__birth_particle_generator( { "n_particles_per_spill": 1, "random_seed_algorithm": "incrementing_random" }, "counter") self.assertEqual(self._beam.n_particles_per_spill, 1) def test_birth_part_gen_2(self): """Test __birth_particle_generator for weighting mode""" self.assertRaises(ValueError, self._beam._Beam__birth_particle_generator, {"weight": 0.}, "binomial") self.assertRaises(ValueError, self._beam._Beam__birth_particle_generator, {"weight": -1.}, "overwrite_existing") self._beam._Beam__birth_particle_generator( { "weight": 2., "random_seed_algorithm": "incrementing_random" }, "binomial") self.assertAlmostEqual(self._beam.weight, 2.) def test_birth_part_gen_3(self): """Test __birth_particle_generator for random number assignment""" seed = self._beam.beam_seed self._beam._Beam__birth_particle_generator( { "n_particles_per_spill": 1, "random_seed_algorithm": "incrementing_random" }, "counter") self.assertTrue(abs(self._beam.beam_seed - seed) > 5) self.assertRaises(ValueError, self._beam._Beam__birth_particle_generator, { "n_particles_per_spill": 1, "random_seed_algorithm": "bob" }, "counter") def test_birth_reference_particle(self): """Test __birth_reference_particle""" self._beam_no_ref._Beam__birth_reference_particle \ ({"reference":TEST_PRIM_P}) ref = xboa.hit.Hit.new_from_dict(TEST_REF) self.assertEqual(self._beam_no_ref.reference, ref) test_broken = copy.deepcopy(TEST_PRIM_P) test_broken["position"] = 0 #self.assertRaises( ValueError, #self._beam_no_ref._Beam__birth_reference_particle, # ({"reference":test_broken}) ) self._beam_no_ref._Beam__birth_reference_particle \ ({"reference":TEST_PRIM_MU}) self.assertEqual(self._beam_no_ref.reference['pid'], -13) def test_birth_transverse_no_mode(self): """Test for bad input""" self.assertRaises(KeyError, self._beam._Beam__birth_transverse_ellipse, {}) self.assertRaises(ValueError, self._beam._Beam__birth_transverse_ellipse, {"transverse_mode": "bob"}) def test_birth_transverse_pencil(self): """Test for pencil beam""" self._beam._Beam__birth_transverse_ellipse( {"transverse_mode": "pencil"}) self.assertTrue( numpy.all( numpy.equal(self._beam.beam_matrix[0:4, 0:4], numpy.diag([1.] * 4)))) self.assertEqual(self._beam.transverse_mode, "pencil") def test_birth_transverse_penn(self): """Beam transverse Penn mode""" test = \ [[1.05668599e+03, -6.33950201e+02, 0.00000000e+00, 6.34327423e+02], [-6.33950201e+02, 1.14145263e+03, -6.34327423e+02, 0.00000000e+00], [0.00000000e+00, -6.34327423e+02, 1.05668599e+03, -6.33950201e+02], [6.34327423e+02, 0.00000000e+00, -6.33950201e+02, 1.14145263e+03]] test_array = numpy.array(test) self._beam._Beam__birth_transverse_ellipse(TEST_PENN) self.assertRaises(ValueError, self._beam._Beam__birth_transverse_ellipse, TEST_PENN_F1) self.assertRaises(ValueError, self._beam._Beam__birth_transverse_ellipse, TEST_PENN_F2) self.assertEqual(self._beam.transverse_mode, "penn") self.__cmp_matrix(test_array, self._beam.beam_matrix[0:4, 0:4]) # TEST_CONSTANT_SOL_NEG is a test for issue #1211 def test_birth_transverse_const_sol(self): """Beam transverse constant solenoid mode""" self._beam._Beam__birth_transverse_ellipse(TEST_CONSTANT_SOL) matrix_const_sol = self._beam.beam_matrix[0:4, 0:4] self._beam._Beam__birth_transverse_ellipse(TEST_CONSTANT_SOL_NEG) matrix_const_sol_neg = self._beam.beam_matrix[0:4, 0:4] for i in range(4): matrix_const_sol_neg[i, i - 4] = -matrix_const_sol_neg[i, i - 4] penn_mod = copy.deepcopy(TEST_PENN) penn_mod["alpha_4d"] = 0. self._beam._Beam__birth_transverse_ellipse(penn_mod) matrix_penn = self._beam.beam_matrix[0:4, 0:4] self.__cmp_matrix(matrix_penn, matrix_const_sol) self.__cmp_matrix(matrix_penn, matrix_const_sol_neg) def test_birth_transverse_twiss(self): """Beam transverse twiss mode""" self._beam._Beam__birth_transverse_ellipse(TEST_TWISS) test_matrix = self._beam.beam_matrix[0:4, 0:4] test_ref = numpy.array( [[1.05668599e+03, -6.33950201e+02, 0.00000000e+00, 0.00e+00], [-6.33950201e+02, 7.60666579e+02, 0.00000000e+00, 0.00e+00], [0.000e+00, 0.00000000e+00, 2.11548746e+02, 2.11316734e+02], [0.000e+00, 0.00000000e+00, 2.11316734e+02, 4.22169951e+02]]) self.__cmp_matrix(test_ref, test_matrix) self.assertRaises(ValueError, self._beam._Beam__birth_transverse_ellipse, TEST_TWISS_F1) self.assertRaises(ValueError, self._beam._Beam__birth_transverse_ellipse, TEST_TWISS_F2) self.assertEqual(self._beam.transverse_mode, "twiss") def test_birth_longitudinal_no_mode(self): """Beam longitudinal - bad user input""" self.assertRaises(KeyError, self._beam._Beam__birth_longitudinal_ellipse, {}) self.assertRaises(KeyError, self._beam._Beam__birth_longitudinal_ellipse, {"longitudinal_mode": "pencil"}) self.assertRaises(ValueError, self._beam._Beam__birth_longitudinal_ellipse, { "longitudinal_mode": "pencil", "momentum_variable": "bob" }) self.assertRaises(ValueError, self._beam._Beam__birth_longitudinal_ellipse, { "longitudinal_mode": "bob", "momentum_variable": "p" }) def test_birth_longitudinal_pencil(self): """Beam longitudinal - pencil mode""" for mom_var in ["p", "pz", "energy"]: self._beam._Beam__birth_longitudinal_ellipse({ "longitudinal_mode": "pencil", "momentum_variable": mom_var }) self.assertTrue( numpy.all(numpy.equal(self._beam.beam_matrix, numpy.diag([1.] * 6)))) self.assertEqual(self._beam.longitudinal_mode, "pencil") def test_birth_long_twiss(self): """Beam longitudinal - twiss mode""" self._beam._Beam__birth_longitudinal_ellipse(TEST_TWISS_L) self.assertEqual(self._beam.longitudinal_mode, "twiss") ref_matrix = numpy.array([[+5.28871865e-01, +1.05658367e+01], [+1.05658367e+01, +4.22169951e+02]]) self.__cmp_matrix(ref_matrix, self._beam.beam_matrix[4:, 4:]) self.assertRaises(ValueError, self._beam._Beam__birth_longitudinal_ellipse, TEST_TWISS_L_F1) self.assertRaises(ValueError, self._beam._Beam__birth_longitudinal_ellipse, TEST_TWISS_L_F2) def test_birth_long_gaussian(self): """Beam longitudinal - gaussian mode""" self._beam._Beam__birth_longitudinal_ellipse(TEST_GAUSSIAN_L1) self.assertEqual(self._beam.longitudinal_mode, "gaussian") equality_matrix = numpy.equal(self._beam.beam_matrix, numpy.diag([1.] * 4 + [1.e12, 1.e4])) self.assertTrue(numpy.all(equality_matrix)) self._beam._Beam__birth_longitudinal_ellipse(TEST_GAUSSIAN_L2) self.assertEqual(self._beam.longitudinal_mode, "gaussian") test_matrix = numpy.diag([1.] * 4 + [1.e4, 1.e4]) test_matrix[4, 5] = 100. * 100. - 1. test_matrix[5, 4] = 100. * 100. - 1. equality_matrix = numpy.equal(self._beam.beam_matrix, test_matrix) self.assertTrue(numpy.all(equality_matrix)) self.assertRaises(ValueError, self._beam._Beam__birth_longitudinal_ellipse, TEST_GAUSSIAN_L_F3) def test_birth_long_t_dist(self): """Beam longitudinal - sawtooth/uniform time distributions""" self._beam._Beam__birth_longitudinal_ellipse(TEST_UNIFORM_T) self.assertEqual(self._beam.longitudinal_mode, "uniform_time") self.assertAlmostEqual(self._beam.t_start, TEST_UNIFORM_T["t_start"]) self.assertAlmostEqual(self._beam.t_end, TEST_UNIFORM_T["t_end"]) self.assertAlmostEqual(self._beam.beam_matrix[5, 5], 1.e4) self.assertRaises(ValueError, self._beam._Beam__birth_longitudinal_ellipse, TEST_UNIFORM_T_F1) self._beam._Beam__birth_longitudinal_ellipse(TEST_SAWTOOTH_T) self.assertEqual(self._beam.longitudinal_mode, "sawtooth_time") self.assertAlmostEqual(self._beam.t_start, TEST_SAWTOOTH_T["t_start"]) self.assertAlmostEqual(self._beam.t_end, TEST_SAWTOOTH_T["t_end"]) self.assertAlmostEqual(self._beam.beam_matrix[5, 5], 1.e4) def test_birth_trans_long_coupling(self): """Beam transverse - longitudinal coupling""" self._beam._Beam__birth_trans_long_coupling({"coupling_mode": "none"}) self.assertRaises(NotImplementedError, self._beam._Beam__birth_trans_long_coupling, {"coupling_mode": "bob"}) def test_birth_beam_mean(self): """Beam mean setup""" for mom_var in ["p", "pz", "energy"]: self._beam._Beam__birth_longitudinal_ellipse({ "longitudinal_mode": "pencil", "momentum_variable": mom_var }) self._beam._Beam__birth_beam_mean() for i, key in enumerate(['x', 'px', 'y', 'py', 't']): self.assertAlmostEqual(self._beam.reference[key], self._beam.beam_mean[i]) self.assertAlmostEqual(self._beam.reference[mom_var], self._beam.beam_mean[5]) def test_birth_beam_polarisation(self): """tests polarisation""" self._beam._Beam__birth_beam_polarisation(TEST_BIRTH) self.assertEquals(self._beam.beam_polarisation['polarisation_mode'], 'flat') self.assertEquals(self._beam.beam_mean_x, 0.0) self.assertEquals(self._beam.beam_sigma_x, 1.0) self.assertEquals(self._beam.beam_mean_y, 0.0) self.assertEquals(self._beam.beam_sigma_y, 1.0) self.assertEquals(self._beam.beam_mean_z, 0.0) self.assertEquals(self._beam.beam_sigma_z, 1.0) print "polarization test_beam", self._beam.beam_polarisation self._beam._Beam__birth_beam_polarisation(TEST_GAUSSIAN_UNITS) self.assertEquals(self._beam.beam_polarisation['polarisation_mode'], 'gaussian_unit_vectors') self.assertEquals(self._beam.beam_mean_x, 2.0) self.assertEquals(self._beam.beam_sigma_x, 1.0) self.assertEquals(self._beam.beam_mean_y, 4.0) self.assertEquals(self._beam.beam_sigma_y, 3.0) self.assertEquals(self._beam.beam_mean_z, 20.0) self.assertEquals(self._beam.beam_sigma_z, 30.0) print "polarization test_beam", self._beam.beam_polarisation def test_birth_a_p_correlation(self): """Amplitude momentum correlation - at birth""" self._beam.momentum_defined_by = "energy" self._beam._Beam__birth_a_p_correlation({"a-p_correlation": TEST_AP_1}) self.assertEquals(self._beam.a_p_correlation["magnitude"], 0.1) self.assertEquals(self._beam.a_p_correlation["momentum_variable"], "p") self._beam._Beam__birth_a_p_correlation({"a-p_correlation": TEST_AP_2}) self.assertEquals(self._beam.a_p_correlation["magnitude"], 0.2) self.assertEquals(self._beam.a_p_correlation["momentum_variable"], "energy") for test, exc in [(TEST_AP_F1, KeyError), (TEST_AP_F2, ValueError), (TEST_AP_F3, KeyError)]: try: test_def = {"a-p_correlation": test} self._beam._Beam__birth_a_p_correlation(test_def) raise (RuntimeError("should have thrown")) except exc: pass def test_birth(self): """ Overall check birth works """ a_beam = beam.Beam() self.assertRaises(KeyError, a_beam.birth, TEST_BIRTH, "counter", 2) def test_process_array_to_primary(self): """Check function that converts from an array to a primary particle""" mean = numpy.array([10., 20., 30., 40., 50., 600.]) mass = xboa.common.pdg_pid_to_mass[13] self._beam.beam_seed = 10 self._beam.particle_seed_algorithm = "beam_seed" primary_hit = self._beam._Beam__process_array_to_hit(mean, 13, 'p') primary = self._beam._Beam__process_hit_to_primary(primary_hit) self.assertAlmostEqual(primary["position"]["x"], 10.) self.assertAlmostEqual(primary["position"]["y"], 30.) self.assertAlmostEqual(primary["position"]["z"], 3.) # from ref self.assertAlmostEqual(primary["momentum"]["x"], 20.) self.assertAlmostEqual(primary["momentum"]["y"], 40.) self.assertAlmostEqual(primary["momentum"]["z"], (600.**2. - 20**2. - 40.**2.)**0.5) self.assertAlmostEqual(primary["energy"], (600.**2. + mass**2.)**0.5) self.assertAlmostEqual(primary["time"], 50.) self.assertEqual(primary["particle_id"], 13) self.assertEqual(primary["random_seed"], 10) primary_hit = self._beam._Beam__process_array_to_hit(mean, 13, 'pz') primary = self._beam._Beam__process_hit_to_primary(primary_hit) self.assertAlmostEqual(primary["momentum"]["x"], 20.) self.assertAlmostEqual(primary["momentum"]["y"], 40.) self.assertAlmostEqual(primary["momentum"]["z"], 600.) self.assertAlmostEqual(primary["energy"], (600.**2. + 20.**2. + 40.**2. + mass**2.)**0.5) primary_hit = self._beam._Beam__process_array_to_hit( mean, -13, 'energy') primary = self._beam._Beam__process_hit_to_primary(primary_hit) self.assertAlmostEqual(primary["momentum"]["x"], 20.) self.assertAlmostEqual(primary["momentum"]["y"], 40.) self.assertAlmostEqual(primary["momentum"]["z"], (600.**2. - xboa.common.pdg_pid_to_mass[13]**2. - 20.**2 - 40.**2)**0.5) self.assertAlmostEqual(primary["energy"], 600.) def test_process_array_to_primary_sign(self): # pylint: disable = C0103 """Check function that converts from an array to a primary particle""" mean = numpy.array([10., 20., 30., 40., 50., 600.]) self._beam._Beam__birth_reference_particle \ ({"reference":TEST_PRIM_MU_NEG}) self._beam.beam_seed = 10 self._beam.particle_seed_algorithm = "beam_seed" primary_hit = self._beam._Beam__process_array_to_hit(mean, 13, 'p') primary = self._beam._Beam__process_hit_to_primary(primary_hit) self.assertLess(primary["momentum"]["z"], 0.) primary_hit = self._beam._Beam__process_array_to_hit(mean, 13, 'pz') primary = self._beam._Beam__process_hit_to_primary(primary_hit) self.assertLess(primary["momentum"]["z"], 0.) primary_hit = self._beam._Beam__process_array_to_hit( mean, 13, 'energy') primary = self._beam._Beam__process_hit_to_primary(primary_hit) self.assertLess(primary["momentum"]["z"], 0.) def test_make_one_primary_gaus(self): """Check function that throws a particle - for gaussian distribution""" a_beam = beam.Beam() a_beam.birth(TEST_BIRTH, "binomial", 2) for i in range(1000): # pylint: disable = W0612 primary = a_beam.make_one_primary() hit = xboa.hit.factory.MausJsonHitFactory.\ hit_from_maus_object('primary', primary, 0) self.assertTrue(hit.check()) def test_make_one_primary_pencil(self): """Check function that throws a particle - for pencil beam""" a_beam = beam.Beam() test_birth = { "weight": 0.5, "random_seed": 10, "random_seed_algorithm": "incrementing_random", "reference": TEST_PRIM_MU, "transverse": { "transverse_mode": "pencil" }, "longitudinal": { "longitudinal_mode": "pencil", "momentum_variable": "p" }, "coupling": { "coupling_mode": "none" }, } a_beam.birth(test_birth, "binomial", 2) for i in range(10): #pylint: disable = W0612 primary = a_beam.make_one_primary() self.assertEqual( a_beam.reference, xboa.hit.factory.MausJsonHitFactory.hit_from_maus_object( 'primary', primary, 0)) def test_make_one_primary_sawtooth(self): """Check function that throws a particle - for sawtooth time dist""" a_beam = beam.Beam() test_birth = { "weight": 0.5, "random_seed": 10, "random_seed_algorithm": "incrementing_random", "reference": TEST_PRIM_MU, "transverse": { "transverse_mode": "pencil" }, "longitudinal": TEST_SAWTOOTH_T, "coupling": { "coupling_mode": "none" }, } a_beam.birth(test_birth, "binomial", 2) for i in range(1000): #pylint: disable = W0612 primary = a_beam.make_one_primary() hit = xboa.hit.factory.MausJsonHitFactory.\ hit_from_maus_object('primary', primary, 0) self.assertGreater(hit['t'], TEST_SAWTOOTH_T["t_start"]) self.assertLess(hit['t'], TEST_SAWTOOTH_T["t_end"]) self.assertGreater(hit['energy'], xboa.common.pdg_pid_to_mass[13]) self.assertTrue(hit.check()) def test_make_one_primary_uniform(self): """Check function that throws a particle - for uniform time dist""" a_beam = beam.Beam() test_birth = { "weight": 0.5, "random_seed": 10, "random_seed_algorithm": "incrementing_random", "reference": TEST_PRIM_MU, "transverse": { "transverse_mode": "pencil" }, "longitudinal": TEST_UNIFORM_T, "coupling": { "coupling_mode": "none" }, # "beam_polarisation" : "Gaussian_Unit_Vectors" } a_beam.birth(test_birth, "binomial", 2) for i in range(1000): #pylint: disable = W0612 primary = a_beam.make_one_primary() hit = xboa.hit.factory.MausJsonHitFactory.\ hit_from_maus_object('primary', primary, 0) self.assertGreater(hit['t'], TEST_UNIFORM_T["t_start"]) self.assertLess(hit['t'], TEST_UNIFORM_T["t_end"]) self.assertGreater(hit['energy'], xboa.common.pdg_pid_to_mass[13]) self.assertTrue(hit.check()) def test_process_get_seed(self): """Check we generate the seed correctly""" self._beam.beam_seed = 9 self._beam.particle_seed_algorithm = "beam_seed" self.assertEqual(self._beam._Beam__process_get_seed(), self._beam.beam_seed) self._beam.particle_seed_algorithm = "random" self.assertNotEqual(self._beam._Beam__process_get_seed(), self._beam._Beam__process_get_seed()) self.assertTrue(abs(self._beam._Beam__process_get_seed()-\ self._beam._Beam__process_get_seed()) > 5) self._beam.beam_seed = 9 self._beam.particle_seed_algorithm = "incrementing" self.assertEqual(self._beam._Beam__process_get_seed(), 9) self.assertEqual(self._beam._Beam__process_get_seed(), 10) self.assertEqual(self._beam._Beam__process_get_seed(), 11) # assigns beam_seed to random number elsewhere self._beam.particle_seed_algorithm = "incrementing_random" self.assertEqual(self._beam._Beam__process_get_seed(), 12) self.assertEqual(self._beam._Beam__process_get_seed(), 13) self.assertEqual(self._beam._Beam__process_get_seed(), 14) def test_process_beam_polarisation(self): """tests polarisation""" a_beam = beam.Beam() array = [] self._beam._Beam__birth_beam_polarisation(TEST_FORWARD) array = self._beam._Beam__process_beam_polarisation() self.assertAlmostEqual( ((array[0]**2) + (array[1]**2) + (array[2]**2))**0.5, 1.0) self.assertTrue(array[0] < array[2] and array[1] < array[2]) flag_more_x = False flag_less_x = False flag_more_y = False flag_less_y = False flag_more_z = False flag_less_z = False for i in range(100): # pylint: disable=W0612 self._beam._Beam__birth_beam_polarisation(TEST_NO_POL) array = self._beam._Beam__process_beam_polarisation() if array[0] > 0.0: flag_more_x = True if array[0] < 0.0: flag_less_x = True if array[1] > 0.0: flag_more_y = True if array[1] < 0.0: flag_less_y = True if array[2] > 0.0: flag_more_z = True if array[2] < 0.0: flag_less_z = True self.assertEqual(flag_more_x or flag_less_x, True) self.assertEqual(flag_more_y or flag_less_y, True) self.assertEqual(flag_more_z or flag_less_z, True) a_beam.make_one_primary() def test_process_a_p_correlation(self): """Test beam.py a-p correlation routine""" self._beam.beam_matrix = numpy.diag([0.1] * 6) self._beam.beam_mean = numpy.zeros((6)) self._beam.a_p_correlation = { "momentum_variable": "pz", "magnitude": 0.5 } ref_pz = 200. hit1 = Hit.new_from_dict({"pz": ref_pz, "mass": 105.658}, "energy") hit2 = hit1.deepcopy() self._beam._Beam__process_a_p_correlation(hit2) self.assertEqual(hit1, hit2) for matrix in [numpy.diag([0.1] * 6), numpy.diag([0.2] * 6)]: self._beam.beam_matrix = matrix hit2 = hit1.deepcopy() hit3 = hit1.deepcopy() hit2['x'] = 2. hit3['px'] = 2. self._beam._Beam__process_a_p_correlation(hit2) self._beam._Beam__process_a_p_correlation(hit3) self.assertAlmostEqual(hit2['pz'], hit3['pz']) self.assertAlmostEqual(hit2['pz'] - ref_pz, ref_pz * 0.5 * 4. * 105.658**2) def __cmp_matrix(self, ref_matrix, test_matrix): """Compare to numpy matrices""" self.assertEqual(ref_matrix.shape, test_matrix.shape) msg = "\nReference:\n"+str(ref_matrix)+\ "\nIn Test:\n"+str(test_matrix) for i in range(ref_matrix.shape[0]): for j in range(test_matrix.shape[1]): self.assertAlmostEqual(ref_matrix[i, j], test_matrix[i, j], 3, msg) _beam = beam.Beam() _beam_no_ref = beam.Beam()