#! /usr/bin/env python3
"""------------------------------*- python -*----------------------------------
  ==  == ====== ====   ====    |
                    \\     ||  | HZDR Multiphase Case Collection for OpenFOAM
  ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.811
  ||  ||  //    ||  // || \\   | License: Creative Commons Attribution 4.0
  ==  == ====== ====   ==  ==  |
-------------------------------------------------------------------------------
License
    This file is part of the HZDR Multiphase Case Collection for OpenFOAM.

    Copyright (C) 2022 Helmholtz-Zentrum Dresden-Rossendorf

    The HZDR Multiphase Case Collection for OpenFOAM 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
    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       = 50.

# 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='')
