# Topology Optimization Based Material Design for 3D Domains Using MATLAB
## 4 The MATLAB Code Implementation
### Model Order Reduction: Code Implementation  
In this section, the MATLAB implementation of the three Model Order Reduction techniques are presented. A detailed description of the simple material design implementation has already been presented in previous work by the authors [17]. In this work, the three classes presented in Ref. [17] are also used; however, some new methods are added in order to enable their efficient use not only at the topology optimization formulation (macro scale) but at the homogenization one (micro scale) as well. The features used for the implementation of the Approximate Reanalysis technique are expanded and compared to the original variant presented in Ref. [17]. Specifically, the re-initialization of the reduced basis vectors in the now updated dynamic takes into consideration the change of the value of the objective function as well as the design variables. Thus, an extra method is now used in the Approximate Reanalysis class updateBase, which is implemented as follows:  
```
#functionupdateBase(obj,c,x)
#obj.save
#obj.save=false;
#obj.cfull=c;
#obj.xfull=x;
#elseifabs((obj.cfull-c)*100/obj.cfull)>obj.rtol||...
#(dot(obj.xfull(:),x(:)))/(norm(obj.xfull(:))*norm(x(:)))>obj.xtol
#obj.update=true;
#end
```  
This method performs two tasks; the first one is to check if a full dimensionality finite element analysis was performed by the solve method. If a full dimensionality one was performed, then the value of the objective function \(c\) and the design variables \(x\) are accordingly stored in the class variables \(cfull\) and \(xfull\). In order to check if a full dimensionality analysis was performed, a class variable called _save_ is utilized, which is set to _true_ after the solution of the full dimensionality finite element analysis as follows:  
```
1 if(obj.loop==1)||(mod(obj.loop,obj.rf)==0)||obj.update
2==true
3U=obj.fea(K,F);
4 obj.fi(:,1)=U;
5 obj.K0=K;
6 obj.save=true;
7 obj.update=false;
8
9 else
```  
The second task of the method is activated only when a full dimensionality finite element analysis has not been performed by the solve method. In such a case, the change on the value of the objective function as well as the design variables of the current iteration are validated using two new class variables \(rtol\) and \(xtol\) for the objective and design variables, respectively. If either condition is validated then an indicator class variable _update_ is set to true. This variable is then checked inside the solve method to determine the re-initialization of the reduced basis vectors. Thus in the definition of the ar class two additional input variables are added: one for the tolerance of the change in the objective function and one for the design variables, as follows:  
```
1 obj=ar(nb,tol,rtol,xtol,rf)In order to integrate the MOR techniques into the optimization procedure, the following changes should be applied. First, two additional input variables \(p1\) and \(p2\) are added to the UCOpt3D function, as follows:
```
functionUCOpt3D(lx,ly,lz,nelx,nely,nelz,nlx,nly,nlz,volfrac, penal,rmin,p1,p2)
```
where \(p1\) defines the MOR model used in the finite element analysis on the macro scale and \(p2\) denotes which MOR model is used inside the homogenization method, i.e., on the micro scale. Next _Line_ 87 of the UCOpt3D function is replaced with the following _Line_, applying MOR for performing the finite element analysis part:
```
U(fredofs,:)=p1.solve(K(fredofs,fredofs),F(fredofs,:));
```
In addition, after the calculation of the objective function, the update of Approximate Reanalysis approach is carried out. In order to ensure that the updateBase method is accessed, an if statement is used, as follows:
```
ifismethod(p1,'updateBase') p1.updateBase(c,x);
```
Regarding the implementation of the MOR model in the homogenization formulation, two extra methods should be added to the MOR classes. These methods are called sameSize and reinitialize.