clear -x dres icase ihet isp rt selopt
%clear all
%clear figure
close all

%icase=8;
%ihet=2;
%isp=1;
%rt=3;
%buck='';
buck='BK0';
%selopt='SELE_MWG';

sph='';
%sph='_noSPH';
%sph='_SPH';
%sph='_SPH_D';
%sph='_SEL';
%FileNameEDI = ['AEDI_'  num2str(icase)] ;
FileNameEDI = ['AEDI'  num2str(icase) buck] ;
%FileNameEDI = ['AEDI_sph_'  num2str(icase)] ;
FileNameMAP = ['AmapH_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph] ;
FileNameTRK = ['ATRK_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt)] ;
FileNameFLU = ['AFLU_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph] ;
FileNameTRKP = ['ATRK_hpin'] ;
FileNameFLUP = ['AFLUP_' num2str(icase) sph] ;
FileNameMACP = ['AMACP_' num2str(icase) sph] ;
%pathDonjon  = ['/Users/suricat/Documents/Work/Sources/Version5_ev694/Donjon/Darwin_x86_64/'];
%pathDragon  = ['/Users/suricat/Documents/Work/Sources/Version5_ev708/Dragon/Darwin_x86_64/'];
%pathDonjon  = ['/Users/suricat/Version5a/Donjon/Darwin_x86_64/_res_SEL/'];
%pathDonjon  = ['/Users/suricat/Version5a/Donjon/Darwin_x86_64/_res_SEL_gaussCPO_gauss/'];
%pathDonjon  = ['/Users/suricat/Version5a/Donjon/Darwin_x86_64/_res_SEL_gauss/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900cluster_data_SELE_GAP/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900cluster_data_SELE_FD/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900cluster_data_EDF/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900cluster_data_new_' selopt '/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900cluster_data_new2_' selopt '/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900clusph_data_new2_' selopt '/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900clusph_data_new_' selopt '/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900clusph_data_nw_' selopt '/'];
pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900clusphA_data_nw_' selopt '/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900cluster_data_EDF_123/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900cluster_data_EDF_112/'];
%pathDonjon  = ['/Users/suricat/Version5e/Donjon/Darwin_x86_64/rep900cluster_data_EDF_nointp/'];
%pathDragon  = ['/Users/suricat/Version5b/Dragon/Darwin_x86_64/'];
%pathDragon  = ['/Users/suricat/Version5e/Dragon/Darwin_x86_64/'];
pathDragon  = ['/Users/suricat/Version5e/Dragon/Darwin_x86_64/clusterBK0/'];


savefig=1;
ext='eps';
makefig=1;
%savepath = [ selopt '_new_fig/' ];
%savepath = [ selopt '_new2_fig/' ];
%savepath = [ selopt '_nwsph_fig/' ];
savepath = [ selopt '_nwsphA_fig/' ];
%savepath = "";
%savepath = "selfig/";
%savepath = "selgaussCPOfig/";
%savepath = "EDF_fig/";
%savepath = "EDF_123_fig/";
%savepath = "EDF_112_fig/";
%savepath = "EDF_nointp_fig/";
%savepath = "SELE_GAP_fig/";
%savepath = "SELE_FD_fig/";

%%%%%%%
ng=2;
npin=17;

FileNameEDI = [pathDragon FileNameEDI];
FileNameMAP = [pathDonjon FileNameMAP];
FileNameTRK = [pathDonjon FileNameTRK];
FileNameFLU = [pathDonjon FileNameFLU];
FileNameTRKP = [pathDonjon FileNameTRKP];
FileNameFLUP = [pathDonjon FileNameFLUP];
FileNameMACP = [pathDonjon FileNameMACP];
npin=17;
Pnorm=10000;

%%%%%%%
lp=1.26;
gp=0.04;

meshsplit=ones(4*3*isp+1,4*3*isp+1);
xsp(1)=0;
id=1;
if(ihet==1) 
 for i=1:3
  for j=1:isp
   id=id+1
   xsp(id)=(gp+lp)/isp+xsp(id-1);
  end
  for j=1:isp*2
   id=id+1;
   xsp(id)=lp*15/isp/2+xsp(id-1);
  end
  for j=1:isp
   id=id+1;
   xsp(id)=(gp+lp)/isp+xsp(id-1);
  end
 end
elseif(ihet==0)
 xsp=0:(4*3*isp);
 xsp=xsp/(4*3*isp)*(3*(2*gp+17*lp));
end
%%%%%%%

holes=ones(npin,npin);
holes(9,9)=0;
holes(6,6)=0;
holes(6,9)=0;
holes(6,12)=0;
holes(9,12)=0;
holes(12,12)=0;
holes(12,9)=0;
holes(12,6)=0;
holes(9,6)=0;
holes(4,4)=0;
holes(4,14)=0;
holes(14,14)=0;
holes(14,4)=0;
holes(3,6)=0;
holes(3,9)=0;
holes(3,12)=0;
holes(6,3)=0;
holes(9,3)=0;
holes(12,3)=0;
holes(15,6)=0;
holes(15,9)=0;
holes(15,12)=0;
holes(6,15)=0;
holes(9,15)=0;
holes(12,15)=0;
holes(18:34,1:17)=holes(1:17,1:17);
holes(35:51,1:17)=holes(1:17,1:17);
holes(:,18:34)=holes(:,1:17);
holes(:,35:51)=holes(:,1:17);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Rebuild 3x3 cluster from transport calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
np2=npin+2;
mapEDI=zeros(3*np2,3*np2);
Dxedi=ones(3*np2,1)*lp;
for i=1:3
  Dxedi((i-1)*np2+1)=gp;
  Dxedi(i*np2)=gp;
end
Dxmap=ones(3*npin,1)*lp;
for i=1:3
  Dxmap((i-1)*npin+1)=Dxmap((i-1)*npin+1)+gp;
  Dxmap(i*npin)=Dxmap(i*npin)+gp;
end
Vol=Dxmap*Dxmap';

ID=0;
IDline=(3*np2+1)/2;
for i=1:(3*np2+1)/2
  for j=1:IDline
    ID=ID+1;
    mapEDI((3*np2+1)/2+(i-1),(3*np2+1)/2+(j-1)+(i-1))=ID;
    mapEDI((3*np2+1)/2-(i-1),(3*np2+1)/2+(j-1)+(i-1))=ID;
    mapEDI((3*np2+1)/2+(i-1),(3*np2+1)/2-(j-1)-(i-1))=ID;
    mapEDI((3*np2+1)/2-(i-1),(3*np2+1)/2-(j-1)-(i-1))=ID;
    mapEDI((3*np2+1)/2+(j-1)+(i-1),(3*np2+1)/2+(i-1))=ID;
    mapEDI((3*np2+1)/2-(j-1)-(i-1),(3*np2+1)/2+(i-1))=ID;
    mapEDI((3*np2+1)/2+(j-1)+(i-1),(3*np2+1)/2-(i-1))=ID;
    mapEDI((3*np2+1)/2-(j-1)-(i-1),(3*np2+1)/2-(i-1))=ID;
  end
  IDline=IDline-1;
end
mapEDI;



disp(['open file : ' FileNameEDI ]);
[pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd] = ASCIIOpnv4(FileNameEDI);
%disp('Data Structure Content')
%ASCIILibv4(pfin,ilvl,RecordPos,RecordNumb,RecordName);

[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'CELLBYCELL  ',1);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'MACROLIB    ',1);
VLe(:,1) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'VOLUME      ');
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'GROUP       ',1);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000001',1);
HFe(:,1) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'H-FACTOR    ');
FLe(:,1) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'FLUX-INTG   ');
SFe(:,1) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'NFTOT       ');
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000001',2);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000002',1);
HFe(:,2) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'H-FACTOR    ');
FLe(:,2) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'FLUX-INTG   ');
SFe(:,2) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'NFTOT       ');
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000002',2);
fclose(pfin);


