欢迎来到某某鲜果配送有限公司!

专注鲜果配送

新鲜 / 健康 / 便利 / 快速 / 放心

全国咨询热线020-88888888
摩登7-摩登娱乐-摩登注册登录入口

新闻中心

 

推荐产品

24小时服务热线 020-88888888

公司资讯

99行Matlab拓扑优化程序解析—0 概述

发布日期:2024-04-29 03:27浏览次数:

本文给出99行Matlab拓扑优化程序的流程图和注释供读者参考,后续会继续更新详细的解析,喜欢的朋友可以关注一下。

图1 99行Matlab拓扑优化程序流程图

其中,除右上角输入的五个参数外,x为伪密度矩阵,U为位移矩阵,dc为灵敏度矩阵。

%%%% A 99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE SIGMUND, JANUARY 2000 %%%
%%%% CODE MODIFIED FOR INCREASED SPEED, September 2002, BY OLE SIGMUND %%%
function top99(nelx,nely,volfrac,penal,rmin) %%(X方向单元数,Y方向单元数,目标体分比,惩罚因子,最小过滤半径)
% INITIALIZE(初始化)
x(1:nely,1:nelx) = volfrac; %%初始化伪密度矩阵x
loop = 0;                   %%初始化迭代次数
change = 1.;                %%初始化循环控制条件(伪密度变化)
% START ITERATION(开始迭代)
while change > 0.01         %%当伪密度变化小于0.01时停止 
  loop = loop + 1;          %%迭代次数+1
  xold = x;                 %%储存当前密度值
% FE-ANALYSIS(有限元分析)
  [U]=FE(nelx,nely,x,penal);        %%调用有限元分析函数,返回整体位移矩阵U        
% OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS(目标函数和灵敏度分析)
  [KE] = lk;                        %%获取单元刚度矩阵KE,各单元均相同
  c = 0.;                           %%初始化柔顺度
  for ely = 1:nely
    for elx = 1:nelx
      n1 = (nely+1)*(elx-1)+ely;    %%计算左上节点编号n1
      n2 = (nely+1)* elx   +ely;    %%计算右上节点编号n2
      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)^penal*Ue'*KE*Ue;                                 %%计算柔顺度
      dc(ely,elx) = -penal*x(ely,elx)^(penal-1)*Ue'*KE*Ue;                %%计算灵敏度
    end
  end
% FILTERING OF SENSITIVITIES(灵敏度过滤)
  [dc]   = check(nelx,nely,rmin,x,dc); %%调用网格过滤函数,返回过滤后的灵敏度   
% DESIGN UPDATE BY THE OPTIMALITY CRITERIA METHOD(优化准则法求解)
  [x]    = OC(nelx,nely,x,volfrac,dc); %%利用优化准则法优化设计变量
% PRINT RESULTS(输出结果)
  change = max(max(abs(x-xold)));      %%更新伪密度变化
  disp([' It.: ' sprintf('%4i',loop) ' Obj.: ' sprintf('%10.4f',c) ...
       ' Vol.: ' sprintf('%6.3f',sum(sum(x))/(nelx*nely)) ...
        ' ch.: ' sprintf('%6.3f',change )])                                  %%输出
% PLOT DENSITIES(绘制优化结果)
  colormap(gray); imagesc(-x); axis equal; axis tight; axis off;pause(1e-6); %%绘图
end 
%%%%%%%%%% OPTIMALITY CRITERIA UPDATE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xnew]=OC(nelx,nely,x,volfrac,dc)  
l1 = 0; l2 = 100000; move = 0.2; %%初始化优化边界,设计变量单次变化最大范围
while (l2-l1 > 1e-4)             %%收敛条件
  lmid = 0.5*(l2+l1);            %%取优化边界中点
  xnew = max(0.001,max(x-move,min(1.,min(x+move,x.*sqrt(-dchttps://zhuanlan.zhihu.com/p/lmid))))); %%计算设计变量
  if sum(sum(xnew)) - volfrac*nelx*nely > 0                             %%边界收缩
    l1 = lmid;
  else
    l2 = lmid;
  end
end
%%%%%%%%%% MESH-INDEPENDENCY FILTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [dcn]=check(nelx,nely,rmin,x,dc)
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);                   %%计算各节点到过滤边界的距离
        sum = sum+max(0,fac);                               %%计算有效区域内各节点到过滤边界的距离之和
        dcn(j,i) = dcn(j,i) + max(0,fac)*x(l,k)*dc(l,k);    %%灵敏度加权叠加
      end
    end
    dcn(j,i) = dcn(j,i)/(x(j,i)*sum);                       %%进行平均计算,完成灵敏度过滤
  end
end
%%%%%%%%%% FE-ANALYSIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [U]=FE(nelx,nely,x,penal)
[KE] = lk; %%获取单元刚度矩阵KE,各单元均相同
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; %%计算左上节点编号n1
    n2 = (nely+1)* elx   +ely; %%计算右上节点编号n2
    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)^penal*KE;                   %%组装总刚度矩阵
  end
end
% DEFINE LOADS AND SUPPORTS (HALF MBB-BEAM)(半MBB梁载荷约束)
F(2,1) = -1;                                                    %%单元1第1个节点(全局节点号2)受Y轴负方向的力
fixeddofs   = union([1:2:2*(nely+1)],[2*(nelx+1)*(nely+1)]);    %%约束自由度,半MBB梁左端部X方向约束,右下角Y方向约束
alldofs     = [1:2*(nely+1)*(nelx+1)];                          %%全部自由度
freedofs    = setdiff(alldofs,fixeddofs);                       %%未约束自由度=全部自由度-约束自由度
% SOLVING
U(freedofs,:) = K(freedofs,freedofs) \\ F(freedofs,:);           %%计算未约束自由度位移     
U(fixeddofs,:)= 0;                                              %%约束自由度位移为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)]; %%单元刚度矩阵
  1. O. Sigmund. A 99 line topology optimization code written in Matlab[J]. Structural and Multidisciplinary Optimization,2001,21(2).
A 99 line topology optimization code written in Matlab.pdf

2. 注释后的程序代码

top99.m


如有不当的地方,欢迎大家指正!

020-88888888

平台注册入口