ainatipen 发表于 2022-1-23 07:52

MMA算法的推导及3D简支梁拓扑优化代码详解

MMA算法的推导及代码详解

对于变密度的参数化方法,设计变量x为材料相对密度,在已知材料的物性,包括弹性模型、密度以及给定载荷的条件下,我们希望简支梁的柔度最小,或者说使得结构势能最小(结构在力作用下的位移,该力做的功也就是势能)。那么当材料总体积保持为常数不变,给定载荷不变的情况下柔度最小可以理解为结构刚度最强。
问题描述

在结构力学相关设计中,通常会出现结构冗余的情况,例如受到一定载荷作用下的悬臂梁和简支梁其结构质量可以进一步减小。这一点在桥梁等设计中体现的更为明显。但是传统的桥梁设计方案是根据经验选择合适的桁架结构然后不断迭代设计方案。这种做法十分繁琐,目前我们可以通过拓扑优化的技术在概念设计阶段就提出最优方案,减少材料的浪费。



目前拓扑优化的参数化方法种类有很多,例如变密度法、水平集法、构型理论等。其中比较常见的是变密度法。密度法假设有一种材料密度可以在0-1之间变化。以这种参数为设计变量,密度为1代表材料存在,密度为0代表材料不存在。这样就在拓扑优化问题与材料分布问题之间建立了联系。
拓扑优化问题与传统最优化问题在基本原理上互通,其数学本质为求多元函数的极值问题。其基本形式可以进行如下抽象:
设 https://www.zhihu.com/equation?tex=%5Cmathbf%7Bx%7D+%3D+%5Cleft%28+x_%7B1%7D%2Cx_%7B2%7D%2C+%5Ccdot+%5Ccdot+%5Ccdot+x_%7Bn%7D+%5Cright%29%5E%7BT%7D 为n维欧氏空间 https://www.zhihu.com/equation?tex=%5Cmathbf%7BR%7D%5E%7Bn%7D 内一点, https://www.zhihu.com/equation?tex=f%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright%29%2Cg_%7Bi%7D%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright%29%5Cleft%28+i+%3D+1%2C2%2C+%5Ccdot+%5Ccdot+%5Ccdot+%2Cm+%5Cright%29 和 https://www.zhihu.com/equation?tex=h_%7Bj%7D%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright%29%5Cleft%28+j+%3D+m+%2B+1%2C+%5Ccdot+%5Ccdot+%5Ccdot+%2Cp+%5Cright%29 均为定的n元函数。最优化问题可表述为:在如下的约束条件下:

https://www.zhihu.com/equation?tex=+g_%7Bi%7D%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright%29+%5Cleq+0~~~~i+%3D+1%2C2+%5Ccdot+%5Ccdot+%5Ccdot+m+

https://www.zhihu.com/equation?tex=+h_%7Bj%7D%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright%29+%3D+0~~~~j+%3D+m+%2B+1%2C+%5Ccdot+%5Ccdot+%5Ccdot+p+
求的最小值(或最大值)。通常称为目标函数, https://www.zhihu.com/equation?tex=g_%7Bi%7D%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright%29+%5Cleq+0 为不等式约束条件, https://www.zhihu.com/equation?tex=h_%7Bj%7D%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright%29+%3D+0 为等式约束条件, https://www.zhihu.com/equation?tex=x 为设计变量。通用的表达形式为:

https://www.zhihu.com/equation?tex=+%5Cleft%5C%7B+%5Cbegin%7Bmatrix%7D+%7B%5Cmin%7B~~~~f%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright%29%7D%7D+%5C%5C+%7Bs.t.~~~~g_%7Bi%7D%5Cleft%28+%5Cmathbf%7Bx%7D+%5Cright.+%29%5Cleq+0~~~~i+%3D+1%2C2%2C+%5Ccdot+%5Ccdot+%5Ccdot+m~%7D+%5C%5C+%7Bh_%7Bj%7D%5Cleft%28%5Cmathbf%7Bx%7D+%5Cright.%29+%3D+0~~~~j+%3D+m+%2B+1%2C+%5Ccdot+%5Ccdot+%5Ccdot+p%7D+%5C%5C+%7Bx+%5Cin+R%5E%7Bn%7D%7D+%5C%5C+%5Cend%7Bmatrix%7D+%5Cright.+
上式又称优化列式,是待求解问题的表达式。
令 https://www.zhihu.com/equation?tex=+R+%3D+%5Cleft%5C%7B+x+%5Cmiddle%7C+g_%7Bi%7D%5Cleft%28+x+%5Cright%29+%5Cleq+0%2Ci+%3D+1%2C2%2C+%5Ccdot+%5Ccdot+%5Ccdot+m%3Bh_%7Bj%7D%5Cleft%28+x+%5Cright%29+%3D+0%2Cj+%3D+m+%2B+1%2C+%5Ccdot+%5Ccdot+%5Ccdot+p+%5Cright%5C%7D+ 称 https://www.zhihu.com/equation?tex=x+%5Cin+R 为上述问题的一个可行解集。
算法推导

基于OC算法并不适用于求解多约束条件的复杂问题,这时候学术界更多使用基于一阶泰勒展开的移动渐近线法。OC算法的详细推导和2D拓扑优化可以看我的另一篇文章https://blog.csdn.net/qq_42183549/article/details/122369170
这样复杂的优化问题一般可以被描述为:

https://www.zhihu.com/equation?tex=P%3A

