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 modifications between solution iterations

Please login with a confirmed email address before reporting spam

I'm developing a transient model using the Livelink interface. The model first calculates an initial solution for the first time window, then I would like to modify the state variable values due to some external disturbance using MATLAB, then proceed to rerun the model for the next time window using the newly adjusted values of some of the variables, but the other variables should be retained from the previous solution iteration.

When I prescribe the use of the previous solution for subsequent iterations as:

model.sol('sol1').feature('v1').set('initmethod', 'sol');
model.sol('sol1').feature('v1').set('initsol', 'sol1');

it seems that the specification of initial conditions through either

model.variable('var15').set('X0', num2str(X0));

or

model.init('init4').set('position',num2str(X0));

are ignored completely and the unchanged values from the previous solution are retained. Is there a plausible method to effectively update some of the variables this way? Perhaps by modifying the dataset of the previous solution?

Thanks.

18 Replies Last Post Jan 31, 2017, 7:15 a.m. EST

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago May 4, 2011, 7:39 a.m. EDT
I have appended the following very simple example to demonstrate the issue I'm hoping to solve.

Executing the short script main_example.m loads and executes the model defined in simple_example.m (which is an m-file export of simple_example.mph). The time list is then updated, the initial values for variables are set to the solution of the previous step, and a variable is arbitrarily initialized to a new value to simulate some external disturbance, and the solution is computed for the next time list. It seems as though the variable update step is disregarded by COMSOL when initial conditions are specified by an existing solver, is there a way to accomplish this?

Any help would be greatly appreciated.

Thanks.
I have appended the following very simple example to demonstrate the issue I'm hoping to solve. Executing the short script main_example.m loads and executes the model defined in simple_example.m (which is an m-file export of simple_example.mph). The time list is then updated, the initial values for variables are set to the solution of the previous step, and a variable is arbitrarily initialized to a new value to simulate some external disturbance, and the solution is computed for the next time list. It seems as though the variable update step is disregarded by COMSOL when initial conditions are specified by an existing solver, is there a way to accomplish this? Any help would be greatly appreciated. Thanks.


Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Jul 29, 2011, 2:10 a.m. EDT
Hi Aaron,
I have a problem quite similar to your. If you solved your problem please share your solution.

In detail:
I am looking to run a comsol model for a given period of time and then transfer the comsol results into a matlab program. These conditions will be modified inside of matlab and will then be sent back to comsol as the new initial conditions. Comsol will then run for another period of time and will once again transfer its results back to matlab for modification and so on and so forth.



In comsol 4.1 say U the initial condition I'd like to pass this vector to comsol and I'd like to use this vector as initial condition in Comsol:
I have tried the following line command:
model.init('init1').set('init','U');
but comsol ignore this condition.
I really appreciate any suggestion.
Best,
erasmo

