Exemple #1
0
 def encode(**kwargs):
     if kwargs['key'] is None and kwargs['key_raw'] is None:
         raise Exceptions.NoKeyException()
     io = IOManager(**kwargs)
     arr = CryptAlghorithm.crypt_class(kwargs['algorithm']).encrypt(
         io.input_arr, io.key_arr)
     io.push(arr)
Exemple #2
0
 def hack(**kwargs):
     if kwargs['hack_tries'] is None:
         raise Exceptions.HackTriesNumberException()
     io = IOManager(**kwargs)
     arr = CryptAlghorithm.crypt_class(kwargs['algorithm']).hack(
         io.input_arr, kwargs['hack_tries'])
     io.push_pack(arr)
Exemple #3
0
    def convert(self, _file_in, _file_out=None, max_entries=None):

        # if there is not an output file, the output is the input with a new file extension:
        if _file_out is None:
            directory = os.path.dirname(_file_in)
            file_root = os.path.basename(_file_in)
            _file_out = directory + os.path.splitext(
                file_root)[0] + '_larcv.root'
            # print _file_out

        self._input_file = _file_in
        self._output_file = _file_out

        if not self._initialized:
            self.initialize_geometry()
            self._initialized = True

        # Create the instances of IO managers:
        self._next_io = IOManager()
        self._next_io.set_file(self._input_file)

        # larcv io:
        self._larcv_io = larcv.IOManager(larcv.IOManager.kWRITE)
        self._larcv_io.set_out_file(self._output_file)
        self._larcv_io.initialize()

        self.event_loop(max_entries=max_entries)
    def __init__(self, parameters):
        r"""Create a new simulation loop instance for a simulation
        using the semiclassical Hagedorn wavepacket based propagation
        method.

        :param parameters: The simulation parameters.
        :type parameters: A :py:class:`ParameterProvider` instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        # The time propagator instance driving the simulation.
        self.propagator = None

        # A `IOManager` instance for saving simulation results.
        self.IOManager = None

        # The time manager
        self._tm = TimeManager(self.parameters)

        # Set up serialization of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file()

        # Save the simulation parameters
        self.IOManager.add_parameters()
        self.IOManager.save_parameters(parameters)
    def __init__(self, parameters):
        """Create a new simulation loop instance for a simulation
        using the Fourier propagation method.

        :param parameters: The simulation parameters.
        :type parameters: A :py:class:`ParameterProvider` instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        # The time propagator instance driving the simulation.
        self.propagator = None

        # An `IOManager` instance for saving simulation results.
        self.IOManager = None

        # Which data do we want to save
        self._tm = self.parameters.get_timemanager()

        # Set up serialization of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file()
        self.IOManager.create_block()

        # Save the simulation parameters
        self.IOManager.add_parameters()
        self.IOManager.save_parameters(parameters)
Exemple #6
0
 def SetBinning(self, varexp, *args):
     if len(args) == 3:
         self._binning[varexp] = IOManager._convertBinning(args)
     elif len(args) == 1 and isinstance(args[0], list):
         self._binning[varexp] = args[0]
     else:
         logger.error("Invalid binning format '{}' for varexp '{}'!".format(
             args, varexp))
Exemple #7
0
def raw_src_mode(caller: EncryptorApp):
    kwargs = {
        'key_raw': str(caller.var1_keyraw.displayText()),
        'img': str(caller.var1_img.displayText()),
        'useimg': False,
        'key': None,
        'crypt': 'encode',
        'src': os.path.realpath(__file__),
        'dest': caller.var1_dest.displayText()
    }
    raw_src = str(caller.var1_textArea.toPlainText())
    if kwargs['img'] != '':
        if not os.path.isfile(kwargs['img']):
            raise Exceptions.BadImageFileException()
        kwargs['useimg'] = True
    arr = CryptAlghorithm.crypt_class(caller.cryptType.currentIndex()).encrypt(
        bytearray(raw_src, sys.stdin.encoding),
        bytearray(kwargs['key_raw'], sys.stdin.encoding))
    io = IOManager(**kwargs)
    io.push(arr)
Exemple #8
0
 def _parse_fat16_structure(self, io_manager: IOManager):
     self.BS_DrvNum = io_manager.read_bytes_and_convert_to_int(1)
     self.BS_Reserved1 = io_manager.read_bytes_and_convert_to_int(1)
     self.BS_bootSig = io_manager.read_bytes_and_convert_to_int(1)
     self.BS_VolID = io_manager.read_bytes_and_convert_to_int(4)
     self.BS_VolLab = io_manager.read_bytes_and_convert_to_int(11)
     self.BS_FilSysType = io_manager.read_bytes_and_convert_to_int(8)
Exemple #9
0
    def Fill(self, *args, **kwargs):
        r"""Fill the histogram with entries.

        If a path (``str``) to an **infile** is given as the only argument the histogram
        if filled using the events in there as specified by the keyword arguments.
        Otherwise the standard :func:`ROOT.TH1.Fill` functionality is used.

        :param \*args: see below

        :param \**kwargs: see below

        :Arguments:
            Depending on the number of arguments (besides **name**) there are three ways
            to initialize a :class:`.Histo1D` object\:

            * *one* argument of type ``str``\:

                #. **infile** (``str``) -- path to the input :py:mod:`ROOT` file (use
                   keyword arguments to define which events to select)

            * otherwise\:

                see :py:mod:`ROOT` documentation of :func:`TH1.Fill` (keyword arguments
                will be ignored)

        :Keyword Arguments:

            * **tree** (``str``) -- name of the input tree

            * **varexp** (``str``) -- name of the branch to be plotted on the x-axis

            * **cuts** (``str``, ``list``, ``tuple``) -- string or list of strings of
              boolean expressions, the latter will default to a logical *AND* of all
              items (default: '1')

            * **weight** (``str``) -- number or branch name to be applied as a weight
              (default: '1')

            * **append** (``bool``) -- append entries to the histogram instead of
              overwriting it (default: ``False``)
        """
        self._varexp = kwargs.get("varexp")
        self._cuts = kwargs.get("cuts", [])
        self._weight = kwargs.get("weight", "1")
        if len(args) == 1 and isinstance(args[0], (str, unicode)):
            IOManager.FillHistogram(self, args[0], **kwargs)
            if not kwargs.get("append", False):
                self._errorband.Reset()
            self._errorband.Add(self)
        else:
            super(Histo1D, self).Fill(*args)
