Skip to content

Commit 65108b8

Browse files
committed
Chemical equilibrium
1 parent f469e25 commit 65108b8

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/ConstraintModels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export sudoku
1919
export chemical_equilibrium
2020

2121
include("assignment.jl")
22+
include("chemical_equilibrium.jl")
2223
include("cut.jl")
2324
include("golomb.jl")
2425
include("magic_square.jl")

src/chemical_equilibrium.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ function chemical_equilibrium(A, B, C)
88
@variable(model, 0 X[1:n] maximum(B))
99

1010
# mass_conservation function
11-
conserve = i -> (x -> sum(A[:, i] .* x) == B[i])
11+
conserve = i -> (x ->
12+
begin
13+
δ = abs(sum(A[:, i] .* x) - B[i])
14+
return δ 1.e-6 ? 0. : δ
15+
end
16+
)
1217

1318
for i in 1:m
14-
@constraint(model, X in Predicate(conserve(i)))
19+
@constraint(model, X in Error(conserve(i)))
1520
end
1621

1722
# computes the total energy freed by the reaction

test/JuMP.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ end
6969

7070
@info "JuMP: basic opt" value(x) value(y) (12*value(x)+20*value(y))
7171
end
72+
73+
@testset "JuMP: Chemical equilibrium" begin
74+
m, X = chemical_equilibrium(atoms_compounds, elements_weights, standard_free_energy)
75+
# set_optimizer_attribute(m, "iteration", 10000)
76+
# set_time_limit_sec(m, 120.0)
77+
optimize!(m)
78+
@info "JuMP: $compounds_names$mixture_name" value.(X)
79+
end

test/instances.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@ standard_free_energy = [
6969
-22.179
7070
]
7171

72-
compounds_names = ["H", "H₂", "H₂O", "N", "N₂", "NH", "NO", "O", "O₂", "OH"]
72+
compounds_names = "x₁⋅H + x₂⋅H₂ + x₃⋅H₂O + x₄⋅N + x₅⋅N₂ + x₆⋅NH + x₇⋅NO + x₈⋅O + x₉⋅O₂ + x₁₀⋅OH"
73+
74+
mixture_name = "½⋅N₂H₄ + ½⋅O₂"
75+
76+
equation = compounds_names * " = " * mixture_name

0 commit comments

Comments
 (0)