Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions src/Electrical/Analog/controlled_sources.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# Controlled Sources: VCVS, VCCS, CCVS, CCCS
# SPICE-equivalent controlled source elements for circuit analysis

"""
VCVS(; name, G = 1.0)

Voltage Controlled Voltage Source (SPICE E-element).

A four-terminal device where the output voltage is proportional to the input voltage.
The input port has infinite impedance (no current flows into the sensing terminals).

# Parameters
- `G`: [V/V] Voltage gain (default: 1.0). Can be negative for inverting behavior.

# Connectors
- `p1` Positive input (sensing) terminal
- `n1` Negative input (sensing) terminal
- `p2` Positive output terminal
- `n2` Negative output terminal

# Equations
```
v2 = G × v1
i1 = 0 (infinite input impedance)
```

# Example
```julia
@named vcvs = VCVS(G = 10.0) # Voltage amplifier with gain 10
```
"""
@component function VCVS(; name, G = 1.0)
@named twoport = TwoPort()
@unpack v1, v2, i1, i2 = twoport

pars = @parameters begin
G = G, [description = "Voltage gain [V/V]"]
end

eqs = Equation[
v2 ~ G * v1,
i1 ~ 0,
]

sys = System(eqs, t, [], pars; name, systems = [])
return extend(sys, twoport)
end

"""
VCCS(; name, Gm = 0.001)

Voltage Controlled Current Source (SPICE G-element).

A four-terminal device where the output current is proportional to the input voltage.
The input port has infinite impedance (no current flows into the sensing terminals).

# Parameters
- `Gm`: [A/V or S] Transconductance (default: 0.001 S = 1 mS)

# Connectors
- `p1` Positive input (sensing) terminal
- `n1` Negative input (sensing) terminal
- `p2` Positive output terminal
- `n2` Negative output terminal

# Equations
```
i2 = Gm × v1
i1 = 0 (infinite input impedance)
```

# Example
```julia
@named vccs = VCCS(Gm = 0.01) # Transconductance amplifier with Gm = 10 mS
```
"""
@component function VCCS(; name, Gm = 0.001)
@named twoport = TwoPort()
@unpack v1, v2, i1, i2 = twoport

pars = @parameters begin
Gm = Gm, [description = "Transconductance [A/V]"]
end

eqs = Equation[
i2 ~ Gm * v1,
i1 ~ 0,
]

sys = System(eqs, t, [], pars; name, systems = [])
return extend(sys, twoport)
end

"""
CCVS(; name, Rm = 1000.0)

Current Controlled Voltage Source (SPICE H-element).

A four-terminal device where the output voltage is proportional to the input current.
The input port has zero impedance (no voltage drop across the sensing terminals).
The controlling current is measured as current flowing from p1 to n1 (through the input port).

# Parameters
- `Rm`: [V/A or Ω] Transresistance (default: 1000.0 Ω = 1 kΩ)

# Connectors
- `p1` Positive input (sensing) terminal
- `n1` Negative input (sensing) terminal
- `p2` Positive output terminal
- `n2` Negative output terminal

# Equations
```
v2 = Rm × i_through (where i_through = current from p1 to n1)
v1 = 0 (zero input impedance)
```

# Example
```julia
@named ccvs = CCVS(Rm = 1000.0) # Transresistance amplifier with Rm = 1 kΩ
```
"""
@component function CCVS(; name, Rm = 1000.0)
@named twoport = TwoPort()
@unpack v1, v2, i1, i2 = twoport

pars = @parameters begin
Rm = Rm, [description = "Transresistance [V/A]"]
end

# Note: i1 = p1.i = current INTO p1. The controlling current is current
# flowing THROUGH the input port (from p1 to n1), which equals -i1.
eqs = Equation[
v2 ~ -Rm * i1,
v1 ~ 0,
]

sys = System(eqs, t, [], pars; name, systems = [])
return extend(sys, twoport)
end

"""
CCCS(; name, α = 1.0)

Current Controlled Current Source (SPICE F-element).

A four-terminal device where the output current is proportional to the input current.
The input port has zero impedance (no voltage drop across the sensing terminals).
The controlling current is measured as current flowing from p1 to n1 (through the input port).

# Parameters
- `α`: [A/A] Current gain (default: 1.0). Can be negative for inverting behavior.

# Connectors
- `p1` Positive input (sensing) terminal
- `n1` Negative input (sensing) terminal
- `p2` Positive output terminal
- `n2` Negative output terminal

# Equations
```
i2 = α × i_through (where i_through = current from p1 to n1)
v1 = 0 (zero input impedance)
```

# Example
```julia
@named cccs = CCCS(α = 100.0) # Current amplifier with gain 100
```
"""
@component function CCCS(; name, α = 1.0)
@named twoport = TwoPort()
@unpack v1, v2, i1, i2 = twoport

pars = @parameters begin
α = α, [description = "Current gain [A/A]"]
end

# Note: i1 = p1.i = current INTO p1. The controlling current is current
# flowing THROUGH the input port (from p1 to n1), which equals -i1.
eqs = Equation[
i2 ~ -α * i1,
v1 ~ 0,
]

sys = System(eqs, t, [], pars; name, systems = [])
return extend(sys, twoport)
end
56 changes: 56 additions & 0 deletions src/Electrical/Analog/linearized.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Linearized Components: LinearizedDiode
# Simplified linear models for small-signal analysis

"""
LinearizedDiode(; name, Vd = 0.7, Rd = 10.0)

Linearized diode model for simplified circuit analysis.

A piecewise-linear diode model with a fixed forward voltage drop and dynamic
resistance. When forward biased (v > Vd), the diode conducts with resistance Rd.
When reverse biased, the diode blocks current.

# Parameters
- `Vd`: [V] Forward voltage drop (default: 0.7)
- `Rd`: [Ω] Dynamic resistance when conducting (default: 10.0)

# Connectors
- `p` Positive terminal (anode)
- `n` Negative terminal (cathode)

# Equations
```
if v > Vd:
i = (v - Vd) / Rd # Forward conducting
else:
i = 0 # Reverse blocking
```

# Behavior
- Forward bias (v > Vd): Diode conducts, i = (v - Vd) / Rd
- Reverse bias (v ≤ Vd): Diode blocks, i = 0

# Example
```julia
@named diode = LinearizedDiode(Vd = 0.7, Rd = 10.0)
```
"""
@component function LinearizedDiode(; name, Vd = 0.7, Rd = 10.0)
@named oneport = OnePort()
@unpack v, i = oneport

pars = @parameters begin
Vd = Vd, [description = "Forward voltage drop [V]"]
Rd = Rd, [description = "Dynamic resistance [Ω]"]
end

# Piecewise-linear behavior:
# Forward bias: i = (v - Vd) / Rd
# Reverse bias: i = 0
eqs = Equation[
i ~ IfElse.ifelse(v > Vd, (v - Vd) / Rd, 0.0),
]

sys = System(eqs, t, [], pars; name, systems = [])
return extend(sys, oneport)
end
Loading