def instantiate_fmu(component, ssp_unzipdir, start_time, parameters={}): fmu_filename = os.path.join(ssp_unzipdir, component.source) component.unzipdir = extract(fmu_filename) # read the model description model_description = read_model_description(fmu_filename, validate=False) # collect the value references component.variables = {} for variable in model_description.modelVariables: # component.vrs[variable.name] = variable.valueReference component.variables[variable.name] = variable fmu_kwargs = { 'guid': model_description.guid, 'unzipDirectory': component.unzipdir, 'modelIdentifier': model_description.coSimulation.modelIdentifier, 'instanceName': component.name } if model_description.fmiVersion == '1.0': component.fmu = FMU1Slave(**fmu_kwargs) component.fmu.instantiate() set_parameters(component, parameters) component.fmu.initialize() else: component.fmu = FMU2Slave(**fmu_kwargs) component.fmu.instantiate() component.fmu.setupExperiment(startTime=start_time) set_parameters(component, parameters) component.fmu.enterInitializationMode() component.fmu.exitInitializationMode()
def initialize(self): """Initializes simulation object. Instantiates FMPy FMUSalve1 or FMUSlave2 object based on FMI version detected. """ init_time = str(time.time())[0:10] random_id = str(uuid.uuid4().fields[-1])[:7] fmu_path = os.path.join(self.runs_path, init_time + "_" + random_id) os.mkdir(fmu_path) self.unzipdir = extract(self.fmu_file, unzipdir=fmu_path) weather_folder = Path(self.unzipdir) / "resources" possible_weather_files = list(weather_folder.rglob("*.mos")) + list( weather_folder.rglob("*.epw") ) weather_default_file_path = weather_folder / possible_weather_files[0] try: os.remove(weather_default_file_path) shutil.copy(self.weather_file_path, weather_default_file_path) except BaseException as e: logging.error(e) logging.error("Problem with the weather file handling") # initialize instance_name = "instance" + init_time # model identifier if self.fmi_type == "modex": model_id = self.model_description.modelExchange.modelIdentifier else: model_id = self.model_description.coSimulation.modelIdentifier kwargs = dict( guid=self.model_description.guid, unzipDirectory=self.unzipdir, modelIdentifier=model_id, instanceName=instance_name, ) if self.fmi_version == "1.0": if self.fmi_type == "cosim": self.fmu = FMU1Slave(**kwargs) else: self.fmu = FMU1Model(**kwargs) elif self.fmi_version == "2.0": if self.fmi_type == "cosim": self.fmu = FMU2Slave(**kwargs) else: self.fmu = FMU2Model(**kwargs) self.fmu.instantiate(loggingOn=True) if self.fmi_version == "2.0": self.fmu.setupExperiment(startTime=self.start_time, stopTime=self.stop_time) # Initialize time and the last_output values self.time = self.start_time
def instantiate_fmu(component, ssp_unzipdir, start_time, stop_time=None, parameter_set=None): """ Instantiate an FMU """ fmu_filename = os.path.join(ssp_unzipdir, component.source) component.unzipdir = extract(fmu_filename) # read the model description model_description = read_model_description(fmu_filename, validate=False) if model_description.coSimulation is None: raise Exception("%s does not support co-simulation." % component.source) # collect the value references component.variables = {} for variable in model_description.modelVariables: # component.vrs[variable.name] = variable.valueReference component.variables[variable.name] = variable fmu_kwargs = { 'guid': model_description.guid, 'unzipDirectory': component.unzipdir, 'modelIdentifier': model_description.coSimulation.modelIdentifier, 'instanceName': component.name } if model_description.fmiVersion == '1.0': component.fmu = FMU1Slave(**fmu_kwargs) component.fmu.instantiate() if parameter_set is not None: set_parameters(component, parameter_set) component.fmu.initialize(stopTime=stop_time) else: component.fmu = FMU2Slave(**fmu_kwargs) component.fmu.instantiate() component.fmu.setupExperiment(startTime=start_time) if parameter_set is not None: set_parameters(component, parameter_set) component.fmu.enterInitializationMode() component.fmu.exitInitializationMode()
def test_common_functions(self): if platform.startswith('win'): fmi_versions = ['1.0', '2.0'] else: return for fmi_version in fmi_versions: model_name = 'BooleanNetwork1' filename = model_name + '.fmu' download_test_file(fmi_version, 'CoSimulation', 'Dymola', '2017', model_name, filename) model_description = read_model_description(filename) unzipdir = extract(filename) guid = model_description.guid variables = {} for v in model_description.modelVariables: variables[v.name] = v args = { 'guid': guid, 'modelIdentifier': model_description.coSimulation.modelIdentifier, 'unzipDirectory': unzipdir, 'instanceName': None } if fmi_version == '1.0': fmu = FMU1Slave(**args) fmu.instantiate("instance1") fmu.initialize() else: fmu = FMU2Slave(**args) fmu.instantiate(loggingOn=False) fmu.setupExperiment(tolerance=None) fmu.enterInitializationMode() fmu.exitInitializationMode() # get types platform types_platform = fmu.getTypesPlatform() if fmi_version == '1.0': self.assertEqual('standard32', types_platform) else: self.assertEqual('default', types_platform) # get FMI version version = fmu.getVersion() self.assertEqual(fmi_version, version) # set debug logging if fmi_version == '1.0': fmu.setDebugLogging(True) else: fmu.setDebugLogging(True, ['logAll']) # set and get Real vr = [variables['booleanPulse1.width'].valueReference] value = [30.0] fmu.setReal(vr, value) result = fmu.getReal(vr) self.assertTrue(result[0] == value[0]) # set and get Integer vr = [variables['integerConstant.k'].valueReference] value = [-4] fmu.setInteger(vr, value) result = fmu.getInteger(vr) self.assertTrue(result[0] == value[0]) # set and get Boolean vr = [variables['rSFlipFlop.Qini'].valueReference] value = [True] fmu.setBoolean(vr, value) result = fmu.getBoolean(vr) self.assertTrue(result[0] == value[0]) # TODO: set and get String # clean up fmu.terminate() fmu.freeInstance() shutil.rmtree(unzipdir)