Skip to content

Commit

Permalink
gen-mcl: Only generate network when string/binary variables present i…
Browse files Browse the repository at this point in the history
…n FMU.

Signed-off-by: Timothy Rule (VM/EMT3) <[email protected]>
  • Loading branch information
timrulebosch committed Dec 20, 2024
1 parent cfbd900 commit e795f10
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 25 deletions.
48 changes: 48 additions & 0 deletions extra/tools/fmi/cmd/fmi/testdata/script/gen-mcl_network.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
env FMUXML=$REPO_DIR/extra/tools/fmi/test/testdata/fmimcl/fmimcl_network.xml

# Create the simulation folder (Simer layout)
env
mkdir sim/lib
exec touch sim/lib/libfmimcl.so
mkdir sim/fmu/fmi2fmu
mkdir sim/fmu/fmi2fmu/binaries/linux64
mkdir sim/fmu/fmi2fmu/resources
cp $FMUXML sim/fmu/fmi2fmu/modelDescription.xml
exec touch sim/fmu/fmi2fmu/binaries/linux64/libfmi2fmu.so
exec ls -R $WORK/sim

# Generate the FMI MCL configuration
env
cd sim
exec fmi gen-mcl -fmu fmu/fmi2fmu -outdir model/fmi2fmu -mcl lib/libfmimcl.so
cd ..
exec ls -R $WORK/sim
exists sim/model/fmi2fmu/model.yaml

# Check the generated files
exec cat sim/model/fmi2fmu/model.yaml
yamlcontains sim/model/fmi2fmu/model.yaml $.kind Model

yamlcontains sim/model/fmi2fmu/model.yaml $.metadata.name Test
yamlcontains sim/model/fmi2fmu/model.yaml $.metadata.annotations.fmi_guid {0bc3244a-e274-4c4a-8205-b2a5a18af23a}
yamlcontains sim/model/fmi2fmu/model.yaml $.metadata.annotations.fmi_model_cosim true
yamlcontains sim/model/fmi2fmu/model.yaml $.metadata.annotations.fmi_model_version 1.1
yamlcontains sim/model/fmi2fmu/model.yaml $.metadata.annotations.fmi_resource_dir fmu/fmi2fmu/resources
yamlcontains sim/model/fmi2fmu/model.yaml $.metadata.annotations.fmi_stepsize 0.0005
yamlcontains sim/model/fmi2fmu/model.yaml $.metadata.annotations.mcl_adapter fmi
yamlcontains sim/model/fmi2fmu/model.yaml $.metadata.annotations.mcl_version 2.0

yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[0].alias signal_channel
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[0].selectors.model Test
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[0].selectors.channel signal_vector

yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[1].alias network_channel
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[1].selectors.model Test
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[1].selectors.channel network_vector

yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.dynlib[0].arch amd64
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.dynlib[0].os linux
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.dynlib[0].path lib/libfmimcl.so
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.mcl[0].arch amd64
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.mcl[0].os linux
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.mcl[0].path fmu/fmi2fmu/binaries/linux64/target.so
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[0].alias signal_channe
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[0].selectors.model Test
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[0].selectors.channel signal_vector

! yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[1].alias network_channel
! yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[1].selectors.model Test
! yamlcontains sim/model/fmi2fmu/model.yaml $.spec.channels[1].selectors.channel network_vector

yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.dynlib[0].arch amd64
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.dynlib[0].os linux
yamlcontains sim/model/fmi2fmu/model.yaml $.spec.runtime.dynlib[0].path lib/libfmimcl.so
Expand Down
27 changes: 20 additions & 7 deletions extra/tools/fmi/internal/app/generate/fmimcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (c *FmiMclCommand) Run() error {
h := fmi2.XmlFmuHandler{}
var fmiMD *fmi2.FmiModelDescription
if fmiMD = h.Detect(fmuModelDescriptionFilename); fmiMD == nil {
return fmt.Errorf("Could not read FMU Model Description file!")
return fmt.Errorf("could not read FMU Model Description file")
}
if err := c.generateModel(*fmiMD); err != nil {
return err
Expand All @@ -79,13 +79,26 @@ func _generateChannels(fmiMD fmi2.FmiModelDescription) ([]kind.Channel, error) {
"channel": "signal_vector",
},
},
{
Alias: stringPtr("network_channel"),
Selectors: &kind.Labels{
"model": fmiMD.ModelName,
"channel": "network_vector",
}
binarySignalCount := func() int {
count := 0
for _, s := range fmiMD.ModelVariables.ScalarVariable {
if s.String != nil {
count++
}
}
return count
}()
if binarySignalCount > 0 {
channels = append(channels,
kind.Channel{
Alias: stringPtr("network_channel"),
Selectors: &kind.Labels{
"model": fmiMD.ModelName,
"channel": "network_vector",
},
},
},
)
}
return channels, nil
}
Expand Down
37 changes: 19 additions & 18 deletions extra/tools/fmi/internal/app/generate/signalgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func (c *GenSignalGroupCommand) generateSignalVector(fmiMD fmi2.FmiModelDescript
}
}

