(* Kimball model and energy derived from MP2/6-31G(d) data source, ES 04 July 2013 *)
Clear[a,u,u1,u2,t];

t=1.889726*{{0.0000000009,0.0000000019,-0.0793743911},{-0.0000000014,-0.9398663736,0.3093075561},{-0.8139481308,0.4699331753,0.3093075629},{0.8139481313,0.4699331772,0.3093075625}};

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

n=Length[u]
num1=Table[i,{i,1,n}];
u1=Table[If[u[[i]]=="C",0.255,If[u[[i]]=="H",0.12,If[u[[i]]=="N",0.24,If[u[[i]]=="O",0.18,If[u[[i]]=="Cl",0.5]]]]],{i,1,n}]
u2=Table[If[u[[i]]=="C",(*0.25618*)0.2562,If[u[[i]]=="H",0.6,If[u[[i]]=="N",0.21522,If[u[[i]]=="O",0.1792,If[u[[i]]=="Cl",0.48698]]]]],{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",7.0]]]]],{i,1,n}]
bc=0;
For[i=1, i<n+1,i++,If[u[[i]]≠"H", bc=bc+1]];
bc
bh=n-bc

Plot1=Table[Graphics3D[{Sphere[{t[[i]]},u1[[i]]]}],{i,1,n}];
Plot1o=Table[Graphics3D[{Opacity[0.8],If[u[[i]]=="N",Lighter[Blue],If[u[[i]]=="H",Lighter[White],If[u[[i]]=="O",Lighter[Red,1],If[u[[i]]=="Cl",Lighter[Green],If[u[[i]]=="B",Lighter[Orange],Darker[Brown]]]]]],Sphere[{t[[i]]},u1[[i]]]}],{i,1,n}];

Plot2=Table[Graphics3D[Text[i,{t[[i,1]]-0.5,t[[i,2]],t[[i,3]]} ,{1,0}]],{i,1,n}];
(*Distance Matrix Bohr*)
dc=Table[Norm[t[[i]]-t[[j]]],{i,1,n},{j,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}];

(* Distance matrix Bohr *)
Dij=Table[If[(dc[[i,j]]<3.0||(u[[i]]=="Cl" && dc[[i,j]]<3.24)),dc[[i,j]],0],{i,1,n},{j,1,n}];
MatrixForm[Prepend[Dij,num1]]

For[i=1 ,i<n+1,i++,
For[j=1, j<bc+1,j++,
If[(Dij[[i,j]]≠0  && u[[i]]== "H"),If[u[[j]]=="C",u2[[i]]=0.59,If[u[[j]]=="F",u2[[i]]=0.5,If[u[[j]]=="N",u2[[i]]=0.5541,If[u[[j]]=="O",u2[[i]]=0.52]]]]]]]
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];*)

(* Total kinetic energy in Eh, = |Etot| *)
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," "]];

Eij=Table[If[i≥j,Dij[[i,j]],0],{i,1,bc-1},{j,1,bc-1}];

nc=0;  
For[i=1,i<bc+1,i++,If[u[[i]]=="C",nc=nc+1,nc]]
cnt=Table[0,{nc}];
nc
no=0;  
For[i=1,i<bc+1,i++,If[u[[i]]=="O",no=no+1,no]]
no

For[j=1,j≤nc,j++,
For[i=bc+1,i≤n,i++,If[ Dij[[i,j]]≠0 ,cnt[[j]]=cnt[[j]]+1,0]]];
cnt

con=Table[{i},{i,1,n}]; conj=Table[0,{nc}];
For[i=1,i≤n,i++,
For[j=1,j≤n,j++,If[ Dij[[i,j]]≠0 ,con[[i]]={con[[i]],j}]]];
conn=Table[Flatten[con[[i]]],{i,1,n}];
(* identify C atoms at double bonds *)
j=0;
For[i=1,i≤nc,i++,If[(Length[conn[[i]]]==4 && u[[conn[[i,4]]]]≠"O"),j=j+1;conj[[j]]=i]];
conj=Take[conj,{1,j}]
nj=Length[conj]
MatrixForm[conn, TableAlignments->Left]

j=0; h=0;
coco=Table[{0,0,0,0},{no}];
coch=coco;
For[i=nc+1, i≤nc+no, i++,If[Length[conn[[i]]]==2,j=j+1;coco[[j]]=Join[{conn[[i,1]]},Take[conn[[conn[[i,2]]]],3]],h=h+1;coch[[h]]=conn[[i]]]];
coco=Take[coco,{1,j}];
coch=Take[coch,{1,h}];
oj=Length[coco]
hj=Length[coch]

