Skip to content
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
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Xunit;

namespace SpiceSharpParser.IntegrationTests.Components
Expand Down Expand Up @@ -455,5 +456,48 @@ public void SubcircuitWithWrongEnding()
var model = parser.ParseNetlist(text);
Assert.True(model.ValidationResult.HasError);
}

[Fact]
public void When_GND_Port_Used_In_Behavioral_Source_With_Tran()
{
// Regression test: SUBCKT with GND port name + behavioral source
// must work in .TRAN, not just .OP. Previously, Generate() bypassed
// the pin map for "GND", creating a floating node instead of mapping
// to the external ground node "0".
var model = GetSpiceSharpModel(
"Subcircuit GND Port TRAN Test",
"V1 IN 0 4.0",
"X1 0 IN OUT test_subckt",
".SUBCKT test_subckt GND INPUT OUTPUT",
"R1 INPUT mid 5k",
"R2 mid GND 5k",
"B1 OUTPUT GND V={0.5 * V(INPUT,GND)}",
".ENDS test_subckt",
".TRAN 1u 100u",
".SAVE V(OUT)",
".END");

var simulations = model.Simulations;
Assert.Single(simulations);

var tran = simulations[0];
double lastValue = double.NaN;

tran.EventExportData += (sender, e) =>
{
var exports = model.Exports.Where(ex => ex.Simulation == tran).ToList();
foreach (var export in exports)
{
lastValue = export.Extract();
}
};

var codes = tran.Run(model.Circuit, -1);
codes = tran.InvokeEvents(codes);
codes.ToArray();

// B1 = 0.5 * V(IN,0) = 0.5 * 4.0 = 2.0V
Assert.InRange(lastValue, 1.9, 2.1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public string Generate(string nodeName)
throw new ArgumentNullException(nameof(nodeName));
}

// Pin map takes priority — subcircuit port mapping must override
// the GND alias below, otherwise a port named "GND" mapped to
// external "0" would create a floating node instead of connecting
// to the real ground.
var pinIdentifier = nodeName;

if (_pinMap.ContainsKey(pinIdentifier))
{
return _pinMap[pinIdentifier];
}

if (nodeName.ToUpper() == "GND")
{
return nodeName;
Expand All @@ -118,16 +129,7 @@ public string Generate(string nodeName)
return nodeName;
}

var pinIdentifier = nodeName;

if (_pinMap.ContainsKey(pinIdentifier))
{
return _pinMap[pinIdentifier];
}
else
{
return $"{SubCircuitFullName}{Separator}{nodeName}";
}
return $"{SubCircuitFullName}{Separator}{nodeName}";
}

/// <summary>
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.11</Version>
<Version>3.2.12</Version>
</PropertyGroup>

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