Esempio n. 1
0
    def setMessageCopy(self, copy_method):
        """
        Sets the type of message copying to use, this will have an impact on performance. It is made customizable as some more general techniques will be much slower.

        :param copy_method: Either an ID of the option, or (recommended) a string specifying the method, see options below

        .. glossary::

           pickle
                use the (c)pickle module

           custom
                define a custom 'copy' function in every message and use this

           none
                don't use any copying at all, unsafe though most other DEVS simulators only supply this
        """
        if not isinstance(copy_method, int) and not isinstance(
                copy_method, str):
            raise DEVSException(
                "Message copy method should be done using an integer or a string"
            )
        if isinstance(copy_method, str):
            options = {"pickle": 0, "custom": 1, "none": 2}
            try:
                copy_method = options[copy_method]
            except IndexError:
                raise DEVSException("Message copy option %s not recognized" %
                                    copy_method)
        self.simulator.msgCopy = copy_method
Esempio n. 2
0
    def saveVCD(self, modelName, time, portName, vcdState):
        """
        Trace a VCD entry
        Args:
            modelName - the name of the model
            time - time at which transition happened
            portName - name of the port
            vcdState - state to trace on the specified port
        """
        # Check if the signal is a valid binary signal
        for i in range(len(vcdState)):
            if (i == 0):
                if vcdState[i] == 'b':
                    continue
                else:
                    raise DEVSException(
                        "Port " + portName + " in model " + modelName +
                        " does not carry a binary signal\n" +
                        "VCD exports should carry a binary signal, not: " +
                        str(vcdState))
            char = vcdState[i]
            if char not in ["0", "1", "E", "x"]:
                raise DEVSException(
                    "Port " + portName + " in model " + modelName +
                    " does not carry a binary signal\n" +
                    "VCD exports should carry a binary signal, not: " +
                    str(vcdState))
        # Find the identifier of this wire
        for i in range(len(self.vcd_varList)):
            if self.vcd_varList[i].modelName == modelName and self.vcd_varList[
                    i].portName == portName:
                identifier = str(self.vcd_varList[i].identifier)
                recordNr = i
                break
            # If the bitSize is not yet defined, define it now
            if self.vcd_varList[i].bitSize is None:
                self.vcd_varList[i].bitSize = len(vcdState) - 1
            elif self.vcd_varList[i].bitSize != len(vcdState) - 1:
                raise DEVSException(
                    "Wire has changing bitsize!\nYou are probably not using bit encoding on the wires!"
                )
            # Now we have to convert between logisim and VCD notation
            vcdState = vcdState.replace('x', 'z')
            vcdState = vcdState.replace('E', 'x')
            # identifier will be defined, otherwise the record was not in the list
            if time > self.vcd_prevtime:
                # Convert float to integer without losing precision
                # ex. 5.0 --> 50, 5.5 --> 55
                t = time[0]
            vcdTime = int(
                str(int(floor(t))) +
                str(int(t - floor(t)) * (len(str(t)) - 2)))

            if (self.vcd_prevtime != vcdTime):
                # The time has passed, so add a new VCD header
                self.vcd_file.write(("#" + str(vcdTime) + "\n").encode())
                self.vcd_prevtime = vcdTime

        self.vcd_file.write((vcdState + " " + identifier + "\n").encode())
Esempio n. 3
0
    def setStateSaving(self, state_saving):
        """
        Sets the type of state saving to use, this will have a high impact on performance. It is made customizable as some more general techniques will be much slower, though necessary in certain models.

        :param state_saving: Either an ID of the option, or (recommended) a string specifying the method, see options below.

        .. glossary::

           deepcopy
                use the deepcopy module

           pickle0
                use the (c)pickle module with pickling protocol 0

           pickleH
                use the (c)pickle module with the highest available protocol

           pickle
                use the (c)pickle module

           copy
                use the copy module (only safe for flat states)

           assign
                simply assign states (only safe for primitive states)

           none
                equivalent to assign (only safe for primitive states)

           custom
                define a custom 'copy' function in every state and use this
        """
        if not isinstance(state_saving, int) and not isinstance(
                state_saving, str):
            raise DEVSException(
                "State saving should be done using an integer or a string")
        if isinstance(state_saving, str):
            options = {
                "deepcopy": 0,
                "pickle0": 1,
                "pickleH": 2,
                "pickle": 2,
                "copy": 3,
                "assign": 4,
                "none": 4,
                "custom": 5,
                "marshal": 6
            }
            try:
                state_saving = options[state_saving]
            except IndexError:
                raise DEVSException("State saving option %s not recognized" %
                                    state_saving)
        self.simulator.state_saving = state_saving
