Skip to content

Commit

Permalink
Merge pull request #165 from SpiceSharp/cust
Browse files Browse the repository at this point in the history
Add example for Mosfet + fix old bug in Mosfet generation
  • Loading branch information
marcin-golebiowski authored Oct 21, 2024
2 parents c045e89 + 95b3461 commit 1386dbf
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
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

0 comments on commit 1386dbf

Please sign in to comment.