OpenPyTEA 3.2.2__py3-none-any.whl → 3.2.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openpytea/__init__.py +17 -2
- openpytea/analysis.py +108 -27
- openpytea/cli.py +11 -1
- openpytea/data/cost_correlations.csv +431 -425
- openpytea/equipment.py +722 -34
- openpytea/io.py +257 -5
- openpytea/plant.py +378 -74
- openpytea/plotting.py +357 -65
- {openpytea-3.2.2.dist-info → openpytea-3.2.3.dist-info}/METADATA +17 -30
- openpytea-3.2.3.dist-info/RECORD +16 -0
- openpytea-3.2.2.dist-info/RECORD +0 -16
- {openpytea-3.2.2.dist-info → openpytea-3.2.3.dist-info}/WHEEL +0 -0
- {openpytea-3.2.2.dist-info → openpytea-3.2.3.dist-info}/entry_points.txt +0 -0
- {openpytea-3.2.2.dist-info → openpytea-3.2.3.dist-info}/licenses/LICENSE +0 -0
- {openpytea-3.2.2.dist-info → openpytea-3.2.3.dist-info}/top_level.txt +0 -0
openpytea/__init__.py
CHANGED
|
@@ -5,8 +5,11 @@ economic sensitivity and uncertainty evaluation.
|
|
|
5
5
|
|
|
6
6
|
from importlib.metadata import version
|
|
7
7
|
|
|
8
|
-
from .plant import Plant
|
|
9
|
-
|
|
8
|
+
from .plant import (Plant, PlantConfig, OpexInput, ProductInput,
|
|
9
|
+
Uncertainty, Dependency)
|
|
10
|
+
from .equipment import (Equipment, CompositeEquipment, inflation_adjustment,
|
|
11
|
+
equipment_options, ProcessType, Category,
|
|
12
|
+
EquipmentType, Material)
|
|
10
13
|
from .analysis import (direct_costs_data, fixed_capital_data,
|
|
11
14
|
fixed_opex_data, variable_opex_data,
|
|
12
15
|
levelized_cost_data, cash_flow_data,
|
|
@@ -15,6 +18,7 @@ from .analysis import (direct_costs_data, fixed_capital_data,
|
|
|
15
18
|
from .plotting import (plot_stacked_bar, plot_cash_flow,
|
|
16
19
|
plot_sensitivity,
|
|
17
20
|
plot_tornado, plot_monte_carlo,
|
|
21
|
+
plot_monte_carlo_cumulative,
|
|
18
22
|
plot_monte_carlo_inputs,
|
|
19
23
|
plot_multiple_monte_carlo)
|
|
20
24
|
|
|
@@ -27,9 +31,19 @@ __license__ = "MIT"
|
|
|
27
31
|
|
|
28
32
|
__all__ = [
|
|
29
33
|
"Plant",
|
|
34
|
+
"PlantConfig",
|
|
35
|
+
"OpexInput",
|
|
36
|
+
"ProductInput",
|
|
37
|
+
"Uncertainty",
|
|
38
|
+
"Dependency",
|
|
30
39
|
"Equipment",
|
|
31
40
|
"CompositeEquipment",
|
|
32
41
|
"inflation_adjustment",
|
|
42
|
+
"equipment_options",
|
|
43
|
+
"ProcessType",
|
|
44
|
+
"Category",
|
|
45
|
+
"EquipmentType",
|
|
46
|
+
"Material",
|
|
33
47
|
"direct_costs_data",
|
|
34
48
|
"fixed_capital_data",
|
|
35
49
|
"fixed_opex_data",
|
|
@@ -44,6 +58,7 @@ __all__ = [
|
|
|
44
58
|
"plot_sensitivity",
|
|
45
59
|
"plot_tornado",
|
|
46
60
|
"plot_monte_carlo",
|
|
61
|
+
"plot_monte_carlo_cumulative",
|
|
47
62
|
"plot_monte_carlo_inputs",
|
|
48
63
|
"plot_multiple_monte_carlo",
|
|
49
64
|
"load_results",
|
openpytea/analysis.py
CHANGED
|
@@ -2,9 +2,11 @@ import warnings
|
|
|
2
2
|
from copy import deepcopy
|
|
3
3
|
from tqdm.auto import tqdm
|
|
4
4
|
from scipy import stats
|
|
5
|
+
from scipy.optimize import root_scalar
|
|
5
6
|
import numpy as np
|
|
6
7
|
|
|
7
|
-
from openpytea.plant import DEFAULT_OPERATOR_HOURLY_RATE
|
|
8
|
+
from openpytea.plant import (DEFAULT_OPERATOR_HOURLY_RATE,
|
|
9
|
+
_additional_capex_items)
|
|
8
10
|
from openpytea.helpers import (_make_label,
|
|
9
11
|
_legacy_label,
|
|
10
12
|
_tex_escape,
|
|
@@ -159,23 +161,19 @@ def fixed_capital_data(plants, additional_capex=False, pct=False):
|
|
|
159
161
|
"Contingency": plant.contigency,
|
|
160
162
|
}
|
|
161
163
|
|
|
162
|
-
#
|
|
163
|
-
#
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
164
|
+
# Named additional CAPEX ({name: cost}) gets a segment per item,
|
|
165
|
+
# unnamed (a list) one summed "Additional CAPEX" segment
|
|
166
|
+
items = _additional_capex_items(
|
|
167
|
+
plant.additional_capex_cost, plant.additional_capex_years
|
|
168
|
+
) if additional_capex else None
|
|
169
|
+
if items is not None:
|
|
170
|
+
names, costs, _ = items
|
|
171
|
+
if names:
|
|
172
|
+
components.update(
|
|
173
|
+
{_make_label(n): float(c) for n, c in zip(names, costs)}
|
|
170
174
|
)
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
total_extra = float(extra) if extra is not None else 0.0
|
|
174
|
-
except (TypeError, ValueError):
|
|
175
|
-
total_extra = 0.0
|
|
176
|
-
|
|
177
|
-
if total_extra != 0:
|
|
178
|
-
components["Additional CAPEX"] = total_extra
|
|
175
|
+
elif costs.sum() != 0:
|
|
176
|
+
components["Additional CAPEX"] = float(costs.sum())
|
|
179
177
|
|
|
180
178
|
components_list.append(components)
|
|
181
179
|
xlabels.append(plant.name)
|
|
@@ -995,6 +993,9 @@ def make_distribution(dist_id, loc=None, scale=None, shape=None,
|
|
|
995
993
|
- 10 : Beta (``loc``=alpha, ``shape``=beta, ``maximum``=upper bound)
|
|
996
994
|
- 11 : GEV (``loc``=mu, ``scale``=sigma, ``shape``=xi)
|
|
997
995
|
- 12 : Student's t (``loc``=median, ``scale``=scale, ``shape``=nu)
|
|
996
|
+
|
|
997
|
+
13 (Reversed lognormal) has no frozen SciPy form; only
|
|
998
|
+
:func:`sample_distribution` supports it.
|
|
998
999
|
loc : float, optional
|
|
999
1000
|
Location parameter; meaning depends on ``dist_id`` (see above).
|
|
1000
1001
|
scale : float, optional
|
|
@@ -1063,10 +1064,69 @@ def make_distribution(dist_id, loc=None, scale=None, shape=None,
|
|
|
1063
1064
|
elif dist_id == 12: # Student's t: loc=median, scale=scale, shape=nu
|
|
1064
1065
|
return stats.t(df=shape, loc=loc, scale=scale)
|
|
1065
1066
|
|
|
1067
|
+
elif dist_id == 13:
|
|
1068
|
+
raise ValueError(
|
|
1069
|
+
"dist_id 13 (Reversed lognormal) has no frozen SciPy "
|
|
1070
|
+
"distribution; draw it with sample_distribution."
|
|
1071
|
+
)
|
|
1072
|
+
|
|
1066
1073
|
else:
|
|
1067
1074
|
raise ValueError(f"Unsupported dist_id for make_distribution: {dist_id}")
|
|
1068
1075
|
|
|
1069
1076
|
|
|
1077
|
+
def _reversed_lognormal_quantile(u, mean, std, minimum, maximum):
|
|
1078
|
+
"""
|
|
1079
|
+
Quantiles ``u`` of a lognormal mirrored at ``maximum`` (long tail
|
|
1080
|
+
towards ``minimum``), truncated to ``[minimum, maximum]``.
|
|
1081
|
+
|
|
1082
|
+
``x = maximum - y`` with ``y`` lognormal on ``(0, maximum - minimum]``.
|
|
1083
|
+
``sigma`` of ``ln y`` is set from ``std`` (the spread before
|
|
1084
|
+
truncation, so the sampled spread comes out smaller), and ``mu`` is
|
|
1085
|
+
solved so the truncated mean equals ``mean``. Quantiles increase
|
|
1086
|
+
with ``u``.
|
|
1087
|
+
"""
|
|
1088
|
+
if maximum is None or minimum is None or mean is None:
|
|
1089
|
+
raise ValueError(
|
|
1090
|
+
"Reversed lognormal (dist_id 13) needs a mean, minimum and "
|
|
1091
|
+
"maximum."
|
|
1092
|
+
)
|
|
1093
|
+
if not minimum < mean < maximum:
|
|
1094
|
+
raise ValueError(
|
|
1095
|
+
"Reversed lognormal (dist_id 13) needs minimum < mean < "
|
|
1096
|
+
f"maximum (got {minimum}, {mean}, {maximum})."
|
|
1097
|
+
)
|
|
1098
|
+
if not std or std <= 0:
|
|
1099
|
+
raise ValueError(
|
|
1100
|
+
f"Reversed lognormal (dist_id 13) needs std > 0 (got {std})."
|
|
1101
|
+
)
|
|
1102
|
+
|
|
1103
|
+
y_mean, y_high = maximum - mean, maximum - minimum
|
|
1104
|
+
sigma = np.sqrt(np.log(1 + (std / y_mean) ** 2))
|
|
1105
|
+
log_high = np.log(y_high)
|
|
1106
|
+
|
|
1107
|
+
def truncated_mean_gap(mu):
|
|
1108
|
+
# E[y | y <= y_high] - y_mean, closed form for a lognormal; y's
|
|
1109
|
+
# lower bound is 0, so only the upper cut appears
|
|
1110
|
+
b = (log_high - mu) / sigma
|
|
1111
|
+
return (np.exp(mu + sigma**2 / 2) * stats.norm.cdf(b - sigma)
|
|
1112
|
+
/ stats.norm.cdf(b) - y_mean)
|
|
1113
|
+
|
|
1114
|
+
try:
|
|
1115
|
+
mu = root_scalar(
|
|
1116
|
+
truncated_mean_gap, method="brentq",
|
|
1117
|
+
bracket=(np.log(y_mean) - 5 * sigma - 1, log_high + 3 * sigma),
|
|
1118
|
+
).root
|
|
1119
|
+
except ValueError:
|
|
1120
|
+
raise ValueError(
|
|
1121
|
+
"Reversed lognormal (dist_id 13): no lognormal with std "
|
|
1122
|
+
f"{std} has mean {mean} within [{minimum}, {maximum}]; the "
|
|
1123
|
+
"mean is too close to the minimum for that spread. Lower std "
|
|
1124
|
+
"or widen the window."
|
|
1125
|
+
) from None
|
|
1126
|
+
y = stats.lognorm(s=sigma, scale=np.exp(mu))
|
|
1127
|
+
return maximum - y.ppf((1 - np.asarray(u)) * y.cdf(y_high))
|
|
1128
|
+
|
|
1129
|
+
|
|
1070
1130
|
def sample_distribution(dist_id, size, loc=None, scale=None, shape=None,
|
|
1071
1131
|
minimum=None, maximum=None, random_state=None):
|
|
1072
1132
|
"""
|
|
@@ -1079,6 +1139,13 @@ def sample_distribution(dist_id, size, loc=None, scale=None, shape=None,
|
|
|
1079
1139
|
samples are drawn and re-drawn (rejection sampling) until ``size``
|
|
1080
1140
|
values fall within ``[minimum, maximum]``.
|
|
1081
1141
|
|
|
1142
|
+
``dist_id`` 13 (Reversed lognormal) is drawn here only: a lognormal
|
|
1143
|
+
mirrored at ``maximum``, so its long tail points down to ``minimum``.
|
|
1144
|
+
``loc`` is the mean of the samples and ``scale`` their std before
|
|
1145
|
+
truncation (the sampled std comes out smaller), both in real units.
|
|
1146
|
+
``maximum`` is the mirror point, not just a cut-off: moving it moves
|
|
1147
|
+
every sample.
|
|
1148
|
+
|
|
1082
1149
|
Parameters
|
|
1083
1150
|
----------
|
|
1084
1151
|
dist_id : int
|
|
@@ -1121,6 +1188,11 @@ def sample_distribution(dist_id, size, loc=None, scale=None, shape=None,
|
|
|
1121
1188
|
if dist_id in (0, 1):
|
|
1122
1189
|
return np.full(size, loc if loc is not None else 0.0)
|
|
1123
1190
|
|
|
1191
|
+
if dist_id == 13: # Reversed lognormal: loc=mean, scale=std, [min, max]
|
|
1192
|
+
rng = np.random.default_rng(random_state)
|
|
1193
|
+
return _reversed_lognormal_quantile(
|
|
1194
|
+
rng.uniform(size=size), loc, scale, minimum, maximum)
|
|
1195
|
+
|
|
1124
1196
|
dist = make_distribution(dist_id, loc=loc, scale=scale, shape=shape,
|
|
1125
1197
|
minimum=minimum, maximum=maximum)
|
|
1126
1198
|
|
|
@@ -1188,8 +1260,10 @@ def _has_uncertainty(cfg):
|
|
|
1188
1260
|
return _resolve_scale(cfg) > 0 or "dist_id" in cfg
|
|
1189
1261
|
|
|
1190
1262
|
|
|
1191
|
-
def _resolve_dist_params(cfg, default_loc=
|
|
1192
|
-
|
|
1263
|
+
def _resolve_dist_params(cfg, default_loc: float = 0.0,
|
|
1264
|
+
default_scale: float = 0.0,
|
|
1265
|
+
default_min: float = 0, default_max: float = 99999,
|
|
1266
|
+
default_id: int = 3):
|
|
1193
1267
|
"""
|
|
1194
1268
|
Extract ``(dist_id, loc, scale, shape, minimum, maximum)`` from a config dict.
|
|
1195
1269
|
|
|
@@ -1238,6 +1312,13 @@ def _resolve_dist_params(cfg, default_loc=0.0, default_scale=0.0,
|
|
|
1238
1312
|
where the rest of the distribution config isn't needed yet.
|
|
1239
1313
|
"""
|
|
1240
1314
|
dist_id = cfg.get("dist_id", default_id)
|
|
1315
|
+
if dist_id == 13 and "max" not in cfg and "maximum" not in cfg:
|
|
1316
|
+
# The cap is where the distribution is mirrored; a default one
|
|
1317
|
+
# (99999, or baseline + 2*std) would silently move every sample
|
|
1318
|
+
raise ValueError(
|
|
1319
|
+
"Reversed lognormal (dist_id 13) needs an explicit 'max': it "
|
|
1320
|
+
"is the cap the distribution is mirrored at."
|
|
1321
|
+
)
|
|
1241
1322
|
loc = cfg.get(
|
|
1242
1323
|
"loc", cfg.get("mean", cfg.get("price", cfg.get("rate", default_loc)))
|
|
1243
1324
|
)
|
|
@@ -1590,7 +1671,7 @@ def monte_carlo(plant,
|
|
|
1590
1671
|
num_samples: int = 100_000,
|
|
1591
1672
|
batch_size: int = 10_000,
|
|
1592
1673
|
additional_capex: bool = False,
|
|
1593
|
-
random_seed: int = None):
|
|
1674
|
+
random_seed: int | None = None):
|
|
1594
1675
|
"""
|
|
1595
1676
|
Run a Monte Carlo uncertainty simulation over a plant's financial metrics.
|
|
1596
1677
|
|
|
@@ -1740,9 +1821,11 @@ def monte_carlo(plant,
|
|
|
1740
1821
|
def cfg(name):
|
|
1741
1822
|
return op_cfg if name == "operator_hourly_rate" else pu.get(name, {})
|
|
1742
1823
|
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1824
|
+
def factor_dist(name):
|
|
1825
|
+
return _resolve_dist_params(
|
|
1826
|
+
cfg(name), default_loc=1, default_scale=0.3,
|
|
1827
|
+
default_min=0.25, default_max=1.75,
|
|
1828
|
+
)
|
|
1746
1829
|
# (name, dist params, sampled only when it configures uncertainty)
|
|
1747
1830
|
project_inputs = [
|
|
1748
1831
|
("plant_utilization", _window_dist_params(
|
|
@@ -1750,10 +1833,8 @@ def monte_carlo(plant,
|
|
|
1750
1833
|
floor=0.0, cap=1.0), True),
|
|
1751
1834
|
("tax_rate", _window_dist_params(
|
|
1752
1835
|
cfg("tax_rate"), plant.tax_rate, 0, floor=0.0, cap=1.0), True),
|
|
1753
|
-
("fixed_capital_factor",
|
|
1754
|
-
|
|
1755
|
-
("fixed_opex_factor", _resolve_dist_params(
|
|
1756
|
-
cfg("fixed_opex_factor"), **factor_window), False),
|
|
1836
|
+
("fixed_capital_factor", factor_dist("fixed_capital_factor"), False),
|
|
1837
|
+
("fixed_opex_factor", factor_dist("fixed_opex_factor"), False),
|
|
1757
1838
|
("operator_hourly_rate", _resolve_item_dist_params(
|
|
1758
1839
|
op_cfg, "rate", DEFAULT_OPERATOR_HOURLY_RATE, 10), False),
|
|
1759
1840
|
("project_lifetime", _window_dist_params(
|
openpytea/cli.py
CHANGED
|
@@ -13,12 +13,22 @@ from openpytea.io import run_equipment, run_plant, run_tea, run_openpytea
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
def _print_run_summary(results, output_dir):
|
|
16
|
-
ran = ", ".join(sorted(results)) or "(none)"
|
|
17
16
|
dest = (
|
|
18
17
|
output_dir
|
|
19
18
|
if output_dir is not None
|
|
20
19
|
else "the directory configured in the analysis file (default 'results')"
|
|
21
20
|
)
|
|
21
|
+
if "plants" in results: # a multi-plant project
|
|
22
|
+
for name, plant_results in results["plants"].items():
|
|
23
|
+
ran = ", ".join(sorted(plant_results)) or "calculations only"
|
|
24
|
+
print(f"{name}: ran {ran}")
|
|
25
|
+
compared = results.get("comparison")
|
|
26
|
+
if compared:
|
|
27
|
+
print(f"Compared across plants: {', '.join(sorted(compared))}")
|
|
28
|
+
print(f"Results written to: {dest} (one folder per plant"
|
|
29
|
+
f"{', plus comparison' if compared else ''})")
|
|
30
|
+
return
|
|
31
|
+
ran = ", ".join(sorted(results)) or "(none)"
|
|
22
32
|
print(f"Ran: {ran}")
|
|
23
33
|
print(f"Results written to: {dest}")
|
|
24
34
|
|