Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BiasingBehavior"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="context"/> is <c>null</c>.</exception>
        public BiasingBehavior(BehavioralBindingContext context)
            : base(context)
        {
            // Make sure that we have access to the voltage over the behavior
            var bp    = context.GetParameterSet <Parameters>();
            var state = context.GetState <IBiasingSimulationState>();

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

            // Create the derivatives, while also giving access to the voltage across the capacitor
            var replacer = new NodeReplacer
            {
                Map = new Dictionary <VariableNode, Node>(new VariableNodeComparer(null, null, bp.VariableComparer))
                {
                    { Node.Variable("x"), Node.Voltage(context.Nodes[0]) - Node.Voltage(context.Nodes[1]) }
                }
            };

            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));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FrequencyBehavior"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="context"/> is <c>null</c>.</exception>
        public FrequencyBehavior(BehavioralBindingContext context)
            : base(context)
        {
            var bp = context.GetParameterSet<Parameters>();
            var state = context.GetState<IComplexSimulationState>();
            _variables = new OnePort<Complex>(
                state.GetSharedVariable(context.Nodes[0]),
                state.GetSharedVariable(context.Nodes[1]));

            // Build the functions
            var derivatives = new List<Func<Complex>>(Derivatives.Count);
            var builder = new ComplexFunctionBuilder();
            builder.VariableFound += (sender, args) =>
            {
                if (args.Variable == null && DerivativeVariables.TryGetValue(args.Node, out var variable))
                    args.Variable = new FuncVariable<Complex>(variable.Name, () => variable.Value, variable.Unit);
            };
            bp.RegisterBuilder(context, builder);
            var rhsLocs = _variables.GetRhsIndices(state.Map);
            var matLocs = new List<MatrixLocation>(Derivatives.Count * 2);
            foreach (var pair in Derivatives)
            {
                var variable = context.MapNode(state, pair.Key);
                if (state.Map.Contains(variable))
                {
                    derivatives.Add(builder.Build(pair.Value));
                    matLocs.Add(new MatrixLocation(rhsLocs[0], state.Map[variable]));
                    matLocs.Add(new MatrixLocation(rhsLocs[1], state.Map[variable]));
                }
            }

            // Get the matrix elements
            _derivatives = derivatives.ToArray();
            _elements = new ElementSet<Complex>(state.Solver, matLocs.ToArray());
        }
        /// <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 });
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Frequency"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="context"/> is <c>null</c>.</exception>
        public Frequency(BehavioralBindingContext context)
            : base(context)
        {
            var bp    = context.GetParameterSet <Parameters>();
            var state = context.GetState <IComplexSimulationState>();

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

            // Build the functions
            var nVariables = new Dictionary <VariableNode, IVariable <Complex> >(Derivatives.Comparer);

            foreach (var variable in Derivatives.Keys)
            {
                var orig = DerivativeVariables[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))
                {
                    args.Variable = new FuncVariable <Complex>(variable.Name, () => variable.Value, variable.Unit);
                }
            };
            bp.RegisterBuilder(context, builder);
            var derivatives = new List <Func <Complex> >(Derivatives.Count);
            var rhsLocs     = state.Map[_branch];
            var matLocs     = new List <MatrixLocation>(Derivatives.Count);

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

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

            _coreElements = new ElementSet <Complex>(state.Solver, new[] {
                new MatrixLocation(br, pos),
                new MatrixLocation(br, neg),
                new MatrixLocation(pos, br),
                new MatrixLocation(neg, br)
            });
        }
        /// <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);
        }
Exemplo n.º 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();
        }