Skip to content

Commit e7d5227

Browse files
Merge pull request #47 from SpiceSharp/development
Implement IRuleSubject for sources
2 parents 5034d5c + db0af8d commit e7d5227

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

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
}

SpiceSharpBehavioral/SpiceSharpBehavioral.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard1.5;netstandard2.0;net45;netcoreapp2.1</TargetFrameworks>
5-
<Version>3.0.5</Version>
5+
<Version>3.0.6</Version>
66
<Authors>Sven Boulanger</Authors>
77
<Title>Spice#.Behavioral</Title>
88
<Description>Spice#.Behavioral is a library that allows using behavioral components in the circuit simulator Spice#.</Description>
@@ -11,12 +11,12 @@
1111
<RepositoryUrl>https://github.com/SpiceSharp/SpiceSharpBehavioral</RepositoryUrl>
1212
<PackageTags>circuit electronics netlist parser spice simulator simulation ode solver design behavioral modeling</PackageTags>
1313
<PackageIconUrl></PackageIconUrl>
14-
<AssemblyVersion>3.0.5.0</AssemblyVersion>
14+
<AssemblyVersion>3.0.6.0</AssemblyVersion>
1515
<PackageReleaseNotes>Refer to the GitHub release for release notes.</PackageReleaseNotes>
1616
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1717
<Company />
1818
<NeutralLanguage>en</NeutralLanguage>
19-
<FileVersion>3.0.5.0</FileVersion>
19+
<FileVersion>3.0.6.0</FileVersion>
2020
<DebugType>full</DebugType>
2121
<DebugSymbols>true</DebugSymbols>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@@ -67,7 +67,7 @@
6767

6868
<ItemGroup>
6969
<PackageReference Include="SpiceSharp">
70-
<Version>3.0.4</Version>
70+
<Version>3.0.5</Version>
7171
</PackageReference>
7272
<PackageReference Include="System.Reflection.Emit.Lightweight">
7373
<Version>4.7.0</Version>

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
{

SpiceSharpBehavioralTest/SpiceSharpBehavioralTest.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@
7575
</ItemGroup>
7676
<ItemGroup>
7777
<PackageReference Include="SpiceSharp">
78-
<Version>3.0.4</Version>
78+
<Version>3.0.5</Version>
7979
</PackageReference>
8080
<PackageReference Include="NUnit">
81-
<Version>3.13.0</Version>
81+
<Version>3.13.1</Version>
8282
</PackageReference>
8383
<PackageReference Include="NUnit.Console">
8484
<Version>3.12.0</Version>

0 commit comments

Comments
 (0)