https://www.zhihu.com/equation?tex=+%5Cbegin%7Bcases%7D+min~~~~~f_0%28x%29%5C%5C+f_i%28x%29%5Cle%5Cwidehat%7Bf_i%7D~~~~~for~i%3D1%2C%5Cdots%2Cm%5C%5C+x_jmin%5Cle~x_j%5Cle~x_jmax+%5Cend%7Bcases%7D+
对该问题的简化分为四步:

[*]选择合适的初始值
[*]计算迭代后得到的以及每个约束函数的梯度
[*] 产生原问题严格凸子问题
[*] 利用迭代产生的结果求解子问题,并返回第二步
所以求解该问题的关键在于生成严格凸的子问题。
对于第k步迭代产生的变量

https://www.zhihu.com/equation?tex=+L_j%5E%7B%28k%29%7D%3Cx_j%5E%7B%28k%29%7D%3CU_j%5E%7B%28k%29%7D+
对于第k次迭代的约束条件被定义为:

https://www.zhihu.com/equation?tex=+f_i%5E%7B%28k%29%7D%3Dr_i%5E%7B%28k%29%7D%2B%5Csum_%7Bj%3D1%7D%5E%7Bn%7D%28%5Cfrac%7Bp_%7Bij%7D%5E%7B%28k%29%7D%7D%7BU_j%5E%7B%28k%29%7D-x_j%7D%2B%5Cfrac%7Bq_%7Bij%7D%5E%7B%28k%29%7D%7D%7Bx_j-L_j%5E%7B%28k%29%7D%7D%29+

https://www.zhihu.com/equation?tex=+where~~~p_%7Bij%7D%5E%7B%28k%29%7D%3D%5Cbegin%7Bcases%7D+%28U_%7Bj%7D%5E%7B%28k%29%7D-x_j%5E%7B%28k%29%7D%29%5E2+%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7B%5Cpartial%7Bx_j%7D%7D%2C~~if%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7Bx_j%7D%3E0%5C+0%2C~~~~~~~~~~~~~~~~~~~~~~~~~~~~~if%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7Bx_j%7D%5Cle0+%5Cend%7Bcases%7D+

https://www.zhihu.com/equation?tex=+~~~~~~~~~~~~~~q_%7Bij%7D%5E%7B%28k%29%7D%3D%5Cbegin%7Bcases%7D+0%2C~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~if%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7Bx_j%7D%5Cgeq0%5C%5C+-%28x_%7Bj%7D%5E%7B%28k%29%7D-L_j%5E%7B%28k%29%7D%29%5E2+%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7B%5Cpartial%7Bx_j%7D%7D%2C~~if%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7Bx_j%7D%3C0%5C%5C+%5Cend%7Bcases%7D+

https://www.zhihu.com/equation?tex=+~~~~~~~~~~~~~~q_%7Bij%7D%5E%7B%28k%29%7D%3D%5Cbegin%7Bcases%7D%5Cnotag+0%2C~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~if%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7Bx_j%7D%5Cgeq0%5C%5C+-%28x_%7Bj%7D%5E%7B%28k%29%7D-L_j%5E%7B%28k%29%7D%29%5E2+%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7B%5Cpartial%7Bx_j%7D%7D%2C~~if%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7Bx_j%7D%3C0%5C%5C+%5Cend%7Bcases%7D+

https://www.zhihu.com/equation?tex=+r_i%5E%7B%28k%29%7D%3Df_i%28x%5E%7B%28k%29%7D%29-%5Csum_%7Bj%3D1%7D%5E%7Bn%7D%28%5Cfrac%7Bp_%7Bij%7D%5E%7B%28k%29%7D%7D%7BU_j%5E%7B%28k%29%7D-x_j%5E%7B%28k%29%7D%7D%2B%5Cfrac%7Bq_%7Bij%7D%5E%7B%28k%29%7D%7D%7Bx_j%5E%7B%28k%29%7D-L_j%5E%7B%28k%29%7D%7D%29%5Cnotag+
上式中所有的偏导数 https://www.zhihu.com/equation?tex=%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7B%5Cpartial%7Bx_j%7D%7D 均为 https://www.zhihu.com/equation?tex=x%3Dx%5E%7B%28k%29%7D 时取得。
可以看到当 https://www.zhihu.com/equation?tex=U%5Cto%7B%2B%5Cinfty%7D 时,有:

https://www.zhihu.com/equation?tex=+%5Cbegin%7Balign%7D+f_i%5E%7B%28k%29%7D%26%3Df_i%28x%5E%7B%28k%29%7D%29%2B%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7B%5Cpartial%7Bx%7D%7D%5Ccdot%7B%28x-x%5E%7Bk%7D%29%7D%5Ccdot%5Cfrac%7B%28U-x%5Ek%29%7D%7B%28U-x%29%7D%5C%5C+%26%3Df_i%28x%5E%7B%28k%29%7D%29%2B%5Cfrac%7B%5Cpartial%7Bf_i%7D%7D%7B%5Cpartial%7Bx%7D%7D%5Ccdot%7B%28x-x%5E%7Bk%7D%29%7D+%5Cend%7Balign%7D+
这显然就是一阶泰勒展开的公式。所以有很多文献都说MMA方法是一种基于一阶泰勒展开的序列凸近似。事实上,大部分的凸近似都借鉴过一阶泰勒展开的思想。
当 https://www.zhihu.com/equation?tex=L%5Cto%7B-%5Cinfty%7D 可以得到相似的结论。
但根据我们绝倒的高等数学的知识,一阶泰勒展开的代数精度很低,只有当近似函数的区间就在展开点附近时,一阶泰勒才近似成立,所以我们需要引入新的参数对这个展开的区间进行限制也就有了下面将要讲到的 https://www.zhihu.com/equation?tex=%5Calpha%E5%92%8C%5Cbeta ,只限制区间或许还不够,我们希望近似函数不完全是线性的,这样我们就可以更容易获得严格凸的特性,于是 https://www.zhihu.com/equation?tex=U%E5%92%8CL 就又起到了控制曲率的作用。
这样做的好处是每一个子问题,其约束条件:

