ghorwin / FMICodeGenerator

A Code Generator that creates native C/C++ code to build Functional Mock-Up Units (FMU) with support for FMI version 2.0 (rollback)
BSD 3-Clause "New" or "Revised" License
51 stars 25 forks source link

Simple ODE in FMU example #40

Closed GreroDe closed 1 month ago

GreroDe commented 2 years ago

I have implemeted a simple Pendulom Example in the FMU generator and It worked. Now I would like to implementthe same logic but this time withuot depending on a internal time factor h , but depending ona external time factor which is giving by the simulator (in this case Matlab) or more direct by the <defaultExperiment. data placed on the modelDescription.xml.

Implementation of the Rungakutta Solver in to the UpdateIfModified Function

`

double g = m_realVar[FMI_PARA_g];
double l = m_realVar[FMI_PARA_l];
double b = m_realVar[FMI_PARA_b];
double m = m_realVar[FMI_PARA_m];
double Ang = m_realVar[FMI_LOCAL_Ang];
double vel = m_realVar[FMI_LOCAL_vel];

//Runga Kutta Method
double h=0.01;

double dx1,dx2,dx3,dx4,dx;
double dv1,dv2,dv3,dv4,dv;

dx1=h*vel;
dv1=h*dydx(Ang,vel,g,l,b,m);

dx2=h*(vel+0.5*dv1);
dv2=h*dydx(Ang+0.5*dx1,vel+0.5*dv1,g,l,b,m);

dx3=h*(vel+0.5*dv2);
dv3=h*dydx(Ang+0.5*dx2,vel+0.5*dv1,g,l,b,m);

dx4=h*(vel+dv3);
dv4=h*dydx(Ang+dx3,vel+dv1,g,l,b,m);

dx=(dx1+2*dx2+2*dx3+dx4)/6;
dv=(dv1+2*dv2+2*dv3+dv4)/6;

Ang=Ang+dx;
vel=vel+dv;

// output variables
m_realVar[FMI_OUTPUT_Ace] = Ang; 
   //Update Angle and Velocity values
m_realVar[FMI_LOCAL_Ang]=Ang;
m_realVar[FMI_LOCAL_vel]=vel;

`

With the ODE function

float dydx(double x, double v,double g,double l,double b, double m) { return((-(g/l)*sin(x))-(b/m)*v); }

ghorwin commented 2 years ago

So, you would like to change the FMU interface?

GreroDe commented 2 years ago

Yes I would like to test it in different platforms and also setting different timesteps on them. So it is not good idea to define internally the Steptime. In other words I would like to get the h value from outside or from the <defaultExperiment data

ghorwin commented 2 years ago

Ok, best way to do this is:

  1. create a copy of your config-file for the FMUGeneratorProject
  2. open FMUGenerator with this copyied project
  3. alter your interface (add a new parameter input, type real, for the time step)
  4. generate the FMU template
  5. copy your old source code into the new template and adjust code as needed

Done.