clc
clear all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Program for modelling J2 Plasticity
% -----------------
% Developed by Alfredo Alameda González
% Computational Solid Mechanics
% May 2017, Universitat Politècnica de Catalunya
% ------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% ****************
% INPUT MATERIAL PROPERTIES
% ****************
% YOUNG's MODULUS E (Recommended value = 200.000)
E = 200000 ;
% ---------------
% POISSON'S RATIO ny (Recommended value = 0.3)
ny = 0.3 ;
% ---------------
% ISOTROPIC PLASTIC HARDENING MODULUS K (Recommended value = 20.000)
K = 20000 ;
% ---------------
% KINEMATIC PLASTIC HARDENING MODULUS H (Recommended value = 20.000)
H = 20000 ;
% ---------------
% YIELD STRESS sigma_y
sigma_y = 20 ;
% ---------------
% VISCOUS COEFFICIENT eta (Recommended value = 50000)
eta = 50000 ;
% ------------------------
%%% ISOTROPIC HARDENING MODEL (LINEAR = 0, NON-LINEAR = 1)
HARD_MODEL = 1 ;
%%% ---------------
%%% Assimptotic stress value for nonlinear isotropic hardening model sigma_inf
sigma_inf = 25 ;
%%% ---------------
%%% Exponential coefficient for nonlinear isotropic hardening model delta
%%% (Recommended value = 1e5)
delta = 1e5 ;
%%% ---------------
% ****************
%  END INPUT MATERIAL PROPERTIES
% ****************

%% ****************
% INPUT LOADING PROPERTIES
% ****************
% ------------------------
% Number of strain steps for each loading cycle
nsteps = 20 ;
% ---------------
% Size of time increments for each loading cycle (Recommended value = 0.10)
delta_t = 0.10 ;
% ---------------
% Loading cycles matrix (considering material starting at rest)
loading_cycles =   1.0e-4 *  [5, 0, 0;      % Loading step 1
                             -5, 0, 0;      % Loading step 2
                              5, 0, 0];     % Loading step 3
% ---------------
% Number of loading cycles
nloadstates = size(loading_cycles,1) ;
% ---------------
% ****************
% END INPUT LOADING PROPERTIES
% ****************

%% ****************
% J2 PLASTICITY ALGORITHM
% ****************

% Computation of the remaining elastic variables
kappa = E/(3*(1-2*ny)) ; % Bulk modulus
lambda = (E*ny)/((1+ny)+(1-2*ny)) ; % Lamé constants
my = E/(2*(1+ny)) ;
C = [E,ny,K,H,sigma_y,eta,sigma_inf,delta,kappa,lambda,my]; % Vector containing material parameters

% Initializing variables
strain_history = zeros(1,13); % Creation of the matrix of historic strains
plastic_strain_history = zeros(1,13); % Creation of matrix of historic plastic strains
stress_history = zeros(1,13); % Creation of matrix of historic stresses
deviatoric_stress_history = zeros(1,6) ; % Creation of matrix of historic deviatoric stresses
eps_min = zeros(1,3); % Sets the initial strain state to zero (material at rest)
eps_p_n = plastic_strain_history(1,:); % Sets the initial plastic strain vector to zero
graph = zeros(nloadstates,1); % Variable used to plot the loading paths with different colors
% ---------------

