-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathexample_diode.py
More file actions
78 lines (59 loc) · 2.34 KB
/
example_diode.py
File metadata and controls
78 lines (59 loc) · 2.34 KB
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
#########################################################################################
##
## PathSim example of a diode circuit
## (Nonlinear algebraic loop)
##
#########################################################################################
# IMPORTS ===============================================================================
import numpy as np
import matplotlib.pyplot as plt
from pathsim import Simulation, Connection
from pathsim.blocks import (
Source,
Amplifier,
Function,
Adder,
Scope
)
# DIODE CIRCUIT =========================================================================
# Circuit parameters
R = 1000.0 # Resistor (Ohms)
I_s = 1e-12 # Diode saturation current (A)
V_T = 0.026 # Thermal voltage at room temperature (V)
# Define diode current function: i = I_s * (exp(v_diode/(n*V_T)) - 1)
def diode_current(v_diode):
"""Diode current as function of diode voltage"""
clipped = np.clip(v_diode/V_T, None, 23)
return I_s * (np.exp(clipped) - 1)
# Define voltage source function
def voltage_source(t):
"""Sinusoidal voltage source"""
return 5.0 * np.sin(2 * np.pi * t)
# Blocks that define the system
Src = Source(voltage_source) # Voltage source
DiodeFn = Function(diode_current) # Diode i-v characteristic
ResAmp = Amplifier(-R) # -R (negative resistance)
Add = Adder() # Adder for KVL
Sc1 = Scope(labels=["v_source", "v_diode"])
Sc2 = Scope(labels=["i_diode"])
blocks = [Src, DiodeFn, ResAmp, Add, Sc1, Sc2]
connections = [
Connection(Src, Add[0], Sc1[0]), # Source to adder and scope
Connection(Add, DiodeFn, Sc1[1]), # Diode voltage to function and scope
Connection(DiodeFn, ResAmp, Sc2), # Diode current to resistor and scope
Connection(ResAmp, Add[1]), # Voltage drop back to adder (algebraic loop)
]
# Simulation instance
Sim = Simulation(
blocks,
connections,
dt=0.01,
tolerance_fpi=1e-12
)
# Run Example ===========================================================================
if __name__ == "__main__":
# Run the simulation for 2 seconds
Sim.run(duration=2.0)
# Plot the results
Sim.plot()
plt.show()