with_units = len(sys.argv) > 2

    if not with_units :
        mass_unit = nbody_system.mass
        length_unit = nbody_system.length
    else :
        mass_unit = units.MSun
        length_unit = units.parsec

    m_min = 0.1 | mass_unit
    m_max = 100 | mass_unit
    alpha = -2.35

    r_vir = 1 | length_unit
    initial_mass_function = SalpeterIMF(m_min, m_max, alpha)
    m_tot, masses = initial_mass_function.next_set(nstars)

    if not with_units :
        convert_nbody = None
        masses /= m_tot.value_in(nbody_system.mass)     # scale to unit mass 
        m_tot = 1 | nbody_system.mass
    else :
        convert_nbody = nbody_system.nbody_to_si(m_tot, r_vir)
        convert_nbody.set_as_default()
        print m_tot

    stars = MakePlummerModel(nstars, convert_nbody, random_state = seed).result;
    stars.mass = masses 
    
    LagrangianRadii(stars, verbose=1)
def main():
    N=1024
    N2 = 0
    x = 1.25

    #convert_nbody = nbody_system.nbody_to_si(1.0*N | units.MSun, 149.5e3 | units.AU)
    
    radius = 0.0 |  nbody_system.length
    #radius = convert_nbody.to_si(radius)
    eta=1/32. | nbody_system.time
    #eta = convert_nbody.to_si(eta) 
    eps=0.001 | nbody_system.length
    #eps = convert_nbody.to_si(eps) 
    dt=0.5 | nbody_system.time
    #dt = convert_nbody.to_si(dt)
    tmax=350 | nbody_system.time
    #tmax = convert_nbody.to_si(tmax)
    
    parts=MakePlummerModel(N).result
    parts.radius=radius
    #Mass segregation
    if N2 >0:
     N1 = N-N2
     m1 = 1/(N1+x*N2)
     m2 = x*m1
     M1 = numpy.zeros((N1,1)) + (m1)
     M2 = numpy.zeros((N2,1)) + (m2)
     M = numpy.concatenate((M1,M2))
     parts.mass = nbody_system.mass.new_quantity(numpy.hstack(M))
    #gravity = Fi(convert_nbody)
    gravity = Fi()
    gravity.initialize_code()
    gravity.parameters.epsilon_squared = eps**2
    gravity.parameters.timestep = eta
    gravity.legacy_interface.set_gdgtol(0.001)
    gravity.legacy_interface.set_gdgop(0)
    gravity.legacy_interface.set_usequad(0)
    gravity.legacy_interface.set_acc_tstp(0)
    gravity.legacy_interface.set_bh_tol(0.5)
    gravity.legacy_interface.set_tstpcr2(0.025)
    gravity.legacy_interface.set_sqrttstp(0)
    gravity.legacy_interface.set_tstepcrit(0.25)
    #gravity.parameters.initial_timestep_parameter=etas 
    gravity.commit_parameters()
    gravity.particles.add_particles(parts)
    time=0 | nbody_system.time
    #time = convert_nbody.to_si(time)
    gravity.synchronize_model()
    #e0=gravity.particles.kinetic_energy() + \
    # gravity.particles.potential_energy()
#    e0=gravity.particles.kinetic_energy() + \     gravity.particles.potential_energy(smoothing_length_squared=eps**2,G=nbody_system.G)
    e0=gravity.kinetic_energy + \
     gravity.potential_energy
    
    output_file = "Fi.hdf5"
    if os.path.exists(output_file):
       os.remove(output_file)
    storage = store.StoreHDF(output_file)
    
    #from_gravity_to_model = gravity.particles.new_channel_to(parts)
    #L = []
    #output = open('data.pkl', 'wb')
    parts = gravity.particles.copy()
#    ek = gravity.kinetic_energy
#    ep = gravity.potential_energy
#    e0 = ek + ep
    while time < tmax:
      ek = gravity.kinetic_energy
      ep = gravity.potential_energy
      print "time,T/V,err_E:", time,(ek/ep).number, ((ep+ek-e0)/e0).number
      #print "time,T/V,err_E:", time, ep
    #  L.append(LagrangianRadii(parts)[-1])
      gravity.particles.new_channel_to(parts).copy()
      #parts = gravity.particles.copy()
      #parts = gravity.particles.copy()
      parts.savepoint(time)
      storage.store(parts.previous_state())
      time=time+dt
      gravity.evolve_model(time)
      gravity.synchronize_model()
    #  write_set_to_file(parts,'cc_nb/cc_nb_'+str(time.number/dt.number)+'.txt')
      #parts_temp = gravity.particles.copy()
      #print parts_temp
    #  pickle.dump(adaas,output)
#    parts.savepoint(time)
#    storage.store(parts.previous_state())
    storage.close()
    #output.close()
    
    del gravity
    del parts
from amuse.legacy.bhtree.interface import BHTreeInterface, BHTree

DEBUG = 1
radius = 0.0 | nbody_system.length
N = 124
eta = 0.005
etas = 0.005
# smoothing value
#eps = 0.001 * 100.0 | nbody_system.length
eps = 0.001 | nbody_system.length
dt = 0.5 | nbody_system.time
tmax = 500.0 | nbody_system.time
isBinary = numpy.zeros(N)
epsTolerance = 1000 * eps.number
  
parts = MakePlummerModel(N).result
parts.radius = radius

#gravity=PhiGRAPE()
# BHTree code
#convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km)
gravity = BHTree()
gravity.parameters.epsilon_squared = eps ** 2
#gravity.parameters.timestep_parameter=eta 
#gravity.parameters.initial_timestep_parameter=etas 
#gravity.commit_parameters()
#gravity.parameters.timestep = dt / 1024.0
gravity.parameters.timestep = dt / 1024.0
gravity.parameters.opening_angle = 0.5 | units.none
gravity.commit_parameters()
#gravity.commit_particles()
from amuse.support.units import units
from amuse.support.io import store
from amuse.support.io import write_set_to_file
import pickle
import os

radius = 0.0 |  nbody_system.length
N=1024
eta=0.005
etas=0.005
eps=0.001 | nbody_system.length
dt=0.5 | nbody_system.time
tmax=500 | nbody_system.time


parts=MakePlummerModel(N).result
#Mass segregation
N2 = 82
x = 1.25
N1 = N-N2
m1 = 1/(N1+x*N2)
m2 = x*m1
M1 = numpy.zeros((N1,1)) + (m1)
M2 = numpy.zeros((N2,1)) + (m2)
M = numpy.concatenate((M1,M2))
parts.mass = nbody_system.mass.new_quantity(numpy.hstack(M))
parts.radius=radius

gravity=PhiGRAPE()#(mode="gpu")
gravity.parameters.epsilon_squared = eps**2
gravity.particles.add_particles(parts)