Beispiel #1
0
async def test_exporter_inclusion(event_loop, exporter, options, tmp_path, dry_run):
    mat1 = Material("Mat1", {79: 0.5, 47: 0.5}, 2.0)
    mat2 = Material("Mat2", {29: 0.5, 30: 0.5}, 3.0)
    options.sample = InclusionSample(mat1, mat2, 10e-6)

    await exporter.export(options, tmp_path, dry_run=dry_run)
    _test_export(tmp_path, 2, 2, dry_run)
Beispiel #2
0
async def test_exporter_verticallayers_couple(
    event_loop, exporter, options, tmp_path, dry_run
):
    mat1 = Material("Mat1", {79: 0.5, 47: 0.5}, 2.0)
    mat2 = Material("Mat2", {29: 0.5, 30: 0.5}, 3.0)
    options.sample = VerticalLayerSample(mat1, mat2)

    await exporter.export(options, tmp_path, dry_run=dry_run)
    _test_export(tmp_path, 2, 2, dry_run)
Beispiel #3
0
    def test__eq__(self):
        m2 = Material('Pure Cu', {29: 1.0}, 8960.0)
        self.assertEqual(m2, self.m)

        m2 = Material('Pure Cu', {29: 1.0}, 8961.0)
        self.assertNotEqual(m2, self.m)

        m2 = Material('Pure Cu', {29: 0.5, 30: 0.5}, 8960.0)
        self.assertNotEqual(m2, self.m)
Beispiel #4
0
async def test_exporter_horizontallayers(
    event_loop, exporter, options, tmp_path, dry_run
):
    mat1 = Material("Mat1", {79: 0.5, 47: 0.5}, 2.0)
    mat2 = Material("Mat2", {29: 0.5, 30: 0.5}, 3.0)
    options.sample = HorizontalLayerSample(mat1)
    options.sample.add_layer(mat2, 10e-9)

    await exporter.export(options, tmp_path, dry_run=dry_run)
    _test_export(tmp_path, 2, 2, dry_run)
Beispiel #5
0
def generate_end_materials(elements_data, colors, density_start = None, density_end = None):
    material_start = Material(
                                name = 'substrate_start',
                                composition = to_weight_fraction(dict(elements_data.iloc[0])), # create the first region
                                density_kg_per_m3 = density_start,
                                color = colors[0] if colors else None)
    material_end = Material(
                                name = 'substrate_end',
                                composition = to_weight_fraction(dict(elements_data.iloc[-1])), # create the last region
                                density_kg_per_m3 = density_end,
                                color = colors[-1] if colors else None)
    return material_start, material_end
Beispiel #6
0
    def testexport_multilayers2(self):
        # Options
        mat1 = Material('Mat1', {79: 0.5, 47: 0.5}, 2.0)
        mat2 = Material('Mat2', {29: 0.5, 30: 0.5}, 3.0)
        mat3 = Material('Mat3', {13: 0.5, 14: 0.5}, 4.0)

        sample = HorizontalLayerSample()
        sample.add_layer(mat1, 15e-9)
        sample.add_layer(mat2, 25e-9)
        sample.add_layer(mat3, 55e-9)
        self.options.sample = sample

        # Export
        self.e.export(self.options, self.tmpdir)

        # Test
        filepaths = glob.glob(os.path.join(self.tmpdir, '*.sim'))
        self.assertEqual(1, len(filepaths))

        casfile = File()
        casfile.readFromFilepath(filepaths[0])
        simdata = casfile.getOptionSimulationData()
        regionops = simdata.getRegionOptions()

        self.assertEqual(3, regionops.getNumberRegions())

        region = regionops.getRegion(0)
        elements = list(map(operator.attrgetter('Z'), region.getElements()))
        self.assertAlmostEqual(mat1.density_kg_per_m3 / 1000.0, region.Rho, 4)
        self.assertEqual('Mat1', region.Name)
        self.assertEqual(2, len(elements))
        self.assertTrue(79 in elements)
        self.assertTrue(47 in elements)

        region = regionops.getRegion(1)
        elements = list(map(operator.attrgetter('Z'), region.getElements()))
        self.assertAlmostEqual(mat2.density_kg_per_m3 / 1000.0, region.Rho, 4)
        self.assertEqual('Mat2', region.Name)
        self.assertEqual(2, len(elements))
        self.assertTrue(29 in elements)
        self.assertTrue(30 in elements)

        region = regionops.getRegion(2)
        elements = list(map(operator.attrgetter('Z'), region.getElements()))
        self.assertAlmostEqual(mat3.density_kg_per_m3 / 1000.0, region.Rho, 4)
        self.assertEqual('Mat3', region.Name)
        self.assertEqual(2, len(elements))
        self.assertTrue(13 in elements)
        self.assertTrue(14 in elements)
