%% Import Script for EBSD Data
%
% 1. Import EBSD data for Nickel sample directly into workspace
%    as MatlabNickel_sample10_scan3_EBSD.mat
%
% 2. Plot EBSD map with orientation colorbar using IPF with sample X
%
% 3. Define elastic Nickel single crystal compliance tensor (S_ijkl) and
%    convert it to stiffness tensor (C_ijkl)
%
% 4. Define tensor using MTEX tensor command
%
% 5. Calculate polycrystaline elastic property averages Voigt, Ruess and Hill
%    a) EBSD individual orientations
%       Hint : calcTensor(ebsd('Nickel').orientations,C)
%
%    b) ODF continous function with Dirichlet Kernel Band-width = 4
%       Hint : (a) calcODF(ebsd('Nickel').orientations,'kernel',DirichletKernel(4))
%              (b) calcTensor(ODF_Dirichlet,C)
%
%    c) ODF continous function with de Vallee Poussin kernel with Halfwidth 2 degrees
%       Hint : (a) calcODF(ebsd('Nickel').orientations,'halfwidth',2*degree)
%              (b) calcTensor(ODF_Vallee_Poussin,C)
%
% 6. Calculate difference of stiffness tensors (C_ijkl) given by EBSD and ODFs
%       for Voigt, Ruess and Hill
%
% 7. Compare calculation times
%       Hint: Tic .... Toc
%
% 8. Calculate the isotropic elastic tensor for Nickel sample 
%       Hint : use a specific MTEX ODF with calcTensor
%
% 9. Calculate the mean value of Young's modulus as (max + min) /2
%       Hint : E = YoungsModulus(C,x)
%
% 10. Is the mean physical property always representitative ?
%
% 11. Calculate the Series 'Hill' average - what is this ?
%
% 12. Calculate the Young's modulus in a specific direction or plane
%
% Script by David Mainprice 22/02/2016
%
% Super-alloy IN739LC EBSD data from Karsten Kunze
%
% Reference publication from Karsten Kunze (ETH,Zurich)
%
% K.Kunze, T.Etter, J.Grässlin, V.Shklover (2014)
% Texture,anisotropy in microstructure and mechanical properties
% of IN738LC alloy processed by selective laser melting
% Materials Science & Engineering A620 213-222.
%
%% Clear memory and close all plots
clear all;
close all;
%% load EBSD data into workspace (EBSD data for Nickel)
% *** change the path for *.mat of your PC's ***
load /MatLab_Programs/Karsten_Chemnitz_Feb2015/Nickel_SLM/Nickel_sample10_scan3_EBSD.mat
%% plotting convention
setMTEXpref('xAxisDirection','north');
setMTEXpref('zAxisDirection','outOfPlane');
%% orientation map one phase with IPF colorbar for r = xvector
close all
figure
% select IPF colorbar for specific phase (a,b,c,alpha,beta,gamma) and symmetry
oM = ipdfHSVOrientationMapping(ebsd('Nickel'))
% define the SPECIMEN DIRECTION (r) for IPF orientation colormap
oM.inversePoleFigureDirection = xvector;
plot(ebsd('Nickel'),oM.orientation2color(ebsd('Nickel').orientations),'figSize','large')
%% Orientation IPF colour bar plot - Cubic with Nickel symmetry and r = x = 1,0,0
figure
plot(oM)
%%
%***********************************************************************
% Define elastic stiffness tensor of Super-alloy IN739LC
%***********************************************************************
% Reference
% Hana,J.,  Albrecht Bertrama, A., , Olschewskia, J., Hermann, W. and Socke, H-G.(1995)
% Identification of elastic constants of alloys with sheet and fibre textures
% based on resonance measurements and finite element analysis
% Materials Science and Engineering A191, 105-111.
%
% Enter tensor as 6 by 6 matrix, S_ijkl compliance in Pascals (1/Pa) line by line.

S = [[  8.249e-12  -3.182e-12 -3.182e-12   .00         .00         .00];...
     [ -3.182e-12   8.249e-12 -3.182e-12   .00         .00         .00];...
     [ -3.182e-12  -3.182e-12  8.249e-12   .00         .00         .00];...
     [   .00         .00        .00       8.161e-12    .00         .00];...
     [   .00         .00        .00        .00        8.161e-12    .00];...
     [   .00         .00        .00        .00         .00        8.161e-12]];
