mxio 发表于 2013-7-19 13:28:35

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

rader001 发表于 2013-10-12 20:44:06

NoEureka 发表于 2013-9-23 09:04
请问为什么我直接运行的时候,报错说Undefined function 'mphshowerrors'。我用的是comsol4.2 ...

4.2的不能用这个函数,需要修改。

showaround 发表于 2013-7-19 20:50:31

楼主乃真汉子也!收下学习了。

atomical 发表于 2013-9-13 18:43:57

楼主的方法简直了,另辟蹊径啊!

NoEureka 发表于 2013-9-23 09:04:56

请问为什么我直接运行的时候,报错说Undefined function 'mphshowerrors'。我用的是comsol4.2

longwen36 发表于 2013-12-4 21:01:56

楼主发的真是好贴啊,抛砖引玉

SMT_Captain 发表于 2014-10-19 16:33:41

学习一下!

不会生气的蜗牛 发表于 2014-10-31 17:16:32

楼主真棒,真是长见识

plwhu 发表于 2015-4-2 16:53:13

谢谢了。。。。。

mr_xumao 发表于 2015-4-7 09:18:57

Thanks for your share!!!

古襄月 发表于 2015-4-7 19:01:57

不错。。

Joice晗 发表于 2015-10-23 09:19:44

感觉真的高大上·~~

SComsol 发表于 2016-2-22 15:10:23

试一下:):lol:)

cuiziyang 发表于 2020-2-18 17:14:47

优秀的人一直都在,像大佬学习!

cuiziyang 发表于 2020-2-18 18:41:49

优秀,像大佬学习66666

pcstudying 发表于 2020-4-8 08:20:37

优秀的人,不仅自己优秀,还会分享,让我们学习
页: [1]
查看完整版本: Comsol with Matlab 应用实例(3)