Beispiel #7
0
def create_model_sample(data, density_start = None, density_end = None):
    elements_data = get_elements_data(data)
    
    colors = generate_colors(steps = len(data) - 1)
    densities = generate_densities(elements_data, density_start, density_end)
    
    material_start, material_end = generate_end_materials(elements_data, colors, density_start, density_end)
    
    sample = VerticalLayerSample(material_start, material_end)
    
    # Iterate through all regions
    for i in range(1, len(data) - 1): # create the remaining regions (in the middle)
        density = densities[i]
        color = colors[i]
        material = Material(
                            name = f'region_{i}',
                            composition = to_weight_fraction(dict(elements_data.iloc[i])),
                            density_kg_per_m3 = density,
                            color = color)
        sample.add_layer(material, thickness_m = data[THICKNESS].iloc[i] / 1e6) # thickness is in micrometer
        
    # creating dummy materials to match template
    dummy_material = Material.from_formula('H', color = None, density_kg_per_m3 = MIN_DENSITY)
    for i in range(TEMPLATE_NUM_REGIONS - len(data)):
        sample.add_layer(dummy_material, thickness_m = MIN_THICKNESS)
    
    return sample
Beispiel #8
0
def create_homogeneous_sample(data, density_start = None, density_end = None):
    '''
    Function to create a list of substrate samples for Xray signal reference
    input: dataframe of atomic fractions at various positions
    output: list[Substrate]
    '''
    elements_data = get_elements_data(data)
    
    colors = generate_colors(steps = len(data) - 1)
    densities = generate_densities(elements_data, density_start, density_end)

    # 1. Creating materials at both ends
    material_start, material_end = generate_end_materials(elements_data, colors, density_start, density_end)
    materials = [material_start, material_end]
    
     # 2. Iterate through all regions, create the material representations
    for i in range(1, len(data) - 1):
        density = densities[i]
        color = colors[i]
        material = Material(
                            name = f'region_{i}',
                            composition = to_weight_fraction(dict(elements_data.iloc[i])),
                            density_kg_per_m3 = density,
                            color = color)
        materials.append(material)
        
    # 3. Creating substrate sample from the materials
    substrate_list = [SubstrateSample(mat) for mat in materials]

    return substrate_list
Beispiel #9
0
    def testexport_spectrum(self):
        # Create options
        mat = Material({
            79: 0.5,
            47: 0.5
        },
                       'Mat1',
                       absorption_energy_eV={ELECTRON: 123.0})

        ops = Options()
        ops.beam.energy_eV = 1234
        ops.beam.diameter_m = 25e-9
        ops.geometry.body.material = mat
        ops.limits.add(ShowersLimit(5678))
        ops.detectors['spectrum'] = \
            PhotonSpectrumDetector((radians(30), radians(40)), (0, radians(360.0)),
                                   500, (0, 1234))

        # Export to WinX-Ray options
        wxrops = self.e.export_wxroptions(ops)

        # Test
        self.assertTrue(wxrops.isXrayCompute())
        self.assertTrue(wxrops.isXrayComputeCharacteristic())
        self.assertAlmostEqual(35.0, wxrops.getTOA_deg(), 4)
        self.assertAlmostEqual(55, wxrops.getAngleThetaDetector_deg(), 4)
        self.assertAlmostEqual(180.0, wxrops.getAnglePhiDetector_deg(), 4)
        self.assertEqual(EvPerChannel.TYPE_5, wxrops.getTypeEVChannel())
        self.assertEqual(246, wxrops.getNumberChannel())
