1010# noinspection PyUnusedLocal
1111class outer_solar_system (ptype ):
1212 """
13- Example implementing the harmonic oscillator
13+ Example implementing the outer solar system problem
1414 """
1515
1616 def __init__ (self , problem_params , dtype_u , dtype_f ):
@@ -23,6 +23,9 @@ def __init__(self, problem_params, dtype_u, dtype_f):
2323 dtype_f: acceleration data type (will be passed parent class)
2424 """
2525
26+ if 'sun_only' not in problem_params :
27+ problem_params ['sun_only' ] = False
28+
2629 # these parameters will be used later, so assert their existence
2730 essential_keys = []
2831 for key in essential_keys :
@@ -32,6 +35,8 @@ def __init__(self, problem_params, dtype_u, dtype_f):
3235
3336 # invoke super init, passing nparts, dtype_u and dtype_f
3437 super (outer_solar_system , self ).__init__ ((3 , 6 ), dtype_u , dtype_f , problem_params )
38+
39+ # gravitational constant
3540 self .G = 2.95912208286E-4
3641
3742 def eval_f (self , u , t ):
@@ -46,24 +51,37 @@ def eval_f(self, u, t):
4651 """
4752 me = acceleration (self .init , val = 0.0 )
4853
49- for i in range (self .init [- 1 ]):
50- for j in range (i ):
51- dx = u .pos .values [:, i ] - u .pos .values [:, j ]
54+ # compute the acceleration due to gravitational forces
55+ # ... only with respect to the sun
56+ if self .params .sun_only :
57+
58+ for i in range (1 , self .init [- 1 ]):
59+ dx = u .pos .values [:, i ] - u .pos .values [:, 0 ]
5260 r = np .sqrt (np .dot (dx , dx ))
5361 df = self .G * dx / (r ** 3 )
54- me .values [:, i ] -= u .m [j ] * df
55- me .values [:, j ] += u .m [i ] * df
62+ me .values [:, i ] -= u .m [0 ] * df
63+
64+ # ... or with all planets involved
65+ else :
66+
67+ for i in range (self .init [- 1 ]):
68+ for j in range (i ):
69+ dx = u .pos .values [:, i ] - u .pos .values [:, j ]
70+ r = np .sqrt (np .dot (dx , dx ))
71+ df = self .G * dx / (r ** 3 )
72+ me .values [:, i ] -= u .m [j ] * df
73+ me .values [:, j ] += u .m [i ] * df
5674
5775 return me
5876
5977 def u_exact (self , t ):
6078 """
61- Routine to compute the exact trajectory at time t
79+ Routine to compute the exact/initial trajectory at time t
6280
6381 Args:
6482 t (float): current time
6583 Returns:
66- dtype_u: exact position and velocity
84+ dtype_u: exact/initial position and velocity
6785 """
6886 assert t == 0.0 , 'error, u_exact only works for the initial time t0=0'
6987 me = particles (self .init )
@@ -82,12 +100,12 @@ def u_exact(self, t):
82100 me .vel .values [:, 4 ] = [0.00288930 , 0.00114527 , 0.00039677 ]
83101 me .vel .values [:, 5 ] = [0.00276725 , - 0.0017072 , - 0.00136504 ]
84102
85- me .m [0 ] = 1.00000597682
86- me .m [1 ] = 0.000954786104043
87- me .m [2 ] = 0.000285583733151
88- me .m [3 ] = 0.0000437273164546
89- me .m [4 ] = 0.0000517759138449
90- me .m [5 ] = 1.0 / 130000000.0
103+ me .m [0 ] = 1.00000597682 # Sun
104+ me .m [1 ] = 0.000954786104043 # Jupiter
105+ me .m [2 ] = 0.000285583733151 # Saturn
106+ me .m [3 ] = 0.0000437273164546 # Uranus
107+ me .m [4 ] = 0.0000517759138449 # Neptune
108+ me .m [5 ] = 1.0 / 130000000.0 # Pluto
91109
92110 return me
93111
0 commit comments