Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for Mosfet + fix old bug in Mosfet generation #165

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Mosfet circuit
Md 0 1 2 3 my-pmos
.model my-pmos pmos(level = 4)
.END
Original file line number Diff line number Diff line change
@@ -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);
});
}


}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
<None Update="Examples\Circuits\Example04_reversed.cir">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Examples\Circuits\MosfetExample.cir">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\pwl_reference.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
2 changes: 1 addition & 1 deletion src/SpiceSharpParser/SpiceSharpParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<StartupObject />
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LangVersion>latest</LangVersion>
<Version>3.2.2</Version>
<Version>3.2.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard1.5|AnyCPU'">
Expand Down
Loading