Esempio n. 4
0
    def setGVTInterval(self, gvt_int):
        """
        Sets the interval in seconds between 2 GVT calculations. This is the time between the ending of the previous run and the start of the next run, to prevent overlapping calculations.

        .. note:: Parameter should be at least 1 to prevent an overload of GVT calculations

        :param gvt_int: interval in seconds (float or integer)
        """
        if not isinstance(gvt_int, float) and not isinstance(gvt_int, int):
            raise DEVSException("GVT interval should be an integer or a float")
        if gvt_int < 1:
            raise DEVSException(
                "GVT interval should be larger than or equal to one")
        self.simulator.GVT_interval = gvt_int
Esempio n. 5
0
    def setTerminationTime(self, time):
        """
        Sets the termination time for the simulation. Setting this will remove a previous termination time and condition.

        :param time: time at which simulation should be halted
        """
        if not isinstance(time, float) and not isinstance(time, int):
            raise DEVSException(
                "Simulation termination time should be either an integer or a float"
            )
        if time < 0:
            raise DEVSException(
                "Simulation termination time cannot be negative")
        self.simulator.termination_condition = None
        # Convert to float, as otherwise we would have to do this conversion implicitly at every iteration
        self.simulator.termination_time = float(time)
Esempio n. 6
0
    def setTerminationModel(self, model):
        """
        Marks a specific AtomicDEVS model as being used in a termination condition. This is never needed in case no termination_condition is used. It will _force_ the model to run at the controller, ignoring the location that was provided in the model itself. Furthermore, it will prevent the model from migrating elsewhere.

        :param model: an AtomicDEVS model that needs to run on the controller and shouldn't be allowed to migrate
        """
        if self.simulator.setup:
            raise DEVSException(
                "Termination models cannot be changed after the first simulation was already ran!"
            )
        if isinstance(model, AtomicDEVS):
            self.simulator.termination_models.add(model.model_id)
        elif isinstance(model, int):
            # A model_id in itself is passed, so just add this
            self.simulator.termination_models.add(model)
        else:
            raise DEVSException(
                "Only AtomicDEVS models can be used in termination conditions!"
            )
Esempio n. 7
0
    def setClassicDEVS(self, classicDEVS=True):
        """
        Use Classic DEVS instead of Parallel DEVS. This option does not affect the use of Dynamic Structure DEVS or realtime simulation. Not usable with distributed simulation.

        :param classicDEVS: whether or not to use Classic DEVS
        """
        if not local(self.simulator) and classicDEVS:
            raise DEVSException(
                "Classic DEVS simulations cannot be distributed!")
        self.simulator.classicDEVS = classicDEVS
Esempio n. 8
0
    def setActivityTrackingVisualisation(self, visualize, x=0, y=0):
        """
        Set the simulation to visualize the results from activity tracking. An x and y parameter can be given to visualize it in a cell style.

        :param visualize: whether or not to visualize it
        :param x: the horizontal size of the grid (optional)
        :param y: the vertical size of the grid (optional)
        """
        if not isinstance(visualize, bool):
            raise DEVSException(
                "Activity Tracking visualisation requires a boolean")
        if visualize and ((x > 0 and y <= 0) or (y > 0 and x <= 0)):
            raise DEVSException(
                "Activity Tracking cell view requires both a positive x and y parameter for the maximal size of the grid"
            )
        self.simulator.activityVisualisation = visualize
        self.simulator.activityTracking = visualize
        self.simulator.x_size = int(x)
        self.simulator.y_size = int(y)
