function result = test_grooteman_11_parallel2(varargin)
% PROB_MULTIPLE_DP_TEST  One line description goes here
%  
% More detailed description of the test goes here.
%
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2011 Deltares
%       Bas Hoonhout
%
%       bas.hoonhout@deltares.nl	
%
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library 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
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tools is part of <a href="http://OpenEarth.Deltares.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 26 Aug 2011
% Created with Matlab version: 7.9.0.529 (R2009b)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

% MTestCategory.WorkInProgress;

%% Read Settings

OPT = struct(...
    'MinAppr', 0, ...
    'NoCrossTerms', false, ...
    'Weightening', 'none', ...
    'StartUp', '', ...
    'Method', 'ADIS', ...
    'seed', NaN);               % number of samples used for crude Monte Carlo

OPT = setproperty(OPT, varargin{:});

%% define stochastic variables
addpath(genpath(pwd),'-end')

result = struct('Exact',{struct('Beta', norm_inv(1-1.23e-4, 0, 1),'P_f', 1.23e-4)});

%% run Directional Sampling Object-Oriented

% construct stochastic variables
StochVars(2,1) = RandomVariable;

for i=1:3
    StochVars(i,1).Name                     = ['U' num2str(i)];
    StochVars(i,1).Distribution             = @norm_inv;
    StochVars(i,1).DistributionParameters   = {0 1};
end

% construct limit state
LSF(2,1) = LimitState;

for i=1:2
    LSF(i,1).Name                           = ['Z' num2str(i)];
    LSF(i,1).ResponseSurface                = AdaptiveResponseSurface;
    LSF(i,1).ResponseSurface.NoCrossTerms   = OPT.NoCrossTerms;
    
    % check for weightening
    switch OPT.Weightening
        case 'none'
            % nothing to be done, defaults are fine
        case 'selection'
            LSF(i,1).ResponseSurface.DefaultFit  = false;
        case 'weighted'
            LSF(i,1).ResponseSurface.DefaultFit  = false;
            LSF(i,1).ResponseSurface.WeightedARS = true;
            LSF(i,1).ResponseSurface.FitFunction = @wpolyfitn;
    end
end

LSF(1,1).LimitStateFunction  = @prob_grooteman_11_parallel2_LS1;
LSF(2,1).LimitStateFunction  = @prob_grooteman_11_parallel2_LS2;

MLS = MultipleLimitState(StochVars, LSF, @prob_grooteman_11_parallel2_aggregate);

% construct line searcher 
LineSearcher            = LineSearch;

switch OPT.Method
    case 'ADIS'
        % construct ADIS object
        DS_gr_11        = AdaptiveDirectionalImportanceSampling(MLS,LineSearcher,0.95,0.2,OPT.seed);
        DS_gr_11.MinNrApproximatedPoints = OPT.MinAppr;
    case 'DS'
        DS_gr_11        = DirectionalSampling(MLS,LineSearcher,0.95,0.2,OPT.seed);
end

if strcmp(OPT.Method,'ADIS')
    if strcmp(OPT.StartUp,'AxialSearch')
        DS_gr_11.StartUpMethods = StartUpAxialSearch;
    elseif strcmp(OPT.StartUp,'AxialPoints')
        DS_gr_01.StartUpMethods = StartUpAxialPoints;
    elseif strcmp(OPT.StartUp,'FastARS')
        DS_gr_11.StartUpMethods = StartUpFastARS;
    end
end

% calculate Pf
DS_gr_11.CalculatePf;

% save results
result.OO_ADIS.DS_object    = DS_gr_11;
result.OO_ADIS.Output.P_f   = DS_gr_11.Pf;
result.OO_ADIS.Output.Beta  = norm_inv(1-DS_gr_11.Pf, 0, 1);
result.OO_ADIS.Output.Calc  =DS_gr_11.LimitState.NumberExactEvaluations;

%% print result
prob_print_test(result);