-
Notifications
You must be signed in to change notification settings - Fork 0
/
turorial3.py
118 lines (75 loc) · 2.4 KB
/
turorial3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# coding: utf-8
# In[12]:
#problem2
import numpy as np
class N_body:
#defining the initial variables
def __init__(self,mass = 0,x_pos = 0, y_pos = 0):
self.mass = mass
self.x_pos = x_pos
self.y_pos = y_pos
#Entries in a dictionary
var = {'#_particles': 10, 'Gravitational constant': 6.67*10**-11}
def potential_energy(self,m,x,y):
r = np.zeros(len(x))
E_top = np.zeros(len(r))
f = np.ones(len(r))
E = np.zeros(len(r))
k = ()
for k in range(0,len(m)):
for i in range(0,len(x)):
r[i] = np.sqrt((x[k]-x[i])**2 + (y[k]-y[i])**2)
if r[i] != 0:
f[i] = r[i]
for i in range(0,len(x)):
E_top[i] = N_body.var['Gravitational constant']*m[k]*m[i]
for i in range(0,len(x)):
if r[i] != 0:
E[i] = E_top[i]/r[i]
print (E.sum())
if __name__=="__main__":
s = N_body.var['#_particles']
x = np.random.randint(1,10,size=s)
y = np.random.randint(1,10,size=s)
m = np.random.randint(1,4,size=s)
test = N_body(m,x,y)
print ('The mass ' + repr(test.mass))
print ('The x position ' + repr(test.x_pos))
print ('The y position ' + repr(test.y_pos), "with potentials below")
energy = test.potential_energy(test.mass,test.x_pos,test.y_pos)
# In[9]:
#problem3
import numpy as np
class N_body:
def __init__(self,mass = 0,x_pos = 0, y_pos = 0):
self.mass = mass
self.x_pos = x_pos
self.y_pos = y_pos
var = {'#_particles': 10, 'Gravitational_constant': 6.67*10**-11}
def SoftenedForce_E(self,m,x,y):
r = np.zeros(len(x))
E_top = np.zeros(len(r))
f = np.ones(len(r))
E = np.zeros(len(r))
k = ()
for k in range(0,len(m)):
for i in range(0,len(x)): #with softening factor
r[i] = (((x[k]-x[i])**2 + (y[k]-y[i])**2 + 0.9)**1.5/np.sqrt(x[k]-x[i])**2 + (y[k]-y[i])**2)
if r[i] != 0:
f[i] = r[i]
for i in range(0,len(x)):
E_top[i] = N_body.var['Gravitational_constant']*m[k]*m[i]
for i in range(0,len(x)):
if r[i] != 0:
E[i] = E_top[i]/r[i]
print (E.sum())
if __name__=="__main__":
s = N_body.var['#_particles']
x = np.random.randint(1,10,size=s)
y = np.random.randint(1,10,size=s)
m = np.random.randint(1,4,size=s)
test = N_body(m,x,y)
print ('mass is ' + repr(test.mass))
print ('x_pos is ' + repr(test.x_pos))
print ('y_pos is ' + repr(test.y_pos), "with softened potential below")
energy = test.SoftenedForce_E(test.mass,test.x_pos,test.y_pos)