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
  • » WilliamEnergy
  • » Profile

Posts

Posts

Hi everyone,

while I was trying to work with matrixes of complex numbers, I encountered some troubles to make the simple product of a matrix A with a matrix B.
Indeed, in the first case, when I implement the following code, it works without any problem:

Code:

model tset

import Modelica.ComplexMath.*;
Complex A[2,2] = [Complex(0,0), Complex(2,3);Complex(2,2),Complex(1,3)];
Complex B[2,2] = [Complex(2,1), Complex(2,3);Complex(2,2),Complex(1,3)];
Complex C[2,2];
equation
A*B = C;
end tset;

However, this piece of code gives an error, even before the compilation:

Code:


model tset
import Modelica.ComplexMath.*;
Complex A[2,2] = [Complex(0,0), Complex(2,3);Complex(2,2),Complex(1,3)];
Complex B[2,2] = [Complex(2,1), Complex(2,3);Complex(2,2),Complex(1,3)];
Complex C[2,2];
equation
C = A*B;
end tset;

https://framapic.org/g8PSlKYV9O4f/xX3zkhly65Nv.PNG

I don't know what causes this problem. I even verified if the problem persists with matrixes of reals, and the thing is with matrixes of reals, it works fine in both ways.

Would you have an idea on what causes this trouble and how to solve it?

Thanks,

WilliamEnergy

I forgot to mention that I also tested the "testRealMatrix1" on Dymola on which it worked. So I don't know what's wrong with OpenModelica and functions using Matrixes of complex numbers...

Hi everyone,

I have to make functions dealing with matrixes of complex numbers. For instance, I would like to make a function returning, as its output, the real part of a matrix of complexes given in input.

I manage to compile the following code, which does the job:

Code:

encapsulated class testRealMatrix2

import Modelica.ComplexMath.real;
import Complex;

Complex A[2,2] = [Complex(1,1),Complex(2,3);
                  Complex(2,2), Complex(6,1)];
Real A_Re[2,2];

algorithm
for i in 1:size(A,1) loop
  for k in 1:size(A,2) loop
  A_Re[i,k] := real(A[i,k]);
  end for;
end for;
                   
end testRealMatrix2;

But when I try to make it as a function, I have troubles to compile (see image below):

Code:


encapsulated class testRealMatrix1
import Modelica.ComplexMath.real;
import Complex;

function realMatrix
input Complex A[2,2];
output Real A_Re[2,2];

algorithm
for i in 1:size(A,1) loop
  for k in 1:size(A,2) loop
  A_Re[i,k] := real(A[i,k]);
  end for;
end for;
end realMatrix;

Complex A[2,2] = [Complex(1,1),Complex(2,3);
                  Complex(2,2), Complex(6,1)];
Real A_Re[2,2];

algorithm
A_Re := realMatrix(A);
                   
end testRealMatrix1;

https://framapic.org/wETZBKGvpdiF/EzEbXGysarzj.PNG

It seems there is an incompatibility of type when traducing the Modelica program in C... But I don't really understand what is the problem and how I could solve it.
Oh, and by the way I also tried to call a function on matrixes of real, it works. It seems the problem only concerns matrixes of complexes.

Would anyone know what's wrong?

By advance, thank you

Hello everyone,

I am trying to call an external C function from my Modelica code Under Windows 8, with OpenModelica1.12.0-64bit.
From what I understood, under OpenModelica, it should be possible to call external C functions by at least 3 different manners:

1) By letting OpenModelica compiler do the work : by directly calling the .c file (as described in here, part "Letting the Modelica compiler do the work")

Click on [Image 1] for more infos on what I did.

2) By separate compilation : By calling the .o file, obtained from the MinGW gcc compilation of the .c file (as described with the ExternalFunc1 calling in

Click on [Image 2] for more infos on what I did.

3) By separate compilation :  By calling the .a and .o files obtained from the MinGW (as described with the ExternalFunc2 calling in here)

Click on [Image 3] for more infos on what I did.

I tried to implement all of the cases, inspiring me from the links cited previously. None of these implementations worked and I don't know what I did wrong.

