A dynamic link library and driving method for driving FMU model data communication
By developing a dynamic link library that drives FMU model data communication, the Modelica language limitations on the utilization of FMU model data structures and simulation data communication are solved, and real-time online simulation communication between the Modelica language platform and external FMU models is realized, ensuring the accuracy and consistency of simulation results.
Patent Information
- Application Number
- CN202510019709.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-07
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2045-01-07
AI Technical Summary
The existing technology cannot realize the full utilization of the FMU model data structure and simulated data communication by the Modelica language, and the limitations of Fortran and C languages by Modelica make it impossible to directly call the FMU model.
Develop a dynamic link library that drives data communication of FMU models, realize simulation data communication with the FMU model through the dynamic link library, establish the mapping relationship between the C language and the Modelica language data types, and realize real-time online simulation communication between the Modelica language platform and the external FMU model.
The simulation communication function of the Modelica language platform for FMU models is realized, allowing FMU models generated by different software and languages to communicate and simulate data in the Modelica language. The simulation results are consistent with the original model construction software and the effectiveness is verified.
Smart Images

Figure CN119415076B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of FMU model simulation, and in particular relates to a dynamic link library and a driving method for driving FMU model data communication. Background Art
[0002] The FMI standard promotes the exchange and joint simulation of functional models, with the FMU model (Functional Mock-up Unit) as its core, supporting seamless model interaction across tools. For example, by converting the FMU model into a Modelica model for simulation, or exporting the model built using Modelica as an FMU model, parameter tuning is performed in Python using fmpy. Modelica, as an object-oriented, non-causal multi-domain modeling language, is widely used in industrial simulation. However, prior art such as patent CN114860388A uses the compilation environment of the application software itself to implement the definition of model parameters, interfaces, functions, algorithms, etc. of modelica semantics and text programming in the modelica language, mainly completing the parsing and translation of the model description file in the FMU model, while the FMU model also contains the calculation formula and data flow process of the native model itself, which is a complete data structure, but the patent does not implement the modelica language to build the FMU model data structure and simulation data communication functions. At present, the prior art cannot realize the comprehensive utilization of the FMU model data structure and simulation data communication by Modelica. In addition, Modelica's restrictions on Fortran and C languages, as well as its incompatibility with FMU models written in C / C++, make it impossible to directly call FMU models. Therefore, in order to achieve simulation scheduling and data communication between Modelica and FMU models, it is necessary to develop a dynamic link library that can build FMU model data structures and bridge Modelica with external C languages. Summary of the invention
[0003] In view of this, the present invention aims to propose a dynamic link library and a driving method for driving FMU model data communication, so as to effectively realize online simulation data communication of FMU model by simulation software based on or supporting Modelica language.
[0004] To achieve the above object, the technical solution of the present invention is achieved as follows:
[0005] A dynamic link library that drives FMU model data communication.
[0006] Further, the simulation software communicates with at least one FMU model in real time online data via a dynamic link library for simulation, including:
[0007] The dynamic link library creates a data structure for communicating with the FMU model data based on the defined FMU model data structure, and realizes simulation data communication between the dynamic link library and the FMU model to obtain simulation status and real-time simulation results;
[0008] The simulation software realizes the simulation data communication of the dynamic link library by establishing a mapping relationship between the C language and its own language data type, and realizes the real-time online simulation communication between the simulation software and the external FMU model through the bridging of the dynamic link library.
[0009] Further, the simulation software includes a simulation platform based on or supporting the modelica language, and the definition of the FMU model data structure includes:
[0010] The simulation mode enumeration class is used to manage the simulation software and query the current stage of the simulation process;
[0011] The simulation runtime status enumeration class is used to indicate the FMU model function call results to the simulation software;
[0012] FMU model simulation callback function structure, which is used for the interaction between the FMU model and the simulation software;
[0013] FMU model instance structure, which is used to encapsulate the necessary information and management functions of the FMU model.
[0014] Further, the FMU model simulation callback function structure includes: FMU model running log callback function, FMU model instance memory allocation callback function, FMU model instance memory release callback function, FMU model simulation step completion callback function, and simulation environment pointer;
[0015] The FMU model instance structure includes, based on the FMU model information: a black box general pointer of all model equations and variable information, the FMU model simulation mode enumeration class, the FMU model instance name, the FMU model globally unique identifier, a flag of whether to output simulation logs, the simulation end time, the FMU model simulation callback function structure, an array pointer of all real number variables in the FMU model, an array pointer of all integer variables in the FMU model, and an array pointer of all Boolean variables in the FMU model.
[0016] Further, the simulation mode enumeration class includes different stages of FMU model simulation: instantiation mode fmi2InstantiationMode, initialization mode fmi2InitializationMode, event mode fmi2EventMode and continuous time mode fmi2ContinuousTimeMode;
[0017] The simulation runtime status enumeration class includes: function successful execution status fmi2OK, function execution warning status fmi2Warning, slave model return value abnormal status fmi2Discard, function execution error status fmi2Error, FMU model instance damaged status fmi2Fatal, and slave model asynchronous execution status fmi2Pending.
[0018] Further, a driving method is based on the dynamic link library for driving FMU model data communication, the driving method drives the simulation software to communicate with the FMU model in real-time online simulation through the dynamic link library, and the data structure of the dynamic link library and the FMU model data communication includes:
[0019] Custom function pointers: Custom function pointers use the typedef keyword to define function pointer variables pointing to FMI2.0 standard functions, so as to realize the communication between functions in the dynamic link library and functions in the FMU model.
[0020] An externally accessible structure object, used to store the handle HINSTANCE variable of the dynamic link library and the function pointer variable;
[0021] Export functions that are visible and callable to external modules are defined using the __declspec(dllexport) modifier, making the exported functions have the same functionality as the FMU model simulation functions.
[0022] Furthermore, the simulation data communication between the dynamic link library and the FMU model includes:
[0023] The dynamic link library obtains the model name in the FMU model instance structure, and uses the LoadLibrary() function to load the FMU model, obtains the handle of the FMU model link library and stores it in the handle HINSTANCE variable of the structure object;
[0024] The handle HINSTANCE variable calls the GetProcAddress() function to pass the standard function name defined in the corresponding FMI standard to obtain the function pointer addresses of all functions in the FMU model link library, and assigns these function pointer addresses to the function pointer variables in the structure object in turn. The exported function calls the FMU model function pointer of the corresponding function to realize the simulation data communication between the dynamic link library and the FMU model.
[0025] Furthermore, when the simulation software is a simulation platform based on or supporting the Modelica language, the mapping relationship between the C language and the Modelica language data types includes:
[0026] Return complex data. For the Modelica language platform, non-basic data structures are mapped to the C language using the ExternalObject type. For the C language, non-basic data structures are returned to the Modelica language platform using the pointer type.
[0027] Return simple data. If it is returned through output parameters, for the Modelica language platform, use the output keyword to define the parameters of the return result. For the C language, use the pointer type to define the corresponding output parameters.
[0028] If the return value is direct, the Modelica language platform corresponds to the function return type of the C language.
[0029] Furthermore, the simulation data communication between the Modelica language platform and the dynamic link library includes:
[0030] The Modelica language platform writes an external function declaration for each function to be called from the FMU model, including the function's input and output parameters and data types;
[0031] Corresponding to the input and output parameter types, the Modelica language platform defines a calling function with the same name as the export function to implement the calling of the export function;
[0032] The name and location of the dynamic link library are specified at the end of the external function through the annotation keyword. The Modelica language platform receives data through the IncludeDirectory and LibraryDirectory instructions to realize the simulation data communication between the Modelica language platform and the dynamic link library.
[0033] Furthermore, an electronic device includes a processor and a memory that is communicatively connected to the processor and is used to store instructions executable by the processor, and the processor is used to execute the driving method.
[0034] Furthermore, a computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the driving method is implemented.
[0035] Compared with the prior art, the dynamic link library and driving method for driving FMU model data communication described in the present invention have the following beneficial effects:
[0036] In some incompatible FMU model development environments such as the Modelica language platform, users only need to add references to the dynamic link library and the corresponding header file in the annotation, and the corresponding mapping table of the C language and Modelica variable type definition, to realize the simulation communication function of Modelica to the FMU model. Thus, the data communication and simulation of FMU models generated by different software and different languages in the Modelica language are realized; and after experiments, it is found that the simulation results obtained by this dynamic link library are consistent with the simulation results of the original model building software, which fully demonstrates the effectiveness of the invention method. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] The accompanying drawings, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.
[0038] In the attached picture:
[0039] Figure 1 A schematic diagram of a dynamic link library bridge diagram of the simulation software and the FMU model according to an embodiment of the present invention;
[0040] Figure 2 A schematic diagram of a flow chart for generating a dynamic link library according to an embodiment of the present invention;
[0041] Figure 3 This is a schematic diagram of the code principle diagram of the driving method described in an embodiment of the present invention. DETAILED DESCRIPTION
[0042] It should be noted that, in the absence of conflict, the embodiments of the present invention and the features in the embodiments may be combined with each other.
[0043] The present invention will be described in detail below with reference to the accompanying drawings and in conjunction with embodiments.
[0044] Dynamic link library that drives FMU model data communication, such as Figure 1 As shown, the simulation software communicates with at least one FMU model in real time online via a dynamic link library for joint simulation. Figure 2As shown, the dynamic link library is preferably generated by C language compilation, and the dynamic link library creates a data structure for communicating with the FMU model data based on the defined FMU model data structure, including a series of function pointers pointing to FMI2.0 standard functions, structures accessible to the outside of the dynamic link library, and export functions, to achieve simulation data communication between the dynamic link library and the FMU model to obtain simulation status and real-time simulation results. On the other hand, the present embodiment preferably uses the modelica language platform as the simulation software for description, and the modelica language platform implements the header file reference of the dynamic link library and the corresponding data structure and communication interface function declaration through external object loading, and establishes a mapping relationship between the C language and the modelica language data type, to achieve simulation data communication between the modelica language platform and the dynamic link library, and finally uses the dynamic link library bridge to achieve real-time online simulation communication between the modelica language platform and the external FMU model.
[0045] Specifically, as shown in Table 1 below, the mapping relationship between C language and Modelica language data types includes:
[0046] Return complex data. For the Modelica language platform, non-basic data structures are mapped to the C language using the ExternalObject type. For the C language, non-basic data structures are returned to the Modelica language platform using the pointer type.
[0047] Return simple data. If it is returned through output parameters, for the Modelica language platform, use the output keyword to define the parameters of the return result. For the C language, use the pointer type to define the corresponding output parameters.
[0048] If the return value is direct, the Modelica language platform corresponds to the function return type of the C language.
[0049] Table 1 Mapping relationship between data types of Modelica language platform and C language
[0050]
[0051] Furthermore, the driving method in this embodiment is based on the above-mentioned dynamic link library to realize the real-time online simulation communication between the simulation software and the FMU model, and the definition of the FMU model data structure includes:
[0052] S1, simulation mode enumeration class, used for modelica language platform management and query the current stage of simulation process: instantiation mode fmi2InstantiationMode, initialization mode fmi2InitializationMode, event mode fmi2EventMode and continuous time mode fmi2ContinuousTimeMode, a total of 4 types;
[0053] S2, simulation runtime status enumeration class, used to indicate the FMU model function call result to the Modelica language platform, including: function successful execution status fmi2OK, function execution warning status fmi2Warning, slave model return value abnormal status fmi2Discard, function execution error status fmi2Error, FMU model instance damaged status fmi2Fatal, slave model asynchronous execution status fmi2Pending, a total of 6 status flags;
[0054] S3, FMU model simulation callback function structure, which is used for the interaction between the FMU model and the modelica language platform, including: FMU model running log callback function, FMU model instance memory allocation callback function, FMU model instance memory release callback function, FMU model simulation step completion callback function, simulation environment pointer, where the simulation environment pointer is used to prompt the operating environment of the subordinate model;
[0055] S4. Create an FMU model instance structure based on the FMU model information. The structure is used to encapsulate the necessary information and management functions of the FMU model, including: a black box general pointer to all model equations and variable information, the FMU model simulation mode enumeration class in S1, the FMU model instance name, the FMU model global unique identifier, whether to output the simulation log flag, the simulation end time, the FMU model simulation callback function structure in S2, the array pointer of all real variables in the FMU model, the array pointer of all integer variables in the FMU model, and the array pointer of all Boolean variables in the FMU model.
[0056] Furthermore, in this embodiment, the data structure of the dynamic link library and the FMU model data communication includes:
[0057] A1. Custom function pointers. Custom function pointers use the typedef keyword to define a series of function pointer variables pointing to FMI2.0 standard functions. The mapping relationship between custom function pointers and functions in the FMU model is set to achieve communication between functions in the dynamic link library and functions in the FMU model, as follows: define typedef fmi2Component(*fmi2InstantiateFunc)(fmi2String, fmi2Type, fmi2String, fmi2String, constfmi2CallbackFunctions*, fmi2Boolean, fmi2Boolean) function pointer. This function pointer will point to the instantiation function of the FMU model, thereby obtaining the FMU model instance of the FMU model in the current simulation environment. Other functions also define initialization function pointers, exit initialization function pointers, single-step simulation function pointers, simulation result acquisition function pointers, FMU model variable assignment function pointers, simulation termination function pointers, etc. For specific parameters and return values, refer to the FMI2.0 standard to complete the relevant definitions;
[0058] A2, an externally accessible structure object, used to store the dynamic link library handle HINSTANCE variable and the function pointer variable in A1;
[0059] A3. The exported function is visible and callable to external modules. The exported function is defined with the __declspec(dllexport) modifier so that the exported function has the same function as the FMU model simulation function in A1, as follows: Corresponding to the FMU model single-step simulation function pointer typedef fmi2Status(*fmi2DoStepFunc)(fmi2Component,fmi2Real,fmi2Real,fmi2Boolean) mentioned in A1, it needs to be defined as __declspec(dllexport) double fmi2_FMU model_do_step(void*m,doublecurrentTime,double stepSize) at this time to enable modules outside the dynamic link library to call this function.
[0060] Furthermore, in this implementation, the simulation data communication between the dynamic link library and the FMU model includes:
[0061] T1, the dynamic link library obtains the model name in the FMU model instance structure, and uses the LoadLibrary() function to load the FMU model, obtains the handle of the FMU model link library and stores it in the handle HINSTANCE variable of the structure object in A2;
[0062] T2, the handle HINSTANCE variable calls the GetProcAddress() function to pass the corresponding standard function name defined in the FMI standard to obtain the function pointer addresses of all functions in the FMU model link library, and assigns these function pointer addresses to the function pointer variables in the structure object in A2 in turn;
[0063] T3. Export the FMU model function pointer of the function call corresponding to the function to realize the simulation data communication between the dynamic link library and the FMU model.
[0064] Taking the single-step simulation function of the FMU model as an example, in the following C code, res is the structure object defined in A2, res->HINSTANCE is the dynamic link library handle, res->adcFmiDoStep is the single-step simulation function pointer variable in the A2 structure object, and LoadLibrary() is used to load the FMU model to obtain the dynamic link library handle and store it in res->HINSTANCE. The GetProcAddress() function is called to store the first address of the FMI2.0 standard fmi2DoStep function in res->adcFmiDoStep. In the fmi2_FMU model_do_step() function, fmi2Status is the runtime status of the FMU model of S2, res ->FMU model Inst is the FMU model instance structure defined in S4, currentTime is the current simulation time of the FMU model, stepSize is the single-step simulation step size of the FMU model, and the exported function fmi2_FMU model_do_step() calls the first address of res ->adcFmiDoStep() function to communicate with the fmi2DoStep of the FMU model, and uses the characteristics of the exported function to provide it to other modules for calling, thereby realizing communication between the dynamic link library and the outside.
[0065] struct FMUModel_exp_struct res = calloc(1, sizeof(struct FMUModel_exp_struct));
[0066] if (res!=0)
[0067] {
[0068] res->HINSTANCE=LoadLibrary(FMU model Path)
[0069] res->adcFmiDoStep=(fmi2DoStepFunc)GetProcAddress(res->hInst,"fmi2DoStep");
[0070] }
[0071] __declspec(dllexport) double fmi2_FMU model_do_step(void* component, double currentTime, double stepSize)
[0072] {
[0073] struct FMU model_exp_struct* res = component;
[0074] fmi2Status status = fmi2Fatal;
[0075] if (res )
[0076] {
[0077] status = res ->adcFmiDoStep(res ->FMU modelInst, currentTime, stepSize,fmi2True);}
[0078] return status ;
[0079] }
[0080] Furthermore, in this embodiment, the simulation data communication between the modelica language platform and the dynamic link library includes:
[0081] The Modelica language platform writes an external function declaration for each function to be called from the FMU model, including the function's input and output parameters and data types. Corresponding to the input and output parameter types, the Modelica language platform defines a calling function with the same name as the exported function, and uses the External "C" keyword to implement the call to the dynamic link library export function. At the end of the external function, the name and location of the dynamic link library are specified through the annotation keyword in the modelica language. The modelica language platform receives data through the IncludeDirectory and LibraryDirectory instructions to implement simulation data communication between the modelica language platform and the dynamic link library, where IncludeDirectory receives the path of the dynamic link library header file, Include receives the header file name, LibraryDirectory receives the path of the dynamic link library, and Library receives the name of the dynamic link library.
[0082] The Modelica language platform in this embodiment can directly communicate with common model data such as Simulink model and AMESIM model through the dynamic link library to generate corresponding simulation results, such as Figure 3 shown.
[0083] Those of ordinary skill in the art will appreciate that the units and method steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0084] In the several embodiments provided in the present application, it should be understood that the disclosed methods and systems can be implemented in other ways. For example, the division of the units described above is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. The above-mentioned units may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiments of the present invention.
[0085] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. These modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present invention, and they should all be included in the scope of the claims and specification of the present invention.
[0086] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A dynamic link library for driving FMU model data communication, characterized in that: The simulation software communicates with at least one FMU model in real time online data via a dynamic link library for simulation, including: The dynamic link library creates a data structure for communicating with the FMU model data based on the defined FMU model data structure, and realizes simulation data communication between the dynamic link library and the FMU model to obtain simulation status and real-time simulation results; The simulation software realizes the simulation data communication of the dynamic link library by establishing the mapping relationship between the C language and its own language data type, and realizes the real-time online simulation communication between the simulation software and the external FMU model through the bridge of the dynamic link library; The simulation software includes a simulation platform based on or supporting the modelica language, and the definition of the FMU model data structure includes: The simulation mode enumeration class is used to manage the simulation software and query the current stage of the simulation process; The simulation runtime status enumeration class is used to indicate the FMU model function call results to the simulation software; FMU model simulation callback function structure, which is used for the interaction between the FMU model and the simulation software; FMU model instance structure, which is used to encapsulate the necessary information and management functions of the FMU model; The FMU model simulation callback function structure includes: an FMU model running log callback function, an FMU model instance memory allocation callback function, an FMU model instance memory release callback function, an FMU model simulation step completion callback function, and a simulation environment pointer; The FMU model instance structure includes, based on the FMU model information: a black box general pointer of all model equations and variable information, the FMU model simulation mode enumeration class, the FMU model instance name, the FMU model globally unique identifier, a flag of whether to output simulation logs, the simulation end time, the FMU model simulation callback function structure, an array pointer of all real number variables in the FMU model, an array pointer of all integer variables in the FMU model, and an array pointer of all Boolean variables in the FMU model.
2. A dynamic link library for driving FMU model data communication according to claim 1, characterized in that: The simulation mode enumeration class includes different stages of FMU model simulation: instantiation mode fmi2InstantiationMode, initialization mode fmi2InitializationMode, event mode fmi2EventMode and continuous time mode fmi2ContinuousTimeMode; The simulation runtime status enumeration class includes: function successful execution status fmi2OK, function execution warning status fmi2Warning, slave model return value abnormal status fmi2Discard, function execution error status fmi2Error, FMU model instance damaged status fmi2Fatal, and slave model asynchronous execution status fmi2Pending.
3. A driving method, based on a dynamic link library for driving FMU model data communication according to claim 1, characterized in that: The driving method drives the real-time online simulation communication between the simulation software and the FMU model through the dynamic link library. The data structure of the dynamic link library and the FMU model data communication includes: Custom function pointers: Custom function pointers use the typedef keyword to define function pointer variables pointing to FMI2.0 standard functions, so as to realize the communication between functions in the dynamic link library and functions in the FMU model. An externally accessible structure object, used to store the handle HINSTANCE variable of the dynamic link library and the function pointer variable; Export functions that are visible and callable to external modules are defined using the __declspec(dllexport) modifier, making the exported functions have the same functionality as the FMU model simulation functions.
4. A driving method according to claim 3, characterized in that: The simulation data communication between the dynamic link library and the FMU model includes: The dynamic link library obtains the model name in the FMU model instance structure, and uses the LoadLibrary() function to load the FMU model, obtains the handle of the FMU model link library and stores it in the handle HINSTANCE variable of the structure object; The handle HINSTANCE variable calls the GetProcAddress() function to pass the standard function name defined in the corresponding FMI standard to obtain the function pointer addresses of all functions in the FMU model link library, and assigns these function pointer addresses to the function pointer variables in the structure object in turn. The exported function calls the FMU model function pointer of the corresponding function to realize the simulation data communication between the dynamic link library and the FMU model.
5. A driving method according to claim 3, characterized in that: When the simulation software is based on or supports the Modelica language simulation platform, the mapping relationship between the C language and the Modelica language data types includes: Return complex data. For the Modelica language platform, non-basic data structures are mapped to the C language using the ExternalObject type. For the C language, non-basic data structures are returned to the Modelica language platform using the pointer type. Return simple data. If it is returned through output parameters, for the Modelica language platform, use the output keyword to define the parameters of the return result. For the C language, use the pointer type to define the corresponding output parameters. If the return value is direct, the Modelica language platform corresponds to the function return type of the C language.
6. A driving method according to claim 5, characterized in that: The simulation data communication between the Modelica language platform and the dynamic link library includes: The Modelica language platform writes an external function declaration for each function to be called from the FMU model, including the function's input and output parameters and data types; Corresponding to the input and output parameter types, the Modelica language platform defines a calling function with the same name as the export function to implement the calling of the export function; The name and location of the dynamic link library are specified at the end of the external function through the annotation keyword. The Modelica language platform receives data through the IncludeDirectory and LibraryDirectory instructions to realize the simulation data communication between the Modelica language platform and the dynamic link library.
7. An electronic device, comprising a processor and a memory connected to the processor for storing instructions executable by the processor, characterized in that: The processor is used to execute a driving method described in any one of claims 3-6.
8. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, a driving method as described in any one of claims 3 to 6 is implemented.
Citation Information
Patent Citations
FMI standard-based model conversion method and model conversion system
CN107908830A
Simulation model interface adaptation development system and working method thereof
CN111367511A