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!