Exemple #10
0
    def __init__(self, parameters):
        r"""Create a new simulation loop instance for a simulation
        using the semiclassical Hagedorn wavepacket based propagation
        method.

        :param parameters: The simulation parameters.
        :type parameters: A :py:class:`ParameterProvider` instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        # The time propagator instance driving the simulation.
        self.propagator = None

        # A `IOManager` instance for saving simulation results.
        self.IOManager = None

        # The time manager
        self._tm = TimeManager(self.parameters)

        # Set up serialization of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file(self.parameters)
Exemple #11
0
    def __init__(self, parameters):
        """Create a new simulation loop instance for a simulation
        using the Fourier propagation method.

        :param parameters: The simulation parameters.
        :type parameters: A :py:class:`ParameterProvider` instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        # The time propagator instance driving the simulation.
        self.propagator = None

        # An `IOManager` instance for saving simulation results.
        self.IOManager = None

        # Which data do we want to save
        self._tm = self.parameters.get_timemanager()

        # Set up serialization of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file(self.parameters)
        self.IOManager.create_block()
    def __init__(self, parameters):
        r"""
        Create a new simulation loop instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        self.tm = TimeManager(parameters)

        #: The time propagator instance driving the simulation.
        self.propagator = None

        #: A ``IOManager`` instance for saving simulation results.
        self.iom = IOManager()
        self.iom.create_file(parameters)
        self.gid = self.iom.create_group()
    def __init__(self, parameters):
        r"""
        Create a new simulation loop instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        #: The time propagator instance driving the simulation.
        self.propagator = None

        #: A ``IOManager`` instance for saving simulation results.
        self.IOManager = None

        #: The number of time steps we will perform.
        self.nsteps = parameters["nsteps"]

        # Set up serializing of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file(self.parameters)
        self.IOManager.create_block()
Exemple #14
0
 def CreateHistograms(self):
     factories = {}
     for histotype, configs in self._configs.items():
         self._store[histotype] = {}
         for i, (infile, config) in enumerate(configs):
             self._store[histotype][i] = {}
             config = DissectProperties(
                 config, [Histo1D, {
                     "Fill": ["tree", "cuts", "weight"]
                 }])
             tree = config["Fill"]["tree"]
             if not (infile, tree) in factories.keys():
                 factories[infile, tree] = IOManager.Factory(infile, tree)
             for varexp, comparator, cutvalue in self._drawcuts:
                 if not varexp in self._binning:
                     logger.warning("No binning defined for varexp '{}'. "
                                    "Skipping...".format(varexp))
                     continue
                 reducedcuts = list(
                     filter(
                         lambda x: x != "".join(
                             [varexp, comparator, cutvalue]),
                         self._cuts,
                     ))
                 self._store[histotype][i][varexp, cutvalue] = Histo1D(
                     "N1_{}".format(uuid4().hex[:8]), "",
                     self._binning[varexp], **config["Histo1D"])
                 factories[infile, tree].Register(
                     self._store[histotype][i][varexp, cutvalue],
                     varexp=varexp,
                     weight=config["Fill"].get("weight", "1"),
                     cuts=list(config["Fill"].get("cuts", []) +
                               reducedcuts + self._preselection),
                 )
     for factory in factories.values():
         factory.Run()
 def _get_file_system(type_of_fat: TypeOfFAT):
     io_manager = IOManager(FAT_16_IMAGE_FOR_DEFRAG if type_of_fat == TypeOfFAT.fat16 else FAT_32_IMAGE_FOR_DEFRAG)
     return parse_disk_image(io_manager), io_manager
Exemple #16
0
            ),
                        xtitle=self._vartitle.get(varexp, varexp),
                        xunits=self._varunits.get(varexp, None),
                        inject0=cutmarker,
                        mkdir=True,
                        **kwargs)