P.s.
I'm able to solve this problem in comsol 3.5 in a simple way;
fem.sol=femtime(fem,'init',U, ...

here U is my vector state that return to Comsol integrator

I'm trying to solve the same problem in comsol 4.1 but the time integrator only accept in 'initmethod' a physically defined initial condition or a previous comsol solution.
Hi Aaron, I have a problem quite similar to your. If you solved your problem please share your solution. In detail: I am looking to run a comsol model for a given period of time and then transfer the comsol results into a matlab program. These conditions will be modified inside of matlab and will then be sent back to comsol as the new initial conditions. Comsol will then run for another period of time and will once again transfer its results back to matlab for modification and so on and so forth. In comsol 4.1 say U the initial condition I'd like to pass this vector to comsol and I'd like to use this vector as initial condition in Comsol: I have tried the following line command: model.init('init1').set('init','U'); but comsol ignore this condition. I really appreciate any suggestion. Best, erasmo P.s. I'm able to solve this problem in comsol 3.5 in a simple way; fem.sol=femtime(fem,'init',U, ... here U is my vector state that return to Comsol integrator I'm trying to solve the same problem in comsol 4.1 but the time integrator only accept in 'initmethod' a physically defined initial condition or a previous comsol solution.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Sep 2, 2012, 2:28 p.m. EDT
Hi everybody,

I 'm facing a similar problem. I need to update a comsol variable with a matlab variable.
More explicitly, I want to use add some noise to the previous solution, before being used as the next initial solution.
But, i'm getting the following error. A scalar value is expected.
Does anyone knows, why I' m getting this error?

Any help will be greatly appreciated,
Hi everybody, I 'm facing a similar problem. I need to update a comsol variable with a matlab variable. More explicitly, I want to use add some noise to the previous solution, before being used as the next initial solution. But, i'm getting the following error. A scalar value is expected. Does anyone knows, why I' m getting this error? Any help will be greatly appreciated,

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Sep 3, 2012, 3:09 a.m. EDT
Hi,


Sorry, I don't have time to read your code.

But I had a similar problem several months ago, but I solved it in the following manner. What you see below is only an extract of my much larger code. But I hope you can follow it.

If you find a better way, please post it for my benefit.



Suresh


--------------------------------------------------

% EXTRACT OF A LARGER CODE FOR DEMONSTRATION

% Load the model and set parameters
model = mphload('ex11_phrq_2D');
model.param.set('t0', tinit);
model.param.set('dt', dtim);
model.param.set('tmax', tmax1);

% extract mesh info from comsol
info = mphxmeshinfo(model,'solname', 'sol1', 'studysteptag', 'st1');
nodofs=length(info.fieldndofs);
my.NumberOfNodes = length(info.dofs.nodes)/nodofs;

for i = 1:tend

% Run the study.
model.study('std1').run;

% obtain parameter values
PVals = model.sol('sol1').getPVals;
lstep = length(PVals);
if (PVals(lstep) <= tfinal)

% Extract the concentration at nodal points at the last time step value to
% send it to external program

Ulast = model.sol('sol1').getU(lstep);
Details deleted

% Obtain the updated value in a vector called InComsol

Details deleted

% Set the new solution vector back into comsol, reset only the last time
% step value with new results stored in the array InComsol
for j = 1:length(PVals)-1;
U(j,:) = model.sol('sol1').getU(j);
end

for j = 1:length(PVals)-1;
model.sol('sol1').setU(j,U(j,:));
end
model.sol('sol1').setU(lstep,InComsol);
model.sol('sol1').setPVals(PVals);
model.sol('sol1').createSolution;
model.sol('sol1').feature('v1').set('initmethod', 'sol');
model.sol('sol1').feature('v1').set('initsol', 'sol1');

% Retreive the last time step value
t_init=PVals(lstep);

% Update the initial time for the new calculation
model.param.set('t0',t_init);

% Display message.
disp(sprintf('Time into analysis - %d',PVals(lstep)));

else
disp(sprintf('Problem completed. %d'));
break;
end
end
Hi, Sorry, I don't have time to read your code. But I had a similar problem several months ago, but I solved it in the following manner. What you see below is only an extract of my much larger code. But I hope you can follow it. If you find a better way, please post it for my benefit. Suresh -------------------------------------------------- % EXTRACT OF A LARGER CODE FOR DEMONSTRATION % Load the model and set parameters model = mphload('ex11_phrq_2D'); model.param.set('t0', tinit); model.param.set('dt', dtim); model.param.set('tmax', tmax1); % extract mesh info from comsol info = mphxmeshinfo(model,'solname', 'sol1', 'studysteptag', 'st1'); nodofs=length(info.fieldndofs); my.NumberOfNodes = length(info.dofs.nodes)/nodofs; for i = 1:tend % Run the study. model.study('std1').run; % obtain parameter values PVals = model.sol('sol1').getPVals; lstep = length(PVals); if (PVals(lstep)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Sep 24, 2012, 10:03 p.m. EDT
I met the same problem with you. Did you solve this problem? How? Can you send your code to me? Thank you very much. My email is: yli70@utk.edu
I met the same problem with you. Did you solve this problem? How? Can you send your code to me? Thank you very much. My email is: yli70@utk.edu

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Sep 24, 2012, 11:01 p.m. EDT
Hello,
I find the error, the load code: model = mphload('1d.spde.mph');
should be before the loop, i.e. k=1:10.
That’s, if you put model = mphload('1d.spde.mph');
Behind the loop, the set order will not change any parameter.
Hope it helps!
Hello, I find the error, the load code: model = mphload('1d.spde.mph'); should be before the loop, i.e. k=1:10. That’s, if you put model = mphload('1d.spde.mph'); Behind the loop, the set order will not change any parameter. Hope it helps!

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 20, 2012, 11:12 a.m. EST
Hi there,

Your piece of code works very well.
I have one more question: how do you save intermediate results for later access?

Cheers
Hi there, Your piece of code works very well. I have one more question: how do you save intermediate results for later access? Cheers

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 20, 2012, 1:09 p.m. EST
Hi,
My code works well now.
If you want to save intermediate result, you can save it into .txt file(just one line code), and in the next loop, you can upload the .txt file(one line code), which is very convenient. Hope it helps you.
Yukun
Hi, My code works well now. If you want to save intermediate result, you can save it into .txt file(just one line code), and in the next loop, you can upload the .txt file(one line code), which is very convenient. Hope it helps you. Yukun

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 29, 2013, 4:29 a.m. EDT
To find some of possible solutions of the problem look at the example:

mphstart();
import com.comsol.model.*;
import com.comsol.model.util.*;
%% parameter initialization
...
%% COMPUTATION
init.solID = 'sol1';
init.soluseID = 'sol2';
init.dsetID = 'dset1';
Model.sol('sol1').clearSolution();
Model.sol('sol2').clearSolution();
Model.sol('sol1').feature('v1').set('initmethod','init');
Model.sol('sol1').feature('v1').set('initsol','zero');
Model.sol('sol1').run('v1');
%%
for i = 1:info.imax
...
if strcmp(init.solID,'sol1')
init.solID = 'sol2';
init.soluseID = 'sol1';
init.dsetID = 'dset2';
elseif strcmp(init.solID,'sol2')
init.solID = 'sol1';
init.soluseID = 'sol2';
init.dsetID = 'dset1';
end;
Model.sol(init.solID).feature('v1').set('initmethod','sol');
Model.sol(init.solID).feature('v1').set('initsol',init.soluseID);
Model.sol(init.solID).feature('v1').set('solnum',solnum);
Model.result().numerical('pev1').set('data',init.dsetID);
...
Model.sol(init.solID).runAll;
...
U = Model.result().numerical('pev1').getReal()';
...
end;

How you can see I use the solver twins to initialize the next solution from the last.

But there is one more problem. The next solution in the inital point has a discontinuity.
To find some of possible solutions of the problem look at the example: mphstart(); import com.comsol.model.*; import com.comsol.model.util.*; %% parameter initialization ... %% COMPUTATION init.solID = 'sol1'; init.soluseID = 'sol2'; init.dsetID = 'dset1'; Model.sol('sol1').clearSolution(); Model.sol('sol2').clearSolution(); Model.sol('sol1').feature('v1').set('initmethod','init'); Model.sol('sol1').feature('v1').set('initsol','zero'); Model.sol('sol1').run('v1'); %% for i = 1:info.imax ... if strcmp(init.solID,'sol1') init.solID = 'sol2'; init.soluseID = 'sol1'; init.dsetID = 'dset2'; elseif strcmp(init.solID,'sol2') init.solID = 'sol1'; init.soluseID = 'sol2'; init.dsetID = 'dset1'; end; Model.sol(init.solID).feature('v1').set('initmethod','sol'); Model.sol(init.solID).feature('v1').set('initsol',init.soluseID); Model.sol(init.solID).feature('v1').set('solnum',solnum); Model.result().numerical('pev1').set('data',init.dsetID); ... Model.sol(init.solID).runAll; ... U = Model.result().numerical('pev1').getReal()'; ... end; How you can see I use the solver twins to initialize the next solution from the last. But there is one more problem. The next solution in the inital point has a discontinuity.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 20, 2013, 10:30 a.m. EST
Hi, Yukun,

Which kind of command were you using to change intermediate solutions? And how did you loop over time? Could you please post part of codes that show how to do it? I recently need to edit the intermediate solutions during solving solute transport. Thanks very much in advance.

-Lichun
Hi, Yukun, Which kind of command were you using to change intermediate solutions? And how did you loop over time? Could you please post part of codes that show how to do it? I recently need to edit the intermediate solutions during solving solute transport. Thanks very much in advance. -Lichun

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 20, 2013, 9:37 p.m. EST
Dear Lichun,
It has been long time, and I forget something, but as far as I know, if you can use "COMSOL" to finish something, then you CAN finish the same objective using "MATLAB WITH COMSOL" by writing code, so "MATLAB WITH COMSOL" is more powerful.

The following the my code to read .txt file and output the intermediate result to .txt file in "MATLAB WITH COMSOL" .(Find what you want since I just copied all of them)

%update u_interp and u_interp2 by uploading uh and vh files
%model.func.create('int1', 'Interpolation');
%model.func.create('int2', 'Interpolation');
model.func('int1').set('funcs', {'u_interp' '1'});
model.func('int1').set('source', 'file');
model.func('int1').set('filename', 'C:\Users\yli70\uh.txt');
model.func('int1').set('struct', 'grid');
model.func('int1').set('extrap', 'linear');
%discrete derivative data
model.func('int2').set('funcs', {'u_interp2' '1'});
model.func('int2').set('source', 'file');
model.func('int2').set('filename', 'C:\Users\yli70\vh.txt');
model.func('int2').set('struct', 'grid');
model.func('int2').set('extrap', 'linear');


%output u,v to uh and vh files
fid=fopen('C:\Users\yli70\uh.txt','wt');
for i=1:(M+1)
fprintf(fid,'%12.12f %12.12f\n',x_grid(i),u(i));
end
fclose(fid);

fid=fopen('C:\Users\yli70\vh.txt','wt');
for i=1:(M+1)
fprintf(fid,'%12.12f %12.12f\n',x_grid(i),v(i));
end
fclose(fid);
Dear Lichun, It has been long time, and I forget something, but as far as I know, if you can use "COMSOL" to finish something, then you CAN finish the same objective using "MATLAB WITH COMSOL" by writing code, so "MATLAB WITH COMSOL" is more powerful. The following the my code to read .txt file and output the intermediate result to .txt file in "MATLAB WITH COMSOL" .(Find what you want since I just copied all of them) %update u_interp and u_interp2 by uploading uh and vh files %model.func.create('int1', 'Interpolation'); %model.func.create('int2', 'Interpolation'); model.func('int1').set('funcs', {'u_interp' '1'}); model.func('int1').set('source', 'file'); model.func('int1').set('filename', 'C:\Users\yli70\uh.txt'); model.func('int1').set('struct', 'grid'); model.func('int1').set('extrap', 'linear'); %discrete derivative data model.func('int2').set('funcs', {'u_interp2' '1'}); model.func('int2').set('source', 'file'); model.func('int2').set('filename', 'C:\Users\yli70\vh.txt'); model.func('int2').set('struct', 'grid'); model.func('int2').set('extrap', 'linear'); %output u,v to uh and vh files fid=fopen('C:\Users\yli70\uh.txt','wt'); for i=1:(M+1) fprintf(fid,'%12.12f %12.12f\n',x_grid(i),u(i)); end fclose(fid); fid=fopen('C:\Users\yli70\vh.txt','wt'); for i=1:(M+1) fprintf(fid,'%12.12f %12.12f\n',x_grid(i),v(i)); end fclose(fid);

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 21, 2013, 3:09 p.m. EST
Hi Yukun,

Thanks for the quick response. I understand how you import and export data. My problem is that I do not even know how could I loop over every time step and modify the intermediate solutions. I know it would be long codes, could you please do me a favor to send them to my email address? Mine is: wlc309@gmail.com. Thanks very much for your help in advance.

Have a great day,
-Lichun
Hi Yukun, Thanks for the quick response. I understand how you import and export data. My problem is that I do not even know how could I loop over every time step and modify the intermediate solutions. I know it would be long codes, could you please do me a favor to send them to my email address? Mine is: wlc309@gmail.com. Thanks very much for your help in advance. Have a great day, -Lichun

Please login with a confirmed email address before reporting spam

Posted: 10 years ago Nov 17, 2014, 1:59 p.m. EST
Hi Lichun,

Did you figure out your problem? Right now, I am meeting the same question that the damping matrix needs to be changed after each time step, or beginning of next time step. If you had already solved your problem, could you share your successful experience with me?

Thank you so much.

Best Regards
Li
Hi Lichun, Did you figure out your problem? Right now, I am meeting the same question that the damping matrix needs to be changed after each time step, or beginning of next time step. If you had already solved your problem, could you share your successful experience with me? Thank you so much. Best Regards Li

Please login with a confirmed email address before reporting spam

Posted: 9 years ago Mar 9, 2015, 5:43 p.m. EDT
hi
thanks for your code
but I don't know what is "InComsol' in the "model.sol('sol1').setU(lstep,InComsol)"
hi thanks for your code but I don't know what is "InComsol' in the "model.sol('sol1').setU(lstep,InComsol)"

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jun 22, 2016, 5:33 a.m. EDT
I do it as the method ,but I find it dose't work.do you have a example foe me?
thank you very much,my email is zenghz0412@hotmail.com
I do it as the method ,but I find it dose't work.do you have a example foe me? thank you very much,my email is zenghz0412@hotmail.com

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Sep 15, 2016, 12:09 p.m. EDT
Hi Suresh,
I found this post which describes a similar problem of mine: in a time iteration, after the first loop, I need to put as initial condition the solution obtained in the previous one; I tried to use your code, but I get error message:
Exception:
com.comsol.util.exceptions.FlException: java.lang.NullPointerException
Messages:
java.lang.NullPointerException

at the line

model.sol('sol4').createSolution;

much time has passed from the post, but I would be grateful if you could help me.
My best regards
Antonino
Hi Suresh, I found this post which describes a similar problem of mine: in a time iteration, after the first loop, I need to put as initial condition the solution obtained in the previous one; I tried to use your code, but I get error message: Exception: com.comsol.util.exceptions.FlException: java.lang.NullPointerException Messages: java.lang.NullPointerException at the line model.sol('sol4').createSolution; much time has passed from the post, but I would be grateful if you could help me. My best regards Antonino

Please login with a confirmed email address before reporting spam

Posted: 7 years ago Jan 31, 2017, 12:07 a.m. EST
Facing with the same problem. Does anyone have simple working code, describing how to use the solution from the first iteration step, as an initial condition for the next iteration step. Any help will be highly appreciated. Thank you!
Facing with the same problem. Does anyone have simple working code, describing how to use the solution from the first iteration step, as an initial condition for the next iteration step. Any help will be highly appreciated. Thank you!

Please login with a confirmed email address before reporting spam

Posted: 7 years ago Jan 31, 2017, 7:15 a.m. EST
Hello Iurii,
for now I'm not working on it, but the last attempt I was doing was via GUI, using either 'linear extrusion' or 'general extrusion' functionality: you have to create two components, say comp1 and comp2; then you compute the quantity of interest in comp1 at time t, which you insert in comp2 using one of the two functionalities. I'm still interested in the developments.
Best regards
Antonino
Hello Iurii, for now I'm not working on it, but the last attempt I was doing was via GUI, using either 'linear extrusion' or 'general extrusion' functionality: you have to create two components, say comp1 and comp2; then you compute the quantity of interest in comp1 at time t, which you insert in comp2 using one of the two functionalities. I'm still interested in the developments. Best regards Antonino

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.