Skip to content

Commit c3b56c6

Browse files
Merge pull request #179 from SpiceSharp/fix_gnd
Wrong handling of "GND" in subcircuits
2 parents 4b41b89 + 91e34ff commit c3b56c6

3 files changed

Lines changed: 57 additions & 11 deletions

File tree

src/SpiceSharpParser.IntegrationTests/Components/SubcircuitTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using Xunit;
34

45
namespace SpiceSharpParser.IntegrationTests.Components
@@ -455,5 +456,48 @@ public void SubcircuitWithWrongEnding()
455456
var model = parser.ParseNetlist(text);
456457
Assert.True(model.ValidationResult.HasError);
457458
}
459+
460+
[Fact]
461+
public void When_GND_Port_Used_In_Behavioral_Source_With_Tran()
462+
{
463+
// Regression test: SUBCKT with GND port name + behavioral source
464+
// must work in .TRAN, not just .OP. Previously, Generate() bypassed
465+
// the pin map for "GND", creating a floating node instead of mapping
466+
// to the external ground node "0".
467+
var model = GetSpiceSharpModel(
468+
"Subcircuit GND Port TRAN Test",
469+
"V1 IN 0 4.0",
470+
"X1 0 IN OUT test_subckt",
471+
".SUBCKT test_subckt GND INPUT OUTPUT",
472+
"R1 INPUT mid 5k",
473+
"R2 mid GND 5k",
474+
"B1 OUTPUT GND V={0.5 * V(INPUT,GND)}",
475+
".ENDS test_subckt",
476+
".TRAN 1u 100u",
477+
".SAVE V(OUT)",
478+
".END");
479+
480+
var simulations = model.Simulations;
481+
Assert.Single(simulations);
482+
483+
var tran = simulations[0];
484+
double lastValue = double.NaN;
485+
486+
tran.EventExportData += (sender, e) =>
487+
{
488+
var exports = model.Exports.Where(ex => ex.Simulation == tran).ToList();
489+
foreach (var export in exports)
490+
{
491+
lastValue = export.Extract();
492+
}
493+
};
494+
495+
var codes = tran.Run(model.Circuit, -1);
496+
codes = tran.InvokeEvents(codes);
497+
codes.ToArray();
498+
499+
// B1 = 0.5 * V(IN,0) = 0.5 * 4.0 = 2.0V
500+
Assert.InRange(lastValue, 1.9, 2.1);
501+
}
458502
}
459503
}

src/SpiceSharpParser/ModelReaders/Netlist/Spice/Context/Names/SubcircuitNodeNameGenerator.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ public string Generate(string nodeName)
108108
throw new ArgumentNullException(nameof(nodeName));
109109
}
110110

111+
// Pin map takes priority — subcircuit port mapping must override
112+
// the GND alias below, otherwise a port named "GND" mapped to
113+
// external "0" would create a floating node instead of connecting
114+
// to the real ground.
115+
var pinIdentifier = nodeName;
116+
117+
if (_pinMap.ContainsKey(pinIdentifier))
118+
{
119+
return _pinMap[pinIdentifier];
120+
}
121+
111122
if (nodeName.ToUpper() == "GND")
112123
{
113124
return nodeName;
@@ -118,16 +129,7 @@ public string Generate(string nodeName)
118129
return nodeName;
119130
}
120131

121-
var pinIdentifier = nodeName;
122-
123-
if (_pinMap.ContainsKey(pinIdentifier))
124-
{
125-
return _pinMap[pinIdentifier];
126-
}
127-
else
128-
{
129-
return $"{SubCircuitFullName}{Separator}{nodeName}";
130-
}
132+
return $"{SubCircuitFullName}{Separator}{nodeName}";
131133
}
132134

133135
/// <summary>

src/SpiceSharpParser/SpiceSharpParser.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<StartupObject />
2323
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2424
<LangVersion>latest</LangVersion>
25-
<Version>3.2.11</Version>
25+
<Version>3.2.12</Version>
2626
</PropertyGroup>
2727

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

0 commit comments

Comments
 (0)