#!/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
X=0.5
Y=0.5
Z=0.5
SOURCES_AND_BOUNDARIES="CUBE.VP=12  LTEMP.T=290   RTEMP.T=310   ADIA.F=5.2"
GREEN_FILE="probe_X${X}-Y${Y}-Z${Z}_N${NREAL}.green"
SETTINGS_FILE=settings.txt
### END USER PARAMETERS SECTION

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

if [ -f "${GREEN_FILE}" ]; then
  printf '\n'
  printf '>>> %s exists.\n' "${GREEN_FILE}"
  printf '>>> Compute probe temperature by applying new source and boundary to the Green function.\n'
  printf '\n'
  printf '%s\n' "${SOURCES_AND_BOUNDARIES}" > "${SETTINGS_FILE}"
  sgreen -g "${GREEN_FILE}" -a "${SETTINGS_FILE}" -e
  printf '\n'

else
  printf '\n'
  printf '>>> %s does not exist.\n' "${GREEN_FILE}"
  printf '>>> Need to compute the Green function with stardis.\n'
  printf '\n'

  #launch stardis to produce the Green function for the probe X,Y,Z at steady state
  stardis -V3 -M model.txt -p "${X},${Y},${Z}" -n "${NREAL}" -G "${GREEN_FILE}"

  printf '\n'
  printf '>>> Now, compute probe temperature by applying new source and boundary to the Green function.\n'
  printf '\n'
  printf '%s\n' "${SOURCES_AND_BOUNDARIES}" > "${SETTINGS_FILE}"
  sgreen -g "${GREEN_FILE}" -a "${SETTINGS_FILE}" -e
  sgreen -g "${GREEN_FILE}" -s "${GREEN_FILE}.html"
  printf '\n'
  printf '>>> A html report (%s.html) was also produced about the Green function.\n' "${GREEN_FILE}"
  printf '\n'
fi
