Skip to content

Commit

Permalink
Corrections on the README.md Examples. Full Alignment with code on th…
Browse files Browse the repository at this point in the history
…e examples directory
  • Loading branch information
nunobrum committed Apr 28, 2024
1 parent bcdf5c3 commit c3115aa
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
52 changes: 39 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ from PyLTSpice import RawRead

from matplotlib import pyplot as plt

LTR = RawRead("TRAN - STEP.raw")
LTR = RawRead("./testfiles/TRAN - STEP.raw")

print(LTR.get_trace_names())
print(LTR.get_raw_property())
Expand All @@ -101,7 +101,8 @@ for step in range(len(steps)):

plt.legend() # order a legend
plt.show()
```
```
-- in examples/raw_read_example.py

### RawWrite ###

Expand All @@ -110,17 +111,17 @@ The following example writes a RAW file with a 3 milliseconds transient simulati

```python
import numpy as np
from PyLTSpice import Trace, RawWrite

from PyLTSpice import RawRead, Trace, RawWrite
LW = RawWrite(fastacces=False)
tx = Trace('time', np.arange(0.0, 3e-3, 997E-11))
vy = Trace('N001', np.sin(2 * np.pi * tx.data * 10000))
vz = Trace('N002', np.cos(2 * np.pi * tx.data * 9970))
LW.add_trace(tx)
LW.add_trace(vy)
LW.add_trace(vz)
LW.save("teste_snippet1.raw")
LW.save("./testfiles/teste_snippet1.raw")
```
-- in examples/raw_write_example.py [RawWrite Example]

### SpiceEditor, AscEditor and SimRunner.py ###

Expand All @@ -136,20 +137,24 @@ Here follows an example of operation.
from PyLTSpice import SimRunner
from PyLTSpice import SpiceEditor

# Force another simulatior
simulator = r"C:\Program Files\LTC\LTspiceXVII\XVIIx64.exe"

# select spice model
LTC = SimRunner(output_folder='./temp')
LTC.create_netlist('Batch_Test.asc')
netlist = SpiceEditor('Batch_Test.net')
LTC.create_netlist('./testfiles/Batch_Test.asc')
netlist = SpiceEditor('./testfiles/Batch_Test.net')
# set default arguments
netlist.set_parameters(res=0, cap=100e-6, run=-1)
netlist.set_parameters(res=0, cap=100e-6)
netlist.set_component_value('R2', '2k') # Modifying the value of a resistor
netlist.set_component_value('R1', '4k')
netlist.set_element_model('V3', "SINE(0 1 3k 0 0 0)") # Modifying the
netlist.set_component_value('XU1:C2', 20e-12) # modifying a define simulation
netlist.add_instructions(
"; Simulation settings",
".save V(Vin) I(R1)",
";.param run = 0"
)
netlist.set_parameter('run', 0)

for opamp in ('AD712', 'AD820'):
netlist.set_element_model('XU1', opamp)
Expand Down Expand Up @@ -181,6 +186,7 @@ enter = input("Press enter to delete created files")
if enter == '':
LTC.file_cleanup()
```
-- in examples/sim_runner_example.py

The example above is using the SpiceEditor to create and modify a spice netlist, but it is also possible to use the
AscEditor to directly modify the .asc file. The edited .asc file can then be opened by the LTSpice GUI and the
Expand Down Expand Up @@ -222,12 +228,26 @@ mc.prepare_testbench(num_runs=1000) # Prepares the testbench for 1000 simulatio
# Finally the netlist is saved to a file
mc.save_netlist('./testfiles/sallenkey_mc.net')

mc.run(100) # Runs the simulation with splits of 100 runs each
mc.run_testbench(runs_per_sim=100) # Runs the simulation with splits of 100 runs each
logs = mc.read_logfiles() # Reads the log files and stores the results in the results attribute
logs.obtain_amplitude_and_phase_from_complex_values() # Splits the complex values into real and imaginary parts
logs.export_data('./temp_mc/data_testbench.csv') # Exports the data to a csv file
logs.plot_histogram('fcut') # Plots the histograms for the results
mc.cleanup_files() # Deletes the temporary files

print("=====================================")
# Now using the second method, where the simulations are ran one by one
mc.clear_simulation_data() # Clears the simulation data
mc.reset_netlist() # Resets the netlist to the original
mc.run_analysis(num_runs=1000) # Runs the 1000 simulations
logs = mc.read_logfiles() # Reads the log files and stores the results in the results attribute
logs.export_data('./temp_mc/data.csv') # Exports the data to a csv file
logs.export_data('./temp_mc/data_sims.csv') # Exports the data to a csv file
logs.plot_histogram('fcut') # Plots the histograms for the results
mc.cleanup_files() # Deletes the temporary files

```
-- in examples/run_montecarlo.py

When opening the created sallenkey_mc.net file, we can see that the following circuit.

![Sallen-Key Amplifier with Montecarlo](./doc/modules/sallenkey_mc.png "Sallen-Key Amplifier with Montecarlo")
Expand Down Expand Up @@ -268,8 +288,8 @@ wca.set_parameter_deviation('Vos', 3e-4, 5e-3)
# Finally the netlist is saved to a file
wca.save_netlist('./testfiles/sallenkey_wc.asc')

wca.run_testbench() # Runs the simulation with splits of 100 runs each

wca.run() # Runs the simulation with splits of 100 runs each
logs = wca.read_logfiles() # Reads the log files and stores the results in the results attribute
logs.export_data('./temp_wca/data.csv') # Exports the data to a csv file

