Add your description here
Project description
🎭 Khimsim (Khimaira-Sim)
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
PeriodicClockandListClock) 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee186df7fd6b08916024962c5122a69f93b96c5edca5d0eb67768e62435b4726
|
|
| MD5 |
851f6204a17d737f9c89a792e8eff276
|
|
| BLAKE2b-256 |
3e1b2431112cee10522a0574e0e96768c665f96b42182251794c093f3aea2d4f
|
Provenance
The following attestation bundles were made for khimsim-0.1.0.tar.gz:
Publisher:
publish.yml on brunogarbe/khimaira-sim
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
khimsim-0.1.0.tar.gz -
Subject digest:
ee186df7fd6b08916024962c5122a69f93b96c5edca5d0eb67768e62435b4726 - Sigstore transparency entry: 2128264518
- Sigstore integration time:
-
Permalink:
brunogarbe/khimaira-sim@563ca9e1b59c4bbd0b2b77ef89fd11a154ccb613 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/brunogarbe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@563ca9e1b59c4bbd0b2b77ef89fd11a154ccb613 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
886ec7a6695b31b9dc980edd947f6ea8e243fc74c68c7c7add74714603f4c69c
|
|
| MD5 |
5ac372035d21537e52b7d5a01ab85894
|
|
| BLAKE2b-256 |
0f77dcd1b38db38ee982612cf68c6dd8b72ddd652e3188c2e67d296b2a6ebda9
|
Provenance
The following attestation bundles were made for khimsim-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on brunogarbe/khimaira-sim
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
khimsim-0.1.0-py3-none-any.whl -
Subject digest:
886ec7a6695b31b9dc980edd947f6ea8e243fc74c68c7c7add74714603f4c69c - Sigstore transparency entry: 2128264585
- Sigstore integration time:
-
Permalink:
brunogarbe/khimaira-sim@563ca9e1b59c4bbd0b2b77ef89fd11a154ccb613 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/brunogarbe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@563ca9e1b59c4bbd0b2b77ef89fd11a154ccb613 -
Trigger Event:
release
-
Statement type: