#! /bin/sh
#*******************************************************************************
#                                                                              *
#                                   Viewmol                                    *
#                                                                              *
#                               R E A D D M O L                                *
#                                                                              *
#                 Copyright (c) Joerg-R. Hill, December 1998                   *
#                                                                              *
#*******************************************************************************
#
# $Id: readdmol,v 1.3 1999/02/07 21:55:32 jrh Exp $
# $Log: readdmol,v $
# Revision 1.3  1999/02/07 21:55:32  jrh
# Release 2.2
#
# Revision 1.2  1998/01/26 00:49:12  jrh
# Release 2.1
#
# Revision 1.1  1996/12/10  18:46:40  jrh
# Initial revision
#
#
if [ ! -f $1 ]
then
  echo "\$error noFile 1 $1"
  echo "\$end"
  exit 1
fi

case `uname -s` in
  IRIX*) AWK=nawk
         ;;
  *)     AWK=awk
         ;;
esac

$AWK 'function vectorlength(x, y, z)
      {
        return(sqrt(x*x+y*y+z*z));
      }
      function angle(x1, y1, z1, x2, y2, z2)
      {
        x=(x1*x2+y1*y2+z1*z1)/(vectorlength(x1, y1, z1)*vectorlength(x2, y2, z2));
        y=sqrt(1-x*x);
        return(57.29577951308232087721*atan2(y, x));
      }
      BEGIN {cycle=0;
		 fixed=0;
             readfrq=0;
             readmodes=0;
             readorbital=0;
             nfreq=0;
             unitcell=0;
		 transx=0.0;
		 transy=0.0;
		 transz=0.0;
		 version=3;
            }
      /^  DDDDDDDD    MM       MM    OOOOOO    LL/ {version=2;}
	/^translated by/ {transx=$3;
				transy=$4;
				transz=$5;
                       }
      /^  a1/ {cell[1,1]=$2;
               cell[1,2]=$3;
               cell[1,3]=$4;
              }
      /^ translation vector \[a0\]    1/ {cell[1,1]=$5;
                                          cell[1,2]=$6;
                                          cell[1,3]=$7;
                                         }
      /^  a2/ {cell[2,1]=$2;
               cell[2,2]=$3;
               cell[2,3]=$4;
              }
      /^ translation vector \[a0\]    2/ {cell[2,1]=$5;
                                          cell[2,2]=$6;
                                          cell[2,3]=$7;
                                         }
      /^  a3/ {cell[3,1]=$2;
               cell[3,2]=$3;
               cell[3,3]=$4;
               a=vectorlength(cell[1,1], cell[1,2], cell[1,3]);
               b=vectorlength(cell[2,1], cell[2,2], cell[2,3]);
               c=vectorlength(cell[3,1], cell[3,2], cell[3,3]);
               alpha=angle(cell[2,1], cell[2,2], cell[2,3], cell[3,1], cell[3,2], cell[3,3]);
               beta=angle(cell[1,1], cell[1,2], cell[1,3], cell[3,1], cell[3,2], cell[3,3]);
               gamma=angle(cell[1,1], cell[1,2], cell[1,3], cell[2,1], cell[2,2], cell[2,3]);
               unitcell=1;
		  }
      /^ translation vector \[a0\]    3/ {cell[3,1]=$5;
                                          cell[3,2]=$6;
                                          cell[3,3]=$7;
               a=vectorlength(cell[1,1], cell[1,2], cell[1,3]);
               b=vectorlength(cell[2,1], cell[2,2], cell[2,3]);
               c=vectorlength(cell[3,1], cell[3,2], cell[3,3]);
               alpha=angle(cell[2,1], cell[2,2], cell[2,3], cell[3,1], cell[3,2], cell[3,3]);
               beta=angle(cell[1,1], cell[1,2], cell[1,3], cell[3,1], cell[3,2], cell[3,3]);
               gamma=angle(cell[1,1], cell[1,2], cell[1,3], cell[2,1], cell[2,2], cell[2,3]);
               unitcell=1;
              }
	/Atoms with "Fixed" Coordinates/ {fixed=1;}
      /^df   [ A-Z][A-z]/ {coord[i,1]=$3+transx;
                           coord[i,2]=$4+transy;
                           coord[i,3]=$5+transz;
                           coord[i,4]=$2;
                           grad[i,1]=$6;
                           grad[i,2]=$7;
                           grad[i++,3]=$8;
				   if (index(fix[i], "x") == 0)
                             gnorm+=$6*$6;
				   if (index(fix[i], "y") == 0)
				     gnorm+=$7*$7;
				   if (index(fix[i], "z") == 0)
				     gnorm+=$8*$8;
                          }
      /^df              ATOMIC  COORDINATES/ {cycle++;
                                              i=0;
                                              gnorm=0.0;
                                              readorbital=0;
                                             }
      /^df   self consistent total energy/ {energy=$6;
                                            sub("Ha", "", energy);
                                           }
      /^df total energy/    {energy=$4;
                             if (cycle == 1) printf("$grad\n");
                             printf("  cycle =    %d  energy =%18.10f |dE/dxyz| =%10.6f\n", cycle, energy, sqrt(gnorm));
                             if (unitcell == 1) printf("  unitcell %f %f %f %f %f %f\n", a, b, c, alpha, beta, gamma);
                             for (j=0; j<i; j++)
                             {
                               printf("%22.14f%22.14f%22.14f  %s\n", coord[j,1], coord[j,2], coord[j,3], coord[j,4]);
                             }
                             for (j=0; j<i; j++)
                             {
                               printf("%22.14f%22.14f%22.14f\n", grad[j,1], grad[j,2], grad[j,3]);
                             }
                            }
      /^df  binding energy/ {energy=$4;
                             if (cycle == 1) printf("$grad\n");
                             printf("  cycle =    %d  energy =%18.10f |dE/dxyz| =%10.6f\n", cycle, energy, sqrt(gnorm));
                             if (unitcell == 1) printf("  unitcell %f %f %f %f %f %f\n", a, b, c, alpha, beta, gamma);
                             for (j=0; j<i; j++)
                             {
                               printf("%22.14f%22.14f%22.14f  %s\n", coord[j,1], coord[j,2], coord[j,3], coord[j,4]);
                             }
                             for (j=0; j<i; j++)
                             {
                               printf("%22.14f%22.14f%22.14f\n", grad[j,1], grad[j,2], grad[j,3]);
                             }
                            }
      /^df \*\*\*\*\* magnitude of the gradient:/ {gnorm=$7;
                                                   if (cycle == 1) printf("$grad\n");
                                                   printf("  cycle =    %d  energy =%18.10f |dE/dxyz| =%10.6f\n", cycle, energy, gnorm);
                                                   if (unitcell == 1) printf("  unitcell %f %f %f %f %f %f\n", a, b, c, alpha, beta, gamma);
                                                   for (j=0; j<i; j++)
                                                   {
                                                     printf("%22.14f%22.14f%22.14f  %s\n", coord[j,1], coord[j,2], coord[j,3], coord[j,4]);
                                                   }
                                                   for (j=0; j<i; j++)
                                                   {
                                                     printf("%22.14f%22.14f%22.14f\n", grad[j,1], grad[j,2], grad[j,3]);
                                                   }
                                                  }
      /^     vibrational frequencies, intensities/ {readfrq=1;
                                                    printf("$vibrational spectrum\n");
                                                   }
