11"""
2- Simple routine for running a gravity code
2+ Simple routine for running a gravity code
33"""
4- from amuse .lab import *
5- from matplotlib import pyplot
6- from amuse .examples .prepare_figure import single_frame , figure_frame , set_tickmarks
7- from amuse .examples .distinct_colours import get_distinct
4+
5+ import argparse
6+ import matplotlib .pyplot as plt
7+ from amuse .units import nbody_system
8+ from amuse .ic .plummer import new_plummer_model
9+ from amuse .community .bhtree import Bhtree
10+ from amuse .community .huayno import Huayno
11+ from amuse .community .kepler import Kepler
12+ from amuse .community .ph4 import Ph4
13+
814
915def virial_ratio_evolution (code , bodies , Q_init , t_end ):
1016 dt = 0.06125 | t_end .unit
1117 bodies .scale_to_standard (virial_ratio = Q_init )
12- bodies .radius = 0 | nbody_system .length
18+ bodies .radius = 0 | nbody_system .length
1319 gravity = code ()
1420 gravity .particles .add_particles (bodies )
1521
@@ -19,52 +25,62 @@ def virial_ratio_evolution(code, bodies, Q_init, t_end):
1925 time = [0.0 ] | t_end .unit
2026 Q = [Q_init ]
2127 while time [- 1 ] < t_end :
22- time .append (time [- 1 ]+ dt )
28+ time .append (time [- 1 ] + dt )
2329 gravity .evolve_model (time [- 1 ])
2430 channel_from_gravity_to_framework .copy ()
25- Ekin = gravity .kinetic_energy
31+ Ekin = gravity .kinetic_energy
2632 Epot = gravity .potential_energy
2733 Etot = Ekin + Epot
28- Q .append (- 1 * Ekin / Epot )
29- print ("T=" , time [- 1 ], "Q= " , Q [- 1 ], end = ' ' )
30- print ("M=" , bodies .mass .sum (), "E= " , Etot , end = ' ' )
31- print ("dE=" , (Etot_init - Etot )/ Etot , "ddE=" , (Etot_prev - Etot )/ Etot )
34+ Q .append (- 1 * Ekin / Epot )
35+ print ("T=" , time [- 1 ], "Q= " , Q [- 1 ], end = " " )
36+ print ("M=" , bodies .mass .sum (), "E= " , Etot , end = " " )
37+ print ("dE=" , (Etot_init - Etot ) / Etot , "ddE=" , (Etot_prev - Etot ) / Etot )
3238 Etot_prev = Etot
3339 gravity .stop ()
3440 return time , Q
3541
36- def main ( N , t_end ):
37- t_end = t_end | nbody_system . time
42+
43+ def gravity_to_virial ( N , t_end ):
3844 Q_init = 0.2
3945 particles = new_plummer_model (N )
40- codes = [ph4 , Huayno , BHTree ]
41- cols = get_distinct (3 )
46+ codes = [Ph4 , Huayno , Bhtree ]
4247 ci = 0
4348 x_label = "time [N-body units]"
44- # y_label = "$Q [\equiv -E_{\rm kin}/E_{\rm pot}]$"
49+ # y_label = "$Q [\equiv -E_{\rm kin}/E_{\rm pot}]$"
4550 y_label = "virial ratio $Q$"
46- figure = single_frame (x_label , y_label , xsize = 14 , ysize = 10 )
47- ax1 = pyplot .gca ()
51+ figure = plt .figure (figsize = (14 , 10 ))
52+ ax1 = figure .add_subplot (1 , 1 , 1 )
53+ ax1 .set_xlabel (x_label )
54+ ax1 .set_ylabel (y_label )
4855 ax1 .set_xlim (0 , t_end .value_in (t_end .unit ))
4956 ax1 .set_ylim (0 , 0.65 )
50- pyplot .plot ([0 , t_end .value_in (t_end .unit )], [0.5 , 0.5 ], lw = 1 , ls = '--' , c = 'k' )
57+ plt .plot ([0 , t_end .value_in (t_end .unit )], [0.5 , 0.5 ], lw = 1 , ls = "--" , c = "k" )
5158 for code in codes :
5259 time , Q = virial_ratio_evolution (code , particles , Q_init , t_end )
53- pyplot .plot (time .value_in (t_end .unit ), Q , c = cols [ci ])
54- ci += 1
55- # pyplot.show()
56- pyplot .savefig ("gravity_to_virial" )
57-
58- def new_option_parser ():
59- from optparse import OptionParser
60- result = OptionParser ()
61- result .add_option ("-N" , dest = "N" , type = "int" ,default = 1000 ,
62- help = "number of stars [10]" )
63- result .add_option ("-t" , dest = "t_end" , type = "float" , default = 2 ,
64- help = "end time of the simulation [1] N-body units" )
65- return result
60+ plt .plot (time .value_in (t_end .unit ), Q )
61+ ci += 1
62+ plt .savefig ("gravity_to_virial" )
63+
64+
65+ def new_argument_parser ():
66+ parser = argparse .ArgumentParser (
67+ formatter_class = argparse .ArgumentDefaultsHelpFormatter
68+ )
69+ parser .add_argument ("-N" , type = int , default = 1000 , help = "number of stars" )
70+ parser .add_argument (
71+ "-t" ,
72+ "--t_end" ,
73+ type = nbody_system .time ,
74+ default = 2 | nbody_system .time ,
75+ help = "end time of the simulation" ,
76+ )
77+ return parser
78+
79+
80+ def main ():
81+ args = new_argument_parser ().parse_args ()
82+ gravity_to_virial (** args .__dict__ )
6683
67- if __name__ in ('__main__' , '__plot__' ):
68- o , arguments = new_option_parser ().parse_args ()
69- main (** o .__dict__ )
7084
85+ if __name__ == "__main__" :
86+ main ()
0 commit comments