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.

How to implement an external calculation into a COMSOL MULTIPHYSICS study

Please login with a confirmed email address before reporting spam

Hello everybody

I've been looking for an answer on the COMSOL forum but nothing helped me so far.

Here's my problem. I would like to calculate a quantity (cracking energy) but COMSOL does not propose it. So I would like to write a program (on Matlab for instance) which calls values from COMSOL (principal strains, principal stretch, etc.), then I'll make some calculation on it and i'll send the results back into COMSOL so i can visualize it in 3D.

My questions are :

  • Which language should I choose to write such a program ?

  • How do I implement it in my COMSOL study ?

  • I heard about LiveLink. If this is the solution, how do I use that functionnality ?

I thank all of you for your precious time !

Scientifically yours.


4 Replies Last Post 6 ott 2021, 11:28 GMT-4
Jim Freels mechanical side of nuclear engineering, multiphysics analysis, COMSOL specialist

Please login with a confirmed email address before reporting spam

Posted: 3 years ago 5 ott 2021, 18:26 GMT-4

First question: Have you looked at the capabilities, and/or considered just adding the equations you needed solved for your external calculation just using COMSOL itself ? That is the simplest way actually, if you take the time to do that. I would be surprised if your algorithm cannot be fit into COMSOL straight out.

If this is not possible (too complex, or have existing code already, etc.), COMSOL basically handles two different external programming languages to the user: MATLAB (requires a license), or JAVA (also requires a license, but can be free). From there, many additional languages can be linked into either MATLAB or JAVA, so the possibilities are many. For example, our group linked FORTRAN code into COMSOL via MATLAB. You will need to follow the COMSOL API in the documentation. MATLAB livelink is certainly an advantage for doing this if you want to go the MATLAB route, and I think a required purchase from COMSOL.

One quick recommendation just to give yourself an idea is to take a simple COMSOL model that you have running, and then save it as a MATLAB file and/or JAVA file using the "save as" selection from the file menu item (normally, if you just click save it defaults to the .mph file extension). Then go take a look at the resulting files using your favorite editor. This might give you a bit of an idea of what is involved.

COMSOL developers please chime in on this question.

-------------------
James D. Freels, Ph.D., P.E.
First question: Have you looked at the capabilities, and/or considered just adding the equations you needed solved for your external calculation just using COMSOL itself ? That is the simplest way actually, if you take the time to do that. I would be surprised if your algorithm cannot be fit into COMSOL straight out. If this is not possible (too complex, or have existing code already, etc.), COMSOL basically handles two different external programming languages to the user: MATLAB (requires a license), or JAVA (also requires a license, but can be free). From there, many additional languages can be linked into either MATLAB or JAVA, so the possibilities are many. For example, our group linked FORTRAN code into COMSOL via MATLAB. You will need to follow the COMSOL API in the documentation. MATLAB livelink is certainly an advantage for doing this if you want to go the MATLAB route, and I think a required purchase from COMSOL. One quick recommendation just to give yourself an idea is to take a simple COMSOL model that you have running, and then save it as a MATLAB file and/or JAVA file using the "save as" selection from the file menu item (normally, if you just click save it defaults to the .mph file extension). Then go take a look at the resulting files using your favorite editor. This might give you a bit of an idea of what is involved. COMSOL developers please chime in on this question.

Henrik Sönnerlind COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 3 years ago 6 ott 2021, 03:06 GMT-4

As I understand the question, you are only interested in having your own variables for postprocessing. Then, I agree with Jim, that it is unlikely that you need anything external. It will most probably be possible to add your own variables under Definitions->Variables.

In the more general case that you want your own material model that affects stiffness matrices and residuals, then there are several different approaches. Modifying things inside COMSOL would still be the first path to explore. Maybe adding distributed ODEs or states. If that does not suffice, the next option is to use an External Material. Then you program the material model in C (or any language that you can call from C). The code is compiled and linked into COMSOL Multiphysics.

-------------------
Henrik Sönnerlind
COMSOL
As I understand the question, you are only interested in having your own variables for postprocessing. Then, I agree with Jim, that it is unlikely that you need anything external. It will most probably be possible to add your own variables under Definitions->Variables. In the more general case that you want your own material model that affects stiffness matrices and residuals, then there are several different approaches. Modifying things inside COMSOL would still be the first path to explore. Maybe adding distributed ODEs or states. If that does not suffice, the next option is to use an External Material. Then you program the material model in C (or any language that you can call from C). The code is compiled and linked into COMSOL Multiphysics.

Please login with a confirmed email address before reporting spam

Posted: 3 years ago 6 ott 2021, 03:21 GMT-4

Thank all of you for your answers. The fact is that I need to calculate the cracking energy for each direction of the space and find the direction that maximizes its value. I'm not sure COMSOL manages such calculation involving loops. Does it ?

Anyway, I'll try to download the LiveLink option to see how it works !

I'll come back on this discussion to explain how I did or didn't manage to implement my algorithm.

Thank all of you for your answers. The fact is that I need to calculate the cracking energy for each direction of the space and find the direction that maximizes its value. I'm not sure COMSOL manages such calculation involving loops. Does it ? Anyway, I'll try to download the LiveLink option to see how it works ! I'll come back on this discussion to explain how I did or didn't manage to implement my algorithm.

Henrik Sönnerlind COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 3 years ago 6 ott 2021, 11:28 GMT-4

If you have that type of algorithm, you probably need programming.

You can for example use Global->Functions->External or Global->Functions->MATLAB. Using such functions, it would be possible for you to define variables in the domain that contain the results you are looking for.

-------------------
Henrik Sönnerlind
COMSOL
If you have that type of algorithm, you probably need programming. You can for example use Global->Functions->External or Global->Functions->MATLAB. Using such functions, it would be possible for you to define variables in the domain that contain the results you are looking for.

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.