#!/bin/bash
#---------------------------------*- sh -*-------------------------------------
# ==  == ====== ====   ====    |
#                   \\     ||  | Multiphase Cases Repository by HZDR
# ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.811
# ||  ||  //    ||  // || \\   | License: Creative Commons Attribution 4.0
# ==  == ====== ====   ==  ==  |
#------------------------------------------------------------------------------
# License
#     This file is part of the Multiphase Cases Repository by HZDR.
#
#     Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
#     Website: https://hzdr.de
#
#     The Multiphase Cases Repository by HZDR is licensed under the
#     Creative Commons Attribution 4.0 International License. To view a copy of
#     this license, visit http://creativecommons.org/licenses/by/4.0/ or send a
#     letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
#
# Description
#     Collect data for post-processing
#
#------------------------------------------------------------------------------

# initialize output file
outfile="caseStudyResults.csv"
rm -f $outfile
echo "hullVelocity massFlowRate drag totalVolumeContinuousGas" \
    >> $outfile # write header

for caseDir in ./case_*/ # for each case directory
do
    caseDir=${caseDir%*/}     # remove the trailing "/"
    caseDir=${caseDir##*/}    # print everything after the final "/"

    echo "$caseDir"

    # extract case parameters from setup
    hullVelocity=$(foamDictionary -entry hull/velocity -value \
        "$caseDir/constant/caseSettings")
    massFlowRate=$(foamDictionary -entry injector/massFlowRate -value \
        "$caseDir/constant/caseSettings")

    # extract quantity of interest from results file
    file="$caseDir/postProcessing/drag/0/surfaceFieldValue.dat"
    drag=$(tail -n 1 "$file" | awk '{print $6}') # last line, column 6

    file="$caseDir/postProcessing/volumeAirC/0/volFieldValue.dat"
    # last line, column 2
    totalVolumeContinuousGas=$(tail -n 1 "$file" | awk '{print $2}')

    # add line to file
    echo "$hullVelocity $massFlowRate $drag $totalVolumeContinuousGas" \
        >> $outfile

done

echo "$outfile"