class FuelFlow(object): """Fuel flow model based on ICAO emmision databank.""" def __init__(self, ac, eng=None): """Initialize FuelFlow object. Args: ac (string): ICAO aircraft type (for example: A320). eng (string): Engine type (for example: CFM56-5A3). Leave empty to use the default engine specified by in the aircraft database. """ self.aircraft = prop.aircraft(ac) if eng is None: eng = self.aircraft["engine"]["default"] self.engine = prop.engine(eng) self.thrust = Thrust(ac, eng) self.drag = Drag(ac) c3, c2, c1 = ( self.engine["fuel_c3"], self.engine["fuel_c2"], self.engine["fuel_c1"], ) # print(c3,c2,c1) self.fuel_flow_model = lambda x: c3 * x**3 + c2 * x**2 + c1 * x @ndarrayconvert def at_thrust(self, acthr, alt=0): """Compute the fuel flow at a given total thrust. Args: acthr (int or ndarray): The total net thrust of the aircraft (unit: N). alt (int or ndarray): Aircraft altitude (unit: ft). Returns: float: Fuel flow (unit: kg/s). """ n_eng = self.aircraft["engine"]["number"] engthr = acthr / n_eng ratio = engthr / self.engine["max_thrust"] ff_sl = self.fuel_flow_model(ratio) ff_corr_alt = self.engine["fuel_ch"] * (engthr / 1000) * (alt * aero.ft) ff_eng = ff_sl + ff_corr_alt fuelflow = ff_eng * n_eng return fuelflow @ndarrayconvert def takeoff(self, tas, alt=None, throttle=1): """Compute the fuel flow at takeoff. The net thrust is first estimated based on the maximum thrust model and throttle setting. Then FuelFlow.at_thrust() is called to compted the thrust. Args: tas (int or ndarray): Aircraft true airspeed (unit: kt). alt (int or ndarray): Altitude of airport (unit: ft). Defaults to sea-level. throttle (float or ndarray): The throttle setting, between 0 and 1. Defaults to 1, which is at full thrust. Returns: float: Fuel flow (unit: kg/s). """ Tmax = self.thrust.takeoff(tas=tas, alt=alt) fuelflow = throttle * self.at_thrust(Tmax) return fuelflow @ndarrayconvert def enroute(self, mass, tas, alt, path_angle=0): """Compute the fuel flow during climb, cruise, or descent. The net thrust is first estimated based on the dynamic equation. Then FuelFlow.at_thrust() is called to compted the thrust. Assuming no flap deflection and no landing gear extended. Args: mass (int or ndarray): Aircraft mass (unit: kg). tas (int or ndarray): Aircraft true airspeed (unit: kt). alt (int or ndarray): Aircraft altitude (unit: ft). path_angle (float or ndarray): Flight path angle (unit: degrees). Returns: float: Fuel flow (unit: kg/s). """ D = self.drag.clean(mass=mass, tas=tas, alt=alt, path_angle=path_angle) # Convert angles from degrees to radians. gamma = np.radians(path_angle) T = D + mass * aero.g0 * np.sin(gamma) T_idle = self.thrust.descent_idle(tas=tas, alt=alt) T = np.where(T < 0, T_idle, T) fuelflow = self.at_thrust(T, alt) # do not return value outside performance boundary, with a margin of 20% T_max = self.thrust.climb(tas=0, alt=alt, roc=0) fuelflow = np.where(T > 1.20 * T_max, np.nan, fuelflow) return fuelflow def plot_model(self, plot=True): """Plot the engine fuel model, or return the pyplot object. Args: plot (bool): Display the plot or return an object. Returns: None or pyplot object. """ import matplotlib.pyplot as plt xx = np.linspace(0, 1, 50) yy = self.fuel_flow_model(xx) # plt.scatter(self.x, self.y, color='k') plt.plot(xx, yy, "--", color="gray") if plot: plt.show() else: return plt
class Performance(): """Calulate Instantaneous performance of one object""" def __init__(self, aircraft_name, write_output: bool = False): self.write = write_output # General Variables self.aircraft_data = prop.aircraft(aircraft_name) self.dt = 1.0 / 60.0 # simulation timestep 60 per seconds aircraft_txt = open(f"./data/{aircraft_name}.json", 'r').read() self.aircraft = json.loads(aircraft_txt) eng_name = self.aircraft_data["engine"]["default"] self.ac_thrust = Thrust(ac=self.aircraft["Name"], eng=eng_name) # self.ac_thrust = Thrust(f"./data/{aircraft_name}_thrust.csv") # Performance Variables (all unit in SI except stated otherwise) self.lift_drag = LiftDrag(f"./data/{aircraft_name}_ld.csv") self.g = 9.81 self.mass = self.aircraft_data["limits"]["MTOW"] self.thrust_lever = 1.0 self.altitude = 0.0 self.pressure = 0.0 self.density = 0.0 self.temp = 0.0 self.cas = 0.0 self.tas = 0.0 self.v_y = 0.0 # vertical Speed self.vs = 0.0 # Vertical Speed [fpm] self.drag = 0.0 self.thrust = 0.0 self.lift = 0.0 self.weight = 0.0 self.t_d = 0.0 # thrust minus drag aka exceed thrust self.l_w = 0.0 # lift minus weight aka exceed lift self.pitch = 0.0 self.fpa = 0.0 self.aoa = 0.0 self.Q = 0.0 # tas² * density self.acc_x = 0.0 self.acc_y = 0.0 self.distance_x = 0.0 # total distance[m] self.d_x = 0.0 # instantaneous X distance[m] self.d_y = 0.0 # instantaneous Y distance[m] self.phase = 0 # Current phase self.cd = 0.0 self.cl = 0.0 self.drag0 = self.aircraft["Drag0"] self.lift0 = self.aircraft["Lift0"] self.gear = False self.flaps = 0 self.pitch_target = 0.0 self.pitch_rate_of_change = 3.0 # rate of change of the pitch [°/sec] self.ac_fuelflow = FuelFlow(ac=self.aircraft["Name"], eng=eng_name) if self.write: self.output = open("output.csv", 'w+') self.output.write(self.__get_header()) self.output.flush() def __get_Q(self): self.pressure, self.density, self.temp = aero.atmos(self.altitude) self.Q = self.density * (self.tas**2) def __calculate_FPA(self): self.fpa = math.degrees(math.atan2(self.d_y, self.d_x)) self.aoa = self.pitch - self.fpa def __calculate_lift(self): if self.gear: self.cl += self.aircraft["Gear"]["Lift"] if self.flaps > 0: self.cl += self.aircraft["Flaps"][self.flaps - 1]["Lift"] self.lift = self.lift0\ + (0.5 * self.Q * self.aircraft["WingSpan"] * self.cl) self.lift *= 1.5 def __calculate_drag(self, new: bool = True): if self.gear: self.cd += self.aircraft["Gear"]["Drag"] if self.flaps > 0: self.cd += self.aircraft["Flaps"][self.flaps - 1]["Drag"] self.drag = self.drag0\ + (0.5 * self.Q * self.aircraft["WingSpan"] * self.cd) def __change_pitch(self) -> None: """ Change pitch in relation of pitch target change of pitch occure with a 3 degrees per seconds change. """ if self.pitch == self.pitch_target: return if self.pitch > self.pitch_target: self.pitch -= self.pitch_rate_of_change * self.dt elif self.pitch < self.pitch_target: self.pitch += self.pitch_rate_of_change * self.dt def run(self) -> bool: """Calculate aircraft performance till thrust reduction altitude Args: target_alt (float, optional): The thrust reduction altitude in meters. Defaults to 457.2m (1500.0 feets) Returns: bool: True if the phase is still valid, else False """ if self.distance_x == 0: self.aoa = self.pitch self.cl, self.cd = self.lift_drag.get_data(self.aoa) self.__get_Q() self.__change_pitch() self.__calculate_drag() self.__calculate_lift() self.g = local_gravity(50.0, self.altitude) self.weight = self.mass * self.g max_thrust = self.ac_thrust.takeoff(alt=self.altitude / aero.ft, tas=self.tas / aero.kts) idle_thrust = self.ac_thrust.descent_idle(tas=self.tas / aero.kts, alt=self.altitude / aero.ft) self.thrust = interpolate(self.thrust_lever, 0.0, 1.0, idle_thrust, max_thrust) fuelflow = self.ac_fuelflow.at_thrust(acthr=self.thrust / 2.0, alt=self.altitude / aero.ft) self.mass -= fuelflow * self.dt * 2 self.t_d = self.thrust - self.drag\ - (self.weight * math.sin(math.radians(self.pitch))) self.l_w = self.lift\ - (self.weight * math.cos(math.radians(self.pitch)))\ + (self.thrust * math.sin(math.radians(self.pitch))) acc = self.t_d / self.mass self.acc_x = acc * math.cos(math.radians(self.pitch)) self.acc_y = acc * math.sin(math.radians(self.pitch)) v_acc = self.l_w / self.mass self.acc_y += v_acc * math.cos(math.radians(self.pitch)) self.acc_x += v_acc * math.sin(math.radians(self.pitch)) self.d_x = (self.tas * self.dt) + 0.5 * self.acc_x * (self.dt**2) self.d_y = (self.v_y * self.dt) + 0.5 * self.acc_y * (self.dt**2) self.tas += self.acc_x * self.dt self.cas = aero.tas2cas(self.tas, self.altitude) self.v_y += self.acc_y * self.dt if self.altitude <= 0: self.altitude = 0 if self.d_y < 0: self.d_y = 0 if self.v_y < 0: self.v_y = 0 self.vs = 0 self.altitude += self.d_y self.distance_x += self.d_x self.vs = self.v_y / aero.fpm self.__calculate_FPA() if self.write: self.output.write(str(self)) self.output.flush() def __str__(self): return f"{self.mass},{self.altitude},{self.pressure},{self.density},"\ f"{self.temp},{self.cas},{self.tas},{self.v_y},{self.vs},"\ f"{self.drag},{self.thrust},{self.t_d},{self.pitch},"\ f"{self.fpa},{self.aoa},{self.Q},{self.acc_x},{self.acc_y},"\ f"{self.distance_x},{self.d_x},{self.d_y},{self.phase},"\ f"{self.cd},{self.drag0},{self.gear},{self.flaps},{self.cl},"\ f"{self.lift},{self.weight},{self.l_w},{self.thrust_lever},"\ f"{self.altitude / aero.ft},{self.g},{self.pitch_target}\n" def __get_header(self): return "Mass,Altitude,Pressure,Density,Temperature,Cas,Tas,Vy,VS,"\ "Drag,Thrust,T-D,Pitch,FPA,AOA,Q,AccelerationX,AccelerationY,"\ "DistanceX,Dx,Dy,Phase,Cd,Cd0,Gear,Flaps,Cl,Lift,Weight,L-W,"\ "Thrust Limit,Altitude FT,Gravity,Target Pitch\n"
class FuelFlow(object): """Fuel flow model based on ICAO emmision databank.""" def __init__(self, ac, eng=None): """Initialize FuelFlow object. Args: ac (string): ICAO aircraft type (for example: A320). eng (string): Engine type (for example: CFM56-5A3). Leave empty to use the default engine specified by in the aircraft database. """ self.aircraft = prop.aircraft(ac) if eng is None: eng = self.aircraft['engine']['default'] self.engine = prop.engine(eng) self.thrust = Thrust(ac, eng) self.drag = Drag(ac) c3, c2, c1 = self.engine['fuel_c3'], self.engine[ 'fuel_c2'], self.engine['fuel_c1'] # print(c3,c2,c1) self.fuel_flow_model = lambda x: c3 * x**3 + c2 * x**2 + c1 * x @ndarrayconvert def at_thrust(self, acthr, alt=0): """Compute the fuel flow at a given total thrust. Args: acthr (int or ndarray): The total net thrust of the aircraft (unit: N). alt (int or ndarray): Aicraft altitude (unit: ft). Returns: float: Fuel flow (unit: kg/s). """ ratio = acthr / (self.engine['max_thrust'] * self.aircraft['engine']['number']) fuelflow = self.fuel_flow_model(ratio) * self.aircraft['engine']['number'] \ + self.engine['fuel_ch'] * (alt*aero.ft) * (acthr/1000) return fuelflow @ndarrayconvert def takeoff(self, tas, alt=None, throttle=1): """Compute the fuel flow at takeoff. The net thrust is first estimated based on the maximum thrust model and throttle setting. Then FuelFlow.at_thrust() is called to compted the thrust. Args: tas (int or ndarray): Aircraft true airspeed (unit: kt). alt (int or ndarray): Altitude of airport (unit: ft). Defaults to sea-level. throttle (float or ndarray): The throttle setting, between 0 and 1. Defaults to 1, which is at full thrust. Returns: float: Fuel flow (unit: kg/s). """ Tmax = self.thrust.takeoff(tas=tas, alt=alt) fuelflow = throttle * self.at_thrust(Tmax) return fuelflow @ndarrayconvert def enroute(self, mass, tas, alt, path_angle=0): """Compute the fuel flow during climb, cruise, or descent. The net thrust is first estimated based on the dynamic equation. Then FuelFlow.at_thrust() is called to compted the thrust. Assuming no flap deflection and no landing gear extended. Args: mass (int or ndarray): Aircraft mass (unit: kg). tas (int or ndarray): Aircraft true airspeed (unit: kt). alt (int or ndarray): Aircraft altitude (unit: ft). path_angle (float or ndarray): Flight path angle (unit: ft). Returns: float: Fuel flow (unit: kg/s). """ D = self.drag.clean(mass=mass, tas=tas, alt=alt, path_angle=path_angle) gamma = np.radians(path_angle) T = D + mass * aero.g0 * np.sin(gamma) T_idle = self.thrust.descent_idle(tas=tas, alt=alt) T = np.where(T < 0, T_idle, T) fuelflow = self.at_thrust(T, alt) return fuelflow def plot_model(self, plot=True): """Plot the engine fuel model, or return the pyplot object. Args: plot (bool): Display the plot or return an object. Returns: None or pyplot object. """ import matplotlib.pyplot as plt xx = np.linspace(0, 1, 50) yy = self.fuel_flow_model(xx) # plt.scatter(self.x, self.y, color='k') plt.plot(xx, yy, '--', color='gray') if plot: plt.show() else: return plt