Skip to main content

A simulation modelling language

Project description

Overview

Solverz is an open-source python-based simulation modelling language that provides symbolic interfaces for you to model your equations and can then generate functions or numba-jitted python modules for numerical solutions.

Solverz supports three types of abstract equation types, that are

  • Algebraic Equations (AEs) $0=F(y,p)$
  • Finite Difference Algebraic Equations (FDAEs) $0=F(y,p,y_0)$
  • Differential Algebraic Equations (DAEs) $M\dot{y}=F(t,y,p)$

where $p$ is the parameter set of your models, $y_0$ is the previous time node value of $y$.

Say, we want to know how long it will take for an apple, launched from the ground into the air, to fall back to the ground. We have the differential equations

$$ \begin{aligned} &v'=-9.8\ &h'=v \end{aligned} $$

with $v(0)=20$ and $h(0)=0$, we can just type the codes

import matplotlib.pyplot as plt
import numpy as np
from Solverz import Model, Var, Ode, Opt, made_numerical, Rodas

# Declare a simulation model
m = Model()
# Declare variables and equations
m.h = Var('h', 0)
m.v = Var('v', 20)
m.f1 = Ode('f1', f=m.v, diff_var=m.h)
m.f2 = Ode('f2', f=-9.8, diff_var=m.v)
# Create the symbolic equation instance and the variable combination 
bball, y0 = m.create_instance()
# Transform symbolic equations to python numerical functions.
nbball = made_numerical(bball, y0, sparse=True)

# Define events, that is,  if the apple hits the ground then the simulation will cease.
def events(t, y):
    value = np.array([y[0]]) 
    isterminal = np.array([1]) 
    direction = np.array([-1]) 
    return value, isterminal, direction

# Solve the DAE
sol = Rodas(nbball,
            np.linspace(0, 30, 100), 
            y0, 
            Opt(event=events))

# Visualize
plt.plot(sol.T, sol.Y['h'][:, 0])
plt.xlabel('Time/s')
plt.ylabel('h/m')
plt.show()

Then we have

image.png

The model is solved with the stiffly accurate Rosenbrock type method, but you can also write your own solvers by the generated numerical interfaces since, for example, the Newton-Raphson solver implememtation for AEs is as simple as below.

@ae_io_parser
def nr_method(eqn: nAE,
              y: np.ndarray,
              opt: Opt = None):
    if opt is None:
        opt = Opt(ite_tol=1e-8)

    tol = opt.ite_tol
    p = eqn.p
    df = eqn.F(y, p)
    ite = 0
    # main loop
    while max(abs(df)) > tol:
        ite = ite + 1
        y = y - solve(eqn.J(y, p), df)
        df = eqn.F(y, p)
        if ite >= 100:
            print(f"Cannot converge within 100 iterations. Deviation: {max(abs(df))}!")
            break

    return aesol(y, ite)

The implementation of the NR solver just resembles the formulae you read in any numerical analysis book. This is because the numerical AE object eqn provides the $F(t,y,p)$ interface and its Jacobian $J(t,y,p)$, which is derived by symbolic differentiation.

Sometimes you have very complex models and you dont want to re-derive them everytime. With Solverz, you can just use

from Solverz import module_printer

pyprinter = module_printer(bball,
                           y0,
                           'bounceball',
                           jit=True)
pyprinter.render()

to generate an independent python module of your simulation models. You can import them to your .py file by

from bounceball import mdl as nbball, y as y0

Installation

Solverz requires python>=3.10, and can be installed locally with

pip install Solverz

Useful Resources

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

solverz-0.5.0.tar.gz (583.4 kB view details)

Uploaded Source

Built Distribution

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

solverz-0.5.0-py3-none-any.whl (117.1 kB view details)

Uploaded Python 3

File details

Details for the file solverz-0.5.0.tar.gz.

File metadata

  • Download URL: solverz-0.5.0.tar.gz
  • Upload date:
  • Size: 583.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for solverz-0.5.0.tar.gz
Algorithm Hash digest
SHA256 d8ab54d2326ec4b446b3ea0bbac158b0d64aa7d3a52723fd0a0e3eff6d0986f1
MD5 29d667dd7e259779990912be873a7efe
BLAKE2b-256 ad78f8a1deece5f06c768a1c0786db305f85d82e6da338c24b4ef06e42c4a873

See more details on using hashes here.

Provenance

The following attestation bundles were made for solverz-0.5.0.tar.gz:

Publisher: publish-to-pypi.yml on smallbunnies/Solverz

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

File details

Details for the file solverz-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: solverz-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 117.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for solverz-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1dc24fee54ff65283402f17a14d3ac88c8d22fce3de9d835535eb7381902b8d5
MD5 43a5dddf919c3fcce7ebb45da3b85899
BLAKE2b-256 275f3da5ba1a4b3fbca41b7d4908b97ac3875a96154776507449eff08418a7e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for solverz-0.5.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on smallbunnies/Solverz

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