Skip to main content

Python library for interfacing to the NL5 DLL based circuit simulator

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

You can get a current list of traces using the "get_trace_names" method.

traces = schematic.get_trace_names()
print(traces)

You can clear all existing traces using the "clear_traces" method.

schematic.clear_traces()
print(traces)

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)"])

If you simply want the data from all traces, you can run "get_data" with no arguments and traces will default to all traces currently added.

data = schematic.get_data()

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.6.tar.gz (1.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nl5py-0.1.6-py3-none-any.whl (1.6 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nl5py-0.1.6.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for nl5py-0.1.6.tar.gz
Algorithm Hash digest
SHA256 624ff516c752f780cb695b3658a1049aadff71987f08ccfee93cc4148a80da56
MD5 983eb792035cb92a47ebcf9e58f7625f
BLAKE2b-256 06221d61e2da2f3a521fa18884b41cf1a41393e1bb305136e4284c0700bc2590

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nl5py-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for nl5py-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2eaef2fa5c9981ee563985ae84f2840e144b1c96e890e3c9687aacf563d653cc
MD5 752a1d3b4346e7e8db5685ca1044ac1c
BLAKE2b-256 956ed796e98f0aacc7210223870174eb23e36b38e691466f3b3e39acc400b0f5

See more details on using hashes here.

Supported by

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