Skip to content

Commit 1145812

Browse files
committed
Refactor
1 parent 8f3b010 commit 1145812

30 files changed

Lines changed: 429 additions & 442 deletions

SpiceSharpBehavioral/Builders/Direct/ComplexBuilder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using SpiceSharp;
2-
using SpiceSharp.Simulations;
32
using SpiceSharpBehavioral.Parsers.Nodes;
43
using System;
5-
using System.Collections.Generic;
64
using System.Numerics;
75

86
namespace SpiceSharpBehavioral.Builders.Direct

SpiceSharpBehavioral/Builders/Functions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using SpiceSharp;
2-
using System;
1+
using System;
32
using System.Numerics;
43

54
namespace SpiceSharpBehavioral.Builders

SpiceSharpBehavioral/Builders/Functions/ComplexFunctionBuilder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using SpiceSharp.Simulations;
2-
using SpiceSharpBehavioral.Parsers.Nodes;
1+
using SpiceSharpBehavioral.Parsers.Nodes;
32
using System;
4-
using System.Collections.Generic;
53
using System.Numerics;
64

75
namespace SpiceSharpBehavioral.Builders.Functions

SpiceSharpBehavioral/Builders/Functions/ComplexFunctionBuilderHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using SpiceSharp;
2-
using SpiceSharpBehavioral.Builders.Direct;
32
using SpiceSharpBehavioral.Diagnostics;
43
using SpiceSharpBehavioral.Parsers.Nodes;
54
using System;

SpiceSharpBehavioral/Builders/Functions/FunctionFoundEventArgs.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using SpiceSharp;
22
using SpiceSharpBehavioral.Parsers.Nodes;
33
using System;
4-
using System.Collections.Generic;
54