(*-------------------------------------------------------------------------*)
(*next: N LP*)
k = 1;
Clear[conn,P];
P = 4;rln=1.154; pn=1;
tpyrn = {{0.0, 0.0, 0.0}};
conn = {{2,3,4,1}};
f={-1};

<< "C:\\Kimb_Mathem\\LpyrNtrans.m"

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

wx=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}];      wy=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}];  wz=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[{Opacity[0.5],Sphere[{wx[[i]],wy[[i]],wz[[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}];
Plot8ib=Table[Graphics3D[{Opacity[0.4],Lighter[Blue],Sphere[tpyrn[[i]],rln]}],{i,1,1}];
Plot9= Table[Graphics3D[{Lighter[Gray,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.07]}],{i,1,nb}];

Show[Plot1o,Plot2,Plot7,Plot8,Plot8ib,Plot9,{AspectRatio->Automatic,Boxed->False,SphericalRegion->True}]

tc=Table[{x[[i]],y[[i]],z[[i]]},{i,1,bc}];   (* core clouds *)
twc=Table[{wx[[i]],wy[[i]],wz[[i]]},{i,1,cw}];  (* Bond cöouds between cores *)
twh=Table[{wxh[[i]],wyh[[i]],wzh[[i]]},{i,1,bh}]; (* H-bonding clouds *) 
tw=Join[tc,twc,twh,tpyrn];
bt=Length[tw]

(* cloud-cloud distances *)
dw=Table[Norm[tw[[i]]-tw[[j]]],{i,1,bt},{j,1,bt}];
(*MatrixForm[dw]*)

(* nucleus-nucleus distances *)
tv1=Table[t[[i]],{i,1,bc}];
tv2=Table[{0,0,0},{i,1,cw}];
tv3= Table[t[[i]],{i,bc+1,n}];
tv6 =Table[{0,0,0},{i,1,1}];
tv  = Join[tv1,tv2,tv3,tv6];
(*MatrixForm[tv]*)
Length[tv]

ow=Join[Table[-2.0,{i,1,bc+nb}],Table[-2.0,{i,1,1}]];
 ow
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,1}]]

(* nucleus-cloud distances *)
dcw=Table[sq=Norm[tw[[i]]-tv[[j]]];If[(sq> 0.1 && i≠j),1/sq,0],{i,1,bt},{j,1,bt}];
Length[dcw]

Elcharge=Sum[ow[[i]],{i,1,Length[ow]}]
Length[ow]
Nuccharge=Sum[cnv[[i]],{i,1,Length[cnv]}]
Length[cnv]

Vee=0.0;
For[i=1,i<bt,i++,
For[j=i+1,j≤bt,j++,
Vee=Vee+ow[[i]]*ow[[j]]/dw[[i,j]]]]
Vee
sc=0.34885;   (* core clouds *)
For[i=1 ,i≤bc,i++,Vee=Vee+3*sc/u2[[i]]]
sw=0.34885;  (* bonding clouds *)
For[i=1 ,i≤nb,i++,Vee=Vee+3*sw/a[[i,3]]]
Vee=Vee+3*sw/rln;
Vee

(* all nuclei with all clouds, except i = j *)
Vne=cnv.(ow.dcw)
For[i=1,i≤bc,i++,Vne=Vne-3.0*cn[[i]]/u2[[i]]]
Vne

For[i=cw+1,i≤nb,i++,Vne=Vne-(3-(u2[[a[[i,1]]]]/a[[i,3]])^2)/a[[i,3]]]
Vne

(* nucleus-nucleus *)

Vnn=0.0;
For[i=1,i<n,i++,
For[j=i+1,j<n+1,j++,Vnn=Vnn+cn[[i]]*cn[[j]]/dc[[i,j]]]]
Vnn

ratio=-(Vne+2*Vnn)/(Vee-Vnn)

ratiomp2=-(-155.6621124+2*11.9014636)/(31.1706102-11.9014636)

Vee
Vnn
Vne
Tlp =1.59*2.25/rln^2

Vtot=Vne+Vee+Vnn
Ttot =Ttot+Tlp
Vir   =-Vtot/Ttot
Etot=Ttot+Vtot