SimStepper¶
- class spicelib.sim.sim_stepping.SimStepper(editor, runner)[source]¶
Bases:
AnyRunnerThis class is intended to be used for simulations with many parameter sweeps. This provides a more user-friendly interface than the SpiceEditor/SimRunner class when there are many parameters to be stepped.
Using the SpiceEditor/SimRunner classes a loop needs to be added for each dimension of the simulations. A typical usage would be as follows:
netlist = SpiceEditor("my_circuit.asc") runner = SimRunner(parallel_sims=4) for dmodel in ("BAT54", "BAT46WJ") netlist.set_element_model("D1", model) # Sets the Diode D1 model for res_value1 in sweep(2.2, 2,4, 0.2): # Steps from 2.2 to 2.4 with 0.2 increments netlist.set_component_value('R1', res_value1) # Updates the resistor R1 value to be 3.3k for temperature in sweep(0, 80, 20): # Makes temperature step from 0 to 80 degrees in 20 degree steps netlist.set_parameters(temp=80) # Sets the simulation temperature to be 80 degrees for res_value2 in (10, 25, 32): netlist.set_component_value('R2', res_value2) # Updates the resistor R2 value to be 3.3k runner.run(netlist) runner.wait_completion() # Waits for the Spice simulations to complete
With SimStepper the same thing can be done as follows, resulting in a cleaner code:
netlist = SpiceEditor("my_circuit.asc") Stepper = SimStepper(netlist, SimRunner(parallel_sims=4, output_folder="./output")) Stepper.add_model_sweep('D1', "BAT54", "BAT46WJ") Stepper.add_component_sweep('R1', sweep(2.2, 2,4, 0.2)) # Steps from 2.2 to 2.4 with 0.2 increments Stepper.add_parameter_sweep('temp', sweep(0, 80, 20)) # Makes temperature step from 0 to 80 degrees in 20 # degree steps Stepper.add_component_sweep('R2', (10, 25, 32)) # Updates the resistor R2 value to be 3.3k Stepper.run_all()
Another advantage of using SimStepper is that it can optionally use the .SAVEBIAS in the first simulation and then use the .LOADBIAS command at the subsequent ones to speed up the simulation times.
- add_instruction(instruction)¶
Adds a SPICE instruction to the netlist.
For example:
.tran 10m ; makes a transient simulation .meas TRAN Icurr AVG I(Rs1) TRIG time=1.5ms TARG time=2.5ms ; Establishes a measuring .step run 1 100, 1 ; makes the simulation run 100 times .control ... control statements on multiple lines ... .endc
- Parameters:
instruction (str) – Spice instruction to add to the netlist. This instruction will be added at the end of the netlist, typically just before the .BACKANNO statement
- Returns:
Nothing
- Return type:
None
- add_instructions(*instructions)¶
Adds a list of instructions to the SPICE NETLIST.
Example:
editor.add_instructions(".STEP run -1 1023 1", ".dc V1 -5 5")
- Parameters:
instructions – Argument list of instructions to add
- Returns:
Nothing
- Return type:
None
- add_model_sweep(comp, iterable)[source]¶
Adds a dimension to the simulation, where a component model is swept.
- add_param_sweep(param, iterable)[source]¶
Adds a dimension to the simulation, where the param is swept.
- add_value_sweep(comp, iterable)[source]¶
Adds a dimension to the simulation, where a component value is swept.
- export_step_info(export_filename, delimiter=';')[source]¶
Exports the stepping values to a CSV file. It writes a row per each simulation done. The columns are all the values that were set during the session. The value on each row is the value of the parameter or component value/model at each simulation. This information can also be accessed using the sim_info attribute. The sim_info is a dictionary where the keys are the simulation number (runno) and the values are an enclosed dictionary containing the parameters there were set for that simulation.
- Parameters:
export_filename (Path | str) – export file path
delimiter (str) – delimiter character on the CSV
- property failSim¶
Number of failed simulations
- property okSim¶
Number of successful simulations
- remove_Xinstruction(search_pattern)¶
- remove_instruction(instruction)¶
- run_all(callback=None, callback_args=None, switches=None, timeout=None, wait_completion=True, filenamer=None, exe_log=False)[source]¶
Runs all sweeps configured with the methods:
add_value_sweep()
add_model_sweep()
add_param_sweep()
This function will call the SimRunner run method for each combination of the sweeps defined. The parameters are mostly the same as in the SimRunner.run() method, except the filenamer and wait_completion parameters.
- Parameters:
callback (Callable[[...], Any] | type[ProcessCallback] | None) – See the SimRunner run method.
callback_args (tuple | dict | None) – See the SimRunner run method.
switches (list | None) – Command line switches override
timeout (float | None) – See the SimRunner run method.
wait_completion (bool) – See the SimRunner run method.
filenamer (Callable[[dict[str, str]], str] | None) – A function that receives a dictionary in keyword form (
**dict) and returns a string. This string will be passed to the run_filename parameter on the SimRunner run method. It is important that the function assures a unique filename per simulation.exe_log (bool) – See the SimRunner run method.
- Returns:
Nothing
- Return type:
None
- property runno¶
Number simulations done.
- set_component_value(reference, value)¶
Changes the value of a component, such as a Resistor, Capacitor or Inductor. For components inside sub-circuits, use the sub-circuit designator prefix with ‘:’ as separator (Example X1:R1) Usage:
runner.set_component_value('R1', '3.3k') runner.set_component_value('X1:C1', '10u')
- Parameters:
reference (str) – Reference of the circuit element to be updated.
value (str, int or float) – value to be set on the given circuit element. Float and integer values will be automatically formatted as per the engineering notations ‘k’ for kilo, ‘m’, for mili and so on.
- Raises:
ComponentNotFoundError - In case the component is not found
ValueError - In case the value doesn’t correspond to the expected format
NotImplementedError - In case the circuit element is defined in a format which is not supported by this version.
If this is the case, use GitHub to start a ticket. https://github.com/nunobrum/spicelib
- set_component_values(**kwargs)¶
Adds one or more components on the netlist. The argument is in the form of a key-value pair where each component designator is the key and the value is value to be set in the netlist.
Usage 1:
editor.set_component_values(R1=330, R2="3.3k", R3="1Meg", V1="PWL(0 1 30m 1 30.001m 0 60m 0 60.001m 1)")
Usage 2:
value_settings = {'R1': 330, 'R2': '3.3k', 'R3': "1Meg", 'V1': 'PWL(0 1 30m 1 30.001m 0 60m 0 60.001m 1)'} editor.set_component_values(**value_settings)
- Key <comp_ref>:
The key is the component designator (Ex: V1) and the value is the value to be set. Values can either be strings; integers or floats
- Returns:
Nothing
- Raises:
ComponentNotFoundError - In case one of the component is not found.
- set_element_model(reference, model)¶
Changes the value of a circuit element, such as a diode model or a voltage supply. Usage:
runner.set_element_model('D1', '1N4148') runner.set_element_model('V1' "SINE(0 1 3k 0 0 0)")
- Parameters:
reference (str) – Reference of the circuit element to be updated.
model (str) – model name of the device to be updated
- Raises:
ComponentNotFoundError - In case the component is not found
ValueError - In case the model format contains irregular characters
NotImplementedError - In case the circuit element is defined in a format which is not supported by this version.
If this is the case, use GitHub to start a ticket. https://github.com/nunobrum/spicelib
- set_parameter(param, value)¶
Sets the value of a parameter in the netlist. If the parameter is not found, it is added to the netlist.
Usage:
runner.set_parameter("TEMP", 80)
This adds onto the netlist the following line:
.PARAM TEMP=80
This is an alternative to the set_parameters which is more pythonic in its usage and allows setting more than one parameter at once.
- Parameters:
param (str) – Spice Parameter name to be added or updated.
value (str, int or float) – Parameter Value to be set.
- Returns:
Nothing
- Return type:
None
- set_parameters(**kwargs)¶
Adds one or more parameters to the netlist. Usage:
for temp in (-40, 25, 125): for freq in sweep_log(1, 100E3,): editor.set_parameters(TEMP=80, freq=freq)
- Key param_name:
Key is the parameter to be set. values the ther corresponding values. Values can either be a str; an int or a float.
- Returns:
Nothing