Beispiel #1
0
def integrate_solar_system(particles, end_time):
    from amuse.lab import Huayno, nbody_system
    from amuse.units import quantities
    convert_nbody = nbody_system.nbody_to_si(particles.mass.sum(), particles[1].position.length())

    gravity = Huayno(convert_nbody)
    gravity.particles.add_particles(particles)
    venus = gravity.particles[1]
    earth = gravity.particles[2]
    
    x_earth = [] | units.AU
    y_earth = [] | units.AU
    x_venus = [] | units.AU
    y_venus = [] | units.AU

    while gravity.model_time < end_time:
        gravity.evolve_model(gravity.model_time + (10 | units.day))
        x_earth.append(earth.x)
        y_earth.append(earth.y)
        x_venus.append(venus.x)
        y_venus.append(venus.y)
#        from amuse.lab import *
#        write_set_to_file(gravity.particles, "gravity.h5", "hdf5")
    gravity.stop()
    return x_earth, y_earth, x_venus, y_venus
Beispiel #2
0
def integrate_solar_system(particles, end_time):
    from amuse.units import constants, units
    #print(particles)
    #convert_body = nbody_system.nbody_to_si(particles.mass.sum(),
    #                                        particles[1].position.length())

    gravity = Huayno()
    gravity.particles.add_particles(particles)
    sun = gravity.particles[0]
    venus = gravity.particles[1]
    earth = gravity.particles[2]

    x_earth = [] | nbody_system.length
    y_earth = [] | nbody_system.length
    x_venus = [] | nbody_system.length
    y_venus = [] | nbody_system.length
    x_sun = [] | nbody_system.length
    y_sun = [] | nbody_system.length

    while gravity.model_time < end_time:

        gravity.evolve_model(gravity.model_time + (0.001 | nbody_system.time))
        x_sun.append(sun.x)
        y_sun.append(sun.y)
        x_earth.append(earth.x)
        y_earth.append(earth.y)
        x_venus.append(venus.x)
        y_venus.append(venus.y)
    gravity.stop()
    return x_earth, y_earth, x_venus, y_venus, x_sun, y_sun
def integrate_solar_system(particles, end_time):
    from amuse.lab import Huayno, nbody_system
    from amuse.units.generic_unit_converter import ConvertBetweenGenericAndSiUnits
    from amuse.units import constants, units
    #print(particles)
    #converter_body = nbody_system.nbody_to_si(particles.mass.sum(),
    #                                       	particles[1].position.length())
    converter_body = nbody_system.nbody_to_si(1 | units.kg, 1 | units.m)
    #print(convert_nbody)
    #converter = ConvertBetweenGenericAndSiUnits(particles)#????????????????????
    #gravity = Huayno(converter.to_si(particles))
    gravity = Huayno(converter_body)
    gravity.particles.add_particles(particles)
    sun = gravity.particles[0]
    venus = gravity.particles[1]
    earth = gravity.particles[2]

    x_earth = [] | units.m
    y_earth = [] | units.m
    x_venus = [] | units.m
    y_venus = [] | units.m
    x_sun = [] | units.m
    y_sun = [] | units.m

    while gravity.model_time < end_time:

        gravity.evolve_model(gravity.model_time + (1 | units.s))
        x_sun.append(sun.x)
        y_sun.append(sun.y)
        x_earth.append(earth.x)
        y_earth.append(earth.y)
        x_venus.append(venus.x)
        y_venus.append(venus.y)
    gravity.stop()
    return x_earth, y_earth, x_venus, y_venus, x_sun, y_sun
