Skip to content

Commit

Permalink
Cleanup of events
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-golebiowski committed Oct 19, 2024
1 parent da694c4 commit d3f0d0c
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 71 deletions.
20 changes: 10 additions & 10 deletions src/SpiceSharpParser.IntegrationTests/BaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,21 @@ public static List<object> RunSimulationsAndReturnExports(SpiceSharpModel reader
};
}

if (simulation is Transient)
if (simulation is Transient transient)
{
var tranResult = new List<Tuple<double, double>>();
result.Add(tranResult);
simulation.EventExportData += (sender, e) =>
{
tranResult.Add(new Tuple<double, double>(e.Time, export.Extract()));
tranResult.Add(new Tuple<double, double>(transient.Time, export.Extract()));
};
}
}

foreach (var simulation in readerResult.Simulations)
{
var codes = simulation.Run(readerResult.Circuit, -1);
codes = simulation.AttachEvents(codes);
codes = simulation.InvokeEvents(codes);

codes.ToArray(); //eval
}
Expand All @@ -190,7 +190,7 @@ public static void RunSimulations(SpiceSharpModel readerResult)
foreach (var simulation in readerResult.Simulations)
{
var codes = simulation.Run(readerResult.Circuit, -1);
codes = simulation.AttachEvents(codes);
codes = simulation.InvokeEvents(codes);

codes.ToArray(); // eval
}
Expand All @@ -207,7 +207,7 @@ public static double RunOpSimulation(SpiceSharpModel readerResult, string nameOf
};

var codes = simulation.Run(readerResult.Circuit, -1);
codes = simulation.AttachEvents(codes);
codes = simulation.InvokeEvents(codes);
codes.ToArray(); // eval
return result;
}
Expand All @@ -227,7 +227,7 @@ public static double[] RunOpSimulation(SpiceSharpModel readerResult, params stri
};

var codes = simulation.Run(readerResult.Circuit, -1);
var attached = simulation.AttachEvents(codes);
var attached = simulation.InvokeEvents(codes);
attached.ToArray(); // eval

return result;
Expand Down Expand Up @@ -255,7 +255,7 @@ public static Tuple<string, double>[] RunOpSimulation(SpiceSharpModel readerResu
};

var codes = simulation.Run(readerResult.Circuit, -1);
var attached = simulation.AttachEvents(codes);
var attached = simulation.InvokeEvents(codes);
attached.ToArray(); // eval

return result;
Expand All @@ -269,11 +269,11 @@ public static Tuple<double, double>[] RunTransientSimulation(SpiceSharpModel rea
var simulation = readerResult.Simulations.First(s => s is Transient);
simulation.EventExportData += (sender, e) =>
{
list.Add(new Tuple<double, double>(e.Time, export.Extract()));
list.Add(new Tuple<double, double>(((Transient)simulation).Time, export.Extract()));
};

var codes = simulation.Run(readerResult.Circuit, -1);
var attached = simulation.AttachEvents(codes);
var attached = simulation.InvokeEvents(codes);
attached.ToArray(); // eval

return list.ToArray();
Expand All @@ -291,7 +291,7 @@ public static Tuple<double, double>[] RunDCSimulation(SpiceSharpModel readerResu
};

var codes = simulation.Run(readerResult.Circuit, -1);
var attached = simulation.AttachEvents(codes);
var attached = simulation.InvokeEvents(codes);
attached.ToArray(); // eval

return list.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void When_DistributionNameSensitive_Positive_Expect_NoException()
var spiceModel = reader.Read(parseResult.FinalModel);

var codes = spiceModel.Simulations[0].Run(spiceModel.Circuit, -1);
codes = spiceModel.Simulations[0].AttachEvents(codes);
codes = spiceModel.Simulations[0].InvokeEvents(codes);

codes.ToArray();
}
Expand All @@ -159,7 +159,7 @@ public void When_DistributionNameSensitive_Negative_Expect_Exception()
var spiceModel = reader.Read(parseResult.FinalModel);

var codes = spiceModel.Simulations[0].Run(spiceModel.Circuit, -1);
codes = spiceModel.Simulations[0].AttachEvents(codes);
codes = spiceModel.Simulations[0].InvokeEvents(codes);


Assert.Throws<ArgumentException>(() => codes.ToArray());
Expand Down
23 changes: 13 additions & 10 deletions src/SpiceSharpParser.IntegrationTests/Waveforms/PulseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ public void Test01()
Assert.NotNull(netlist);

var simulation = netlist.Simulations.First(s => s is Transient);
var raw = (Transient)simulation;
bool riseHit = false, risenHit = false, fallHit = false, fallenHit = false;

simulation.EventExportData += (sender, args) =>
{
if (Math.Abs(args.Time - 0.2) < 1e-12)
if (Math.Abs(raw.Time - 0.2) < 1e-12)
riseHit = true;
if (Math.Abs(args.Time - 0.3) < 1e-12)
if (Math.Abs(raw.Time - 0.3) < 1e-12)
risenHit = true;
if (Math.Abs(args.Time - 0.7) < 1e-12)
if (Math.Abs(raw.Time - 0.7) < 1e-12)
fallHit = true;
if (Math.Abs(args.Time - 0.8) < 1e-12)
if (Math.Abs(raw.Time - 0.8) < 1e-12)
fallenHit = true;
};

