Skip to content

Commit c512c43

Browse files
Merge pull request #71 from SpiceSharp/development
Development
2 parents 10c832d + 8f3b010 commit c512c43

17 files changed

Lines changed: 77 additions & 45 deletions

File tree

SpiceSharpBehavioral/Builders/Functions/ComplexFunctionBuilderHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private static void If(IILState<Complex> ils, IReadOnlyList<Node> arguments)
185185
arguments.Check(3);
186186
ilsc.PushReal(arguments[0]);
187187
ilsc.PushDouble(0.5);
188-
ilsc.PushCheck(OpCodes.Bgt_S, arguments[1], arguments[2]);
188+
ilsc.PushCheck(OpCodes.Bgt, arguments[1], arguments[2]);
189189
}
190190
private static void Limit(IILState<Complex> ils, IReadOnlyList<Node> arguments) => ils.Call(HelperFunctions.Limit, arguments);
191191

SpiceSharpBehavioral/Builders/Functions/RealFunctionBuilderHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ private static void If(IILState<double> ils, IReadOnlyList<Node> arguments)
179179
ils.Push(0.5);
180180
var lblElse = ils.Generator.DefineLabel();
181181
var lblEnd = ils.Generator.DefineLabel();
182-
ils.Generator.Emit(OpCodes.Ble_S, lblElse);
182+
ils.Generator.Emit(OpCodes.Ble, lblElse);
183183
ils.Push(arguments[1]);
184-
ils.Generator.Emit(OpCodes.Br_S, lblEnd);
184+
ils.Generator.Emit(OpCodes.Br, lblEnd);
185185
ils.Generator.MarkLabel(lblElse);
186186
ils.Push(arguments[2]);
187187
ils.Generator.MarkLabel(lblEnd);

SpiceSharpBehavioral/Components/BehavioralBindingContext.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ namespace SpiceSharp.Components.BehavioralComponents
2323
[BindingContextFor(typeof(BehavioralCurrentSource))]
2424
public class BehavioralBindingContext : ComponentBindingContext
2525
{
26-
/// <summary>
27-
/// Gets the current references.
28-
/// </summary>
29-
/// <value>
30-
/// The current references.
31-
/// </value>
32-
/// <remarks>
33-
/// The reason we are tracking branches, is because they reference other components.
34-
/// </remarks>
35-
public Dictionary<VariableNode, IBehaviorContainer> Branches { get; private set; }
36-
3726
/// <summary>
3827
/// Initializes a new instance of the <see cref="BehavioralBindingContext" /> class.
3928
/// </summary>
@@ -71,6 +60,29 @@ public Dictionary<VariableNode, Node> CreateDerivatives(Node function)
7160
return derivatives.Derive(function) ?? new Dictionary<VariableNode, Node>(comparer);
7261
}
7362

63+
/// <summary>
64+
/// Maps all the nodes that are referenced in the function.
65+
/// </summary>
66+
/// <typeparam name="T">The variable value type.</typeparam>
67+
/// <param name="factory">The factory for variables.</param>
68+
/// <param name="function">The function containing the variables.</param>
69+
/// <param name="ownBranch">Optionally a branch for the current behavior branch current.</param>
70+
public Dictionary<VariableNode, IVariable<T>> MapNodes<T>(IVariableFactory<IVariable<T>> factory, Node function, IVariable<T> ownBranch = null)
71+
{
72+
var state = GetState<IBiasingSimulationState>();
73+
var bp = GetParameterSet<Parameters>();
74+
var comparer = new VariableNodeComparer(state.Comparer, Simulation.EntityBehaviors.Comparer, bp.VariableComparer);
75+
var variables = new Dictionary<VariableNode, IVariable<T>>(comparer);
76+
77+
var nf = new NodeFinder();
78+
foreach (var variable in nf.Build(function).Where(v => v.NodeType == NodeTypes.Voltage || v.NodeType == NodeTypes.Current))
79+
{
80+
if (!variables.ContainsKey(variable))
81+
variables.Add(variable, MapNode(factory, variable, ownBranch));
82+
}
83+
return variables;
84+
}
85+
7486
/// <summary>
7587
/// Remap a variable node to an <see cref="IVariable{T}"/>.
7688
/// </summary>

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralCapacitor/Biasing.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using SpiceSharpBehavioral.Parsers.Nodes;
77
using System;
88
using System.Collections.Generic;
9-
using System.Linq;
109

