
#======================================================================
# Test setup:
#======================================================================

cd $CASEROOT

# Reset all previous env_mach_pes settings
if ( -e env_mach_pes.xml.1 )  then
  cp -f env_mach_pes.xml.1  env_mach_pes.xml
  cp -f env_mach_pes.xml.1  LockedFiles/env_mach_pes.xml.locked
else
  echo " ERROR: env_mach_pes.xml.1 does not exists" 
  echo "   this would been produced in the build - must run $CASE.test_build"
  exit -1
endif

# note - if you change the env_mach_pes.xml file - should always
# rerun the following two cesm_setup commands to ensure that the right
# settings are in the run script
# note that the following two commands will eliminate all the batch files except
# for the test file and copy the env_mach_pes.xml to the LockedFiles directory
./cesm_setup -clean -testmode
./cesm_setup

# Set env_run.xml values for run
set STOP_N      = `./xmlquery STOP_N      -value`
set STOP_OPTION = `./xmlquery STOP_OPTION -value`

./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE
./xmlchange -file env_run.xml -id REST_OPTION  -val never
./xmlchange -file env_run.xml -id HIST_OPTION  -val ${STOP_OPTION}
./xmlchange -file env_run.xml -id HIST_N       -val ${STOP_N}

#======================================================================
# (1) Test run:
# do an initial run test with default thread settings
#======================================================================

cd $CASEROOT

echo "" >>& $TESTSTATUS_LOG 
echo " doing a ${STOP_N} ${STOP_OPTION} multi threaded test" >>& $TESTSTATUS_LOG 
echo "   no restarts are written" >>& $TESTSTATUS_LOG 

./$CASE.run
if ($status != 0) then
    echo " ERROR: $CASE.run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

set CplLogFile = `ls -1t $RUNDIR/cpl.log* | head -1`
if ( $?CplLogFile ) then
    if (-e $CplLogFile) then
       set pass = `zgrep "SUCCESSFUL TERMINATION" $CplLogFile | wc -l`
       if ($pass != 1) then
           echo "ERROR: coupler log $CplLogFile indicates model run failed" >>& $TESTSTATUS_LOG 
 	   exit -1
       else 
           echo "Success: test log is $CplLogFile" >>& $TESTSTATUS_LOG 
       endif
    endif
else
    echo "ERROR: no coupler log created, model run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

set tput = `zgrep "# simulated years " $CplLogFile`
echo "multi threaded tput = $tput" >>& $TESTSTATUS_LOG

echo "" >>& $TESTSTATUS_LOG
echo "moving relevant history files to suffix with command " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix base $add_iop" >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "base" $add_iop

#======================================================================
# (2) Test run: 
# do another initial with nthrds 1
#======================================================================

cd $CASEROOT

echo " doing a ${STOP_N} ${STOP_OPTION} single threaded test" >>& $TESTSTATUS_LOG 
echo "   no restarts are written" >>& $TESTSTATUS_LOG 

./xmlchange -file env_mach_pes.xml -id NTHRDS_ATM -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_LND -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_ROF -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_WAV -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_GLC -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_OCN -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_ICE -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_CPL -val 1

cp -f env_mach_pes.xml LockedFiles/env_mach_pes.xml.locked

./$CASE.run
if ($status != 0) then
    echo " ERROR: $CASE.run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

set CplLogFile = `ls -1t $RUNDIR/cpl.log* | head -1`
if ( $?CplLogFile ) then
    if (-e $CplLogFile) then
       set pass = `zgrep "SUCCESSFUL TERMINATION" $CplLogFile | wc -l`
       if ($pass != 1) then
           echo "ERROR: coupler log $CplLogFile indicates model run failed" >>& $TESTSTATUS_LOG 
 	   exit -1
       else 
           echo "Success: test log is $CplLogFile" >>& $TESTSTATUS_LOG 
       endif
    endif
else
    echo "ERROR: no coupler log created, model run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

set tput = `zgrep "# simulated years " $CplLogFile`
echo "single threaded tput = $tput" >>& $TESTSTATUS_LOG

echo "" >>& $TESTSTATUS_LOG
echo "moving relevant history files to suffix with command " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix 1thread $add_iop" >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix 1thread $add_iop

#======================================================================
# Test status check:
#======================================================================

set CPLLOG = $CplLogFile

echo "DONE ${CASEBASEID} : ($msg finished, successful coupler log) " >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

$SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 1thread $add_iop -msg "$msg" >>& $TESTSTATUS_OUT




