Exemplo n.º 1
0
    def Construct(self):
        check_overlap = False
        many = False

        geometry_keys = list(self.conf['Geometry'].keys())
        if len(geometry_keys) != 1:
            raise ValueError('Must define exactly one top-level Geometry')
        world_name = geometry_keys[0]

        global geom_s, geom_l, geom_p
        geom_s = {}
        geom_l = {}
        geom_p = {}

        for geom in depth_first_tree_traversal(self.conf['Geometry']):
            geom_type = geom['Type']
            geom_name = geom['Name']
            parent_name = geom_name.rsplit('.', 1)[0]
            if parent_name in geom_p:
                parent_p = geom_p[parent_name]
            else:
                parent_p = None

            if geom_type == 'G4Box':
                solid = g4.G4Box(geom_name, geom['pX'] * mm, geom['pY'] * mm,
                                 geom['pZ'] * mm)
            elif geom_type == 'G4Tubs':
                solid = g4.G4Tubs(geom_name, geom['pRMin'] * mm,
                                  geom['pRMax'] * mm, geom['pDz'] * mm,
                                  geom['pSPhi'] * deg, geom['pDPhi'] * deg)
            elif geom_type == 'CadMesh':
                mesh = compton.cadmesh.TessellatedMesh(geom['File'])
                if 'SolidName' in geom:
                    solid = mesh.GetSolid(geom['SolidName'])
                else:
                    solid = mesh.GetSolid(0)
            else:
                raise ValueError(
                    "unimplemented geometry type '{}'".format(geom_type))

            logical = g4.G4LogicalVolume(
                solid, g4.gNistManager.FindOrBuildMaterial(geom['Material']),
                geom_name)
            transform = g4.G4Transform3D()
            if 'Transformation' in geom:
                for operation, value in zip(*geom['Transformation']):
                    translation = g4.G4ThreeVector()
                    rotation = g4.G4RotationMatrix()
                    if operation == 'TranslateX':
                        translation += g4.G4ThreeVector(value * mm, 0, 0)
                    elif operation == 'TranslateY':
                        translation += g4.G4ThreeVector(0, value * mm, 0)
                    elif operation == 'TranslateZ':
                        translation += g4.G4ThreeVector(0, 0, value * mm)
                    elif operation == 'RotateX':
                        rotation.rotateX(value * deg)
                    elif operation == 'RotateY':
                        rotation.rotateY(value * deg)
                    elif operation == 'RotateZ':
                        rotation.rotateZ(value * deg)
                    else:
                        assert (False)
                    transform = (g4.G4Transform3D(rotation, translation) *
                                 transform)
            if 'Rotation' in geom:
                euler = np.array(geom['Rotation']) * deg
                rotation = g4.G4RotationMatrix()
                rotation.rotateZ(euler[0])
                rotation.rotateY(euler[1])
                rotation.rotateZ(euler[2])
            else:
                rotation = g4.G4RotationMatrix()
            if 'Translation' in geom:
                translation = g4.G4ThreeVector(*np.array(geom['Translation']) *
                                               mm)
            else:
                translation = g4.G4ThreeVector()
            physical = g4.G4PVPlacement(
                g4.G4Transform3D(rotation, translation) * transform, geom_name,
                logical, parent_p, many, 0, check_overlap)

            if 'Visible' in geom:
                logical.SetVisAttributes(g4.G4VisAttributes(geom['Visible']))
            if 'Color' in geom:
                logical.SetVisAttributes(
                    g4.G4VisAttributes(g4.G4Color(*geom['Color'])))

            geom_s[geom_name] = solid
            geom_l[geom_name] = logical
            geom_p[geom_name] = physical

        return geom_p[world_name]
