Ejemplo n.º 1
0
    def __init__(self, name, num_rtol, num_atol, order, units, coupling,
                 external_coupling, start_rate, initial_state):

        algebraic_functions = {}

        state_vars = []

        input_vars = []

        AbstractSimulationUnit.__init__(self, name, algebraic_functions,
                                        state_vars, input_vars)

        self._num_rtol = num_rtol
        self._num_atol = num_atol

        self._start_rate = start_rate

        self._initial_state = initial_state
        self._units = units
        self._coupling = coupling
        self._external_coupling = external_coupling
        self._order = order

        self._y = None

        self._inputs = None
Ejemplo n.º 2
0
    def __init__(self, name, num_rtol, num_atol, state_transition_function,
                 autonomous):
        """
        The state transition functions hold the encoding of the statechart.
        They are called with the current state and the elapsed time on that state.
        """

        self._state_transition_function = state_transition_function

        self._num_rtol = num_rtol
        self._num_atol = num_atol

        self.in_event = "__in_event"
        self.out_event = "__out_event"
        self.__current_state = "state"

        self.__last_transition_time = 0.0

        self.__autonomous = autonomous

        input_vars = [self.in_event] if not autonomous else []

        AbstractSimulationUnit.__init__(self, name, {},
                                        [self.__current_state, self.out_event],
                                        input_vars)
Ejemplo n.º 3
0
    def exitInitMode(self):
        l.debug(">%s.MultiRateAdaptationUnit.exitInitMode()", self._name)

        AbstractSimulationUnit.exitInitMode(self)

        GaussSeidelMaster.finish_initialize(self._order, self._units)

        l.debug("<%s.MultiRateAdaptationUnit.exitInitMode()", self._name)
Ejemplo n.º 4
0
    def enterInitMode(self):
        l.debug(">%s.DecoupleAdaptation.enterInitMode()", self._name)
        AbstractSimulationUnit.enterInitMode(self)

        # Add a dummy value at time 0, just to fill in the array
        self.setValues(0, 0, {self.in_v: 0.0})

        l.debug("<%s.DecoupleAdaptation.enterInitMode()", self._name)
    def __init__(self, name, num_rtol, num_atol, state_derivatives,
                 algebraic_functions, input_vars):
        AbstractSimulationUnit.__init__(self, name, algebraic_functions,
                                        state_derivatives.keys(), input_vars)

        self._num_rtol = num_rtol
        self._num_atol = num_atol

        self._state_derivatives = state_derivatives
Ejemplo n.º 6
0
    def rollback(self, toStep):
        l.debug(">%s.MultiRateAdaptationUnit.rollback()", self._name)
        AbstractSimulationUnit.rollback(self, toStep)

        for unit in self._units:
            unit.rollback(self._last_commited_step)

        self._step = self._last_commited_step

        l.debug("<%s.MultiRateAdaptationUnit.rollback()", self._name)
Ejemplo n.º 7
0
    def __init__(self, name):

        self.in_v = "in_v"
        self.out_v = "out_v"
        input_vars = [self.in_v]
        state_vars = [self.out_v]

        algebraic_functions = {}

        AbstractSimulationUnit.__init__(self, name, algebraic_functions,
                                        state_vars, input_vars)
Ejemplo n.º 8
0
    def __init__(self, name):

        self.in_event = "in_event"
        self.out_down = "out_down"
        self.out_up = "out_up"
        input_vars = [self.in_event]
        state_vars = [self.out_down, self.out_up]

        algebraic_functions = {}

        AbstractSimulationUnit.__init__(self, name, algebraic_functions,
                                        state_vars, input_vars)
    def exitInitMode(self):
        l.debug(">%s.AlgebraicAdaptation_Power_Window_Obstacle.exitInitMode()",
                self._name)

        AbstractSimulationUnit.exitInitMode(self)

        self._power.exitInitMode()
        self._window.exitInitMode()
        self._obstacle.exitInitMode()

        l.debug("<%s.AlgebraicAdaptation_Power_Window_Obstacle.exitInitMode()",
                self._name)
    def __init__(self, name, num_rtol, num_atol, START_STEP_SIZE,
                 FMU_START_RATE):

        self._num_rtol = num_rtol
        self._num_atol = num_atol

        self._max_iterations = 100

        self._power = PowerFMU("power",
                               num_rtol,
                               num_atol,
                               START_STEP_SIZE / FMU_START_RATE,
                               J=0.085,
                               b=5,
                               K=7.45,
                               R=0.15,
                               L=0.036,
                               V_a=12)

        self.omega = self._power.omega
        self.theta = self._power.theta

        self._window = WindowFMU("window",
                                 num_rtol,
                                 num_atol,
                                 START_STEP_SIZE / FMU_START_RATE,
                                 r=0.11,
                                 b=10)

        self._obstacle = ObstacleFMU("obstacle",
                                     num_rtol,
                                     num_atol,
                                     START_STEP_SIZE / FMU_START_RATE,
                                     c=1e5,
                                     fixed_x=0.45)

        self.up = self._power.up
        self.down = self._power.down

        input_vars = [self.down, self.up]

        self.i = self._power.i
        self.omega = self._power.omega
        self.theta = self._power.theta
        self.x = self._window.x
        self.F = self._obstacle.F

        state_vars = [self.i, self.omega, self.theta, self.x, self.F]

        algebraic_functions = {}

        AbstractSimulationUnit.__init__(self, name, algebraic_functions,
                                        state_vars, input_vars)
