% Script for DA Challenges output writing (example of the First DA challenge dataset)

% Mesh extent and number of points in each direction

Xlin = linspace(-50,50,251);
Ylin = linspace(-25,25,126);
Zlin = linspace(0.01,30.01,76);

% 3D mesh: first index describes variation in X, second in Y, third in Z

[Y,X,Z] = meshgrid(Ylin,Xlin,Zlin);

VX = zeros(size(X));
VY = zeros(size(X));
VZ = zeros(size(X));
dVXdX = zeros(size(X));
dVXdY = zeros(size(X));
dVXdZ = zeros(size(X));
dVYdX = zeros(size(X));
dVYdY = zeros(size(X));
dVYdZ = zeros(size(X));
dVZdX = zeros(size(X));
dVZdY = zeros(size(X));
dVZdZ = zeros(size(X));
p = zeros(size(X));

%If not in that format already, write/interpolate your results into the 3D numpy arrays for velocity, velocity gradient and pressure, etc.
%VX = ...
%VY = ...
%...
%p = ...

% Pressure normalization: in the First DA challenge, this is with respect to (X,Y,Z)=(0,0.2,0.01)

p = p-p(126,64,1);

% Output file naming

algoStr = 'myAlgo'
%In case you upload two results obtained with two different algorithms, then use different team names, e.g. myAlgo_1 and myAlgo_2
myPpp = 0.16

resultFile = sprintf('%s_OutputDA_ppp_0_%d_0025.h5',algoStr,round(myPpp*1000))

% At file writing, convert the 3D arrays to a 1D linear array with ordering as requested in the instructions.
% 
% Note that VXout = VX(:) is equivalent to e.g.
% 
% ii=1
% for k= 1:length(Zlin)
%     for j =1:length(Ylin)
%         for k =1:length(Xlin)
%             VXout(ii) = VX(i,j,k)
%             ii = ii+1
%          end
%     end
% end

h5create(resultFile,'/VX',length(VX(:)));
h5write(resultFile,'/VX',VX(:));
h5create(resultFile,'/VY',length(VY(:)));
h5write(resultFile,'/VY',VY(:));
h5create(resultFile,'/VZ',length(VZ(:)));
h5write(resultFile,'/VZ',VZ(:));

h5create(resultFile,'/dVXdX',length(dVXdX(:)));
h5write(resultFile,'/dVXdX',dVXdX(:));
h5create(resultFile,'/dVYdX',length(dVYdX(:)));
h5write(resultFile,'/dVYdX',dVYdX(:));
h5create(resultFile,'/dVZdX',length(dVZdX(:)));
h5write(resultFile,'/dVZdX',dVZdX(:));

h5create(resultFile,'/dVXdY',length(dVXdY(:)));
h5write(resultFile,'/dVXdY',dVXdY(:));
h5create(resultFile,'/dVYdY',length(dVYdY(:)));
h5write(resultFile,'/dVYdY',dVYdY(:));
h5create(resultFile,'/dVZdY',length(dVZdY(:)));
h5write(resultFile,'/dVZdY',dVZdY(:));

h5create(resultFile,'/dVXdZ',length(dVXdZ(:)));
h5write(resultFile,'/dVXdZ',dVXdZ(:));
h5create(resultFile,'/dVYdZ',length(dVYdZ(:)));
h5write(resultFile,'/dVYdZ',dVYdZ(:));
h5create(resultFile,'/dVZdZ',length(dVZdZ(:)));
h5write(resultFile,'/dVZdZ',dVZdZ(:));

h5create(resultFile,'/p',length(p(:)));
h5write(resultFile,'/p',p(:));


% display contents of created file
h5disp(resultFile);