Beispiel #10
0
    def testvalidate_material_exception(self):
        material = Material(' ', {120: 0.5}, -1.0, 'blah')
        self.assertRaises(ValidationError, self.v.validate_material, material,
                          self.options)

        errors = set()
        self.v._validate_material(material, self.options, errors)
        self.assertEqual(5, len(errors))
Beispiel #11
0
def test_material_getparameters_validate(parameter_types, error_count):
    material = Material("TiCrCu", {22: 0.2, 24: 0.2, 29: 0.6})
    parameters = material.get_parameters(lambda options: material)

    # Set parameter types
    for parameter, parameter_type in zip(parameters, parameter_types):
        parameter.type_ = parameter_type

    errors = parameters[0].validate(None)
    assert len(errors) == error_count
Beispiel #12
0
    def setUp(self):
        unittest.TestCase.setUp(self)

        self.tmpdir = tempfile.mkdtemp()

        self.e = Exporter()

        self.ops = Options('aatest')
        self.ops.beam.energy_eV = 4e3
        self.ops.geometry.body.material = \
            Material({6: 0.4, 13: 0.6}, absorption_energy_eV={ELECTRON: 234.0})
        self.ops.limits.add(ShowersLimit(1234))
Beispiel #13
0
    def testexport_substrate(self):
        # Create options
        mat = Material({
            79: 0.5,
            47: 0.5
        },
                       'Mat1',
                       absorption_energy_eV={ELECTRON: 123.0})

        ops = Options()
        ops.beam.energy_eV = 1234
        ops.beam.diameter_m = 25e-9
        ops.beam.origin_m = (100e-9, 0, 1)
        ops.geometry.body.material = mat
        ops.limits.add(ShowersLimit(5678))
        ops.detectors['bse'] = BackscatteredElectronEnergyDetector(
            123, (0, 567))
        ops.detectors['bse polar'] = BackscatteredElectronPolarAngularDetector(
            125)
        ops.detectors['xrays'] = \
            PhotonIntensityDetector((radians(30), radians(40)), (0, radians(360.0)))
        ops.detectors['prz'] = \
            PhiZDetector((radians(30), radians(40)), (0, radians(360.0)), 750)

        # Export to WinX-Ray options
        wxrops = self.e.export_wxroptions(ops)

        # Test
        self.assertAlmostEqual(1.234, wxrops.getIncidentEnergy_keV(), 4)
        self.assertAlmostEqual(25.0, wxrops.getBeamDiameter_nm(), 4)
        self.assertEqual(5678, wxrops.getNbElectron())

        zs, _wfs = wxrops.getElements()
        self.assertTrue(79 in zs)
        self.assertTrue(47 in zs)
        self.assertAlmostEqual(13.6007, wxrops.getMeanDensity_g_cm3(),
                               4)  # internally calculated by WinXray
        self.assertAlmostEqual(123, wxrops.getMinimumElectronEnergy_eV(), 4)

        self.assertTrue(wxrops.isComputeBSEDistribution())
        self.assertTrue(wxrops.isComputeBSEEnergy())
        self.assertEqual(123, wxrops.getNbBSEEnergy())
        self.assertTrue(wxrops.isComputeBSEAngular())
        self.assertEqual(125, wxrops.getNbBSEAngular())

        self.assertTrue(wxrops.isXrayCompute())
        self.assertTrue(wxrops.isXrayComputeCharacteristic())
        self.assertAlmostEqual(35.0, wxrops.getTOA_deg(), 4)
        self.assertAlmostEqual(55, wxrops.getAngleThetaDetector_deg(), 4)
        self.assertAlmostEqual(180.0, wxrops.getAnglePhiDetector_deg(), 4)
        self.assertEqual(750, wxrops.getNumberFilm())
