function stateVector = packState(solverState)
% pack solver state variables in a single vector
%
% THIS IS NO USER FUNCTION

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2012 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.


% The vector will look, like:
%  
% 1) nucleation bin pivot (dim1, dim2, ..., number, density=nan)
% 2) nucleation bin boundary (dim1, dim2, ..., number=0, density=nan)
% 3) all pivots (dim1, dim2, ..., number, probability density)
% 4) all bulk states

%% init
nBulkState = size(solverState.bulkStateVector, 1);
nDim = size(solverState.hcMatrix, 1);
nPivot = size(solverState.hcMatrix, 2);
stateVectorSize = nBulkState + nPivot*(nDim + 1 + solverState.flagDensity);
stateVector = nan(stateVectorSize, 1);

%% pivot part
if solverState.flagDensity
    stateMatrix = [solverState.hcMatrix; solverState.numberVector; ...
        solverState.probabilityVector];
else
    stateMatrix = [solverState.hcMatrix; solverState.numberVector];
end
stateVector(1:(stateVectorSize - nBulkState), 1) = stateMatrix(:);

%% bulk states
if ~isempty(solverState.bulkStateVector)
    stateVector((stateVectorSize-nBulkState+1):stateVectorSize) = ...
        solverState.bulkStateVector;
end

end