Skip to content

Commit

Permalink
Performance (#47)
Browse files Browse the repository at this point in the history
* Prevent allocation at fmi2CompletedIntegratorStep

* Set for fast lookup
  • Loading branch information
CasBex authored Sep 4, 2023
1 parent 9b9e9a5 commit 8f0d5db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/FMI2/struct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ mutable struct FMU2Component{F}
E::Union{FMUJacobian, Nothing}
F::Union{FMUJacobian, Nothing}

# performance
stepEnterEventMode::Array{fmi2Boolean}
terminateSimulation::Array{fmi2Boolean}
ptr_stepEnterEventMode::Ptr{fmi2Boolean}
ptr_terminateSimulation::Ptr{fmi2Boolean}

# deprecated
realValues::Dict
senseFunc::Symbol
Expand Down Expand Up @@ -323,6 +329,10 @@ mutable struct FMU2Component{F}
inst.jac_ẋy_x = zeros(fmi2Real, 0, 0)
inst.jac_ẋy_u = zeros(fmi2Real, 0, 0)

inst.stepEnterEventMode = zeros(fmi2Boolean, 1)
inst.terminateSimulation= zeros(fmi2Boolean, 1)
inst.ptr_stepEnterEventMode = pointer(inst.stepEnterEventMode)
inst.ptr_terminateSimulation = pointer(inst.terminateSimulation)
return inst
end

Expand Down Expand Up @@ -597,4 +607,4 @@ Overload the Base.show() function for custom printing of the FMU2.
Base.show(io::IO, fmu::FMU2) = print(io,
"Model name: $(fmu.modelDescription.modelName)
Type: $(fmu.type)"
)
)
4 changes: 3 additions & 1 deletion src/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mutable struct FMUJacobian{R, V}
valid::Bool

∂f_refs::AbstractArray{<:V}
∂f_refsset::Set{<:V}
∂x_refs::AbstractArray{<:V}

updateFct! # ToDo: Add type ::U
Expand All @@ -26,6 +27,7 @@ mutable struct FMUJacobian{R, V}
inst.c = zeros(R, length(∂x_refs))
inst.valid = false
inst.∂f_refs = ∂f_refs
inst.∂f_refsset = Set(∂f_refs)
inst.∂x_refs = ∂x_refs
inst.updateFct! = updateFct!

Expand Down Expand Up @@ -80,4 +82,4 @@ function vjp!(jac::FMUJacobian, x; ∂f_refs=jac.∂f_refs, ∂x_refs=jac.∂x_r
jac.c[:] = jac.mtx' * x
jac.numVJPs += 1
return jac.c
end
end

0 comments on commit 8f0d5db

Please sign in to comment.