Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Calling external function too many times

Calling external function too many times

Hello I am implementing my own realtime with calling to the external functions of openmodelica and i found this odd bug, I used dassl even rungekutta method but simulation calls external functions too often. Why is that ? look at the output (first is time of the simulation, second is sim time minus internal modelica tock function (tick was at time 0), third is in nanoseconds and last in whole seconds. I am trying to implement function nanosleep() in external f. I tried using many things and function to slow simulation to real time with no good luck. Syncronizing block from drivers is broken, even example of Two tank system with realtime is also broken (takes tooo long to compute but maybe it has this bug also).  It shoould be called only in times 0.05 0.1 0.15 0.2 not multiple times per 0.05. Am i right?
Thanks in advance

simulate(Segway.simulation,cflags="-g",measureTime=true,method="rungekutta")
record SimulationResult
    resultFile = "/home/fei_admin2/NetBeansProjects/OMCklient/testing/Segway.simulation_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 0.2, numberOfIntervals = 2, tolerance = 1e-06, method = 'rungekutta', fileNamePrefix = 'Segway.simulation', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '-g', simflags = ''",
    messages = "0.05000000|0.049975|49975|0
0.05000000|0.049655|49655|0
0.05000000|0.049533|49533|0
0.05000000|0.049414|49414|0
0.05000000|0.049299|49299|0
0.05000000|0.049187|49187|0
0.05000000|0.049075|49074|0
0.05000000|0.048964|48963|0
0.05000000|0.048853|48852|0
0.05000000|0.048742|48741|0
0.05000000|0.048610|48609|0
0.05000000|0.048500|48499|0
0.05000000|0.048366|48365|0
0.05000000|0.048249|48249|0
0.05000000|0.048140|48139|0
0.05000000|0.048031|48030|0
0.05000000|0.047923|47922|0
0.10000000|0.097796|97796|0
0.10000000|0.097633|97633|0
0.10000000|0.097476|97475|0
0.10000000|0.097317|97316|0
0.10000000|0.097160|97159|0
0.10000000|0.097003|97002|0
0.10000000|0.096846|96846|0
0.10000000|0.096587|96586|0
0.10000000|0.096426|96425|0
0.10000000|0.096269|96269|0
0.10000000|0.096111|96111|0
0.10000000|0.095955|95954|0
0.10000000|0.095798|95798|0
0.10000000|0.095611|95610|0
0.15000000|0.149978|149977|0
0.15000000|0.149735|149735|0
0.15000000|0.149396|149395|0
0.15000000|0.149003|149002|0
0.15000000|0.148613|148613|0
0.15000000|0.148397|148396|0
0.15000000|0.148182|148181|0
0.15000000|0.147953|147953|0
0.15000000|0.147744|147744|0
0.15000000|0.147523|147523|0
0.15000000|0.147293|147292|0
0.15000000|0.147071|147071|0
0.15000000|0.146850|146849|0
0.15000000|0.146628|146628|0
0.20000000|0.196393|196392|0
0.20000000|0.196120|196120|0
0.20000000|0.195849|195849|0
0.20000000|0.195579|195579|0
0.20000000|0.195310|195309|0
0.20000000|0.195024|195023|0
0.20000000|0.194752|194752|0
0.20000000|0.194452|194451|0
0.20000000|0.194167|194167|0
0.20000000|0.193883|193883|0
0.20000000|0.193552|193551|0
0.20000000|0.193275|193274|0
0.20000000|0.193013|193012|0
0.20000000|0.192724|192724|0
0.20000000|0.199962|199961|0
0.20000000|0.199668|199667|0
",
    timeFrontend = 0.277995768,
    timeBackend = 0.033761828,
    timeSimCode = 0.018183594,
    timeTemplates = 0.04200065300000001,
    timeCompile = 1.195023154,
    timeSimulation = 0.021323447,
    timeTotal = 1.58840941
end SimulationResult;
Warning: The initial conditions are not fully specified. Use +d=initialization for more information.
Warning: There are iteration variables with default zero start attribute. Use +d=initialization for more information.

Edited by: bumino - Feb-23-16 14:15:20

Re: Calling external function too many times

i multiplied number of nanoseconds to wait * 1000 (i forgot it was not 10^9 not 10^6) and now it works:)  but still i dont get why it was called so many times even with fixed step in 1 step