https://www.zhihu.com/equation?tex=+%5Cfrac%7B%5Cpartial%5E2%7Bf_i%5E%7B%28k%29%7D%7D%7D%7B%5Cpartial%7Bx_j%5E%7B%282%29%7D%7D%7D%3D%5Cfrac%7B2p_%7Bij%5E%7B%28k%29%7D%7D%7D%7B%28U_j%5E%7B%28k%29%7D-x_j%29%5E3%7D%2B%5Cfrac%7B2q_%7Bij%7D%5E%7B%28k%29%7D%7D%7B%28x_j-L_j%5E%7B%28k%29%7D%29%5E%7B3%7D%7D+

https://www.zhihu.com/equation?tex=+%5Cfrac%7B%5Cpartial%5E2f_i%5E%7B%28k%29%7D%7D%7B%5Cpartial%7Bx_j%7D%5Cpartial%7Bx_i%7D%7D%3D0%2Cif~i%5Cneq%7Bj%7D+
又因为 https://www.zhihu.com/equation?tex=p_%7Bij%7D%5E%7B%28k%29%7D%5Cgeq0%2Cq_%7Bij%7D%5E%7B%28k%29%7D%5Cgeq0 ,是一个严格凸的函数,这样就完成了从非凸向凸的转化。
易得近似函数有一条渐近线 https://www.zhihu.com/equation?tex=x_j%3DU_j ,通过调整 https://www.zhihu.com/equation?tex=U_j 的值控制近似函数的边界和曲率。该种近似方法的本质是一阶泰勒展开,在 https://www.zhihu.com/equation?tex=x-%3Ex%5E%7B%28k%29%7D 更精确的取值范围,即下次一迭代结果 https://www.zhihu.com/equation?tex=x%5E%7B%28k%2B1%29%7D 不会离太远。
定义 https://www.zhihu.com/equation?tex=L_J%5E%7B%28k%29%7D%5Cle%5Calpha_%7Bj%7D%5E%7B%28k%29%7D%5Cleq%7Bx_j%7D%5E%7B%28k%29%7D%5Cleq%7B%5Cbeta_j%7D%5E%7B%28k%29%7D%5Cle%7BU_j%7D%5E%7B%28k%29%7D 其中 https://www.zhihu.com/equation?tex=%5Calpha_j%5E%7B%28k%29%7D%3D0.9%2AL_j%5E%7B%28k%29%7D%2B0.1%2Ax_j%5E%7B%28k%29%7D , https://www.zhihu.com/equation?tex=%5Cbeta_j%5E%7B%28k%29%7D%3D0.9%2AU_j%5E%7B%28k%29%7D%2B0.1%2Ax_j%5E%7B%28k%29%7D ,在实际计算过程中U和L一般是通过物理条件确定的定值,可以不随迭代发生变化。但是在复杂问题中,如果可行区间更小则收敛性更好。这里提供一种迭代方法:
在迭代的前两步可以取:

https://www.zhihu.com/equation?tex=+L%5E%7B%28k%29%7D_j%3Dx_j%5E%7B%28k%29%7D-%28x_jmax-x_jmin%29~~%5C%26~~U%5E%7B%28k%29%7D_j%3Dx_j%5E%7B%28k%29%7D%2B%28x_jmax-x_jmin%29+
当K https://www.zhihu.com/equation?tex=%5Cge K+1:

https://www.zhihu.com/equation?tex=+L%5E%7B%28k%29%7D_j%3Dx_j%5E%7B%28k%29%7D-s%28x_j%5E%7B%28k-1%29%7D-L_j%5E%7B%28k-1%29%7D%29~~%5C%26~~U%5E%7B%28k%29%7D_j%3Dx_j%5E%7B%28k%29%7D%2Bs%28x_j%5E%7B%28k-1%29%7D-U_j%5E%7B%28k-1%29%7D%29+
如此操作,我们可以得到一系列严格的凸子问题:

https://www.zhihu.com/equation?tex=+%5Cbegin%7Balign%7D%5Cnotag+min~~~~~~~~~~~~~~~~~%26%5Csum_%7Bj%3D1%7D%5E%7Bn%7D%28%5Cfrac%7Bp_0j%7D%7BU_j-x_j%7D-%5Cfrac%7Bq_0j%7D%7Bx_j-L_j%7D%29%2Br_0+%5C%5C+%26s.t.%5Csum_%7Bj%3D1%7D%5E%7Bn%7D%28%5Cfrac%7Bp_%7Bij%7D%7D%7BU_j-x_j%7D-%5Cfrac%7Bq_%7Bij%7D%7D%7Bx_j-L_j%7D%29%5Cle%7Bb_i%7D+%5C%5C+%26%5Calpha_%7Bj%7D%5Cleq%7Bx_j%7D%5Cleq%7B%5Cbeta_j%7D%5Cnotag+%5Cend%7Balign%7D+
求解 https://www.zhihu.com/equation?tex=p%5E%7B%28k%29%7D 可列拉格朗日函数:

https://www.zhihu.com/equation?tex=+l%28x%2Cy%29%3Df_0%5E%7B%28k%29%7D%28x%29%2B%5Csum_%7Bj%3D1%7D%5E%7Bn%7Dy_if_i%5E%7B%28k%29%7D%28x%29+
写成分量的形式:

