From 41322dea056ec583d86587fef0761af2f83dcc5d Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 4 Dec 2017 23:24:20 +0100 Subject: rewrote the script to generate stars in a 2D space --- src/gen.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'src/gen.py') diff --git a/src/gen.py b/src/gen.py index 829c30d..ca7616b 100755 --- a/src/gen.py +++ b/src/gen.py @@ -5,6 +5,7 @@ import numpy as np import math import os import socket +import time host = socket.gethostname() # variables @@ -12,28 +13,18 @@ sigma = 200 f_0 = 0.1 R_s = 1e4 -# M.. -Mxx = 0 -Mxy = 8e-6 -Myy = 0 - # constants pi = math.pi e = math.e G = 4.302e-3 # rho function -def rho(x, y, z): - r = math.sqrt(x**2 + y**2 + z**2) +def rho(r): a = (1) / (math.sqrt( 2 * pi ) * sigma ) b = math.exp( - (phi(r) / sigma ** 2 ) ) c = a * b return c -def rho_new(x, y, z): - a = (1 - ((1) / (2 * (sigma ** 2))) * ( Mxx * x**2 + 2 * Mxy * x * y + Myy * y**2 ) ) - return rho(x, y, z) * a - # phi function def phi(x): if x == 0: @@ -62,24 +53,33 @@ def gen_stars(stars): range_max = int(length) # define the rho range - rand_min = rho_new(0, 0, 0) - rand_max = rho_new(length, 0, 0) + rand_min = rho(0) + rand_max = rho(math.sqrt(length**2 + length**2)) # create random stars for r in range(0, stars): 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) rand_val = np.random.uniform(rand_min, rand_max, size=1) + rho_xy = rho(math.sqrt(x**2 + y**2)) + + print("{:<20}{:<20}{:<20}{:<20}".format(str(x), str(y), str(rho_xy), str(rand_val))) # if the random value is smaller than the rho value, generate a star - if rand_val < rho(x, y, z): + if rand_val < rho_xy: # open a file to write to with open(path, "a") as data: # write the data to the file - data.write(str(x).strip("[]") + "," + str(y).strip("[]") + "," + str(z).strip("[]") + "\n") + data.write(str(x).strip("[]") + "," + str(y).strip("[]") + "\n") + print(str(x) + ", " + str(y)) + + print("range_min: " + str(range_min)) + print("range_max: " + str(range_max)) + + print("rand_min: " + str(rand_min)) + print("rand_max: " + str(rand_max)) # generate n stars -gen_stars(5e7) +gen_stars(1e5) -- cgit 1.4.1 From 19e8be5a5cca95474abf934fd13404dd5b73fe84 Mon Sep 17 00:00:00 2001 From: hanemile Date: Tue, 5 Dec 2017 12:03:44 +0100 Subject: implemented displaying the stars using an x-y-coordinate system --- src/gen.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/gen.py') diff --git a/src/gen.py b/src/gen.py index ca7616b..8f8b26f 100755 --- a/src/gen.py +++ b/src/gen.py @@ -6,6 +6,7 @@ import math import os import socket import time +import matplotlib.pyplot as plt host = socket.gethostname() # variables @@ -35,6 +36,9 @@ def phi(x): c = a * b return c +listx = [] +listy = [] + def gen_stars(stars): stars = int(stars) @@ -63,8 +67,6 @@ def gen_stars(stars): rand_val = np.random.uniform(rand_min, rand_max, size=1) rho_xy = rho(math.sqrt(x**2 + y**2)) - print("{:<20}{:<20}{:<20}{:<20}".format(str(x), str(y), str(rho_xy), str(rand_val))) - # if the random value is smaller than the rho value, generate a star if rand_val < rho_xy: @@ -73,6 +75,8 @@ def gen_stars(stars): # write the data to the file data.write(str(x).strip("[]") + "," + str(y).strip("[]") + "\n") + listx.append(x) + listy.append(y) print(str(x) + ", " + str(y)) print("range_min: " + str(range_min)) @@ -82,4 +86,9 @@ def gen_stars(stars): print("rand_max: " + str(rand_max)) # generate n stars -gen_stars(1e5) +gen_stars(1e6) + +# plot the stars coordinates in 2D space +plt.scatter(listx, listy) +# plt.plot(listx, listy) +plt.show() -- cgit 1.4.1 From 9014e9febc70f81ad9f3454317a7b16e578dd7a9 Mon Sep 17 00:00:00 2001 From: hanemile Date: Tue, 5 Dec 2017 12:15:18 +0100 Subject: removed the old plot --- src/gen.py | 1 - 1 file changed, 1 deletion(-) (limited to 'src/gen.py') diff --git a/src/gen.py b/src/gen.py index 8f8b26f..242636d 100755 --- a/src/gen.py +++ b/src/gen.py @@ -90,5 +90,4 @@ gen_stars(1e6) # plot the stars coordinates in 2D space plt.scatter(listx, listy) -# plt.plot(listx, listy) plt.show() -- cgit 1.4.1 From 3945015abbfc48da21f2cc81dee006a9955fcc84 Mon Sep 17 00:00:00 2001 From: hanemile Date: Tue, 5 Dec 2017 16:09:14 +0100 Subject: added comments, command-line arguments and some fancy output --- src/gen.py | 27 --------------------------- 1 file changed, 27 deletions(-) (limited to 'src/gen.py') diff --git a/src/gen.py b/src/gen.py index 242636d..b4b7933 100755 --- a/src/gen.py +++ b/src/gen.py @@ -9,33 +9,6 @@ import time import matplotlib.pyplot as plt host = socket.gethostname() -# variables -sigma = 200 -f_0 = 0.1 -R_s = 1e4 - -# constants -pi = math.pi -e = math.e -G = 4.302e-3 - -# rho function -def rho(r): - a = (1) / (math.sqrt( 2 * pi ) * sigma ) - b = math.exp( - (phi(r) / sigma ** 2 ) ) - c = a * b - return c - -# phi function -def phi(x): - if x == 0: - return -4 * pi * f_0 * G * R_s**2 - - a = - ( 4 * pi * G * f_0 * R_s ** 3 ) / x - b = np.log(1. + (x / R_s) ) - c = a * b - return c - listx = [] listy = [] -- cgit 1.4.1 From 073e28e0e052941cf0913182944df6cf6363764d Mon Sep 17 00:00:00 2001 From: hanemile Date: Sat, 9 Dec 2017 17:53:54 +0100 Subject: fixed some bugs --- src/coord.py | 5 +++-- src/gen.py | 66 ------------------------------------------------------------ 2 files changed, 3 insertions(+), 68 deletions(-) delete mode 100755 src/gen.py (limited to 'src/gen.py') diff --git a/src/coord.py b/src/coord.py index 4094b25..932cf80 100755 --- a/src/coord.py +++ b/src/coord.py @@ -102,11 +102,12 @@ def main(): time_all = whole_time + time_write_file + time_min = round(time_all / 60, 1) + # print some stats print("") print("{:<30}{:<30}".format("Time (complete)", str(round(time_all, 4)) + " seconds")) - print("{:<30}{:<30}".format("Time (calculate stars)", str(round(whole_time, 4)) + " seconds")) - print("{:<30}{:<30}".format("Time (write to file)", str(round(time_write_file, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (complete)", str(round(time_min, 4)) + " minutes")) print("{:-<40}".format("")) print("{:<20}{:<20}".format("Number of Stars", str(nos))) print("{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked))) diff --git a/src/gen.py b/src/gen.py deleted file mode 100755 index b4b7933..0000000 --- a/src/gen.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python - -from numpy import genfromtxt -import numpy as np -import math -import os -import socket -import time -import matplotlib.pyplot as plt -host = socket.gethostname() - -listx = [] -listy = [] - -def gen_stars(stars): - stars = int(stars) - - # lists - listrho = [] - - # create new file for every calculation - path = "data/" + str(host) + "_" + str(os.getpid()) + ".csv" - print("path: " + str(path)) - - # define the size of the galaxy - length = 1.5e6 - - # define the borders - range_min = -int(length) - range_max = int(length) - - # define the rho range - rand_min = rho(0) - rand_max = rho(math.sqrt(length**2 + length**2)) - - # create random stars - for r in range(0, stars): - x = np.random.uniform(range_min, range_max, size=1) - y = np.random.uniform(range_min, range_max, size=1) - rand_val = np.random.uniform(rand_min, rand_max, size=1) - rho_xy = rho(math.sqrt(x**2 + y**2)) - - # if the random value is smaller than the rho value, generate a star - if rand_val < rho_xy: - - # open a file to write to - with open(path, "a") as data: - - # write the data to the file - data.write(str(x).strip("[]") + "," + str(y).strip("[]") + "\n") - listx.append(x) - listy.append(y) - print(str(x) + ", " + str(y)) - - print("range_min: " + str(range_min)) - print("range_max: " + str(range_max)) - - print("rand_min: " + str(rand_min)) - print("rand_max: " + str(rand_max)) - -# generate n stars -gen_stars(1e6) - -# plot the stars coordinates in 2D space -plt.scatter(listx, listy) -plt.show() -- cgit 1.4.1