From 18a9eca574abc4dcb3a149da3b8dd7e99cff6ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Go=C5=82=C4=99biowski?= Date: Sun, 20 Oct 2024 17:06:44 +0200 Subject: [PATCH] Add example about custom Mosfet --- .../Examples/Circuits/MosfetExample.cir | 4 +++ .../Extensions/CustomMosfetModelGenerator.cs | 28 +++++++++++++++++++ .../Extensions/CustomMosfetModelTest.cs | 28 +++++++++++++++++++ .../SpiceSharpParser.IntegrationTests.csproj | 3 ++ .../Semiconductors/MosfetGenerator.cs | 4 +-- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/SpiceSharpParser.IntegrationTests/Examples/Circuits/MosfetExample.cir create mode 100644 src/SpiceSharpParser.IntegrationTests/Examples/Extensions/CustomMosfetModelGenerator.cs create mode 100644 src/SpiceSharpParser.IntegrationTests/Examples/Extensions/CustomMosfetModelTest.cs diff --git a/src/SpiceSharpParser.IntegrationTests/Examples/Circuits/MosfetExample.cir b/src/SpiceSharpParser.IntegrationTests/Examples/Circuits/MosfetExample.cir new file mode 100644 index 00000000..65591be8 --- /dev/null +++ b/src/SpiceSharpParser.IntegrationTests/Examples/Circuits/MosfetExample.cir @@ -0,0 +1,4 @@ +Mosfet circuit +Md 0 1 2 3 my-pmos +.model my-pmos pmos(level = 4) +.END diff --git a/src/SpiceSharpParser.IntegrationTests/Examples/Extensions/CustomMosfetModelGenerator.cs b/src/SpiceSharpParser.IntegrationTests/Examples/Extensions/CustomMosfetModelGenerator.cs new file mode 100644 index 00000000..c94cbd77 --- /dev/null +++ b/src/SpiceSharpParser.IntegrationTests/Examples/Extensions/CustomMosfetModelGenerator.cs @@ -0,0 +1,28 @@ +using SpiceSharp.Components; +using SpiceSharpParser.ModelReaders.Netlist.Spice.Context.Models; +using SpiceSharpParser.ModelReaders.Netlist.Spice.Readers.EntityGenerators; +using SpiceSharpParser.ModelReaders.Netlist.Spice.Readers.EntityGenerators.Models; + +namespace SpiceSharpParser.IntegrationTests.Examples.Extensions +{ + public class CustomMosfetModelGenerator : MosfetModelGenerator + { + + public CustomMosfetModelGenerator() + { + Levels.Add(4, (name, type, _) => + { + var m = new Mosfet3Model(name); + switch (type.ToLower()) + { + case "nmos": m.SetParameter("nmos", true); break; + case "pmos": m.SetParameter("pmos", true); break; + } + + return new Model(name, m, m.Parameters); + }); + } + + + } +} \ No newline at end of file diff --git a/src/SpiceSharpParser.IntegrationTests/Examples/Extensions/CustomMosfetModelTest.cs b/src/SpiceSharpParser.IntegrationTests/Examples/Extensions/CustomMosfetModelTest.cs new file mode 100644 index 00000000..9cbda205 --- /dev/null +++ b/src/SpiceSharpParser.IntegrationTests/Examples/Extensions/CustomMosfetModelTest.cs @@ -0,0 +1,28 @@ +using System.IO; +using Xunit; + +namespace SpiceSharpParser.IntegrationTests.Examples.Extensions +{ + public class CustomMosfetModelTest : BaseTests + { + [Fact] + public void When_CustomMosfetModel_Used_NoExceptions() + { + // Create a model from text file + string path = Path.Combine(Directory.GetCurrentDirectory(), "Examples/Circuits/MosfetExample.cir"); + var netlistContent = File.ReadAllText(path); + var parser = new SpiceNetlistParser(); + parser.Settings.Lexing.HasTitle = true; + var parseResult = parser.ParseNetlist(netlistContent); + + // Convert to Spice# + var spiceSharpReader = new SpiceSharpReader(); + spiceSharpReader.Settings.CaseSensitivity.IsModelTypeCaseSensitive = false; + spiceSharpReader.Settings.Mappings.Models.Map(new[] { "PMOS", "NMOS" }, new CustomMosfetModelGenerator()); + var spiceSharpModel = spiceSharpReader.Read(parseResult.FinalModel); + + Assert.False(spiceSharpModel.ValidationResult.HasError); + Assert.False(spiceSharpModel.ValidationResult.HasWarning); + } + } +} diff --git a/src/SpiceSharpParser.IntegrationTests/SpiceSharpParser.IntegrationTests.csproj b/src/SpiceSharpParser.IntegrationTests/SpiceSharpParser.IntegrationTests.csproj index 779e77f2..6cd3a046 100644 --- a/src/SpiceSharpParser.IntegrationTests/SpiceSharpParser.IntegrationTests.csproj +++ b/src/SpiceSharpParser.IntegrationTests/SpiceSharpParser.IntegrationTests.csproj @@ -90,6 +90,9 @@ Always + + Always + Always diff --git a/src/SpiceSharpParser/ModelReaders/Netlist/Spice/Readers/EntityGenerators/Components/Semiconductors/MosfetGenerator.cs b/src/SpiceSharpParser/ModelReaders/Netlist/Spice/Readers/EntityGenerators/Components/Semiconductors/MosfetGenerator.cs index f8d18cc9..474dc50d 100644 --- a/src/SpiceSharpParser/ModelReaders/Netlist/Spice/Readers/EntityGenerators/Components/Semiconductors/MosfetGenerator.cs +++ b/src/SpiceSharpParser/ModelReaders/Netlist/Spice/Readers/EntityGenerators/Components/Semiconductors/MosfetGenerator.cs @@ -78,9 +78,9 @@ public override IEntity Generate(string componentIdentifier, string originalName return null; } - if (Mosfets.ContainsKey(model.GetType())) + if (Mosfets.ContainsKey(model.Entity.GetType())) { - var mosfetDetails = Mosfets[model.GetType()].Invoke(componentIdentifier); + var mosfetDetails = Mosfets[model.Entity.GetType()].Invoke(componentIdentifier); mosfet = mosfetDetails.Mosfet; context.SimulationPreparations.ExecuteActionBeforeSetup((simulation) =>