声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2228|回复: 3

[共享资源] 求教,这段程序代码什么意思看不懂

[复制链接]
发表于 2010-11-12 16:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
% use save command to conserve x and m varibles
function husttopopt(nelx,nely,volfrac,penal,rmin)            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%penal is penalization factor
% INITIALIZE
m=1;
x(1:nely,1:nelx,m) = volfrac;
loop = 0;
change = 1.;
% START ITERATION
while change > 0.01  
%while change >0.0001
  loop = loop + 1;
%  xold = x;
% FE-ANALYSIS
  [U]=FE(nelx,nely,x,penal,m);         
% OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
  [KE] = lk;
  c = 0.;
  for ely = 1:nely
    for elx = 1:nelx
      n1 = (nely+1)*(elx-1)+ely;
      n2 = (nely+1)* elx   +ely;
      Ue = U([2*n1-1;2*n1; 2*n2-1;2*n2; 2*n2+1;2*n2+2; 2*n1+1;2*n1+2],1);
      c = c + x(ely,elx,m)^penal*Ue'*KE*Ue;
      dc(ely,elx) = -penal*x(ely,elx,m)^(penal-1)*Ue'*KE*Ue;
    end
  end
% FILTERING OF SENSITIVITIES
  [dc]   = check(nelx,nely,rmin,x,dc,m);   
% DESIGN UPDATE BY THE OPTIMALITY CRITERIA METHOD
  [x]    = OC(nelx,nely,x,volfrac,dc,m);
% PRINT RESULTS
  change = max(max(abs(x(:,:,m+1)-x(:,:,m))));
  disp([' It.: ' sprintf('%4i',loop) ' Obj.: ' sprintf('%10.4f',c) ...
       ' Vol.: ' sprintf('%6.3f',sum(sum(x(:,:,m+1)))/(nelx*nely)) ...
        ' ch.: ' sprintf('%6.3f',change )])
% PLOT DENSITIES  
  colormap(gray); imagesc(-x(:,:,m+1));axis equal; axis tight; axis off;pause(1e-6);
  xc(m)=c;   xdc(:,:,m)=dc;  
  xchange(m)=change;   xvol(m)=sum(sum(x(:,:,m+1)))/(nelx*nely);
  m=m+1;
  if loop >= 200
      break;
  end
end
save xmdata x xc xdc m xchange xvol;
%%%%%%%%%% OPTIMALITY CRITERIA UPDATE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [x]=OC(nelx,nely,x,volfrac,dc,m)  
l1 = 0; l2 = 100000; move = 0.2;
while (l2-l1 > 1e-4)
  lmid = 0.5*(l2+l1);
  x(:,:,m+1) = max(0.001,max(x(:,:,m)-move,min(1.,min(x(:,:,m)+move,x(:,:,m).*((-dc./lmid).^0.5)))));%%%%%%%%%%%%%%%%sqrt=0.5 is damping coefficient
  if sum(sum(x(:,:,m+1))) - volfrac*nelx*nely > 0
    l1 = lmid;
  else
    l2 = lmid;
  end
end
%%%%%%%%%% MESH-INDEPENDENCY FILTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [dcn]=check(nelx,nely,rmin,x,dc,m)
dcn=zeros(nely,nelx);
for i = 1:nelx
  for j = 1:nely
    sum=0.0;
    for k = max(i-floor(rmin),1):min(i+floor(rmin),nelx)
      for l = max(j-floor(rmin),1):min(j+floor(rmin),nely)
        fac = rmin-sqrt((i-k)^2+(j-l)^2);          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%rimn is filter radius
        sum = sum+max(0,fac);
        dcn(j,i) = dcn(j,i) + max(0,fac)*x(l,k,m)*dc(l,k);
        end
    end
    dcn(j,i) = dcn(j,i)/(x(j,i,m)*sum);
  end