if __name__ == "__main__":

    testsamples = []

    for i in range(4):
        testsamples.append("../data/testsample_{}.root".format(i))
        try:
            IOManager.CreateTestSample(testsamples[i], overwrite=False)
        except IOError:
            pass

    cuts = [
        "branch_4>1.5",
        "branch_1+branch_5<3.75",
        "(branch_6>=4.5)&&(abs(branch_7)<=9.5)",
    ]

    binnings = {
        "branch_4": [20, 0.0, 10.0],  # fixed bin width
        "branch_1+branch_5": [20, 0.0, 10.0],
        "branch_6":
        [[i * 0.25 for i in range(40)] + [10, 11, 12, 13, 14, 15, 17.5, 20]
         ],  # variable bin width
class SimulationLoopHagedorn(SimulationLoop):
    r"""
    This class acts as the main simulation loop. It owns a propagator that
    propagates a set of initial values during a time evolution. All values are
    read from the ``Parameters.py`` file.
    """

    def __init__(self, parameters):
        r"""
        Create a new simulation loop instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        #: The time propagator instance driving the simulation.
        self.propagator = None

        #: A ``IOManager`` instance for saving simulation results.
        self.IOManager = None

        #: The number of time steps we will perform.
        self.nsteps = parameters["nsteps"]

        # Set up serializing of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file(self.parameters)
        self.IOManager.create_block()

    def prepare_simulation(self):
        r"""
        Set up a Hagedorn propagator for the simulation loop. Set the
        potential and initial values according to the configuration.

        :raise ValueError: For invalid or missing input data.
        """
        potential = PF().create_potential(self.parameters)
        N = potential.get_number_components()

        # Check for enough initial values
        if self.parameters["leading_component"] > N:
            raise ValueError("Leading component index out of range.")

        if len(self.parameters["parameters"]) < N:
            raise ValueError("Too few initial states given. Parameters are missing.")

        if len(self.parameters["coefficients"]) < N:
            raise ValueError("Too few initial states given. Coefficients are missing.")

        # Create a suitable wave packet
        packet = HagedornWavepacket(self.parameters)

        # See if we have a list of parameter tuples or just a single 5-tuple
        # This is for compatibility with the inhomogeneous case.
        try:
            # We have a list of parameter tuples, take the one given by the leading component
            len(self.parameters["parameters"][0])
            parameters = self.parameters["parameters"][self.parameters["leading_component"]]
        except TypeError:
            # We have just a single 5-tuple of parameters, take it.
            parameters = self.parameters["parameters"]

        # Set the Hagedorn parameters
        packet.set_parameters(parameters)
        packet.set_quadrature(None)

        # Set the initial values
        for component, data in enumerate(self.parameters["coefficients"]):
            for index, value in data:
                packet.set_coefficient(component, index, value)

        # Project the initial values to the canonical basis
        packet.project_to_canonical(potential)

        # Finally create and initialize the propagator instace
        self.propagator = HagedornPropagator(potential, packet, self.parameters["leading_component"], self.parameters)

        # Which data do we want to save
        tm = self.parameters.get_timemanager()
        slots = tm.compute_number_saves()

        self.IOManager.add_grid(self.parameters, blockid="global")
        self.IOManager.add_wavepacket(self.parameters, timeslots=slots)

        # Write some initial values to disk
        nodes = self.parameters["f"] * sp.pi * sp.arange(-1, 1, 2.0 / self.parameters["ngn"], dtype=np.complexfloating)
        self.IOManager.save_grid(nodes, blockid="global")
        self.IOManager.save_wavepacket_parameters(self.propagator.get_wavepackets().get_parameters(), timestep=0)
        self.IOManager.save_wavepacket_coefficients(self.propagator.get_wavepackets().get_coefficients(), timestep=0)

    def run_simulation(self):
        r"""
        Run the simulation loop for a number of time steps. The number of steps is calculated in the ``initialize`` function.
        """
        tm = self.parameters.get_timemanager()

        # Run the simulation for a given number of timesteps
        for i in xrange(1, self.nsteps + 1):
            print(" doing timestep " + str(i))

            self.propagator.propagate()

            # Save some simulation data
            if tm.must_save(i):
                self.IOManager.save_wavepacket_parameters(
                    self.propagator.get_wavepackets().get_parameters(), timestep=i
                )
                self.IOManager.save_wavepacket_coefficients(
                    self.propagator.get_wavepackets().get_coefficients(), timestep=i
                )

    def end_simulation(self):
        r"""
        Do the necessary cleanup after a simulation. For example request the
        IOManager to write the data and close the output files.
        """
        self.IOManager.finalize()
 def test_read_some_bytes_with_incorrect_value(self):
     io_manager = IOManager('test_io_manager')
     self.check_error(io_manager.read_some_bytes, ValueError, True, 0)
     self.check_error(io_manager.read_some_bytes, ValueError, True, -10)
class SimulationLoopHagedornInhomogeneous(SimulationLoop):
    r"""This class acts as the main simulation loop. It owns a propagator that
    propagates a set of initial values during a time evolution.
    """

    def __init__(self, parameters):
        r"""Create a new simulation loop instance for a simulation
        using the semiclassical Hagedorn wavepacket based propagation
        method.

        :param parameters: The simulation parameters.
        :type parameters: A :py:class:`ParameterProvider` instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        # The time propagator instance driving the simulation.
        self.propagator = None

        # A `IOManager` instance for saving simulation results.
        self.IOManager = None

        # The time manager
        self._tm = TimeManager(self.parameters)

        # Set up serialization of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file()

        # Save the simulation parameters
        self.IOManager.add_parameters()
        self.IOManager.save_parameters(parameters)


    def prepare_simulation(self):
        r"""Set up a Hagedorn propagator for the simulation loop. Set the
        potential and initial values according to the configuration.

        :raise: :py:class:`ValueError` For invalid or missing input data.
        """
        # The potential instance
        potential = BlockFactory().create_potential(self.parameters)

        # Project the initial values to the canonical basis
        BT = BasisTransformationHAWP(potential)

        # Finally create and initialize the propagator instance
        # TODO: Attach the "leading_component to the hawp as codata
        self.propagator = HagedornPropagatorInhomogeneous(self.parameters, potential)

        # Create suitable wavepackets
        for packet_descr in self.parameters["initvals"]:
            packet = BlockFactory().create_wavepacket(packet_descr)
            # Transform to canonical basis
            BT.set_matrix_builder(packet.get_innerproduct())
            BT.transform_to_canonical(packet)
            # And hand over
            self.propagator.add_wavepacket((packet,))

        # Add storage for each packet
        npackets = len(self.parameters["initvals"])
        slots = self._tm.compute_number_saves()
        key = ("q","p","Q","P","S","adQ")

        for i in xrange(npackets):
            bid = self.IOManager.create_block()
            self.IOManager.add_inhomogwavepacket(self.parameters, timeslots=slots, blockid=bid, key=key)

        # Write some initial values to disk
        for packet in self.propagator.get_wavepackets():
            self.IOManager.save_inhomogwavepacket_description(packet.get_description())
            # Pi
            self.IOManager.save_inhomogwavepacket_parameters(packet.get_parameters(key=key), timestep=0, key=key)
            # Basis shapes
            for shape in packet.get_basis_shapes():
                self.IOManager.save_inhomogwavepacket_basisshapes(shape)
            # Coefficients
            self.IOManager.save_inhomogwavepacket_coefficients(packet.get_coefficients(), packet.get_basis_shapes(), timestep=0)


    def run_simulation(self):
        r"""Run the simulation loop for a number of time steps.
        """
        # The number of time steps we will perform.
        nsteps = self._tm.compute_number_timesteps()

        # Which parameter data to save.
        key = ("q","p","Q","P","S","adQ")

        # Run the prepropagate step
        self.propagator.pre_propagate()
        # Note: We do not save any data here

        # Run the simulation for a given number of timesteps
        for i in xrange(1, nsteps+1):
            print(" doing timestep "+str(i))

            self.propagator.propagate()

            # Save some simulation data
            if self._tm.must_save(i):
                # Run the postpropagate step
                self.propagator.post_propagate()

                # TODO: Generalize for arbitrary number of wavepackets
                packets = self.propagator.get_wavepackets()
                assert len(packets) == 1

                for packet in packets:
                    # Pi
                    self.IOManager.save_inhomogwavepacket_parameters(packet.get_parameters(key=key), timestep=i, key=key)
                    # Basis shapes (in case they changed!)
                    for shape in packet.get_basis_shapes():
                        self.IOManager.save_inhomogwavepacket_basisshapes(shape)
                    # Coefficients
                    self.IOManager.save_inhomogwavepacket_coefficients(packet.get_coefficients(), packet.get_basis_shapes(), timestep=i)

                # Run the prepropagate step
                self.propagator.pre_propagate()

        # Run the postpropagate step
        self.propagator.post_propagate()
        # Note: We do not save any data here


    def end_simulation(self):
        r"""Do the necessary cleanup after a simulation. For example request the
        :py:class:`IOManager` to write the data and close the output files.
        """
        self.IOManager.finalize()
Exemple #20
0
class Converter(object):
    def __init__(self):
        super(Converter, self).__init__()

        # Pointers to the files:
        self._input_file = None
        self._output_file = None

        # IO Instances:
        self._larcv_io = None
        self._next_io = None

        self._initialized = False

        self._pc = ParticleConverter()

    def convert(self, _file_in, _file_out=None, max_entries=None):

        # if there is not an output file, the output is the input with a new file extension:
        if _file_out is None:
            directory = os.path.dirname(_file_in)
            file_root = os.path.basename(_file_in)
            _file_out = directory + os.path.splitext(
                file_root)[0] + '_larcv.root'
            # print _file_out

        self._input_file = _file_in
        self._output_file = _file_out

        if not self._initialized:
            self.initialize_geometry()
            self._initialized = True

        # Create the instances of IO managers:
        self._next_io = IOManager()
        self._next_io.set_file(self._input_file)

        # larcv io:
        self._larcv_io = larcv.IOManager(larcv.IOManager.kWRITE)
        self._larcv_io.set_out_file(self._output_file)
        self._larcv_io.initialize()

        self.event_loop(max_entries=max_entries)

    def initialize_geometry(self):
        '''Set up and cache the geometry information

        Creates meta objects for larcv and reads the database for h5.
        '''

        # Read in the database to get the pmt and sipm locations:
        self._pmt_locations = load_db.DataPMT()
        self._sipm_locations = load_db.DataSiPM()
        self._det_geo = load_db.DetectorGeo()

        min_x = numpy.min(self._sipm_locations.X)
        max_x = numpy.max(self._sipm_locations.X)
        min_y = numpy.min(self._sipm_locations.Y)
        max_y = numpy.max(self._sipm_locations.Y)
        min_z = self._det_geo.ZMIN
        max_z = self._det_geo.ZMAX

        n_x = int(max_x - min_x)
        n_y = int(max_y - min_y)
        n_z = int(max_z - min_z)

        # Create just one meta to use for NEXT-New
        self._mc_meta = larcv.Voxel3DMeta()
        self._mc_meta.set(min_x, min_y, min_z, max_x, max_y, max_z, n_x, n_y,
                          n_z)

        print '_sipm_locations.X', _sipm_locations.X
        print '_sipm_locations.Y', _sipm_locations.Y

        n_x = n_x / 10
        n_y = n_y / 10

        self._pmaps_meta = larcv.Voxel3DMeta()
        self._pmaps_meta.set(min_x, min_y, min_z, max_x, max_y, max_z, n_x,
                             n_y, n_z)

        return

    def convert_mc_information(self):

        if self._larcv_io is None:
            raise Exception("No larcv IO manager found.")

        if self._next_io is None:
            raise Exception("No next IO manager found.")

        # Convert particle object
        hits = self._next_io.mc().hits(self._event)
        particles = self._next_io.mc().particles(self._event)
        larcv_particle_set = self._larcv_io.get_data("particle", "mcpart")
        larcv_voxel3d = self._larcv_io.get_data("sparse3d", "mcpart")
        larcv_cluster3d = self._larcv_io.get_data("cluster3d", "mcpart")
        larcv_particle_set.clear()
        larcv_voxel3d.clear()
        larcv_cluster3d.clear()

        larcv_voxel3d.meta(self._mc_meta)
        larcv_cluster3d.meta(self._mc_meta)

        particle_index_mapping = dict()
        i = 0
        for particle in particles:
            larcv_particle = larcv.Particle()
            larcv_particle.id(i)
            larcv_particle.track_id(int(particle['particle_indx']))
            particle_index_mapping[particle['particle_indx']] = i
            larcv_particle.parent_track_id(int(particle['mother_indx']))
            larcv_particle.pdg_code(
                self._pc.get_pdg(particles[i]['particle_name']))
            larcv_particle.position(particle['initial_vertex'][0],
                                    particle['initial_vertex'][1],
                                    particle['initial_vertex'][2],
                                    particle['initial_vertex'][3])
            larcv_particle.end_position(particle['final_vertex'][0],
                                        particle['final_vertex'][1],
                                        particle['final_vertex'][2],
                                        particle['final_vertex'][3])
            # Momentum:
            larcv_particle.momentum(particle['momentum'][0],
                                    particle['momentum'][1],
                                    particle['momentum'][2])
            #kinetic energy:
            larcv_particle.energy_init(particle['kin_energy'])
            larcv_particle.creation_process(particle['creator_proc'])

            larcv_particle_set.append(larcv_particle)
            i += 1
            # print particle

        # Create a set of clusters to match the length of the particles:
        larcv_cluster3d.resize(i + 1)

        for hit in hits:
            xyz = hit['hit_position']
            larcv_voxel3d.emplace(xyz[0], xyz[1], xyz[2], hit['hit_energy'])
            # Get the particle index of this hit:
            if hit['particle_indx'] in particle_index_mapping.keys():
                idx = particle_index_mapping[int(hit['particle_indx'])]
            else:
                idx = i
            larcv_voxel_index = self._mc_meta.id(xyz[0], xyz[1], xyz[2])
            voxel = larcv.Voxel(larcv_voxel_index, hit['hit_energy'])
            larcv_cluster3d.writeable_voxel_set(idx).add(voxel)

        return True

    def convert_pmaps(self):

        if self._larcv_io is None:
            raise Exception("No larcv IO manager found.")

        if self._next_io is None:
            raise Exception("No next IO manager found.")

        pmaps = self._next_io.pmaps()
        larcv_voxel = self._larcv_io.get_data("sparse3d", "pmaps")
        larcv_voxel.clear()
        larcv_voxel.meta(self._pmaps_meta)

        larcv_meta = self._larcv_io.get_data("meta", "pmaps")

        # Get the sipms location
        # _sipm_locations = load_db.DataSiPM()

        # Use S1 to get t0
        if pmaps.s1() is None:
            return False
        if pmaps.s2Pmt() is None:
            return False
        if pmaps.s2Si() is None:
            return False

        s1_peak_time_idx = numpy.argmax(numpy.asarray(pmaps.s1()['ene']))
        t0 = pmaps.s1()['time'][s1_peak_time_idx]
        t0 *= 1e-3

        # Covert the S2 df to a dictionary
        s2_dict = {}

        for i in xrange(0, len(pmaps.s2())):
            current_peak = s2_dict.setdefault(pmaps.s2()['peak'][i], ([], []))
            current_peak[0].append(pmaps.s2()['time'][i])
            current_peak[1].append(pmaps.s2()['ene'][i])

        # print 's2_dict', s2_dict

        # Covert the S2Si df to a dictionary
        # s2si_dict is a dictionary {peak number, sipms dictionary}
        # 'sipms dictionary' is a dictionary {sipms number, time and energy arrays}
        s2si_dict = {}

        for i in xrange(0, len(pmaps.s2Si())):

            peak_number = pmaps.s2Si()['peak'][i]
            sipm_number = pmaps.s2Si()['nsipm'][i]

            current_peak = s2si_dict.setdefault(peak_number, {})
            current_sipms = current_peak.setdefault(sipm_number, ([], []))

            # Get the time from previous S2 dictionary, and save time and energy
            e = pmaps.s2Si()['ene'][i]
            t = s2_dict[peak_number][0][len(current_sipms[0])]
            current_sipms[0].append(t)
            current_sipms[1].append(e)

            x = self._sipm_locations.X[sipm_number]
            y = self._sipm_locations.Y[sipm_number]
            z = 1e-3 * t - t0

            if e > 0.00001:
                larcv_voxel.emplace(x, y, z, e)

        # Covert the S2PMT df to a dictionary
        # s2pmt_dict is a dictionary {peak number, pmt dictionary}
        # 'pmt dictionary' is a dictionary {pmt number, time and energy arrays}
        s2pmt_dict = {}

        times = ROOT.std.vector('double')()
        energies = ROOT.std.vector('double')()

        for i in xrange(0, len(pmaps.s2Pmt())):

            peak_number = pmaps.s2Pmt()['peak'][i]
            pmt_number = pmaps.s2Pmt()['npmt'][i]

            current_peak = s2pmt_dict.setdefault(peak_number, {})
            current_pmts = current_peak.setdefault(pmt_number, ([], []))

            # Get the time from previous S2 dictionary, and save time and energy
            e = pmaps.s2Pmt()['ene'][i]
            t = s2_dict[peak_number][0][len(current_pmts[0])]
            current_pmts[0].append(t)
            current_pmts[1].append(e)

            times.push_back(t)
            energies.push_back(e)

        larcv_meta.store("s2pmt_time", times)
        larcv_meta.store("s2pmt_energy", energies)

        return True

    def event_loop(self, max_entries=None):

        if not self._initialized:
            raise Exception("Need to initialize before event loop.")

        entry_count = 0
        for entry in self._next_io.entries():

            if entry_count % 1 == 0:
                sys.stdout.write("Processed entry {}.\n".format(entry_count))

            # Read the entry in the next IO:
            self._next_io.go_to_entry(entry)

            self._entry = entry
            self._event = self._next_io.event()
            self._run = self._next_io.run()

            if self._run < 0:
                self._run = 0

            ##########################
            # Do the conversions here.
            ##########################
            _ok = self.convert_mc_information()
            _ok = self.convert_pmaps() and _ok

            # print _ok

            if _ok:
                self._larcv_io.set_id(int(self._run), 0, int(self._event))
                self._larcv_io.save_entry()
                entry_count += 1

            if max_entries is not None and entry > max_entries:
                break

        self._larcv_io.finalize()

        sys.stdout.write(
            "Total number of entries converted: {}\n".format(entry_count))
class TestIOManager(unittest.TestCase):
    def setUp(self):
        self.manager = IOManager()

    def tearDown(self):
        if self.manager.is_running():
            self.manager.stop()

    def test_starts_and_stops(self):
        pass

    def test_is_running(self):
        self.assertTrue(self.manager.is_running())
        self.manager.stop()
        self.assertFalse(self.manager.is_running())

    def test_calls_callbacks(self):
        pipe = PipeWrapper()
        event = threading.Event()

        def callback(flag):
            if flag == select.POLLIN:
                text = pipe.reader.readline()
                if text == "super beans\n":
                    event.set()
            else:
                self.assertEqual(flag, select.POLLHUP)

        self.manager.add_file(pipe.reader, lambda flag: callback(flag))

        self.assertFalse(event.is_set())
        pipe.send("super beans\n")
        event.wait(1)
        self.assertTrue(event.is_set())

    def test_removes_callbacks(self):
        pipe = PipeWrapper()
        event = threading.Event()

        def callback(flag):
            if flag == select.POLLIN:
                text = pipe.reader.readline()
                if text == "super beans\n":
                    event.set()
            else:
                self.assertEqual(flag, select.POLLHUP)

        self.manager.add_file(pipe.reader, lambda flag: callback(flag))

        self.assertFalse(event.is_set())
        pipe.send("super beans\n")
        event.wait(1)
        self.assertTrue(event.is_set())

        event.clear()

        self.manager.remove_file(pipe.reader)

        self.assertFalse(event.is_set())
        pipe.send("super beans\n")
        event.wait(
            0.05
        )  # note that this will always be taken - the callback shouldn't fire
        self.assertFalse(event.is_set())

    def test_sends_hangups(self):
        pipe = PipeWrapper()
        event = threading.Event()

        def callback(flag):
            if flag != select.POLLHUP and flag != select.POLLNVAL:
                self.assertEqual(flag, select.POLLHUP | select.POLLNVAL)
            event.set()

        self.manager.add_file(pipe.reader, callback)
        pipe.writer.close()
        event.wait(1)
        self.assertTrue(event.is_set())
 def setUp(self):
     self.manager = IOManager()
class SimulationLoopFourier(SimulationLoop):
    """This class acts as the main simulation loop. It owns a propagator that
    propagates a set of initial values during a time evolution.
    """

    def __init__(self, parameters):
        """Create a new simulation loop instance for a simulation
        using the Fourier propagation method.

        :param parameters: The simulation parameters.
        :type parameters: A :py:class:`ParameterProvider` instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        # The time propagator instance driving the simulation.
        self.propagator = None

        # An `IOManager` instance for saving simulation results.
        self.IOManager = None

        # Which data do we want to save
        self._tm = self.parameters.get_timemanager()

        # Set up serialization of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file()
        self.IOManager.create_block()

        # Save the simulation parameters
        self.IOManager.add_parameters()
        self.IOManager.save_parameters(parameters)


    def prepare_simulation(self):
        r"""Set up a Fourier propagator for the simulation loop. Set the
        potential and initial values according to the configuration.

        :raise: :py:class:`ValueError` For invalid or missing input data.
        """
        # The potential instance
        potential = BlockFactory().create_potential(self.parameters)

        # Compute the position space grid points
        grid = BlockFactory().create_grid(self.parameters)

        # Construct initial values
        I = Initializer(self.parameters)
        initialvalues = I.initialize_for_fourier(grid)

        # Transform the initial values to the canonical basis
        BT = BasisTransformationWF(potential)
        BT.set_grid(grid)
        BT.transform_to_canonical(initialvalues)

        # Finally create and initialize the propagator instance
        self.propagator = FourierPropagator(potential, initialvalues, self.parameters)

        # Write some initial values to disk
        slots = self._tm.compute_number_saves()

        self.IOManager.add_grid(self.parameters, blockid="global")
        self.IOManager.add_fourieroperators(self.parameters)
        self.IOManager.add_wavefunction(self.parameters, timeslots=slots)

        self.IOManager.save_grid(grid.get_nodes(flat=True), blockid="global")
        self.IOManager.save_fourieroperators(self.propagator.get_operators())
        self.IOManager.save_wavefunction(initialvalues.get_values(), timestep=0)


    def run_simulation(self):
        r"""Run the simulation loop for a number of time steps.
        """
        # The number of time steps we will perform.
        nsteps = self._tm.compute_number_timesteps()

        # Run the prepropagate step
        self.propagator.pre_propagate()
        # Note: We do not save any data here

        # Run the simulation for a given number of timesteps
        for i in xrange(1, nsteps+1):
            print(" doing timestep "+str(i))

            self.propagator.propagate()

            # Save some simulation data
            if self._tm.must_save(i):
                # Run the postpropagate step
                self.propagator.post_propagate()
                self.IOManager.save_wavefunction(self.propagator.get_wavefunction().get_values(), timestep=i)
                # Run the prepropagate step
                self.propagator.pre_propagate()

        # Run the postpropagate step
        self.propagator.post_propagate()
        # Note: We do not save any data here


    def end_simulation(self):
        """Do the necessary cleanup after a simulation. For example request the
        :py:class:`IOManager` to write the data and close the output files.
        """
        self.IOManager.finalize()
    def setUp(self):
        self.io_manager_16 = IOManager(FAT_16_IMAGE_FOR_DEFRAG)
        self.error_maker_16 = self.initialisation_error_maker(self.io_manager_16)

        self.io_manager_32 = IOManager(FAT_32_IMAGE_FOR_DEFRAG)
        self.error_maker_32 = self.initialisation_error_maker(self.io_manager_32)
 def test_jump_back_with_correct_value(self):
     io_manager = IOManager('test_io_manager')
     io_manager.read_some_bytes(1)
     io_manager.jump_back(1)
     self.assertEqual(io_manager._current_position, 0)
 def test_read_bytes_and_convert_to_int_with_incorrect_value(self):
     io_manager = IOManager('test_io_manager')
     self.check_error(io_manager.read_bytes_and_convert_to_int, ValueError, True, 0)
     self.check_error(io_manager.read_bytes_and_convert_to_int, ValueError, True, -10)
 def test_read_bytes_and_convert_to_int_with_correct_value(self):
     io_manager = IOManager('test_io_manager')
     result = io_manager.read_bytes_and_convert_to_int(1)
     self.assertEqual(53, result)
Exemple #28
0
class SimulationLoopHagedornInhomogeneous(SimulationLoop):
    r"""This class acts as the main simulation loop. It owns a propagator that
    propagates a set of initial values during a time evolution.
    """
    def __init__(self, parameters):
        r"""Create a new simulation loop instance for a simulation
        using the semiclassical Hagedorn wavepacket based propagation
        method.

        :param parameters: The simulation parameters.
        :type parameters: A :py:class:`ParameterProvider` instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        # The time propagator instance driving the simulation.
        self.propagator = None

        # A `IOManager` instance for saving simulation results.
        self.IOManager = None

        # The time manager
        self._tm = TimeManager(self.parameters)

        # Set up serialization of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file(self.parameters)

    def prepare_simulation(self):
        r"""Set up a Hagedorn propagator for the simulation loop. Set the
        potential and initial values according to the configuration.

        :raise ValueError: For invalid or missing input data.
        """
        # The potential instance
        potential = BlockFactory().create_potential(self.parameters)

        # Project the initial values to the canonical basis
        BT = BasisTransformationHAWP(potential)

        # Finally create and initialize the propagator instace
        # TODO: Attach the "leading_component to the hawp as codata
        self.propagator = HagedornPropagatorInhomogeneous(
            self.parameters, potential)

        # Create suitable wavepackets
        for packet_descr in self.parameters["initvals"]:
            packet = BlockFactory().create_wavepacket(packet_descr)
            # Transform to canonical basis
            BT.set_matrix_builder(packet.get_quadrature())
            BT.transform_to_canonical(packet)
            # And hand over
            self.propagator.add_wavepacket((packet, ))

        # Add storage for each packet
        npackets = len(self.parameters["initvals"])
        slots = self._tm.compute_number_saves()

        for i in xrange(npackets):
            bid = self.IOManager.create_block()
            self.IOManager.add_inhomogwavepacket(self.parameters,
                                                 timeslots=slots,
                                                 blockid=bid)

        # Write some initial values to disk
        for packet in self.propagator.get_wavepackets():
            self.IOManager.save_inhomogwavepacket_description(
                packet.get_description())
            # Pi
            self.IOManager.save_inhomogwavepacket_parameters(
                packet.get_parameters(), timestep=0)
            # Basis shapes
            for shape in packet.get_basis_shape():
                self.IOManager.save_inhomogwavepacket_basisshapes(shape)
            # Coefficients
            self.IOManager.save_inhomogwavepacket_coefficients(
                packet.get_coefficients(),
                packet.get_basis_shape(),
                timestep=0)

    def run_simulation(self):
        r"""Run the simulation loop for a number of time steps.
        """
        # The number of time steps we will perform.
        nsteps = self._tm.compute_number_timesteps()

        # Run the simulation for a given number of timesteps
        for i in xrange(1, nsteps + 1):
            print(" doing timestep " + str(i))

            self.propagator.propagate()

            # Save some simulation data
            if self._tm.must_save(i):
                # TODO: Generalize for arbitrary number of wavepackets
                packets = self.propagator.get_wavepackets()
                assert len(packets) == 1

                for packet in packets:
                    # Pi
                    self.IOManager.save_inhomogwavepacket_parameters(
                        packet.get_parameters(), timestep=i)
                    # Basis shapes (in case they changed!)
                    for shape in packet.get_basis_shape():
                        self.IOManager.save_inhomogwavepacket_basisshapes(
                            shape)
                    # Coefficients
                    self.IOManager.save_inhomogwavepacket_coefficients(
                        packet.get_coefficients(),
                        packet.get_basis_shape(),
                        timestep=i)

    def end_simulation(self):
        r"""Do the necessary cleanup after a simulation. For example request the
        :py:class:`IOManager` to write the data and close the output files.
        """
        self.IOManager.finalize()
Exemple #29
0
def wire():

    io = IOManager()
    #leds = LEDManager()
    ap = fft.AudioProcessor()

    driver = DriverLPD8806(160,c_order=ChannelOrder.RGB,use_py_spi=True,dev="/dev/spidev0.0",SPISpeed=16)
    ledmatrix = led.LEDMatrix(driver,width=20,height=8,threadedUpdate=True,rotation=1,serpentine=False)

    io.start_stream()

    update = 0
    count = 0.0

    while True:
        #This reads the data from the input stream 
        data = io.read()
    
        #This writes the data out to the hardware audio out port
        
        io.write(data)

        
        if len(data):
            brightness = ap.get_visualizer_array(data,io.chunk,io.rate)

            if update == 0:
                update = 10
            else:
                update = 0

            if int(count) >= int(255.0):
                count = 0.0
            else:
                count += .25
            
            """
            HORIZONTAL
            leds.fill(color=colors.hue2rgb_rainbow(int(count)),start=update*20,end=int((update*20+(brightness[update]*20))))
            leds.fill(color=(0,0,0),start=int(((update*20+(brightness[update]*20)))),end=(update* 20)+20)
            """

            ledmatrix.drawLine(x0=0,y0=int(update),x1=int(brightness[update]*7),y1=update,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update]*7),y0=update,x1=7,y1=update,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+1,x1=int(brightness[update+1]*8),y1=update+1,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+1]*8),y0=update+1,x1=8,y1=update+1,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+2,x1=int(brightness[update+2]*8),y1=update+2,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+2]*8),y0=update+2,x1=8,y1=update+2,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+3,x1=int(brightness[update+3]*7),y1=update+3,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+3]*8),y0=update+3,x1=8,y1=update+3,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+4,x1=int(brightness[update+4]*8),y1=update+4,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+4]*8),y0=update+4,x1=8,y1=update+4,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+5,x1=int(brightness[update+5]*8),y1=update+5,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+5]*8),y0=update+5,x1=8,y1=update+5,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+6,x1=int(brightness[update+6]*8),y1=update+6,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+6]*8),y0=update+6,x1=8,y1=update+6,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+7,x1=int(brightness[update+7]*8),y1=update+7,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+7]*8),y0=update+7,x1=8,y1=update+7,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+8,x1=int(brightness[update+8]*8),y1=update+8,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+8]*8),y0=update+8,x1=8,y1=update+8,color=(0,0,0))

            ledmatrix.drawLine(x0=0,y0=update+9,x1=int(brightness[update+9]*8),y1=update+9,color=colors.hue2rgb_rainbow(int(count)))
            ledmatrix.drawLine(x0=int(brightness[update+9]*8),y0=update+9,x1=8,y1=update+9,color=(0,0,0))

            ledmatrix.update()
        else:
        	ledmatrix.fillScreen(color=(0,0,0))
 def _init_dp(file_name):
     io_manager = IOManager(file_name)
     info = InfoAboutImage(io_manager)
     fp = FatProcessor(info, io_manager)
     return DirectoryParser(fp)
