概述
主要的解析来自百度,但是添加了很多新手看不懂的解释,毕竟我是小白……
function top(nelx,nely,volfrac,penal,rmin);
nelx=80; % x轴方向上单元个数
nely=20; % y轴方向上单元个数
volfrac=0.4; %体积比
penal=3; %材料插值的惩罚因子
rmin=2; %敏度过滤半径
% INITIALIZE
x(1:nely,1:nelx) = volfrac; % x是设计变量(单元伪密度)
loop = 0; %存放迭代次数的变量
change = 1.;%每次迭代,目标函数(柔度)的改变值,用来判断何时收敛
% START ITERATION
while change > 0.01 %当两次连续目标函数迭代的差<=0.01时,迭代结束
loop = loop + 1;
xold = x; %把前一次的设计变量付给xold
% FE-ANALYSIS
[U]=FE(nelx,nely,x,penal); %有限元分析,得到位移矢量U
% 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)^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(-dc./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) %自定义函数,最后返回[U]
[KE] = lk; %单元刚度矩阵
% sparse 把一个全矩阵转化为一个稀疏矩阵,只存储每一个非零元素的三个值:元素值,元素的行号和列号
%总体刚度矩阵的稀疏矩阵
% *2是因为x,y都有一个数
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; %右上
% 左上,右上,右下,左下 自由度
% 一个点有两个,所以要*2。第一个从1开始,所以*2之后要-1。
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)
F(2,1) = -1; % 应用了一个在左上角的垂直单元力。
%按着图上来的,最左边和右下角已经固定
fixeddofs = union([1:2:2*(nely+1)],[2*(nelx+1)*(nely+1)]); %固定结点
alldofs = [1:2*(nely+1)*(nelx+1)]; %所有结点
% setdiff 因无约束自由度与固定自由度的不同来找到无约束自由度
freedofs = setdiff(alldofs,fixeddofs); %不受约束的自由度
% SOLVING
U(freedofs,:) = K(freedofs,freedofs) F(freedofs,:);
U(fixeddofs,:)= 0; % 矩阵A的第r行:A(r,:)
%%%%%%%%%% 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];
%u1,v1, u2,v2, u3,v3, u4,v4
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)];
附录 99行代码
1 %%%% A99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE
SIGMUND,OCTOBER 1999 %%%
2function top(nelx,nely,volfrac,penal,rmin);
3 %INITIALIZE
4x(1:nely,1:nelx) = volfrac;
5 loop =0;
6 change= 1.;
7 %START ITERATION
8 whilechange > 0.01
9 loop =loop + 1;
10 xold= x;
11 %FE-ANALYSIS
12[U]=FE(nelx,nely,x,penal);
13 %OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
14 [KE]= lk;
15 c =0.;
16 forely = 1:nely
17 forelx = 1:nelx
18 n1 =(nely+1)*(elx-1)+ely;
19 n2 =(nely+1)* elx +ely;
20 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);
21 c = c+ x(ely,elx)^penal*Ue’*KE*Ue;
22 dc(ely,elx)= -penal*x(ely,elx)^(penal-1)*
Ue’*KE*Ue;
23 end
24 end
25 %FILTERING OF SENSITIVITIES
26 [dc]= check(nelx,nely,rmin,x,dc);
27 %DESIGN UPDATE BY THE OPTIMALITY CRITERIA METHOD
28 [x] =OC(nelx,nely,x,volfrac,dc);
29 %PRINT RESULTS
30change = max(max(abs(x-xold)));
31disp([’ It.: ’ sprintf(’%4i’,loop) ’ Obj.: ’
sprintf(’%10.4f’,c)...
32 ’Vol.: ’ sprintf(’%6.3f’,sum(sum(x))/
(nelx*nely))...
33 ’ch.: ’ sprintf(’%6.3f’,change )])
34 %PLOT DENSITIES
35colormap(gray); imagesc(-x); axis equal; axis
tight;axis off;pause(1e-6);
36 end
37%%%%%%%%%% OPTIMALITY CRITERIA UPDATE %%%%%%%%%
38function [xnew]=OC(nelx,nely,x,volfrac,dc)
39 l1 =0; l2 = 100000; move = 0.2;
40 while(l2-l1 > 1e-4)
41 lmid= 0.5*(l2+l1);
42 xnew= max(0.001,max(x-move,min(1.,min(x+move,x.
*sqrt(-dc./lmid)))));
43 ifsum(sum(xnew)) - volfrac*nelx*nely > 0;
44 l1 =lmid;
45 else
46 l2 =lmid;
47 end
48 end
49%%%%%%%%%% MESH-INDEPENDENCY FILTER %%%%%%%%%%%
50function [dcn]=check(nelx,nely,rmin,x,dc)
51 dcn=zeros(nely,nelx);
52 for i= 1:nelx
53 for j= 1:nely
54sum=0.0;
55 for k= max(i-round(rmin),1):
min(i+round(rmin),nelx)
56 for l= max(j-round(rmin),1):
min(j+round(rmin),nely)
57 fac =rmin-sqrt((i-k)^2+(j-l)^2);
58 sum =sum+max(0,fac);
59dcn(j,i) = dcn(j,i) + max(0,fac)*x(l,k)
*dc(l,k);
60 end
61 end
62dcn(j,i) = dcn(j,i)/(x(j,i)*sum);
63 end
64 end
65%%%%%%%%%% FE-ANALYSIS %%%%%%%%%%%%
66function [U]=FE(nelx,nely,x,penal)
67 [KE]= lk;
68 K =sparse(2*(nelx+1)*(nely+1), 2*(nelx+1)*
(nely+1));
69 F =sparse(2*(nely+1)*(nelx+1),1); U =
sparse(2*(nely+1)*(nelx+1),1);
70 forely = 1:nely
71 forelx = 1:nelx
72 n1 =(nely+1)*(elx-1)+ely;
73 n2 =(nely+1)* elx +ely;
74 edof= [2*n1-1; 2*n1; 2*n2-1; 2*n2; 2*n2+1;
2*n2+2;2*n1+1;2*n1+2];
75 K(edof,edof)= K(edof,edof) +
x(ely,elx)^penal*KE;
76 end
77 end
78 %DEFINE LOADSAND SUPPORTS(HALF MBB-BEAM)
79F(2,1) = -1;
80fixeddofs = union([1:2:2*(nely+1)],
[2*(nelx+1)*(nely+1)]);
81alldofs = [1:2*(nely+1)*(nelx+1)];
82freedofs = setdiff(alldofs,fixeddofs);
83 %SOLVING
84U(freedofs,:) = K(freedofs,freedofs)
F(freedofs,:);
85U(fixeddofs,:)= 0;
86%%%%%%%%%% ELEMENT STIFFNESS MATRIX %%%%%%%
87function [KE]=lk
88 E =1.;
89 nu =0.3;
90 k=[1/2-nu/6 1/8+nu/8 -1/4-nu/12 -1/8+3*nu/8 ...
91 -1/4+nu/12-1/8-nu/8 nu/6 1/8-3*nu/8];
92 KE =E/(1-nu^2)*
[ k(1)k(2) k(3) k(4) k(5) k(6) k(7) k(8)
93 k(2)k(1) k(8) k(7) k(6) k(5) k(4) k(3)
94 k(3)k(8) k(1) k(6) k(7) k(4) k(5) k(2)
95 k(4)k(7) k(6) k(1) k(8) k(3) k(2) k(5)
96 k(5)k(6) k(7) k(8) k(1) k(2) k(3) k(4)
97 k(6)k(5) k(4) k(3) k(2) k(1) k(8) k(7)
98 k(7)k(4) k(5) k(2) k(3) k(8) k(1) k(6)
99 k(8)k(3) k(2) k(5) k(4) k(7) k(6) k(1)];
最后
以上就是迅速羊为你收集整理的99行拓扑优化matlab程序解读,99行拓扑优化 代码解析的全部内容,希望文章能够帮你解决99行拓扑优化matlab程序解读,99行拓扑优化 代码解析所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复