Esempio n. 1
0
 def zDot(self, t):
     day = t/24/60/60
     mjd = day + self.jdDep
     stateCyl, __, __ = ephemeris(self.body, mjd, mode=self.ephemSource)
     return stateCyl[5]
Esempio n. 2
0
    def samplePlanets(self, add=[]):
        '''
        Returns the state of the bodies visited during the patched transfer
        Ephemeris source is passed as a list in self.ephems
        The body names defined in the transfer objects must correspond to the
        names or IDs expected by the ephemeris source
        add manually adds another body to be included in the plot
        Output structure: Dictionary with a sub-dictionary for each body,
            containing a (3 x samples) numpy array
        '''

        print('Sample celestial body ephemerides')

        # retrieve names of all visited bodies
        bodyNames = []
        bodyPykep = []
        for trans in self.transfers:
            body1 = trans.departureBody
            body2 = trans.arrivalBody
            bodyNames.extend([body1, body2])

        for entry in add:
            bodyNames.extend(entry)

        # remove duplicates (kepping the order)
        # bodyNames = list(set(bodyNames))
        bodyNames = list(OrderedDict.fromkeys(bodyNames))
        print(f'Visited bodies: {bodyNames}')

        # retrieve the corresponding ephemeris source if mode is 'auto'
        # else default to the spice ephemerides
        # Example syntax for spice: '1234', for jpl: 'mars', for gtoc2: 123
        mode = []
        if self.ephems == 'auto':
            for body in bodyNames:
                if type(body) == str:
                    try:
                        int(body)
                        mode.append('spice')
                    except ValueError:
                        mode.append('jpl')
                else:
                    mode.append('gtoc2')
        else:
            mode = ['spice'] * len(bodyNames)

        # create nested dictionary
        states = ['rCart', 'vCart', 'rCyl', 'vCyl']
        bodyEphemeris = {
            name: {state: np.empty([3, self.samples])
                   for state in states}
            for name in bodyNames
        }

        # retrieve values for each body and each state vector
        # for body in bodyNames:
        #     pkBody = pk.planet.spice(body, 'sun', 'eclipj2000')
        #     for i, t in enumerate(self.tSamples):
        #         rCart, vCart = pkBody.eph(t)
        #         bodyEphemeris[body]['rCart'][:, i] = rCart
        #         bodyEphemeris[body]['vCart'][:, i] = vCart
        #         bodyEphemeris[body]['rCyl'][:, i] = Pcart2cyl(rCart)
        #         bodyEphemeris[body]['vCyl'][:, i] = Vcart2cyl(vCart, rCart)

        # retrieve values for each body and each state vector
        for j, body in enumerate(bodyNames):
            for i, t in enumerate(self.tSamples):
                stateCyl, stateCart, __ = ephemeris(body, t, mode=mode[j])
                bodyEphemeris[body]['rCart'][:, i] = stateCart[0:3]
                bodyEphemeris[body]['vCart'][:, i] = stateCart[3:6]
                bodyEphemeris[body]['rCyl'][:, i] = stateCyl[0:3]
                bodyEphemeris[body]['vCyl'][:, i] = stateCyl[3:6]

        return bodyEphemeris, bodyNames
Esempio n. 3
0
 def r(self, t):
     # convert time from [s since launch] to [mjd2000]
     day = t/24/60/60
     mjd = day + self.jdDep
     stateCyl, __, __ = ephemeris(self.body, mjd, mode=self.ephemSource)
     return stateCyl[0]
Esempio n. 4
0
    def planetSystem(self,
                     save=None,
                     folder=None,
                     plotSOI=False,
                     scaling=False):
        '''
        Plots the orbits of the visited planets
        Options:    save    save the plot (True/False) in png and pdf
                    folder  specify differing folder for the saved plot
                    plotSOI plots the sphere of influence of the planets
                    scaling adjusts the axis limits to the same, summetric
                            range, pass a number in [AU]
        '''

        print('Plot the planets in 3D.')

        ephem = self.bodyEphemeris

        # start figure
        fig = newFigure(height=6, target='paper')
        ax = fig.gca(projection='3d')

        # Sun
        ax.scatter([0], [0], [0],
                   s=100,
                   color='yellow',
                   label='Sun',
                   marker='o',
                   edgecolor='orange')

        # celestial bodies: orbits and position at launch
        for body in self.bodyNames:
            ax.plot(ephem[body]['rCart'][0, :] / pk.AU,
                    ephem[body]['rCart'][1, :] / pk.AU,
                    ephem[body]['rCart'][2, :] / pk.AU,
                    label=body)
            ax.scatter(ephem[body]['rCart'][0, 0] / pk.AU,
                       ephem[body]['rCart'][1, 0] / pk.AU,
                       ephem[body]['rCart'][2, 0] / pk.AU)
            if plotSOI:
                plotting = True
                if body == '1':
                    jplName = 'mercury'
                elif body == '2':
                    jplName = 'venus'
                if body == '3':
                    jplName = 'earth'
                elif body == '4':
                    jplName = 'mars'
                elif body == '5':
                    jplName = 'jupiter'
                elif body == '6':
                    jplName = 'saturn'
                elif body == '7':
                    jplName = 'uranus'
                elif body == '8':
                    jplName = 'neptune'
                else:
                    print(f'\tNo SOI computed for {body}.')
                    plotting = False

                if plotting:
                    __, __, planet = ephemeris(jplName, 0, mode='jpl')
                    muBody = planet.mu_self
                    muCentral = pk.MU_SUN
                    rSOI = np.sqrt(
                          ephem[body]['rCart'][0, 0]**2
                        + ephem[body]['rCart'][1, 0]**2
                        + ephem[body]['rCart'][2, 0]**2)\
                        * (muBody/muCentral)**(2/5)
                    self.plotSphere(ephem[body]['rCart'][:, 0] / pk.AU,
                                    rSOI / pk.AU)
                    # print(f'\tGravitational parameter {body}: {muBody:.2E} m^3/s^2')
                    print(
                        f'\tRadius of SOI {body}: {rSOI/1e3:.2E} km = {rSOI/pk.AU:.2E} AU'
                    )

        # formatting
        if scaling:
            axisEqual3D(ax)
            # ax.set_xlim(-scaling, scaling)
            # ax.set_ylim(-scaling, scaling)
            # ax.set_zlim(-scaling, scaling)

        ax.set_xlabel('x [AU]', labelpad=15)
        ax.set_ylabel('y [AU]', labelpad=15)
        ax.set_zlabel('z [AU]', labelpad=15)
        plt.grid()
        plt.legend(loc='lower right')

        if save == None:
            save = self.save
        if folder == None:
            folder = self.folder
        if save:
            checkFolder(folder)
            plt.savefig(os.path.join(os.getcwd(), folder, 'solarSystem.pdf'),
                        dpi=300)
            plt.savefig(os.path.join(os.getcwd(), folder, 'solarSystem.png'),
                        dpi=300)
        if self.show:
            plt.show()