"""auxiliary variable entity (Auxv class definition)"""
from typing import Any, Union, Tuple
import os
if os.environ.get('__USE_CUDA', '0') == '1':
import cupy as np
from cupy import ndarray
else:
import numpy as np
from numpy import ndarray
from g3d.defs import G3DEntity
from g3d.defs import FunctionByPass
from g3d.gas import Gas
from g3d.flow.vars import Cons
[docs]
class Auxv(G3DEntity):
def __init__(self, *args: Any, **kwargs: Any) -> None:
self._p = 101325.
self._u = 0.
self._v = 0.
self._w = 0.
self._t = 293.
self._c = Gas().get_speed_of_sound(self._t)
super().__init__(*args, **kwargs)
def _set_default(self) -> None:
super()._set_default()
self._tag = 'g3d_auxv'
def _set_from_args(self, *args: Any, **kwargs: Any) -> ndarray:
bps = self._get_function_evaluate_status(**kwargs)
if bps is FunctionByPass.SELF:
args_stat = np.ones(len(args))
else:
args_stat = super()._set_from_args(*args)
if bps in [FunctionByPass.EVALUATE, FunctionByPass.SELF]:
if np.sum(args_stat) > 0:
flow_data = []
gas = None
cons = None
flow_data_updated = False
speed_of_sound_given = False
for i in range(len(args)):
if args_stat[i] == 1:
arg = args[i]
if type(arg) is Auxv:
args_stat[i] = 0
self.copy(arg)
elif type(arg) is Gas:
args_stat[i] = 0
gas = arg
elif type(arg) is Cons:
args_stat[i] = 0
cons = arg
elif type(arg) is list:
args_stat[i] = 0
if self._list_is_all_floats(arg) and len(arg) >= 5:
if len(arg) == 5:
self._p, self._u, self._v, self._w, self._t = arg
if len(arg) == 6:
self._p, self._u, self._v, self._w, self._t, self._c = arg
speed_of_sound_given = True
flow_data_updated = True
elif type(arg) in [float, np.float64, np.float32, int, np.int64, np.int32]:
args_stat[i] = 0
flow_data.append(np.float64(arg))
if cons is not None: # use new instance of gas in case gas is not given
self.update(cons, gas)
elif gas is not None and flow_data_updated and not speed_of_sound_given:
self._c = gas.get_speed_of_sound(self._t)
elif len(flow_data) >= 5:
self._set_from_args(flow_data, bps=FunctionByPass.SELF)
return args_stat
[docs]
def set_from_key_value(self, key: str, value: Any) -> bool:
if super().set_from_key_value(key, value):
return True
elif key == 'p':
self._p = value
return True
elif key == 'u':
self._u = value
return True
elif key == 'v':
self._v = value
return True
elif key == 'w':
self._w = value
return True
elif key == 't':
self._t = value
return True
elif key == 'c':
self._c = value
return True
return False
@property
def p(self) -> float:
"""
:return:
"""
return self._p
@p.setter
def p(self, val: float) -> None:
self._p = val
@property
def u(self) -> float:
"""
:return:
"""
return self._u
@u.setter
def u(self, val: float) -> None:
self._u = val
@property
def v(self) -> float:
"""
:return:
"""
return self._v
@v.setter
def v(self, val: float) -> None:
self._v = val
@property
def w(self) -> float:
"""
:return:
"""
return self._w
@w.setter
def w(self, val: float) -> None:
self._w = val
@property
def t(self) -> float:
"""
:return:
"""
return self._t
@t.setter
def t(self, val: float) -> None:
self._t = val
@property
def c(self) -> float:
"""
:return:
"""
return self._c
@c.setter
def c(self, val: float) -> None:
self._c = val
[docs]
def copy(self, auxv: 'Auxv') -> None:
super().copy(auxv)
self._p = auxv.p
self._u = auxv.u
self._v = auxv.v
self._w = auxv.w
self._t = auxv.t
self._c = auxv.c
[docs]
def update_speed_of_sound(self, gas: Gas = None) -> None:
"""
:param gas:
"""
if gas is None:
gas = Gas()
self._c = gas.get_speed_of_sound(self._t)
[docs]
def get_velocity_vector(self) -> ndarray:
"""
:return:
"""
return np.array([self._u, self._v, self._w])
[docs]
def set_velocity_vector(self, vel: Union[Tuple[float, float, float], list[float], ndarray]) -> None:
"""
:param vel:
"""
self._u, self._v, self._w = vel
[docs]
def get_Cons(self, gas: Gas) -> Cons:
"""
:param gas:
:return:
"""
cons = Cons()
cons.set_from_primitive(self._p, self._u, self._v, self._w, self._t, gas)
return cons
[docs]
def uiui(self) -> float:
"""
:return:
"""
tmp = self.get_velocity_vector()
return sum(tmp * tmp)
[docs]
def vel_dot_product(self, vel: Union[list[float], ndarray, 'Auxv']) -> float:
"""
:param vel:
:return:
"""
if type(vel) is Auxv:
return vel.vel_dot_product(self.get_velocity_vector())
tmp = self.get_velocity_vector()
return sum(tmp * vel)
[docs]
def set_from_conservative(self, ro: float, ru: float, rv: float, rw: float, et: float, gas: Gas = None) -> None:
"""
:param ro:
:param ru:
:param rv:
:param rw:
:param et:
:param gas:
"""
cons = Cons()
cons.set(ro, ru, rv, rw, et)
self.update(cons, gas)
[docs]
def update(self, cons: Cons, gas: Gas = None) -> None:
"""
:param cons:
:param gas:
"""
self._u = cons.ru / cons.ro
self._v = cons.rv / cons.ro
self._w = cons.rw / cons.ro
self._p = gas.gm1 * (cons.et - 0.5 * cons.ro * self.uiui())
if gas is None:
gas = Gas()
self._t = gas.get_temperature(self._p, cons.ro)
self._c = gas.get_speed_of_sound(self._t)
[docs]
def print(self) -> None:
print(' pressure %11.5E [Pa] ' % self._p)
print(' temperature %11.5E [K] ' % self._t)
print(' x-velocity %11.5E [m/s]' % self._u)
print(' y-velocity %11.5E [m/s]' % self._v)
print(' z-velocity %11.5E [m/s]' % self._w)