Dear Alexander,

I confirm what Adrian wrote, at least in principle (implementational details might vary a little bit). If you want to communicate with the outside at given times, using when equations and putting your function inside the when clause is the right thing to do. Otherwise, variable step size implicit solvers such as DASSRT might invoke your function multiple times, possibly backwards and forward in time, depending on their inner working mechanism.

If you want to synchronize with a real time code, you might consider inline integration. With inline integration, the integration algorithm (usually something simple as forward or backward Euler) is applied symbolically to the equations, and then omc generates fixed-time-step code. For instance, if you apply inline integration with the explicit Euler algorithm to

der(x) = -y + 1;
y = 2*f(x);

you get a simulation code such as

time = 0;
loop
  y := 2*f(x);
  x := x + dt*(-y+1);
  time = time+dt;
end loop;

In this particular case, you might have the guarantee that your function f(.) is only called once every time step, and all time steps are equally spaced by dt. Hakan Lundvall is working to implement this kind of functionality in omc, though I don't know what the exact status is.

Cheers,
                                Francesco


Pop ha scritto:

Hi,

As far as I know dasrt is using variable time step during integration.
Of course, people which know more about this, please invalidate me :)

You can tell dasrt to output results at certain steps and
you do that via the parameters you give for the simulation:
timeStep = (stopTime-startTime)/numberOfIntervals, but that
doesn't mean that when it call an external function it will
do it at a specific time. It may call it 20000 times in a
time step.

If you want to feed values synchronously I guess (I'm not not
100% sure until I test it) you can do it in a when clause.

when sample(time, 0.5) then
 newVar = call_to_your_function();
 // you might even need to use reinit(var, newVar)
 // here but again I'm not sure
end when;

When the condition in the when clause is reached the solver returns
(due to a zero crossing event, here generated by sample) and then
the simulation runtime runs the discrete part of the system and if
there are no more events it enters again the simulation loop.

Well, please take what I said with an open mind as I'm not the best
guy to comment on this. Any other input from other more qualified
people is welcome (to me too as I may learn more)!

Cheers,
Adrian Pop/

[EMAIL PROTECTED] wrote:
Hi

the new OpenModelica "morning-build" that fixes the error: http://www.ida.liu.se/~pelab/modelica/OpenModelica/OMC/nightly-builds/


Great, thanks again!

I don't quite get why would you want to use an external function that returns the same value

I wanted to test the external function mechanism. Since it didn't
worked the first time I tried a minimal configuration - an
argument-free function.

Maybe the function even stays that way - without arguments. All I
want to do is feed the model input data externally. The question is
when to provide a new value. I'm going to synchronise that from the
runtime library code. Though I have a small issue with that. I
figured solver_dasrt.cpp line 239 would start the main simulation
loop. Yet simulation time only counts up every 5th loop run with the
model used (time step of 0.005). Is there some way of tackling the
definite start of a new time step from the code?

Regards, Alexander


but is not my place to complain and I'm glad you did as it exposed
a bug in the compiler :) Thanks for reporting it!

Cheers, Adrian Pop/

[EMAIL PROTECTED] wrote:
That was fast. Thank you Adrian!



-----Ursprüngliche Nachricht----- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Adrian Pop Gesendet: Montag, 20. Oktober 2008 13:25 An: [email protected] Betreff: Re: External functions without arguments are not found


Hi,

I found and corrected the bug. I'll test it a bit more before I commit the fix. I'll then generate a new nightly-build later today
 and get back to you when is available.

Cheers, Adrian Pop/

Adrian Pop wrote:
Hi,

This is clearly a funny bug. It should work. I'll have a look at
it and let you know when is fixed.

Cheers, Adrian Pop/

[EMAIL PROTECTED] wrote:
Hi,

I tried the ExternalLibraries example from the User Guide in
the testmodels directory of the 1.4.4 release. It worked fine.

Then I wanted to change one function to take no arguments. That
didn't work. Did I do something wrong or is this a bug? Description follows.

Regards, Alexander


I changed ExternalFunc1_ext() to take no arguments. Changed all
 the signatures and implementations. Like in the C file:

double ExternalFunc1_ext() { return 2; }

And in the model:

function ExternalFunc1 output Real y; external y=ExternalFunc1_ext() annotation(Library="libExternalFunc1_ext.o", Include="#include\"ExternalFunc1_ext.h\""); end ExternalFunc1;

With a later call to:

der(x)=-ExternalFunc1();

Running the script gives off:

runScript("ExternalLibraries.mos") "true 0 0 record resultFile =
"Simulation failed. No matching function found for ExternalFunc1() of type function() => Real, candidates are function() => Real Error occured while flattening model ExternalLibraries " end record

true"





--
Francesco Casella - Ph.D.
Dipartimento di Elettronica e Informazione
Politecnico di Milano
Via Ponzio 34/5
I-20133 MILANO - ITALY

Tel:    +39-02-2399-3465 (Leonardo)
        +39-02-2399-7749 (Polo di Cremona)
Fax:    +39-02-2399-3412
e-mail: [EMAIL PROTECTED]
web:    http://home.dei.polimi.it/casella
Skype:  callto://francesco.casella

Reply via email to