Skip to main content

Python library for interfacing to the NL5 DLL based circuit simulator

Reason this release was yanked:

Issue with DLLs not included

Project description

nl5py

Python library for interfacing to the NL5 DLL based circuit simulator

nl5py is a Python interface for modifying, simulating, and extracting data from NL5 schematics using the NL5 DLL circuit simulator. There are plans in the future to also support the HTTP API for interfacing to open schmatics in the regular NL5 program.

Loading NL5 License

NL5 DLL can be used without a license, but will be limited to 20 components for tansient simulations. If your schematic has more than 20 components and no license is detected, the following exception will be raised when you attempt to run a transient simulation.

Exception: NL5_Simulate: Too many components for Demo version

There are two ways to include a license file. Either simple place an nl5.nll file in the same directory as the schematic file you are loading/simulating, or use the load_license() function.

from nl5py import load_license
license_info = load_license("nl5.nll")

Loading a Schematic

The Schematic class is the primary interface class. You initialize the class with a path to an existing NL5 schematic file, which will load this into the NL5 DLL.

from nl5py import Schematic
schematic = Schematic("analog.nl5")

Modifying Circuit Parameters

You can modify the properties of circuit elements using the set_value method.

schematic.set_value("C1", 2.1)  # change C1 to 2.1 Farads
schematic.set_value("C1.IC", 5.2) # Set the initial voltage of C1 to 5.2V

The library will verify that commands are recieved by the NL5 DLL without errors. If an error is detected, it gets converted into an Exception and raised.

schematic.set_value("C100", 2.1)  # C100 does not exist

Since C100 does not exist in the schematic, it will throw the following exception:

Exception: NL5_SetValue: parameter C100 not found

Modifying Subcircuits

If you want to modify elements in a subcircuit, you must do so using the set_text method and send all of the value changes as a string of comma deliminted commands.

# Change C1 and C2 in the subcircuit inside X1
commands = ["C1 = 1", "C2 = 2"]
schematic.set_text("X1.Cmd", ",".join(commands))

If you want to set a value that needs to be comma deliminited, such as a PWL value, the PWL section should be in quotes.

# C1 is a PWL capacitor
commands = ['C1.PWL = "1, 1, 2"', "C2 = 2"]
schematic.set_text("X1.Cmd", ",".join(commands))

Transient Simulations

Before a transient simulation is run, the user must ensure that the circuit includes all the traces (voltages, currents, powers, etc) that they want to observe. Traces can be added programmatically using the add_trace method.

schematic.add_trace(name="C1", trace_type="V")  # add the voltage on C1
schematic.add_trace(name="C1", trace_type="I")  # add the current on C1

All of the NL5 trace types are supported via the following "trace_type" strings:

trace_type Description
"V" Voltage
"I" Current
"P" Power
"Var" Variable
"Func" Function
"Data" Data

If no trace type is specified, it defaults to "Function".

schematic.add_trace(name="V(C2)")  		 # add the voltage on C1 via a function trace
schematic.add_trace(name="V(C1)+V(C2)")  # add the sum of voltages on C1 and C2 via a function trace

Running a transient simulation is done using the simulate_transient method.

schematic.simulate_transient(screen=20, step=1e-3)

After a simulation has completed, data can be extracted using get_data.

data = schematic.get_data(traces=["V(1)", "V(2)", "V(3)", "V(4)", "V(5)"])

The data is returned in the form of a pandas dataframe.

print(data.head())
              V(1)      V(2)      V(3)      V(4)      V(5)
0.000000  0.000000  0.000000  0.000000  0.000000  0.000000
0.000250  0.015708  0.005236  0.005236  0.005236  0.005236
0.000500  0.031416  0.010472  0.010472  0.010472  0.010472
0.000667  0.041888  0.013963  0.013963  0.013963  0.013963
0.000833  0.052360  0.017453  0.017453  0.017453  0.017453

AC Simulations

The Schematic class also supports AC simulations, which work in much the same way as the transient simulations.

# set the AC source
schematic.set_ac_source("I1")

# add AC traces
# TODO: Does not yet appear to be supported by DLL API

# run simulation
schematic.simulate_ac(start_frequency=1e3, stop_frequency=1e6, num_points=5000)

# extract data
data = schematic.get_ac_data(traces=["V(1)", "V(2)"])

The data returned from get_ac_data is a hierarchical index'd data frame with magnitude and phase info for each signal.

print(data.head())
            V(1)                       V(2)                               
            magnitude       phase      magnitude       phase
1000.000000  0.009758  179.121846       0.009758  179.121846
1199.839968  0.014131  178.946299       0.014131  178.946299
1399.679936  0.019365  178.770721       0.019365  178.770721
1599.519904  0.025496  178.595105       0.025496  178.595105
1799.359872  0.032566  178.419447       0.032566  178.419447

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nl5py-0.1.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

nl5py-0.1.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file nl5py-0.1.0.tar.gz.

File metadata

  • Download URL: nl5py-0.1.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.11

File hashes

Hashes for nl5py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 42c021e04cad1be514848f9eab1628a4195667213be01a13501b0bb8c37bec1b
MD5 d79bc2768fa5b380d74af28dc66d5fa2
BLAKE2b-256 76ec49915acc94d6e98550ac86caff96732ca13b6651f4cc192be353e4c1b5ec

See more details on using hashes here.

File details

Details for the file nl5py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nl5py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.11

File hashes

Hashes for nl5py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30033ca427dca77bb1c232206a2538f969728198b3f84adb5f1ac4a6c8a2d323
MD5 6c5e7855cbdf6d52fc8187cc50ad7b65
BLAKE2b-256 1edb215957cea73c7c4a597326c2e2592bb2eb98f863cc724d187db368ecf6f9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page