Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,167 +1,192 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.set_page_config(page_title="
|
| 7 |
-
st.title("⚡ Electrical Engineering
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
st.sidebar.
|
| 11 |
-
section = st.sidebar.radio("
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
st.header("🛠️ Power Distribution Calculations")
|
| 24 |
-
# (Will add detailed code next)
|
| 25 |
-
|
| 26 |
-
def grid_requirement_page():
|
| 27 |
-
st.header("🌎 Grid Requirement Calculator")
|
| 28 |
-
# (Area converter + grid size calculator)
|
| 29 |
-
|
| 30 |
-
def nepra_docs_page():
|
| 31 |
-
st.header("📚 NEPRA Codes and Documents")
|
| 32 |
-
uploaded_file = st.file_uploader("Upload NEPRA PDF Document", type=["pdf"])
|
| 33 |
-
if uploaded_file:
|
| 34 |
-
st.success("File Uploaded Successfully!")
|
| 35 |
-
with st.expander("📖 View Document"):
|
| 36 |
-
st.download_button("Download NEPRA Document", uploaded_file, file_name="NEPRA_Document.pdf")
|
| 37 |
-
# Future: Inline PDF preview (with external libraries if needed)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
if section == "Generation":
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
elif section == "Distribution":
|
| 45 |
-
distribution_page()
|
| 46 |
-
elif section == "Grid Requirements":
|
| 47 |
-
grid_requirement_page()
|
| 48 |
-
elif section == "NEPRA Docs":
|
| 49 |
-
nepra_docs_page()
|
| 50 |
-
def area_conversion_calculator():
|
| 51 |
-
st.subheader("🌎 Area Conversion and Grid MW Calculation")
|
| 52 |
-
|
| 53 |
-
col1, col2 = st.columns(2)
|
| 54 |
-
|
| 55 |
-
with col1:
|
| 56 |
-
area_value = st.slider("Enter Area Value:", 1.0, 5000.0, step=1.0)
|
| 57 |
-
area_unit = st.selectbox("Select Unit", ["Acres", "Km²", "m²"])
|
| 58 |
-
|
| 59 |
-
with col2:
|
| 60 |
-
mw_density = st.slider("MW per km² (Standard: 50MW/km²)", 10.0, 100.0, 50.0)
|
| 61 |
-
|
| 62 |
-
# Conversion
|
| 63 |
-
def area_conversion(area_value, from_unit):
|
| 64 |
-
if from_unit == "Acres":
|
| 65 |
-
m2 = area_value * 4046.86
|
| 66 |
-
elif from_unit == "Km²":
|
| 67 |
-
m2 = area_value * 1_000_000
|
| 68 |
-
else: # m²
|
| 69 |
-
m2 = area_value
|
| 70 |
-
acres = m2 / 4046.86
|
| 71 |
-
km2 = m2 / 1_000_000
|
| 72 |
-
return m2, acres, km2
|
| 73 |
-
|
| 74 |
-
m2, acres, km2 = area_conversion(area_value, area_unit)
|
| 75 |
-
|
| 76 |
-
# Grid Requirements
|
| 77 |
-
required_mw = km2 * mw_density
|
| 78 |
-
|
| 79 |
-
# Output nicely
|
| 80 |
-
st.success(f"Converted Area:")
|
| 81 |
-
st.metric(label="Square Meters (m²)", value=f"{m2:,.2f}")
|
| 82 |
-
st.metric(label="Acres", value=f"{acres:,.2f}")
|
| 83 |
-
st.metric(label="Square Kilometers (km²)", value=f"{km2:,.4f}")
|
| 84 |
-
|
| 85 |
-
st.info(f"🔋 Based on {mw_density:.1f} MW/km², you would require **{required_mw:.2f} MW** for this area.")
|
| 86 |
-
|
| 87 |
-
st.caption("⚙️ Formula: Required MW = Area (km²) × MW Density (MW/km²)")
|
| 88 |
-
|
| 89 |
-
# Drop this inside `grid_requirement_page()`:
|
| 90 |
-
def grid_requirement_page():
|
| 91 |
-
st.header("🌎 Grid Requirement Calculator")
|
| 92 |
-
area_conversion_calculator()
|
| 93 |
-
def set_bg_color():
|
| 94 |
-
st.markdown("""
|
| 95 |
-
<style>
|
| 96 |
-
body {
|
| 97 |
-
background-color: #f0f8ff; /* Light blue */
|
| 98 |
-
}
|
| 99 |
-
.stButton>button {
|
| 100 |
-
color: white;
|
| 101 |
-
background-color: #4CAF50;
|
| 102 |
-
}
|
| 103 |
-
.stSlider>div>div {
|
| 104 |
-
background: linear-gradient(to right, #ff7e5f, #feb47b);
|
| 105 |
-
}
|
| 106 |
-
</style>
|
| 107 |
-
""", unsafe_allow_html=True)
|
| 108 |
-
area_value = st.slider("🌎 Enter Area Size:", 1.0, 5000.0, 100.0, step=1.0)
|
| 109 |
-
mw_density = st.slider("⚡ MW Density per km²:", 10.0, 100.0, 50.0)
|
| 110 |
-
st.metric("Area (km²)", f"{km2:.4f} km²", delta=f"{acres:.2f} acres")
|
| 111 |
-
st.metric("Required Power", f"{required_mw:.2f} MW")
|
| 112 |
-
with st.expander("📖 See Full Formulas"):
|
| 113 |
-
st.code("""
|
| 114 |
-
MW Required = Area (km²) × MW per km²
|
| 115 |
-
Area (km²) = Area (m²) / 1,000,000
|
| 116 |
-
Area (acres) = Area (m²) / 4046.86
|
| 117 |
-
""")
|
| 118 |
-
def generation_page():
|
| 119 |
-
st.header("⚡ Power Generation Calculations")
|
| 120 |
-
|
| 121 |
-
calc_type = st.selectbox("Select Calculation", [
|
| 122 |
-
"Load Factor Calculation",
|
| 123 |
"Plant Capacity Factor",
|
| 124 |
"Cost per MWh",
|
| 125 |
-
"
|
| 126 |
])
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
st.write("""
|
| 130 |
-
- Load Factor = Average Load / Peak Load
|
| 131 |
-
- Plant Capacity Factor = Actual Output / Max Possible Output
|
| 132 |
-
- Cost per MWh = Total Cost / Energy Generated
|
| 133 |
-
""")
|
| 134 |
-
|
| 135 |
-
if calc_type == "Load Factor Calculation":
|
| 136 |
avg_load = st.number_input("Average Load (MW)", min_value=0.0)
|
| 137 |
peak_load = st.number_input("Peak Load (MW)", min_value=0.1)
|
| 138 |
-
if st.button("
|
| 139 |
load_factor = avg_load / peak_load
|
| 140 |
-
st.success(f"Load Factor
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
elif input_unit == "m²":
|
| 153 |
-
m2 = area_input
|
| 154 |
-
elif input_unit == "km²":
|
| 155 |
-
m2 = area_input * M2_IN_KM2
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
#
|
| 165 |
-
|
| 166 |
-
st.metric(label="Area (acres)", value=f"{acres:.2f} acres")
|
| 167 |
-
st.metric(label="Required MW", value=f"{required_mw:.2f} MW")
|
|
|
|
| 1 |
+
# Electrical Calculations Software
|
| 2 |
+
# Covering generation, transmission, distribution, load flow, short circuit, cable sizing, transformers, power factor, harmonics
|
| 3 |
+
|
| 4 |
import streamlit as st
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
+
# Page config
|
| 8 |
+
st.set_page_config(page_title="Comprehensive Electrical Engineering Calculator", layout="wide")
|
| 9 |
+
st.title("⚡ Electrical Engineering Mega Calculator")
|
| 10 |
+
|
| 11 |
+
# Sidebar Navigation
|
| 12 |
+
st.sidebar.title("Navigation")
|
| 13 |
+
section = st.sidebar.radio("Select Section", [
|
| 14 |
+
"Generation",
|
| 15 |
+
"Transmission",
|
| 16 |
+
"Distribution",
|
| 17 |
+
"Power Quality",
|
| 18 |
+
"Miscellaneous",
|
| 19 |
+
"Standards & Codes"
|
| 20 |
+
])
|
| 21 |
+
|
| 22 |
+
# Constants for unit conversions
|
| 23 |
+
M2_IN_ACRE = 4046.86
|
| 24 |
+
M2_IN_KM2 = 1_000_000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Generation Section
|
| 27 |
if section == "Generation":
|
| 28 |
+
st.header("🔋 Power Generation Calculations")
|
| 29 |
+
gen_calc = st.selectbox("Choose Calculation", [
|
| 30 |
+
"Load Factor",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"Plant Capacity Factor",
|
| 32 |
"Cost per MWh",
|
| 33 |
+
"Energy Generation"
|
| 34 |
])
|
| 35 |
+
|
| 36 |
+
if gen_calc == "Load Factor":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
avg_load = st.number_input("Average Load (MW)", min_value=0.0)
|
| 38 |
peak_load = st.number_input("Peak Load (MW)", min_value=0.1)
|
| 39 |
+
if st.button("Calculate Load Factor"):
|
| 40 |
load_factor = avg_load / peak_load
|
| 41 |
+
st.success(f"Load Factor = {load_factor:.2f} ({load_factor*100:.2f}%)")
|
| 42 |
+
|
| 43 |
+
elif gen_calc == "Plant Capacity Factor":
|
| 44 |
+
actual_output = st.number_input("Actual Energy Output (MWh)", min_value=0.0)
|
| 45 |
+
max_output = st.number_input("Maximum Possible Output (MWh)", min_value=0.1)
|
| 46 |
+
if st.button("Calculate Plant Capacity Factor"):
|
| 47 |
+
pcf = actual_output / max_output
|
| 48 |
+
st.success(f"Plant Capacity Factor = {pcf:.2f} ({pcf*100:.2f}%)")
|
| 49 |
+
|
| 50 |
+
elif gen_calc == "Cost per MWh":
|
| 51 |
+
total_cost = st.number_input("Total Generation Cost (USD)", min_value=0.0)
|
| 52 |
+
energy_generated = st.number_input("Energy Generated (MWh)", min_value=0.1)
|
| 53 |
+
if st.button("Calculate Cost/MWh"):
|
| 54 |
+
cost_mwh = total_cost / energy_generated
|
| 55 |
+
st.success(f"Cost per MWh = ${cost_mwh:.2f}")
|
| 56 |
+
|
| 57 |
+
elif gen_calc == "Energy Generation":
|
| 58 |
+
capacity_mw = st.number_input("Installed Capacity (MW)", min_value=0.0)
|
| 59 |
+
operating_hours = st.number_input("Operating Hours", min_value=0.0)
|
| 60 |
+
if st.button("Calculate Total Energy"):
|
| 61 |
+
total_energy = capacity_mw * operating_hours
|
| 62 |
+
st.success(f"Total Energy Generated = {total_energy:.2f} MWh")
|
| 63 |
+
|
| 64 |
+
# Transmission Section
|
| 65 |
+
elif section == "Transmission":
|
| 66 |
+
st.header("🚧 Transmission System Calculations")
|
| 67 |
+
trans_calc = st.selectbox("Choose Calculation", [
|
| 68 |
+
"Impedance Calculation",
|
| 69 |
+
"Short Circuit Current",
|
| 70 |
+
"Grid Requirement"
|
| 71 |
+
])
|
| 72 |
|
| 73 |
+
if trans_calc == "Impedance Calculation":
|
| 74 |
+
resistance = st.number_input("Resistance (Ohms)", min_value=0.0)
|
| 75 |
+
reactance = st.number_input("Reactance (Ohms)", min_value=0.0)
|
| 76 |
+
if st.button("Calculate Impedance"):
|
| 77 |
+
impedance = np.sqrt(resistance**2 + reactance**2)
|
| 78 |
+
st.success(f"Impedance = {impedance:.4f} Ω")
|
| 79 |
+
|
| 80 |
+
elif trans_calc == "Short Circuit Current":
|
| 81 |
+
voltage = st.number_input("System Voltage (V)", min_value=0.1)
|
| 82 |
+
impedance = st.number_input("Impedance (Ohms)", min_value=0.01)
|
| 83 |
+
if st.button("Calculate Isc"):
|
| 84 |
+
isc = voltage / (np.sqrt(3) * impedance)
|
| 85 |
+
st.success(f"Short Circuit Current = {isc:.2f} A")
|
| 86 |
+
|
| 87 |
+
elif trans_calc == "Grid Requirement":
|
| 88 |
+
area_unit = st.selectbox("Area Unit", ["Acres", "m²", "km²"])
|
| 89 |
+
area_value = st.number_input(f"Area ({area_unit})", min_value=0.1)
|
| 90 |
+
density = st.slider("Power Density (MW/km²)", 5.0, 100.0, 30.0)
|
| 91 |
+
|
| 92 |
+
if area_unit == "Acres":
|
| 93 |
+
m2 = area_value * M2_IN_ACRE
|
| 94 |
+
elif area_unit == "m²":
|
| 95 |
+
m2 = area_value
|
| 96 |
+
elif area_unit == "km²":
|
| 97 |
+
m2 = area_value * M2_IN_KM2
|
| 98 |
|
| 99 |
+
km2 = m2 / M2_IN_KM2
|
| 100 |
+
required_mw = km2 * density
|
| 101 |
|
| 102 |
+
st.metric("Area (km²)", f"{km2:.2f}")
|
| 103 |
+
st.metric("Required MW", f"{required_mw:.2f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# Distribution Section
|
| 106 |
+
elif section == "Distribution":
|
| 107 |
+
st.header("🏙️ Distribution System Calculations")
|
| 108 |
+
dist_calc = st.selectbox("Choose Calculation", [
|
| 109 |
+
"Cable Sizing",
|
| 110 |
+
"Voltage Drop",
|
| 111 |
+
"Transformer Sizing"
|
| 112 |
+
])
|
| 113 |
|
| 114 |
+
if dist_calc == "Cable Sizing":
|
| 115 |
+
current = st.number_input("Current (A)", min_value=0.0)
|
| 116 |
+
density = st.number_input("Permissible Current Density (A/mm²)", min_value=0.01)
|
| 117 |
+
if st.button("Calculate Cable Size"):
|
| 118 |
+
cable_area = current / density
|
| 119 |
+
st.success(f"Required Cable Area = {cable_area:.2f} mm²")
|
| 120 |
+
|
| 121 |
+
elif dist_calc == "Voltage Drop":
|
| 122 |
+
resistance = st.number_input("Resistance per phase (Ohms)", min_value=0.0)
|
| 123 |
+
current = st.number_input("Current (A)", min_value=0.0)
|
| 124 |
+
system_voltage = st.number_input("System Voltage (V)", min_value=0.1)
|
| 125 |
+
if st.button("Calculate Voltage Drop"):
|
| 126 |
+
vd = 2 * resistance * current
|
| 127 |
+
percentage = (vd / system_voltage) * 100
|
| 128 |
+
st.success(f"Voltage Drop = {vd:.2f} V ({percentage:.2f}%)")
|
| 129 |
+
|
| 130 |
+
elif dist_calc == "Transformer Sizing":
|
| 131 |
+
connected_load = st.number_input("Connected Load (kW)", min_value=0.0)
|
| 132 |
+
power_factor = st.number_input("Power Factor", min_value=0.1, max_value=1.0)
|
| 133 |
+
if st.button("Calculate Transformer Size"):
|
| 134 |
+
size = connected_load / power_factor
|
| 135 |
+
st.success(f"Transformer Size = {size:.2f} kVA")
|
| 136 |
+
|
| 137 |
+
# Power Quality Section
|
| 138 |
+
elif section == "Power Quality":
|
| 139 |
+
st.header("🎯 Power Quality Calculations")
|
| 140 |
+
quality_calc = st.selectbox("Choose Calculation", [
|
| 141 |
+
"Power Factor Correction",
|
| 142 |
+
"Harmonic Frequency"
|
| 143 |
+
])
|
| 144 |
|
| 145 |
+
if quality_calc == "Power Factor Correction":
|
| 146 |
+
existing_pf = st.number_input("Existing Power Factor", min_value=0.1, max_value=1.0)
|
| 147 |
+
desired_pf = st.number_input("Desired Power Factor", min_value=0.1, max_value=1.0)
|
| 148 |
+
load_kw = st.number_input("Load (kW)", min_value=0.0)
|
| 149 |
+
if st.button("Calculate Required kVAR"):
|
| 150 |
+
kvar_needed = load_kw * (np.tan(np.arccos(existing_pf)) - np.tan(np.arccos(desired_pf)))
|
| 151 |
+
st.success(f"Reactive Power (kVAR) needed = {kvar_needed:.2f}")
|
| 152 |
+
|
| 153 |
+
elif quality_calc == "Harmonic Frequency":
|
| 154 |
+
base_freq = st.number_input("Base Frequency (Hz)", value=50)
|
| 155 |
+
harmonic_order = st.number_input("Harmonic Order", value=3)
|
| 156 |
+
if st.button("Calculate Harmonic Frequency"):
|
| 157 |
+
harmonic_freq = base_freq * harmonic_order
|
| 158 |
+
st.success(f"Harmonic Frequency = {harmonic_freq:.2f} Hz")
|
| 159 |
+
|
| 160 |
+
# Miscellaneous Section
|
| 161 |
+
elif section == "Miscellaneous":
|
| 162 |
+
st.header("📈 Other Useful Calculations")
|
| 163 |
+
misc_calc = st.selectbox("Choose Calculation", [
|
| 164 |
+
"Apparent Power",
|
| 165 |
+
"Resonant Frequency"
|
| 166 |
+
])
|
| 167 |
+
|
| 168 |
+
if misc_calc == "Apparent Power":
|
| 169 |
+
real_power = st.number_input("Real Power (W)", min_value=0.0)
|
| 170 |
+
reactive_power = st.number_input("Reactive Power (VAR)", min_value=0.0)
|
| 171 |
+
if st.button("Calculate Apparent Power"):
|
| 172 |
+
apparent = np.sqrt(real_power**2 + reactive_power**2)
|
| 173 |
+
st.success(f"Apparent Power = {apparent:.2f} VA")
|
| 174 |
+
|
| 175 |
+
elif misc_calc == "Resonant Frequency":
|
| 176 |
+
L = st.number_input("Inductance (H)", min_value=0.000001)
|
| 177 |
+
C = st.number_input("Capacitance (F)", min_value=0.000001)
|
| 178 |
+
if st.button("Calculate Resonant Frequency"):
|
| 179 |
+
freq = 1 / (2 * np.pi * np.sqrt(L * C))
|
| 180 |
+
st.success(f"Resonant Frequency = {freq:.2f} Hz")
|
| 181 |
+
|
| 182 |
+
# Standards and Codes Section
|
| 183 |
+
elif section == "Standards & Codes":
|
| 184 |
+
st.header("📚 Electrical Standards and Documents")
|
| 185 |
+
st.info("Upload NEPRA codes, IEEE standards, or IEC guidelines for reference.")
|
| 186 |
+
uploaded_file = st.file_uploader("Upload Document", type=["pdf", "docx", "txt"])
|
| 187 |
+
if uploaded_file:
|
| 188 |
+
st.success(f"Uploaded file: {uploaded_file.name}")
|
| 189 |
+
st.download_button("Download File", uploaded_file, file_name=uploaded_file.name)
|
| 190 |
|
| 191 |
+
# End of Code
|
| 192 |
+
|
|
|
|
|
|