% This function writes the grid point values to a vtk
% file to generate the contour plots to be viewed by
% using Paraview. This function and its slightly
% modified versions are used throughout the book
% for visualization and animation of the results.
% Although, Matlab and Octave have impressive
% graphic routines; however, Paraview has more
% powerful features for visualization and spatial
% and temporal analysis of the data.

% If more than one dataset need to be written to
% the file:
% 1. These data sets should be included as input
% to the function in line 1, i.e., data1, data2,
% data3. . ...etc.).
% 2. The lines 44 through 54 should be repeated for
% each new data set. The title in line 44, should
% also be changed representing the content of
% the data set. For example if next dataset is for
% order parameters something like ‘SCALARS
% ORP float 1\n’.

function [ ]= write_vtk_grid_values(nx,ny,dx,dy,istep,data1)

format long

%-- open output file

fname=sprintf('time_%d.vtk',istep);
out =fopen(fname,'w');

nz=1;

npoin =nx*ny*nz;

% start writing ASCII VTK file:

% header of VTK file

fprintf(out,'# vtk DataFile Version 2.0\n');
fprintf(out,'time_10.vtk\n');
fprintf(out,'ASCII\n');
fprintf(out,'DATASET STRUCTURED_GRID\n’');

%--- coords of grid points:
fprintf(out,'DIMENSIONS %5d %5d%5d\n',nx,ny,nz);

fprintf(out,'POINTS %7d float\n',npoin);

for i = 1:nx
for j = 1:ny

x =(i-1)*dx;
y =(j-1)*dy;
z = 0.0;

fprintf(out, '%14.6e %14.6e%14.6e\n',x,y,z);

end
end

%--- write grid point values:

fprintf(out,'POINT_DATA %5d\n',npoin);

fprintf(out,'SCALARS CONfloat 1\n');

fprintf(out,'fLOOKUP_TABLEdefault\n');

for i = 1:nx
for j = 1:ny
ii=(i-1)*nx+j;

fprintf(out,'%14.6e\n',data1(i,j));
end
end
fclose(out);

end %endfunction