nX=40; % number of grid pts along x, y and z
nY=40;
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/2;

r = 10; % sphere radius

for x = 1:nX % loop over all three dimensions
   for y = 1:nY
      for z = 1:nZ

      if (((x-xc)**2 + (y-yc)**2 + (z-zc)**2) < r*r) m1 = 1;
      else m1 = 0;
      endif
      m2 = 1 - m1; % ensure that m1 + m2 = 1 for all x,y,z
  
      fprintf(mapFid, '%f   %f\n', m1, m2); % write map file

      endfor
   endfor
endfor

fclose(mapFid);

