Skip to main content

ADM1 anaerobic digestion model accelerated with Numba and numbalsoda.

Project description

adm1-numba

adm1-numba is an Anaerobic Digestion Model No. 1 (ADM1) reactor simulation package using Numba and numbalsoda.

ADM1 is a dynamic model for anaerobic digestion introduced by Batstone et al. in the original IWA Scientific and Technical Report No. 13: Anaerobic Digestion Model No. 1 (ADM1). It describes biochemical conversion, acid-base chemistry, inhibition, and gas-liquid transfer in anaerobic digesters. adm1-numba implementation closely follows the work Aspects on ADM1 Implementation within the BSM2 Framework by Rosen, C. and Jeppsson, U.

Existing Python ADM1 implementations such as pyADM1 are convenient but can be slow and may not use the latest implementation choices. ADM1F provides a fast implementation, but it requires a C++/PETSc-based installation, which can add deployment effort. adm1-numba keeps the interface simple for Python users while accelerating the model with Numba and numbalsoda. On default steady-state run results, it shows minimal differences compared with ADM1F while remaining easy to install and integrate into Python workflows.

This package is designed for repeated model calls in control, optimization, and reinforcement learning (RL) applications. See our publication using this implementation.

Installation

Install with:

pip install adm1-numba

Quickstart

from adm1_numba import DEFAULT_F0, DEFAULT_U0, run_stage

sol, qch4, qco2, ph = run_stage(
    DEFAULT_U0.copy(),
    deltaT=10.,
    tint=1e-2,
    f0=DEFAULT_F0.copy(),
)

print(sol.shape)
print(qco2[-1])

Use .copy() when passing default arrays if you plan to mutate input values.

Direct ODE Integration

run_stage is a wrapper around numbalsoda.lsoda. You can also integrate the ADM1 ODE model directly with the exported function pointer:

import numpy as np
from numbalsoda import lsoda

from adm1_numba import DEFAULT_F0, DEFAULT_U0, calculate_observables, funcptr

t_eval = np.arange(0.0, 1.0, 1e-3)
sol, success = lsoda(
    funcptr,
    DEFAULT_U0.copy(),
    t_eval,
    DEFAULT_F0.copy(),
    rtol=1e-7,
    atol=1e-9,
)

if not success:
    raise RuntimeError("ADM1 integration failed")

qch4, qco2, ph = calculate_observables(sol)

print(sol.shape)
print(qch4[-1], qco2[-1], ph[-1])

Multi-stage Control Example

The example below runs multiple stages. A dummy agent changes the input flow rate Q with random exploration noise, and each stage starts from the final state of the previous stage.

import numpy as np

from adm1_numba import DEFAULT_F0, DEFAULT_U0, run_stage

class DummyAgent:
    def __init__(self, baseline_q: float = 170, noise_scale: float = 10.0):
        self.baseline_q = baseline_q
        self.noise_scale = noise_scale

    def act(self, stage: int, rng: np.random.Generator) -> float:
        periodic_signal = 15.0 * np.sin(stage / 5.0)
        exploration_noise = rng.normal(0.0, self.noise_scale)
        return max(1.0, self.baseline_q + periodic_signal + exploration_noise)

Q_index = 23
rng = np.random.default_rng(1)
agent = DummyAgent(baseline_q=DEFAULT_F0[Q_index])

u = DEFAULT_U0.copy()
f = DEFAULT_F0.copy()
history = []

for stage in range(30):
    f[Q_index] = agent.act(stage, rng)

    sol, qch4, qco2, ph = run_stage(
        u,
        deltaT=1.0,
        f0=f,
    )

    u = sol[-1].copy()
    history.append(
        {
            "stage": stage,
            "Q": f[Q_index],
            "Qch4": qch4[-1],
            "Qco2": qco2[-1],
            "pH": ph[-1],
        }
    )

print(history[-1])

Citation

To cite this repository:

@article{gao2024reinforcement,
	title = {Reinforcement learning-based control for waste biorefining processes under uncertainty},
	author = {Gao, Ji and Wahlen, Abigael and Ju, Caleb and Chen, Yongsheng and Lan, Guanghui and Tong, Zhaohui},
	journal = {Communications Engineering},
	year = {2024},
	volume = {3},
	number = {1},
	pages = {38},
	doi = {10.1038/s44172-024-00183-7},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adm1_numba-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

adm1_numba-0.1.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file adm1_numba-0.1.0.tar.gz.

File metadata

  • Download URL: adm1_numba-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for adm1_numba-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3d8a54cf0c67995690def644a94c65e34df309b0591d7897e72a8df781459594
MD5 ac7bb9679005f2fcdbf3239910a9d978
BLAKE2b-256 df2bbbac7d69e0cd745224e66c3d4bc2469c82baddcef4de3518d54718345648

See more details on using hashes here.

Provenance

The following attestation bundles were made for adm1_numba-0.1.0.tar.gz:

Publisher: workflow.yml on JGIoA/adm1-numba

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file adm1_numba-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: adm1_numba-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for adm1_numba-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f81bdb4be65bc5fe54c3f07da3fc540ea0f94b37445c6cdfc4f90cacc66bbd59
MD5 bd7abda68f43e5ce2ca3bbca080c3428
BLAKE2b-256 6f9245603b52954a84f586423ab5a633bea5f8fc77d8fe4990a9d8348c59d1d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for adm1_numba-0.1.0-py3-none-any.whl:

Publisher: workflow.yml on JGIoA/adm1-numba

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page