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
2 changes: 1 addition & 1 deletion .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
args:
# Unique keys of your project and organization. You can find them in SonarCloud > Information (bottom-left menu)
# mandatory
-Dsonar.projectKey="SpiceSharp_SpiceSharpParser"
-Dsonar.projectKey="SpiceSharp_SpiceSharpParserNew"
-Dsonar.organization="spicesharp"
# Comma-separated paths to directories containing main source files.
#-Dsonar.sources= # optional, default is project base directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ public IRandomNumberProvider GetRandom(int? randomSeed)
_cacheLock.EnterWriteLock();
try
{
var randomGenerator = new DefaultRandomNumberProvider(new Random(randomSeed.Value));
_randomGenerators[randomSeed.Value] = randomGenerator;

return randomGenerator;
// Double-check after entering write lock
if (!_randomGenerators.ContainsKey(randomSeed.Value))
{
var randomGenerator = new DefaultRandomNumberProvider(new Random(randomSeed.Value));
_randomGenerators[randomSeed.Value] = randomGenerator;
return randomGenerator;
}
else
{
return _randomGenerators[randomSeed.Value];
}
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,14 @@ public void Add(IEntity entity, string parameterName, string expression, bool be
throw new ArgumentNullException(nameof(expression));
}

if (!SimulationSpecificUpdates.ContainsKey(simulation))
{
SimulationSpecificUpdates[simulation] = new Dictionary<IEntity, EntityUpdate>();
}

if (!SimulationSpecificUpdates[simulation].ContainsKey(entity))
{
SimulationSpecificUpdates[simulation][entity] = new EntityUpdate();
}
var simulationUpdates = SimulationSpecificUpdates.GetOrAdd(simulation, _ => new Dictionary<IEntity, EntityUpdate>());
var entityUpdate = simulationUpdates.ContainsKey(entity)
? simulationUpdates[entity]
: (simulationUpdates[entity] = new EntityUpdate());

if (beforeTemperature)
{
SimulationSpecificUpdates[simulation][entity].ParameterUpdatesBeforeTemperature.Add(new EntityParameterExpressionValueUpdate()
entityUpdate.ParameterUpdatesBeforeTemperature.Add(new EntityParameterExpressionValueUpdate()
{
Expression = new DynamicExpression(expression),
ParameterName = parameterName,
Expand All @@ -140,14 +135,11 @@ public void Add(IEntity entity, string parameterName, string expression, bool be
throw new ArgumentNullException(nameof(expression));
}

if (!CommonUpdates.ContainsKey(entity))
{
CommonUpdates[entity] = new EntityUpdate();
}
var entityUpdate = CommonUpdates.GetOrAdd(entity, _ => new EntityUpdate());

if (beforeTemperature)
{
CommonUpdates[entity].ParameterUpdatesBeforeTemperature.Add(new EntityParameterExpressionValueUpdate()
entityUpdate.ParameterUpdatesBeforeTemperature.Add(new EntityParameterExpressionValueUpdate()
{
Expression = new DynamicExpression(expression),
ParameterName = parameterName,
Expand All @@ -167,14 +159,11 @@ public void Add(IEntity entity, string parameterName, double value, bool beforeT
throw new ArgumentNullException(nameof(parameterName));
}

if (!CommonUpdates.ContainsKey(entity))
{
CommonUpdates[entity] = new EntityUpdate();
}
var entityUpdate = CommonUpdates.GetOrAdd(entity, _ => new EntityUpdate());

if (beforeTemperature)
{
CommonUpdates[entity].ParameterUpdatesBeforeTemperature.Add(
entityUpdate.ParameterUpdatesBeforeTemperature.Add(
new EntityParameterDoubleValueUpdate() { ParameterName = parameterName, Value = value });
}
}
Expand All @@ -196,19 +185,14 @@ public void Add(IEntity entity, string parameterName, double value, bool beforeT
throw new ArgumentNullException(nameof(parameterName));
}

if (!SimulationSpecificUpdates.ContainsKey(simulation))
{
SimulationSpecificUpdates[simulation] = new Dictionary<IEntity, EntityUpdate>();
}

if (!SimulationSpecificUpdates[simulation].ContainsKey(entity))
{
SimulationSpecificUpdates[simulation][entity] = new EntityUpdate();
}
var simulationUpdates = SimulationSpecificUpdates.GetOrAdd(simulation, _ => new Dictionary<IEntity, EntityUpdate>());
var entityUpdate = simulationUpdates.ContainsKey(entity)
? simulationUpdates[entity]
: (simulationUpdates[entity] = new EntityUpdate());

if (beforeTemperature)
{
SimulationSpecificUpdates[simulation][entity].ParameterUpdatesBeforeTemperature.Add(new EntityParameterDoubleValueUpdate { ParameterName = parameterName, Value = value });
entityUpdate.ParameterUpdatesBeforeTemperature.Add(new EntityParameterDoubleValueUpdate { ParameterName = parameterName, Value = value });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,8 @@ public void AddBeforeSetup(ISimulationWithEvents simulation, Action<ISimulationW
throw new ArgumentNullException(nameof(simulation));
}

if (!SimulationBeforeSetupActions.ContainsKey(simulation))
{
SimulationBeforeSetupActions[simulation] = new List<SimulationUpdateAction>() { new SimulationUpdateAction(action) };
}
else
{
SimulationBeforeSetupActions[simulation].Add(new SimulationUpdateAction(action));
}
var actions = SimulationBeforeSetupActions.GetOrAdd(simulation, _ => new List<SimulationUpdateAction>());
actions.Add(new SimulationUpdateAction(action));
}

public void AddBeforeTemperature(ISimulationWithEvents simulation, Action<ISimulationWithEvents, SimulationEvaluationContexts> action)
Expand All @@ -100,14 +94,8 @@ public void AddBeforeTemperature(ISimulationWithEvents simulation, Action<ISimul
throw new ArgumentNullException(nameof(simulation));
}

if (!SimulationBeforeTemperatureActions.ContainsKey(simulation))
{
SimulationBeforeTemperatureActions[simulation] = new List<SimulationUpdateAction>() { new SimulationUpdateAction(action) };
}
else
{
SimulationBeforeTemperatureActions[simulation].Add(new SimulationUpdateAction(action));
}
var actions = SimulationBeforeTemperatureActions.GetOrAdd(simulation, _ => new List<SimulationUpdateAction>());
actions.Add(new SimulationUpdateAction(action));
}

public void AddBeforeSetup(Action<ISimulationWithEvents, SimulationEvaluationContexts> action)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using SpiceSharp.Entities;
using SpiceSharp.Simulations;
using SpiceSharpParser.Common;
using SpiceSharpParser.ModelReaders.Netlist.Spice.Context;
using SpiceSharpParser.ModelReaders.Netlist.Spice.Context.Models;
Expand All @@ -14,7 +13,7 @@ namespace SpiceSharpParser.ModelReaders.Netlist.Spice.Readers.Controls.Simulatio
public class EnableStochasticModelsSimulationDecorator
{
private readonly IReadingContext _context;
private readonly ConcurrentDictionary<string, Dictionary<string, double>> _lotValues = new ();
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, double>> _lotValues = new ();

public EnableStochasticModelsSimulationDecorator(IReadingContext context)
{
Expand Down Expand Up @@ -98,22 +97,13 @@ private void SetModelDevModelParameters(ISimulationWithEvents simulation, IEntit

private double GetValueForLotParameter(EvaluationContext evaluationContext, string baseModel, string parameterName, double currentValue, double percentValue, string distributionName, IEqualityComparer<string> comparer)
{
if (_lotValues.ContainsKey(baseModel) && _lotValues[baseModel].ContainsKey(parameterName))
{
return _lotValues[baseModel][parameterName];
}

var random = evaluationContext.Randomizer.GetRandomProvider(distributionName);
double newValue = currentValue + ((percentValue / 100.0) * currentValue * random.NextSignedDouble());
var modelValues = _lotValues.GetOrAdd(baseModel, _ => new ConcurrentDictionary<string, double>(comparer));

if (!_lotValues.ContainsKey(baseModel))
return modelValues.GetOrAdd(parameterName, _ =>
{
_lotValues[baseModel] = new Dictionary<string, double>(comparer);
}

_lotValues[baseModel][parameterName] = newValue;

return newValue;
var random = evaluationContext.Randomizer.GetRandomProvider(distributionName);
return currentValue + ((percentValue / 100.0) * currentValue * random.NextSignedDouble());
});
}
}
}
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.5</Version>
<Version>3.2.6</Version>
</PropertyGroup>

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