Ejemplo n.º 11
0
    def __init__(self, name, input_var, output_var):

        self.input_var = "in_v"
        self.output_var = "out_v"

        self.__value_to_hold = 0.0

        input_vars = []
        state_vars = []

        algebraic_functions = {}

        AbstractSimulationUnit.__init__(self, name, algebraic_functions,
                                        state_vars, input_vars)
Ejemplo n.º 12
0
 def __init__(self, name, num_rtol, num_atol, threshold, upward):
     
     self._num_rtol = num_rtol
     self._num_atol = num_atol
     
     assert upward, "Not implemented yet."
     
     self.__crossUpward = upward
     
     self.__threshold = threshold
     
     self.armature_current = "armature_current"
     self.out_obj = "out_obj"
     input_vars = [self.armature_current]
     state_vars = [self.out_obj]
     
     algebraic_functions = {}
     AbstractSimulationUnit.__init__(self, name, algebraic_functions, state_vars, input_vars)
Ejemplo n.º 13
0
    def enterInitMode(self):
        l.debug(">%s.MultiRateAdaptationUnit.enterInitMode()", self._name)

        AbstractSimulationUnit.enterInitMode(self)

        self._y = []
        for _ in self._units:
            self._y.append(None)

        self._step = 0
        self._last_commited_step = 0

        GaussSeidelMaster.start_initialize(self._order, self._units)

        GaussSeidelMaster.set_initial_states(self._units, self._order,
                                             self._initial_state)

        l.debug("<%s.MultiRateAdaptationUnit.enterInitMode()", self._name)
 def __init__(self, name, num_rtol, num_atol, state_transition_function, initial_state, input_vars, output_vars):
     """
     The state transition functions hold the encoding of the statechart.
     They are called with the current state and the elapsed time on that state.
     """
     
     self._state_transition_function = state_transition_function
     
     self._num_rtol = num_rtol
     self._num_atol = num_atol
     
     self.__current_state = "state"
     
     self.__initial_state = initial_state
     
     self.__last_transition_time = 0.0
     
     state_vars = [self.__current_state] + output_vars
     
     AbstractSimulationUnit.__init__(self, name, {}, state_vars, input_vars)
 def setValues(self, step, iteration, values):
     l.debug(">%s.StatechartSimulationUnit_CTInOut.setValues(%d, %d, %s)", self._name, step, iteration, values)
     
     AbstractSimulationUnit.setValues(self, step, iteration, values)
     
     if self._mode == INIT_MODE:
         assert step == 0
         state_snapshot = self.getValues(step, iteration, [self.__current_state])
         input_snapshot = self.getValues(step, iteration, self._getInputVars())
         previous_input_snaptshop =  {}
         
         (next_state, output_assignments) = self._doStepFunction(0.0, 
                                                 state_snapshot, input_snapshot, previous_input_snaptshop)
         
         # Commit the new state and outputs.
         AbstractSimulationUnit.setValues(self, step, iteration, {self.__current_state : next_state})
         AbstractSimulationUnit.setValues(self, step, iteration, output_assignments)
             
     l.debug("<%s.StatechartSimulationUnit_CTInOut.setValues()", self._name)
 def enterInitMode(self):
     l.debug(">%s.StatechartSimulationUnit_CTInOut.enterInitMode()", self._name)
     
     self.setValues(0, 0, {self.__current_state: self.__initial_state})
     
     # Compute all default transitions at this point
     state_snapshot = self.getValues(0, 0, [self.__current_state])
     input_snapshot = {}
     previous_input_snaptshop =  {}
     
     (next_state, output_assignments) = self._doStepFunction(0.0, 
                                             state_snapshot, input_snapshot, previous_input_snaptshop)
     
     # Commit the new state and outputs.
     AbstractSimulationUnit.setValues(self, 0, 0, {self.__current_state : next_state})
     AbstractSimulationUnit.setValues(self, 0, 0, output_assignments)
     
     AbstractSimulationUnit.enterInitMode(self)
     
     l.debug("<%s.StatechartSimulationUnit_CTInOut.enterInitMode()", self._name)
    def setValues(self, step, iteration, values):
        l.debug(
            ">%s.AlgebraicAdaptation_Power_Window_Obstacle.setValues(%d, %d, %s)",
            self._name, step, iteration, values)

        # Filter just the inputs.
        inputs = {self.up: values[self.up], self.down: values[self.down]}

        AbstractSimulationUnit.setValues(self, step, iteration, inputs)

        if self._mode == INIT_MODE:
            # Initialize the internal FMUs and compute the value of the armature.

            l.debug("Initializing strong component...")

            step = iteration = 0

            wOut_tau_delayed = 0.0
            wOut_x_delayed = 0.0

            # Set power inputs (or initial state, given by values)
            self._power.setValues(step, iteration, values)
            self._power.setValues(step, iteration,
                                  {self._power.tau: wOut_tau_delayed})

            # Get power initial outputs
            pOut = self._power.getValues(
                step, iteration,
                [self._power.omega, self._power.theta, self._power.i])

            # Set obstacle initial inputs
            # Assume they are zero because the outputs of the window are delayed.
            self._obstacle.setValues(step, iteration,
                                     {self._obstacle.x: wOut_x_delayed})
            # Get obstacle outputs
            oOut = self._obstacle.getValues(step, iteration,
                                            [self._obstacle.F])

            # Set window inputs
            self._window.setValues(
                step, iteration, {
                    self._window.omega_input: pOut[self._power.omega],
                    self._window.theta_input: pOut[self._power.theta],
                    self._window.F_obj: oOut[self._obstacle.F]
                })
            # Get window outputs
            wOut = self._window.getValues(step, iteration,
                                          [self._window.x, self._window.tau])

            # Set corrected power windows
            self._power.setValues(
                step,
                iteration,
                {
                    self._power.tau:
                    wOut[self._window.tau]  # Delayed input from window
                })

            # We know that convergence is easily achieved for this initialisation
            assert self._isClose(wOut[self._window.tau], wOut_tau_delayed)
            assert self._isClose(wOut[self._window.x], wOut_x_delayed)

            # Record the outputs
            AbstractSimulationUnit.setValues(
                self, step, iteration, {
                    self.i: pOut[self._power.i],
                    self.theta: pOut[self._power.theta],
                    self.omega: pOut[self._power.omega],
                    self.x: wOut[self._window.x],
                    self.F: oOut[self._obstacle.F]
                })

            l.debug("Strong component initialized.")

        l.debug("<%s.AlgebraicAdaptation_Power_Window_Obstacle.setValues()",
                self._name)
    def _doInternalSteps(self, time, step, iteration, step_size):
        l.debug(">%s._doInternalSteps(%f, %d, %d, %f)", self._name, time, step,
                iteration, step_size)

        assert step_size > 0.0, "step_size too small: {0}".format(step_size)
        #assert self._biggerThan(step_size, 0), "step_size too small: {0}".format(step_size)
        assert iteration == 0, "Fixed point iterations not supported outside of this component."

        converged = False
        internal_iteration = 0

        cOut = self.getValues(step, iteration, [self.up, self.down])
        wOut = self._window.getValues(step - 1, iteration,
                                      [self._window.x, self._window.tau])
        pOut = None

        while not converged and internal_iteration < self._max_iterations:
            self._power.setValues(
                step,
                iteration,
                {
                    self._power.tau: wOut[self._window.tau],  # Delayed input
                    self._power.up: cOut[self.up],
                    self._power.down: cOut[self.down]
                })
            self._power.doStep(time, step, iteration, step_size)
            pOut = self._power.getValues(
                step, iteration,
                [self._power.omega, self._power.theta, self._power.i])

            self._obstacle.setValues(
                step, iteration,
                {self._obstacle.x: wOut[self._window.x]})  # Delayed input
            self._obstacle.doStep(time, step, iteration, step_size)
            oOut = self._obstacle.getValues(step, iteration,
                                            [self._obstacle.F])

            self._window.setValues(
                step, iteration, {
                    self._window.omega_input: pOut[self._power.omega],
                    self._window.theta_input: pOut[self._power.theta],
                    self._window.F_obj: oOut[self._obstacle.F]
                })
            self._window.doStep(time, step, iteration, step_size)
            wOut_corrected = self._window.getValues(
                step, iteration, [self._window.x, self._window.tau])

            l.debug("Iteration step completed:")
            l.debug("wOut=%s;", wOut)
            l.debug("wOut_corrected=%s.", wOut_corrected)


            if self._isClose(wOut[self._window.x], wOut_corrected[self._window.x]) \
                and self._isClose(wOut[self._window.tau], wOut_corrected[self._window.tau]):
                converged = True

            internal_iteration = internal_iteration + 1
            wOut = wOut_corrected

        if converged:
            l.debug("Fixed point found after %d iterations",
                    internal_iteration)
        else:
            l.debug("Fixed point not found after %d iterations",
                    internal_iteration)
            raise RuntimeError("Fixed point not found")

        AbstractSimulationUnit.setValues(
            self, step, iteration, {
                self.i: pOut[self._power.i],
                self.theta: pOut[self._power.theta],
                self.omega: pOut[self._power.omega],
                self.x: wOut[self._window.x],
                self.F: oOut[self._obstacle.F]
            })

        l.debug("<%s._doInternalSteps() = (%s, %d)", self._name, STEP_ACCEPT,
                step_size)
        return (STEP_ACCEPT, step_size)