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
|
#!/usr/bin/env python
# import libraries
import time
import numpy as np
import sys
from subprocess import call
# define the number of stars that should be generated
nos = int(sys.argv[1])
# define various paths
path = "data/2e7.csv"
save_path = "stars/" + sys.argv[2] + ".csv"
# define the random-value range [rho_min; rho_max]
rand_min = 0
rand_max = 1477.1586582000994
# define the range (size) of the galaxy
range_min = -1e8
range_max = -range_min
# main function
def main():
# define the variables for storing the amount of stars kept or kicked away
stars_kept = 0
stars_kicked = 0
# start the timer
start = time.time()
# open the rho file
with open(path) as data:
# read out the lines from the rho file
rho_file = data.readlines()
# for every star...
while(stars_kept < nos):
# generate the random star-coordinates
x = np.random.uniform(range_min, range_max, size=1)
y = np.random.uniform(range_min, range_max, size=1)
z = np.random.uniform(range_min, range_max, size=1)
# calculate the distance of the star to the center of the galaxy
r = np.sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))
# generate a random value in the range [rand_min; rand_max]
a = np.random.uniform(rand_min, rand_max, size=1)
# read out the corresponding rho value from the lookuptable (rho-file)
b = float(rho_file[round(int(r / 10), 0)].split(", ")[1].strip("\n"))
# if the random value is smaller than the corresponding rho value
if(a < b):
with open(save_path, "a") as stars_data:
stars_data.write(str(float(x)) + ", " + str(float(y)) + ", " + str(float(z)) + "\n")
# increment the stars_kept counter
stars_kept += 1
print(stars_kept)
else:
# increment the star_kicked counter
stars_kicked += 1
print("")
end = time.time()
whole_time = end - start
out = ">> Finished generating stars in " + str(whole_time) + " seconds\n"
print(out)
# time_all = whole_time + time_write_file
time_all = whole_time
time_min = round(time_all / 60, 1)
# print some stats
print("")
print("{:<20}{:<20}".format("Time (complete)", str(round(time_all, 4)) + " seconds"))
print("{:-<40}".format(""))
print("{:<20}{:<20}".format("Number of Stars", str(nos)))
print("{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked)))
print("{:<20}{:<20}".format("Percent: ", str( nos / stars_kicked * 100 ) + "%"))
hour = int( time_all // 3600 )
time_all = time_all % 3600
minutes = int( time_all // 60 )
time_all = time_all % 60
seconds = int( time_all )
a = "stars/" + str(sys.argv[2]) + ".csv"
time_a = str(hour) + ":" + str(minutes) + ":" + str(seconds)
b = "{:<20}{:<20}".format("Time (h:m:s)", time_a )
c = "{:<20}{:<20}".format("Number of Stars", str(nos))
d = "{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked))
e = "{:<20}{:<20}".format("Percent: ", str( nos / stars_kicked * 100 ) + "%")
f = a + "\n" + b + "\n" + c + "\n" + d + "\n" + e
call(["telegram-send", "--pre", str(f) ])
call(["telegram-send", "-f", str(a) ])
if __name__ == "__main__":
main()
|