function f = AbaqusAllResult(xx)
% The Objective function in terms of the mean squared error (MSE) between 
% the experimental and modelled stress history defined in discrete-time

Phi0=xx(1);
Sig0=xx(2);
hs=xx(3);
k=xx(4);
b=xx(5);
n=xx(6);

m=0.01;

% record the simulation information
FidInf = fopen('Opt_Info.txt','a');
fprintf(FidInf, 'First dotPhi0: %12.5E \n', Phi0);
fprintf(FidInf, 'First Sig0, hS and k: %12.5E, %12.5E, %12.5E \n', Sig0, hs, k);
fprintf(FidInf, 'First b and n: %12.5E, %12.5E \n', b, n);

%==================================================================
%==================================================================
Abaqusname=['check'];

% delete the old files
rmcommond = ['rm -rf ' Abaqusname '.*'];
status = dos(rmcommond);

fidin = fopen('bar1.inp','r');
fidout = fopen([Abaqusname '.inp'],'w');

while 1
    tline = fgetl(fidin);
    if ~ischar(tline),   break,   end
    fprintf(fidout,'%s \n',tline);
end
fclose(fidin);
% define the material parameters
fprintf(fidout, '%12.5E, %12.5E, \n', Phi0, m);
fprintf(fidout, '%12.5E, %12.5E, %12.5E, %12.5E, %12.5E \n', Sig0, hs, k, b, n);

fidin = fopen('bar2.inp','r');
while 1
    tline = fgetl(fidin);
    if ~ischar(tline),   break,   end
    fprintf(fidout,'%s \n',tline);
end
fclose(fidin);
fclose(fidout);
% Input File Generation Completed

% excute the abaqus file
abaquscommand=['abaqus -j ' Abaqusname '.inp user=code_exp.f -inter'];
[abaqusstatus, abaquslog]=dos(abaquscommand);
abqsucess = 1;
% abaqusstatus = 0, and the abaqus command is completed
if ~(abaqusstatus)
    AbaqusComp_Flag=['Abaqus JOB ' Abaqusname ' COMPLETED'];
    k=strfind(abaquslog, AbaqusComp_Flag);
    [d dd]=size(k);
    % find the exact AbaqusComp_Flag
    if (d >= 1)
        fprintf(FidInf, '%s \n', AbaqusComp_Flag);
    else
        AbaqusInComp_Flag=['Abaqus JOB ' Abaqusname ' IS NOT COMPLETED'];
        fprintf(FidInf,'%s \n', AbaqusInComp_Flag);
%        error('something is wrong in line 101');
        abqsucess = 0;
    end
else
     fprintf(FidInf,'ABAQUS RUN FAILs \n');
     abqsucess = 0;
end

% read the result data from abaqus
if (abqsucess > 0.5)
    % delete the old files
    rmcommond = ['rm -rf Stress_Strain.dat'];
    status = dos(rmcommond);
    abpycommand=['abaqus python script.py'];
    [abpystatus, abpylog]=dos(abpycommand);
    if (abpystatus)
        fprintf(FidInf,'Python Failed \n');
    end 
    %load the stress-strain in the file
    load Stress_Strain.dat;
    epsilon=Stress_Strain(:,1);
    sigma=Stress_Strain(:,2);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
    %load target data (stress unit [MPa])
    load SS.dat;
	% interpolate data on regular intervals
	St=0:0.001:0.15;
	for i=1:length(St)
		S_Exp(i)=interpolate(SS(:,1),SS(:,2),St(i));
	end
	for i=1:length(St)
		S_Est(i)=interpolate(epsilon(:),sigma(:),St(i));
	end
	% estimate hardening rate (1/s)*(ds/de) all values logarithmic
	% Converting to logarithmic
	for i=1:length(St)
		lS_Exp(i)=S_Exp(i)*(1+St(i));
		lS_Est(i)=S_Est(i)*(1+St(i));
		lSt(i)=log(1+St(i));
	end	
	% Estimating derivative
	k=0;
    for i=2:(length(lSt)-1)
		if (lSt(i) > 0.01)
        k=k+1;
        temp1=(lS_Exp(i)-lS_Exp(i-1))/(lSt(i)-lSt(i-1));
        temp2=(lS_Exp(i+1)-lS_Exp(i))/(lSt(i+1)-lSt(i));
        dSdE_Exp(k)=0.5*(temp1+temp2)/lS_Exp(i);
        temp1=(lS_Est(i)-lS_Est(i-1))/(lSt(i)-lSt(i-1));
        temp2=(lS_Est(i+1)-lS_Est(i))/(lSt(i+1)-lSt(i));
        dSdE_Est(k)=0.5*(temp1+temp2)/lS_Est(i);	
        lStN(k)=lSt(i);
		end
    end
	% Estimating root mean square error Err=sqrt(SUM(Est-Exp)^2/n_data)
	% Estimating RMSE in the estimated stress value
	temp1=0;
	for i=1:length(St)
		temp1=temp1+(S_Est(i)-S_Exp(i))^2;
	end
	f1=sqrt(temp1/length(St));
	% Estimating RMSE in the estimated derivative devided by stress
	temp1=0;
	for i=1:length(lStN)
		temp1=temp1+(dSdE_Est(i)-dSdE_Exp(i))^2;
	end
	f2=sqrt(temp1/length(lStN));
	% Higher weight to f2
	f2=f2*100;	
else
    % if abaqus fails to complete, set the objective to be a big number
    % the algorithm will continue search the optimal objective function
    f1 = 1.E9;
	f2 = 1.E9;
end

f = f1+f2;

fprintf(FidInf, 'f1 is %12.5e \n', f1);
fprintf(FidInf, 'f2 is %12.5e \n', f2);
fprintf(FidInf, 'MSE is %12.5e \n', f);
fclose(FidInf);

end