Skip to content

Commit

Permalink
Merge pull request #155 from SpiceSharp/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
marcin-golebiowski authored Aug 29, 2021
2 parents 84e05dd + f3306a1 commit d15eb8b
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
<PackageReference Include="SpiceSharp" Version="3.1.5" />
<PackageReference Include="SpiceSharpBehavioral" Version="3.1.1" />
<PackageReference Include="SpiceSharpBehavioral" Version="3.1.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using SpiceSharpParser.Common;
using SpiceSharpParser.ModelReaders.Netlist.Spice;
using System;
using System.IO;
using System.Text;
using Xunit;

namespace SpiceSharpParser.IntegrationTests.Common
Expand Down Expand Up @@ -54,6 +58,35 @@ public void FunctionNamePositive()
spiceModel.Simulations[0].Run(spiceModel.Circuit);
}

[Fact]
public void FunctionNamePositiveTest2()
{
var parser = new SpiceNetlistParser();

parser.Settings.Lexing.HasTitle = true;
parser.Settings.Parsing.IsEndRequired = true;

var text = string.Join(
Environment.NewLine,
"CaseSensitivity",
"R1 0 1 1",
"V1 0 1 {PWR(V(2),2)}",
"V2 0 2 10",
".OP",
".End");

var parseResult = parser.ParseNetlist(text);
var reader = new SpiceSharpReader();
reader.Settings.CaseSensitivity.IsFunctionNameCaseSensitive = false;

var spiceModel = reader.Read(parseResult.FinalModel);

Assert.NotNull(parseResult);
Assert.False(parseResult.ValidationResult.HasError);
Assert.False(parseResult.ValidationResult.HasWarning);
spiceModel.Simulations[0].Run(spiceModel.Circuit);
}

[Fact]
public void When_DistributionNameNotSensitive_Expect_NoException()
{
Expand Down Expand Up @@ -542,5 +575,52 @@ public void EntityParameterPositive3()

Assert.True(EqualsWithTol(export, references));
}

[Fact]
public void SubcircuitPositive()
{
var text = string.Join(Environment.NewLine, "Subcircuit - Case",
"V1 IN 0 4.0",
"X1 IN OUT resistor",
"RX OUT 0 1",
".SUBCKT RESISTOR input output params: R=1",
"R1 input output {R}",
".ENDS RESISTOR",
".OP",
".SAVE V(OUT)",
".END");
var parser = new SpiceNetlistParser();
var netlist = parser.ParseNetlist(text);

var spiceSharpSettings = new SpiceNetlistReaderSettings(new SpiceNetlistCaseSensitivitySettings() { IsSubcircuitNameCaseSensitive = false }, () => parser.Settings.WorkingDirectory, Encoding.Default);
var spiceSharpReader = new SpiceNetlistReader(spiceSharpSettings);

var model = spiceSharpReader.Read(netlist.FinalModel);
Assert.False(model.ValidationResult.HasError);
}