Exemplo n.º 2
0
    def ConstructDevice(self):
        """
        """
        # make colours
        white = g4.G4Color(1.0, 1.0, 1.0)
        orange = g4.G4Color(.75, .55, 0.0)
        world_visatt = g4.G4VisAttributes(True, white)
        process_space_visatt = g4.G4VisAttributes(False, orange)

        # ========
        #  World
        # ========
        world_length_x = settings.world_length_x * g4.micrometer
        world_length_y = settings.world_length_y * g4.micrometer
        world_length_z = settings.world_length_z * g4.micrometer

        world_solid = g4.G4Box("world_solid", world_length_x, world_length_y,
                               world_length_z)

        world_logical = g4.G4LogicalVolume(world_solid, self.world_material,
                                           "world_logical")
        world_logical.SetVisAttributes(world_visatt)

        self.world_physical = g4.G4PVPlacement(g4.G4Transform3D(),
                                               world_logical, "world_physical",
                                               None, False, 0)

        # ===============
        #  process space
        # ===============
        process_space_length_x = settings.process_space_length_x * g4.micrometer
        process_space_length_y = settings.process_space_length_y * g4.micrometer
        process_space_length_z = settings.process_space_length_z * g4.micrometer

        process_space_solid = g4.G4Box("process_space_solid",
                                       process_space_length_x,
                                       process_space_length_y,
                                       process_space_length_z)

        process_space_logical = g4.G4LogicalVolume(process_space_solid,
                                                   self.process_space_material,
                                                   "process_space_logical")
        process_space_logical.SetVisAttributes(process_space_visatt)

        process_space_pos = g4.G4ThreeVector(0, 0, -process_space_length_z)
        process_space_physical = g4.G4PVPlacement(None, process_space_pos,
                                                  "process_space_physical",
                                                  process_space_logical,
                                                  self.world_physical, False,
                                                  0, True)

        # ========================
        #  process wafer phantom
        # ========================
        voxel_length_x = process_space_length_x / self.n_voxel_x
        voxel_length_y = process_space_length_y / self.n_voxel_y
        voxel_length_z = process_space_length_z / self.n_voxel_z
        voxel_solid = g4.G4Box("voxel_solid", voxel_length_x, voxel_length_y,
                               voxel_length_z)
        self.voxel_logical = g4.G4LogicalVolume(voxel_solid,
                                                self.process_space_material,
                                                "voxel_logical")
        # voxel_logical.SetVisAttributes(g4.G4VisAttributes(G4VisAttributes::GetInvisible()))

        wafer = PhantomParameterisationColour()
        # set color
        for mat_name, color in settings.COLOR_MAP.items():
            cred = color[0]
            cgreen = color[1]
            cblue = color[2]
            copacity = color[3]
            g4_color = g4.G4Color(cred, cgreen, cblue, copacity)
            visAtt = g4.G4VisAttributes(g4_color)
            # visAtt.SetForceSolid(True)
            visAtt.SetVisibility(True)
            wafer.AddColor(mat_name, visAtt)

        wafer.SetVoxelDimensions(voxel_length_x, voxel_length_y,
                                 voxel_length_z)
        wafer.SetNoVoxel(self.n_voxel_x, self.n_voxel_y, self.n_voxel_z)
        wafer.SetMaterials(self.materials)
        wafer.SetMaterialIndices(self.material_IDs)
        wafer.BuildContainerSolid(process_space_physical)
        wafer.CheckVoxelsFillContainer(process_space_solid.GetXHalfLength(),
                                       process_space_solid.GetYHalfLength(),
                                       process_space_solid.GetZHalfLength())

        n_voxel = self.n_voxel_x * self.n_voxel_y * self.n_voxel_z
        wafer_physical = G4PVParameterised("wafer_physical",
                                           self.voxel_logical,
                                           process_space_logical,
                                           g4.G4global.EAxis.kXAxis, n_voxel,
                                           wafer, False)
        wafer_physical.SetRegularStructureId(1)

        return self.world_physical
Exemplo n.º 3
0
    def addVolume(self,
                  name,
                  material,
                  vol_type,
                  dimensions,
                  position,
                  colour='red'):
        '''
        Add a new volume to the Geometry

        Arguments
        ---------

        name       (string)                  Volume name
        
        material   (string)                  GEANT4 material, e.g. 'Si'

        vol_type   (string)                  Volume type, e.g. 'Box'

        dimensions (float, float, float)     Dimensions as either floats or 
                   (string, string, string)  strings. e.g. (1,1,2) or ('1cm', '1cm', '2cm')

        position   (float, float, float)     Position as either strings or floats
                   (string, string, string)

        Optional Arguments
        ------------------

        colour     (string)                  Colour of volume

        '''

        try:
            assert vol_type in _g4_vol_types
        except AssertionError as e:
            self._logger.error(
                "Volume of type '{}' not present in GEANT4".format(vol_type))
            raise e

        try:
            self._log_vols[name] = g4py.ezgeom.G4EzVolume(name)
            self._logger.info(
                'Creating {} Volume from {} of size ({}, {}, {})'.format(
                    vol_type, material, *dimensions))
            getattr(self._log_vols[name], 'Create{}Volume'.format(vol_type))(
                self._get_material(material), *self._parse_units(dimensions))
            _color = G4.G4Color(*_colour_dict[colour.lower()])
            self._log_vols[name].SetColor(_color)
            self._log_vols[name].PlaceIt(
                G4.G4ThreeVector(*self._parse_units(position)))

        except Exception as e:
            if isBoostArgumentError(e):
                self._logger.error('Invalid Arguments for Volume Creation')
                self._logger.error(_g4_vol_types[vol_type])
                self._logger.error(
                    'Arguments:\n\tMaterial: {}\n\tDimension: {}\n\tPosition: {}'
                    .format(
                        material, ', '.join(
                            [str(self._parse_units(i)) for i in dimensions]),
                        ', '.join(
                            [str(self._parse_units(i)) for i in position])))
            raise e
