Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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.

Variable exchange between Matlab and Comsol

Please login with a confirmed email address before reporting spam

Hello everyone,

I am running Comsol using the LiveLink with Matlab and am trying to do the following:

I have a value, dt, which is 0 everywhere in the beginning.
(I can either use dt=0, make an interpolation file x,y,dt(x,y), maybe make a Matlab fct.,...)
I run Comsol the first time.

Now, I use results ct calculated from the Matlab run to compute new dt values:

ct = mpheval(model, ct) // this also gives me access to the coordinates of the mesh points...
dt_new = f(ct.d1) // any function really

But how do I get Comsol to take dt_new at each meash point for the next run?!?
I've tried:

a) Using a simple variable dt.
Reassigning dt values by setIndex and using same indices as mpheval gives me.
--> Doesn't work.
b) Using an interpolation function reading dt.txt which contains: x y dt, initially: 0:1:x_max, 0:1:y_max, 0
Rewrite dt.txt to read: ct.p(1,:), ct.p(2,:), f(ct.d1)=dt_new (obtained by mpheval & Matlab calculation)
Now I have tried to: importData for the interpolation again, remove the interpolation & create a new one etc.
None of this works. It either gives weird errors (remove & create, importData) or doesn't do anything
(use solely setIndex)

What am I doing wrong in b)?
Or do I want to use a Matlab function under global definitions in Comsol?
(But how do I make that access values from a previous Matlab run?!?)

The problem to me seems to be simple:

Assign value dt=0 to mesh points. Run Comsol.
Get value ct at each mesh point. Calculate dt based on ct. Assign value dt to each mesh point. Run Comsol again.

It is straightforward to access the results at each mesh point.
Why is it so hard to assign values to mesh points for the next run?

Thanks in advance for any help.
Greets,
Thorsten

4 Replies Last Post 2013年7月15日 GMT-4 05:00

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2013年1月18日 GMT-5 16:46
Hello Thorsten,

I'm facing the same problem. I believe that having an interpolation function is a nice idea, but probably not the proper way to do it.

What I understood is that Matlab and Comsol exchange any information by means of what they call "dataset": solutions, mesh, etc... Then these are translated into matlab's arrays. So I guess we should create a dataset, give to it the values from Matlab (in the proper way... Comsol might need for structures data).

What I was looking for is a command that could introduce values from Matlab into a dataset. What I found so far, but not tested yet, is the following syntax:

whatever.data.set( <tag of what you change>, [your matlab values] )

Of course, the syntax it really does depend on what you want to import into comsol. the"whatever" may be either: mesh, solution, etc...

I believe that's the proper way to do things, even though I didn't have time to test it :-) So, let me know if you figure out something!

Mattia
Hello Thorsten, I'm facing the same problem. I believe that having an interpolation function is a nice idea, but probably not the proper way to do it. What I understood is that Matlab and Comsol exchange any information by means of what they call "dataset": solutions, mesh, etc... Then these are translated into matlab's arrays. So I guess we should create a dataset, give to it the values from Matlab (in the proper way... Comsol might need for structures data). What I was looking for is a command that could introduce values from Matlab into a dataset. What I found so far, but not tested yet, is the following syntax: whatever.data.set( , [your matlab values] ) Of course, the syntax it really does depend on what you want to import into comsol. the"whatever" may be either: mesh, solution, etc... I believe that's the proper way to do things, even though I didn't have time to test it :-) So, let me know if you figure out something! Mattia

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2013年1月21日 GMT-5 03:04
Hey,

thanks for the idea.
I tried something similar in a) with the setIndex command,
which seemed perfect. But failed, since I hadn't introduced
the variable as array.
Using structures sounds like a promising concept and
I will definitely look into that.
I will let you know, as soon as something works :)

Greets,
Thorsten
Hey, thanks for the idea. I tried something similar in a) with the setIndex command, which seemed perfect. But failed, since I hadn't introduced the variable as array. Using structures sounds like a promising concept and I will definitely look into that. I will let you know, as soon as something works :) Greets, Thorsten

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2013年1月22日 GMT-5 11:33
It works now with the help of the Comsol support.
The problem was the importData command,
which makes Comsol take the numbers and
disregard the file in the future. Without it,
it takes into account changes in the file:

model.func.create('int3', 'Interpolation');
model.func('int3').model('mod1');
model.func('int3').set('funcname', 'dt');
model.func('int3').set('source', 'file');
model.func('int3').set('filename', '...\dt.txt');
model.func('int3').setIndex('funcs', 'dt', 0, 0);
model.func('int3').set('defvars', 'on');
model.func('int3').name('dt');
model.sol('sol1').runAll;

% Modify dt.txt
model.sol('sol1').runAll;

% Modify dt.txt
model.sol('sol1').runAll;

Each Model is now run with a different dt as input,
which was calculated in Matlab based on the previous
Comsol results.

Still, the solution has the feel of a work-around and I
would wish for a more direct way. But: it is a lot
faster than I would have expected in my case.

Greets,
Thorsten
It works now with the help of the Comsol support. The problem was the importData command, which makes Comsol take the numbers and disregard the file in the future. Without it, it takes into account changes in the file: model.func.create('int3', 'Interpolation'); model.func('int3').model('mod1'); model.func('int3').set('funcname', 'dt'); model.func('int3').set('source', 'file'); model.func('int3').set('filename', '...\dt.txt'); model.func('int3').setIndex('funcs', 'dt', 0, 0); model.func('int3').set('defvars', 'on'); model.func('int3').name('dt'); model.sol('sol1').runAll; % Modify dt.txt model.sol('sol1').runAll; % Modify dt.txt model.sol('sol1').runAll; Each Model is now run with a different dt as input, which was calculated in Matlab based on the previous Comsol results. Still, the solution has the feel of a work-around and I would wish for a more direct way. But: it is a lot faster than I would have expected in my case. Greets, Thorsten

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2013年7月15日 GMT-4 05:00
Hello Thorsten,

Can you please explain what the support told you about the problem?

I am facing a similar one. I receive a variable with mpheval to Matlab. It comes as a struct. I want to send the values into a function in Matlab so that I can modify the values slightly and then send the struct back to Comsol.

I could not understand how I could do this from your post. Can you please help?

Thank you in advance.
Onur
Hello Thorsten, Can you please explain what the support told you about the problem? I am facing a similar one. I receive a variable with mpheval to Matlab. It comes as a struct. I want to send the values into a function in Matlab so that I can modify the values slightly and then send the struct back to Comsol. I could not understand how I could do this from your post. Can you please help? Thank you in advance. Onur

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.