modelon-community / fmi-library

C library for importing FMUs
Other
115 stars 34 forks source link

create Fmu #122

Closed YanceyYq closed 5 months ago

YanceyYq commented 5 months ago

How to use FMILibrary to package shared library and description file into an FMU

PeterMeisrimelModelon commented 5 months ago

Hi,

this is an FMU export question and not within the scope of fmi-library, which is an FMU import tool.

/Peter

YanceyYq commented 5 months ago

When I manually package a binary file and a description file into an FMU file, it causes an error when parsing it using the FMI-Library.

PeterMeisrimelModelon commented 5 months ago

What is the error message?

Also, please check the FMI specification chapter "FMU Distribution"

/Peter

YanceyYq commented 5 months ago

After executing the line: fmi_version_enu_t version = fmi_import_get_fmi_version(context, fmuPath, tmpPath); The value of version is fmi_version_unknown_enu (0).

PeterMeisrimelModelon commented 5 months ago

Can you share your modelDescription.xml?

YanceyYq commented 5 months ago

modelDescription.txt

PeterMeisrimelModelon commented 5 months ago

There were a couple of mistakes in there; this one is valid, please check the diff:

<?xml version="1.0" encoding="UTF-8"?>  
<fmiModelDescription fmiVersion="2.0" modelName="Cos" guid="1ab5f37481bd5cf4f791a144ee2114f3" generationTool="JModelica.org" generationDateAndTime="2019-06-29T12:20:28" variableNamingConvention="structured" numberOfEventIndicators="0">
<CoSimulation modelIdentifier="Cos" needsExecutionTool="false" canHandleVariableCommunicationStepSize="true" canInterpolateInputs="true" maxOutputDerivativeOrder="0" canRunAsynchronuously="false" canBeInstantiatedOnlyOncePerProcess="true" canNotUseMemoryManagementFunctions="false" canSerializeFMUstate="false" providesDirectionalDerivative="false" /> 

<ModelVariables>  
    <ScalarVariable  
        name="u1"  
        valueReference="0"  
        causality="input"  
        variability="continuous"  
        description="Input time for the sine wave generator">
        <Real start="0.0" unit="s"/>
    </ScalarVariable>

    <ScalarVariable  
        name="y1"  
        valueReference="1"  
        causality="output"  
        variability="continuous"  
        description="Output sine wave value">
        <Real />
    </ScalarVariable>
</ModelVariables>

<ModelStructure/>
</fmiModelDescription>

Also note that it needs to modelDescription.xml, not .txt.

Also, since you seem to just be getting started in FMU Export, you may find the following resource useful:

https://modelica.github.io/fmi-guides/main/fmi-guide/ (FMI3 though; many concepts should be similar though) https://github.com/modelica/Reference-FMUs

YanceyYq commented 5 months ago

Using the XML content you provided, the version is still returning fmi_version_unknown_enu (0).

PeterMeisrimelModelon commented 5 months ago

Can you share your calling sequence?

YanceyYq commented 5 months ago

I don't understand what you need?

PeterMeisrimelModelon commented 5 months ago

A complete c code example that reproduces the problem.

The following example works for me

#include <stdio.h>
#include "fmilib.h"

int main(int argc, char **argv){
    fmi_import_context_t *ctx = fmi_import_allocate_context(jm_get_default_callbacks());
    fmi_version_enu_t version = fmi_import_get_fmi_version(ctx, "test_fmu.fmu", ".");
    printf("fmi version = %d\n", (int)version);
}

Output:

[INFO][FMILIB] XML specifies FMI standard version 2.0
fmi version = 2

where "test_fmu.fmu" is simply the modelDescription.xml zipped up and renamed.

YanceyYq commented 5 months ago

How should I resolve this?

PeterMeisrimelModelon commented 5 months ago

I don't understand the question, can you try the code I provided with the described steps and check if that works for you?

YanceyYq commented 5 months ago

I'm sorry, I made a silly mistake. I've already solved it.