%% Urea growth - scaling images
% This one assumes that runUreaGenericGrowth was already run and it
%   produces data that is required to explain the Grassmann space.
clc
dynamicCaseName = 'ureaGenericGrowth';

% load sample points
[result pbeDef] = loadLocalPbeResult([dynamicCaseName '_noGrowthLimit']);

%% unscaled
generateSingleProjection('ureaGenericGrowth_tmp', pbeDef, result, ...
    {'targetDim', 2, ...
     'method', 'nonlinearLocal', ...
     'projectionType', 'true', ...
     'errorType', 'PCA', ...
     'manualScaling', eye(3), ...
     'measureList', 'volume'}, 1)
dataApprox = load('database/ureaGenericGrowth_tmp.matx', '-mat');
dataApprox = dataApprox.dataApprox;

rangeVector = -2*pi:(pi/50):2*pi;
[xMatrix, yMatrix] = meshgrid(rangeVector, rangeVector);
% compute objective values
objectiveMatrix = 0*xMatrix;
for iElement = 1:numel(xMatrix)
    if mod(iElement, 50) == 0
        disp(iElement / numel(xMatrix))
    end
    objectiveMatrix(iElement) = ...
        dataApprox.nonlinearData.fminconStruct.objective(...
            [xMatrix(iElement); yMatrix(iElement)]);
end

% save data
zMatrix = objectiveMatrix;
save('./data/ureaGrassSpacePcaUnscaled.matx', '-mat', ...
    'xMatrix', 'yMatrix', 'zMatrix');

% quick plot
figure,hold on
surf(xMatrix/pi, yMatrix/pi, log10(objectiveMatrix), 'lineStyle', 'none')
title('unscaled PCA')

%% fully scaled PCA
generateSingleProjection('ureaGenericGrowth_tmp', pbeDef, result, ...
    {'targetDim', 2, ...
     'method', 'nonlinearLocal', ...
     'projectionType', 'true', ...
     'errorType', 'PCA', ...
     'manualScaling', 1, ...
     'measureList', 'volume'}, 1)
dataApprox = load('database/ureaGenericGrowth_tmp.matx', '-mat');
dataApprox = dataApprox.dataApprox;

rangeVector = -0.5*pi:(pi/100):0.5*pi;
[xMatrix, yMatrix] = meshgrid(rangeVector, rangeVector);
% compute objective values
objectiveMatrix = 0*xMatrix;
for iElement = 1:numel(xMatrix)
    if mod(iElement, 50) == 0
        disp(iElement / numel(xMatrix))
    end
    objectiveMatrix(iElement) = ...
        dataApprox.nonlinearData.fminconStruct.objective(...
            [xMatrix(iElement); yMatrix(iElement)]);
end

% save data
zMatrix = objectiveMatrix;
save('./data/ureaGrassSpacePcaFullyScaled.matx', '-mat', ...
    'xMatrix', 'yMatrix', 'zMatrix');

% quick plot
figure,hold on
surf(xMatrix/pi, yMatrix/pi, log10(objectiveMatrix), 'lineStyle', 'none')
title('scaled PCA')

%% medium scaled PCA
generateSingleProjection('ureaGenericGrowth_tmp', pbeDef, result, ...
    {'targetDim', 2, ...
     'method', 'nonlinearLocal', ...
     'projectionType', 'true', ...
     'errorType', 'PCA', ...
     'manualScaling', 0.5, ...
     'measureList', 'volume'}, 1)
dataApprox = load('database/ureaGenericGrowth_tmp.matx', '-mat');
dataApprox = dataApprox.dataApprox;

rangeVector = -0.5*pi:(pi/100):0.5*pi;
[xMatrix, yMatrix] = meshgrid(rangeVector, rangeVector);
% compute objective values
objectiveMatrix = 0*xMatrix;
for iElement = 1:numel(xMatrix)
    if mod(iElement, 50) == 0
        disp(iElement / numel(xMatrix))
    end
    objectiveMatrix(iElement) = ...
        dataApprox.nonlinearData.fminconStruct.objective(...
            [xMatrix(iElement); yMatrix(iElement)]);
end

% save data
zMatrix = objectiveMatrix;
save('./data/ureaGrassSpacePcaMediumScaled.matx', '-mat', ...
    'xMatrix', 'yMatrix', 'zMatrix');

