File size: 4,495 Bytes
f421c2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import numpy as np
import numpy.testing as nt
import openpnm as op
from scipy.interpolate import interp1d
class TransientReactiveTransportTest:
def setup_class(self):
np.random.seed(0)
self.net = op.network.Cubic(shape=[3, 3, 1], spacing=1e-6)
self.net.add_model_collection(
op.models.collections.geometry.spheres_and_cylinders)
self.net.regenerate_models()
self.net['pore.volume'] = 1e-12
self.phase = op.phase.Phase(network=self.net)
self.phase['pore.A'] = -1e-13
self.phase['pore.k'] = 2
self.phase['throat.diffusive_conductance'] = 1e-12
mod = op.models.physics.source_terms.standard_kinetics
self.phase.add_model(propname='pore.reaction',
model=mod,
prefactor='pore.A',
exponent='pore.k',
X='pore.concentration',
regen_mode='deferred')
self.alg = op.algorithms.TransientReactiveTransport(network=self.net,
phase=self.phase)
self.alg.settings._update({'quantity': 'pore.concentration',
'conductance': 'throat.diffusive_conductance'})
self.alg.set_value_BC(pores=self.net.pores('front'), values=2)
self.alg.set_source(propname='pore.reaction', pores=self.net.pores('back'))
def test_transient_reactive_transport(self):
self.alg.run(x0=0, tspan=(0, 1))
desired = 1.13133
actual = self.alg.x.mean()
nt.assert_allclose(actual, desired, rtol=1e-5)
def test_transient_solution(self):
self.alg.run(x0=0, tspan=(0, 1), saveat=0.1)
from openpnm.algorithms._solution import TransientSolution
quantity = self.alg.settings['quantity']
# Ensure solution object is attached to the algorithm
assert isinstance(self.alg.soln[quantity], TransientSolution)
# Test shape
nt.assert_array_equal(self.alg.soln[quantity].shape, (self.alg.Np, 11))
# Test stored time points
nt.assert_array_equal(self.alg.soln[quantity].t, np.arange(0, 1.1, 0.1))
# Ensure solution is callable (i.e., interpolates intermediate times)
assert hasattr(self.alg.soln[quantity], "__call__")
# Test solution interpolation
f = interp1d(self.alg.soln[quantity].t, self.alg.soln[quantity])
nt.assert_allclose(f(0.05), self.alg.soln[quantity](0.05))
# Ensure no extrapolation
with nt.assert_raises(Exception):
self.alg.soln(1.01)
def test_consecutive_runs_preserves_solution(self):
# Integrate from 0 to 0.3 in two steps
self.alg.run(x0=0, tspan=(0, 0.1))
self.alg.run(x0=self.alg.x, tspan=(0.1, 0.3))
out1 = self.alg.soln
# Integrate from 0 to 0.3 in one go
self.alg.run(x0=0, tspan=(0, 0.3))
out2 = self.alg.soln
# Ensure the results match
quantity = self.alg.settings['quantity']
nt.assert_allclose(out1[quantity](0.3), out2[quantity](0.3), rtol=1e-5)
def test_adding_bc_over_sources(self):
with nt.assert_raises(Exception):
self.alg.set_value_BC(pores=self.net.pores("right"), values=0.3)
def test_adding_sources_over_bc(self):
with nt.assert_raises(Exception):
self.alg.set_source(propname='pore.reaction',
pores=self.net.pores('left'))
def test_ensure_settings_are_valid(self):
alg = op.algorithms.TransientReactiveTransport(network=self.net,
phase=self.phase)
# Commenting the next bits until settings are settled
# with nt.assert_raises_regex(Exception, r".*quantity.*"):
# alg.run(x0=0, tspan=(0, 1))
# alg.settings['quantity'] = 'pore.concentration'
# with nt.assert_raises_regex(Exception, r".*conductance.*"):
# alg.run(x0=0, tspan=(0, 1))
# alg.settings['conductance'] = 'throat.diffusive_conductance'
# alg.run(x0=0, tspan=(0, 1))
def teardown_class(self):
ws = op.Workspace()
ws.clear()
if __name__ == '__main__':
t = TransientReactiveTransportTest()
t.setup_class()
self = t
for item in t.__dir__():
if item.startswith('test'):
print(f'Running test: {item}')
t.__getattribute__(item)()
|