/// <summary>
        /// Remap a variable node to an <see cref="IVariable{T}"/>.
        /// </summary>
        /// <typeparam name="T">the variable type.</typeparam>
        /// <param name="factory">The variable factory.</param>
        /// <param name="node">The node.</param>
        /// <param name="ownBranch">The branch.</param>
        /// <returns>The variable.</returns>
        public IVariable <T> MapNode <T>(IVariableFactory <IVariable <T> > factory, VariableNode node, IVariable <T> ownBranch = null)
        {
            switch (node.NodeType)
            {
            case NodeTypes.Voltage:
                return(factory.GetSharedVariable(node.Name));

            case NodeTypes.Current:
                var container = Simulation.EntityBehaviors[node.Name];
                if (container == Behaviors)
                {
                    if (ownBranch == null)
                    {
                        throw new SpiceSharpException($"The behavior for {Entity.Name} does not define a branch current.");
                    }
                    return(ownBranch);
                }
                else if (container.TryGetValue <IBranchedBehavior <T> >(out var branched))
                {
                    return(branched.Branch);
                }
                else if (typeof(T) == typeof(double) && container.TryGetValue(out IBiasingBehavior tmpb) && tmpb is CurrentSources.Biasing biasing)
                {
                    var result = new FuncVariable <double>($"I({biasing.Name})", () => biasing.Current, Units.Ampere);
                    return(result as IVariable <T>);
                }
                else if (typeof(T) == typeof(Complex) && container.TryGetValue(out IFrequencyBehavior tmpf) && tmpf is CurrentSources.Frequency frequency)
                {
                    var result = new FuncVariable <Complex>($"I({frequency.Name})", () => frequency.ComplexCurrent, Units.Ampere);
                    return(result as IVariable <T>);
                }
예제 #2
0
        public void Test___Method_GetValue___Generic()
        {
            var testee = new FuncVariable <string>()
            {
                Func = () => "foo"
            };

            Assert.AreEqual("foo", testee.GetValue <string>());
        }
예제 #3
0
        public void Test___Method_GetValue()
        {
            var testee = new FuncVariable()
            {
                Func = () => 10
            };

            Assert.AreEqual(10, testee.GetValue <int>());
        }
        /// <summary>
        /// Remap a variable node to an <see cref="IVariable{T}"/>.
        /// </summary>
        /// <typeparam name="T">the variable type.</typeparam>
        /// <param name="factory">The variable factory.</param>
        /// <param name="node">The node.</param>
        /// <param name="ownBranch">The branch.</param>
        /// <returns>The variable.</returns>
        public IVariable <T> MapNode <T>(IVariableFactory <IVariable <T> > factory, VariableNode node, IVariable <T> ownBranch = null)
        {
            switch (node.NodeType)
            {
            case NodeTypes.Voltage:
                return(factory.GetSharedVariable(node.Name));

            case NodeTypes.Current:
                IBehaviorContainer container;
                IBiasingBehavior   tmpb;
                IFrequencyBehavior tmpf;

                // Get the relevant behaviors
                if (Simulation.EntityBehaviors.Comparer.Equals(node.Name, Behaviors.Name))
                {
                    container = Behaviors;
                }
                else
                {
                    container = Simulation.EntityBehaviors[node.Name];
                }

                if (container == Behaviors)
                {
                    if (ownBranch == null)
                    {
                        throw new SpiceSharpException($"The behavior for {Entity.Name} does not define a branch current.");
                    }
                    return(ownBranch);
                }
                else if (container.TryGetValue <IBranchedBehavior <T> >(out var branched))
                {
                    // Best-case scenario! Our behaviors define a branched behavior!
                    return(branched.Branch);
                }
                else if (typeof(T) == typeof(double) && container.TryGetValue(out tmpb) && tmpb is CurrentSources.Biasing biasing)
                {
                    // If whatever is being is asked is the current from a current source, then we also don't have a problem
                    var result = new FuncVariable <double>($"I({biasing.Name})", () => biasing.Current, Units.Ampere);
                    return(result as IVariable <T>);
                }
                else if (typeof(T) == typeof(Complex) && container.TryGetValue(out tmpf) && tmpf is CurrentSources.Frequency frequency)
                {
                    // Current source = no problem
                    var result = new FuncVariable <Complex>($"I({frequency.Name})", () => frequency.ComplexCurrent, Units.Ampere);
                    return(result as IVariable <T>);
                }
                else
                {
                    var result = container.CreatePropertyGetter <T>("i");
                    if (result == null)
                    {
                        goto default;
                    }
                    SpiceSharpWarning.Warning(this, SpiceSharpBehavioral.Properties.Resources.LooseLinkCurrent.FormatString(container.Name));
                    return(new FuncVariable <T>($"@{container.Name}[i]", result, Units.Ampere));
                }

            default:
                throw new SpiceSharpException($"Could not determine the variable {node.Name}");
            }
        }