65
namespace SpiceSharpBehavioral.Builders.Functions
76
{

SpiceSharpBehavioral/Builders/Functions/RealFunctionBuilder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using SpiceSharp.Simulations;
2-
using SpiceSharpBehavioral.Parsers.Nodes;
1+
using SpiceSharpBehavioral.Parsers.Nodes;
32
using System;
4-
using System.Collections.Generic;
53

64
namespace SpiceSharpBehavioral.Builders.Functions
75
{

SpiceSharpBehavioral/Builders/Functions/RealILState.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using SpiceSharp;
2-
using SpiceSharpBehavioral.Diagnostics;
32
using SpiceSharpBehavioral.Parsers.Nodes;
43
using System;
5-
using System.Collections.Generic;
64
using System.Reflection;
75
using System.Reflection.Emit;
86

SpiceSharpBehavioral/Components/BehavioralBindingContext.cs renamed to SpiceSharpBehavioral/Components/BehavioralSources/BehavioralBindingContext.cs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
using SpiceSharp.Algebra;
2-
using SpiceSharp.Attributes;
1+
using SpiceSharp.Attributes;
32
using SpiceSharp.Behaviors;
4-
using SpiceSharp.Components.CommonBehaviors;
53
using SpiceSharp.Simulations;
64
using SpiceSharpBehavioral;
75
using SpiceSharpBehavioral.Builders;
6+
using SpiceSharpBehavioral.Builders.Functions;
87
using SpiceSharpBehavioral.Parsers.Nodes;
98
using System;
109
using System.Collections.Generic;
1110
using System.Linq;
1211
using System.Numerics;
1312

14-
namespace SpiceSharp.Components.BehavioralComponents
13+
namespace SpiceSharp.Components.BehavioralSources
1514
{
1615
/// <summary>
1716
/// A context for behavioral components.
@@ -67,7 +66,8 @@ public Dictionary<VariableNode, Node> CreateDerivatives(Node function)
6766
/// <param name="factory">The factory for variables.</param>
6867
/// <param name="function">The function containing the variables.</param>
6968
/// <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)
69+
/// <returns>The variables by their nodes.</returns>
70+
public Dictionary<VariableNode, IVariable<T>> GetVariableNodes<T>(IVariableFactory<IVariable<T>> factory, Node function, IVariable<T> ownBranch = null)
7171
{
7272
var state = GetState<IBiasingSimulationState>();
7373
var bp = GetParameterSet<Parameters>();
@@ -78,11 +78,44 @@ public Dictionary<VariableNode, IVariable<T>> MapNodes<T>(IVariableFactory<IVari
7878
foreach (var variable in nf.Build(function).Where(v => v.NodeType == NodeTypes.Voltage || v.NodeType == NodeTypes.Current))
7979
{
8080
if (!variables.ContainsKey(variable))
81-
variables.Add(variable, MapNode(factory, variable, ownBranch));
81+
variables.Add(variable, GetVariableNode(factory, variable, ownBranch));
8282
}
8383
return variables;
8484
}
8585

86+
/// <summary>
87+
/// Maps all the nodes that are referenced in the function and makes them discoverable by the function builder.
88+
/// </summary>
89+
/// <typeparam name="T">The variable value type.</typeparam>
90+
/// <param name="factory">The factory for variables.</param>
91+
/// <param name="function">The function containing the variables.</param>
92+
/// <param name="builder">The function builder.</param>
93+
/// <param name="ownBranch">Optionally, a branch for the current behavior branch current.</param>
94+
/// <returns>The variables by their nodes.</returns>
95+
public Dictionary<VariableNode, IVariable<T>> MapVariableNodes<T>(IVariableFactory<IVariable<T>> factory, Node function, IFunctionBuilder<T> builder, IVariable<T> ownBranch = null)
96+
{
97+
var variables = GetVariableNodes(factory, function, ownBranch);
98+
MapVariableNodes(variables, builder);
99+
return variables;
100+
}
101+
102+
/// <summary>
103+
/// Maps the nodes in the dictionary such that they are discoverable by the function builder.
104+
/// </summary>
105+
/// <typeparam name="T">The variable value type.</typeparam>
106+
/// <param name="variables">The dictionary of variables.</param>
107+
/// <param name="builder">The function builder.</param>
108+
public void MapVariableNodes<T>(Dictionary<VariableNode, IVariable<T>> variables, IFunctionBuilder<T> builder)
109+
{
110+
builder.VariableFound += (sender, args) =>
111+
{
112+
if (args.Variable != null)
113+
return; // Already found
114+
if (variables.TryGetValue(args.Node, out var value))
115+
args.Variable = value;
116+
};
117+
}
118+
86119
/// <summary>
87120
/// Remap a variable node to an <see cref="IVariable{T}"/>.
88121
/// </summary>
@@ -91,7 +124,7 @@ public Dictionary<VariableNode, IVariable<T>> MapNodes<T>(IVariableFactory<IVari
91124
/// <param name="node">The node.</param>
92125
/// <param name="ownBranch">The branch.</param>
93126
/// <returns>The variable.</returns>
94-
public IVariable<T> MapNode<T>(IVariableFactory<IVariable<T>> factory, VariableNode node, IVariable<T> ownBranch = null)
127+
public IVariable<T> GetVariableNode<T>(IVariableFactory<IVariable<T>> factory, VariableNode node, IVariable<T> ownBranch = null)
95128
{
96129
switch (node.NodeType)
97130
{
@@ -145,5 +178,21 @@ public IVariable<T> MapNode<T>(IVariableFactory<IVariable<T>> factory, VariableN
145178
throw new SpiceSharpException($"Could not determine the variable {node.Name}");
146179
}
147180
}
181+
182+
/// <summary>
183+
/// Converts any variables in a dictionary for a complex behavior that wishes to reuse it.
184+
/// </summary>
185+
/// <param name="variables">The variables.</param>
186+
/// <param name="builder">The complex function builder.</param>
187+
public void ConvertVariables(IReadOnlyDictionary<VariableNode, IVariable<double>> variables, IFunctionBuilder<Complex> builder)
188+
{
189+
builder.VariableFound += (sender, args) =>
190+
{
191+
if (args.Variable != null)
192+
return; // Already found
193+
if (variables.TryGetValue(args.Node, out var value))
194+
args.Variable = new FuncVariable<Complex>(value.Name, () => value.Value, value.Unit);
195+
};
196+
}
148197
}
149198
}

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralCapacitor/BehavioralCapacitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using SpiceSharp.Attributes;
2-
using SpiceSharp.Components.BehavioralComponents;
2+
using SpiceSharp.Components.BehavioralSources;
33
using SpiceSharp.Validation;
44
using System.Linq;
55

SpiceSharpBehavioral/Components/BehavioralSources/BehavioralCapacitor/Biasing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using SpiceSharp.Behaviors;
2-
using SpiceSharp.Components.BehavioralComponents;
2+
using SpiceSharp.Components.BehavioralSources;
33
using SpiceSharp.Components.CommonBehaviors;
44
using SpiceSharp.Simulations;
55
using SpiceSharpBehavioral.Builders;
@@ -59,7 +59,7 @@ public Biasing(BehavioralBindingContext context)
5959
};
6060
Function = replacer.Build(bp.Function);
6161
Derivatives = context.CreateDerivatives(Function);
62-
VariableNodes = context.MapNodes(state, Function);
62+
VariableNodes = context.GetVariableNodes(state, Function);
6363
}
6464
}
6565
}

0 commit comments

Comments
 (0)