for i = 1 : nloadstates % Loop on loading cycles
    eps_max = loading_cycles(i,:) ;
    strain_path = StrainPath(eps_min,eps_max,nsteps);
    graph(i) = length(strain_path);
    strain_history = [strain_history;strain_path]; % Updates strain history matrix with the corresponding strain path in each loading cycle
    
    for j = 1 : size(strain_path,1) % Loop on strain steps
        eps_n1 = strain_path(j,:) ; % Refreshes strain vector picking a new row from strain history matrix
        
        % Computation of the trial state at time n+1
        eps_p_n1_trial = eps_p_n ;
        eps_e_n1_trial = eps_n1 - eps_p_n1_trial ;
        sigma_n1_trial = SigmaTrial(C,eps_e_n1_trial) ;       
        
        % Computation of trial yield function as part of the trial state
        if HARD_MODEL == 0 
            f_n1_trial = norm(Deviatoric(ToMatrix(sigma_n1_trial(1:6)))-ToMatrix(sigma_n1_trial(8:13))) - sqrt(2/3)*(sigma_y - sigma_n1_trial(7));
        elseif HARD_MODEL == 1
            f_n1_trial = norm(Deviatoric(ToMatrix(sigma_n1_trial(1:6)))-ToMatrix(sigma_n1_trial(8:13))) - sqrt(2/3)*(sigma_y + diff_potential(eps_p_n1_trial(7),sigma_inf,sigma_y,delta,K));
        end
       
        % Checking of the trial yield function
        if f_n1_trial <= 0  % ELASTIC STEP
            eps_p_n1 = eps_p_n1_trial ;                     
            sigma_n1 = sigma_n1_trial ;
            
                        
        else                % PLASTIC STEP            
            if HARD_MODEL == 0   % Linear isotropic hardening
                gamma_n1 = (((2*my + 2/3*K + 2/3*H + eta/delta_t)^(-1)) * f_n1_trial) / delta_t ; % Computes discrete plastic multiplier
                
            elseif HARD_MODEL == 1   % Nonlinear isotropic hardening
                chi_n = eps_p_n(7) ;
                gamma_n1 = NewtonMethod(f_n1_trial,chi_n,delta_t,C) ; % Computes discrete plastic multiplier by means of Newton-Rapshon method

            end
            
            [sigma_n1,n_n1_trial] = CPP(sigma_n1_trial,gamma_n1,C,delta_t) ; % Return mapping algorithm (closest-point-projection)
            eps_p_n1 = CPP2(eps_p_n,sigma_n1_trial,n_n1_trial,gamma_n1,C,delta_t); % Updates plastic internal variable at time n+1
       
        end
        
        plastic_strain_history = [plastic_strain_history;eps_p_n1]; % Stores the plastic strain vector in its corresponding historical matrix
        stress_history = [stress_history;sigma_n1]; % Stores the stress vector in its corresponding historical matrix
        deviatoric_stress_history = [deviatoric_stress_history;ToVoigt(Deviatoric(ToMatrix(sigma_n1(1:6))))]; % Stores the deviatoric stress vector in its corresponding historical matrix
        eps_p_n = eps_p_n1 ; % Refreshes the value of the plastic strain to be used in the next step       

    end
    eps_min = eps_max ; % Recovers the maximum value of the present loading cycle to be used as the starting point for the next one
    
    
    
end

% ****************
% END J2 PLASTICITY ALGORITHM
% ****************

%% ****************
% PLOTTING
% ****************



% Stress11-strain11 curve
figure(1)
subplot(1,2,1)
ncolores = 6 ;
colores =  ColoresMatrix(ncolores);
markers = MarkerMatrix(ncolores) ;
step1 = 1;
for k = 1 : nloadstates
    step2 = step1 + graph(k) ;
    plot(strain_history(step1:step2,1),stress_history(step1:step2,1),'Marker',markers{2},'Color',colores(k+2,:))
    hold on
    grid on
    step1 = step2 ;
end
xlabel('uniaxial strain \epsilon_1_1')
ylabel('uniaxial stress \sigma_1_1')
title('STRESS_1_1-STRAIN_1_1 CURVE')

% Dev(Stress11)-strain11 curve
subplot(1,2,2)
step1 = 1;
for k = 1 : nloadstates
    step2 = step1 + graph(k) ;
    plot(strain_history(step1:step2,1),deviatoric_stress_history(step1:step2,1),'Marker',markers{2},'Color',colores(k+2,:))
    hold on
    grid on
    step1 = step2 ;
end
xlabel('uniaxial strain \epsilon_1_1')
ylabel('deaviatoric uniaxial stress dev(\sigma_1_1)')
title('DEVIATORIC STRESS_1_1-STRAIN_1_1 CURVE')

% % % % % Stress-time curve
% % % % figure(3)
% % % % t = [0 : delta_t : delta_t*size(stress_history,1)-delta_t] ;
% % % % plot(t,stress_history(:,1))
% % % % xlabel('t')
% % % % ylabel('\sigma_1_1')
% % % % title('Stress_1_1 - time curve')
% % % % hold on
% % % % grid on

% % % % % Deviatoric Stress-time curve
% % % % figure(4)
% % % % t = [0 : delta_t : delta_t*size(stress_history,1)-delta_t] ;
% % % % plot(t,deviatoric_stress_history(:,1))
% % % % xlabel('t')
% % % % ylabel('\sigma_1_1')
% % % % title('Deviatoric stress_1_1 - time curve')
% % % % hold on
% % % % grid on

% ****************
% END PLOTTING
% ****************