def handle_result(self, solver, t, y):
     Explicit_Problem.handle_result(self, solver, t, y)
     # Extra output for algorithm analysis
     if solver.report_continuously:
         h, nq = solver.get_algorithm_data()
         solver.h_sol.extend([h])
         solver.nq_sol.extend([nq])
Esempio n. 2
0
    def simulate(self, Tend, nIntervals, gridWidth):

        problem = Explicit_Problem(self.rhs, self.y0)
        problem.name = 'RK34'
        problem.handle_result = self.handle_result
        problem.handle_event = self.handle_event
        problem.time_events = self.time_events
        problem.finalize = self.finalize

        if hasattr(self, 'state_events'):
            print 'Warning: state_event function in RK34 is not supported and will be ignored!'

        simulation = RungeKutta34(problem)

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = self.verbosity
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used
        simulation.inith = self.inith

        # Calculate nOutputIntervals:
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Check for feasible input parameters
        if nOutputIntervals == 0:
            print 'Error: gridWidth too high or nIntervals set to 0! Continue with nIntervals=1'
            nOutputIntervals = 1
        # Perform simulation
        simulation.simulate(Tend, nOutputIntervals)  # to get the values: t_new, y_new = simulation.simulate
Esempio n. 3
0
    def simulate(self, Tend, nIntervals, gridWidth):

        problem = Explicit_Problem(self.rhs, self.y0)
        problem.name = 'RK34'
        problem.handle_result = self.handle_result
        problem.handle_event = self.handle_event
        problem.time_events = self.time_events
        problem.finalize = self.finalize

        if hasattr(self, 'state_events'):
            print 'Warning: state_event function in RK34 is not supported and will be ignored!'

        simulation = RungeKutta34(problem)

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = self.verbosity
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used
        simulation.inith = self.inith

        # Calculate nOutputIntervals:
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Check for feasible input parameters
        if nOutputIntervals == 0:
            print 'Error: gridWidth too high or nIntervals set to 0! Continue with nIntervals=1'
            nOutputIntervals = 1
        # Perform simulation
        simulation.simulate(Tend, nOutputIntervals)  # to get the values: t_new, y_new = simulation.simulate
    def simulate(self, Tend, nIntervals, gridWidth):

        problem = Explicit_Problem(self.rhs, self.y0)
        problem.name = 'CVode'
        # solver.rhs = self.right_hand_side
        problem.handle_result = self.handle_result
        problem.state_events = self.state_events
        problem.handle_event = self.handle_event
        problem.time_events = self.time_events
        problem.finalize = self.finalize

        simulation = CVode(problem)

        # Change multistep method: 'adams' or 'VDF'
        if self.discr == 'Adams':
            simulation.discr = 'Adams'
            simulation.maxord = 12
        else:
            simulation.discr = 'BDF'
            simulation.maxord = 5
        # Change iteration algorithm: functional(FixedPoint) or newton
        if self.iter == 'FixedPoint':
            simulation.iter = 'FixedPoint'
        else:
            simulation.iter = 'Newton'

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = self.verbosity
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used

        # '''Initialize problem '''
        # self.t_cur = self.t0
        # self.y_cur = self.y0

        # Calculate nOutputIntervals:
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Check for feasible input parameters
        if nOutputIntervals == 0:
            print 'Error: gridWidth too high or nIntervals set to 0! Continue with nIntervals=1'
            nOutputIntervals = 1
        # Perform simulation
        simulation.simulate(
            Tend, nOutputIntervals
        )  # to get the values: t_new, y_new = simulation.simulate
Esempio n. 5
0
 def test_handle_result(self):
     """
     This function tests the handle result.
     """
     f = lambda t,x: x**0.25
     def handle_result(solver,t,y):
         assert solver.t == t
     
     prob = Explicit_Problem(f, [1.0])
     prob.handle_result = handle_result
     
     sim = CVode(prob)
     sim.continuous_output = True
     sim.simulate(10.)
Esempio n. 6
0
    def simulate(self, Tend, nIntervals, gridWidth):

        problem = Explicit_Problem(self.rhs, self.y0)
        problem.name = 'CVode'
        # solver.rhs = self.right_hand_side
        problem.handle_result = self.handle_result
        problem.state_events = self.state_events
        problem.handle_event = self.handle_event
        problem.time_events = self.time_events
        problem.finalize = self.finalize

        simulation = CVode(problem)

        # Change multistep method: 'adams' or 'VDF'
        if self.discr == 'Adams':
            simulation.discr = 'Adams'
            simulation.maxord = 12
        else:
            simulation.discr = 'BDF'
            simulation.maxord = 5
        # Change iteration algorithm: functional(FixedPoint) or newton
        if self.iter == 'FixedPoint':
            simulation.iter = 'FixedPoint'
        else:
            simulation.iter = 'Newton'

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = self.verbosity
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used

        # '''Initialize problem '''
        # self.t_cur = self.t0
        # self.y_cur = self.y0

        # Calculate nOutputIntervals:
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Check for feasible input parameters
        if nOutputIntervals == 0:
            print 'Error: gridWidth too high or nIntervals set to 0! Continue with nIntervals=1'
            nOutputIntervals = 1
        # Perform simulation
        simulation.simulate(Tend, nOutputIntervals)  # to get the values: t_new, y_new = simulation.simulate