now i got (result below) and if i coose number of intervals 2 it computes 4 intervals (if i dont include last one), which is kinda odd but I will consider this

simulate(Segway.simulation,cflags="-g",measureTime=true,method="rungekutta")
record SimulationResult
    resultFile = "/home/fei_admin2/NetBeansProjects/OMCklient/testing/Segway.simulation_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 0.2, numberOfIntervals = 2, tolerance = 1e-06, method = 'rungekutta', fileNamePrefix = 'Segway.simulation', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '-g', simflags = ''",
    messages = "0.05000000|0.049978|0.000021|49978279|0|27228704|1|0
0.10000000|0.049249|0.050750|49248864|0|27228704|1|0
0.15000000|0.149976|0.000023|149975904|0|27228704|1|0
0.20000000|0.049730|0.150269|49730294|0|27228704|1|0
0.20000000|0.199975|0.000024|199974647|0|1|16|0
",
    timeFrontend = 0.403733309,
    timeBackend = 0.028026882,
    timeSimCode = 0.011971422,
    timeTemplates = 0.017074025,
    timeCompile = 1.061730883,
    timeSimulation = 0.509047586,
    timeTotal = 2.031659746
end SimulationResult;
Warning: The initial conditions are not fully specified. Use +d=initialization for more information.
Warning: There are iteration variables with default zero start attribute. Use +d=initialization for more information.

Edited by: bumino - Feb-23-16 14:47:14

Re: Calling external function too many times

Hard to tell what went wrong without seeing the code. Did you synchronize on events or in a regular equation? Anyway, the version after OMC 1.9.4 will probably do real-time synchronization on a flag. You can test it out from my branch: https://github.com/sjoelund/OMCompiler/tree/opc-da (passing -rt 1 to synchronize to real-time).

Re: Calling external function too many times

thank you for your response I am developing currently in Linux is that branch also 4 linux ?current/smile  will I receive it also if I download latest night build?current/smile

well i synchronize it outside modelica current/smile I know it is quite messy and a few microseconds out of RT but it works:)
now I figured that why isnt probably example DACSRealTimeSynchronization.mo working - but i havent tested it yet its only a possibility(it fixed my code though).
In external function they are using rt_tick(0); / rt_tock(0); but I found out after a few hours that this timer is being reseted somewhere in modelica probably, so I changed to rt_tick(1); / rt_tock(1); and now it works:)

heres my code
// function which launches only once in algorithm in algorithm if initial() .... calling external function .... endif
double initTimetck(double tn)
{
  rt_tick(1);
  return 0.0;
}

//here things happens (I comunicate via shared memory but that is not important for anyone)

double toktime=rt_tock(1);
    double timesimtogo = (timesim-toktime);
    if (0 < timesimtogo ){
        struct timespec timesleep,timeleft;
        timesleep.tv_nsec=((timesim-toktime) - ((long int)timesimtogo) )  * 1000000000  ;
        timesleep.tv_sec=(long int) timesimtogo;
        int test = nanosleep(&timesleep,&timeleft);

        printf("%.8f|%f|%f|%ld|%ld|%ld|%ld|%d\n", timesim, timesimtogo,toktime,timesleep.tv_nsec, timesleep.tv_sec,timeleft.tv_nsec, timeleft.tv_sec,test);

       printf("%d\n", test);
        }

this function is called each step of simulation
so far it works i just hope there is no other tick(timer reset) for this timer 1 current/big_smile )







       

Edited by: bumino - Feb-23-16 17:01:11

Re: Calling external function too many times

Yes, the branch works on Linux. And no, you won't get it pre-built from anywhere. rt_tick(1) is used if you use measureTime=true (it is the clock measuring the time for initialization). There won't be any spare timers when measureTime=true is given.

There are 0 guests and 0 other users also viewing this topic
You are here: