Skip to content

Add callback mechanism to TransientReactiveTransport#2254

Draft
ma-sadeghi wants to merge 10 commits into
devfrom
callback_mechanism
Draft

Add callback mechanism to TransientReactiveTransport#2254
ma-sadeghi wants to merge 10 commits into
devfrom
callback_mechanism

Conversation

@ma-sadeghi

@ma-sadeghi ma-sadeghi commented Dec 16, 2021

Copy link
Copy Markdown
Member

Closes #2207.

Here's a working example:

import numpy as np
import openpnm as op
import matplotlib.pyplot as plt

Nx = 101
shape = [Nx, 1, 1]
spacing = 1/Nx * 5
net = op.network.Cubic(shape=shape, spacing=spacing)
air = op.phases.Air(network=net)
air["throat.diffusive_conductance"] = spacing * np.ones(net.Nt)
net["pore.volume"] = spacing**3

# Set up transient Fickian diffusion algorithm
tfd = op.algorithms.TransientFickianDiffusion(network=net, phase=air)
tfd.set_value_BC(net.pores("left"), 0)
tfd.set_value_BC(net.pores("right"), 0)

# Define a pulse signal
def pulse(t, y):
    if 0 <= t <= 0.05:
        y[net.Np//2] = 1.0

# Add the pulse signal to the algorithm as a callback
tfd.set_callback(pulse)

# Solve the transient algorithm
c0 = np.zeros(tfd.Np)
tspan = [0, 0.4]
sol = tfd.run(x0=c0, tspan=tspan)

# Plot c vs. time
tout = np.linspace(tspan[0], tspan[1], 10)
fig, ax = plt.subplots()
for i, t in enumerate(tout):
    ax.plot(sol(t), label=f"{t:.2f} (s)")
ax.legend()
ax.set_title("Dissipation of a pulse signal , c(x=0,L) = 0")
ax.set_xlabel("distance (m)")
ax.set_ylabel("concentration (mol/m$^3$)")

@codecov

codecov Bot commented Dec 16, 2021

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.7%. Comparing base (49cc886) to head (10ac36f).

Additional details and impacted files
@@          Coverage Diff          @@
##             dev   #2254   +/-   ##
=====================================
  Coverage   87.7%   87.7%           
=====================================
  Files        151     151           
  Lines       8879    8888    +9     
=====================================
+ Hits        7792    7800    +8     
- Misses      1087    1088    +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ma-sadeghi ma-sadeghi changed the title Add callback mechanism to TransientReactiveTransport Add callback mechanism to TransientReactiveTransport Dec 5, 2025
@jgostick jgostick marked this pull request as draft April 20, 2026 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a apply_callbacks method to the Transient solver

2 participants