Esempio n. 9
0
def local(sim):
    """
    Test whether or not the simulation is done locally

    :param sim: the simulator with the locations
    :returns: bool -- whether or not the simulation is local
    """
    if len(sim.locations) == 0:
        raise DEVSException(
            "There are no Atomic DEVS models present in your provided model")
    return sim.server.size == 1
Esempio n. 10
0
    def setVCD(self, filename):
        """
        Sets the use of a VCD tracer.

        Calling this function multiple times will register a tracer for each of them (thus output to multiple files is possible, though more inefficient than simply (manually) copying the file at the end).

        :param filename: string representing the filename to write the trace to
        """
        if not isinstance(filename, str):
            raise DEVSException("VCD filename should be a string")
        self.setCustomTracer("tracerVCD", "TracerVCD", [filename])
Esempio n. 11
0
    def setRealTime(self, realtime=True, scale=1.0):
        """
        Sets the use of realtime instead of 'as fast as possible'.

        :param realtime: whether or not to use realtime simulation
        :param scale: the scale for scaled real time, every delay will be multiplied with this number
        """
        if not local(self.simulator):
            raise DEVSException(
                "Real time simulation is only possible in local simulation!")
        self.simulator.realtime = realtime
        self.simulator.realtimeScale = scale
Esempio n. 12
0
    def setRealTimePorts(self, ports):
        """
        Sets the dictionary of ports that can be used to put input on. If realtime is not yet set, this will auto-enable it.

        :param ports: dictionary with strings as keys, ports as values
        """
        if not self.simulator.realtime:
            self.setRealTime(True)
        if not isinstance(ports, dict):
            raise DEVSException(
                "Realtime input port references should be a dictionary")
        self.simulator.realTimeInputPortReferences = ports
Esempio n. 13
0
    def setActivityRelocatorBasicBoundary(self, swappiness):
        """
        Sets the use of the *activity* relocator called *'Basic Boundary'*.

        :param swappiness: how big the deviation from the average should be before scheduling relocations
        """
        if swappiness < 1.0:
            raise DEVSException(
                "Basic Boundary Activity Relocator should have a swappiness >= 1.0"
            )
        self.setActivityRelocatorCustom("basicBoundaryRelocator",
                                        "BasicBoundaryRelocator", swappiness)
Esempio n. 14
0
    def setCheckpointing(self, name, chk_int):
        """
        .. warning:: name parameter will be used as a filename, so avoid special characters

        Sets the interval between 2 checkpoints in terms of GVT calculations. This option generates PDC files starting with 'name'. This is only possible when using MPI.

        :param name: name to prepend to each checkpoint file
        :param chk_int: number of GVT runs that are required to trigger a checkpoint. For example 3 means that a checkpoint will be created after each third GVT calculation
        """
        if not isinstance(chk_int, int):
            raise DEVSException("Checkpoint interval should be an integer")
        if not isinstance(name, str):
            raise DEVSException("Checkpoint name should be a string")
        if chk_int < 1:
            raise DEVSException(
                "Checkpoint interval should be larger than or equal to one")
        if self.simulator.realtime:
            raise DEVSException(
                "Checkpointing is not possible under realtime simulation")
        self.simulator.CHK_interval = chk_int
        self.simulator.CHK_name = name
Esempio n. 15
0
    def setSchedulerDiscreteTime(self, locations=None):
        """
        Use a basic 'discrete time' style scheduler. If the model is scheduled, it has to be at the same time as all other scheduled models. It isn't really discrete time in the sense that it allows variable step sizes, only should ALL models agree on it.

        :param locations: if it is an iterable, the scheduler will only be applied to these locations. If it is None, all nodes will be affected.

        .. warning:: Only use in local simulation!
        """
        if not local(self):
            raise DEVSException(
                "Do not use this scheduler for distributed simulation")
        self.setSchedulerCustom("schedulerDT", "SchedulerDT", locations)
