Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation fixes and moved to FMI.jl #49

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 45 additions & 7 deletions src/FMI2/cconst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const fmi2Real = Creal # defined in FMICore.jl, dependent on the Julia arch
const fmi2Integer = Cint
const fmi2Byte = Char
const fmi2ValueReference = Cuint

"""
fmi2FMUstate is a pointer to a data structure in the FMU that saves the internal FMU state of the actual or
a previous time instant. This allows to restart a simulation from a previous FMU state.

Source: FMI2.0.3 Spec [p.17]; See also section 2.1.8
"""
const fmi2FMUstate = Ptr{Cvoid}
const fmi2Component = Ptr{Cvoid}
const fmi2ComponentEnvironment = Ptr{Cvoid}
Expand All @@ -30,19 +37,36 @@ export fmi2Char, fmi2String, fmi2Boolean, fmi2Real, fmi2Integer, fmi2Byte, fmi2V
Source: FMISpec2.0.2[p.18]: 2.1.3 Status Returned by Functions

Status returned by functions. The status has the following meaning:
fmi2OK – all well
fmi2Warning – things are not quite right, but the computation can continue. Function “logger” was called in the model (see below), and it is expected that this function has shown the prepared information message to the user.
fmi2Discard – this return status is only possible if explicitly defined for the corresponding function
- fmi2OK – all well
- fmi2Warning – things are not quite right, but the computation can continue. Function “logger” was called in the model (see below), and it is expected that this function has shown the prepared information message to the user.
- fmi2Discard – this return status is only possible if explicitly defined for the corresponding function
(ModelExchange: fmi2SetReal, fmi2SetInteger, fmi2SetBoolean, fmi2SetString, fmi2SetContinuousStates, fmi2GetReal, fmi2GetDerivatives, fmi2GetContinuousStates, fmi2GetEventIndicators;
CoSimulation: fmi2SetReal, fmi2SetInteger, fmi2SetBoolean, fmi2SetString, fmi2DoStep, fmiGetXXXStatus):
For “model exchange”: It is recommended to perform a smaller step size and evaluate the model equations again, for example because an iterative solver in the model did not converge or because a function is outside of its domain (for example sqrt(<negative number>)). If this is not possible, the simulation has to be terminated.
For “co-simulation”: fmi2Discard is returned also if the slave is not able to return the required status information. The master has to decide if the simulation run can be continued. In both cases, function “logger” was called in the FMU (see below) and it is expected that this function has shown the prepared information message to the user if the FMU was called in debug mode (loggingOn = fmi2True). Otherwise, “logger” should not show a message.
fmi2Error – the FMU encountered an error. The simulation cannot be continued with this FMU instance. If one of the functions returns fmi2Error, it can be tried to restart the simulation from a formerly stored FMU state by calling fmi2SetFMUstate.
- fmi2Error – the FMU encountered an error. The simulation cannot be continued with this FMU instance. If one of the functions returns fmi2Error, it can be tried to restart the simulation from a formerly stored FMU state by calling fmi2SetFMUstate.
This can be done if the capability flag canGetAndSetFMUstate is true and fmi2GetFMUstate was called before in non-erroneous state. If not, the simulation cannot be continued and fmi2FreeInstance or fmi2Reset must be called afterwards.4 Further processing is possible after this call; especially other FMU instances are not affected. Function “logger” was called in the FMU (see below), and it is expected that this function has shown the prepared information message to the user.
fmi2Fatal – the model computations are irreparably corrupted for all FMU instances. [For example, due to a run-time exception such as access violation or integer division by zero during the execution of an fmi function]. Function “logger” was called in the FMU (see below), and it is expected that this function has shown the prepared information message to the user. It is not possible to call any other function for any of the FMU instances.
fmi2Pending – this status is returned only from the co-simulation interface, if the slave executes the function in an asynchronous way. That means the slave starts to compute but returns immediately. The master has to call fmi2GetStatus(..., fmi2DoStepStatus) to determine if the slave has finished the computation. Can be returned only by fmi2DoStep and by fmi2GetStatus (see section 4.2.3).
- fmi2Fatal – the model computations are irreparably corrupted for all FMU instances. [For example, due to a run-time exception such as access violation or integer division by zero during the execution of an fmi function]. Function “logger” was called in the FMU (see below), and it is expected that this function has shown the prepared information message to the user. It is not possible to call any other function for any of the FMU instances.
- fmi2Pending – this status is returned only from the co-simulation interface, if the slave executes the function in an asynchronous way. That means the slave starts to compute but returns immediately. The master has to call fmi2GetStatus(..., fmi2DoStepStatus) to determine if the slave has finished the computation. Can be returned only by fmi2DoStep and by fmi2GetStatus (see section 4.2.3).
"""
const fmi2Status = Cuint
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the declaration seperated from the doc string now?
(doc string starts in L44, declaration is in L36)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because otherwise each status would need its own docstring. Even if that option is preferred, I recommend to implement it in the future and go with this solution for now


"""
Source: FMISpec2.0.2[p.18]: 2.1.3 Status Returned by Functions

