Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 6.79 KB | None | 0 0
  1. const a0   = 3.56679     # a0 for diamond   [A]
  2. const felE = 10000.
  3. const felAngle = 30 * pi/180
  4. const hc = 12398.42               # physical constant [eV * A]
  5.  
  6. # Useful constants
  7. const lxr    = hc / felE
  8. const lxr4pi = lxr / (4*pi)
  9.  
  10. # Defines k0 in lab frame
  11. const k0     = 2*pi/lxr * [sin(felAngle); 0; cos(felAngle)]
  12.  
  13. # mRadians calculated with XOP (could be improved)
  14. const laue_cutoff111 = 21.35e-6 * 20
  15. const laue_cutoff220 = 15.16e-6 * 20
  16. const laue_cutoff311 = 10.53e-6 * 20
  17. const laue_cutoff400 = 12.34e-6 * 20
  18.  
  19. const p111 = [
  20.              1  1  1;
  21.             -1  1  1;
  22.              1 -1  1;
  23.              1  1 -1;
  24.             -1 -1 -1;
  25.              1 -1 -1;
  26.             -1  1 -1;
  27.             -1 -1  1]
  28.  
  29. const p220 = [
  30.              2  2  0;
  31.             -2  2  0;
  32.              2  0  2;
  33.             -2  0  2;
  34.              0  2  2;
  35.              0 -2  2;
  36.             -2 -2  0;
  37.              2 -2  0;
  38.             -2  0 -2;
  39.              2  0 -2;
  40.              0 -2 -2;
  41.              0  2 -2]
  42.  
  43. function reussFit(a, invCmat, sigma_h, t, N, dalpha, i_range, j_min,
  44.                   hist_results=false, texture=false, Ph=0)
  45.     # Set up stress tensor in lab frame
  46.     S_samp = -1. * [
  47.             sigma_h-t/3  0             0;
  48.             0            sigma_h-t/3   0;
  49.             0            0             sigma_h+2*t/3]
  50.  
  51.     # Initialize arrays
  52.     data111 = Float64[]
  53.     data220 = Float64[]
  54.     data311 = Float64[]
  55.     data400 = Float64[]
  56.     samp    = Float64[]
  57.  
  58.     stre_vec = [
  59.         S_samp[1,1]; S_samp[2,2]; S_samp[3,3];
  60.         S_samp[2,3]; S_samp[3,1]; S_samp[1,2]]
  61.                          
  62.     #println("S_samp:   $S_samp")
  63.     #println("invCmat:  $invCmat")
  64.     #println("stre_vec: $stre_vec")
  65.    
  66.     # Calculate strain vector
  67.     stra_vec = invCmat * stre_vec
  68.     #println("stra_vec $stra_vec")
  69.  
  70.     for i in i_range
  71.         for j in j_min:i+1
  72.             xp, yp  = project(j,i,N)
  73.             h, k, l = normalize([j; i; N])
  74.  
  75.             push!(samp, xp, yp)
  76.  
  77.             # Calculate rotation matrix, add eps to avoid div by 0
  78.             for alpha in (0:dalpha:360) * pi/180
  79.                 RM    = fullR(alpha, h,k,l)
  80.                
  81.                 #println("RM: $RM")
  82.                
  83.                 # Transform stress tensor into crystal coordinates
  84.                 S_crys = RM * (S_samp * RM')
  85.  
  86.                # Stress vector in crystal coordinates
  87.                stress_vec = [
  88.                    S_crys[1,1]; S_crys[2,2]; S_crys[3,3];
  89.                    S_crys[2,3]; S_crys[3,1]; S_crys[1,2]]
  90.  
  91.                #println("stress_vec: $stress_vec")
  92.  
  93.                # Calculated strain of crystal vectors
  94.                e_xx, e_yy, e_zz, e_yz, e_zx, e_xy = invCmat * stress_vec
  95.  
  96.                #println("e_xx, e_yy, e_zz, e_yz, e_zx, e_xy: $e_xx, $e_yy, $e_zz, $e_yz, $e_zx, $e_xy")
  97.  
  98.                # Calculate deformed lattice vectors using strains
  99.                def_a = (1+e_xx)*a * [
  100.                        cos(e_xy)*cos(e_zx);
  101.                        sin(e_xy);
  102.                        sin(e_zx)]
  103.                def_b = (1+e_yy)*a * [
  104.                        sin(e_xy);
  105.                        cos(e_xy)*cos(e_yz);
  106.                        sin(e_yz)]
  107.                def_c = (1+e_zz)*a * [
  108.                        sin(e_zx),
  109.                        sin(e_yz),
  110.                        cos(e_zx)*cos(e_yz)]
  111.  
  112.                #println("def_a, def_b, def_c, $def_a, $def_b, $def_c")
  113.  
  114.                # Calculate compressed cell volume, compression
  115.                V    = abs(dot(def_a, cross(def_b, def_c)))
  116.                comp = a^3 / V
  117.                rnf  = 2*pi / V
  118.  
  119.                # Calculate reciprocal lattice
  120.                rda = rnf * cross(def_b, def_c)
  121.                rdb = rnf * cross(def_c, def_a)
  122.                rdc = rnf * cross(def_a, def_b)
  123.  
  124.                #println("rda, rdb, rdc: $rda, $rdb, $rdc")
  125.                
  126.                # Analyze compressed planes
  127.                for p in 1:size(p111)[1]
  128.                    d, ks, fitv = analyze_plane(p111[p,:], rda,rdb,rdc, RM)
  129.                    
  130.                    kx, ky, kz = ks
  131.  
  132.                    if fitv < laue_cutoff111
  133.                        push!(data111, fitv, d, kx, ky, kz, comp, h, k, l)
  134.                    end
  135.                end
  136.  
  137.                for p in 1:size(p220)[1]
  138.                    d, ks, fitv = analyze_plane(p220[p,:], rda,rdb,rdc, RM)
  139.  
  140.                    kx, ky, kz = ks
  141.  
  142.                    if fitv < laue_cutoff220
  143.                        push!(data220, fitv, d, kx, ky, kz, comp, h, k, l)
  144.                    end
  145.                end
  146.  
  147.                #= Skip 311 and 400 planes for now
  148.                for p in arange(len(p311)):
  149.                    [d, ks, fitv] = analyze_plane(p311[p,:], rda,rdb,rdc, RM)
  150.  
  151.                    kx,ky,kz = ks
  152.  
  153.                    if fitv < laue_cutoff311:
  154.                        data311.append([fitv, d, kx, ky, kz, comp, h, k, l])
  155.  
  156.                for p in arange(len(p400)):
  157.                    [d, ks, fitv] = analyze_plane(p400[p,:], rda,rdb,rdc, RM)
  158.  
  159.                    kx,ky,kz = ks
  160.  
  161.                    if fitv < laue_cutoff400:
  162.                        data400.append([fitv, d, kx, ky, kz, comp, h, k, l])
  163.                =#
  164.            end
  165.        end
  166.    end
  167.  
  168.    data111 = reshape(data111, (9, div(length(data111),9)))
  169.    data220 = reshape(data220, (9, div(length(data220),9)))
  170.    samp    = reshape(samp, (2, div(length(samp),2)))
  171.  
  172.    return data111, data220, samp
  173. end
  174.  
  175. function analyze_plane(hkl, rda, rdb, rdc, R)
  176.    # Return d spacing, outgoing wavevector, and fit value for plane hkl
  177.    Gp      = hkl[1]*rda + hkl[2]*rdb + hkl[3]*rdc
  178.    GpNorm  = norm(Gp)
  179.    d       = 2*pi / GpNorm
  180.  
  181.    # Convert G to lab frame
  182.    G   = R' * Gp
  183.     k   = k0 + G
  184.     fit = abs(asin(lxr4pi*GpNorm) + asin(dot(k0,G) / (2*pi/lxr*GpNorm)))
  185.  
  186.     return d, k, fit
  187. end
  188.  
  189. function project(x, y, z)
  190.     # Project (xyz) to (xy) in inverse pole figure
  191.     den = sqrt(x^2 + y^2 + z^2) + z
  192.     xp  = y/den
  193.     yp  = x/den
  194.  
  195.     return xp, yp
  196. end
  197.  
  198. function R_y(theta)
  199.     # Rotation matrix for angle theta about y axis
  200.     return [
  201.             cos(theta)    0   sin(theta);
  202.             0             1   0;
  203.             -sin(theta)   0   cos(theta)]
  204. end
  205.  
  206. function R_z(theta)
  207.     # Rotation matrix for angle theta about z axis
  208.     return [
  209.             cos(theta)   -sin(theta)   0;
  210.             sin(theta)   cos(theta)    0;
  211.             0            0             1]
  212. end
  213.  
  214. function fullR(alpha, h,k,l)
  215.     # Rotation matrix for crystallite with [hkl] normal to surface
  216.     # with rotation alpha, where alpha = 0 defined by ...
  217.     beta  = acos(l / sqrt(h^2 + k^2 + l^2))
  218.     gamma = acos(h / sqrt(h^2 + k^2 + eps()))
  219.  
  220.     return R_z(alpha) * (R_y(beta) * R_z(gamma))
  221. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement