diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 87ed6662d4..8476ac50fe 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -362,11 +362,24 @@ function parseFMIVariableName(name::AbstractString) name = replace(name, "." => "__") der = 0 if startswith(name, "der(") - idx = findfirst(',', name) + + # account for multi-dimensional array variable derivatives, e.g. der(x[1,2], 2) + array_variable_pattern = r"\[\d+?,\d+?\]" + patternmatches = match(array_variable_pattern, name) + if (patternmatches !== nothing) + safe_array_index_str = replace(String(patternmatches.match), "," => "_") + safe_name = replace(name, array_variable_pattern => safe_array_index_str) + else + safe_name = name + end + + + idx = findfirst(',', safe_name) if idx === nothing name = @view name[5:(end - 1)] der = 1 else + der = parse(Int, @view name[(idx + 1):(end - 1)]) name = @view name[5:(idx - 1)] end diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index de5b8a1dac..c2b8d980ee 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -274,6 +274,12 @@ end sol.t; idxs = [sys.adder1.c, sys.adder2.c]).u rtol=1e-3 end + @testset "multiDimArray Support" begin + path_to_FMU = joinpath(FMU_DIR, "SimpleArrayModel.fmu") + fmu = loadFMU(path_to_FMU) + @named model = MTK.FMIComponent(Val(2); fmu, type = :ME) + @test model !== nothing + end function build_looped_sspace(sspace1, sspace2) @variables x(t) = 1 @mtkcompile sys = System([D(x) ~ x, sspace1.u ~ sspace2.x, sspace2.u ~ sspace1.y], diff --git a/test/fmi/fmus/SimpleArrayModel.fmu b/test/fmi/fmus/SimpleArrayModel.fmu new file mode 100644 index 0000000000..cfc0ad56c6 Binary files /dev/null and b/test/fmi/fmus/SimpleArrayModel.fmu differ diff --git a/test/fmi/fmus/SimpleArrayModel/README.MD b/test/fmi/fmus/SimpleArrayModel/README.MD new file mode 100644 index 0000000000..568f8a91ee --- /dev/null +++ b/test/fmi/fmus/SimpleArrayModel/README.MD @@ -0,0 +1,5 @@ +SimpleArrayModel.fmu is a v2 ME FMU that contains multidimensional array variables with derivatives. It is included to test SciML issue number 3934. It is generated from the Modelica model in test_mtk_2darray_bug.mo using Dymola 2022x. The translation command is + +```julia +translateModelFMU("SimpleArrayModel", false, "", "2", "me", true, 0, fill("",0)); +``` \ No newline at end of file diff --git a/test/fmi/fmus/SimpleArrayModel/test_mtk_2darray_bug.mo b/test/fmi/fmus/SimpleArrayModel/test_mtk_2darray_bug.mo new file mode 100644 index 0000000000..f53787c777 --- /dev/null +++ b/test/fmi/fmus/SimpleArrayModel/test_mtk_2darray_bug.mo @@ -0,0 +1,25 @@ +model SimpleArrayModel + "Simple model with a 2D array variable and its derivative" + + parameter Integer n = 3 "First dimension size"; + parameter Integer m = 2 "Second dimension size"; + + Real x[n,m] "Two-dimensional array variable"; + Real dx[n,m] "Derivative of the array variable"; + +equation + // Define the derivative relationship + der(x) = dx; + + // Simple example dynamics for the derivative + for i in 1:n loop + for j in 1:m loop + dx[i,j] = -x[i,j] + sin(time); + end for; + end for; + +initial equation + // Initial conditions + x = zeros(n,m); + +end SimpleArrayModel; \ No newline at end of file