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)
def testexpand_analyses_to_single_detector(): det1 = PhotonDetector("det1", 0.1) det2 = PhotonDetector("det2", 3.3) analyses = [ PhotonIntensityAnalysis(det1), PhotonIntensityAnalysis(det2), KRatioAnalysis(det1), KRatioAnalysis(det2), ] combinations = expand_analyses_to_single_detector(analyses) assert len(combinations) == 2 combinations = expand_to_single(analyses) assert len(combinations) == 4
def setUp(self): super().setUp() det = PhotonDetector('det', math.radians(40.0)) self.a = PhotonIntensityAnalysis(det) self.options = self.create_basic_options()
def options(): program = Casino2Program(number_trajectories=50) beam = GaussianBeam(15e3, 10e-9) sample = SubstrateSample(Material.pure(29)) detector = PhotonDetector("xray", math.radians(40.0)) analyses = [PhotonIntensityAnalysis(detector)] tags = ["basic", "test"] return Options(program, beam, sample, analyses, tags)
def test_validate_detector_photon_invalid(exporter, options): detector = PhotonDetector("", 2.0, -1.0) erracc = ErrorAccumulator() exporter._validate_detector_photon(detector, options, erracc) assert len(erracc.exceptions) == 3 assert len(erracc.warnings) == 0
def test_validate_analysis_photonintensity(exporter, options): detector = PhotonDetector("test", 1.0, 1.0) analysis = PhotonIntensityAnalysis(detector) erracc = ErrorAccumulator() exporter._validate_analysis_photonintensity(analysis, options, erracc) assert len(erracc.exceptions) == 0 assert len(erracc.warnings) == 0
def options(): program = PenepmaProgram(number_trajectories=100) program.exporter.dump_interval_s = 2 beam = CylindricalBeam(15e3, 10e-9) sample = SubstrateSample(Material.pure(29)) detector = PhotonDetector("xray", math.radians(40.0)) analyses = [PhotonIntensityAnalysis(detector)] tags = ["basic", "test"] return Options(program, beam, sample, analyses, tags)
def _validate_detector_photon(self, detector, options, errors): name = \ self._validate_detector_base_name(detector.name, options, errors) elevation_rad = \ self._validate_detector_photon_elevation_rad(detector.elevation_rad, options, errors) azimuth_rad = \ self._validate_detector_photon_azimuth_rad(detector.azimuth_rad, options, errors) return PhotonDetector(name, elevation_rad, azimuth_rad)
def test_validate_analysis_kratio_invalid(exporter, options): detector = PhotonDetector("test", 1.0, 1.0) analysis = KRatioAnalysis(detector) analysis.add_standard_material(14, Material.pure(13)) erracc = ErrorAccumulator() exporter._validate_analysis_kratio(analysis, options, erracc) assert len(erracc.exceptions) == 1 assert len(erracc.warnings) == 0
def builder(): detector = PhotonDetector("det", 1.1, 2.2) analysis = PhotonIntensityAnalysis(detector) builder = EmittedPhotonIntensityResultBuilder(analysis) builder.add_intensity((13, "Ka1"), 1.0, 0.1) builder.add_intensity((13, "Ka2"), 2.0, 0.2) builder.add_intensity((13, "Kb1"), 4.0, 0.5) builder.add_intensity((13, "Kb3"), 5.0, 0.7) builder.add_intensity((13, "Ll"), 3.0, 0.1) return builder
def options(): """ Creates basic options using the mock program. """ 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"] return Options(program, beam, sample, analyses, tags)
def builder(): detector = PhotonDetector("det", 1.1, 2.2) analysis = KRatioAnalysis(detector) builder = KRatioResultBuilder(analysis) builder.add_kratio((13, "Ka1"), 1.0, 1.0) builder.add_kratio((13, "Ka2"), 2.0, 1.0) builder.add_kratio((13, "Kb1"), 4.0, 1.0) builder.add_kratio((13, "Kb3"), 5.0, 1.0) builder.add_kratio((13, "Ll"), 3.0, 1.0) return builder
def testbuild5(self): b = OptionsBuilder() b.add_program(self.program) b.add_beam(self.create_basic_beam()) b.add_sample(self.create_basic_sample()) det = self.create_basic_photondetector() b.add_analysis(KRatioAnalysis(det)) det = PhotonDetector(3.3, 4.4) b.add_analysis(KRatioAnalysis(det)) self.assertEqual(4, len(b)) self.assertEqual(4, len(b.build()))
def create_basic_photondetector(self): return PhotonDetector('xray', math.radians(40.0))
def test_photonintensityanalyisbuilder_differentdetector(builder, detector): builder.add_photon_detector(detector) builder.add_photon_detector(PhotonDetector("det2", math.radians(41.0))) assert len(builder) == 2 assert len(builder.build()) == 2
def test_photonintensityanalyisbuilder_samedetector(builder, detector): builder.add_photon_detector(detector) builder.add_photon_detector(PhotonDetector("det", math.radians(40.0))) assert len(builder) == 1 assert len(builder.build()) == 1
def detector(): return PhotonDetector("det", math.radians(40.0))
beam = GaussianBeam(15e3, 10e-9) mat1 = Material.pure(29) mat2 = Material.from_formula('SiO2') mat3 = Material('Stuff', {27: 0.5, 25: 0.2, 8: 0.3}, 4500.0) sample = SubstrateSample(mat2) #sample = HorizontalLayerSample(mat3) #sample.add_layer(mat2, 10e-9) #sample.add_layer(mat3, 25e-9) #sample = VerticalLayerSample(mat1, mat2) #sample.add_layer(mat3, 10e-9) photon_detector = PhotonDetector('xray', math.radians(35.0)) analysis = KRatioAnalysis(photon_detector) limit = ShowersLimit(1000) options = Options(program, beam, sample, [analysis], [limit]) # Run simulation from pymontecarlo.runner.local import LocalSimulationRunner from pymontecarlo.project import Project project = Project() project.filepath = '/tmp/example.mcsim' with LocalSimulationRunner(project, max_workers=3) as runner: futures = runner.submit(options)
def main(argv=None): options_builder = OptionsBuilder() program = Casino2Program() program.number_trajectories = 1000 options_builder.add_program(program) beam_builder = GaussianBeamBuilder() beam_builder.add_energy_eV(15e3) beam_builder.add_diameter_m(10e-9) beam_builder.add_linescan_x(-1e-07, 1e-07, 5e-08) beams = beam_builder.build() print("beams", beams) options_builder.beams.extend(beams) mat1 = Material.pure(26) mat2 = Material.pure(14) sample = VerticalLayerSample(mat1, mat2) options_builder.add_sample(sample) photon_detector = PhotonDetector("xray", math.radians(35.0)) # analysis = KRatioAnalysis(photon_detector) analysis = PhotonIntensityAnalysis(photon_detector) options_builder.add_analysis(analysis) list_options = options_builder.build() # Run simulation project = Project() # for options in opt: with LocalSimulationRunner(project, max_workers=3) as runner: futures = runner.submit(*list_options) print("{} simulations launched".format(len(futures))) while not runner.wait(1): print(runner.progress) print("{} simulations succeeded".format(runner.done_count)) print("{} simulations failed".format(runner.failed_count)) for future in runner.failed_futures: print(future.exception()) # Results project.recalculate() print("{} were simulated".format(len(project.simulations))) ################################################################################################ ### Ab hier wird es relevant ### ################################################################################################ # Hier werden die Ergebnisse gespeichert data_all = [] for simulation in project.simulations: # results = simulation.results position = simulation.options.beam.x0_m for unkresult in simulation.find_result(EmittedPhotonIntensityResult): for xrayline, unkintensity in unkresult.items(): # z = xrayline.atomic_number data = unkintensity.nominal_value data_all.append((xrayline, position, data)) results = {} # Zu jeder simulierten Röntgenlinie wird der Datensatz in einem Dictionary gespeichert for x, p, d in data_all: if not x in results: results[x] = [] results[x].append((p, d)) # Sortiere Werte und gebe sie aus sorted_results = [] for k, v in results.items(): v.sort(key=lambda t: t[0]) print("{}\t{}".format(k, v)) print(v) plot_results(k, v) sorted_results.append((k, v)) return sorted_results print(sorted_results)