% Plot the rate constant versus temperature for alanine dipeptide data.
% This script also generates an Arrhenius plot.

clear;

% Load data from NetCDF file.
filename = 'alanine-dipeptide-transition-rate-psi.nc';
nc = netcdf(filename);

% Extract variables into a structure.
nvars = length(nc.VarArray);
vars = struct();
for i = 1:nvars  
  vars = setfield(vars, nc.VarArray(i).Str, nc.VarArray(i).Data);
end

%% Create figure.
hfig = figure(1);

% Plot total log weight from each temperature to all other temperatures.

log_w_kl = vars.log_w_kl;
K = size(log_w_kl,1);
for k = 1:K
  log_w_kl(k,:) = log_w_kl(k,:) - max(log_w_kl(k,:));
end
N_k = sum(exp(log_w_kl),2);
disp(sprintf('on average, %f temperatures contribute to each reweighted temperature', mean(N_k)));

for k = 1:K
  log_w_kl(k,:) = log_w_kl(k,:) - logsum(log_w_kl(k,:));
end

colormap(flipud(hot));
imagesc(vars.temperature_k, vars.temperature_k, log_w_kl, [-10 0]);
axis equal;
%colorbar('PlotBoxAspectRatioMode', 'manual', 'PlotBoxAspectRatio', [1 20 1]);
delta = (vars.temperature_k(2) - vars.temperature_k(1)) * 0.7;
Tmin = min(vars.temperature_k)-delta;
Tmax = max(vars.temperature_k)+delta;
axis([Tmin Tmax Tmin Tmax]);

xlabel('contributing T (K)');
ylabel('target T (K)');

%colorbar;
colorbar('PlotBoxAspectRatioMode', 'manual', 'PlotBoxAspectRatio', [1 8 1]);

%% Write plot.
outfilename = 'alanine-dipeptide-log-contributions.eps';
exportfig(hfig, outfilename, 'width', 3.25, 'height', 3.75, 'color', 'rgb', 'resolution', 600);
unix(sprintf('epstopdf %s', outfilename));

