From 8d42bbb4be0fa5f12f9a025448d8c65c3097cc9f Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Thu, 31 Oct 2019 23:30:01 +0000
Subject: [PATCH] busy with a second repo to test stuff

---
 TODO.org                                  |  37 +++--
 binaryc_python_utils/example_perl.pm      | 166 +++++++++++-----------
 binaryc_python_utils/logging_functions.py |  11 --
 3 files changed, 105 insertions(+), 109 deletions(-)

diff --git a/TODO.org b/TODO.org
index 5c13dd893..0bb06c8b8 100644
--- a/TODO.org
+++ b/TODO.org
@@ -17,10 +17,21 @@ We can do that with providing a logging string alltogether, or generate a loggin
 
 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) = @_;
@@ -56,22 +67,13 @@ void custom_output_function(SV * x)
 ";
 }
 
-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);
-                       '
-    );
+
 
 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' =>
             [
@@ -82,7 +84,7 @@ $population->set(    C_auto_logging => {
             ]
     });
 
-
+**** code to handle that input
 Which is handled in perl via (see binary_grid2.pm
 sub autogen_C_logging_code
 {
@@ -122,8 +124,8 @@ sub autogen_C_logging_code
 
 
 
-
-*** TODO Make function in python that puts code into c function
+*** 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]
 *** TODO Resolve current issue malloc
@@ -135,6 +137,11 @@ ImportError: /home/david/projects/binary_c_root/binary_c-python/libbinary_c_api.
 
 I get this error when I am using the master version of binary_c with either branches of the python wrapper
 
+*** TODO Make sure this works with the last major release of binaryc
+
+*** TODO Finish testing a simpler case (see other repo)
+
+
 ** 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]
diff --git a/binaryc_python_utils/example_perl.pm b/binaryc_python_utils/example_perl.pm
index da2aa8c7f..58877d2f6 100644
--- a/binaryc_python_utils/example_perl.pm
+++ b/binaryc_python_utils/example_perl.pm
@@ -1,95 +1,95 @@
 # Autologging
 # Perl code for autogeneration
-# 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 = '';
+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 $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";
+                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;
-# }
+    return $code;
+}
 
-# Which is used in flexi-grid via this:
-# $population->set(    C_auto_logging => {
-#         'MY_STELLAR_DATA' =>
-#             [
-#              'model.time',
-#              'star[0].mass',
-#              'model.probability',
-#              'model.dt'
-#             ]
-#     });
+Which is used in flexi-grid via this:
+$population->set(    C_auto_logging => {
+        'MY_STELLAR_DATA' =>
+            [
+             'model.time',
+             'star[0].mass',
+             'model.probability',
+             'model.dt'
+            ]
+    });
 
 # binary_c_log_code
-# sub binary_c_log_code
-# {
-#     my ($code) = @_;
-#     return "
-# #pragma push_macro(\"MAX\")
-# #pragma push_macro(\"MIN\")
-# #undef MAX
-# #undef MIN
-# #include \"binary_c.h\"
+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);
+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));
-# }
+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\")
-# ";
-# }
+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\")
+";
+}
 
-# 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);
-#                        '
-#     );
+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);
+                       '
+    );
diff --git a/binaryc_python_utils/logging_functions.py b/binaryc_python_utils/logging_functions.py
index d77eceb8f..5e5353e29 100644
--- a/binaryc_python_utils/logging_functions.py
+++ b/binaryc_python_utils/logging_functions.py
@@ -1,16 +1,5 @@
 import textwrap
 # Functions for the automatic logging of stuff
-# https://stackoverflow.com/questions/41954269/create-c-function-pointers-structure-in-python
-# https://stackabuse.com/enhancing-python-with-custom-c-extensions/ Read
-# https://stackoverflow.com/questions/49941617/runtime-generation-and-compilation-of-cython-functions
-# https://realpython.com/cpython-source-code-guide/
-# https://docs.python.org/3.6/c-api/index.html
-# https://stackoverflow.com/questions/6626167/build-a-pyobject-from-a-c-function
-# https://docs.python.org/3.6/extending/newtypes_tutorial.html?highlight=pointer
-# https://realpython.com/cpython-source-code-guide/
-# https://diseraluca.github.io/blog/2019/03/21/wetting-feet-with-python-c-api
-# https://docs.python.org/3/c-api/function.html
-
 
 # See example_perl.pm autologging
 def autogen_C_logging_code(logging_dict):
-- 
GitLab