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
  • Index
  • » Users
  • » bumino
  • » Profile

Posts

Posts

Is there a way how to force fixed step in simulatiom? Becuse even when I use rungekutta method in some cases step is happening 2times as it should or 1 step is called 8 times (this happens when i use code
algorithm
  // here i call external function and add values to some variables
  if initial() then
    initTime(time);
  end if;
if noEvent(ErrorType > 100) then
    terminate("error or simulation stop");
  end if;
 
when it is just
  if initial() then
    initTime(time);
  end if;
if noEvent(ErrorType > 100) then
    terminate("error or simulation stop");
  end if;


no 8 times step calling but first  IF is not  done (with or without noEvent in second IF)
)

if i move if(initial) to other block without if in algorithm  it works

Thanks for reply current/smile so I will probably try to combine it with some return value current/smile And one little question is there a way how to make der(variable) work if you get variable from external function?{or cant because  it is somehow linked to symbolic representation in compiler?} I tried many things but at the end I think I must stick to not ideal deraivation block which works current/smile. Which seems logical. But maybe I am missing something.

Is it possible to call something similiar to terminate() from extrenal C function? I would want to save results into CSV file so exit probably wont work:)

I also tried it with arrays

I can read from array, but when I try to write to array segmentation fault occurs

Also documenations says that there is size_t with length about array but there is not.


Found the core of the problem

one have to call it like this :::
function foo
input Real a[:,:,:];
output Real x;
external;
end foo;

and not

function foo2
input Real a[:,:,:];
output Real x;
external x=foo(a);
end foo2;

Can you please help me and tell me how to declare structure in C header file so it would be compatibile with OpenModelica?

Because I wont compile a file without it and then a collision occurs as in post above.

I looked into it more and a new problem arised. How should I declare this input structure in my code ? Which type it should be? I tried to build exactly same structure as was pointed out in documentation, but it does not work (in structure/record mapping there is really too few written about it)

Error: Error building simulator. Build log: clang   -fPIC -O0 -march=native   -g   -I"/usr/include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Segway.simulation.o Segway.simulation.c
clang   -fPIC -O0 -march=native   -g   -I"/usr/include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Segway.simulation_functions.o Segway.simulation_functions.c
Segway.simulation_functions.c:21:45: error: passing 'Segway_comunication__helper_segway__vars' (aka 'struct Segway_comunication__helper_segway__vars_s') to parameter of incompatible type 'struct segway_vars *'
  _goon_ext = ComunicationInterface(_u_ext, _inpstruct_ext, _simTime_ext);
                                            ^~~~~~~~~~~~~~
./Handle_data.h:18:56: note: passing argument to parameter 'seg_output_p' here
int ComunicationInterface(double u,struct segway_vars *seg_output_p, double timesim);


in C code

Code:

struct segway_vars {

    double phi;
    double speedR;
    double speedL;
    double posR;
    double posL;

};
in OM

Code:

  record segway_vars

    Real phi(start = 0);
    Real speedR(start = 0);
    Real speedL(start = 0);
    Real posR(start = 0);
    Real posL(start = 0);
  end segway_vars;

There is really not an example? It would have explained a lot         

To: sjoelund.se
Thank you I now understand. I thought that meaning of passing the reference was about returning pointer. Now I understand that it is about changing the structure in function via passed reference current/smile I will try to do it this way.

To adrpo
I just needed to return 5 vals from my function. ( I dont exactly now if u meant that I should use same model as sjoelund.se proposed with structures but these structures I like more:) because it is defined better by var names:) )

Thank you both for help current/smile Have a nice week.

So basically there is written, that arrays are not possible. But i found there that structures / records are possible. But there is so little documentation about it. Can you  please help me ? There is a piece of code which I have in modelica
1 class

Code:

segway_vars vars;


  record segway_vars
    Real phi(start = 0);
    Real speedR(start = 0);
    Real speedL(start = 0);
    Real posR(start = 0);
    Real posL(start = 0);
  end segway_vars;

/// record is defined and initialised

// now there is a calling to external function
  function Get_outp
    input Real u1, u2;
    input Modelica.SIunits.Time simTime;
    /*input Real time;*/
    output segway_vars mystruct;
 
    external mystruct = ComunicationInterface(u1, u2, simTime) annotation(Library = "libgetref.o", Include = "#include \"Handle_data.h\"");
  end Get_outp;

//just equation part
vars = Get_outp(u1, u2, time);

This is what i have done in openmodelica and compiling gets before calling external function(when i run it without libraries for my functions) so it should be ok i think.

The question is about C part(where i think problem lies). In documentation I have found out in 12.9.1.3 Records that Records are passed by reference (i.e. a pointer to the record is being passed).

So i created global structure in my header file

Code:


typedef struct segway_vars {
    double phi;
    double speedR;
    double speedL;
    double posR;
    double posL;
} seg_output;
seg_output * seg_output_p;

double Get_reference_signal(double refval, int shkey);
seg_output *ComunicationInterface(double u1,double u2, double timesim);
double initTimetck(double);

And then I initialise memory fot it in function initTimettck which run only once on initial by

Code:

seg_output_p = malloc (sizeof (struct segway_vars));

And then I use it like this in function

Code:

seg_output *ComunicationInterface(double u1, double u2, double timesim) {


     //some not important code and then this


    seg_output_p->phi=AT;
    seg_output_p->posL=0.2;
    seg_output_p->posR=0.3;
    seg_output_p->speedL=0.4;
    seg_output_p->speedR=0.5;
//(fixed values are just for testing for now)
    return seg_output_p;
}

