Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

GridLAB-D Python Bindings

This package provides Python bindings for GridLAB-D, a power system simulation platform.

Installation

  1. Build the GridLAB-D core (if not already built):

    # From the repository root
    mkdir build
    cd build
    cmake -DCMAKE_BUILD_TYPE=Debug ..
    cmake --build .
    
  2. Install the Python package in development mode:

    # From the repository root
    cd python_bindings
    pip install -e .
    
  3. Run tests to verify installation:

    cd python_bindings/tests
    pytest -v
    

Building Wheels for Distribution

To create wheel (.whl) and source distribution (.tar.gz) files for PyPI:

  1. Run the preparation script (copies built libraries into the package):

    cd python_bindings
    ./prepare_pypi_build.sh
    
  2. Build the distribution files:

    python3 -m build
    

    This creates both files in the dist/ directory.

Note: pip install -e . works for local development without running the preparation script because it accesses libraries directly from ../build/lib/. However, python -m build creates an isolated environment and requires the prebuilt libraries to be bundled within the package directory.

API Usage Examples

Basic Usage

import gridlabd

# Create a GridLAB-D instance
gld = gridlabd.GridLabD()S

# Load a model file
result = gld.load("path/to/model.glm")
assert result == 0, "Failed to load model"

# Initialize the model
result = gld.setup_after_load()
assert result == 0, "Failed to initialize"

# Run the simulation
result = gld.run()
assert result == 0, "Simulation failed"

print("Simulation completed successfully!")

Querying Objects and Properties

import gridlabd

# Load and initialize model
gld = gridlabd.GridLabD()
gld.load("test_HVAC_balance.glm")
gld.setup_after_load()

# Get all classes in the model
classes = gld.get_all_classes()
print(f"Classes in model: {classes}")

# Get all objects of a specific class
houses = gld.get_objects_by_class("house")
print(f"Found {len(houses)} houses")

# Get properties from a single object
if houses:
    props = gld.get_object_properties(houses[0])
    print(f"Floor area: {props.get('floor_area')}")

# Get all objects with all their properties
all_houses = gld.get_all_objects("house")
for house in all_houses:
    print(f"House {house['__name__']}: floor_area={house['floor_area']}")

# Get entire model as nested dictionary
model = gld.get_model()
for class_name, objects in model.items():
    print(f"{class_name}: {len(objects)} objects")

Setting Properties

import gridlabd

gld = gridlabd.GridLabD()
gld.load("model.glm")
gld.setup_after_load()

# Get objects
houses = gld.get_objects_by_class("house")

# Set a property value
if houses:
    result, value = gld.set_property(houses[0], "air_temperature", "72 degF")
    print(f"Set temperature result: {result}")
    
    # Verify the change
    result, new_value = gld.get_property(houses[0], "air_temperature")
    print(f"New temperature: {new_value}")

Stepping Through Simulation

import gridlabd

gld = gridlabd.GridLabD()
gld.load("model.glm")
gld.setup_after_load()

# Step through simulation timestep by timestep
for i in range(10):
    status, timestamp = gld.step()
    if status < 0:
        print("Simulation complete")
        break
    
    # Query state at each timestep
    houses = gld.get_all_objects("house")
    if houses:
        temp = houses[0].get('air_temperature')
        print(f"Timestep {i}, Time {timestamp}: Temperature = {temp}")

Message Capture

GridLAB-D messages (warnings, errors, debug output) are automatically captured and can be retrieved programmatically. By default, C++ output is suppressed to keep your console clean.

import gridlabd

# Default: C++ output suppressed, clean console
gld = gridlabd.GridLabD()

# Load and run model
gld.load("model.glm")
gld.setup_after_load()
gld.run()

# Get captured messages programmatically
messages = gld.get_messages()
for msg in messages:
    print(f"[{msg['type']}] {msg['timestamp']}: {msg['message']}")

# Filter for errors only
errors = [m for m in messages if m['type'] == 'ERROR']
print(f"Found {len(errors)} errors")

Verbose mode - Enable C++ console output for debugging:

# Show C++ output on stderr (useful for debugging)
gld = gridlabd.GridLabD(verbose=True)
gld.load("model.glm")
gld.run()

# Messages are still captured even in verbose mode
messages = gld.get_messages()

Message capture controls:

# Disable message capture (not recommended)
gld.enable_message_capture(False)

# Clear captured messages
gld.clear_messages()

# Set message limit (default: 10000)
gld.set_message_capture_limit(5000)
limit = gld.get_message_capture_limit()

Download files

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

Source Distribution

gridlabd-1.0.17a1.tar.gz (3.5 MB view details)

Uploaded Source

Built Distribution

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

gridlabd-1.0.17a1-cp39-cp39-macosx_26_0_arm64.whl (4.6 MB view details)

Uploaded CPython 3.9macOS 26.0+ ARM64

File details

Details for the file gridlabd-1.0.17a1.tar.gz.

File metadata

  • Download URL: gridlabd-1.0.17a1.tar.gz
  • Upload date:
  • Size: 3.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for gridlabd-1.0.17a1.tar.gz
Algorithm Hash digest
SHA256 d2bf614207b80716d068a76988427870b5b606cc5f40de6c514b0c61b782b454
MD5 42793b689267882cff037dd05470da45
BLAKE2b-256 f7b2ac4c618d086e89801839cea35f8d1a897a2d2b42f57df552a06be08fbec1

See more details on using hashes here.

File details

Details for the file gridlabd-1.0.17a1-cp39-cp39-macosx_26_0_arm64.whl.

File metadata

File hashes

Hashes for gridlabd-1.0.17a1-cp39-cp39-macosx_26_0_arm64.whl
Algorithm Hash digest
SHA256 62ba496b013c34aed74688ba6f7a5acbc90ca7aae662f713019285887cf771a6
MD5 09c68ccef7560a42e84ac7363b0f5e6d
BLAKE2b-256 4c6fce297ab64462723339f8cb9349e3e31b78fbfb75419c6b0a76f0170028b9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.17a1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page