function pneopt = mpopt2pneopt(mpopt, model, alg)
%MPOPT2PNEOPT   Create/modify PNES_MASTER options struct from MPOPT.
%
%   PNEOPT = MPOPT2PNEOPT(MPOPT, MODEL)
%   PNEOPT = MPOPT2PNEOPT(MPOPT, MODEL, ALG)
%
%   Uses a MATPOWER options struct, MPOPT, to create or modify an
%   PNES_MASTER options struct.
%
%   Inputs (default values in parentheses):
%       MPOPT : MATPOWER options struct
%       MODEL ('PNE') : (optional) one of the following model types, required
%               for selection of solver in case ALG is 'DEFAULT' (solver
%               precedence for each model type list in parentheses):
%           'PNE' - nonlinear equation with all continuous variables
%                   (NEWTON, FSOLVE)
%       ALG ('DEFAULT') : (optional) any valid value of OPT.alg for
%               PNES_MASTER.
%
%   Output:
%       PNEOPT : an options struct for use by PNES_MASTER and friends
%
%   See PNES_MASTER, MPOPTION.

%   MP-Opt-Model
%   Copyright (c) 2015-2021, Power Systems Engineering Research Center (PSERC)
%   by Ray Zimmerman, PSERC Cornell
%
%   This file is part of MP-Opt-Model.
%   Covered by the 3-clause BSD License (see LICENSE file for details).
%   See https://github.com/MATPOWER/mp-opt-model for more info.

%% set default args
if nargin < 3
    alg = 'DEFAULT';
    if nargin < 2
        model = '';
    end
end

%% use pf.alg to set nleqs_opt for corrector
switch mpopt.pf.alg
    case 'FSOLVE'
        nleqs_alg = 'FSOLVE';
    otherwise
        nleqs_alg = 'NEWTON';
end

%% create PNES_MASTER options struct
pneopt = struct( ...
    'alg',              alg, ...
    'verbose',          mpopt.verbose, ...
    'nleqs_opt',        struct( ...
        'alg',              nleqs_alg, ...
        'tol',              mpopt.pf.tol    ), ...
    'stop_at',          mpopt.cpf.stop_at, ...
    'solve_base',       1, ...
    'parameterization', mpopt.cpf.parameterization, ...
    'step',             mpopt.cpf.step, ...
    'step_min',         mpopt.cpf.step_min, ...
    'step_max',         mpopt.cpf.step_max, ...
    'adapt_step',       mpopt.cpf.adapt_step, ...
    'adapt_step_damping',   mpopt.cpf.adapt_step_damping, ...
    'adapt_step_tol',   mpopt.cpf.adapt_step_tol, ...
    'target_lam_tol',   mpopt.cpf.target_lam_tol, ...
    'nose_tol',         mpopt.cpf.nose_tol, ...
    'output_fcn',       @(varargin)cpf_pne_output_fcn(obj, ad, varargin{:}), ...
    'plot',             struct( ...
        'level',            mpopt.cpf.plot.level, ...
        'idx',              mpopt.cpf.plot.bus, ...
        'idx_default',      @()cpf_plot_idx_default(obj, dm, ad), ...
        'yname',            'V', ...
        'yfcn',             @(v_,idx)cpf_plot_yfcn(obj, dm, ad, v_, idx), ...
        'title',            'Voltage at Bus %d', ...
        'title2',           'Voltage at Multiple Buses', ...
        'ylabel',           'Voltage Magnitude', ...
        'legend',           'Bus %d'    ) ...
);
