modelon-community / PyFMI

PyFMI is a package for loading and interacting with Functional Mock-Up Units (FMUs) both for Model Exchange and Co-Simulation, which are compiled dynamic models compliant with the Functional Mock-Up Interface (FMI)
http://jmodelica.org/pyfmi
GNU Lesser General Public License v3.0
158 stars 38 forks source link

Error in initializing FMU during reset() method #172

Closed jiayilong closed 1 year ago

jiayilong commented 1 year ago

Hi everyone,

I'm encountering an error when trying to initialize an FMU in the reset() method of a custom PyFMIEnvironment class. The error message I'm seeing is:

"[ERROR][Slave] [error][FMU status:Error] fmiInitializeSlave: Could not rename the temporary input file. Initialization of Slave failed."

This error occurs every time I try to run the reset() method, but the simulation runs fine if I skip the reset() method and go straight to the step() method.

Here's a snippet of my code:

# Import libraries
from pyfmi import load_fmu
import numpy as np
import matplotlib.pyplot as plt

class PyFMIEnvironment():
    def __init__(self, fmu_path, start_time, end_time, time_step, num_steps):
    # Define model name and simulation parameters
        self.model = load_fmu(fmu_path)
        self.observation_space = self.model.get_model_variables(causality=1)
        self.action_space = self.model.get_model_variables(causality=0)
        self.start_time = start_time
        self.end_time = end_time
        self.time_step = time_step
        self.num_steps = num_steps
        self.opts = self.model.simulate_options()
        self.opts['ncp'] = num_steps
        self.opts['initialize'] = False
        self.model.initialize(self.start_time, self.end_time)
        print([self.model.get(var) for var in self.observation_space])

    def reset(self):
        self.model.reset()
        self.opts['ncp'] = self.num_steps
        self.opts['initialize'] = False
        self.model.initialize(self.start_time, self.end_time)
        print([self.model.get(var) for var in self.observation_space])

    def step(self, action):
        assert self.model.time is not None, "Model not initialized"
        for var, value in zip(self.action_space.keys(), action):
            self.model.set(var, value)
        self.model.do_step(self.model.time, self.time_step)
        next_state = [self.model.get(var) for var in self.observation_space]
        reward = self.get_reward(next_state)
        done = self.model.time >= self.end_time
        return next_state, reward, done, {}

    def get_reward(self, state):
        # define the reward function based on the state of the model
        reward = 0
        return reward

days = 10
hours = 24
minutes = 60
seconds = 60
en_timestp = 6 # EnergyPlus time steps per hour
num_steps = days * hours * en_timestp # Number of simulation steps

start_time = 0
end_time = days * hours * minutes * seconds # Simulation end time in seconds
time_step = end_time / num_steps # Simulation time step size in seconds
fmu_path = "./FMU/ASHRAE901_OfficeSmall_STD2019_Tucson.fmu"

env = PyFMIEnvironment(fmu_path = fmu_path, start_time = start_time, end_time = end_time, time_step = time_step, num_steps = num_steps)
env.reset()

I'm running this code on Windows 10 with Python 3.8. I've tried restarting my computer and checking file permissions, but I'm still getting the same error.

If anyone has any ideas on what could be causing this error. Thank you very much for help.

modelonrobinandersson commented 1 year ago

Hi @jiayilong this error seems to originate from the FMU itself and not from PyFMI. In order to understand this better, can you try the following and see if it triggers the error?

from pyfmi import load_fmu
fmu_path = "./FMU/ASHRAE901_OfficeSmall_STD2019_Tucson.fmu"
fmu = load_fmu(fmu_path)
fmu.reset()
modelonrobinandersson commented 1 year ago

Closing this issue due to inactivity. Feel free to reopen once relevant again.