%--------------------------------------------------------------------------------------------------%
% Copyright (C) <2011> by <Ralph Smith (rsmith@eos.ncsu.edu), Zhengzheng Hu (zhu4@ncsu.edu)>
% <Department of Mathematics, North Carolina State University, Raleigh, NC 27695>
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
% associated documentation files (the "Software"), to deal in the Software without restriction,
% including without limitation the rights to use, copy, modify, merge, publish, distribute,
% sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all copies or
% substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
% NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
% DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
% OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%--------------------------------------------------------------------------------------------------%
%                                Input to the main_opt
% This function sets up initial physical parameters and the running options
% for the optimization
%
%--------------------------------------------------------------------------------------------------%
function [opt,ratedata,params,scales,non_opt_params] = opt_input
% Running options
% Two cases are avaiable, one without prestress (opt.csnum=1), and one with (opt.csnum=2)
opt.csnum = 1;
% 0 for optimization, 1 for plotting initial, 2 for plotting final results
opt.runflag = 0;           
%
% Input files
% Exp. data needs to be defined as a struct that has the following
% componenets: T (time), E (input voltage in volts), P (polarization in
% C/m^2), and S (strain in percentage)
if(opt.csnum==1)
    opt.filename = 'exp_data/York_PZT_0Mpa';
    opt.dataname = 'York_PZT_0Mpa';
    non_opt_params.SIGMA = -0.0e6;
    % Rate dependent data 
    ratedata.lb1 = 30303;   % lower bound for the first input, where the relaxation is negligible
    ratedata.ub1 = 31800;   % upper bound for the first input
    ratedata.lb2 = 21951;   % lower bound for the second input, around Ec_bar
    ratedata.ub2 = 22851;   % upper bound for the second input
    ratedata.step1 = 2; % step size for the first input
    ratedata.step2 = 5; % step size for the second input
else
    opt.filename = 'exp_data/York_PZT_10p6Mpa_Sampled';
    opt.dataname = 'York_PZT_10p6Mpa_Sampled';
    non_opt_params.SIGMA = -10.6e6;
    ratedata.lb1 = 24471;
    ratedata.ub1 = 25271;
    ratedata.lb2 = 16600;
    ratedata.ub2 = 17200;
    ratedata.step1 = 5;
    ratedata.step2 = 2;
end
%
%--------------------------------------------------------------------------------------------------%
% Define number of basis functions for sig_I (beta) and sig_C (alpha).
% each has two parts: fraction part and integer part.
% eg: if base.alpha_frac=2, then the basis functions are: 2^(-1)*sig_C and 2^(-2)*sig_C
% eg: if base.alpha_frac=3, then the basis functions are: 2^(-1)*sig_C, 2^(-2)*sig_C, and 2^(-3)*sig_C
% eg: if base.alpha_int=2, then the basis functions are: 2^(0)*sig_C, 2^(1)*sig_C
%
non_opt_params.alpha_int = 2;
non_opt_params.alpha_frac = 3;
non_opt_params.beta_int = 2;
non_opt_params.beta_frac = 3;
%
%--------------------------------------------------------------------------------------------------%
% All parameters without the scaling factors.
%
if(opt.csnum==1) % recommended initial parameters for the zero stress case
    params.P_R_p = 0.2000;
    params.P_R_90 = 0.0;
    params.EPS_R_p = 0.8095;
    params.EPS_R_90 =0.0;
    params.chi_p = 0.47132;
    non_opt_params.fc_bar = 0.1e6;
    non_opt_params.sig_I = 0.25e6;
    non_opt_params.sig_C =0.5*log(1/.5);
else   % recommended initial parameters for the prestress case
    params.P_R_p = 0.1757;
    params.P_R_90 = 0.0;
    params.EPS_R_p =0.4498;
    params.EPS_R_90 = 0.0;
    params.chi_p = 0.51305;
    non_opt_params.fc_bar = 0.1e6;
    non_opt_params.sig_I = 0.25e6;
    non_opt_params.sig_C =0.5*log(1/.5);
end
params.d_p = 0.374;
params.d_90 = 0;
params.s_p = 0.188;

params.gamma = 0.06;
params.tau_90 = 0.7;
% alpha's and beta's are set to be 1 initially
params.alpha_frac = ones(1,non_opt_params.alpha_frac);
params.alpha_int = ones(1,non_opt_params.alpha_int);
params.beta_frac = ones(1,non_opt_params.beta_frac);
params.beta_int = ones(1,non_opt_params.beta_int);
%
%--------------------------------------------------------------------------------------------------%
% The scaling factors, so that the above parameter values are between 0 and 1.
%
scales.P_R_p = 1.0;
scales.P_R_90 = 0.0;
scales.EPS_R_p = 1e-3;
scales.EPS_R_90 = -1e-2;
scales.chi_p = 1e-7;
scales.d_p = 1e-9;
scales.d_90 = 0;
scales.s_p = 1e-10;
%
scales.gamma = 1e-2;
scales.tau_90 = 1e-1;
%
scales.alpha_frac = ones(1,non_opt_params.alpha_frac);
scales.alpha_int = ones(1,non_opt_params.alpha_int);
scales.beta_frac = ones(1,non_opt_params.beta_frac);
scales.beta_int = ones(1,non_opt_params.beta_int);
%
%--------------------------------------------------------------------------------------------------%
% Numerical parameters
opt.NE = 100; %number of points in the loopup table
opt.Ni = 41; % number of quadrature points for the activiation energy density function
opt.Nj = 41; % number of quadrature points for the interaction field density function
opt.wt = 100; % weight in optimization
opt.int = 10;  % the first point used in the optimization to calculate the residual
%
%Actuator parameters based one NEC Actuator
non_opt_params.area = 6.5e-3*6.5e-3;    % cross section area of the stack actuator
non_opt_params.ell = 10e-3;                         % length of the stack actuator
non_opt_params.ks = 2.7e6;                         % spring constant
%