SFe;
HFmapEDI=zeros(3*np2,3*np2);
SFmapEDI=zeros(3*np2,3*np2);
FLmapEDI=zeros(3*np2,3*np2);
VLmapEDI=zeros(3*np2,3*np2);

for i=1:3*np2
for j=1:3*np2
%Dxedi;
%for i=1:3*2
%for j=1:3*2
  VLmapEDI(i,j)=VLmapEDI(i,j)+VLe(mapEDI(i,j));
  Vedi(i,j)=Dxedi(i)*Dxedi(j);
  for g=1:ng
    HFmapEDI(i,j)=HFmapEDI(i,j)+HFe(mapEDI(i,j),g)*FLe(mapEDI(i,j),g)/VLmapEDI(i,j)*Dxedi(i)*Dxedi(j);
    SFmapEDI(i,j)=SFmapEDI(i,j)+SFe(mapEDI(i,j),g)*FLe(mapEDI(i,j),g)/VLmapEDI(i,j)*Dxedi(i)*Dxedi(j);
    FLmapEDI(i,j)=FLmapEDI(i,j)+FLe(mapEDI(i,j),g)/VLmapEDI(i,j)*Dxedi(i)*Dxedi(j);
  end
end
end


%VLmapEDI./Vedi
%hc=HFmapEDI(1,1)
%hl=HFmapEDI(1,2)
%hu=HFmapEDI(2,2)

