ThummeTo / FMIImport.jl

FMIImport.jl implements the import functionalities of the FMI-standard (fmi-standard.org) for the Julia programming language. FMIImport.jl provides the foundation for the Julia packages FMI.jl and FMIFlux.jl.
MIT License
18 stars 17 forks source link

Bug: prepareValue for non-standard vectors #92

Closed CasBex closed 1 year ago

CasBex commented 1 year ago

Hi, I'm using FMIImport in part of my simulations, and ran into some issues here. I'm running my own simulation loop (so not the standard fmiSimulate function) but I copied parts of that loop in FMI.jl. To set the inputs when calling the fmu I use something akin to the following:

sim!(fmu::FMU2, c::FMU2Component, u::AbstractArray, t::Real, dt::Real)
    md = fmu.modelDescription
    fmi2SetReal(c, md.inputValueReferences, u)
    fmi2DoStep(c, dt; currentCommunicationPoint=t)
    return fmi2GetReal(fmu, md.outputValueReferences)
end

The issue is that fmi2SetReal only works with non-abstract vectors for the values argument. In my case, values is a view of an array, then the call fmi2SetReal(c::FMUComponent, vr::Vector{UInt32}, values::SubArray{Float64, 1, ...}) throws the following error

AssertionError: fmi2SetReal(...): `vr` (4) and `values` (1) need to be the same length.

The culprit is the[prepareValue function in FMIImport.jl, which transforms values::SubArray to a vector with 1 element values::Vector{SubArray}. values fails the check isa(values, Array). When I override this implementation (see below) my sim! function runs perfectly. Note that Array has been replaced by AbstractArray

function FMIImport.prepareValue(value)
    if isa(value, AbstractArray) && length(size(value)) == 1
        return value
    else
        return [value]
    end

    @assert false "prepareValue(...): Unknown dimension of structure `$dim`."
end 

A multiple dispatch solution may be a bit more elegant though.

FMIImport.prepareValue(value) = [value]
FMIImport.prepareValue(value::AbstractVector) = value
ThummeTo commented 1 year ago

Sure, this seems fine. I let the checks run! Thanks!

ThummeTo commented 1 year ago

Thanks, closed in v0.15.6