#!/usr/bin/perl -wi

#
# GAMESSPLUS soil utility program, based on GAMESSPLUS v2009
#

#
# Set some default values
#

$dgwa = 0.0;
$dgsa = 0.0;
$dgsw = 0.0;
$level = 'am1';
$density = 0.11;
$dgwflag = 1;
$dgsoilflag = 1;
$logKoc = 0.0;
$dandt = localtime;
$rungmsdir = "./rungmsplus";


#
# Read input file
#

open(IN, "<$ARGV[0]");@line="";@line=<IN>;close(IN);
$prefix = $ARGV[0]; $prefix =~ s/\.inp//i;

for $i (0 .. $#line) {
   chomp($line[$i]); $line[$i] = uc($line[$i]);
   if ($line[$i] =~ m/\$DATA/) {
     $input = $line[$i] ;
     $input =~ s/\$DATA//i;
     if ($line[$i]=~ m/\$END/) {
     $input =~ s/\$END//i;
     }  else {
       for $j ($i+1 .. $#line) {
          chomp($line[$j]);$line[$j] = uc($line[$j]);
          if ($line[$j] =~ m/\$END/) {
             $input = "$input \n $line[$j]";
             $input =~ s/\$END//i;
             last;
          } else {
             $input = "$input\n$line[$j]";
             $input =~ s/\$END//i;
          }
       }
     }
   }

   if ($line[$i] =~ m/\$DELTAGW/) {
     $dgwflag = 0;
     $dgwa = $line[$i] ;
     $dgwa =~ s/\$DELTAGW//i;
     if ($line[$i]=~ m/\$END/) {
     $dgwa =~ s/\$END//i;
     }  else {
       for $j ($i+1 .. $#line) {
          chomp($line[$j]);$line[$j] = uc($line[$j]);
          if ($line[$j] =~ m/\$END/) {
             $dgwa = "$dgwa \n $line[$j]";
             $dgwa =~ s/\$END//i;
             last;
          } else {
             $dgwa = "$dgwa\n$line[$j]";
             $dgwa =~ s/\$END//i;
          }
       }
     }
   }

   if ($line[$i] =~ m/\$DELTAGSOIL/) {
     $dgsoilflag = 0;
     $dgsa = $line[$i] ;
     $dgsa =~ s/\$DELTAGSOIL//i;
     if ($line[$i]=~ m/\$END/) {
     $dgsa =~ s/\$END//i;
     }  else {
       for $j ($i+1 .. $#line) {
          chomp($line[$j]);$line[$j] = uc($line[$j]);
          if ($line[$j] =~ m/\$END/) {
             $dgsa = "$dgsa \n $line[$j]";
             $dgsa =~ s/\$END//i;
             last;
          } else {
             $dgsa = "$dgsa\n$line[$j]";
             $dgsa =~ s/\$END//i;
          }
       }
     }
   }

   if ($line[$i] =~ m/\$SCF/) {
     $level = $line[$i] ;
     $level =~ s/\$SCF//i;
     if ($line[$i]=~ m/\$END/) {
     $level =~ s/\$END//i;
     }  else {
       for $j ($i+1 .. $#line) {
          chomp($line[$j]);$line[$j] = uc($line[$j]);
          if ($line[$j] =~ m/\$END/) {
             $level = "$level \n $line[$j]";
             $level =~ s/\$END//i;
             last;
          } else {
             $level = "$level\n$line[$j]";
             $level =~ s/\$END//i;
          }
       }
     }
   }

   if ($line[$i] =~ m/\$SOILDENSITY/) {
     $density = $line[$i] ;
     $density =~ s/\$SOILDENSITY//i;
     if ($line[$i]=~ m/\$END/) {
     $density =~ s/\$END//i;
     }  else {
       for $j ($i+1 .. $#line) {
          chomp($line[$j]);$line[$j] = uc($line[$j]);
          if ($line[$j] =~ m/\$END/) {
             $density = "$density \n $line[$j]";
             $density =~ s/\$END//i;
             last;
          } else {
             $density = "$density\n$line[$j]";
             $density =~ s/\$END//i;
          }
       }
     }
   }
}

#################################################################
# If DELTAGW is not input by user, use SM5.42 to calculate it
#################################################################

# Make input file

