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.

avoid MPHLOAD every iteration in matlab livelink

Please login with a confirmed email address before reporting spam

Hello All,

I am using the genetic algorithm in Matlab which calls a Comsol permanent magnet simulation in each iteration of the optimization via livelink.

The simulation is very slow - running the simulation in comsol takes about 1-2 seconds, while running the simulation through matlab takes about 6 seconds which is killing my optimization. In each iteration, the fitness function runs the matlab code as follows

1)
model = mphopen('Topology_optimization_6_efficient_basic');
2)
model.param.set('mag1',val_n(1));
......... (set other parameters)
3)
model.sol('sol1').run;

The loading operation (1) takes about 3 seconds, operation (2) is almost instant and operation (3), actually solving the model, takes about 3 seconds. So it seems obvious - if I can remove the mphopen command, my 6 day simulation should run about twice as fast!

But doing this has not proved challenging. Basically, I would like to run the "mphopen" command BEFORE i call the genetic algorithm, then in each iteration the fitness function has access to model.param and model.sol, et cetera - thus it is a problem of the scope of "model". As such, is there a way I can define "model" in matlab to be global, such that I can change parameters in a different function without calling "mphopen" each time?

Any help here would be most appreciated.

Best,

Reed


p.s. Comsol 5.2, AC/DC module, matlab 2016a



2 Replies Last Post 17 nov. 2016, 17:57 UTC−5
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 3 nov. 2016, 06:37 UTC−4
Hi Reed

When solving in a loop we recommned that you use mphload instead of using mphopen since mphopen introduces extrat logic to handle a list of most recently loaded files.

I suggest you load the model once from the main script like this:

model = mphload('Topology_optimization_6_efficient_basic', 'MyModel');

Inside you objective function you can now do of one these:

1)
import com.comsol.model.util.*
model = ModelUtil.model('MyModel');
% gives you access to the MyModel model on the server using the variable name model in Matlab

2)
model = evalin('base', 'model');
% gets the model variable from the main workspace

3)
Use persistent variables.


--
Lars Gregersen
Comsol Denmark
Hi Reed When solving in a loop we recommned that you use mphload instead of using mphopen since mphopen introduces extrat logic to handle a list of most recently loaded files. I suggest you load the model once from the main script like this: model = mphload('Topology_optimization_6_efficient_basic', 'MyModel'); Inside you objective function you can now do of one these: 1) import com.comsol.model.util.* model = ModelUtil.model('MyModel'); % gives you access to the MyModel model on the server using the variable name model in Matlab 2) model = evalin('base', 'model'); % gets the model variable from the main workspace 3) Use persistent variables. -- Lars Gregersen Comsol Denmark

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 17 nov. 2016, 17:57 UTC−5
Dr. Gregersen,

Thank you for the detailed response!

I was able to shave down the operating time in half (from 6s to 3s), so I am replying to help those in the future who google this thread. It ended up being more of a Matlab issue than a Comsol issue.

Load the model "object" in the main code and pass the model object into the fitness function in each iteration as follows.

-> Re-define your fitness function to include model as an input BEFORE calling your optimization

(Main / outside function)
model = mphload('Topology_optimization_6_efficient_basic');
FitnessFunction = @(x) magFitness(x,model);

[x,fval] = ga(FitnessFunction,49,[],[],[],[],lb,ub,[],[1:49],opts) % matlab optimization call

NOTE - see how I defined both X and MODEL as inputs, where x is the array which the genetic algorithm manipulates. This way, we open up the comsol model outside the optimization but have access to the model object in each iteration of the optimization.

Now, inside the fitness function, we can change the Comsol parameters and re-run like

(In fitness function)
model.physics('mfnc').feature('mfc81').selection.set(some_variable);
try
model.sol('sol1').run;
catch
fprintf('Failed to converge on solution: try / catch');
end

Hope this helps someone!



Dr. Gregersen, Thank you for the detailed response! I was able to shave down the operating time in half (from 6s to 3s), so I am replying to help those in the future who google this thread. It ended up being more of a Matlab issue than a Comsol issue. Load the model "object" in the main code and pass the model object into the fitness function in each iteration as follows. -> Re-define your fitness function to include model as an input BEFORE calling your optimization (Main / outside function) model = mphload('Topology_optimization_6_efficient_basic'); FitnessFunction = @(x) magFitness(x,model); [x,fval] = ga(FitnessFunction,49,[],[],[],[],lb,ub,[],[1:49],opts) % matlab optimization call NOTE - see how I defined both X and MODEL as inputs, where x is the array which the genetic algorithm manipulates. This way, we open up the comsol model outside the optimization but have access to the model object in each iteration of the optimization. Now, inside the fitness function, we can change the Comsol parameters and re-run like (In fitness function) model.physics('mfnc').feature('mfc81').selection.set(some_variable); try model.sol('sol1').run; catch fprintf('Failed to converge on solution: try / catch'); end Hope this helps someone!

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.