Beispiel #14
0
    def _validate_material(self, material, options, errors):
        if material is VACUUM:
            return material

        name = \
            self._validate_material_base_name(material.name, options, errors)
        composition = \
            self._validate_material_base_composition(material.composition, options, errors)
        density_kg_per_m3 = \
            self._validate_material_base_density_kg_per_m3(material.density_kg_per_m3, options, errors)
        color = \
            self._validate_material_base_color(material.color, options, errors)

        return Material(name, composition, density_kg_per_m3, color)
Beispiel #15
0
    def setUp(self):
        unittest.TestCase.setUp(self)

        self.ops = Options('aatest')
        self.ops.beam.energy_eV = 4e3
        self.ops.geometry.material = \
            Material({6: 0.4, 13: 0.6}, absorption_energy_eV={ELECTRON: 234.0})
        self.ops.detectors['xray'] = PhotonIntensityDetector((0, 1), (2, 3))
        self.ops.detectors['prz'] = PhiZDetector((0, 1), (2, 3), 128)
        self.ops.limits.add(ShowersLimit(1234))

        self.i = Importer()

        self._testdata = os.path.join(os.path.dirname(__file__), 'testdata')
Beispiel #16
0
    def setUp(self):
        TestCase.setUp(self)

        self.outputdir = tempfile.mkdtemp()
        self.workdir = tempfile.mkdtemp()

        ops = Options('test')
        ops.beam.origin_m = (0.0, 0.0, 0.001)
        ops.geometry.body.material = \
            Material({79: 1.0}, absorption_energy_eV={ELECTRON: 56.0})
        ops.detectors['time'] = TimeDetector()
        ops.limits.add(ShowersLimit(1))
        self.ops = Converter().convert(ops)[0]

        self.worker = Worker(program)
Beispiel #17
0
    def materials(self):
        try:
            name = self.field_name.name()

            composition = self.tbl_composition.composition()
            if not composition:
                return ()

            density_kg_per_m3 = self.field_density.density_kg_per_m3()

            color = self.field_color.color()

            return (Material(name, composition, density_kg_per_m3, color), )
        except:
            return ()
Beispiel #18
0
def test_material_getparameters_parametertype(parameter_types,
                                              expected_composition):
    material = Material("TiCrCu", {22: 0.2, 24: 0.2, 29: 0.6})
    parameters = material.get_parameters(lambda options: material)

    # Set parameter types
    for parameter, parameter_type in zip(parameters, parameter_types):
        parameter.type_ = parameter_type

    # Set titanium concentration
    parameters[0].set_value(None, 0.5)

    # Test
    for z, expected_wf in expected_composition.items():
        assert material.composition[z] == pytest.approx(expected_wf, abs=1e-6)
Beispiel #19
0
    def setUp(self):
        TestCase.setUp(self)

        self.outputdir = tempfile.mkdtemp()
        self.workdir = tempfile.mkdtemp()

        ops = Options('test')
        ops.beam = PencilBeam(5e3)
        ops.geometry.body.material = \
            Material({6: 0.4, 13: 0.6}, absorption_energy_eV={ELECTRON: 234.0})
        ops.detectors['xray'] = \
            PhotonIntensityDetector.annular(d2r(40.0), d2r(5.0))
        ops.limits.add(ShowersLimit(1))
        self.ops = Converter().convert(ops)[0]

        self.worker = Worker(program)
