|
| 1 | +# ___________________________________________________________________________ |
| 2 | +# |
| 3 | +# Pyomo: Python Optimization Modeling Objects |
| 4 | +# Copyright (c) 2008-2024 |
| 5 | +# National Technology and Engineering Solutions of Sandia, LLC |
| 6 | +# Under the terms of Contract DE-NA0003525 with National Technology and |
| 7 | +# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain |
| 8 | +# rights in this software. |
| 9 | +# This software is distributed under the 3-clause BSD License. |
| 10 | +# |
| 11 | +# Pyomo.DoE was produced under the Department of Energy Carbon Capture Simulation |
| 12 | +# Initiative (CCSI), and is copyright (c) 2022 by the software owners: |
| 13 | +# TRIAD National Security, LLC., Lawrence Livermore National Security, LLC., |
| 14 | +# Lawrence Berkeley National Laboratory, Pacific Northwest National Laboratory, |
| 15 | +# Battelle Memorial Institute, University of Notre Dame, |
| 16 | +# The University of Pittsburgh, The University of Texas at Austin, |
| 17 | +# University of Toledo, West Virginia University, et al. All rights reserved. |
| 18 | +# |
| 19 | +# NOTICE. This Software was developed under funding from the |
| 20 | +# U.S. Department of Energy and the U.S. Government consequently retains |
| 21 | +# certain rights. As such, the U.S. Government has been granted for itself |
| 22 | +# and others acting on its behalf a paid-up, nonexclusive, irrevocable, |
| 23 | +# worldwide license in the Software to reproduce, distribute copies to the |
| 24 | +# public, prepare derivative works, and perform publicly and display |
| 25 | +# publicly, and to permit other to do so. |
| 26 | +# ___________________________________________________________________________ |
| 27 | + |
| 28 | +# from pyomo.contrib.parmest.examples.reactor_design import reactor_design_model |
| 29 | +# if we refactor to use the same create_model function as parmest, |
| 30 | +# we can just import instead of redefining the model |
| 31 | + |
| 32 | +import pyomo.environ as pyo |
| 33 | +from pyomo.dae import ContinuousSet, DerivativeVar |
| 34 | +from pyomo.contrib.doe import ( |
| 35 | + ModelOptionLib, |
| 36 | + DesignOfExperiments, |
| 37 | + MeasurementVariables, |
| 38 | + DesignVariables, |
| 39 | +) |
| 40 | +from pyomo.common.dependencies import numpy as np |
| 41 | + |
| 42 | + |
| 43 | +def create_model_legacy(mod=None, model_option=None): |
| 44 | + model_option = ModelOptionLib(model_option) |
| 45 | + |
| 46 | + model = mod |
| 47 | + |
| 48 | + if model_option == ModelOptionLib.parmest: |
| 49 | + model = pyo.ConcreteModel() |
| 50 | + return_m = True |
| 51 | + elif model_option == ModelOptionLib.stage1 or model_option == ModelOptionLib.stage2: |
| 52 | + if model is None: |
| 53 | + raise ValueError( |
| 54 | + "If model option is stage1 or stage2, a created model needs to be provided." |
| 55 | + ) |
| 56 | + return_m = False |
| 57 | + else: |
| 58 | + raise ValueError( |
| 59 | + "model_option needs to be defined as parmest, stage1, or stage2." |
| 60 | + ) |
| 61 | + |
| 62 | + model = _create_model_details(model) |
| 63 | + |
| 64 | + if return_m: |
| 65 | + return model |
| 66 | + |
| 67 | + |
| 68 | +def create_model(): |
| 69 | + model = pyo.ConcreteModel() |
| 70 | + return _create_model_details(model) |
| 71 | + |
| 72 | + |
| 73 | +def _create_model_details(model): |
| 74 | + |
| 75 | + # Rate constants |
| 76 | + model.k1 = pyo.Var(initialize=5.0 / 6.0, within=pyo.PositiveReals) # min^-1 |
| 77 | + model.k2 = pyo.Var(initialize=5.0 / 3.0, within=pyo.PositiveReals) # min^-1 |
| 78 | + model.k3 = pyo.Var( |
| 79 | + initialize=1.0 / 6000.0, within=pyo.PositiveReals |
| 80 | + ) # m^3/(gmol min) |
| 81 | + |
| 82 | + # Inlet concentration of A, gmol/m^3 |
| 83 | + model.caf = pyo.Var(initialize=10000, within=pyo.PositiveReals) |
| 84 | + |
| 85 | + # Space velocity (flowrate/volume) |
| 86 | + model.sv = pyo.Var(initialize=1.0, within=pyo.PositiveReals) |
| 87 | + |
| 88 | + # Outlet concentration of each component |
| 89 | + model.ca = pyo.Var(initialize=5000.0, within=pyo.PositiveReals) |
| 90 | + model.cb = pyo.Var(initialize=2000.0, within=pyo.PositiveReals) |
| 91 | + model.cc = pyo.Var(initialize=2000.0, within=pyo.PositiveReals) |
| 92 | + model.cd = pyo.Var(initialize=1000.0, within=pyo.PositiveReals) |
| 93 | + |
| 94 | + # Constraints |
| 95 | + model.ca_bal = pyo.Constraint( |
| 96 | + expr=( |
| 97 | + 0 |
| 98 | + == model.sv * model.caf |
| 99 | + - model.sv * model.ca |
| 100 | + - model.k1 * model.ca |
| 101 | + - 2.0 * model.k3 * model.ca**2.0 |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + model.cb_bal = pyo.Constraint( |
| 106 | + expr=(0 == -model.sv * model.cb + model.k1 * model.ca - model.k2 * model.cb) |
| 107 | + ) |
| 108 | + |
| 109 | + model.cc_bal = pyo.Constraint( |
| 110 | + expr=(0 == -model.sv * model.cc + model.k2 * model.cb) |
| 111 | + ) |
| 112 | + |
| 113 | + model.cd_bal = pyo.Constraint( |
| 114 | + expr=(0 == -model.sv * model.cd + model.k3 * model.ca**2.0) |
| 115 | + ) |
| 116 | + |
| 117 | + return model |
| 118 | + |
| 119 | + |
| 120 | +def main(legacy_create_model_interface=False): |
| 121 | + |
| 122 | + # measurement object |
| 123 | + measurements = MeasurementVariables() |
| 124 | + measurements.add_variables("ca", indices=None, time_index_position=None) |
| 125 | + measurements.add_variables("cb", indices=None, time_index_position=None) |
| 126 | + measurements.add_variables("cc", indices=None, time_index_position=None) |
| 127 | + measurements.add_variables("cd", indices=None, time_index_position=None) |
| 128 | + |
| 129 | + # design object |
| 130 | + exp_design = DesignVariables() |
| 131 | + exp_design.add_variables( |
| 132 | + "sv", |
| 133 | + indices=None, |
| 134 | + time_index_position=None, |
| 135 | + values=1.0, |
| 136 | + lower_bounds=0.1, |
| 137 | + upper_bounds=10.0, |
| 138 | + ) |
| 139 | + exp_design.add_variables( |
| 140 | + "caf", |
| 141 | + indices=None, |
| 142 | + time_index_position=None, |
| 143 | + values=10000, |
| 144 | + lower_bounds=5000, |
| 145 | + upper_bounds=15000, |
| 146 | + ) |
| 147 | + |
| 148 | + theta_values = {"k1": 5.0 / 6.0, "k2": 5.0 / 3.0, "k3": 1.0 / 6000.0} |
| 149 | + |
| 150 | + if legacy_create_model_interface: |
| 151 | + create_model_ = create_model_legacy |
| 152 | + else: |
| 153 | + create_model_ = create_model |
| 154 | + |
| 155 | + doe1 = DesignOfExperiments( |
| 156 | + theta_values, exp_design, measurements, create_model_, prior_FIM=None |
| 157 | + ) |
| 158 | + |
| 159 | + result = doe1.compute_FIM( |
| 160 | + mode="sequential_finite", # calculation mode |
| 161 | + scale_nominal_param_value=True, # scale nominal parameter value |
| 162 | + formula="central", # formula for finite difference |
| 163 | + ) |
| 164 | + |
| 165 | + # doe1.model.pprint() |
| 166 | + |
| 167 | + result.result_analysis() |
| 168 | + |
| 169 | + # print("FIM =\n",result.FIM) |
| 170 | + # print("jac =\n",result.jaco_information) |
| 171 | + # print("log10 Trace of FIM: ", np.log10(result.trace)) |
| 172 | + # print("log10 Determinant of FIM: ", np.log10(result.det)) |
| 173 | + |
| 174 | + # test result |
| 175 | + expected_log10_trace = 6.815 |
| 176 | + log10_trace = np.log10(result.trace) |
| 177 | + relative_error_trace = abs(log10_trace - 6.815) |
| 178 | + assert relative_error_trace < 0.01, ( |
| 179 | + "log10(tr(FIM)) regression test failed, answer " |
| 180 | + + str(round(log10_trace, 3)) |
| 181 | + + " does not match expected answer of " |
| 182 | + + str(expected_log10_trace) |
| 183 | + ) |
| 184 | + |
| 185 | + expected_log10_det = 18.719 |
| 186 | + log10_det = np.log10(result.det) |
| 187 | + relative_error_det = abs(log10_det - 18.719) |
| 188 | + assert relative_error_det < 0.01, ( |
| 189 | + "log10(det(FIM)) regression test failed, answer " |
| 190 | + + str(round(log10_det, 3)) |
| 191 | + + " does not match expected answer of " |
| 192 | + + str(expected_log10_det) |
| 193 | + ) |
| 194 | + |
| 195 | + doe2 = DesignOfExperiments( |
| 196 | + theta_values, exp_design, measurements, create_model_, prior_FIM=None |
| 197 | + ) |
| 198 | + |
| 199 | + square_result2, optimize_result2 = doe2.stochastic_program( |
| 200 | + if_optimize=True, |
| 201 | + if_Cholesky=True, |
| 202 | + scale_nominal_param_value=True, |
| 203 | + objective_option="det", |
| 204 | + jac_initial=result.jaco_information.copy(), |
| 205 | + step=0.1, |
| 206 | + ) |
| 207 | + |
| 208 | + optimize_result2.result_analysis() |
| 209 | + log_det = np.log10(optimize_result2.det) |
| 210 | + print("log(det) = ", round(log_det, 3)) |
| 211 | + log_det_expected = 19.266 |
| 212 | + assert abs(log_det - log_det_expected) < 0.01, "log(det) regression test failed" |
| 213 | + |
| 214 | + doe3 = DesignOfExperiments( |
| 215 | + theta_values, exp_design, measurements, create_model_, prior_FIM=None |
| 216 | + ) |
| 217 | + |
| 218 | + square_result3, optimize_result3 = doe3.stochastic_program( |
| 219 | + if_optimize=True, |
| 220 | + scale_nominal_param_value=True, |
| 221 | + objective_option="trace", |
| 222 | + jac_initial=result.jaco_information.copy(), |
| 223 | + step=0.1, |
| 224 | + ) |
| 225 | + |
| 226 | + optimize_result3.result_analysis() |
| 227 | + log_trace = np.log10(optimize_result3.trace) |
| 228 | + log_trace_expected = 7.509 |
| 229 | + print("log(trace) = ", round(log_trace, 3)) |
| 230 | + assert ( |
| 231 | + abs(log_trace - log_trace_expected) < 0.01 |
| 232 | + ), "log(trace) regression test failed" |
| 233 | + |
| 234 | + |
| 235 | +if __name__ == "__main__": |
| 236 | + main(legacy_create_model_interface=False) |
0 commit comments