def _adapt_input(self, input): if input != None: input_names = input[0] self.input_len_names = len(input_names) self.input_real_value_refs = [] self.input_real_mask = [] self.input_other = [] self.input_other_mask = [] if isinstance(input_names,str): input_names = [input_names] for i,name in enumerate(input_names): if self._model.get_variable_causality(name) != fmi.FMI2_INPUT: raise fmi.FMUException("Variable '%s' is not an input. Only variables specified to be inputs are allowed."%name) if self._model.get_variable_data_type(name) == fmi.FMI2_REAL: self.input_real_value_refs.append(self._model.get_variable_valueref(name)) self.input_real_mask.append(i) else: self.input_other.append(name) self.input_other_mask.append(i) self.input_real_mask = N.array(self.input_real_mask) self.input_other_mask = N.array(self.input_other_mask) self.input = input
def j(self, t, y, sw=None): """ The jacobian function for an ODE problem. """ if self._extra_f_nbr > 0: y_extra = y[-self._extra_f_nbr:] y = y[:-self._extra_f_nbr] #Moving data to the model self._model.time = t #Check if there are any states if self._f_nbr != 0: self._model.continuous_states = y #Sets the inputs, if any self._set_input_values(t) #Evaluating the jacobian #If there are no states return a dummy jacobian. if self._f_nbr == 0: return N.array([[0.0]]) A = self._model._get_A(add_diag=True, output_matrix=self._A) if self._A is None: self._A = A if self._extra_f_nbr > 0: if hasattr(self._extra_equations, "jac"): if self._sparse_representation: Jac = A.tocoo() #Convert to COOrdinate A2 = self._extra_equations.jac(y_extra).tocoo() data = N.append(Jac.data, A2.data) row = N.append(Jac.row, A2.row+self._f_nbr) col = N.append(Jac.col, A2.col+self._f_nbr) #Convert to compresssed sparse column Jac = sp.coo_matrix((data, (row, col))) Jac = Jac.tocsc() else: Jac = N.zeros((self._f_nbr+self._extra_f_nbr,self._f_nbr+self._extra_f_nbr)) Jac[:self._f_nbr,:self._f_nbr] = A if isinstance(A, N.ndarray) else A.toarray() Jac[self._f_nbr:,self._f_nbr:] = self._extra_equations.jac(y_extra) else: raise fmi.FMUException("No Jacobian provided for the extra equations") else: Jac = A return Jac
def __init__(self, model, input=None, result_file_name='', with_jacobian=False, start_time=0.0, logging=False, result_handler=None): """ Initialize the problem. """ self._model = model self._adapt_input(input) self.timings = {"handle_result": 0.0} #Set start time to the model self._model.time = start_time self.t0 = start_time self.y0 = self._model.continuous_states self.name = self._model.get_name() [f_nbr, g_nbr] = self._model.get_ode_sizes() self._f_nbr = f_nbr self._g_nbr = g_nbr if g_nbr > 0: self.state_events = self.g self.time_events = self.t #If there is no state in the model, add a dummy #state der(y)=0 if f_nbr == 0: self.y0 = N.array([0.0]) #Determine the result file name if result_file_name == '': self.result_file_name = model.get_name()+'_result.txt' else: self.result_file_name = result_file_name self.debug_file_name = model.get_name().replace(".","_")+'_debug.txt' self.debug_file_object = None #Default values self.export = result_handler #Internal values self._sol_time = [] self._sol_real = [] self._sol_int = [] self._sol_bool = [] self._logg_step_event = [] self._write_header = True self._logging = logging #Stores the first time point #[r,i,b] = self._model.save_time_point() #self._sol_time += [self._model.t] #self._sol_real += [r] #self._sol_int += [i] #self._sol_bool += b self._jm_fmu = self._model.get_generation_tool() == "JModelica.org" if with_jacobian: raise fmi.FMUException("Jacobians are not supported using FMI 1.0, please use FMI 2.0")
def j(self, t, y, sw=None): """ The jacobian function for an ODE problem. """ if self._extra_f_nbr > 0: y_extra = y[-self._extra_f_nbr:] y = y[:-self._extra_f_nbr] #Moving data to the model self._model.time = t #Check if there are any states if self._f_nbr != 0: self._model.continuous_states = y #Sets the inputs, if any self._set_input_values(t) #Evaluating the jacobian #If there are no states return a dummy jacobian. if self._f_nbr == 0: return N.array([[0.0]]) #Mimic the epsilon computation from the respective solver """ if self._solver == "CVode": h = t - self._model._last_accepted_time w = 1.0/(self._model._relative_tolerance*abs(y)+self._model.nominal_continuous_states*self._model._relative_tolerance) fnorm = (sum((y*w)**2)/self._f_nbr)**0.5 inc = (1000 * abs(h) * self._RUROUND * self._f_nbr * fnorm) if (fnorm != 0.0 and h != 0.0) else 1.0 for i in range(self._f_nbr): w[i] = max(self._RUROUND*abs(y[i]), inc/w[i]) eps = w elif self._solver == "Radau5ODE": eps = (self._UROUND*N.maximum(1e-5,abs(y)))**0.5 else: eps = None """ eps = None A = self._model._get_A(add_diag=True, output_matrix=self._A, perturbation=eps) if self._A is None: self._A = A if self._extra_f_nbr > 0: if hasattr(self._extra_equations, "jac"): if self._sparse_representation: Jac = A.tocoo() #Convert to COOrdinate A2 = self._extra_equations.jac(y_extra).tocoo() data = N.append(Jac.data, A2.data) row = N.append(Jac.row, A2.row + self._f_nbr) col = N.append(Jac.col, A2.col + self._f_nbr) #Convert to compresssed sparse column Jac = sp.coo_matrix((data, (row, col))) Jac = Jac.tocsc() else: Jac = N.zeros((self._f_nbr + self._extra_f_nbr, self._f_nbr + self._extra_f_nbr)) Jac[:self._f_nbr, :self._f_nbr] = A if isinstance( A, N.ndarray) else A.toarray() Jac[self._f_nbr:, self._f_nbr:] = self._extra_equations.jac(y_extra) else: raise fmi.FMUException( "No Jacobian provided for the extra equations") else: Jac = A return Jac
def solve(self): """ Runs the simulation. """ result_handler = self.result_handler h = (self.final_time-self.start_time)/self.ncp grid = N.linspace(self.start_time,self.final_time,self.ncp+1)[:-1] status = 0 final_time = self.start_time #For result writing start_time_point = timer() result_handler.integration_point() self.timings["storing_result"] = timer() - start_time_point #Start of simulation, start the clock time_start = timer() for t in grid: status = self.model.do_step(t,h) self.status = status if status != 0: if status == fmi.FMI_ERROR: result_handler.simulation_end() raise fmi.FMUException("The simulation failed. See the log for more information. Return flag %d."%status) elif status == fmi.FMI_DISCARD and (isinstance(self.model, fmi.FMUModelCS1) or isinstance(self.model, fmi.FMUModelCS2)): try: if isinstance(self.model, fmi.FMUModelCS1): last_time = self.model.get_real_status(fmi.FMI1_LAST_SUCCESSFUL_TIME) else: last_time = self.model.get_real_status(fmi.FMI2_LAST_SUCCESSFUL_TIME) if last_time > t: #Solver succeeded in taken a step a little further than the last time self.model.time = last_time final_time = last_time start_time_point = timer() result_handler.integration_point() self.timings["storing_result"] += timer() - start_time_point except fmi.FMUException: pass break #result_handler.simulation_end() #raise Exception("The simulation failed. See the log for more information. Return flag %d"%status) final_time = t+h start_time_point = timer() result_handler.integration_point() self.timings["storing_result"] += timer() - start_time_point if self.options["time_limit"] and (timer() - time_start) > self.options["time_limit"]: raise fmi.TimeLimitExceeded("The time limit was exceeded at integration time %.8E."%final_time) if self.input_traj != None: self.model.set(self.input_traj[0], self.input_traj[1].eval(t+h)[0,:]) #End of simulation, stop the clock time_stop = timer() result_handler.simulation_end() if self.status != 0: print('Simulation terminated prematurely. See the log for possibly more information. Return flag %d.'%status) #Log elapsed time print('Simulation interval : ' + str(self.start_time) + ' - ' + str(final_time) + ' seconds.') print('Elapsed simulation time: ' + str(time_stop-time_start) + ' seconds.') self.timings["computing_solution"] = time_stop - time_start - self.timings["storing_result"]
def __init__(self, start_time, final_time, input, model, options): """ Simulation algortihm for FMUs (Co-simulation). Parameters:: model -- fmi.FMUModelCS1 object representation of the model. options -- The options that should be used in the algorithm. For details on the options, see: * model.simulate_options('FMICSAlgOptions') or look at the docstring with help: * help(pyfmi.fmi_algorithm_drivers.FMICSAlgOptions) Valid values are: - A dict that overrides some or all of the default values provided by FMICSAlgOptions. An empty dict will thus give all options with default values. - FMICSAlgOptions object. """ self.model = model self.timings = {} self.time_start_total = timer() # set start time, final time and input trajectory self.start_time = start_time self.final_time = final_time self.input = input self.status = 0 # handle options argument if isinstance(options, dict) and not \ isinstance(options, FMICSAlgOptions): # user has passed dict with options or empty dict = default self.options = FMICSAlgOptions(options) elif isinstance(options, FMICSAlgOptions): # user has passed FMICSAlgOptions instance self.options = options else: raise InvalidAlgorithmOptionException(options) # set options self._set_options() input_traj = None if self.input: if hasattr(self.input[1],"__call__"): input_traj=(self.input[0], TrajectoryUserFunction(self.input[1])) else: input_traj=(self.input[0], TrajectoryLinearInterpolation(self.input[1][:,0], self.input[1][:,1:])) #Sets the inputs, if any self.model.set(input_traj[0], input_traj[1].eval(self.start_time)[0,:]) self.input_traj = input_traj #time_start = timer() if self.options["result_handling"] == "file": self.result_handler = ResultHandlerFile(self.model) elif self.options["result_handling"] == "binary": self.result_handler = ResultHandlerBinaryFile(self.model) elif self.options["result_handling"] == "memory": self.result_handler = ResultHandlerMemory(self.model) elif self.options["result_handling"] == "csv": self.result_handler = ResultHandlerCSV(self.model, delimiter=",") elif self.options["result_handling"] == "custom": self.result_handler = self.options["result_handler"] if self.result_handler is None: raise fmi.FMUException("The result handler needs to be specified when using a custom result handling.") if not isinstance(self.result_handler, ResultHandler): raise fmi.FMUException("The result handler needs to be a subclass of ResultHandler.") elif self.options["result_handling"] == "none": #No result handling (for performance) self.result_handler = ResultHandlerDummy(self.model) else: raise fmi.FMUException("Unknown option to result_handling.") self.result_handler.set_options(self.options) time_end = timer() #self.timings["creating_result_object"] = time_end - time_start time_start = time_end time_res_init = 0.0 # Initialize? if self.options['initialize']: if isinstance(self.model, fmi.FMUModelCS1) or isinstance(self.model, fmi_extended.FMUModelME1Extended): self.model.initialize(start_time, final_time, stop_time_defined=self.options["stop_time_defined"]) elif isinstance(self.model, fmi.FMUModelCS2): self.model.setup_experiment(start_time=start_time, stop_time_defined=self.options["stop_time_defined"], stop_time=final_time) self.model.initialize() else: raise fmi.FMUException("Unknown model.") time_res_init = timer() self.result_handler.initialize_complete() time_res_init = timer() - time_res_init elif self.model.time is None and isinstance(self.model, fmi.FMUModelCS2): raise fmi.FMUException("Setup Experiment has not been called, this has to be called prior to the initialization call.") elif self.model.time is None: raise fmi.FMUException("The model need to be initialized prior to calling the simulate method if the option 'initialize' is set to False") if abs(start_time - model.time) > 1e-14: logging.warning('The simulation start time (%f) and the current time in the model (%f) is different. Is the simulation start time correctly set?'%(start_time, model.time)) time_end = timer() self.timings["initializing_fmu"] = time_end - time_start - time_res_init time_start = time_end self.result_handler.simulation_start() self.timings["initializing_result"] = timer() - time_start - time_res_init
def __init__(self, start_time, final_time, input, model, options): """ Create a simulation algorithm using Assimulo. Parameters:: model -- fmi.FMUModel object representation of the model. options -- The options that should be used in the algorithm. For details on the options, see: * model.simulate_options('AssimuloFMIAlgOptions') or look at the docstring with help: * help(pyfmi.fmi_algorithm_drivers.AssimuloFMIAlgOptions) Valid values are: - A dict that overrides some or all of the default values provided by AssimuloFMIAlgOptions. An empty dict will thus give all options with default values. - AssimuloFMIAlgOptions object. """ self.model = model self.timings = {} self.time_start_total = timer() try: import assimulo except: raise fmi.FMUException( 'Could not find Assimulo package. Check pyfmi.check_packages()') # import Assimulo dependent classes from pyfmi.simulation.assimulo_interface import FMIODE, FMIODESENS, FMIODE2, FMIODESENS2 # set start time, final time and input trajectory self.start_time = start_time self.final_time = final_time self.input = input # handle options argument if isinstance(options, dict) and not \ isinstance(options, AssimuloFMIAlgOptions): # user has passed dict with options or empty dict = default self.options = AssimuloFMIAlgOptions(options) elif isinstance(options, AssimuloFMIAlgOptions): # user has passed AssimuloFMIAlgOptions instance self.options = options else: raise InvalidAlgorithmOptionException(options) # set options self._set_options() #time_start = timer() input_traj = None if self.input: if hasattr(self.input[1],"__call__"): input_traj=(self.input[0], TrajectoryUserFunction(self.input[1])) else: input_traj=(self.input[0], TrajectoryLinearInterpolation(self.input[1][:,0], self.input[1][:,1:])) #Sets the inputs, if any input_names = [input_traj[0]] if isinstance(input_traj[0],str) else input_traj[0] input_values = input_traj[1].eval(self.start_time)[0,:] if len(input_names) != len(input_values): raise fmi.FMUException("The number of input variables is not equal to the number of input values, please verify the input object.") self.model.set(input_names, input_values) if self.options["result_handling"] == "file": self.result_handler = ResultHandlerFile(self.model) elif self.options["result_handling"] == "binary": if self.options["sensitivities"]: logging.warning('The binary result file do not currently support storing of sensitivity results. Switching to textual result format.') self.result_handler = ResultHandlerFile(self.model) else: self.result_handler = ResultHandlerBinaryFile(self.model) elif self.options["result_handling"] == "memory": self.result_handler = ResultHandlerMemory(self.model) elif self.options["result_handling"] == "csv": self.result_handler = ResultHandlerCSV(self.model, delimiter=",") elif self.options["result_handling"] == "custom": self.result_handler = self.options["result_handler"] if self.result_handler is None: raise fmi.FMUException("The result handler needs to be specified when using a custom result handling.") if not isinstance(self.result_handler, ResultHandler): raise fmi.FMUException("The result handler needs to be a subclass of ResultHandler.") elif self.options["result_handling"] == "none": #No result handling (for performance) self.result_handler = ResultHandlerDummy(self.model) else: raise fmi.FMUException("Unknown option to result_handling.") self.result_handler.set_options(self.options) time_end = timer() #self.timings["creating_result_object"] = time_end - time_start time_start = time_end time_res_init = 0.0 # Initialize? if self.options['initialize']: try: rtol = self.solver_options['rtol'] except KeyError: rtol, atol = self.model.get_tolerances() if isinstance(self.model, fmi.FMUModelME1): self.model.time = start_time #Set start time before initialization self.model.initialize(tolerance=rtol) elif isinstance(self.model, fmi.FMUModelME2) or isinstance(self.model, fmi_coupled.CoupledFMUModelME2): self.model.setup_experiment(tolerance=rtol, start_time=self.start_time, stop_time=self.final_time) self.model.initialize() self.model.event_update() self.model.enter_continuous_time_mode() else: raise fmi.FMUException("Unknown model.") time_res_init = timer() self.result_handler.initialize_complete() time_res_init = timer() - time_res_init elif self.model.time is None and isinstance(self.model, fmi.FMUModelME2): raise fmi.FMUException("Setup Experiment has not been called, this has to be called prior to the initialization call.") elif self.model.time is None: raise fmi.FMUException("The model need to be initialized prior to calling the simulate method if the option 'initialize' is set to False") #See if there is an time event at start time if isinstance(self.model, fmi.FMUModelME1): event_info = self.model.get_event_info() if event_info.upcomingTimeEvent and event_info.nextEventTime == model.time: self.model.event_update() if abs(start_time - model.time) > 1e-14: logging.warning('The simulation start time (%f) and the current time in the model (%f) is different. Is the simulation start time correctly set?'%(start_time, model.time)) time_end = timer() self.timings["initializing_fmu"] = time_end - time_start - time_res_init time_start = time_end self.result_handler.simulation_start() self.timings["initializing_result"] = timer() - time_start + time_res_init # Sensitivities? if self.options["sensitivities"]: if self.model.get_generation_tool() != "JModelica.org" and \ self.model.get_generation_tool() != "Optimica Compiler Toolkit": if isinstance(self.model, fmi.FMUModelME2): for var in self.options["sensitivities"]: causality = self.model.get_variable_causality(var) if causality != fmi.FMI2_INPUT: raise fmi.FMUException("The sensitivity parameter is not specified as an input which is required.") else: raise fmi.FMUException("Sensitivity calculations only possible with JModelica.org generated FMUs") if self.options["solver"] != "CVode": raise fmi.FMUException("Sensitivity simulations currently only supported using the solver CVode.") #Checks to see if all the sensitivities are inside the model #else there will be an exception self.model.get(self.options["sensitivities"]) if not self.input and (isinstance(self.model, fmi.FMUModelME2) or isinstance(self.model, fmi_coupled.CoupledFMUModelME2)): if self.options["sensitivities"]: self.probl = FMIODESENS2(self.model, result_file_name=self.result_file_name, with_jacobian=self.with_jacobian, start_time=self.start_time, parameters=self.options["sensitivities"],logging=self.options["logging"], result_handler=self.result_handler) else: self.probl = FMIODE2(self.model, result_file_name=self.result_file_name, with_jacobian=self.with_jacobian, start_time=self.start_time,logging=self.options["logging"], result_handler=self.result_handler,extra_equations=self.options["extra_equations"]) elif isinstance(self.model, fmi.FMUModelME2) or isinstance(self.model, fmi_coupled.CoupledFMUModelME2): if self.options["sensitivities"]: self.probl = FMIODESENS2( self.model, input_traj, result_file_name=self.result_file_name, with_jacobian=self.with_jacobian, start_time=self.start_time,parameters=self.options["sensitivities"],logging=self.options["logging"], result_handler=self.result_handler) else: self.probl = FMIODE2( self.model, input_traj, result_file_name=self.result_file_name, with_jacobian=self.with_jacobian, start_time=self.start_time,logging=self.options["logging"], result_handler=self.result_handler, extra_equations=self.options["extra_equations"]) elif not self.input: if self.options["sensitivities"]: self.probl = FMIODESENS(self.model, result_file_name=self.result_file_name,with_jacobian=self.with_jacobian,start_time=self.start_time,parameters=self.options["sensitivities"],logging=self.options["logging"], result_handler=self.result_handler) else: self.probl = FMIODE(self.model, result_file_name=self.result_file_name,with_jacobian=self.with_jacobian,start_time=self.start_time,logging=self.options["logging"], result_handler=self.result_handler) else: if self.options["sensitivities"]: self.probl = FMIODESENS( self.model, input_traj, result_file_name=self.result_file_name,with_jacobian=self.with_jacobian,start_time=self.start_time,parameters=self.options["sensitivities"],logging=self.options["logging"], result_handler=self.result_handler) else: self.probl = FMIODE( self.model, input_traj, result_file_name=self.result_file_name,with_jacobian=self.with_jacobian,start_time=self.start_time,logging=self.options["logging"], result_handler=self.result_handler) # instantiate solver and set options self.simulator = self.solver(self.probl) self._set_solver_options()