Code weaving method for multi-model co-simulation
Through the post-weaving method based on the FMI standard, the combination of scripts and binary code is used to solve the problem of multi-model simulation code collaboration, and the efficient operation and structure of multi-model semi-physical simulation code is realized, which is suitable for the needs of a variety of modeling tools and operating systems.
Patent Information
- Application Number
- CN202510497766.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-21
- Publication Date
- 2025-08-15
AI Technical Summary
The existing technology has problems such as poor universality, difficulty in compiling and linking, and difficulty in compatible simulation execution tools in multi-model simulation code collaboration, and cannot adapt to the needs of the collaboration of multiple modeling tools.
The post-weaving method based on the FMI standard is adopted, and the initialization, input, output and closing parts of the interface operation code are generated through the comprehensive application of scripts and binary codes, and the initialization, input, output and closing parts of the interface operation code are weaved at the corresponding locations of the model simulation code to form semi-physical simulation code.
It realizes efficient collaboration of multi-model simulation code, can perform semi-physical simulation in multiple heterogeneous models and multiple host environments, maintains the structure of the code, adapts to a variety of modeling tools and operating systems, meets the time limit requirements of simulation step size, and the code is clear and reviewable.
Smart Images

Figure CN120491965A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a code weaving method for multi-model collaborative simulation. Background Art
[0002] To implement simulation based on mathematical models, mathematical modeling tools are typically used to generate code, resulting in model simulation code. To further implement hardware-in-the-loop (HIL) simulation, simulation design tools generate interface operation code, which is then incorporated into the model simulation code to produce HIL code. This HIL code is then downloaded to a simulation execution tool, which runs on a simulation machine with a physical interface (hereinafter referred to as the "simulator"), ultimately implementing HIL simulation. This involves three software tools: a modeling tool, a simulation design tool, and a simulation execution tool.
[0003] Weaving itself is a method of cross-aggregating codes from different sources to form new codes. The woven code can be divided into two ends, one end is the slice code and the other end is the target code. The slice code is embedded into the target code by modification, insertion, etc. to form a new code. The weaving process should not affect the logical framework of the target code, but add the function of the slice code to the target code. According to the different target codes, the weaving code can be divided into three categories: source code-based, binary code-based, and script-based. Script-based weaving is between the two. In the semi-physical simulation environment, from the perspective of weaving operations, the model simulation code is the target code and the interface operation code is the slice code.
[0004] In practice, hardware-in-the-loop simulation vendors such as dSPACE and SpeedGoat all use plug-ins to add to modeling tools for code weaving, that is, embedding simulation design tools into modeling tools, and completing the weaving of interface operation codes while generating model simulation codes. This weaving technology can be called "early weaving". This early weaving is not much different from the weaving concept mentioned above. The main difference in performance is that the simulation design tool is embedded in the modeling tool as a plug-in, and no longer explicitly generates interface operation codes, but directly enters the weaving link, such as Figure 1 However, this "early weaving" approach binds the model simulation code to the interface operation code, making it unsuitable for collaboration with multiple modeling tools and lacking versatility. Furthermore, this early weaving approach, which is primarily based on source code, also presents a series of tool-related difficulties, such as compilation and linking.
[0005] Compared with the early weaving, there is also the corresponding "late weaving", such as Figure 2As shown, the advantage of this late-stage weaving technique is that the interface operation code does not affect the logical framework of the model simulation code. However, before the emergence of the FMI standard, early-stage weaving was the common approach. This was due to several insurmountable difficulties with late weaving, including: 1) insufficient information required for weaving; 2) difficulty controlling the compilation environment; 3) difficulty linking; and 4) difficulty maintaining compatibility with simulation execution tools. With the advent of the universal model interface standard FMI (Functional Mock-up Interface), simulation execution tools have become more powerful, making script-based weaving feasible and providing the foundation for overcoming these difficulties.
[0006] In the prior art, patent application number CN202011504184.7 (Patent 1) discloses a program call tracing method, device, and storage medium, and patent application number CN202210395650.5 discloses a code processing method and device, storage medium, and electronic device (Patent 2). Although both mention code weaving technology, the code weaving technology in Patent 1 solves the problem of intra-instance and cross-process call tracing. It weaves code according to the weaving points and tracing declarations of the source program code, and adds related call information during the weaving process. Patent 2 creates a target class through code weaving to enable different classes to know the association at compile time, and to achieve intelligent prompts during the code editing process. Neither of them involves the problem of multi-model simulation code collaboration. Summary of the Invention
[0007] Aiming at the problem that multiple different model simulation codes have difficulty in coordinating during semi-physical simulation, the present invention proposes a code weaving method for multi-model collaborative simulation. This method is based on the FMI standard and achieves efficient collaboration of multi-model simulation codes through the comprehensive use of script and binary code weaving.
[0008] In order to achieve the above-mentioned object of the invention, the technical solution of the present invention is as follows: A code weaving method for multi-model collaborative simulation, comprising: Step S1: Generate an FMU file that complies with the FMI standard using a modeling tool, wherein the FMU file includes model simulation code and model interface information; Step S2: combining the design information of the simulation design tool with the model interface information of the FMU file to generate a binding table of the model logic interface of the interface operation code and the physical interface of the simulation machine; Step S3: weaving the FMU file containing the model simulation code with the interface binding table to form a semi-physical simulation code, and downloading it to the simulation execution tool; code weaving includes: Generate the initialization part of the interface operation code according to the interface binding table and weave it in. The weaving point is before the initialization function of the model simulation code. Generate the connection part of the interface input code according to the model interface information and interface binding table, and weave it in. The weaving point is before the step function of the model simulation code. Generate the connection part of the interface output code according to the model interface information and interface binding table, and weave it in. The weaving point is after the step function of the model simulation code. Generate the closing part of the interface operation code according to the model interface information and weave it in. The weaving point is after the release function of the model simulation code. Step S4: Execute hardware-in-the-loop simulation using a simulation execution tool.
[0009] Furthermore, the FMU file that complies with the FMI standard is generated using a modeling tool, and the modeling tool includes MATLAB-Simulink, Ansys CFX, CATIA, NI-LabVIEW, and MS-Excel.
[0010] Furthermore, the interface binding table contains paired values, each pair containing a model interface information and a physical interface information; the model interface information is referenced from the model interface description file in the FMU file, and the physical interface information references the physical interface description file preset in the simulation design tool.
[0011] Furthermore, when the code is woven, the interface binding table is generated as an XML file; when the interface binding table is downloaded to the simulation execution tool, it is transformed into a memory mapping table.
[0012] Furthermore, the weaving of the initialization part of the interface operation code is implemented by the following method: the initialization function of the model simulation code is encapsulated as a script function, and the call script statement of the interface operation code initialization part function code is inserted at the head of the script function, and the initialization entry function of the model simulation code is inserted at the tail.
[0013] Furthermore, to weave the initialization part of the interface operation code, the following function code should be inserted before the initialization entry function of the model simulation code: Set the initial values of all inputs and outputs; Load and dynamically link all interface operation code function parts used; Convert the interface binding table into a memory mapping table; Initialize all physical interfaces according to the interface binding table.
[0014] Furthermore, the weaving of the interface input part and the interface output part is implemented by the following method: The single-step call entry function of the model simulation code is encapsulated as a script function; the function call statement of the interface input part is inserted into the head of the script function; the model simulation code single-step call entry function call statement is inserted into the middle of the script function; and the function call statement of the interface output part is inserted into the tail of the script function.
[0015] Furthermore, the weaving of the interface input part requires inserting the following function code call statement before the single-step call entry function of the model simulation code: Read the physical input interface; Write to the model input interface; The above operations iterate according to the interface binding table.
[0016] Furthermore, the weaving of the interface output part requires inserting the following function code call statement after the single-step call entry function of the model simulation code: Read model output interface; Write to the physical output interface; The above operations iterate according to the interface binding table.
[0017] Furthermore, the weaving of the closing part of the interface operation code is implemented by the following method: the closing call entry function of the model simulation code is encapsulated as a script function; the closing call entry function call statement of the model simulation code is inserted into the head of the script function; and the interface operation code closing part function code call statement is inserted into the tail of the script function.
[0018] Furthermore, the weaving of the closing part of the interface operation code requires inserting the following function code after the closing call entry function of the model simulation code itself: Shut down all physical interfaces in the reverse order of the interface binding table; Unloads all dynamically linked slice code.
[0019] In summary, the present invention has the following advantages: 1. When weaving the code, the present invention divides the interface operation code into a functional part and a connection part. The functional part of the interface operation code is a binary code, which is loaded in a pre-compiled and dynamically linked manner; the connection part of the interface operation code is embedded in the target code by using a script generation and interpreted execution method. This weaving method comprehensively utilizes script-based and binary-based weaving methods, maintains the structured characteristics of the code, provides sufficient detection entries, and can be used in all modeling tools that support the FMI standard, with good versatility.
[0020] 2. The code weaving method proposed in the present invention can bind the input and output ports of the model with actual physical signals, thereby realizing the semi-physical simulation operation when multiple heterogeneous models are distributed on multiple hosts.
[0021] 3. The code generated by the method of the present invention can meet the requirements of the general simulation architecture and is applicable to multiple business fields, multiple simulation working conditions, multiple modeling tools, multiple operating systems, and various types of hardware boards.
[0022] 4. The code generated by the method of the present invention can meet the time limit requirements of the semi-physical simulation on the step length, and complete the I / O operation and data maintenance within one simulation step length.
[0023] 5. The code generated by the method of the present invention can maintain a clear structure and can be reviewed, checked and tested. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 Schematic diagram of the "early weaving" method used in hardware-in-the-loop simulation; Figure 2 Schematic diagram of the "late-stage weaving" method used for hardware-in-the-loop simulation; Figure 3 Schematic diagram of the output and reference of FMU files; Figure 4 The introduction process of FMU; Figure 5 Schematic diagram for integrating FMU into the framework; Figure 6 A flowchart of the operation of the present invention is provided; Figure 7 This is a schematic diagram of the composition of the interface binding table; Figure 8 The figure is a schematic diagram of code after code weaving using the method of the present invention. DETAILED DESCRIPTION
[0025] In order to more clearly illustrate the present invention, the present invention is further described below in conjunction with preferred embodiments and drawings. Those skilled in the art should understand that the following specific description is illustrative rather than restrictive and should not be used to limit the scope of protection of the present invention.
[0026] In a general architecture for dynamic simulation on a lower-level computer, code generation and weaving techniques are required to link simulation models with real physical signals. Given the numerous challenges with current weaving techniques and the increasing adoption of the FMI standard, this paper proposes a code weaving method for multi-model collaborative simulation. This architecture primarily relies on the FMI standard for post-weaving. After model simulation code is generated, the interface operation code is then weaved in scripts in multiple stages, enabling hardware-in-the-loop simulation based on mathematical models.
[0027] Before introducing the specific implementation steps of this solution, we first give an overview of the principles of FMI. The FMI standard is the general model interface standard FMI (Functional Mock-up Interface), which provides structured code weaving conditions to standardize the interface between modeling tools and simulation environments. Its core carrier is the FMU file. The full name of FMU is Functional Mockup Unit, which is specifically a compressed file created and output by the modeling tool. It contains model descriptions, code generated according to the model, and solvers, etc., which are introduced and used by the simulation environment. The output and reference of FMU files are conceptually as follows: Figure 3 As shown in the figure, the solver is included in the FMU file, which is a feature of the co-simulation method selected for this architecture.
[0028] like Figure 4 As shown in Figure 1, the introduction process of FMU generally includes three main steps: FMU integration, code generation, and simulation operation. Obviously, FMU does not contain information and code related to hardware board operation. These need to be interconnected to form a closed loop and complete the semi-physical simulation operation. This architecture uses code generation and weaving technology to achieve integration. Figure 5 The figure shows the integration of the FMU into the framework. As can be seen from the figure, the binding table and the slave computer's board I / O operations are external to the FMU. Through code generation and weaving, they are connected to the simulation program to form a complete hardware-in-the-loop simulation program. The core element of the integration process is the binding table. As can be seen from the figure, generating the binding table requires two pieces of information. First, the model input and output information from the FMU model description file. This information enables the host computer software to determine the model's input and output requirements. Second, the hardware configuration information from the host computer software, primarily the quantity, type, and specifications of the slave computer's hardware resources. This information allows the host computer software to determine whether the model's input and output requirements can be met and propose a configuration plan. With complete information and matching hardware resources, the host computer software can generate the binding table based on a specific algorithm. This work is completed during the design phase. The host computer software design will not be discussed here.
[0029] The binding table is sent to the general architecture of the dynamic simulation of the lower computer during the run preparation phase. Input and output codes are generated and woven into the architecture to implement the connection relationship specified by the binding table, thereby performing signal interaction with the real device. Figure 5 Steps 1, 2, 3, and 6 are all related to the integration of the FMU. The entire information flow closed loop is summarized as follows: 1——Set model input parameters through the FMI interface function fmi2SetX; 2——Get model output parameters through FMI interface function fmi2GetX; 3——Complete the conversion from engineering quantity to physical quantity (ICD signal) through the generated weaving code; 4——Send the ICD signal to the real physical device through ICD package output; 5——Receive and unpack through ICD to obtain ICD signal (physical quantity); 6——Complete the conversion from physical quantity to engineering quantity through the generated weaving code.
[0030] Through these mechanisms at design time and runtime, the FMU can be integrated into the general architecture of the lower computer dynamic simulation.
[0031] Refer to the instruction manual Figure 6 , the specific steps of a code weaving method for multi-model collaborative simulation proposed by the present invention are described below.
[0032] Step 1: Generate an FMU file that complies with the FMI standard using the modeling tool. The FMU file contains model simulation code, model interface information, etc.
[0033] In this step, the generation of FMU files that comply with the FMI standard depends on the functional implementation of the modeling tool itself. Commonly used modeling tools include MATLAB-Simulink, Ansys CFX, CATIA, NI-LabVIEW, MS-Excel, etc.
[0034] Step 2: The simulation design tool combines the design information with the model interface information contained in the FMU file to generate the "model logical interface and simulation machine physical interface binding table" part of the interface operation code (hereinafter referred to as the "interface binding table").
[0035] like Figure 7 As shown in the figure, the interface binding table mainly consists of pairs of values, each pair containing a model interface information and a physical interface information. The model interface information is referenced from the model interface description file in the FMU file, and the physical interface information is referenced from the physical interface description file preset in the simulation design tool. The physical interface description file preset in the simulation design tool is preset during deployment.
[0036] Preferably, when the code is woven, the interface binding table is generated as an XML file. When the interface binding table is downloaded to the simulation execution tool, it is converted into a memory mapping table for fast retrieval and becomes part of the woven code.
[0037] Step 3: Weave the FMU file containing the model simulation code and the interface binding table into the code to form the semi-physical simulation code, and download it to the simulation execution tool.
[0038] Specifically, the code weaving process includes: a) Generate the initialization part of the interface operation code according to the interface binding table and weave it in. The weaving point is before the initialization function of the model simulation code.
[0039] The model simulation code has its own initialization entry function. To weave the initialization part of the interface operation code, insert the following function code before the initialization entry function of the model simulation code: ①Set the initial values of all inputs and outputs; ② Load and dynamically link all interface operation code function parts used; ③Convert the interface binding table into a memory mapping table; ④ Initialize all physical interfaces according to the interface binding table, including opening the board, setting the working status, etc.
[0040] The specific implementation method is: the initialization entry function of the model simulation code is encapsulated as a script function, and the call script statement of the initialization part function code of the above-mentioned interface operation code is inserted at the head of the script function, and the initialization entry function of the model simulation code is inserted at the tail.
[0041] It should be noted that there is no real-time restriction on the initialization part, so all miscellaneous work before the simulation step is started is arranged to be completed here.
[0042] b) Generate the connection part of the interface input code according to the model interface information and the interface binding table, and weave it in. The weaving point is before the step function of the model simulation code.
[0043] The model simulation code has its own single-step call entry function. The interface input part needs to insert the following function code before the single-step call entry function of the model simulation code: ①Read the physical input interface; ②Write into the model input interface; ③The above operations are iterated according to the interface binding table.
[0044] c) Generate the connection part of the interface output code according to the model interface information and the interface binding table, and weave it in. The weaving point is after the step function of the model simulation code.
[0045] In this step, the interface output part needs to insert the following function code after the single-step call entry function of the model simulation code: ①Read model output interface; ②Write to the physical output interface; ③The above operations are iterated according to the interface binding table.
[0046] The specific implementation method is: The model simulation code single-step call entry function is encapsulated as a script function; the function call statement of the interface input part is inserted into the head of the script function; the call statement of the model simulation code single-step call entry function is inserted into the middle of the script function; and the function call statement of the interface output part is inserted into the tail of the script function.
[0047] All the script statements here are simple transfer calls, which do not affect the real-time performance of the simulation single-step function.
[0048] d) Generate the closing part of the interface operation code according to the model interface information and weave it in. The weaving point is after the release function of the model simulation code.
[0049] The model simulation code has its own closing call entry function. To weave the closing part of the interface operation code, the following function code needs to be inserted after the closing call entry function of the model simulation code: ① Close all physical interfaces in the reverse order of the interface binding table, including resetting the working status and shutting down the board; ② Uninstall all dynamically linked slice codes.
[0050] The specific implementation method is: the closing call entry function of the model simulation code is encapsulated as a script function; the closing call entry function call statement of the model simulation code is inserted at the head of the script function; and the function code call statement of the closing part of the above-mentioned interface operation code is inserted at the end of the script function.
[0051] FMI clarifies the weaving points of each code. The present invention realizes weaving by packaging and replacing the weaving point entries of the FMI standard. On this basis, the present invention uses a mixture of script-based weaving and binary code-based weaving technologies instead of source code-based weaving. Specifically, the interface operation code (i.e., slice code) is divided into a functional part and a connection part. The functional part of the interface operation code is binary code, which is loaded in a pre-compiled and dynamically linked manner; the connection part of the interface operation code is embedded in the target code by script generation and interpreted execution. In a semi-physical simulation environment, from the perspective of weaving operations, the model simulation code in the FMU file provided by the simulation modeling tool is the target code, and the interface operation code provided by the simulation running tool is the slice code.
[0052] In this embodiment, during the initialization phase, the binary portion of all interface operation codes is connected to the script via FFI / JIT. This architecture uses the Lua JIT (Lua, Just In Time compiler) engine, which is the foundation for the introduction of FFI. JIT here refers to just-in-time compilation, which compiles Lua scripts into native CPU code at runtime.
[0053] FFI is a mechanism provided by Lua JIT that allows Lua code to directly call C functions and access C data structures without wrapping the C code into LuaC API functions. This greatly enhances Lua's capabilities in system programming, high-performance computing, and integration with existing C libraries.
[0054] Because the JIT compiler has an extremely deep understanding of the local CPU and its architecture, it can understand the binary code generated by compiled languages. The FFI leverages LuaJIT's understanding of the underlying machine code and memory layout. When C data types and function signatures are defined in a script, LuaJIT's FFI generates the corresponding machine code based on these definitions to correctly handle data and function calls. For example, when defining a C structure, the FFI allocates memory according to the memory layout rules of the C language structure. This is achieved by precisely controlling the underlying memory model.
[0055] Furthermore, when calling C functions, FFI correctly handles parameter passing and return value reception based on the function signature and return value type. It converts Lua data types to C-compatible data types and converts C return values back to Lua data types after the function call completes. This process is accomplished by LuaJIT's internal FFI mechanisms.
[0056] In this architectural scenario, FFI provides a simple and efficient way for Lua scripts to seamlessly interact with simulation code written in an external compiled language, taking full advantage of the rich library ecosystem and high performance of compiled languages.
[0057] After this weaving method of the present invention, the final code is a functional block, which is connected by a simple script. Here, the script is not used to process the key content of the simulation such as simulation step size and functional logic. The script only plays the role of "glue", "sticking" the code together to form a whole. Figure 8 shown.
[0058] Step 4: Use the simulation execution tool to perform semi-physical simulation. The simulation execution tool is pre-installed with an interpreter executor, physical interface input and output parts, etc., providing an execution basis for the semi-physical simulation code.
[0059] The above description is only a preferred embodiment of the present invention and does not limit the present invention in any form. Any simple modification or equivalent change made to the above embodiment based on the technical essence of the present invention shall fall within the scope of protection of the present invention.
Claims
1. A code weaving method for multi-model collaborative simulation, characterized in that: include: Step S1: Generate an FMU file that complies with the FMI standard using a modeling tool, wherein the FMU file includes model simulation code and model interface information; Step S2: combining the design information of the simulation design tool with the model interface information of the FMU file to generate a binding table of the model logic interface of the interface operation code and the physical interface of the simulation machine; Step S3, weaving the FMU file containing the model simulation code with the interface binding table to form a semi-physical simulation code, and downloading it to the simulation execution tool; The code weaving process includes: Generate the initialization part of the interface operation code according to the interface binding table and weave it in. The weaving point is before the initialization function of the model simulation code. Generate the connection part of the interface input code according to the model interface information and interface binding table, and weave it in. The weaving point is before the step function of the model simulation code. Generate the connection part of the interface output code according to the model interface information and interface binding table, and weave it in. The weaving point is after the step function of the model simulation code. Generate the closing part of the interface operation code according to the model interface information and weave it in. The weaving point is after the release function of the model simulation code. Step S4: Execute hardware-in-the-loop simulation using a simulation execution tool.
2. A code weaving method for multi-model collaborative simulation according to claim 1, characterized in that: The FMU file that complies with the FMI standard is generated using modeling tools, and the modeling tools include MATLAB-Simulink, Ansys CFX, CATIA, NI-LabVIEW, and MS-Excel.
3. The code weaving method for multi-model collaborative simulation according to claim 1, characterized in that: The interface binding table contains paired values, each pair containing a model interface information and a physical interface information; the model interface information is referenced from the model interface description file in the FMU file, and the physical interface information is referenced from the physical interface description file preset in the simulation design tool.
4. The code weaving method for multi-model collaborative simulation according to claim 1, characterized in that: When the code is woven, the interface binding table is generated as an XML file; when the interface binding table is downloaded to the simulation execution tool, it is converted into a memory mapping table.
5. The code weaving method for multi-model collaborative simulation according to claim 1, characterized in that: The weaving of the initialization part of the interface operation code is implemented by the following method: the initialization function of the model simulation code is encapsulated as a script function, and the call script statement of the interface operation code initialization part function code is inserted at the head of the script function, and the initialization entry function of the model simulation code is inserted at the tail.
6. A code weaving method for multi-model collaborative simulation according to claim 1 or 5, characterized in that: To weave the initialization part of the interface operation code, insert the following function code call statement before the initialization entry function of the model simulation code: Set the initial values of all inputs and outputs; Load and dynamically link all interface operation code function parts used; Convert the interface binding table into a memory mapping table; Initialize all physical interfaces according to the interface binding table.
7. The code weaving method for multi-model collaborative simulation according to claim 1, characterized in that: The weaving of the interface input part and the interface output part is achieved by the following method: The single-step call entry function of the model simulation code is encapsulated as a script function; the function call statement of the interface input part is inserted into the head of the script function; the model simulation code single-step call entry function call statement is inserted into the middle of the script function; and the function call statement of the interface output part is inserted into the tail of the script function.
8. A code weaving method for multi-model collaborative simulation according to claim 1 or 7, characterized in that: The weaving of the interface input part requires inserting the following function code call statement before the single-step call entry function of the model simulation code: Read the physical input interface; Write to the model input interface; The above operations iterate according to the interface binding table.
9. A code weaving method for multi-model collaborative simulation according to claim 1 or 7, characterized in that: The weaving of the interface output part requires inserting the following function code call statement after the single-step call entry function of the model simulation code: Read model output interface; Write to the physical output interface; The above operations iterate according to the interface binding table.
10. The code weaving method for multi-model collaborative simulation according to claim 1, characterized in that: The weaving of the closing part of the interface operation code is implemented by the following method: the closing call entry function of the model simulation code is encapsulated as a script function; the closing call entry function call statement of the model simulation code is inserted into the head of the script function; and the function code call statement of the closing part of the interface operation code is inserted into the tail of the script function.
11. A code weaving method for multi-model collaborative simulation according to claim 1 or 10, characterized in that: The weaving of the closing part of the interface operation code requires inserting the following function code after the closing call entry function of the model simulation code itself: Shut down all physical interfaces in the reverse order of the interface binding table; Unloads all dynamically linked slice code.
Citation Information
Patent Citations
Program call tracking method and device and storage medium
CN112558947A
Code Processing Method, Device, Storage Medium, and Electronic Device
CN114840274B