# Topology Optimization Based Material Design for 3D Domains Using MATLAB
## 4 The MATLAB Code Implementation
### Model Order Reduction: Code Implementation
It is worth mentioning that in order to proceed with 3D printing of the micro structures derived through the optimization procedure and then assembling them to form the macro structure, the continuity of the unit cells when assembled to form the macro structure should be considered during the optimization phase. ### 3D Cantilever Test Example

The parameters used for the formulation of the 3D cantilever test example are as follows: a structured finite element mesh of \(120\times 40\times 8\) along the the abscissa, ordinate, and applicate directions was used for the discretization of the macro domain, and a \(20\times 20\times 20\) structured grid along the three directions for the discretization of the micro domain. In addition, the target volume was set equal to 50% of the design domain (i.e., the micro one), the penalization factor was set equal to 3, and no filter application was set for the unit cell. Regarding the MOR models, for all three models the size of the reduced basis was set to 5 for the macro domain and to 3 for the micro domain, while the tolerance was set equal to 0.01 for both scales. For the approximate reanalysis technique, the frequency of the reduced basis update was set to 8 for the macro domain and 3 for the homogenization, and the tolerance for the compliance change and density change was set to 30 and 1.50, respectively. The script for the implementation of the POD approach with the UCOpt3D function is presented below:
```
p1=pod(5,0.01);%Forthemacrodomain p2=pod(3,0.01);%forthemicrodomain UCOpt3D(10,10,10,120,40,8,20,20,20,0.5,3,0,p1,p2);
```
For the implementation of the on-the-fly approach, _Lines_ 1 and 2 of the above script have to be replaced, as follows:
```
p1=onthefly(5,0.01);%Forthemacrodomain p2=onthefly(3,0.01);%forthemicrodomain
```
whereas for the implementation of the approximate reanalysis approach, _Lines_ 1 and 2 of the above script have to be replaced, as follows:
```
p1 ar=ar(8,0.01,30,1.50,5);%Forthemacrodomain p2 ar=ar(8,0.01,30,1.50,3);%Forthemicrodomain
```
It is worth noticing that, apart from the size of the reduced basis, the remaining parameters of the MOR techniques were the same for both scales. For the implementation of the 3D cantilever test example, the loading and boundary conditions need to be provided into the UCOpt3D function. The settings for the loading and boundary conditions are the same as those presented in Ref. [3]. Therefore, for the loading conditions, _Lines_ 10 to 13 of the UCOpt3D function are set as follows:

Figure 1: The macro domains of the three considered test examples. (**a**) 3D Cantilever Beam test example. (**b**) 3D wheel test example. (**c**) 3D MBB beam test example. * % USER-DEFINED LOAD DOFs
* 1 il = nelx; j1 = 0; kl = 0:nelz; % Coordinates
* 12 loadind = kl*(nelx+1)*(nely+1)+i1*(nely+1)+(nely+1-j1); % Node IDs
* 1; % DOFs

whereas for the boundary conditions, _Lines_ 14 to 17 of the UCOpt3D function are set as follows:

* % USER-DEFINED SUPPORT FIXED DOFs
* 15[jf,kf] = meshgrid(1:nely+1,1:nelz+1); % Coordinates
* 16 fixedind = (kf-1)*(nely+1)*(nelx+1)+jf; % Node IDs
* 17 fixedof = [3*fixedind(:); 3*fixedind(:)-1; 3*fixedind(:)-2]; % DOFs

The results obtained from the three MOR techniques used for solving the topology optimization and homogenization formulations of the material design problem are presented in Table 1. The first column of Table 1 refers to the utilized MOR approach, the second column refers to the total number of optimization iterations (both full and reduced ones), the third column refers to the number of full optimization iterations, and the fourth column refers to the compliance of the resulting structure.