#PBS -N test_server
#PBS -q nowy
#PBS -l nodes=4:ppn=4

#-----------------------------------------------------------------------------
setenv UNRES_BIN /users2/czarek/UNRES/git3_tmp/build/bin/unresMD-mult_ifort_MPI_E0LL2Y.exe
#-----------------------------------------------------------------------------
setenv DD /users2/czarek/UNRES/git3_tmp/unres/PARAM
setenv BONDPAR $DD/bond_AM1_ext_dum.parm
setenv THETPAR $DD/theta_abinitio-ext-old.parm
setenv THETPARPDB $DD/thetaml_ext.5parm
setenv ROTPARPDB $DD/scgauss_ext.parm
setenv ROTPAR $DD/rotamers_AM1_aura_ext.10022007.parm
setenv TORPAR $DD/tor_opt_iter1_7p_old-ext.parm
setenv TORDPAR $DD/torsion_double_631Gdp_old_ext.parm
setenv ELEPAR $DD/electr_631Gdp_ext.parm
setenv SIDEPAR $DD/sc_GB_opt_iter1_7p_old-ext_lip.parm
setenv FOURIER $DD/fourier_opt_iter1_7p_old-ext.parm
setenv SCCORPAR $DD/sccor_am1_pawel_ext.dat
setenv SCPPAR $DD/scp_ext.parm
setenv PATTERN $DD/patterns.cart
setenv LIPTRANPAR $DD/Lip_tran_initial_ext.parm
#-----------------------------------------------------------------------------
cd $PBS_O_WORKDIR

setenv MPIRUN "/users2/local/mpich2-1.4.1p1_intel/bin/mpirun "
set NPROCS=`cat $PBS_NODEFILE | wc -l`

setenv FGPROCS 2
setenv POT GB
setenv PREFIX file
setenv OUT1FILE YES

#UNRES MREMD
$MPIRUN -machinefile $PBS_NODEFILE -np $NPROCS $UNRES_BIN

#WHAM
setenv WHAM_BIN /users2/czarek/UNRES/git3_tmp/build/bin/wham-mult_ifort_MPI_E0LL2Y.exe
setenv CONTFUNC GB
setenv SIDEP $DD/contact_ext.3.parm
setenv SCRATCHDIR .
setenv PREFIX file_wham

$MPIRUN -machinefile $PBS_NODEFILE -np 2 $WHAM_BIN

#test images
setenv line `awk '{}END{print int(NR*0.2)}' file_GB000.stat`

if ( `grep -c pdbref file.inp` ) then
 tail -q -n +$line file_GB*.stat |  awk '{if ( NF == 14 ) print}' >remd_all.stat
 cat file_GB*.stat |  awk '{if ( NF == 14 ) print}' > remd_all0.stat
else
 tail -q -n +$line file_GB*.stat |  awk '{if ( NF == 10 ) print}' >remd_all.stat
 cat file_GB*.stat |awk '{if ( NF == 10 ) print}' > remd_all0.stat
endif
../files/matplotlib_hist.py $temperatures
rm remd_all.stat remd_all0.stat

#CLUSTER WHAM
setenv INPUT file_cluster
setenv INTIN file_wham
setenv OUTPUT file_cluster
setenv PDB CART
setenv COORD CX
setenv PRINTCOOR PRINT_PDB
setenv CLUSTER_WHAM_BIN /users2/czarek/UNRES/git3_tmp/build/bin/cluster_wham-mult_ifort_MPI_E0LL2Y.exe

$MPIRUN -machinefile $PBS_NODEFILE -np 2 $CLUSTER_WHAM_BIN

ln -s file_wham_T*K_ave.pdb ave
../files/cluster2allatom.sh ave
rm ave

#amber min refinement
foreach i (`seq 1 5`)
../files/refine.sh MODEL$i.pdb $ssbond
end

/users2/local/pymol_1.6/pymol -c ../files/model.pml

if ( `grep -c pdbref file.inp` ) then
 awk '{printf "%s%s%s\n",substr($0,0,21)," ",substr($0,23)}' plik.pdb > tmp.pdb
 /users2/local/mmtsb/perl/convpdb.pl -renumber 1 -out generic tmp.pdb > plik1.pdb
 rm tmp.pdb

 /users2/local/bin/tmscore MODEL1.pdb plik1.pdb > tmscore1.out
 /users2/local/bin/tmscore MODEL2.pdb plik1.pdb > tmscore2.out
 /users2/local/bin/tmscore MODEL3.pdb plik1.pdb > tmscore3.out
 /users2/local/bin/tmscore MODEL4.pdb plik1.pdb > tmscore4.out
 /users2/local/bin/tmscore MODEL5.pdb plik1.pdb > tmscore5.out
 if (-e saxs.data) then
  awk -f ../files/saxs_dist.awk plik1.pdb > plik_saxs.data
 endif
# rm plik1.pdb
endif

if (-e saxs.data) then
awk -f ../files/saxs_dist.awk MODEL1.pdb > MODEL1_saxs.data
awk -f ../files/saxs_dist.awk MODEL2.pdb > MODEL2_saxs.data
awk -f ../files/saxs_dist.awk MODEL3.pdb > MODEL3_saxs.data
awk -f ../files/saxs_dist.awk MODEL4.pdb > MODEL4_saxs.data
awk -f ../files/saxs_dist.awk MODEL5.pdb > MODEL5_saxs.data
../files/plot_saxs.py
endif


#END
touch finished
