def setUp(self): super().setUp() self.options = self.create_basic_options() dirpath = os.path.join(os.path.dirname(__file__), 'testdata', 'sim1') imp = Casino2Importer() results = imp.import_(self.options, dirpath) self.simulation = Simulation(self.options, results)
async def test_import_(event_loop, importer, options, testdatadir): dirpath = os.path.join(testdatadir, "sim1") results = await importer.import_(options, dirpath) simulation = Simulation(options, results) assert len(simulation.results) == 1 result = simulation.find_result(EmittedPhotonIntensityResult)[0] assert len(result) == 43 q = result[("Au", "La")] assert q.n == pytest.approx(2.73255e-7, abs=1e-13) q = result[("Si", "Ka1")] assert q.n == pytest.approx(1.6331941e-6, abs=1e-13)
def create_basic_simulation(self): options = self.create_basic_options() results = [] results.append(self.create_basic_photonintensityresult()) return Simulation(options, results)
def simulation(): program = ProgramMock() beam = GaussianBeam(15e3, 10e-9) sample = SubstrateSample(Material.pure(29)) detector = PhotonDetector("xray", math.radians(40.0)) analyses = [PhotonIntensityAnalysis(detector)] tags = ["basic", "test"] options = Options(program, beam, sample, analyses, tags) results = [] analysis = PhotonIntensityAnalysis(detector) builder = EmittedPhotonIntensityResultBuilder(analysis) builder.add_intensity((29, "Ka1"), 1.0, 0.1) builder.add_intensity((29, "Ka2"), 2.0, 0.2) builder.add_intensity((29, "Kb1"), 4.0, 0.5) builder.add_intensity((29, "Kb3"), 5.0, 0.7) builder.add_intensity((29, "Kb5I"), 1.0, 0.1) builder.add_intensity((29, "Kb5II"), 0.5, 0.1) builder.add_intensity((29, "Ll"), 3.0, 0.1) results.append(builder.build()) analysis = KRatioAnalysis(detector) builder = KRatioResultBuilder(analysis) builder.add_kratio((29, "Ka1"), 1.0, 1.0) builder.add_kratio((29, "Ka2"), 2.0, 1.0) builder.add_kratio((29, "Kb1"), 0.5, 1.0) builder.add_kratio((29, "Kb3"), 1.5, 1.0) builder.add_kratio((29, "Kb5I"), 1.0, 1.0) builder.add_kratio((29, "Kb5II"), 0.5, 1.0) builder.add_kratio((29, "Ll"), 2.0, 1.0) results.append(builder.build()) return Simulation(options, results)
async def test_casino2_cancel(event_loop, options, tmpdir): # Increase number of electrons options.program.number_trajectories = 10000 worker = Casino2Worker() token = Token("test") simulation = Simulation(options) outputdir = str(tmpdir) task = asyncio.create_task(worker.run(token, simulation, outputdir)) await asyncio.sleep(0.5) task.cancel() try: await task except asyncio.CancelledError: assert True, "Task was cancelled properly" else: assert False assert token.state == TokenState.CANCELLED assert token.progress == 1.0 assert token.status == "Cancelled" assert len(simulation.results) == 0
def test_prepare_simulations_already_in_project(runner, options): simulation = Simulation(options) simulation.results.append(object()) runner.project.simulations.append(simulation) simulations = runner.prepare_simulations(options) assert len(simulations) == 0
def _create_simulations(self, list_options, identifiers): simulations = [] for options, identifier in zip(list_options, identifiers): simulation = Simulation(options, identifier=identifier) simulations.append(simulation) return simulations
async def testrun(event_loop, options, tmpdir): worker = WorkerMock() token = Token("test") simulation = Simulation(options) await worker.run(token, simulation, tmpdir) assert token.state == TokenState.DONE assert token.progress == 1.0 assert token.status == "Done" assert len(simulation.results) == 1
async def test_casino2_worker(event_loop, options, sample, tmpdir): options.sample = sample worker = Casino2Worker() token = Token("test") simulation = Simulation(options) outputdir = str(tmpdir) await worker.run(token, simulation, outputdir) assert token.state == TokenState.DONE assert token.progress == 1.0 assert token.status == "Done" assert len(simulation.results) == 1
async def testrun_error(event_loop, options, tmpdir): options.beam.energy_eV = 0.0 # To cause WorkerErrorr worker = WorkerMock() token = Token("test") simulation = Simulation(options) try: await worker.run(token, simulation, tmpdir) except ExportError: assert True else: assert False assert token.state == TokenState.ERROR
async def testrun_cancel(event_loop, options, tmpdir): worker = WorkerMock() token = Token("test") simulation = Simulation(options) task = asyncio.create_task(worker.run(token, simulation, tmpdir)) await asyncio.sleep(0.5) task.cancel() try: await task except asyncio.CancelledError: assert True, "Task was cancelled properly" else: assert False assert token.state == TokenState.CANCELLED
def _exclude_simulated_options(self, list_options): final_list_options = [] for options in list_options: # Exclude already submitted options if options in self._submitted_options: continue # Exclude if simulation with same options already exists in project # and has results dummy_simulation = Simulation(options, identifier="dummy") if dummy_simulation in self.project.simulations: index = self.project.simulations.index(dummy_simulation) real_simulation = self.project.simulations[index] if real_simulation.results: continue final_list_options.append(options) return final_list_options
async def test_penepma_worker(event_loop, options, sample, tmp_path): options.sample = sample worker = PenepmaWorker() token = Token("test") simulation = Simulation(options) outputdir = tmp_path await worker.run(token, simulation, outputdir) assert token.state == TokenState.DONE assert token.progress == 1.0 assert token.status == "Done" assert len(simulation.results) == 2 assert outputdir.joinpath("pe-geometry.rep").exists() assert outputdir.joinpath("penepma.dat").exists() assert outputdir.joinpath("penepma-res.dat").exists() assert outputdir.joinpath("dump1.dat").exists() assert outputdir.joinpath("pe-intens-01.dat").exists() assert outputdir.joinpath("pe-material.dat").exists()
class TestCasino2Importer(TestCase): def setUp(self): super().setUp() self.options = self.create_basic_options() dirpath = os.path.join(os.path.dirname(__file__), 'testdata', 'sim1') imp = Casino2Importer() results = imp.import_(self.options, dirpath) self.simulation = Simulation(self.options, results) def testskeleton(self): self.assertEqual(1, len(self.simulation.results)) def test_import_analysis_photonintensity_emitted(self): result = self.simulation.find_result(EmittedPhotonIntensityResult)[0] self.assertEqual(29, len(result)) q = result[('Au', 'La')] self.assertAlmostEqual(2.73255e-7, q.n, 13) q = result[('Si', 'Ka1')] self.assertAlmostEqual(1.6331941e-6, q.n, 13)
def create_simulation(options): builder = EmittedPhotonIntensityResultBuilder(self.a) for z, wf in options.sample.material.composition.items(): builder.add_intensity((z, 'Ka'), wf * 1e3, math.sqrt(wf * 1e3)) result = builder.build() return Simulation(options, [result])
def _run_and_test(self, sample): self.options.sample = sample simulation = Simulation(self.options) self.worker.run(self.token, simulation, self.outputdir) self.assertEqual(1, len(simulation.results))