Status returned by functions. The status has the following meaning:
- fmi2OK – all well
- fmi2Warning – things are not quite right, but the computation can continue. Function “logger” was called in the model (see below), and it is expected that this function has shown the prepared information message to the user.
- fmi2Discard – this return status is only possible if explicitly defined for the corresponding function
(ModelExchange: fmi2SetReal, fmi2SetInteger, fmi2SetBoolean, fmi2SetString, fmi2SetContinuousStates, fmi2GetReal, fmi2GetDerivatives, fmi2GetContinuousStates, fmi2GetEventIndicators;
CoSimulation: fmi2SetReal, fmi2SetInteger, fmi2SetBoolean, fmi2SetString, fmi2DoStep, fmiGetXXXStatus):
For “model exchange”: It is recommended to perform a smaller step size and evaluate the model equations again, for example because an iterative solver in the model did not converge or because a function is outside of its domain (for example sqrt(<negative number>)). If this is not possible, the simulation has to be terminated.
For “co-simulation”: fmi2Discard is returned also if the slave is not able to return the required status information. The master has to decide if the simulation run can be continued. In both cases, function “logger” was called in the FMU (see below) and it is expected that this function has shown the prepared information message to the user if the FMU was called in debug mode (loggingOn = fmi2True). Otherwise, “logger” should not show a message.
- fmi2Error – the FMU encountered an error. The simulation cannot be continued with this FMU instance. If one of the functions returns fmi2Error, it can be tried to restart the simulation from a formerly stored FMU state by calling fmi2SetFMUstate.
This can be done if the capability flag canGetAndSetFMUstate is true and fmi2GetFMUstate was called before in non-erroneous state. If not, the simulation cannot be continued and fmi2FreeInstance or fmi2Reset must be called afterwards.4 Further processing is possible after this call; especially other FMU instances are not affected. Function “logger” was called in the FMU (see below), and it is expected that this function has shown the prepared information message to the user.
- fmi2Fatal – the model computations are irreparably corrupted for all FMU instances. [For example, due to a run-time exception such as access violation or integer division by zero during the execution of an fmi function]. Function “logger” was called in the FMU (see below), and it is expected that this function has shown the prepared information message to the user. It is not possible to call any other function for any of the FMU instances.
- fmi2Pending – this status is returned only from the co-simulation interface, if the slave executes the function in an asynchronous way. That means the slave starts to compute but returns immediately. The master has to call fmi2GetStatus(..., fmi2DoStepStatus) to determine if the slave has finished the computation. Can be returned only by fmi2DoStep and by fmi2GetStatus (see section 4.2.3).
"""
const fmi2StatusOK = Cuint(0)
const fmi2StatusWarning = Cuint(1)
const fmi2StatusDiscard = Cuint(2)
Expand Down Expand Up @@ -121,6 +145,13 @@ To simplify porting, no C types are used in the function interfaces, but the ali
All definitions in this section are provided in the header file “fmi2TypesPlatform.h”.
"""
const fmi2True = fmi2Boolean(true)

0815Creeper marked this conversation as resolved.
Show resolved Hide resolved
"""
Source: FMISpec2.0.2[p.16]: 2.1.2 Platform Dependent Definitions

To simplify porting, no C types are used in the function interfaces, but the alias types are defined in this section.
All definitions in this section are provided in the header file “fmi2TypesPlatform.h”.
"""
const fmi2False = fmi2Boolean(false)
export fmi2True, fmi2False

Expand Down Expand Up @@ -150,7 +181,14 @@ export fmi2StatusKind, fmi2StatusKindDoStepStatus, fmi2StatusKindPendingStatus,

# Custom helper, not part of the FMI-Spec.
"""
ToDo.
Types of dependency:

- `fmi2DependencyKindDependent`: no particular structure, f(v)
- `fmi2DependencyKindConstant`: constant factor, c*v (for Real valued variables only)
- `fmi2DependencyKindFixed`: tunable factor, p*v (for Real valued variables only)
- `fmi2DependencyKindDependent`: discrete factor, d*v (for Real valued variables only)

Source: FMI2.0.3 Spec for fmi2VariableDependency [p.60]
"""
const fmi2DependencyKind = Cuint
const fmi2DependencyKindDependent = Cuint(0)
Expand Down
16 changes: 8 additions & 8 deletions src/FMI2/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
export fmi2StatusToString

