Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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 initial solution from Matlab

Please login with a confirmed email address before reporting spam

Hi,

I have a non-linear problem and want to get the stiffness matrix and some other stuff in a special linearisation point. For this I need to set the initial guess of a solver. There the problems start:

I found two possibilities:
* Use model.sol('sol1').setU(u0); followed by model.sol('sol1').createSolution();
This results in a java.lang.NullPointerException

* Using the init feature of the physics model:
model.physics('poeq').feature('init1').set('u', 1, 'setInitialSolution(x,y)');
For this I need a way to specify additional arguments such as the mesh and u0 to setInitialSolution so that I can calculate for every location x,y a starting value from my vector u0 by interpolating (probably only the node values are requested).

I know that I can add scalar parameters to the Matlab function setInitialSolution. But I need mesh information! One way would be transfering files via the file system, this works but is not elegant. Another solution I performed was to connect with the comsol server to request my model from it (works!) and to continue with mphxmeshinfo(). This fails (hangs forever) and results (once killed) in an error message:
Could not start Comsol.

Probably it is because of the calling chain:
Matlab -> Comsol (via model.sol('sol1').runAll()) -> Matlab (setInitialSolution) -> Comsol (mphxmeshinfo)

Is there no elegant way to solve these problems (calling setU, specifying addional arguments to Matlab functions called by Comsol)?

I have a small example with demonstrates this (with a Poisson problem for simplicity) but as I cannot attach it (file extension error) I post it here:

Matlab driver file:
% create model (Poisson equation)
import com.comsol.model.util.*

model = ModelUtil.create('Model');
model.modelNode.create('mod1');
model.geom.create('geom1', 2);
model.mesh.create('mesh1', 'geom1');

model.physics.create('poeq', 'PoissonEquation', 'geom1');
model.physics('poeq').feature('peq1').set('f', 1, 'x+2*y');
model.physics('poeq').prop('ShapeProperty').set('order', 1, '1');

model.sol.create('sol1');
model.geom('geom1').feature.create('sq1', 'Square');

model.mesh('mesh1').automatic(true);
model.mesh('mesh1').autoMeshSize(9); % 9 very coarse

model.study.create('std1');
model.sol('sol1').feature.create('st1', 'StudyStep');
model.sol('sol1').feature.create('v1', 'Variables');
model.sol('sol1').feature.create('s1', 'Stationary');
model.sol('sol1').attach('std1');

model.physics('poeq').feature.create('dir1', 'DirichletBoundary', 1);
model.physics('poeq').feature('dir1').selection.set([1 2 3 4]);
model.physics('poeq').feature('dir1').set('r', 1, '0');

model.sol('sol1').runAll;

% Model set up, now try to set a special initial guess

if false
model.func.create('extm1', 'MATLAB');
model.func('extm1').setIndex('funcs', 'setInitialSolutionBuggy', 0, 0);
model.func('extm1').setIndex('funcs', 'x, y', 0, 1);

model.physics('poeq').feature('init1').set('u', 1, 'setInitialSolutionBuggy(x,y)');
model.sol('sol1').feature('s1').active(false);
model.sol('sol1').run(); % set initial data
else
u = model.sol('sol1').getU();
u0 = u + 1; % a compatible solution vector (proper length, ...)
model.sol('sol1').setU(u0);
model.sol('sol1').createSolution(); % bang, java.lang.NullPointerException
end

setInitialSolutionBuggy.m:
function ret = setInitialSolutionBuggy(x, y)
% Main problem: Obtain mesh data from model
% Cannot be specified as argument (only scalar params!?)

import com.comsol.model.*
import com.comsol.model.util.*

% either set (many!!!?) java paths with javaaddpath followed by
% ModelUtil.connect('localhost', 2036);

% or
path(path, '/opt/comsol42a/mli');
mphstart

file = fopen('ComsolBug1.txt', 'a');
ret = ModelUtil.tags;

fprintf(file, 'Existing models:\n');
for i=1:length(ret)
fprintf(file, ' * %s\n', ret(i).toCharArray());
end

model = ModelUtil.model('Model');
fprintf(file, 'Model opened\n');

% now it hangs in mphxmeshinfo!
info = mphxmeshinfo(model,'solname','sol1');
fprintf(file, 'You will never see this!\n');
end



0 Replies Last Post Sep 6, 2012, 8:58 a.m. EDT
COMSOL Moderator

Hello Jens Seidel

Your Discussion has gone 30 days without a reply. If you still need help with COMSOL and have an on-subscription license, please visit our Support Center for help.

If you do not hold an on-subscription license, you may find an answer in another Discussion or in the Knowledge Base.

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.