Example #1
0
def test_thermal(force_recompute=False,
                 filename='/tmp/glider_opty_4d_thermal.npz'):
    atm = go_u.AtmosphereWharingtonSym(radius=40., strength=-1, cst=[4, 0, 0])
    _p = go4.Planner(_obj_fun=go4.obj_final_z,
                     _obj_grad=go4.obj_grad_final_z,
                     _atm=atm,
                     _n_constraint=(-40, 40),
                     _e_constraint=(-40, 40),
                     x0=40,
                     y0=0,
                     z0=-1,
                     psi0=np.pi,
                     duration=50,
                     hz=50.,
                     obj_scale=1.)
    go4.compute_or_load(atm,
                        _p,
                        force_recompute,
                        filename,
                        tol=1e-5,
                        max_iter=1000)
    plot_all(_p,
             n0=-40,
             n1=50,
             dn=5.,
             e0=-40,
             e1=40,
             de=5,
             h0=0.,
             h1=70,
             dh=2.5)
    def __init__(self,
                 _obj_fun=obj_final_z,
                 _obj_grad=obj_grad_final_z,
                 _atm=None,
                 _min_bank=-np.deg2rad(60.),
                 _max_bank=np.deg2rad(60.),
                 _min_v=7.,
                 _max_v=20.,
                 x0=-25,
                 y0=0,
                 z0=1,
                 psi0=0):
        self.duration = 40
        self.num_nodes = 2000  # time discretization
        self.interval_value = self.duration / (self.num_nodes - 1)
        print('solver: interval_value: {:.3f}s ({:.1f}hz)'.format(
            self.interval_value, 1. / self.interval_value))
        self.atm = _atm if _atm is not None else go_u.AtmosphereWharingtonSym()

        # symbols
        self._st = sym.symbols('t')
        self._sx, self._sy, self._sz, self._sv, self._sphi, self._spsi = sym.symbols(
            'x, y, z, v, phi, psi', cls=sym.Function)
        self._state_symbols = (self._sx(self._st), self._sy(self._st),
                               self._sz(self._st), self._spsi(self._st))
        self._input_symbols = (self._sv, self._sphi)

        self._slice_x = slice(0 * self.num_nodes, 1 * self.num_nodes, 1)
        self._slice_y = slice(1 * self.num_nodes, 2 * self.num_nodes, 1)
        self._slice_z = slice(2 * self.num_nodes, 3 * self.num_nodes, 1)
        self._slice_psi = slice(3 * self.num_nodes, 4 * self.num_nodes, 1)
        self._slice_phi = slice(4 * self.num_nodes, 5 * self.num_nodes, 1)
        self._slice_v = slice(5 * self.num_nodes, 6 * self.num_nodes, 1)

        # Specify the known system parameters.
        self._par_map = collections.OrderedDict()
        #self._par_map[g] = 9.81
        #self._par_map[v] = 8.0

        # Specify the symbolic instance constraints, i.e. initial and end conditions.
        self._instance_constraints = (self._sx(0.) - x0, self._sy(0.) - y0,
                                      self._sz(0.) - z0, self._spsi(0.) - psi0)

        self._bounds = {
            self._sphi(self._st): (_min_bank, _max_bank),
            self._sv(self._st): (_min_v, _max_v)
        }

        # Create an optimization problem.
        self.prob = opty.direct_collocation.Problem(
            lambda _free: _obj_fun(self.num_nodes, 1., _free),
            lambda _free: _obj_grad(self.num_nodes, 1., _free),
            self.get_eom(),
            self._state_symbols,
            self.num_nodes,
            self.interval_value,
            known_parameter_map=self._par_map,
            instance_constraints=self._instance_constraints,
            bounds=self._bounds,
            parallel=False)
