# Multi-Scale Concurrent Topology Optimization Based on BESO, Implemented in MATLAB
## 5 MATLAB Implementation
### MATLAB Implementation for 2D Design Space  
The UCOpt MATLAB function developed for the optimal material design using TO [32] was used as the basis for the multi-scale concurrent MATLAB code presented in the current section. Thus, only the parts of the MATLAB code modified will be highlighted. The name of the new MATLAB function for solving the multi-scale concurrent TO problem formulation is set as ConcTopOptBESO. The first set of modifications is focused on the input and output parameters. The volume target variable \(volfrac\) of the original UCOpt function is changed to take into account the two different volume constraints applied to the optimization problem. Thus, \(volfrac\) is modified into \(volfrac\) and \(volfy\) as the target volume fractions of the macro and micro-scales, respectively. Similarly, the filtering radius variable \(rmin\) is modified into two different variables named \(rxmin\) and \(rymin\). The first variable is utilized for the filtering of the sensitivities of the macro-scale whereas the second variable is utilized for the filtering of the sensitivities of the micro-scale. Parameter \(ft\) is due to the filtering application on only the sensitivity number, with no density filtering available. An additional variable is added to the input parameters of the ConcTopOptBESO MATLAB function called _er._ As described in [30], the _er_ variable is the evolutionary rate of the two target volumes. Regarding the output parameters, the final geometries of the macro and micro-scales as well as the homogenized elasticity matrix are added in the form of the variables \(x,y\) and \(CH\), respectively. Hence, the final form of the input function is formulated following Listing 1. ```
function[c,CH,x,y]=ConCTopOptBESO(lx,ly,nelx,nely,nlx,nly, volfx,volfy,penal,rxmin,rymin,er,p)
```  
At the beginning of the new function, in addition to the initialization of the variables \(E0\), \(Emin\) and \(nu\), an extra variable \(xmin\) is initialized for the later implementation of the expression of Equation (3). The value of \(xmin\) is set as a small number, in this case \(1e-8\). In the case of the multi-scale concurrent TO, the topology optimization is performed in both the macro and micro-scales. Thus, two different sets of material densities are implemented, one for the macro and one for the micro-scale. To distinguish between the two sets, a different variable is assigned to each set. The variable \(x\) is set for the macro-scale material densities and the variable \(y\) is set for micro-scale material densities. The MATLAB function UCOpt was formulated to perform the optimal material design using TO in the micro-scale. Hence, to formulate the two different sets of material densities, all instances of the variable \(x\) in the original UCOpt function are modified to \(y\). Moving to the preparation of the filtering matrices and the initialization sections, two different filtering matrices in the form of \(Hx\), \(Hsx\) and \(Hy\), \(Hsy\) are computed for the macro and micro-scale, respectively. The procedure for computing both sets is identical, with the only difference in the filtering radius considered for each scale (\(rxmin\) for the macro and \(rymin\) for the micro-scale, respectively). In the initialization section, in addition to the initialization of the \(y\) variable, the \(x\) variable is initialized following Listing 2. ```
1x=ones(nely,nelx);
```  
For the initialization of matrix \(y\), in the original UCOpt function and rectangle mesh was set with a hole at its center. An Additional, initialization can include the set of ones to all \(y\) values. Furthermore, two new variables are implemented called \(volx\) and \(voly\), and initialized at one. Similarly to the code provided in [30], variables \(volx\) and \(voly\) represent the current volume fraction target of each iteration for the macro and micro-scale, respectively. Moving inside the optimization procedure, an extra step is added at the beginning of the optimization iteration.