Skip to content

Commit 83f690c

Browse files
Merge .measure from #160
1 parent e294b9b commit 83f690c

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

PySpice/Spice/Simulation.py

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def to_list(self):
5858
##############################################
5959

6060
def __str__(self):
61-
6261
return '.{0.analysis_name} {1}'.format(self, join_list(self.to_list()))
6362

6463
####################################################################################################
@@ -80,7 +79,6 @@ class DcSensitivityAnalysisParameters(AnalysisParameters):
8079
##############################################
8180

8281
def __init__(self, output_variable):
83-
8482
self._output_variable = output_variable
8583

8684
##############################################
@@ -92,7 +90,6 @@ def output_variable(self):
9290
##############################################
9391

9492
def to_list(self):
95-
9693
return (
9794
self._output_variable,
9895
)
@@ -144,7 +141,6 @@ def stop_frequencyr(self):
144141
##############################################
145142

146143
def to_list(self):
147-
148144
return (
149145
self._output_variable,
150146
self._variation,
@@ -183,7 +179,6 @@ def parameters(self):
183179
##############################################
184180

185181
def to_list(self):
186-
187182
return self._parameters
188183

189184
####################################################################################################
@@ -297,6 +292,37 @@ def to_list(self):
297292

298293
####################################################################################################
299294

295+
class MeasureParameters(AnalysisParameters):
296+
297+
"""This class defines measurements on analysis.
298+
299+
"""
300+
301+
__analysis_name__ = 'meas'
302+
303+
##############################################
304+
305+
def __init__(self, analysis_type, name, *args):
306+
307+
_analysis_type = str(analysis_type).upper()
308+
if (_analysis_type not in ('AC', 'DC', 'OP', 'TRAN', 'TF', 'NOISE')):
309+
raise ValueError('Incorrect analysis type {}'.format(analysis_type))
310+
311+
self._parameters = [_analysis_type, name, *args]
312+
313+
##############################################
314+
315+
@property
316+
def parameters(self):
317+
return self._parameters
318+
319+
##############################################
320+
321+
def to_list(self):
322+
return self._parameters
323+
324+
####################################################################################################
325+
300326
class CircuitSimulation:
301327

302328
"""Define and generate the spice instruction to perform a circuit simulation.
@@ -315,6 +341,7 @@ def __init__(self, circuit, **kwargs):
315341
self._circuit = circuit
316342

317343
self._options = {} # .options
344+
self._measures = [] # .measure
318345
self._initial_condition = {} # .ic
319346
self._saved_nodes = set()
320347
self._analyses = {}
@@ -424,27 +451,27 @@ def save_currents(self, value):
424451
##############################################
425452

426453
def reset_analysis(self):
427-
428454
self._analyses.clear()
429455

430456
##############################################
431457

432458
def analysis_iter(self):
433-
434459
return self._analyses.values()
435460

436461
##############################################
437462

438463
def _add_analysis(self, analysis_parameters):
439-
440464
self._analyses[analysis_parameters.analysis_name] = analysis_parameters
441465

442466
##############################################
443467

444-
def operating_point(self):
468+
def _add_measure(self, measure_parameters):
469+
self._measures.append(measure_parameters)
445470

446-
"""Compute the operating point of the circuit with capacitors open and inductors shorted."""
471+
##############################################
447472

473+
def operating_point(self):
474+
"""Compute the operating point of the circuit with capacitors open and inductors shorted."""
448475
self._add_analysis(OperatingPointAnalysisParameters())
449476

450477
##############################################
@@ -586,6 +613,29 @@ def ac(self, variation, number_of_points, start_frequency, stop_frequency):
586613

587614
##############################################
588615

616+
def measure(self, analysis_type, name, *args):
617+
618+
"""Add a measure in the circuit.
619+
620+
Examples of usage::
621+
622+
simulator.measure('TRAN', 'tdiff', 'TRIG AT=10m', 'TARG v(n1) VAL=75.0 CROSS=1')
623+
simulator.measure('tran', 'tdiff', 'TRIG AT=0m', f"TARG par('v(n1)-v(derate)') VAL=0 CROSS=1")
624+
625+
Note: can be used with the .options AUTOSTOP to stop the simulation at Trigger.
626+
627+
Spice syntax:
628+
629+
.. code:: spice
630+
631+
.meas tran tdiff TRIG AT=0m TARG v(n1) VAL=75.0 CROSS=1
632+
633+
"""
634+
635+
self._add_measure(MeasureParameters(analysis_type, name, *args))
636+
637+
##############################################
638+
589639
def transient(self, step_time, end_time, start_time=0, max_time=None,
590640
use_initial_condition=False):
591641

@@ -646,6 +696,8 @@ def __str__(self):
646696
else:
647697
all_str = ''
648698
netlist += '.save ' + all_str + join_list(saved_nodes) + os.linesep
699+
for measure_parameters in self._measures:
700+
netlist += str(measure_parameters) + os.linesep
649701
for analysis_parameters in self._analyses.values():
650702
netlist += str(analysis_parameters) + os.linesep
651703
netlist += '.end' + os.linesep
@@ -732,29 +784,24 @@ def _run(self, analysis_method, *args, **kwargs):
732784
##############################################
733785

734786
def operating_point(self, *args, **kwargs):
735-
736787
return self._run('operating_point', *args, **kwargs)
737788

738789
##############################################
739790

740791
def dc(self, *args, **kwargs):
741-
742792
return self._run('dc', *args, **kwargs)
743793

744794
##############################################
745795

746796
def dc_sensitivity(self, *args, **kwargs):
747-
748797
return self._run('dc_sensitivity', *args, **kwargs)
749798

750799
##############################################
751800

752801
def ac(self, *args, **kwargs):
753-
754802
return self._run('ac', *args, **kwargs)
755803

756804
##############################################
757805

758806
def transient(self, *args, **kwargs):
759-
760807
return self._run('transient', *args, **kwargs)

0 commit comments

Comments
 (0)