Exemple #31
0
    def __init__(self, io_manager: IOManager):
        io_manager.seek(0)

        self.BS_jmpBoot = io_manager.read_bytes_and_convert_to_int(3)
        self.BS_OEMName = io_manager.read_bytes_and_convert_to_int(8)
        self.BPB_BytsPerSec = io_manager.read_bytes_and_convert_to_int(
            2)  # Количество байт в одном секторе
        #                                                               (512, 1024, 2048 or 4096)
        self.BPB_SecPerClus = io_manager.read_bytes_and_convert_to_int(
            1)  # Количество секторов в кластере
        self.BPB_ResvdSecCnt = io_manager.read_bytes_and_convert_to_int(2)
        self.BPB_NumFATs = io_manager.read_bytes_and_convert_to_int(
            1)  # Количество таблиц FAT на диске
        self.BPB_RootEntCnt = io_manager.read_bytes_and_convert_to_int(
            2)  # Для FAT16 поле содержит число 32-байтных
        #                                                               элементов корневой директории. Для FAT32 дисков,
        #                                                               это поле должно быть 0
        self.BPB_TotSec16 = io_manager.read_bytes_and_convert_to_int(
            2)  # Старое 16-битное поле: общее количество
        #                                                              секторов на диске
        self.BPB_Media = io_manager.read_bytes_and_convert_to_int(1)
        self.BPB_FATSz16 = io_manager.read_bytes_and_convert_to_int(
            2)  # FAT16 это количество секторов одной FAT. Для
        #                                                            FAT32 это значение 0
        self.BPB_SecPerTrk = io_manager.read_bytes_and_convert_to_int(2)
        self.BPB_NumHeads = io_manager.read_bytes_and_convert_to_int(2)
        self.BPB_HiddSec = io_manager.read_bytes_and_convert_to_int(4)
        self.BPB_TotSec32 = io_manager.read_bytes_and_convert_to_int(
            4)  # Новое 32-битное поле: общее количество
        #                                                             секторов на диске

        self.BPB_FATSz32 = io_manager.read_bytes_and_convert_to_int(
            4)  # Поле, необходимое для некоторых вычислений,
        #                                                            может быть некорректным в некоторых ситуациях
        io_manager.jump_back(4)  # сохранение целостности данных

        if self.BPB_FATSz16 != 0:
            fat_sz = self.BPB_FATSz16
        else:
            fat_sz = self.BPB_FATSz32

        self._root_dir_sectors = (
            (self.BPB_RootEntCnt * 32) +
            (self.BPB_BytsPerSec - 1)) // self.BPB_BytsPerSec
        self.first_data_sector = self.BPB_ResvdSecCnt + self._root_dir_sectors + self.BPB_NumFATs * fat_sz

        self.count_of_clusters = self._get_count_of_clusters()
        self.fat_type = self._get_fat_type()

        if self.fat_type == TypeOfFAT.fat16:
            self._parse_fat16_structure(io_manager)
        else:
            self._parse_fat32_structure(io_manager)

        self.first_root_dir_sec = self._get_first_root_dir_sec()
