#!/bin/sh

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use thermo_pw.x to calculate the"
$ECHO "lattice constant, bulk modulus, and other anharmonic properties"
$ECHO "of silicon as a function of the temperature."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="thermo_pw.x"
PSEUDO_LIST="Si.pz-vbc.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"


# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done

for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
THERMO_PW_COMMAND="$PARA_IMAGE_PREFIX $BIN_DIR/thermo_pw.x $PARA_IMAGE_POSTFIX"
# check for gnuplot
$ECHO
$ECHO "  running thermo_pw.x as: $THERMO_PW_COMMAND"
$ECHO

$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/g*/silicon*
rm -rf $TMP_DIR/g*/_ph*/silicon*
$ECHO " done"

cat > thermo_control << EOF
 &INPUT_THERMO
  what='mur_lc_t',
  deltat=3.
 /
EOF

cat > si.mur_lc_t.in << EOF

 &control
   calculation = 'scf'
   prefix='silicon',
   pseudo_dir = '$PSEUDO_DIR/',
   outdir='$TMP_DIR/'
 /
 &system
   ibrav= 2, 
   celldm(1)= 10.20, 
   nat=2, 
   ntyp=1,
   ecutwfc=24.0,
 /
 &electrons
    conv_thr =  1.0d-8
 /
ATOMIC_SPECIES
 Si  28.086  Si.pz-vbc.UPF
ATOMIC_POSITIONS (alat)
 Si 0.00 0.00 0.00
 Si 0.25 0.25 0.25
K_POINTS AUTOMATIC
2 2 2 1 1 1 
EOF

cat > ph_control << EOF
ph_title
 &inputph
  tr2_ph=1.0d-12,
  prefix='silicon',
  fildyn='si.dyn.xml',
  ldisp=.TRUE.
  nq1=4, nq2=4, nq3=4,
/
EOF

$ECHO " running the thermo mur_lc_t calculation for Si,"
$ECHO " dispersions can be found in output_disp.g#.ps,"
$ECHO " phonon_dos in output_dos.g#.ps,"
$ECHO " harmonic thermodynamic properties in output_therm.g#.ps,"
$ECHO " gruneisen parameters in output_grun.ps, "
$ECHO " phonon frequencies interpolated at T = 4 K volume in output_grun_freq.ps, "
$ECHO " anharmonic thermodynamic properties in output_anhar.ps...\c"

$THERMO_PW_COMMAND < si.mur_lc_t.in > si.mur_lc_t.out
check_failure $?
$ECHO " done"

cat > cv.exp <<EOF
1   0.000007740
2   0.00006192
3   0.0002092
4   0.0004937
5   0.0009665
6   0.001669
7   0.002651
8   0.003960
9   0.005637
10  0.007740
15  0.03054
20  0.09456
25  0.2385
30  0.481
35  0.810
40  1.237
45  1.710
50  2.205
60  3.230
70  4.251
75  4.765
80  5.283
90  6.293
100 7.296
110 8.285
120 9.250
125 9.721
130 10.186
140 11.107
150 11.955
160 12.776
170 13.556
175 13.921
180 14.286
190 14.990
200 15.642
210 16.223
220 16.761
225 17.037
230 17.291
240 17.778
250 18.219
260 18.625
270 19.015
273.15  19.133
280  19.381
290  19.738
298.15  20.007
300   20.066
350  21.324
400  22.258
450  23.000
500  23.588
550  24.047
600  24.420
650  24.748
700  25.050
750  25.334
800  25.608
850  25.865
900  26.110
950  26.344
EOF

cat > anhar.exp <<EOF
13.536   0.00122
14.092   0.00135
15.504   0.00124
16.433   0.00092
17.499   0.00036
17.993  -0.00025
19.286  -0.00169
19.990  -0.00272
21.994  -0.00742
24.003  -0.01476
26.038  -0.02435
28.024  -0.03722
29.493  -0.04913
30.489  -0.05744
33.330  -0.08457
37.997  -0.13838
43.159  -0.20470
48.506  -0.2745
53.597  -0.3355
55.389  -0.3550
60.155  -0.4005
63.494  -0.4275
66.754  -0.4474
69.675  -0.4613
72.813  -0.4708
76.227  -0.4755
80.192  -0.4713
81.099  -0.4676
81.994  -0.4656
83.012  -0.4660
84.026  -0.4622
85.004  -0.4585
86.002  -0.4505
87.019  -0.4448
88.019  -0.4400
89.002  -0.4344
90.010  -0.4296
92.261  -0.4156
93.987  -0.3995
95.999  -0.3805
98.050  -0.3600
99.540  -0.3438
101.25  -0.3275
105.06  -0.2750
110.10  -0.2108
115.05  -0.1357
120.01  -0.0566
125.00  -0.0291
129.98   0.1191
134.77   0.2084
139.39   0.2926
144.63   0.3935
149.96   0.4963
154.90   0.5913
160.06   0.6905
165.63   0.7963
169.98   0.8768
174.91   0.9682
179.97   1.0623
185.21   1.1531
189.94   1.2350
194.98   1.3232
199.99   1.4043
204.96   1.4857
209.96   1.5651
214.99   1.6220
220.01   1.7170
225.01   1.7850
230.01   1.8543
235.01   1.9239
240.01   1.9856
245.00   2.0450
249.99   2.1094
255.00   2.1674
260.00   2.2248
265.00   2.2737
270.00   2.3292
275.00   2.3824
280.00   2.4327
285.01   2.4795
290.01   2.5270
295.00   2.5720
299.00   2.6184
304.99   2.6581
310.01   2.6953
315.00   2.7361
320.00   2.7708
325.00   2.8036
330.00   2.8440
335.00   2.8761
339.86   2.9074
100 -0.339
120 -0.057
140  0.306
160  0.689
180  1.061
200  1.406
220  1.715
240  1.986
260  2.223
280  2.432
300  2.616
320  2.772
340  2.909
360  3.041
380  3.153
400  3.253
500  3.614
600  3.842
700  4.016
800  4.151
EOF

$ECHO
$ECHO "$EXAMPLE_DIR: done"

