11using SpiceSharp . Algebra ;
2+ using SpiceSharp . Attributes ;
23using SpiceSharp . Behaviors ;
34using SpiceSharp . Components . CommonBehaviors ;
45using SpiceSharp . Simulations ;
@@ -16,6 +17,10 @@ namespace SpiceSharp.Components.BehavioralComponents
1617 /// A context for behavioral components.
1718 /// </summary>
1819 /// <seealso cref="ComponentBindingContext" />
20+ [ BindingContextFor ( typeof ( BehavioralCapacitor ) ) ]
21+ [ BindingContextFor ( typeof ( BehavioralResistor ) ) ]
22+ [ BindingContextFor ( typeof ( BehavioralVoltageSource ) ) ]
23+ [ BindingContextFor ( typeof ( BehavioralCurrentSource ) ) ]
1924 public class BehavioralBindingContext : ComponentBindingContext
2025 {
2126 /// <summary>
@@ -82,27 +87,47 @@ public IVariable<T> MapNode<T>(IVariableFactory<IVariable<T>> factory, VariableN
8287 return factory . GetSharedVariable ( node . Name ) ;
8388
8489 case NodeTypes . Current :
85- var container = Simulation . EntityBehaviors [ node . Name ] ;
90+ IBehaviorContainer container ;
91+ IBiasingBehavior tmpb ;
92+ IFrequencyBehavior tmpf ;
93+
94+ // Get the relevant behaviors
95+ if ( Simulation . EntityBehaviors . Comparer . Equals ( node . Name , Behaviors . Name ) )
96+ container = Behaviors ;
97+ else
98+ container = Simulation . EntityBehaviors [ node . Name ] ;
99+
86100 if ( container == Behaviors )
87101 {
88102 if ( ownBranch == null )
89103 throw new SpiceSharpException ( $ "The behavior for { Entity . Name } does not define a branch current.") ;
90104 return ownBranch ;
91105 }
92106 else if ( container . TryGetValue < IBranchedBehavior < T > > ( out var branched ) )
107+ {
108+ // Best-case scenario! Our behaviors define a branched behavior!
93109 return branched . Branch ;
94- 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 )
95112 {
113+ // If whatever is being is asked is the current from a current source, then we also don't have a problem
96114 var result = new FuncVariable < double > ( $ "I({ biasing . Name } )", ( ) => biasing . Current , Units . Ampere ) ;
97115 return result as IVariable < T > ;
98116 }
99- 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 )
100118 {
119+ // Current source = no problem
101120 var result = new FuncVariable < Complex > ( $ "I({ frequency . Name } )", ( ) => frequency . ComplexCurrent , Units . Ampere ) ;
102121 return result as IVariable < T > ;
103122 }
104123 else
105- 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+ }
106131
107132 default :
108133 throw new SpiceSharpException ( $ "Could not determine the variable { node . Name } ") ;
0 commit comments