Question is what have I done wrong? Complilation of simulation says:

Code:

Error: Error building simulator. Build log: clang   -fPIC -O0 -march=native   -g   -I"/usr/include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Segway.simulation.o Segway.simulation.c

clang   -fPIC -O0 -march=native   -g   -I"/usr/include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Segway.simulation_functions.o Segway.simulation_functions.c
Segway.simulation_functions.c:21:17: error: assigning to 'Segway_comunication__helper_segway__vars' (aka 'struct Segway_comunication__helper_segway__vars_s') from incompatible type 'seg_output *' (aka 'struct segway_vars *')
  _mystruct_ext = ComunicationInterface(_u1_ext, _u2_ext, _simTime_ext);
                ^ ~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

Or do you have any working example please? Thank you


Hello
I would like to ask if there is a way how to  pass more then one return value from external C  function. Is it possible? I know that in C i have to send just refference, but i am not quite sure how it works in/with openmodelica.  Does someone have an example ?current/smile Thanks

Thank you current/smile. I decided to implement sockets and it works fine (only after closing socket omc autocloses  but thats not really a problem in my application:).

Hello,
I would like to ask, which means of inter process communication is availible with OMC? I am building a php aplication which should be able to start  OMC and run multiple simulations so it would be as fast as possible and I dont have to start over OMC and load all libraries each  time. I search it a lot and found sockets but it is said it is deprecated method and a CORBA. But I dont really understand CORBA, is it also IPC? Is it usable from PHP? I am using ubuntu, so there is some support for message queues or pipe or sth like that?current/smile
Thanks in advance:)

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 )







       

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.

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.

Hello i would like to ask how can I put simulation flags and other things to omshell simulation ?

like simulate(Segway.simulation,cflags="-g",measureTime=true,method="dassl")

but I would like to use things from omedit like number of processors, options of dassl method and so on.. is thete a documentation for this? because i cant find any..

Thanks in advance.

I tried to do this but serial communication got wrong when loading serial port fd. So I communicate via shared memory to a C program which handle serial port and update the shared memmory

Hello
I am trying to call an external C function which will generate unique key (for shared memory) at the start of simulation and then save it into a variable which will be accesible from submodels in my scheme. But I cant to find a way to call the function only one time at the start of simulation(when {time== 0 didnt work}) and then how to set it as global variable so other submodels can acces it. Thanks for reply:)

thank you both current/smile this is really very helpful. I will look into this solutions more:))

Thanks  for reply current/smile
I would like to ask an question about option 2. Will the simulation wait for the result of a externally called function(if i use something to slow execution of function  ? Or is there a way to make simulation synchronous with real time? So when I start simulation it wont run fast (all at once) but will be executed in time(eg. 10 sec of simulation is about 10 sec in real life current/big_smile ) Thanks.

Thank you current/smile. This information really helped:) and is there a kind of API which could do something like OMI? like i described in first post:)  (or for example running a simulation, more/less in realtime where i could change some variables/parameters from outside (e.g. sockets,or some IPC like pipe, shared memory etc (it can work on linux)) ? without FMI i mean if sth exists:)

Hello

I would like to ask about interactive simulation. So it is the simulation where i can change for example(as shown in documentation) input flow of water into a tank during the simulation externally for example by sending requested value by sockets.  I can achieve it by using “OpenModelica Interactive” (OMI). This OMI can be started by $omc +d=interactive  . About it is written in chapter 6 of PDF document.  I am trying to make my own GUI which will comunicate by sockets and sends inputflow to tank to openmodelica. But this is not stated in chapter 6 more.. only results of an example (no commands no how to run a model in interactive simulation etc... ) . Where could i find how to do it ? Or am I doing sth wrong? Thanks for reply

Thanks for reply:) and where can i find more documentation how to create own FMI and implement it with openmodelica?

Hello I am trying to work with openmodelica in interactive mode I will work on realtime controll of a realsystem with controller output computed in modelica


Which is the last version which supported this interactive mode ? Is there a documentation to it?

After a few tries i found that you can run interactive mode from terminal(i am using ubuntu) comunication with sockets by:  omc -d=interactive
which is some debug interactive mode. But when i try to comunicate it by sending for example setcontrolclienturl#2#127.0.0.1#18888# end
it responds with  this error

Error occurred building AST Syntax Error [:1:19-1:19:writable] Error: Lexer failed to recognize '#2#127.0.0.' [:1:27-1:29:writable] Warning: Treating .0 as 0.0. This is not standard Modelica and only done for compatibility with old code. Support for this feature may be removed in the future. [:1:29-1:31:writable] Warning: Treating .1 as 0.1. This is not standard Modelica and only done for compatibility with old code. Support for this feature may be removed in the future.

but when i try for example 1+1 it returns 2 current/smile but then modelica quits which is also not wanted current/smile

i need to be able to pause simulaiton execution recieve actual output from real system and send it to controller in modelica (maybe by changing parameter or sth like that) and resume simulation and do this on and on..

Should i use some old version of modelica (which one? ) which supports it corretly or i can make it work with this interactive debug mode ? Really thanks for reply. (and yea dont tell me i shouldnt do it because i do it for my masterdegree diploma work and i cant use corba because it starts modelica for every simulation and i need to run it continuosly)
Really thanks for every reply:)

  • Index
  • » Users
  • » bumino
  • » Profile
You are here: