#!/bin/bash

# A report file generated for instance with Monte-Carlo by Dinver
REPORT_FILE=~/courses/201903-Aquila/fieldday/site_2/site_2_reports/R0-R1-L0-core_strict.report

rm split*.model
gpdcreport $REPORT_FILE | awk 'BEGIN{fileNo=1}/#/{outf=sprintf("split%06i.model",fileNo);if(NR>1) fileNo++}!/#/{if(NF==1) print >> outf; else print $1,$2,$3,$4,100,50 >> outf}'

for MODEL_FILE in $(ls split*.model); do
  echo -n "$MODEL_FILE  "
  (
  echo $MODEL_FILE | ./psvsh1d | awk '!/#/{print $1,$7/2}' > cecile
  gpsh $MODEL_FILE -s frequency -nobugreport -qp 100 -qs 50 | grep -v "#" > cpp
  ) 2> /dev/null
  DIFF=$(paste cpp cecile | awk '{e+=($2-$4)*($2-$4)}END{print e}')
  echo $DIFF
  if [ "$(echo $DIFF | awk '{if($1>1e-8) print "stop"}')" = "stop" ]; then
    exit 2
  fi
done
exit 0
