Skip to main content


For the last few weeks I have been working on a Simulink / Labview / Ptolemy II like program for modelling... all sorts of stuff!! I am interested in modeling with different systems of computation, and of course some engineering problems, mechanical and electrical control etc. Ptolemey II is an open source java based system which a whole lot of people from Berkley have been working on for a number of years - I am not trying to match what they have - or get anywhere near the breadth that the commercial products Simulink and Labview have. But I do think something useful is very achievable - and of course leveraging what I can from Ptolemy II and basing mine on Python - heavily using SciPy, NumPy and MatPlotLib.

So far I have been working on the semantics - in essence creating a new language for defining models by using blocks (or Actors) that carry out a very specific function. An example actor might be a Ramp Source or a Sin Function. Each of these actors run in their own thread and communicate via dedicated Channels - which are based on the thread safe fifo queue implementation in the Python standard library. These base level actors can be composed together to create models, which are also actors in their own right - running in their own thread with all communication occurring through input and output channels.

The final goal is to have a visual interface - drag and drop blocks around, then connect up the model with channels. For now the whole thing is code based, so one of the more complicated models I have made is a simple pulse width modulator generator.

Here is the full code that defines and runs the model:
Created on 13/12/2009

@author: brian

from models.actors import Plotter, Ramp, Summer, Copier, Sin, GreaterThan, Subtractor, Constant, PassThrough
from models import CTSinGenerator
import matplotlib.pyplot as plt
from models.actors.Actor import MakeChans

import logging
logging.basicConfig(level=logging.INFO)"Logger enabled")

def run_multi_sum():
    This example connects 3 sources ( 2 ramps and a random) to a summer block
    The final output AND one of the ramps are dynamically plotted.
    The components are all generating the same sequence of tags so are always
    sim_time = 0.02
    sim_res = 10000
    wire_names = ('ramp',
    raw_wires = MakeChans(len(wire_names))
    wires = dict(zip( wire_names, raw_wires ))
    ramp_src = Ramp(wires['ramp_probe'], freq=500, simulation_time=sim_time, resolution=sim_res)
    sin_src = CTSinGenerator(wires['sin'], amplitude=0.5, freq=50.0, phi=0.0, timestep=1.0/sim_res, simulation_time=sim_time)
    const_src = Constant(wires['const_offset'], 0.5, resolution=sim_res, simulation_time=sim_time)
    offset_sin_sum = Summer([wires['sin'], wires['const_offset'] ], wires['offset_sin_probe'])
    ramp_cloning_probe = Copier(wires['ramp_probe'], [wires['ramp'], wires['ramp_plot']])
    ramp_plotter = Plotter(wires['ramp_plot'])
    sin_cloning_probe = Copier(wires['offset_sin_probe'], [wires['offset_sin'], wires['sin_plot']])
    sin_plotter = Plotter(wires['sin_plot'])

    # Output = sin - ramp
    subtractor = Subtractor(wires['offset_sin'], wires['ramp'], wires['diff'])

    # Want to see when that is > 0
    comparison = GreaterThan(wires['diff'], wires['pwm_bool'], threshold=0.0, boolean_output=True)
    if_device = PassThrough(wires['pwm_bool'], wires['on_value'], wires['pwm_value'], else_data_input=wires['off_value'])
    output_value_on = Constant(wires['on_value'], 1.0, resolution=sim_res, simulation_time=sim_time)
    output_value_off = Constant(wires['off_value'], 0.0, resolution=sim_res, simulation_time=sim_time)
    pwm_plotter = Plotter(wires['pwm_value'])
    components = [ramp_src, 
                  ]"Starting simulation")
    [component.start() for component in components]
    logging.debug("Finished starting actors")'The program will stay "running" while the plot is open')   

    [component.join() for component in components]

    logging.debug("Finished running simulation")

if __name__ == '__main__':

And here is the output:

This relatively simple model has 15 threads, and takes no time at all to run. There is still a long way to go, but if you are interested check out the project homepage on google code:
Or clone the source directly with: hg clone scipy-sim

Popular posts from this blog

My setup for downloading & streaming movies and tv

I recently signed up for Netflix and am retiring my headless home media pc. This blog will have to serve as its obituary. The box spent about half of its life running FreeNAS, and half running Archlinux. I’ll briefly talk about my experience with FreeNAS, the migration, and then I’ll get to the robust setup I ended up with.

The machine itself cost around $1000 in 2014. Powered by an AMD A4-7300 3.8GHz cpu with 8GB of memory. A SilverStone DS380 case is both functional, quiet and looks great. The hard drives have been updated over the last two years until it had a full compliment of 6 WD Green 4TiB drives - all spinning bits of metal though.

Initially I had the BSD based FreeNAS operating system installed. I had a single hard drive in its own ZFS pool for TV and Movies, and a second ZFS pool comprised of 5 hard drives for documents and photos.

FreeNAS is straight forward to use and setup, provided you only want to do things supported out of the box or by plugins. Each plugin is install…

Driveby contribution to Python Cryptography

While at PyConAU 2016 I attended the Monday sprints and spent some time looking at a proposed feature I hoped would soon be part of cryptography. As most readers of this blog will know, cryptography is a very respected project within the Python ecosystem and it was an interesting experience to see how such a prominent open source project handles contributions and reviews.

The feature in question is the Diffie-Hellman Key Exchange algorithm used in many cryptography applications. Diffie-Helman Key Exchange is a way of generating a shared secret between two parties where the secret can't be determined by an eavesdropper observing the communication. DHE is extremely common - it is one of the primary methods used to provide "perfect forward secrecy" every time you initiate a TLS connection to an HTTPS website. Mathematically it is extremely elegant and the inventors were the recipients of the 2015 Turing award.

I wanted to write about this particular contribution because man…

Python, Virtualenv and Docker

Unsurprisingly I use some very popular Scientific Python packages like Numpy, Scipy and Scikit Learn. These packages don't get on that well with virtualenv and pip as they take a lot of external dependencies to build. These dependencies can be optional libraries like libblas and libatlas which if present will make Numpy run faster, or required dependencies like a fortran compiler.

Back in the good old days you wouldn't pin all your dependency versions down and you'd end up with a precarious mix of apt-get installed and pip installed packages. Working with other developers, especially on different operating system update schedules could be a pain. It was time to update your project when it breaks because of a dependency upgraded by the operating system.

Does virtualenv fully solve this? No, not when you have hard requirements on the binaries that must be installed at a system level.

Docker being at a lower level gives you much more control without adding too much extra comp…