def test_write_geomap(self):
        estimator = SHEstimator()
        estimator.estimator = SHGeoType.geomap
        estimator.geometry = CarthesianMesh()
        estimator.geometry.axis[0].start = -1.0
        estimator.geometry.axis[1].start = -25.0
        estimator.geometry.axis[2].start = -15.0
        estimator.geometry.axis[0].stop = 1.0
        estimator.geometry.axis[1].stop = 25.0
        estimator.geometry.axis[2].stop = 35.0
        estimator.geometry.axis[0].nbins = 1
        estimator.geometry.axis[1].nbins = 50
        estimator.geometry.axis[2].nbins = 50
        estimator.detector_type = SHDetType.zone
        estimator.particle_type = SHParticleType.unknown
        estimator.filename = "ex_yzzon"
        line1, line2 = EstimatorWriter.get_lines(estimator)
        ref_line1 = "GEOMAP          -1.0     -25.0     -15.0       1.0      25.0      35.0"
        ref_line2 = "                   1        50        50         0      ZONE  ex_yzzon"
        self.assertEqual(str(line1), ref_line1)
        self.assertEqual(str(line2), ref_line2)

        estimator.detector_type = SHDetType.medium
        line1, line2 = EstimatorWriter.get_lines(estimator)
        ref_line2 = "                   1        50        50         0    MEDIUM  ex_yzzon"
        self.assertEqual(str(line1), ref_line1)
        self.assertEqual(str(line2), ref_line2)

        estimator.detector_type = SHDetType.rho
        line1, line2 = EstimatorWriter.get_lines(estimator)
        ref_line2 = "                   1        50        50         0       RHO  ex_yzzon"
        self.assertEqual(str(line1), ref_line1)
        self.assertEqual(str(line2), ref_line2)
Exemple #2
0
def main(args=sys.argv[1:]):
    """
    Compose programatically detect.dat SHIELDHIT-12A input file
    with fixed mesh and many combinations of detector and particle type.
    :param args: part of sys.argv, used here to simplify automated testing
    :return: None
    """

    # create empty estimator object
    estimator = SHEstimator()

    # create carthesian mesh
    # it is done once and in single place
    # editing in manually in detect.dat file would require changes in many lines,
    # for every combination of particle and detector type, making it error prone
    estimator.estimator = SHGeoType.msh
    estimator.geometry = CarthesianMesh()
    estimator.geometry.set_axis(axis_no=0, start=-5.0, stop=5.0, nbins=1)
    estimator.geometry.set_axis(axis_no=1, start=-5.0, stop=5.0, nbins=1)
    estimator.geometry.set_axis(axis_no=2, start=0.0, stop=30.0, nbins=300)

    # possible detector types and associated names
    det_types = {SHDetType.energy: "en", SHDetType.fluence: "fl"}

    # possible particle types and associated names
    particle_types = {
        SHParticleType.all: "all",
        SHParticleType.proton: "p",
        SHParticleType.neutron: "n"
    }

    # open detector.dat file for writing
    with open("detect.dat", "w") as f:
        f.write(CardLine.credits + "\n")

        # loop over all combinations of detector and particle types
        # output filename will be composed from associated detector and particle names
        for dt, pt in product(det_types.keys(), particle_types.keys()):
            estimator.detector_type = dt
            estimator.particle_type = pt
            estimator.filename = det_types[dt] + "_" + particle_types[pt]
            text = EstimatorWriter.get_text(estimator, add_comment=True)
            f.write(text)
        f.write(CardLine.comment + "\n")
 def test_write_msh(self):
     estimator = SHEstimator()
     estimator.estimator = SHGeoType.msh
     estimator.geometry = CarthesianMesh()
     estimator.geometry.axis[0].start = -5.0
     estimator.geometry.axis[1].start = -5.0
     estimator.geometry.axis[2].start = 0.0
     estimator.geometry.axis[0].stop = 5.0
     estimator.geometry.axis[1].stop = 5.0
     estimator.geometry.axis[2].stop = 30.0
     estimator.geometry.axis[0].nbins = 1
     estimator.geometry.axis[1].nbins = 1
     estimator.geometry.axis[2].nbins = 300
     estimator.detector_type = SHDetType.energy
     estimator.particle_type = SHParticleType.all
     estimator.filename = "ex_zmsh"
     line1, line2 = EstimatorWriter.get_lines(estimator)
     ref_line1 = "MSH             -5.0      -5.0       0.0       5.0       5.0      30.0"
     ref_line2 = "                   1         1       300        -1    ENERGY   ex_zmsh"
     self.assertEqual(str(line1), ref_line1)
     self.assertEqual(str(line2), ref_line2)
 def test_write_geomap_compact(self):
     estimator = SHEstimator()
     estimator.estimator = SHGeoType.geomap
     estimator.geometry = CarthesianMesh()
     estimator.geometry.set_axis(axis_no=0, start=-1.0, stop=1.0, nbins=1)
     estimator.geometry.set_axis(axis_no=1,
                                 start=-25.0,
                                 stop=25.0,
                                 nbins=50)
     estimator.geometry.set_axis(axis_no=2,
                                 start=-15.0,
                                 stop=35.0,
                                 nbins=50)
     estimator.detector_type = SHDetType.zone
     estimator.particle_type = SHParticleType.unknown
     estimator.filename = "ex_yzzon"
     line1, line2 = EstimatorWriter.get_lines(estimator)
     ref_line1 = "GEOMAP          -1.0     -25.0     -15.0       1.0      25.0      35.0"
     ref_line2 = "                   1        50        50         0      ZONE  ex_yzzon"
     self.assertEqual(str(line1), ref_line1)
     self.assertEqual(str(line2), ref_line2)
