Using MATLAB to control Topology Optimization module in COMSOL

Please login with a confirmed email address before reporting spam

**Help needed in implementing MATLAB script to maintain control over COMSOL Topology Optimization iterations **

Hello everyone,

As the title suggests, I want to use MATLAB Livelink with COMSOL to implement Topology Optimization. The concept is simple. For beta parameter between 1 and 1024, I want to control the number of iterations with three methods: 1 - Max iterations depending on beta value (this works) 2 - Ending iterations early based on some arbitary convergence criteria (this also works) 3- Reusing solution from previous beta as the initial solution for the next beta ( This does not work).

I was wondering if anyone has any idea on how this can be implemented without using the parametric sweep feature of COMSOL as that would disable controls 1 and 2 mentioned above.

Here is my script:

clc;clear %% Topology Optimization for a Micromixer with multiple studies

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

%% Progress log logFile = 'D:\Users\pq6034\Desktop\HIWI\progress.log'; fid = fopen(logFile,'w'); fclose(fid); ModelUtil.showProgress(logFile); ModelUtil.showProgress(true);

%% Define model to study model = mphopen('NSCD_OPEN_3.mph');

%% Initial Variables nmax = 100; chunksize = 10; betas = 0:1:10; nupd = 30;

%% Output folder saveDir = 'Results_Micromixer_30.06.26'; if ~exist(saveDir,'dir') mkdir(saveDir); end

%% Automatically build study -> solver map studySolverMap = containers.Map; solTags = model.sol.tags(); fprintf('\nStudy-to-solver mapping:\n'); fprintf('--------------------------------------------\n');

for i = 1:length(solTags) solTag = char(solTags(i)); studyTag = char(model.sol(solTag).feature('st1').getString('study')); studyLabel = char(model.study(studyTag).label()); studySolverMap(studyTag) = solTag;

fprintf('%-5s (%-20s) --> %s\n', ...
        studyTag, studyLabel, solTag);

end

fprintf('--------------------------------------------\n\n');

%% Studies to run studies = {'std','std1','std2','std3','std4','std5'}; %studies = {'std2'};

for s = 1:length(studies)

studyTag = studies{s};

fprintf('\n====================================\n');
fprintf('Running study %s\n',studyTag);
fprintf('====================================\n');

%% Solver tag
solTag = studySolverMap(studyTag);

fprintf('Using solver %s\n',solTag);
info = mphxmeshinfo(model,'soltag',solTag);

idx_theta_c = find(info.dofs.nameinds == 2);
sv_idx = info.dofs.solvectorinds(idx_theta_c);

theta_saved = [];


%% Beta loop
for k = 1:length(betas)

    beta = 2^betas(k);

    fprintf('\n====================================\n');
    fprintf('Beta = %g\n',beta);
    fprintf('====================================\n');

    model.param.set('betap',num2str(beta));


    %% Maximum iterations for this beta
    if beta < 1024
        maxIter = nupd;
    else
        maxIter = nmax;
    end

    %% First optimization chunk
    model.sol(solTag).feature('o1').set('mmamaxiteractive',true);
    model.sol(solTag).feature('o1').set('mmamaxiter',chunksize);

    fprintf('Running first %d iterations...\n',chunksize);

    model.sol(solTag).runFrom('o1');

    counter = 10;

    %% Call function to evaluate change in objective
    [converged,totalChange] = monitorObjective(logFile);

    %% Continue optimization
    while ~converged && counter < maxIter
        counter = counter + 10;
        model.sol(solTag).feature('o1').set('mmamaxiter',chunksize);
        fprintf('Continuing to %d iterations...\n',counter);
        model.sol(solTag).continueRun();
        [converged,totalChange] = monitorObjective(logFile);

    end

    fprintf('Finished beta = %g\n',beta);


    %% Checkpoint save for beta
    mphsave(model,fullfile(saveDir,...
        sprintf('%s_beta_%g.mph',studyTag,beta)));

end %% End of Beta loop

%% Save study
mphsave(model,fullfile(saveDir,...
    sprintf('%s_finalcompiled.mph',studyTag)));

end %% End of study

%% Final save mphsave(model,fullfile(saveDir,'Runx_final.mph')); disp('All studies completed.');


Reply

Please read the discussion forum rules before posting.

Please log in to post a reply.

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.