def parse(self, element): geo = _GeometryXMLHandler.parse(self, element) materials_lookup = self._parse_materials(element) subelement = element.find('substrate') if subelement is None: substrate_material = VACUUM else: indexes = self._parse_numerical_parameter(subelement, 'material') substrate_material = list(map(materials_lookup.get, indexes)) obj = HorizontalLayers(substrate_material, None, geo.tilt_rad, geo.rotation_rad) subelement = element.find('layers') if subelement is None: raise ValueError("Element 'layers' not found") for subsubelement in subelement: indexes = self._parse_numerical_parameter(subsubelement, 'material') material = list(map(materials_lookup.get, indexes)) thickness_m = self._parse_numerical_parameter(subsubelement, 'thickness') obj.add_layer(material, thickness_m) return obj
def value(self): geometry = \ HorizontalLayers(substrate_material=self._lst_substrate.values(), tilt_rad=self._txt_tilt.values(), rotation_rad=self._txt_rotation.values()) for layer in self._lst_layer.values(): geometry.add_layer(layer.material, layer.thickness_m) return geometry
def setUp(self): TestCase.setUp(self) self.g1 = HorizontalLayers(Material.pure(29)) self.g2 = HorizontalLayers(None) # No substrate self.g3 = HorizontalLayers(Material.pure(29)) # Empty layer self.g1.add_layer(Material.pure(30), 123.456) self.g1.add_layer(Material.pure(31), 456.789) self.g2.add_layer(Material.pure(30), 123.456) self.g2.add_layer(Material.pure(31), 456.789) self.g3.add_layer(Material.pure(30), 123.456) self.g3.add_layer(Material.pure(31), 456.789) self.g3.add_layer(VACUUM, 456.123)
def setUp(self): unittest.TestCase.setUp(self) self.h = HorizontalLayersXMLHandler() self.obj1 = HorizontalLayers(Material.pure(29), None, 1.1, 2.2) self.obj1.add_layer(Material.pure(30), 123.456) self.obj1.add_layer(Material.pure(31), 456.789) self.obj2 = HorizontalLayers() self.obj2.add_layer(Material.pure(30), 123.456) self.obj2.add_layer(VACUUM, 50.0) self.obj2.add_layer(Material.pure(31), 456.789) etree.register_namespace('mc', 'http://pymontecarlo.sf.net') source = BytesIO(b'<mc:horizontalLayers xmlns:mc="http://pymontecarlo.sf.net" rotation="2.2" tilt="1.1"><materials><mc:material _index="1" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material><mc:material _index="2" density="5910.0" name="Gallium"><composition><element weightFraction="1.0" z="31" /></composition></mc:material><mc:material _index="3" density="8960.0" name="Copper"><composition><element weightFraction="1.0" z="29" /></composition></mc:material></materials><substrate material="3" /><layers><layer material="1" thickness="123.456" /><layer material="2" thickness="456.789" /></layers></mc:horizontalLayers>') self.element1 = etree.parse(source).getroot() etree.register_namespace('mc', 'http://pymontecarlo.sf.net') source = BytesIO(b'<mc:horizontalLayers xmlns:mc="http://pymontecarlo.sf.net" rotation="0.0" tilt="0.0"><materials><mc:material _index="1" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material><mc:material _index="2" density="5910.0" name="Gallium"><composition><element weightFraction="1.0" z="31" /></composition></mc:material></materials><layers><layer material="1" thickness="123.456" /><layer material="0" thickness="50.0" /><layer material="2" thickness="456.789" /></layers></mc:horizontalLayers>') self.element2 = etree.parse(source).getroot()
def testexport_horizontal_layers(self): # Create mat1 = PenelopeMaterial({79: 0.5, 47: 0.5}, 'mat1') mat2 = PenelopeMaterial({29: 0.5, 30: 0.5}, 'mat2') mat3 = PenelopeMaterial({13: 0.5, 14: 0.5}, 'mat3') ops = Options() ops.beam.energy_eV = 1234 ops.beam.diameter_m = 25e-9 ops.beam.origin_m = (100e-9, 0, 1) ops.geometry = HorizontalLayers(mat1) ops.geometry.add_layer(mat2, 52e-9) ops.geometry.add_layer(mat3, 25e-9) ops.limits.add(TimeLimit(100)) self.c._convert_geometry(ops) self.e.export_geometry(ops.geometry, self.tmpdir) # Test geofilepath = os.path.join(self.tmpdir, 'horizontallayers.geo') repfilepath = os.path.join(self.tmpdir, 'geometry.rep') nmat, nbody = pengeom.init(geofilepath, repfilepath) self.assertEqual(3, nmat) self.assertEqual(6, nbody) matfilepath = os.path.join(self.tmpdir, 'mat1.mat') self.assertTrue(os.path.exists(matfilepath)) matfilepath = os.path.join(self.tmpdir, 'mat2.mat') self.assertTrue(os.path.exists(matfilepath)) matfilepath = os.path.join(self.tmpdir, 'mat3.mat') self.assertTrue(os.path.exists(matfilepath))
class TestHorizontalLayers(TestCase): def setUp(self): TestCase.setUp(self) self.g1 = HorizontalLayers(Material.pure(29)) self.g2 = HorizontalLayers(None) # No substrate self.g3 = HorizontalLayers(Material.pure(29)) # Empty layer self.g1.add_layer(Material.pure(30), 123.456) self.g1.add_layer(Material.pure(31), 456.789) self.g2.add_layer(Material.pure(30), 123.456) self.g2.add_layer(Material.pure(31), 456.789) self.g3.add_layer(Material.pure(30), 123.456) self.g3.add_layer(Material.pure(31), 456.789) self.g3.add_layer(VACUUM, 456.123) def tearDown(self): TestCase.tearDown(self) def testskeleton(self): # Horizontal layers 1 self.assertTrue(self.g1.has_substrate()) self.assertEqual('Copper', str(self.g1.substrate.material)) self.assertEqual(2, len(self.g1.layers)) self.assertEqual('Zinc', str(self.g1.layers[0].material)) self.assertAlmostEqual(123.456, self.g1.layers[0].thickness_m, 4) self.assertEqual('Gallium', str(self.g1.layers[1].material)) self.assertAlmostEqual(456.789, self.g1.layers[1].thickness_m, 4) # Horizontal layers 2 self.assertFalse(self.g2.has_substrate()) self.assertEqual(2, len(self.g2.layers)) self.assertEqual('Zinc', str(self.g2.layers[0].material)) self.assertAlmostEqual(123.456, self.g2.layers[0].thickness_m, 4) self.assertEqual('Gallium', str(self.g2.layers[1].material)) self.assertAlmostEqual(456.789, self.g2.layers[1].thickness_m, 4) # Horizontal layers 3 self.assertTrue(self.g3.has_substrate()) self.assertEqual('Copper', str(self.g3.substrate.material)) self.assertEqual(3, len(self.g3.layers)) self.assertEqual('Zinc', str(self.g3.layers[0].material)) self.assertAlmostEqual(123.456, self.g3.layers[0].thickness_m, 4) self.assertEqual('Gallium', str(self.g3.layers[1].material)) self.assertAlmostEqual(456.789, self.g3.layers[1].thickness_m, 4) self.assertEqual('Vacuum', str(self.g3.layers[2].material)) self.assertAlmostEqual(456.123, self.g3.layers[2].thickness_m, 4) def testsubstrate(self): self.g1.substrate.material = VACUUM self.assertFalse(self.g1.has_substrate()) # Horizontal layers 1 self.assertEqual(float('-inf'), self.g1.substrate.xmin_m) self.assertEqual(float('inf'), self.g1.substrate.xmax_m) self.assertEqual(float('-inf'), self.g1.substrate.ymin_m) self.assertEqual(float('inf'), self.g1.substrate.ymax_m) self.assertEqual(float('-inf'), self.g1.substrate.zmin_m) self.assertAlmostEqual(-580.245, self.g1.substrate.zmax_m, 4) def testlayers(self): # Horizontal layers 1 self.assertEqual(float('-inf'), self.g1.layers[0].xmin_m) self.assertEqual(float('inf'), self.g1.layers[0].xmax_m) self.assertEqual(float('-inf'), self.g1.layers[0].ymin_m) self.assertEqual(float('inf'), self.g1.layers[0].ymax_m) self.assertAlmostEqual(-123.456, self.g1.layers[0].zmin_m, 4) self.assertAlmostEqual(0.0, self.g1.layers[0].zmax_m, 4) self.assertEqual(float('-inf'), self.g1.layers[1].xmin_m) self.assertEqual(float('inf'), self.g1.layers[1].xmax_m) self.assertEqual(float('-inf'), self.g1.layers[1].ymin_m) self.assertEqual(float('inf'), self.g1.layers[1].ymax_m) self.assertAlmostEqual(-580.245, self.g1.layers[1].zmin_m, 4) self.assertAlmostEqual(-123.456, self.g1.layers[1].zmax_m, 4) # Horizontal layers 2 self.assertEqual(float('-inf'), self.g2.layers[0].xmin_m) self.assertEqual(float('inf'), self.g2.layers[0].xmax_m) self.assertEqual(float('-inf'), self.g2.layers[0].ymin_m) self.assertEqual(float('inf'), self.g2.layers[0].ymax_m) self.assertAlmostEqual(-123.456, self.g2.layers[0].zmin_m, 4) self.assertAlmostEqual(0.0, self.g2.layers[0].zmax_m, 4) self.assertEqual(float('-inf'), self.g2.layers[1].xmin_m) self.assertEqual(float('inf'), self.g2.layers[1].xmax_m) self.assertEqual(float('-inf'), self.g2.layers[1].ymin_m) self.assertEqual(float('inf'), self.g2.layers[1].ymax_m) self.assertAlmostEqual(-580.245, self.g2.layers[1].zmin_m, 4) self.assertAlmostEqual(-123.456, self.g2.layers[1].zmax_m, 4) # Horizontal layers 3 self.assertEqual(float('-inf'), self.g3.layers[0].xmin_m) self.assertEqual(float('inf'), self.g3.layers[0].xmax_m) self.assertEqual(float('-inf'), self.g3.layers[0].ymin_m) self.assertEqual(float('inf'), self.g3.layers[0].ymax_m) self.assertAlmostEqual(-123.456, self.g3.layers[0].zmin_m, 4) self.assertAlmostEqual(0.0, self.g3.layers[0].zmax_m, 4) self.assertEqual(float('-inf'), self.g3.layers[1].xmin_m) self.assertEqual(float('inf'), self.g3.layers[1].xmax_m) self.assertEqual(float('-inf'), self.g3.layers[1].ymin_m) self.assertEqual(float('inf'), self.g3.layers[1].ymax_m) self.assertAlmostEqual(-580.245, self.g3.layers[1].zmin_m, 4) self.assertAlmostEqual(-123.456, self.g3.layers[1].zmax_m, 4) self.assertEqual(float('-inf'), self.g3.layers[2].xmin_m) self.assertEqual(float('inf'), self.g3.layers[2].xmax_m) self.assertEqual(float('-inf'), self.g3.layers[2].ymin_m) self.assertEqual(float('inf'), self.g3.layers[2].ymax_m) self.assertAlmostEqual(-1036.368, self.g3.layers[2].zmin_m, 4) self.assertAlmostEqual(-580.245, self.g3.layers[2].zmax_m, 4) def testget_bodies(self): self.assertEqual(3, len(self.g1.get_bodies())) self.assertEqual(2, len(self.g2.get_bodies())) self.assertEqual(4, len(self.g3.get_bodies())) def testget_materials(self): self.assertEqual(3, len(self.g1.get_materials())) self.assertEqual(2, len(self.g2.get_materials())) self.assertEqual(3, len(self.g3.get_materials())) def testexpand(self): self.assertEqual(1, len(expand(self.g1))) self.g1.add_layer(Material.pure(79), [1.0, 2.0]) self.assertEqual(2, len(expand(self.g1)))
class TestHorizontalLayersXMLHandler(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.h = HorizontalLayersXMLHandler() self.obj1 = HorizontalLayers(Material.pure(29), None, 1.1, 2.2) self.obj1.add_layer(Material.pure(30), 123.456) self.obj1.add_layer(Material.pure(31), 456.789) self.obj2 = HorizontalLayers() self.obj2.add_layer(Material.pure(30), 123.456) self.obj2.add_layer(VACUUM, 50.0) self.obj2.add_layer(Material.pure(31), 456.789) etree.register_namespace('mc', 'http://pymontecarlo.sf.net') source = BytesIO(b'<mc:horizontalLayers xmlns:mc="http://pymontecarlo.sf.net" rotation="2.2" tilt="1.1"><materials><mc:material _index="1" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material><mc:material _index="2" density="5910.0" name="Gallium"><composition><element weightFraction="1.0" z="31" /></composition></mc:material><mc:material _index="3" density="8960.0" name="Copper"><composition><element weightFraction="1.0" z="29" /></composition></mc:material></materials><substrate material="3" /><layers><layer material="1" thickness="123.456" /><layer material="2" thickness="456.789" /></layers></mc:horizontalLayers>') self.element1 = etree.parse(source).getroot() etree.register_namespace('mc', 'http://pymontecarlo.sf.net') source = BytesIO(b'<mc:horizontalLayers xmlns:mc="http://pymontecarlo.sf.net" rotation="0.0" tilt="0.0"><materials><mc:material _index="1" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material><mc:material _index="2" density="5910.0" name="Gallium"><composition><element weightFraction="1.0" z="31" /></composition></mc:material></materials><layers><layer material="1" thickness="123.456" /><layer material="0" thickness="50.0" /><layer material="2" thickness="456.789" /></layers></mc:horizontalLayers>') self.element2 = etree.parse(source).getroot() def tearDown(self): unittest.TestCase.tearDown(self) def testcan_parse(self): self.assertTrue(self.h.can_parse(self.element1)) self.assertTrue(self.h.can_parse(self.element2)) def testparse(self): # Horizontal layers 1 obj = self.h.parse(self.element1) self.assertTrue(obj.has_substrate()) self.assertEqual('Copper', str(obj.substrate.material)) self.assertEqual(2, len(obj.layers)) self.assertEqual('Zinc', str(obj.layers[0].material)) self.assertAlmostEqual(123.456, obj.layers[0].thickness_m) self.assertEqual('Gallium', str(obj.layers[1].material)) self.assertAlmostEqual(456.789, obj.layers[1].thickness_m) self.assertAlmostEqual(1.1, obj.tilt_rad, 4) self.assertAlmostEqual(2.2, obj.rotation_rad, 4) # Horizontal layers 2 obj = self.h.parse(self.element2) self.assertFalse(obj.has_substrate()) self.assertEqual(3, len(obj.layers)) self.assertEqual('Zinc', str(obj.layers[0].material)) self.assertAlmostEqual(123.456, obj.layers[0].thickness_m) self.assertEqual('Vacuum', str(obj.layers[1].material)) self.assertAlmostEqual(50.0, obj.layers[1].thickness_m) self.assertEqual('Gallium', str(obj.layers[2].material)) self.assertAlmostEqual(456.789, obj.layers[2].thickness_m) self.assertAlmostEqual(0.0, obj.tilt_rad, 4) self.assertAlmostEqual(0.0, obj.rotation_rad, 4) def testcan_convert(self): self.assertTrue(self.h.can_convert(self.obj1)) self.assertTrue(self.h.can_convert(self.obj2)) def testconvert(self): # Horizontal layers 1 element = self.h.convert(self.obj1) self.assertAlmostEqual(1.1, float(element.get('tilt')), 4) self.assertAlmostEqual(2.2, float(element.get('rotation')), 4) self.assertEqual(3, len(list(element.find('materials')))) self.assertEqual(2, len(list(element.find('layers')))) # Horizontal layers 2 element = self.h.convert(self.obj2) self.assertAlmostEqual(0.0, float(element.get('tilt')), 4) self.assertAlmostEqual(0.0, float(element.get('rotation')), 4) self.assertEqual(2, len(list(element.find('materials')))) self.assertEqual(3, len(list(element.find('layers'))))