#!/bin/sh
# @configure_input@

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2011 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------

echo  "  ********************************************"
echo  "      Code_Saturne / Code_Aster coupling      "
echo  "  ********************************************"
echo
#
# Parameters common to Code_Saturne and Code_Aster
#
NBPDTM=10
NBSSIT=1
DTREF=0.0001
TTINIT=0.0
EPSILO=0.00001
#
# Code_Saturne parameters
#
SAT_PARAM=
SAT_MESH=
#
# Code_Aster parameters
#
AST_COMM=
AST_MESH=
AST_EXPORT=fsi.export
#
# Scratchdir (where the coupling application is going
# to be run)
#
TMP_PREFIX=/tmp
#
# =====================================================
# Initialization
# =====================================================
#
# Directory of the current study
#
STUDY_PATH=CASEDIRNAME
#
# Assign the various names used here (study, Code_Saturne
# case name and Code_Aster case name) to some variables
#
STUDY_NAME=STUDYNAME
SAT_NAME=CASENAME
AST_NAME=ASTERNAME
#
# Define a name for the directory 'RUN_DIR' where the
# coupling application will be run, and the
# corresponding path 'RUN_PATH'
#
# - if the scratchdir is not defined by the user then
#   use the user home as default scratchdir
#
if [ -z "$TMP_PREFIX" ]
then
    TMP_PREFIX=${HOME}
fi
#
# - define the whole path to the working directory
#
USER=`whoami`
DATE=`date '+%m%d%H%M'`
SCRATCH_DIR=tmp_Saturne
#
RUN_DIR=${STUDY_NAME}.${DATE}
RUN_PATH=${TMP_PREFIX}/${SCRATCH_DIR}/${RUN_DIR}
#
# Define the sub-directories where Code_Saturne and Code_Aster are run
#
AST_SCRATCH_PATH=${RUN_PATH}/${AST_NAME}
SAT_SCRATCH_PATH=${RUN_PATH}/${SAT_NAME}
#
# Define the results directories
#
RESULTS_DIR=`date '+%Y%m%d-%H%M'`
AST_RESULTS_DIR=${AST_NAME}
SAT_RESULTS_DIR=${SAT_NAME}
#
RESULTS_PATH=${STUDY_PATH}/RESU_COUPLING/${RESULTS_DIR}
AST_RESULTS_PATH=${RESULTS_PATH}/${AST_RESULTS_DIR}
SAT_RESULTS_PATH=${RESULTS_PATH}/${SAT_RESULTS_DIR}
#
# Code_Saturne installation parameters
#
prefix=/usr
bindir=/usr/bin
libdir=/usr/lib
pkgdatadir=/usr/share/code_saturne
pkglibexecdir=/usr/lib/code_saturne
salomepythondir=/usr/lib/python2.7/dist-packages/salome
#
# SALOME
#
salomeenv=""
salomekernelbindir=/usr/bin/salome
#
# Code_Aster
#
asterhome=ASTERHOME
#
# =====================================================
# Creation of the working directories
# =====================================================
#
cd ${TMP_PREFIX}
#
if [ ! -d ${SCRATCH_DIR} ]
then
    mkdir -p ${SCRATCH_DIR}
fi
#
# Verify that the workdir does not already exist
#
if test -d ${RUN_PATH}
then
    echo "Working directory $RUN_PATH already exists."
    echo "The simulation will not be run"
    exit 1
else
    mkdir -p ${RUN_PATH}
    echo "Working directory successfully created"
    echo "  - $RUN_PATH"