Exemple #32
0

if __name__ == "__main__":

    from Histo1D import Histo1D
    from IOManager import IOManager

    filename = "../data/ds_data18.root"
    logy = True

    h1 = ROOT.TH1D("test1", "TITLE_1", 20, 0.0, 400.0)
    h2 = ROOT.TH1D("test2", "TITLE_2", 20, 0.0, 400.0)
    h3 = ROOT.TH1D("test3", "TITLE_3", 20, 0.0, 400.0)
    IOManager.FillHistogram(h1,
                            filename,
                            tree="DirectStau",
                            varexp="MET",
                            cuts="tau1Pt>650")
    IOManager.FillHistogram(h2,
                            filename,
                            tree="DirectStau",
                            varexp="MET",
                            cuts="tau1Pt>750")
    IOManager.FillHistogram(h3,
                            filename,
                            tree="DirectStau",
                            varexp="MET",
                            cuts="tau1Pt>550")

    p1 = Plot(npads=1)
    p1.Register(h1, 0, template="signal", logy=logy, xunits="GeV")
 def test_fat32_recognition(self):
     io_manager = IOManager(FAT_32_IMAGE)
     file_system = parse_disk_image(io_manager)
     self.assertEqual(file_system.get_type_of_fat(), TypeOfFAT.fat32)
