#*/
#* Copyright (c) 2011 Gitanjali Yadav (gy@nipgr.res.in)
#* All rights reserved.
#*
#* Redistribution and use in source and binary forms, with or without
#* modification, are permitted without a fee provided that the following
#* conditions are met:
#*
#* 1. This software is only used for private, research, or academic 
#*    purposes.
#*    
#* 2. Redistributions of source code must retain the above copyright
#*    notice, this list of conditions and the following disclaimer.
#*
#* 3. Redistributions in binary form must reproduce the above copyright
#*    notice, this list of conditions and the following disclaimer in the
#*    documentation and/or other materials provided with the distribution.
#*
#* 4. Any changes made to this package must be submitted to the author.
#*    The legal status of the submitted changes must allow their inclusion
#*    into this package under this license.
#*
#* 5. Publications in the field of pseudorandom number generation, which
#*    made use of this package must include a reference to this package.
#*      
#* Any use of this software in a commercial environment requires a
#* written licence from the author. Contact Gitanjali Yadav
#* (gy@nipgr.res.in) to negotiate the terms.
#*
#* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
#* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
#* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#* 
#* IN NO EVENT SHALL GITANJALI YADAV BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
#* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES 
#* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
#* NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
#* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
#* OF THIS SOFTWARE.
#*
#*/
#*
#*/
#*  run_random_cascade 	For Simulating a New Random Sequence of Node Deletions  
#*
#*  Author:	Gitanjali Yadav (gy@nipgr.res.in)
#*
#*  Last Modification: Wed May 23 12:50:35 CEST 2012
#*
#*  USAGE: ' ./run_random_cascade ' 
#*/

#*/ Modification History:

#   12/10/05: First Version.
#   22/10/05: A New Random Cascade And Comparative Plots
#*/

echo ""
echo ===== SIMULATING NEW RANDOM CASCADE ===
rm -f i* 
echo ALL >isp_id
echo DEGREERandom >itype_id
cat DATA.nodes | awk 'BEGIN{srand();}{print rand()"\t"$0}' | sort -k1 -n | cut -f2- | sed 's/ //g' >DATA_DEGREERandom_ALL.cascade
cp DATA_DEGREERandom_ALL.cascade iextinction.cascade 
echo cp start.tmp itotal_extinct >irun_extinction
echo rm -f iprimary_extinctions >>irun_extinction
echo cp DATA.ncol inetwork_new >>irun_extinction
sed 's/^/.\/get_perturbation_network "/' iextinction.cascade | sed 's/$/"/'  >irunfile
paste -d: irunfile serial_number | grep -v '^:' | sed 's/:/    /' >>irun_extinction
chmod a+x irun_extinction
cat header_line >ioutfile1
rm -f iextinct_list
./irun_extinction >>ioutfile1
cat ioutfile1 > DATA_DEGREERandom_ALL.out
cat DATA_DEGREERandom_ALL.out | cut -f1,3,6 | grep -w 0 | awk '{print $2}' >iend
echo THE NETWORK COLLAPSES COMPLETELY IN `cat iend` SEQUENTIAL PERTURBATIONS
echo ===== CASCADE COMPLETE! ===========
echo ""
rm -f i*
echo ===== NOW PLOTTING SIMULATION RESULTS ===========
grep -v ALREADY DATA_DEGREERandom_ALL.out | grep -v Network >out1
sed 's/H/1/g' run_plot_result.R | sed 's/OUTFILE/out1/g' | sed 's/OUT/DEGREE_MaxMin/g' | sed 's/DIRECTION/SEQUENCE: HIGHEST TO LOWEST DEGREE/' >runplot_tmp.R
`cat Rpath` --no-restore --no-save --no-readline --slave < runplot_tmp.R >>R.out
cat header_line out1 >DATA_DEGREERandom_ALL.result
echo ===== SINGLE SERIES PLOTS COMPLETE! ========================
cat outline1 DATA_DEGREEMinMax_ALL.out | grep -v ALREADY | grep -v Network >out1
cat outline1 DATA_DEGREERandom_ALL.out | grep -v ALREADY | grep -v Network >out2
sed 's/H1/3/g' run_plot_result2.R | sed 's/H2/1/g' | sed 's/OUT_/DEGREE_Pair_MinMax_Random_/g'  | sed 's/FIRST/LOWEST TO HIGHEST/' | sed 's/SECOND/RANDOM/' >runplot2.R
`cat Rpath` --no-restore --no-save --no-readline --slave < runplot2.R >>R.out
cat outline1 DATA_DEGREEMaxMin_ALL.out | grep -v ALREADY | grep -v Network >out1
sed 's/H1/2/g' run_plot_result2.R | sed 's/H2/1/g' | sed 's/OUT_/DEGREE_Pair_MaxMin_Random_/g'  | sed 's/FIRST/HIGHEST TO LOWEST/' | sed 's/SECOND/RANDOM/' >runplot2.R
`cat Rpath` --no-restore --no-save --no-readline --slave < runplot2.R >>R.out
echo ===== PAIRWISE SERIES PLOTS COMPLETE! ======================
cat outline1 DATA_DEGREEMinMax_ALL.out | grep -v ALREADY | grep -v Network >out1
cat outline1 DATA_DEGREEMaxMin_ALL.out | grep -v ALREADY | grep -v Network >out2
cat outline1 DATA_DEGREERandom_ALL.out | grep -v ALREADY | grep -v Network >out3
sed 's/H1/3/g' run_plot_result3.R | sed 's/H2/2/g' | sed 's/H3/1/g' | sed 's/OUT_/DEGREE_All_/g' >runplot3.R
`cat Rpath` --no-restore --no-save --no-readline --slave < runplot3.R >>R.out
echo ===== ALL SERIES PLOTS COMPLETE! ========================
echo ""
