Making Use of Dengo templates

In this chapter, we will walk through how to build the C library with the template provided in dengo.templates. This is particularly useful when one wants to extend the solver to solve the chemistry alongside hydrodynamical simulations like enzo.

In this example, we will stick with the 2-species primordial chemistry model we used in the last two chapters.

Generate templates with ChemicalNetwork.write_solver

With the ChemicalNetwork object, Dengo can write the C solver. The corresponding auxillary library paths are needed to be set as the environomental variables, in order to compile our python modules.

  • HDF5_DIR (HDF5 installation path)

  • CVODE_PATH (CVode installation path)

  • SUITESPARSE_PATH (SuiteSparse library which is optional unless we use KLU option)

  • DENGO_INSTALL_PATH (Installation Path of Dengo)

solver_template are the existing templates under dengo.templates. Currently there are two major ones be_chem_solve

import dengo
from dengo.chemical_network import \
 ChemicalNetwork, \
 reaction_registry, \
 cooling_registry, species_registry
import dengo.primordial_rates
import dengo.primordial_cooling

dengo.primordial_rates.setup_primordial()

simpleNetwork = ChemicalNetwork()
simpleNetwork.add_reaction("k01")
simpleNetwork.add_reaction("k02")
simpleNetwork.add_cooling("reHII")
simpleNetwork.init_temperature((1e0, 1e8))
Adding reaction: k01 : 1*H_1 + 1*de => 1*H_2 + 2*de
Adding reaction: k02 : 1*H_2 + 1*de => 1*H_1
solver_name = "simpleNetwork"
simpleNetwork.write_solver(
    solver_name, 
    output_dir = ".", 
    solver_template = "cv_omp/sundials_CVDls", 
    ode_solver_source = "initialize_cvode_solver.C"
)
!ls simpleNetwork_*
simpleNetwork_solver.C
simpleNetwork_solver.h
simpleNetwork_solver_main.C
simpleNetwork_solver_main.py
simpleNetwork_solver_run.cpython-38-x86_64-linux-gnu.so
simpleNetwork_solver_run.cpython-38-x86_64-linux-gnu.so.reload1
simpleNetwork_solver_run.cpython-38-x86_64-linux-gnu.so.reload2
simpleNetwork_solver_run.pxd
simpleNetwork_solver_run.pyx
simpleNetwork_solver_run.pyxbld
simpleNetwork_solver_run.pyxdep
simpleNetwork_tables.h5

ChemicalNetwork.write_solver

Look in your directory there are 9 extra files, from C to python codes!

Main components that drives Dengo C-solver

  • _solver.h

  • _solver.C (major modules in Dengo)

  • _solver_main.h

  • _solver_main.C (example script to use the C library)

  • initialize_cvode_solver.C (wrapper function for the CVode library)

  • Makefile (to compile the dengo library libdengo.a)

Helper function to compile Dengo C files for Python wrapper

  • _solver_run.pyxbld

  • _solver_run.pyxdep

  • _solver_run.pxd

  • _solver_run.pyx (major Python wrapper)