class TestMaterialXMLHandler(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.h = MaterialXMLHandler() self.obj = Material({'Cu': 1.0}, 'Pure Cu', 8960.0, {POSITRON: 52.0}) etree.register_namespace('mc', 'http://pymontecarlo.sf.net') source = BytesIO(b'<mc:material xmlns:mc="http://pymontecarlo.sf.net" density="8960.0" name="Pure Cu"><composition><element weightFraction="1.0" z="29" /></composition><absorptionEnergy particle="positron">52.0</absorptionEnergy></mc:material>') self.element = etree.parse(source).getroot() def tearDown(self): unittest.TestCase.tearDown(self) def testcan_parse(self): self.assertTrue(self.h.can_parse(self.element)) def testparse(self): obj = self.h.parse(self.element) self.assertEqual('Pure Cu', str(obj)) self.assertIn(29, obj.composition) self.assertAlmostEqual(1.0, obj.composition[29], 4) self.assertAlmostEqual(8960.0, obj.density_kg_m3, 4) self.assertAlmostEqual(52, obj.absorption_energy_eV[POSITRON], 4) def testcan_convert(self): self.assertTrue(self.h.can_convert(self.obj)) def testconvert(self): element = self.h.convert(self.obj) self.assertEqual('Pure Cu', element.get('name')) children = list(element.find('composition')) self.assertEqual(1, len(children)) self.assertEqual(29, int(children[0].get('z'))) self.assertEqual('1.0', children[0].get('weightFraction')) self.assertEqual('8960.0', element.get('density')) children = list(element.findall('absorptionEnergy')) self.assertEqual(3, len(children)) for child in children: if child.get('particle') == 'positron': self.assertAlmostEqual(52.0, float(child.text), 4) else: self.assertAlmostEqual(Material.DEFAULT_ABSORPTION_ENERGY_eV, float(child.text), 4)
def parse(self, element): mat = _MaterialXMLHandler.parse(self, element) elastic_scattering = (float(element.get('c1')), float(element.get('c2'))) cutoff_energy_inelastic_eV = float(element.get('wcc')) cutoff_energy_bremsstrahlung_eV = float(element.get('wcr')) interaction_forcings = [] for subelement in element.findall('interactionForcing'): particle = _PARTICLES_LOOKUP[subelement.get('particle')] collision = _COLLISIONS_LOOKUP[subelement.get('collision')] forcer = float(subelement.get('forcer')) weight = (float(subelement.get('wlow')), float(subelement.get('whigh'))) interaction_forcings.append( InteractionForcing(particle, collision, forcer, weight)) maximum_step_length_m = float(element.get('dsmax')) return PenelopeMaterial(mat.composition, mat.name, mat.density_kg_m3, mat.absorption_energy_eV, elastic_scattering, cutoff_energy_inelastic_eV, cutoff_energy_bremsstrahlung_eV, interaction_forcings, maximum_step_length_m)
def setUp(self): unittest.TestCase.setUp(self) self.h = MaterialXMLHandler() self.obj = Material({'Cu': 1.0}, 'Pure Cu', 8960.0, {POSITRON: 52.0}) etree.register_namespace('mc', 'http://pymontecarlo.sf.net') source = BytesIO(b'<mc:material xmlns:mc="http://pymontecarlo.sf.net" density="8960.0" name="Pure Cu"><composition><element weightFraction="1.0" z="29" /></composition><absorptionEnergy particle="positron">52.0</absorptionEnergy></mc:material>') self.element = etree.parse(source).getroot()
def convert(self, obj): element = _MaterialXMLHandler.convert(self, obj) element.set('c1', str(obj.elastic_scattering.c1)) element.set('c2', str(obj.elastic_scattering.c2)) element.set('wcc', str(obj.cutoff_energy_inelastic_eV)) element.set('wcr', str(obj.cutoff_energy_bremsstrahlung_eV)) for intforce in obj.interaction_forcings: subelement = etree.SubElement(element, 'interactionForcing') subelement.set('particle', str(intforce.particle)) subelement.set('collision', str(intforce.collision)) subelement.set('forcer', str(intforce.forcer)) subelement.set('wlow', str(intforce.weight[0])) subelement.set('whigh', str(intforce.weight[1])) element.set('dsmax', str(obj.maximum_step_length_m)) return element
def parse(self, element): mat = _MaterialXMLHandler.parse(self, element) elastic_scattering = (float(element.get('c1')), float(element.get('c2'))) cutoff_energy_inelastic_eV = float(element.get('wcc')) cutoff_energy_bremsstrahlung_eV = float(element.get('wcr')) interaction_forcings = [] for subelement in element.findall('interactionForcing'): particle = _PARTICLES_LOOKUP[subelement.get('particle')] collision = _COLLISIONS_LOOKUP[subelement.get('collision')] forcer = float(subelement.get('forcer')) weight = (float(subelement.get('wlow')), float(subelement.get('whigh'))) interaction_forcings.append(InteractionForcing(particle, collision, forcer, weight)) maximum_step_length_m = float(element.get('dsmax')) return PenelopeMaterial(mat.composition, mat.name, mat.density_kg_m3, mat.absorption_energy_eV, elastic_scattering, cutoff_energy_inelastic_eV, cutoff_energy_bremsstrahlung_eV, interaction_forcings, maximum_step_length_m)