Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ext/MTKFMIExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/fmi/fmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Binary file added test/fmi/fmus/SimpleArrayModel.fmu
Binary file not shown.
5 changes: 5 additions & 0 deletions test/fmi/fmus/SimpleArrayModel/README.MD
Original file line number Diff line number Diff line change
@@ -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));
```
25 changes: 25 additions & 0 deletions test/fmi/fmus/SimpleArrayModel/test_mtk_2darray_bug.mo
Original file line number Diff line number Diff line change
@@ -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;
Loading