MATLAB Comsol Multiphysics Script Example

Mesh generation script
function out = createMPHmesh(modelPath, stlFilePath, blockHeightMultiplier, hmax, hmin)

addpath('//Applications/COMSOL61/Multiphysics/mli')
savepath
import com.comsol.model.*
import com.comsol.model.util.*

try
    % Attempt to start the COMSOL server
    mphstart();
catch ME
    % Check if the error is due to already being connected to the server
    if contains(ME.message, 'Already connected to a server')
        % If already connected, you can safely ignore the error and continue
        disp('Already connected to COMSOL server.');
    else
        % If it's a different error, rethrow it
        rethrow(ME);
    end
end

model = ModelUtil.create('Model');

model.modelPath(modelPath);

model.component.create('comp1', true);

model.component('comp1').geom.create('geom1', 3);

model.component.create('mcomp1', 'MeshComponent');

model.geom.create('mgeom1', 3);

model.component('comp1').mesh.create('mesh1');
model.mesh.create('mpart1', 'mgeom1');
model.mesh('mpart1').create('imp1', 'Import');
model.mesh('mpart1').feature('imp1').set('source', 'stl');
model.mesh('mpart1').feature('imp1').set('filename', stlFilePath);
model.mesh('mpart1').feature('imp1').set('createdom', true);
model.mesh('mpart1').feature('imp1').importData;
model.mesh('mpart1').feature('imp1').set('facepartition', 'minimal');
model.mesh('mpart1').run;

model.component('comp1').geom('geom1').create('imp1', 'Import');
model.component('comp1').geom('geom1').feature('imp1').set('type', 'mesh');
model.component('comp1').geom('geom1').feature('imp1').set('mesh', 'mpart1');
model.component('comp1').geom('geom1').feature('imp1').importData;
% Run the geometry to ensure it is built
model.geom('geom1').run();
% Extract bounding box dimensions

bbox = model.component('comp1').geom('geom1').getBoundingBox();

width = bbox(2) - bbox(1);
depth = bbox(4) - bbox(3);
height = bbox(6) - bbox(5);

% Calculate the center of the bounding box
center_x = (bbox(2) + bbox(1)) / 2;
center_y = (bbox(4) + bbox(3)) / 2;
center_z = (bbox(6) + bbox(5)) / 2;

% Define parameters for block dimensions slightly larger than the bounding box
block_width = width * 2;
block_depth = depth * 2;
block_height = height * blockHeightMultiplier;


model.component('comp1').geom('geom1').create('blk1', 'Block');
model.component('comp1').geom('geom1').feature('blk1').set('pos', [center_x - block_width / 2, center_y - block_depth / 2, center_z - block_height / 2]);
model.component('comp1').geom('geom1').feature('blk1').set('size', [block_width, block_depth, block_height]);
model.component('comp1').geom('geom1').create('int1', 'Intersection');
model.component('comp1').geom('geom1').feature('int1').selection('input').set({'blk1' 'imp1'});
model.component('comp1').geom('geom1').run;

model.component('comp1').physics.create('solid', 'SolidMechanics', 'geom1');

model.component('comp1').mesh('mesh1').create('ftet1', 'FreeTet');
model.component('comp1').mesh('mesh1').feature('size').set('custom', 'on');
model.component('comp1').mesh('mesh1').feature('size').set('hmax', hmax);
model.component('comp1').mesh('mesh1').feature('size').set('hmin', hmin);
model.component('comp1').mesh('mesh1').run;

model.study.create('std1');
model.study('std1').create('stat', 'Stationary');

% Export the mesh as a .mphtxt file
model.component('comp1').mesh('mesh1').export.set('nativequadratic', true);
exportFilePath = fullfile(modelPath, 'mesh.mphtxt');
model.component('comp1').mesh('mesh1').export(exportFilePath);

% Print confirmation message
disp('Mesh correctly exported');

out = model;