Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
2.4 kB
3
Indexable
from typing import Optional, Union
from pydantic import BaseModel, Field, root_validator, validator

from pint import UnitRegistry

UREG = UnitRegistry(fmt_locale="pt_BR", autoconvert_offset_to_baseunit=True)
Q_ = UREG.Quantity


Unit = Union[Q_, float, int]


class BalancoEnergiaGlobalArgs(BaseModel):
    m: Optional[Unit] = Field(None, description="Massa específica")
    cp: Optional[Unit] = Field(None)
    t_ent: Optional[Unit] = Field(None, description="Temperatura quente de entrada")
    t_sai: Optional[Unit] = Field(None, description="Temperatura quente")
    q: Optional[Unit] = Field(None, description="Taxa de transferência de calor")

    class Config:
        arbitrary_types_allowed = True

    @root_validator
    def grau_de_liberdade(cls, values: dict) -> dict:
        if list(values.values()).count(None) > 1:
            raise ValueError("Deve haver apenas uma variavel sem valor!")
        return values

    @validator("m")
    def m_converter_unidade(cls, value: Q_) -> Q_:
        return Q_(value, "kg/s")

    @validator("cp")
    def cp_converter_unidade(cls, value: Q_) -> Q_:
        return Q_(value, "J/(kg.degK)")

    @validator("t_ent", "t_sai")
    def temperatura_converter_unidade(cls, value: Q_) -> Q_:
        return Q_(value, "degC")

    @validator("q")
    def calcular_taxa_transferencia_calor(cls, value: Q_) -> Q_:
        return Q_(value, "W")


class BalancoEnergiaGlobal:
    def __init__(self, args: BalancoEnergiaGlobalArgs) -> None:
        self.__m = args.m
        self.__cp = args.cp
        self.__t_ent = args.t_ent
        self.__t_sai = args.t_sai
        self.__q = args.q

    @property
    def m(self) -> Q_:
        if self.__m is None:
            raise NotImplementedError()
        return self.__m

    @property
    def cp(self) -> Q_:
        if self.__cp is None:
            raise NotImplementedError()
        return self.__cp

    @property
    def t_ent(self) -> Q_:
        if self.__t_ent is None:
            raise NotImplementedError()
        return self.__t_ent

    @property
    def t_sai(self) -> Q_:
        if self.__t_sai is None:
            raise NotImplementedError()
        return self.__t_sai

    @property
    def q(self) -> Q_:
        if self.__q is None:
            self.__q = Q_(self.m * self.cp * (self.t_ent - self.t_sai), "W")
        return self.__q