1110
namespace SpiceSharp.Components.BehavioralCapacitorBehaviors
1211
{
@@ -23,7 +22,7 @@ public class Biasing : Behavior
2322
/// <summary>
2423
/// Gets the variables that are associated with each variable node.
2524
/// </summary>
26-
protected Dictionary<VariableNode, IVariable<double>> DerivativeVariables { get; }
25+
protected Dictionary<VariableNode, IVariable<double>> VariableNodes { get; }
2726

2827
/// <summary>
2928
/// The function that computes the value.
@@ -60,9 +59,7 @@ public Biasing(BehavioralBindingContext context)
6059
};
6160
Function = replacer.Build(bp.Function);
6261
Derivatives = context.CreateDerivatives(Function);
63-
DerivativeVariables = new Dictionary<VariableNode, IVariable<double>>(Derivatives.Comparer);
64-
foreach (var key in Derivatives.Keys)
65-
DerivativeVariables.Add(key, context.MapNode(state, key));
62+
VariableNodes = context.MapNodes(state, Function);
6663
}
6764
}
6865
}

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralCapacitor/Frequency.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Frequency(BehavioralBindingContext context)
7373
var builder = new ComplexFunctionBuilder();
7474
builder.VariableFound += (sender, args) =>
7575
{
76-
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
76+
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
7777
args.Variable = new FuncVariable<Complex>(variable.Name, () => variable.Value, variable.Unit);
7878
};
7979
bp.RegisterBuilder(context, builder);

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralCapacitor/Time.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ public Time(BehavioralBindingContext context)
7474
var builder = new RealFunctionBuilder();
7575
builder.VariableFound += (sender, args) =>
7676
{
77-
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
77+
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
7878
args.Variable = variable;
7979
};
8080
bp.RegisterBuilder(context, builder);
8181
var matLocs = new List<MatrixLocation>(Derivatives.Count * 2);
8282
var rhsLocs = Variables.GetRhsIndices(state.Map);
8383
foreach (var pair in Derivatives)
8484
{
85-
var variable = DerivativeVariables[pair.Key];
85+
var variable = VariableNodes[pair.Key];
8686
if (state.Map.Contains(variable))
8787
{
8888
derivatives.Add(builder.Build(pair.Value));

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralCurrentSource/Biasing.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public partial class Biasing : Behavior,
3232
/// <summary>
3333
/// Gets the variables that are associated with each variable node.
3434
/// </summary>
35-
protected Dictionary<VariableNode, IVariable<double>> DerivativeVariables { get; }
35+
protected Dictionary<VariableNode, IVariable<double>> VariableNodes { get; }
3636

3737
/// <summary>
3838
/// The function that computes the value.
@@ -88,21 +88,21 @@ public Biasing(BehavioralBindingContext context)
8888
// Let's build the derivative functions and get their matrix locations/rhs locations
8989
Function = bp.Function;
9090
Derivatives = context.CreateDerivatives(Function);
91-
DerivativeVariables = Derivatives.Keys.ToDictionary(d => d, d => context.MapNode(state, d), Derivatives.Comparer);
91+
VariableNodes = context.MapNodes(state, Function);
9292
var derivatives = new List<Func<double>>(Derivatives.Count);
9393
var derivativeVariables = new List<IVariable<double>>(Derivatives.Count);
9494
var builder = new RealFunctionBuilder();
9595
builder.VariableFound += (sender, args) =>
9696
{
97-
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
97+
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
9898
args.Variable = variable;
9999
};
100100
bp.RegisterBuilder(context, builder);
101101
var matLocs = new List<MatrixLocation>(Derivatives.Count * 2);
102102
var rhsLocs = _variables.GetRhsIndices(state.Map);
103103
foreach (var pair in Derivatives)
104104
{
105-
var variable = DerivativeVariables[pair.Key];
105+
var variable = VariableNodes[pair.Key];
106106
if (state.Map.Contains(variable))
107107
{
108108
derivatives.Add(builder.Build(pair.Value));

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralCurrentSource/Frequency.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Frequency(BehavioralBindingContext context)
7373
var builder = new ComplexFunctionBuilder();
7474
builder.VariableFound += (sender, args) =>
7575
{
76-
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
76+
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
7777
args.Variable = new FuncVariable<Complex>(variable.Name, () => variable.Value, variable.Unit);
7878
};
7979
bp.RegisterBuilder(context, builder);

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralResistor/Biasing.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public partial class Biasing : Behavior,
3535
/// <summary>
3636
/// Gets the variables that are associated with each variable node.
3737
/// </summary>
38-
protected Dictionary<VariableNode, IVariable<double>> DerivativeVariables { get; }
38+
protected Dictionary<VariableNode, IVariable<double>> VariableNodes { get; }
3939

4040
/// <summary>
4141
/// The function that computes the value.
@@ -100,21 +100,24 @@ public Biasing(BehavioralBindingContext context)
100100
// Let's build the derivative functions and get their matrix locations/rhs locations
101101
Function = Node.Multiply(Node.Current(Name), bp.Function);
102102
Derivatives = context.CreateDerivatives(Function);
103-
DerivativeVariables = Derivatives.Keys.ToDictionary(d => d, d => context.MapNode(state, d, _branch), Derivatives.Comparer);
103+
VariableNodes = context.MapNodes(state, Function, _branch);
104104
var derivatives = new List<Func<double>>(Derivatives.Count);
105105
var derivativeVariables = new List<IVariable<double>>(Derivatives.Count);
106106
var builder = new RealFunctionBuilder();
107107
builder.VariableFound += (sender, args) =>
108108
{
109-
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
110-
args.Variable = variable;
109+
if (args.Variable == null)
110+
{
111+
if (VariableNodes.TryGetValue(args.Node, out var variable))
112+
args.Variable = variable;
113+
}
111114
};
112115
bp.RegisterBuilder(context, builder);
113116
var matLocs = new List<MatrixLocation>(Derivatives.Count);
114117
var rhsLocs = state.Map[_branch];
115118
foreach (var pair in Derivatives)
116119
{
117-
var variable = DerivativeVariables[pair.Key];
120+
var variable = VariableNodes[pair.Key];
118121
if (state.Map.Contains(variable))
119122
{
120123
derivatives.Add(builder.Build(pair.Value));

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralResistor/Frequency.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ public Frequency(BehavioralBindingContext context)
8585
var nVariables = new Dictionary<VariableNode, IVariable<Complex>>(Derivatives.Comparer);
8686
foreach (var variable in Derivatives.Keys)
8787
{
88-
var orig = DerivativeVariables[variable];
88+
var orig = VariableNodes[variable];
8989
nVariables.Add(variable, new FuncVariable<Complex>(orig.Name, () => orig.Value, orig.Unit));
9090
}
9191
var builder = new ComplexFunctionBuilder();
9292
builder.VariableFound += (sender, args) =>
9393
{
94-
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
94+
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
9595
args.Variable = new FuncVariable<Complex>(variable.Name, () => variable.Value, variable.Unit);
9696
};
9797
bp.RegisterBuilder(context, builder);

0 commit comments

Comments
 (0)