public void When_BuildNodeFunctions_Expect_Reference(Node node, double expected)
        {
            var builder = new RealFunctionBuilder();

            builder.RegisterDefaultFunctions();
            Assert.AreEqual(expected, builder.Build(node).Invoke(), 1e-20);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Biasing"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="context"/> is <c>null</c>.</exception>
        public Biasing(BehavioralBindingContext context)
            : base(context)
        {
            var bp    = context.GetParameterSet <Parameters>();
            var state = context.GetState <IBiasingSimulationState>();

            _variables = new OnePort <double>(
                state.GetSharedVariable(context.Nodes[0]),
                state.GetSharedVariable(context.Nodes[1]));
            _branch = state.CreatePrivateVariable(Name.Combine("branch"), Units.Ampere);

            // 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);
            var builder = new RealFunctionBuilder();

            builder.VariableFound += (sender, args) =>
            {
                if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
                {
                    args.Variable = variable;
                }
            };
            bp.RegisterBuilder(context, builder);
            var derivatives         = new List <Func <double> >(Derivatives.Count);
            var derivativeVariables = new List <IVariable <double> >(Derivatives.Count);
            var matLocs             = new List <MatrixLocation>(Derivatives.Count);
            var rhsLocs             = state.Map[_branch];

            foreach (var pair in Derivatives)
            {
                var variable = DerivativeVariables[pair.Key];
                if (state.Map.Contains(variable))
                {
                    derivatives.Add(builder.Build(pair.Value));
                    derivativeVariables.Add(variable);
                    matLocs.Add(new MatrixLocation(rhsLocs, state.Map[variable]));
                }
            }
            _value = builder.Build(Function);

            // Get the matrix elements
            _derivatives         = derivatives.ToArray();
            _values              = new double[_derivatives.Length];
            _derivativeVariables = derivativeVariables.ToArray();
            _elements            = new ElementSet <double>(state.Solver, matLocs.ToArray());
            int br  = state.Map[_branch];
            int pos = state.Map[_variables.Positive];
            int neg = state.Map[_variables.Negative];

            _coreElements = new ElementSet <double>(state.Solver, new[] {
                new MatrixLocation(br, pos),
                new MatrixLocation(br, neg),
                new MatrixLocation(pos, br),
                new MatrixLocation(neg, br)
            }, new[] { br });
        }
        public void When_BuildRandomNode_Expect_NoException()
        {
            var builder = new RealFunctionBuilder();

            builder.RegisterDefaultFunctions();
            var func = builder.Build(Node.Function("rnd", new Node[0]));

            func();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Biasing"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="context"/> is <c>null</c>.</exception>
        public Biasing(BehavioralBindingContext context)
            : base(context)
        {
            var bp    = context.GetParameterSet <Parameters>();
            var state = context.GetState <IBiasingSimulationState>();

            _variables = new OnePort <double>(
                state.GetSharedVariable(context.Nodes[0]),
                state.GetSharedVariable(context.Nodes[1]));

            // 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);
            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;
                }
            };
            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];
                if (state.Map.Contains(variable))
                {
                    derivatives.Add(builder.Build(pair.Value));
                    derivativeVariables.Add(variable);
                    matLocs.Add(new MatrixLocation(rhsLocs[0], state.Map[variable]));
                    matLocs.Add(new MatrixLocation(rhsLocs[1], state.Map[variable]));
                }
            }
            _value               = builder.Build(Function);
            _derivatives         = derivatives.ToArray();
            _derivativeVariables = derivativeVariables.ToArray();
            _values              = new double[_derivatives.Length * 2 + 2];

            // Get the matrix elements
            _elements = new ElementSet <double>(state.Solver, matLocs.ToArray(), rhsLocs);
        }
        public void When_BuildVariable_Expect_Reference()
        {
            var builder  = new RealFunctionBuilder();
            var variable = new GetSetVariable <double>("a", Units.Volt);

            builder.VariableFound += (sender, args) =>
            {
                if (args.Variable == null && args.Node.Name == "a")
                {
                    args.Variable = variable;
                }
            };

            variable.Value = 2.0;
            Assert.AreEqual(5.0, builder.Build(Node.Variable("a") + 3.0).Invoke(), 1e-20);
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Time"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="context"/> is <c>null</c>.</exception>
        public Time(BehavioralBindingContext context)
            : base(context)
        {
            var bp    = context.GetParameterSet <Parameters>();
            var state = context.GetState <IBiasingSimulationState>();

            _method = context.GetState <IIntegrationMethod>();
            _time   = context.GetState <ITimeSimulationState>();

            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;
                }
            };
            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];
                if (state.Map.Contains(variable))
                {
                    derivatives.Add(builder.Build(pair.Value));
                    derivativeVariables.Add(variable);
                    matLocs.Add(new MatrixLocation(rhsLocs[0], state.Map[variable]));
                    matLocs.Add(new MatrixLocation(rhsLocs[1], state.Map[variable]));
                }
            }
            _value               = builder.Build(Function);
            _derivatives         = derivatives.ToArray();
            _values              = new double[_derivatives.Length * 2 + 2];
            _derivativeVariables = derivativeVariables.ToArray();

            // Get the matrix elements
            _elements = new ElementSet <double>(state.Solver, matLocs.ToArray(), rhsLocs);

            // Create the derivative
            _qcap = _method.CreateDerivative();
        }
        public void When_FunctionCall_Expect_Reference()
        {
            var builder = new RealFunctionBuilder();
            var func1   = builder.Build(Node.Add(1.0, 2.0));

            builder.FunctionFound += (sender, args) =>
            {
                if (!args.Created && args.Function.Name == "f")
                {
                    args.ILState.Call(func1.Invoke);
                    args.Created = true;
                }
            };
            var func = builder.Build(Node.Function("f") * 2.0);

            Assert.AreEqual(6.0, func(), 1e-20);
        }