Exemple #5
0
def msh_geometries():
    nbins = 10
    xmin = -20.0
    xmax = -xmin
    ymin = xmin
    ymax = -ymin
    zmin = -10.0
    zmax = 30.0
    dx = 1.0
    dy = 1.0
    dz = 1.0

    result = OrderedDict()

    # 0-D
    geometry = CarthesianMesh()
    geometry.set_axis(axis_no=0, start=-dx, stop=dx, nbins=1)
    geometry.set_axis(axis_no=1, start=-dy, stop=dy, nbins=1)
    geometry.set_axis(axis_no=2, start=5.0, stop=6.0, nbins=1)
    result["0"] = geometry

    general = general_geometries(CarthesianMesh,
                                 nbins=nbins,
                                 xmin=xmin,
                                 xmax=xmax,
                                 ymin=ymin,
                                 ymax=ymax,
                                 zmin=zmin,
                                 zmax=zmax,
                                 dx=dx,
                                 dy=dy,
                                 dz=dz)

    result.update(general)

    return result
def add_scoring(input_file):
    """
    Add scoring of deposited energy and neutron fluence on 4 different mesh types (8 scorers in total)
    :param input_file: Fluka input_file object, will be modified
    :return:
    """

    # Dictionary of scoring type (generalized particle)
    # keys will be used as parts of output filenames, this is why they are short
    # for list of all possible generalized particles, see: http://www.fluka.org/fluka.php?id=man_onl&sub=7
    det_types = {"en": "ENERGY", "n": "NEUTRON"}

    # Define 4 different scoring meshes (1D and 2D), using pymchelper classes
    msh_z = CarthesianMesh()
    msh_z.set_axis(axis_no=0, start=-0.5, stop=0.5, nbins=1)
    msh_z.set_axis(axis_no=1, start=-0.5, stop=0.5, nbins=1)
    msh_z.set_axis(axis_no=2, start=0.0, stop=5.0, nbins=500)

    msh_xy = CarthesianMesh()
    msh_xy.set_axis(axis_no=0, start=-5.0, stop=5.0, nbins=500)
    msh_xy.set_axis(axis_no=1, start=-5.0, stop=5.0, nbins=500)
    msh_xy.set_axis(axis_no=2, start=2.8, stop=2.9, nbins=1)

    msh_yz = CarthesianMesh()
    msh_yz.set_axis(axis_no=0, start=-0.1, stop=0.1, nbins=1)
    msh_yz.set_axis(axis_no=1, start=-5.0, stop=5.0, nbins=500)
    msh_yz.set_axis(axis_no=2, start=0.0, stop=5.0, nbins=500)

    msh_zx = CarthesianMesh()
    msh_zx.set_axis(axis_no=0, start=-5.0, stop=5.0, nbins=500)
    msh_zx.set_axis(axis_no=1, start=-0.1, stop=0.1, nbins=1)
    msh_zx.set_axis(axis_no=2, start=0.0, stop=5.0, nbins=500)

    mesh_types = {"z": msh_z, "xy": msh_xy, "yz": msh_yz, "zx": msh_zx}

    # Fluka is using concept of output unit numbers, this is starting number,
    # for each new scorer will be decreased by one
    output_unit = -21

    # scoring, see http://www.fluka.org/fluka.php?id=man_onl&sub=84
    # template cards for USRBIN scorers:
    usrbin_card1 = Card("USRBIN")
    usrbin_card1.setWhat(
        1, 0.0
    )  # WHAT(1) - type of binning selected (0.0 : Mesh Cartesian, no sym.)
    usrbin_card2 = Card("USRBIN")
    usrbin_card2.setSdum("&")  # continuation card

    # loop over all possible detectors and meshes
    for det_type, mesh_type in product(det_types.keys(), mesh_types.keys()):

        # make copies of template cards
        card1 = usrbin_card1.clone()
        card2 = usrbin_card2.clone()

        # generate comment
        card1.setComment("scoring {:s} on mesh {:s}".format(
            det_types[det_type], mesh_type))
        card1.setSdum("{:s}_{:s}".format(
            det_type, mesh_type))  # SDUM - identifying the binning detector
        card1.setWhat(
            2, det_types[det_type]
        )  # WHAT(2) - particle (or particle family) type to be scored
        card1.setWhat(
            3, output_unit
        )  # WHAT(3) - output unit (> 0.0 : formatted data,  < 0.0 : unformatted data)

        msh = mesh_types[mesh_type]
        card1.setWhat(
            4, msh.axis[0].stop)  # WHAT(4) - For Cartesian binning: Xmax
        card1.setWhat(
            5, msh.axis[1].stop)  # WHAT(5) - For Cartesian binning: Ymax
        card1.setWhat(
            6, msh.axis[2].stop)  # WHAT(6) - For Cartesian binning: Zmax
        card2.setWhat(
            1, msh.axis[0].start)  # WHAT(1) - For Cartesian binning: Xmin
        card2.setWhat(
            2, msh.axis[1].start)  # WHAT(2) - For Cartesian binning: Ymin
        card2.setWhat(
            3, msh.axis[2].start)  # WHAT(3) - For Cartesian binning: Zmin
        card2.setWhat(4, msh.axis[0].nbins
                      )  # WHAT(4) - For Cartesian binning: number of X bins
        card2.setWhat(5, msh.axis[1].nbins
                      )  # WHAT(5) - For Cartesian binning: number of Y bins
        card2.setWhat(6, msh.axis[2].nbins
                      )  # WHAT(6) - For Cartesian binning: number of Z bins
        output_unit -= 1

        # finally add generated cards to input structure
        input_file.addCard(card1)
        card1.appendWhats(card2.whats()[1:])
