# Topology Optimization Based Material Design for 3D Domains Using MATLAB
## 4 The MATLAB Code Implementation
### Model Order Reduction: Code Implementation
Their purpose is to reinitialize the MOR model if the active degrees of freedom of the finite element analysis in the homogenization formulation change. The change of the degrees of freedom may occur because the MOR model stays the same during the optimization procedure but the active degrees of freedom are set in each call for the homogenization method depending of the material densities values, using the following _Line_:
```
activedofs=edof(E>Emin,:);activedofs=sort(unique( activedofs(:)));
```
The first method takes as input the size of active degrees of freedom of the finite element analysis and returns a true or false indicator by checking the size of the reduced basis vectors. The implementation is simple, as presented below:
```
functionflag=sameSize(obj,sz) flag=size(obj.fi,1)==sz;
```
where \(sz\) is the number of active degrees of freedom and \(fi\) is the reduced basis matrix of the MOR model. The second method called reinitialize basically resets the MOR model without the need to recreate the model class. For the case of the Approximate Reanalysis model, this is achieved as follows:
```
functionp=reinitialize(obj) p=ar(obj.Nb,obj.tol,obj.rtol,obj.xtol,obj.rf);
```
The re-initialization of each MOR class is different depending on each approach parameter. For the POD approach, the implementation of the reinitialize method is as follows:functionp=reinitialize(obj)P=pod(obj.Nb,obj.tol);

end

whereas for the on-the-fly approach, the implementation of the reinitialize method is as follows:

functionp=reinitialize(obj)P=onthelfly(obj.Nb,obj.tol);

end

To apply the MOR model in the homogenization method, an additional input variable \(p\) is added to the homo3 DY function, as follows:

Then, _Lines_ 59 to 66 of the original homo3D are replaced with the following:

ifp.sameSize(size(F(activedofs(4:end),1),1))==falseP=p.reinitialize();

end

fori=1:6X(activedofs(4:end),i)=p.solve(K(activedofs(4:end),...

activedofs(4:end)),F(activedofs(4:end),i));

end

The \(if\) statement of the code above is utilized to check if the number of the active degrees of freedom on the current homogenization procedure is the same with the size of the reduced bases vectors of the MOR model. If the numbers does not match, a re-initialization of the MOR model is then performed using the reinitialize method. Then the MOR model is utilized to solve the finite element analysis for each of the six cases required by the homogenization function. ## 5 Test Examples

In this section, three test examples are presented for demonstrating the use of the proposed MATLAB code. In all test examples the three MOR models are applied and the resulting computational reduction achieved is presented. The parameters chosen for the three MOR models were chosen arbitrarily. A sensitivity analysis study performed by the authors will be presented soon [23], where the influence of the various parameters of the MOR models is examined. The three examples were chosen from the publication of the original top3D MATLAB code [3]. Although, the three examples considered to present the capabilities of the code for the material design correspond to rather simple macro domain geometries, more complex geometries for the macro domain can be dealt with as well by the code presented herein--with slight modifications. The first example refers to the classical 3D cantilever beam problem, the second example refers to the 3D wheel problem, and the third example refers to the MBB beam. The macro domains for three test examples are presented in Figure 1. For all test examples, the final form of the unit cell as well as the number of iterations required (denoted as full optimization loops in the tables) and the number of full dimensionality finite element analyses performed are provided (denoted as full optimization loops in the tables). The objective function value is presented for all three MOR approaches and without the use of any MOR model. In the second and third test examples the resulting topology of the micro scale is converted into a STL format using Ref. [24].