(* Kimball model and energy derived from Facio-PDB source, ES February 2013 *) 
(* z-coords set to zero; largest was 0.015 Angstr. Vnn 778.38917 -> 778.39211 *)
Clear[tw,n2,n,nb,bc,bh,u,u1,u2,Vee,Vne,Vnn];

t1={{-5.370547274853261,-0.04221321967024272,0.}, {-2.7711146226119587,0.009677015794301802,0.}, {-1.369177799279897,-2.2598118072185427,0.} ,{-2.663248297965583,-4.5802145189189405,0.}, {-5.329865545095108,-4.598885566551149,0.}, {-6.660399809094564,-2.362362756092832,0.} ,{1.3647415417931947,-2.248107297752364,0.} ,{-1.2638747111885928,-6.9057349179865835,0.} ,{2.747592808144639,0.031906173615971145,0.} ,{2.6768534125712753,-4.5585462431542485,0.} ,{5.343538497812032,-4.555110502577383,0.} ,{1.2956568741091432,-6.8943381152995755,0.} ,{5.346926138497718,0.0008204956372210859,0.} ,{6.656456595553607,-2.3089267620424723,0.}, {-6.32826793851116,-6.414727870756178,0.} ,{-8.729566979621303,-2.3764983913869857,0.} ,{2.368867388719558,-8.666265857821955,0.} ,{6.356196635701345,-6.361998176099762,0.} ,{8.725438428913769,-2.305808469359278,0.} ,{-2.3229154966040984,-8.684578582359277,0.} ,{-1.369177799279897,2.2598118072185427,0.}, {-2.663248297965583,4.5802145189189405,0.} ,{-5.329865545095108,4.598885566551149,0.} ,{-6.660399809094564,2.362362756092832,0.}, {1.3647415417931947,2.248107297752364,0.} ,{-1.2638747111885928,6.9057349179865835,0.} ,{2.6768534125712753,4.5585462431542485,0.} ,{5.343538497812032,4.555110502577383,0.}, {1.2956568741091432,6.8943381152995755,0.}, {6.656456595553607,2.3089267620424723,0.} ,{-6.32826793851116,6.414727870756178,0.} ,{-8.729566979621303,2.3764983913869857,0.} ,{2.368867388719558,8.666265857821955,0.} ,{6.356196635701345,6.361998176099762,0.} ,{8.725438428913769,2.305808469359278,0.} ,{-2.3229154966040984,8.684578582359277,0.}};

n=Length[t1]

u= {"C","C","C","C","C","C","C","C","C","C","C","C","C","C","H","H","H","H","H","H","C","C","C","C","C","C","C","C","C","C","H","H","H","H","H","H"};

un=Table[If[u[[i]]=="H","Z",u[[i]]],{i,1,n}]

num=Table[i,{i,1,n}];

um=Table[{un[[i]],num[[i]]},{i,1,n}];

us=Sort[um]

num=Table[us[[i,2]],{i,1,n}]

For[i=1,i<n+1,i++,If[us[[i,1]]=="Z",us[[i,1]]="H"]];

u1=Table[If[us[[i,1]]=="C",0.255,If[us[[i,1]]=="H",0.1,If[us[[i,1]]=="N",0.24,If[us[[i,1]]=="O",0.178,If[us[[i,1]]=="Cl",0.5]]]]],{i,1,n}]
u2=Table[If[us[[i,1]]=="C",0.25713,If[us[[i,1]]=="H",0.6,If[us[[i,1]]=="N",0.22522,If[us[[i,1]]=="O",0.178,If[us[[i,1]]=="Cl",0.5]]]]],{i,1,n}]

u=Table[us[[i,1]],{i,1,n}]

cn=Table[If[u[[i]]=="C",6.0,If[u[[i]]=="H",1.0,If[u[[i]]=="N",7.0,If[u[[i]]=="O",8.0,If[u[[i]]=="Cl",17.0]]]]],{i,1,n}]

bc=0;
For[i=1, i<n+1,i++,If[u[[i]]≠"H", bc=bc+1]];
bc
bh=n-bc

DeleteFile[ "C:\\Kimb_Mathem\\tmp3"];
For[i=1,i<n+1,i++,
{u[[i]],Extract[t1,{i}]}>>> "C:\\Kimb_Mathem\\tmp3"]

t=Table[t1[[num[[i]]]],{i,1,n}];

x=Table[t[[i,1]],{i,1,n}];
y=Table[t[[i,2]],{i,1,n}];
z=Table[t[[i,3]],{i,1,n}];
m=Table[If[u[[i]]=="C",12.0107,If[u[[i]]=="H",1.00794]],{i,1,n}];
M=Sum[m[[i]],{i,n}]