var events = simulation.Run(netlist.Circuit);
simulation.AttachEvents(events).ToArray();
simulation.InvokeEvents(events).ToArray();

Assert.True(riseHit);
Assert.True(risenHit);
Expand All @@ -56,23 +57,25 @@ public void Test02()
Assert.NotNull(netlist);

var simulation = netlist.Simulations.First(s => s is Transient);
var raw = (Transient)simulation;

bool riseHit = false, risenHit = false, fallHit = false, fallenHit = false;

simulation.EventExportData += (sender, args) =>
{
if (Math.Abs(args.Time - 0.2) < 1e-12)
if (Math.Abs(raw.Time - 0.2) < 1e-12)
riseHit = true;
if (Math.Abs(args.Time - 0.3) < 1e-12)
if (Math.Abs(raw.Time - 0.3) < 1e-12)
risenHit = true;
if (Math.Abs(args.Time - 0.7) < 1e-12)
if (Math.Abs(raw.Time - 0.7) < 1e-12)
fallHit = true;
if (Math.Abs(args.Time - 0.8) < 1e-12)
if (Math.Abs(raw.Time - 0.8) < 1e-12)
fallenHit = true;
};


var events = simulation.Run(netlist.Circuit);
var codes = simulation.AttachEvents(events);
var codes = simulation.InvokeEvents(events);

//eval
codes.ToArray();
Expand Down
12 changes: 4 additions & 8 deletions src/SpiceSharpParser.IntegrationTests/Waveforms/PwlTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using SpiceSharp.Simulations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace SpiceSharpParser.IntegrationTests.Waveforms
Expand All @@ -23,18 +19,18 @@ public void Test01()
Assert.NotNull(netlist);

var simulation = netlist.Simulations.First(s => s is Transient);

var raw = (Transient)simulation;
var wasHit1 = false;
var wasHit2 = false;

simulation.EventExportData += (sender, args) =>
{
if (args.Time == 1.111)
if (raw.Time == 1.111)
{
wasHit1 = true;
}
if (args.Time == 3.34)
if (raw.Time == 3.34)
{
wasHit2 = true;
}
Expand All @@ -43,7 +39,7 @@ public void Test01()
};

var codes = simulation.Run(netlist.Circuit);
var withEvents = simulation.AttachEvents(codes);
var withEvents = simulation.InvokeEvents(codes);

withEvents.ToArray(); //eval

Expand Down
6 changes: 3 additions & 3 deletions src/SpiceSharpParser.IntegrationTests/Waveforms/SineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Test01(double offset, double amplitude, double frequency, double del
Assert.True(EqualsWithTol(expected, simulation.GetVoltage("1")));
};

foreach (var code in simulation.AttachEvents(simulation.Run(netlist.Circuit)))
foreach (var code in simulation.InvokeEvents(simulation.Run(netlist.Circuit)))
{
}

Expand All @@ -69,7 +69,7 @@ public void Test02(double offset, double amplitude, double frequency, double del

simulation.EventExportData += (sender, args) =>
{
var time = args.Time;
var time = ((Transient)simulation).Time;
time -= delay;
// Calculate sine wave result (no offset)
Expand All @@ -89,7 +89,7 @@ public void Test02(double offset, double amplitude, double frequency, double del
};

var events = simulation.Run(netlist.Circuit);
foreach (var code in simulation.AttachEvents(events))
foreach (var code in simulation.InvokeEvents(events))
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override void Read(Control statement, IReadingContext context)
}
}