#     /^  Frequencies \(cm-1\) and normal modes \(ang\)/ {readmodes=1;
      /^  Frequencies \(cm-1\) and normal modes/ {readmodes=1;
                                                  total=0;
                                                  mode=0;
                                                 }
      /^  Eigenvalues and occupations:/ {readorbital=1;
                                         norbital=0;}
      /^    state/                      {readorbital=1;
                                         norbital=0;}
      /^   Searching for/               {readorbital=0;}
      /^ Molecular orbital spectrum/    {readorbital=0;}
	/^  Beta orbitals, symmetry block/ {readorbital=0;}
	/^  Orbital occupation is/        {readorbital=0;}
	/^Ef/                             {readorbital=0;}
	/Hessian modes/ {fixed=0;}
      /^ *[0-9]/ {if (readfrq == 1)
                  {
                    printf("A1 %10.1f %10.5f 1.0\n", $3, $4/979.5936733887993);
                    nfreq++;
                  }
			if (fixed == 1)
			{
			  s=$2 $3 $4;
                    fix[$1-1]=tolower(s);
			}
                 }
      /^ *[-.0-9]*[DE][+-][0-9][0-9]/ {if (version == 2 && readorbital == 1)
                                      {
                                        for (j=1; j<=NF; j++)
                                        {
                                          norbital++;
                                          orbital[norbital,1]=symmetry;
				  		      orbital[norbital,2]=$j;
							sub("D", "E", orbital[norbital,2]);
                                        }
                                      }
                                     }
      /[0-9][0-9]*[ ]*+[ ]*[0-9][0-9]*/ {if (version > 2 && readorbital == 1)
                                         {
                                           norbital++;
                                           orbital[norbital,1]=$4;
 			  	                   orbital[norbital,2]=$5;
                                         }
                                        }
      /^ [A-z][ A-z]* [xyz]/ {if (readmodes == 1)
                              {
                                for (j=3; j<=NF; j++)
                                {
                                  nm[mode+j-3,total]=$j;
                                }
                                total++;
                              }
                             }
      /^ *[xyz]/ {if (readmodes == 1)
                  {
                    for (j=2; j<=NF; j++)
                    {
                      nm[mode+j-2,total]=$j;
                    }
                    total++;
                    if (total == 3*i)
                    {
                      mode+=NF-1;
                      total=0;
                    }
                       }
                     }
      /^  \*\*\*\*\*\*\*\*/ {readfrq=0;}
      /^ Zero point vibrational energy/ {readmodes=0;
                                         printf("$vibrational normal modes\n");
                                         for (j=0; j<3*i; j++)
                                         {
                                           for (k=0; k<nfreq; k++)
                                           {
                                             if (k % 5 == 0)
                                             {
                                               if (k != 0) printf("\n");
                                               printf("%d %d", j, j);
                                             }
                                             printf(" %10.6f", nm[k,j]);
                                           }
                                           printf("\n");
                                         }
                                        }
    /^  Alpha orbitals,/ {j=index($6, ".");
				  if (j != 0) symmetry=substr($6, 1, j-1);
				  else symmetry=$6;
                         }
      END {printf("$coord 0.52917706\n");
           for (j=0; j<i; j++)
           {
             printf("%22.14f%22.14f%22.14f  %s %s\n", coord[j,1], coord[j,2], coord[j,3], coord[j,4], fix[j]);
           }
           if (unitcell == 1) printf("$unitcell %f %f %f %f %f %f\n", a, b, c, alpha, beta, gamma);
	     if (norbital > 0)
	     {
             printf("$scfmo\n");
             for (j=1; j<=norbital; j++)
             {
               printf("%d %s  eigenvalue= %20.15e  nsaos=0\n", j, orbital[j,1], orbital[j,2]);
             }
	     }
           printf("$end\n");}' $1
