#! /usr/bin/env python3
#-------------------------------*- python -*-----------------------------------
# ==  == ====== ====   ====    |
#                   \\     ||  | Multiphase Code Repository by HZDR
# ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.767
# ||  ||  //    ||  // || \\   | License: GPL-3.0-or-later
# ==  == ====== ====   ==  ==  |
#------------------------------------------------------------------------------
# License
#     This file is part of the Multiphase Code Repository by HZDR.
#
#     Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
#     Website: https://hzdr.de
#
#     Multiphase Code Repository by HZDR is based on the free software for
#     computational fluid dynamics (CFD) from the OpenFOAM Foundation.
#     Copyright (C) 2025 by OpenFOAM Foundation, Website: https://openfoam.org
#
#     If you are interested in which files are original OpenFOAM Foundation
#     files, which OpenFOAM Foundation files were modified, and which files were
#     newly created, see FILES.md.
#
#     Multiphase Code Repository by HZDR 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.
#
#     Multiphase Code Repository by HZDR 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 the Multiphase Code Repository by HZDR. If not, see
#     <http://www.gnu.org/licenses/>.
#
#------------------------------------------------------------------------------

"""Calculate analytical solution for Taylor Green Vortex"""

# pylint: disable=invalid-name,redefined-outer-name

# load modules
import os
import shutil
import numpy as np
from numpy import pi, cos, sin, exp

# set output directory
outdir = "postProcessing_analytical"

# probe locations and instant point of time
x0      = 0.
y0      = 0.5*pi
x1      = 0.
y1      = 0.
t       = 0.5

# thermophysical properties
nu      = 0.01
rho     = 1

# create arrays for changing variables t and y
time    = np.linspace(0, t, num=201)
yline   = np.linspace(-0.5*pi, 0.5*pi, num=1001)

# analytical solution functions for TaylorGreenVortex
def F(t):
    """temporal term"""
    return exp(-2.*nu*t)


def u(x, y, t):
    """u component of velocity"""
    return cos(x)*sin(y)*F(t)


def v(x, y, t):
    """v component of velocity"""
    return -sin(x)*cos(y)*F(t)


def p(x, y, t):
    """pressure"""
    return -rho/4.*(cos(2.*x) + cos(2.*y))*F(t)**2


def zero(arr):
    """return an array of zeros for the third spatial direction"""
    return 0.*arr


# remove relevant directories, if existent
if os.path.isdir(outdir):
    shutil.rmtree(outdir)

path = f'{outdir}/probes/0'
os.makedirs(path)

# save probes of velocity
ut = np.column_stack((
    time,
    u(x0, y0, time), v(x0, y0, time), zero(time),
    u(x1, y1, time), v(x1, y1, time), zero(time)
))
np.savetxt(path + '/U.gas1', ut,
           fmt='%1.7e (%1.7e %1.7e %1.7e) (%1.7e %1.7e %1.7e)')

# save probes of pressure
pt = np.column_stack((
    time,
    p(x0, y0, time),
    p(x1, y1, time)
))
np.savetxt(path + '/p_rgh', pt)

path = f'{outdir}/graph/{t:g}'
os.makedirs(path)

# save line
ypu = np.column_stack((
    yline,
    p(0, yline, t),
    u(0, yline, t), v(0, yline, t), zero(yline)
))
np.savetxt(path + '/line.csv', ypu, header='y p_rgh U.gas1_x U.gas1_y U.gas1_z',
           comments='')