Esempio n. 16
0
    def setVerbose(self, filename=None):
        """
        Sets the use of a verbose tracer.

        Calling this function multiple times will register a tracer for each of them (thus output to multiple files is possible, though more inefficient than simply (manually) copying the file at the end).

        :param filename: string representing the filename to write the trace to, None means stdout
        """
        if not isinstance(filename, str) and filename is not None:
            raise DEVSException(
                "Verbose filename should either be None or a string")
        self.setCustomTracer("tracerVerbose", "TracerVerbose", [filename])
Esempio n. 17
0
    def setRealTimeInputFile(self, generatorfile):
        """
        Sets the realtime input file to use. If realtime is not yet set, this will auto-enable it.

        :param generatorfile: the file to use, should be a string, NOT a file handle. None is acceptable if no file should be used.
        """
        if not self.simulator.realtime:
            self.setRealTime(True)
        if not isinstance(generatorfile, str) and generatorfile is not None:
            raise DEVSException(
                "Realtime generator should be a string or None")
        self.simulator.generatorfile = generatorfile
Esempio n. 18
0
    def setCell(self,
                x_size=None,
                y_size=None,
                cell_file="celltrace",
                multifile=False):
        """
        Sets the cell tracing flag of the simulation

        :param cell: whether or not verbose output should be generated
        :param x_size: the horizontal length of the grid
        :param y_size: the vertical length of the grid
        :param cell_file: the file to save the generated trace to
        :param multifile: if True, each timestep will be save to a seperate file (nice for visualisations)
        """
        if x_size is None or y_size is None:
            raise DEVSException("Cell Tracing requires both an x and y size")
        if x_size < 1 or y_size < 1:
            raise DEVSException("Cell Tracing sizes should be positive")
        self.setCustomTracer(
            "tracerCell", "TracerCell",
            [cell_file, int(x_size),
             int(y_size), multifile])
Esempio n. 19
0
    def setSchedulerCustom(self, filename, schedulerName, locations=None):
        """
        Use a custom scheduler

        :param filename: filename of the file containing the scheduler class
        :param schedulerName: class name of the scheduler contained in the file
        :param locations: if it is an iterable, the scheduler will only be applied to these locations. If it is None, all nodes will be affected.
        """
        if not isinstance(filename, str):
            raise DEVSException("Custom scheduler filename should be a string")
        if not isinstance(schedulerName, str):
            raise DEVSException(
                "Custom scheduler classname should be a string")
        if locations is None:
            # Set global scheduler, so overwrite all previous configs
            self.simulator.schedulerType = (filename, schedulerName)
            self.simulator.schedulerLocations = {}
        else:
            # Only for a subset of models, but keep the default scheduler
            for location in locations:
                self.simulator.schedulerLocations[location] = (filename,
                                                               schedulerName)
Esempio n. 20
0
    def setDSDEVS(self, dsdevs=True):
        """
        Whether or not to enable Dynamic Structure DEVS simulation. If this is set to True, the modelTransition method will be called on all transitioned models. 
        If this is False, the modelTransition method will not be called, even if one is defined! Enabling this incurs a (slight) slowdown in the simulation,
        due to the additional function calls and checks that have to be made. Currently only available in local simulation.

        :param dsdevs: enable or not
        """
        if local(self.simulator):
            self.simulator.dsdevs = dsdevs
        elif not dsdevs:
            raise DEVSException(
                "Dynamic Structure DEVS is currently only available in local simulation!"
            )
