#!/usr/bin/env perl

# The input arguments to this script are set up in 
# Xyce_Test/TestScripts/tsc_run_test_suite and are as follows:
# $ARGV[0] = location of Xyce binary
# $ARGV[1] = location of xyce_verify.pl script
# $ARGV[2] = location of compare script  (This is NEVER used and can be ignored)
# $ARGV[3] = location of circuit file to test
# $ARGV[4] = location of gold standard prn file

# This test case runs two HB netlists, one with normal Newton solve, and
# one with natural parameter continuation applied to a global parameter
# during the HB solve phase.

# The test is that both runs produce the same solution (in frequency and
# time domains) at the end.

# in Xyce 6.2 and earlier, Xyce would exit with an error
# when trying to apply the continuation.  During part of the development 
# of bug 573, but after fixing it partially, the solution would be wrong
# because the DC source would not have been swept properly.

$XYCE=$ARGV[0];
$XYCE_VERIFY=$ARGV[1];
$CIRFILE=$ARGV[3]; 
#$GOLDPRN=$ARGV[4];  # There is no gold standard.

$CIRFILE =~ s/.cir/_hb.cir/;
$CIRFILE2 = $CIRFILE;
$CIRFILE2 =~ s/_hb/_hb_contin/;

$AC_COMPARE=$ARGV[1];
$AC_COMPARE =~ s/xyce_verify/ACComparator/;

# Now run Xyce on the netlists
$CMD="$XYCE $CIRFILE > $CIRFILE.out 2> $CIRFILE.err";
if (system($CMD) != 0)
{
    print "Xyce EXITED WITH ERROR! on $CIRFILE\n";
    $xyceexit=1;
}
else
{
    if (-z "$CIRFILE.err" ) {`rm -f $CIRFILE.err`;}
}


if (defined ($xyceexit)) {print "Exit code = 10\n"; exit 10;}

$CMD="$XYCE $CIRFILE2 > $CIRFILE2.out 2> $CIRFILE2.err";
if (system($CMD) != 0)
{
    print "Xyce EXITED WITH ERROR! on $CIRFILE2\n";
    $xyceexit=1;
}
else
{
    if (-z "$CIRFILE2.err" ) {`rm -f $CIRFILE2.err`;}
}


if (defined ($xyceexit)) {print "Exit code = 10\n"; exit 10;}

# Now compare the two .HB.TD files with xyce_verify

$CMD="$XYCE_VERIFY $CIRFILE $CIRFILE.HB.TD.prn $CIRFILE2.HB.TD.prn > $CIRFILE.prn.out 2> $CIRFILE.prn.err";
if (system($CMD) != 0)
{
    print "Time domain compare failed \n";
    print "Exit code = 2\n";
    exit 2;
}

$CMD="$AC_COMPARE $CIRFILE.HB.FD.prn $CIRFILE2.HB.FD.prn >> $CIRFILE.prn.out 2>> $CIRFILE.prn.err";
if (system($CMD) != 0)
{
    print "Frequency domain compare failed \n";
    print "Exit code = 2\n";
    exit 2;
}
else
{
    if (-z "$CIRFILE.prn.err" ) {`rm -f $CIRFILE.prn.err`;}
}

print "Exit code = 0\n";
exit 0;
