EXAMPLE 004

Cessna 310, ISA1976 integrated with Flat Earth (euler angles).

Example with trimmed aircraft: stationary, horizontal turn.

The main purpose of this example is to check if the aircraft trimmed in a given state maintains the trimmed flight condition.


In [1]:
# -*- coding: utf-8 -*-

In [2]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

In [3]:
from pyfme.aircrafts import Cessna310
from pyfme.environment.environment import Environment
from pyfme.environment.atmosphere import ISA1976
from pyfme.environment.gravity import VerticalConstant
from pyfme.environment.wind import NoWind
from pyfme.models.systems import EulerFlatEarth
from pyfme.simulator import BatchSimulation
from pyfme.utils.trimmer import steady_state_flight_trimmer

In [4]:
aircraft = Cessna310()
atmosphere = ISA1976()
gravity = VerticalConstant()
wind = NoWind()
environment = Environment(atmosphere, gravity, wind)

Initial conditions


In [5]:
TAS = 312.5 * 0.3048  # m/s
h0 = 8000 * 0.3048  # m
psi0 = 1  # rad
x0, y0 = 0, 0  # m
turn_rate = 0.1  # rad/s
gamma0 = 0.00  # rad

In [6]:
system = EulerFlatEarth(lat=0, lon=0, h=h0, psi=psi0, x_earth=x0, y_earth=y0)

not_trimmed_controls = {'delta_elevator': 0.05,
                        'hor_tail_incidence': 0.00,
                        'delta_aileron': 0.01 * np.sign(turn_rate),
                        'delta_rudder': 0.01 * np.sign(turn_rate),
                        'delta_t': 0.5}

controls2trim = ['delta_elevator', 'delta_aileron', 'delta_rudder', 'delta_t']

In [7]:
trimmed_ac, trimmed_sys, trimmed_env, results = steady_state_flight_trimmer(
    aircraft, system, environment, TAS=TAS, controls_0=not_trimmed_controls,
    controls2trim=controls2trim, gamma=gamma0, turn_rate=turn_rate, verbose=2)


   Iteration     Total nfev        Cost      Cost reduction    Step norm     Optimality   
       0              1         2.1686e+01                                    4.29e+02    
       1              2         1.2128e+00      2.05e+01       2.27e-01       2.07e+01    
       2              3         1.3870e-01      1.07e+00       9.67e-02       9.89e-01    
       3              4         6.6502e-03      1.32e-01       3.94e-02       1.69e-01    
       4              5         3.5340e-05      6.61e-03       1.02e-02       1.14e-02    
       5              6         1.3344e-09      3.53e-05       8.00e-04       6.98e-05    
       6              7         1.9498e-18      1.33e-09       4.95e-06       2.67e-09    
`gtol` termination condition is satisfied.
Function evaluations: 7, initial cost: 2.1686e+01, final cost 1.9498e-18, first-order optimality 2.67e-09.

In [8]:
print(results)


{'alpha': 0.019529309320263952, 'u': 95.223417561068644, 'v': 1.2665225538517186, 'p': -0.0023270662627858109, 'theta': 0.023272763409201803, 'delta_elevator': 0.029787609227457483, 'delta_aileron': 0.007165065320805227, 'beta': 0.01329721656892729, 'w': 1.8598840315837593, 'delta_rudder': 0.015606196554096622, 'delta_t': 0.12917363267464527, 'ls_opt':  active_mask: array([0, 0, 0, 0, 0, 0])
        cost: 1.949757310979204e-18
         fun: array([  1.95791861e-09,  -2.59846172e-11,  -2.42182424e-10,
        -2.64470227e-12,   7.49041766e-11,  -3.35291095e-11])
        grad: array([  8.16997040e-13,  -1.97775066e-13,   2.86894366e-13,
         1.70289479e-16,   6.24443975e-14,   2.06590277e-08])
         jac: array([[ -1.91113859e+01,  -1.93227960e-01,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   1.05515253e+01],
       [ -2.25375338e-01,  -2.38018564e+01,   0.00000000e+00,
          0.00000000e+00,   7.82855099e+00,   0.00000000e+00],
       [ -1.56157245e+02,  -1.29512046e-07,  -2.75701144e+01,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [  8.69714184e-11,  -7.26793198e+00,   0.00000000e+00,
          1.14060853e+01,   1.27323743e+00,   0.00000000e+00],
       [ -5.40887250e+00,  -5.29566469e-03,  -8.91366883e+01,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [ -3.15351522e-03,   7.73000444e+00,   0.00000000e+00,
         -8.99691950e-01,  -6.16931623e+00,   0.00000000e+00]])
     message: '`gtol` termination condition is satisfied.'
        nfev: 7
        njev: 7
  optimality: 2.668601344942849e-09
      status: 1
     success: True
           x: array([ 0.01952931,  0.01329722,  0.02978761,  0.00716507,  0.0156062 ,
        0.12917362]), 'phi': 0.77100703888401023, 'q': 0.069666914139476843, 'r': 0.069666914139476857}

In [9]:
my_simulation = BatchSimulation(trimmed_ac, trimmed_sys, trimmed_env)

In [10]:
tfin = 150  # seconds
N = tfin * 100 + 1
time = np.linspace(0, tfin, N)
initial_controls = trimmed_ac.controls

In [11]:
controls = {}
for control_name, control_value in initial_controls.items():
    controls[control_name] = np.ones_like(time) * control_value

my_simulation.set_controls(time, controls)

In [12]:
par_list = ['x_earth', 'y_earth', 'height',
            'psi', 'theta', 'phi',
            'u', 'v', 'w',
            'v_north', 'v_east', 'v_down',
            'p', 'q', 'r',
            'alpha', 'beta', 'TAS',
            'F_xb', 'F_yb', 'F_zb',
            'M_xb', 'M_yb', 'M_zb']

my_simulation.set_par_dict(par_list)
my_simulation.run_simulation()

In [13]:
plt.style.use('ggplot')

In [14]:
for ii in range(len(par_list) // 3):
    three_params = par_list[3*ii:3*ii+3]
    fig, ax = plt.subplots(3, 1, sharex=True)
    for jj, par in enumerate(three_params):
        ax[jj].plot(time, my_simulation.par_dict[par])
        ax[jj].set_ylabel(par)
        ax[jj].set_xlabel('time (s)')
    fig.tight_layout()
        
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(my_simulation.par_dict['x_earth'],
        my_simulation.par_dict['y_earth'],
        my_simulation.par_dict['height'])

ax.plot(my_simulation.par_dict['x_earth'],
        my_simulation.par_dict['y_earth'],
        my_simulation.par_dict['height'] * 0)
ax.set_xlabel('x_earth')
ax.set_ylabel('y_earth')
ax.set_zlabel('z_earth')

plt.show()