Skip to main content

Python bindings for ModelSolver library

Project description

dTwin

dTwin is a Python interface to a high‑performance C++ nonlinear model solver capable of solving:

  • Static problems

    • Nonlinear algebraic equations (NLE)
    • Weighted Least Squares (WLS)
  • Dynamic problems

    • Ordinary Differential Equations (ODE)
    • Nonlinear Differential‑Algebraic Equations (DAE)

It is built using the C++ modelSolver library (part of the dTwin framework).

This README explains how to install, understand, and use dTwin step by step, with real working examples.


1. Installation

Install from TestPyPI / PyPI

pip install --index-url https://test.pypi.org/simple/ dTwin

or

pip install dTwin --index-url https://test.pypi.org/simple/

(or from PyPI once released)

pip install dTwin

Verify installation

import dTwin
from dTwin import modelSolver
print(dTwin.__doc__)

2. What does dTwin.modelSolver solve?

Problem type Description
Static (NLE) Systems of nonlinear algebraic equations
Static (WLS) Weighted least squares estimation
Dynamic (ODE) Time‑domain simulation of ODE systems
Dynamic (DAE) Time‑domain simulation of nonlinear DAEs

Models are described using .dmodl files, a domain‑specific language for defining variables, parameters, equations, limits, controllers, and submodels.


3. Core Python API overview

Enumerations

dTwin.StaticProblem.NLE
dTwin.StaticProblem.WLS

dTwin.DynamicProblem.ODE
dTwin.DynamicProblem.DAE

dTwin.Solution.OK

Vector types (C++ backed)

These are zero‑copy wrappers around C++ memory:

dTwin.DoubleVector
dTwin.StringVector
dTwin.UintVector

They behave like Python vectors:

v = dTwin.DoubleVector(3)
v[0] = 1.0
print(len(v), v)

##4. Examples

There are several examples in the models subfolder.

4.1 Static model example (Power‑flow NLE)

Model file: PF_PV_03.dmodl

This model solves a power‑flow problem with:

  • PV and PQ buses
  • Generator voltage regulation (PV node with limits)
  • Reactive power limits

(Details: see associated arXiv paper.)

Python usage

import dTwin
from pathlib import Path

p_log = dTwin.getConsoleLogger()

# Create real‑valued static model (NLE)
p_model = dTwin.createRealStaticModel(
    dTwin.StaticProblem.NLE,
    p_log
)

# Load model
p_model.initFromFile("PF_PV_03.dmodl")

# Obtain solver
p_solver = p_model.getSolverInterface()

# Solve
status = p_solver.solve()
assert status == dTwin.Solution.OK

# Extract outputs
out_indices = p_model.getOutputSymbolIndices()
out_names   = p_model.getOutputSymbolNames(out_indices)
out_values  = p_model.getOutputSymbolValues(out_indices)

for i in range(len(out_names)):
    print(out_names[i], out_values[i])

p_model.release()

Parameter manipulation

idx = p_model.getParameterIndex("P3_inj")
vals = p_model.getParameterValues(dTwin.UintVector([idx]))
vals[0] -= 0.1
p_model.setParameterValues(dTwin.UintVector([idx]), vals)

p_solver.solve()

4.2 Dynamic DAE example (Generator frequency regulation)

Model file: FreqReg_01.dmodl

This model simulates:

  • Synchronous generator swing equation
  • PI frequency controller
  • Nonlinear network algebraic equations
  • Initial power‑flow solved via a SubModel
  • Fully nonlinear time‑domain simulation

Python usage

import dTwin

p_log = dTwin.getConsoleLogger()

p_model = dTwin.createRealDynamicModel(
    dTwin.DynamicProblem.DAE,
    p_log
)

p_model.initFromFile("FreqReg_01.dmodl")

p_solver = p_model.getSolverInterface()

# Time step (optional)
dt = p_solver.getStepSize()
if dt <= 0:
    p_solver.setStepSize(0.001)    

# Initialize
p_solver.reset(0.0)

out_idx   = p_model.getOutputSymbolIndices()
out_names = p_model.getOutputSymbolNames(out_idx)

# Time loop
t = 0.0
T_END = 20.0

while t <= T_END:
    #change model parameter to simulate transients
    sol = p_solver.step()
    assert sol == dTwin.Solution.OK

    values = p_model.getOutputSymbolValues(out_idx)
    print(t, list(values))
    t += dt

p_model.release()