Example #3
0
def test_thermal(force_recompute=False, filename='/home/poine/tmp/glider_opty_4d_thermal_{}.npz', exp_id='2'):
    if exp_id == '0':
        atm = go_u.AtmosphereWharingtonSym(radius=40., strength=-1)
    elif exp_id == '1':
        atm = go_u.AtmosphereWharingtonOval(radius=60., strength=-0.8)
    elif exp_id == '2':
        atm = go_u.atm2()
    #atm = go_u.AtmosphereArrayTest()

    _p = sgo4d.Planner( _obj_fun=sgo4d.obj_sum_z, _obj_grad=sgo4d.obj_grad_sum_z,
                        #_obj_fun=sgo4d.obj_final_z, _obj_grad=sgo4d.obj_grad_final_z,
                        _atm=atm, e0=-100, n0=0, u0=25, psi0=0,
                        _v_constraint = (7., 15.),
                        _e_constraint = (-120, 120),
                        _n_constraint = (-120, 120),
                        #_u_constraint = (   25, 100),
                        duration=40, hz=50., obj_scale=1.)

    #sgo4d.compute_or_load(atm, _p, force_recompute, filename, tol=1e-5, max_iter=4000, initial_guess=None)
    sgo4d.compute_or_load(atm, _p, force_recompute, filename.format(exp_id), tol=1e-4, max_iter=4000, initial_guess=None)
    print(f'last altitude {_p.sol_u[-1]} m')
    go_u.plot_solution_chronogram(_p); plt.savefig('plots/glider_4d_thermal_chrono.png')
    go_u.plot_solution_2D_en(_p); plt.savefig('plots/glider_4d_thermal_en.png')
    go_u.plot_solution_2D_nu(_p, n0=-40, n1=50, dn=5., e0=0., h0=0., h1=70, dh=2.5); plt.savefig('plots/glider_4d_thermal_nu.png')
    go_u.plot_solution_2D_eu(_p, contour_wz=True, e0=-40, e1=50, de=5., n0=0., h0=0., h1=70, dh=2.5); plt.savefig('plots/glider_4d_thermal_eu.png')
    go_u.plot_solution_3D(_p); plt.savefig('plots/glider_4d_thermal_3D.png')
    plt.show()
def main(force_recompute=False, filename='/tmp/glider2_opty.pkl'):
    atm = go_u.AtmosphereWharingtonSym(center=[0, 0, 0],
                                       radius=40,
                                       strength=1.)
    _p = Planner(_atm=atm, x0=-100, y0=-100, z0=5)
    _p.configure(tol=1e-7, max_iter=2000)
    _p.run()
    go_u.plot_solution_chronogram(_p)
    go_u.plot_solution_2D_en(_p)
    go_u.plot_solution_2D_nu(_p)
    go_u.plot_solution_3D(_p)
    plt.show()
    def __init__(self,
                 _obj_fun=obj_final_z, _obj_grad=obj_grad_final_z,
                 _atm=None,
                 _min_bank=-np.deg2rad(60.), _max_bank=np.deg2rad(60.),
                 _min_v=7., _max_v=20.,
                 x0=-25, y0=0, z0=1, psi0=0,
                 xd0=12., yd0=0., zd0=0.,
                 duration=40., hz=50.):
        self.duration  = duration
        self.num_nodes = int(duration*hz)+1
        self.interval_value = 1./hz
        print('solver: interval_value: {:.3f}s ({:.1f}hz)'.format(self.interval_value, 1./self.interval_value))
        self.atm = _atm if _atm is not None else go_u.AtmosphereWharingtonSym()
        self.glider = _g = Glider()

        self._slice_x   = slice(0*self.num_nodes, 1*self.num_nodes, 1)
        self._slice_y   = slice(1*self.num_nodes, 2*self.num_nodes, 1)
        self._slice_z   = slice(2*self.num_nodes, 3*self.num_nodes, 1)
        self._slice_xd  = slice(3*self.num_nodes, 4*self.num_nodes, 1)
        self._slice_yd  = slice(4*self.num_nodes, 5*self.num_nodes, 1)
        self._slice_zd  = slice(5*self.num_nodes, 6*self.num_nodes, 1)
        self._slice_psi = slice(6*self.num_nodes, 7*self.num_nodes, 1)
        self._slice_phi = slice(7*self.num_nodes, 8*self.num_nodes, 1)
        
        # Specify the known system parameters.
        self._par_map = collections.OrderedDict()
        scale=100.
        self._instance_constraints = (_g._sx(0.)-x0, _g._sy(0.)-y0, _g._sz(0.)-z0,
                                      _g._sxd(0.)-xd0, _g._syd(0.)-yd0, _g._szd(0.)-zd0,
                                      _g._spsi(0.)-psi0)
        self._bounds = {_g._sphi(_g._st): (_min_bank, _max_bank),
#                        _g._sv(_g._st): (_min_v, _max_v),
                        _g._sx(_g._st): (-50, 10),
                        _g._sy(_g._st): (-25, 25)
                       }
        self.prob =  opty.direct_collocation.Problem(lambda _free: _obj_fun(self.num_nodes, scale, _free),
                                                     lambda _free: _obj_grad(self.num_nodes, scale, _free),
                                                     _g.get_eom(self.atm),
                                                     _g._state_symbols,
                                                     self.num_nodes,
                                                     self.interval_value,
                                                     known_parameter_map=self._par_map,
                                                     instance_constraints=self._instance_constraints,
                                                     bounds=self._bounds,
                                                     parallel=False)
