Search code examples
pythonfmi

Unable to find out which FMU has error in fmpy


I simulated a batch of 5 FMUs by putting a function containing the fmpy simulate in a loop. 4 of them simulate correctly. The fifth one due to a wrong parameter shows an error.

My question is, is there any way to find out in which FMU the error comes during simulation

Ideally the output should look like: FMU1: OK FMU2: OK FMI3: 'Error' and so on

def wf(i):
    result = simulate_fmu(
        fmupath[i],
        validate=False,
        start_time=0,
        stop_time=endtime,
        solver='CVode',
        output_interval=stepsize,
        record_events=False,
        start_values = parameters[i],
        output = resultvariables,
    )
    dfindres = pd.DataFrame.from_dict(result)
    return dfindres

results = [wf(i) for i in range(5)]

Solution

  • You can catch the exception and return the status message together with the result like so

    def wf(i):
        try:
            result = simulate_fmu(
                fmupath[i],
                validate=False,
                start_time=0,
                stop_time=endtime,
                solver='CVode',
                output_interval=stepsize,
                record_events=False,
                start_values=parameters[i],
                output=resultvariables,
            )
        except Exception as e:
            return None, "FMU%d: Error" % i
    
        dfindres = pd.DataFrame.from_dict(result)
        return dfindres, "FMU%d: OK" % i
    
    # list of tuples (result, status)
    results = [wf(i) for i in range(5)]