A method and apparatus for serializing external object data in Modelica
By defining serialization and deserialization interface functions in the external object class definition, the problem of Modelica's external object data not being cached is solved, enabling efficient reading and writing of external object data and improving the debugging efficiency of complex models.
Patent Information
- Application Number
- CN202411744489.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-30
- Publication Date
- 2025-12-02
- Estimated Expiration
- 2044-11-30
AI Technical Summary
In existing technologies, Modelica external object data cannot achieve solver state caching, resulting in time-consuming and inefficient debugging of complex models, and making it impossible to debug models through non-simulation restart methods.
By defining serialization and deserialization interface functions in the external object class definition, the conversion and restoration of the state information of the external object data are realized. Serialization interface functions are provided to convert external object data into storable or transmittable byte data, and deserialization interface functions are provided to restore the byte data into the state information of the external object data. These interface functions are called during the solver state caching and restoration process.
It enables efficient reading and writing of external object data, reduces parsing difficulty, improves the debugging efficiency of complex Modelica models, and solves the problem of solver state caching that cannot be implemented when integrating external object models.
Smart Images

Figure CN119578104B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of system modeling and simulation technology, and in particular to a method and apparatus for serializing external object data in Modelica. Background Technology
[0002] Modelica is an object-oriented, multi-domain system modeling language. Its non-causal modeling, multi-domain support, and object-oriented features enable it to efficiently and flexibly describe complex systems involving multiple disciplines and coupled subsystems, making it an ideal choice for modeling and simulating complex dynamic systems. It is widely used in industries such as automotive, aerospace, energy, and construction. In practical modeling and simulation applications, complex system models described using Modelica often possess mathematical properties such as large equation size, strong nonlinearity, and rigidity, leading to low simulation efficiency, poor result accuracy, and iteration non-convergence. When simulation anomalies occur, modelers expect to debug the model within the anomaly time interval. This requires restarting the solver and simulating to the anomaly point before performing single-step debugging. The debugging process may require multiple iterations of restarting the simulation. If the model simulation efficiency is low and the anomaly occurs late, restarting the solver and simulating to the anomaly point is a very time-consuming process, severely reducing the continuity and efficiency of debugging work and affecting the model development schedule. To avoid the efficiency issues caused by the aforementioned simulation restarts, one approach is to pre-cache the solver state at a point before the abnormal event occurs, load the cached solver state during the debugging phase, and perform simulation and debugging based on this state, thereby avoiding simulation restarts.
[0003] To achieve the above objectives, the solver state cache program must be able to read model data and simulation data that fully characterize the solver state, so as to restore the simulation state at the time of the debugging phase. As an open physical modeling language, Modelica integrates models described by other languages through mechanisms such as standardized definitions of external functions and external objects. External objects are data memory managed by other languages and are invisible to the solver program. For example, the solver state cache program cannot access this data memory during the caching phase, nor can it restore this data memory during the state recovery phase. This means that complex Modelica models with integrated external objects can only be debugged by restarting the simulation. Therefore, implementing the serialization and deserialization of Modelica external object data is a problem that urgently needs to be solved. Summary of the Invention
[0004] In view of this, it is necessary to provide a method and apparatus for serializing Modelica external object data to solve the defect in the existing technology that complex Modelica models integrating external objects cannot implement solver state caching.
[0005] To address the aforementioned problems, in a first aspect, embodiments of the present invention provide a Modelica external object data serialization method, comprising:
[0006] An external object model for external object data is constructed. The external object model includes serialization interface functions and deserialization interface functions. The serialization interface functions are used to convert the state information of the external object data into storable or transmittable byte data, and the deserialization interface functions are used to restore the byte data to the state information of the external object data.
[0007] Select at least one target component simulation model built using the multi-domain unified modeling language Modelica from the preset model library. During the simulation, when the solver executes the state caching program and caches the external object data of the external object model, if it is determined that a serialization interface function exists, then the serialization interface function is called to retrieve the external object data. During the simulation, when the solver executes the state recovery program and restores the external object data of the external object model, if it is determined that a deserialization interface function exists, then the deserialization interface function is called to restore the external object data.
[0008] Preferably, the serialization interface function includes a first input parameter, a second input parameter, and an output parameter;
[0009] The first input parameter is used to receive external object data to be serialized;
[0010] The second input parameter is a string variable used to receive or provide array space for storing external object data during serialization, and to indicate the required array space size during output;
[0011] The first output parameter is used to return the success status of the serialization operation or the array space size required for the external object data;
[0012] The deserialization interface function includes a third input parameter for receiving external object data to be deserialized;
[0013] The fourth input parameter, a string variable, contains byte data used to recover the state information of the external object's data.
[0014] The second output parameter is used to return the success status of the deserialization operation.
[0015] Preferably, after constructing the external object model of the external object data, the method further includes:
[0016] Generate the serialization and deserialization interface functions into the external object registration function of the external object model;
[0017] If the external object class definition contains other function definitions besides the constructor and destructor, then text matching is used to determine whether they are serialization interface function and deserialization interface function definitions; if not, an error is reported.
[0018] If the external object class definition includes serialization and deserialization interface functions, then these functions will be generated into the external object registration function of the external object model code.
[0019] Preferably, after selecting at least one target component simulation model built using the multi-domain unified modeling language Modelica from a preset model library, the model further includes:
[0020] After the simulation starts, the solver calls the external object registration function during the instantiation phase to obtain the data memory address of the external object data, as well as the serialization interface function handle and the deserialization interface function handle.
[0021] Preferably, the solver records the data memory address of the external object data, and passes the data memory address as a parameter to the specific address when the external object registration function is called subsequently.
[0022] The external object registration function completes the registration process by constructing an external object instance, allocating a destructor handle, a serialization interface function handle, and a deserialization interface function handle, and assigning them to the corresponding member variables of the ExtObjectManager data structure.
[0023] Preferably, the solver also provides an external object management data structure, ExtObjectManager, for storing the data memory address, destructor handle, serialization interface function handle, and deserialization interface function handle of the external object data. The definition of the ExtObjectManager data structure includes:
[0024] The member variable `obj` is used to store the memory address of the external object's data.
[0025] The member variable dtor is a destructor handle used to release the memory of external object data;
[0026] The member variable `sel` is a handle to the serialization interface function, used to convert external object data into a byte sequence.
[0027] The member variable dsel is a handle to the deserialization interface function, used to recover external object data from a byte sequence.
[0028] Preferably, when caching the external object data of the external object model, if a serialization interface function is determined to exist, the serialization interface function is called to retrieve the external object data. During the simulation, when the solver executes the state recovery procedure, when restoring the external object data of the external object model, if a deserialization interface function is determined to exist, the deserialization interface function is called to restore the external object data, including:
[0029] During simulation, when the solver executes the state caching procedure to cache the target component simulation model data, for external object data in the target component simulation model, the solver checks whether there is a serialization interface function for the external object data; if the serialization interface function exists, the solver calls the serialization interface function to obtain and cache the external object data; if the serialization interface function does not exist, the solver outputs a warning message indicating that it cannot perform serialization operation on the external object data;
[0030] During the simulation, when the solver executes the state recovery procedure to restore the target component simulation model data, for the external object data in the target component simulation model, the solver checks whether there is a deserialization interface function for the external object data; if the deserialization interface function exists, the solver calls the deserialization interface function to restore the external object data; if the deserialization interface function does not exist, the solver outputs a warning message indicating that the external object data cannot be deserialized.
[0031] In a second aspect, embodiments of the present invention provide a Modelica external object data serialization apparatus, comprising:
[0032] The first module constructs an external object model for external object data. The external object model includes serialization interface functions and deserialization interface functions. The serialization interface functions are used to convert the state information of the external object data into storable or transmittable byte data, and the deserialization interface functions are used to restore the byte data to the state information of the external object data.
[0033] The second module selects at least one target component simulation model built using the multi-domain unified modeling language Modelica from the preset model library. During the simulation, when the solver executes the state caching program and caches the external object data of the external object model, if it is determined that a serialization interface function exists, the serialization interface function is called to retrieve the external object data. During the simulation, when the solver executes the state recovery program and restores the external object data of the external object model, if it is determined that a deserialization interface function exists, the deserialization interface function is called to restore the external object data.
[0034] Thirdly, the present invention also provides an electronic device, including a memory and a processor, wherein,
[0035] The memory is used to store programs;
[0036] The processor, coupled to the memory, is used to execute the program stored in the memory to implement the steps in the Modelica external object data serialization method as described in the first aspect embodiment of the present invention.
[0037] Fourthly, the present invention also provides a computer-readable storage medium for storing a computer-readable program or instructions, which, when executed by a processor, can implement the steps in the Modelica external object data serialization method as described in the first aspect embodiment of the present invention.
[0038] The Modelica external object data serialization method provided by this invention, compared with existing technologies, avoids confusion with other external object operation methods and reduces parsing difficulty by defining serialization and deserialization interface functions in the external object class definition. Furthermore, by defining the serialization and deserialization interface functions of the external object, the solver program can read and write external object data to memory. In engineering modeling and simulation applications, the use of external object models is widespread for purposes such as historical asset model reuse and intellectual property protection. This invention further improves the solver state caching function, solving the technical problem that complex Modelica models integrating external objects cannot achieve solver state caching, and significantly improving the debugging efficiency of problem models. Attached Figure Description
[0039] Figure 1 The flowchart of the Modelica external object data serialization method provided by the present invention;
[0040] Figure 2 This is a flowchart illustrating the external object data serialization process according to an embodiment of the present invention.
[0041] Figure 3 This is a flowchart illustrating the deserialization process of external object data according to an embodiment of the present invention.
[0042] Figure 4 This is a structural block diagram of the Modelica external object data serialization device provided in an embodiment of the present invention;
[0043] Figure 5 This is a structural block diagram of the electronic device provided by the present invention. Detailed Implementation
[0044] Preferred embodiments of the present invention will now be described in detail with reference to the accompanying drawings, which form part of this application and are used together with the embodiments of the present invention to illustrate the principles of the present invention, but are not intended to limit the scope of the present invention.
[0045] In this document, the term "embodiment" means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0046] Data serialization refers to the process of encoding data structures or objects during data transmission, transmitting them using this encoding over a network or other transmission medium, and then decoding them at the data receiver according to appropriate deserialization rules to obtain the data. Similarly, data storage also requires serialization and deserialization methods. Serialized data is stored on durable storage media, and when data is needed, the serialized data in the storage media is decoded to obtain the original data.
[0047] In view of this, the present invention provides a method and apparatus for serializing Modelica external object data. By defining serialization and deserialization interface functions in the external object class definition, confusion with other external object operation methods can be avoided, reducing the parsing difficulty. The following will elaborate and describe this through several embodiments.
[0048] Figures 1 to 3 This is a flowchart of the Modelica external object data serialization method provided by the present invention. Figure 1 As shown, the Modelica external object data serialization method includes steps S1 to S5, wherein:
[0049] S1. Construct an external object model for external object data, wherein the external object model includes serialization interface functions and deserialization interface functions;
[0050] Specifically, the serialization interface function is used to convert the state information of external object data into byte data that can be stored or transmitted, and the deserialization interface function is used to restore the byte data to the state information of external object data.
[0051] It should be noted that, in this embodiment, the external object refers to the entity structure that needs to be simulated and modeled. The external object data includes the initial state or conditions of the external object, as well as input parameter data, environmental parameter data, and interaction behavior data. Among them, the input parameter data refers to the input parameters that the external object may provide to the simulation model, such as control signals and external stimuli. These parameters may change over time and are used to simulate the impact of the external object on the simulation model. The environmental parameter data refers to the external environmental parameters of the simulation model, such as temperature, humidity, air pressure, and wind speed. These parameters may affect the behavior and performance of the simulation model and therefore need to be considered during the simulation process. The interaction behavior data refers to the interaction behavior data between the external object and the simulation model, such as communication protocols and data exchange formats. These data define the interaction methods and rules between the external object and the simulation model to ensure that they can correctly exchange data and transmit information.
[0052] Specifically, in this embodiment, the external object model based on Modelica is a vehicle system model, which includes an engine subsystem, chassis system, electrical subsystem, etc. Through physical and mathematical modeling, the variables and equations within the vehicle describe the dynamic behavior of the studied vehicle system. Model simulation calculates these equations to obtain the results of the variables. An external object is also a model; its variables and other data information constitute a memory space. The equations for solving the model are encapsulated into operation methods. After Modelica integrates this external object, it can calculate or retrieve its internal variables by calling its operation methods. For example, the vehicle chassis system is represented by an external object. This subsystem connects to the body subsystem; the movement of the suspension affects the vehicle height, attitude, and vehicle load, etc. These can all be described by establishing corresponding mathematical equations, and then corresponding operation methods are provided for calculation. These calculation results represent the state of the system and need to be cached for state update calculations in the next moment. The memory space of the external object is used to cache the state information of this model.
[0053] It's important to note that external objects provide a mechanism to encapsulate complex data structures or resources (such as file handles, network connections, and complex objects from external libraries) into objects for use by Modelica models. External objects mainly consist of the following three parts:
[0054] Constructor: Creates external resources, i.e., in-memory data;
[0055] Destructor: Releases external resources;
[0056] Operation method: Perform specific operations on external objects;
[0057] The following is an example of an external object, where the external functions are implemented in C language, specifically including the constructor (Constructor), destructor (Destructor), and operation methods (SetElement and GetElement).
[0058] Among them, the structure definition (ExternalObj) is as follows:
[0059] int nelem;: This is an integer variable used to store the length of the cache array.
[0060] double* buffer;: This is a pointer to a double type, used to dynamically allocate an array to store buffered data.
[0061] Constructor:
[0062] Input parameter: int n, representing the length of the cache array to be created.
[0063] Function: Allocates memory to the ExternalObj structure and its buffer member. The buffer is initialized as an array of length n+1, and all elements are initialized to 0 using memset.
[0064] Return value: Returns a pointer to the newly created ExternalObj structure.
[0065] Destructor:
[0066] Input parameter: void* data, which is a pointer to an ExternalObj structure, but the void* type is used for versatility.
[0067] Function: Releases the memory occupied by the ExternalObj structure and its buffer members.
[0068] Note: This function first checks if the passed pointer is NULL to avoid errors when trying to free a null pointer.
[0069] Operation methods (SetElement and GetElement):
[0070] SetElement: Allows users to set the value at a specified index position in the cache array.
[0071] GetElement: Allows users to retrieve the value at a specified index position in a cached array.
[0072] Input parameters: void* data (pointer to the ExternalObj structure), int ith (index of the element to be set), and for SetElement, double val (value to be set).
[0073] This embodiment also provides a Modelica model, ExtObjExample, which defines a class named ExternalObj that extends ExternalObject. The ExternalObj class encapsulates the constructor and destructor functions for the external object implemented in C, as well as operation functions for setting and retrieving array elements. These C functions are connected to the Modelica model through external "C" declarations.
[0074] In the Modelica model, instances of the ExternalObj class are created by the C language's Constructor function, which takes an integer n as input and returns a pointer to an ExternalObj structure (represented as an object of type ExternalObj in Modelica). The destructor function takes an ExternalObj object as input and releases the memory associated with that object.
[0075] The SetElem and GetElem functions are used to set and retrieve the elements of an array in an ExternalObj object, respectively. These functions are implemented using the external C language's SetElement and GetElement functions. They accept an ExternalObj object, an integer index, and a real number as input (for SetElem), or accept only an ExternalObj object and an integer index as input (for GetElem), and return a real number (for GetElem).
[0076] Since Modelica models cannot directly access the data memory constructed by the C language, they must read or write to this data memory through the provided operation methods (i.e., the SetElem and GetElem functions). This encapsulation ensures a clear interface between Modelica models and external C code, and allows Modelica models to interact with external objects in a type-safe manner.
[0077] This embodiment defines two special operation methods for serializing and deserializing external object data. When the user defines these two operation methods for external objects, the corresponding external object serialization and deserialization function interfaces are generated into the external object registration function of the model code during model compilation. During the simulation instantiation phase, the solver calls the external object registration function to obtain the serialization and deserialization function handles of the external object. When the solver executes the state caching program, it further calls the external object serialization function to obtain the data memory. During the debugging phase, the solver executes the state recovery program, resetting the data memory of the current external object through the deserialization interface to restore the previously serialized data.
[0078] Users construct external object models according to the Modelica language specification, including defining their constructors, destructors, and related operation methods. In addition to the constructors and destructors, the external object class definition further defines serialization and deserialization interface functions. These two interface functions must be defined in the external object class definition; otherwise, the compiler will only treat them as ordinary operation methods.
[0079] 1) The Modelica language specification supports object-oriented modeling, which is similar to object-oriented programming in C++. It involves abstracting and studying the attributes and behaviors of objects to form specific types, such as defining a car type, and then instantiating this type when it is used.
[0080] 2) Type definition involves using keywords provided by the modeling or programming language, such as `Class`, to describe the object's attributes and behaviors according to the language specification. For certain things, such as built-in types, which are predefined by the language and have fixed names (like keywords), users can inherit from these base types (base classes) to further refine their definitions.
[0081] 3) The external object class is a base class predefined by the Modelica language specification. It has no actual content. The specific constructor, destructor and operation methods need to be implemented by the user.
[0082] 4) This embodiment focuses on system modeling and simulation. System modeling may use external object classes, meaning some models require this type of representation. Therefore, when defining this type, its serialization and deserialization interfaces need to be further implemented. This embodiment defines the form of this interface function and its subsequent usage mechanism.
[0083] 5) Currently, the Modelica language specification defines that external object class definitions can only contain constructors and destructors, but can define many related operation methods. If this embodiment treats the serialization and deserialization interface functions as ordinary operation methods, then the compilation process will face significant difficulties in identifying and resolving these operation methods as serialization or deserialization functions. If this class definition is directly extended, defining the serialization and deserialization functions along with the constructors and destructors in the external object definition would essentially treat the latter two as a default interface similar to constructors and destructors, thus reducing compilation complexity and improving efficiency.
[0084] S2. Compilation system model: If an external object model is integrated, the external object serialization interface function and deserialization interface function will be generated into the external object registration function of the model code.
[0085] It should be noted that the system model integrating the external object model generates model code. The compiler pre-extenses the corresponding external object parser. Because the Modelica language specification restricts external object class definitions to only have a constructor and a destructor, the compiler needs to extend the external object parser. When the parser processes the external object class definition, if it contains other function definitions besides the constructor and destructor, it determines whether these are the serialization and deserialization interface functions `serialize` and `deserialize` through text matching. If not, an error is reported. If the external object class definition does define `serialize` and `deserialize`, they are generated into the external object registration function of the model code.
[0086] S3. After starting the simulation, the solver calls the external object registration function during the instantiation phase to obtain the data memory address of the external object data, as well as the serialization interface function handle and the deserialization interface function handle.
[0087] S4 selects at least one target component simulation model built based on the multi-domain unified modeling language Modelica from the preset model library. During the simulation, when the solver executes the state caching program and caches the external object data of the external object model, if it is determined that there is a serialization interface function, the serialization interface function is called to retrieve the external object data.
[0088] Specifically, during the simulation process, when the solver executes the state caching procedure to cache the target component simulation model data, for the external object data in the target component simulation model, the solver checks whether there is a serialization interface function for the external object data; if the serialization interface function exists, the solver calls the serialization interface function to obtain and cache the external object data; if the serialization interface function does not exist, the solver outputs a warning message indicating that it cannot perform serialization operation on the external object data.
[0089] S5. During the simulation, when the solver executes the state recovery procedure, if it determines that a deserialization interface function exists when restoring the external object data of the external object model, it calls the deserialization interface function to restore the external object data.
[0090] Specifically, during the simulation process, when the solver executes the state recovery procedure to restore the target component simulation model data, for the external object data in the target component simulation model, the solver checks whether there is a deserialization interface function for the external object data; if the deserialization interface function exists, the solver calls the deserialization interface function to restore the external object data; if the deserialization interface function does not exist, the solver outputs a warning message indicating that the external object data cannot be deserialized.
[0091] Based on the above embodiments, as a preferred implementation, the serialization interface function includes a first input parameter, a second input parameter, and an output parameter;
[0092] The first input parameter is used to receive external object data to be serialized;
[0093] The second input parameter is a string variable used to receive or provide array space for storing external object data during serialization, and to indicate the required array space size during output;
[0094] The first output parameter is used to return the success status of the serialization operation or the array space size required for the external object data;
[0095] The deserialization interface function includes a third input parameter for receiving external object data to be deserialized;
[0096] The fourth input parameter, a string variable, contains byte data used to recover the state information of the external object's data.
[0097] The second output parameter is used to return the success status of the deserialization operation.
[0098] It should be noted that the serialization interface function `serialize` stores the memory size of the external object `obj` (which is `size` bytes) into a `byte` array. The first input parameter of the serialization interface function is the external object; the second input parameter is a string variable `byte`, which is an array provided by the solver state buffer program, and the array size is `size` bytes. The `size` variable is an output variable because the solver state buffer program must know the size of the external object's data to provide sufficient array space. Therefore, in addition to serializing data, the serialization function can also be used to obtain the memory size of the external object's data. This is achieved through different parameter passing methods:
[0099] 1) If the serialization interface function is called with a null pointer as the byte parameter, the function should calculate the data memory size of the external object and return it via the output variable size;
[0100] 2) If the byte parameter given when calling the serialization interface function is not a null pointer, the function should perform a serialization operation, storing the external object data in memory into a byte array space.
[0101] Deserialization means the reverse operation of extracting data structures from a series of bytes, that is, the process of converting binary data back to the original object. In this embodiment, deserialization refers to resetting the state information of an external object instance using byte data in a given array space. The deserialization interface function `deserialize` resets the state of the external object `obj` using `size` bytes of data from a byte array space. The first input parameter of the deserialization interface function is the external object; the second input parameter is a string variable `byte`, which is the serialized data of the external object provided by the solver state cache program, and the array space size is `size` bytes.
[0102] Based on the above embodiments, as a preferred implementation, if the compilation system model integrates an external object model, then the external object serialization interface function and deserialization interface function are generated into the external object registration function of the model code, including:
[0103] Generate the serialization and deserialization interface functions into the external object registration function of the external object model;
[0104] If the external object class definition contains other function definitions besides the constructor and destructor, then text matching is used to determine whether they are serialization interface function and deserialization interface function definitions; if not, an error is reported.
[0105] If the external object class definition includes serialization and deserialization interface functions, then these functions will be generated into the external object registration function of the external object model code.
[0106] Based on the above embodiments, as a preferred implementation, the solver records the data memory address of the external object data, and passes the data memory address as a parameter to the specific address when the external object registration function operation method is called subsequently;
[0107] The external object registration function completes the registration process by constructing an external object instance, allocating a destructor handle, a serialization interface function handle, and a deserialization interface function handle, and assigning them to the corresponding member variables of the ExtObjectManager data structure.
[0108] Based on the above embodiments, as a preferred implementation, the solver also provides an external object management data structure ExtObjectManager for storing the data memory address, destructor handle, serialization interface function handle, and deserialization interface function handle of the external object data. The definition of the ExtObjectManager data structure includes:
[0109] The member variable `obj` is used to store the memory address of the external object's data.
[0110] The member variable dtor is a destructor handle used to release the memory of external object data;
[0111] The member variable `sel` is a handle to the serialization interface function, used to convert external object data into a byte sequence.
[0112] The member variable dsel is a handle to the deserialization interface function, used to recover external object data from a byte sequence.
[0113] Figure 4 This is a structural block diagram of the Modelica external object data serialization device provided by the present invention, with reference to... Figure 4 In a preferred embodiment of the present invention, the present invention provides a Modelica external object data serialization apparatus 400, comprising:
[0114] The first module 401 constructs an external object model of external object data. The external object model includes a serialization interface function and a deserialization interface function. The serialization interface function is used to convert the state information of the external object data into storable or transmittable byte data, and the deserialization interface function is used to restore the byte data to the state information of the external object data.
[0115] The second module 402 selects at least one target component simulation model built using the multi-domain unified modeling language Modelica from the preset model library. During the simulation, when the solver executes the state caching program and caches the external object data of the external object model, if it is determined that a serialization interface function exists, the serialization interface function is called to retrieve the external object data. During the simulation, when the solver executes the state recovery program and restores the external object data of the external object model, if it is determined that a deserialization interface function exists, the deserialization interface function is called to restore the external object data.
[0116] The Modelica external object data serialization apparatus provided by the present invention executes the Modelica external object data serialization method provided in the above embodiments through the first module 401 and the second module 402. The Modelica external object data serialization method has been described in detail in the above embodiments, and will not be repeated here.
[0117] Figure 5 A structural block diagram of the electronic device provided by the present invention, such as Figure 5 As shown, the present invention also provides an electronic device 500, which can be a mobile terminal, desktop computer, laptop, handheld computer, server, or other computing device. The electronic device 500 includes a processor 501 and a memory 502, wherein the memory 502 stores a Modelica external object data serialization program 505.
[0118] In some embodiments, memory 502 may be an internal storage unit of a computer device, such as a hard disk or memory. In other embodiments, memory 502 may be an external storage device of a computer device, such as a plug-in hard disk, smart media card (SMC), secure digital card (SD), flash card, etc. Further, memory 502 may include both internal and external storage units of the computer device. Memory 502 is used to store application software and various types of data installed on the computer device, such as program code for installing the computer device. Memory 502 can also be used to temporarily store data that has been output or will be output. In one embodiment, when the Modelica external object data serialization program 505 is executed by processor 501, the following steps are implemented:
[0119] S1. Construct an external object model for external object data, wherein the external object model includes serialization interface functions and deserialization interface functions;
[0120] S2, Compilation system model: If an external object model is integrated, the external object serialization interface function and deserialization interface function will be generated into the external object registration function of the model code.
[0121] S3. After starting the simulation, the solver calls the external object registration function during the instantiation phase to obtain the data memory address of the external object data, as well as the serialization interface function handle and the deserialization interface function handle.
[0122] S4. Select at least one target component simulation model built based on the multi-domain unified modeling language Modelica from the preset model library. During the simulation, when the solver executes the state caching program and caches the external object data of the external object model, if it is determined that there is a serialization interface function, call the serialization interface function to retrieve the external object data.
[0123] S5. During the simulation, when the solver executes the state recovery procedure, if it determines that a deserialization interface function exists when restoring the external object data of the external object model, it calls the deserialization interface function to restore the external object data.
[0124] In some embodiments, processor 501 may be a central processing unit (CPU), microprocessor, or other data processing chip, used to run program code stored in memory 502 or process data, such as executing the Modelica external object data serialization program.
[0125] This embodiment also provides a computer-readable storage medium storing a Modelica external object data serialization program, which, when executed by a processor, performs the following steps:
[0126] S1. Construct an external object model for external object data, wherein the external object model includes serialization interface functions and deserialization interface functions;
[0127] S2, Compilation system model: If an external object model is integrated, the external object serialization interface function and deserialization interface function will be generated into the external object registration function of the model code.
[0128] S3. After starting the simulation, the solver calls the external object registration function during the instantiation phase to obtain the data memory address of the external object data, as well as the serialization interface function handle and the deserialization interface function handle.
[0129] S4 selects at least one target component simulation model built based on the multi-domain unified modeling language Modelica from the preset model library. During the simulation, when the solver executes the state caching program and caches the external object data of the external object model, if it is determined that there is a serialization interface function, the serialization interface function is called to retrieve the external object data.
[0130] S5. During the simulation, when the solver executes the state recovery procedure, if it determines that a deserialization interface function exists when restoring the external object data of the external object model, it calls the deserialization interface function to restore the external object data.
[0131] The embodiments described above are merely illustrative of several implementations of the present invention, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and these modifications and improvements all fall within the scope of protection of the present invention. Therefore, the scope of protection of this patent should be determined by the appended claims.
[0132] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for serializing external object data in Modelica, characterized in that, include: An external object model for external object data is constructed. The external object model includes serialization interface functions and deserialization interface functions. The serialization interface functions are used to convert the state information of the external object data into storable or transmittable byte data, and the deserialization interface functions are used to restore the byte data to the state information of the external object data. Select at least one target component simulation model built using the multi-domain unified modeling language Modelica from the preset model library. During the simulation, when the solver executes the state caching program and caches the external object data of the external object model, if it is determined that a serialization interface function exists, the serialization interface function is called to retrieve the external object data. During the simulation, when the solver executes the state recovery program and restores the external object data of the external object model, if it is determined that a deserialization interface function exists, the deserialization interface function is called to restore the external object data. The serialization interface function includes a first input parameter, a second input parameter, and a first output parameter; The first input parameter is used to receive external object data to be serialized; The second input parameter is a string variable used to receive or provide array space for storing external object data during serialization, and to indicate the required array space size during output; The first output parameter is used to return the success status of the serialization operation or the array space size required for the external object data; The deserialization interface function includes a third input parameter for receiving external object data to be deserialized; The fourth input parameter, a string variable, contains byte data used to recover the state information of the external object's data. The second output parameter is used to return the success status of the deserialization operation.
2. The Modelica external object data serialization method according to claim 1, characterized in that, After constructing the external object model of the external object data, the method further includes: Generate the serialization and deserialization interface functions into the external object registration function of the external object model; If the external object class definition contains other function definitions besides the constructor and destructor, then text matching is used to determine whether they are serialization interface function and deserialization interface function definitions; if not, an error is reported. If the external object class definition includes serialization and deserialization interface functions, then these functions will be generated into the external object registration function of the external object model code.
3. The Modelica external object data serialization method according to claim 2, characterized in that, After selecting at least one target component simulation model built using the multi-domain unified modeling language Modelica from the preset model library, the following are also included: After the simulation starts, the solver calls the external object registration function during the instantiation phase to obtain the data memory address of the external object data, as well as the serialization interface function handle and the deserialization interface function handle.
4. The Modelica external object data serialization method according to claim 3, characterized in that, The solver records the memory address of the external object data and passes the memory address as a parameter to the specific address when the external object registration function is called subsequently. The external object registration function completes the registration process by constructing an external object instance, allocating a destructor handle, a serialization interface function handle, and a deserialization interface function handle, and assigning them to the corresponding member variables of the ExtObjectManager data structure.
5. The Modelica external object data serialization method according to claim 4, characterized in that, The solver also provides an external object management data structure, ExtObjectManager, for storing the data memory address, destructor handle, serialization interface function handle, and deserialization interface function handle of the external object data. The definition of the ExtObjectManager data structure includes: The member variable `obj` is used to store the memory address of the external object's data. The member variable dtor is a destructor handle used to release the memory of external object data; The member variable `sel` is a handle to the serialization interface function, used to convert external object data into a byte sequence. The member variable dsel is a handle to the deserialization interface function, used to recover external object data from a byte sequence.
6. The Modelica external object data serialization method according to claim 3, characterized in that, When caching external object data of the external object model, if a serialization interface function is determined to exist, the serialization interface function is called to retrieve the external object data. During the simulation, when the solver executes the state recovery procedure, when restoring the external object data of the external object model, if a deserialization interface function is determined to exist, the deserialization interface function is called to restore the external object data, including: During simulation, when the solver executes the state caching procedure to cache the target component simulation model data, for external object data in the target component simulation model, the solver checks whether there is a serialization interface function for the external object data; if the serialization interface function exists, the solver calls the serialization interface function to obtain and cache the external object data; if the serialization interface function does not exist, the solver outputs a warning message indicating that it cannot perform serialization operation on the external object data; During the simulation, when the solver executes the state recovery procedure to restore the target component simulation model data, for the external object data in the target component simulation model, the solver checks whether there is a deserialization interface function for the external object data; if the deserialization interface function exists, the solver calls the deserialization interface function to restore the external object data; if the deserialization interface function does not exist, the solver outputs a warning message indicating that the external object data cannot be deserialized.
7. A Modelica external object data serialization device, characterized in that, include: The first module constructs an external object model for external object data. The external object model includes serialization interface functions and deserialization interface functions. The serialization interface functions are used to convert the state information of the external object data into storable or transmittable byte data, and the deserialization interface functions are used to restore the byte data to the state information of the external object data. The second module selects at least one target component simulation model built using the multi-domain unified modeling language Modelica from the preset model library. During the simulation, when the solver executes the state caching program and caches the external object data of the external object model, if it is determined that a serialization interface function exists, the serialization interface function is called to retrieve the external object data. During the simulation, when the solver executes the state recovery program and restores the external object data of the external object model, if it is determined that a deserialization interface function exists, the deserialization interface function is called to restore the external object data. The serialization interface function includes a first input parameter, a second input parameter, and a first output parameter; The first input parameter is used to receive external object data to be serialized; The second input parameter is a string variable used to receive or provide array space for storing external object data during serialization, and to indicate the required array space size during output; The first output parameter is used to return the success status of the serialization operation or the array space size required for the external object data; The deserialization interface function includes a third input parameter for receiving external object data to be deserialized; The fourth input parameter, a string variable, contains byte data used to recover the state information of the external object's data. The second output parameter is used to return the success status of the deserialization operation.
8. An electronic device, Its features are, Including memory and processor, among which, The memory is used to store programs; The processor, coupled to the memory, is used to execute the program stored in the memory to implement the steps in the Modelica external object data serialization method according to any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, Used to store computer-readable programs or instructions, which, when executed by a processor, can implement the steps in the Modelica external object data serialization method according to any one of claims 1 to 6.
Citation Information
Patent Citations
External serialization and deserialization
CN102981841A