class ElementInstantiationTests_withObjects(unittest.TestCase): def setUp(self): self.pynacInstance = Pynac( os.path.join(os.path.dirname(__file__), 'ESS_with_SC_ana.in')) def test_quad_class(self): quads = self.pynacInstance.get_x_objs('QUADRUPO') if not quads: raise AssertionError('No quads in this lattice') for quad in quads: self.assertIsInstance(quad, ele.Quad) def test_cavmc_class(self): cavs = self.pynacInstance.get_x_objs('CAVMC') if not cavs: raise AssertionError('No CAVMCs in this lattice') for cav in cavs: self.assertIsInstance(cav, ele.CavityAnalytic) def test_drift_class(self): drifts = self.pynacInstance.get_x_objs('DRIFT') if not drifts: raise AssertionError('No drifts in this lattice') for drift in drifts: self.assertIsInstance(drift, ele.Drift) def test_cavsc_class(self): cavscs = self.pynacInstance.get_x_objs('CAVSC') if not cavscs: raise AssertionError('No CAVSCs in this lattice') for cav in cavscs: self.assertIsInstance(cav, ele.AccGap) def test_reject_class(self): apers = self.pynacInstance.get_x_objs('REJECT') if not apers: raise AssertionError('No REJECTs in this lattice') for aper in apers: self.assertIsInstance(aper, ele.Set4DAperture) def test_buncher_class(self): bunchers = self.pynacInstance.get_x_objs('BUNCHER') if not bunchers: raise AssertionError('No bunchers in this lattice') for buncher in bunchers: self.assertIsInstance(buncher, ele.Buncher) def test_field_class(self): fields = self.pynacInstance.get_x_objs('FIELD') if not fields: raise AssertionError('No FIELDs in this lattice') for field in fields: self.assertIsInstance(field, ele.AccFieldFromFile) def test_steer_class(self): steerers = self.pynacInstance.get_x_objs('STEER') if not steerers: raise AssertionError('No steerers in this lattice') for steerer in steerers: self.assertIsInstance(steerer, ele.Steerer)
class PynacTest(unittest.TestCase): pynacInstance = Pynac( os.path.join(os.path.dirname(__file__), 'ESS_with_SC_ana.in')) def test_getPlotInds(self): self.assertEqual(self.pynacInstance.get_num_plots(), 7) def test_RemoveElement(self): oldInds = self.pynacInstance.get_x_inds('DRIFT') del self.pynacInstance.lattice[oldInds[0]] newInds = self.pynacInstance.get_x_inds('DRIFT') self.assertEqual(len(oldInds), len(newInds) + 1) def test_getXinds_quads(self): quadInds = self.pynacInstance.get_x_inds('QUADRUPO') self.assertEqual(len(quadInds), 243) def test_getXinds_cavmcs(self): cavmcinds = self.pynacInstance.get_x_inds('CAVMC') self.assertEqual(len(cavmcinds), 62) def test_getXinds_nonsense(self): nonsenseinds = self.pynacInstance.get_x_inds('BLAHBLAHBLAH') self.assertEqual(len(nonsenseinds), 0) def test_setNewRDBeamFile(self): newfilename = 'testfilename.in' self.pynacInstance.set_new_rdbeam_file(newfilename) inds = self.pynacInstance.get_x_inds('RDBEAM') self.assertEqual(self.pynacInstance.lattice[inds[0]][1][0][0], 'testfilename.in')
class RunningPynacTest(unittest.TestCase): @classmethod def setUpClass(self): self.pynacInstance = Pynac( os.path.join(os.path.dirname(__file__), 'ESS_with_SC_ana.in')) self.pynacInstance.run() def test_PynacRuns(self): self.assertTrue(os.path.exists('dynac.short')) def test_PynacIsIdenticalToDynac(self): with open('emit.plot') as f: pynacFile = ''.join(f.readlines()) with open('ref_emit.plot') as f: dynacFile = ''.join(f.readlines()) self.assertEqual(pynacFile, dynacFile) def test_getNumberOfParticles(self): p = get_number_of_particles() self.assertEqual(p, 1000) @classmethod def tearDownClass(self): # Python3.2 doesn't have FileNotFoundError, so make the following tries to # get it, and defaults to OSError if it's not found. FileNotFound = getattr(__builtins__, 'FileNotFoundError', OSError) filelist = [ 'beam_core.dst', 'beam_remove.dst', 'cavdat.out', 'dynac_in_pr.dst', 'dynac.dmp', 'dynac.long', 'dynac.print', 'dynac.short', 'emit.plot', 'lost_particles.data', 'pynacrun.log', ] for f in filelist: try: os.remove(f) except FileNotFound: pass
class ElementManipulationTest(unittest.TestCase): def setUp(self): self.pynacInstance = Pynac( os.path.join(os.path.dirname(__file__), 'ESS_with_SC_ana.in')) def test_scaleQuad(self): quadinds = self.pynacInstance.get_x_inds('QUADRUPO') quad = self.pynacInstance.lattice[quadinds[0]] quad.scaleField(0) self.assertEqual(quad.B.val, 0) quad.scaleField(10) self.assertEqual(quad.B.val, 0) def test_setQuad(self): quadinds = self.pynacInstance.get_x_inds('QUADRUPO') quad = self.pynacInstance.lattice[quadinds[0]] quad.setField(0) self.assertEqual(quad.B.val, 0) quad.setField(10) self.assertEqual(quad.B.val, 10) def test_scaleCavMC(self): cavinds = self.pynacInstance.get_x_inds('CAVMC') cav = self.pynacInstance.lattice[cavinds[0]] cav.scaleField(0) self.assertEqual(cav.fieldReduction.val, -100) def test_adjustCAVMCPhase(self): adjustBy = 10 cavinds = self.pynacInstance.get_x_inds('CAVMC') cav = self.pynacInstance.lattice[cavinds[0]] originalPhase = cav.phase newPhase = ele.Param(val=originalPhase.val + adjustBy, unit=originalPhase.unit) cav.adjustPhase(adjustBy) self.assertEqual(cav.phase, newPhase) cav.adjustPhase(-adjustBy) self.assertEqual(cav.phase, originalPhase) def test_scaleSteerer(self): steererinds = self.pynacInstance.get_x_inds('STEER') steerer = self.pynacInstance.lattice[steererinds[0]] steerer.scaleField(0) self.assertEqual(steerer.field_strength.val, 0) steerer.scaleField(10) self.assertEqual(steerer.field_strength.val, 0) def test_setSteerer(self): steererinds = self.pynacInstance.get_x_inds('STEER') steerer = self.pynacInstance.lattice[steererinds[0]] steerer.setField(0) self.assertEqual(steerer.field_strength.val, 0) steerer.setField(10) self.assertEqual(steerer.field_strength.val, 10)
class ElementInstantiationTests_withInds(unittest.TestCase): def setUp(self): self.pynacInstance = Pynac( os.path.join(os.path.dirname(__file__), 'ESS_with_SC_ana.in')) def test_quad_class(self): quadinds = self.pynacInstance.get_x_inds('QUADRUPO') quad = self.pynacInstance.lattice[quadinds[0]] self.assertIsInstance(quad, ele.Quad) def test_cavmc_class(self): cavinds = self.pynacInstance.get_x_inds('CAVMC') cav = self.pynacInstance.lattice[cavinds[0]] self.assertIsInstance(cav, ele.CavityAnalytic) def test_drift_class(self): driftinds = self.pynacInstance.get_x_inds('DRIFT') drift = self.pynacInstance.lattice[driftinds[0]] self.assertIsInstance(drift, ele.Drift) def test_cavsc_class(self): cavscinds = self.pynacInstance.get_x_inds('CAVSC') cav = self.pynacInstance.lattice[cavscinds[0]] self.assertIsInstance(cav, ele.AccGap) def test_reject_class(self): aperinds = self.pynacInstance.get_x_inds('REJECT') aper = self.pynacInstance.lattice[aperinds[0]] self.assertIsInstance(aper, ele.Set4DAperture) def test_buncher_class(self): buncherinds = self.pynacInstance.get_x_inds('BUNCHER') buncher = self.pynacInstance.lattice[buncherinds[0]] self.assertIsInstance(buncher, ele.Buncher) def test_field_class(self): fieldinds = self.pynacInstance.get_x_inds('FIELD') field = self.pynacInstance.lattice[fieldinds[0]] self.assertIsInstance(field, ele.AccFieldFromFile)
def setUp(self): self.pynacInstance = Pynac( os.path.join(os.path.dirname(__file__), 'ESS_with_SC_ana.in'))