#!/usr/bin/perl 
#
# Usage:
#      perl rWMS.pl ch3.out
#  
#
@E_num = (2, 2, 2, 2, 3, 3, 2);
@E_name = ('_ENERGR', '_EF12_RHFRELAX', 'EMP2             =', 'EF12             =', 'CCSD-F12b correlation energy', 'Triples (T) contribution', 'ENERGC           =') ;

$workdir=`pwd`;
chomp $workdir;
# parameters define WMS
 $HF_a = 2.178;
 $CARBS_a = 2.309;
 $MP2c_a = 1.018;
 $F12_a =1.126;
 $deta_CCSD_MP2_F12_a = 1.569;
 $T_a = 2.175;
 $MP2CV_a = 3.55;
 $CCCV_a = 1.0;
 $TCV_a = 3.8;
 $Rel_a = 2.00;

# grab the raw energies
@HF = graben($E_name[0], $E_num[0]);
@CARBS = graben($E_name[1], $E_num[1]);
@MP2 = graben($E_name[2], $E_num[2]);
@F12 = graben($E_name[3], $E_num[3]);
@CCSD_F12b = graben($E_name[4], $E_num[4]);
@T = graben($E_name[5], $E_num[5]);
@CCSD_CV = graben($E_name[6], $E_num[6]);

$name = $ARGV[0];
@out=split(/\./, $name);

# core valence correlation
$MP2_val_DZ = $MP2[2] - $HF[2];
$MP2_core_DZ = $MP2[3] - $HF[3];
$dMP2_CV_DZ = $MP2_core_DZ - $MP2_val_DZ; 

$MP2_val_TZ = $MP2[4] - $HF[4];
$MP2_core_TZ = $MP2[5] - $HF[5];
$dMP2_CV_TZ = $MP2_core_TZ - $MP2_val_TZ;

$CCSD_val_DZ = $CCSD_CV[0] - $MP2_val_DZ ;
$CCSD_core_DZ = $CCSD_CV[1] - $MP2_core_DZ ;
$dCCSD_CV_DZ = $CCSD_core_DZ - $CCSD_val_DZ ;


$T_val_DZ = $T[4];
$T_core_DZ = $T[5];
$dT_CV_DZ = $T_core_DZ - $T_val_DZ ;

$MP2CV_c = 3**$MP2CV_a/(3**$MP2CV_a - 2**$MP2CV_a);
$MP2CV_CBS = $dMP2_CV_DZ + $MP2CV_c*($dMP2_CV_TZ - $dMP2_CV_DZ);
$CV = $MP2CV_CBS + $CCCV_a*$dCCSD_CV_DZ + $TCV_a*$dT_CV_DZ;  

# Relativistic 
$HF_DK_DZ = $HF[6];
$HF_noRel_DZ= $HF[7];
$dHF_Rel_DZ = $HF_DK_DZ - $HF_noRel_DZ;

$MP2_DK_DZ = $MP2[6] - $HF[6];
$MP2_noRel_DZ= $MP2[7] - $HF[7];
$dMP2_Rel_DZ = $MP2_DK_DZ - $MP2_noRel_DZ;

$HF_DK_TZ = $HF[8];
$HF_noRel_TZ= $HF[9];
$dHF_Rel_TZ = $HF_DK_TZ - $HF_noRel_TZ;

$MP2_DK_TZ = $MP2[8] - $HF[8];
$MP2_noRel_TZ = $MP2[9] - $HF[9];
$dMP2_Rel_TZ = $MP2_DK_TZ - $MP2_noRel_TZ;

$Rel_DZ = $MP2[6] -$MP2[7] ;
$Rel_TZ = $MP2[8] -$MP2[9] ;
$Rel_c = 3**$Rel_a/(3**$Rel_a - 2**$Rel_a);
$Rel_CBS = $Rel_DZ + $Rel_c*($Rel_TZ - $Rel_DZ);

# valence correlation 
$HF_DZ = $HF[0] - $CARBS[0];
$HF_TZ = $HF[1] - $CARBS[1];
$HF_CBS = $HF_DZ + $HF_a*($HF_TZ - $HF_DZ);


$CARBS_DZ =  $CARBS[0];
$CARBS_TZ =  $CARBS[1];
$CARBS_CBS = $CARBS_DZ + $CARBS_a*($CARBS_TZ - $CARBS_DZ);

$MP2c_DZ = $MP2[0] - $HF[0];
$MP2c_TZ = $MP2[1] - $HF[1];
$MP2c_CBS = $MP2c_DZ + $MP2c_a*($MP2c_TZ - $MP2c_DZ);

$F12_DZ = $F12[0];
$F12_TZ = $F12[1];
$F12_CBS = $F12_DZ + $F12_a*($F12_TZ - $F12_DZ);

$deta_CCSD_MP2_F12_DZ = $CCSD_F12b[0] - $F12_DZ - $MP2c_DZ ;
$deta_CCSD_MP2_F12_TZ = $CCSD_F12b[1] - $F12_TZ - $MP2c_TZ ;
$deta_CCSD_MP2_F12_CBS = $deta_CCSD_MP2_F12_DZ + $deta_CCSD_MP2_F12_a*($deta_CCSD_MP2_F12_TZ - $deta_CCSD_MP2_F12_DZ);

$T_DZ = $T[0];
$T_TZ = $T[2];
$T_CBS = $T_DZ + $T_a*($T_TZ - $T_DZ);

$HF_Sum_CBS = $HF_CBS + $CARBS_CBS;
$MP2_CBS = $MP2c_CBS + $F12_CBS ;
$CCSD_CBS = $MP2c_CBS + $F12_CBS + $deta_CCSD_MP2_F12_CBS;
  

$EWM = $HF_Sum_CBS + $CCSD_CBS + $T_CBS + $HOVC + $CV + $Rel_CBS;  
#print "$HF_Sum_CBS        $CCSD_CBS   $T_CBS  $CV   $Rel_CBS   $EWM\n";
print "$EWM \n";


sub graben {
  my (@en, $i, $j, $k, $ename, $enum, @temp, $tl);
  $ename = $_[0];
  $enum = $_[1];
  system("grep \"$ename\" $ARGV[0] > $workdir/tempen.out");
  open(FILE1, "<$workdir/tempen.out") || die "can't open $!"; 
  @lines = (<FILE1>);
  close FILE1;
  for $j (0 .. @lines-1) {
    $tl = $lines[$j];
    @temp=($tl =~ /\S+/g);
    $en[$j] = $temp[$enum];
  } 
  system("rm -rf  $workdir/tempen.out"); 
return @en;
}
