#!/bin/bash
#
# run_kpp4palm - script for running gasphase preprocessor

#------------------------------------------------------------------------------#
# This file is part of the PALM model system.
#
# PALM 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 3 of the License, or (at your option) any later version.
#
# PALM 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
# PALM. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2017-2018  Klaus Ketelsen and MPI-CH (April 2007)
# Copyright 2017-2018  Karlsruhe Institute of Technology
# Copyright 2017-2018  Leibniz Universitaet Hannover
#------------------------------------------------------------------------------#
#
# Current revisions:
# ------------------
# 
# 
# Former revisions:
# -----------------
# $Id: run_kpp4palm.sh 4454 2020-03-11 11:47:22Z forkel $ 
# updated some comments
# 
# 
# 4451 2020-03-10 07:25:32Z raasch
# rename run_kpp4palm.ksh to run_kpp4palm.sh, convert to bash
# default mechanism changed to phstatp
# 
# 3487 2018-11-05 07:18:02Z maronga
# Some correctetion of comments
# 
# 3458 2018-10-30 14:51:23Z kanani
# passed $MECH insted of $DEFDIR to kpp4palm.ksh                   forkel  25. Sept. 2018
# re-activaded  choice of deindexing method and vector mode        forkel  June 2018
# 
# 2718 2018-01-02 08:49:38Z maronga
# Initial revision
# 
# 
#
#
# Other notes:
# ------------
# Small adaptations (added MECH as argument, re-introduced relative path,
#                    adaption to changing kp4 to kpp4palm)
# filename changed from run_kp4.ksh to run_kpp4palm.ksh
#
# Nov. 2016: Initial Version of KPP chemistry by Klaus Ketelsen
#

echo "\$1 = " $1 

if [[ $1 == "clean" ]]
then
  cd kpp
  make distclean
  cd ..

  cd kpp4palm
  make distclean                 
  cd ..
  exit
fi

export PATH=$PATH:`pwd`/kpp4palm/bin

# build kpp

cp mechanisms/UserRateLaws.f90 kpp/util
cd kpp
make
cd ..

# build kpp4palm.exe

cd kpp4palm
make install                    
cd ..

# run kpp4palm/bin/kpp4palm.sh with the following arguments
#
#    -m   MECH="phstatp";;          # chemical mechanism 
#                                     (phstat, phstatp, smog, simple,cbm4, etc, see directory mechanisms)
#    -i   DE_INDEX=2;;              # deindexing method (0,1,2,3; 0=no deindexing)
#    -v   MODE="-v";;               # vector mode on (not working completely yet)
#    -l   VLEN=$OPTARG;;            # Set vector length (not working completely yet)
#    -k   KEEP="NO";;               # if "yes" keep Working directory
#    -u   UPDT="NO";;               # if "yes" update the existing f90 code in the def_MECH directory
#                                   # Use "YES" with care as the existing sample file is overwritten
#
##   Further options (currently not passed as changing does not make sense to change these)
##    -f   DE_INDEX_FAST="YES";;     # if set, fast deindexing
##    -o   OUTDIR=$OPTARG;;          # Output directory of Generated Code
##                                     (`pwd`/../../../SOURCE)
##    -p   PREFIX=$OPTARG;;          # Name Prefix (chem_gasphase_mod)
##    -s   KPP_SOLVER=$OPTARG;;      # Name Prefix (rosenbrock)

 
echo $PATH
# use phstatp (photo-stationary) mechanism as default
MECH=phstatp
DEIN=2

while  getopts m:i:kuvl:  opt   # get options
do case $opt in
      m)   MECH=$OPTARG;;          # mechanism name as part of mechanisms/def_[mechanism_name]

      i)   DEIN=$OPTARG;;          # deindexing method (0,1,2,3; 0=no deindexing)

      k)   KEEP="-k";;             # keep Working directory

      u)   UPDT="-u";;             # update sample output file

      v)   MODE="-v";;             # vector mode on 

      c)   COPY="-c";;             # copy output to def_MECH

      l)   VLEN=$OPTARG;;          # Set vector length

      \?)  print ${0##*/} "unknown option:" $OPTARG
           print "USAGE: ${0##*/} [ -m mechanism_name] [ -i n (n=0,1,2,3)] [ -k] [ -u] [ -v] [ -l N (N=vector length)]"
           exit 1;;
   esac
done
echo Now do kpp4palm for $MECH

kpp4palm.sh -m $MECH -i $DEIN $KEEP $UPDT $MODE -l $VLEN
