(* Kimball model and energy derived from experimental molecule data source, ES FePenary 2013 *) 
Clear[tw,n2,n,nb,bc,bh,u,u1,u2,Vee,Vne,Vnn];

ChemicalData["Nonane","MoleculePlot"]

(*t1=0.01889726*ChemicalData["Nonane","AtomPositions"]*)
(*MatrixForm[t1]*)
t1=1.889726*{{-0.0170907674,-0.2934505208,0.1753687138},{-1.1251660533,0.1953284034,-0.7553053207},{1.1821185436,0.6506280639,0.2328314898},{-2.3250475755,-0.7477732278,-0.8105383079},{2.2895164985,0.1613368322,1.1635034353},{-3.4355992160,-0.2596249378,-1.7379123231},{3.4871841592,1.1063781650,1.2270913541},{-4.6285164248,-1.2108475470,-1.7840796011},{4.5871644985,0.6057252427,2.1595417743},{0.3188224460,-1.2870958223,-0.1510663159},{-0.4246586828,-0.4233085228,1.1872041671},{-1.4601254199,1.1893501400,-0.4291949957},{-0.7180806285,0.3245146739,-1.7674111045},{0.8461124882,1.6440415152,0.5596963476},{1.5899248961,0.7809819809,-0.7787656913},{-1.9909206038,-1.7416280009,-1.1381703923},{-2.7306855894,-0.8782336931,0.2019999184},{2.6282275664,-0.8304884266,0.8345224791},{1.8809084968,0.0268589708,2.1743359722},{-3.7682384675,0.7330197808,-1.4092171251},{-3.0289950132,-0.1291421364,-2.7487139107},{3.1469165775,2.0960400357,1.5566778752},{3.8932984999,1.2409481896,0.2168057334},{-4.3235596535,-2.2005912919,-2.1380120540},{-5.0680086398,-1.3331005128,-0.7893045574},{-5.4107113137,-0.8409462639,-2.4534668974},{4.2090749993,0.4888371934,3.1798886627},{4.9638402525,-0.3675386788,1.8298798064},{5.4323351266,1.2996903955,2.1918738676}};
MatrixForm[t1]

(*ChemicalData["Nonane","BondTally"]*)

u=ChemicalData["Nonane","VertexTypes"]

n=Length[u]

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]]]],{i,1,n}]
u2=Table[If[us[[i,1]]=="C",0.255,If[us[[i,1]]=="H",0.6,If[us[[i,1]]=="N",0.22522,If[us[[i,1]]=="O",0.178]]]],{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]]]],{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"]

ChemicalData["Nonane","MolecularFormulaString"]

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

Plot1=Table[Graphics3D[{If[u[[i]]=="N",Lighter[Blue],If[u[[i]]=="O",Lighter[Red],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.3,t[[i,2]],t[[i,3]]} ,{1,0}]],{i,1,n}];

ChemicalData["Nonane","CHStructureDiagram"]

x=Table[t[[i,1]],{i,1,n}];
y=Table[t[[i,2]],{i,1,n}];
z=Table[t[[i,3]],{i,1,n}];

(* Distance matrix Bohr *)
Dij=Table[If[i≥j,If[sq=Sqrt[(x[[i]]-x[[j]])^2+(y[[i]]-y[[j]])^2+(z[[i]]-z[[j]])^2]; sq<3.0,sq,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,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[If[u[[a[[i,1]]]]≠"H",(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]]]],0],{i,1,nb}];

wyc=Table[If[u[[a[[i,1]]]]≠"H",(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]]]],0],{i,1,nb}];

wzc=Table[If[u[[a[[i,1]]]]≠"H",(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]]]],0],{i,1,nb}];

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

wxh=Table[If[u[[a[[i,1]]]]=="H",(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]]]],0],{i,1,nb}];

wyh=Table[If[u[[a[[i,1]]]]=="H",(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]]]],0],{i,1,nb}];

wzh=Table[If[u[[a[[i,1]]]]=="H",(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]]]],0],{i,1,nb}];

Plot8=Table[Graphics3D[{Opacity[0.5],If[wxh[[i]]≠0,Sphere[{wxh[[i]],wyh[[i]],wzh[[i]]},a[[i,3]]]]}],{i,1,nb}];

Plot9= Table[Graphics3D[{Darker[Red,0.8],Cylinder[{{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.05]}],{i,1,nb}];
Show[Plot1,Plot7,Plot8,Plot9,{AspectRatio->Automatic,Boxed->False}]
Show[Plot1,Plot2,Plot9,{AspectRatio->Automatic}]

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+cw]],wyh[[i+cw]],wzh[[i+cw]]},{i,1,bh}]; (* H-bonding clouds *)
tw=Join[tc,twc,twh];
bt=Length[tw]
MatrixForm[tw]
(* coordinates of clouds *)

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]*)
(* cloud-cloud 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}];
(*MatrixForm[dc]*)  (* nucleus-nucleus distances *)

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}];
tv  = Join[tv1,tv2,tv3];
MatrixForm[tv]

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,-2/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]*)  
(* nucleus-cloud distances *)

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+4/dw[[i,j]]]]
Vee

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

(*For[i=1,i<bt+1,i++,
For[j=1,j<bt+1,j++,(*sq=Sqrt[(tw[[i,1]]-t[[j,1]])^2+(tw[[i,2]]-t[[j,2]])^2+(tw[[i,3]]-t[[j,3]])^2];*)If[ i≠j,Vne=Vne+cnv[[j]]*dcw[[i,j]],0]]]
Vne*)
(*For[i=1,i<bt+1,i++,
For[j=1,j<bt+1,j++,Vne=Vne-If[(dcw[[i,j]]≠0 && i≠j),
2*cnv[[j]]/dcw[[i,j]],0]]]
Vne*)

(* protons with bonding clouds*)
(*For[i=bc+1,i<bt+1,i++,
For[j=bc+1,j<n+1,j++,If[i≠j,
Vne=Vne-2/Sqrt[(tw[[i,1]]-t[[j,1]])^2+(tw[[i,2]]-t[[j,2]])^2+(tw[[i,3]]-t[[j,3]])^2],0]]]
Vne*)

(* protons with core clouds*)
(*For[i=1,i<bc+1,i++,
For[j=bc+1,j<n+1,j++,
Vne=Vne-2/Sqrt[(t[[i,1]]-t[[j,1]])^2+(t[[i,2]]-t[[j,2]])^2+(t[[i,3]]-t[[j,3]])^2]]]
Vne*)

(* core nuclei with core clouds*)
(*For[i=1,i<bc+1,i++,
For[j=i+1,j<bc+1,j++,
Vne=Vne-2*cnv[[j]]/Sqrt[(t[[i,1]]-t[[j,1]])^2+(t[[i,2]]-t[[j,2]])^2+(t[[i,3]]-t[[j,3]])^2]]]
Vne*)

(* all nuclei with all clouds, except i = j *)
Vne=Apply[Plus,cnv.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
Vtot=Vne+Vee+Vnn
Ttot
-Vtot/Ttot
Etot=Ttot+Vtot