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
Posted 3 mai 2011, 12:04 UTC−4 Interfacing Version 5.1 18 Replies
Please login with a confirmed email address before reporting spam
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.
Please login with a confirmed email address before reporting spam
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.
Attachments:
Please login with a confirmed email address before reporting spam
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
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
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
Please login with a confirmed email address before reporting spam
Please login with a confirmed email address before reporting spam
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
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
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
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
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
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
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
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
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
thank you very much,my email is zenghz0412@hotmail.com
Please login with a confirmed email address before reporting spam
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
Please login with a confirmed email address before reporting spam
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.
Suggested Content
- FORUM How to Update Conditions Between Iterations
- KNOWLEDGE BASE Improving Convergence of Nonlinear Stationary Models
- KNOWLEDGE BASE Error: Singular Matrix
- KNOWLEDGE BASE Maximum number of linear iterations reached
- BLOG A Practical Implementation of Multiphysics Topology Optimization