import o2sclpy

"""
Explanation: Skyrme example
End of explanation
"""


link=o2sclpy.linker()
link.link_o2scl_o2graph(True,True)

"""
Explanation: Link the O$_2$scl library
End of explanation
"""


fc=o2sclpy.find_constants(link)
hc=fc.find_unique('hbarc','MeV*fm')
print('hbarc = %7.6e' % (hc))

"""
Explanation: Get the value of $\hbar c$ from an O$_2$scl find_constants object
End of explanation
"""


cu=link.o2scl_settings.get_convert_units()

"""
Explanation: Get a copy (a pointer to) the O$_2$scl unit conversion object
End of explanation
"""


neut=o2sclpy.fermion(link)
neut.g=2.0
neut.m=cu.convert('g','1/fm',fc.find_unique('massneutron','g'))

prot=o2sclpy.fermion(link)
prot.g=2.0
prot.m=cu.convert('g','1/fm',fc.find_unique('massproton','g'))

"""
Explanation: Create neutron and proton objects and set their spin degeneracy and
masses. The masses are expected to be in units of inverse
femtometers.
End of explanation
"""


sk=o2sclpy.eos_had_skyrme(link)
o2sclpy.skyrme_load(link,sk,'NRAPR',False,0)

"""
Explanation: Create the Skyrme EOS object and load the NRAPR parameterization
End of explanation
"""


sk.saturation()
print('NRAPR: n0=%7.6e 1/fm^3, E/A=%7.6e MeV' % (sk.n0,sk.eoa*hc))
print('')

"""
Explanation: Compute nuclear saturation and output the saturation density
and binding energy
End of explanation
"""


nc=o2sclpy.nstar_cold(link)

"""
Explanation: Create the nstar_cold object for automatically computing the
beta-equilibrium EOS and solving the TOV equations.
End of explanation
"""


nc.set_eos(sk)

"""
Explanation: Let the nstar_cold object know we want to use the NRAPR EOS
End of explanation
"""


ret1=nc.calc_eos(0.01)

"""
Explanation: Compute the EOS
End of explanation
"""


eos_table=nc.get_eos_results()
print('EOS table:')
for i in range(0,eos_table.get_ncolumns()):
    col=eos_table.get_column_name(i)
    unit=eos_table.get_unit(col)
    print('Column',i,str(col,'UTF-8'),str(unit,'UTF-8'))
print('')

# from https://stackoverflow.com/questions/24277488/in-python-how-to-capture-
# the-stdout-from-a-c-shared-library-to-a-variable
import ctypes
import os
import sys
import threading

# Create pipe and dup2() the write end of it on top of stdout, saving a copy
# of the old stdout
#stdout_fileno = sys.stdout.fileno()
stdout_fileno=1

stdout_save = os.dup(stdout_fileno)
stdout_pipe = os.pipe()
os.dup2(stdout_pipe[1], stdout_fileno)
os.close(stdout_pipe[1])

captured_stdout = ''
def drain_pipe():
    global captured_stdout
    while True:
        data = os.read(stdout_pipe[0], 1024)
        if not data:
            break
        captured_stdout += str(data,'UTF-8')

t = threading.Thread(target=drain_pipe)
t.start()

ret2=nc.calc_nstar()

# Close the write end of the pipe to unblock the reader thread and trigger it
# to exit
os.close(stdout_fileno)
t.join()

# Clean up the pipe and restore the original stdout
os.close(stdout_pipe[0])
os.dup2(stdout_save, stdout_fileno)
os.close(stdout_save)
print('y')
print(captured_stdout)

"""
Explanation: Summarize the columns in the EOS table
End of explanation
"""


# ret2=nc.calc_nstar()

"""
Explanation: Compute the M-R curve using the TOV equations
End of explanation
"""


tov_table=nc.get_tov_results()
print('')

"""
Explanation: Get the table for the TOV results
End of explanation
"""


print('TOV table:')
for i in range(0,tov_table.get_ncolumns()):
    col=tov_table.get_column_name(i)
    unit=tov_table.get_unit(col)
    print('Column',i,str(col,'UTF-8'),str(unit,'UTF-8'))
print('')

"""
Explanation: Summarize the columns in the TOV table
End of explanation
"""


