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

How to use a C function in OpenModelica model

How to use a C function in OpenModelica model

I am working on a model given on https://mbe.modelica.university/behavio … ntroller/. The name of this model is HysteresisEmbeddedControl. In this model, there is a function in Modelica as given below:

Code:


impure function computeHeat "Modelica wrapper for an embedded C controller"
  input Real T;
  input Real Tbar;
  input Real Q;
  output Real heat;
  external "C" annotation (Include="#include \"ComputeHeat.c\"",
    IncludeDirectory="modelica://ModelicaByExample.Functions.ImpureFunctions/source");
end computeHeat;

As given above, the definition of this function is given as below:

Code:


#ifndef _COMPUTE_HEAT_C_
#define _COMPUTE_HEAT_C_

#define UNINITIALIZED -1
#define ON 1
#define OFF 0

double
computeHeat(double T, double Tbar, double Q) {
  static int state = UNINITIALIZED;
  if (state==UNINITIALIZED) {
    if (T>Tbar) state = OFF;
    else state = ON;
  }
  if (state==OFF && T<Tbar-2) state = ON;
  if (state==ON && T>Tbar+2) state = OFF;

  if (state==ON) return Q;
  else return 0;
}
#endif

To reproduce this model, I have created a new Model named HysteresisEmbeddedControl in OMEdit. Then, I create a function named computeHeat and insert it into this model. Now, I am not sure how to use the C function in my model. I am having OpenModelica 1.17.0 on Ubuntu 20.04. Could anyone please provide some insights into this?

Edited by: SudhakarK - May-18-21 20:32:51

Re: How to use a C function in OpenModelica model

I could solve it by specifying the directory where the C code is located.

Re: How to use a C function in OpenModelica model

Hi, I'm interested in use C functions in OME but 'till now I haven't success, I think the problem is thtat I don't write the adress of the C file correctly, could you be so kind and post how you wrote it? I mean write here the complete path.
Thank you in advance.

Re: How to use a C function in OpenModelica model

Thanks for your response. Let us say, you have a C code, named as ComputeHeat.c and it is located in a folder whose path is /home/fossee/Desktop/testSim on a Linux Machine. So, I will use the following code chunk to include the C code in my model.


Code:



    external "C"  annotation(
    Include = "#include \"ComputeHeat.c\"",
    ncludeDirectory = "file:///home/fossee/Desktop/testSim");

Hope this helps!

Re: How to use a C function in OpenModelica model

If ComputeHeat.c is in the same folder as the containing model you can use:

Code:

modelica://CurrentModelName/ComputeHeat.c

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