-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreliability.py
217 lines (173 loc) · 8.61 KB
/
reliability.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import pandas as pd
import numpy as np
import math
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import itertools
from scipy.stats import binom
class SystemAvailabilityScreener:
'''
Class defines the power system demands, unit reliability, unit outage hours.
Configures 4 sizing options (1 x 100%, 2 x 50%, 3 x 33%, 4 x 25%)
and 6 redundancy configurations (n+0, n+1, n+2, n+3, n+4, n+5).
Calculates per state reliability for each configuration with 0 units O/S nad 1 unit O/S and calculates system availability.
Returns 3 plots to communicate results.
'''
def __init__(self, demand, reliability, outage_hours):
'''
Instantiates the SystemAvailabilityScreener Class with system demand, reliability, and outage hours as inputs.
Creates data frames for sizing cases, results and a creates a list to store the final plots.
'''
self.electricity_demand = demand
self.unit_reliability = reliability
self.outage_hours = outage_hours
self.cases = pd.DataFrame()
self.results = pd.DataFrame()
self.availability_plots = []
self.define_cases()
self.calc_reliability()
self.get_tier_level()
def define_cases(self):
unit_sizes = range(1, 5) # 1, 2, 3, 4
redundancies = range(6) # 0 to 5
# Generate all combinations of unit sizes and redundancies
case_data = [
{
'unit_sizing_case': f'unit_size_{i}',
'demand_kW': self.electricity_demand,
'unit_capacity_kW': np.ceil(self.electricity_demand / i),
'units_reqd': i,
'units_installed': i + j,
'unit_reliability_pct': self.unit_reliability,
'unit_schedule_outage_hrs': self.outage_hours
}
for i, j in itertools.product(unit_sizes, redundancies)
]
self.cases = pd.DataFrame.from_records(case_data)
self.cases['system_capacity_kW'] = np.round(self.cases['unit_capacity_kW'] * self.cases['units_installed'], -2)
self.cases['redundant_units'] = self.cases['units_installed'] - self.cases['units_reqd']
def calc_reliability(self):
'''
Calculates the system-weighted reliability to meet system load,
accounting for periods with and without a unit outage.
'''
# Convert reliability percentage to probability
unit_reliability = self.cases["unit_reliability_pct"] / 100
outage_hours = self.cases["unit_schedule_outage_hrs"]
total_hours = 8760 # Total hours in a year
# Calculate availability when no units are in scheduled outage
avail_no_outage = 1 - binom.cdf(self.cases["units_reqd"] - 1,
self.cases["units_installed"],
unit_reliability)
# Calculate availability when one unit is in scheduled outage
avail_outage = 1 - binom.cdf(self.cases["units_reqd"] - 1,
self.cases["units_installed"] - 1,
unit_reliability)
# Compute system availability
outage_contribution = avail_outage * self.cases["units_installed"] * outage_hours
normal_contribution = avail_no_outage * (total_hours - self.cases["units_installed"] * outage_hours)
self.cases["system_availability"] = (outage_contribution + normal_contribution) / total_hours
def get_tier_level(self):
'''
Defines a tier level from Uptime Institute tier levels
reliability > 99.671: Tier 1
reliability > 99.741: Tier 2
reliability > 99.982: Tier 3
reliability > 99.995: Tier 4
returns Tier # in text format
'''
bins = [0, 99.671, 99.741, 99.982, 99.995, 100 ]
tiers = ["Tier_NA","Tier_1","Tier_2","Tier_3","Tier_4" ]
self.cases["tier_level"] = pd.cut(self.cases['system_availability']*100, bins=bins, labels=tiers, right=True)
def create_plots(self):
"""Creates an interactive scatter plot of system capacity vs. availability."""
# html_table = self.cases.to_html()
# self.availability_plots.append(html_table)
fig = px.scatter(
self.cases.sort_values("tier_level"), x='system_capacity_kW', y='system_availability',
color='tier_level',
title='Availability vs. Capacity and Uptime Institute Tier Levels',
labels={'system_capacity_kW': 'System Capacity (kW)', 'system_availability': 'System Availability (%)', 'tier_level': 'Tier Level'},
color_discrete_map={'Tier_NA': 'grey', 'Tier_1': 'orange', 'Tier_2': 'red', 'Tier_3': 'blue', 'Tier_4': 'green'}
)
# Add a vertical line for peak demand
fig.add_vline(
x=self.cases.demand_kW.mean(),
line=dict(color='black', dash='dash'),
annotation_text='Peak Demand kW',
annotation_position='top left'
)
fig.update_layout(width=800, height=600, plot_bgcolor="#DBE2EF")
plot = fig.to_html(full_html=False)
self.availability_plots.append(plot)
# create boxplots
fig = px.box(
self.cases.sort_values("tier_level"), x='tier_level', y='system_capacity_kW',
color='tier_level',
title='System Capacity Range by Tier Level',
labels={'tier_level': 'Tier Level', 'system_capacity_kW': 'System Capacity (kW)'},
color_discrete_map={'Tier_NA': 'grey', 'Tier_1': 'orange', 'Tier_2': 'red', 'Tier_3': 'blue', 'Tier_4': 'green'}
)
fig.update_layout(width=800, height=600, plot_bgcolor="#DBE2EF")
plot = fig.to_html(full_html=False)
self.availability_plots.append(plot)
# create barplots
# Define subplot titles
tiers = ['Tier_1', 'Tier_2', 'Tier_3', 'Tier_4']
fig = make_subplots(rows=2, cols=2, subplot_titles=tiers)
# Define custom color mapping
color_map = {
'unit_size_1': 'orange',
'unit_size_2': 'red',
'unit_size_3': 'blue',
'unit_size_4': 'green'
}
# Track which legends have been added to avoid repetition
added_legends = set()
# Iterate over tiers
for i, tier in enumerate(tiers):
filtered_df = self.cases[self.cases.tier_level == tier]
# Determine subplot row and column
row, col = divmod(i, 2)
# Iterate over unique "unit_sizing_case" categories
for engine, group in filtered_df.groupby("unit_sizing_case"):
show_legend = engine not in added_legends # Show legend only once
added_legends.add(engine) # Mark legend as added
fig.add_trace(
go.Bar(
x=group['units_installed'],
y=group['system_capacity_kW'],
name=engine, # Will be renamed later
marker=dict(color=color_map.get(engine, 'gray')),
width=0.3, # Adjust for uniform bar width
showlegend=show_legend # Show legend only once
),
row=row+1, col=col+1
)
# Update axis titles for each subplot
fig.update_xaxes(title_text="Units Installed", row=row+1, col=col+1)
fig.update_yaxes(title_text="System Capacity (kW)", row=row+1, col=col+1)
# Rename legend labels
legend_mapping = {
"unit_size_1": "100%",
"unit_size_2": "50%",
"unit_size_3": "33.3%",
"unit_size_4": "25%"
}
fig.for_each_trace(lambda t: t.update(name=legend_mapping.get(t.name, t.name)))
# Update layout
fig.update_layout(
width=800,
height=600,
title='System Capacity and Number of Units Required by Tier Level',
legend_title="Unit Capacity",
bargap=0.2, # Adjusts space between bars
bargroupgap=0.1, # Adjusts space between grouped bars
plot_bgcolor="#DBE2EF"
)
# Ensure consistent axis limits
fig.update_xaxes(range=[self.cases["units_installed"].min(), self.cases["units_installed"].max()])
fig.update_yaxes(range=[self.cases["system_capacity_kW"].min(), self.cases["system_capacity_kW"].max()])
plot = fig.to_html(full_html=False)
self.availability_plots.append(plot)