// Write the SignalGroups.
fmt.Fprintf(flag.CommandLine.Output(), "Appending file: %s\n", c.outputFile)

signalVector := kind.SignalGroup{
Kind: "SignalGroup",
Metadata: &kind.ObjectMetadata{
Expand All @@ -124,26 +127,24 @@ func (c *GenSignalGroupCommand) generateSignalVector(fmiMD fmi2.FmiModelDescript
},
}
signalVector.Spec.Signals = scalarSignals

networkVector := kind.SignalGroup{
Kind: "SignalGroup",
Metadata: &kind.ObjectMetadata{
Name: stringPtr(fmiMD.ModelName),
Labels: &kind.Labels{
"channel": "network_vector",
"model": fmiMD.ModelName,
},
Annotations: &kind.Annotations{
"vector_type": "binary",
writeYaml(&signalVector, c.outputFile, true)
if len(binarySignals) > 0 {
networkVector := kind.SignalGroup{
Kind: "SignalGroup",
Metadata: &kind.ObjectMetadata{
Name: stringPtr(fmiMD.ModelName),
Labels: &kind.Labels{
"channel": "network_vector",
"model": fmiMD.ModelName,
},
Annotations: &kind.Annotations{
"vector_type": "binary",
},
},
},
}
networkVector.Spec.Signals = binarySignals
writeYaml(&networkVector, c.outputFile, true)
}
networkVector.Spec.Signals = binarySignals

// Write the SignalGroups.
fmt.Fprintf(flag.CommandLine.Output(), "Appending file: %s\n", c.outputFile)
writeYaml(&signalVector, c.outputFile, true)
writeYaml(&networkVector, c.outputFile, true)

return nil
}
43 changes: 43 additions & 0 deletions extra/tools/fmi/test/testdata/fmimcl/fmimcl_network.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<fmiModelDescription fmiVersion="2.0" modelName="Test" guid="{0bc3244a-e274-4c4a-8205-b2a5a18af23a}"
description="" author="" version="1.1" generationTool="" generationDateAndTime=""
variableNamingConvention="" numberOfEventIndicators="">
<CoSimulation modelIdentifier="target" canHandleVariableCommunicationStepSize=""
canInterpolateInputs=""></CoSimulation>
<DefaultExperiment startTime="0" stopTime="42" stepSize="0.0005"></DefaultExperiment>
<ModelVariables>
<ScalarVariable name="scalar_1" valueReference="1" causality="input" initial=""
variability="">
<Real start="42" reinit="" derivative=""></Real>
</ScalarVariable>
<ScalarVariable name="scalar_2" valueReference="2" causality="output" initial=""
variability="">
<Real></Real>
</ScalarVariable>
<ScalarVariable name="scalar_3" valueReference="3" initial="" variability="">
<Real></Real>
</ScalarVariable>
<ScalarVariable name="boolean_1" valueReference="4" causality="input" variability="discrete">
<Boolean start="false"></Boolean>
</ScalarVariable>
<ScalarVariable name="boolean_2" valueReference="5" causality="output" variability="">
<Boolean></Boolean>
</ScalarVariable>
<ScalarVariable name="scalar_4" valueReference="6" causality="local" initial=""
variability="">
<Real></Real>
</ScalarVariable>
<ScalarVariable name="string_7" valueReference="7" causality="input" initial=""
variability="">
<String></String>
</ScalarVariable>
<ScalarVariable name="string_8" valueReference="8" causality="output" initial=""
variability="">
<String></String>
</ScalarVariable>
</ModelVariables>
<ModelStructure>
<Outputs></Outputs>
<Derivatives></Derivatives>
<InitialUnknowns></InitialUnknowns>
</ModelStructure>
</fmiModelDescription>

0 comments on commit e795f10

Please sign in to comment.