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.
Pass a vector calculated by Matlab to Comsol
Posted 12.12.2019, 10:27 GMT-5 LiveLink for MATLAB Version 5.4 6 Replies
Please login with a confirmed email address before reporting spam
Does anyone know how to pass a vector calculated by Matlab to a variable or material property in Comsol? (except using the interpolation function or external matlab function). Just like using a simple API syntax "model.variable('var1').set('E', num2str(E0))" to pass a Matlab integer or double variable to a Comsol variable.
All the best, Songcai
Please login with a confirmed email address before reporting spam
Hi Songcai
For most properties you can simply do this
model.variable('var1').set('E', E0)
You don't need to use num2str if E0 is just a number. Some properties can be vectors, but variables are defined by a single expression (which can be a number).
Perhaps if you explain in more detail what you are trying to do we can come up with more help...
-------------------Lars Gregersen
Comsol Denmark
Please login with a confirmed email address before reporting spam
Thanks for your reply, Lars .
Please login with a confirmed email address before reporting spam
Hi Songcai
For most properties you can simply do this
model.variable('var1').set('E', E0)
You don't need to use num2str if E0 is just a number. Some properties can be vectors, but variables are defined by a single expression (which can be a number).
Perhaps if you explain in more detail what you are trying to do we can come up with more help...
Thanks for your reply, Lars .
I define a local variable that is related to some field variables (such as stresses and fluid pressure) at the previous time step. I know this can be achieved by adding a domain ODE to store the field variables (stresses and fluid pressure) at the previous time step. But I also need to repeatedly calculate the multiphysics coupling problem at this time step until the local variable satisfies the convergence criterion. So, I adopt the Livelink for Matlab to control the solution process and modify the local variable. In the Matlab script, I extract the relevant field variables (stresses and fluid pressure), and then calculate the required local variable. This problem is how to pass the calculated variable (it is a vector, the same size as the field variable) to the local variable in Comsol? I read a lot of your threads and they are very constructive. I think there might be three feasible methods, but I'm not sure. So I need you to correct it.
(1) Define a global Matlab function related to the local variable in Comsol. I am not sure whether this data can be transferred in real time between comsol and matlab. Do I need to check the box 'Clear functions automatically before solving'?
(2) Define a interpolation function in Comsol to load the local variable data saved in matlab. I sure the this data can be transferred in real time by using 'Refresh'.
(3) Define a domain ODE for the local variable in Comsol, use the getU and setU to reset the local variable at each time step.
Also, which method is more efficient ?
Best wishes! Songcai
Please login with a confirmed email address before reporting spam
I have tried all the methods, the last two methods work. There are some problems with Method 1. We cannot guarantee the same vector size. For example, in Comsol, we define a Matlab function V = myfun (x, y) related to coordinates x, y. In matlab, we extract the relevant field variables using mpheval, and then calculate the required variable V. However, when Comsol calls myfun (x, y), it will appear: Incorrect size of returned vector, ie length ( x) is not equal to length (V).
I don't know if I made a mistake somewhere, can anyone give some advice?
Best regards,
Songcai
Please login with a confirmed email address before reporting spam
When using MATLAB functions while solving we send in a group of arguments values (here x and y) since calls from Comsol to Matlab are expensive. It is the task of your function to return a value V for each set of coordinate values and hence V will have the same length as x and y. You should test this on the command line like this: V = myfun((1:10)', (1:10)') and see how it goes. Why not use mphinterp instead of mpheval to get values at the correct coordinates?
It looks as if you are trying to implement a meterial model!? Have you checked that Comsol Multiphysics doesn't have something built in you can use: https://www.comsol.com/nonlinear-structural-materials-module
Adding additional field variables such as ODEs to your model to handle the memory effect seems like the most appropiate solution. I'm a but confused about this sentence "But I also need to repeatedly calculate the multiphysics coupling problem at this time step until the local variable satisfies the convergence criterion". Often it is not necessary to perform these calculations outside of Comsol. If you can formulate your constraints as equations you can use Comsol's solvers to solve everything.
-------------------Lars Gregersen
Comsol Denmark
Please login with a confirmed email address before reporting spam
Thanks, Lars
The mphinterp function can resolve the problem of "Incorrect size of returned vector". I am simulating a damage problem involving the deterioration of material properties. Therefore, when the material is damaged at a certain step, I need to repeatedly calculate this coupling problem in this step until the material properties no longer change under new stresses, and then proceed to the next step. The computational time is very expensive when using Matlab to control the solution process (a strict time step), so I will try to implement it through Comsol according to your advice.
Best,
Songcai
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.
Suggested Content
- BLOG Optimizing Microwave Ovens with Solid-State RF Cooking
- KNOWLEDGE BASE COMSOL Does Not Start with the Latest MATLAB Version
- BLOG Optimizing the Geometry of Optical Antennas with Genetic Algorithms
- KNOWLEDGE BASE Problems Launching COMSOL Server™ 5.3 with MATLAB®
- BLOG Maximum Evaluations on Parallel Sections