<jupyter_start><jupyter_text>Create a system and save it to a C file<jupyter_code>sys_eq = pyequion.create_equilibrium(['NaHCO3', 'CaCl2'])
pyequion.save_res_to_c_code(sys_eq, 'dummy', 'calc_cnv_res_equilibrium_NaHCO3_CaCl2', 
    # fixed_temperature=25.0
)<jupyter_output><empty_output><jupyter_text>Check the file system for the created header and source files Generating Cython module for calling the C codeReference: https://www.sympy.org/scipy-2017-codegen-tutorial/1. Firstly a magic cell is used to create the build configuration file1. Nextly, the cython file that will bridge the python interpreter with the c-function is defined and make it available to python interpreter<jupyter_code>%%writefile calc_cnv_res_equilibrium_NaHCO3_CaCl2.pyxbld
import numpy

#            module name specified by `%%cython_pyximport` magic
#            |        just `modname + ".pyx"`
#            |        |
def make_ext(modname, pyxfilename):
    from setuptools.extension import Extension
    return Extension(modname,
                     sources=[pyxfilename, 'calc_cnv_res_equilibrium_NaHCO3_CaCl2.c'],
                     include_dirs=['.', numpy.get_include()])
%%cython_pyximport calc_cnv_res_equilibrium_NaHCO3_CaCl2
import numpy as np
cimport numpy as cnp # cimport gives us access to NumPy's C API

# here we just replicate the function signature from the header
cdef extern from "calc_cnv_res_equilibrium_NaHCO3_CaCl2.h":
    void calc_cnv_res_equilibrium_NaHCO3_CaCl2(double T, double *concs, double *x, double *res)

# here is the "wrapper" signature that conforms to the odeint interface
def cy_calc_cnv_res_equilibrium_NaHCO3_CaCl2(double T, cnp.ndarray[cnp.double_t, ndim=1] concs, cnp.ndarray[cnp.double_t, ndim=1] x):
    # preallocate our output array
    cdef cnp.ndarray[cnp.double_t, ndim=1] res = np.empty(x.size, dtype=np.double)
    # now call the C function
    calc_cnv_res_equilibrium_NaHCO3_CaCl2(<double> T, <double *> concs.data, <double *> x.data, <double *> res.data)
    # return the result
    return res
def wrapper_py_res_func(x, T, concs):
    return cy_calc_cnv_res_equilibrium_NaHCO3_CaCl2(T, concs, x)
sol = pyequion.solve_solution({'NaHCO3': 10, 'CaCl2': 5})
cy_calc_cnv_res_equilibrium_NaHCO3_CaCl2(25.0+273.15, np.array([10.0, 5.0]), sol.x)<jupyter_output><empty_output><jupyter_text>Generating C Code for the Jacobian<jupyter_code>pyequion.save_jacobian_of_res_to_c_code(sys_eq, 'dummy', 'calc_cnv_jac_equilibrium_NaHCO3_CaCl2', 
    # fixed_temperature=25.0
)

%%writefile calc_cnv_jac_equilibrium_NaHCO3_CaCl2.pyxbld
import numpy

#            module name specified by `%%cython_pyximport` magic
#            |        just `modname + ".pyx"`
#            |        |
def make_ext(modname, pyxfilename):
    from setuptools.extension import Extension
    return Extension(modname,
                     sources=[pyxfilename, 'calc_cnv_jac_equilibrium_NaHCO3_CaCl2.c'],
                     include_dirs=['.', numpy.get_include()])
%%cython_pyximport calc_cnv_jac_equilibrium_NaHCO3_CaCl2
import numpy as np
cimport numpy as cnp # cimport gives us access to NumPy's C API

# here we just replicate the function signature from the header
cdef extern from "calc_cnv_jac_equilibrium_NaHCO3_CaCl2.h":
    void calc_cnv_jac_equilibrium_NaHCO3_CaCl2(double T, double *x, double *res)

# here is the "wrapper" signature that conforms to the odeint interface
def cy_calc_cnv_jac_equilibrium_NaHCO3_CaCl2(double T, cnp.ndarray[cnp.double_t, ndim=1] x):
    # preallocate our output array
    cdef cnp.ndarray[cnp.double_t, ndim=1] J = np.empty((x.size*x.size), dtype=np.double)
    # now call the C function
    calc_cnv_jac_equilibrium_NaHCO3_CaCl2(<double> T, <double *> x.data, <double *> J.data)
    # return the result

    mat_J = J.reshape((x.size, -1))
    return mat_J
def wrapper_py_jac_func(x, T, concs):
    return cy_calc_cnv_jac_equilibrium_NaHCO3_CaCl2(T, x)
sol = pyequion.solve_solution({'NaHCO3': 10, 'CaCl2': 5})
J = cy_calc_cnv_jac_equilibrium_NaHCO3_CaCl2(25, np.full(15, -3.0))
J.shape
J
## Solving the system


np.full(15, -3)
concs = np.array([10e-3,5e-3])
root_sol = scipy.optimize.root(wrapper_py_res_func, sol.x, args=(25+273.15, concs), jac=wrapper_py_jac_func)
root_sol
wrapper_py_jac_func(np.full(15,-3.0), 25+273.15, concs)
# wrapper_py_res_func(np.full(15,-3.0), 25+273.15, concs)
wrapper_py_res_func(sol.x, 25+273.15, concs)
sol.x
cy_calc_cnv_res_equilibrium_NaHCO3_CaCl2(25+273.15, concs, sol.x)
concs
10e-3<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10131081981781827, 'fraction_numerical': 0.03221506331926239, 'mean_word_length': 4.735031847133758, 'pattern_counts': {'":': 2, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '650239', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1710, 'n_words': 367, 'path': 'samples/code_generation/CodeGeneration.ipynb', 'repo_name': 'caiofcm/pyequion', 'size': '4501'}