private static void CreateRowInPrint(ref int rowIndex, ISimulationWithEvents simulation, IReadingContext context, ExportData eventArgs, List<Export> exports, Print print)
private static void CreateRowInPrint(ref int rowIndex, ISimulationWithEvents simulation, IReadingContext context, object eventArgs, List<Export> exports, Print print)
{
Row row = new Row(rowIndex++);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void CreateAcSweepPlot(string variableName, List<Export> exports, IReadi

private void AddOpPointToSeries(Context.Sweeps.ParameterSweep firstParameterSweep, Export export, IReadingContext context, Series series)
{
export.Simulation.EventExportData += (object sender, ExportData e) =>
export.Simulation.EventExportData += (object sender, object e) =>
{
var expressionContext = context.EvaluationContext.GetSimulationContext(export.Simulation);
var firstParameterSweepParameter = expressionContext.Parameters[firstParameterSweep.Parameter.Value];
Expand All @@ -241,17 +241,17 @@ private void AddOpPointToSeries(Context.Sweeps.ParameterSweep firstParameterSwee

private void AddTranPointsToSeries(Export export, Series series)
{
export.Simulation.EventExportData += (object sender, ExportData e) =>
export.Simulation.EventExportData += (object sender, object e) =>
{
series.Points.Add(new Point() { X = e.Time, Y = export.Extract() });
series.Points.Add(new Point() { X = (export.Simulation as Transient).Time, Y = export.Extract() });
};
}

private void AddAcPointsToSeries(Export export, Series series)
{
export.Simulation.EventExportData += (object sender, ExportData e) =>
export.Simulation.EventExportData += (object sender, object e) =>
{
series.Points.Add(new Point() { X = e.Frequency, Y = export.Extract() });
series.Points.Add(new Point() { X = (export.Simulation as AC).Frequency, Y = export.Extract() });
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public WaveControl(IMapper<Exporter> mapper, IExportFactory factory)
public override void Read(Control statement, IReadingContext context)
{
var transient = (ISimulationWithEvents)context.Result.Simulations.FirstOrDefault(s => s is Transient);
var sim = (Transient)transient;

if (transient != null)
{
Expand All @@ -56,7 +57,7 @@ public override void Read(Control statement, IReadingContext context)
var pwlData = new List<(double, double)>();
transient.EventExportData += (sender, args) =>
{
pwlData.Add((args.Time, monoChannelExport.Extract()));
pwlData.Add((sim.Time, monoChannelExport.Extract()));
};

transient.EventAfterExecute += (sender, args) =>
Expand All @@ -76,8 +77,8 @@ public override void Read(Control statement, IReadingContext context)
var rightData = new List<(double, double)>();
transient.EventExportData += (sender, args) =>
{
leftData.Add((args.Time, leftChannelExport.Extract()));
rightData.Add((args.Time, rightChannelExport.Extract()));
leftData.Add((sim.Time, leftChannelExport.Extract()));
rightData.Add((sim.Time, rightChannelExport.Extract()));
};

transient.EventAfterExecute += (sender, args) =>
Expand Down
13 changes: 6 additions & 7 deletions src/SpiceSharpParser/ModelWriters/CSharp/Events/ACWithEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace SpiceSharpParser.Common
{
public class ACWithEvents : AC, ISimulationWithEvents
{
protected ACWithEvents(string name) : base(name)
protected ACWithEvents(string name)
: base(name)
{
}

Expand Down Expand Up @@ -34,9 +35,8 @@ public ACWithEvents(string name, IEnumerable<double> frequencySweep): base(name,

public event OnExportData EventExportData;

public IEnumerable<int> AttachEvents(IEnumerable<int> codes)
public IEnumerable<int> InvokeEvents(IEnumerable<int> codes)
{
EventBeforeSetup.Invoke(this, EventArgs.Empty);
foreach (var code in codes)
{
switch (code)
Expand Down Expand Up @@ -73,11 +73,10 @@ public IEnumerable<int> AttachEvents(IEnumerable<int> codes)
EventAfterExecute.Invoke(this, EventArgs.Empty);
break;

case AC.ExportSmallSignal:

case AC.Exports:

double frequency = base.Frequency;
EventExportData.Invoke(this, new ExportData { Frequency = frequency });
double frequency = Frequency;
EventExportData?.Invoke(this, EventArgs.Empty);
break;
}
yield return code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public DcWithEvents(string name, IEnumerable<ISweep> sweeps) : this(name)

public event OnExportData EventExportData;

public IEnumerable<int> AttachEvents(IEnumerable<int> codes)
public IEnumerable<int> InvokeEvents(IEnumerable<int> codes)
{
foreach (var code in codes)
{
Expand All @@ -54,7 +54,7 @@ public IEnumerable<int> AttachEvents(IEnumerable<int> codes)
EventAfterValidation?.Invoke(this, EventArgs.Empty);
break;

case 65536:
case DC.BeforeSetup:
EventBeforeSetup?.Invoke(this, EventArgs.Empty);
break;
case DC.AfterSetup:
Expand All @@ -79,7 +79,7 @@ public IEnumerable<int> AttachEvents(IEnumerable<int> codes)
break;

case DC.ExportSweep:
EventExportData?.Invoke(this, new ExportData { });
EventExportData?.Invoke(this, EventArgs.Empty);
break;
}
yield return code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using SpiceSharp;
using SpiceSharp.Entities;
using SpiceSharp.Simulations;
using SpiceSharp.Simulations;
using System.Collections.Generic;

namespace SpiceSharpParser.Common
Expand All @@ -21,14 +19,7 @@ namespace SpiceSharpParser.Common

public delegate void OnAfterExecute(object sender, object argument);

public delegate void OnExportData(object sender, ExportData argument);

public class ExportData
{
public double Time { get; set; }
public double Frequency { get; internal set; }
}

public delegate void OnExportData(object sender, object argument);

public interface ISimulationWithEvents : ISimulation
{
Expand All @@ -52,6 +43,6 @@ public interface ISimulationWithEvents : ISimulation

event OnExportData EventExportData;

IEnumerable<int> AttachEvents(IEnumerable<int> codes);
IEnumerable<int> InvokeEvents(IEnumerable<int> codes);
}
}
Loading

0 comments on commit d3f0d0c

Please sign in to comment.