- 积分
- 15
- 注册时间
- 2011-4-2
- 仿真币
-
- 最后登录
- 1970-1-1
|
一直有个想法,做一个随机圆柱体填充到立方体中,效果如图:
计算几何的算法会更好,无奈翻了下发现都是公式,不知道怎么去用。于是用了个偷懒的方法——试探。当然这种试探的方法,效率就很低了。
我的方法主要用到了:在底面找个随机位置,然后随机方向上放置圆柱体,然后判断是否和之前的圆柱体相交。
从而达到建立互不干涉的圆柱体。- 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 = [blk_length blk_length blk_length];
- 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 = [0 0 0];
- 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', [rand rand large_blk_position_z]);
- model.geom('geom1').feature('cyl1').set('ax2', [180*rand-90 360*rand]);
- 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', [rand rand large_blk_position_z]);
- model.geom('geom1').feature(cy_name).set('ax2', [180*rand-90 360*rand]);
- model.geom('geom1').run(cy_name);
- model.geom('geom1').feature.create('int1', 'Intersection');
- tmp_name = [cy_name {'uni1'}];
- 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;
复制代码mxio 2013.7.19 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
×
评分
-
1
查看全部评分
-
|