% Invert compliance to Stiffness C_ijkl and convert to GPa


% Define Cartesian Tensor crystal for elastic stiffness C_ijkl symmetry & frame
CS_Tensor_IN739LC = ebsd('Nickel').CS

% Define elastic stiffness tensor Cijkl with MTEX load tensor command

%**************************************************************************
%% Plot Young's modulus
%**************************************************************************
figure('position',[0 0 500 400])
plot(C_IN739LC,'plotType','YoungsModulus','3d','complete');
colorbar
saveFigure('Nickel_SC_youngs_Modulus_3d.pdf')
%***********************************************************************
%% Average tensors for single phases from corrected EBSD data
%***********************************************************************
%
%              NORTH
%                Y
%                |       
%         ----------------
%         |AGGREGATE  Vp |
%   WEST  |PHYSICAL   Vs1|--> X EAST 
%         |PROPERTIES Vs2|
%         ----------------
%             SOUTH
%% close all plots
close all;
%%
%**************************************************************************
% EBSD route using individual measurements
%**************************************************************************
% 100% Super-alloy IN739LC polycrystal : Voigt,Hill and Reuss averages
%
fprintf(' \n')
fprintf(' 100%% Super-alloy IN739LC : Voigt, Hill and Reuss averages : EBSD route using individual measurements \n')
fprintf(' \n')
%
tic


% Timing
EBSD_timing = toc;
%%
%
%**************************************************************************
% Harmonic ODF route using Dirichlet Kernel
%**************************************************************************
% 100% Super-alloy IN739LC polycrystal : Voigt,Hill and Reuss averages
%
fprintf(' \n')
fprintf(' 100%% Super-alloy IN739LC : Voigt, Hill and Reuss averages : Harmonic ODF route with Dirichlet Kernel Band-width = 4\n')
fprintf(' \n')
%
% calculate ODF using Dirichlet Kernel Band-width = Lmax = 4 for elastic properties (tensor 4th rank)
tic
%

%

% Timing
Harmonic_Dirichlet_Kernel_BW4_timing = toc;
%%
fprintf(' \n')
fprintf(' 100%% Super-alloy IN739LC : Voigt, Hill and Reuss averages : difference tensors (C_EBSD - C_ODF Dirichlet Kernel BW=4) \n')
fprintf(' \n')


%%
%**************************************************************************
% Harmonic ODF route using De Vallee Poussin kernel
%**************************************************************************
% 100% Super-alloy IN739LC polycrystal : Voigt,Hill and Reuss averages
%
fprintf(' \n')
fprintf(' 100%% Super-alloy IN739LC : Voigt, Hill and Reuss averages : Harmonic ODF route with Vallee Poussin kernel HW=2 degrees\n')
fprintf(' \n')
%
% De Vallee Poussin kernel with Halfwidth 2 degrees Band width = 206 for elastic properties (tensor 4th rank)
%
% Use standard ODF with HW = 2 degrees
tic

%

% Timing
Harmonic_De_Vallee_Poussin_Kernel_HW2_timing = toc;
%%
fprintf(' \n')
fprintf(' 100%% Super-alloy IN739LC : Voigt, Hill and Reuss averages : difference tensors (C_EBSD - C_ODF Poussin kernel with Halfwidth 2 degrees) \n')
fprintf(' \n')



%% Calculation timings
fprintf(' \n')
fprintf(' Number of individual EBSD orientations = %i \n',length(ebsd('Nickel').orientations))
fprintf(' \n')
fprintf(' \n')
fprintf(' EBSD route timing (seconds) = %6.2f \n',EBSD_timing)
fprintf(' \n')
fprintf(' \n')
fprintf(' Harmonic ODF route with Dirichlet Kernel BW=4 timing (seconds) = %6.2f \n',Harmonic_Dirichlet_Kernel_BW4_timing)
fprintf(' \n')
fprintf(' \n')
fprintf(' Harmonic ODF route with Vallee Poussin kernel HW=2 timing (seconds) = %6.2f \n',Harmonic_De_Vallee_Poussin_Kernel_HW2_timing)
fprintf(' \n')
%% Default MTEX colormap - jet colormap begin with white
setMTEXpref('defaultColorMap',WhiteJetColorMap);
%
disp(' ')
disp(' Script has successfully terminated')
disp(' ')
%
% End of script
%

