声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1463|回复: 2

[编程技巧] matlab拓扑优化固有频率最大为目标体积比为约束

[复制链接]
发表于 2011-3-21 20:47 | 显示全部楼层 |阅读模式

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

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

x
固有频率最大为目标,体积比为约束,在一个矩形设计域里4个角固定,设计加速度弹性梁和质量块的最优结构拓扑形式。程序请教热心高手看看我 哪儿有错
% use save command to conserve x and m varibles
function husttopoptzcjzcj(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  
  loop = loop + 1;
% FE-ANALYSIS
  [U]=FE(nelx,nely,x,penal,m);       %% [U]表示质量矩阵的逆矩阵与刚度矩阵乘积,即报告书中矩阵[S]  
% OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
  [KE] = lk;
  [ME] = zl;
c1=0;
  c2=0;
   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],[2*n1-1;2*n1; 2*n2-1;2*n2; 2*n2+1;2*n2+2; 2*n1+1;2*n1+2]);
       [mt,my]= maxtr(Ue);    %%调用程序求特征值mt和特征向量my
      c1 = x(ely,elx,m)^penal*my'*KE*my;   
      c2 =  x(ely,elx,m)*my'*ME*my;
      c= c+c1/c2;    %%目标函数
      dc(ely,elx) = -penal*x(ely,elx,m)^(penal-1)*my'*KE*my + c*my'*ME*my; %%%敏度分析,后一项“+”“-”还不确定
    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;
[ME]= zl;
K = sparse(2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1));
M = sparse(2*(nely+1)*(nelx+1), 2*(nelx+1)*(nely+1));
U = zeros( 2*(nely+1)*(nelx+1),  2*(nely+1)*(nelx+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];
    K(edof,edof) = K(edof,edof) + x(ely,elx,m)^penal*KE;  %%刚度矩阵
    M(edof,edof) = M(edof,edof) + x(ely,elx,m)*ME;   %%质量矩阵
  end
end
% DEFINE LOADS AND SUPPORTS (HALF MBB-BEAM)
d1=union([1:2],[((nely+1)*2-1):((nely+1)*2)]);
d2=union([(nelx*2*(nely+1)+1):(nelx*2*(nely+1)+2)],[((nely+1)*(nelx+1)*2-1):(nely+1)*(nelx+1)*2]);
fixeddofs=union(d1,d2);%%%固定约束四角点
alldofs     = [1:2*(nely+1)*(nelx+1)];%%全约束
freedofs    = setdiff(alldofs,fixeddofs);%%自由
% SOLVING
U(freedofs,:) = M(freedofs,freedofs) \ K(freedofs,:)  %%源程序中U表示位移, 现在表示质量矩阵的逆矩阵与刚度矩阵乘积,即报告书中矩阵[S]   
U(fixeddofs,:)= 0;   %%四角固定,不知道刚度矩阵k和质量矩阵m在约束点是多少,如果是位移可以设置为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)];
%%%%%%%%%% Mass STIFFNESS MATRIX 质量矩阵%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ME]=zl
r=1.;
w=[4  0  2  0  1  0  2  0];
ME=r/9*[ w(1) w(2) w(3) w(4) w(5) w(6) w(7) w(8)
         w(2) w(1) w(2) w(3) w(4) w(5) w(6) w(7)
         w(3) w(2) w(1) w(2) w(3) w(4) w(5) w(6)
         w(4) w(3) w(2) w(1) w(2) w(3) w(4) w(5)
         w(5) w(4) w(3) w(2) w(1) w(2) w(3) w(4)
         w(6) w(5) w(4) w(3) w(2) w(1) w(2) w(3)
         w(7) w(6) w(5) w(4) w(3) w(2) w(1) w(2)
         w(8) w(7) w(6) w(5) w(4) w(3) w(2) w(1)];
%%%%%%%%%% Eigenvalues and Eigenvectors求特征值和特征向量%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [mt,my]= maxtr(Ue)
n=length(Ue);
x0=diag(ones(n));
k=1;
x=Ue*x0;
while norm(x-x0)>0.0001
    k=k+1;
    q=x;
    y=x/maxnorm(x);
    x=Ue*y;
    x0=q;
end
mt=maxnorm(x);
my=y;
%%%%%%%%%% Eigenvalues and Eigenvectors求特征值和特征向量(求取数列中模最大分量)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  t= maxnorm(Ue)
n=length(Ue);
t=0;
for  i=1:n
    if abs(Ue(i)/max(abs(Ue)))>=1
        t=Ue(i);
    end
end

本帖被以下淘专辑推荐:

回复
分享到:

使用道具 举报

 楼主| 发表于 2011-3-21 20:49 | 显示全部楼层
回复 1 # ajiang 的帖子

如果哪位热心人愿意帮我看看,加我扣扣:358939206
 楼主| 发表于 2011-4-1 16:56 | 显示全部楼层
唉,真的没人懂吗,求高手帮我看看,有搞拓扑优化的多联系交流,扣扣:358939206
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-12-28 06:16 , Processed in 0.092373 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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