Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

External function in C - error "Wrong architecture"

Please login with a confirmed email address before reporting spam

Hi,

I am using COMSOL 4.0a on mac os X.

I want to write external functions in C and use these in COMSOL.

Here is an exemple

file add.c

#include "add.h"
int add(int a, int b)
{
return a+b;
}

file add.h
int add(int,int)

I get my library by using

cc -o libadd.dylib add.c -dynamiclib

But when I try to use my library in comsol, I have the error

"wrong architecture"

Does anyone knows what's the trick ?

Thanks for your help,

Sébastien

11 Replies Last Post 15 nov. 2010, 08:37 UTC−5
COMSOL Moderator

Hello Sébastien Janas

Your Discussion has gone 30 days without a reply. If you still need help with COMSOL and have an on-subscription license, please visit our Support Center for help.

If you do not hold an on-subscription license, you may find an answer in another Discussion or in the Knowledge Base.


Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 4 nov. 2010, 03:31 UTC−4
Nobody have used an external function wrote in C with comsol 4.0a ???
Nobody have used an external function wrote in C with comsol 4.0a ???

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 4 nov. 2010, 06:41 UTC−4
Hi Sebastien,
if you are trying to solve a numerical problem in C
I suggest you use matlab.
there have been more success with matlab working with comsol than functions written is C.

Hi Sebastien, if you are trying to solve a numerical problem in C I suggest you use matlab. there have been more success with matlab working with comsol than functions written is C.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 4 nov. 2010, 06:58 UTC−4
Hi Zvika,

Thanks for your answer.

I have tried this possibility, but I didn't managed to make matlab functions working. Do you have somewhere a good tuto on the use of matlab functions in comsol ?

For exemple, here is what I have tried

a simple function add.m

--------------------------------
function F = add(a,b)
F = a+b;
--------------------------------

I put this file on my desktop, and my desktop is in the path of matlab.

Then I lauch COMSOL and COMSOL SERVER, I connect COMSOL to the server.

I create a MATLAB function with global definitions->function where I put "add" in the function case, and the two arguments a,b in the arguments case.

When I want to use my add function, I have an error "Failed to load library".

Hi Zvika, Thanks for your answer. I have tried this possibility, but I didn't managed to make matlab functions working. Do you have somewhere a good tuto on the use of matlab functions in comsol ? For exemple, here is what I have tried a simple function add.m -------------------------------- function F = add(a,b) F = a+b; -------------------------------- I put this file on my desktop, and my desktop is in the path of matlab. Then I lauch COMSOL and COMSOL SERVER, I connect COMSOL to the server. I create a MATLAB function with global definitions->function where I put "add" in the function case, and the two arguments a,b in the arguments case. When I want to use my add function, I have an error "Failed to load library".

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 7 nov. 2010, 09:05 UTC−5
when I had problems with my matlab functions in comsol
I contacted the support and it came down to these things:
1. you're function should be able to receive vectors and return vectors.
when you solve your model with a matlab function included, a matlab window need to pop up. if it doesn't you need to check these things:
2. the path where the m file is have to be added to the matlab path.
3. the comsol's setting to know where matlab is should point to where matlab's bin library is
for example C:\Program Files\MATLAB\R2010a\bin

I hope my advices are helful
when I had problems with my matlab functions in comsol I contacted the support and it came down to these things: 1. you're function should be able to receive vectors and return vectors. when you solve your model with a matlab function included, a matlab window need to pop up. if it doesn't you need to check these things: 2. the path where the m file is have to be added to the matlab path. 3. the comsol's setting to know where matlab is should point to where matlab's bin library is for example C:\Program Files\MATLAB\R2010a\bin I hope my advices are helful

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 8 nov. 2010, 01:10 UTC−5
Great thanks Zvika.

I tried, but it don't works.

Do you connect Comsol to the server when you use a matlab function ?

I have changed the comsol settings in comsol.ini and comsolserver.ini, is there an other file ?

Best regards,

Sébastien
Great thanks Zvika. I tried, but it don't works. Do you connect Comsol to the server when you use a matlab function ? I have changed the comsol settings in comsol.ini and comsolserver.ini, is there an other file ? Best regards, Sébastien

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 11 nov. 2010, 05:24 UTC−5
Hi, Sebastien