% quick plot
figure,hold on
surf(xMatrix/pi, yMatrix/pi, log10(objectiveMatrix), 'lineStyle', 'none')
title('medium scaled PCA')

%% unscaled VOLUME-RELMAX
generateSingleProjection('ureaGenericGrowth_tmp', pbeDef, result, ...
    {'targetDim', 2, ...
     'method', 'nonlinearLocal', ...
     'projectionType', 'true', ...
     'errorType', 'relativeMax', ...
     'manualScaling', eye(3), ...
     'measureList', 'volume'}, 1)
dataApprox = load('database/ureaGenericGrowth_tmp.matx', '-mat');
dataApprox = dataApprox.dataApprox;

rangeVector = -2*pi:(pi/50):2*pi;
[xMatrix, yMatrix] = meshgrid(rangeVector, rangeVector);
% compute objective values
objectiveMatrix = 0*xMatrix;
for iElement = 1:numel(xMatrix)
    if mod(iElement, 50) == 0
        disp(iElement / numel(xMatrix))
    end
    objectiveMatrix(iElement) = ...
        dataApprox.nonlinearData.fminconStruct.objective(...
            [xMatrix(iElement); yMatrix(iElement)]);
end

% save data
zMatrix = objectiveMatrix;
save('./data/ureaGrassSpaceVolumeUnscaled.matx', '-mat', ...
    'xMatrix', 'yMatrix', 'zMatrix');

% quick plot
figure,hold on
surf(xMatrix/pi, yMatrix/pi, log10(objectiveMatrix), 'lineStyle', 'none')
title('unscaled volume')
colorbar

%% fullly scaled VOLUME-RELMAX
generateSingleProjection('ureaGenericGrowth_tmp', pbeDef, result, ...
    {'targetDim', 2, ...
     'method', 'nonlinearLocal', ...
     'projectionType', 'true', ...
     'errorType', 'relativeMax', ...
     'manualScaling', 1, ...
     'measureList', 'volume'}, 1)
dataApprox = load('database/ureaGenericGrowth_tmp.matx', '-mat');
dataApprox = dataApprox.dataApprox;

rangeVector = -0.5*pi:(pi/100):0.5*pi;
[xMatrix, yMatrix] = meshgrid(rangeVector, rangeVector);
% compute objective values
objectiveMatrix = 0*xMatrix;
for iElement = 1:numel(xMatrix)
    if mod(iElement, 50) == 0
        disp(iElement / numel(xMatrix))
    end
    objectiveMatrix(iElement) = ...
        dataApprox.nonlinearData.fminconStruct.objective(...
            [xMatrix(iElement); yMatrix(iElement)]);
end

% save data
zMatrix = objectiveMatrix;
save('./data/ureaGrassSpaceVolumeFullyScaled.matx', '-mat', ...
    'xMatrix', 'yMatrix', 'zMatrix');

% quick plot
figure,hold on
surf(xMatrix/pi, yMatrix/pi, log10(objectiveMatrix), 'lineStyle', 'none')
title('fully scaled PCA')
colorbar

%% medium scaled VOLUME-RELMAX
generateSingleProjection('ureaGenericGrowth_tmp', pbeDef, result, ...
    {'targetDim', 2, ...
     'method', 'nonlinearLocal', ...
     'projectionType', 'true', ...
     'errorType', 'relativeMax', ...
     'manualScaling', 0.5, ...
     'measureList', 'volume'}, 1)
dataApprox = load('database/ureaGenericGrowth_tmp.matx', '-mat');
dataApprox = dataApprox.dataApprox;

rangeVector = -0.5*pi:(pi/100):0.5*pi;
[xMatrix, yMatrix] = meshgrid(rangeVector, rangeVector);
% compute objective values
objectiveMatrix = 0*xMatrix;
for iElement = 1:numel(xMatrix)
    if mod(iElement, 50) == 0
        disp(iElement / numel(xMatrix))
    end
    objectiveMatrix(iElement) = ...
        dataApprox.nonlinearData.fminconStruct.objective(...
            [xMatrix(iElement); yMatrix(iElement)]);
end

% save data
zMatrix = objectiveMatrix;
save('./data/ureaGrassSpaceVolumeMediumScaled.matx', '-mat', ...
    'xMatrix', 'yMatrix', 'zMatrix');

% quick plot
figure,hold on
surf(xMatrix/pi, yMatrix/pi, log10(objectiveMatrix), 'lineStyle', 'none')
title('medium scaled VOLUME')
colorbar