#!/bin/sh

# -------------------------
# INPUT FILE PRE-PROCESSING
# -------------------------

# This demo does not need file pre-processing, but normally (see
# below) APREPRO or DPREPRO is used to "cut-and-paste" data from the
# params.in.# file written by DAKOTA into a template input file for
# the user's simulation code.

# aprepro run6crh_rigid_template.i temp_rigid.new
# grep -vi aprepro temp_rigid.new > run6crh_rigid.i

dprepro $1 transportProperties.template transportProperties.in

# For this example we just prepare the application input by copying
# the parameters:
cp transportProperties.in constant/transportProperties


# -------------------
# RUN SIMULATION CODE
# -------------------

source $OPENFOAM_ROOT/etc/bashrc

blockMesh
decomposePar -force

echo "$0 running application on $SLURM_NTASKS processors."

# NOTE: for simple, parallel apps, the "/path/to" might simply be $topdir
#       (see comments at top of this file regarding modules and consistent MPI)
srun icoFoam -parallel

# use sleep command if file I/O timing is a problem
#sleep 10


# ---------------------------
# OUTPUT FILE POST PROCESSING
# ---------------------------

reconstructPar

awk '/value/ {print $2} ' 0.1/uniform/cumulativeContErr  | tr ";" " " > results2dakota
mv results2dakota $2

# When using DAKOTA's fork interface, the application can directly
# write its output (if in the right format) to results.out.$num
# (../$2) for DAKOTA, however for the system interface, use the
# following move to avoid a race condition:

#mv results2dakota ../$2
#cd ..

# -------------
# CLEANUP
# -------------

# uncomment to cleanup work directories as evaluations progress
#rm -rf ./workdir.$num
#rm ./results.out.$num

