interface between dealii and matlab for data transfer (using mex files?)

32 peržiūros
Praleisti ir pereiti prie pirmo neskaityto pranešimo

Simon

neskaityta,
2022-08-14 20:22:302022-08-14
kam: deal.II User Group
Dear all,

my question is specific and regards the data transfer between Matlab and a dealii program. In a nutshell, I have to call my dealii program from Matlab with a Matlab vector as argument (my dealii program needs the values in the vector). I figured out that Mex files are one way to transfer data to a c++ program.


Here is a snippet of my Matlab script:

function res = fun(p,y)
s = call_my_dealii_program_passing_the_vector_p
res = s - y;
end

My issue is *how to pass the vector 'p' to the dealii program*.

There are MEX files which provide an interface between Matlab and C++ programs. A standard MEX file looks like this:

//file mymex.cpp
#include "mex.h"
//include some more header files from c++ or dealii
//write here the whole class definition and implemenation

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//this is the interface function to Matlab where also 'p' is passed
//in here, I will also create the instance of my dealii program to run the typical member //functions ike make_grid(), system_setup(),...
}

The above mex file is compiled within the Matlab gui using a c++ compiler (mex mymex.cpp).

If I do not include any dealii headers in  mymex.cpp , the compilation works.
However, including dealii headers results in a bunch of error messages when I compile the program in Matlab (mex mymex.cpp), because environment variables are not known to Matlab,...
Usually, I just call 'spack load dealii' followed by 'make run' to run my dealii programs, but I do not know how to 'forward' all this information to the Matlab compilation process.

All that said, has someone already worked with Mex files together with dealii?

I also appreciate other approaches to manage the data transfer between Matlab and dealii.


Best
Simon

Bruno Turcksin

neskaityta,
2022-08-15 08:23:582022-08-15
kam: deal.II User Group
Simon,

You could just write/read files to transfer the data and you can launch your deal.II code from Matlab using https://www.mathworks.com/help/matlab/ref/system.html This way Matlab does not need to know anything about deal.II. You just write and read files, and you execute a bash command.

Best,

Bruno

Simon Wiesheier

neskaityta,
2022-08-15 08:52:302022-08-15
kam: dea...@googlegroups.com
Dear Bruno, 


I also thought about transferring the data by reading from/writing to a file. 
But I was looking for a more general approach without having to create files. 

I was also pondering if it is possible to compile the mex file like a normal dealii program (by adding a few Matlab macros to the files generated by cmake).
But I can not say how much is involved to do this due to my modest knowledge about cmake.  
If it were possible and someone wanted to guide me through that process, I would be willed to try that out; otherwise I go with the easier approach and work with files. 


Best
Simon

--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "deal.II User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dealii/beTOBvu5ko4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/f7904da5-4e91-408a-a8c3-c512db95cb8dn%40googlegroups.com.

Bruno Turcksin

neskaityta,
2022-08-15 09:05:572022-08-15
kam: dea...@googlegroups.com
Simon

Le lun. 15 août 2022 à 08:52, Simon Wiesheier <simon.w...@gmail.com> a écrit :

I also thought about transferring the data by reading from/writing to a file. 
But I was looking for a more general approach without having to create files. 

Writing files is no less general than coupling directly Matlab and deal.II The main advantage of coupling the two codes directly is that it is faster but it should only matter if you have to transfer a very large amount of data or if you do it very often.
 
I was also pondering if it is possible to compile the mex file like a normal dealii program (by adding a few Matlab macros to the files generated by cmake).
But I can not say how much is involved to do this due to my modest knowledge about cmake.  
If it were possible and someone wanted to guide me through that process, I would be willed to try that out; otherwise I go with the easier approach and work with files. 

The way you probably want to do that is to start from a CMakeLists.txt similar to the one that you are using to compile your deal.II code and use https://cmake.org/cmake/help/latest/module/FindMatlab.html to compile the mex file. I have never done it so I don't know if it "just works" or if it is more involved.

Best,

Bruno

Simon Sticko

neskaityta,
2022-08-15 10:50:552022-08-15
kam: dea...@googlegroups.com
Hi,

I have done the mex-coupling with dealii before. As Bruno said, it is easiest to mex with cmake. Example in attached zip. Hope it helps.


Best,
Simon Sticko

On 15/08/2022 15:05, Bruno Turcksin wrote:
> Simon
>
> Le lun. 15 août 2022 à 08:52, Simon Wiesheier <simon.w...@gmail.com <mailto:simon.w...@gmail.com>> a écrit :
>
>
> I also thought about transferring the data by reading from/writing to a file.
> But I was looking for a more general approach without having to create files.
>
> Writing files is no less general than coupling directly Matlab and deal.II The main advantage of coupling the two codes directly is that it is faster but it should only matter if you have to transfer a very large amount of data or if you do it very often.
>
> I was also pondering if it is possible to compile the mex file like a normal dealii program (by adding a few Matlab macros to the files generated by cmake).
> But I can not say how much is involved to do this due to my modest knowledge about cmake.
> If it were possible and someone wanted to guide me through that process, I would be willed to try that out; otherwise I go with the easier approach and work with files.
>
> The way you probably want to do that is to start from a CMakeLists.txt similar to the one that you are using to compile your deal.II code and use https://cmake.org/cmake/help/latest/module/FindMatlab.html <https://cmake.org/cmake/help/latest/module/FindMatlab.html> to compile the mex file. I have never done it so I don't know if it "just works" or if it is more involved.
>
> Best,
>
> Bruno
>
> --
> The deal.II project is located at http://www.dealii.org/ <http://www.dealii.org/>
> For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en <https://groups.google.com/d/forum/dealii?hl=en>
> ---
> You received this message because you are subscribed to the Google Groups "deal.II User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to dealii+un...@googlegroups.com <mailto:dealii+un...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/CAGVt9eMM_kwCXbSHZONL2p8icdeiwDF%3DQrvQ-qbyQFYxqEPL4g%40mail.gmail.com <https://groups.google.com/d/msgid/dealii/CAGVt9eMM_kwCXbSHZONL2p8icdeiwDF%3DQrvQ-qbyQFYxqEPL4g%40mail.gmail.com?utm_medium=email&utm_source=footer>.
mex_example.zip

Andreas Hegendörfer

neskaityta,
2022-08-16 14:10:292022-08-16
kam: dea...@googlegroups.com
Hi Simon,
maybe you can solve your problem with Matlab's C++ interface (https://de.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-c-1.html).
The interface allows you to access Matlab's command line via C++ code, and, thus, you can use the full capabilities of Matlab within your C++ code. 

You could do the following:

call function res = fun(p,y) in C++
transfer vector p from matlab to C++
run deal.ii calculation in C++
res = s - y;
end

BR Andreas


Virenfrei. www.avast.com

--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to the Google Groups "deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/b047d35d-b3e3-465b-88d8-c708343086bfn%40googlegroups.com.

Simon Wiesheier

neskaityta,
2022-08-18 09:55:552022-08-18
kam: dea...@googlegroups.com
Hi Andreas, Hi Simon,

your approaches work for me.
Thank you for pointing out!

Best regards
Simon

Atsakyti visiems
Atsakyti autoriui
Persiųsti
0 naujų pranešimų