end
%%%%%%%%%% FE-ANALYSIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [U]=FE(nelx,nely,x,penal,m)
[KE] = lk;
K = sparse(2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1));
F = sparse(2*(nely+1)*(nelx+1),1); U = zeros(2*(nely+1)*(nelx+1),1);
for elx = 1:nelx
  for ely = 1:nely
    n1 = (nely+1)*(elx-1)+ely;
    n2 = (nely+1)* elx   +ely;
    edof = [2*n1-1; 2*n1; 2*n2-1; 2*n2; 2*n2+1; 2*n2+2; 2*n1+1; 2*n1+2];
%    y(ely,elx)=x(ely,elx,m);
    K(edof,edof) = K(edof,edof) + x(ely,elx,m)^penal*KE;
  end
end
% DEFINE LOADS AND SUPPORTS (HALF MBB-BEAM)
%F(2,1) = -1;
%fixeddofs   = union([1:2:2*(nely+1)],[2*(nelx+1)*(nely+1)]);
%F((nelx*(nely+1)+2),1)=-1;
%fixeddofs =union([(nely+1)*2],[((nely+1)*(nelx+1)*2-1):(nely+1)*(nelx+1)*2]);
%臂梁中间载荷
F((nelx*2*(nely+1)+2*floor(nely/2)),1)=-1;
fixeddofs =[1:2*(nely+1)];

alldofs     = [1:2*(nely+1)*(nelx+1)];
freedofs    = setdiff(alldofs,fixeddofs);
% SOLVING
U(freedofs,:) = K(freedofs,freedofs) \ F(freedofs,:);      
U(fixeddofs,:)= 0;
%%%%%%%%%% ELEMENT STIFFNESS MATRIX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [KE]=lk
E = 1.;
nu = 0.3;
k=[ 1/2-nu/6   1/8+nu/8 -1/4-nu/12 -1/8+3*nu/8 ...
   -1/4+nu/12 -1/8-nu/8  nu/6       1/8-3*nu/8];
KE = E/(1-nu^2)*[ k(1) k(2) k(3) k(4) k(5) k(6) k(7) k(8)
                  k(2) k(1) k(8) k(7) k(6) k(5) k(4) k(3)
                  k(3) k(8) k(1) k(6) k(7) k(4) k(5) k(2)
                  k(4) k(7) k(6) k(1) k(8) k(3) k(2) k(5)
                  k(5) k(6) k(7) k(8) k(1) k(2) k(3) k(4)
                  k(6) k(5) k(4) k(3) k(2) k(1) k(8) k(7)
                  k(7) k(4) k(5) k(2) k(3) k(8) k(1) k(6)
                  k(8) k(3) k(2) k(5) k(4) k(7) k(6) k(1)];
回复
分享到:

使用道具 举报

发表于 2010-11-12 19:26 | 显示全部楼层
希望楼主给出背景知识啊!
 楼主| 发表于 2010-11-13 20:03 | 显示全部楼层
加我q;358939206,这是关于拓扑优化的 程序要有优化设计的 知识做基础
发表于 2010-11-13 21:14 | 显示全部楼层
本帖最后由 zhouyang664 于 2010-11-14 23:54 编辑

建议先看下:@)
建议提问的网友分清 编程问题 和 专业问题
http://forum.vibunion.com/forum-viewthread-tid-36746-highlight-%B1%E0%B3%CC%CE%CA%CC%E2.html
还有LZ的三个帖好像重覆, 根本可以合并
http://forum.vibunion.com/thread-97304-1-1.html  http://forum.vibunion.com/thread-97284-1-1.html
http://forum.vibunion.com/thread-97282-1-1.html

如LZ所说"这是关于拓扑优化的程序要有优化设计的知识做基础", 还没研究过, 仅提供相关连接, LZ参考下
xinyuxf : http://forum.vibunion.com/thread-91992-1-1.html
86029 : http://forum.vibunion.com/thread-92471-1-1.html
原址 : http://www.topopt.dtu.dk/ ,  code & paper : http://www.topopt.dtu.dk/?q=node/2

点评

赞成,并不是每个人都有专业知识的!  发表于 2010-11-14 22:35
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-29 12:21 , Processed in 0.063326 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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