%Add water gap to pin (npin+2 -> npin)
for i=1:3
HFmapEDI((i-1)*np2+2,:)=HFmapEDI((i-1)*np2+1,:)+HFmapEDI((i-1)*np2+2,:);
HFmapEDI(i*np2-1,:)=HFmapEDI(i*np2,:)+HFmapEDI(i*np2-1,:);
HFmapEDI((i-1)*np2+1,:)=0;
HFmapEDI(i*np2,:)=0;

SFmapEDI((i-1)*np2+2,:)=SFmapEDI((i-1)*np2+1,:)+SFmapEDI((i-1)*np2+2,:);
SFmapEDI(i*np2-1,:)=SFmapEDI(i*np2,:)+SFmapEDI(i*np2-1,:);
SFmapEDI((i-1)*np2+1,:)=0;
SFmapEDI(i*np2,:)=0;

FLmapEDI((i-1)*np2+2,:)=FLmapEDI((i-1)*np2+1,:)+FLmapEDI((i-1)*np2+2,:);
FLmapEDI(i*np2-1,:)=FLmapEDI(i*np2,:)+FLmapEDI(i*np2-1,:);
FLmapEDI((i-1)*np2+1,:)=0;
FLmapEDI(i*np2,:)=0;
end

for i=1:3
HFmapEDI(:,(i-1)*np2+2)=HFmapEDI(:,(i-1)*np2+1)+HFmapEDI(:,(i-1)*np2+2);
HFmapEDI(:,i*np2-1)=HFmapEDI(:,i*np2)+HFmapEDI(:,i*np2-1);
HFmapEDI(:,(i-1)*np2+1)=0;
HFmapEDI(:,i*np2)=0;

SFmapEDI(:,(i-1)*np2+2)=SFmapEDI(:,(i-1)*np2+1)+SFmapEDI(:,(i-1)*np2+2);
SFmapEDI(:,i*np2-1)=SFmapEDI(:,i*np2)+SFmapEDI(:,i*np2-1);
SFmapEDI(:,(i-1)*np2+1)=0;
SFmapEDI(:,i*np2)=0;

FLmapEDI(:,(i-1)*np2+2)=FLmapEDI(:,(i-1)*np2+1)+FLmapEDI(:,(i-1)*np2+2);
FLmapEDI(:,i*np2-1)=FLmapEDI(:,i*np2)+FLmapEDI(:,i*np2-1);
FLmapEDI(:,(i-1)*np2+1)=0;
FLmapEDI(:,i*np2)=0;
end

%SFmapEDI

%Normalize Flux in cluster for Ptot=Pnorm
Ptot=0;
for i=1:3*np2
for j=1:3*np2
  Ptot=Ptot+HFmapEDI(i,j);
end
end

HFmapEDI=HFmapEDI/Ptot*Pnorm;
SFmapEDI=SFmapEDI/Ptot*Pnorm;
FLmapEDI=FLmapEDI/Ptot*Pnorm;

for i=1:3
for j=1:3
  
  HFmapT(((i-1)*npin+1):(i*npin),((j-1)*npin+1):(j*npin))=HFmapEDI(((i-1)*np2+2):(i*np2-1),((j-1)*np2+2):(j*np2-1));
  SFmapT(((i-1)*npin+1):(i*npin),((j-1)*npin+1):(j*npin))=SFmapEDI(((i-1)*np2+2):(i*np2-1),((j-1)*np2+2):(j*np2-1));
  FLmapT(((i-1)*npin+1):(i*npin),((j-1)*npin+1):(j*npin))=FLmapEDI(((i-1)*np2+2):(i*np2-1),((j-1)*np2+2):(j*np2-1));
end
end

%%%%%diviser flux par le volume
FLmapT=FLmapT./Vol;

HFmapT(:,3*npin+1)=HFmapT(:,3*npin);
HFmapT(3*npin+1,:)=HFmapT(3*npin,:);
SFmapT(:,3*npin+1)=SFmapT(:,3*npin);
SFmapT(3*npin+1,:)=SFmapT(3*npin,:);
FLmapT(:,3*npin+1)=FLmapT(:,3*npin);
FLmapT(3*npin+1,:)=FLmapT(3*npin,:);