Exemplo n.º 4
0
    def __init__(self, parent, shift_z):
        nist = G4.G4NistManager.Instance()
        Vc = nist.FindOrBuildMaterial("G4_Galactic")
        Al = nist.FindOrBuildMaterial("G4_Al")
        Ge = nist.FindOrBuildMaterial("G4_Ge")
        Pg = nist.FindOrBuildMaterial("G4_PLEXIGLASS")
        Ml = nist.FindOrBuildMaterial("G4_MYLAR")
        Ai = nist.FindOrBuildMaterial("G4_AIR")
        A = 0.5 * 51.1 * mm  # Crystal Radius
        B = 72.9 * mm  # Crystal Length
        C = 0.5 * 8.8 * mm  # Hole Radius
        D = 64.0 * mm  # Hole Depth
        E = 0.5 * 8.8 * mm  # Nominal Hole End Radius
        F = 94. * mm  # CUP Length
        G = 3.0 * mm  # Top Gap between CUP and the Shell
        H1 = 0.03 * mm  # CUP Top Al Thickness
        H2 = 0.03 * mm  # CUP Top Maylar Thickness
        I = 1.0 * mm  # Shell Top Thickness
        J = 8.0 * mm  # Nominal Crystal End Radius
        K = 0.8 * mm  # CUP Walls Thickness
        L = 1.0 * mm  # Shell Walls Thickness
        M = 0.0003 * mm  # Outer Dead Layer Thickness
        N = 0.7 * mm  # Inner Dead Layer Thikness
        O = 3.0 * mm  # CUP Bottom Thickness
        P = 0.5 * 76.0 * mm  # Shell Radius
        Q = 120. * mm  # Shell Length
        Z = 0.0 * mm  # zero
        s, t = 0.0, 360.0 * deg  # start, turn

        # Detector Base
        base = G4.G4Tubs("base", Z, P, 0.5 * O, s, t)
        logBase = G4.G4LogicalVolume(base, Pg, "logBase")
        # Detector
        HPGe = G4.G4Tubs("HPGe", Z, P, 0.5 * Q, s, t)
        logHPGe = G4.G4LogicalVolume(HPGe, Vc, "logHPGe")
        # Making the Crystal shape
        crystal1 = G4.G4Tubs("cyl1", Z, A - J, 0.5 * B, s, t)
        crystal2 = G4.G4Tubs("cyl2", Z, A, 0.5 * (B - J), s, t)
        torroid1 = G4.G4Torus("tor1", Z, J, A - J, s, t)
        xyz = G4.G4ThreeVector(Z, Z, -0.5 * J)
        crystal3 = G4.G4UnionSolid("cry3", crystal1, crystal2, None, xyz)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * B - J)
        crystal4 = G4.G4UnionSolid("cry4", crystal3, torroid1, None, xyz)
        # Making the Active Crystal shape
        activeCrystal1 = G4.G4Tubs("acyl1", Z, A - J, 0.5 * (B - M), s, t)
        activeCrystal2 = G4.G4Tubs("acyl2", Z, A - M, 0.5 * (B - J), s, t)
        activeTor1 = G4.G4Torus("activeTor1", Z, J - M, A - J, s, t)
        xyz = G4.G4ThreeVector(Z, Z, -0.5 * (J - M))
        activeCrystal3 = G4.G4UnionSolid("cry3", activeCrystal1,
                                         activeCrystal2, None, xyz)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (B - M) - J)
        activeCrystal4 = G4.G4UnionSolid("cry4", activeCrystal3, activeTor1,
                                         None, xyz)
        # Making the hole
        hole1 = G4.G4Tubs("hole1", Z, C, 0.5 * (D - C), s, t)
        hole2 = G4.G4Orb("hole2", C)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (D - C))
        hole = G4.G4UnionSolid("hole", hole1, hole2, None, xyz)
        # Making outer dead layer
        xyz = G4.G4ThreeVector(Z, Z, Z)
        outerDeadLayer = G4.G4SubtractionSolid("outerDeadLayer", crystal4,
                                               activeCrystal4, None, xyz)
        # Making the inner dead layer
        innerDead1 = G4.G4Tubs("innerDead1", Z, C + N, 0.5 * (D - C), s, t)
        innerDead2 = G4.G4Orb("innerDead2", C + N)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (D - C))
        innerDead3 = G4.G4UnionSolid("innerDead3", innerDead1, innerDead2,
                                     None, xyz)
        xyz = G4.G4ThreeVector(Z, Z, Z)
        innerDeadLayer = G4.G4SubtractionSolid("innerDeadLayer", innerDead3,
                                               hole, None, xyz)
        # Making final detector shape
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (D - C) - 0.5 * (B - M))
        activeCrystal = G4.G4SubtractionSolid("activeCrystal", activeCrystal4,
                                              innerDead3, None, xyz)
        logOuterDead = G4.G4LogicalVolume(outerDeadLayer, Ge, "logOuterDead")
        logInnerDead = G4.G4LogicalVolume(innerDeadLayer, Ge, "logInnerDead")
        logActive = G4.G4LogicalVolume(activeCrystal, Ge, "logActive")
        # Mylar layer
        mylarLayer = G4.G4Tubs("mylarLayer", Z, K + A, 0.5 * H2, s, t)
        logMylar = G4.G4LogicalVolume(mylarLayer, Ml, "logMylar")
        # CUP
        CUP1 = G4.G4Tubs("CUP1", Z, K + A, 0.5 * F, s, t)
        CUP2 = G4.G4Tubs("CUP2", Z, A, 0.5 * (F - H1 - O), s, t)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (O - H1))
        CUP = G4.G4SubtractionSolid("CUP", CUP1, CUP2, None, xyz)
        logCUP = G4.G4LogicalVolume(CUP, Al, "logCUP")
        #detector shell
        shell1 = G4.G4Tubs("shell1", Z, P, 0.5 * Q, s, t)
        shell2 = G4.G4Tubs("shell2", Z, P - L, 0.5 * Q - L, s, t)
        xyz = G4.G4ThreeVector(Z, Z, Z)
        shell = G4.G4SubtractionSolid("shell", shell1, shell2, None, xyz)
        logShell = G4.G4LogicalVolume(shell, Al, "logShell")

        # Placement of all Elements
        xyz = G4.G4ThreeVector(Z, Z, -(I + G + F) - 0.5 * O + shift_z)
        G4.G4PVPlacement(None, xyz, logBase, "physBase", parent, False, 0)
        xyz = G4.G4ThreeVector(Z, Z, -0.5 * Q + shift_z)
        G4.G4PVPlacement(None, xyz, logHPGe, "physHPGe", parent, False, 0)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (Q - B) - I - G - H1)
        G4.G4PVPlacement(None, xyz, logOuterDead, "physOuterDead", logHPGe,
                         False, 0)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (Q + D - C) - I - G - H1 - B)
        G4.G4PVPlacement(None, xyz, logInnerDead, "physInnerDead", logHPGe,
                         False, 0)
        xyz = G4.G4ThreeVector(Z, Z, Z)
        G4.G4PVPlacement(None, xyz, logShell, "physShell", logHPGe, False, 0)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (Q + H2) - L - G)
        G4.G4PVPlacement(None, xyz, logMylar, "physMylar", logHPGe, False, 0)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (Q - F) - L - G)
        G4.G4PVPlacement(None, xyz, logCUP, "physCUP", logHPGe, False, 0)
        xyz = G4.G4ThreeVector(Z, Z, 0.5 * (Q - M - B) - L - G - H1)
        G4.G4PVPlacement(None, xyz, logActive, "physActive", logHPGe, False, 0)
        self.logical = logActive

        # Detector Visualization Attributes
        BaseVA = G4.G4VisAttributes(G4.G4Color(1.0, 1.0, 1.0, 1.0))
        BaseVA.SetForceSolid(True)
        logBase.SetVisAttributes(BaseVA)
        HPGeVA = G4.G4VisAttributes(G4.G4Color(1.0, 1.0, 0.0, 0.0))
        HPGeVA.SetForceSolid(True)
        logHPGe.SetVisAttributes(HPGeVA)
        ShellVA = G4.G4VisAttributes(G4.G4Color(1.0, 0.5, 0.9, 0.1))
        ShellVA.SetForceSolid(True)
        logShell.SetVisAttributes(ShellVA)
        CupVA = G4.G4VisAttributes(G4.G4Color(0.2, 1.0, 0.0, 0.1))
        CupVA.SetForceSolid(True)
        logCUP.SetVisAttributes(CupVA)
        MylarVA = G4.G4VisAttributes(G4.G4Color(0.2, 1.0, 0.0, 0.6))
        MylarVA.SetForceSolid(True)
        logMylar.SetVisAttributes(MylarVA)
        OutdlVA = G4.G4VisAttributes(G4.G4Color(0.9, 1.0, 0.0, 0.1))
        OutdlVA.SetForceSolid(True)
        logOuterDead.SetVisAttributes(OutdlVA)
        InndlVA = G4.G4VisAttributes(G4.G4Color(0.9, 1.0, 0.0, 0.1))
        InndlVA.SetForceSolid(True)
        logInnerDead.SetVisAttributes(InndlVA)
        CrystVA = G4.G4VisAttributes(G4.G4Color(0.8, 0.2, 0.8, 0.5))
        CrystVA.SetForceSolid(True)
        logActive.SetVisAttributes(CrystVA)