Distributed time management method and system for super real-time behavior simulation
By using the precise time protocol to synchronize simulation in hyper-real-time behavior simulation, the real-time simulation time is calculated, and the switching result is selected through the macro definition switching method for interface encapsulation, the problems of low time management accuracy, poor consistency and high network latency in the existing technology are solved, and efficient and accurate hyper-real-time simulation and seamless migration of intelligent algorithms are achieved.
Patent Information
- Application Number
- CN202411470633.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-21
- Publication Date
- 2025-03-18
AI Technical Summary
The existing hyperreal-time simulation technology has problems such as low accuracy, poor consistency and high network latency in time management, which is difficult to meet the needs of distributed hyperreal-time behavior simulation.
The simulation time synchronization is performed using the precise time protocol based on the preset data distribution service, the real-time simulation time is calculated through historical data, and the macro definition switching method is used to select and switch between simulation time and wall clock time, and finally the consistent interface packaging of the system time library is carried out.
High-precision ultra-real-time simulation is realized, which improves the consistency and efficiency of time management, reduces integration risks, and supports seamless migration of intelligent algorithms between simulation and actual environments.
Smart Images

Figure CN119652456A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer simulation, and in particular to a distributed time management method and system for ultra-real-time behavior simulation. Background Art
[0002] As a tool to simulate real-world behaviors and events, simulation technology plays an important role in many fields. With the rapid development of information technology, simulation technology has been widely used in military, aerospace, traffic management, urban planning, education and training. It provides users with a safe, economical and efficient experimental and training platform, allowing users to test and evaluate the impact of different decisions in a controlled environment.
[0003] Behavioral simulation focuses on simulating the behavior patterns of individuals or groups in a specific environment and studying their decision-making and interaction processes. This simulation technology helps to understand the dynamic changes of individual behaviors in complex systems and how these behaviors affect the performance of the entire system. Behavioral simulation has important applications in military tactical simulation, battlefield environment simulation, training and education, social sciences, economics, and organizational behavior research. By simulating the complex interactions and decision-making processes of the real world, behavioral simulation provides in-depth insights and decision support for multiple fields.
[0004] Time management plays an important role in behavioral simulation. It ensures that the simulation environment can accurately capture the time dimension of individual behavior, which profoundly affects the timeliness of decision-making processes and behavioral feedback. The following are several key roles of time management in behavioral simulation: Accuracy: Time management in behavioral simulation ensures accurate simulation of event sequence and duration, which is critical to achieving highly realistic behavioral simulations.
[0005] Synchronicity: In distributed behavioral simulation, time management ensures that behavioral events on different nodes can be synchronized, maintaining the global consistency of the simulation.
[0006] Interactivity: Time management promotes effective interaction between individuals or groups, ensuring that decisions and behaviors in behavioral simulations can respond quickly and influence each other.
[0007] There are two key times in time management: wall clock time and simulation time: (1) Wall clock time: Wall clock time refers to the actual time in the real world. It is consistent with the concept of time in our daily life and is the current time provided by the computer's system clock.
[0008] (2) Simulation time: Simulation time refers to the time managed and controlled within the simulation system. It does not depend on the passage of time in the real world, but is driven by the simulation system itself.
[0009] According to the relationship between the wall clock time and the speed of simulation time, there are two simulation modes, namely real-time simulation and super real-time simulation. The characteristic of real-time simulation is that the speed of simulation time is consistent with the speed of wall clock time, while super real-time simulation is a special simulation technology in which the speed of simulation time exceeds the speed of wall clock time. This simulation mode is particularly important in scenarios that require rapid prediction or evaluation, such as emergency response training or performance evaluation of complex systems. It requires the simulation system to be able to run at a higher rate in order to simulate a longer time span in a shorter time. In super real-time simulation technology, time management is a core issue. Super real-time behavior simulation time management is a technology and strategy used to accurately and efficiently manage simulation time and events when the simulation time advances faster than the real world time.
[0010] With the development of intelligent simulation, the development and integration of intelligent systems and algorithms are becoming more and more critical. A common requirement in these fields is to ensure that intelligent algorithms developed in simulation environments can be seamlessly migrated to actual application scenarios. To achieve this, consistency between simulation interfaces and physical interfaces is essential.
[0011] The consistency of the simulation interface and the physical interface means that the data format, communication protocol, and control logic used in the simulation phase should be the same as those in the actual operating environment. This consistency provides a reliable training platform for intelligent algorithms, so that their performance in the simulation environment can accurately predict their performance in the real world. In addition, the consistency of the interface also promotes the rapid deployment and integration of intelligent algorithms, reducing the technical barriers and costs that may arise in the migration process from simulation to actual operation. For example, in the field of autonomous driving, algorithms need to be able to process real-time data from sensors and make fast and accurate driving decisions. If the interface in the simulation environment is consistent with the interface actually used by the vehicle, the algorithm tested and verified in the simulation can be directly applied to the real vehicle without additional adaptation or modification. This not only speeds up the development cycle, but also reduces R&D costs.
[0012] In short, the consistency between the simulation interface and the physical interface provides a solid foundation for the application of intelligent algorithms in multiple fields, ensures a smooth transition from simulation to actual operation, reduces technical risks, and accelerates the implementation of innovative technologies.
[0013] In the field of ultra-real-time distributed behavioral simulation, although existing technologies such as timestamp technology and time synchronization protocols have made significant progress in improving the accuracy and efficiency of simulation time management, they still show limitations in meeting the requirements of behavioral simulation characteristics and interface consistency. Specific challenges include: (1) Network latency and computing power differences: These issues in distributed simulation environments pose challenges to maintaining simulation time consistency, especially in behavioral simulation, which may affect the real-time response and decision-making process of individual or group behavior.
[0014] (2) Consistency between simulation interface and physical interface: After effective training in a simulation environment, intelligent algorithms need to be migrated to actual applications. Inconsistent interfaces will lead to adaptation problems during the migration process, affecting algorithm performance and reliability.
[0015] (3) Seamless connection and parallel migration of intelligent algorithms: In behavioral simulation, intelligent algorithms need to be seamlessly connected and migrated in parallel between different simulation scenarios and actual applications. Existing technologies have limitations in achieving this, which increases the complexity and cost of technology integration.
[0016] (4) Accuracy and efficiency of super real-time simulation: Super real-time simulation requires the simulation system to simulate a longer time span in a shorter time, which places higher demands on the accuracy and efficiency of time management. Existing technologies face challenges in meeting this requirement while maintaining the consistency of time synchronization.
[0017] These challenges indicate that existing simulation time management methods have limitations in dealing with distributed ultra-real-time simulation tasks and the parallel migration of simulation interfaces and physical interfaces, and further innovation and improvement are needed. Summary of the invention
[0018] The present invention provides a distributed time management method and system for ultra-real-time behavior simulation, which are used to solve the problems of low precision, poor consistency and high network delay in existing ultra-real-time simulation.
[0019] The present invention provides a distributed time management method for ultra-real-time behavior simulation, comprising: Simulation time synchronization via a precise time protocol based on a pre-defined data distribution service; Calculate the real-time simulation time based on the synchronized simulation time in history and the pre-acquired wall clock time; Selectively switching between the real-time simulation time and the wall clock time by using a preset macro-defined-based time management mode switching method, and generating a selection switching result; Based on the selected switching result, an interface encapsulation consistent with the system time library is performed to complete the time management of the super real-time behavior simulation.
[0020] According to a distributed time management method for ultra-real-time behavior simulation provided by the present invention, the simulation time synchronization is performed through a precise time protocol based on a preset data distribution service, specifically comprising: Deploy a master clock on the master node of the physical simulation and deploy slave clocks on each behavior simulation node; The master clock and the slave clock exchange information through a data distribution service to complete distributed simulation time synchronization.
[0021] According to a distributed time management method for ultra-real-time behavior simulation provided by the present invention, the master clock and the slave clock exchange information through a data distribution service to complete distributed simulation time synchronization, specifically including: The master clock includes a Sync message publisher, an Announce message publisher, a Delay_Req message subscriber, and a Delay_Resp message publisher, and publishes and subscribes to messages by using the Sync topic, Announce topic, Delay_Req topic, and Delay_Resp topic of the data distribution service; The slave clock includes a Sync message subscriber, an Announce message subscriber, a Delay_Req message publisher, and a Delay_Resp message subscriber, and receives and processes synchronization and delay request messages from the master clock by using the topic of the data distribution service; Publishers in the master clock and slave clocks publish corresponding topics, and subscribers subscribe to corresponding topics, thus achieving reliable transmission and subscription distribution of messages.
[0022] According to a distributed time management method for super real-time behavior simulation provided by the present invention, the real-time simulation time is calculated based on the synchronized simulation time in history and the pre-acquired wall clock time, specifically including: Collect the synchronized simulation time and the corresponding wall clock time at key time points, and record and save the current simulation time and wall clock time each time the simulation time is updated; The current real-time simulation time is calculated by the association formula between the preset wall clock time and the simulation time; The current real-time simulation time is verified, and the real-time simulation time within the error range after verification is output to the user.
[0023] According to a distributed time management method for super real-time behavior simulation provided by the present invention, the real-time simulation time and the wall clock time are selectively switched through a preset macro-defined-based time management mode switching method to generate a selection switching result, specifically including: Setting the macro-defined time mode through a preset macro-defined time management mode switching method; When the time mode is set to SIMULATION_TIME, the simulation time will be used as the basis for time management. When the time mode is set to WALL_CLOCK_TIM, the real wall clock time will be used as the basis for time management. Generates the selection switching result based on the time management basis selected by the macro definition.
[0024] According to a distributed time management method for ultra-real-time behavior simulation provided by the present invention, the interface encapsulation consistent with the system time library is performed based on the selected switching result to complete the time management of ultra-real-time behavior simulation, specifically including: Based on the selected switching result, five time-related interfaces, now, sleep_for, time_since_epoch, localtime_r and gettimeofda, are encapsulated to enable the user to choose to operate according to the wall clock time or the simulation time as needed; The consistency of time processing and simulation scenarios is achieved through time-related interface encapsulation, completing the time management of super real-time behavior simulation.
[0025] The present invention also provides a distributed time management system for ultra-real-time behavior simulation, the system comprising: A time synchronization module, used for simulating time synchronization through a precise time protocol based on a preset data distribution service; A time calculation module is used to calculate the real-time simulation time based on the synchronized simulation time in history and the pre-acquired wall clock time; A macro definition switching module, used to select and switch between the real-time simulation time and the wall clock time through a preset macro definition-based time management mode switching method, and generate a selection switching result; The consistency encapsulation module is used to perform interface encapsulation consistent with the system time library based on the selected switching result to complete the time management of super real-time behavior simulation.
[0026] The present invention also provides an electronic device, comprising a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the distributed time management method for ultra-real-time behavior simulation as described above is implemented.
[0027] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the distributed time management method for ultra-real-time behavior simulation as described in any one of the above is implemented.
[0028] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the distributed time management method for ultra-real-time behavior simulation as described above is implemented.
[0029] The invention provides a distributed time management method and system for ultra-real-time behavior simulation. The method and system use a precise time protocol to design a simulation time synchronization protocol to achieve time synchronization between physical simulation nodes and behavior simulation nodes in a distributed simulation environment. The method uses the historical data of wall clock time and simulation time to calculate the simulation time, which reduces the granularity of obtaining the simulation time when the simulation time update granularity is relatively large, thereby achieving high-precision support for ultra-real-time simulation. The method is implemented by setting a macro-defined time mode for users to choose between simulation time and wall clock time, and designing an interface package that is consistent with a system time library, so that users can seamlessly move the intelligent model between the simulation and physical environments in parallel, while improving development efficiency and enhancing the adaptability of the model, thereby effectively reducing integration risks. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0031] Figure 1 The present invention provides a flow chart of a distributed time management method for ultra-real-time behavior simulation.
[0032] Figure 2 This is a schematic diagram of the PTP time synchronization principle provided by the present invention.
[0033] Figure 3 This is the overall architecture diagram of the distributed ultra-real-time behavior simulation system provided by the present invention.
[0034] Figure 4 The present invention provides a timing diagram for calculating simulation time from wall clock time.
[0035] Figure 5 This is a time management mode switching flow chart provided by the present invention.
[0036] Figure 6 This is a time management mode switching flow chart provided by the present invention.
[0037] Figure 7 It is a flow chart of the sleep_for function implemented by the present invention.
[0038] Figure 8It is a flow chart of the time_since_epoch function provided by the present invention.
[0039] Fig. 9 The present invention provides a gettimeofday function implementation flow chart.
[0040] Fig.10 The present invention provides a module connection diagram of a distributed time management system for ultra-real-time behavior simulation.
[0041] Fig.11 It is a structural schematic diagram of the electronic device provided by the present invention.
[0042] Reference numerals: 110: time synchronization module; 120: time calculation module; 130: macro definition switching module; 140: consistency encapsulation module; 1110: processor; 1120: communication interface; 1130: memory; 1140: communication bus. DETAILED DESCRIPTION
[0043] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0044] Combine the following Figure 1 The invention describes a distributed time management method for ultra-real-time behavior simulation, including: step 100, synchronizing simulation time through a precise time protocol based on a preset data distribution service.
[0045] Specifically, clock synchronization is a technology that ensures the time consistency of multiple devices or systems. It is particularly important in distributed systems because these systems may consist of multiple nodes that are geographically dispersed. Clock synchronization can ensure that events are recorded and processed in the correct order, improving system reliability. Existing clock synchronization solutions include: Network Time Protocol (NTP), Precision Time Protocol (PTP, IEEE 1588), Synchronous Ethernet (SyncE), and Global Positioning System (GPS). Among them, PTP can provide sub-microsecond time synchronization accuracy, can adapt to changes in network conditions, and automatically adjust clock deviations and delays. Figure 2 Shown is the PTP time synchronization principle diagram.
[0046] The PTP time synchronization process is as follows: Sync Message, the master clock sends a sync message to the slave clock, and the message contains the current time information. This step is used to initially synchronize the simulation time of each slave clock; Delay Request, the slave clock sends a delay request to the network to measure the network delay between the slave clock and the master clock; Delay Response, the network forwards the delay request to the slave clock to help measure the network delay; Adjust Clock, the slave clock adjusts its own clock to synchronize with the master clock. The adjustment process uses the previously received sync message; Announce Message, the master clock sends an announcement message to the slave clock to announce the status and identity information of the master clock. This process ensures that the slave clocks in the network are synchronized with the master clock through a series of message transmission and network delay compensation, thereby ensuring that the simulation time of all nodes is synchronized.
[0047] In the present invention, simulation time synchronization is performed through a precise time protocol based on a preset data distribution service, specifically including: deploying a master clock at a master node of physical simulation and deploying each slave clock at each behavior simulation node; The master clock and the slave clock exchange information through a data distribution service to complete distributed simulation time synchronization.
[0048] Specifically, Data Distribution Service (DDS) is a middleware for real-time data exchange, providing a publish-subscribe model suitable for distributed systems. DDS has the characteristics of high performance, low latency and high reliability, and has been widely used in aviation, defense, industrial control and intelligent transportation. Using DDS to implement the PTP protocol can fully utilize its advantages in distributed systems and achieve high-precision time synchronization.
[0049] Therefore, this patent uses the DDS-based PTP protocol as a clock synchronization protocol in a parallel ultra-real-time behavior simulation system to accurately calibrate the simulation time of each simulation node, such as Figure 3 Shown is the overall architecture diagram of the distributed ultra-real-time behavior simulation system.
[0050] The master clock is deployed on the master node of the physical simulation, and the slave clocks are deployed on each behavior simulation node. The master clock and the slave clocks exchange messages through DDS to achieve distributed time synchronization.
[0051] The master clock and the slave clock exchange information through the data distribution service to complete the distributed simulation time synchronization, which includes: The master clock includes a Sync message publisher, an Announce message publisher, a Delay_Req message subscriber, and a Delay_Resp message publisher, and publishes and subscribes to messages by using the Sync topic, Announce topic, Delay_Req topic, and Delay_Resp topic of the data distribution service; The slave clock includes a Sync message subscriber, an Announce message subscriber, a Delay_Req message publisher, and a Delay_Resp message subscriber, and receives and processes synchronization and delay request messages from the master clock by using the topic of the data distribution service; Publishers in the master clock and slave clocks publish corresponding topics, and subscribers subscribe to corresponding topics, thus achieving reliable transmission and subscription distribution of messages.
[0052] The time of each node is synchronized through the distributed data distribution service (DDS) combined with the PTP protocol.
[0053] Step 200: Calculate the real-time simulation time based on the synchronized simulation time in the history and the pre-acquired wall clock time.
[0054] Specifically, in the physical simulation engine, the state data including the simulation time is updated periodically. For example, when the physical simulation engine runs at a high frame rate of 2000FPS, the theoretical wall clock time interval corresponding to each frame is very short, only 0.5 milliseconds. However, for distributed systems, each time synchronization involves multiple communications between nodes. It is difficult to complete multiple communications between nodes within 0.5ms, and time synchronization faces the bottleneck of communication frequency.
[0055] In this case, even if the physical simulation engine can run at a high frame rate, the update frequency of the simulation time cannot match it due to communication limitations. This results in limited synchronization accuracy of the simulation time and cannot fully utilize the ultra-real-time simulation advantages brought by the high frame rate.
[0056] In the present invention, the real-time simulation time is calculated based on the synchronized simulation time in the history and the pre-acquired wall clock time, specifically including: Collect the synchronized simulation time and the corresponding wall clock time at key time points, and record and save the current simulation time and wall clock time each time the simulation time is updated; The current real-time simulation time is calculated by the association formula between the preset wall clock time and the simulation time; The current real-time simulation time is verified, and the real-time simulation time within the error range after verification is output to the user.
[0057] Furthermore, in order to obtain more precise simulation time, the simulation time is predicted by the wall clock time, thereby obtaining a simulation time with a smaller granularity. The process is divided into the following stages.
[0058] Data collection: During the simulation run, the system continuously collects simulation time and corresponding wall clock time data at key time points. Each time the simulation time is updated, the system records and saves the current simulation time and wall clock time.
[0059] Relationship between wall clock time and simulation time: In the scenario of simulation time synchronization, we can use the following formula to predict the current simulation time.
[0060] (1).
[0061] In the above formula,
[0062] (2).
[0063] in: is the average rate of change during simulation time, is the last known simulation time, is the last known wall-clock time, n is the number of historical data points.
[0064] By averaging multiple data points, the impact of random noise is dispersed, so that the impact of each data point on the result is reduced. This means that individual outliers will not have much impact on the results, thus effectively reducing the interference of noise.
[0065] In addition, formula (1) also retains the overall trend of the data. Although it reduces the volatility of the data, it does not change the main trend of the data. By calculating the rate of change (such as ), which can capture the changing trend between simulation time and wall clock time.
[0066] Calculation of simulation time: calculate the corresponding simulation time in real time based on n historical data points ,like Figure 4 Shown is a timing diagram for obtaining simulation time from wall clock time within one simulation time update cycle.
[0067] During the simulation process, various factors (such as network delays, calculation errors, etc.) may cause deviations in time data. The verifier verifies these data to ensure the accuracy of the simulation time. By verifying the time data, anomalies can be discovered and handled in a timely manner to reduce the risks caused by time errors. The verifier compares the calculated simulation time with the actual time based on the preset time threshold or deviation range to determine whether it is within an acceptable error range. Figure 4In the timing diagram shown, the user first triggers the clock synchronizer to listen for new simulation time. The clock synchronizer then updates the internal simulation time and records the current wall clock time and simulation time in the clock mapper. During the simulation time update cycle, the clock synchronizer continues to record the new wall clock time and simulation time. When the user requests to obtain the current simulation time, the clock converter first queries the historical data in the time mapper and calculates the average change rate of the simulation time. , based on the current wall clock time and Calculate the estimated simulation time After that, the clock converter requests the checker to check the calculation result and sends the checked current simulation time to Return to the user.
[0068] The present invention provides the capability of supporting ultra-real-time simulation with high precision, which is derived from the design of calculating the simulation time based on the historical data of the wall clock time and the simulation time, and reduces the granularity of obtaining the simulation time when the simulation time update granularity is relatively large.
[0069] Step 300: Select and switch between the real-time simulation time and the wall clock time through a preset macro-defined-based time management mode switching method to generate a selection switching result.
[0070] Specifically, the user specifies the time management mode of the simulation system by setting the macro definition TIME_MODE in the C++ header file. This macro definition contains two modes: SIMULATION_TIME and WALL_CLOCK_TIME. When TIME_MODE is set to SIMULATION_TIME, the simulation system will use simulation time as the basis for its time management. When TIME_MODE is set to WALL_CLOCK_TIME, the simulation system will use the real-time system time (i.e., wall clock time) for time management.
[0071] In the present invention, during the initialization phase of the simulation system, the corresponding time manager is selected for initialization according to the TIME_MODE macro defined in the header file, such as Figure 5 The figure shows the time management mode switching flow chart. This macro definition switching method is suitable for situations where the time management mode needs to be flexibly selected according to simulation requirements. For example, when users are performing fast simulation predictions, they can use the simulation time mode; and in simulation training that needs to be synchronized with the actual time, they can select the wall clock time mode.
[0072] The present invention provides the ability to easily switch time management modes according to different scenarios. This ability is achieved by setting the macro definition TIME_MODE for users to choose between simulation time and wall clock time, and designing an interface package that maintains consistency with the system time library, so that users can seamlessly move the intelligent model between the simulation and physical environments in parallel, while improving development efficiency and enhancing the adaptability of the model, effectively reducing integration risks.
[0073] Step 400: Based on the selected switching result, an interface encapsulation consistent with the system time library is performed to complete the time management of the super real-time behavior simulation.
[0074] Specifically, in the field of ultra-real-time simulation, the accuracy of time processing is crucial to ensure the authenticity and reliability of simulation. Since there may be inconsistency between wall clock time and simulation time, directly using the time-related APIs provided by the system (such as obtaining the current time, thread sleep, etc.) will cause problems. To this end, the present invention proposes a solution, which allows users to choose to operate according to wall clock time or simulation time as needed by encapsulating 5 commonly used time-related APIs: now, sleep_for(), time_since_epoch(), localtime_r() and gettimeofda. Such encapsulation ensures the consistency of time processing with the simulation scene, solves the problem of inconsistent time intervals, and improves the accuracy and efficiency of simulation.
[0075] Take the now function as an example. The now function is a function provided by the C++ standard library to obtain the current time point. Different clock types provide different now functions to obtain the current time point. The C++ standard library implements different now functions based on different clocks. Common clock types include std::chrono::system_clock, std::chrono::steady_clock, and std::chrono::high_resolution_clock. Among them, using std::chrono::system_clock in the simulation system can provide accurate system time, convenient time operations, and good cross-platform compatibility, so that the simulation system can better reflect the passage of time in the real world, and perform event recording and time synchronization.
[0076] In the C++ standard library, the now function based on the std::chrono::system_clock clock is declared as: struct system_clock
[0077] {
[0078] / / Other code
[0079] typedef chrono::time_point<system_clock, duration> time_point; static time_point
[0080] now() noexcept; / / Other code
[0081] }.
[0082] Through system_clock, the simulation system can benefit in multiple scenarios, including real-time simulation, event sorting, and data collection, so it is particularly important to provide the simulation system with a function that can obtain the current simulation time. Therefore, this patent provides a time_point acquisition function now(), which can be set by the user and automatically returns the current time in the corresponding time_point format, such as Figure 6 The figure shows the flow chart of how the now function is implemented.
[0083] (1) First, determine whether the time mode is defined. If not, it is set to the wall clock time mode by default. If it is defined, continue.
[0084] (2) Check the value of the time mode: a. If the time mode is wall clock time, call the now() function of the system API to obtain the current time point and return it. b. If the time mode is simulation time, obtain the double type simulation time, convert the double format simulation time into endpoint format and return it.
[0085] Take the sleep_for function as an example. std::this_thread::sleep_for is a function in the C++ standard library that suspends the current thread for a specified period of time. It accepts a std::chrono::duration object as a parameter, which indicates the time the thread needs to sleep. In the C++ standard library, the sleep_for function is declared as: sleep_for(constchrono::duration<_Rep, _Period>&__rtime).
[0086] The sleep_for function is often used to introduce delays in the code, control the execution frequency, wait for resources to become available, etc. Therefore, it is necessary to provide a function that can sleep at a specified time for the simulation system. This patent provides a function sleep_for that can sleep at a specified time. The function can support sleeping according to the wall clock time or sleeping according to the simulation time, such as Figure 7 Shown is the flowchart of the sleep_for function implementation.
[0087] The specific process includes: (1) First check whether the time mode has been defined. If not, report an error and end. If it has been defined, continue.
[0088] (2) Get the current time and perform the following operations according to the value of the time mode: if it is wall clock time, return the current time point of the system clock; if it is simulation time, return the current time point of the simulation clock; if it is an unknown value, report an error and end.
[0089] (3) Calculate the end time based on the current time and duration.
[0090] (4) Start the waiting loop thread and check whether the current time is less than the end time: if so, wait for the condition variable. If not, unlock the mutex and continue.
[0091] (5) Waiting for the condition variable, the thread suspends until the condition is met (the current time reaches the end time or the done flag is true).
[0092] (6) Notify the condition variable to wake up the thread. In another thread, when the simulation time or wall clock time reaches the end time, lock the mutex, set done to true, notify the condition variable, wake up the waiting thread, and unlock the mutex.
[0093] (7) Return the end time.
[0094] As can be seen from the flowchart, the sleep_for function first checks whether the time mode has been defined, and chooses to use wall clock time or simulation time to obtain the current time based on its value. The function uses condition variables and mutex locks to implement waiting logic until the current time reaches or exceeds the specified end time, thereby realizing thread sleep.
[0095] Taking the time_since_epoch function as an example, the time_since_epoch of the std::chrono::system_clock clock is a member function in the std::chrono library of C++. It can provide the number of seconds or higher-precision time units since the Unix epoch (January 1, 1970 00:00:00 UTC), and return a duration type object. time_since_epoch plays a key role in obtaining accurate timestamps, calculating time intervals, and achieving time synchronization. It provides developers with a flexible and accurate method to handle time-related tasks. This patent implements a function time_since_epoch that obtains the length of time according to wall clock time and simulation time. Its return value type duration is consistent with the time_since_epoch function of the C++ standard library, such as Figure 8 Shown is a flowchart for implementing the time_since_epoch function.
[0096] Depend on Figure 8 It can be seen that the implementation process of the time_since_epoch function is as follows.
[0097] (1) Determine whether the macro time mode has been defined.
[0098] a. If the time mode is undefined, define it as wall clock mode.
[0099] b. If the time pattern is defined, continue with the next steps.
[0100] (2) Make a judgment based on the current value of TIME_MODE.
[0101] a. If the value is wall clock time, get the wall clock time as the current time and continue.
[0102] b. If the value is simulation time, get the simulation time as the current time and continue.
[0103] c. If the value is unknown, report an error and end the process.
[0104] (3) Convert the current time obtained into duration type.
[0105] (4) Returns the converted duration type result.
[0106] In the above process, the time mode is used to determine whether to return the wall clock time or the simulation time, and finally the length of time since the epoch is calculated based on the current time.
[0107] Take the gettimeofday function as an example. The gettimeofday function is a system call used to obtain the current time in Unix and Unix-like systems. It provides a method to obtain the number of seconds and microseconds since the Unix epoch (January 1, 1970 00:00:00 UTC). The function prototype is as follows: int gettimeofday(struct timeval *tv, struct timezone *tz).
[0108] tv is a pointer to a struct timeval, which contains two fields: tv_sec (seconds) and tv_usec (microseconds). If tv is not NULL, gettimeofday will fill this structure with the current time. tz is a pointer to a struct timezone, which is used to specify time zone information. In modern POSIX systems, this parameter is usually not used and can be set to NULL.
[0109] The present invention provides a function gettimeofday that can obtain the current simulation time or wall clock time. The function implementation flow chart is as follows: Fig. 9 shown.
[0110] The specific process includes: (1) First determine whether the time mode has been defined.
[0111] a. If not defined, proceed to step (2).
[0112] b. If it is defined, jump to step (3).
[0113] (2) Set the time mode to WallClock and continue.
[0114] (3) Get the currently defined time mode value.
[0115] a. If the time mode is wall clock time mode, get the current wall clock time as the current time and continue.
[0116] b. If the time mode is simulation time mode, get the current simulation time as the current time and continue.
[0117] c. If the TIME_MODE value is unknown, print an error message, return -1, and end the process.
[0118] (4) Convert the current time obtained into duration type time.
[0119] (5) Determine whether the converted time is negative.
[0120] a. If yes, print the error message 'The current time is earlier than the Unix epoch time' and return -1.
[0121] bIf no, proceed to the next step.
[0122] (6) Set the number of seconds of the duration type time to the tv_sec field of the tv structure.
[0123] (7) Set the number of microseconds of the duration type time to the tv_usec field of the tv structure.
[0124] (8) Complete the filling of the tv structure.
[0125] (9) The process ends normally and returns 0.
[0126] This is achieved by designing an interface package that maintains consistency with the system time library, allowing users to seamlessly move intelligent models between simulation and physical environments, while improving development efficiency and enhancing the adaptability of the model, effectively reducing integration risks.
[0127] refer to Fig.10 The present invention also discloses a distributed time management system for ultra-real-time behavior simulation, the system comprising: A time synchronization module 110, configured to perform simulation time synchronization through a precise time protocol based on a preset data distribution service; A time calculation module 120, for calculating the real-time simulation time based on the synchronized simulation time in history and the pre-acquired wall clock time; A macro definition switching module 130, used to select and switch between the real-time simulation time and the wall clock time through a preset macro definition-based time management mode switching method, and generate a selection switching result; The consistency encapsulation module 140 is used to perform interface encapsulation consistent with the system time library based on the selected switching result, so as to complete the time management of the super real-time behavior simulation.
[0128] The simulation time synchronization is performed through a precise time protocol based on a preset data distribution service, specifically including: Deploy a master clock on the master node of the physical simulation and deploy slave clocks on each behavior simulation node; The master clock and the slave clock exchange information through a data distribution service to complete distributed simulation time synchronization.
[0129] The master clock and the slave clock exchange information through the data distribution service to complete the distributed simulation time synchronization, including: The master clock includes a Sync message publisher, an Announce message publisher, a Delay_Req message subscriber, and a Delay_Resp message publisher, and publishes and subscribes to messages by using the Sync topic, Announce topic, Delay_Req topic, and Delay_Resp topic of the data distribution service; The slave clock includes a Sync message subscriber, an Announce message subscriber, a Delay_Req message publisher, and a Delay_Resp message subscriber, and receives and processes synchronization and delay request messages from the master clock by using the topic of the data distribution service; Publishers in the master clock and slave clocks publish corresponding topics, and subscribers subscribe to corresponding topics, thus achieving reliable transmission and subscription distribution of messages.
[0130] The real-time simulation time is calculated based on the historically synchronized simulation time and the pre-acquired wall clock time, including: Collect the synchronized simulation time and the corresponding wall clock time at key time points, and record and save the current simulation time and wall clock time each time the simulation time is updated; The current real-time simulation time is calculated by the association formula between the preset wall clock time and the simulation time; The current real-time simulation time is verified, and the real-time simulation time within the error range after verification is output to the user.
[0131] The switching between the real-time simulation time and the wall clock time is selected and switched by a preset macro-defined time management mode switching method, and a selection switching result is generated, which specifically includes: Setting the macro-defined time mode through a preset macro-defined time management mode switching method; When the time mode is set to SIMULATION_TIME, the simulation time will be used as the basis for time management. When the time mode is set to WALL_CLOCK_TIM, the real wall clock time will be used as the basis for time management. Generates the selection switching result based on the time management basis selected by the macro definition.
[0132] Based on the selected switching result, an interface encapsulation consistent with the system time library is performed to complete the time management of the ultra-real-time behavior simulation, specifically including: Based on the selected switching result, five time-related interfaces, now, sleep_for(), time_since_epoch(), localtime_r() and gettimeofda, are encapsulated to enable the user to choose to operate according to the wall clock time or the simulation time as needed; The consistency of time processing and simulation scenarios is achieved through time-related interface encapsulation, completing the time management of super real-time behavior simulation.
[0133] A distributed time management system for ultra-real-time behavior simulation provided by the present invention realizes time synchronization between physical simulation nodes and behavior simulation nodes in a distributed simulation environment by using a precise time protocol to design a simulation time synchronization protocol; the design of calculating the simulation time using historical data of wall clock time and simulation time reduces the granularity of obtaining the simulation time when the simulation time update granularity is relatively large, thereby realizing high-precision support for ultra-real-time simulation; this is achieved by setting a macro-defined time mode for users to choose between simulation time and wall clock time, and designing an interface package that maintains consistency with the system time library, thereby enabling users to seamlessly move the intelligent model in parallel between the simulation and physical environments, while improving development efficiency and enhancing the adaptability of the model, thereby effectively reducing integration risks.
[0134] Fig.11 An example of a physical structure diagram of an electronic device is shown in FIG. Fig.11 As shown, the electronic device may include: a processor 1110, a communication interface 1120, a memory 1130 and a communication bus 1140, wherein the processor 1110, the communication interface 1120 and the memory 1130 communicate with each other through the communication bus 1140. The processor 1110 may call the logic instructions in the memory 1130 to execute a distributed time management method for super real-time behavior simulation, the method comprising: synchronizing the simulation time through a precise time protocol based on a preset data distribution service; calculating the real-time simulation time based on the synchronized simulation time in the history and the pre-acquired wall clock time; performing a selection switch between the real-time simulation time and the wall clock time through a preset macro-defined based time management mode switching method to generate a selection switch result; performing an interface encapsulation consistent with the system time library based on the selection switch result to complete the time management of super real-time behavior simulation.
[0135] In addition, the logic instructions in the above-mentioned memory 1130 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0136] On the other hand, the present invention also provides a computer program product, which includes a computer program, and the computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute a distributed time management method for super real-time behavior simulation provided by the above methods, the method including: synchronizing simulation time through a precise time protocol based on a preset data distribution service; calculating the real-time simulation time based on the synchronized simulation time in the history and the pre-acquired wall clock time; selecting and switching between the real-time simulation time and the wall clock time through a preset macro-defined based time management mode switching method to generate a selection switching result; based on the selection switching result, performing an interface encapsulation consistent with the system time library to complete the time management of super real-time behavior simulation.
[0137] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute a distributed time management method for super real-time behavior simulation provided by the above methods, the method comprising: synchronizing simulation time through a precise time protocol based on a preset data distribution service; calculating the real-time simulation time based on the synchronized simulation time in the history and the pre-acquired wall clock time; selectively switching between the real-time simulation time and the wall clock time through a preset macro-defined-based time management mode switching method to generate a selective switching result; and performing interface encapsulation consistent with the system time library based on the selective switching result to complete the time management of super real-time behavior simulation.
[0138] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0139] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0140] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A distributed time management method for ultra-real-time behavior simulation, characterized in that: include: Simulation time synchronization via a precise time protocol based on a pre-defined data distribution service; Calculate the real-time simulation time based on the synchronized simulation time in history and the pre-acquired wall clock time; Selectively switching between the real-time simulation time and the wall clock time by using a preset macro-defined-based time management mode switching method, and generating a selection switching result; Based on the selected switching result, an interface encapsulation consistent with the system time library is performed to complete the time management of the super real-time behavior simulation.
2. The distributed time management method for ultra-real-time behavior simulation according to claim 1, characterized in that: The simulation time synchronization is performed by using a precise time protocol based on a preset data distribution service, specifically including: Deploy a master clock on the master node of the physical simulation and deploy slave clocks on each behavior simulation node; The master clock and the slave clock exchange information through a data distribution service to complete distributed simulation time synchronization.
3. The distributed time management method for ultra-real-time behavior simulation according to claim 2, characterized in that: The master clock and the slave clock exchange information through the data distribution service to complete the distributed simulation time synchronization, which specifically includes: The master clock includes a Sync message publisher, an Announce message publisher, a Delay_Req message subscriber, and a Delay_Resp message publisher, and publishes and subscribes to messages by using the Sync topic, Announce topic, Delay_Req topic, and Delay_Resp topic of the data distribution service; The slave clock includes a Sync message subscriber, an Announce message subscriber, a Delay_Req message publisher, and a Delay_Resp message subscriber, and receives and processes synchronization and delay request messages from the master clock by using the topic of the data distribution service; Publishers in the master clock and slave clocks publish corresponding topics, and subscribers subscribe to corresponding topics, thus achieving reliable transmission and subscription distribution of messages.
4. The distributed time management method for ultra-real-time behavior simulation according to claim 1, characterized in that: The real-time simulation time is calculated based on the synchronized simulation time in the history and the pre-acquired wall clock time, specifically including: Collect the synchronized simulation time and the corresponding wall clock time at key time points, and record and save the current simulation time and wall clock time each time the simulation time is updated; The current real-time simulation time is calculated by the association formula between the preset wall clock time and the simulation time; The current real-time simulation time is verified, and the real-time simulation time within the error range after verification is output to the user.
5. The distributed time management method for ultra-real-time behavior simulation according to claim 1, characterized in that: The method of switching between the real-time simulation time and the wall clock time by using the preset macro-defined-based time management mode switching method to generate a switching result specifically includes: Setting the macro-defined time mode through a preset macro-defined time management mode switching method; When the time mode is set to SIMULATION_TIME, the simulation time will be used as the basis for time management. When the time mode is set to WALL_CLOCK_TIME, the real wall clock time will be used as the basis for time management. Generates the selection switching result based on the time management basis selected by the macro definition.
6. The distributed time management method for ultra-real-time behavior simulation according to claim 1, characterized in that: The method of performing interface encapsulation consistent with the system time library based on the selected switching result to complete the time management of the ultra-real-time behavior simulation specifically includes: Based on the selected switching result, five time-related interfaces, now, sleep_for, time_since_epoch, localtime_r and gettimeofda, are encapsulated to enable the user to choose to operate according to the wall clock time or the simulation time as needed; The consistency of time processing and simulation scenarios is achieved through time-related interface encapsulation, completing the time management of super real-time behavior simulation.
7. A distributed time management system for ultra-real-time behavior simulation, characterized in that: The system comprises: A time synchronization module, used for simulating time synchronization through a precise time protocol based on a preset data distribution service; A time calculation module is used to calculate the real-time simulation time based on the synchronized simulation time in history and the pre-acquired wall clock time; A macro definition switching module, used to select and switch between the real-time simulation time and the wall clock time through a preset macro definition-based time management mode switching method, and generate a selection switching result; The consistency encapsulation module is used to perform interface encapsulation consistent with the system time library based on the selected switching result to complete the time management of super real-time behavior simulation.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the distributed time management method for ultra-real-time behavior simulation as claimed in any one of claims 1 to 6 is implemented.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the distributed time management method for ultra-real-time behavior simulation as claimed in any one of claims 1 to 6 is implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the distributed time management method for ultra-real-time behavior simulation as claimed in any one of claims 1 to 6 is implemented.