forked from Pyomo/pyomo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactor_updatesuffix.py
More file actions
67 lines (52 loc) · 2.46 KB
/
Copy pathreactor_updatesuffix.py
File metadata and controls
67 lines (52 loc) · 2.46 KB
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
# ___________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright (c) 2008-2025
# National Technology and Engineering Solutions of Sandia, LLC
# Under the terms of Contract DE-NA0003525 with National Technology and
# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
from pyomo.common.dependencies import numpy as np, pathlib
from pyomo.contrib.doe.examples.reactor_experiment import ReactorExperiment
from pyomo.contrib.doe import DesignOfExperiments
from pyomo.contrib.doe import utils
from pyomo.contrib.parmest.utils.model_utils import update_model_from_suffix
import pyomo.environ as pyo
import json
# Example to run a DoE on the reactor
def run_reactor_update_suffix_items():
# Read in file
DATA_DIR = pathlib.Path(__file__).parent
file_path = DATA_DIR / "result.json"
with open(file_path) as f:
data_ex = json.load(f)
# Put temperature control time points into correct format for reactor experiment
data_ex["control_points"] = {
float(k): v for k, v in data_ex["control_points"].items()
}
# Create a ReactorExperiment object; data and discretization information are part
# of the constructor of this object
experiment = ReactorExperiment(data=data_ex, nfe=10, ncp=3)
# Call the experiment's model using get_labeled_model
reactor_model = experiment.get_labeled_model()
# Show the model
reactor_model.pprint()
# Update the model to change the values of the desired component
# Here we will update the unknown parameters of the reactor model
example_suffix = "measurement_error"
suffix_obj = reactor_model.measurement_error
me_vars = list(suffix_obj.keys()) # components
orig_vals = np.array([suffix_obj[v] for v in me_vars])
# Original values
print("Original σ values:", orig_vals)
# Update the suffix with new values
new_vals = orig_vals + 1
# Here we are updating the values of the unknown parameters
# You must know the length of the list and order of the suffix items to update them correctly
update_model_from_suffix(suffix_obj, new_vals)
# Updated values
print("Updated σ values :", [suffix_obj[v] for v in me_vars])
if __name__ == "__main__":
run_reactor_update_suffix_items()