Beispiel #4
0
def integrate_solar_system(particles, end_time):
    from amuse.lab import Huayno, nbody_system
    convert_nbody = nbody_system.nbody_to_si(particles.mass.sum(),
                                             particles[1].position.length())

    gravity = Huayno(convert_nbody)
    gravity.particles.add_particles(particles)
    venus = gravity.particles[1]
    earth = gravity.particles[2]

    x_earth = [] | units.AU
    y_earth = [] | units.AU
    x_venus = [] | units.AU
    y_venus = [] | units.AU

    while gravity.model_time < end_time:
        gravity.evolve_model(gravity.model_time + (1 | units.day))
        x_earth.append(earth.x)
        y_earth.append(earth.y)
        x_venus.append(venus.x)
        y_venus.append(venus.y)
    gravity.stop()
    return x_earth, y_earth, x_venus, y_venus
Beispiel #5
0
def integrate_solar_system():

    # Get initial conditions.
    solar = planetary_system()[0]
    debris = planetary_system()[1]

    star = solar[0]
    planet = solar[1]
    moon = solar[2]

    # Integrators set to Huayno.
    converter = nbody_system.nbody_to_si(star.mass, 1. | units.AU)
    system_gravity = Huayno(converter)
    system_gravity.particles.add_particle(star)
    system_gravity.particles.add_particle(planet)
    system_gravity.particles.add_particle(moon)

    disk_gravity = Huayno(converter)
    disk_gravity.particles.add_particle(debris)

    # Set up channels.
    channel_from_system_to_framework = system_gravity.particles.new_channel_to(
        solar)
    channel_from_disk_to_framework = disk_gravity.particles.new_channel_to(
        debris)

    # Set up bridge.
    gravity = bridge.Bridge(use_threading=False, method=SPLIT_4TH_S_M4)
    gravity.add_system(system_gravity, (disk_gravity, ))
    gravity.add_system(disk_gravity, (system_gravity, ))

    # Record positions.
    x_planet = quantities.AdaptingVectorQuantity()
    y_planet = quantities.AdaptingVectorQuantity()
    x_moon = quantities.AdaptingVectorQuantity()
    y_moon = quantities.AdaptingVectorQuantity()
    x_debris = quantities.AdaptingVectorQuantity()
    y_debris = quantities.AdaptingVectorQuantity()

    # Evolving time.
    t = 0 | units.day
    dt = 1 | units.day  # to make sure getting 250 snapshots
    t_end = 1 | units.yr  # 29.4571 yr Saturn's orbital period

    # Record time and energies.
    time = quantities.AdaptingVectorQuantity()
    system_E = quantities.AdaptingVectorQuantity()
    disk_E = quantities.AdaptingVectorQuantity()
    bridge_E = quantities.AdaptingVectorQuantity()

    # Evolve model.
    print 'Begin evolving'
    while t < t_end:
        t += dt
        gravity.evolve_model(t)

        channel_from_system_to_framework.copy()
        channel_from_disk_to_framework.copy()

        x_planet.append(planet.x)
        y_planet.append(planet.y)
        x_moon.append(moon.x)
        y_moon.append(moon.y)
        x_debris.append(debris.x)
        y_debris.append(debris.y)

        time.append(t)
        system_E.append(system_gravity.kinetic_energy +
                        system_gravity.potential_energy)
        disk_E.append(disk_gravity.kinetic_energy +
                      disk_gravity.potential_energy)
        bridge_E.append(gravity.kinetic_energy + gravity.potential_energy)

        pyplot.clf()
        pyplot.xlabel('x [au]')
        pyplot.ylabel('y [au]')
        #pyplot.xlim(-2.2, -1.9)
        #pyplot.ylim(8.7, 8.8)
        pyplot.scatter(star.x.value_in(units.AU),
                       star.y.value_in(units.AU),
                       color='black',
                       label='star')
        pyplot.scatter(planet.x.value_in(units.AU),
                       planet.y.value_in(units.AU),
                       color='blue',
                       label='planet')
        pyplot.scatter(moon.x.value_in(units.AU),
                       moon.y.value_in(units.AU),
                       color='red',
                       label='moon')
        pyplot.scatter(debris.x.value_in(units.AU),
                       debris.y.value_in(units.AU),
                       color='green',
                       s=1,
                       label='disk')
        #pyplot.legend()
        pyplot.savefig(
            '/home/rywang/capexam/plots/animation/positions_{0:03}.png'.format(
                t.number))

        print 'Generating plot{}'.format(t.number)

    gravity.stop()
    print 'End evolving'

    # Making animation.
    subprocess.call(
        "mencoder 'mf:///home/rywang/capexam/plots/animation/positions_*.png' -mf type=png:fps=20 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o /home/rywang/capexam/plots/animation/animation.mpg",
        shell=True)

    return time, system_E, disk_E, bridge_E, x_planet, y_planet, x_moon, y_moon, x_debris, y_debris