(*coords=(m.t)/M*)
no={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
coords=(no.t)/n
x=Table[t[[i,1]]-coords[[1]],{i,1,n}]; 
y=Table[t[[i,2]]-coords[[2]],{i,1,n}];
z=Table[t[[i,3]]-coords[[3]],{i,1,n}];
t=Table[{x[[i]],y[[i]],z[[i]]},{i,1,n}];

(*w3=Sqrt[3.0]/2; ws=0.5;
x=Table[t[[i,1]]*w3-ws*t[[i,2]],{i,1,n}]; 
y=Table[t[[i,1]]*ws+w3*t[[i,2]]-2.8727498,{i,1,n}];
t=Table[{x[[i]],y[[i]],z[[i]]},{i,1,n}];
MatrixForm[t]*)

coords=(m.t)/M

Plot1=Table[Graphics3D[{If[u[[i]]=="N",Lighter[Blue],If[u[[i]]=="O",Lighter[Red],If[u[[i]]=="Cl",Lighter[Green],If[u[[i]]=="B",Lighter[Orange],Lighter[Brown]]]]],Sphere[{t[[i,1]],t[[i,2]],t[[i,3]]},u1[[i]]]}],{i,1,n}];
Plot2=Table[Graphics3D[Text[i,{t[[i,1]]-0.6,t[[i,2]],t[[i,3]]} ,{1,0}]],{i,1,n}];
dc=Table[Sqrt[(t[[i,1]]-t[[j,1]])^2+(t[[i,2]]-t[[j,2]])^2+(t[[i,3]]-t[[j,3]])^2],{i,1,n},{j,1,n}];

(* Distance Matrix Bohr *)
Dij=Table[If[i≥j,If[(dc[[i,j]]<3.0 ||(u[[i]]=="Cl" && j==10 && i== 17)),dc[[i,j]],0],0],{i,1,n},{j,1,n}];
MatrixForm[Dij]

For[i=1 ,i<n+1,i++,
For[j=1, j<i,j++,
If[(Dij[[i,j]]≠0 && u[[i]]== "H"),If[u[[j]]=="C",u2[[i]]=0.5693,If[u[[j]]=="F",u2[[i]]=0.5,If[u[[j]]=="N",u2[[i]]=0.5541,If[u[[j]]=="O",u2[[i]]=0.5241]]]]]]]
u2

Fij=Dij-u2;
(*MatrixForm[Fij];*)
Gij=Table[If[i≥j,If[Fij[[i,j]]>0,Fij[[i,j]],0]," "],{i,1,n},{j,1,n}];
(*MatrixForm[Gij];*)

Hij=Transpose[Gij];
(*MatrixForm[Hij];*)

Kij=Hij-u2;
(*MatrixForm[Kij];*)
Lij=Table[If[i≤j,If[Kij[[i,j]]>0,Kij[[i,j]],0]," "],{i,1,n},{j,1,n}];
(*MatrixForm[Lij];*)

Lij=Table[If[i≤j,If[Lij[[i,j]]>0,If[u[[j]]≠"H",0.5*Lij[[i,j]],Lij[[i,j]]],0]," "],{i,1,n},{j,1,n}];  
Qij=Lij;
(*MatrixForm[Lij];*)

Nij=Table[If[i≤j,If[Lij[[i,j]]>0,2.25/Lij[[i,j]]^2,0]," "],{i,1,n},{j,1,n}];
(*MatrixForm[Nij]*)

Tval=Sum[If[i≤j,Nij[[i,j]],0],{i,1,n},{j,1,n}]

Tcor=Sum[If[u[[i]]≠"H",2.25/u2[[i]]^2,0],{i,1,n}]

Ttot=Tval+Tcor  (* plus π and lone pairs: + 8.7616 *)

TvalH=Sum[If[(i≤j && u[[j]]== "H"),Nij[[i,j]],0],{i,1,n},{j,1,n}]

TvalC=Tval-TvalH

Mij=Prepend[Qij,u];
Oij=Transpose[Mij];
Pij=Prepend[Oij,Prepend[u," "]];

MatrixForm[Pij]

(* sigma bond cloud radii *)

For[i=2,i<n+2,i++,
For[j=2,j≤i,j++,
If[(Pij[[i,j]]>0 && ( u[[i-1]]=="O" ||  u[[j-1]]=="O" )) ,Print[u[[i-1]],i-1," ",u[[j-1]],j-1,"  ",Extract[Pij,{i,j}]]]]]

DeleteFile[ "C:\\Kimb_Mathem\\tmp4"];
For[i=2,i<n+2,i++,
For[j=2,j≤i,j++,
If[Pij[[i,j]]>0,{i-1,j-1,Extract[Pij,{i,j}]}>>> "C:\\Kimb_Mathem\\tmp4"]]]

a=ReadList["C:\\Kimb_Mathem\\tmp4"]
nb=Length[a]
cw=nb-bh

wxc=Table[(a[[i,3]]+u2[[a[[i,1]]]])/(u2[[a[[i,1]]]]+2*a[[i,3]]+u2[[a[[i,2]]]])*(x[[a[[i,2]]]]-x[[a[[i,1]]]])+x[[a[[i,1]]]],{i,1,cw}];
wyc=Table[(a[[i,3]]+u2[[a[[i,1]]]])/(u2[[a[[i,1]]]]+2*a[[i,3]]+u2[[a[[i,2]]]])*(y[[a[[i,2]]]]-y[[a[[i,1]]]])+y[[a[[i,1]]]],{i,1,cw}];   
wzc=Table[(a[[i,3]]+u2[[a[[i,1]]]])/(u2[[a[[i,1]]]]+2*a[[i,3]]+u2[[a[[i,2]]]])*(z[[a[[i,2]]]]-z[[a[[i,1]]]])+z[[a[[i,1]]]],{i,1,cw}];

Plot7=Table[Graphics3D[{Darker[Gray,0.8],{Opacity[0.4],If[wxc[[i]]≠0,Sphere[{wxc[[i]],wyc[[i]],wzc[[i]]},a[[i,3]]]]}}],{i,1,cw}];

wxh=Table[(a[[i,3]]+u2[[a[[i,2]]]])/(u2[[a[[i,1]]]]+a[[i,3]]+u2[[a[[i,2]]]])*(x[[a[[i,1]]]]-x[[a[[i,2]]]])+x[[a[[i,2]]]],{i,cw+1,nb}];
wyh=Table[(a[[i,3]]+u2[[a[[i,2]]]])/(u2[[a[[i,1]]]]+a[[i,3]]+u2[[a[[i,2]]]])*(y[[a[[i,1]]]]-y[[a[[i,2]]]])+y[[a[[i,2]]]],{i,cw+1,nb}];
wzh=Table[(a[[i,3]]+u2[[a[[i,2]]]])/(u2[[a[[i,1]]]]+a[[i,3]]+u2[[a[[i,2]]]])*(z[[a[[i,1]]]]-z[[a[[i,2]]]])+z[[a[[i,2]]]],{i,cw+1,nb}];

Plot8=Table[Graphics3D[{Opacity[0.5],Sphere[{wxh[[i]],wyh[[i]],wzh[[i]]},a[[i+cw,3]]]}],{i,1,bh}];

rpo=1.4;
px=Table[x[[i]],{i,1,bc}];
py=Table[y[[i]],{i,1,bc}];
pz=Table[z[[i]]+rpo,{i,1,bc}];
Plot8a=Table[Graphics3D[{Opacity[0.3],Lighter[Cyan],Sphere[{px[[i]],py[[i]],pz[[i]]},rpo]}],{i,1,bc}];
rpi=1.4;
qx=Table[x[[i]],{i,1,bc}];
qy=Table[y[[i]],{i,1,bc}];
qz=Table[z[[i]]-rpi,{i,1,bc}];
Plot8b=Table[Graphics3D[{Opacity[0.3],Lighter[Yellow],Sphere[{qx[[i]],qy[[i]],qz[[i]]},rpi]}],{i,1,bc}];

Plot9= Table[Graphics3D[{(*Darker[Red,0.8],*)Tube[{{x[[a[[i,1]]]],y[[a[[i,1]]]],z[[a[[i,1]]]]},{x[[a[[i,2]]]],y[[a[[i,2]]]],z[[a[[i,2]]]]}},0.15]}],{i,1,nb}];

Plot10=Graphics3D[{Darker[Red,0.5],Sphere[{coords[[1]],coords[[2]],coords[[3]]},0.2]}];

Show[Plot1,Plot7,Plot8,Plot8a,Plot8b,Plot9,{AspectRatio->Automatic,Boxed->False, SphericalRegion->True}]
Show[Plot1,Plot2,Plot9,Plot10,{AspectRatio->Automatic,Axes->Automatic,Boxed->True, SphericalRegion->True}]

tc=Table[{x[[i]],y[[i]],z[[i]]},{i,1,bc}];   (* core clouds *)
twc=Table[{wxc[[i]],wyc[[i]],wzc[[i]]},{i,1,cw}];  (* Bond cöouds between cores *)
twh=Table[{wxh[[i]],wyh[[i]],wzh[[i]]},{i,1,bh}]; (* H-bonding clouds *)
tpo=Table[{px[[i]],py[[i]],pz[[i]]},{i,1,bc}]; (* pi clouds above *)
tpu=Table[{qx[[i]],qy[[i]],qz[[i]]},{i,1,bc}]; (* pi clouds below *) 
tw=Join[tc,twc,twh,tpo,tpu];
bt=Length[tw]
MatrixForm[tw]
(* coordinates of clouds *)

(* cloud-cloud distances *)
dw=Table[Sqrt[(tw[[i,1]]-tw[[j,1]])^2+(tw[[i,2]]-tw[[j,2]])^2+(tw[[i,3]]-tw[[j,3]])^2],{i,1,bt},{j,1,bt}];
(*MatrixForm[dw]*)

(* nucleus-nucleus distances *)
dc=Table[Sqrt[(t[[i,1]]-t[[j,1]])^2+(t[[i,2]]-t[[j,2]])^2+(t[[i,3]]-t[[j,3]])^2],{i,1,n},{j,1,n}];

(* nucleus-nucleus distances *)
(*MatrixForm[dc] *)

tv1=Table[{x[[i]],y[[i]],z[[i]]},{i,1,bc}];
tv2=Table[{0,0,0},{i,1,cw}];
tv3= Table[{x[[i]],y[[i]],z[[i]]},{i,bc+1,n}];
tv4 =Table[{0,0,0},{i,1,bc}]; 
tv5 =Table[{0,0,0},{i,1,bc}]; 
tv  = Join[tv1,tv2,tv3,tv4,tv5];
MatrixForm[tv]

ow=Join[Table[-2.0,{i,1,bc+nb}],Table[-0.5,{i,1,2*bc}]](* nucleus-cloud distances *)
cnv=Join[Table[cn[[i]],{i,1,bc}],Table[0.0,{i,1,cw}],Table[cn[[i]],{i,bc+1,n}],Table[0.0,{i,1,2*bc}]]

dcw=Table[sq=Sqrt[(tw[[i,1]]-tv[[j,1]])^2+(tw[[i,2]]-tv[[j,2]])^2+(tw[[i,3]]-tv[[j,3]])^2];If[sq> 0.1,1/sq,0],{i,1,bt},{j,1,bt}];
dcw=Table[If[i== j,dcw[[i,j]]=0.0,dcw[[i,j]]],{i,1,bt},{j,1,bt}];
(*MatrixForm[dcw]*)

(*cnv=Join[Table[cn[[i]],{i,1,bc}],Table[0.0,{i,1,cw}],Table[cn[[i]],{i,bc+1,n}]]*)

Vee=0.0; Vnn=0.0;

(* all clouds repulsion *)
For[i=1,i<bt,i++,
For[j=i+1,j<bt+1,j++,
Vee=Vee+ow[[i]]*ow[[j]]/dw[[i,j]]]]
Vee
(* half cloud with other common halfcloud: subtract WW *)
For [i=nb+bc+1,i<nb+2*bc+1,i++,Vee=Vee-ow[[i]]*ow[[i+bc]]/dw[[i,i+bc]]];
Vee

sc=0.3625;   (* core clouds *)
For[i=1 ,i<bc+1,i++,Vee=Vee+3*sc/u2[[i]]]
sw=0.3624;  (* bonding clouds *)
For[i=1 ,i<nb+1,i++,Vee=Vee+3*sw/a[[i,3]]]
Vee

(* all nuclei with all clouds, except i = j *)
Vne=cnv.(ow.dcw)

(* core nuclei in their cloud centers *)

For[i=1,i<bc+1,i++,Vne=Vne-3*cn[[i]]/u2[[i]]]
Vne

(* protons in their bonding cloud, distance u2[[a[[i,1]]]] to cloud center *)
For[i=cw+1,i<nb+1,i++,Vne=Vne-(3-(u2[[a[[i,1]]]]/a[[i,3]])^2)/a[[i,3]]]
Vne

(* nucleus-nucleus *)
For[i=1,i<n,i++,
For[j=i+1,j<n+1,j++,Vnn=Vnn+cn[[i]]*cn[[j]]/dc[[i,j]]]]
Vnn

Vee
Vnn
Vne
Tpi = 2.296*bc*0.5*1.125*(1/rpo^2+1/rpi^2) (* pi kin e added *)
Vtot=Vne+Vee+Vnn
Ttot = Ttot+Tpi
-Vtot/Ttot
Etot=Ttot+Vtot