Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
LalitChauhan56 committed Jul 27, 2023
2 parents b369300 + f60d522 commit c2843fe
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
75 changes: 74 additions & 1 deletion src/compound.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,80 @@ function get_stoich(reaction::Reaction)
end


function balance(reaction::Reaction)

Check warning on line 152 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L152

Added line #L152 was not covered by tests
# Calculate the stoichiometric coefficients for the balanced reaction.
stoichiometries = get_stoich(reaction)

Check warning on line 154 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L154

Added line #L154 was not covered by tests

# Divide the stoichiometry vector into substrate and product stoichiometries.
substoich = stoichiometries[1:length(reaction.substrates)]
prodstoich = stoichiometries[(length(reaction.substrates)) + 1:end]

Check warning on line 158 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L157-L158

Added lines #L157 - L158 were not covered by tests

# Create a new reaction with the balanced stoichiometries
balanced_reaction = Reaction(reaction.rate, reaction.substrates, reaction.products, substoich, prodstoich)

Check warning on line 161 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L161

Added line #L161 was not covered by tests

# Return the balanced reaction
return balanced_reaction

Check warning on line 164 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L164

Added line #L164 was not covered by tests
end

### Balancing Code

function create_matrix(reaction::Catalyst.Reaction)
compounds = [reaction.substrates; reaction.products]
atoms = []
n_atoms = 0
A = zeros(Int, 0, length(compounds))

Check warning on line 173 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L169-L173

Added lines #L169 - L173 were not covered by tests

for (j, compound) in enumerate(compounds)
if iscompound(compound)
pairs = component_coefficients(compound)
if pairs == Nothing
continue

Check warning on line 179 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L175-L179

Added lines #L175 - L179 were not covered by tests
end
else
pairs = [(compound, 1)]

Check warning on line 182 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L182

Added line #L182 was not covered by tests
end

for pair in pairs
atom, coeff = pair
i = findfirst(x -> isequal(x, atom), atoms)
if i === nothing
push!(atoms, atom)
n_atoms += 1
A = [A; zeros(Int, 1, length(compounds))]
i = n_atoms

Check warning on line 192 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L185-L192

Added lines #L185 - L192 were not covered by tests
end
coeff = any(map(p -> isequal(p, compounds[j]), reaction.products)) ? -coeff : coeff
A[i, j] = coeff
end
end

Check warning on line 197 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L194-L197

Added lines #L194 - L197 were not covered by tests

# Append a row with last element as 1
new_row = zeros(Int, 1, size(A, 2))
new_row[end] = 1
A = vcat(A, new_row)

Check warning on line 202 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L200-L202

Added lines #L200 - L202 were not covered by tests

return A

Check warning on line 204 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L204

Added line #L204 was not covered by tests
end

function get_stoich(reaction::Reaction)

Check warning on line 207 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L207

Added line #L207 was not covered by tests
# Create the matrix A using create_matrix function.
A = create_matrix(reaction)

Check warning on line 209 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L209

Added line #L209 was not covered by tests

# Create the vector b. The last element is 1 and others are 0.
b = zeros(size(A,1))
b[end] = 1

Check warning on line 213 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L212-L213

Added lines #L212 - L213 were not covered by tests

# Compute x= A^-1 *b
x = inv(A)*b

Check warning on line 216 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L216

Added line #L216 was not covered by tests

# Normalize the stoichiometric coefficients to be integers.
smallest_value = minimum(x[x .> 0])
x = x / smallest_value

Check warning on line 220 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L219-L220

Added lines #L219 - L220 were not covered by tests

return round.(Int, x) # rounding is due to potential minimal numerical inaccuracies

Check warning on line 222 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L222

Added line #L222 was not covered by tests
end


function balance(reaction::Reaction)

Check warning on line 226 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L226

Added line #L226 was not covered by tests
# Calculate the stoichiometric coefficients for the balanced reaction.
stoichiometries = get_stoich(reaction)

Check warning on line 228 in src/compound.jl

View check run for this annotation

Codecov / codecov/patch

src/compound.jl#L228

Added line #L228 was not covered by tests
Expand Down Expand Up @@ -177,4 +251,3 @@ end
# end
# end
# ex
# end
2 changes: 1 addition & 1 deletion test/miscellaneous_tests/compound_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ end
let
@variables t
@species C(t) H(t) O(t)
@compound OH(t) O H
@compound OH(t) 1O 1H
@compound C3H5OH3(t) 3C 5H 3OH

@test !iscompound(O)
Expand Down

0 comments on commit c2843fe

Please sign in to comment.