https://www.zhihu.com/equation?tex=+l%28x%2Cy%29%3D%5Cfrac%7Bp_%7B0j%7D%2By%5ETp_j%7D%7BU_j-x_j%7D-%5Cfrac%7Bq_%7B0j%7D%2By%5ETq_j%7D%7Bx_j-L_j%7D+
其对偶函数 https://www.zhihu.com/equation?tex=W%28y%29 可以写为:

https://www.zhihu.com/equation?tex=+W%28y%29%3D%5Cunderset%7Bx%7D%7Bmin%7D%5C%7B%7BL%28x%2Cy%29%3B%5Calpha_%7Bj%7D%5Cleq%7Bx_j%7D%5Cleq%7B%5Cbeta_j%7D%7D%2Cfor~all~j%5C%7D+
根据凸优化>的相关知识,求原问题的极小值等价于求对偶问题的极大值。

可以转化为对偶问题:

https://www.zhihu.com/equation?tex=+%5Cbegin%7Balign%7D+%26max~~~~W%28y%29%3Dr_0-y%5ETb%2B%5Csum_%7Bj%3D1%7D%5E%7Bn%7D%28%5Cfrac%7Bp_%7B0j%7D%2By%5ETp_j%7D%7BU_j-x_j%7D-%5Cfrac%7Bq_%7B0j%7D%2By%5ETq_j%7D%7Bx_j-L_j%7D%29%5C%5C+%26s.t.~~~~~~~y%5Cgeq%7B0%7D%5Cnotag+%5Cend%7Balign%7D+
在第一次迭代的过程中如果起始点选择不当可能会出现子问题   不可行,即没有任何可行解。为了解决这一问题我们引入变量于是子问题可以写为:

https://www.zhihu.com/equation?tex=minimize~~~~~~~~~~%5Csum_%7Bj%3D1%7D%5E%7Bn%7D%28%5Cfrac%7Bp_%7B0j%7D%7D%7BU_j-x_j%7D-%5Cfrac%7Bq_%7B0j%7D%7D%7Bx_j-L_j%7D%29%2B%5Csum_%7Bi%3D1%7D%5E%7Bm%7D%28d_iz_i%2Bd_iz_i%5E2%29%2Br_0%5C

https://www.zhihu.com/equation?tex=+s.t.+~~~~~~~~~~~~~~~~~~~~%5Csum_%7Bj%3D1%7D%5E%7Bn%7D%28%5Cfrac%7Bp_%7Bij%7D%7D%7BU_j-x_j%7D-%5Cfrac%7Bq_%7Bij%7D%7D%7Bx_j-L_j%7D%29-z_i%5Cleq%7Bb_i%7D%2C~for~i%3D1%2C...%2Cm

https://www.zhihu.com/equation?tex=and~~~~~~~~~~~~~~~~~~~~%5Calpha_%7Bj%7D%5Cleq%7Bx_j%7D%5Cleq%7B%5Cbeta_j%7D%2C~for~j%3D1%2C...%2Cn~%2Cz_i%5Cge%7B0%7D%2C~for~i%3D1%2C...%2Cm
其中 https://www.zhihu.com/equation?tex=p_%7Bij%7D%5Cge0%2Cq_%7Bij%7D%5Cge0%2C+L_j%5Cle%5Calpha_%7Bj%7D%5Cleq%7B%5Cbeta_j%7D%5Cle%7BU_j%7D%2Cd_i%3E0 都应该是一个相对较大的数。很容易证明如果该系数足够大那么在子问题中自变量会自动变为0,另一方面,如果未修改的子问题不可行,则在中一些自变量应该是严格正的。在实践中不必太大,应为过大的会造成数值上的不稳定,可以取10或100。
结果展示

给定参数top3d(30,20,10,0.4,3,1.5),参数的意义分别为长、宽、高、网格尺度、罚函数、最小过滤半径。可以得到结果:




代码及注释

源代码来自kai liu,原文使用OC算法,作者完成了MMA算法的移植。
top3d
%本文给定top3d(30,20,10,0.4,3,1.5)
function top3d(nelx,nely,nelz,volfrac,penal,rmin)
% 定义循环参数
maxloop = 200;    % 最大迭代次数
tolx = 0.01;      % 终止条件(残差)
displayflag = 0;% 显示结构表示
% 材料的属性
E0 = 1;         % 固体区域的杨氏模量
Emin = 1e-9;      % 非固体区域的杨氏模量,尽可能小但是为了避免奇异性一般不取0
nu = 0.3;         % 泊松比
% USER-DEFINED LOAD DOFs
= meshgrid(nelx, 0, 0:nelz);               % Coordinates
loadnid = kl*(nelx+1)*(nely+1)+il*(nely+1)+(nely+1-jl); % Node IDs
loaddof = 3*loadnid(:) - 1;                           % DOFs
% USER-DEFINED SUPPORT FIXED DOFs
= meshgrid(0,0:nely,0:nelz);                  % Coordinates
fixednid = kf*(nelx+1)*(nely+1)+iif*(nely+1)+(nely+1-jf); % Node IDs
fixeddof = ; % DOFs
% 有限元分析程序
nele = nelx*nely*nelz;
ndof = 3*(nelx+1)*(nely+1)*(nelz+1);
F = sparse(loaddof,1,-1,ndof,1);
U = zeros(ndof,1);
freedofs = setdiff(1:ndof,fixeddof);
KE = lk_H8(nu);
nodegrd = reshape(1:(nely+1)*(nelx+1),nely+1,nelx+1);
nodeids = reshape(nodegrd(1:end-1,1:end-1),nely*nelx,1);
nodeidz = 0:(nely+1)*(nelx+1):(nelz-1)*(nely+1)*(nelx+1);
nodeids = repmat(nodeids,size(nodeidz))+repmat(nodeidz,size(nodeids));
edofVec = 3*nodeids(:)+1;
edofMat = repmat(edofVec,1,24)+ ...
    repmat( -3 -2 -1 ...
    3*(nely+1)*(nelx+1)+ -3 -2 -1]],nele,1);
