diff --git a/.gitignore b/.gitignore index 4073a0198598387d8a284b82ecbe9ee00d9de6b0..f48659b52618b2a50cc5e8989af6a60ae864c32d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.so +*.pyc binary_c_python_api.o output/* diff --git a/examples/full_evolution_with_plot.py b/examples/full_evolution_with_plot.py index 96a8af73f6ea53cfdcfaa8958f0638930aee1786..6f0dff1e212c7f5bbeda626b39e0699d3a0bc5dd 100644 --- a/examples/full_evolution_with_plot.py +++ b/examples/full_evolution_with_plot.py @@ -4,6 +4,10 @@ import matplotlib.pyplot as plt # Append root dir of this project to include functionality sys.path.append(os.path.dirname(os.getcwd())) import binary_c +<<<<<<< HEAD +======= + +>>>>>>> 19cf329fcbd85a0dff06eaec60030d6bf3ebc0b0 from utils.defaults import physics_defaults from utils.functions import create_arg_string @@ -65,4 +69,4 @@ results = example_with_loading_default_args() plt.plot(results['time'], results['mass']) plt.plot(results['time'], results['mass2']) plt.xscale('log') -plt.show() \ No newline at end of file +plt.show() diff --git a/examples/simple_test.py b/examples/simple_test.py new file mode 100644 index 0000000000000000000000000000000000000000..75191f5bcc984c7a8df7430b2d36721ce987e726 --- /dev/null +++ b/examples/simple_test.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 +import os +import sys + +# Append root dir of this project to include functionality +sys.path.append(os.path.dirname(os.getcwd())) +import binary_c + +from utils.defaults import physics_defaults +from utils.functions import create_arg_string + + +def run_test_binary(): + m1 = 15.0 # Msun + m2 = 14.0 # Msun + separation = 0 # 0 = ignored, use period + orbital_period = 4530.0 # days + eccentricity = 0.0 + metallicity = 0.02 + max_evolution_time = 15000 + buffer = "" + # argstring = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} eccentricity {4:g} metallicity {5:g} max_evolution_time {6:g} ".format(m1,m2,separation,orbital_period,eccentricity,metallicity,max_evolution_time) + + argstring = f"binary_c M_1 {m1} M_2 {m2} separation {separation} orbital_period {orbital_period} eccentricity {eccentricity} metallicity {metallicity} max_evolution_time {max_evolution_time}" + + output = binary_c.run_binary(argstring) + + # print ("Binary_c output:\n\n") + print (output) + +run_test_binary() diff --git a/snippets/arg_test.py b/snippets/arg_test.py new file mode 100644 index 0000000000000000000000000000000000000000..78fced845236be0aeaa9f712617df18595ecea55 --- /dev/null +++ b/snippets/arg_test.py @@ -0,0 +1,63 @@ +import argparse + + +class grid(object): + def __init__(self, name): + self.name = name + self.grid_options = {} + + def load_grid_options(self): + self.grid_options = { + 'test1': 0, + 'test2': 1, + } + + def argparse(self): + """ + This function handles the arg parsing of the grid. + Make sure that every grid_option key/value is included in this, + preferably with an explanation on what that parameter will do + """ + + + parser = argparse.ArgumentParser(description='Arguments for the binary_c python wrapper grid') + + # add arguments here + parser.add_argument('--test1', type=int, help='input for test1') + parser.add_argument('--test2', type=int, help='input for test2') + + # Load the args from the cmdline + args = parser.parse_args() + + # Copy current grid_option set + new_grid_options = self.grid_options.copy() + + # loop over grid_options + for arg in vars(args): + # print ("arg: {arg} value: {value}".format(arg=arg, value=getattr(args, arg))) + + # If an input has been given in the cmdline: override the previous value of grid_options + if getattr(args, arg): + new_grid_options[arg] = getattr(args, arg) + + # Put the new grid options back + self.grid_options = new_grid_options.copy() + +newgrid = grid('test') +newgrid.load_grid_options() +print(newgrid.grid_options) + +newgrid.argparse() +print(newgrid.grid_options) + +# Custom set a single value: +newgrid.grid_options['test2'] = 2 +print(newgrid.grid_options) + +# Custom set multiple values: +newgrid.grid_options.update({ + 'test1':4, + 'test2':-2, + }) +print(newgrid.grid_options) + diff --git a/snippets/d.py b/snippets/d.py new file mode 100644 index 0000000000000000000000000000000000000000..ea3c6bdf3dcec9316ea339a28a430f4ea8716afd --- /dev/null +++ b/snippets/d.py @@ -0,0 +1,25 @@ +import multiprocessing +import time + + +def doubler(number): + return number ** 2 + +def count(number): + nr = 0 + for i in range(number): + nr += i + return number + +if __name__ == '__main__': + numbers = range(2, 100000) + pool = multiprocessing.Pool(processes=6) + # print() + + rs = pool.map_async(pool.map(count, numbers), range(len(numbers))) + pool.close() # No more work + while (True): + if (rs.ready()): break + remaining = rs._number_left + print("Waiting for", remaining, "tasks to complete...") + time.sleep(0.5) \ No newline at end of file diff --git a/snippets/mp2.py b/snippets/mp2.py new file mode 100644 index 0000000000000000000000000000000000000000..a7c9bc0653af00e95ccea144ce2918bf41fdbbf6 --- /dev/null +++ b/snippets/mp2.py @@ -0,0 +1,17 @@ +import time + +def basic_func(x): + if x == 0: + return 'zero' + elif x%2 == 0: + return 'even' + else: + return 'odd' + +starttime = time.time() +for i in range(0,10): + y = i*i + time.sleep(2) + print('{} squared results in a/an {} number'.format(i, basic_func(y))) + +print('That took {} seconds'.format(time.time() - starttime)) \ No newline at end of file diff --git a/snippets/mp3.py b/snippets/mp3.py new file mode 100644 index 0000000000000000000000000000000000000000..3e7b748bb44658a1e793b63e677c1a8e8f0ed4ec --- /dev/null +++ b/snippets/mp3.py @@ -0,0 +1,28 @@ +import time +import multiprocessing + +def basic_func(x): + if x == 0: + return 'zero' + elif x%2 == 0: + return 'even' + else: + return 'odd' + +def multiprocessing_func(x): + y = x*x + time.sleep(2) + print('{} squared results in a/an {} number'.format(x, basic_func(y))) + +if __name__ == '__main__': + starttime = time.time() + processes = [] + for i in range(0,100): + p = multiprocessing.Process(target=multiprocessing_func, args=(i,)) + processes.append(p) + p.start() + + for process in processes: + process.join() + + print('That took {} seconds'.format(time.time() - starttime)) \ No newline at end of file diff --git a/snippets/mp3_pool.py b/snippets/mp3_pool.py new file mode 100644 index 0000000000000000000000000000000000000000..9b40efd977f4af9006499cdd597f02e808d39dd5 --- /dev/null +++ b/snippets/mp3_pool.py @@ -0,0 +1,23 @@ +import time +import multiprocessing +# https://medium.com/@urban_institute/using-multiprocessing-to-make-python-code-faster-23ea5ef996ba +def basic_func(x): + if x == 0: + return 'zero' + elif x%2 == 0: + return 'even' + else: + return 'odd' + +def multiprocessing_func(x): + y = x*x + time.sleep(2) + print('{} squared results in a/an {} number'.format(x, basic_func(y))) + +if __name__ == '__main__': + + starttime = time.time() + pool = multiprocessing.Pool() + pool.map(multiprocessing_func, range(0,10)) + pool.close() + print('That took {} seconds'.format(time.time() - starttime)) \ No newline at end of file diff --git a/snippets/multiprocessing_test.py b/snippets/multiprocessing_test.py new file mode 100644 index 0000000000000000000000000000000000000000..5c56076fb430a1cfc344113f2d29ba33acac3ed5 --- /dev/null +++ b/snippets/multiprocessing_test.py @@ -0,0 +1,30 @@ +from multiprocessing import Process, Queue +import time +import sys + +def reader_proc(queue): + ## Read from the queue; this will be spawned as a separate Process + while True: + msg = queue.get() # Read from the queue and do nothing + if (msg == 'DONE'): + break + +def writer(count, queue): + ## Write to the queue + for ii in range(0, count): + queue.put(ii) # Write 'count' numbers into the queue + queue.put('DONE') + +if __name__=='__main__': + pqueue = Queue() # writer() writes to pqueue from _this_ process + for count in [10**4, 10**5, 10**6]: + ### reader_proc() reads from pqueue as a separate process + reader_p = Process(target=reader_proc, args=((pqueue),)) + reader_p.daemon = True + reader_p.start() # Launch reader_proc() as a separate python process + + _start = time.time() + writer(count, pqueue) # Send a lot of stuff to reader() + reader_p.join() # Wait for the reader to finish + print("Sending {0} numbers to Queue() took {1} seconds".format(count, + (time.time() - _start))) \ No newline at end of file diff --git a/utils/__pycache__/defaults.cpython-35.pyc b/utils/__pycache__/defaults.cpython-35.pyc deleted file mode 100644 index a32ade99bbfa82a830d3b87e46bc48ee0ce68ab5..0000000000000000000000000000000000000000 Binary files a/utils/__pycache__/defaults.cpython-35.pyc and /dev/null differ diff --git a/utils/__pycache__/defaults.cpython-36.pyc b/utils/__pycache__/defaults.cpython-36.pyc deleted file mode 100644 index 66affbd99df305c1259678cf298c21735745c30f..0000000000000000000000000000000000000000 Binary files a/utils/__pycache__/defaults.cpython-36.pyc and /dev/null differ diff --git a/utils/__pycache__/functions.cpython-35.pyc b/utils/__pycache__/functions.cpython-35.pyc deleted file mode 100644 index 62464c4667b48e168d6014c7ba2aa05b6cfb2208..0000000000000000000000000000000000000000 Binary files a/utils/__pycache__/functions.cpython-35.pyc and /dev/null differ diff --git a/utils/__pycache__/functions.cpython-36.pyc b/utils/__pycache__/functions.cpython-36.pyc deleted file mode 100644 index 7aae7529a0a2c7d369cbd2ff93845ac4b5687f1f..0000000000000000000000000000000000000000 Binary files a/utils/__pycache__/functions.cpython-36.pyc and /dev/null differ