nX=120; % number of grid pts along x, y and z
nY=120;
nZ=40;

mapFid = fopen('map.dat', 'w+'); % file in which material map will be written

xc = nX/2; % center of sphere
yc = nY/2;
zc = nZ/10;

characteristics
%r = 30; % cylinder base radius: 1 grid pt = 1 nm
%l = 12; % cylinder height: 2 grid pts = 1 nm

maxSb = 0.2;
maxP  = 0.15;

map = zeros(nX*nY*nZ,4);
i=0;
for x = 1:nX % loop over all three dimensions
   for y = 1:nY
      for z = 1:nZ
      i=i+1;
      if (
          (z >= zc) &&
          (z <= zc+l) &&
          ((x-xc)**2 + (y-yc)**2 < (r-(r*(z-zc)/l))**2)
          ) 
          m1 = maxSb*(1+z-zc)/l;  % InAsSb % max at top, zero at bottom
          if ((m1 > maxSb) || (m1 < 0)) m1 = 0; endif  % safety just in case we need more InAs somewhere or so
          m2 = maxP*(l-(z-zc))/l;  % InAsP % max at bottom, zero at top
          if ((m2 > maxP) || (m2 < 0)) m2 = 0; endif  % safety just in case we need more InAs somewhere or so
          m3 = 1-m1-m2; % InAs
          m4 = 0; % Vacuum
      
      elseif (z<zc) m3 = 1; m1 = 0; m2 = 0; m4 = 0;
      else
      m4 = 1; m1 = 0; m2 = 0; m3 = 0;
      endif
      map(i,1) = m1;
      map(i,2) = m2;
      map(i,3) = m3;
      map(i,4) = m4;
     
  
%      fprintf(mapFid, '%f   %f   %f   %f\n', m1, m2, m3, m4); % write map file

      endfor
   endfor
endfor

softmap=zeros(nX*nY*nZ,4);

delta = 0.1;
nSteps = 10;
for n = 1:nSteps
i=0;
   for x = 1:nX
      xp=x+1; if (xp > nX) xp = 1; endif
      xm=x-1; if (xm < 1) xm = nX; endif
      for y = 1:nY
         yp=y+1; if (yp > nY) yp = 1; endif
         ym=y-1; if (ym < 1) ym = nY; endif
         for z = 1:nZ
            zp=z+1; if (zp > nZ) zp = 1; endif
            zm=z-1; if (zm < 1) zm = nZ; endif
 
            pos =(z + (y-1)*nZ + (x-1)*nZ*nY);
            posXM = (z + (y-1)*nZ + (xm-1)*nZ*nY);  
            posXP = (z + (y-1)*nZ + (xp-1)*nZ*nY);  
            posYM = (z + (ym-1)*nZ + (x-1)*nZ*nY);  
            posYP = (z + (yp-1)*nZ + (x-1)*nZ*nY);  
            posZM = (zm + (y-1)*nZ + (x-1)*nZ*nY);  
            posZP = (zp + (y-1)*nZ + (x-1)*nZ*nY);  
            m1S = ((1-delta) * map(pos,1) + (delta/6) * (map(posXM,1)) + (delta/6) * (map(posXP,1)) + (delta/6) * (map(posYM,1)) + (delta/6) * (map(posYP,1)) + (delta/6) * (map(posZM,1)) + (delta/6) * (map(posZP,1)));
            m2S = ((1-delta) * map(pos,2) + (delta/6) * (map(posXM,2)) + (delta/6) * (map(posXP,2)) + (delta/6) * (map(posYM,2)) + (delta/6) * (map(posYP,2)) + (delta/6) * (map(posZM,2)) + (delta/6) * (map(posZP,2)));
            m3S = ((1-delta) * map(pos,3) + (delta/6) * (map(posXM,3)) + (delta/6) * (map(posXP,3)) + (delta/6) * (map(posYM,3)) + (delta/6) * (map(posYP,3)) + (delta/6) * (map(posZM,3)) + (delta/6) * (map(posZP,3)));
            m4S = ((1-delta) * map(pos,4) + (delta/6) * (map(posXM,4)) + (delta/6) * (map(posXP,4)) + (delta/6) * (map(posYM,4)) + (delta/6) * (map(posYP,4)) + (delta/6) * (map(posZM,4)) + (delta/6) * (map(posZP,4)));
            softmap(pos,1) = m1S;
            softmap(pos,2) = m2S;
            softmap(pos,3) = m3S;
            softmap(pos,4) = m4S;

         endfor
      endfor
   endfor
map=softmap;
endfor

% output
for i = 1:nX*nY*nZ
   m1 = map(i,1);
   m2 = map(i,2);
   m3 = map(i,3);
   m4 = map(i,4);
   fprintf(mapFid, '%f   %f   %f   %f\n', m1, m2, m3, m4); % write map file
endfor

fclose(mapFid);


ecutFid = fopen('ecut.sx', 'w+'); % file in which material map will be written
ecut = 0.999 * (pi / (0.5 * 18.897))**2/4 % maximum cut off energy:
					% 0.5 * 18.897 = step width in bohr radii
fprintf(ecutFid, 'Ecut = %f; // hartree \n', ecut); % write maximum cut off energy to file ecut.sx
fclose(ecutFid);

