找回密码
 注册
Simdroid-非首页
查看: 1686|回复: 15

[15.分享让我牛B] Comsol with Matlab 应用实例(3)

[复制链接]
发表于 2013-7-19 13:28:35 | 显示全部楼层 |阅读模式 来自 上海
一直有个想法,做一个随机圆柱体填充到立方体中,效果如图:

计算几何的算法会更好,无奈翻了下发现都是公式,不知道怎么去用。于是用了个偷懒的方法——试探。当然这种试探的方法,效率就很低了。
我的方法主要用到了:在底面找个随机位置,然后随机方向上放置圆柱体,然后判断是否和之前的圆柱体相交。
从而达到建立互不干涉的圆柱体。
  1. function out = randcylinder
  2. % This is a script for Comsol(4.x) with Matlab.
  3. % It product random position cylinders inside a block.
  4. %
  5. % Copyright to mxio at Shanghai University.
  6. %
  7. %                                                        mxio
  8. %                                                19,July,2013
  9. import com.comsol.model.*
  10. import com.comsol.model.util.*
  11. model = ModelUtil.create('Model');
  12. model.modelNode.create('mod1');
  13. model.geom.create('geom1', 3);

  14. blk_length = 1;
  15. blk_size = [blk_length blk_length blk_length];
  16. cyl_radius = 0.02 * blk_length;
  17. cyl_length = blk_length + 4.5 * cyl_radius;       % about 3*sqrt(2)*cyl_radius
  18. large_blk_position_z = -1.5 * cyl_radius;
  19. blk_position = [0 0 0];
  20. cy_num_total = 50;
  21. cy_num = 2;
  22. rng('shuffle'); % random number seed

  23. model.geom('geom1').feature.create('cyl1', 'Cylinder');
  24. model.geom('geom1').feature('cyl1').set('r', num2str(cyl_radius));
  25. model.geom('geom1').feature('cyl1').set('h', num2str(cyl_length));
  26. model.geom('geom1').feature('cyl1').set('pos', [rand rand large_blk_position_z]);
  27. model.geom('geom1').feature('cyl1').set('ax2', [180*rand-90 360*rand]);
  28. model.geom('geom1').run('cyl1');


  29. cy_name_cell= {'cyl1'};
  30. while cy_num<=cy_num_total
  31.     model.geom('geom1').feature.create('uni1', 'Union');
  32.     model.geom('geom1').feature('uni1').selection('input').set(cy_name_cell);
  33.     model.geom('geom1').run('uni1');
  34.     cy_name = ['cyl',num2str(cy_num)];
  35.     model.geom('geom1').feature.create(cy_name, 'Cylinder');
  36.     model.geom('geom1').feature(cy_name).set('r', num2str(cyl_radius));
  37.     model.geom('geom1').feature(cy_name).set('h', num2str(cyl_length));
  38.     model.geom('geom1').feature(cy_name).set('pos', [rand rand large_blk_position_z]);
  39.     model.geom('geom1').feature(cy_name).set('ax2', [180*rand-90 360*rand]);
  40.     model.geom('geom1').run(cy_name);
  41.     model.geom('geom1').feature.create('int1', 'Intersection');
  42.     tmp_name = [cy_name {'uni1'}];
  43.     model.geom('geom1').feature('int1').selection('input').set(tmp_name);
  44.     model.geom('geom1').run('int1');
  45.     if isempty(mphshowerrors(model))
  46.         model.geom('geom1').feature.remove(cy_name);
  47.     else
  48.         cy_name_cell{cy_num}= cy_name;
  49.         cy_num = cy_num + 1;
  50.     end
  51.     model.geom('geom1').feature.remove('uni1');
  52.     model.geom('geom1').feature.remove('int1');
  53. end

  54. model.geom('geom1').feature.create('blk1', 'Block');
  55. model.geom('geom1').feature('blk1').set('size',blk_size);
  56. model.geom('geom1').feature('blk1').set('pos', blk_position);

  57. model.geom('geom1').feature.create('uni1', 'Union');
  58. model.geom('geom1').feature('uni1').selection('input').set(cy_name_cell);
  59. model.geom('geom1').run('uni1');

  60. model.geom('geom1').feature.create('int1', 'Intersection');
  61. tmp_name = {'uni1', 'blk1'};
  62. model.geom('geom1').feature('int1').selection('input').set(tmp_name);
  63. model.geom('geom1').run('int1');

  64. model.geom('geom1').runAll;
  65. mphgeom(model);

  66. out = model;
复制代码
mxio
2013.7.19

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

1

查看全部评分

发表于 2013-10-12 20:44:06 | 显示全部楼层 来自 北京
Simdroid开发平台
NoEureka 发表于 2013-9-23 09:04
请问为什么我直接运行的时候,报错说Undefined function 'mphshowerrors'。我用的是comsol4.2 ...

4.2的不能用这个函数,需要修改。
回复 1 不支持 0

使用道具 举报

发表于 2013-7-19 20:50:31 | 显示全部楼层 来自 陕西西安
楼主乃真汉子也!收下学习了。
回复 不支持

使用道具 举报

发表于 2013-9-13 18:43:57 | 显示全部楼层 来自 辽宁大连
楼主的方法简直了,另辟蹊径啊!
回复 不支持

使用道具 举报

发表于 2013-9-23 09:04:56 | 显示全部楼层 来自 大连理工大学
请问为什么我直接运行的时候,报错说Undefined function 'mphshowerrors'。我用的是comsol4.2
回复 不支持

使用道具 举报

发表于 2013-12-4 21:01:56 | 显示全部楼层 来自 清华大学
楼主发的真是好贴啊,抛砖引玉
回复 不支持

使用道具 举报

发表于 2014-10-31 17:16:32 | 显示全部楼层 来自 山西晋中
楼主真棒,真是长见识
回复 不支持

使用道具 举报

发表于 2015-4-7 09:18:57 | 显示全部楼层 来自 山东青岛
Thanks for your share!!!
回复 不支持

使用道具 举报

发表于 2015-10-23 09:19:44 | 显示全部楼层 来自 上海
感觉真的高大上·~~
回复 不支持

使用道具 举报

发表于 2020-2-18 17:14:47 | 显示全部楼层 来自 中国
优秀的人一直都在,像大佬学习!
回复 不支持

使用道具 举报

发表于 2020-2-18 18:41:49 | 显示全部楼层 来自 中国
优秀,像大佬学习66666
回复 不支持

使用道具 举报

发表于 2020-4-8 08:20:37 | 显示全部楼层 来自 中国
优秀的人,不仅自己优秀,还会分享,让我们学习
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-4-27 15:34 , Processed in 0.040381 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表