iK = reshape(kron(edofMat,ones(24,1))',24*24*nele,1);
jK = reshape(kron(edofMat,ones(1,24))',24*24*nele,1);
% 过滤器
iH = ones(nele*(2*(ceil(rmin)-1)+1)^2,1);
jH = ones(size(iH));
sH = zeros(size(iH));
k = 0;
for k1 = 1:nelz
    for i1 = 1:nelx
      for j1 = 1:nely
            e1 = (k1-1)*nelx*nely + (i1-1)*nely+j1;
            for k2 = max(k1-(ceil(rmin)-1),1):min(k1+(ceil(rmin)-1),nelz)
                for i2 = max(i1-(ceil(rmin)-1),1):min(i1+(ceil(rmin)-1),nelx)
                  for j2 = max(j1-(ceil(rmin)-1),1):min(j1+(ceil(rmin)-1),nely)
                        e2 = (k2-1)*nelx*nely + (i2-1)*nely+j2;
                        k = k+1;
                        iH(k) = e1;
                        jH(k) = e2;
                        sH(k) = max(0,rmin-sqrt((i1-i2)^2+(j1-j2)^2+(k1-k2)^2));
                  end
                end
            end
      end
    end
end
H = sparse(iH,jH,sH);
Hs = sum(H,2);
% 迭代初始化
x = repmat(volfrac,);
xPhys = x;
loop = 0;
change = 1;
% 开始迭代
while change > tolx && loop < maxloop
    loop = loop+1;
    % 有限元分析
    sK = reshape(KE(:)*(Emin+xPhys(:)'.^penal*(E0-Emin)),24*24*nele,1);
    K = sparse(iK,jK,sK); K = (K+K')/2;
    U(freedofs,:) = K(freedofs,freedofs)\F(freedofs,:);
    % 定义目标函数与灵敏度分析
    ce = reshape(sum((U(edofMat)*KE).*U(edofMat),2),);
    c = sum(sum(sum((Emin+xPhys.^penal*(E0-Emin)).*ce)));
    dc = -penal*(E0-Emin)*xPhys.^(penal-1).*ce;
    dv = ones(nely,nelx,nelz);
    % 对灵敏度进行过滤与投影,需要注意的是,并不能数学上证明对灵敏度进行过滤的
    % 稳定性,如果出现不适定的情况可以尝试更改过滤为密度过滤
    dc(:) = H*(dc(:)./Hs);
    dv(:) = H*(dv(:)./Hs);
    % 移动渐近线法求解
    xval= x(:);
    f0val = c;
    df0dx = dc(:);
    fval= sum(xPhys(:))/(volfrac*nele) - 1;
    dfdx= dv(:)' / (volfrac*nele);
    = ...
    mmasub(m, n, loop, xval, xmin, xmax, xold1, xold2, ...
    f0val,df0dx,fval,dfdx,low,upp,a0,a,c_MMA,d);
    % 更新迭代变量
    xnew   = reshape(xmma,nely,nelx,nelz);
    xPhys(:) = (H*xnew(:))./Hs;
    xold2    = xold1(:);
    xold1    = x(:);
    change = max(abs(xnew(:)-x(:)));
    x = xnew;
    % 输出结果
    fprintf(' It.:%5i Obj.:%11.4f Vol.:%7.3f ch.:%7.3f\n',loop,c,mean(xPhys(:)),change);
    % 输出密度
    if displayflag, clf; display_3D(xPhys); end %#ok<UNRCH>
end
clf; display_3D(xPhys);
end


% === 生成单元刚度矩阵 ===
function = lk_H8(nu)
A = [32 6 -8 6 -6 4 3 -6 -10 3 -3 -3 -4 -8;
    -48 0 0 -24 24 0 0 0 12 -12 0 12 12 12];
k = 1/144*A'*;

K1 = [k(1) k(2) k(2) k(3) k(5) k(5);
    k(2) k(1) k(2) k(4) k(6) k(7);
    k(2) k(2) k(1) k(4) k(7) k(6);
    k(3) k(4) k(4) k(1) k(8) k(8);
    k(5) k(6) k(7) k(8) k(1) k(2);
    k(5) k(7) k(6) k(8) k(2) k(1)];
K2 = [k(9)k(8)k(12) k(6)k(4)k(7);
    k(8)k(9)k(12) k(5)k(3)k(5);
    k(10) k(10) k(13) k(7)k(4)k(6);
    k(6)k(5)k(11) k(9)k(2)k(10);
    k(4)k(3)k(5)k(2)k(9)k(12)
    k(11) k(4)k(6)k(12) k(10) k(13)];
K3 = [k(6)k(7)k(4)k(9)k(12) k(8);
    k(7)k(6)k(4)k(10) k(13) k(10);
    k(5)k(5)k(3)k(8)k(12) k(9);
    k(9)k(10) k(2)k(6)k(11) k(5);
    k(12) k(13) k(10) k(11) k(6)k(4);
    k(2)k(12) k(9)k(4)k(5)k(3)];
K4 = [k(14) k(11) k(11) k(13) k(10) k(10);
    k(11) k(14) k(11) k(12) k(9)k(8);
    k(11) k(11) k(14) k(12) k(8)k(9);
    k(13) k(12) k(12) k(14) k(7)k(7);
    k(10) k(9)k(8)k(7)k(14) k(11);
    k(10) k(8)k(9)k(7)k(11) k(14)];
K5 = [k(1) k(2)k(8)k(3) k(5)k(4);
    k(2) k(1)k(8)k(4) k(6)k(11);
    k(8) k(8)k(1)k(5) k(11) k(6);
    k(3) k(4)k(5)k(1) k(8)k(2);
    k(5) k(6)k(11) k(8) k(1)k(8);
    k(4) k(11) k(6)k(2) k(8)k(1)];
K6 = [k(14) k(11) k(7)k(13) k(10) k(12);
    k(11) k(14) k(7)k(12) k(9)k(2);
    k(7)k(7)k(14) k(10) k(2)k(9);
    k(13) k(12) k(10) k(14) k(7)k(11);
    k(10) k(9)k(2)k(7)k(14) k(7);
    k(12) k(2)k(9)k(11) k(7)k(14)];
KE = 1/((nu+1)*(1-2*nu))*...
    [ K1K2K3K4;
    K2'K5K6K3';
    K3' K6K5' K2';
    K4K3K2K1'];
end
% === 展示3D效果图 (ISO-VIEW) ===
function display_3D(rho)
= size(rho);
hx = 1; hy = 1; hz = 1;            % 定义效果图的单元大小
face = ;
set(gcf,'Name','ISO display','NumberTitle','off');
for k = 1:nelz
    z = (k-1)*hz;
    for i = 1:nelx
      x = (i-1)*hx;
      for j = 1:nely
            y = nely*hy - (j-1)*hy;
            if (rho(j,i,k) > 0.5)% 定义展示出的密度阈值
                vert = ;
                vert(:,) = vert(:,); vert(:,2,:) = -vert(:,2,:);
                patch('Faces',face,'Vertices',vert,'FaceColor',);
                hold on;
            end
      end
    end
end
axis equal; axis tight; axis off; box on; view(); pause(1e-6);
endmma
%-------------------------------------------------------
%    MMA求解程序 MMA.m
%    本代码将原本的非线性问题线性近似,非凸问题凸近似,得到一系列
%    线性的、凸的子问题,再通过subsolv.m迭代求解这些线性的、凸的子问题最终找到最优值点
function = ...
mmasub(m,n,iter,xval,xmin,xmax,xold1,xold2, ...
f0val,df0dx,fval,dfdx,low,upp,a0,a,c,d)
%    This function mmasub performs one MMA-iteration, aimed at
%    solving the nonlinear programming problem:
%    该函数进行MMA迭代,已解决符合以下形式的非线性(非凸)优化问题   
%      Minimizef_0(x) + a_0*z + sum( c_i*y_i + 0.5*d_i*(y_i)^2 )
%    subject tof_i(x) - a_i*z - y_i <= 0,i = 1,...,m
%                xmin_j <= x_j <= xmax_j,    j = 1,...,n
%                z >= 0,   y_i >= 0,         i = 1,...,m
%   输入参数有:
%   m    = 约束条件的数量
%   n    = 变量的个数
%iter= 迭代步数
%xval= 包含所有xj的向量
%xmin= 包含所有xj取值下限的向量
%xmax= 包含所有xj取值上限的向量
%xold1 = 上次迭代产生的x_val
%xold2 = 上上次迭代产生的的xval
%f0val = 目标函数的现有值
%df0dx = 包含目标函数对所有xj偏导数的列向量
%fval= 时约束函数fi值的列向量
%dfdx= x_j.fi对xj偏导数的矩阵
%low   = 前一次迭代的渐近线下界
%upp   = 前一次迭代的渐进线上界
%引入下面四个量防止在初始值选择不恰当的形况下出现子问题不适定(没有结)的情况
%a0    = a_0*z中的常系数a_0
%a   = 用来存储a_i*z中系数 a_i的列向量
%c   = 用来存储c_i*y_i中系数c_i的列向量
%d   =用来存储0.5*d_i*(y_i)^2中系数d_i的列向量,d_i应该是一个足够大的数,
%可以证明当d_i足够大时y_i趋近于0   
%输出的量:
%xmma= 子问题求解出的xj的列向量
%ymma= 子问题求解出的yi的列向量
%zmma= z的标量
%lam   = m个约束对应的拉格朗日乘子
%xsi   = n个α<x_j对应的拉格朗日乘子
%eta   = n个x_j<β对应的拉格朗日乘子
%   mu   = m个y_i<0对应的拉格朗日乘子。
%zet   = z>0对应的拉格朗日乘子
%   s    = m个约束条件对应的松弛因子
%low   = 本次求解的子问题对应的渐近线的下界
%upp   = 本次求解的子问题对应的渐近线上界
%
%epsimin = sqrt(m+n)*10^(-9);计算机精度的最小值
epsimin = 10^(-7);
raa0 = 0.00001;
move = 1.0;
albefa = 0.1;
asyinit = 0.5;
asyincr = 1.2;
asydecr = 0.7;
eeen = ones(n,1);
eeem = ones(m,1);
zeron = zeros(n,1);

% Calculation of the asymptotes low and upp :计算渐近线的上下界
if iter < 2.5
low = xval - asyinit*(xmax-xmin);
upp = xval + asyinit*(xmax-xmin);
else
zzz = (xval-xold1).*(xold1-xold2);
factor = eeen;
factor(find(zzz > 0)) = asyincr;
factor(find(zzz < 0)) = asydecr;
low = xval - factor.*(xold1 - low);
upp = xval + factor.*(upp - xold1);
lowmin = xval - 10*(xmax-xmin);
lowmax = xval - 0.01*(xmax-xmin);
uppmin = xval + 0.01*(xmax-xmin);
uppmax = xval + 10*(xmax-xmin);
low = max(low,lowmin);
low = min(low,lowmax);
upp = min(upp,uppmax);
upp = max(upp,uppmin);
end

% 计算设计变量的精确界限α和β

zzz1 = low + albefa*(xval-low);
zzz2 = xval - move*(xmax-xmin);
zzz= max(zzz1,zzz2);
alfa = max(zzz,xmin);
zzz1 = upp - albefa*(upp-xval);
zzz2 = xval + move*(xmax-xmin);
zzz= min(zzz1,zzz2);
beta = min(zzz,xmax);

% 计算 p0, q0, P, Q 和 b.

xmami = xmax-xmin;
xmamieps = 0.00001*eeen;
xmami = max(xmami,xmamieps);
xmamiinv = eeen./xmami;
ux1 = upp-xval;
ux2 = ux1.*ux1;
xl1 = xval-low;
xl2 = xl1.*xl1;
uxinv = eeen./ux1;
xlinv = eeen./xl1;
%
p0 = zeron;
q0 = zeron;
p0 = max(df0dx,0);
q0 = max(-df0dx,0);
%p0(find(df0dx > 0)) = df0dx(find(df0dx > 0));
%q0(find(df0dx < 0)) = -df0dx(find(df0dx < 0));
pq0 = 0.001*(p0 + q0) + raa0*xmamiinv;
p0 = p0 + pq0;
q0 = q0 + pq0;
p0 = p0.*ux2;
q0 = q0.*xl2;
%
P = sparse(m,n);
Q = sparse(m,n);
P = max(dfdx,0);
Q = max(-dfdx,0);
%P(find(dfdx > 0)) = dfdx(find(dfdx > 0));
%Q(find(dfdx < 0)) = -dfdx(find(dfdx < 0));
PQ = 0.001*(P + Q) + raa0*eeem*xmamiinv';
P = P + PQ;
Q = Q + PQ;
P = P * spdiags(ux2,0,n,n);
Q = Q * spdiags(xl2,0,n,n);
b = P*uxinv + Q*xlinv - fval ;
%
%%% 使用反演原对偶牛顿方法求解子问题
= ...
subsolv(m,n,epsimin,low,upp,alfa,beta,p0,q0,P,Q,a0,a,b,c,d);subsolve
%子问题求解程序 subsolv.m
%
function = ...
subsolv(m,n,epsimin,low,upp,alfa,beta,p0,q0,P,Q,a0,a,b,c,d);
%
% 使用该函数求解MMA生成的子问题可以写成:
%         
% minimize   SUM[ p0j/(uppj-xj) + q0j/(xj-lowj) ] + a0*z +
%          + SUM[ ci*yi + 0.5*di*(yi)^2 ],
%
% subject to SUM[ pij/(uppj-xj) + qij/(xj-lowj) ] - ai*z - yi <= bi,
%            alfaj <=xj <=betaj,yi >= 0,z >= 0.
% 然后使用拉格朗日的方法求解子问题的对偶问题      
% Input:m, n, low, upp, alfa, beta, p0, q0, P, Q, a0, a, b, c, d.
% Output: xmma,ymma,zmma, slack variables and Lagrange multiplers.
%
een = ones(n,1);
eem = ones(m,1);
epsi = 1;
epsvecn = epsi*een;
epsvecm = epsi*eem;
x = 0.5*(alfa+beta);
y = eem;
z = 1;
lam = eem;
xsi = een./(x-alfa);
xsi = max(xsi,een);
eta = een./(beta-x);
eta = max(eta,een);
mu= max(eem,0.5*c);
zet = 1;
s = eem;
itera = 0;

while epsi > epsimin
epsvecn = epsi*een;
epsvecm = epsi*eem;
ux1 = upp-x;
xl1 = x-low;
ux2 = ux1.*ux1;
xl2 = xl1.*xl1;
uxinv1 = een./ux1;
xlinv1 = een./xl1;

plam = p0 + P'*lam ;
qlam = q0 + Q'*lam ;
gvec = P*uxinv1 + Q*xlinv1;
dpsidx = plam./ux2 - qlam./xl2 ;

rex = dpsidx - xsi + eta;
rey = c + d.*y - mu - lam;
rez = a0 - zet - a'*lam;
relam = gvec - a*z - y + s - b;
rexsi = xsi.*(x-alfa) - epsvecn;
reeta = eta.*(beta-x) - epsvecn;
remu = mu.*y - epsvecm;
rezet = zet*z - epsi;
res = lam.*s - epsvecm;

residu1 = ';
residu2 = ';
residu = ';
residunorm = sqrt(residu'*residu);
residumax = max(abs(residu));

ittt = 0;
while residumax > 0.9*epsi & ittt < 100
    ittt=ittt + 1;
    itera=itera + 1;

    ux1 = upp-x;
    xl1 = x-low;
    ux2 = ux1.*ux1;
    xl2 = xl1.*xl1;
    ux3 = ux1.*ux2;
    xl3 = xl1.*xl2;
    uxinv1 = een./ux1;
    xlinv1 = een./xl1;
    uxinv2 = een./ux2;
    xlinv2 = een./xl2;
    plam = p0 + P'*lam ;
    qlam = q0 + Q'*lam ;
    gvec = P*uxinv1 + Q*xlinv1;
    GG = P*spdiags(uxinv2,0,n,n) - Q*spdiags(xlinv2,0,n,n);
    dpsidx = plam./ux2 - qlam./xl2 ;
    delx = dpsidx - epsvecn./(x-alfa) + epsvecn./(beta-x);
    dely = c + d.*y - lam - epsvecm./y;
    delz = a0 - a'*lam - epsi/z;
    dellam = gvec - a*z - y - b + epsvecm./lam;
    diagx = plam./ux3 + qlam./xl3;
    diagx = 2*diagx + xsi./(x-alfa) + eta./(beta-x);
    diagxinv = een./diagx;
    diagy = d + mu./y;
    diagyinv = eem./diagy;
    diaglam = s./lam;
    diaglamyi = diaglam+diagyinv;

    if m < n
      blam = dellam + dely./diagy - GG*(delx./diagx);
      bb = ';
      Alam = spdiags(diaglamyi,0,m,m) + GG*spdiags(diagxinv,0,n,n)*GG';
      AA = [Alam   a
            a'    -zet/z ];
      solut = AA\bb;
      dlam = solut(1:m);
      dz = solut(m+1);
      dx = -delx./diagx - (GG'*dlam)./diagx;
    else
      diaglamyiinv = eem./diaglamyi;
      dellamyi = dellam + dely./diagy;
      Axx = spdiags(diagx,0,n,n) + GG'*spdiags(diaglamyiinv,0,m,m)*GG;
      azz = zet/z + a'*(a./diaglamyi);
      axz = -GG'*(a./diaglamyi);
      bx = delx + GG'*(dellamyi./diaglamyi);
      bz= delz - a'*(dellamyi./diaglamyi);
      AA = [Axx   axz
            axz'azz ];
      bb = [-bx' -bz]';
      solut = AA\bb;
      dx= solut(1:n);
      dz = solut(n+1);
      dlam = (GG*dx)./diaglamyi - dz*(a./diaglamyi) + dellamyi./diaglamyi;
    end

    dy = -dely./diagy + dlam./diagy;
    dxsi = -xsi + epsvecn./(x-alfa) - (xsi.*dx)./(x-alfa);
    deta = -eta + epsvecn./(beta-x) + (eta.*dx)./(beta-x);
    dmu= -mu + epsvecm./y - (mu.*dy)./y;
    dzet = -zet + epsi/z - zet*dz/z;
    ds   = -s + epsvecm./lam - (s.*dlam)./lam;
    xx= [ y'zlam'xsi'eta'mu'zets']';
    dxx = ';

    stepxx = -1.01*dxx./xx;
    stmxx= max(stepxx);
    stepalfa = -1.01*dx./(x-alfa);
    stmalfa = max(stepalfa);
    stepbeta = 1.01*dx./(beta-x);
    stmbeta = max(stepbeta);
    stmalbe= max(stmalfa,stmbeta);
    stmalbexx = max(stmalbe,stmxx);
    stminv = max(stmalbexx,1);
    steg = 1/stminv;

    xold   =   x;
    yold   =   y;
    zold   =   z;
    lamold =lam;
    xsiold =xsi;
    etaold =eta;
    muold=mu;
    zetold =zet;
    sold   =   s;

    itto = 0;
    resinew = 2*residunorm;
    while resinew > residunorm & itto < 50
    itto = itto+1;

    x   =   xold + steg*dx;
    y   =   yold + steg*dy;
    z   =   zold + steg*dz;
    lam = lamold + steg*dlam;
    xsi = xsiold + steg*dxsi;
    eta = etaold + steg*deta;
    mu= muold+ steg*dmu;
    zet = zetold + steg*dzet;
    s   =   sold + steg*ds;
    ux1 = upp-x;
    xl1 = x-low;
    ux2 = ux1.*ux1;
    xl2 = xl1.*xl1;
    uxinv1 = een./ux1;
    xlinv1 = een./xl1;
    plam = p0 + P'*lam ;
    qlam = q0 + Q'*lam ;
    gvec = P*uxinv1 + Q*xlinv1;
    dpsidx = plam./ux2 - qlam./xl2 ;

    rex = dpsidx - xsi + eta;
    rey = c + d.*y - mu - lam;
    rez = a0 - zet - a'*lam;
    relam = gvec - a*z - y + s - b;
    rexsi = xsi.*(x-alfa) - epsvecn;
    reeta = eta.*(beta-x) - epsvecn;
    remu = mu.*y - epsvecm;
    rezet = zet*z - epsi;
    res = lam.*s - epsvecm;

    residu1 = ';
    residu2 = ';
    residu = ';
    resinew = sqrt(residu'*residu);
    steg = steg/2;
    end
residunorm=resinew;
residumax = max(abs(residu));
steg = 2*steg;
end
epsi = 0.1*epsi;
end

xmma   =   x;
ymma   =   y;
zmma   =   z;
lamma =lam;
xsimma =xsi;
etamma =eta;
mumma=mu;
zetmma =zet;
smma   =   s;参考文献

SVANBERG K. MMA and GCMMA – two methods for nonlinear optimization. : 15.
STEPHEN BOYD, LIEVEN VANDENBERGHE. 凸优化. 王书宁, 译, 许鋆, 译, 黄晓霖, 译. 清华大学出版社.
LIU K, TOVAR A. An efficient 3D topology optimization code written in Matlab. Structural and Multidisciplinary Optimization, 2014, 50(6): 1175–1196.

yukamu 发表于 2022-1-23 07:57

哇哦,写的太详细了,感谢分享,正卡在MMA调参这里
页: [1]
查看完整版本: MMA算法的推导及3D简支梁拓扑优化代码详解