%%                        MTEX Workshop 2020
%
%%                         Exercises Day 2
%
%% Exercise 2.1
% 
%% a) 
% Generate a symmetry group that consist of three fold axis in direction
% (111) and a two fold axis perpendicular to it. Visualize the symmetry
% group using the command plot.
%
% useful commands: rotation.byAxisAngle, crystalSymmetry.byElements

rot3 = rotation.byAxisAngle(vector3d(1,1,1),120*degree);
rot2 = rotation.byAxisAngle(vector3d(1,-1,0),180*degree);


cs = crystalSymmetry.byElements([rot3,rot2])

plot(cs)

%% b)
% Turn the above symmetry into a Laue group by adding the inversion and
% plot it again.
%
% useful commands: crystalSymmetry/add, rotation.inversion

cs.add(rotation.inversion)

plot(cs.Laue,'lower')

%% c)
% Add to the above symmetry a four fold axis in direction (001). Which
% symmetry group do we obtain?

cs = cs.add(rotation.byAxisAngle(vector3d.Z,90*degree))

plot(cs)

%% d)
% Verify, that a mirroring at a plane with normal (111) is nothing else
% then a rotation around the axis (111) about 180 degree followed by an
% inversion.


%% Exercise 2.2
%
%% a) 
% Define trigonal crystal symmetry 321 and a random crystal direction.
% Compute all symmetrically equivalent crystal directions without using the
% command symmetrise and check that your result is correct.
%
% useful commands: Miller.rand


cs = crystalSymmetry('321')

m = Miller.rand(cs)

mSym = m.symmetrise
cs * m


%% b)
% Compute the angles between all symmetrically equivalent directions
%
% useful commands: angle, angle_outer, symmetrise
% useful options: noSymmetry

delta = angle_outer(mSym,mSym,'noSymmetry')./degree

%% c) 
% Find a crystal direction such that there are only two symmetrically
% equivalent directions with respect to the symmetry 321
%
% useful commands: unique
% useful options: noSymmetry

m = Miller(0,0,1,cs)
unique(m.symmetrise,'noSymmetry')


%% d) 
% Find a crystal direction such that there are only two symmetrically
% equivalent directions with respect to the symmetry 321

m = Miller(2,-1,-1,0,cs,'uvw')
unique(m.symmetrise,'noSymmetry')


%% e)
% What are the common symmetries of the point groups 321 and m-3?
% Can you do this without using the command disjoined?
% Do the common symmetries again form a point group?


cs2 = crystalSymmetry('m-3')

plot(disjoint(cs2,cs))

%% Exercises 2.3
%
% Define a crystal symmetry such that (110) is not parallel to {110}.
% Verify this using the command angle


%% Exercise 2.4
%
%% 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('112')
ori = orientation.rand(1000,cs)

histogram(ori.angle./degree)

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


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


%% 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.5
%
% 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?


[cId] = calcCluster(ori)

scatter(ori(cId==1),'axisAngle')
hold on
scatter(ori(cId==2),'axisAngle')
scatter(ori(cId==0),'axisAngle')
hold off

%%

[cId] = calcCluster(ori)

plotPDF(ori(cId==1),h,'MarkerSize',4)
hold on
plotPDF(ori(cId==2),h,'MarkerSize',4)
hold off

%%

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


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


%% 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)


%% Exercise 2.6
% 
% 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.


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


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


%% Exercise 2.7
%
%% 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 2.8
%
%% 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 2.9
%
%% a) 
% Most fundamental sectors of crystal symmetries are triangles. The
% fundamental sector of which crystal symmetry is a quadrangle? Visualize
% it.


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

