This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest_tutorials.py
150 lines (124 loc) · 5.77 KB
/
test_tutorials.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
##############################################################################
# Institute for the Design of Advanced Energy Systems Process Systems
# Engineering Framework (IDAES PSE Framework) Copyright (c) 2018-2019, by the
# software owners: The Regents of the University of California, through
# Lawrence Berkeley National Laboratory, National Technology & Engineering
# Solutions of Sandia, LLC, Carnegie Mellon University, West Virginia
# University Research Corporation, et al. All rights reserved.
#
# Please see the files COPYRIGHT.txt and LICENSE.txt for full copyright and
# license information, respectively. Both files are also available online
# at the URL "https://github.com/IDAES/idaes-pse".
##############################################################################
"""
Tests that tutorials run.
*** NOTE: These tutorials have been deprecated. The tests are not run.
Author: Andrew Lee
Modified by: Dan Gunter (to run in `examples` context)
"""
# stdlib
from pathlib import Path
import sys
# third-party
import pytest
from pyomo.environ import SolverFactory
from pyomo.opt import SolverStatus, TerminationCondition
# package
from idaes.core.util.model_statistics import degrees_of_freedom
def __deprecated():
"""Since these tutorials have been deprecated, do not run the tests.
"""
# assume parent of this test's directory is top
TOP_DIR = Path(__file__).parent.parent
# add tutorials to path and import them
sys.path.append(str(TOP_DIR / "src" / "tutorials"))
import Tutorial_1_Basic_Flowsheets
import Tutorial_2_Basic_Flowsheet_Optimization
import Tutorial_3_Dynamic_Flowsheets
# See if ipopt is available and set up solver
if not SolverFactory("ipopt").available():
solver = None
else:
solver = True
@pytest.mark.integration
@pytest.mark.skipif(solver is None, reason="Solver not available")
def test_tutorial_1():
m, results = Tutorial_1_Basic_Flowsheets.main()
# Check for optimal solution
assert results.solver.termination_condition == TerminationCondition.optimal
assert results.solver.status == SolverStatus.ok
assert degrees_of_freedom(m) == 0
assert m.fs.Tank2.outlet.flow_vol[0].value == pytest.approx(1.0, abs=1e-2)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "Ethanol"].value == pytest.approx(
89.628, abs=1e-2
)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "EthylAcetate"].value == pytest.approx(
10.372, abs=1e-2
)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "NaOH"].value == pytest.approx(
10.372, abs=1e-2
)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "SodiumAcetate"].value == pytest.approx(
89.628, abs=1e-2
)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "H2O"].value == pytest.approx(
55388.0, abs=1
)
assert m.fs.Tank2.outlet.pressure[0].value == pytest.approx(101325, abs=1)
assert m.fs.Tank2.outlet.temperature[0].value == pytest.approx(304.20, abs=1e-1)
@pytest.mark.integration
@pytest.mark.skipif(solver is None, reason="Solver not available")
def test_tutorial_2():
m, results = Tutorial_2_Basic_Flowsheet_Optimization.main()
# Check for optimal solution
assert results.solver.termination_condition == TerminationCondition.optimal
assert results.solver.status == SolverStatus.ok
assert degrees_of_freedom(m) == 1
assert m.fs.Tank2.outlet.flow_vol[0].value == pytest.approx(1.0, abs=1e-2)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "Ethanol"].value == pytest.approx(
92.106, abs=1e-2
)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "EthylAcetate"].value == pytest.approx(
7.894, abs=1e-2
)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "NaOH"].value == pytest.approx(
7.894, abs=1e-2
)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "SodiumAcetate"].value == pytest.approx(
92.106, abs=1e-2
)
assert m.fs.Tank2.outlet.conc_mol_comp[0, "H2O"].value == pytest.approx(
55388.0, abs=1
)
assert m.fs.Tank2.outlet.pressure[0].value == pytest.approx(101325, abs=1)
assert m.fs.Tank2.outlet.temperature[0].value == pytest.approx(304.23, abs=1e-1)
assert m.fs.Tank1.volume[0].value == pytest.approx(1.215, abs=1e-2)
assert m.fs.Tank2.volume[0].value == pytest.approx(1.785, abs=1e-2)
@pytest.mark.integration
@pytest.mark.skipif(solver is None, reason="Solver not available")
def test_tutorial_3():
m, results = Tutorial_3_Dynamic_Flowsheets.main()
# Check for optimal solution
assert results.solver.termination_condition == TerminationCondition.optimal
assert results.solver.status == SolverStatus.ok
assert degrees_of_freedom(m) == 0
assert m.fs.time.last() == 20.0
assert len(m.fs.time) == 51
assert m.fs.Tank2.outlet.flow_vol[20.0].value == pytest.approx(1.0, abs=1e-2)
assert m.fs.Tank2.outlet.conc_mol_comp[20.0, "Ethanol"].value == pytest.approx(
43.62, abs=1e-1
)
assert m.fs.Tank2.outlet.conc_mol_comp[20.0, "EthylAcetate"].value == pytest.approx(
1.65, abs=1e-1
)
assert m.fs.Tank2.outlet.conc_mol_comp[20.0, "NaOH"].value == pytest.approx(
6.38, abs=1e-1
)
assert m.fs.Tank2.outlet.conc_mol_comp[
20.0, "SodiumAcetate"
].value == pytest.approx(43.62, abs=1e-1)
assert m.fs.Tank2.outlet.conc_mol_comp[20.0, "H2O"].value == pytest.approx(
55388.0, abs=1
)
assert m.fs.Tank2.outlet.pressure[20.0].value == pytest.approx(101325, abs=1)
assert m.fs.Tank2.outlet.temperature[20.0].value == pytest.approx(303.66, abs=1e-1)