question

Nil Ns avatar image
0 Likes"
Nil Ns asked Nil Ns commented

Model DateTime in RealTime()

Hello,


I am working on an emulation model. Therefore, the execution speed should be 1vs1 with reality. As I acquire data, I change the state of the simulation and from these, I graph.

The problem is that, even when setting the speed to 1, sometimes it goes a bit slower (not only due to internal calculations, but also when I move the view or objects). As I am using real-time data, I need that if this happens, time advances faster until Model.DateTime is equal to realtime() again.

I have created a couple of codes:

The first one is executed at the start to set the correct Model.dateTime.

if(Model.parameters.RealTime){
     if(Model.time == 0){

          string year=realtime(12).substr(21,4);            //  2023
          string datestr=realtime(11);                      //  11/13/23
          string date=datestr.substr(1,6)+year;             //  11/13/2023
          string realtimeString=date+" "+realtime(10);      //  11/13/2023 23:23:10
          DateTime dt=DateTime(realtimeString,"%m/%d/%Y %T");

          function_s(getmodelunit(START_TIME_NODE), "setDateTime", dt);
     }
}
runspeed(1);


The second one is intended to be executed periodically to compensate times. This second one is the one that has the problem. It is not working exactly as it should, and even when it does, it consumes so many computational resources that it delays the model again. Is there a better way to achieve this?


string year=realtime(12).substr(21,4);            //  2023
string datestr=realtime(11);                      //  11/13/23
string date=datestr.substr(1,6)+year;             //  11/13/2023
string realtimeString=date+" "+realtime(10);      //  11/13/2023 23:23:10
DateTime dt = DateTime(realtimeString,"%m/%d/%Y %T");

//milisegundos
string mili = realtime(2);
double numMili = mili.toNum();


dt = dt + DateTime(numMili/1000);
DateTime delay = dt - Model.dateTime;

double seconds = delay.totalSeconds;

//mpt(token.count+". Cambio de hora por: "+seconds+"\n");


if(seconds > 0){
//delayednodefunction(Model.find("Tools/UserCommands/SetSpeed/code"),seconds,1);
runspeed(10);

await Delay.seconds(seconds);

runspeed(1);


}
else{

runspeed(0);

await Delay.realTime(-seconds);

runspeed(1);
}
}




Thank you very much in advance!

FlexSim 24.0.1
emulationrealtime
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jason Lightfoot avatar image
0 Likes"
Jason Lightfoot answered Jason Lightfoot edited

I think for this application you should be using:

runspeed(1, RUN_SPEED_HIGH_PRECISION)

runspeed description.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Felix Möhlmann avatar image
0 Likes"
Felix Möhlmann answered Nil Ns commented

1. You can simplify the logic to get to the difference between the model time and real time by using the fixed offset between the number of seconds in the DateTime and the number of seconds returned by "realtime(0)".

DateTime offset = DateTime("01/01/1970 00:00:00", "%c");    // 11644473600
double curRealTime = realtime(); DateTime curModelTime = Model.dateTime; double delta = curRealTime - (curModelTime - offset);

2. In my experience, the "await" statement is particularly resource intensive. This, coupled with the fact that your code will execute very often when the time difference is small, probably makes this approach quite slow.

I would instead suggest to use PID (Wikipedia) logic that gets executed in a fixed interval (maybe start with something like 10 times a second) and adjusts the runspeed in a more flexible manner.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Nil Ns avatar image Nil Ns commented ·

Thank you Fleix. I’m going to try the High precision metod and if it keeps happening, I will modify the code to do what you indicate, with the feedback loop to continuously adjust the execution speed and without awake.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.