4.3 Dynamic model with transfer functions (Dorf's example)

Model file: TF_Dorf_E760_PD_Disk_RK4.dmodl.

Features:

  • Laplace transfer functions (TFs)
  • PID (PD) controller
  • Implicit RK4/RK6 integration

No Python code changes are required — simply load the model and simulate as shown above.


5. Memory management (important)

Models are C++ objects.

  • They are automatically released when garbage‑collected
  • You may explicitly free memory (optional):
p_model.release()

This is recommended for long simulations or batch runs.


6. Design notes (advanced users)

  • All vectors are backed by natID's fast cnt::SafeFullVector<T>
  • No implicit memory copies between Python and C++
  • td::String (natID) is transparently convertible to/from Python str
  • Dynamic solvers support implicit DAE solving
  • SubModels allow hierarchical initialization and reuse

7. Requirements

  • Python 3.13+
  • Windows: visual C++ runtimes
  • NumPy (optional, for post‑processing)
  • OS‑specific native libraries bundled in internally

8. License & citation

If you use dTwin in academic work, please cite the accompanying paper (see arXiv link).


9. Summary

dTwin enables research‑grade nonlinear modeling directly from Python while retaining ultra-fast C++ performance.

It is suitable for:

  • Digital twinning
  • Simulation
  • Dynamic simulation
  • Power systems
  • Control systems
  • Nonlinear estimation

Happy modeling 🚀

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

dtwin-1.1.9-cp313-cp313-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.13Windows x86-64

dtwin-1.1.9-cp313-cp313-manylinux_2_28_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

dtwin-1.1.9-cp313-cp313-macosx_12_0_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.13macOS 12.0+ x86-64

dtwin-1.1.9-cp313-cp313-macosx_12_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.13macOS 12.0+ ARM64

dtwin-1.1.9-cp312-cp312-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.12Windows x86-64

dtwin-1.1.9-cp312-cp312-macosx_12_0_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12macOS 12.0+ x86-64

dtwin-1.1.9-cp312-cp312-macosx_12_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.12macOS 12.0+ ARM64

dtwin-1.1.9-cp311-cp311-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.11Windows x86-64

dtwin-1.1.9-cp311-cp311-manylinux_2_28_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

dtwin-1.1.9-cp311-cp311-macosx_12_0_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11macOS 12.0+ x86-64

dtwin-1.1.9-cp311-cp311-macosx_12_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.11macOS 12.0+ ARM64

dtwin-1.1.9-cp310-cp310-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.10Windows x86-64

dtwin-1.1.9-cp310-cp310-manylinux_2_28_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

dtwin-1.1.9-cp310-cp310-macosx_12_0_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10macOS 12.0+ x86-64

dtwin-1.1.9-cp310-cp310-macosx_12_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.10macOS 12.0+ ARM64

dtwin-1.1.9-cp39-cp39-win_amd64.whl (3.9 MB view details)

Uploaded CPython 3.9Windows x86-64

dtwin-1.1.9-cp39-cp39-manylinux_2_28_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

dtwin-1.1.9-cp39-cp39-macosx_12_0_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9macOS 12.0+ x86-64

dtwin-1.1.9-cp39-cp39-macosx_12_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.9macOS 12.0+ ARM64

File details

Details for the file dtwin-1.1.9-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: dtwin-1.1.9-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dtwin-1.1.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ca14382ffe1fbf845cb3993b4622a9a10c75d7767003a4252b9f472ec400f259
MD5 ced4e14b5abe6b895dfcc560c882635d
BLAKE2b-256 0cae50c406a76b5676e2cc9847996b8719b573a1193bfd78939eb963e6c63e77

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e41c0cdfde3b2de854d6a48ea27cc5b91aeff5304ded9a2a98be51743ecaea9b
MD5 0288157a307d8fccde8fa284644b4b08
BLAKE2b-256 43aa3d54cb23fdd2f0bc480460c080258a1b1a8149e567b9b492e1431aa5fd65

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp313-cp313-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp313-cp313-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 244d62ce738a7fe867440a0fbf18d8c8f5852211346604417abdb2f583a08f90
MD5 4ade5c8065749f1376f2e752b7958b2c
BLAKE2b-256 aadb7245989638b3131ac1bd9c5fc85cf7636824482b0431e696b84fd535d005

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp313-cp313-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp313-cp313-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 dbbe982eaf7da9b4b03f3e27e12caadad611b8f517ac1ef15b92bbaf4e7c3183
MD5 4f59e087568d6e657f4566081cfbac74
BLAKE2b-256 7e0790c27262e26f0b57c66c5cdae31597d65c11fb52e90b198e1de8fd20c771

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: dtwin-1.1.9-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dtwin-1.1.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3835d2e6208815606980ccf72ce2b744378abf16ab1ca042efba52523c2ce3fa
MD5 ba987c951dc6c39c60f141998d078612
BLAKE2b-256 a23f43078f8d42a6cb74c6c843621af7e1856c0e993157efa8c96f39e2ea5737

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp312-cp312-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp312-cp312-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 b13fd093333564e078f90e23254eadd93d061e9e101302cf02fbec7d0b0ce841
MD5 e5d2e06ba10148e2a518a4bacff651dc
BLAKE2b-256 cb8269b0bab9e23338cc473c78e6983edcfe5c97f9b1058a31582812688fa356

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp312-cp312-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp312-cp312-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 744bbacf0238f048a0eff677fde9868ea09c1ec4c47d08e24e6d9a94fb7c697d
MD5 c379bd54884e5b1e0b9769a5e70a9c1c
BLAKE2b-256 99c0832e8a81bfc94ef688cfddaa92621152521cb8425765259050e5da988ac7

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: dtwin-1.1.9-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dtwin-1.1.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0dd4b9b645d1c2bb151f4d17e97272e4b0aba65dde6e54073e1c033343abdc62
MD5 d3e6d6f537e443e21f4f11e1923181e3
BLAKE2b-256 ccddb77b5cf855df9decbb1cbd3a6b5b556cc28dcc26442944faf6959321eb1f

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d41de0a9f7c02f484a96806077a36ce239a9bab851a78c252c1c70ff78f635b9
MD5 655f2aa200ece3356980b1133c337109
BLAKE2b-256 acf9047cdc80dff39cae1f12a1a59504e7c00c9cf9902315a55c42953f840ae9

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp311-cp311-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp311-cp311-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 18058f64439d8e13c93cdd7c9eec76a4bdb514db2034f81baf426de2d0451d02
MD5 9aee54cabac234af90e0a26943e0a43e
BLAKE2b-256 0940fd1c2abc84d2b10206b31c55430fa7ec083aa64dbc75ca2bb8c29a841814

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp311-cp311-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp311-cp311-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 147d17a81c22395e7a3fa997b8b4738cc33df75debd76f28e7b7bb6e1d89252d
MD5 f38e84647359ee4b53358c4a34ee1844
BLAKE2b-256 5ea5a5b920956e4e7db394212461e4e2f81985de324132183888575350857675

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: dtwin-1.1.9-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dtwin-1.1.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 25202fbb0261e9ae03da3c1ee28c6f209f6d5ea8a8df913819e2d9fc87992d44
MD5 6a14677e14b6da9aea2a936db3dbd64d
BLAKE2b-256 8b7a1ee7dfd768ae4d2dc2698d936772c0e3960a4d90980a1ab845a13780899b

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ace5fd81cd880ef3328e1ad1cfc0a157cff7f8f2b3d71e9aeef07c119115db60
MD5 824199f2494f7e88c54fa6c699a96f9b
BLAKE2b-256 1c4b912f7ed370b30dbd59c871efe3f953265ea28c87b625baec7ed32d05cf17

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp310-cp310-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 e49748724b28d4814b95571ba6d74cd9017eed7abdff86019210acbcda2ea760
MD5 758a18e4fc66dbc84fd868cb2786bd75
BLAKE2b-256 b24545d8e4bc129e0d22c2a65d07ff6078d30d747e69816b84b7dc4e1039575c

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp310-cp310-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp310-cp310-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 b161716ee2f53acf97e623e1d712a07a107da62ba1ac1ff2b656cd7fd848ece7
MD5 e6959b04572f5df4901ee74fb716e91a
BLAKE2b-256 dac188035e9b51211c320280412e6792ee783935fd96905fad21df48b80a0d91

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: dtwin-1.1.9-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 3.9 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dtwin-1.1.9-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 42d7712fdabed2d8b6edfc6d882f33c4d5461d19cd786c56a95b5959885fe22a
MD5 c6a9eb5a75c675e7e2f72f3e40686ccd
BLAKE2b-256 1f4b49d4dc50f394434a59241424d36e1e9a892be60a0a2c0aba4cfbc01aa6db

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dc536801e3b3e87984bf2d9b143ceedbc2e79c3e5d8a5b9e8d192042762bbf89
MD5 3ac12d2899c18663db2014c417d36da5
BLAKE2b-256 c5ba59238ec5ce8e94ca949376e6340658f8e9791d9c053da9ce2874b6c32b17

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp39-cp39-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for dtwin-1.1.9-cp39-cp39-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 0b6c31a1199d947648049638573a0ae3bd546f83e19adf33c3328f6af731fa17
MD5 b743d2893ec053dc629c96db64619aaa
BLAKE2b-256 43e4e42530edc028e1bbc9809fa217b7d463a42c5a290eee637c19dbf3689704

See more details on using hashes here.

File details

Details for the file dtwin-1.1.9-cp39-cp39-macosx_12_0_arm64.whl.

File metadata

  • Download URL: dtwin-1.1.9-cp39-cp39-macosx_12_0_arm64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.9, macOS 12.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dtwin-1.1.9-cp39-cp39-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 d9575875febf4923b300aa8fac24b1c4861b07e59b37f605cfd5bbca6ca9c902
MD5 9789f1fdc2b673227355dc241fae497c
BLAKE2b-256 5aed4885c5ca1bf8ab44c5351513093e16154cbb90f88055c932c25cbe19d15b

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