Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using SpiceSharp.Components.BehavioralComponents;
using SpiceSharp.Attributes;
using SpiceSharp.Validation;
using System.Linq;

namespace SpiceSharp.Components
{
/// <summary>
/// A behavioral current source.
/// </summary>
/// <seealso cref="BehavioralComponent" />
[Pin(0, "I+"), Pin(1, "I-"), Connected]
public class BehavioralCurrentSource : BehavioralComponent
[Pin(0, "I+"), Pin(1, "I-"), IndependentSource, Connected]
public class BehavioralCurrentSource : BehavioralComponent, IRuleSubject
{
/// <summary>
/// The behavioral current source base pin count
Expand Down Expand Up @@ -37,5 +39,14 @@ public BehavioralCurrentSource(string name, string pos, string neg, string expre
Connect(pos, neg);
Parameters.Expression = expression;
}

/// <inheritdoc/>
void IRuleSubject.Apply(IRules rules)
{
var p = rules.GetParameterSet<ComponentRuleParameters>();
var nodes = Nodes.Select(name => p.Factory.GetSharedVariable(name)).ToArray();
foreach (var rule in rules.GetRules<IConductiveRule>())
rule.AddPath(this, ConductionTypes.None, nodes[0], nodes[1]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using SpiceSharp.Components.BehavioralComponents;
using SpiceSharp.Attributes;
using SpiceSharp.Components.BehavioralComponents;
using SpiceSharp.Validation;
using System.Linq;

namespace SpiceSharp.Components
{
/// <summary>
/// A behavioral voltage source.
/// </summary>
/// <seealso cref="BehavioralComponent" />
public class BehavioralVoltageSource : BehavioralComponent
[Pin(0, "V+"), Pin(1, "V-"), VoltageDriver(0, 1), IndependentSource]
public class BehavioralVoltageSource : BehavioralComponent, IRuleSubject
{
/// <summary>
/// The behavioral voltage source base pin count
Expand Down Expand Up @@ -35,5 +39,16 @@ public BehavioralVoltageSource(string name, string pos, string neg, string expre
Connect(pos, neg);
Parameters.Expression = expression;
}

/// <inheritdoc/>
void IRuleSubject.Apply(IRules rules)
{
var p = rules.GetParameterSet<ComponentRuleParameters>();
var nodes = Nodes.Select(name => p.Factory.GetSharedVariable(name)).ToArray();
foreach (var rule in rules.GetRules<IConductiveRule>())
rule.AddPath(this, nodes[0], nodes[1]);
foreach (var rule in rules.GetRules<IAppliedVoltageRule>())
rule.Fix(this, nodes[0], nodes[1]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ public void When_UsedAsImpedanceOp_Expect_Reference(string expression, double dc
op.Run(ckt);
}

[Test]
public void When_DifferentialVoltage_Expect_Reference()
{
var ckt = new Circuit(
new VoltageSource("V1", "a", "0", new Sine(0, 1, 100)),
new VoltageSource("V2", "b", "0", new Sine(0, 2, 65)),
new BehavioralCurrentSource("I1", "0", "out", "V(a,b)/2"),
new Resistor("R1", "out", "0", 1));

var tran = new Transient("tran", 1e-6, 1e-3);
tran.ExportSimulationData += (sender, args) =>
{
Assert.AreEqual(0.5 * (args.GetVoltage("a") - args.GetVoltage("b")), args.GetVoltage("out"), 1e-12);
};
tran.Run(ckt);
}

[Test]
public void When_CapacitorTransient_Expect_Reference()
{
Expand Down