Skip to main content

Add your description here

Project description

🎭 Khimsim (Khimaira-Sim)

Python Version License: GPL v3

khimsim is a high-performance Python library for compiling and simulating hybrid dynamical systems (block diagrams). It supports continuous-time ODE dynamics, multi-rate discrete-time clocks, event-driven state transitions (zero-crossings), and simultaneous resolution of algebraic loops.

Features

  • Continuous-Time Simulation: Native support for ODE models integrated using adaptive-step solvers (via SciPy solve_ivp).
  • Discrete-Time Simulation: Clock-bound execution (via PeriodicClock and ListClock) with Zero-Order Hold (ZOH) semantics.
  • Event-Driven Transitions: High-precision zero-crossing locator that stops integration to apply discrete updates (e.g., bouncing ball, relays) and handles state jumps.
  • Algebraic Loop Solver: Detects strongly-connected cycles of direct-feedthrough blocks and solves them simultaneously at runtime using scipy.optimize.fsolve.
  • Pythonic & Flexible Block API: Leverages method signature inspection so blocks only request the parameters they need (t, x, xd, u).
  • Comprehensive Blocks Library:
    • Continuous: Integrator, Filtered Derivative, PID, State-Space, Transfer Function, Zero-Pole, Transport Delay.
    • Discrete: Delay, ZOH, Discrete PID, Discrete Transfer Function, Filters, Tapped Delay, Variable Delay.
    • Discontinuities: Relay (with hysteresis events), Saturation, Dead Zone, PWM, Coulomb & Viscous Friction.
    • Math: Abs, Gain (scalar, vector, and matrix), Math Functions, Sign, Sum.
    • Sources: Constant, Step, Ramp, Sine, Pulse, Repeating Sequence, File Reader, Random Noise, Equation.

Installation

Ensure you have Python 3.13+ and the dependencies installed. This project uses uv for dependency management:

# Clone the repository
git clone https://github.com/your-repo/khimaira-sim.git
cd khimaira-sim

# Install dependencies and sync virtual environment
uv sync

Quick Start Example

Here is a simple example of a closed-loop system containing a continuous-time Plant ($x' = -x + u$) controlled by a proportional controller ($u = K(ref - y)$).

import numpy as np
import matplotlib.pyplot as plt
from khimsim import System, Simulator, Step, Gain, Integrator, Sum

# 1. Instantiate the blocks
ref = Step(step_time=1.0, initial_value=0.0, step_value=2.0, name="reference")
error = Sum(signs="+-", name="error")
ctrl = Gain(K=4.0, name="controller")
plant = Integrator(initial_value=0.0, name="plant")

# 2. Add blocks to the system
sys = System()
sys.add(ref)
sys.add(error)
sys.add(ctrl)
sys.add(plant)

# 3. Connect the diagram
sys.connect(ref, 0, error, 0)    # ref -> positive input of sum
sys.connect(plant, 0, error, 1)  # plant output -> negative input of sum
sys.connect(error, 0, ctrl, 0)   # error -> controller
sys.connect(ctrl, 0, plant, 0)   # controller output -> plant integrator

# 4. Compile and Run the Simulation
sys.compile()
sim = Simulator(sys)
t, y = sim.run(t_start=0.0, t_final=5.0)

# 5. Plot Results
# Extract global output indices of the blocks
ref_idx = sys.blocks_spec[ref].output_index[0]
plant_idx = sys.blocks_spec[plant].output_index[0]

plt.plot(t, y[ref_idx, :], 'r--', label="Reference")
plt.plot(t, y[plant_idx, :], 'b-', label="Plant State")
plt.xlabel("Time (s)")
plt.ylabel("Value")
plt.legend()
plt.title("Proportional Feedback Loop")
plt.show()

Running Tests

We use pytest for unit testing:

uv run pytest

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

khimsim-0.1.0.tar.gz (75.4 kB view details)

Uploaded Source

Built Distribution

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

khimsim-0.1.0-py3-none-any.whl (83.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: khimsim-0.1.0.tar.gz
  • Upload date:
  • Size: 75.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for khimsim-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ee186df7fd6b08916024962c5122a69f93b96c5edca5d0eb67768e62435b4726
MD5 851f6204a17d737f9c89a792e8eff276
BLAKE2b-256 3e1b2431112cee10522a0574e0e96768c665f96b42182251794c093f3aea2d4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for khimsim-0.1.0.tar.gz:

Publisher: publish.yml on brunogarbe/khimaira-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: khimsim-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 83.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for khimsim-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 886ec7a6695b31b9dc980edd947f6ea8e243fc74c68c7c7add74714603f4c69c
MD5 5ac372035d21537e52b7d5a01ab85894
BLAKE2b-256 0f77dcd1b38db38ee982612cf68c6dd8b72ddd652e3188c2e67d296b2a6ebda9

See more details on using hashes here.

Provenance

The following attestation bundles were made for khimsim-0.1.0-py3-none-any.whl:

Publisher: publish.yml on brunogarbe/khimaira-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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