-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_serial.py
More file actions
executable file
·99 lines (81 loc) · 3.24 KB
/
Copy pathmain_serial.py
File metadata and controls
executable file
·99 lines (81 loc) · 3.24 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import json
import os
from timeit import default_timer as timer
from phys import get_accel
from plotlib import plot_particle_traj
if __name__=="__main__":
dt = 3600 # one hour
fintime = 365*24*3600 # one Earth year
nsteps = int(fintime/dt)
checkpointstep = nsteps+1 # Save checkpoint data, turned off for now
histstep = 20 # Steps at which to save output
cmap = "viridis_r"
iplot = True # To show plot
isave = True # To save plot
dat = np.loadtxt('./input_data.solar_system',dtype='str')
npart = dat.shape[0]
names = dat[:,0]
mass = np.float32(dat[:,1])
positions = np.float32(dat[:,2:5])
velocities = np.float32(dat[:,5:])
accel = np.zeros([npart,3])
# Random particles
# npart = 100
# mass = 10**np.random.uniform(23,30,npart)
# positions = 10**np.random.uniform(5,9,npart*3).reshape([npart,3])
# velocities = 10**np.random.uniform(-3,1,npart*3).reshape([npart,3])
# accel = np.zeros([npart,3])
pos_plot = positions
timing = []
t = 0
checkpoint_counter=0
step_counter=0 #Actual number of steps run
for istep in range(nsteps):
tic = timer()
positions += 0.5*dt*velocities
for ipart in range(npart):
accel[ipart,:] = get_accel( positions[ipart,:],
np.delete(mass,ipart),
np.delete(positions,ipart,axis=0)
)
velocities += dt*accel
positions += 0.5*dt*velocities
toc = timer()
timing.append(toc-tic)
if not istep%histstep:
v_sq = np.sum(velocities**2,axis=1)
ke = np.sum(0.5 * mass * v_sq)
X = np.array([t, ke])
fmt = ['%.5e','%.5e']
with open('kinetic_energy.dat','a') as ke_file:
np.savetxt(ke_file,X.reshape(1,X.shape[0]),fmt=fmt)
pos_plot = np.vstack([pos_plot,positions])
print("Saving KE at step %d/%d , time=%.5e" %(istep,nsteps,t))
if not istep%checkpointstep:
# Save some parameters
params_dict = {'npart' : npart,
'dt' : dt,
'steps' : step_counter,
'time' : t
}
with open('params.json','w') as params_file:
json.dump(params_dict,params_file)
X = np.hstack([positions,velocities])
with open('checkpoint_%d.dat' %checkpoint_counter,'w') as chk_file:
np.savetxt(chk_file,X,fmt='%.5e')
checkpoint_counter += 1
t += dt
step_counter += 1
print("Mean time per step: %f" %(np.mean(timing)))
if iplot:
fig,ax = plot_particle_traj(npart,pos_plot,cmap)
if isave:
if not os.path.exists('images'):
os.mkdir('images')
plt.savefig('images/trajectories.pdf',bbox_inches='tight')
plt.savefig('images/trajectories.png',dpi=300,bbox_inches='tight')
plt.show()