%--------------------------------------------------------------------------------------------------%
%
% DriverSimulation.m: 
%   Main program for simulating the hem model using experimental data
%
%   Parameters:
%       none
%
%   Returns: 
%       none (plots comparison between model and experimental data)
%
%   Required files: load_initial_parameters.m, load_experimental_data.m, load_optimal_parameters.m,
%       subinterval_pts.m, extract_densitities.m, AlphaLogNormal.m, AlphaNormal.m, pxx_he.m,
%       sma_strain.m, resistance.m, resistance_partials.m
%                               
%                       ----------------------------
%
% Copyright (C) <2011> by <Ralph Smith (rsmith@ncsu.edu), John Crews(jhcrews@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.
%--------------------------------------------------------------------------------------------------%

clear all
close all
clc

% uncomment to run initial parameters
params = load_initial_parameters();
% uncomment to run optimal parameters
% params = load_optimal_parameters('OptimizationResults65and80_allparams.mat');

load_experimental_data;

% select experimental data
exp_stress = stress65_exp;
exp_strain = strain65_exp;
exp_res = res65_exp;
exp_temp = 273+65; % [K]
% initial phase fraction
sindex = find(exp_stress>5e6,1,'first');
xp0 = (exp_strain(sindex)+params.epsT)/(2*params.epsT);
ic = [xp0 1-xp0];

[xp xm xa strain res] = sma_hem_constanttemp(t_exp,ic,exp_stress,exp_temp,params);


figure
set(gcf,'Color','w')
hold on
plot(exp_strain.*100, exp_stress./1e6,'.b','MarkerSize',16)
plot(strain.*100, exp_stress./1e6,'b','LineWidth',2)
xlabel('Strain [%]')
ylabel('Stress [MPa]');
l = legend('Experimental Data','Model');
set(l,'Location','NorthWest');
box on
grid on
set(gca,'Ylim',[0 700]);
set(gca,'Xlim',[0 7]);

figure
set(gcf,'Color','w')
hold on
plot(exp_strain.*100, exp_res,'.b','MarkerSize',16)
plot(strain.*100, res,'b','LineWidth',2)
xlabel('Strain [%]')
ylabel('Resistance [\Omega]');
l = legend('Experimental Data','Model');
set(l,'Location','NorthWest');
box on
grid on
set(gca,'Xlim',[0 7]);