Skip to content

Commit ad28aa2

Browse files
committed
Implement IRuleSubject for current and voltage sources
1 parent b20723e commit ad28aa2

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralCurrentSource/BehavioralCurrentSource.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using SpiceSharp.Components.BehavioralComponents;
22
using SpiceSharp.Attributes;
3+
using SpiceSharp.Validation;
4+
using System.Linq;
35

46
namespace SpiceSharp.Components
57
{
68
/// <summary>
79
/// A behavioral current source.
810
/// </summary>
911
/// <seealso cref="BehavioralComponent" />
10-
[Pin(0, "I+"), Pin(1, "I-"), Connected]
11-
public class BehavioralCurrentSource : BehavioralComponent
12+
[Pin(0, "I+"), Pin(1, "I-"), IndependentSource, Connected]
13+
public class BehavioralCurrentSource : BehavioralComponent, IRuleSubject
1214
{
1315
/// <summary>
1416
/// The behavioral current source base pin count
@@ -37,5 +39,14 @@ public BehavioralCurrentSource(string name, string pos, string neg, string expre
3739
Connect(pos, neg);
3840
Parameters.Expression = expression;
3941
}
42+
43+
/// <inheritdoc/>
44+
void IRuleSubject.Apply(IRules rules)
45+
{
46+
var p = rules.GetParameterSet<ComponentRuleParameters>();
47+
var nodes = Nodes.Select(name => p.Factory.GetSharedVariable(name)).ToArray();
48+
foreach (var rule in rules.GetRules<IConductiveRule>())
49+
rule.AddPath(this, ConductionTypes.None, nodes[0], nodes[1]);
50+
}
4051
}
4152
}

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralVoltageSource/BehavioralVoltageSource.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
using SpiceSharp.Components.BehavioralComponents;
1+
using SpiceSharp.Attributes;
2+
using SpiceSharp.Components.BehavioralComponents;
3+
using SpiceSharp.Validation;
4+
using System.Linq;
25

36
namespace SpiceSharp.Components
47
{
58
/// <summary>
69
/// A behavioral voltage source.
710
/// </summary>
811
/// <seealso cref="BehavioralComponent" />
9-
public class BehavioralVoltageSource : BehavioralComponent
12+
[Pin(0, "V+"), Pin(1, "V-"), VoltageDriver(0, 1), IndependentSource]
13+
public class BehavioralVoltageSource : BehavioralComponent, IRuleSubject
1014
{
1115
/// <summary>
1216
/// The behavioral voltage source base pin count
@@ -35,5 +39,16 @@ public BehavioralVoltageSource(string name, string pos, string neg, string expre
3539
Connect(pos, neg);
3640
Parameters.Expression = expression;
3741
}
42+
43+
/// <inheritdoc/>
44+
void IRuleSubject.Apply(IRules rules)
45+
{
46+
var p = rules.GetParameterSet<ComponentRuleParameters>();
47+
var nodes = Nodes.Select(name => p.Factory.GetSharedVariable(name)).ToArray();
48+
foreach (var rule in rules.GetRules<IConductiveRule>())
49+
rule.AddPath(this, nodes[0], nodes[1]);
50+
foreach (var rule in rules.GetRules<IAppliedVoltageRule>())
51+
rule.Fix(this, nodes[0], nodes[1]);
52+
}
3853
}
3954
}

SpiceSharpBehavioralTest/Components/BehavioralCurrentSourceTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ public void When_UsedAsImpedanceOp_Expect_Reference(string expression, double dc
6666
op.Run(ckt);
6767
}
6868

69+
[Test]
70+
public void When_DifferentialVoltage_Expect_Reference()
71+
{
72+
var ckt = new Circuit(
73+
new VoltageSource("V1", "a", "0", new Sine(0, 1, 100)),
74+
new VoltageSource("V2", "b", "0", new Sine(0, 2, 65)),
75+
new BehavioralCurrentSource("I1", "0", "out", "V(a,b)/2"),
76+
new Resistor("R1", "out", "0", 1));
77+
78+
var tran = new Transient("tran", 1e-6, 1e-3);
79+
tran.ExportSimulationData += (sender, args) =>
80+
{
81+
Assert.AreEqual(0.5 * (args.GetVoltage("a") - args.GetVoltage("b")), args.GetVoltage("out"), 1e-12);
82+
};
83+
tran.Run(ckt);
84+
}
85+
6986
[Test]
7087
public void When_CapacitorTransient_Expect_Reference()
7188
{

0 commit comments

Comments
 (0)