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

fmi2SetDebugLogging is not working properly #112

Open
juguma opened this issue May 17, 2024 · 2 comments
Open

fmi2SetDebugLogging is not working properly #112

juguma opened this issue May 17, 2024 · 2 comments

Comments

@juguma
Copy link

juguma commented May 17, 2024

The FMIImport-function fmi2SetDebugLogging can't be used to activate logging - the type of the last argument is ::Ptr{Nothing} which is at least not very intuitive to fill.

Furthermore, the fmi2ModelDescription in FMIImport lacks the info about the logCategories, even if they exist in the modelDescription.

Below and attached are a script and an FMU which demonstrate and solve the problem. The FMU was explicitly prepared to print messages for the different logcategories and with a different fmi2Status (to show that the printed fmi2Status and the logCategories work independently). All the extra Messages were added to "fmi2SetupExperiment", hence this function is run several time.

using FMI
using FMIImport
using FMIImport: fmi2SetDebugLogging
import FMIImport.FMICore: FMUComponent

FMUPath = "LorenzTwice.fmu"
fmu = fmiLoad(FMUPath)

fmu.modelDescription.logCategories = ["logStatusOKInfo", "logStatusWarning", "logStatusOKDebug","logStatusError","logAll"]  
#<-further down: proposal how to fill these values from modelDescription during fmiLoad

fmu.executionConfig.loggingOn = false
c = fmi2Instantiate!(fmu; type=UInt32(0))

#the FMIImport-function:
#fmi2SetDebugLogging(c,1,1,["LogStatusOKWarning"])
#can't be used to activate logging - the type of the last argument is ::Ptr{Nothing} which is at least not very intuitive to fill

#go more low-level (in FMICore):
#status = fmi2SetDebugLogging(c.fmu.cSetDebugLogging, c.compAddr, logginOn, nCategories, categories)

# set all categories to false:
status = fmi2SetDebugLogging(c.fmu.cSetDebugLogging, c.compAddr, Int32(0), UInt64(0), [pointer("this argument is not used here")])
#<-works

#switch on one category at once and see the output:
lcs = fmu.modelDescription.logCategories
for lc in lcs
    @info lc
    status = fmi2SetDebugLogging(c.fmu.cSetDebugLogging, c.compAddr, Int32(1), UInt64(1), [pointer(lc)])
    fmi2SetupExperiment(c,0.0,0.1)
    status = fmi2SetDebugLogging(c.fmu.cSetDebugLogging, c.compAddr, Int32(0), UInt64(1), [pointer(lc)])
end
#<-this works as expected


#hence modify the last argument of the FMIImport-function
#proposal:
function FMIImport.fmi2SetDebugLogging(c::FMU2Component, loggingOn::fmi2Boolean, nCategories::Unsigned, categories::Vector{String})
    @info "This is the redefined fmi2SetDebugLogging from FMIImport"
    #<remove this line prior to commit
    status = fmi2SetDebugLogging(c.fmu.cSetDebugLogging, c.compAddr, loggingOn, nCategories, [pointer(cat) for cat in categories])      
end

#such that the following call sequence returns the same output:
status = fmi2SetDebugLogging(c, Int32(0), UInt64(0), ["this argument is not used here"])

#switch on one category at once and see the output:
for lc in lcs
    @info lc
    status = fmi2SetDebugLogging(c, Int32(1), UInt64(1), [lc])
    fmi2SetupExperiment(c,0.0,0.1)
    status = fmi2SetDebugLogging(c, Int32(0), UInt64(1), [lc])
end
#<- this works :-)

#######
# what remains:
# how to add logCategories to fmu.modelDescription
# node logCatgegories can contain an array of "Category" (0..n), each Category having a "name" and a "description"
# extend function fmi2LoadModelDescription(pathToModelDescription::String) from FMIImport
using FMI: fmi2ModelDescription
using FMIImport: parseNodeString


md = fmu.modelDescription
using EzXML
pathToModelDescription = fmu.path*"\\modelDescription.xml"
doc = readxml(pathToModelDescription)
root = doc.root

# defaults
 md.logCategories = nothing
#<- add this line to fmi2LoadModelDescription (not necessary, since already set to nothing in struct constructor)

#->define the function parseLogCategories (Variants 2 and 3 below)
#Variant 1: only return category names as an array(, least informative but sufficient)
#advantage:cheapest to write, easiest to read names from
#disadvantage: no description info
function parseLogCategories(nodes::EzXML.Node, md::fmi2ModelDescription)
    categories = Array{String, 1}()

    for node in eachelement(nodes)
        push!(categories,node["name"])
    end
    categories
end


for node in eachelement(root)
    if node.name == "LogCategories" 
        md.logCategories = parseLogCategories(node,md)
    end
end
#<-add case in loop over nodes in fmi2LoadModelDescription


##################################################################
# alternatives for parseLogCategories:

#Variant 2: return names and description as a Dictionary
#advantage: all information, nicely ordered, names to get via keys(a)
#disadvantage: fmi2ModelDescription: logCategories type needs to be changed
function parseLogCategories(nodes::EzXML.Node, md::fmi2ModelDescription)
    categories = Dict{String, String}()

    for node in eachelement(nodes)
        categories[node["name"]]=node["description"]
    end
    categories
end



#Variant 3: defining a separate struct.
#advantage: no need to change fmi2ModelDescription struct (since an Array of Category(es) is returned)
#disatvantage: hard to read "names" later on
mutable struct Category
    # mandatory
    name::Union{String, Nothing}
    # optional
    description::Union{String, Nothing}

    function Category()
        inst = new()
        inst.name = nothing 
        inst.description = nothing
        return inst
    end
end
function parseLogCategories(nodes::EzXML.Node, md::fmi2ModelDescription)
    categories = Array{Category, 1}()
    for node in eachelement(nodes)
        category = Category()
        # mandatory 
        category.name = node["name"]
        # optional
        category.description = parseNodeString(node, "description")       
        push!(categories,category)
    end
    categories
end

Based on what you prefer and the status of upcomping package updates, feel free to simply add the proposed modifications yourself (since at least Variant 2 would involve modifications in FMI as well), or let me know if you prefer a pull request.

@juguma
Copy link
Author

juguma commented May 17, 2024

LorenzTwice.zip
(Please rename back to *.fmu)

@ThummeTo
Copy link
Owner

PR would be nice :-)

(maybe on top of 1.0.9 release, if #132 is fine)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants