Please login with a confirmed email address before reporting spam
Posted:
1 decade ago
07.06.2012, 04:26 GMT-4
Hi,
It is possible. I have not tried to read in a grid from a matlab array directly. But I have tried to read in a grid from a text file which is re-written at each time step as part of MATLAB calculation and essentially contains: x, y, f(x,y).
Based on livelink manual, for example, I just show you the creation of a text file called ksoft.txt
%%
% Open the file ksoft.txt for writting and enter the
% evaluated data with the format '%%x y ksoft'. Exclude in the file the NaN
% value that result of evaluating the ksoft outside the domain.
fid = fopen(filename1,'w');
fprintf(fid,'%%x y ksoft\n');
for j = 1:nog
if ~isnan(x1(j))||~isnan(y1(j))
fprintf(fid,'%10.10f %10.10f %10.10f\n',x1(j),y1(j),ksoft1(j));
end
end
fclose(fid);
I then have the following two lines:
model.func('int1').setIndex('funcs', 'int2', 0, 0);
model.study('std1').run;
To know what the int2 is doing, I attach a screenshot.
I guess that implementing directly from MATLAB array should also be simple but I have not had time to do it.
Sorry I cannot be of more help.
Suresh
Hi,
It is possible. I have not tried to read in a grid from a matlab array directly. But I have tried to read in a grid from a text file which is re-written at each time step as part of MATLAB calculation and essentially contains: x, y, f(x,y).
Based on livelink manual, for example, I just show you the creation of a text file called ksoft.txt
%%
% Open the file ksoft.txt for writting and enter the
% evaluated data with the format '%%x y ksoft'. Exclude in the file the NaN
% value that result of evaluating the ksoft outside the domain.
fid = fopen(filename1,'w');
fprintf(fid,'%%x y ksoft\n');
for j = 1:nog
if ~isnan(x1(j))||~isnan(y1(j))
fprintf(fid,'%10.10f %10.10f %10.10f\n',x1(j),y1(j),ksoft1(j));
end
end
fclose(fid);
I then have the following two lines:
model.func('int1').setIndex('funcs', 'int2', 0, 0);
model.study('std1').run;
To know what the int2 is doing, I attach a screenshot.
I guess that implementing directly from MATLAB array should also be simple but I have not had time to do it.
Sorry I cannot be of more help.
Suresh
Please login with a confirmed email address before reporting spam
Posted:
1 decade ago
07.06.2012, 14:36 GMT-4
Hi Suresh,
Thanks for your suggestion, I'll give it a try. I don't have any problem with going from Matlab to COMSOL via a .txt file.
Would I complete the task simply by setting my material parameter to 'ksoft', using your example?
Cheers,
Brian
Hi Suresh,
Thanks for your suggestion, I'll give it a try. I don't have any problem with going from Matlab to COMSOL via a .txt file.
Would I complete the task simply by setting my material parameter to 'ksoft', using your example?
Cheers,
Brian
Please login with a confirmed email address before reporting spam
Posted:
1 decade ago
07.06.2012, 15:47 GMT-4
Suresh,
I'm not quite sure what your last 2 lines accomplish. I managed to get COMSOL to read and interpolate the .txt file by setting up int1 and the variable by hand in COMSOL, but I need to be able to set these through MATLAB (since it's going to iterate in a MATLAB loop). Any thoughts?
Thanks,
Brian
Suresh,
I'm not quite sure what your last 2 lines accomplish. I managed to get COMSOL to read and interpolate the .txt file by setting up int1 and the variable by hand in COMSOL, but I need to be able to set these through MATLAB (since it's going to iterate in a MATLAB loop). Any thoughts?
Thanks,
Brian
Please login with a confirmed email address before reporting spam
Posted:
1 decade ago
07.06.2012, 15:59 GMT-4
I'd suggest to avoid using interpolation function in this case. It's rather slow/inaccurate. Instead, call Matlab function directly from COMSOL (Global def.-Functions-Matlab).
Input to the function should be xyz coordinates (vectors) and output is the function value (vector of the same size). I define electric fields in such a way and it's so much faster and accurate than doing through text files.
I'd suggest to avoid using interpolation function in this case. It's rather slow/inaccurate. Instead, call Matlab function directly from COMSOL (Global def.-Functions-Matlab).
Input to the function should be xyz coordinates (vectors) and output is the function value (vector of the same size). I define electric fields in such a way and it's so much faster and accurate than doing through text files.
Please login with a confirmed email address before reporting spam
Posted:
1 decade ago
07.06.2012, 17:19 GMT-4
Hi Brian,
I am not sure of your question.
All the lines I gave you are to be written into a program file that you create in matlab.
If you are loading filename.m file then the interpolation command would already be in the m file itself so you probably don't need it. This script and many more would be necessary if you are writing out the entire script yourself without calling the m file.
The next line "model.study('std1').run;" is necessary because I have to run the model. What I showed you was just an extract from a much larger matlab input file. I have a lot more scripting to effect changes at every time step within a loop and therefore I run the model after changing them. I am not sure if I understood your question correctly here. So I attach here again a partial code that may help you a bit more.
Alexander is right that it is better to call matlab array directly, because in this approach, as you have guessed you have to keep writing data into a text file at each step of the loop. I think this could be slow. I had no problems with accuracy at least for the problem I was solving, it also depends on how many data points you have.
Suresh
------------------------------------------------
clear
%%
% have to load mph here
model = mphload('modelname'); % no need to put .mph extension to the modelname
% allocate arrays and initialise ksoft
filename1='D:\My_research\ksoft.txt';
pd1=mpheval(model,'u','pattern','gauss');
nog=length(pd1.p(1,:));
ksoft1=zeros(nog,1);
ksoft1(:)=1e-4;
x1=pd1.p(1,:);
y1=pd1.p(2,:);
%%
% Open the file ksoft.txt for writting and enter the
% evaluated data with the format '%%x y ksoft'. Exclude in the file the NaN
% value that result of evaluating the ksoft outside the domain.
fid = fopen(filename1,'w');
fprintf(fid,'%%x y ksoft\n');
for j = 1:nog
if ~isnan(x1(j))||~isnan(y1(j))
fprintf(fid,'%10.10f %10.10f %10.10f\n',x1(j),y1(j),ksoft1(j));
end
end
fclose(fid);
%%
% Run a loop
for i = 1:50
%%
% set parameter values
model.param.set('bound_disp', i*1e-6);
for iter = 1:2
% Run the study.
if (iter>1)
model.param.set('param1',1);
model.param.set('param2',1);
else
model.param.set('param1',0);
model.param.set('param2',0);
end
model.func('int1').setIndex('funcs', 'int2', 0, 0);
model.study('std1').run;
% re-evaluate ksoft
script deleted
% re-write ksoft.txt
script deleted
%%
% Set the new solution vector back into comsol, reset only the last time
% % step value
model.sol('sol1').setU(Ulast);
model.sol('sol1').createSolution;
model.sol('sol1').feature('v1').set('initmethod', 'sol');
model.sol('sol1').feature('v1').set('initsol', 'sol1');
disp(sprintf(' Completed iteration no. - %d',iter));
end
disp(sprintf('Completed load step. - %d',i));
end
end
disp(sprintf('Problem completed. %d'));
Hi Brian,
I am not sure of your question.
All the lines I gave you are to be written into a program file that you create in matlab.
If you are loading filename.m file then the interpolation command would already be in the m file itself so you probably don't need it. This script and many more would be necessary if you are writing out the entire script yourself without calling the m file.
The next line "model.study('std1').run;" is necessary because I have to run the model. What I showed you was just an extract from a much larger matlab input file. I have a lot more scripting to effect changes at every time step within a loop and therefore I run the model after changing them. I am not sure if I understood your question correctly here. So I attach here again a partial code that may help you a bit more.
Alexander is right that it is better to call matlab array directly, because in this approach, as you have guessed you have to keep writing data into a text file at each step of the loop. I think this could be slow. I had no problems with accuracy at least for the problem I was solving, it also depends on how many data points you have.
Suresh
------------------------------------------------
clear
%%
% have to load mph here
model = mphload('modelname'); % no need to put .mph extension to the modelname
% allocate arrays and initialise ksoft
filename1='D:\My_research\ksoft.txt';
pd1=mpheval(model,'u','pattern','gauss');
nog=length(pd1.p(1,:));
ksoft1=zeros(nog,1);
ksoft1(:)=1e-4;
x1=pd1.p(1,:);
y1=pd1.p(2,:);
%%
% Open the file ksoft.txt for writting and enter the
% evaluated data with the format '%%x y ksoft'. Exclude in the file the NaN
% value that result of evaluating the ksoft outside the domain.
fid = fopen(filename1,'w');
fprintf(fid,'%%x y ksoft\n');
for j = 1:nog
if ~isnan(x1(j))||~isnan(y1(j))
fprintf(fid,'%10.10f %10.10f %10.10f\n',x1(j),y1(j),ksoft1(j));
end
end
fclose(fid);
%%
% Run a loop
for i = 1:50
%%
% set parameter values
model.param.set('bound_disp', i*1e-6);
for iter = 1:2
% Run the study.
if (iter>1)
model.param.set('param1',1);
model.param.set('param2',1);
else
model.param.set('param1',0);
model.param.set('param2',0);
end
model.func('int1').setIndex('funcs', 'int2', 0, 0);
model.study('std1').run;
% re-evaluate ksoft
script deleted
% re-write ksoft.txt
script deleted
%%
% Set the new solution vector back into comsol, reset only the last time
% % step value
model.sol('sol1').setU(Ulast);
model.sol('sol1').createSolution;
model.sol('sol1').feature('v1').set('initmethod', 'sol');
model.sol('sol1').feature('v1').set('initsol', 'sol1');
disp(sprintf(' Completed iteration no. - %d',iter));
end
disp(sprintf('Completed load step. - %d',i));
end
end
disp(sprintf('Problem completed. %d'));