1- using SpiceSharp . Algebra ;
2- using SpiceSharp . Attributes ;
1+ using SpiceSharp . Attributes ;
32using SpiceSharp . Behaviors ;
4- using SpiceSharp . Components . CommonBehaviors ;
53using SpiceSharp . Simulations ;
64using SpiceSharpBehavioral ;
75using SpiceSharpBehavioral . Builders ;
6+ using SpiceSharpBehavioral . Builders . Functions ;
87using SpiceSharpBehavioral . Parsers . Nodes ;
98using System ;
109using System . Collections . Generic ;
1110using System . Linq ;
1211using 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}
0 commit comments