public void When_BooleanParameter_Expect_DirectAccess()
        {
            // Create voltage source
            var s = new VoltageSwitch("SW 1");
            var p = s.Parameters;

            // Check on
            s.SetParameter("on", true);
            Assert.AreEqual(true, p.ZeroState);

            // Check off
            s.SetParameter("off", true);
            Assert.AreEqual(false, p.ZeroState);
        }
Example #2
0
        public void When_VSWBooleanParameter_Expect_DirectAccess()
        {
            // Create voltage source
            var s = new VoltageSwitch("SW 1");
            var p = s.ParameterSets.Get <SpiceSharp.Components.VoltageSwitchBehaviors.BaseParameters>();

            // Check on
            s.ParameterSets.SetParameter("on");
            Assert.AreEqual(true, p.ZeroState);

            // Check off
            s.ParameterSets.SetParameter("off");
            Assert.AreEqual(false, p.ZeroState);
        }
        private VoltageSwitch CreateVoltageSwitch(string name, string pos, string neg, string contPos, string contNeg, string model)
        {
            var vsw = new VoltageSwitch(name, pos, neg, contPos, contNeg, model);

            return(vsw);
        }
        /// <summary>
        /// Generates a voltage switch.
        /// </summary>
        /// <param name="name">Name of voltage switch to generate.</param>
        /// <param name="parameters">Parameters for voltage switch.</param>
        /// <param name="context">Reading context.</param>
        /// <returns>
        /// A new voltage switch.
        /// </returns>
        protected IEntity GenerateVoltageSwitch(string name, ParameterCollection parameters, ICircuitContext context)
        {
            if (parameters.Count < 5)
            {
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "Wrong parameter count for voltage switch", parameters.LineInfo));
                return(null);
            }

            string modelName = parameters.Get(4).Image;

            var model = context.ModelsRegistry.FindModel(modelName);

            if (model.Entity is VSwitchModel)
            {
                BehavioralResistor resistor = new BehavioralResistor(name);
                Model resistorModel         = model;
                context.CreateNodes(resistor, parameters.Take(BehavioralResistor.BehavioralResistorPinCount));
                var vSwitchModelParameters = resistorModel.Parameters as VSwitchModelParameters;

                double vOff = vSwitchModelParameters.OffVoltage;
                double rOff = vSwitchModelParameters.OffResistance;
                double vOn  = vSwitchModelParameters.OnVoltage;
                double rOn  = vSwitchModelParameters.OnResistance;

                string vc            = $"v({parameters.Get(2)}, {parameters.Get(3)})";
                double lm            = Math.Log(Math.Sqrt(rOn * rOff));
                double lr            = Math.Log(rOn / rOff);
                double vm            = (vOn + vOff) / 2.0;
                double vd            = vOn - vOff;
                string resExpression = GetVSwitchExpression(vOff, rOff, vOn, rOn, vc, lm, lr, vm, vd);

                resistor.Parameters.Expression  = resExpression;
                resistor.Parameters.ParseAction = (expression) =>
                {
                    var parser = new ExpressionParser(context.Evaluator.GetEvaluationContext(null), false, context.CaseSensitivity);
                    return(parser.Resolve(expression));
                };

                context.SimulationPreparations.ExecuteActionAfterSetup(
                    (simulation) =>
                {
                    if (context.ModelsRegistry is StochasticModelsRegistry stochasticModelsRegistry)
                    {
                        resistorModel = stochasticModelsRegistry.ProvideStochasticModel(name, simulation, model);

                        if (!context.Result.FindObject(resistorModel.Name, out _))
                        {
                            stochasticModelsRegistry.RegisterModelInstance(resistorModel);
                        }
                    }

                    vOff          = vSwitchModelParameters.OffVoltage;
                    rOff          = vSwitchModelParameters.OffResistance;
                    vOn           = vSwitchModelParameters.OnVoltage;
                    rOn           = vSwitchModelParameters.OnResistance;
                    lm            = Math.Log(Math.Sqrt(rOn * rOff));
                    lr            = Math.Log(rOn / rOff);
                    vm            = (vOn + vOff) / 2.0;
                    vd            = vOn - vOff;
                    resExpression = GetVSwitchExpression(vOff, rOff, vOn, rOn, vc, lm, lr, vm, vd);
                    resistor.Parameters.Expression  = resExpression;
                    resistor.Parameters.ParseAction = (expression) =>
                    {
                        var parser = new ExpressionParser(context.Evaluator.GetEvaluationContext(simulation), false, context.CaseSensitivity);
                        return(parser.Resolve(expression));
                    };
                });
                return(resistor);
            }

            VoltageSwitch vsw = new VoltageSwitch(name);

            context.CreateNodes(vsw, parameters);

            context.SimulationPreparations.ExecuteActionBeforeSetup((simulation) =>
            {
                context.ModelsRegistry.SetModel(
                    vsw,
                    simulation,
                    parameters.Get(4),
                    $"Could not find model {parameters.Get(4)} for voltage switch {name}",
                    (Context.Models.Model model2) => { vsw.Model = model2.Name; },
                    context.Result);
            });

            // Optional ON or OFF
            if (parameters.Count == 6)
            {
                switch (parameters.Get(5).Image.ToLower())
                {
                case "on":
                    vsw.SetParameter("on", true);
                    break;

                case "off":
                    vsw.SetParameter("off", true);
                    break;

                default:
                    context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "ON or OFF expected", parameters.LineInfo));
                    return(vsw);
                }
            }
            else if (parameters.Count > 6)
            {
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "Too many parameters for voltage switch", parameters.LineInfo));
                return(vsw);
            }

            return(vsw);
        }
        /// <summary>
        /// Generates a voltage switch.
        /// </summary>
        /// <param name="name">Name of voltage switch to generate.</param>
        /// <param name="parameters">Parameters for voltage switch.</param>
        /// <param name="context">Reading context.</param>
        /// <returns>
        /// A new voltage switch.
        /// </returns>
        protected SpiceSharp.Components.Component GenerateVoltageSwitch(string name, ParameterCollection parameters, ICircuitContext context)
        {
            if (parameters.Count < 5)
            {
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "Wrong parameter count for voltage switch", parameters.LineInfo));
                return(null);
            }

            string modelName = parameters.Get(4).Image;

            if (context.ModelsRegistry.FindModel <Model>(modelName) is VSwitchModel vmodel)
            {
                Resistor resistor      = new Resistor(name);
                Model    resistorModel = vmodel;
                context.CreateNodes(resistor, parameters.Take(2));
                context.SimulationPreparations.ExecuteTemperatureBehaviorBeforeLoad(resistor);

                context.SimulationPreparations.ExecuteActionBeforeSetup(
                    (simulation) =>
                {
                    if (context.ModelsRegistry is StochasticModelsRegistry stochasticModelsRegistry)
                    {
                        resistorModel = stochasticModelsRegistry.ProvideStochasticModel(name, simulation, vmodel);

                        if (!context.Result.FindObject(resistorModel.Name, out _))
                        {
                            stochasticModelsRegistry.RegisterModelInstance(resistorModel);
                            context.Result.Circuit.Add(resistorModel);
                        }
                    }

                    string resExpression =
                        $"table(v({parameters.Get(2)}, {parameters.Get(3)}), @{resistorModel.Name}[voff], @{resistorModel.Name}[roff] , @{resistorModel.Name}[von], @{resistorModel.Name}[ron])";
                    context.SetParameter(resistor, "resistance", resExpression, beforeTemperature: true, onload: true);
                });
                return(resistor);
            }
            else
            {
                VoltageSwitch vsw = new VoltageSwitch(name);
                context.CreateNodes(vsw, parameters);

                context.SimulationPreparations.ExecuteActionBeforeSetup((simulation) =>
                {
                    context.ModelsRegistry.SetModel(
                        vsw,
                        simulation,
                        parameters.Get(4),
                        $"Could not find model {parameters.Get(4)} for voltage switch {name}",
                        (VoltageSwitchModel model) => { vsw.Model = model.Name; },
                        context.Result);
                });

                // Optional ON or OFF
                if (parameters.Count == 6)
                {
                    switch (parameters.Get(5).Image.ToLower())
                    {
                    case "on":
                        vsw.ParameterSets.SetParameter("on");
                        break;

                    case "off":
                        vsw.ParameterSets.SetParameter("off");
                        break;

                    default:
                        context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "ON or OFF expected", parameters.LineInfo));
                        return(vsw);
                    }
                }
                else if (parameters.Count > 6)
                {
                    context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "Too many parameters for voltage switch", parameters.LineInfo));
                    return(vsw);
                }

                return(vsw);
            }
        }