From 5e17688870d04f4a0ef259431f73287ea21b793d Mon Sep 17 00:00:00 2001 From: hanemile Date: Sat, 9 Dec 2017 14:19:59 +0100 Subject: minor ajustments --- src/README.md | 25 ++++++++++ src/coord.py | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lookup.py | 86 ++++++++++++++++++++++++++++++++++ src/view.py | 48 +++++++++++++++++++ 4 files changed, 305 insertions(+) create mode 100644 src/README.md create mode 100755 src/coord.py create mode 100755 src/lookup.py create mode 100644 src/view.py diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..bf572b2 --- /dev/null +++ b/src/README.md @@ -0,0 +1,25 @@ +# README + +### workflow: + +--- + +#### Generate the lookuptable + + + $ ./lookup.py + +- the __number of values__ can be given using the scientific notaion e.g. 1e7. +- the __filename__ should not overlap with any existing filename + +--- + +#### Generate the coordinates + + + $ ./coord.py + + +#### Display the stars using Blender + + $ blender --python view.py diff --git a/src/coord.py b/src/coord.py new file mode 100755 index 0000000..f801aee --- /dev/null +++ b/src/coord.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python + +# import libraries +import time +import numpy as np +import sys + +# define the number of stars that should be generated +# (this is not the final value -> the final value is about 1000 times smaller) +nos = int(sys.argv[1]) + +# define some arrays for storing the values +arr_stars = np.zeros((int(nos), 3)) +arr_r = np.zeros((int(nos), 1)) +arr_saved_stars = np.zeros((int(nos), 3)) + +# define various paths +path = "data/rho6.csv" +save_path = "stars/star18.csv" +# star13 -> 586 stars + +# 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 = -1e7 +range_max = 1e7 + +# main function +def main(): + + time_start_gen_array = time.time() + + # generate n=nos stars + for i in range(0, nos): + + # generate the random values + arr_stars[i][0] = np.random.uniform(range_min, range_max, size=1) + arr_stars[i][1] = np.random.uniform(range_min, range_max, size=1) + arr_stars[i][2] = np.random.uniform(range_min, range_max, size=1) + + # calculate the distance of the star to the center of the galaxy + arr_r[i][0] = np.sqrt(pow(arr_stars[i][0], 2) + pow(arr_stars[i][1], 2) + pow(arr_stars[i][2], 2)) + + # print the randomly generated arrays + print(arr_stars) + print(arr_r) + + time_end_gen_array = time.time() + time_all_gen_array = time_end_gen_array - time_start_gen_array + # 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... + for i in range(0, nos): + # print(i) + + # random value + a = np.random.uniform(rand_min, rand_max, size=1) + + # corresponding rho value + b = float(rho_file[int(round(arr_r[i][0], 0))].split(", ")[1].strip("\n")) + + # if the random value is smaller than the corresponding rho value + if( a < b): + # add the coordinate to arr_saved_stars + arr_saved_stars[stars_kept][0] = arr_stars[i][0] + arr_saved_stars[stars_kept][1] = arr_stars[i][1] + arr_saved_stars[stars_kept][2] = arr_stars[i][2] + + # increment the star_kept counter + stars_kept += 1 + + else: + # increment the star_kicked counter + stars_kicked += 1 + + if(i % (nos/(nos/10)) == 0): + if(i != 0): + a = str(round(i / nos * 100, 1)) + "%" + time_temp = time.time() + time_past = round(time_temp - start, 2) + print("{:<10}{:<20}".format(a, time_past)) + + print("") + end = time.time() + whole_time = end - start + print(">> Finished generating stars in " + str(whole_time) + " seconds\n") + + start_delete_rows = time.time() + + # delete all unused rows + print(">> Deleting unused rows in the arr_saved_stars array") + # for i in range(nos - stars_kicked, nos): + # np.delete(arr_saved_stars, (i), axis=0) + end_delete_rows = time.time() + time_delete_rows = end_delete_rows - start_delete_rows + print(">> Finished deleting stars in " + str(round(time_delete_rows, 4)) + " seconds \n") + + start_write_file = time.time() + + # write the star coordinates to a file + print(">> Writing the star-data to " + save_path) + with open(save_path, "a") as stars_data: + for i in range(0, nos): + x = arr_saved_stars[i][0] + y = arr_saved_stars[i][1] + z = arr_saved_stars[i][2] + + stars_data.write(str(x) + ", " + str(y) + ", " + str(z) + "\n") + + end_write_file = time.time() + time_write_file = end_write_file - start_write_file + print(">> Finished writing star-data to " + save_path + " in " + str(round(time_write_file, 4)) + " seconds\n") + + + stars_percent = stars_kept / nos * 100 + + time_all = whole_time + time_write_file + time_delete_rows + + # print some stats + print("") + print("{:<30}{:<30}".format("Time (complete)", str(round(time_all, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (gen arrays)", str(round(time_all_gen_array, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (calculate stars)", str(round(whole_time, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (delete rows)", str(round(time_delete_rows, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (write to file)", str(round(time_write_file, 4)) + " seconds")) + print("{:-<40}".format("")) + print("{:<20}{:<20}".format("Number of Stars", str(nos))) + print("{:<20}{:<20}".format("Stars Kept:", str(stars_kept))) + print("{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked))) + print("{:<20}{:<20}".format("Stars Percent", str(round(stars_percent, 4)) + "%")) + +if __name__ == "__main__": + main() diff --git a/src/lookup.py b/src/lookup.py new file mode 100755 index 0000000..52e1966 --- /dev/null +++ b/src/lookup.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python + +# Import some libraries +import math +import numpy as np +import matplotlib.pyplot as plt +import time +import os +import sys + +# Defining some variables +sigma = 200 +f_0 = 0.1 +R_s = 1e4 + +# Defining some 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 ) ) + return a * b + +# phi function +def phi(x): + if x == 0: + return -4 * pi * f_0 * G * R_s**2 + else: + a = - ( 4 * pi * G * f_0 * R_s ** 3 ) / x + b = np.log(1. + (x / R_s) ) + return a * b + +# Defining a list to store the rho-values for plotting +list_rho = [] + +# Define the path to where the data should be stored +path = 'data/' + str(sys.argv[2]) + '.csv' + +# get the start time +start = time.time() + +# define the number of stars using system arguments +stars = int(float(sys.argv[1])) + +# open the file where the information should be written to +with open(path, "a") as data: + + # for every star + for i in range(0, stars): + + # calculate the rho value + rho_i = rho(i/10) + + # append the rho value to list_rho for plotting + # list_rho.append(rho_i) + + # print the distance to the center of the universe and the rho value to + # the user + print(str(i) + ", " + str(rho_i)) + + # write the data into the file + data.write(str(i) + ", " + str(rho_i) + "\n") + +# get the end time +end = time.time() + +# calculate the runtime +runtime = end - start + +# print some information to the user +print("\n Runtime: ", end="") +print(str(runtime) + " seconds") + +print(" Stars: " + str(stars)) + +print(" Rho-values per second: ", end="") +print(str(stars / runtime)) + +# plt.plot(list_rho) +# plt.xscale('log') +# plt.yscale('log') +# plt.grid() +# plt.show() diff --git a/src/view.py b/src/view.py new file mode 100644 index 0000000..5e3808d --- /dev/null +++ b/src/view.py @@ -0,0 +1,48 @@ +import bpy +from numpy import genfromtxt +import os +import sys + +directory = "stars/" +# # print(directory) +# +files = ["final_star15.csv"] + +# print("### \n\n") +# +# for data_file in os.listdir(directory): +# files.append(data_file) +# print(data_file) +# +# print("### \n\n") + +for data in files: + path = str(directory) + str(data) + print(path) + + verts = genfromtxt(path, delimiter=', ', skip_header=0) + + print(verts) + +# verts = [(-1.0, 1.0, 0.0), (-1.0, -1.0, 0.0), (1.0, -1.0, 0.0), (1.0, 1.0, 0.0)] + + # create mesh and object + mesh = bpy.data.meshes.new(data) + object = bpy.data.objects.new(data,mesh) + + # set mesh location + object.location = bpy.context.scene.cursor_location + bpy.context.scene.objects.link(object) + + # create mesh from python data + mesh.from_pydata(verts,[],[]) + mesh.update(calc_edges=True) + + bpy.ops.object.select_all(action='SELECT') + +for area in bpy.context.screen.areas: + if area.type == 'VIEW_3D': + for region in area.regions: + if region.type == 'WINDOW': + override = {'area': area, 'region': region, 'edit_object': bpy.context.edit_object} +bpy.ops.view3d.view_all(override) -- cgit 1.4.1