[Fact]
public void SubcircuitPositive2()
{
var text = string.Join(Environment.NewLine, "Subcircuit - Case",
"V1 IN 0 4.0",
"X1 IN OUT resistor",
"RX OUT 0 1",
".SUBCKT RESISTOR input output params: R=1",
"R1 input output {R}",
".ENDS RESISTOR",
".OP",
".SAVE V(OUT)",
".END");

var parser = new SpiceNetlistParser();
var netlist = parser.ParseNetlist(text);

var spiceSharpSettings = new SpiceNetlistReaderSettings(new SpiceNetlistCaseSensitivitySettings() { IsSubcircuitNameCaseSensitive = true }, () => parser.Settings.WorkingDirectory, Encoding.Default);
var spiceSharpReader = new SpiceNetlistReader(spiceSharpSettings);

var model = spiceSharpReader.Read(netlist.FinalModel);
Assert.True(model.ValidationResult.HasError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@


<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
<PackageReference Include="coverlet.msbuild" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="SpiceSharp" Version="3.1.5" />
<PackageReference Include="SpiceSharpBehavioral" Version="3.1.1" />
<PackageReference Include="SpiceSharpBehavioral" Version="3.1.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
6 changes: 3 additions & 3 deletions src/SpiceSharpParser.Tests/SpiceSharpParser.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
<PackageReference Include="coverlet.msbuild" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="SpiceSharp" Version="3.1.5" />
<PackageReference Include="SpiceSharpBehavioral" Version="3.1.1" />
<PackageReference Include="SpiceSharpBehavioral" Version="3.1.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ public class SpiceNetlistCaseSensitivitySettings
/// Gets or sets a value indicating whether expression names are case-sensitive.
/// </summary>
public bool IsExpressionNameCaseSensitive { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether subcircuit names are case-sensitive.
/// </summary>
public bool IsSubcircuitNameCaseSensitive { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public interface IReadingContext
ICollection<IReadingContext> Children { get; }

/// <summary>
/// Gets the list of available subcircuit for the context.
/// Gets the dictionary of available subcircuit for the context.
/// </summary>
ICollection<SubCircuit> AvailableSubcircuits { get; }
Dictionary<string, SubCircuit> AvailableSubcircuits { get; }

/// <summary>
/// Gets the list of available subcircuit for the context.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpiceSharp;
using SpiceSharp.Components;
using SpiceSharp.Entities;
Expand All @@ -17,7 +16,6 @@
using SpiceSharpParser.ModelReaders.Netlist.Spice.Readers.Controls.Simulations.Configurations;
using SpiceSharpParser.Models.Netlist.Spice.Objects;
using SpiceSharpParser.Models.Netlist.Spice.Objects.Parameters;
using SpiceSharpParser.Parsers.Expression;

namespace SpiceSharpParser.ModelReaders.Netlist.Spice.Context
{
Expand Down Expand Up @@ -61,9 +59,9 @@ public ReadingContext(
Children = new List<IReadingContext>();
StatementsReader = statementsReader;
WaveformReader = waveformReader;
ReaderSettings = readerSettings;
AvailableSubcircuits = CreateAvailableSubcircuitsCollection();
AvailableSubcircuitDefinitions = CreateAvailableSubcircuitDefinitions();
ReaderSettings = readerSettings;
Exporters = exporters;
ContextEntities = new Circuit(new EntityCollection(StringComparerProvider.Get(readerSettings.CaseSensitivity.IsEntityNamesCaseSensitive)));
SimulationConfiguration = simulationConfiguration;
Expand Down Expand Up @@ -108,7 +106,7 @@ public ReadingContext(
/// <summary>
/// Gets available subcircuits in context.
/// </summary>
public ICollection<SubCircuit> AvailableSubcircuits { get; }
public Dictionary<string, SubCircuit> AvailableSubcircuits { get; }

public Dictionary<string, SubcircuitDefinition> AvailableSubcircuitDefinitions { get; }

Expand Down Expand Up @@ -389,27 +387,27 @@ public ExpressionResolver CreateExpressionResolver(Simulation simulation)
return parser;
}

protected ICollection<SubCircuit> CreateAvailableSubcircuitsCollection()
protected Dictionary<string, SubCircuit> CreateAvailableSubcircuitsCollection()
{
if (Parent != null)
{
return new List<SubCircuit>(Parent.AvailableSubcircuits);
return new Dictionary<string, SubCircuit>(Parent.AvailableSubcircuits, StringComparerProvider.Get(ReaderSettings.CaseSensitivity.IsSubcircuitNameCaseSensitive));
}
else
{
return new List<SubCircuit>();
return new Dictionary<string, SubCircuit>(StringComparerProvider.Get(ReaderSettings.CaseSensitivity.IsSubcircuitNameCaseSensitive));
}
}

protected Dictionary<string, SubcircuitDefinition> CreateAvailableSubcircuitDefinitions()
{
if (Parent != null)
{
return new Dictionary<string, SubcircuitDefinition>(Parent.AvailableSubcircuitDefinitions);
return new Dictionary<string, SubcircuitDefinition>(Parent.AvailableSubcircuitDefinitions, StringComparerProvider.Get(ReaderSettings.CaseSensitivity.IsSubcircuitNameCaseSensitive));
}
else
{
return new Dictionary<string, SubcircuitDefinition>();
return new Dictionary<string, SubcircuitDefinition>(StringComparerProvider.Get(ReaderSettings.CaseSensitivity.IsSubcircuitNameCaseSensitive));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,20 @@ private SubCircuit FindSubcircuitDefinition(ParameterCollection parameters, IRea
}

string subCircuitDefinitionName = parameters.Get(parameters.Count - skipCount - 1).Value;
var result = context.AvailableSubcircuits.ToList().Find(subCkt => subCkt.Name == subCircuitDefinitionName);

if (result == null)
if (context.AvailableSubcircuits.TryGetValue(subCircuitDefinitionName, out var result))
{
context.Result.ValidationResult.AddError(
ValidationEntrySource.Reader,
$"Could not find '{subCircuitDefinitionName}' subcircuit",
parameters.LineInfo);
if (result == null)
{
context.Result.ValidationResult.AddError(
ValidationEntrySource.Reader,
$"Could not find '{subCircuitDefinitionName}' subcircuit",
parameters.LineInfo);
}
return result;
}

return result;
return null;
}

private List<string> GetPinNames(ParameterCollection parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void Read(SubCircuit statement, IReadingContext context)
throw new ArgumentNullException(nameof(context));
}

context.AvailableSubcircuits.Add(statement);
context.AvailableSubcircuits[statement.Name] = statement;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using SpiceSharp;
using SpiceSharp.Entities;
using SpiceSharpBehavioral.Builders.Direct;
using SpiceSharpBehavioral.Builders.Functions;
using SpiceSharpBehavioral.Parsers.Nodes;
using SpiceSharpParser.Common;
using SpiceSharpParser.Common.Evaluation;
using SpiceSharpParser.Common.Mathematics.Probability;
Expand Down Expand Up @@ -53,6 +56,13 @@ public SpiceSharpModel Read(SpiceNetlist netlist)
// Set the separator.
Utility.Separator = Settings.Separator;

// Set functions case-sensitivity
ComplexBuilderHelper.RemapFunctions(StringComparerProvider.Get(Settings.CaseSensitivity.IsFunctionNameCaseSensitive));
ComplexFunctionBuilderHelper.RemapFunctions(StringComparerProvider.Get(Settings.CaseSensitivity.IsFunctionNameCaseSensitive));
RealBuilderHelper.RemapFunctions(StringComparerProvider.Get(Settings.CaseSensitivity.IsFunctionNameCaseSensitive));
RealFunctionBuilderHelper.RemapFunctions(StringComparerProvider.Get(Settings.CaseSensitivity.IsFunctionNameCaseSensitive));
DerivativesHelper.RemapFunctions(StringComparerProvider.Get(Settings.CaseSensitivity.IsFunctionNameCaseSensitive));

// Get reading context
var nodeNameGenerator = new MainCircuitNodeNameGenerator(
new[] { "0" },
Expand Down
6 changes: 3 additions & 3 deletions 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.1.1</Version>
<Version>3.1.2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard1.5|AnyCPU'">
Expand All @@ -35,9 +35,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
<PackageReference Include="SpiceSharp" Version="3.1.5" />
<PackageReference Include="SpiceSharpBehavioral" Version="3.1.1" />
<PackageReference Include="SpiceSharpBehavioral" Version="3.1.2" />
<PackageReference Include="SpiceSharpGenerator" Version="1.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit d15eb8b

Please sign in to comment.