if ($dgwflag == 1) {
   open(OUT ,">water.$prefix.inp");
   print OUT " \$CONTRL SCFTYP=RHF RUNTYP=ENERGY COORD=UNIQUE \$END\n";
   if ($level=~ m/AM1/) {
     print OUT " \$BASIS GBASIS=AM1 \$END\n";
     print OUT " \$GMSOL ISCRF=1 ICMD=11 \$END\n";
   }
   if ($level=~ m/ABINITIO/) {
     print OUT " \$GMSOL ISCRF=1 ICMD=1 \$END\n";
   }
   print OUT " \$DATA";
   print OUT "$input";
   print OUT "\$END";
   close(OUT);

# Run job interactively

  system("$rungmsdir water.$prefix 1 1 >& water.$prefix.log 2>&1");

# Make sure calculation ran correctly. If not, stop.

  if (`grep "DeltaG-S\(liq\) free energy of  solvation" water.$prefix.log`) {
  }  else {
     print "The free energy of solvation calculation in water did not run properly.\n";
     print "Check the output file, called water.$prefix.log,for more details.\n";
     exit;
  }

# Get free energy of solvation from output file

  open(IN,"<water.$prefix.log");
  while(<IN>) {
    if (/     \(9\) = \(6\) - \(0\)\s+([-]?\d*\.\d+)\s*/) {
      $dgwa = $1;
    }
  }
  close(IN);
}

#################################################################
# If DELTAGSOIL is not input by user, use SM5.42 to calculate it
#################################################################

# Make input file

if ($dgsoilflag == 1) {
   open(OUT ,">soil.$prefix.inp");
   print OUT " \$CONTRL SCFTYP=RHF RUNTYP=ENERGY COORD=UNIQUE \$END\n";
   if ($level=~ m/AM1/) {
     print OUT " \$BASIS GBASIS=AM1 \$END\n";
     print OUT " \$GMSOL ISCRF=1 IAQU=0 ICMD=11 \n";
     print OUT "  DIELEC=15.0 SOLN=1.541 SOLA=0.36 SOLB=0.34 \n";
     print OUT "  SOLG=63.3 SOLC=0.0 SOLH=0.0 \$END\n";
   }
   if ($level=~ m/ABINITIO/) {
     print OUT " \$GMSOL ISCRF=1 IAQU=0 ICMD=1 \n";
     print OUT "  DIELEC=15.0 SOLN=1.379 SOLA=0.61 SOLB=0.60 \n";
     print OUT "  SOLG=46.0 SOLC=0.0 SOLH=0.0 \$END\n";
   }
   print OUT " \$DATA";
   print OUT "$input";
   print OUT "\$END";
   close(OUT);

# Run job interactively

  system("$rungmsdir soil.$prefix 1 1 >& soil.$prefix.log 2>&1");

# Make sure calculation ran correctly. If not, stop.

  if (`grep "DeltaG-S\(liq\) free energy of  solvation" soil.$prefix.log`) {
  }  else {
     print "The free energy of solvation calculation in soil did not run properly.\n";
     print "Check the output file, called soil.$prefix.log,for more details.\n";
     exit;

     print "Check the output file, called soil.$prefix.log,for more details.\n";
     exit;
  }

# Get free energy of solvation from output file

  open(IN,"<soil.$prefix.log");
  while(<IN>) {
    if (/     \(9\) = \(6\) - \(0\)\s+([-]?\d*\.\d+)\s*/) {
      $dgsa = $1;
    }
  }
  close(IN);
}

#
# Calculate deltag(water --> soil), and logKoc
# Note the conversion from natural logarithm to
# the common (base10) logarithm.
#
 
$dgsw = $dgsa - $dgwa;
$logKoc = -($dgsw)/1.36 - log($density)/log(10);

#
# Write output.
#

open(OUT, ">$prefix.log");
print OUT << "EOF";

GAMESSPLUS Soil Sorption Utility calculation
based on GAMESSPLUS v2009

$dandt
$input
\$SCF		=  $level
\$SOILDENSITY	=  $density g/mL
EOF
if ($dgwflag == 0) {
print OUT "\$DELTAGW	=  $dgwa kcal/mol\n";
}

print OUT << "EOF";

All values are for a temperature of 298 K.
Free energies are calculated using a 1M --> 1M standard state
log = log(base10)
-----------------------------------------------------------------
EOF
print OUT " Free Energy of Transfer (air --> water)\t"; 
printf OUT "%3.2f",$dgwa;
print OUT "  kcal/mol";
print OUT "\n";
print OUT " Free Energy of Transfer (air --> soil)\t\t";
printf OUT "%3.2f",$dgsa;
print OUT "  kcal/mol";
print OUT "\n";
print OUT " Free Energy of Transfer (water --> soil)\t";
printf OUT "%3.2f",$dgsw;
print OUT "  kcal/mol";
print OUT "\n";
print OUT " log Koc\t\t\t\t\t ";
printf OUT "%3.2f",$logKoc;
print OUT "\n";
print OUT << "EOF";
-----------------------------------------------------------------

End of output.
EOF