Beispiel #20
0
    def testexport_models(self):
        # Create options
        mat = Material({
            79: 0.5,
            47: 0.5
        },
                       'Mat1',
                       absorption_energy_eV={ELECTRON: 123.0})

        ops = Options()
        ops.beam.energy_eV = 1234
        ops.beam.diameter_m = 25e-9
        ops.geometry.body.material = mat
        ops.limits.add(ShowersLimit(5678))

        ops.models.add(ELASTIC_CROSS_SECTION.rutherford)
        ops.models.add(IONIZATION_CROSS_SECTION.casnati1982)
        ops.models.add(IONIZATION_POTENTIAL.joy_luo1989)
        ops.models.add(RANDOM_NUMBER_GENERATOR.press1966_rand3)
        ops.models.add(DIRECTION_COSINE.demers2000)
        ops.models.add(MASS_ABSORPTION_COEFFICIENT.thinh_leroux1979)

        # Export to WinX-Ray options
        wxrops = self.e.export_wxroptions(ops)

        # Test
        self.assertEqual(ElectronElasticCrossSection.TYPE_RUTHERFORD,
                         wxrops.getTypePartialCrossSection())
        self.assertEqual(ElectronElasticCrossSection.TYPE_RUTHERFORD,
                         wxrops.getTypeTotalCrossSection())
        self.assertEqual(IonizationCrossSection.TYPE_CASNATI,
                         wxrops.getTypeXrayCrossSectionBremsstrahlung())
        self.assertEqual(IonizationCrossSection.TYPE_CASNATI,
                         wxrops.getTypeXrayCrossSectionCharacteristic())
        self.assertEqual(IonizationPotential.TYPE_JOY_LUO,
                         wxrops.getTypeIonisationPotential())
        self.assertEqual(DirectionCosine.TYPE_DEMERS,
                         wxrops.getTypeDirectionCosines())
        self.assertEqual(EnergyLoss.TYPE_JOY_LUO, wxrops.getTypeEnergyLoss())
        self.assertEqual(RandomNumberGenerator.TYPE_RAN3,
                         wxrops.getTypeRandomGenerator())
        self.assertEqual(MassAbsorptionCoefficient.TYPE_THINH_LEROUX,
                         wxrops.getTypeMac())
Beispiel #21
0
    def setUp(self):

        # test data
        self.perspectives = (Perspective.XZ, Perspective.YZ, Perspective.XY)

        self.mat_ds = Material('Ds', {110: 1.}, 1.)
        self.mat_rg = Material('Rg', {111: 1.}, 1.)
        self.mat_au = Material('Au', {79: 1.}, 1.)

        self.layer = [
            Layer(Material('Re', {75: 1.}, 1.), .1),
            Layer(Material('Os', {76: 1.}, 1.), .15),
            Layer(Material('Ir', {77: 1.}, 1.), .2),
            Layer(Material('Pt', {78: 1.}, 1.), .05)
        ]

        # matplotlib
        self.figure = figure.Figure()
        self.ax = self.figure.add_subplot(111)
Beispiel #22
0
    def testexport_different_opening(self):
        # Create options
        mat = Material({
            79: 0.5,
            47: 0.5
        },
                       'Mat1',
                       absorption_energy_eV={ELECTRON: 123.0})

        ops = Options()
        ops.beam.energy_eV = 1234
        ops.beam.diameter_m = 25e-9
        ops.geometry.body.material = mat
        ops.limits.add(ShowersLimit(5678))
        ops.detectors['xrays'] = \
            PhotonIntensityDetector((radians(30), radians(40)), (0, radians(360.0)))
        ops.detectors['prz'] = \
            PhiZDetector((radians(30), radians(50)), (0, radians(360.0)), 750)

        # Test
        self.assertRaises(ExporterException, self.e.export_wxroptions, ops)
