import numpy as np
# Global Constants
V = 0 # Voltage in Volts
I = 0 # Current in Amperes
R = 0 # Resistance in Ohms
# Define the Circuit Elements
class Resistor:
def __init__(self, resistance):
self.resistance = resistance
def __str__(self):
return f"Resistor {self.resistance} Ohms"
def get_resistance(self):
return self.resistance
class Capacitor:
def __init__(self, capacitance):
self.capacitance = capacitance
def __str__(self):
return f"Capacitor {self.capacitance} Farads"
def get_capacitance(self):
return self.capacitance
class Inductor:
def __init__(self, inductance):
self.inductance = inductance
def __str__(self):
return f"Inductor {self.inductance} Henries"
def get_inductance(self):
return self.inductance
# Network class for multiple components connected together
class Circuit:
def __init__(self):
self.components = [] # List to hold components like resistors, capacitors, etc.
def add_component(self, component):
self.components.append(component)
def __str__(self):
return " -> ".join(str(comp) for comp in self.components)
# Function to build a resistive circuit and analyze it using Nodal Analysis
def build_resistive_circuit():
# Create some resistors
r1 = Resistor(100)
r2 = Resistor(200)
r3 = Resistor(300)
# Create a circuit and add resistors
circuit = Circuit()
circuit.add_component(r1)
circuit.add_component(r2)
circuit.add_component(r3)
# Print the circuit
print(f"Circuit: {circuit}")
# Perform Nodal Analysis or another circuit analysis method
# For simplicity, this part can be extended to include more advanced operations
# Placeholder for Nodal Analysis method:
solve_nodal_analysis(circuit)
# Placeholder for Nodal Analysis method (using Kirchhoff's Current Law)
def solve_nodal_analysis(circuit):
# This would normally involve solving a system of linear equations
# For simplicity, let's just assume some voltages are known (we'll add complexity
later)
print("Performing Nodal Analysis (Placeholder)...")
voltages = np.array([5, 10, 15]) # Just an example of known voltages for nodes
currents = np.array([0.05, 0.1, 0.15]) # Example currents for nodes
print("Nodal Voltages: ", voltages)
print("Nodal Currents: ", currents)
# You would typically solve a matrix equation here for the real circuit analysis
# Frequency Response (for AC analysis)
def calculate_frequency_response(circuit, frequency):
# Placeholder for AC analysis
print(f"Calculating Frequency Response at {frequency}Hz for the given circuit.")
# Implement actual frequency response calculation for AC circuits here
# Transient Analysis (for RC, RL, and RLC circuits)
def perform_transient_analysis(circuit, time_interval):
print(f"Performing Transient Analysis for the circuit over {time_interval}
seconds.")
# Actual transient analysis logic would go here (e.g., solving differential
equations)
# Main driver function to simulate the circuits
def main():
print("Welcome to the Electric Circuit Simulator!")
print("Building a resistive circuit and solving using Nodal Analysis...")
# Build and solve a basic resistive circuit
build_resistive_circuit()
# Perform AC analysis for frequency response (Placeholder)
frequency = 50 # Example frequency in Hz
calculate_frequency_response(None, frequency)
# Perform transient analysis (Placeholder)
time_interval = 10 # Example time in seconds
perform_transient_analysis(None, time_interval)
# Run the simulation
if __name__ == "__main__":
main()