Skip to content

Commit e361216

Browse files
committed
Add possibility to have current through resistor - PR #56
1 parent 8a17255 commit e361216

4 files changed

Lines changed: 58 additions & 10 deletions

File tree

SpiceSharpBehavioral/Components/BehavioralBindingContext.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,46 @@ public IVariable<T> MapNode<T>(IVariableFactory<IVariable<T>> factory, VariableN
8888

8989
case NodeTypes.Current:
9090
IBehaviorContainer container;
91+
IBiasingBehavior tmpb;
92+
IFrequencyBehavior tmpf;
93+
94+
// Get the relevant behaviors
9195
if (Simulation.EntityBehaviors.Comparer.Equals(node.Name, Behaviors.Name))
9296
container = Behaviors;
9397
else
9498
container = Simulation.EntityBehaviors[node.Name];
99+
95100
if (container == Behaviors)
96101
{
97102
if (ownBranch == null)
98103
throw new SpiceSharpException($"The behavior for {Entity.Name} does not define a branch current.");
99104
return ownBranch;
100105
}
101106
else if (container.TryGetValue<IBranchedBehavior<T>>(out var branched))
107+
{
108+
// Best-case scenario! Our behaviors define a branched behavior!
102109
return branched.Branch;
103-
else if (typeof(T) == typeof(double) && container.TryGetValue(out IBiasingBehavior tmpb) && tmpb is CurrentSources.Biasing biasing)
110+
}
111+
else if (typeof(T) == typeof(double) && container.TryGetValue(out tmpb) && tmpb is CurrentSources.Biasing biasing)
104112
{
113+
// If whatever is being is asked is the current from a current source, then we also don't have a problem
105114
var result = new FuncVariable<double>($"I({biasing.Name})", () => biasing.Current, Units.Ampere);
106115
return result as IVariable<T>;
107116
}
108-
else if (typeof(T) == typeof(Complex) && container.TryGetValue(out IFrequencyBehavior tmpf) && tmpf is CurrentSources.Frequency frequency)
117+
else if (typeof(T) == typeof(Complex) && container.TryGetValue(out tmpf) && tmpf is CurrentSources.Frequency frequency)
109118
{
119+
// Current source = no problem
110120
var result = new FuncVariable<Complex>($"I({frequency.Name})", () => frequency.ComplexCurrent, Units.Ampere);
111121
return result as IVariable<T>;
112122
}
113123
else
114-
goto default;
124+
{
125+
var result = container.CreatePropertyGetter<T>("i");
126+
if (result == null)
127+
goto default;
128+
SpiceSharpWarning.Warning(this, SpiceSharpBehavioral.Properties.Resources.LooseLinkCurrent.FormatString(container.Name));
129+
return new FuncVariable<T>($"@{container.Name}[i]", result, Units.Ampere);
130+
}
115131

116132
default:
117133
throw new SpiceSharpException($"Could not determine the variable {node.Name}");

SpiceSharpBehavioral/Properties/Resources.Designer.cs

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SpiceSharpBehavioral/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,7 @@
120120
<data name="ArgumentMismatch" xml:space="preserve">
121121
<value>Invalid number of arguments: {0} expected but {1} were given.</value>
122122
</data>
123+
<data name="LooseLinkCurrent" xml:space="preserve">
124+
<value>The current through {0} is only loosely linked. This might result in less accurate results, or instability. Consider placing a voltage source in series and referencing the current through that instead.</value>
125+
</data>
123126
</root>

SpiceSharpBehavioralTest/Components/MixedTests.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
using NUnit.Framework;
22
using SpiceSharp;
33
using SpiceSharp.Components;
4-
using SpiceSharp.Entities;
54
using SpiceSharp.Simulations;
65
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
9-
using System.Text;
10-
using System.Threading.Tasks;
116

127
namespace SpiceSharpBehavioralTest.Components
138
{
@@ -111,5 +106,31 @@ private static void Entry(Circuit ckt, string name, string m_in, string m_out, s
111106
ckt.Add(new BehavioralCurrentSource($"{name}.Guv", "0", v_vel, vel($"V({mss})")));
112107
ckt.Add(new Resistor($"{name}.Ruv", "0", v_vel, 1.0));
113108
}
109+
110+
[Test]
111+
public void When_ResistorCurrent_Expect_ReferenceWarning()
112+
{
113+
var ckt = new Circuit(
114+
new VoltageSource("V1", "in", "0", 1.0),
115+
new Resistor("R1", "in", "0", 1e3),
116+
new BehavioralVoltageSource("V2", "out", "0", "I(R1)"));
117+
118+
// Catch the warning
119+
int warnings = 0;
120+
SpiceSharpWarning.WarningGenerated += (sender, args) =>
121+
{
122+
warnings++;
123+
};
124+
125+
// Run the simulation
126+
var op = new OP("op");
127+
op.ExportSimulationData += (sender, args) =>
128+
{
129+
Assert.AreEqual(1.0 / 1e3, args.GetVoltage("out"), 1e-9);
130+
};
131+
op.Run(ckt);
132+
133+
Assert.AreEqual(1, warnings);
134+
}
114135
}
115136
}

0 commit comments

Comments
 (0)