Commit 57d5ced2 authored by David Hendriks's avatar David Hendriks
Browse files

removed testing stuff. fixed example

parent ebba9983
Loading
Loading
Loading
Loading

TODO.org

deleted100644 → 0
+0 −168
Original line number Diff line number Diff line
* Todo list for the binary_c-python
** Logging functionality:
*** Idea
Idea is to be able to give a string via python that will be used in the through the libbinary_c.so so that log_every_timestep.c 
The code below is the piece in log_every_timestep that uses it.

    if(stardata->preferences->custom_output_function != NULL)
    {
        Dprint("calling custom output function %p\n",
               stardata->preferences->custom_output_function);
        stardata->preferences->custom_output_function(stardata);
    }

So the function should recieve 'stardata' as input.

We can do that with providing a logging string alltogether, or generate a logging function

In either way, this should be passed to stardata->preferences->custom_output_function as a pointer to that function

*** Provide string for logging
In perl this is done in the following way:
**** code to input
And then to use it via 
    $population->set(
        C_logging_code => '
             PRINTF("MY_STELLAR_DATA %g %g %g %g\n",
                 stardata->model.time,
                 stardata->star[0].mass,
                 stardata->model.probability,
                 stardata->model.dt);
                       '
    );
**** code to handle that input
sub binary_c_log_code
{
    my ($code) = @_;
    return "
#pragma push_macro(\"MAX\")
#pragma push_macro(\"MIN\")
#undef MAX
#undef MIN
#include \"binary_c.h\"

void custom_output_function(SV * x);
SV * custom_output_function_pointer(void);

SV * custom_output_function_pointer()
{
    /*
     * use PTR2UV to convert the function pointer 
     * &custom_output_function to an unsigned int,
     * which is then converted to a Perl SV
     */
    return (SV*)newSVuv(PTR2UV(&custom_output_function));
}

void custom_output_function(SV * x)
{
    struct stardata_t * stardata = (struct stardata_t *)x;
    $code;
}
#undef MAX 
#undef MIN
#pragma pop_macro(\"MIN\")
#pragma pop_macro(\"MAX\")
";
}
Or use it via:
*** auto logging
We should also try to be able to have the code autogenerate some logging function via the following:
**** input in perl
$population->set(    C_auto_logging => {
        'MY_STELLAR_DATA' =>
            [
             'model.time',
             'star[0].mass',
             'model.probability',
             'model.dt'
            ]
    });
