|
1 | 1 | ### Reaction Complex Handling ###
|
2 | 2 |
|
| 3 | +# get the species indices and stoichiometry while filtering out constant species. |
| 4 | +function filter_constspecs(specs, stoich::AbstractVector{V}, smap) where {V <: Integer} |
| 5 | + isempty(specs) && (return Vector{Int}(), Vector{V}()) |
| 6 | + |
| 7 | + if any(isconstant, specs) |
| 8 | + ids = Vector{Int}() |
| 9 | + filtered_stoich = Vector{V}() |
| 10 | + for (i, s) in enumerate(specs) |
| 11 | + if !isconstant(s) |
| 12 | + push!(ids, smap[s]) |
| 13 | + push!(filtered_stoich, stoich[i]) |
| 14 | + end |
| 15 | + end |
| 16 | + else |
| 17 | + ids = map(Base.Fix1(getindex, smap), specs) |
| 18 | + filtered_stoich = copy(stoich) |
| 19 | + end |
| 20 | + ids, filtered_stoich |
| 21 | +end |
| 22 | + |
3 | 23 | """
|
4 | 24 | reactioncomplexmap(rn::ReactionSystem)
|
5 | 25 |
|
@@ -48,26 +68,6 @@ function reactioncomplexmap(rn::ReactionSystem)
|
48 | 68 | complextorxsmap
|
49 | 69 | end
|
50 | 70 |
|
51 |
| -# get the species indices and stoichiometry while filtering out constant species. |
52 |
| -function filter_constspecs(specs, stoich::AbstractVector{V}, smap) where {V <: Integer} |
53 |
| - isempty(specs) && (return Vector{Int}(), Vector{V}()) |
54 |
| - |
55 |
| - if any(isconstant, specs) |
56 |
| - ids = Vector{Int}() |
57 |
| - filtered_stoich = Vector{V}() |
58 |
| - for (i, s) in enumerate(specs) |
59 |
| - if !isconstant(s) |
60 |
| - push!(ids, smap[s]) |
61 |
| - push!(filtered_stoich, stoich[i]) |
62 |
| - end |
63 |
| - end |
64 |
| - else |
65 |
| - ids = map(Base.Fix1(getindex, smap), specs) |
66 |
| - filtered_stoich = copy(stoich) |
67 |
| - end |
68 |
| - ids, filtered_stoich |
69 |
| -end |
70 |
| - |
71 | 71 | @doc raw"""
|
72 | 72 | reactioncomplexes(network::ReactionSystem; sparse=false)
|
73 | 73 |
|
@@ -396,6 +396,25 @@ function deficiency(rn::ReactionSystem)
|
396 | 396 | nps.deficiency
|
397 | 397 | end
|
398 | 398 |
|
| 399 | +# Used in the subsequent function. |
| 400 | +function subnetworkmapping(linkageclass, allrxs, complextorxsmap, p) |
| 401 | + rxinds = sort!(collect(Set(rxidx for rcidx in linkageclass |
| 402 | + for rxidx in complextorxsmap[rcidx]))) |
| 403 | + rxs = allrxs[rxinds] |
| 404 | + specset = Set(s for rx in rxs for s in rx.substrates if !isconstant(s)) |
| 405 | + for rx in rxs |
| 406 | + for product in rx.products |
| 407 | + !isconstant(product) && push!(specset, product) |
| 408 | + end |
| 409 | + end |
| 410 | + specs = collect(specset) |
| 411 | + newps = Vector{eltype(p)}() |
| 412 | + for rx in rxs |
| 413 | + Symbolics.get_variables!(newps, rx.rate, p) |
| 414 | + end |
| 415 | + rxs, specs, newps # reactions and species involved in reactions of subnetwork |
| 416 | +end |
| 417 | + |
399 | 418 | """
|
400 | 419 | subnetworks(rn::ReactionSystem)
|
401 | 420 |
|
@@ -433,24 +452,6 @@ function subnetworks(rs::ReactionSystem)
|
433 | 452 | subnetworks
|
434 | 453 | end
|
435 | 454 |
|
436 |
| -function subnetworkmapping(linkageclass, allrxs, complextorxsmap, p) |
437 |
| - rxinds = sort!(collect(Set(rxidx for rcidx in linkageclass |
438 |
| - for rxidx in complextorxsmap[rcidx]))) |
439 |
| - rxs = allrxs[rxinds] |
440 |
| - specset = Set(s for rx in rxs for s in rx.substrates if !isconstant(s)) |
441 |
| - for rx in rxs |
442 |
| - for product in rx.products |
443 |
| - !isconstant(product) && push!(specset, product) |
444 |
| - end |
445 |
| - end |
446 |
| - specs = collect(specset) |
447 |
| - newps = Vector{eltype(p)}() |
448 |
| - for rx in rxs |
449 |
| - Symbolics.get_variables!(newps, rx.rate, p) |
450 |
| - end |
451 |
| - rxs, specs, newps # reactions and species involved in reactions of subnetwork |
452 |
| -end |
453 |
| - |
454 | 455 | """
|
455 | 456 | linkagedeficiencies(network::ReactionSystem)
|
456 | 457 |
|
@@ -625,25 +626,7 @@ function conservationlaws(nsm::T; col_order = nothing) where {T <: AbstractMatri
|
625 | 626 | T(N')
|
626 | 627 | end
|
627 | 628 |
|
628 |
| -""" |
629 |
| - conservationlaws(rs::ReactionSystem) |
630 |
| -
|
631 |
| -Return the conservation law matrix of the given `ReactionSystem`, calculating it if it is |
632 |
| -not already stored within the system, or returning an alias to it. |
633 |
| -
|
634 |
| -Notes: |
635 |
| -- The first time being called it is calculated and cached in `rn`, subsequent calls should |
636 |
| - be fast. |
637 |
| -""" |
638 |
| -function conservationlaws(rs::ReactionSystem) |
639 |
| - nps = get_networkproperties(rs) |
640 |
| - !isempty(nps.conservationmat) && (return nps.conservationmat) |
641 |
| - nsm = netstoichmat(rs) |
642 |
| - nps.conservationmat = conservationlaws(nsm; col_order = nps.col_order) |
643 |
| - cache_conservationlaw_eqs!(rs, nps.conservationmat, nps.col_order) |
644 |
| - nps.conservationmat |
645 |
| -end |
646 |
| - |
| 629 | +# Used in the subsequent function. |
647 | 630 | function cache_conservationlaw_eqs!(rn::ReactionSystem, N::AbstractMatrix, col_order)
|
648 | 631 | nullity = size(N, 1)
|
649 | 632 | r = numspecies(rn) - nullity # rank of the netstoichmat
|
@@ -681,6 +664,25 @@ function cache_conservationlaw_eqs!(rn::ReactionSystem, N::AbstractMatrix, col_o
|
681 | 664 | nothing
|
682 | 665 | end
|
683 | 666 |
|
| 667 | +""" |
| 668 | + conservationlaws(rs::ReactionSystem) |
| 669 | +
|
| 670 | +Return the conservation law matrix of the given `ReactionSystem`, calculating it if it is |
| 671 | +not already stored within the system, or returning an alias to it. |
| 672 | +
|
| 673 | +Notes: |
| 674 | +- The first time being called it is calculated and cached in `rn`, subsequent calls should |
| 675 | + be fast. |
| 676 | +""" |
| 677 | +function conservationlaws(rs::ReactionSystem) |
| 678 | + nps = get_networkproperties(rs) |
| 679 | + !isempty(nps.conservationmat) && (return nps.conservationmat) |
| 680 | + nsm = netstoichmat(rs) |
| 681 | + nps.conservationmat = conservationlaws(nsm; col_order = nps.col_order) |
| 682 | + cache_conservationlaw_eqs!(rs, nps.conservationmat, nps.col_order) |
| 683 | + nps.conservationmat |
| 684 | +end |
| 685 | + |
684 | 686 | """
|
685 | 687 | conservedquantities(state, cons_laws)
|
686 | 688 |
|
|
0 commit comments