Exemplo n.º 1
0
        /// <summary>
        /// Export model code
        /// </summary>
        /// <param name="mparams">The model parameters</param>
        /// <returns></returns>
        public override string ExportModel(SpiceParam mparams)
        {
            string           code     = GetModelCode(mparams);
            HashSet <string> leftover = new HashSet <string>();

            return(Code.Format(code));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Apply parameters
        /// </summary>
        /// <param name="code">The code to be replaced</param>
        /// <param name="sp">The parameters</param>
        /// <param name="leftover">The leftover variables that could not be found</param>
        /// <returns></returns>
        private string ApplyParameters(string code, string obj, SpiceParam sp, HashSet <string> leftover, string prefix = "")
        {
            // The model parameters can be replaced by just their name
            Regex mv = new Regex($@"{obj}\s*\-\>\s*(?<var>\w+)\s*(?<assignment>\=)?");

            return(mv.Replace(code, (Match m) =>
            {
                // Check if the model variable exists
                string var = m.Groups["var"].Value;
                if (sp.GivenVariable.ContainsKey(var))
                {
                    return prefix + sp.GivenVariable[var] + ".Given";
                }
                if (!sp.Variables.Contains(var))
                {
                    leftover.Add(var);
                }
                else if (sp.GivenVariable.ContainsValue(var) && m.Groups["assignment"].Success)
                {
                    var += ".Value";
                }

                if (m.Groups["assignment"].Success)
                {
                    return prefix + var + " =";
                }
                return prefix + var;
            }));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the model code
        /// </summary>
        /// <param name="mparams">The model parameters</param>
        /// <returns></returns>
        public override string ExportModel(SpiceParam mparams)
        {
            string code = GetModelCode(mparams);

            code = ApplyCircuit(code);

            return(Code.Format(code));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get the device code
        /// </summary>
        /// <param name="mparams">The model parameters</param>
        /// <param name="dparams">The device parameters</param>
        /// <returns></returns>
        public override string ExportDevice(SpiceParam mparams, SpiceParam dparams)
        {
            string code = GetDeviceCode(mparams, dparams);

            code = ApplyCircuit(code);

            return(Code.Format(code));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Export model code
        /// </summary>
        /// <param name="p">The parameter</param>
        /// <returns></returns>
        public override string ExportModel(SpiceParam modelparams)
        {
            string code = GetModelCode(modelparams);

            code = GetDefaultValues(code, ModelDefaultValues);

            return(Code.Format(code));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Get the device code
        /// </summary>
        /// <param name="mparams">The model parameters</param>
        /// <param name="dparams">The device parameters</param>
        /// <returns></returns>
        public override string ExportDevice(SpiceParam mparams, SpiceParam dparams)
        {
            string code = GetDeviceCode(mparams, dparams);

            // Replace CKTterr by the method
            code = Regex.Replace(code, @"CKTterr\(\s*(?<state>\w+)\s*,\s*ckt\s*,\s*(?<var>\w+)\s*\)\s*;", (Match m) => $"method.Terr({states} + {m.Groups["state"].Value}, ckt, ref {m.Groups["var"].Value});");

            return(Code.Format(code));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get the model code
        /// </summary>
        /// <param name="mparam">The model parameters</param>
        /// <param name="ckt">The circuit identifier</param>
        /// <returns></returns>
        protected string GetModelCode(SpiceParam mparam, string ckt = "ckt")
        {
            string code = ModelCode.Trim();

            code = ApplyGeneral(code, ckt);
            code = ApplyParameters(code, ModelParameter, mparam, ModelVariablesExtra);

            return(code);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Export device code
        /// </summary>
        /// <param name="p">The parameter</param>
        /// <returns></returns>
        public override string ExportDevice(SpiceParam deviceparams, SpiceParam modelparams)
        {
            string code = GetDeviceCode(deviceparams, modelparams);

            code = GetDefaultValues(code, DeviceDefaultValues);
            code = GetNodes(code);
            code = GetDeviceStates(code);

            return(Code.Format(code));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Export device code
        /// </summary>
        /// <param name="mparams">The model parameters</param>
        /// <param name="dparams">The device parameters</param>
        /// <returns></returns>
        public override string ExportDevice(SpiceParam mparams, SpiceParam dparams)
        {
            string code = GetDeviceCode(mparams, dparams);

            // Find temperature stuff
            code = Regex.Replace(code, @"ckt\s*\-\>\s*CKTtemp", "ckt.State.Temperature");
            code = Regex.Replace(code, @"ckt\s*\-\>\s*CKTnomTemp", "ckt.State.NominalTemperature");

            // -> Is never possible, so let's go for dots
            code = Regex.Replace(code, @"\s*\-\>\s*", ".");

            return(Code.Format(code));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Get the device code
        /// </summary>
        /// <param name="mparam">The model parameters</param>
        /// <param name="dparam">The device parameters</param>
        /// <param name="ckt">The circuit identifier</param>
        /// <param name="model">The model identifier</param>
        /// <returns></returns>
        protected string GetDeviceCode(SpiceParam mparam, SpiceParam dparam, string ckt = "ckt", string model = "Model")
        {
            string code = DeviceCode.Trim();

            code = ApplyGeneral(code, ckt);
            code = ApplyParameters(code, DeviceParameter, dparam, DeviceVariablesExtra);
            code = ApplyParameters(code, ModelParameter, mparam, ModelVariablesExtra, model + ".");

            // Replace all shared variables
            foreach (var s in SharedLocalVariables)
            {
                code = Regex.Replace(code, $@"(?<!\w){s.Key}(?!\w)", $"{model}.{s.Key}");
            }

            return(code);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Export the code for the device
 /// </summary>
 /// <param name="mparams"></param>
 /// <param name="dparams"></param>
 /// <returns></returns>
 public abstract string ExportDevice(SpiceParam mparams, SpiceParam dparams);
Exemplo n.º 12
0
 /// <summary>
 /// Export the code for the model
 /// </summary>
 /// <param name="mparams">The model parameters</param>
 /// <returns></returns>
 public abstract string ExportModel(SpiceParam mparams);
Exemplo n.º 13
0
        /// <summary>
        /// Read a device
        /// </summary>
        /// <param name="dev"></param>
        public SpiceClassGenerator(SpiceDevice dev, Methods export = Methods.All)
        {
            // Get all model and device methods
            dev.BuildMethodTable();
            name = dev.DeviceName.ToUpper();

            // Extract the parameters
            paramExtr = new ParameterExtractor();
            paramExtr.Extract(dev);

            // Get all parameters
            paramMod = new SpiceParam(dev, paramExtr.Model, SpiceDevice.Methods.ModelParam, SpiceDevice.Methods.ModelAsk);
            paramDev = new SpiceParam(dev, paramExtr.Device, SpiceDevice.Methods.Param, SpiceDevice.Methods.Ask);

            // Extract the setup
            if (export.HasFlag(Methods.Setup))
            {
                setup    = new SpiceSetup(dev);
                setupDev = setup.ExportDevice(paramMod, paramDev);
                setupMod = setup.ExportModel(paramMod);
                foreach (var v in setup.SharedLocalVariables)
                {
                    shared.Add(v.Key, v.Value);
                }
                foreach (var v in setup.ModelVariablesExtra)
                {
                    modelextra.Add(v);
                }
                foreach (var v in setup.DeviceVariablesExtra)
                {
                    deviceextra.Add(v);
                }

                // Update the methods
                paramMod.UpdateMethods(dev, setup);
                paramDev.UpdateMethods(dev, setup);
            }

            // Extract the state definitions
            if (setup != null)
            {
                definitions = new SpiceDefinitions(dev, setup);
                foreach (var v in definitions.StateNames)
                {
                    paramDev.Variables.Add(v);
                }
            }

            // Temperature-dependent calculations
            if (export.HasFlag(Methods.Temperature))
            {
                temp    = new SpiceTemperature(dev);
                tempDev = temp.ExportDevice(paramMod, paramDev);
                tempMod = temp.ExportModel(paramMod);
                foreach (var v in temp.SharedLocalVariables)
                {
                    if (shared.ContainsKey(v.Key) && shared[v.Key] != v.Value)
                    {
                        throw new Exception($"Cannot share variable {v.Key}");
                    }
                    shared.Add(v.Key, v.Value);
                }
                foreach (var v in temp.ModelVariablesExtra)
                {
                    modelextra.Add(v);
                }
                foreach (var v in temp.DeviceVariablesExtra)
                {
                    deviceextra.Add(v);
                }
            }

            // Loading
            if (export.HasFlag(Methods.Load) && setup != null)
            {
                load    = new SpiceLoad(dev, setup);
                loadDev = load.ExportDevice(paramMod, paramDev);
                loadMod = load.ExportModel(paramMod);
                foreach (var v in load.SharedLocalVariables)
                {
                    if (shared.ContainsKey(v.Key) && shared[v.Key] != v.Value)
                    {
                        throw new Exception($"Cannot share variable {v.Key}");
                    }
                    shared.Add(v.Key, v.Value);
                }
                foreach (var v in load.ModelVariablesExtra)
                {
                    modelextra.Add(v);
                }
                foreach (var v in load.DeviceVariablesExtra)
                {
                    deviceextra.Add(v);
                }
            }

            // AC loading
            if (export.HasFlag(Methods.AcLoad) && setup != null)
            {
                acload    = new SpiceAcLoad(dev, setup);
                acloadDev = acload.ExportDevice(paramMod, paramDev);
                acloadMod = acload.ExportModel(paramMod);
                foreach (var v in acload.SharedLocalVariables)
                {
                    if (shared.ContainsKey(v.Key) && shared[v.Key] != v.Value)
                    {
                        throw new Exception($"Cannot share variable {v.Key}");
                    }
                    shared.Add(v.Key, v.Value);
                }
                foreach (var v in acload.ModelVariablesExtra)
                {
                    modelextra.Add(v);
                }
                foreach (var v in acload.DeviceVariablesExtra)
                {
                    deviceextra.Add(v);
                }
            }

            // PZ loading
            if (export.HasFlag(Methods.PzLoad))
            {
                pzload    = new SpicePzLoad(dev, setup);
                pzloadDev = pzload.ExportDevice(paramMod, paramDev);
                pzloadMod = pzload.ExportModel(paramMod);
                foreach (var v in pzload.SharedLocalVariables)
                {
                    if (shared.ContainsKey(v.Key) && shared[v.Key] != v.Value)
                    {
                        throw new Exception($"Cannot share variable {v.Key}");
                    }
                    shared.Add(v.Key, v.Value);
                }
                foreach (var v in pzload.ModelVariablesExtra)
                {
                    modelextra.Add(v);
                }
                foreach (var v in pzload.DeviceVariablesExtra)
                {
                    deviceextra.Add(v);
                }
            }

            // Truncation
            if (export.HasFlag(Methods.Truncate))
            {
                trunc    = new SpiceTruncate(dev, setup);
                truncDev = trunc.ExportDevice(paramMod, paramDev);
                truncMod = trunc.ExportModel(paramMod);
                foreach (var v in trunc.SharedLocalVariables)
                {
                    if (shared.ContainsKey(v.Key) && shared[v.Key] != v.Value)
                    {
                        throw new Exception($"Cannot share variable {v.Key}");
                    }
                    shared.Add(v.Key, v.Value);
                }
                foreach (var v in trunc.ModelVariablesExtra)
                {
                    modelextra.Add(v);
                }
                foreach (var v in trunc.DeviceVariablesExtra)
                {
                    deviceextra.Add(v);
                }
            }

            // Apply default values!
            string[] names = paramMod.Declarations.Keys.ToArray();
            foreach (string n in names)
            {
                // Find out if the name of the variable with this ID
                if (setup.ModelDefaultValues.ContainsKey(n))
                {
                    paramMod.Declarations[n] = Regex.Replace(paramMod.Declarations[n], @"\(\);", $"({setup.ModelDefaultValues[n]});");
                }
            }
            names = paramDev.Declarations.Keys.ToArray();
            foreach (string n in names)
            {
                // Find out if the name of the variable with this ID
                if (setup.DeviceDefaultValues.ContainsKey(n))
                {
                    paramDev.Declarations[n] = Regex.Replace(paramDev.Declarations[n], @"\(\);", $"({setup.DeviceDefaultValues[n]});");
                }
            }
        }