Comsol with Matlab 应用实例(1)

画一个半径随机的几何体在一个方形阵列中,不多说,直接上图:

 

调用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')

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值