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.

Setting an initial solution from Matlab

Please login with a confirmed email address before reporting spam

Good afternoon Comsol community.

I am working with Matlab Livelink and I want to set an initial solution vector of a comsol model and use it in a loop.

The documentaion says the following: To extract the solution vector of the computed solution, run the function mphgetu as in this command:

U = mphgetu(model);

So here is my code idea:

import com.comsol.model.* import com.comsol.model.util.* modelin = mphopen('C:\Users\Lenovo\Desktop\Two_layers_modifiedDIC2020.mph') componin = modelin.component geometrin = modelin.component('comp1').geom('geom1')

fr_ini = app.FrecuenciainicialEditField.Value; fr_fin = app.FrecuenciafinalEditField.Value;

for i = fr_ini:10:fr_fin

                %%%CODE TO SET THE SOLUTION VECTOR TO WORK%%%
                modelin.param.set('f', fr_ini);
                modelin.study('std2').run;
                subplot(1,1,1);
                mphplot(modelin,'pg17');
                u = mphgetu(modelin)
end

Thanks in advance.

1 Reply Last Post 2021年1月12日 GMT-5 03:17
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 4 years ago 2021年1月12日 GMT-5 03:17

Hi Victor

Using u = mphgetu(modelin) as a means to set the initial conditions should only be used when there are no alternatives.

Usually you would set the initial conditions simly by using the Initial Condition node for your physics. You can either set the initial value to a constant or use an interpolation function - either can be based on a previously solved model.

If you must you can extract the solution using mphgetu, but you have to use the COMSOL API to insert the extracted values into the model again. See the method setU in the Programming Reference Manual. You may have to the info from mphxmeshinfo to learn where different parts of the solution ends up in the solution vector u.

-------------------
Lars Gregersen
Comsol Denmark
Hi Victor Using u = mphgetu(modelin) as a means to set the initial conditions should only be used when there are no alternatives. Usually you would set the initial conditions simly by using the Initial Condition node for your physics. You can either set the initial value to a constant or use an interpolation function - either can be based on a previously solved model. If you must you _can_ extract the solution using mphgetu, but you have to use the COMSOL API to insert the extracted values into the model again. See the method setU in the Programming Reference Manual. You may have to the info from mphxmeshinfo to learn where different parts of the solution ends up in the solution vector u.

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.