#!/bin/bash
#
# Copyright (c) 2020 Skycloud Software Corporation
#
# This script runs Fluent in command line mode
#
#----------------- Change the following line according your environment ------

fluent_cmd="/opt/apps/ansys_inc/v195/fluent/bin/fluent -lsf -rshlsf -mpi=intel -g -pdefault"

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


usage() {
    echo "Usage: $0 2d|2dd|3d|3dd [-c cas_file_path [-d dat_file_path]] | [-j journey_file_path] [-meshing] [-t iteration(default:1000)]"
    exit 1
}

iteration=1000
while [ $# != '0' ]; do
    case $1 in
    *) optarg=$2;;
    esac

    case $1 in
    2d)
        solver=$1
        ;;
    2dd)
        solver=$1
        ;;
    3d)
        solver=$1
        ;;
    3dd)
        solver=$1
        ;;
    -c)
        case=$optarg
        shift
        ;;
    -d)
        dat=$optarg
        shift
        ;;
    -j)
        journal=$optarg
        shift
        ;;
    -t)
        iteration=$optarg
        shift
        ;;
    -meshing)
        meshing=$1 
        ;;
    -h)
        usage ;;
    *)
        echo "ERROR: invalid option"
        usage ;;
    esac
    shift
done

if [ _$solver == '_' ]; then
    echo 'Please specify solver'
    exit 1
fi

if [ _$journal == '_' ]; then
    if [ _$case == '_' ]; then
        echo 'CAS file missing. At least Journel file or case file needs to be provided.'
        exit 1
    fi
    journal=`pwd`/fluent_$CB_JOBID.jou
    if [ _$dat == '_' ]; then
        # cas file only
        echo "/file/read-case" $case > $journal
        echo "/solve/init/init" >> $journal
    else
        echo "/file/read-case" $case > $journal
        echo "/file/read-data" $dat >> $journal
    fi
    echo "/solve/iterate" $iteration >> $journal
    echo "yes" >> $journal
    echo "exit" >> $journal
    echo "OK" >> $journal
fi
fluent_cmd="$fluent_cmd $solver $meshing -i $journal"
cpost -d "$fluent_cmd" $CB_JOBID
exec $fluent_cmd