the comsol-matlab connection is made by comsol when you run a matlab function
in comsol. the manual connection is not needed. a small window of matlab should appear when
you run the model.
I suggest you change the comsol settings in all the ini files.
try this as well -
Open a Command Prompt (DOS box) and exeute the following commands

cd C:\Program Files\MATLAB\R2010a\bin
matlab -regserver

then restart

Zvika
Hi, Sebastien the comsol-matlab connection is made by comsol when you run a matlab function in comsol. the manual connection is not needed. a small window of matlab should appear when you run the model. I suggest you change the comsol settings in all the ini files. try this as well - Open a Command Prompt (DOS box) and exeute the following commands cd C:\Program Files\MATLAB\R2010a\bin matlab -regserver then restart Zvika

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 11 nov. 2010, 09:54 UTC−5

--------------------------------
function F = add(a,b)
F = a+b;
--------------------------------

Sébastien




1. you're function should be able to receive vectors and return vectors.
when you solve your model with a matlab function included, a matlab window need to pop up. if it doesn't you need to check these things:

Zvika



Hi Zvika
please if you guys don't mind i am interest in this discussion also,

going back to Sébastien's matlab function above given a =[2 18 4 0]'; b=[-2 0.051 0 -1]';, hence F is a 4x1 vector

Zvika, were you successful in calling this function in COMSOL40a? if so can you share a simple mph file implementing this matlab function call?

thanks in advance, i just want to make sure we are not pursing a "ghost idea".

--
merci

XIfe
[QUOTE] -------------------------------- function F = add(a,b) F = a+b; -------------------------------- Sébastien [/QUOTE] [QUOTE] 1. you're function should be able to receive vectors and return vectors. when you solve your model with a matlab function included, a matlab window need to pop up. if it doesn't you need to check these things: Zvika [/QUOTE] Hi Zvika please if you guys don't mind i am interest in this discussion also, going back to Sébastien's matlab function above given a =[2 18 4 0]'; b=[-2 0.051 0 -1]';, hence F is a 4x1 vector Zvika, were you successful in calling this function in COMSOL40a? if so can you share a simple mph file implementing this matlab function call? thanks in advance, i just want to make sure we are not pursing a "ghost idea". -- merci XIfe

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 11 nov. 2010, 20:00 UTC−5
Hi everybody,

In fact, the problem for me was that I am using MATLAB 2008, and it seems that the use of matlab function is only possible with MATLAB 2009. I tried with a version of MATLAB 2009 and it works fine.

Best regards,

Sébastien
Hi everybody, In fact, the problem for me was that I am using MATLAB 2008, and it seems that the use of matlab function is only possible with MATLAB 2009. I tried with a version of MATLAB 2009 and it works fine. Best regards, Sébastien

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 12 nov. 2010, 10:12 UTC−5
Sébastien,

awesome! please can describe how you did this? i will really be grateful.

did you use Definition - Functions - Analytic or what? to call "add.m" file?


--
merci
Sébastien, awesome! please can describe how you did this? i will really be grateful. did you use Definition - Functions - Analytic or what? to call "add.m" file? -- merci

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 12 nov. 2010, 12:06 UTC−5
Hi,

In fact it's really simple if you use MATLAB 2009.

In global definition -> function -> matlab I just wrote add in the "function" case and a,b in the "arguments" case.

I have added the path of my function in matlab.

I can use my function add.m in a analytic function of comsol, or in a global variable.

It's a pity that you have to get the last version of matlab to use this functionality. My lab can't buy the new version every year.
Hi, In fact it's really simple if you use MATLAB 2009. In global definition -> function -> matlab I just wrote add in the "function" case and a,b in the "arguments" case. I have added the path of my function in matlab. I can use my function add.m in a analytic function of comsol, or in a global variable. It's a pity that you have to get the last version of matlab to use this functionality. My lab can't buy the new version every year.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 15 nov. 2010, 08:37 UTC−5
Hi,

indeed the comsol only works with the most recent matlab 2009,2010.
I forgot to ask if youre using one of those.
I'm happy it works for you.

Zvika
Hi, indeed the comsol only works with the most recent matlab 2009,2010. I forgot to ask if youre using one of those. I'm happy it works for you. Zvika

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.