fi
#
# Create workdirs for Code_Saturne and Code_Aster
#
mkdir -p ${SAT_SCRATCH_PATH} ${AST_SCRATCH_PATH}
#
# Create intermediate script for YACS
#
mkdir -p ${RUN_PATH}/bin
#
fsisaturne=${RUN_PATH}/bin/FSI_SATURNE.exe
echo '#!/bin/sh' > $fsisaturne
echo 'export SALOME_CONTAINER=$1' >> $fsisaturne
echo 'export SALOME_CONTAINERNAME=$2' >> $fsisaturne
echo 'export SALOME_INSTANCE=$3' >> $fsisaturne
echo ${SAT_SCRATCH_PATH}'/run_solver.sh' >> $fsisaturne
#
fsiaster=${RUN_PATH}/bin/FSI_ASTER.exe
echo '#!/bin/sh' > $fsiaster
echo 'export SALOME_CONTAINER=$1' >> $fsiaster
echo 'export SALOME_CONTAINERNAME=$2' >> $fsiaster
echo 'export SALOME_INSTANCE=$3' >> $fsiaster
echo ${AST_SCRATCH_PATH}'/aster_by_yacs.sh' >> $fsiaster
#
chmod +x $fsisaturne $fsiaster
#
# =====================================================
# Setting up Code_Saturne temporary workdir
# =====================================================
#
echo "Setting up Code_Saturne workdir"
#
cd ${SAT_SCRATCH_PATH}
#
# Create Code_Saturne user source temporary directory
# ===================================================
#
mkdir -p src_saturne
#
# Get user sources
#
USER_SRC=${STUDY_PATH}/${SAT_NAME}/SRC/*.f90" "${STUDY_PATH}/${SAT_NAME}/SRC/*.h" "${STUDY_PATH}/${SAT_NAME}/SRC/*.c
#
for file in ${USER_SRC}
do
  if [ -f ${file} ]
      then
      cp -f ${file} src_saturne
  fi
done
#
# Compile Saturne
# ===============
#
${bindir}/code_saturne compile --source=src_saturne > compile.log 2>&1
#
if [ $? -ne 0 ]
    then
    echo "$SAT_NAME sources compilation or link error "
    echo "The simulation will not be run"
    rm -f *.o
    exit 1
else
    echo "  - $SAT_NAME sources compilation Ok"
fi
#
# Run the Saturne pre-processor
# =============================
#
cp ${STUDY_PATH}/MESH/${SAT_MESH} .
#
${pkglibexecdir}/cs_preprocess --out mesh_input --case ${SAT_NAME} ${COMMAND_REORIENT} ${SAT_MESH} > preprocessor.log 2>&1
#
if [ $? != 0 ]
    then
    echo "*********************************************"
    echo "Error running Code_Saturne preprocessor.     "
    echo "Check preprocessor log (listpre) for details."
    echo "     The simulation will not be run          "
    echo "*********************************************"
    echo
    exit 1
else
    echo "  - Normal end of CS preprocessor"
fi
#
# Copy of the parameter file
# ==========================
#
if [ ! -z "$SAT_PARAM" ]
    then
    file=${STUDY_PATH}/${SAT_NAME}/DATA/${SAT_PARAM}
    if [ -f ${file} ]
        then
        cp ${file} .
        file_tmp=${SAT_SCRATCH_PATH}/${SAT_PARAM}
        COMMAND_PARAM="--param $file_tmp"
    else
        echo '***********************************************'
        echo ' ERROR                                         '
        echo ' The parameters file ' ${file}
        echo ' can not be accessed.                          '
        echo ' The simulation will not be run                '
        echo '***********************************************'
        exit 1
    fi
fi
#
# Create the run_solver.sh file
#
runsolver=${SAT_SCRATCH_PATH}/run_solver.sh
echo '#!/bin/sh' > $runsolver
echo 'cd ..' >> $runsolver
echo ${SAT_SCRATCH_PATH}'/cs_solver \' >> $runsolver
echo $COMMAND_PARAM' \' >> $runsolver
echo '--yacs-module='$libdir'/salome/libFSI_SATURNEExelib.so' >> $runsolver
#
chmod +x $runsolver
#
# =====================================================
# Setting up Code_Aster temporary workdir
# =====================================================
#
echo "Setting up Code_Aster workdir"
#
if [ ! -z "$AST_COMM" ]
    then
    file=${STUDY_PATH}/${AST_NAME}/${AST_COMM}
    #
    if [ -f $file ]
        then
        cp $file $AST_SCRATCH_PATH
    else
        echo '***********************************************'
        echo ' ERROR                                         '
        echo ' The command file ' $file
        echo ' can not be accessed.                          '
        echo ' The simulation will not be run                '
        echo '***********************************************'
        exit 1
    fi
else
    echo "**************************************"
    echo "AST command file name is not specified"
    echo "The simulation will not be run        "
    echo "**************************************"
    exit 1
fi
#
if [ ! -z "$AST_MESH" ]
    then
    file=$STUDY_PATH/$AST_NAME/$AST_MESH
    #
    if [ -f $file ]
        then
        cp $file $AST_SCRATCH_PATH
        AST_MESH_GLOB_NAME=$AST_SCRATCH_PATH/$AST_MESH
    else
        echo '***********************************************'
        echo ' ERROR                                         '
        echo ' The command file ' $file
        echo ' can not be accessed.                          '
        echo ' The simulation will not be run                '
        echo '***********************************************'
        exit 1
    fi
else
    echo "*************************************"
    echo "AST mesh file name is not specified  "
    echo "The simulation will not be run       "
    echo "*************************************"
    exit 1
fi
#
if [ ! -z "$AST_EXPORT" ]
    then
    file=${STUDY_PATH}/${AST_NAME}/${AST_EXPORT}
    #
    if [ -f $file ]
        then
        cp $file $AST_SCRATCH_PATH
    else
        echo '***********************************************'
        echo ' ERROR                                         '
        echo ' The export file ' $file
        echo ' can not be accessed.                          '
        echo ' The simulation will not be run                '
        echo '***********************************************'
        exit 1
    fi
else
    echo "**************************************"
    echo "AST export file name is not specified "
    echo "The simulation will not be run        "
    echo "**************************************"
    exit 1
fi
#
# Create FSI specific config.txt file
#
sed -e "s|profile.sh|$asterhome/profile.sh|" $asterhome/config.txt \
    -e "s|Execution/E_SUPERV.py|$salomepythondir/FSI_ASTER_component.py|" \
    > $AST_SCRATCH_PATH/fsi_config.txt
#
# Create the aster_by_yacs.sh file
#
asterbyyacs=${AST_SCRATCH_PATH}/aster_by_yacs.sh
echo '#!/bin/sh' > $asterbyyacs
echo 'cd ..' >> $asterbyyacs
echo 'export PATH=/home/aster/bin:$PATH' >> $asterbyyacs
echo 'as_run '$AST_EXPORT >> $asterbyyacs
#
chmod +x $asterbyyacs
#
# =====================================================
# Starting calculation
# =====================================================
#
echo
echo  "  ********************************************"
echo  "               Starting calculation           "
echo  "  ********************************************"
echo
#
# Go to the working directory
#
cd $RUN_PATH
#
# Creating YACS coupling scheme
# =============================
#
echo "Creating YACS coupling scheme"
#
awk '{
       gsub(/@AST_WORKINGDIR@/,"'"$AST_SCRATCH_PATH"'",$0);
       gsub(/@COCAS_WORKINGDIR@/,"'"$RUN_PATH"'",$0);
       gsub(/@CFD_WORKINGDIR@/,"'"$SAT_SCRATCH_PATH"'",$0);
       gsub(/@AST_MAIL@/,"'"$AST_MESH_GLOB_NAME"'",$0);
       gsub(/@NBPDTM@/,"'"$NBPDTM"'",$0);
       gsub(/@NBSSIT@/,"'"$NBSSIT"'",$0);
       gsub(/@DTREF@/,"'"$DTREF"'",$0);
       gsub(/@TTINIT@/,"'"$TTINIT"'",$0);
       gsub(/@EPSILO@/,"'"$EPSILO"'",$0);
       gsub(/@COMM_FNAME@/,"'"$STUDY_PATH/$AST_NAME/$AST_COMM"'",$0);
       print
    }' \
    ${pkgdatadir}/salome/fsi_yacs_scheme.xml > fsi_yacs_scheme.xml
#
# Creating SALOME application
# ===========================
#
appli=$RUN_PATH/appli
#
$salomekernelbindir/appli_gen.py --prefix=$appli --config=$pkgdatadir/salome/fsi_appli_config.xml
#
# Create a localexec script
#
localexec=$RUN_PATH/localexec
echo '#!/bin/sh' > $localexec
echo ${salomeenv} >> $localexec
echo 'export PATH='${RUN_PATH}'/bin:$PATH' >> $localexec
echo 'appli='$appli >> $localexec
echo '$appli/runSession killSalome.py' >> $localexec
echo 'unset PYTHONHOME' >> $localexec
echo '$appli/runAppli -t' >> $localexec
echo '$appli/runSession $appli/bin/salome/driver -e -d 0 fsi_yacs_scheme.xml' >> $localexec
echo "exit \$?" >> $localexec
chmod 700 $localexec
#
# Excecute the script
#
./localexec
#
echo
echo  "  ********************************************"
echo  "               End of calculation            "
echo  "  ********************************************"
echo
#
# =====================================================
# End of calculation, copying results...
# =====================================================
#
echo "Copying results in ${RESULTS_PATH}"
#
cd ${STUDY_PATH}/RESU_COUPLING
#
mkdir -p ${AST_RESULTS_PATH}
mkdir -p ${SAT_RESULTS_PATH}
#
cp -r ${AST_SCRATCH_PATH}/* ${AST_RESULTS_PATH}
cp -r ${SAT_SCRATCH_PATH}/* ${SAT_RESULTS_PATH}
#
