#!/bin/csh  -f
################################################################
# vasp is implied; correct appropriately if another DFT is used
################################################################
# script for collecting forces into F_irrep#.mat file.
#
# OUTCAR files should be sorted in directories irrep_#/d=ampl
# (ampl - value of the amplitude)
# using the name of the input file (= vector), e.g. 
# e.g. OUTCAR_1, OUTCAR_2, etc.
################################################################
# several amplitudes possible for every irrep
################################################################

# input: irrep

#echo '>>>>> Specify irrep: <<<<<<'
#set ir=($<)  
set ir=$1
set dir0=irrep_$ir

#check if file F_$ir.mat exists
if (-e   F_$ir.mat) /bin/rm  F_$ir.mat   
if (-e   tmp.f ) /bin/rm  tmp.f

###################################################
######  checking here first #######################
###################################################

#[1]#### check if directory exists
if (-ed  $dir0 ) then
    echo directory $dir0 exists
else
    echo directory $dir0 does NOT exist. Exit.
    exit
endif

#[2]#### check if there are directories inside; these
######## will also give the number of amplitudes

set dir=`ls -d $dir0/d=*`
set n_amp=`echo $dir|wc -w`

echo '>>>>> All available amplitudes: <<<<<<'
echo directory $dir
echo 'Number of amplitudes found = ' $n_amp
if( $n_amp == 0 ) then
   echo 'No amplitudes found. Exit.'
   exit
endif

#[3]#### check if there are OUTCAR files inside; their
######## number will give the dimension of this irrep.
######## Enough to check just one amplitude directory

set ndim=`ls $dir[1]/OUTCAR_* | wc -w` 
if ($ndim == 0) then
  echo 'Directory empty, no OUTCAR files found. Exit'
  exit
endif
echo Dimension of the space is $ndim

###########################################################
######## start calculation ################################
###########################################################

echo '# amplitudes ' $n_amp  > F_$ir.mat
# loop over all vectors in the space 

set n=0

start:
  @ n = $n + 1
  if ( $n > $ndim ) goto finish 
  echo $n $ndim
  
# loop over all available displacements

  set m=0
  amplitudes:

    @ m = $m + 1
    if ( $m > $n_amp ) goto finish_amp 

    echo '# ' $n $m $dir[$m]  >> F_$ir.mat

    if( -e $dir[$m]/OUTCAR_$n ) then
       echo 'getting forces from ' $dir[$m]/OUTCAR_$n
       $HOME_TETR/get_forces T $dir[$m]/OUTCAR_$n > er.file
       cat er.file
       set er=`grep ERROR er.file | wc -w `
       if ( $er != 0 ) then
          echo ERROR in getting forces. Exiting
          /bin/rm  F_$ir.mat tmp.f
          exit
       endif
       cat tmp.f >> F_$ir.mat
       /bin/rm tmp.f
    else
       echo 'file ' $dir[$m]/OUTCAR_$n 'does not exist'
       /bin/rm F_$ir.mat
       exit
    endif
    goto amplitudes

# finish the loop over amplitudes

  finish_amp:

goto start 

# finish the loop over vectors

finish:
echo '# end ' >> F_$ir.mat
/bin/rm tmp.f er.file