Beispiel #23
0
def test_material_advanced_widget_setMaterial(qtbot, material_advanced_widget):
    material = Material("foo", {13: 1.0}, 9000)
    material_advanced_widget.setMaterial(material)

    widget = material_advanced_widget.field_name.suffixWidget()
    assert not widget.isChecked()

    widget = material_advanced_widget.field_name.widget()
    assert widget.text() == material.name

    widget = material_advanced_widget.field_density.suffixWidget()
    assert widget.isChecked()

    widget = material_advanced_widget.field_density.widget()
    assert widget.value() == pytest.approx(material.density_g_per_cm3, abs=1e-4)

    composition = material_advanced_widget.tbl_composition.composition()
    assert composition == material.composition

    materials = material_advanced_widget.materials()

    assert len(materials) == 1
    assert materials[0] == material
Beispiel #24
0
def lazy_material():
    return Material("test", {29: 1.0})
Beispiel #25
0
def test_material_eq(material):
    assert material == Material("Pure Cu", {29: 1.0}, 8960.0)

    assert not material == Material("Pure Cu", {29: 1.0}, 8961.0)
    assert not material == Material("Pure Cu", {29: 0.5, 30: 0.5}, 8960.0)
Beispiel #26
0
async def test_exporter_sphere(event_loop, exporter, options, tmp_path, dry_run):
    mat1 = Material("Mat1", {79: 0.5, 47: 0.5}, 2.0)
    options.sample = SphereSample(mat1, 250e-6)

    await exporter.export(options, tmp_path, dry_run=dry_run)
    _test_export(tmp_path, 1, 1, dry_run)
Beispiel #27
0
@pytest.fixture
def ax():
    fig, ax = plt.subplots(1, 1)

    yield ax

    plt.close()
    del fig


@pytest.fixture
def samplefigure():
    return SampleFigure()


MATERIAL_DS = Material("Ds", {110: 1.0}, 1.0)
MATERIAL_RG = Material("Rg", {111: 1.0}, 1.0)
MATERIAL_AU = Material("Au", {79: 1.0}, 1.0)

LAYERS = [
    Layer(MATERIAL_DS, 0.1),
    Layer(MATERIAL_RG, 0.15),
    Layer(MATERIAL_AU, 0.2),
    Layer(MATERIAL_DS, 0.05),
]


def test_samplefigure_draw_nothing(samplefigure, ax):
    samplefigure.draw(ax)
    assert len(ax.collections) == 0
Beispiel #28
0
            fp.write(b'\x00\x00\x00\x00')

            # DELPHI: Inc(s,l);Blockwrite(Matfile,rho[0],l,f);Inc(Sum,f);
            fp.write(struct.pack('f', density_g_cm3))

            # DELPHI:
            # l:=NC+1;
            # for i:=0 to NP do begin
            #     Inc(s,l);BlockWrite(Matfile,PC[i],l,f);Inc(Sum,f);
            # end;
            fp.write(struct.pack('b', len(composition)))
            fp.write(b'\x00' * len(composition))

            # DELPHI: Inc(s,l);BlockWrite(Matfile,PM,l,f);Inc(Sum,f);
            fp.write(struct.pack('b', len(composition)))
            for i in range(len(composition)):
                fp.write(struct.pack('b', i + 1))

        return filepath


if __name__ == '__main__':
    from pymontecarlo.options.options import Options
    from pymontecarlo.options.material import Material

    options = Options('test')
    options.geometry.body.material = Material({6: 0.4, 13: 0.1, 79: 0.5})
    options.limits.add(ShowersLimit(1000))

    Exporter().export(options, '/tmp')
Beispiel #29
0
 def testconvert(self):
     handler = MaterialHtmlHandler()
     material = Material('Brass', {29: 0.5, 30: 0.5}, 8960.0)
     root = handler.convert(material)
     self.assertEqual(1, len(root.children))
Beispiel #30
0
    def setUp(self):
        super().setUp()

        self.m = Material('Pure Cu', {29: 1.0}, 8960.0, '#FF0000')