def add_scoring(input_file):
    """
    Add scoring of deposited energy and neutron fluence on 4 different mesh types (8 scorers in total)
    :param input_file: Fluka input_file object, will be modified
    :return:
    """

    # Dictionary of scoring type (generalized particle)
    # keys will be used as parts of output filenames, this is why they are short
    # for list of all possible generalized particles, see: http://www.fluka.org/fluka.php?id=man_onl&sub=7
    det_types = {"en": "ENERGY", "n": "NEUTRON"}

    # Define 4 different scoring meshes (1D and 2D), using pymchelper classes
    msh_z = CarthesianMesh()
    msh_z.set_axis(axis_no=0, start=-0.5, stop=0.5, nbins=1)
    msh_z.set_axis(axis_no=1, start=-0.5, stop=0.5, nbins=1)
    msh_z.set_axis(axis_no=2, start=0.0, stop=5.0, nbins=500)

    msh_xy = CarthesianMesh()
    msh_xy.set_axis(axis_no=0, start=-5.0, stop=5.0, nbins=500)
    msh_xy.set_axis(axis_no=1, start=-5.0, stop=5.0, nbins=500)
    msh_xy.set_axis(axis_no=2, start=2.8, stop=2.9, nbins=1)

    msh_yz = CarthesianMesh()
    msh_yz.set_axis(axis_no=0, start=-0.1, stop=0.1, nbins=1)
    msh_yz.set_axis(axis_no=1, start=-5.0, stop=5.0, nbins=500)
    msh_yz.set_axis(axis_no=2, start=0.0, stop=5.0, nbins=500)

    msh_zx = CarthesianMesh()
    msh_zx.set_axis(axis_no=0, start=-5.0, stop=5.0, nbins=500)
    msh_zx.set_axis(axis_no=1, start=-0.1, stop=0.1, nbins=1)
    msh_zx.set_axis(axis_no=2, start=0.0, stop=5.0, nbins=500)

    mesh_types = {"z": msh_z, "xy": msh_xy, "yz": msh_yz, "zx": msh_zx}

    # Fluka is using concept of output unit numbers, this is starting number,
    # for each new scorer will be decreased by one
    output_unit = -21

    # scoring, see http://www.fluka.org/fluka.php?id=man_onl&sub=84
    # template cards for USRBIN scorers:
    usrbin_card1 = Card("USRBIN")
    usrbin_card1.setWhat(1, 0.0)  # WHAT(1) - type of binning selected (0.0 : Mesh Cartesian, no sym.)
    usrbin_card2 = Card("USRBIN")
    usrbin_card2.setSdum("&")  # continuation card

    # loop over all possible detectors and meshes
    for det_type, mesh_type in product(det_types.keys(), mesh_types.keys()):

        # make copies of template cards
        card1 = usrbin_card1.clone()
        card2 = usrbin_card2.clone()

        # generate comment
        card1.setComment("scoring {:s} on mesh {:s}".format(det_types[det_type], mesh_type))
        card1.setSdum("{:s}_{:s}".format(det_type, mesh_type))  # SDUM - identifying the binning detector
        card1.setWhat(2, det_types[det_type])  # WHAT(2) - particle (or particle family) type to be scored
        card1.setWhat(3, output_unit)  # WHAT(3) - output unit (> 0.0 : formatted data,  < 0.0 : unformatted data)

        msh = mesh_types[mesh_type]
        card1.setWhat(4, msh.axis[0].stop)  # WHAT(4) - For Cartesian binning: Xmax
        card1.setWhat(5, msh.axis[1].stop)  # WHAT(5) - For Cartesian binning: Ymax
        card1.setWhat(6, msh.axis[2].stop)  # WHAT(6) - For Cartesian binning: Zmax
        card2.setWhat(1, msh.axis[0].start)  # WHAT(1) - For Cartesian binning: Xmin
        card2.setWhat(2, msh.axis[1].start)  # WHAT(2) - For Cartesian binning: Ymin
        card2.setWhat(3, msh.axis[2].start)  # WHAT(3) - For Cartesian binning: Zmin
        card2.setWhat(4, msh.axis[0].nbins)  # WHAT(4) - For Cartesian binning: number of X bins
        card2.setWhat(5, msh.axis[1].nbins)  # WHAT(5) - For Cartesian binning: number of Y bins
        card2.setWhat(6, msh.axis[2].nbins)  # WHAT(6) - For Cartesian binning: number of Z bins
        output_unit -= 1

        # finally add generated cards to input structure
        input_file.addCard(card1)
        card1.appendWhats(card2.whats()[1:])