# Make Movies - Top3d

If you want to make a topology optimization movie, simply follow this tutorial

In some designs, some elements may be desired to be solid or void (active or passive). A $\texttt{nely} \times \texttt{nelx}\times \texttt{nelz}$ matrix with ones at elements desired to be solid, and a $\texttt{nely}\times \texttt{nelx}\times \texttt{nelz}$ matrix with zeros at elements desired to be void can be added to the program.

### Step.1: Save iteration history

Add the following lines after line 94

% SAVE HISTORY
save(['xPhys_it',num2str(loop,'%03d'),'.mat'], 'xPhys');


It should looks like this:

% PLOT DENSITIES
if displayflag, clf; display_3D(xPhys); end %#ok<UNRCH>
% SAVE HISTORY
save(['xPhys_it',num2str(loop,'%03d'),'.mat'], 'xPhys');


### Step.2: Save the doMovie.m under the save directory as top3d.m

function doMovie
% Make a topology optimizatin movie
% By K. Liu

% cutoffL allow smooth animation from 1 to 50 iterations.
% change 0.5 to the desired element density cutoff value
% change smaller 50 to allow faster animation, larger 50 to allow slower animation
cutoffL = linspace(0,0.5,50);

% Do Movie
i = 0;
while (1)
i = i + 1;
filename = ['xPhys_it',num2str(i,'%03d'),'.mat'];
if exist(filename,'file') == 0
break;
end
if i <= length(cutoffL)
cutoff = cutoffL(i);
else
cutoff = max(cutoffL);
end

plot_3d(xPhys, cutoff, 1, [0.7 0.7 0.7]);
drawnow
mov(i) = getframe(gcf); %#ok<AGROW>
end

movie2avi(mov, 'myTopopt.avi'); %#ok<MOVIE2>
end

% DISPLAY 3D TOPOLOGY (ISO-VIEW)
% Special thanks to Tomas Zegard (UIUC) for quick plot function
% Code snippets from Top3d (top3dapp.com)
% Top3d - An efficient 3D topology optimization program written in MATLAB
% By K. Liu and A. Tovar
% http://www.top3dapp.com
function [p] = plot_3d(x, cutoff, Alpha, fcolor)

if nargin < 4, fcolor = 'r'; end

[nely,nelx,nelz] = size(x);
aux = zeros(nely+2,nelx+2,nelz+2);
aux(2:end-1,2:end-1,2:end-1) = x;

cla, hold on, view(30,30), rotate3d on, axis equal, axis([0 nelx 0 nelz 0 nely]), box
set(gca,'YDir','reverse','ZDir','reverse','ZtickLabel',flipud(get(gca,'Ztick')'));
[X,Y,Z] = meshgrid(0:nelx+1,0:nely+1,0:nelz+1);
p = patch(isosurface(X-0.5,Z-0.5,Y-0.5,aux,cutoff),...
'FaceColor',fcolor,'EdgeColor','none','FaceAlpha',Alpha);
camlight, lighting gouraud;
axis off; box off; set(gcf, 'color', [1 1 1])
drawnow
end


### Step.3: Run the program

After running the topology optimization, run the doMovie program

doMovie