def main(force_recompute=False, filename='/tmp/glider_opty.pkl'):
    if 1:
        _atm = go_u.AtmosphereWharingtonSym(center=[0, 0, 0],
                                            radius=40,
                                            strength=1.)
        _atm.center = np.array([-20, 0, 0])
        _atm.strength = 2.
        _atm.radius = 20.
    else:
        _atm = go_u.AtmosphereRidgeSym()

    _p = Planner(_atm=_atm, x0=0, z0=20., psi0=np.pi /
                 2)  #obj_cc, obj_grad_cc)#_min_bank=np.deg2rad(-10))
    _p.configure(tol=1e-8, max_iter=1000)
    _p.run_or_load(filename, force_recompute)
    plot_run(_p)
    alt_final, alt_mean = _p.sol_z[-1], np.mean(_p.sol_z)
    txt = 'alt: final {:.1f} m, mean {:.1f}'.format(alt_final, alt_mean)
    go_u.plot_solution_chronogram(_p)
    go_u.plot_solution_2D_en(_p, title=txt)
    go_u.plot_solution_2D_nu(_p, title=txt)
    go_u.plot_solution_3D(_p, title=txt)
    plt.show()
def atm0():
    return go_u.AtmosphereWharingtonSym(center=[0, 0, 0],
                                        radius=40,
                                        strength=1.)
    def __init__(
            self,
            _obj_fun=obj_final_z,
            _obj_grad=obj_grad_final_z,
            _atm=None,
            _phi_constraint=(-np.deg2rad(60.), np.deg2rad(
                60.)),  #_min_bank=-np.deg2rad(60.), _max_bank=np.deg2rad(60.),
            _v_constraint=(7., 20.),  # velocity constraint
            _u_constraint=None,
            _n_constraint=(-100, 100),
            _e_constraint=(-100, 100),
            e0=-25,
            n0=0,
            u0=1,
            psi0=0,
            duration=40.,
            hz=50.,
            obj_scale=1.):
        self.obj_scale = obj_scale  # objective function scaling
        self.num_nodes = int(duration * hz) + 1
        self.interval_value = 1. / hz
        self.duration = (self.num_nodes - 1) * self.interval_value  #duration
        print('solver: interval_value: {:.3f}s ({:.1f}hz)'.format(
            self.interval_value, 1. / self.interval_value))
        self.atm = _atm if _atm is not None else go_u.AtmosphereWharingtonSym()

        self.glider = _g = Glider()

        self._slice_e, self._slice_n, self._slice_u, self._slice_psi, self._slice_phi, self._slice_v = \
            [slice(_i*self.num_nodes, (_i+1)*self.num_nodes, 1) for _i in range(6)]

        self._e_constraint = _e_constraint
        self._n_constraint = _n_constraint
        self._u_constraint = _u_constraint

        # Specify the known system parameters.
        self._par_map = collections.OrderedDict()
        #self._par_map[g] = 9.81
        #self._par_map[v] = 8.0

        #scale=100.#-0.1#-1.#-10.

        # Specify the symbolic instance constraints, i.e. initial and end conditions.
        self._instance_constraints = (_g._se(0.) - e0, _g._sn(0.) - n0,
                                      _g._su(0.) - u0, _g._spsi(0.) - psi0)

        self._bounds = {
            _g._sphi(_g._st): _phi_constraint,
            _g._sv(_g._st): _v_constraint,
            _g._se(_g._st): _e_constraint,
            _g._sn(_g._st): _n_constraint,
        }
        if self._u_constraint is not None:
            self._bounds[_g._su(_g._st)] = _u_constraint
        self.prob = opty.direct_collocation.Problem(
            lambda _free: _obj_fun(_free, self),
            lambda _free: _obj_grad(_free, self),
            _g.get_eom(self.atm),
            _g._state_symbols,
            self.num_nodes,
            self.interval_value,
            known_parameter_map=self._par_map,
            instance_constraints=self._instance_constraints,
            bounds=self._bounds,
            parallel=False)