if(makefig==1) % figure begin

figure(1)
surf(HFmapT)
view(0,90)
title(['HFmapT ' num2str(icase)])
colorbar("EastOutside")
caxis([2 5])
axis([26 52 26 52])
if(savefig==1)
  saveas(gcf,[savepath 'HFmapT_' num2str(icase)],ext);
end
%figure(2)
%surf(SFmapT)
%view(0,90)
%title('SFmapT')
%colorbar("EastOutside")
%caxis([6E10 16E10])

figure(2)
surf(FLmapT)
view(0,90)
title(['FlmapT ' num2str(icase)])
colorbar("EastOutside")
axis([26 52 26 52])
%caxis([6E10 16E10])
if(savefig==1)
  saveas(gcf,[savepath 'FlmapT_' num2str(icase)],ext);
end

end  % figure end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Rebuild 3x3 cluster from diffusion calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear pfin ilvl RecordPos RecordNumb RecordName CurRecInd


if(ihet<4)
% from heterogeneous / homogeneous diffusion calculations


disp(['open file : ' FileNameMAP ]);
[pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd] = ASCIIOpnv4(FileNameMAP);
%disp('Data Structure Content')
%ASCIILibv4(pfin,ilvl,RecordPos,RecordNumb,RecordName);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'ASSEMBLY    ',1);
HFm=zeros(npin**2,9);
SFm=zeros(npin**2,9);
for i=1:9
  recname=strcat('elt#0000000',num2str(i));
  [pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,recname,1);
  tmp = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'PIN-POWER   ');
%  size(tmp)
  HFm(:,i) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'PIN-POWER   ');
  SFm(:,i) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'SIG_F*PHI   ');
  Flm(:,i) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'FLUX        ');
  [pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,recname,2);
end

fclose(pfin);


HFmapD=zeros(3*npin,3*npin);
SFmapD=zeros(3*npin,3*npin);
FlmapD=zeros(3*npin,3*npin,2);
iass=1;
for i=1:3
for j=1:3  
  ipin=1;
  for ip=1:npin
  for jp=1:npin 
  HFmapD((i-1)*npin+ip,(j-1)*npin+jp)=HFm(ipin,iass);%*Dxmap((i-1)*npin+ip)*Dxmap((j-1)*npin+jp);
  SFmapD((i-1)*npin+ip,(j-1)*npin+jp)=SFm(ipin,iass);%*Dxmap((i-1)*npin+ip)*Dxmap((j-1)*npin+jp);
  FlmapD((i-1)*npin+ip,(j-1)*npin+jp,1)=Flm(ipin,iass);
  FlmapD((i-1)*npin+ip,(j-1)*npin+jp,2)=Flm(ipin+npin*npin,iass);
  ipin=ipin+1;
  end
  end
  iass=iass+1;
end
end


%Normalize Flux in cluster for Ptot=Pnorm
Ptot=0;
for i=1:3*npin
for j=1:3*npin
  Ptot=Ptot+HFmapD(i,j);
end
end

HFmapD=HFmapD/Ptot*Pnorm;
SFmapD=SFmapD/Ptot*Pnorm;

HFmapD(:,3*npin+1)=HFmapD(:,3*npin);
HFmapD(3*npin+1,:)=HFmapD(3*npin,:);
SFmapD(:,3*npin+1)=SFmapD(:,3*npin);
SFmapD(3*npin+1,:)=SFmapD(3*npin,:);
FlmapD(:,3*npin+1,1)=FlmapD(:,3*npin,1);
FlmapD(3*npin+1,:,1)=FlmapD(3*npin,:,1);
FlmapD(:,3*npin+1,2)=FlmapD(:,3*npin,2);
FlmapD(3*npin+1,:,2)=FlmapD(3*npin,:,2);

if(makefig==1) % figure begin

