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
Expand Up @@ -185,7 +185,7 @@ private static void If(IILState<Complex> ils, IReadOnlyList<Node> arguments)
arguments.Check(3);
ilsc.PushReal(arguments[0]);
ilsc.PushDouble(0.5);
ilsc.PushCheck(OpCodes.Bgt_S, arguments[1], arguments[2]);
ilsc.PushCheck(OpCodes.Bgt, arguments[1], arguments[2]);
}
private static void Limit(IILState<Complex> ils, IReadOnlyList<Node> arguments) => ils.Call(HelperFunctions.Limit, arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ private static void If(IILState<double> ils, IReadOnlyList<Node> arguments)
ils.Push(0.5);
var lblElse = ils.Generator.DefineLabel();
var lblEnd = ils.Generator.DefineLabel();
ils.Generator.Emit(OpCodes.Ble_S, lblElse);
ils.Generator.Emit(OpCodes.Ble, lblElse);
ils.Push(arguments[1]);
ils.Generator.Emit(OpCodes.Br_S, lblEnd);
ils.Generator.Emit(OpCodes.Br, lblEnd);
ils.Generator.MarkLabel(lblElse);
ils.Push(arguments[2]);
ils.Generator.MarkLabel(lblEnd);
Expand Down
34 changes: 23 additions & 11 deletions SpiceSharpBehavioral/Components/BehavioralBindingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ namespace SpiceSharp.Components.BehavioralComponents
[BindingContextFor(typeof(BehavioralCurrentSource))]
public class BehavioralBindingContext : ComponentBindingContext
{
/// <summary>
/// Gets the current references.
/// </summary>
/// <value>
/// The current references.
/// </value>
/// <remarks>
/// The reason we are tracking branches, is because they reference other components.
/// </remarks>
public Dictionary<VariableNode, IBehaviorContainer> Branches { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="BehavioralBindingContext" /> class.
/// </summary>
Expand Down Expand Up @@ -71,6 +60,29 @@ public Dictionary<VariableNode, Node> CreateDerivatives(Node function)
return derivatives.Derive(function) ?? new Dictionary<VariableNode, Node>(comparer);
}

/// <summary>
/// Maps all the nodes that are referenced in the function.
/// </summary>
/// <typeparam name="T">The variable value type.</typeparam>
/// <param name="factory">The factory for variables.</param>
/// <param name="function">The function containing the variables.</param>
/// <param name="ownBranch">Optionally a branch for the current behavior branch current.</param>
public Dictionary<VariableNode, IVariable<T>> MapNodes<T>(IVariableFactory<IVariable<T>> factory, Node function, IVariable<T> ownBranch = null)
{
var state = GetState<IBiasingSimulationState>();
var bp = GetParameterSet<Parameters>();
var comparer = new VariableNodeComparer(state.Comparer, Simulation.EntityBehaviors.Comparer, bp.VariableComparer);
var variables = new Dictionary<VariableNode, IVariable<T>>(comparer);

var nf = new NodeFinder();
foreach (var variable in nf.Build(function).Where(v => v.NodeType == NodeTypes.Voltage || v.NodeType == NodeTypes.Current))
{
if (!variables.ContainsKey(variable))
variables.Add(variable, MapNode(factory, variable, ownBranch));
}
return variables;
}

/// <summary>
/// Remap a variable node to an <see cref="IVariable{T}"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using SpiceSharpBehavioral.Parsers.Nodes;
using System;
using System.Collections.Generic;
using System.Linq;

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

/// <summary>
/// The function that computes the value.
Expand Down Expand Up @@ -60,9 +59,7 @@ public Biasing(BehavioralBindingContext context)
};
Function = replacer.Build(bp.Function);
Derivatives = context.CreateDerivatives(Function);
DerivativeVariables = new Dictionary<VariableNode, IVariable<double>>(Derivatives.Comparer);
foreach (var key in Derivatives.Keys)
DerivativeVariables.Add(key, context.MapNode(state, key));
VariableNodes = context.MapNodes(state, Function);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Frequency(BehavioralBindingContext context)
var builder = new ComplexFunctionBuilder();
builder.VariableFound += (sender, args) =>
{
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
args.Variable = new FuncVariable<Complex>(variable.Name, () => variable.Value, variable.Unit);
};
bp.RegisterBuilder(context, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public Time(BehavioralBindingContext context)
var builder = new RealFunctionBuilder();
builder.VariableFound += (sender, args) =>
{
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
args.Variable = variable;
};
bp.RegisterBuilder(context, builder);
var matLocs = new List<MatrixLocation>(Derivatives.Count * 2);
var rhsLocs = Variables.GetRhsIndices(state.Map);
foreach (var pair in Derivatives)
{
var variable = DerivativeVariables[pair.Key];
var variable = VariableNodes[pair.Key];
if (state.Map.Contains(variable))
{
derivatives.Add(builder.Build(pair.Value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class Biasing : Behavior,
/// <summary>
/// Gets the variables that are associated with each variable node.
/// </summary>
protected Dictionary<VariableNode, IVariable<double>> DerivativeVariables { get; }
protected Dictionary<VariableNode, IVariable<double>> VariableNodes { get; }

/// <summary>
/// The function that computes the value.
Expand Down Expand Up @@ -88,21 +88,21 @@ public Biasing(BehavioralBindingContext context)
// Let's build the derivative functions and get their matrix locations/rhs locations
Function = bp.Function;
Derivatives = context.CreateDerivatives(Function);
DerivativeVariables = Derivatives.Keys.ToDictionary(d => d, d => context.MapNode(state, d), Derivatives.Comparer);
VariableNodes = context.MapNodes(state, Function);
var derivatives = new List<Func<double>>(Derivatives.Count);
var derivativeVariables = new List<IVariable<double>>(Derivatives.Count);
var builder = new RealFunctionBuilder();
builder.VariableFound += (sender, args) =>
{
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
args.Variable = variable;
};
bp.RegisterBuilder(context, builder);
var matLocs = new List<MatrixLocation>(Derivatives.Count * 2);
var rhsLocs = _variables.GetRhsIndices(state.Map);
foreach (var pair in Derivatives)
{
var variable = DerivativeVariables[pair.Key];
var variable = VariableNodes[pair.Key];
if (state.Map.Contains(variable))
{
derivatives.Add(builder.Build(pair.Value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Frequency(BehavioralBindingContext context)
var builder = new ComplexFunctionBuilder();
builder.VariableFound += (sender, args) =>
{
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
args.Variable = new FuncVariable<Complex>(variable.Name, () => variable.Value, variable.Unit);
};
bp.RegisterBuilder(context, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial class Biasing : Behavior,
/// <summary>
/// Gets the variables that are associated with each variable node.
/// </summary>
protected Dictionary<VariableNode, IVariable<double>> DerivativeVariables { get; }
protected Dictionary<VariableNode, IVariable<double>> VariableNodes { get; }

/// <summary>
/// The function that computes the value.
Expand Down Expand Up @@ -100,21 +100,24 @@ public Biasing(BehavioralBindingContext context)
// Let's build the derivative functions and get their matrix locations/rhs locations
Function = Node.Multiply(Node.Current(Name), bp.Function);
Derivatives = context.CreateDerivatives(Function);
DerivativeVariables = Derivatives.Keys.ToDictionary(d => d, d => context.MapNode(state, d, _branch), Derivatives.Comparer);
VariableNodes = context.MapNodes(state, Function, _branch);
var derivatives = new List<Func<double>>(Derivatives.Count);
var derivativeVariables = new List<IVariable<double>>(Derivatives.Count);
var builder = new RealFunctionBuilder();
builder.VariableFound += (sender, args) =>
{
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
args.Variable = variable;
if (args.Variable == null)
{
if (VariableNodes.TryGetValue(args.Node, out var variable))
args.Variable = variable;
}
};
bp.RegisterBuilder(context, builder);
var matLocs = new List<MatrixLocation>(Derivatives.Count);
var rhsLocs = state.Map[_branch];
foreach (var pair in Derivatives)
{
var variable = DerivativeVariables[pair.Key];
var variable = VariableNodes[pair.Key];
if (state.Map.Contains(variable))
{
derivatives.Add(builder.Build(pair.Value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public Frequency(BehavioralBindingContext context)
var nVariables = new Dictionary<VariableNode, IVariable<Complex>>(Derivatives.Comparer);
foreach (var variable in Derivatives.Keys)
{
var orig = DerivativeVariables[variable];
var orig = VariableNodes[variable];
nVariables.Add(variable, new FuncVariable<Complex>(orig.Name, () => orig.Value, orig.Unit));
}
var builder = new ComplexFunctionBuilder();
builder.VariableFound += (sender, args) =>
{
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
args.Variable = new FuncVariable<Complex>(variable.Name, () => variable.Value, variable.Unit);
};
bp.RegisterBuilder(context, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial class Biasing : Behavior,
/// <summary>
/// Gets the variables that are associated with each variable node.
/// </summary>
protected Dictionary<VariableNode, IVariable<double>> DerivativeVariables { get; }
protected Dictionary<VariableNode, IVariable<double>> VariableNodes { get; }

/// <summary>
/// The function that computes the value.
Expand Down Expand Up @@ -100,11 +100,11 @@ public Biasing(BehavioralBindingContext context)
// Let's build the derivative functions and get their matrix locations/rhs locations
Function = bp.Function;
Derivatives = context.CreateDerivatives(Function);
DerivativeVariables = Derivatives.Keys.ToDictionary(d => d, d => context.MapNode(state, d, _branch), Derivatives.Comparer);
VariableNodes = context.MapNodes(state, Function, _branch);
var builder = new RealFunctionBuilder();
builder.VariableFound += (sender, args) =>
{
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
args.Variable = variable;
};
bp.RegisterBuilder(context, builder);
Expand All @@ -114,7 +114,7 @@ public Biasing(BehavioralBindingContext context)
var rhsLocs = state.Map[_branch];
foreach (var pair in Derivatives)
{
var variable = DerivativeVariables[pair.Key];
var variable = VariableNodes[pair.Key];
if (state.Map.Contains(variable))
{
derivatives.Add(builder.Build(pair.Value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public FrequencyBehavior(BehavioralBindingContext context)
var builder = new ComplexFunctionBuilder();
builder.VariableFound += (sender, args) =>
{
if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
if (args.Variable == null && VariableNodes.TryGetValue(args.Node, out var variable))
args.Variable = new FuncVariable<Complex>(variable.Name, () => variable.Value, variable.Unit);
}; var rhsLocs = state.Map[_branch];
bp.RegisterBuilder(context, builder);
Expand Down
3 changes: 2 additions & 1 deletion SpiceSharpBehavioral/Components/BuilderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using SpiceSharpBehavioral.Builders.Functions;
using SpiceSharpBehavioral;
using System.Numerics;
using SpiceSharpBehavioral.Parsers.Nodes;

namespace SpiceSharp.Components.BehavioralComponents
{
Expand Down Expand Up @@ -218,7 +219,7 @@ public static void RegisterDefaultBuilder(object sender, BuilderCreatedEventArgs
}
else
{
variables.Add("smallsig", new ConstantVariable<Complex>("smallsig", 1.0, _scalar));
variables.Add("smallsig", new ConstantVariable<Complex>("smallsig", 0.0, _scalar));
}

// Some standard constants
Expand Down
6 changes: 3 additions & 3 deletions SpiceSharpBehavioral/SpiceSharpBehavioral.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>3.1.2</Version>
<Version>3.1.3</Version>
<Authors>Sven Boulanger</Authors>
<Title>Spice#.Behavioral</Title>
<Description>Spice#.Behavioral is a library that allows using behavioral components in the circuit simulator Spice#.</Description>
Expand All @@ -11,12 +11,12 @@
<RepositoryUrl>https://github.com/SpiceSharp/SpiceSharpBehavioral</RepositoryUrl>
<PackageTags>circuit electronics netlist parser spice simulator simulation ode solver design behavioral modeling</PackageTags>
<PackageIconUrl></PackageIconUrl>
<AssemblyVersion>3.1.2.0</AssemblyVersion>
<AssemblyVersion>3.1.3.0</AssemblyVersion>
<PackageReleaseNotes>Refer to the GitHub release for release notes.</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Company />
<NeutralLanguage>en</NeutralLanguage>
<FileVersion>3.1.2.0</FileVersion>
<FileVersion>3.1.3.0</FileVersion>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
18 changes: 18 additions & 0 deletions SpiceSharpBehavioralTest/Components/BehavioralResistorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,23 @@ public void When_ZeroDerivative_Expect_Reference()
};
op.Run(ckt);
}

[Test]
public void When_If_Expect_Reference()
{
var ckt = new Circuit(
new VoltageSource("V1", "2", "0", 0.5),
new VoltageSource("V2", "1", "0", 10),
new BehavioralResistor("RSwitch", "1", "0", "IF(v(2, 0) >= 1, 10, (v(2, 0) <= 0 ? 1000000 : (exp(8.05904782547916 + 3 * -11.5129254649702 * (v(2, 0)-0.5)/(2*1) - 2 * -11.5129254649702 * pow(v(2, 0)-0.5, 3)/(pow(1,3))))))")
);
var op = new OP("Voltage switch simulation");
var refExport = new RealCurrentExport(op, "V2");
op.ExportSimulationData += (sender, args) =>
{
Assert.AreEqual(-0.00316228, refExport.Value, 1e-8);
};

op.Run(ckt);
}
}
}
1 change: 1 addition & 0 deletions SpiceSharpBehavioralTest/Parsers/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static IEnumerable<TestCaseData> Expressions
yield return new TestCaseData("min(-2,6*2)", Node.Function("min", Node.Minus(2.0), Node.Multiply(6.0, 2.0))).SetName("{m}(\"min(-2,6*2)\")"); // Function with multiple arguments
yield return new TestCaseData("rnd()", Node.Function("rnd")).SetName("{m}(\"rnd()\")"); // Function without arguments
yield return new TestCaseData("-.14e3", Node.Minus(Node.Constant(0.14e3))).SetName("{m}(-.14e3)");
yield return new TestCaseData("V(2,0)", Node.Voltage("2", "0"));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions SpiceSharpBehavioralTest/SpiceSharpBehavioralTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@
<Version>3.13.2</Version>
</PackageReference>
<PackageReference Include="NUnit.Console">
<Version>3.12.0</Version>
<Version>3.13.0</Version>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter">
<Version>4.0.0</Version>
<Version>4.2.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down