GRAIN BOUNDARIES 3D STRUCTURE

DEFINITIONS

buildLattice[{a1_,a2_,a3_},n_:5,co_:6,b_:{{0,0,0}}]:=DeleteCases[Flatten[Table[i1 a1+ i2 a2+ i3 a3+ib,{i1,-n,n},{i2,-n,n},{i3,-n,n},{ib,b}],3],v_/;Norm[v]>co]

Module[{v,vv,c,s},
rotateVV[a_,b_]:=(v=Cross[a/Norm[a],b/Norm[b]];s=Norm[v]; If[s<1*^-10,IdentityMatrix[3],c=a.b/(Norm[a]Norm[b]); vv={{0,-v[[3]],v[[2]]},{v[[3]],0,-v[[1]]},{-v[[2]],v[[1]],0}};
IdentityMatrix[3]+vv+vv.vv  (1-c)/s^2]);
rotateUt[u_,th_]:=(v=u/Norm[u];
 vv={{0,-v[[3]],v[[2]]},{v[[3]],0,-v[[1]]},{-v[[2]],v[[1]],0}};
Cos[th] IdentityMatrix[3]+Sin[th] vv+(1-Cos[th]) Transpose[{v}].{v})
]

Module[{b1,b2,pz,zmx},
srfPlot[pl_,zdir_:{0,0,1},up_:{1,0,0},cdir_:{0,0,1},{sz1_:0.3,style1a_:Blue,style1b_:White},{sz2_:0.3,style2a_:Red,style2b_:White},zcoff_:3]:=(pz=Sort[pl,(#1.zdir<#2.zdir)&];zmx=pz[[-1]].zdir;
pz=DeleteCases[pz,u_/;zmx-(u.zdir)>zcoff];
b1=up-zdir(up.zdir)/(zdir.zdir);b1=b1/Norm[b1]; b2=Cross[b1,zdir]; b2=b2/Norm[b2];
Graphics[{
{
If[cdir.#1<0,Directive[{FaceForm[Blend[{style1a,style1b},1-Exp[2 #1.cdir]]],EdgeForm[Black]}],
Directive[{FaceForm[Blend[{style2a,style2b},1-Exp[-2#1.cdir]]],EdgeForm[Black]}],Print[cdir]]
,
Disk[{#1.b2,#1.b1},If[cdir.#1<0,sz1,sz2]]
}&/@pz}])
]

Remove[coincPlot]

Module[{b1,b2,rn,rp,ni,op,np,pba,pbb,pca,pcb,pz,pza,pzb,zmx},
nearP[p_,pl_]:=(rp=pl[[1]];rn=Norm[p-rp];Do[If[(ni=Norm[p-pi])<rn,{rn,rp}={ni,pi}],{pi,pl}];rp);
rmGBOverlap[{pla_,plb_},cdir_:{0,0,1},sz_:0.3]:=(
{pca,pcb}={pla,plb};
pbb=Cases[pcb,x_/;-4sz<x.cdir<0.5*sz];
pba=Cases[pca,x_/;-0.5 sz < x.cdir<4*sz];
pca=Cases[pca,x_/;(x.cdir>-3.5*sz &&  ((np=nearP[x,pbb]; Norm[x-np]>2sz ||(x.cdir>1*^-3&& Norm[x-np]<1*^-2) || x.cdir>= - np.cdir)))];
pcb=Cases[pcb,x_/;(-x.cdir>-3.5*sz && ((np=nearP[x,pba]; Norm[x-np]>2sz ||(x.cdir<1*^-3&&Norm[x-np]<1*^-2 )|| -x.cdir> np.cdir)))];
pbb=Cases[pcb,x_/;-4sz<x.cdir<0.5*sz];
pba=Cases[pca,x_/;-0.5 sz < x.cdir<4*sz];
pca=Cases[pca,x_/;(x.cdir>-3.5*sz &&  ((np=nearP[x,pbb]; Norm[x-np]>2sz ||(x.cdir>1*^-3&& Norm[x-np]<1*^-2) || x.cdir>= - np.cdir)))];
pcb=Cases[pcb,x_/;(-x.cdir>-3.5*sz && ((np=nearP[x,pba]; Norm[x-np]>2sz ||(x.cdir<1*^-3&&Norm[x-np]<1*^-2 )|| -x.cdir> np.cdir)))];
{pca,pcb}
);
clsPlot[{pla_,plb_},zdir_:{0,0,1},up_:{0,1,0},cdir_:{1,0,0},cexp_:2,zcoff_:3,zblend_:White,sz_:0.25,stylea_:Red,styleb_:Blue]:=(
b1=up-zdir(up.zdir)/(zdir.zdir);b1=b1/Norm[b1]; b2=Cross[b1,zdir]; b2=b2/Norm[b2];
pza=Table[{{pl.zdir,pl.cdir},op=Opacity[If[cexp>0,Exp[cexp pl.cdir]/2,1]];FaceForm[Directive[{op,Blend[{stylea,zblend},1-Exp[-Abs[pl.zdir]]]}]],EdgeForm[Directive[{op,Blend[{Black,zblend},1-Exp[-Abs[pl.zdir]]]}]],Disk[{pl.b2,pl.b1},sz]},{pl,pla}];
pzb=Table[{{pl.zdir,-pl.cdir},op=Opacity[If[cexp>0,Exp[-cexp pl.cdir]/2,1]];FaceForm[Directive[{op,Blend[{styleb,zblend},1-Exp[-Abs[pl.zdir]]]}]],EdgeForm[Directive[{op,Blend[{Black,zblend},1-Exp[-Abs[pl.zdir]]]}]],Disk[{pl.b2,pl.b1},sz]},{pl,plb}];
pz=Drop[#1,1]&/@DeleteCases[Sort[Join[pza,pzb],(#1[[1,1]]<#2[[1,1]] || (Abs[#1[[1,1]]-#2[[1,1]]]<1*^-5&& #1[[1,2]]>#2[[1,2]]))&],{{_,x_},__}/;x<-zcoff];
Graphics[{
{
pz
}}]);
coincPlot[{pla_,plb_},zdir_,up_,ctol_:0.01,szc_:0.1,stylec_:Black]:=(
Graphics[{FaceForm[stylec],EdgeForm[Black],Opacity[1],
b1=up-zdir(up.zdir)/(zdir.zdir);b1=b1/Norm[b1]; b2=Cross[b1,zdir]; b2=b2/Norm[b2];
If[ctol≤0,{},
Disk[{#1.b2,#1.b1},szc]&/@DeleteCases[Flatten[Table[If[Norm[pa-pb]<ctol,pa,{}],{pa,Cases[pla,x_/;Abs[x.zdir]<szc/5]},{pb,Cases[plb,x_/;Abs[x.zdir]<szc/5]}],1],{}]
]}])
]

interactive - surfaces

interactive - tilt grain boundaries

basefcc={{1/2,1/2,0},{1/2,0,1/2},{0,1/2,1/2}};
basesc={{1,0,0},{0,1,0},{0,0,1}}/Sqrt[2];
base=basefcc;
coff=3.01;shift=0.4; mxz=1.5;
lattice=1.buildLattice[base,Ceiling[coff*3],3+coff Sqrt[2]];
col1=RGBColor[0.8,0,0];
col2=RGBColor[0.2,0.3,1.0];

Manipulate[fig=Show[
lat1=
Cases[(-{tx,ty,tz}/2+#1)&/@(lattice.Transpose[rotateUt[uref,psi*Pi/180]].(rbase=Transpose[rotateVV[utilt,{0,0,1}]]).Transpose[rotateVV[uref.rbase,{1,0,0}]].Transpose[rotateUt[{0,0,1},(phi-th/2)*Pi/180]])
,{x_,y_,z_}/;If[view=="Top",(-coff<x≤mxz&& -coff<y<coff&& -coff<z≤coff),(-coff<x≤coff&& -coff<y<coff&& -mxz<z≤1*^-3)]];
lat2=
Cases[({tx,ty,tz}/2+#1)&/@(lattice.(rbase=Transpose[rotateVV[utilt,{0,0,1}]]).Transpose[rotateVV[uref.rbase,{1,0,0}]].Transpose[rotateUt[{0,0,1},(phi+th/2)*Pi/180]] ),
{x_,y_,z_}/;If[view=="Top",(-mxz<x<coff&& -coff<y<coff&& -coff<z≤coff),(-coff<=x<coff&& -coff<y<coff&& -mxz<z≤1*^-3)]];
If[rmover,{xyz1,xyz2}=rmGBOverlap[{lat1,lat2},{1,0,0},sz],
{xyz1,xyz2}={lat1,lat2};];
If[utilt.uref≠0,Graphics[Text["Axis normal and reference plane are not perpendicular",{0,0}]],
Switch[view,"Side",
Show[clsPlot[
{xyz1,xyz2}
,{0,0,1},{0,1,0},{1,0,0},If[rtrans,1,0],mxz,White,sz,col1,col2],
If[rcoinc,coincPlot[{lat1,lat2},{0,0,1},{0,1,0},0.05,sz/3,Black],{}],
Graphics[{Line[{{0,-coff},{0,coff}}]}]
,
ImageSize->7*72]
,"Top",
Show[
clsPlot[
{{},xyz2}
,{1,0,0},{0,1,0},{1,0,0},If[rtrans,2,0],mxz,White,sz,col1,col2],
clsPlot[
{xyz1,{}}
,{1,0,0},{0,1,0},{1,0,0},If[rtrans,2,0],mxz,White,sz/2,col1,col2],
If[rcoinc,coincPlot[{lat1,lat2},{1,0,0},{0,1,0},0.05,sz/3,Black],{}],
Graphics[{Line[{{-coff,-coff},{coff,-coff},{coff,coff},{-coff,coff},{-coff,-coff}}]}]
,
ImageSize->7*72]
,"3D",Graphics3D[{
col1,
{Opacity[If[rtrans,Exp[{1,0,0}.#1*2]/2,1]],Sphere[#1,sz]}&/@xyz1,
col2,
{Opacity[If[rtrans,Exp[-{1,0,0}.#1*2]/2,1]],Sphere[#1,sz]}&/@xyz2
}]
],
ImageSize->7*72]
]
,
{{nbase,"fcc","Lattice"},{"fcc","sc"}},
{{size,3,"Surface range"},2.5,6,0.5},
{{depth,1.5,"Surface depth"},1.,4,0.25},
Button["reset",(lattice=1.buildLattice[Switch[nbase,"fcc",basefcc,"sc",basesc],Ceiling[size*3],2+size Sqrt[2]];coff=size;mxz=depth;
),ImageSize->80,Method->"Preemptive"],
{{sz,0.3,"Atom size"},0.1,0.5},
{{view,"Side","View"},{"Side","Top","3D"}},
{{rmover,False,"Remove Overlap"},{True,False}},
{{rtrans,True,"Transparency"},{True,False}},
{{rcoinc,False,"Coincidence Lattice"},{True,False}},
{{utilt,{0,0,1},"Tilt axis"},{{0,0,1}->"<0 0 1>",{1,1,1}->"<1 1 1>",{1,1,0}->"<1 1 0>",{1,-1,0}->"(1 (OverBar[1]) 0)",{2,1,1}->"<2 1 1>"}},
{{uref,{1,0,0},"Reference Plane"},{{1,0,0}->"(1 0 0)",{1,1,1}->"(1 1 1)",{1,-1,0}->"(1 (OverBar[1]) 0)",{-1,1,1}->"((OverBar[1]) 1 1)",{1,1,-2}->"(1 1 (OverBar[2]))"}},
{{phi,0,"Reference angle"},0,180},
{{th,0,"Tilt angle"},0,180},
{{psi,0,"Twist angle"},0,180},
{{tx,0"Tx"},-coff,coff},{{ty,0"Ty"},-coff,coff},{{tz,0"Tz"},-1,1},
SaveDefinitions->True
]