Comsol with Matlab 应用实例(3)
一直有个想法,做一个随机圆柱体填充到立方体中,效果如图:计算几何的算法会更好,无奈翻了下发现都是公式,不知道怎么去用。于是用了个偷懒的方法——试探。当然这种试探的方法,效率就很低了。
我的方法主要用到了:在底面找个随机位置,然后随机方向上放置圆柱体,然后判断是否和之前的圆柱体相交。
从而达到建立互不干涉的圆柱体。function out = randcylinder
% This is a script for Comsol(4.x) with Matlab.
% It product random position cylinders inside a block.
%
% Copyright to mxio at Shanghai University.
%
% mxio
% 19,July,2013
import com.comsol.model.*
import com.comsol.model.util.*
model = ModelUtil.create('Model');
model.modelNode.create('mod1');
model.geom.create('geom1', 3);
blk_length = 1;
blk_size = ;
cyl_radius = 0.02 * blk_length;
cyl_length = blk_length + 4.5 * cyl_radius; % about 3*sqrt(2)*cyl_radius
large_blk_position_z = -1.5 * cyl_radius;
blk_position = ;
cy_num_total = 50;
cy_num = 2;
rng('shuffle'); % random number seed
model.geom('geom1').feature.create('cyl1', 'Cylinder');
model.geom('geom1').feature('cyl1').set('r', num2str(cyl_radius));
model.geom('geom1').feature('cyl1').set('h', num2str(cyl_length));
model.geom('geom1').feature('cyl1').set('pos', );
model.geom('geom1').feature('cyl1').set('ax2', );
model.geom('geom1').run('cyl1');
cy_name_cell= {'cyl1'};
while cy_num<=cy_num_total
model.geom('geom1').feature.create('uni1', 'Union');
model.geom('geom1').feature('uni1').selection('input').set(cy_name_cell);
model.geom('geom1').run('uni1');
cy_name = ['cyl',num2str(cy_num)];
model.geom('geom1').feature.create(cy_name, 'Cylinder');
model.geom('geom1').feature(cy_name).set('r', num2str(cyl_radius));
model.geom('geom1').feature(cy_name).set('h', num2str(cyl_length));
model.geom('geom1').feature(cy_name).set('pos', );
model.geom('geom1').feature(cy_name).set('ax2', );
model.geom('geom1').run(cy_name);
model.geom('geom1').feature.create('int1', 'Intersection');
tmp_name = ;
model.geom('geom1').feature('int1').selection('input').set(tmp_name);
model.geom('geom1').run('int1');
if isempty(mphshowerrors(model))
model.geom('geom1').feature.remove(cy_name);
else
cy_name_cell{cy_num}= cy_name;
cy_num = cy_num + 1;
end
model.geom('geom1').feature.remove('uni1');
model.geom('geom1').feature.remove('int1');
end
model.geom('geom1').feature.create('blk1', 'Block');
model.geom('geom1').feature('blk1').set('size',blk_size);
model.geom('geom1').feature('blk1').set('pos', blk_position);
model.geom('geom1').feature.create('uni1', 'Union');
model.geom('geom1').feature('uni1').selection('input').set(cy_name_cell);
model.geom('geom1').run('uni1');
model.geom('geom1').feature.create('int1', 'Intersection');
tmp_name = {'uni1', 'blk1'};
model.geom('geom1').feature('int1').selection('input').set(tmp_name);
model.geom('geom1').run('int1');
model.geom('geom1').runAll;
mphgeom(model);
out = model;
mxio2013.7.19 NoEureka 发表于 2013-9-23 09:04
请问为什么我直接运行的时候,报错说Undefined function 'mphshowerrors'。我用的是comsol4.2 ...
4.2的不能用这个函数,需要修改。 楼主乃真汉子也!收下学习了。 楼主的方法简直了,另辟蹊径啊! 请问为什么我直接运行的时候,报错说Undefined function 'mphshowerrors'。我用的是comsol4.2 楼主发的真是好贴啊,抛砖引玉 学习一下! 楼主真棒,真是长见识 谢谢了。。。。。 Thanks for your share!!! 不错。。 感觉真的高大上·~~ 试一下:):lol:) 优秀的人一直都在,像大佬学习! 优秀,像大佬学习66666 优秀的人,不仅自己优秀,还会分享,让我们学习
页:
[1]