![TroglodyteDerivations's picture](https://cdn-avatars.huggingface.co/v1/production/uploads/65cd11573003e8e9050e8dc2/puOzi9hBVzd9s7oNV0rmL.jpeg)
Updated line 176 with: cs_area = st.number_input('Cross-Sectional Area',value=9.0e-4)
5a42d80
verified
import streamlit as st | |
import numpy as np | |
import matplotlib.pyplot as plt | |
class MultiLimbCoreModel: | |
def __init__(self, no_of_turns, cs_area, lengths, length_lg_2, mu_r, r, series_res): | |
self.no_of_turns = no_of_turns | |
self.cs_area = cs_area | |
self.lengths = lengths # Dictionary keys 'a', 'b', 'c', 'w' | |
self.length_lg_2 = length_lg_2 | |
self.mu_0 = 4 * np.pi * 1.0e-7 | |
self.mu_r = mu_r | |
self.mu = self.mu_0 * self.mu_r | |
self.r = r | |
self.series_res = series_res | |
self.flux_linkage = 0 | |
self.ind_current = 0 | |
self.t1 = 0 | |
self.dt = 1.0e-6 | |
# Data for plotting | |
self.simulation_times = [] | |
self.inductor_currents = [] | |
self.voltage_sources = [] | |
self.inductor_emfs = [] | |
self.inductor_flux_linkages = [] | |
self.indmodel_flux = [] | |
self.indmodel_flux2 = [] | |
self.indmodel_flux3 = [] | |
self.phi_values = [] | |
def calculate_resistances(self): | |
R1 = (2 * self.lengths['a'] + self.lengths['b'] + 2.0 * self.lengths['w'] - 2 * self.length_lg_2) / (self.mu * self.cs_area) | |
Rg1 = (2 * self.length_lg_2) / (self.mu_0 * self.cs_area) | |
R2 = (self.lengths['b'] + self.lengths['w']) / (self.mu * self.cs_area) | |
R3 = (2 * self.lengths['c'] + self.lengths['b'] + 2.0 * self.lengths['w'] - 2 * self.length_lg_2) / (self.mu * self.cs_area) | |
Rg2 = (self.length_lg_2) / (self.mu_0 * self.cs_area) | |
R_eq = R1 + Rg1 + (R2 * (R3 + Rg2) / (R2 + R3 + Rg2)) | |
return R_eq, R1, Rg1, R2, Rg2, R3 | |
def update(self, vmeas, t_clock): | |
if t_clock >= self.t1: | |
R_eq, R1, Rg1, R2, Rg2, R3 = self.calculate_resistances() | |
k1 = (vmeas - self.ind_current * self.r) | |
k2 = (vmeas - (self.ind_current + self.dt * k1 / 2.0) * self.r) | |
k3 = (vmeas - (self.ind_current + self.dt * k2 / 2.0) * self.r) | |
k4 = (vmeas - (self.ind_current + self.dt * k3) * self.r) | |
k = (k1 + 2 * k2 + 2 * k3 + k4) * self.dt / 6.0 | |
self.flux_linkage += k | |
flux = self.flux_linkage / self.no_of_turns | |
self.ind_current = flux * R_eq / self.no_of_turns | |
phi = self.no_of_turns * self.ind_current / R_eq | |
vsrc = vmeas - self.ind_current * self.series_res | |
indmodel_emf = vmeas - self.ind_current * self.r | |
# Multi Core Limbs | |
indmodel_flux = flux | |
indmodel_flux2 = flux * (R2 * (R3 + Rg2) / (R2 + R3 + Rg2)) / R2 | |
indmodel_flux3 = flux * (R2 * (R3 + Rg2) / (R2 + R3 + Rg2)) / R3 | |
# Store data for plotting | |
self.simulation_times.append(t_clock) | |
self.inductor_currents.append(self.ind_current) | |
self.voltage_sources.append(vsrc) | |
self.inductor_emfs.append(indmodel_emf) | |
self.inductor_flux_linkages.append(self.flux_linkage) | |
self.indmodel_flux.append(indmodel_flux) # Limb 1 | |
self.indmodel_flux2.append(indmodel_flux2) # Limb 2 | |
self.indmodel_flux3.append(indmodel_flux3) # Limb 3 | |
self.phi_values.append(phi) | |
self.t1 += self.dt | |
def plot_reluctances_of_each_limb(self): | |
fig, ax = plt.subplots(figsize=(12, 8)) | |
ax.plot(self.simulation_times, self.indmodel_flux, label='Limb 1 Flux') | |
ax.plot(self.simulation_times, self.indmodel_flux2, label='Limb 2 Flux') | |
ax.plot(self.simulation_times, self.indmodel_flux3, label='Limb 3 Flux') | |
ax.set_xlabel('Time (s)') | |
ax.set_ylabel('Flux (Weber)') | |
ax.legend() | |
ax.set_title('Flux in Each Limb Over Time') | |
st.pyplot(fig) | |
def plot_reluctances_of_each_limb_log_base_10(self): | |
fig, ax = plt.subplots(figsize=(12, 8)) | |
ax.plot(self.simulation_times, self.indmodel_flux, label='Limb 1 Flux') | |
ax.plot(self.simulation_times, self.indmodel_flux2, label='Limb 2 Flux') | |
ax.plot(self.simulation_times, self.indmodel_flux3, label='Limb 3 Flux') | |
ax.set_xlabel('Time (s)') | |
ax.set_ylabel('Flux (Weber)') | |
ax.set_yscale('log', base=10) | |
ax.legend() | |
ax.set_title('Flux in Each Limb Over Time (Log-Scale)') | |
st.pyplot(fig) | |
def plot_inductor_currents(self): | |
fig, ax = plt.subplots(figsize=(12, 8)) | |
ax.plot(self.simulation_times, self.inductor_currents, label='Inductor Current') | |
ax.set_xlabel('Time (s)') | |
ax.set_ylabel('Current (Amperes)') | |
ax.legend() | |
ax.set_title('Inductor Current Over Time') | |
st.pyplot(fig) | |
def plot_inductor_currents_log_base_10(self): | |
fig, ax = plt.subplots(figsize=(12, 8)) | |
ax.plot(self.simulation_times, self.inductor_currents, label='Inductor Current') | |
ax.set_xlabel('Time (s)') | |
ax.set_ylabel('Current (Amperes)') | |
ax.set_yscale('log', base=10) | |
ax.legend() | |
ax.set_title('Inductor Current Over Time (Log-Scale)') | |
st.pyplot(fig) | |
def plot_results(self): | |
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8)) | |
ax1.plot(self.simulation_times, self.inductor_flux_linkages, label='Flux Linkage') | |
ax1.set_xlabel('Time (s)') | |
ax1.set_ylabel('Flux Linkage') | |
ax1.legend() | |
ax1.set_title('Flux Linkage Over Time') | |
ax2.plot(self.simulation_times, self.inductor_emfs, label='Induced EMF', color='red') | |
ax2.set_xlabel('Time (s)') | |
ax2.set_ylabel('EMF') | |
ax2.legend() | |
ax2.set_title('Induced EMF Over Time') | |
plt.tight_layout() | |
st.pyplot(fig) | |
def plot_results_log_base_10(self): | |
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8)) | |
ax1.plot(self.simulation_times, self.inductor_flux_linkages, label='Flux Linkage') | |
ax1.set_xlabel('Time (s)') | |
ax1.set_ylabel('Flux Linkage') | |
ax1.set_yscale('log', base=10) | |
ax1.legend() | |
ax1.set_title('Flux Linkage Over Time (Log-Scale)') | |
ax2.plot(self.simulation_times, self.inductor_emfs, label='Induced EMF', color='red') | |
ax2.set_xlabel('Time (s)') | |
ax2.set_ylabel('EMF') | |
ax2.set_xscale('log', base=10) | |
ax2.legend() | |
ax2.set_title('Induced EMF Over Time (Log-Scale)') | |
plt.tight_layout() | |
st.pyplot(fig) | |
def plot_phi_values(self): | |
fig, ax = plt.subplots(figsize=(12, 8)) | |
ax.plot(self.simulation_times, self.phi_values, label='Phi Values') | |
ax.set_xlabel('Time (s)') | |
ax.set_ylabel('Phi (Weber)') | |
ax.legend() | |
ax.set_title('Phi Values Over Time') | |
st.pyplot(fig) | |
def plot_phi_values_log_base_10(self): | |
fig, ax = plt.subplots(figsize=(12, 8)) | |
ax.plot(self.simulation_times, self.phi_values, label='Phi Values') | |
ax.set_xlabel('Time (s)') | |
ax.set_ylabel('Phi (Weber)') | |
ax.set_yscale('log', base=10) | |
ax.legend() | |
ax.set_title('Phi Values Over Time (Log-Scale') | |
st.pyplot(fig) | |
# Streamlit App | |
def main(): | |
st.title('Multi-Limb Core Model Simulation') | |
# Input parameters | |
no_of_turns = st.number_input('Number of Turns',min_value=125, max_value=777, value=500) | |
cs_area = st.number_input('Cross-Sectional Area',value=9.0e-4) | |
lengths = { | |
'a': st.number_input('Length a', min_value=1.0e-2, max_value=25.0e-2, value=5.0e-2), | |
'b': st.number_input('Length b', min_value=1.125e-2, max_value=28.8e-2,value=4.5e-2), | |
'c': st.number_input('Length c', min_value=0.50e-2, max_value=88.4e-2, value=4.0e-2), | |
'w': st.number_input('Length w', min_value=0.625e-2, max_value=100e-2,value=3.0e-2) | |
} | |
length_lg_2 = st.number_input('Length lg_2', value=0.1e-3) | |
mu_r = st.number_input('Relative Permeability', min_value=1000.0, max_value=1000.0, value=1000.0) | |
r = st.number_input('Resistance', min_value=0.01, max_value=0.99, value=0.01) | |
series_res = st.number_input('Series Resistance',min_value=2, max_value=22, value=10) | |
vmeas = st.number_input('Voltage Measurement', min_value=5, max_value=100,value=100) | |
simulation_time = st.number_input('Simulation Time', min_value=0.125, max_value=0.625,value=0.125) | |
if st.button('Run Simulation'): | |
model = MultiLimbCoreModel(no_of_turns, cs_area, lengths, length_lg_2, mu_r, r, series_res) | |
t_clock = 0 | |
while t_clock < simulation_time: | |
model.update(vmeas, t_clock) | |
t_clock += model.dt | |
st.subheader('Results') | |
model.plot_reluctances_of_each_limb() | |
model.plot_reluctances_of_each_limb_log_base_10() | |
model.plot_inductor_currents() | |
model.plot_inductor_currents_log_base_10() | |
model.plot_results() | |
model.plot_results_log_base_10() | |
model.plot_phi_values() | |
model.plot_phi_values_log_base_10() | |
if __name__ == '__main__': | |
main() |