Как можно улучшить код для расчетов на python?
Есть код который выполняет расчеты частей топливных форсунок. Есть два класса, в которых происходят основные расчеты, они называются LiquidJetInjector и GasJetInjector соответственно.
В классе Reynolds, хранятся константы для вычислений.
Класс SimilarCalculations представляет собой класс, в котором происходят вычисления, необходимые как в LiquidJetInjector, так и в GasJetInjector.
Чтобы код работал корректно, сначала нам необходимо передать основные значения в класс SimilarCalculations, а затем в тот класс, расчеты которого нам интересны.
Как можно улучшить данный код и как можно создать общую точку входа для него, чтобы исключить поочередный вызов двух классов?
import math
from enum import Enum
class SimilarCalculations:
def __init__(self, diameter: float, length: float, mass_flow_rate: float, viscosity: float, density: float):
self.diameter = diameter
self.length = length
self.mass_flow_rate = mass_flow_rate
self.viscosity = viscosity
self.density = density
def injector_nozzle_area(self) -> float:
return (math.pi * self.diameter ** 2) / 4
def reynolds_number(self) -> float:
return (4 * self.mass_flow_rate) / (math.pi * self.diameter * self.viscosity)
def average_speed(self) -> float:
return self.mass_flow_rate / (self.density * self.injector_nozzle_area())
class Reynolds(Enum):
LAMINAR = 2000
TURBULENT = 10000
class LiquidJetInjector:
def __init__(self, density_comb: float, sigma_fuel: float):
self.calculations = SimilarCalculations(diameter, length, mass_flow_rate, viscosity, density)
self.density_comb = density_comb
self.sigma_fuel = sigma_fuel
self.reynolds_number = self.calculations.reynolds_number()
self.injector_nozzle_area = self.calculations.injector_nozzle_area()
self.average_speed = self.calculations.average_speed()
def linear_hydraulic_resistance(self) -> float:
if self.reynolds_number < Reynolds.LAMINAR.value:
return 64 / self.reynolds_number
elif Reynolds.LAMINAR.value <= self.reynolds_number <= Reynolds.TURBULENT.value:
return 0.3164 * self.reynolds_number ** (-0.25)
else:
return 0.031
def injector_losses_inlet(self) -> float:
if self.reynolds_number < Reynolds.LAMINAR.value:
return 2.2 - 0.726 * math.exp(-74.5 * ((viscosity * length) / mass_flow_rate))
else:
return 1 + 2.65 * self.linear_hydraulic_resistance()
def injector_flow_coefficient(self) -> float:
return 1 / (math.sqrt(self.injector_losses_inlet() + self.linear_hydraulic_resistance() *
(length / diameter)))
def pressure_drop_injector(self) -> float:
return mass_flow_rate ** 2 / (2 * density * self.injector_flow_coefficient() ** 2 * self.injector_nozzle_area **
2)
def weber_criterion(self) -> float:
return (self.density_comb * self.average_speed ** 2 * diameter) / self.sigma_fuel
def media_diameter_spray_droplets(self) -> float:
return diameter * round(math.pow((27 * math.pi) / 4, 1 / 3.)) * self.weber_criterion() ** (-0.333)
class GasJetInjector:
def __init__(self, combustion_pressure: float, pressure_drop_internal_circuit: float, gas_constant_gen_gas: float,
temperature_gen_gas: float, entropy_expansion_ratio: float):
self.calculations = SimilarCalculations(diameter, length, mass_flow_rate, viscosity, density)
self.reynolds_number = self.calculations.reynolds_number()
self.injector_nozzle_area = self.calculations.injector_nozzle_area()
self.average_speed = self.calculations.average_speed()
self.combustion_pressure = combustion_pressure
self.pressure_drop_internal_circuit = pressure_drop_internal_circuit
self.gas_constant_gen_gas = gas_constant_gen_gas
self.temperature_gen_gas = temperature_gen_gas
self.entropy_expansion_ratio = entropy_expansion_ratio
def calculate_injector_pressure(self) -> float:
return self.combustion_pressure + self.pressure_drop_internal_circuit
def density_gen_gas(self) -> float:
return self.calculate_injector_pressure() / (self.gas_constant_gen_gas * self.temperature_gen_gas)
def injector_flow_coefficient(self) -> float:
return (math.sqrt(1.23 ** 2 + (232 * length) / (self.reynolds_number * diameter))) / \
((116 * length) / (self.reynolds_number * diameter))
def injector_nozzle_area_outlet(self) -> float:
return mass_flow_rate / (self.injector_flow_coefficient() * density * (
self.combustion_pressure / self.calculate_injector_pressure()) ** (1 / self.entropy_expansion_ratio) *
math.sqrt(2 * (self.entropy_expansion_ratio / (self.entropy_expansion_ratio - 1)) *
self.gas_constant_gen_gas * self.temperature_gen_gas * (1 - (
self.combustion_pressure / self.calculate_injector_pressure()) ** (
(self.entropy_expansion_ratio - 1) / self.entropy_expansion_ratio))))
def checking_diameter_injector(self) -> float:
return math.sqrt((4 * self.injector_nozzle_area_outlet()) / math.pi)
Для тестирования значений в конце кода использовал такую конструкцию (она временная и в финальной версии ее не будет, но для понимания работы прикреплю ее)
diameter = 10.5 * 10**(-3)
length = 24
mass_flow_rate = 0.8053
viscosity = 0.00116
density = 76.93
#
density_comb = 0.1
sigma_fuel = 0.5
#
combustion_pressure = 14.58 * 10**6
pressure_drop_internal_circuit = 10**6
gas_constant_gen_gas = 260.5
temperature_gen_gas = 777.4
entropy_expansion_ratio = 1.23
#
calc = SimilarCalculations(diameter, length, mass_flow_rate, viscosity, density)
calc_2 = LiquidJetInjector(density_comb, sigma_fuel)
calc_3 = GasJetInjector(combustion_pressure, pressure_drop_internal_circuit, gas_constant_gen_gas, temperature_gen_gas, entropy_expansion_ratio)
#
print(calc_3.injector_nozzle_area_outlet())
print(calc.average_speed())
print(calc.injector_nozzle_area())