Skip to content
Snippets Groups Projects
Commit f0d5a6a7 authored by David Hendriks's avatar David Hendriks
Browse files

added new function to get the default args of binaryc

parent 989586da
No related branches found
No related tags found
No related merge requests found
......@@ -124,3 +124,50 @@ int run_binary(char * argstring,
return 0;
}
int return_arglines(char ** buffer,
size_t * nbytes)
{
/* memory for N binary systems */
struct libbinary_c_stardata_t *stardata;
struct libbinary_c_store_t * store = NULL;
/* make new stardata */
stardata = NULL;
char * empty_str = "";
binary_c_new_system(&stardata,
NULL,
NULL,
&store,
&empty_str,
-1);
/* disable logging */
snprintf(stardata->preferences->log_filename,
STRING_LENGTH-1,
"%s",
"/dev/null");
snprintf(stardata->preferences->api_log_filename_prefix,
STRING_LENGTH-1,
"%s",
"/dev/null");
/* output to strings */
stardata->preferences->internal_buffering = 2;
stardata->preferences->internal_buffering_compression = 0;
stardata->preferences->batchmode = BATCHMODE_LIBRARY;
/* List available arguments */
binary_c_list_args(stardata);
/* get buffer pointer */
binary_c_buffer_info(stardata,buffer,nbytes);
/* set raw_buffer_size = -1 to prevent it being freed */
stardata->tmpstore->raw_buffer_size = -1;
/* free stardata (except the buffer) */
binary_c_free_memory(&stardata,TRUE,TRUE,FALSE);
binary_c_free_store_contents(store);
return 0;
}
\ No newline at end of file
......@@ -9,6 +9,9 @@ int run_binary(char * argstring,
char ** buffer,
size_t * nbytes);
int return_arglines(char ** buffer,
size_t * nbytes);
/* C macros */
#define BINARY_C_APITEST_VERSION 0.1
#define APIprint(...) APIprintf(__VA_ARGS__);
......
......@@ -9,6 +9,9 @@ int run_binary(char * argstring,
char ** buffer,
size_t * nbytes);
int return_arglines(char ** buffer,
size_t * nbytes);
/* C macros */
#define BINARY_C_APITEST_VERSION 0.1
#define APIprint(...) APIprintf(__VA_ARGS__);
......
......@@ -35,7 +35,8 @@ static char new_binary_system_docstring[] =
"Return an object containing a binary, ready for evolution";
static char function_prototype_docstring[] =
"The prototype for a binary_c python function";
static char return_arglines_docstring[] =
"Return the default args for a binary_c system";
static struct libbinary_c_store_t *store = NULL;
#ifdef __DEPRECATED
......@@ -44,6 +45,8 @@ static PyObject* binary_c_create_binary(PyObject *self, PyObject *args);
static PyObject* binary_c_run_binary(PyObject *self, PyObject *args);
static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args);
static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args);
static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args);
/*
......@@ -59,6 +62,7 @@ static PyMethodDef module_methods[] = {
{"run_binary", binary_c_run_binary, METH_VARARGS, run_binary_docstring},
{"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring},
{"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_docstring},
{"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring},
{NULL, NULL, 0, NULL}
};
......@@ -186,3 +190,17 @@ static PyObject* binary_c_run_binary(PyObject *self, PyObject *args)
return ret;
}
}
static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args)
{
/* Binary structures */
char * buffer;
int nbytes;
int out MAYBE_UNUSED = return_arglines(&buffer,
&nbytes);
/* copy the buffer to a python string */
PyObject * ret = Py_BuildValue("s", buffer);
free(buffer);
/* Return an object containing the arg list */
return ret;
}
\ No newline at end of file
......@@ -13,6 +13,9 @@ int run_binary (char * argstring,
char ** outstring,
int * nbytes);
int return_arglines(char ** buffer,
int * nbytes);
/* C macros */
#define BINARY_C_APITEST_VERSION 0.1
#define APIprint(...) APIprintf(__VA_ARGS__);
......
......@@ -120,3 +120,50 @@ int run_binary(char * argstring,
binary_c_free_store_contents(store);
return 0;
}
int return_arglines(char ** buffer,
int * nbytes)
{
/* memory for N binary systems */
struct libbinary_c_stardata_t *stardata;
struct libbinary_c_store_t * store = NULL;
/* make new stardata */
stardata = NULL;
char * empty_str = "";
binary_c_new_system(&stardata,
NULL,
NULL,
&store,
&empty_str,
-1);
/* disable logging */
snprintf(stardata->preferences->log_filename,
STRING_LENGTH-1,
"%s",
"/dev/null");
snprintf(stardata->preferences->api_log_filename_prefix,
STRING_LENGTH-1,
"%s",
"/dev/null");
/* output to strings */
stardata->preferences->internal_buffering = 2;
stardata->preferences->internal_buffering_compression = 0;
stardata->preferences->batchmode = BATCHMODE_LIBRARY;
/* List available arguments */
binary_c_list_args(stardata);
/* get buffer pointer */
binary_c_buffer_info(stardata,buffer,nbytes);
/* set raw_buffer_size = -1 to prevent it being freed */
stardata->tmpstore->raw_buffer_size = -1;
/* free stardata (except the buffer) */
binary_c_free_memory(&stardata,TRUE,TRUE,FALSE);
binary_c_free_store_contents(store);
return 0;
}
\ No newline at end of file
# WARNING DEPRECATED FROM 11-aug-2019
# File containing physics_defaults
physics_defaults = {
......
......@@ -14,13 +14,30 @@ def create_arg_string(arg_dict):
arg_string = arg_string.strip()
return arg_string
def get_defaults():
"""
Function that calls the binaryc get args function and cast it into a dictionary
All the values are strings
"""
default_output = binary_c.return_arglines()
default_dict = {}
for default in default_output.split('\n'):
if not default in ['__ARG_BEGIN', '__ARG_END', '']:
key, value = default.split(' = ')
# Filter out NULLS (not compiled anyway)
if not value=='NULL':
default_dict[key] = value
return default_dict
def run_system(**kwargs):
"""
Wrapper to run a system with settings
"""
# Load args
physics_args = physics_defaults.copy()
# Load default args
physics_args = get_defaults()
# For example
# physics_args['M_1'] = 20
......@@ -81,4 +98,5 @@ def parse_output(output, selected_header):
for key in keys:
final_values_dict[key].append(value_dict[key])
return final_values_dict
\ No newline at end of file
return final_values_dict
No preview for this file type
#!/usr/bin/python3
import binary_c
############################################################
# Test script to run a binary using the binary_c Python
# module.
############################################################
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)
output = binary_c.run_binary(argstring)
print ("\n\nBinary_c output:\n\n")
print (output)
binary_star=binary_c.new_system()
print(dir(binary_c))
# print(binary_star)
run_test_binary()
ding = binary_c.return_arglines()
print(ding)
\ No newline at end of file
......@@ -5,7 +5,10 @@ from collections import defaultdict
import numpy as np
import pandas as pd
# sys.path.append('../')
import binary_c
from binaryc_python_utils.defaults import physics_defaults
from binaryc_python_utils.functions import create_arg_string, parse_output, run_system
......@@ -28,10 +31,13 @@ print("The following keys are present in the results:\n{}".format(result.keys())
# Cast the data into a dataframe.
df = pd.DataFrame.from_dict(result, dtype=np.float64)
sliced_df = df[df.t < 1000] # Cut off late parts of evolution
print(sliced_df["t"])
print(df)
# sliced_df = df[df.t < 1000] # Cut off late parts of evolution
# print(sliced_df["t"])
plt.plot(sliced_df['t'], sliced_df['radius'])
plt.xlabel('Time (Myr)')
plt.ylabel('Radius (Rsol)')
plt.show()
\ No newline at end of file
# plt.plot(sliced_df['t'], sliced_df['radius'])
# plt.xlabel('Time (Myr)')
# plt.ylabel('Radius (Rsol)')
# plt.show()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment