示例#1
0
        public override SpiceSharp.Components.Model Generate(string id, string type, ParameterCollection parameters, ICircuitContext context)
        {
            var clonedParameters = (ParameterCollection)parameters.Clone();

            int    level = 1;
            string version = null;
            int    lindex = -1, vindex = -1;

            for (int i = 0; i < clonedParameters.Count; i++)
            {
                if (clonedParameters[i] is AssignmentParameter ap)
                {
                    if (ap.Name.ToLower() == "level")
                    {
                        lindex = i;
                        level  = (int)Math.Round(context.Evaluator.EvaluateDouble(ap.Value));
                    }

                    if (ap.Name.ToLower() == "version")
                    {
                        vindex  = i;
                        version = ap.Value.ToLower();
                    }

                    if (vindex >= 0 && lindex >= 0)
                    {
                        break;
                    }
                }
            }

            if (lindex >= 0)
            {
                clonedParameters.RemoveAt(lindex);
            }

            if (vindex >= 0)
            {
                clonedParameters.RemoveAt(vindex < lindex ? vindex : vindex - 1);
            }

            // Generate the model
            SpiceSharp.Components.Model model = null;
            if (Levels.ContainsKey(level))
            {
                model = Levels[level].Invoke(id, type, version);
            }
            else
            {
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Error, $"Unknown mosfet model level {level}", parameters.LineInfo));
                return(null);
            }

            // Read all the parameters
            SetParameters(context, model, clonedParameters);

            return(model);
        }
示例#2
0
        public override SpiceSharp.Components.Model Generate(string id, string type, ParameterCollection parameters, ICircuitContext context)
        {
            SpiceSharp.Components.Model model = null;

            switch (type.ToLower())
            {
            case "sw": model = new VoltageSwitchModel(id); break;

            case "csw": model = new CurrentSwitchModel(id); break;

            case "vswitch": model = new VSwitchModel(id); break;

            case "iswitch": model = new ISwitchModel(id); break;
            }

            if (model != null)
            {
                SetParameters(context, model, parameters);
            }

            return(model);
        }
        public override SpiceSharp.Components.Model Generate(string id, string type, ParameterCollection parameters, ICircuitContext context)
        {
            SpiceSharp.Components.Model model = null;

            switch (type.ToLower())
            {
            case "res":
            case "r":
                model = new ResistorModel(id);
                break;

            case "c":
                model = new CapacitorModel(id);
                break;
            }

            if (model != null)
            {
                SetParameters(context, model, parameters);
            }

            return(model);
        }
        /// <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);
            }
        }
        /// <summary>
        /// Generates a current switch.
        /// </summary>
        /// <param name="name">Name of current switch.</param>
        /// <param name="parameters">Parameters of current switch.</param>
        /// <param name="context">Reading context.</param>
        /// <returns>
        /// A new instance of current switch.
        /// </returns>
        protected SpiceSharp.Components.Component GenerateCurrentSwitch(string name, ParameterCollection parameters, ICircuitContext context)
        {
            if (parameters.Count < 4)
            {
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "Wrong parameter count for current switch", parameters.LineInfo));
                return(null);
            }

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

            if (context.ModelsRegistry.FindModel <Model>(modelName) is ISwitchModel s)
            {
                Resistor resistor      = new Resistor(name);
                Model    resistorModel = s;
                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, s);

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

                    double rOff = resistorModel.ParameterSets.GetParameter <double>("roff");

                    string resExpression = $"pos(table(i({parameters.Get(2)}), @{resistorModel.Name}[ioff], @{resistorModel.Name}[roff] , @{resistorModel.Name}[ion], @{resistorModel.Name}[ron]), {rOff.ToString(CultureInfo.InvariantCulture)})";
                    context.SetParameter(resistor, "resistance", resExpression, beforeTemperature: true, onload: true);
                });
                return(resistor);
            }
            else
            {
                CurrentSwitch csw = new CurrentSwitch(name);
                context.CreateNodes(csw, parameters);

                // Get the controlling voltage source
                if (parameters[2] is WordParameter || parameters[2] is IdentifierParameter)
                {
                    csw.ControllingName = context.NameGenerator.GenerateObjectName(parameters.Get(2).Image);
                }
                else
                {
                    context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "Voltage source name expected", parameters.LineInfo));
                    return(null);
                }

                // Get the model
                context.SimulationPreparations.ExecuteActionBeforeSetup((simulation) =>
                {
                    context.ModelsRegistry.SetModel(
                        csw,
                        simulation,
                        parameters.Get(3),
                        $"Could not find model {parameters.Get(3)} for current switch {name}",
                        (CurrentSwitchModel model) => csw.Model = model.Name,
                        context.Result);
                });

                // Optional on or off
                if (parameters.Count > 4)
                {
                    switch (parameters.Get(4).Image.ToLower())
                    {
                    case "on":
                        csw.ParameterSets.SetParameter("on");
                        break;

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

                    default:
                        context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "ON or OFF expected", parameters.LineInfo));
                        return(csw);
                    }
                }

                return(csw);
            }
        }
示例#6
0
        public override SpiceSharp.Components.Component Generate(string componentIdentifier, string originalName, string type, ParameterCollection parameters, ICircuitContext context)
        {
            // Errors
            switch (parameters.Count)
            {
            case 0:
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader,
                                                                  ValidationEntryLevel.Error,
                                                                  $"Node expected for component {componentIdentifier}", parameters.LineInfo));
                return(null);

            case 1:
            case 2:
            case 3:
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader,
                                                                  ValidationEntryLevel.Error,
                                                                  $"Node expected", parameters.LineInfo));
                return(null);

            case 4:
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader,
                                                                  ValidationEntryLevel.Error,
                                                                  $"Model name expected", parameters.LineInfo));
                return(null);
            }

            // Get the model and generate a component for it
            SpiceSharp.Components.Component mosfet = null;
            var   modelNameParameter = parameters.Get(4);
            Model model = context.ModelsRegistry.FindModel <Model>(modelNameParameter.Image);

            if (model == null)
            {
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader,
                                                                  ValidationEntryLevel.Error,
                                                                  $"Could not find model {modelNameParameter.Image} for mosfet {originalName}", parameters.LineInfo));

                return(null);
            }

            if (Mosfets.ContainsKey(model.GetType()))
            {
                var mosfetDetails = Mosfets[model.GetType()].Invoke(componentIdentifier);
                mosfet = mosfetDetails.Mosfet;

                context.SimulationPreparations.ExecuteActionBeforeSetup((simulation) =>
                {
                    context.ModelsRegistry.SetModel(
                        mosfetDetails.Mosfet,
                        simulation,
                        modelNameParameter,
                        $"Could not find model {modelNameParameter.Image} for mosfet {componentIdentifier}",
                        mosfetDetails.SetModelAction,
                        context.Result);
                });
            }
            else
            {
                context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader,
                                                                  ValidationEntryLevel.Error,
                                                                  $"Invalid model {model.GetType()} for {componentIdentifier}", parameters.LineInfo));

                return(null);
            }

            // The rest is all just parameters
            context.CreateNodes(mosfet, parameters);
            SetParameters(context, mosfet, parameters.Skip(5), true);
            return(mosfet);
        }