def _create_standard_options(self, options): builder = OptionsBuilder() builder.add_program(options.program) beam = GaussianBeam(energy_eV=options.beam.energy_eV, diameter_m=0.0, particle=options.beam.particle) builder.add_beam(beam) for material in options.sample.materials: for z in material.composition: standard = self.get_standard_material(z) builder.add_sample(SubstrateSample(standard)) for limit in options.limits: builder.add_limit(options.program, limit) for model in options.models: builder.add_model(options.program, model) analysis = PhotonIntensityAnalysis(self.photon_detector) builder.add_analysis(analysis) return builder.build()
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 test_compose_beam_gaussian(self): beam = GaussianBeam(energy_eV=1, diameter_m=1) sf = SampleFigure(None, [beam], []) for perspective in self.perspectives: c = sf._compose_beam_gaussian(beam, perspective, 2.0) self.assertEqual(len(c), 1)
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_beam_gaussian_invalid(exporter, options): beam = GaussianBeam(0.0, -1.0, "particle", float("inf"), float("nan")) erracc = ErrorAccumulator() exporter._validate_beam_gaussian(beam, options, erracc) assert len(erracc.exceptions) == 6 assert len(erracc.warnings) == 0
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 _convert_beam(self, options): if type(options.beam) is PencilBeam: old = options.beam options.beam = GaussianBeam(old.energy_eV, 0.0, old.particle, old.origin_m, old.direction, old.aperture_rad) self._warn( "Pencil beam converted to Gaussian beam with 0 m diameter") return _Converter._convert_beam(self, options)
def testcalculate(self): # Create options beam = GaussianBeam(20e3, 10.e-9) sample = SubstrateSample(Material.from_formula('CaSiO4')) limit = ShowersLimit(100) unkoptions = Options(self.program, beam, sample, [self.a], [limit]) list_standard_options = self.a.apply(unkoptions) self.assertEqual(3, len(list_standard_options)) # Create simulations 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]) unksim = create_simulation(unkoptions) stdsims = [ create_simulation(options) for options in list_standard_options ] sims = stdsims + [unksim] # Calculate newresult = self.a.calculate(unksim, sims) self.assertTrue(newresult) newresult = self.a.calculate(unksim, sims) self.assertFalse(newresult) # Test results = unksim.find_result(KRatioResult) self.assertEqual(1, len(results)) result = results[0] self.assertEqual(3, len(result)) q = result[('Ca', 'Ka')] self.assertAlmostEqual(0.303262, q.n, 4) self.assertAlmostEqual(0.019880, q.s, 4) q = result[('Si', 'Ka')] self.assertAlmostEqual(0.212506, q.n, 4) self.assertAlmostEqual(0.016052, q.s, 4) q = result[('O', 'Ka')] self.assertAlmostEqual(0.484232 / 0.470749, q.n, 4) self.assertAlmostEqual(0.066579, q.s, 4)
def run(): # pragma: no cover import sys from pymontecarlo.options.beam import GaussianBeam from pymontecarlo.options.sample import HorizontalLayerSample from pymontecarlo.options.material import Material app = QtWidgets.QApplication(sys.argv) widget = SampleFigureWidget() sample = HorizontalLayerSample(Material.pure(29)) sample.add_layer(Material.pure(30), 10e-9) widget.setSample(sample) beam = GaussianBeam(15e3, 5e-9) widget.addBeam(beam) mainwindow = QtWidgets.QMainWindow() mainwindow.setCentralWidget(widget) mainwindow.show() app.exec_()
def _convert_beam(self, options): if type(options.beam) is PencilBeam: old = options.beam options.beam = GaussianBeam(old.energy_eV, 0.0, old.particle, old.origin_m, old.direction, old.aperture_rad) self._warn( "Pencil beam converted to Gaussian beam with 0 m diameter") if not _Converter._convert_beam(self, options): return False if options.beam.energy_eV > 1e6: self._warn("Beam energy must be less than 1MeV", "This options definition was removed.") return False if options.beam.aperture_rad != 0.0: self._warn('Beam aperture is not supported.' "This options definition was removed.") return False return True
) def test_samplefigure_draw_sample(samplefigure, ax, sample, perspective, expected_path_count): samplefigure.sample = sample samplefigure.perspective = perspective samplefigure.draw(ax) assert len(ax.collections) == 1 assert len(ax.collections[0]._paths) == expected_path_count @pytest.mark.parametrize( "beam,perspective,expected_path_count", [ (GaussianBeam(1, 1), Perspective.XY, 1), (GaussianBeam(1, 1), Perspective.XZ, 1), (GaussianBeam(1, 1), Perspective.YZ, 1), (CylindricalBeam(1, 1), Perspective.XY, 1), (CylindricalBeam(1, 1), Perspective.XZ, 1), (CylindricalBeam(1, 1), Perspective.YZ, 1), (PencilBeam(1), Perspective.XY, 1), (PencilBeam(1), Perspective.XZ, 1), (PencilBeam(1), Perspective.YZ, 1), ], ) def test_samplefigure_draw_beam(samplefigure, ax, beam, perspective, expected_path_count): samplefigure.beams.append(beam) samplefigure.perspective = perspective
def create_basic_beam(self): return GaussianBeam(15e3, 10e-9)
def test_draw_beam(self): beam = GaussianBeam(energy_eV=1, diameter_m=1) sf = SampleFigure(None, [beam], []) sf._draw_beam(self.ax, beam, Perspective.XZ, 2.0) self.assertEqual(len(self.ax.collections), 1)
import math # Create options import pymontecarlo from pymontecarlo.options.beam import GaussianBeam from pymontecarlo.options.material import Material from pymontecarlo.options.sample import HorizontalLayerSample, SubstrateSample, VerticalLayerSample from pymontecarlo.options.detector import PhotonDetector from pymontecarlo.options.analysis import KRatioAnalysis from pymontecarlo.options.limit import ShowersLimit from pymontecarlo.options.options import Options program = pymontecarlo.settings.get_program('casino2') 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)
def plot(self): tilt_rad = math.radians(self._slider_tilt_deg.value()) rotation_rad = math.radians(self._slider_rotation_deg.value()) layer = [ Layer(RE, 10e-9), Layer(OS, 15e-9), Layer(IR, 20e-9), Layer(PT, 5e-9) ] sample_cls = self._combo_sample.currentData() if sample_cls == SubstrateSample: sample = SubstrateSample(DS, tilt_rad=tilt_rad, rotation_rad=rotation_rad) elif sample_cls == InclusionSample: sample = InclusionSample(DS, AU, 0.5e-6, tilt_rad=tilt_rad, rotation_rad=rotation_rad) elif sample_cls == HorizontalLayerSample: sample = HorizontalLayerSample(DS, layer, tilt_rad=tilt_rad, rotation_rad=rotation_rad) elif sample_cls == VerticalLayerSample: sample = VerticalLayerSample(DS, RG, layer, tilt_rad=tilt_rad, rotation_rad=rotation_rad) elif sample_cls == SphereSample: sample = SphereSample(AU, 0.5e-6, tilt_rad=tilt_rad, rotation_rad=rotation_rad) else: sample = None beam_cls = self._combo_beam.currentData() if beam_cls == GaussianBeam: beams = [GaussianBeam(42.0, 5e-9)] else: beams = [] # trajectory_cls = self._combo_trajectory.currentData() # TODO handle trajectories trajectories = [] sf = SampleFigure(sample, beams, trajectories) if self.radio_yz.isChecked(): sf.perspective = Perspective.YZ elif self.radio_xy.isChecked(): sf.perspective = Perspective.XY else: sf.perspective = Perspective.XZ self._figure.clf() ax = self._figure.add_subplot(111) ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) sf.draw(ax) scalebar = ScaleBar(1.0, location="lower left") ax.add_artist(scalebar) self._canvas.draw_idle()