Beispiel #6
0
def MWG_tack_evolver(complete_pre_tack_system, converter, N_objects, times,
                     save_file_times, potential):
    #Initialise the gravity code and add the particles to it

    gravity_code = Huayno(converter)
    gravity_code.particles.add_particles(complete_pre_tack_system)
    channel = gravity_code.particles.new_channel_to(complete_pre_tack_system)

    gravity_bridge = 0
    gravity_bridge = bridge.Bridge(use_threading=False)
    gravity_bridge.add_system(gravity_code, (potential, ))
    gravity_bridge.timestep = 100 | units.yr

    #----------------------------------------------------------------------------------------------------
    #Here we define the 'correct' sma's for the different migrations. Also, the initial
    #planetary inclinations are stored for later use.

    initial_sma = np.array([3.5, 4.5, 6.013, 8.031]) | units.AU
    saturn_sma = np.array([1.5, 4.5, 6.013, 8.031]) | units.AU
    outward_sma = np.array([1.5, 1.5 *
                            ((3 / 2)**(2 / 3)), 6.013, 8.031]) | units.AU
    post_tack_sma = np.array([5.4, 7.1, 10.5, 13.]) | units.AU
    current_sma = [3.5, 4.5, 6.013, 8.031] | units.AU

    inclinations = [0, 0, 0, 0] | units.deg

    for k in range(4):
        orbital_elements = get_orbital_elements_from_binary(
            complete_pre_tack_system[0] + complete_pre_tack_system[k + 1],
            G=constants.G)
        inclinations[k] = orbital_elements[5]
    #----------------------------------------------------------------------------------------------------
    #Here we define the parameters used in the 'semi_major_axis_next_step' functions
    #The sma's are based on exact resonances. The time_scales are taken from literature.
    #The 0 values will be changed during the evolution of the model.
    #pre_resonant is used in the outward migration, when jupiter and saturn are
    #already in resonance with eachother, so that pre_resonant = True

    a_start = [1.5, 1.5 * ((3 / 2)**(2 / 3)), 0, 0] | units.AU
    a_end = [
        5.4, 5.4 * ((3 / 2)**(2 / 3)), 5.4 *
        ((3 / 2)**(2 / 3) * (9 / 5)**(2 / 3)), 5.4 * ((3 / 2)**(2 / 3) *
                                                      (5 / 2)**(2 / 3))
    ] | units.AU
    time_start = [1.025 * 10**5, 1.025 * 10**5, 0, 0] | units.yr
    time_scale = [5 * 10**5, 5 * 10**5, 0, 0] | units.yr

    resonances = [2 / 3, 3 / 2, 9 / 5, 5 / 2]
    pre_resonant = [False, False, True, True]

    outward_migration_started = False

    dead_comets = []

    #----------------------------------------------------------------------------------------------------
    #Below, the evolution starts.

    for i in tqdm(range(len(times) - 1)):
        gravity_code.evolve_model(times[i])
        channel.copy()

        #Save the model when we want it to
        if times[i] in save_file_times:
            write_set_to_file(
                gravity_code.particles,
                directory + 'MWG_Tack_run' + str(run_number) + '_time=' +
                str(np.log10(times[i].value_in(units.yr)))[0:5] + '.hdf5',
                format='hdf5',
                overwrite_file=True)

        #For each timestep determine the current sma's
        for j in range(4):
            current_sma[j] = sma_determinator(gravity_code.particles[0],
                                              gravity_code.particles[j + 1])

        #-----------------------------------------------------------------------------------------------------
        #This chunk of code describes the inward migration of jupiter
        #The first orbiter and the second particle in the gravity_code.
        if times[i] < 10**5 | units.yr:
            #This pushes Jupiter slightly inward
            sma_next_step = semi_major_axis_next_step_in_jup(
                times[i + 1], 10**5 | units.yr, 3.5 | units.AU, 1.5 | units.AU)

            binary = Particles(0)
            binary.add_particle(gravity_code.particles[0])
            binary.add_particle(gravity_code.particles[1])

            orbital_params = get_orbital_elements_from_binary(binary,
                                                              G=constants.G)
            true_anomaly, ascending_node, pericenter = orbital_params[4].in_(
                units.deg), orbital_params[6].in_(
                    units.deg), orbital_params[7].in_(units.deg)

            sun_and_planet = new_binary_from_orbital_elements(
                1 | units.MSun,
                orbital_params[1],
                sma_next_step,
                0,
                true_anomaly,
                inclinations[0],
                ascending_node,
                pericenter,
                G=constants.G)

            gravity_code.particles[1].position = (sun_and_planet[1].x +
                                                  gravity_code.particles[0].x,
                                                  sun_and_planet[1].y +
                                                  gravity_code.particles[0].y,
                                                  sun_and_planet[1].z +
                                                  gravity_code.particles[0].z)
            gravity_code.particles[1].velocity = (sun_and_planet[1].vx +
                                                  gravity_code.particles[0].vx,
                                                  sun_and_planet[1].vy +
                                                  gravity_code.particles[0].vy,
                                                  sun_and_planet[1].vz +
                                                  gravity_code.particles[0].vz)

            #During the tack, the masses of the planets increase towards their current values
            gravity_code.particles[2].mass *= 2**(1.5 / (10**5))
            gravity_code.particles[3].mass *= 1.2**(1.5 / (10**5))
            gravity_code.particles[4].mass *= 1.2**(1.5 / (10**5))

            #This keeps the other planets in place
            for j in range(3):
                binary = Particles(0)
                binary.add_particle(gravity_code.particles[0])
                binary.add_particle(gravity_code.particles[j + 2])

                orbital_params = get_orbital_elements_from_binary(
                    binary, G=constants.G)
                true_anomaly, ascending_node, pericenter = orbital_params[
                    4].in_(units.deg), orbital_params[6].in_(
                        units.deg), orbital_params[7].in_(units.deg)

                sun_and_planet = new_binary_from_orbital_elements(
                    1 | units.MSun,
                    orbital_params[1],
                    initial_sma[1 + j],
                    0,
                    true_anomaly,
                    inclinations[j + 1],
                    ascending_node,
                    pericenter,
                    G=constants.G)

                gravity_code.particles[j + 2].position = (
                    sun_and_planet[1].x + gravity_code.particles[0].x,
                    sun_and_planet[1].y + gravity_code.particles[0].y,
                    sun_and_planet[1].z + gravity_code.particles[0].z)
                gravity_code.particles[j + 2].velocity = (
                    sun_and_planet[1].vx + gravity_code.particles[0].vx,
                    sun_and_planet[1].vy + gravity_code.particles[0].vy,
                    sun_and_planet[1].vz + gravity_code.particles[0].vz)

        #------------------------------------------------------------------------------------------------------------
        #This chunk of code describes the inward migration of saturn
        elif 1 * 10**5 | units.yr <= times[i] < 1.025 * 10**5 | units.yr:
            #This pushes Saturn slightly inward
            sma_next_step = semi_major_axis_next_step_in_sat(
                times[i + 1], 2.5 * 10**3 | units.yr, 4.5 | units.AU,
                1.5 * ((3 / 2)**(2 / 3)) | units.AU)

            binary = Particles(0)
            binary.add_particle(gravity_code.particles[0])
            binary.add_particle(gravity_code.particles[2])

            orbital_params = get_orbital_elements_from_binary(binary,
                                                              G=constants.G)
            true_anomaly, ascending_node, pericenter = orbital_params[4].in_(
                units.deg), orbital_params[6].in_(
                    units.deg), orbital_params[7].in_(units.deg)

            sun_and_planet = new_binary_from_orbital_elements(
                1 | units.MSun,
                orbital_params[1],
                sma_next_step,
                0,
                true_anomaly,
                inclinations[1],
                ascending_node,
                pericenter,
                G=constants.G)

            gravity_code.particles[2].position = (sun_and_planet[1].x +
                                                  gravity_code.particles[0].x,
                                                  sun_and_planet[1].y +
                                                  gravity_code.particles[0].y,
                                                  sun_and_planet[1].z +
                                                  gravity_code.particles[0].z)
            gravity_code.particles[2].velocity = (sun_and_planet[1].vx +
                                                  gravity_code.particles[0].vx,
                                                  sun_and_planet[1].vy +
                                                  gravity_code.particles[0].vy,
                                                  sun_and_planet[1].vz +
                                                  gravity_code.particles[0].vz)

            gravity_code.particles[2].mass *= 1.5**(0.5 / (2500))
            gravity_code.particles[3].mass *= (17.15 / 6)**(0.5 / (5 * 10**5))
            gravity_code.particles[4].mass *= (14.54 / 6)**(0.5 / (5 * 10**5))

            #This keeps the other planets in place
            for j in [0, 2, 3]:
                binary = Particles(0)
                binary.add_particle(gravity_code.particles[0])
                binary.add_particle(gravity_code.particles[j + 1])

                orbital_params = get_orbital_elements_from_binary(
                    binary, G=constants.G)
                true_anomaly, ascending_node, pericenter = orbital_params[
                    4].in_(units.deg), orbital_params[6].in_(
                        units.deg), orbital_params[7].in_(units.deg)

                sun_and_planet = new_binary_from_orbital_elements(
                    1 | units.MSun,
                    orbital_params[1],
                    saturn_sma[j],
                    0,
                    true_anomaly,
                    inclinations[j],
                    ascending_node,
                    pericenter,
                    G=constants.G)

                gravity_code.particles[j + 1].position = (
                    sun_and_planet[1].x + gravity_code.particles[0].x,
                    sun_and_planet[1].y + gravity_code.particles[0].y,
                    sun_and_planet[1].z + gravity_code.particles[0].z)
                gravity_code.particles[j + 1].velocity = (
                    sun_and_planet[1].vx + gravity_code.particles[0].vx,
                    sun_and_planet[1].vy + gravity_code.particles[0].vy,
                    sun_and_planet[1].vz + gravity_code.particles[0].vz)

        #------------------------------------------------------------------------------------------------------------
        #This chunk of code describes the outward migration of all planets
        elif 1.025 * 10**5 | units.yr <= times[i] < 6 * 10**5 | units.yr:

            #This bit checks if uranus and neptune already should start migrating
            for k in range(4):
                if pre_resonant[k] == True:
                    if current_sma[k] / current_sma[1] < (resonances[k])**(2 /
                                                                           3):
                        pre_resonant[k] = False
                        a_start[k] = current_sma[k]
                        time_start[k] = times[i]
                        time_scale[k] = (6 * 10**5 | units.yr) - times[i]

            #If pre_resonant == False, pushes the planet outward. If true, keeps it in place
            for l in range(4):
                if pre_resonant[l] == False:
                    sma_next_step = semi_major_axis_next_step_out(
                        times[i + 1], time_start[l], a_end[l], a_start[l],
                        time_scale[l])

                    binary = Particles(0)
                    binary.add_particle(gravity_code.particles[0])
                    binary.add_particle(gravity_code.particles[l + 1])

                    orbital_params = get_orbital_elements_from_binary(
                        binary, G=constants.G)
                    true_anomaly, ascending_node, pericenter = orbital_params[
                        4].in_(units.deg), orbital_params[6].in_(
                            units.deg), orbital_params[7].in_(units.deg)

                    sun_and_planet = new_binary_from_orbital_elements(
                        1 | units.MSun,
                        orbital_params[1],
                        sma_next_step,
                        0,
                        true_anomaly,
                        inclinations[l],
                        ascending_node,
                        pericenter,
                        G=constants.G)

                    gravity_code.particles[l + 1].position = (
                        sun_and_planet[1].x + gravity_code.particles[0].x,
                        sun_and_planet[1].y + gravity_code.particles[0].y,
                        sun_and_planet[1].z + gravity_code.particles[0].z)
                    gravity_code.particles[l + 1].velocity = (
                        sun_and_planet[1].vx + gravity_code.particles[0].vx,
                        sun_and_planet[1].vy + gravity_code.particles[0].vy,
                        sun_and_planet[1].vz + gravity_code.particles[0].vz)

                else:
                    binary = Particles(0)
                    binary.add_particle(gravity_code.particles[0])
                    binary.add_particle(gravity_code.particles[l + 1])

                    orbital_params = get_orbital_elements_from_binary(
                        binary, G=constants.G)
                    true_anomaly, ascending_node, pericenter = orbital_params[
                        4].in_(units.deg), orbital_params[6].in_(
                            units.deg), orbital_params[7].in_(units.deg)

                    sun_and_planet = new_binary_from_orbital_elements(
                        1 | units.MSun,
                        orbital_params[1],
                        outward_sma[l],
                        0,
                        true_anomaly,
                        inclinations[l],
                        ascending_node,
                        pericenter,
                        G=constants.G)

                    gravity_code.particles[l + 1].position = (
                        sun_and_planet[1].x + gravity_code.particles[0].x,
                        sun_and_planet[1].y + gravity_code.particles[0].y,
                        sun_and_planet[1].z + gravity_code.particles[0].z)
                    gravity_code.particles[l + 1].velocity = (
                        sun_and_planet[1].vx + gravity_code.particles[0].vx,
                        sun_and_planet[1].vy + gravity_code.particles[0].vy,
                        sun_and_planet[1].vz + gravity_code.particles[0].vz)

            gravity_code.particles[3].mass *= (17.15 / 6)**(15 / (5 * 10**5))
            gravity_code.particles[4].mass *= (14.54 / 6)**(15 / (5 * 10**5))

        #------------------------------------------------------------------------------------------------------------
        #This chunk of code describes the post tack movements of the planets
        else:
            #---------------------------------------------------------------------------------------------------------------

            for l in range(4):
                if abs(current_sma[l] / post_tack_sma[l]) > 1.25 or abs(
                        current_sma[l] / post_tack_sma[l]
                ) < 0.75:  #The orbits are too much perturbed, so we end the simulation
                    return

                elif abs(current_sma[l] / post_tack_sma[l]) > 1.05 or abs(
                        current_sma[l] / post_tack_sma[l]
                ) < 0.95:  #The orbits are slightly perturbed, so we redefinie them
                    print("Here", complete_pre_tack_system[l + 1].name,
                          "was redefined")
                    binary = Particles(0)
                    binary.add_particle(gravity_code.particles[0])
                    binary.add_particle(gravity_code.particles[l + 1])

                    orbital_params = get_orbital_elements_from_binary(
                        binary, G=constants.G)
                    true_anomaly, ascending_node, pericenter = orbital_params[
                        4].in_(units.deg), orbital_params[6].in_(
                            units.deg), orbital_params[7].in_(units.deg)

                    sun_and_planet = new_binary_from_orbital_elements(
                        1 | units.MSun,
                        orbital_params[1],
                        post_tack_sma[l],
                        0,
                        true_anomaly,
                        inclinations[l],
                        ascending_node,
                        pericenter,
                        G=constants.G)

                    gravity_code.particles[l + 1].position = (
                        sun_and_planet[1].x + gravity_code.particles[0].x,
                        sun_and_planet[1].y + gravity_code.particles[0].y,
                        sun_and_planet[1].z + gravity_code.particles[0].z)
                    gravity_code.particles[l + 1].velocity = (
                        sun_and_planet[1].vx + gravity_code.particles[0].vx,
                        sun_and_planet[1].vy + gravity_code.particles[0].vy,
                        sun_and_planet[1].vz + gravity_code.particles[0].vz)
                else:
                    pass

                if i % 100 == 0:
                    print(complete_pre_tack_system[l].name, ' is at ',
                          (gravity_code.particles[l + 1].position -
                           gravity_code.particles[0].position).length().in_(
                               units.AU))
        #----------------------------------------------------------------------------------------------------------------------
        #Here we look for 'escaped' and 'out of bounds' comets
            out_of_bounds, escaped_comets = [], []
            for i in range(len(gravity_code.particles)):
                if (gravity_code.particles[i].position -
                        gravity_code.particles[0].position
                    ).length() > 500 | units.AU:
                    escaped_comets.append(gravity_code.particles[i])
                if (gravity_code.particles[i].position -
                        gravity_code.particles[0].position
                    ).length() > 250000 | units.AU:
                    out_of_bounds.append(gravity_code.particles[i])
                    dead_comets.append(gravity_code.particles[i])
            for particle in out_of_bounds:
                complete_pre_tack_system.remove_particle(particle)
                complete_pre_tack_system.synchronize_to(gravity_code.particles)
            if i % 1000 == 0:
                print("The amount of currently escaped comets is ",
                      len(escaped_comets))
                print("The amount of dead comets is ", len(dead_comets))

        if i % 1000 == 0:
            print("The sma's are: ", current_sma[0], current_sma[1],
                  current_sma[2], current_sma[3])

    gravity_code.stop()
    return complete_pre_tack_system
