class Model(object): """ Model for static parameter estimation """ def __init__(self, fmu_path, opts=None): self.logger = logging.getLogger(type(self).__name__) self.model = FmiModel(fmu_path, opts=opts) # Log level try: self.model.model.set_log_level(FMI_WARNING) except AttributeError as e: self.logger.error(e.message) self.logger.error('Proceeding with standard log level...') # Simulation count self.sim_count = 0 def set_input(self, df, exclude=list()): """ Sets inputs. :param df: Dataframe, time given in seconds :param exclude: list of strings, names of columns to be excluded :return: None """ self.model.inputs_from_df(df, exclude) def set_param(self, df): """ Sets parameters. It is possible to set only a subset of model parameters. :param df: Dataframe with header and a single row of data :return: None """ self.model.parameters_from_df(df) def set_outputs(self, outputs): """ Sets output variables. :param outputs: list of strings :return: None """ self.model.specify_outputs(outputs) def simulate(self, com_points=None): # TODO: com_points should be adjusted to the number of samples self.sim_count += 1 self.info('Simulation count = ' + str(self.sim_count)) return self.model.simulate(com_points=com_points) def info(self, txt): class_name = self.__class__.__name__ if VERBOSE: if isinstance(txt, str): print('[' + class_name + '] ' + txt) else: print('[' + class_name + '] ' + repr(txt))
def test_simulation(self): output_names = self.ideal.columns.tolist() model = Model(self.fmu_path) model.inputs_from_df(self.inp) model.specify_outputs(output_names) model.parameters_from_df(self.known_df) res1 = model.simulate(reset=True) res2 = model.simulate(reset=False) self.assertTrue(res1.equals(res2), "Dataframes not equal") input_size = self.inp.index.size result_size = res1.index.size self.assertTrue(input_size == result_size, "Result size different than input")
def _get_model_instance(self, fmu_path, inputs, known_pars, est, output_names): self.logger.debug("Getting model instance...") self.logger.debug(f"inputs = {inputs}") self.logger.debug(f"known_pars = {known_pars}") self.logger.debug(f"est = {est}") self.logger.debug(f"estpars_2_df(est) = {estpars_2_df(est)}") self.logger.debug(f"output_names = {output_names}") model = Model(fmu_path) model.inputs_from_df(inputs) model.parameters_from_df(known_pars) model.parameters_from_df(estpars_2_df(est)) model.specify_outputs(output_names) self.logger.debug(f"Model instance initialized: {model}") self.logger.debug(f"Model instance initialized: {model.model}") res = model.simulate() self.logger.debug(f"test result: {res}") return model
t = np.arange(0, 86401, 300) inp['time'] = t inp['u'] = np.full(t.shape, 10.) inp = inp.set_index('time') # inp.to_csv(os.path.join('examples', 'sin', 'resources', 'input.csv')) # True parameters a = 3. b = 1.5 par = pd.DataFrame(index=[0]) par['a'] = a par['b'] = b # par.to_csv(os.path.join('examples', 'sin', 'resources', # 'true_parameters.csv'), index=False) model.inputs_from_df(inp) model.parameters_from_df(par) model.specify_outputs(['y']) ideal = model.simulate(com_points=inp.index.size - 1) # ideal.to_csv(os.path.join('examples', 'sin', 'resources', 'ideal.csv')) # Estimation ============================================== # Working directory workdir = os.path.join('examples', 'sin', 'workdir') if not os.path.exists(workdir): os.mkdir(workdir) assert os.path.exists(workdir), "Work directory does not exist" # Estimated and known parameters known = {}