Expand All @@ -279,6 +299,8 @@ for param in ('fcut', 'fcut_FROM'):

wca.cleanup_files() # Deletes the temporary files
```
-- in examples/run_worst_case.py

When opening the created sallenkey_wc.net file, we can see that the following circuit.

![Sallen-Key Amplifier with WCA](./doc/modules/sallenkey_wc.png "Sallen-Key Amplifier with WCA")
Expand Down Expand Up @@ -306,9 +328,12 @@ written. There are two possible usages of this module, either programmatically b
accessing data through the class as exemplified here:

```python
#!/usr/bin/env python
# coding=utf-8

from PyLTSpice.log.ltsteps import LTSpiceLogReader

data = LTSpiceLogReader("Batch_Test_AD820_15.log")
data = LTSpiceLogReader("./testfiles/Batch_Test_AD820_15.log")

print("Number of steps :", data.step_count)
step_names = data.get_step_vars()
Expand All @@ -324,6 +349,7 @@ for i in range(data.step_count):

print("Total number of measures found :", data.measure_count)
```
-- in examples/ltsteps_example.py

The second possibility is to use the module directly on the command line

Expand Down
18 changes: 18 additions & 0 deletions examples/raw_read_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from PyLTSpice import RawRead

from matplotlib import pyplot as plt

LTR = RawRead("./testfiles/TRAN - STEP.raw")

print(LTR.get_trace_names())
print(LTR.get_raw_property())

IR1 = LTR.get_trace("I(R1)")
x = LTR.get_trace('time') # Gets the time axis
steps = LTR.get_steps()
for step in range(len(steps)):
# print(steps[step])
plt.plot(x.get_wave(step), IR1.get_wave(step), label=steps[step])

plt.legend() # order a legend
plt.show()
24 changes: 21 additions & 3 deletions examples/raw_write_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np
from PyLTSpice import RawRead, Trace, RawWrite


def test_readme_snippet():
# -- Start of RawWrite Example --
import numpy as np
from PyLTSpice import RawRead, Trace, RawWrite
LW = RawWrite(fastacces=False)
tx = Trace('time', np.arange(0.0, 3e-3, 997E-11))
vy = Trace('N001', np.sin(2 * np.pi * tx.data * 10000))
Expand All @@ -11,9 +11,13 @@ def test_readme_snippet():
LW.add_trace(vy)
LW.add_trace(vz)
LW.save("./testfiles/teste_snippet1.raw")
# -- End of RawWrite Example --


def test_trc2raw(): # Convert Teledyne-Lecroy trace files to raw files
# -- Start of Lecroy Raw File into LTspice raw file Example --
import numpy as np
from PyLTSpice import RawRead, Trace, RawWrite
f = open(r"./testfiles/Current_Lock_Front_Right_8V.trc")
raw_type = '' # Initialization of parameters that need to be overridden by the file header
wave_size = 0
Expand All @@ -33,9 +37,13 @@ def test_trc2raw(): # Convert Teledyne-Lecroy trace files to raw files
LW.add_trace(Trace('Ampl', [x[1] for x in data]))
LW.save("teste_trc.raw")
f.close()
# -- End of Lecroy Raw File into LTspice raw file Example --


def test_axis_sync(): # Test axis sync
# -- Start of Combining two different time axis --
import numpy as np
from PyLTSpice import RawRead, Trace, RawWrite
LW = RawWrite()
tx = Trace('time', np.arange(0.0, 3e-3, 997E-11))
vy = Trace('N001', np.sin(2 * np.pi * tx.data * 10000))
Expand Down Expand Up @@ -64,9 +72,12 @@ def test_axis_sync(): # Test axis sync
print(v[ii], vy[ii], v[ii] - vy[ii])
print(max_error)
"""
# -- End of Combining two different Raw Files --


def test_write_ac():
# -- Start of Writing .AC raw files Example --
from PyLTSpice import RawRead, Trace, RawWrite
LW = RawWrite()
LR = RawRead("./testfiles/PI_Filter.raw")
LR1 = RawRead("./testfiles/PI_Filter_resampled.raw")
Expand All @@ -76,19 +87,25 @@ def test_write_ac():
LW.save("./testfiles/PI_filter_rewritten.raw")
LW.flag_fastaccess = True
LW.save("./testfiles/PI_filter_rewritten_fast.raw")
# -- End of Writing .AC raw files Example --


def test_write_tran():
# -- Start of creating a subset of a raw file --
from PyLTSpice import RawRead, Trace, RawWrite
LR = RawRead("./testfiles/TRAN - STEP.raw")
LW = RawWrite()
LW.add_traces_from_raw(LR, ('V(out)', 'I(C1)'))
LW.flag_fastaccess = False
LW.save("./testfiles/TRAN - STEP0_normal.raw")
LW.flag_fastaccess = True
LW.save("./testfiles/TRAN - STEP0_fast.raw")
# -- End of creating a subset of a raw file --


def test_combine_tran():
# -- Start of Combining two different Raw Files --
from PyLTSpice import RawRead, RawWrite
LW = RawWrite()
for tag, raw in (
("AD820_15", "./testfiles/Batch_Test_AD820_15.raw"),
Expand All @@ -102,6 +119,7 @@ def test_combine_tran():
LW.add_traces_from_raw(LR, ("V(out)", "I(R1)"), rename_format="{}_{tag}", tag=tag, force_axis_alignment=True)
LW.flag_fastaccess = False
LW.save("./testfiles/Batch_Test_Combine.raw")
# -- End of Combining two different Raw Files --


test_readme_snippet()
Expand Down

0 comments on commit c3115aa

Please sign in to comment.