#!/bin/sh

# Copyright (C) 2021, 2022, 2026 |Meso|Star> (contact@meso-star.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

set -e

### USER PARAMETERS SECTION
NREAL=10000
TIME="10 50 100 200 300 400"
FILE="stardis_result_N${NREAL}.txt"
### END USER PARAMETERS SECTION

#erase FILE result if exists
rm -f "${FILE}"

#check stardis installation
if ! command -v stardis > /dev/null ; then
  >&2 printf '>>> stardis command not found !\n'
  >&2 printf '>>> To register stardis in the current shell you must type :\n'
  >&2 printf '>>> . ~/Stardis-XXX-GNU-Linux64/etc/stardis.profile\n'
  >&2 printf '>>> where ~/Stardis-XXX-GNU-Linux64 is the stardis directory installation\n'
  exit 1
fi

#launch Stardis for each defined TIME
printf '#time Temperature  errorbar  N_failures N_Realizations\n' >> "${FILE}"
for i in ${TIME}; do
printf '%s  ' "${i}"  >> "${FILE}"
stardis -V 3 -M model.txt -p 0.5,0.5,0.5,"${i}"  -n "${NREAL}" >> "${FILE}"
done

printf '\n'
printf '>>> Stardis simulation done\n'
printf '>>> Results are writte in %s \n' "${FILE}"
printf '\n'

#plot result with gnuplot
if command -v gnuplot > /dev/null
then
  {
    printf 'plot '\''analytical_T.txt'\'' w l title '\''Analytical'\''\n'
    printf 'replot '\''%s'\'' u 1:2:3 w yerrorbar title '\''Stardis'\''\n' "${FILE}"
    printf 'pause -1\n'
  } > plot.gp
  gnuplot plot.gp
else
  >&2 printf 'gnuplot is not install on your system. You can install gnuplot or view the simulation results in other tool.\n'
fi
