%%                        MTEX Workshop 2020
%
%%                         Exercises Day 2
%
%% Exercise 1
%
%% a)
% Generate 1000 random orientations with symmetry 112. Compute the
% rotational angle of all these orientations and display the coresponding
% histogram.
%
% useful commands: histogram, orientation.rand

cs = crystalSymmetry('222')
ori = orientation.rand(10000,cs)

histogram(ori.angle./degree)

%% b)
% What is the maximum rotational angle? What is the corresponding axis?


oR = cs.fundamentalRegion;

plot(oR)

oR.maxAngle ./ degree

oR.V.angle./ degree
oR.V.axis

%% c)
% Plot the position of the {111} planes with respect to the random
% orientations from a) without using the command plotPDF.


h = Miller(1,1,1,cs)
plotPDF(ori,h)



%% d)
% Visualize the random orientations from a) in the Z - inverse pole figure
% without using the command plotIPDF.
%
% usefull options: fundamentalSector


plot(inv(ori) * vector3d.Z,'fundamentalSector')


%% d)
% Redo a) - d) for the symmetries 222 and 432.


%% Exercise 2
%
% Consider orthorhombic crystal symmetry and inspect the following pole
% figures:

cs = crystalSymmetry('222');

ori = secret('2_5'); % some secret orientations

% plot pole figures
h = Miller({0,0,1},{0,1,0},{1,0,0},{1,1,0},{1,1,1},cs)
plotPDF(ori,h,'MarkerSize',4)

%% a)
% How many prefered orientations are displayed? Why the number of spots
% differes so much between the different pole figures?
%
% useful commands: Miller/multiplicity, calcCluster


h.multiplicity


cId = calcCluster(ori);

scatter(ori,ind2color(1+cId),'axisAngle')

%%

plotPDF(ori,ind2color(1+cId),h,'MarkerSize',4)

%%

close all
plot(h(1).symmetrise,'upper')
hold on
for i = 2:length(h)
  plot(h(i).symmetrise)
end
hold off


%% b)
% Determine approximately the prefered orientation(s) and annotate them to
% the pole figure.


[cId,cMean] = calcCluster(ori);

annotate(cMean,'add2all')


%% c)
% Replace the crystal symmetry by the corresponding Laue group? Plot the
% pole figures. How many poles do you expect and how many do you observe?


plotPDF(ori,h,'antipodal','MarkerSize',4)

h.CS = h.CS.Laue

h.multiplicity


%% Exercise 3
% 
% Consider cubic crystal symmetry and inspect the following inverse pole
% figures:

cs = crystalSymmetry('m-3m')
ori = secret('2_6'); % some secret orientations

plotIPDF(ori,[vector3d.Z,vector3d.X],'MarkerSize',3)


%% a)
% Determine approximately the preferred orientation.

o = orientation.map(Miller(0,0,1,cs),vector3d.Z,Miller(1,1,0,cs),vector3d.X)

plotIPDF(o,[vector3d.Z,vector3d.X])

mean(angle(ori,o)./degree)


%% b)
% What is the percentage of orientations within a 5 degree ball around the
% preferrd orientation?


o = mean(ori)

volume(ori,o,5*degree) * 100


%% c)
% Visualize those orientations in 3d Euler angle space, in 3d axis angle
% space and in phi2 sections.


plot(ori,'axisAngle','filled')



%% Exercise 4
%
%% a) 
% Consider hexagonal symmetry with two different setups x||a and x||a*.
% Define the orientatation (20,30,40) with respect to both setups and
% visualize them in the three dimensional Euler angle space and the 
% three dimensional axis angle space. What do you observe?

cs1 = crystalSymmetry('622','x||a');
cs2 = crystalSymmetry('622','x||a*');

ori1 = orientation.byEuler(10*degree,20*degree,30*degree,cs1)
ori2 = orientation.byEuler(10*degree,20*degree,30*degree,cs2)

scatter(ori1,'linewidth',3)
hold on
scatter(ori2,'linewidth',3)
hold off

%%

scatter(ori1,'axisAngle','linewidth',3)
hold on
scatter(ori2,'axisAngle','linewidth',3)
hold off

%% b)
% Visualize both orientations in a pole figure plot. Are those the same or
% different? Why?

h1 = Miller({1,0,-1,0},{1,1,-2,1},cs1);
plotPDF(ori1, h1)
nextAxis
h2 = Miller({1,0,-1,0},{1,1,-2,1},cs2);
plotPDF(ori2, h2)