Esempio n. 7
0
 def test_handle_result(self):
     """
     This function tests the handle result.
     """
     f = lambda t,x: x**0.25
     def handle_result(solver,t,y):
         assert solver.t == t
     
     prob = Explicit_Problem(f, [1.0])
     prob.handle_result = handle_result
     
     sim = CVode(prob)
     sim.report_continuously = True
     sim.simulate(10.)
Esempio n. 8
0
 def handle_result(self, solver, t, y):
     if t < self.t_cutoff:
         Explicit_Problem.handle_result(self, solver, t, y)
Esempio n. 9
0
 def handle_result(self, solver, t, y):
     Explicit_Problem.handle_result(self, solver, t, y)
     self.order.append(solver.get_last_order())
Esempio n. 10
0
    def simulate(self, Tend, nIntervals, gridWidth):

        # define assimulo problem:(has to be done here because of the starting value in Explicit_Problem
        solver = Explicit_Problem(self.rhs, self.y0)
        ''' *******DELETE LATER '''''''''
#        problem.handle_event = handle_event
#        problem.state_events = state_events
#        problem.init_mode = init_mode

        solver.handle_result = self.handle_result


        solver.name = 'Simple Explicit Example'
        simulation = CVode(solver)  # Create a RungeKutta34 solver
        # simulation.inith = 0.1 #Sets the initial step, default = 0.01

        # Change multistep method: 'adams' or 'VDF'
        if self.discr == 'Adams':
            simulation.discr = 'Adams'
            simulation.maxord = 12
        else:
            simulation.discr = 'BDF'
            simulation.maxord = 5

        # Change iteration algorithm: functional(FixedPoint) or newton
        if self.iter == 'FixedPoint':
            simulation.iter = 'FixedPoint'
        else:
            simulation.iter = 'Newton'

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = 0
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used

        # Create Solver and set settings
#        noRootFunctions = np.size(self.state_events(self.t0, np.array(self.y0)))

#        solver = sundials.CVodeSolver(RHS = self.f, ROOT = self.rootf, SW = [False]*noRootFunctions,
#                       abstol = self.atol, reltol = self.rtol)
        # solver.settings.JAC = None   #Add user-dependent jacobian here

        '''Initialize problem '''
#        solver.init(self.t0, self.y0)
        self.handle_result(self.t0, self.y0)
        nextTimeEvent = self.time_events(self.t0, self.y0)
        self.t_cur = self.t0
        self.y_cur = self.y0
        state_event = False
#
#
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Define step length depending on if gridWidth or nIntervals has been chosen
        if nOutputIntervals > 0:
            # Last point on grid (does not have to be Tend:)
            if(gridWidth <> None):
                dOutput = gridWidth
            else:
                dOutput = (Tend - self.t0) / nIntervals
        else:
            dOutput = Tend

        outputStepCounter = long(1)
        nextOutputPoint = min(self.t0 + dOutput, Tend)

        while self.t_cur < Tend:

            # Time-Event detection and step time adjustment
            if nextTimeEvent is None or nextOutputPoint < nextTimeEvent:
                time_event = False
                self.t_cur = nextOutputPoint
            else:
                time_event = True
                self.t_cur = nextTimeEvent



            try:
#                #Integrator step
#                self.y_cur = solver.step(self.t_cur)
#                self.y_cur = np.array(self.y_cur)
#                state_event = False
                # Simulate




                # take a step to next output point:
                t_new, y_new = simulation.simulate(self.t_cur)  # 5, 10) #5, 10  self.t_cur self.t_cur  2. argument nsteps Simulate 5 seconds
                # t_new, y_new are both vectors of the time and states at t_cur and all intermediate
                # points before it! So take last values:
                self.t_cur = t_new[-1]
                self.y_cur = y_new[-1]
                state_event = False

            except:
                import sys
                print "Unexpected error:", sys.exc_info()[0]
