%% Snyder Cuboid
% In this case, we construct the approximation by hand.
%
% WE ASSUME that the simulation was already generated by
%   evaluateSnyderCuboid.
clc

dynamicCaseName = 'lactoseGrowth';

%% simulation
% execute simulation
executeLocalPbeCase(dynamicCaseName);
% load simulation data
[result pbeDef] = loadLocalPbeResult(dynamicCaseName);
% generate point sets for approximation
% generatePointSets(result, pbeDef, ...
%     struct('nBin', 50, 'nSample', 1000, ...
%            'viewStatistics', 0, 'caseName', dynamicCaseName)...
%     )

%% graphs
close all
% [timeVector totalVolumeVector] = extractDataFromPbeResult(...
%     'integralProperty', 'volume', result, pbeDef)
% figure, plot(timeVector, totalVolumeVector);
% a bulk property
for iProp = 1:4
    [timeVector totalVolumeVector] = extractDataFromPbeResult(...
    'bulkProperty', iProp, result, pbeDef)
    figure, plot(timeVector, totalVolumeVector);
    title(['property ' num2str(iProp)])
end

% TODO % 3
% test

% %% Load and Approximation
% % load data
% % [result pbeDef] = loadLocalPbeResult(dynamicCaseName);
% 
% shapeMatrix = [eye(3) 100*ones(3, 4)]';
% % shapeMatrix = [1 2 3 100 100 100 100]';
% shapeMatrix = mapToValidityCone(shapeMatrix, pbeDef.dec, 0);
% spanMatrix = [1./pbeDef.dec.cdef.relativeGrowthCaseTwo ...
%     shapeMatrix];
% % spanMatrix = [pbeDef.dec.cdef.relativeGrowthCaseTwo ...
% %     shapeMatrix];
% % orthogonalize
% spanMatrix = orth(spanMatrix);
% 
% % check shapes
% % figure,viewPolytope(struct('A', pbeDef.dec.A, 'h', ...
% %     pbeDef.dec.mappingReducedToFull*spanMatrix(:,1)));
% % figure,viewPolytope(struct('A', pbeDef.dec.A, 'h', ...
% %     pbeDef.dec.mappingReducedToFull*spanMatrix(:,2)));
% % figure,viewPolytope(struct('A', pbeDef.dec.A, 'h', ...
% %     pbeDef.dec.mappingReducedToFull*spanMatrix(:,3)));
% % figure,viewPolytope(struct('A', pbeDef.dec.A, 'h', ...
% %     pbeDef.dec.mappingReducedToFull*spanMatrix(:,4)));
% 
% thisFile = ['./database/' dynamicCaseName '_4_handmade_.matx'];
% if ~exist(thisFile, 'file')
%     dec = projectDec(pbeDef.dec, spanMatrix, 'zerotol', 1e-8);
%     dataApprox.dec = dec;
%     dataApprox.cdef = pbeDef.dec.cdef;
%     save(thisFile, '-mat', 'dataApprox');
% % else
% %     data = load(thisFile, '-mat');
% end
%     
% caseList = {''};
% 
% %% Evaluate approximation (projection)
% % dataApprox = evaluateConfinedProjection(dynamicCaseName, ...
% %     [dynamicCaseName '_4_handmade_'])
% 
% %% Compute dynamical evolution for approximation
% writePbeDefinitionForProjection(dynamicCaseName, 'handmade', caseList, 4);
% thisCase = [dynamicCaseName '_4_handmade_'];
% executeLocalPbeCase(thisCase)
% 
% evaluateConfinedSimulation(dynamicCaseName, thisCase)