So I would like to know if someone could help me calling external C functions, I would be very grateful. Obviously, if necessary, I can give you more informations about what I did (how I installed MinGW...). Also, I would like to know if it is possible (if yes, how) to call external C functions from .dll and .lib files.

By advance, thank you,

William

Hello everyone and thank you by advance for your further help,

I would like to call an external function, coded in C, to calculate the Eigen values of a Matrix of complexes.
The C-function is stored in a directory, "Eigen_Library", constituted as follows:

Eigen_Library:
       -> Eigen_Library_Header.h
       -> Eigen_Library.lib
       -> Eigen_Files:
              -> Eigen_Library_c_File.c
              -> Eigen_Library_cpp_File.cpp      (the C++ function is actually encapsulated into a C function)


The header file contains the following:

#ifndef EXAMPLE_H_INCLUDED
#define EXAMPLE_H_INCLUDED
typedef unsigned int size_t;
extern void fct_tab_c(double *, double *, size_t, size_t);
#ifdef __cplusplus
     extern "C" {
#endif
         void fct_tab(double *,double *, size_t, size_t);
#ifdef __cplusplus
     }
#endif


#endif //
EXAMPLE_H_INCLUDED


To call the function defined in the header file, I define a package, "PersoEigenLibrary", as follows:

encapsulated package PersoEigenLibrary
  import Modelica.ComplexMath.*;
  import Complex;
  import EigenLibrary;

  function Eigen_vectors_extern_function
      "Extern function that gives eigen vectors of complex 3x3 matrices"
    input Real tab[:,:];
    output Real tab_res[size(tab,1),2*size(tab,2)];
    external "C"
    fct_tab_c(tab,tab_res,size(tab_res,1),size(tab_res,2))
      annotation(Include="#include <Eigen_Library_HEADER.h>",Library="Eigen_Library");
    end Eigen_vectors_extern_function;

 
  class testofEigenLibrary
     Real ReA[3,3] = [1,1,2;4,3,2;1,2,4];
     Real ImA[3,3] = [2,2,6;4,5,4;3,2,1];
     Real A[3,6] = [ReA,ImA];
     Real res[3,12];
     algorithm
     res := Eigen_vectors_extern_function(A);
  end testofEigenLibrary;

end PersoEigenLibrary;


Then, I instantiate the class "testofEigenLibrary", defined previously in the package:

class testPersoEigenLibrary
import PersoEigenLibrary.*;
testofEigenLibrary b;
end testPersoEigenLibrary;


My problem is that I get the following messages:

https://www.openmodelica.org/images/agorapro/attachments/6364/mini_MessageError1.PNG

https://www.openmodelica.org/images/agorapro/attachments/6364/mini_MessageError2.PNG

Hence, my questions are the following:
- Where should I store the directory containing my header file,  my C and C++ functions, and their .lib file?
- How should I organize this directory?
- Is my header file correct?
- Do I need to load this directory as a Library? If yes, how?

By advance, thank you for your help.

William

Mar-06-18 13:41:35
Category: Developer

Hello sjoelund.se,

Thank you for your quick reply. I don't really understand, what is OpenModelica Scripting? Indeed, I do not manage to run the OpenModelica.Scripting.loadFile(...) function in OMedit.

What I do:

class test
import OpenModelica.Scripting.loadFile;
Boolean loadFileErrorFlag;
String pathname = "rabbit.mo";
algorithm
loadFileErrorFlag := loadFile(fileName= pathname);
end test;

What I get:
https://www.openmodelica.org/images/agorapro/attachments/6364/mini_CompilerMessageScreenshot.PNG

Best regards

Hello everybody,

I would like to make a function which creates a .mo file and which edits it.
Do you know if it is possible in OpenModelica? If yes, how?

By advance, thank you for your help!  current/smile

William Afonso

Mar-06-18 12:56:36
Category: Developer

Hello everybody,

I would like to use an User Interface to load the path of a file in the variables of my program. To give you an idea of what I am looking for, the equivalent function in Matlab would be "uigetfile". Do you know if a such Library exists under OpenModelica?

Thank you by advance for your help!  current/smile

William

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