#!/bin/csh -f
# Sample simulator to Dakota system call script
# See Advanced Simulation Code Interfaces chapter in Users Manual

# $argv[1] is params.in.(fn_eval_num) FROM Dakota
# $argv[2] is results.out.(fn_eval_num) returned to Dakota

# ------------------------
# Set up working directory
# ------------------------

set num = `echo $argv[1] | cut -c 11-`
mkdir workdir.$num
# or for applications requiring the template files in the working directory
#cp -r templatedir workdir.$num

cd workdir.$num

# --------------
# PRE-PROCESSING
# --------------
# Incorporate the parameters from DAKOTA into the template, writing ros.in
# Use the following line if SNL's APREPRO utility is used instead of DPrePro.
# ../aprepro -c '*' -q --nowarning ros.template ros.in

../dprepro ../$argv[1] ../templatedir/ros.template ros.in

# --------
# ANALYSIS
# --------

../rosenbrock_bb

# ---------------
# POST-PROCESSING
# ---------------

# extract function value from the simulation output
grep 'Function value' ros.out | cut -c 18- >! $argv[2]
# extract gradients from the simulation output (in this case will be ignored
# by DAKOTA if not needed)
grep -i 'Function g' ros.out | cut -c 21- >> $argv[2] 

# moving $argv[2] at the end avoids any problems with read race conditions
mv $argv[2] ../.

# --------
# Clean up
# --------

cd ..
#\rm -rf workdir.$num
