Universal FMU model generation method
By defining classes for custom FMU models and compiling using Cmake tools, the autonomy of FMU model generation is achieved, the limitations of relying on third-party commercial software in the existing technology are solved, and the direct conversion of handwritten models into models that conform to the FMI interface is realized.
Patent Information
- Application Number
- CN202510319299.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-18
- Publication Date
- 2025-06-24
AI Technical Summary
In the prior art, FMU model generation needs to be modeled based on third-party commercial software, and most plug-ins only support FMU loading, parsing and running, and cannot be directly used to model and generate FMU models.
A general FMU model generation method is proposed. By defining a class of a custom FMU model, including the base class of all built-in interface functions under the FMI specification, and registering a custom model object in the Get_Model_Info code, users can view detailed instructions. Use the Cmake tool to compile the entire custom model to form an FMU model file.
Get rid of the limitations that the FMU model needs to rely on third-party commercial software for modeling, and support the development side to implement the process of crossing complex FMI interfaces in object classes through encapsulation mathematical methods, and directly convert the handwritten model into a model that conforms to the FMI interface.
Smart Images

Figure CN120196615A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer simulation calculation, and particularly relates to a general method for generating FMU models. Background Art
[0002] FMU (Functional Mock-up Unit) is a model exchange and co-simulation technology based on the Functional Mock-up Interface (FMI) standard. FMI is an open standard independent of specific tools, used to support model exchange and co-simulation in different simulation environments. An FMU is stored as a compressed file of the zip type with the suffix.fmu, and the file contains a model description file (in XML format), executable code (C code dynamic library), and related resource files. The FMI standard was first released by the Modelica Association in 2010, aiming to promote compatibility and interoperability among multi-domain system modeling tools. Currently, FMI has been widely applied in fields such as automotive, aerospace, industrial control, and energy management. The FMU model mainly supports Model Exchange based on the FMI standard, a scenario suitable for importing dynamic system models into different simulation tools, where the solver is responsible for solving equations and the FMU provides equation definitions; in addition, the FMU model also supports Co-Simulation, a scenario suitable for co-simulation among multiple simulation tools. In this mode, the FMU model itself contains a solver and can work collaboratively with other simulation tools through a communication interface.
[0003] The core of FMU model generation lies in packaging a complex physical or mathematical model into an independent unit compatible with the FMI standard. Conventional FMU model generation methods require using the original modeling tool in combination with a modeling tool or plugin that supports the FMI standard to export the model as an FMU file.
[0004] However, most modeling tools and plugins that support the FMI standard are internal functions of commercial software or can only package mathematical models built by a certain software. And most plugins only support the functions of loading, parsing, and running FMUs and cannot be directly used for modeling and generating FMU models. Summary of the Invention
[0005] In view of the problems mentioned in the above background art, the present invention proposes a general method for generating FMU models. This method gets rid of the limitation that FMU models need to rely on third-party commercial software for modeling, and supports the development side to directly convert a handwritten model into a model that conforms to the FMI interface by encapsulating the mathematical method to be implemented in the object class demo and bypassing the complex FMI interface implementation process.
[0006] Specifically, the present invention provides a general method for generating an FMU model, including the following processes: Define a class for a custom FMU model, which contains the base classes of all built-in interface functions under the FMI specification; Define the data structure of the FMU model and set the simulation algorithm. The data structure is derived from the basic data class, and the information objects containing FMI data are all wrapped into enumeration classes; Register the custom model object in the Get_Model_Info code so that users can view the detailed description of the custom model object when calling; Define an instantiation pointer for the FMU model class; Use the Cmake tool to compile the entire custom model to form an FMU model file.
[0007] As a further description of the present invention, the fixed interface functions in the base class are directly defined in the base class, and the interface functions and parameters that need to be changed according to the FMU model function are set as virtual functions and left blank in the base class and defined in the custom model class.
[0008] As a further description of the present invention, when defining the class of the custom FMU model, it is necessary to define the parameters and their attributes in the model and register and assign values in the initialization method; As a further description of the present invention, the data structure is derived from the basic data class, and the specific process is as follows: Design a data template, and then diversify the data to Real, Integer, string, and boolean data in the FMI format.
[0009] As a further description of the present invention, when defining the instantiation pointer of the FMU class, use a unique_ptr pointer for definition.
[0010] As a further description of the present invention, the use of the Cmake tool to compile the entire custom model to form an FMU model specifically includes: Define CPP rules and compilation platforms, define model types and generation rules, set the model release mode and storage bit positions, link the source code and generate installation targets, and finally generate a description file.
[0011] Compared with the prior art, the present invention has the following beneficial technical effects: The present invention gets rid of the limitation that the FMU model needs to rely on third-party commercial software for modeling, and supports that the development side can directly convert the handwritten model into a model that conforms to the FMI interface by encapsulating the mathematical methods to be implemented in the object class demo and bypassing the complex FMI interface implementation process.
[0012] The present invention only uses source code, a code compiler, and a cmake project management tool to achieve the lightweight of the project, with less code volume and can be compiled and run on different systems.
[0013] Other features and advantages of the present technical solution will be described in the subsequent specification, and part of them will become obvious from the specification, or be understood by implementing the present technical solution. The objectives and other advantages of the present technical solution can be achieved and obtained through the structures specifically pointed out in the written specification and the accompanying drawings.
[0014] The following will further describe the technical solution of the present technical solution in detail through the accompanying drawings and embodiments. Description of the Drawings
[0015] The accompanying drawings are used to provide a further understanding of the present technical solution, and constitute a part of the specification. Together with the embodiments of the present technical solution, they are used to explain the present technical solution and do not constitute a limitation to the present technical solution. In the accompanying drawings: Figure 1 It is a flowchart of a general FMU model generation method provided by the present invention.
[0016] Figure 2 It is the performance of the classic pinball simulation model generated by the method provided by the present invention in the official test library fmpy. Detailed Embodiments
[0017] The following will describe the preferred embodiments of the present technical solution with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present technical solution and are not used to limit the present technical solution.
[0018] The present invention proposes a method that can directly construct an FMU model file through CPP project compilation in the FMI rule. Specifically, as Figure 1 shown, the present invention provides a general FMU model generation method, including the following steps: Step 1: Define a class class FMU: FMU_class for a custom FMU model, which contains the base classes of all built-in interface functions under the FMI specification.
[0019] The fixed interface functions in the base class are directly defined in the base class, while the key interface functions and parameters that need to be changed according to the FMU model function are set as virtual functions and left blank in the base class and defined in the custom model class.
[0020] Specifically, when defining the class of the custom FMU model, it is necessary to define the parameters and their attributes in the model and register and assign values in the initialization method.
[0021] More specifically, the method for defining parameters is the register_variable method: After each parameter is defined and assigned a value, an object of XXXVariable is returned. This class contains the model parameter attributes under the FMI rules. The specific code for defining parameters is as follows: register_variable(parameterType( "paramName", [this] {return param;}, [this](int value) { param = value;}) .setCausality(causality_t::TYPE) .setVariability(variability_t::TYPE) .setInitial(initial_t::TYPE)); Among them, parameterType is the parameter type in the FMI rules, paramName is the parameter name in the FMU model, and param is the parameter defined in the FMU class. The subsequent set methods are all used to set the attributes of the parameters.
[0022] Step 2: Define the data structure of the FMU model and set the simulation algorithm. Among them, the data structure is derived from the basic data class, and the information objects containing FMI data are all wrapped into enumeration classes.
[0023] Specifically, the above data structure is derived from the basic data class. The specific process is as follows: Design a data template, and then diversify the data to Real, Integer, string, and boolean data in the FMI format. This design allows all data types under the FMI rules to be directly defined in the FMU object class and set the internal information of the corresponding data.
[0024] Specifically, the core simulation algorithm of the FMU model is encapsulated in the do_step function. The function is a custom method and includes the core model simulation algorithm. Operation parameters such as the simulation duration and simulation step size need to be provided. The core of the algorithm is to set the iterative calculation method according to the mathematical calculation process of the model (this part of the method is user-defined). This model algorithm can be completed by simply manually writing or calling an executable program using the command line.
[0025] Step 3: Register this custom model object in the Get_Model_Info code so that users can view the detailed description of this custom model object when calling.
[0026] Specifically, each FMU model needs to implement the Get_Model_Info() method, and the return value is a model_info object. This class defines the basic information of the model under the FMI rules (detailed description of the custom model object), enabling users to view the detailed description of the custom model object when calling this method. The pseudo-code is as follows: struct model_info { std::string modelName; std::string author; std::string description; std::string modelIdentifier; std::string version; std::string variableNamingConvention{"data type"}; bool needsExecutionTool{false}; bool canHandleVariableCommunicationStepSize{true}; bool canBeInstantialtedOnlyOncePerProcess{false}; bool canGetAndSetFMUstate{false}; bool canSerializeFMUstate{false}; … }。
[0027] Step 4: Define the instantiation pointer of the FMU model class.
[0028] Specifically, when defining the instantiation pointer of the FMU class, use the unique_ptr pointer for definition. This instantiation code undertakes the method of calling other basic functions except the customized functions in the project. The specific pseudo-code is as follows: std::unique_ptr<FMU_Class> FMU_Class::createInstance(const std::string& instanceName, const std::string& fmuResourceLocation) { return std::make_unique<CustomClassName>(instanceName,fmuResourceLocation); }.
[0029] Step 5: Use the Cmake tool to compile the entire custom model to form an FMU model file.
[0030] The above uses the Cmake tool to compile the entire custom model to form an FMU model, specifically including: defining CPP rules and compilation platforms, defining model types and generation rules, setting the model release mode and storage bit positions, linking source code and generating installation targets, and finally generating a description file.
[0031] The compilation of this model is built using the Cmake method. By implementing a custom model class that links source code, a dynamic library and a description file are finally generated and packaged as F1+3.
[0032] Functions of the FMU model file; The pseudocode of the basic architecture is as follows: set(CMAKE_CXX_STANDARD 20) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(modelIdentifier identity) include(GenerateFMU method) f (MSVC) string(REPLACE " / MD" " / MT" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") string(REPLACE " / MDd" " / MTd" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") endif () if ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8") set(BITNESS 64) else () set(BITNESS 32) endif () if (WIN32) set(TARGET_PLATFORM win${BITNESS}) elseif (APPLE) set(TARGET_PLATFORM darwin${BITNESS}) else () set(TARGET_PLATFORM linux${BITNESS}) endif () Since the default platform and tools of the present invention are used for code compilation with Visual Studio on the Windows platform. Therefore, the default values filled in the pseudocode are MSVC and WIN32, and the user side can flexibly adjust the platform and tools according to its own needs.
[0033] The present invention defines a CMake function for packaging project generated files into a fixed format. Therefore, macros for source code linking, path rules under different operating platforms, and file packaging methods for models are defined in the present invention. The pseudocode is as follows: function(generateFMU modelIdentifier) target_sources(${modelIdentifier} PRIVATE "$<TARGET_OBJECTS: Library Name>") target_include_directories("${modelIdentifier}" PRIVATE "${PROJECT_SOURCE_DIR} / export / include") target_compile_definitions("${modelIdentifier}" PRIVATE FMU4CPP_MODEL_IDENTIFIER="${modelIdentifier}") set(outputDir "$<1:${CMAKE_BINARY_DIR} / ${modelIdentifier} / binaries / ${TARGET_PLATFORM}>") if (WIN32) set_target_properties(${modelIdentifier} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${outputDir}" ) else () set_target_properties(${modelIdentifier} PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${outputDir}" ) endif () add_custom_command(TARGET ${modelIdentifier} POST_BUILD WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" COMMAND descriptionGenerator ${modelIdentifier} "${outputDir} / $<TARGET_FILE_NAME:${modelIdentifier}>") add_custom_command(TARGET ${modelIdentifier} POST_BUILD WORKING_DIRECTORY "${CMAKE_BINARY_DIR} / ${modelIdentifier}" COMMAND ${CMAKE_COMMAND} -E tar "c" "${modelIdentifier}.fmu" --format=zip "${CMAKE_BINARY_DIR} / ${modelIdentifier} / binaries" "${CMAKE_BINARY_DIR} / ${modelIdentifier} / modelDescription.xml") endfunction() The function of this part is mainly to generate the dynamic link library and model description file compiled from the source code. The reason for using CMake programming in this part is that CMake can be compiled and run across platforms. In this way, the closed-loop of cross-platform compilation of the entire model is completed.
[0034] After final compilation, a Name.fmu file can be obtained in the set path. The specific implemented model functions are reflected according to the functions defined in the previously handwritten class source code.
[0035] The present invention combines project construction to replace pure code work, integrates the entire complex data integration, compilation, linking, and packaging processes into the project compilation process for coupling, and greatly saves the code amount for directly completing function compilation.
[0036] In summary, the present invention encapsulates all interfaces of the FMU model with CPP classes, uses this class as an object of an FMU model to generate corresponding model files, and the performance of the classic pinball simulation model generated by the method provided by the present invention in the official test library fmpy is as Figure 2 shown, the custom model effect provided by the present invention is exactly the same as the model test effect given by the official.
[0037] Obviously, those skilled in the art can make various changes and modifications to this technical solution without departing from the spirit and scope of this technical solution. Thus, if these modifications and variations of this technical solution fall within the scope of the claims of this technical solution and its equivalent technologies, then this technical solution is also intended to include these changes and modifications.
Claims
1. A general FMU model generation method, characterized in that: include: Define a custom FMU model class, which contains the base class of all built-in interface functions under the FMI specification; Define the data structure of the FMU model and set the simulation algorithm. The data structure is derived from the basic data class, and the information objects containing FMI data are packaged into enumeration classes; Register the custom model object in the Get_Model_Info code so that the user can view the detailed description of the custom model object when calling it; Define the instantiation pointer of the FMU model class; Use the Cmake tool to compile the entire custom model to form an FMU model file.
2. The general FMU model generation method according to claim 1, characterized in that: The fixed interface functions in the base class are directly defined in the base class, and the interface functions and parameters that need to be changed according to the FMU model functions are set as virtual functions and left blank in the base class and defined in the custom model class.
3. The general FMU model generation method according to claim 1, characterized in that: When defining a custom FMU model class, you need to define the parameters and properties in the model and register and assign values in the initialization method.
4. The general FMU model generation method according to claim 1, characterized in that: The data structure is derived from the basic data class, and the specific process is as follows: Design a data template and then expand the data diversity to Real, Integer, string, and boolean data in FMI format.
5. The general FMU model generation method according to claim 1, characterized in that: When defining the instantiation pointer of the FMU class, use the unique_ptr pointer for definition.
6. The general FMU model generation method according to claim 1, characterized in that: The Cmake tool is used to compile the entire custom model to form an FMU model, which specifically includes: Define CPP rules and compilation platform, define model type and generation rules, set model release mode and storage bit, link source code and generate installation target, and finally generate description file.