%% c)
% Visualize both orientations in an inverse pole figure plot. Are those the
% same or different? Why?

r = [vector3d.X, vector3d(1,1,1)];
plotIPDF(ori1, r)
nextAxis
plotIPDF(ori1, r)


%% d)
% Define the crystal direction (11-21) for both setups and compute the
% resulting specimen directions for any combination of setups, i.e., setup
% 1/2 for the orientation times setup 1/2 for the crystal direction. What
% does the resulting specimen direction depend on? Why?

ori1 * h1(1)
ori1 * h2(1)
ori2 * h1(1)
ori2 * h2(1)

%% e)
% Define the orientation (11-21)[1-100] with respect to both setups. Are
% they the same?

ori1 = orientation.byMiller({1,1,-2,1},{1,-1,0,0},cs1)
ori2 = orientation.byMiller({1,1,-2,1},{1,-1,0,0},cs2)

ori2.transformReferenceFrame(cs1)


%% Exercise 5
%
%% a)
% Consider cubic crystal symmetry and orthorhombic specimen symmetry and
% define the Brass, Goss, Cube and Copper standard orientations.

%% b)
% Determine the multiplicity of each of the standard orientations.

%% c)
% Visualize the orientatios in the different 3d and 2d orientation
% representations. How is the multiplicity related to the geometrical
% position within the orientation space?

%% Exercise 6
%
%% a) 
% Most fundamental sectors of crystal symmetries are triangles. The
% fundamental sector of which crystal symmetry is a quadrangle? Visualize
% it.

cs = crystalSymmetry('23')

plot(cs)
hold on
plot(cs.fundamentalSector,'Color','orange','LineWidth',2)
hold off


%% b)
% In fact the fundamental sector for this symmetry could by replaced by a
% triangle. Which one? Define and visualize it.


%% Exercise 7
% 
%% a) 
% Inspect the orientations of the MTEX Titanium sample data set.

mtexdata titanium

ori = ebsd.orientations

%% b)
% Compute an ODF and visualize it by a c-axis pole figure and sigma
% sections.
%
% useful commands: calcODF

odf = calcODF(ori)

plotPDF(odf,odf.CS.cAxis)


%% c)
% Repeat (b) with differet kernel halfwidths. Which halfwidth would you
% think to be optimal?




%% d)
% Determine "optimal" kernel using the function calcKernel with different
% methods, i.e., ruleOfThumb, magicRule, default. Compare with your choice
% from (b).



%% e)
% You should have recogniczed that the halfwidth estimated in d) is much
% smaller than your choice from c). The reason is, that calcKernel assumes
% the orientations as spatialy independent - which they are clearly not in
% the case of grains. As a way out you may do the following:
%
% (i)  reconstruct grains
% (ii) Determine "optimal" kernel using the function calcKernel but from
% the grain meanorientations instead of the EBSD orientations.

%% f)
% Compute the textureindex of the reconstructed ODF. Make a plot that shows
% the dependency between the halfwidth used for ODF estimation and the
% texture index of the resulting ODF.


%% g)
% Compute a density function from the c-axis poles of the origial EBSD data
% and compare it with the pole density function computed from the ODF.


%% i)
% Compute a random sample of this c-axis pole density function and check
% how close they are to the original sampling.



%% Exercise 8
%
%% a)
% Import the Dubna Neutron test data and inspect them visually.

mtexdata dubna

plot(pf)

%% b)
% In each pole figure there are two theta angles for which the intensities
% do not fit very well to the other theta angles. Identify these circles
% and remove them from the data.
%
% useful commands: pf.r, pf.r.theta


%% c)
% Reconstruct an ODF from the neutron data and visualize compare its pole
% density functions with the measured data.
%
% useful commands: ODF/plotPDF


%% d)
% Plot the c-axis pole figure from the reconstructed ODF. Compute and mark
% the prefered direction of the c-axis.


%% e)
% Determine volume percentage of c-axes orientations within a 10 degree
% ball round the prefered c-axis direction. Compare the percentage with an
% untextured ODF.

%% f)
% Visualize the ODF in sigma sections and compare these with the c-axis
% pole figure. 


%% g)
% Find two prefered orientations within the ODF.
%
% useful commands: ODF/max


%% h)
% Try to approximate the reconstructed ODF by an model ODF consisting of 3
% components only. 
%
% useful commands: unimodalODF, uniformODF, fibreODF, binghamODF, 


%% i)
% Determine the error between the ODF computed from the neutron data and
% your approximation in h).
%
% useful commands: ODF/calcError