figure(3)
surf(HFmapD)
view(0,90)
title(['HFmapD ' num2str(icase) ' h' num2str(ihet) ' s' num2str(isp) ' t' num2str(rt) sph])
colorbar("EastOutside")
caxis([2 5])
axis([26 52 26 52])
if(savefig==1)
  saveas(gcf,[savepath 'HFmapD_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph],ext);
end

%figure(4)
%surf(SFmapD)
%view(0,90)
%title('SFmapD')
%colorbar("EastOutside")
%caxis([6E10 16E10])

end  % figure end


for i=1:3*npin
for j=1:3*npin
  if(SFmapT(i,j)==0)
    SFmapT(i,j)=1E-20;
    SFmapD(i,j)=0;
    HFmapT(i,j)=1E-20;
    HFmapD(i,j)=1E-20;
  end
end
end
SFdiff=(SFmapD-SFmapT)./SFmapT*100;
HFdiff=(HFmapD-HFmapT)./HFmapT*100;
%HFdiff=(HFmapD-HFmapT);

k=0;
drms=0;
dmin=0;
dmax=0;
for j=1:3*npin
for i=1:3*npin
  if(holes(i,j)==1)
    k=k+1;
    HFdifflin(k)=HFdiff(i,j);
    drms=drms+HFdifflin(k)^2;
  else
    HFdiff(i,j)=-20;
  end
end
end
dmin=min(HFdifflin)
dmax=max(HFdifflin)
dstd=std(HFdifflin)
drms=(drms/length(HFdifflin))^0.5

Xmap(1)=0;
for i=1:3*npin
  Xmap(i+1)=Xmap(i)+Dxmap(i);
end


if(makefig==1) % figure begin

figure(5)
%surf(Xmap,Xmap,HFdiff)
surf(HFdiff)
hold on
%for i=1:(4*3*isp+1)
% plot3([xsp(i) xsp(i)],[0 xsp(4*3*isp+1)],[dmax dmax],'-k');
% plot3([0 xsp(4*3*isp+1)],[xsp(i) xsp(i)],[dmax dmax],'-k');
%end
view(0,90)
title(['HFdiff ' num2str(icase) ' h' num2str(ihet) ' s' num2str(isp) ' t' num2str(rt) sph])
%mesh(xsp,xsp,meshsplit*dmax,'LineWidth',15,'LineStyle','--')
caxis([dmin-2 dmax])
%caxis([-7 5])
%caxis([-6 7])
colorbar("EastOutside")
axis([25.99 52 25.99 52])
%axis([Xmap((3*npin+1)/2)-0.001 Xmap(3*npin+1) Xmap((3*npin+1)/2)-0.001 Xmap(3*npin+1)])
if(savefig==1)
%  saveas(gcf,[savepath 'HFdiff_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph],ext);
  saveas(gcf,[savepath 'HFdiff_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph '_uscale'],ext);
end

%figure(6)
%mesh(xsp,xsp,meshsplit,'LineWidth',15,'LineStyle','--')
%view(0,90)

figure(7)
X=[26 52];
Y0=[26 26];
Y1=[52 52];
Y2=[35 35];
plot(X,Y0,'-b',X,Y1,'-b',X,Y2,'-b',Y0,X,'-b',Y1,X,'-b',Y2,X,'-b')
for i=(3*npin+1)/2:3*npin
for j=(3*npin+1)/2:3*npin
  if(holes(i,j)==1)
     if (abs(HFdiff(i,j))<0.5 )
      col=[1 1 1];
    elseif (abs(HFdiff(i,j))<=1)
      col=[0 1 1];
    elseif (abs(HFdiff(i,j))<=2)
      col=[1 1 0];
    elseif (abs(HFdiff(i,j))<=5) 
      col=[0.5 0.25 0.25];
    else 
      col=[1 0 0];
    end
   text(i+0.1,j+0.5,num2str(HFdiff(i,j),'%3.1f '),...
         'FontSize',6,...
 	       'BackgroundColor',col);
  end
end
end
axis([26 52 26 52])
if(savefig==1)
  saveas(gcf,[savepath 'HFdifftab_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph],ext);
end

end  % figure end


resname=[savepath 'res' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph '.tex'];
fresul=fopen(resname,'w');
if(isp==1)
 splitstr='4x4*';
elseif(isp==2)
 splitstr='8x8*';
end
captionA=['\\caption{Relative error between reconstruction and transport calculations for case ' num2str(icase) '(\\%%), het' num2str(ihet) ' - split ' splitstr ' - DUAL ' num2str(rt) ...
  ' 3}\\label{tabRes_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph '} \n']

fprintf(fresul,'\\begin{figure}[htbp] \\begin{center} \n',' ');
fprintf(fresul,captionA);
fprintf(fresul,'\\begin{tiny} \n',' ');
fprintf(fresul,'\\vspace{0.5cm} \n');
fprintf(fresul,'\\begin{tabular}{|*{9}{@{}m{0.038\\textwidth}@{}}|*{17}{@{}m{0.038\\textwidth}@{}}|c} \n\\cline{1-26} \n',' ');
for j=3*npin:-1:(3*npin+1)/2
for i=(3*npin+1)/2:3*npin
  if(holes(i,j)==1)
    if (abs(HFdiff(i,j))<0.5 )
      fprintf(fresul,' %3.1f   &  ' ,HFdiff(i,j));
    elseif (abs(HFdiff(i,j))<=1)
      fprintf(fresul,'\\textcolor{SkyBlue}{ %3.1f  } &  ' ,HFdiff(i,j));
    elseif (abs(HFdiff(i,j))<=2)
      fprintf(fresul,'\\textcolor{GreenYellow} {%3.1f }  &  ' ,HFdiff(i,j));
    elseif (abs(HFdiff(i,j))<=5) 
      fprintf(fresul,'\\textcolor{Dandelion}{ %3.1f }  &  ' ,HFdiff(i,j));
    else 
      fprintf(fresul,'\\textcolor{Red}{%3.1f  } &  ' ,HFdiff(i,j));
    end
  else
%  fprintf(fresul,' \\cellcolor{Gray} &  ',' ' );
  fprintf(fresul,'  &  ',' ' );
  end
end
fprintf(fresul,' \\\\ \n',' ');
if(j==35)
  fprintf(fresul,' \\cline{1-26} \n',' ');
end
end
fprintf(fresul,'\\cline{1-26} \n');
fprintf(fresul,'\\end{tabular} \n');
fprintf(fresul,'\\end{tiny} \n',' ');
fprintf(fresul,'\\end{center} \\end{figure} \n\n');
fclose(fresul)


 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Rebuild flux 3x3 cluster from diffusion calculations
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear pfin ilvl RecordPos RecordNumb RecordName CurRecInd

disp(['open file : ' FileNameTRK ]);
[pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd] = ASCIIOpnv4(FileNameTRK);
%disp('Data Structure Content')
keyflx = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'KEYFLX      ');
xx     = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'XX          ');
% we suppose that XX=YY in this computations
fclose(pfin);

clear pfin ilvl RecordPos RecordNumb RecordName CurRecInd

disp(['open file : ' FileNameFLU ]);
[pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd] = ASCIIOpnv4(FileNameFLU);
%ASCIILibv4(pfin,ilvl,RecordPos,RecordNumb,RecordName);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'FLUX        ',1);
flup(:,1)   = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000001');
flup(:,2)   = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000002');
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'            ',0);
fclose(pfin);

nx=(length(xx))^0.5;

for g=1:2
id=1;
for j=1:nx
for i=1:nx
  fluh(i,j,g)=flup(keyflx(i+(j-1)*nx),g);
  id=id+1;
end
end
end
fluh(nx+1,:,:)=fluh(nx,:,:);
fluh(:,nx+1,:)=fluh(:,nx,:);


dxhet(1)=0;
for i=1:nx
  dxhet(i+1)=dxhet(i)+xx(i);
end
%splf=1;
splf=isp;
%dxhet

if(makefig==1) % figure begin

%figure(8)
%subplot(2,2,1)
%surf(dxhet,dxhet,fluh(:,:,1))
%view(0,90)
%colorbar("NorthOutside")
%title('Group 1');
%axis([dxhet(3*4*splf/2+1) dxhet(3*4*splf+1) dxhet(3*4*splf/2+1) dxhet(3*4*splf+1)])
%
%subplot(2,2,3)
%surf(dxhet,dxhet,fluh(:,:,2))
%view(0,90)
%colorbar("NorthOutside")
%title('Group 2');
%axis([dxhet(3*4*splf/2+1) dxhet(3*4*splf+1) dxhet(3*4*splf/2+1) dxhet(3*4*splf+1)])
%
%subplot(1,2,2)
%surf(dxhet,dxhet,fluh(:,:,1)+fluh(:,:,2))
%view(0,90)
%colorbar("NorthOutside")
%title('Group all');
%axis([dxhet(3*4*splf/2+1) dxhet(3*4*splf+1) dxhet(3*4*splf/2+1) dxhet(3*4*splf+1)])
%
%if(savefig==1)
%  saveas(gcf,[savepath 'Flhet_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph],ext);
%end


Xmap(1)=0;
for i=1:3*npin
  Xmap(i+1)=Xmap(i)+Dxmap(i);
end
%figure(9)
%subplot(2,2,1)
%surf(Xmap((3*npin+1)/2:(3*npin+1)),Xmap((3*npin+1)/2:(3*npin+1)),FlmapD(((3*npin+1)/2:(3*npin+1)),((3*npin+1)/2:(3*npin+1)),1))
%view(0,90)
%colorbar("NorthOutside")
%title('Group 1');
%axis([Xmap((3*npin+1)/2) Xmap(3*npin+1) Xmap((3*npin+1)/2) Xmap(3*npin+1)])
%
%subplot(2,2,3)
%surf(Xmap((3*npin+1)/2:(3*npin+1)),Xmap((3*npin+1)/2:(3*npin+1)),FlmapD(((3*npin+1)/2:(3*npin+1)),((3*npin+1)/2:(3*npin+1)),2))
%view(0,90)
%colorbar("NorthOutside")
%title('Group 2');
%axis([Xmap((3*npin+1)/2) Xmap(3*npin+1) Xmap((3*npin+1)/2) Xmap(3*npin+1)])
%
%subplot(1,2,2)
%surf(Xmap((3*npin+1)/2:(3*npin+1)),Xmap((3*npin+1)/2:(3*npin+1)),(FlmapD(((3*npin+1)/2:(3*npin+1)),((3*npin+1)/2:(3*npin+1)),1)+FlmapD(((3*npin+1)/2:(3*npin+1)),((3*npin+1)/2:(3*npin+1)),2)))
%view(0,90)
%colorbar("NorthOutside")
%title('Group all');
%axis([Xmap((3*npin+1)/2) Xmap(3*npin+1) Xmap((3*npin+1)/2) Xmap(3*npin+1)])
%
%if(savefig==1)
%  saveas(gcf,[savepath 'Flpin_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph],ext);
%end


figure(10)

subplot(1,2,1)
surf(dxhet,dxhet,fluh(:,:,1)+fluh(:,:,2))
view(0,90)
colorbar("NorthOutside")
title('Heterogeneous');
axis([dxhet(3*4*splf/2+1)-0.001 dxhet(3*4*splf+1) dxhet(3*4*splf/2+1)-0.001 dxhet(3*4*splf+1)])

subplot(1,2,2)
%surf(Xmap((3*npin+1)/2:(3*npin+1)),Xmap((3*npin+1)/2:(3*npin+1)),(FlmapD(((3*npin+1)/2:(3*npin+1)),((3*npin+1)/2:(3*npin+1)),1)+FlmapD(((3*npin+1)/2:(3*npin+1)),((3*npin+1)/2:(3*npin+1)),2)))
surf(Xmap,Xmap,(FlmapD(:,:,1)+FlmapD(:,:,2)))
view(0,90)
colorbar("NorthOutside")
title('Pin by Pin');
axis([Xmap((3*npin+1)/2)-0.001 Xmap(3*npin+1) Xmap((3*npin+1)/2)-0.001 Xmap(3*npin+1)])

if(savefig==1)
  saveas(gcf,[savepath 'Flhetpin_' num2str(icase) '_h' num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph],ext);
end

end  % figure end



else
% from pin by pin diffusion calculations

disp(['open file : ' FileNameTRKP ]);
[pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd] = ASCIIOpnv4(FileNameTRKP);
%disp('Data Structure Content')
keyflx = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'KEYFLX      ');
xx     = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'XX          ');
% we suppose that XX=YY in this computations
fclose(pfin);

clear pfin ilvl RecordPos RecordNumb RecordName CurRecInd

disp(['open file : ' FileNameFLUP ]);
[pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd] = ASCIIOpnv4(FileNameFLUP);
%ASCIILibv4(pfin,ilvl,RecordPos,RecordNumb,RecordName);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'FLUX        ',1);
flup(:,1)   = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000001');
flup(:,2)   = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000002');
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'            ',0);
fclose(pfin);

nx=(length(xx))^0.5;


disp(['open file : ' FileNameMACP ]);
[pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd] = ASCIIOpnv4(FileNameMACP);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'GROUP       ',1);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000001',1);
HFp(:,1) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'H-FACTOR    ');
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000001',2);
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000002',1);
HFp(:,2) = ASCIIGetv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'H-FACTOR    ');
[pfin,ilvl,nbdata,CurRecInd] = ASCIISixv4(pfin,ilvl,RecordPos,RecordNumb,RecordName,CurRecInd,'elt#00000002',2);
fclose(pfin);

HFmapD=zeros(3*npin,3*npin);
for g=1:ng
id=0;
for j=1:3*npin
for i=1:3*npin
id=id+1;
HFmapD(i,j)=HFmapD(i,j)+HFp(id,g)*flup(keyflx(id),g)*xx(i)*xx(j);
FLmapD(i,j)=flup(keyflx(id),g)*xx(i)*xx(j);
end
end
end

%Normalize Flux in cluster for Ptot=Pnorm
Ptot=0;
for i=1:3*npin
for j=1:3*npin
  Ptot=Ptot+HFmapD(i,j);
end
end

HFmapD=HFmapD/Ptot*Pnorm;
FLmapD=FLmapD/Ptot*Pnorm;

for i=1:3*npin
for j=1:3*npin
  if(holes(i,j)==0)
    HFmapT(i,j)=1E-20;
    HFmapD(i,j)=0;
  end
end
end

HFmapD(:,3*npin+1)=HFmapD(:,3*npin);
HFmapD(3*npin+1,:)=HFmapD(3*npin,:);
HFdiff=(HFmapD-HFmapT)./HFmapT*100;

k=0;
drms=0;
for j=1:3*npin
for i=1:3*npin
  if(holes(i,j)==1)
    k=k+1;
    HFdifflin(k)=HFdiff(i,j);
    drms=drms+HFdifflin(k)^2;
  else
    HFdiff(i,j)=-20;
  end
end
end
dmin=min(HFdifflin)
dmax=max(HFdifflin)
dstd=std(HFdifflin)
drms=(drms/length(HFdifflin))^0.5

if(makefig==1) % figure begin

figure(3)
surf(HFmapD)
view(0,90)
title(['HFmapD ' num2str(icase) ' hpin'])
colorbar("EastOutside")
caxis([2 5])
axis([26 52 26 52])
if(savefig==1)
  saveas(gcf,[savepath 'HFmapD_' num2str(icase) '_hpin'],ext);
end

figure(5)
surf(HFdiff)
caxis([dmin-2 dmax])
view(0,90)
title(['HFdiff ' num2str(icase) ' hpin'])
colorbar("EastOutside")
axis([26 52 26 52])
if(savefig==1)
  saveas(gcf,[savepath 'HFdiff_' num2str(icase) '_hpin'],ext);
end


%Fldiff=(FlmapD-FlmapT)./FlmapT*100;
%figure(11)
%surf(Fldiff)
%view(0,90)
%title([savepath 'Fldiff ' num2str(icase) ' h0'])


end  % figure end


resname=[savepath 'respin' num2str(icase) '.tex'];
fresul=fopen(resname,'w');
captionA=['\\caption{Relative error between reconstruction and transport calculations for case ' num2str(icase) '(\\%%), Pin-by-pin}\\label{tabRes_h' ...
  num2str(ihet) '_s' num2str(isp) '_t' num2str(rt) sph '} \n']

fprintf(fresul,'\\begin{figure}[htbp] \\begin{center} \n',' ');
fprintf(fresul,captionA);
fprintf(fresul,'\\begin{tiny} \n',' ');
fprintf(fresul,'\\vspace{0.5cm} \n');
fprintf(fresul,'\\begin{tabular}{|*{9}{@{}m{0.038\\textwidth}@{}}|*{17}{@{}m{0.038\\textwidth}@{}}|c} \n\\cline{1-26} \n',' ');
for j=3*npin:-1:(3*npin+1)/2
for i=(3*npin+1)/2:3*npin
  if(holes(i,j)==1)
    if (abs(HFdiff(i,j))<0.5 )
      fprintf(fresul,' %3.1f   &  ' ,HFdiff(i,j));
    elseif (abs(HFdiff(i,j))<=1)
      fprintf(fresul,'\\textcolor{SkyBlue}{ %3.1f  } &  ' ,HFdiff(i,j));
    elseif (abs(HFdiff(i,j))<=2)
      fprintf(fresul,'\\textcolor{GreenYellow} {%3.1f }  &  ' ,HFdiff(i,j));
    elseif (abs(HFdiff(i,j))<=5) 
      fprintf(fresul,'\\textcolor{Dandelion}{ %3.1f }  &  ' ,HFdiff(i,j));
    else 
      fprintf(fresul,'\\textcolor{Red}{%3.1f  } &  ' ,HFdiff(i,j));
    end
  else
%  fprintf(fresul,' \\cellcolor{Gray} &  ',' ' );
  fprintf(fresul,'  &  ',' ' );
  end
end
fprintf(fresul,' \\\\ \n',' ');
if(j==35)
  fprintf(fresul,' \\cline{1-26} \n',' ');
end
end
fprintf(fresul,'\\cline{1-26} \n');
fprintf(fresul,'\\end{tabular} \n');
fprintf(fresul,'\\end{tiny} \n',' ');
fprintf(fresul,'\\end{center} \\end{figure} \n\n');
fclose(fresul);

end

