#!/bin/bash --login
#$ -cwd          ## Current directory
#$ -m ea 
#$ -pe mpi-24-ib.pe 48

module load apps/binapps/abaqus/2020
module load apps/binapps/abaqus/2020 compilers/intel/17.0.7
# Ensure licenses are available. If not, job will automatically requeue.
. $ABAQUS_HOME/liccheck.sh

function amend_DFLUX(){
    bead_length=$1
    arc_speed=$2
    heat_input=$3
    bead_length_1=$(echo "scale=3;$bead_length/1000"|bc)
    welding_time=$(echo "scale=5;$bead_length/$arc_speed*1000"|bc)
    arc_speed_1=$(echo "scale=5;$arc_speed/1000000"|bc)
    sed -i 's/^.*bead_length =.*$/\tbead_length = '$bead_length_1'/' UFLUX.for
    sed -i 's/^.*arc_speed =.*$/\tarc_speed = '$arc_speed_1'/' UFLUX.for
    sed -i 's/^.*heat_input =.*$/\theat_input = '$heat_input'/' UFLUX.for
    sed -i 's/^.*welding_time =.*$/\twelding_time = '$welding_time'/' UFLUX.for
}


PRONAME=`basename $0`
CURPATH=$(cd "$(dirname "$0")"; pwd)
for file in `find . -name "*.inp"`; do
# for file in $CURPATH/*.inp; do 
    temp_file=`basename $file  .inp`  
    
    for odb in `find . -name "*.odb"`; do
        odb_file=`basename $odb  .odb`
        if [ "$odb_file" == "$temp_file" ]
        then
            continue 2
        fi
    done
    para_array=(${temp_file//-/ })
    unset para_array[0]
    amend_DFLUX ${para_array[4]} ${para_array[5]} ${para_array[6]}
    abq2020 job=$temp_file user=UFLUX.for cpus=$NSLOTS interactive
done
