Thursday, August 18, 2016

Comsol Multiphysics (from livelink)

Reference Manuals:

Comsol Reference Manual:
COMSOL_Reference_Manual.pdf

Comsol with Livelink Reference Manual:
LiveLink_For_MATLAB_Users_Guide.pdf

A good way to start:

Do you need it? Comsol offers a method to perform a parameter shift which could avoid going into these troubles.

Otherwise, open the GUI and create your model. The following ideas are rule of thumb that makes everything simpler afterwards:
  • use as many parameters as you can
  • refer to domains with implicit selections (explicit selections might get undefined when the geometry get modified
  • perform every basic task you want to be able to modify later (geometry, meshing, material properties, physics definition, solver and run the simulation).
From there, two scenarios are possible, the clean one and the messy one. The clean one is simpler but gives less control. The messy one gives access to all the parameter of the model.
The clean method: 
Save the Comsol model as a standard "simulation.mph" file and later open Comsol with the following command line (from the console):
comsol server matlab 
Start a new script and add the following line:

mphload('simulation.mph');
It is then possible to access all the information from the model with the command
 mphnavigator
All the parameter can be accessed and modified with the command described bellow.

The messy method:
Perform every task and compact the history (file > Compact History), then save the file as a model file for Matlab (.*m).

Every task performed before will be translated in command lines. The Matlab file is self-contained.

Few useful commands for display:

plot the geometry:
mphgeom(model);

plot the mesh:

model.mesh.run()
mphmesh(model);

plot a result:
mphplot(model, 'pg1');

Extracting information:
[d1, ..., dn] = mphinterp(model,{'e1', ..., 'en'},'coord',<coord>);

More examples to access data:
data = mpheval(model,'mf.normB');                              %give 'mf.normB' for each points
databoundaries = mpheval(model,'mf.normB', 'selection', 43);             %give 'mf.normB' for each points on the boundary 43
dataPoint = mphinterp(model,'mf.normB','coord',[5e-2;-2e-2;1e-3]);  %give 'mf.normB' at a particular point
data_point.amplitude = mphinterp(out,'log(mf.normB)','coord',[ones(size(-2.5:0.01:2.5))*0.27;-2.5:0.01:2.5]);  %line measurement

%rename file
filename = ['/home/user/plot' num2str(index) '.txt'];
model.result.export('plot1').set('filename', filename);

%remove the header
model.result.export('plot1').set('header', false);

%change the size of something:
model.param.set('L','18[cm]');

%run the solver
model.sol('sol1').run;

%check the time of a comand
tic;
time = toc;
fprint('blabla %gt\n', time);

Debugging:

The feedbacks provided by Livelink are not very informative. A good way to  debug the model is to save the model as an "mph" file and check what is happening from the GUI. To do so (the full path is needed as Comsol is run from a weird place):
model.save('/home/user/test.mph');

or
 
mphsave(model,'filename')