#            except CVodeRootException, info:
#                self.t_cur = info.t
#                self.y_cur = info.y
#                self.y_cur = np.array(self.y_cur)
#                time_event = False
#                state_event = True
#
#
            # Depending on events have been detected do different tasks
            if time_event or state_event:
                event_info = [state_event, time_event]
                if not self.handle_event(self, event_info):
                    break
                solver.init(self.t_cur, self.y_cur)

                nextTimeEvent = self.time_events(self.t_cur, self.y_cur)
                # If no timeEvent happens:
                if nextTimeEvent <= self.t_cur:
                    nextTimeEvent = None

            if self.t_cur == nextOutputPoint:
                # Write output if not happened before:
                if not time_event and not state_event:
                    self.handle_result(nextOutputPoint, self.y_cur)
                outputStepCounter += 1
                nextOutputPoint = min(self.t0 + outputStepCounter * dOutput, Tend)

        self.finalize()
Esempio n. 11
0
 def handle_result(self, solver, t, y):
     if t < self.t_cutoff:
         Explicit_Problem.handle_result(self, solver, t, y)
Esempio n. 12
0
    def simulate(self, Tend, nIntervals, gridWidth):

        # define assimulo problem:(has to be done here because of the starting value in Explicit_Problem
        solver = Explicit_Problem(self.rhs, self.y0)
        ''' *******DELETE LATER '''''''''
#        problem.handle_event = handle_event
#        problem.state_events = state_events
#        problem.init_mode = init_mode

        solver.handle_result = self.handle_result


        solver.name = 'Simple Explicit Example'
        simulation = CVode(solver)  # Create a RungeKutta34 solver
        # simulation.inith = 0.1 #Sets the initial step, default = 0.01

        # Change multistep method: 'adams' or 'VDF'
        if self.discr == 'Adams':
            simulation.discr = 'Adams'
            simulation.maxord = 12
        else:
            simulation.discr = 'BDF'
            simulation.maxord = 5

        # Change iteration algorithm: functional(FixedPoint) or newton
        if self.iter == 'FixedPoint':
            simulation.iter = 'FixedPoint'
        else:
            simulation.iter = 'Newton'

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = 0
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used

        # Create Solver and set settings
#        noRootFunctions = np.size(self.state_events(self.t0, np.array(self.y0)))

#        solver = sundials.CVodeSolver(RHS = self.f, ROOT = self.rootf, SW = [False]*noRootFunctions,
#                       abstol = self.atol, reltol = self.rtol)
        # solver.settings.JAC = None   #Add user-dependent jacobian here

        '''Initialize problem '''
#        solver.init(self.t0, self.y0)
        self.handle_result(self.t0, self.y0)
        nextTimeEvent = self.time_events(self.t0, self.y0)
        self.t_cur = self.t0
        self.y_cur = self.y0
        state_event = False
#
#
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Define step length depending on if gridWidth or nIntervals has been chosen
        if nOutputIntervals > 0:
            # Last point on grid (does not have to be Tend:)
            if(gridWidth <> None):
                dOutput = gridWidth
            else:
                dOutput = (Tend - self.t0) / nIntervals
        else:
            dOutput = Tend

        outputStepCounter = long(1)
        nextOutputPoint = min(self.t0 + dOutput, Tend)

        while self.t_cur < Tend:

            # Time-Event detection and step time adjustment
            if nextTimeEvent is None or nextOutputPoint < nextTimeEvent:
                time_event = False
                self.t_cur = nextOutputPoint
            else:
                time_event = True
                self.t_cur = nextTimeEvent



            try:
#                #Integrator step
#                self.y_cur = solver.step(self.t_cur)
#                self.y_cur = np.array(self.y_cur)
#                state_event = False
                # Simulate




                # take a step to next output point:
                t_new, y_new = simulation.simulate(self.t_cur)  # 5, 10) #5, 10  self.t_cur self.t_cur  2. argument nsteps Simulate 5 seconds
                # t_new, y_new are both vectors of the time and states at t_cur and all intermediate
                # points before it! So take last values:
                self.t_cur = t_new[-1]
                self.y_cur = y_new[-1]
                state_event = False

            except:
                import sys
                print "Unexpected error:", sys.exc_info()[0]
#            except CVodeRootException, info:
#                self.t_cur = info.t
#                self.y_cur = info.y
#                self.y_cur = np.array(self.y_cur)
#                time_event = False
#                state_event = True
#
#
            # Depending on events have been detected do different tasks
            if time_event or state_event:
                event_info = [state_event, time_event]
                if not self.handle_event(self, event_info):
                    break
                solver.init(self.t_cur, self.y_cur)

                nextTimeEvent = self.time_events(self.t_cur, self.y_cur)
                # If no timeEvent happens:
                if nextTimeEvent <= self.t_cur:
                    nextTimeEvent = None

            if self.t_cur == nextOutputPoint:
                # Write output if not happened before:
                if not time_event and not state_event:
                    self.handle_result(nextOutputPoint, self.y_cur)
                outputStepCounter += 1
                nextOutputPoint = min(self.t0 + outputStepCounter * dOutput, Tend)

        self.finalize()