Modelica simulation component generation method based on Matlab language
Through the dynamic library encapsulation method of Matlab language, Modelica components are automatically generated, which solves the difficulty of writing external language functions and the use of different compilers, and achieves efficient Modelica simulation component generation and cross-language modeling simulation.
Patent Information
- Application Number
- CN202210365973.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-04-13
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2041-04-13
AI Technical Summary
In the existing technology, when using the Modelica language for system simulation, the writing of external language functions requires professional knowledge, which is difficult and inefficient. Moreover, when selecting different compilers, the source code of the external functions needs to be obtained, which leads to the problem of modeling failure.
The dynamic library using Matlab language is converted to generate Modelica components through secondary encapsulation method to achieve compiler compatibility and interface conversion, automatically generate Modelica components in batches, and avoid the need to obtain source code.
It improves the efficiency of Modelica simulation component generation, simplifies the process of cross-language modeling and simulation, solves the problem of using different compilers at the same time, and realizes cross-language simulation on the Modelica platform.
Smart Images

Figure CN114721776B
Abstract
Description
Technical Field
[0001] The invention belongs to the field of computer simulation, and in particular relates to a Modelica simulation component generation method in Matlab language. Background Art
[0002] Modelica is an open source multi-domain unified modeling language that has object-oriented characteristics and non-causal modeling capabilities, good interactivity and strong model expressiveness, and is widely used in multi-domain simulation fields.
[0003] When using Modelica for system simulation, you can use the Modelica language to model or use external language functions written in C or Fortran. When modeling external language functions, the common method is to write a Modelica function with the same interface as the original external language function according to the Modelica language specification, map the parameter type according to the parameter mapping rules, and call the external language function through the dynamic library method through the external keyword to implement the implementation of the Modelica function. At present, this manual method has two problems: first, it requires programmers with professional knowledge to write code, which is difficult to operate and inefficient; second, the Modelica language itself can use Mingw or vs compiler to compile C language source code. When the selected compiler is different from the compiler used to make the external function dynamic library, the conventional method is to obtain the source code of the external function and then use the same compiler as Modelica to regenerate the external function dynamic library, otherwise the modeling will fail. Summary of the invention
[0004] The purpose of the present invention is to provide a Modelica simulation component generation method of Matlab language in view of the above problems, which automatically converts the dynamic library written in Matlab language by using the secondary packaging method to generate Modelica components, replacing manual work, and solving the problem that the external function dynamic library compiled by non-Mingw compiler cannot be used in the Modelica environment where the Mingw compiler is selected, avoiding the need to obtain source code; the conversion process is carried out in batches, greatly improving the conversion efficiency.
[0005] The present invention combines dynamic library conversion and mo file description to realize making Matlab functions provided in the form of dynamic libraries into Modelica components, encapsulates the original dynamic library functions by generating new dynamic libraries, and realizes compiler compatibility and interface conversion.
[0006] The technical solution of the present invention is a method for generating Modelica simulation components in Matlab language, comprising the following steps:
[0007] Step 1: Get the dynamic libraries and Matlab functions to be converted one by one from the specified folder;
[0008] Step 2: Analyze and obtain the return value type, function name, and function parameter list of each Matlab function;
[0009] Step 3: Generate project files and source code files;
[0010] Step 4: Generate an external function that complies with the Modelica usage specification in the code file for each Matlab function;
[0011] Step 4.1: Add the include of the original dynamic library header file in the source code file;
[0012] Step 4.2: For each original dynamic library export function prototype output by step 2, generate a new function prototype in the source code, the return value of the new function is an integer; the new function name is converted from the name of the export function corresponding to the original dynamic library; the parameter list of the new function is formed by adding the return value parameter of the original function to the parameter list of the original function;
[0013] Step 4.3: Generate a function body consisting of curly brace pairs;
[0014] Step 4.4: Generate execution statements in the function body;
[0015] Step 5: Compile the project generated in step 3 into a dynamic library file;
[0016] Step 6: Create the mo file corresponding to the external language function, introduce the external function into the Modelica function, and generate the Modelica component.
[0017] Step 1 uses the default mode or configuration file mode to obtain the dynamic library and Matlab function to be converted. When the default mode is used, the dynamic library files with the extension dll in the specified folder are traversed. For each dynamic library file, a header file with the same file name as the main file name of the dynamic library file and the extension h is searched in the current folder. If there is no header file, an error is recorded and the next dynamic library file is processed until all dynamic library files are processed. When the configuration file mode is used, the configuration file is read, and the dynamic library files and corresponding header files are read in sequence according to the file names recorded in the configuration file. For each header file, the comments are removed by text analysis, and then the identifier list is obtained by analyzing according to the identifier extraction state transition diagram method, and then the Matlab function prototype is obtained by reading in reverse order from the list.
[0018] Step 2 specifically includes the following sub-steps:
[0019] Step 2.1: First, get the function's return value type and function name from the identifier list;
[0020] Step 2.2: Starting from the left bracket, analyze the state transition diagram according to the function parameters to determine the function's parameter list including parameter types and parameter names.
[0021] Preferably, the function parameter analysis state transition diagram includes a state set {0, 1, 2, 3, 5, 7, 8, 9, 11, 12, 13, 22}, and state 0 is the initial state.
[0022] The state processing flow of function parameter analysis includes:
[0023] 1) In state 0, input a space to keep state 0; input a letter to enter state 1; input ")" to enter state 8; otherwise enter state 9;
[0024] 2) In state 1, input letters or numbers to continue state 1; input space to enter state 2; input "*" to enter state 3; input "," to enter state 5; input ")" to enter state 8; input "(", ";" or other to enter state 9;
[0025] 3) In state 2, input a space to continue state 2, input a letter to enter state 11; input "*" to enter state 3; input "," to enter state 5; input ")" to enter state 8; input other inputs to enter state 9;
[0026] 4) In state 3, input "*" or space to continue state 3; input "," to enter state 5; input ")" to enter state 8; input letters to enter state 12; input other characters to enter state 9;
[0027] 5) In state 5, input a space to keep in state 5; input a letter to enter state 1; input other inputs to enter state 9;
[0028] 6) In state 7, enter a space to continue state 7; enter other keys to enter state 9;
[0029] 7) In state 8, input a space to continue state 8; input “;” to enter state 7; input other inputs to enter state 9;
[0030] 8) In state 9, return -1 and end the state processing flow;
[0031] 9) In state 11, input letters or numbers to continue state 11; input space to enter state 22; input "," to enter state 5; input ")" to enter state 8; input other to enter state 9;
[0032] 10) In state 12, input letters or numbers to continue state 12; input space to enter state 13; input "," to enter state 5; input ")" to enter state 8; input other to enter state 9;
[0033] 11) In state 13, input space to continue state 13; input "," to enter state 5; input ")" to enter state 8; input other inputs to enter state 9;
[0034] 12) In state 22, input a space to continue to maintain state 22; input a letter to enter state 11; input “,” to enter state 5; input “)” to enter state 8; input other inputs to enter state 9.
[0035] Step 3 specifically includes the following sub-steps:
[0036] Step 3.1: Generate Mingw standard project files programmatically;
[0037] Step 3.2: Generate a C language source code file. The source code file name is converted from the original dynamic library file name; and add the source code file to the project generated in step 3.1.
[0038] Step 6 specifically includes the following sub-steps:
[0039] Step 6.1: Generate Modelica language function names based on external function names, and generate function name prefixes according to certain rules to avoid duplicate names;
[0040] Step 6.2: Map the input and output types and data types of the parameters to the input and output of the Modelica language and the corresponding data types according to the language specification;
[0041] Step 6.3: Generate the function name, dynamic library file path and header file path of the external reference, and generate the component installation boot file in XML format. The relevant dynamic library files, static library files, mo files, and xml files are uniformly output to the same folder and whether to compress and package them is determined according to application needs.
[0042] In step 4.4, for the dynamic library using the mwArray data type, first generate a temporary variable of the mwArray type for each parameter in the newly generated function parameter list in the function body, and assign values to the newly generated temporary variables using different assignment methods according to the parameter combination type, such as common type and array type; use the temporary variable to call the original dynamic library function, and for the mwArray type variable as the call result, reverse convert the mwArray into the parameter type of the function prototype and return it.
[0043] Compared with the prior art, the beneficial effects of the present invention include:
[0044] 1) The present invention provides an automatic conversion method and system for batch-generating Modelica components from dynamic libraries written in Matlab language, which is convenient for computer execution, replaces manual work, saves time and effort, greatly improves conversion efficiency, and facilitates cross-language modeling and simulation on the Modelica simulation platform;
[0045] 2) The present invention solves the problem that due to the limitation of Modelica's own interface, the compiler selected by the Modelica simulation system and the compiler used for making the Matlab function dynamic library are different and difficult to convert at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.
[0047] Figure 1 Schematic diagram of a system for batch generating Modelica components according to an embodiment of the present invention.
[0048] Figure 2 Schematic diagram of input and output of the Modelica component batch generation system according to an embodiment of the present invention.
[0049] Figure 3 This is a state transition diagram for extracting an identifier according to an embodiment of the present invention.
[0050] Figure 4 This is a state transition diagram for function parameter analysis according to an embodiment of the present invention.
[0051] Figure 5 Schematic diagram of a configuration file according to an embodiment of the present invention.
[0052] Figure 6 Schematic diagram of the header file content according to an embodiment of the present invention. DETAILED DESCRIPTION
[0053] Embodiment 1
[0054] like Figure 1 As shown, the Modelica component batch generation system includes:
[0055] Extract components in batches, and obtain original files such as dynamic libraries and header files to be processed in batches from folders;
[0056] The function analysis component analyzes the return value type, function name, and function parameter list of each exported function of the corresponding dynamic library from the header file to be processed. The function parameter list includes the input and output type of the parameter, the parameter data type, and the parameter name;
[0057] The project generation component generates a new external dynamic library project that complies with the Modelica specification. This project implements the encapsulation call of the original dynamic library.
[0058] The function generation component generates an external function that complies with the Modelica usage specification. The generated external function will have different prototypes and function implementation codes according to the different languages used by the original dynamic library.
[0059] The project compilation component automatically calls the system compiler to compile the newly generated project and source code files into a dynamic library that complies with the Modelica usage specifications, completing the compilation of the dynamic library project and source code;
[0060] Modelica component generation parts, create corresponding mo files for external functions, and generate Modelica components.
[0061] The method for generating Modelica simulation components in Matlab language of the above system comprises the following steps:
[0062] Step 1: Get the dynamic libraries and Matlab functions to be converted one by one from the specified folder;
[0063] Use the default mode to obtain the dynamic library and Matlab function to be converted. The folder specified by the user contains one or more dynamic library files to be processed and their corresponding header files. In the default mode, traverse the dynamic library files with the extension dll in the folder. For each dynamic library file, search in the current folder for a header file with the same file name as the main file of the dynamic library file and the extension h. The header file content is as follows: Figure 6 If not, an error is recorded and the next dynamic library file is processed until all dynamic library files are processed.
[0064] Step 2: Analyze and obtain the return value type, function name, and function parameter list of each Matlab function;
[0065] Step 2.1: First, get the function's return value type and function name from the identifier list;
[0066] Step 2.2: Start from the left bracket and follow Figure 4 The function parameter analysis state transition diagram shown determines the parameter list of the function including parameter types and parameter names.
[0067] Step 3: Generate project files and source code files;
[0068] Step 3.1: Generate Mingw standard project files programmatically;
[0069] Step 3.2: Generate a C language source code file. The source code file name is converted from the original dynamic library file name; and add the source code file to the project generated in step 3.1.
[0070] Step 4: Generate an external function that complies with the Modelica usage specification in the code file for each Matlab function;
[0071] Step 4.1: Add the include of the original dynamic library header file in the source code file;
[0072] Step 4.2: For each original dynamic library export function prototype output by step 2, generate a new C language function prototype in the source code. The return value type of the new function is an integer int type. When the original function is successfully called, 0 is returned. If the call is unsuccessful, an integer value indicating an error code is returned. The new function name is converted from the name of the export function corresponding to the original dynamic library.
[0073] The input and output parameters related to the function are put into the parameter list of the new function, and the input parameters are modified by const before the variable.
[0074] Step 4.3: Generate a function body consisting of curly brace pairs;
[0075] Step 4.4: Generate execution statements within the function body.
[0076] Step 5: Call the Mingw compiler to compile the newly created project and source code files to generate new dynamic library files and static library files.
[0077] Step 6: Create the mo file corresponding to the external language function, introduce the external function into the Modelica function, generate the Modelica component, and the component file uses annnotation to call the original dynamic library function through the newly created dynamic library function;
[0078] Step 6.1: Generate Modelica language function names based on external function names. To avoid duplicate names, generate function name prefixes according to certain rules.
[0079] Step 6.2: Map the input and output types and data types of the parameters to the input and output of the Modelica language and the corresponding data types according to the language specification;
[0080] Step 6.3: Generate the function name, dynamic library file path and header file path of the external reference; put the new dynamic library file, static library file, header file and original dynamic library file together with the mo file, so that it can be used in the Modelica language model.
[0081] In the folder specified in the embodiment, there are dynamic libraries A.dll, B.dll, C.dll and corresponding header files CHeadForA.h, CHeadForB.h, CHeadForC.h. In the CHeadForA.h file, there are declarations of functions such as AFUN1, AFUN2, AFUN3. Similarly, in the CHeadForB.h and CHeadForC.h files, there are declarations of functions such as BFUN1, BFUN2, CFUN1, CFUN2. A.dll, A.lib, B.dll, B.lib, C.dll, C.lib, CHeadForA.h, CHeadForB.h, CHeadForC.h and related files are all stored in the directory DIR. In the simulation system based on the Modelica modeling and simulation platform, functions AFUN1, AFUN2, AFUN3, BFUN1, BFUN2, CFUN1, CFUN2, etc. need to be used.
[0082] like Figure 2 As shown, the directory DIR of the designated folder is passed as an input parameter to the Modelica component batch generation system of the present invention, and the Modelica component batch generation system batch generates Modelica components, and the Matlab function in the original dynamic library is used in each Modelica component in the form of an external function. The Modelica component can be used in the Modelica simulation system like a native function.
[0083] Embodiment 2
[0084] The second embodiment adopts the same Modelica component batch generation system as the first embodiment.
[0085] The method for generating Modelica simulation components in Matlab language includes the following steps:
[0086] Step 1: Get the dynamic libraries and Matlab functions to be converted one by one from the specified folder;
[0087] Use the configuration file mode to obtain the dynamic library and Matlab function to be converted. In the configuration file mode, read the configuration file, and read the dynamic library file and the corresponding header file in turn according to the file name recorded in the configuration file. Use text analysis to remove comments from each header file, and then follow Figure 3 The identifier extraction state transition diagram shown is used to analyze the identifier list, and then the Matlab function prototype is obtained by reading from the list in reverse order.
[0088] Step 2: Analyze and obtain the return value type, function name, and function parameter list of each Matlab function;
[0089] Step 2.1: First, get the function's return value type and function name from the identifier list;
[0090] Step 2.2: Start from the left bracket and follow Figure 4 The function parameter analysis state transition diagram shown determines the parameter list of the function including parameter types and parameter names.
[0091] Step 3: Generate project files and source code files;
[0092] Step 3.1: Generate Mingw standard project files programmatically;
[0093] Step 3.2: Generate a C language source code file. The source code file name is converted from the original dynamic library file name; and add the source code file to the project generated in step 3.1.
[0094] Step 4: Generate an external function that complies with the Modelica usage specification in the code file for each Matlab function;
[0095] Step 4.1: Add the include of the original dynamic library header file in the source code file;
[0096] Step 4.2: For each original dynamic library export function prototype output by step 2, generate a new C language function prototype in the source code. The return value type of the new function is an integer int type. When the original function is successfully called, 0 is returned. If the call is unsuccessful, an integer value indicating an error code is returned. The new function name is converted from the name of the export function corresponding to the original dynamic library.
[0097] The input and output parameters related to the function are put into the parameter list of the new function, and the input parameters are modified by const before the variable.
[0098] Step 4.3: Generate a function body consisting of curly brace pairs;
[0099] Step 4.4: Generate execution statements in the function body;
[0100] For dynamic libraries written in Matlab language that use the mwArray data type, a temporary variable of the mwArray type is first generated for each parameter in the newly generated function parameter list in the function body, and different assignment methods are used to assign values to the newly generated temporary variables according to the parameter combination type, such as common type and array type; the original dynamic library function is called with the temporary variable, and since the call result is a mwArray type variable, the mwArray needs to be reversely converted into the parameter type of the function prototype before returning.
[0101] Step 5: Call the Mingw compiler to compile the newly created project and source code files to generate new dynamic library files and static library files.
[0102] Step 6: Create the mo file corresponding to the external language function, introduce the external function into the Modelica function, generate the Modelica component, and the component file uses annnotation to call the original dynamic library function through the newly created dynamic library function;
[0103] Step 6.1: Generate Modelica language function names based on external function names. To avoid duplicate names, generate function name prefixes according to certain rules.
[0104] Step 6.2: Map the input and output types and data types of the parameters to the input and output of the Modelica language and the corresponding data types according to the language specification;
[0105] Step 6.3: Generate the function name, dynamic library file path and header file path of the external reference; put the new dynamic library file, static library file, header file and original dynamic library file together with the mo file, so that it can be used in the Modelica language model.
[0106] In the embodiment, in the original dynamic library folder to be packaged, prepare Figure 5 The text configuration file shown in the figure lists each dynamic library in the folder, the corresponding header file, and the compiler used. The encapsulation program reads the configuration file to encapsulate the dynamic libraries generated by different compilers in the same folder.
Claims
1. A method for generating Modelica simulation components in Matlab language, characterized in that: The following steps are involved: Step 1: Get the dynamic libraries and Matlab functions to be converted one by one from the specified folder; Step 2: Analyze and obtain the return value type, function name, and function parameter list of each Matlab function; Step 3: Generate project files and source code files; Step 4: Generate an external function that complies with the Modelica usage specification in the code file for each Matlab function; Step 4.1: Add the include of the original dynamic library header file in the source code file; Step 4.2: For each original dynamic library export function prototype output by step 2, generate a new function prototype in the source code, the return value of the new function is an integer; the new function name is converted from the name of the export function corresponding to the original dynamic library; the parameter list of the new function is formed by adding the return value parameter of the original function to the parameter list of the original function; Step 4.3: Generate a function body consisting of curly brace pairs; Step 4.4: Generate execution statements in the function body; Step 5: Compile the project generated in step 3 into a dynamic library file; Step 6: Create the mo file corresponding to the external language function, introduce the external function into the Modelica function, and generate the Modelica component.
2. The method for generating Modelica simulation components according to claim 1, characterized in that: Step 1 uses the default mode or configuration file mode to obtain the dynamic library and Matlab function to be converted; In the default mode, the dynamic library files with the extension dll in the specified folder are traversed. For each dynamic library file, a header file with the same file name as the main file name of the dynamic library file and the extension h is searched in the current folder. If there is no header file, an error is recorded and the next dynamic library file is processed until all dynamic library files are processed. When using the configuration file mode, read the configuration file, read the dynamic library file and the corresponding header file in turn according to the file name recorded in the configuration file, remove comments from each header file using text analysis, and then analyze according to the identifier extraction state transition diagram to obtain the identifier list, and then read from the list in reverse order to obtain the Matlab function prototype.
3. The method for generating Modelica simulation components according to claim 2, characterized in that: Step 2 includes: Step 2.1: First, get the function's return value type and function name from the identifier list; Step 2.2: Starting from the left bracket, analyze the state transition diagram according to the function parameters to determine the function's parameter list including parameter types and parameter names.
4. The method for generating Modelica simulation components according to claim 3, characterized in that: The function parameter analysis state transition diagram includes a state set {0, 1, 2, 3, 5, 7, 8, 9, 11, 12, 13, 22}, where state 0 is the initial state. The state processing flow of the function parameter analysis includes: 1) In state 0, input a space to keep state 0; input a letter in state 0 to enter state 1; input ")" in state 0 to enter state 8; in other cases in state 0 to enter state 9; 2) In state 1, input letters or numbers to continue state 1; input space in state 1 to enter state 2; input "*" in state 1 to enter state 3; input "," in state 1 to enter state 5; input ")" in state 1 to enter state 8; input "(", ";" or other characters in state 1 to enter state 9; 3) In state 2, input a space to continue state 2; input a letter in state 2 to enter state 11; input "*" in state 2 to enter state 3; input "," in state 2 to enter state 5; input ")" in state 2 to enter state 8; input other characters in state 2 to enter state 9; 4) In state 3, input "*" and space to continue state 3; input "," in state 3 to enter state 5; input ")" in state 3 to enter state 8; input letters in state 3 to enter state 12; input other characters in state 3 to enter state 9; 5) In state 5, input a space to continue in state 5; input a letter in state 5 to enter state 1; input other characters in state 5 to enter state 9; 6) In state 7, input a space to keep in state 7; input other characters in state 7 to enter state 9; 7) In state 8, input a space to continue state 8; in state 8, input ";" to enter state 7; in state 8, input other characters to enter state 9; 8) In state 9, return -1 and end the state processing flow; 9) In state 11, input letters or numbers to continue state 11; input space in state 11 to enter state 22; input "," in state 11 to enter state 5; input ")" in state 11 to enter state 8; input other characters in state 11 to enter state 9; 10) In state 12, input letters or numbers to continue state 12; input space in state 12 to enter state 13; input "," in state 12 to enter state 5; input ")" in state 12 to enter state 8; input other characters in state 12 to enter state 9; 11) In state 13, input a space to continue state 13; input "," in state 13 to enter state 5; input ")" in state 13 to enter state 8; input other characters in state 13 to enter state 9; 12) In state 22, input a space to continue state 22; input a letter in state 22 to enter state 11; input "," in state 22 to enter state 5; input ")" in state 22 to enter state 8; input other characters in state 22 to enter state 9.
5. The method for generating Modelica simulation components according to claim 4, characterized in that: Step 3 includes: Step 3.1: Generate Mingw standard project files programmatically; Step 3.2: Generate a C language source code file. The source code file name is converted from the original dynamic library file name; and add the source code file to the project generated in step 3.
1.
6. The method for generating Modelica simulation components according to claim 5, characterized in that: Step 6 includes: Step 6.1: Generate Modelica language function names based on external function names, and generate function name prefixes according to certain rules to avoid duplicate names; Step 6.2: Map the input and output types and data types of the parameters to the input and output of the Modelica language and the corresponding data types according to the language specification; Step 6.3: Generate the function name, dynamic library file path and header file path of the external reference, and generate the component installation boot file in XML format. The relevant dynamic library files, static library files, mo files, and xml files are uniformly output to the same folder and whether to compress and package them is determined according to application needs.
7. The method for generating Modelica simulation components according to claim 6, characterized in that: In step 4.4, for the dynamic library using the mwArray data type, first generate a temporary variable of the mwArray type for each parameter in the newly generated function parameter list in the function body, and assign values to the newly generated temporary variables using different assignment methods according to the parameter combination type; use the temporary variable to call the original dynamic library function, and for the mwArray type variable as the call result, reverse convert the mwArray into the parameter type of the function prototype and return it.