class SimulationLoopFourier(SimulationLoop):
    r"""
    This class acts as the main simulation loop. It owns a propagator that
    propagates a set of initial values during a time evolution. All values are
    read from the ``Parameters.py`` file.
    """

    def __init__(self, parameters):
        r"""
        Create a new simulation loop instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        #: The time propagator instance driving the simulation.
        self.propagator = None

        #: A ``IOManager`` instance for saving simulation results.
        self.IOManager = None

        #: The number of time steps we will perform.
        self.nsteps = parameters["nsteps"]

        # Set up serializing of simulation data
        self.IOManager = IOManager()
        self.IOManager.create_file(self.parameters)
        self.IOManager.create_block()


    def prepare_simulation(self):
        r"""
        Set up a Fourier propagator for the simulation loop. Set the
        potential and initial values according to the configuration.

        :raise ValueError: For invalid or missing input data.
        """
        # Compute the position space grid points
        nodes = self.parameters["f"] * sp.pi * sp.arange(-1, 1, 2.0/self.parameters["ngn"], dtype=np.complexfloating)

        # The potential instance
        potential = PF().create_potential(self.parameters)

        # Check for enough initial values
        if not self.parameters.has_key("initial_values"):
            if len(self.parameters["parameters"]) < potential.get_number_components():
                raise ValueError("Too few initial states given. Parameters are missing.")

            if len(self.parameters["coefficients"]) < potential.get_number_components():
                raise ValueError("Too few initial states given. Coefficients are missing.")

        # Calculate the initial values sampled from a hagedorn wave packet
        d = dict([("ncomponents", 1), ("basis_size", self.parameters["basis_size"]), ("eps", self.parameters["eps"])])

        # Initial values given in the "fourier" specific format
        if self.parameters.has_key("initial_values"):
            initialvalues = [ np.zeros(nodes.shape, dtype=np.complexfloating) for i in xrange(self.parameters["ncomponents"]) ]

            for level, params, coeffs in self.parameters["initial_values"]:
                hwp = HagedornWavepacket(d)
                hwp.set_parameters(params)

                for index, value in coeffs:
                    hwp.set_coefficient(0, index, value)

                iv = hwp.evaluate_at(nodes, component=0, prefactor=True)

                initialvalues[level] = initialvalues[level] + iv

        # Initial value read in compatibility mode to the packet algorithms
        else:
            # See if we have a list of parameter tuples or just a single 5-tuple
            # This is for compatibility with the inhomogeneous case.
            try:
                # We have a list of parameter tuples this is ok for the loop below
                len(self.parameters["parameters"][0])
                parameters = self.parameters["parameters"]
            except TypeError:
                # We have just a single 5-tuple of parameters, we need to replicate for looping
                parameters = [ self.parameters["parameters"] for i in xrange(self.parameters["ncomponents"]) ]

            initialvalues = []

            for level, item in enumerate(parameters):
                hwp = HagedornWavepacket(d)
                hwp.set_parameters(item)

                # Set the coefficients of the basis functions
                for index, value in self.parameters["coefficients"][level]:
                    hwp.set_coefficient(0, index, value)

                iv = hwp.evaluate_at(nodes, component=0, prefactor=True)

                initialvalues.append(iv)

        # Project the initial values to the canonical basis
        initialvalues = potential.project_to_canonical(nodes, initialvalues)

        # Store the initial values in a WaveFunction object
        IV = WaveFunction(self.parameters)
        IV.set_grid(nodes)
        IV.set_values(initialvalues)

        # Finally create and initialize the propagator instace
        self.propagator = FourierPropagator(potential, IV, self.parameters)

        # Which data do we want to save
        tm = self.parameters.get_timemanager()
        slots = tm.compute_number_saves()

        print(tm)

        self.IOManager.add_grid(self.parameters, blockid="global")
        self.IOManager.add_fourieroperators(self.parameters)
        self.IOManager.add_wavefunction(self.parameters, timeslots=slots)

        # Write some initial values to disk
        self.IOManager.save_grid(nodes, blockid="global")
        self.IOManager.save_fourieroperators(self.propagator.get_operators())
        self.IOManager.save_wavefunction(IV.get_values(), timestep=0)


    def run_simulation(self):
        r"""
        Run the simulation loop for a number of time steps. The number of steps is calculated in the ``initialize`` function.
        """
        tm = self.parameters.get_timemanager()

        # Run the simulation for a given number of timesteps
        for i in xrange(1, self.nsteps+1):
            print(" doing timestep "+str(i))

            self.propagator.propagate()

            # Save some simulation data
            if tm.must_save(i):
                self.IOManager.save_wavefunction(self.propagator.get_wavefunction().get_values(), timestep=i)


    def end_simulation(self):
        r"""
        Do the necessary cleanup after a simulation. For example request the
        IOManager to write the data and close the output files.
        """
        self.IOManager.finalize()
Exemple #35
0
 def _parse_fat32_structure(self, io_manager: IOManager):
     self.BPB_FATSz32 = io_manager.read_bytes_and_convert_to_int(4)
     self.BPB_ExtFlags = io_manager.read_bytes_and_convert_to_int(2)
     self.BPB_FSVer = io_manager.read_bytes_and_convert_to_int(2)
     self.BPB_RootClus = io_manager.read_bytes_and_convert_to_int(
         4)  # номер первого кластера корневой директории
     self.BPB_FSInfo = io_manager.read_bytes_and_convert_to_int(2)
     self.BPB_BkBootSec = io_manager.read_bytes_and_convert_to_int(2)
     self.BPB_Reserved = io_manager.read_bytes_and_convert_to_int(12)
     self.BS_DrvNum = io_manager.read_bytes_and_convert_to_int(1)
     self.BS_Reserved1 = io_manager.read_bytes_and_convert_to_int(1)
     self.BS_BootSig = io_manager.read_bytes_and_convert_to_int(1)
     self.BS_VolID = io_manager.read_bytes_and_convert_to_int(4)
     self.BS_VolLab = io_manager.read_bytes_and_convert_to_int(11)
     self.BS_FilSysType = io_manager.read_bytes_and_convert_to_int(8)
 def test_jump_back_with_incorrect_value(self):
     io_manager = IOManager('test_io_manager')
     io_manager.read_some_bytes(1)
     self.check_error(io_manager.jump_back, ValueError, True, 2)
     self.check_error(io_manager.jump_back, ValueError, True, 0)
     self.check_error(io_manager.jump_back, ValueError, True, -10)
 def test_read_some_bytes_with_correct_value(self):
     io_manager = IOManager('test_io_manager')
     result = io_manager.read_some_bytes(1)
     self.assertEqual(b'5', result)
 def _init_fp(file_name):
     io_manager = IOManager(file_name)
     info = InfoAboutImage(io_manager)
     return FatProcessor(info, io_manager)
class SimulationLoopSpawnAdiabatic(SimulationLoop):
    r"""
    This class acts as the main simulation loop. It owns a propagator that
    propagates a set of initial values during a time evolution. All values are
    read from the ``Parameters.py`` file.
    """

    def __init__(self, parameters):
        r"""
        Create a new simulation loop instance.
        """
        # Keep a reference to the simulation parameters
        self.parameters = parameters

        self.tm = TimeManager(parameters)

        #: The time propagator instance driving the simulation.
        self.propagator = None

        #: A ``IOManager`` instance for saving simulation results.
        self.iom = IOManager()
        self.iom.create_file(parameters)
        self.gid = self.iom.create_group()


    def prepare_simulation(self):
        r"""
        Set up a Spawning propagator for the simulation loop. Set the
        potential and initial values according to the configuration.

        :raise ValueError: For invalid or missing input data.
        """
        potential = PotentialFactory().create_potential(self.parameters)
        N = potential.get_number_components()

        # Check for enough initial values
        if self.parameters["leading_component"] > N:
            raise ValueError("Leading component index out of range.")

        if len(self.parameters["parameters"]) < N:
            raise ValueError("Too few initial states given. Parameters are missing.")

        if len(self.parameters["coefficients"]) < N:
            raise ValueError("Too few initial states given. Coefficients are missing.")

        # Create a suitable wave packet
        packet = HagedornWavepacket(self.parameters)
        packet.set_parameters(self.parameters["parameters"][self.parameters["leading_component"]])
        packet.set_quadrature(None)

        # Set the initial values
        for component, data in enumerate(self.parameters["coefficients"]):
            for index, value in data:
                packet.set_coefficient(component, index, value)

        # Project the initial values to the canonical basis
        packet.project_to_canonical(potential)

        # Finally create and initialize the propagator instace
        inner = HagedornPropagator(potential, packet, self.parameters["leading_component"], self.parameters)
        self.propagator = SpawnAdiabaticPropagator(inner, potential, packet, self.parameters["leading_component"], self.parameters)

        # Write some initial values to disk
        slots = self.tm.compute_number_saves()
        for packet in self.propagator.get_wavepackets():
            bid = self.iom.create_block(groupid=self.gid)
            self.iom.add_wavepacket(self.parameters, timeslots=slots, blockid=bid)
            self.iom.save_wavepacket_coefficients(packet.get_coefficients(), blockid=bid, timestep=0)
            self.iom.save_wavepacket_parameters(packet.get_parameters(), blockid=bid, timestep=0)


    def run_simulation(self):
        r"""
        Run the simulation loop for a number of time steps. The number of steps is calculated in the ``initialize`` function.
        """
        tm = self.tm

        # Run the simulation for a given number of timesteps
        for i in xrange(1, tm.get_nsteps()+1):
            print(" doing timestep "+str(i))

            self.propagator.propagate(tm.compute_time(i))

            # Save some simulation data
            if tm.must_save(i):
                # Check if we need more data blocks for newly spawned packets
                while self.iom.get_number_blocks(groupid=self.gid) < self.propagator.get_number_packets():
                    bid = self.iom.create_block(groupid=self.gid)
                    self.iom.add_wavepacket(self.parameters, blockid=bid)

                for index, packet in enumerate(self.propagator.get_wavepackets()):
                    self.iom.save_wavepacket_coefficients(packet.get_coefficients(), timestep=i, blockid=index)
                    self.iom.save_wavepacket_parameters(packet.get_parameters(), timestep=i, blockid=index)


    def end_simulation(self):
        r"""
        Do the necessary cleanup after a simulation. For example request the
        IOManager to write the data and close the output files.
        """
        self.iom.finalize()