画一个半径随机的几何体在一个方形阵列中,不多说,直接上图:
调用CwM完成这项工作:
% This is a script for Comsol(4.x) with Matlab.
% It product random radius circles inside a square box array.
%
% Copyright to mxio at Shanghai University.
%
% mxio
% 10,July,2013
import com.comsol.model.*
import com.comsol.model.util.*
model = ModelUtil.create('Model');
model.modelNode.create('mod1');
model.geom.create('geom1', 2);
% Model Parameters
rd_seed = rng('shuffle'); % random number seed
sq_length = 1; % square length
sq_posx = 0; % square position x
sq_posy = 0; % square position y
sq_disx = 1.5; % square displacement x
sq_disy = 1.5; % square displacement y
sq_numx = 5; % square number x
sq_numy = 5; % square number y
sq_num = sq_numx * sq_numy; % square number total
cl_num = 3; % circle number inside the square
cl_radius_normal = sq_length * 0.45; % circle radius : half for the maximum
co_min = 0.2; % random number must be > co_min
%------------------------------------------------------------------------------
idx = 1; % index for square
idx2 = 1; % index for circle
for i = 1: sq_numx
for j = 1:sq_numy
sq_name = ['sq',num2str(idx)];
model.geom('geom1').feature.create(sq_name, 'Square');
model.geom('geom1').feature(sq_name).set('base', 'center');
model.geom('geom1').feature(sq_name).set('size', num2str(sq_length));
model.geom('geom1').feature(sq_name).setIndex('pos', num2str(sq_posx + (i-1)*sq_disx), 0);
model.geom('geom1').feature(sq_name).setIndex('pos', num2str(sq_posx + (j-1)*sq_disy), 1);
for k = 1:cl_num
cl_name = ['c', num2str(idx2)];
co_rand = rand();
while co_rand << co_min
co_rand = rand();
end
cl_radius = cl_radius_normal * co_rand;
model.geom('geom1').feature.create(cl_name, 'Circle');
model.geom('geom1').feature(cl_name).set('r', num2str(cl_radius));
model.geom('geom1').feature(cl_name).setIndex('pos', num2str(sq_posx + (i-1)*sq_disx), 0);
model.geom('geom1').feature(cl_name).setIndex('pos', num2str(sq_posx + (j-1)*sq_disy), 1);
idx2 = idx2 +1;
end
idx = idx + 1;
end
end
model.geom('geom1').runAll;
%------------------------------------------------------------------------------
mphsave(model,'rand_circle')