def solve(self): """ Runs the simulation. """ h = (self.final_time - self.start_time) / self.ncp grid = N.linspace(self.start_time, self.final_time, self.ncp + 1)[:-1] status = 0 #For result writing result_write = ResultWriterDymola(self.model) result_write.write_header() for t in grid: status = self.model.do_step(t, h) if status != 0: result_write.write_finalize() raise Exception( "The simulation failed. See the log for more information. Return flag %d" % status) result_write.write_point() result_write.write_finalize()
def write_data(simulator,write_scaled_result=False, result_file_name=''): """ Writes simulation data to a file. Takes as input a simulated model. """ #Determine the result file name if result_file_name == '': result_file_name=simulator.problem._model.get_name()+'_result.txt' model = simulator.problem._model t = N.array(simulator.problem._sol_time) r = N.array(simulator.problem._sol_real) data = N.c_[t,r] if len(simulator.problem._sol_int) > 0 and len(simulator.problem._sol_int[0]) > 0: i = N.array(simulator.problem._sol_int) data = N.c_[data,i] if len(simulator.problem._sol_bool) > 0 and len(simulator.problem._sol_bool[0]) > 0: #b = N.array(simulator.problem._sol_bool).reshape( # -1,len(model._save_bool_variables_val)) b = N.array(simulator.problem._sol_bool) data = N.c_[data,b] export = ResultWriterDymola(model) export.write_header(file_name=result_file_name) map(export.write_point,(row for row in data)) export.write_finalize()
def __init__(self, model, input=None, result_file_name='', with_jacobian=False, start_time=0.0, logging=False): """ Initialize the problem. """ self._model = model self.input = input self.input_names = [] #Set start time to the model self._model.time = start_time self.t0 = start_time self.y0 = self._model.continuous_states self.problem_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' #Default values self.export = ResultWriterDymola_deprecated(model) if (isinstance(model,fmi_deprecated.FMUModel) or isinstance(model,fmi_deprecated.FMUModel2)) else ResultWriterDymola(model) #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 if with_jacobian: self.jac = self.j #Activates the jacobian