"""
ToDo.
converts a [`fmi2Causality`](@ref) to the corresponding String ("parameter", "calculatedParameter", "input", "output", "local", "independent")
0815Creeper marked this conversation as resolved.
Show resolved Hide resolved
"""
function fmi2CausalityToString(c::fmi2Causality)
if c == fmi2CausalityParameter
Expand All @@ -48,7 +48,7 @@ end
export fmi2CausalityToString

"""
ToDo.
converts a String ("parameter", "calculatedParameter", "input", "output", "local", "independent") to the corresponding [`fmi2Causality`](@ref)
"""
function fmi2StringToCausality(s::AbstractString)
if s == "parameter"
Expand All @@ -70,7 +70,7 @@ end
export fmi2StringToCausality

"""
ToDo.
converts a String ("constant", "fixed", "tunable", "discrete", "continuous") to the corresponding [`fmi2Variability`](@ref)
"""
function fmi2VariabilityToString(c::fmi2Variability)
if c == fmi2VariabilityConstant
Expand All @@ -90,7 +90,7 @@ end
export fmi2VariabilityToString

"""
ToDo.
converts a [`fmi2Variability`](@ref) to the corresponding String ("constant", "fixed", "tunable", "discrete", "continuous")
"""
function fmi2StringToVariability(s::AbstractString)
if s == "constant"
Expand All @@ -110,7 +110,7 @@ end
export fmi2StringToVariability

"""
ToDo.
converts a [`fmi2Initial`](@ref) to the corresponding String ("approx", "exact", "calculated")
"""
function fmi2InitialToString(c::fmi2Initial)
if c == fmi2InitialApprox
Expand All @@ -126,7 +126,7 @@ end
export fmi2InitialToString

"""
ToDo.
converts a String ("approx", "exact", "calculated") to the corresponding [`fmi2Initial`](@ref)
"""
function fmi2StringToInitial(s::AbstractString)
if s == "approx"
Expand All @@ -142,7 +142,7 @@ end
export fmi2StringToInitial

"""
ToDo.
converts a [`fmi2DependencyKind`](@ref) to the corresponding String ("dependent", "constant", "fixed", "tunable", "discrete")
"""
function fmi2DependencyKindToString(c::fmi2DependencyKind)
if c == fmi2DependencyKindDependent
Expand All @@ -162,7 +162,7 @@ end
export fmi2DependencyKindToString

"""
ToDo.
converts a String ("dependent", "constant", "fixed", "tunable", "discrete") to the corresponding [`fmi2DependencyKind`](@ref)
"""
function fmi2StringToDependencyKind(s::AbstractString)
if s == "dependent"
Expand Down
4 changes: 3 additions & 1 deletion src/FMI2/ctype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ end
export fmi2Annotation

"""
ToDo
Mutable Struct representing existance and kind of dependencies of an Unknown on Known Variables in Continuous-Time and Event Mode (ME) and at Communication Points (CS)

See also FMI2.0.3 Spec fmi2VariableDependency [p.60]
"""
mutable struct fmi2VariableDependency
# mandatory
Expand Down
9 changes: 7 additions & 2 deletions src/FMI2/struct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Overload the Base.show() function for custom printing of the FMU2.
Base.show(io::IO, e::FMU2Event) = print(io, e.indicator == 0 ? "Time-Event @ $(round(e.t; digits=4))s" : "State-Event #$(e.indicator) @ $(round(e.t; digits=4))s")

"""
ToDo
The mutable struct representing a specific Solution of a FMI2 FMU.
"""
mutable struct FMU2Solution{C} <: FMUSolution where {C}
component::C # FMU2Component
Expand Down Expand Up @@ -191,7 +191,12 @@ function Base.show(io::IO, sol::FMU2Solution)
end

"""
ToDo.
Source: FMISpec 2.0.3 [p.16f]

This is a pointer to a data structure in the simulation environment that calls the FMU. Using this
pointer, data from the modelDescription.xml file [(for example, mapping of valueReferences to
variable names)] can be transferred between the simulation environment and the logger function
(see [FMISpec 2.0.3] section 2.1.5).
"""
mutable struct FMU2ComponentEnvironment
logStatusOK::Bool
Expand Down
Loading