**** code to handle that input
Which is handled in perl via (see binary_grid2.pm
sub autogen_C_logging_code
{
    # given a hash of arrays of variable names, where the hash
    # key is the header, autogenerate PRINTF statements
    my ($self) = @_;
    my $code = undef;
    if(defined $self->{_grid_options}->{C_auto_logging} &&
       ref $self->{_grid_options}->{C_auto_logging} eq 'HASH'
        )
    {
        $code = '';

        foreach my $header (keys %{$self->{_grid_options}->{C_auto_logging}})
        {
            if(ref $self->{_grid_options}->{C_auto_logging}->{$header} eq 'ARRAY')
            {
                $code .= 'PRINTF("'.$header.' ';
                foreach my $x (@{$self->{_grid_options}->{C_auto_logging}->{$header}})
                {
                    $code .= '%g ';
                }
                $code .= '\n"';

                foreach my $x (@{$self->{_grid_options}->{C_auto_logging}->{$header}})
                {
                    $code .= ',((double)stardata->'.$x.')';
                }
                $code .= ');'
            }
        }
    }
    print "MADE AUTO CODE \n\n************************************************************\n\n$code\n\n************************************************************\n";

    return $code;
}
*** DONE Make function in python that puts code into c function
    CLOSED: [2019-10-31 Thu 11:13]
*** DONE Make function in python that generates c-function from a list of arguments
    CLOSED: [2019-10-29 Tue 23:52]
*** DONE Resolve current issue malloc
    CLOSED: [2019-11-08 Fri 11:12]
➜  binary_c-python git:(master) ✗ python python_API_test.py 
Traceback (most recent call last):
  File "python_API_test.py", line 3, in <module>
    import binary_c
ImportError: /home/david/projects/binary_c_root/binary_c-python/libbinary_c_api.so: undefined symbol: MALLOC

I get this error when I am using the master version of binary_c with either branches of the python wrapper..

That went very deep haha. alot of memory allocation stuff

*** DONE Make sure this works with the last major release of binaryc
    CLOSED: [2019-11-08 Fri 15:00]
*** DONE Finish testing a simpler case (see other repo)
    CLOSED: [2019-11-08 Fri 09:37]
*** DONE Make master master work
    CLOSED: [2019-11-08 Fri 15:00]
*** DONE Sync master with david_branch
    CLOSED: [2019-11-08 Fri 15:00]
*** DONE make tag of old master branch for future reference
    CLOSED: [2019-11-08 Fri 15:00]
*** DONE Implement the autogeneration of the library
    CLOSED: [2019-11-08 Fri 15:48]
*** DONE Load all the things with the c-types
    CLOSED: [2019-11-08 Fri 18:49]
*** DONE Implement new function for run_binary_with_custom_logging
    CLOSED: [2019-11-08 Fri 21:19]
*** DONE Make new c function run_binary_with_custom_logging
    CLOSED: [2019-11-08 Fri 21:19]
*** TODO Put in some new tests in the python test api
*** DONE Make sure the sharedlibs get written to the correct directory
    CLOSED: [2019-11-10 Sun 00:21]
** General:
*** DONE Get a more reliable way of loading the default values (running a ./tbse echo or something?)
    CLOSED: [2019-10-29 Tue 17:44]
*** DONE make routine that reads out all the lines, splits them into pieces and reads out the correct key
    CLOSED: [2019-10-29 Tue 17:43]
*** TODO Put header and other source files in a dedicated directory
*** TODO Use sphinx or read the docs for auto generation of documentation
*** TODO Have the compiled files put into a build directory
*** TODO add pythonpath thing to readme
*** TODO make script that will set up binaryc automatically so that this can become an out of the box thing
*** TODO Test the importing of this code from different places
** Population ideas
*** TODO Queuing system and some multiprocessing to run many systems
*** TODO Consider rewriting the work that perl does
+6 −5
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ def run_example_binary():
    output = binary_c.run_binary(argstring)
    print (output)

# run_example_binary()
run_example_binary()

def run_example_binary_with_run_system():
    """
@@ -71,7 +71,7 @@ def run_example_binary_with_run_system():

    # Some routine to plot.

# run_example_binary_with_run_system()
run_example_binary_with_run_system()


def run_example_binary_with_custom_logging():
@@ -110,7 +110,7 @@ def run_example_binary_with_custom_logging():
    # Do whatever you like with the dataframe.
    print(df)

# run_example_binary_with_custom_logging()
run_example_binary_with_custom_logging()

def run_example_binary_with_writing_logfile():
    """
@@ -119,9 +119,10 @@ def run_example_binary_with_writing_logfile():

    import pandas as pd
    import numpy as np
    import tempfile

    # Run system. all arguments can be given as optional arguments.
    output = run_system(M_1=10, M_2=20, separation=0, orbital_period=100000000000, log_filename=os.getcwd()+'/test_log.txt')
    output = run_system(M_1=10, M_2=20, separation=0, orbital_period=100000000000, log_filename=tempfile.gettempdir()+'/test_log.txt')

    # Catch results that start with a given header. (Mind that binary_c has to be configured to print them if your not using a custom logging function)
    result_example_header = parse_output(output, 'example_header')
@@ -140,4 +141,4 @@ def run_example_binary_with_writing_logfile():

    # Some routine to plot.

# run_example_binary_with_writing_logfile()
 No newline at end of file
run_example_binary_with_writing_logfile()
 No newline at end of file

testing_examples/__init__.py

deleted100644 → 0
+0 −0

Empty file deleted.

+0 −165

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −69
Original line number Diff line number Diff line
import os, sys
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

from utils.defaults import physics_defaults
from utils.functions import create_arg_string

def example_with_loading_default_args():
    """
    Example function loading the default physics args for a binary_c system. Got
    it from the binary_grid2 perl module

    This function works if binary_c is set to log something every timestep so that we can plot the evolution of a system
    """

    # Load args
    physics_args = physics_defaults.copy()

    # Manually set M_1, M_2, orbital_period and separation values:
    physics_args['M_1'] = 20
    physics_args['M_2'] = 15
    physics_args['separation'] = 0 # 0 = ignored, use period
    physics_args['orbital_period'] = 4530.0

    arg_string = create_arg_string(physics_args)
    arg_string = f'binary_c {arg_string}' 

    buffer = ""

    output = binary_c.run_binary(arg_string)

    # Make some 
    results = {}
    time_arr = []
    mass_arr = []
    mass_2_arr = []

    # split output on newlines
    for line in output.split('\n'):
        # Skip any blank lines
        if not line=='':
            split_line = line.split()
            header = split_line[0]
            value_array = split_line[1:]

            # Use parse data here:
            if header=='TESTLOG':
                # Add values to lists
                time_arr.append(float(value_array[0]))
                mass_arr.append(float(value_array[1]))
                mass_2_arr.append(float(value_array[4]))

    # Save in results dir
    results['time'] = time_arr
    results['mass'] = mass_arr
    results['mass2'] = mass_2_arr

    return results

results = example_with_loading_default_args()

# Plot some stuff
plt.plot(results['time'], results['mass'])
plt.plot(results['time'], results['mass2'])
plt.xscale('log')
plt.show()
Loading