Esempio n. 21
0
    def setRelocationDirective(self, time, model, destination):
        """
        Creates a relocation directive, stating that a relocation of a certain model should happen at or after the specified time (depending on when the GVT progresses over this time).

        If multiple directives exist for the same model, the one with the highest time will be executed.

        :param time: time after which the relocation should happen
        :param model: the model to relocate at the specified time. Can either be its ID, or an AtomicDEVS or CoupledDEVS model. Note that providing a CoupledDEVS model is simply a shortcut for relocating the COMPLETE subtree elsewhere, as this does not stop at kernel boundaries.
        :param destination: the location to where the model should be moved
        """
        if not isinstance(destination, int) and not isinstance(
                destination, str):
            raise DEVSException(
                "Relocation directive destination should be an integer or string"
            )
        destination = int(destination)
        if destination not in range(self.simulator.server.size):
            raise DEVSException(
                "Relocation directive got an unknown destination, got: %s, expected one of %s"
                % (destination, range(self.simulator.server.size)))

        from manualRelocator import ManualRelocator
        if not isinstance(self.simulator.activityRelocator, ManualRelocator):
            raise DEVSException(
                "Relocation directives can only be set when using a manual relocator (the default)\nYou seem to have changed the relocator, so please revert it back by calling the 'setManualRelocator()' first!"
            )

        if isinstance(model, int):
            self.simulator.activityRelocator.addDirective(
                time=time, model=model, destination=destination)
        elif isinstance(model, AtomicDEVS):
            self.simulator.activityRelocator.addDirective(
                time=time, model=model.model_id, destination=destination)
        elif isinstance(model, CoupledDEVS):
            for m in model.componentSet:
                self.simulator.setRelocationDirective(time, m, destination)
Esempio n. 22
0
    def setLogging(self, destination, level):
        """
        Sets the logging destination for the syslog server.

        :param destination: A tuple/list containing an address, port pair defining the location of the syslog server. Set to None to prevent modification
        :param level: the level at which logging should happen. This can either be a logging level from the logging module, or it can be a string specifying this level. Accepted strings are: 'debug', 'info', 'warning', 'warn', 'error', 'critical'
        """
        if self.simulator.nested:
            raise DEVSException(
                "Logging in nested simulation is not allowed, the logging settings of the parent are used!"
            )
        if not isinstance(self.destination, tuple) and not isinstance(
                self.simulator.destination, list) and (destination
                                                       is not None):
            raise DEVSException(
                "Logging destination should be a tuple or a list containing an IP addres, followed by a port address"
            )
        if isinstance(level, str):
            import logging
            # Convert to the correct location
            level = level.lower()
            loglevels = {
                "debug": logging.DEBUG,
                "info": logging.INFO,
                "warning": logging.WARN,
                "warn": logging.WARN,
                "error": logging.ERROR,
                "critical": logging.CRITICAL
            }
            try:
                level = loglevels[level]
            except IndexError:
                raise DEVSException("Logging level %s not recognized" % level)
        if destination is not None:
            self.simulator.address = destination
        self.simulator.loglevel = level
Esempio n. 23
0
    def setLocationCellMap(self, locationmap, x=0, y=0):
        """
        Set the simulation to produce a nice Cell DEVS like output file of the current location. This file will be regenerated as soon as some relocations are processed.

        :param locationmap: whether or not to generate this file
        :param x: the horizontal size of the grid
        :param y: the vertical size of the grid
        """
        if locationmap and (x <= 0 or y <= 0):
            raise DEVSException(
                "Location cell view requires a positive x and y parameter for the maximal size of the grid"
            )
        self.simulator.locationCellView = locationmap
        self.simulator.x_size = int(x)
        self.simulator.y_size = int(y)
Esempio n. 24
0
    def setDrawModel(self,
                     drawModel=True,
                     outputfile="model.dot",
                     hideEdgeLabels=False):
        """
        Whether or not to draw the model and its distribution before simulation starts.

        :param drawModel: whether or not to draw the model
        :param hideEdgeLabels: whether or not to hide the labels of the connections, this speeds up the model drawing and allows for reasonably sized diagrams
        """
        if self.simulator.setup:
            raise DEVSException(
                "Model can only be drawn at the first simulation run due to the model being optimized before simulation"
            )
        self.simulator.drawModel = drawModel
        self.simulator.drawModelFile = outputfile
        self.simulator.hideEdgeLabels = hideEdgeLabels