Beispiel #7
0
def MWG_evolver(complete_post_tack_system, converter, N_objects, potential, end_time, time_step):
    #Initialise the gravity code and add the particles to it
    gravity_code = Huayno(converter)
    gravity_code.particles.add_particles(complete_post_tack_system)
    channel = gravity_code.particles.new_channel_to(complete_post_tack_system)
    
    gravity_bridge = 0
    gravity_bridge = bridge.Bridge(use_threading=False)
    gravity_bridge.add_system(gravity_code, (potential,))
    gravity_bridge.timestep = 100 |units.yr
    
    times = np.arange(0., end_time, time_step) | units.yr #All time steps to which we want to evolve the model
    
    #---------------------------------------------------------------------------------------------------------
    #Here we define the planetary orbital parameters that should be returned to if the planets start moving too much 
    current_sma = np.array([0, 0, 0, 0]) | units.AU
    correct_sma =  np.array([5.4, 7.1, 10.5, 13]) | units.AU
    inclinations =  np.array([0, 0, 0, 0]) | units.deg
    
    
    system = new_solar_system()
    system = system[system.mass > 10**-5 | units.MSun] # Takes gas giants and Sun only
    system.move_to_center()
    for k in range(4):
        orbital_elements = get_orbital_elements_from_binary(system[0]+ system[k+1], G=constants.G)
        inclinations[k] =  orbital_elements[5]
    #------------------------------------------------------------------------------------------------------------
    dead_comets = [] #Here all 'dead' comets are stored
    
    #Below the evolving starts
    for i in tqdm(range(len(times))):
        gravity_bridge.evolve_model(times[i])
        channel.copy()
        
        #---------------------------------------------------------------------------------------------------------------
        #Here we check if the planetary orbits are still 'correct' and act for three degrees of incorrectness.
        for j in range(4):
            current_sma[j] = sma_determinator(gravity_code.particles[0], gravity_code.particles[j+1])
        
        for l in range(4):
            if abs(current_sma[l]/correct_sma[l]) > 1.25 or abs(current_sma[l]/correct_sma[l]) < 0.75: #The orbits are too much perturbed, so we end the simulation
                return
        
            elif abs(current_sma[l]/correct_sma[l]) > 1.05 or abs(current_sma[l]/correct_sma[l]) < 0.95: #The orbits are slightly perturbed, so we redefinie them
                print("Here", complete_post_tack_system[l+1].name, "was redefined")
                binary = Particles(0)
                binary.add_particle(gravity_code.particles[0])
                binary.add_particle(gravity_code.particles[l+1])

                orbital_params = get_orbital_elements_from_binary(binary, G = constants.G)
                true_anomaly, ascending_node, pericenter = orbital_params[4].in_(units.deg), orbital_params[6].in_(units.deg), orbital_params[7].in_(units.deg)

                sun_and_plan = new_binary_from_orbital_elements(1 | units.MSun, orbital_params[1], #We keep the current angles, but change the a, e and i back
                                                      correct_sma[l], 0, true_anomaly, inclinations[l], ascending_node, pericenter, G=constants.G)
                
                gravity_code.particles[l+1].position = (sun_and_plan[1].x+gravity_code.particles[0].x, sun_and_plan[1].y+gravity_code.particles[0].y, sun_and_plan[1].z+gravity_code.particles[0].z)
                gravity_code.particles[l+1].velocity = (sun_and_plan[1].vx+gravity_code.particles[0].vx, sun_and_plan[1].vy+gravity_code.particles[0].vy, sun_and_plan[1].vz+gravity_code.particles[0].vz)
            else: #The orbits do not need changing
                pass
        #----------------------------------------------------------------------------------------------------------------------
        #Once we checked for the orbital correctness, we can save data 
        
        if i%4000 == 0:
            write_set_to_file(gravity_code.particles, directory + 'MWG_run' + str(run_number) +'_time=' + str(np.log10(times[i].value_in(units.yr)))[0:5] + '.hdf5', format='hdf5', overwrite_file = True)
            
        #--------------------------------------------------------------------------------------------------------------------
        #Here we look for 'escaped' and 'out of bounds' comets
        out_of_bounds, escaped_comets = [], []
        for i in range(len(gravity_code.particles)):
            if (gravity_code.particles[i].position-gravity_code.particles[0].position).length() > 500 | units.AU:
                escaped_comets.append(gravity_code.particles[i])
                if (gravity_code.particles[i].position-gravity_code.particles[0].position).length() > 250000 | units.AU:
                    out_of_bounds.append(gravity_code.particles[i])
                    dead_comets.append(gravity_code.particles[i])
        for particle in out_of_bounds: #Out of bounds comets are removed completely
            complete_post_tack_system.remove_particle(particle)
            complete_post_tack_system.synchronize_to(gravity_code.particles)
            
        
        if i%100 == 0:
            print("The amount of currently escaped comets is ", len(escaped_comets))
            print("The amount of dead comets is ", len(dead_comets))
            for m in range(4):
                print(complete_post_tack_system[m+1].name, " is at ", (gravity_code.particles[m+1].position-gravity_code.particles[0].position).length().in_(units.AU))
    
        
    gravity_code.stop()
    write_set_to_file(gravity_code.orbiters, directory + 'MWG_run' + str(run_number) +'_final.hdf5', format='hdf5', overwrite_file = True)
    return complete_post_tack