% Demonstrate the numbering of conductances in the 2D diffusion models
%
% Samuli Siltanen May 2025

% Pixel grid size
N = 3;

% Construct simple conductance matrices
condmatH = ones(N,N-1);
condmatV = ones(N-1,N);

% Set geometrical parameters
geometry_set;

% Graphical parameters
condmax = 1.1;
lwidth_grid = .5;
fsize = 8;

% Horizontal neighbors

% Create plot window
figure(1)
clf

% Conductivities inside the grid (not at the boundary
star_x = [-Dx/2 0    Dx/2  0   ];
star_y = [ 0    Dx/2 0    -Dx/2];
% Conductivities between horizontal neighbors
for rrr = 1:N
    for ccc = 1:(N-1)
        curr_cond_color = colorscale_cond(condmatH(rrr,ccc),condmax);
        pc = patch(ccc*Dx+star_x,(N-rrr+.5)*Dx+star_y,curr_cond_color);
        set(pc,'edgecolor',curr_cond_color);
        index = (ccc-1)*N+rrr;
        text(ccc*Dx-.35*Dx,(N-rrr+.5)*Dx,num2str(index),'fontsize',fsize);
        hold on
    end
end

% Plot grid
p1 = plot([0 N],[0 0],'k','linewidth',lwidth_grid);
set(p1,'color',color_gridline)
hold on
p2 = plot([0 0],[0 N],'k','linewidth',lwidth_grid);
set(p2,'color',color_gridline)
for iii = 1:N
    for jjj = 1:N
        p3 = plot(jjj*[Dx Dx],[(iii-1)*Dx iii*Dx],'k','linewidth',lwidth_grid);
        set(p3,'color',color_gridline)
        p3 = plot([jjj*Dx (jjj-1)*Dx],iii*[Dx Dx],'k','linewidth',lwidth_grid);
        set(p3,'color',color_gridline)
    end
end

% Axis settings
axis([-3*Dx (N+3)*Dx -3*Dx (N+3)*Dx])
axis equal
axis off

% Write image to file
eval(['print -dpng -r500 images/conductanceindicesH_',num2str(N),'grid.png'])

% Horizontal neighbors

% Create plot window
figure(2)
clf

% Conductivities inside the grid (not at the boundary
star_x = [-Dx/2 0    Dx/2  0   ];
star_y = [ 0    Dx/2 0    -Dx/2];
% Conductivities between vertical neighbors
for rrr = 1:(N-1)
    for ccc = 1:N
        curr_cond_color = colorscale_cond(condmatV(rrr,ccc),condmax);
        pc = patch((ccc-.5)*Dx+star_x,(N-rrr)*Dx+star_y,curr_cond_color);
        set(pc,'edgecolor',curr_cond_color);
        index = N*(N-1)+(ccc-1)*(N-1)+rrr;
        text((ccc-.6)*Dx,(N-rrr+.2)*Dx,num2str(index),'fontsize',fsize);
        hold on
    end
end

% Plot grid
p1 = plot([0 N],[0 0],'k','linewidth',lwidth_grid);
set(p1,'color',color_gridline)
hold on
p2 = plot([0 0],[0 N],'k','linewidth',lwidth_grid);
set(p2,'color',color_gridline)
for iii = 1:N
    for jjj = 1:N
        p3 = plot(jjj*[Dx Dx],[(iii-1)*Dx iii*Dx],'k','linewidth',lwidth_grid);
        set(p3,'color',color_gridline)
        p3 = plot([jjj*Dx (jjj-1)*Dx],iii*[Dx Dx],'k','linewidth',lwidth_grid);
        set(p3,'color',color_gridline)
    end
end

% Axis settings
axis([-3*Dx (N+3)*Dx -3*Dx (N+3)*Dx])
axis equal
axis off

% Write image to file
eval(['print -dpng -r500 images/conductanceindicesV_',num2str(N),'grid.png'])
