本文整理汇总了Python中chemreac.ReactionDiffusion.dense_jac_cmaj方法的典型用法代码示例。如果您正苦于以下问题:Python ReactionDiffusion.dense_jac_cmaj方法的具体用法?Python ReactionDiffusion.dense_jac_cmaj怎么用?Python ReactionDiffusion.dense_jac_cmaj使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chemreac.ReactionDiffusion
的用法示例。
在下文中一共展示了ReactionDiffusion.dense_jac_cmaj方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_n_jac_diags
# 需要导入模块: from chemreac import ReactionDiffusion [as 别名]
# 或者: from chemreac.ReactionDiffusion import dense_jac_cmaj [as 别名]
def test_n_jac_diags(n_jac_diags):
N, n, nstencil = 10, 1, 7
rd = ReactionDiffusion(n, [], [], [], N=N, nstencil=nstencil,
n_jac_diags=n_jac_diags, D=[9])
assert np.allclose(rd.xcenters,
[.05, .15, .25, .35, .45, .55, .65, .75, .85, .95])
y0 = np.ones(N)
# Dense
jref_cdns = np.zeros((n*N, n*N), order='F')
jout_cdns = np.zeros((n*N, n*N), order='F')
sm = SymRD.from_rd(rd)
sm.dense_jac(0.0, y0.flatten(), jref_cdns)
rd.dense_jac_cmaj(0.0, y0.flatten(), jout_cdns)
assert np.allclose(jout_cdns, jref_cdns)
# Banded
jref_cbnd = rd.alloc_jout(order='F', pad=0)
jout_cbnd = rd.alloc_jout(order='F')
sm.banded_jac(0.0, y0.flatten(), jref_cbnd)
rd.banded_jac_cmaj(0.0, y0.flatten(), jout_cbnd)
assert np.allclose(jout_cbnd[rd.n*rd.n_jac_diags:, :], jref_cbnd)
# Compressed
jref_cmprs = rd.alloc_jout_compressed()
jout_cmprs = rd.alloc_jout_compressed()
sm.compressed_jac(0.0, y0.flatten(), jref_cmprs)
rd.compressed_jac_cmaj(0.0, y0.flatten(), jout_cmprs)
assert np.allclose(jout_cmprs, jref_cmprs)
示例2: test_ReactionDiffusion__3_reactions_4_species_5_bins_k_factor
# 需要导入模块: from chemreac import ReactionDiffusion [as 别名]
# 或者: from chemreac.ReactionDiffusion import dense_jac_cmaj [as 别名]
#.........这里部分代码省略.........
def cflux(si, bi):
f = 0.0
for k in range(nstencil):
f += rd.D_weight[bi*nstencil+k]*y0[pxci2bi[lb[bi]+k], si]
return D[si]*f
r = [
[k[0]*modulation[0][bi]*y0[bi, 0]*y0[bi, 1] for
bi in range(N)],
[k[1]*modulation[1][bi]*y0[bi, 3]*y0[bi, 2] for
bi in range(N)],
[k[2]*y0[bi, 1]**2 for bi in range(N)],
]
fref = np.array([[
-r[0][bi] + r[1][bi] + cflux(0, bi),
-r[0][bi] + r[1][bi] - 2*r[2][bi] + cflux(1, bi),
r[0][bi] - r[1][bi] + cflux(2, bi),
r[2][bi] + cflux(3, bi)
] for bi in range(N)]).flatten()
# Now let's check that the Jacobian is correctly computed.
def dfdC(bi, lri, lci):
v = 0.0
for ri in range(len(stoich_active)):
totl = (stoich_prod[ri].count(lri) -
stoich_active[ri].count(lri))
if totl == 0:
continue
actv = stoich_active[ri].count(lci)
if actv == 0:
continue
v += actv*totl*r[ri][bi]/y0[bi, lci]
return v
def jac_elem(ri, ci):
bri, bci = ri // n, ci // n
lri, lci = ri % n, ci % n
elem = 0.0
def _diffusion():
_elem = 0.0
for k in range(nstencil):
if pxci2bi[lb[bri]+k] == bci:
_elem += D[lri]*rd.D_weight[bri*nstencil+k]
return _elem
if bri == bci:
# on block diagonal
elem += dfdC(bri, lri, lci)
if lri == lci:
elem += _diffusion()
elif bri == bci - 1:
if lri == lci:
elem = _diffusion()
elif bri == bci + 1:
if lri == lci:
elem = _diffusion()
return elem
jref = np.zeros((n*N, n*N), order='C')
for ri, ci in np.ndindex(n*N, n*N):
jref[ri, ci] = jac_elem(ri, ci)
# Compare to what is calculated using our C++ callback
_test_f_and_dense_jac_rmaj(rd, 0, y0.flatten(), fref, jref)
jout_cmaj = np.zeros((n*N, n*N), order='F')
rd.dense_jac_cmaj(0.0, y0.flatten(), jout_cmaj)
assert np.allclose(jout_cmaj, jref)
ref_banded_j = get_banded(jref, n, N)
ref_banded_j_symbolic = rd.alloc_jout(order='F', pad=0)
symrd = SymRD.from_rd(rd)
symrd.banded_jac(0.0, y0.flatten(), ref_banded_j_symbolic)
assert np.allclose(ref_banded_j_symbolic, ref_banded_j)
jout_bnd_packed_cmaj = np.zeros((3*n+1, n*N), order='F')
rd.banded_jac_cmaj(0.0, y0.flatten(), jout_bnd_packed_cmaj)
if os.environ.get('plot_tests', False):
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from chemreac.util.plotting import coloured_spy
fig = plt.figure()
ax = fig.add_subplot(3, 1, 1)
coloured_spy(ref_banded_j, ax=ax)
plt.title('ref_banded_j')
ax = fig.add_subplot(3, 1, 2)
coloured_spy(jout_bnd_packed_cmaj[n:, :], ax=ax)
plt.title('jout_bnd_packed_cmaj')
ax = fig.add_subplot(3, 1, 3)
coloured_spy(ref_banded_j-jout_bnd_packed_cmaj[n:, :], ax=ax)
plt.title('diff')
plt.savefig(__file__+'.png')
assert np.allclose(jout_bnd_packed_cmaj[n:, :], ref_banded_j)