A simple modelling language

Recently I had reason to get interested in process modelling.  Ultimately I ended up writing an Antlr4 grammar for Modelica (here), but in the mean time I came up with SML (Simple Modeling Language).  The Antlr4 grammar is sml.g4.

The characteristics I wanted in my own modeling languages were:

  • Ability to define models as text files
  • Models should be as Object Oriented as possible
  • Ability to compose models.  That is; ability to have models that include models.
  • Ability to define variables that are internal to models and variables that are exposed by models (i.e. “ports”)
  • Ability to put models in namespaces
  • Ability to define equations in models that related the variables.  The equations should be expressed in standard form.
  • Support for differential equations is essential

SML accomplishes these goals.  An example SML model is a standard spring from 1st year Engineering, here.  The model file is:

model tge.spring;
#
# vars
# 
variables:
    # spring constant
    public k;
    # force difference
    public df;
    # distance difference
    public x;
#
# Equations
#
equations:
	df:= k*x;

This model is in the namespace “tge.string”.  It exposes three public variables “k”, “df” and “x”.  The relationship b/t the variables is “df=k*x”.  There is a simple example, of a standard pendulum here.

More complex examples are here.  One such example is a classic RC circuit.  This model defines the structure of the circuit itself, and references the resistor, capacitor and source via includes of those models from their own SML model files

model tge.rc1;
#
# A simple RC model 
#
#
#      ------- R1 ---- C2 --
#      |                   |
#      V                   |
#      |-------------------|
#
#
imports:
	tge.resistor;
	tge.capacitor;
	tge.vsource;
variables:
	# declare a resistor instance called "R1"
	component tge.resistor R1;
	# declare a capacitor instance called "C1"
	component tge.capacitor C1;
	# declare a vsource instance called "V"
	component tge.vsource V;
equations:
	# set dV of tge.vsource to 5V
	voltage:=D.dV-5;
	# set R of tge.resistor to 10 ohms
	resistance:=R1.R-10;
	# set C of tge.capacitor to 100 F
	resistance:=C1.C-100;
	# connect the +ve end of V to R1
	positiveConnection:= V.v1-R1.v1;
        # connect the resistor to the capacitor
        resistors:=R1.v2-C1.v1;
	# connect the -ve end of V
	groundConnection:= V.v2-R2.v2;

Ultimately, with Antlr4 it should be possible to generate model parsers in Java, C# and potentially C++, that can consume SML files, ensure that the model composition is reasonable, and generate input files for mathematical solvers.  The work of producing solver input files from SML models is essentially the work of collapsing an object tree to a flat model.

 

Similar Posts

  • AGC Grammar

    Every IT geek is, to some degree, fascinated with the Apollo program which put a human on the moon for the first time.  Naturally, there is also curiosity about the computers on the Apollo moon lander, and the software that ran on them.  The source code that went to the moon is available now, and…

  • Home Automation DevOps

    Home automation is coming into the mainstream a with recent offerings from the big tech companies, and I’m interested in it too. I’ve had a number of home automation controllers, most recently an H3 Pro SEL from HomeSeer. I’ve also become quite interested recently in DevOps monitoring, using InfluxDB, and Grafana.  Sensu, Telegraf and Prometheus…

  • Building QEMU

    In general, I install QEMU on my Macbook using MacPorts.  However I recently had a need to get the tip of the QEMU development tree. Getting the QEMU source tree is trivial: git clone git://git.qemu-project.org/qemu.git I needed an updated version of dtc: git submodule update –init dtc The build instructions from the README are: mkdir…

  • pdp-7 Unix

    Unix version 0 was written in 1963 by Ken Thompson, on a PDP-7.  Recently, the source code code Unix V0 has been discovered, and you can read it here, as pdf scans of printouts.  You can read about the discovery, and the effort to boot Unix V0 on a real PDP-7 here.  The project home…

  • 6502 Assembler

    Back in the dark ages (the 1980’s), people like myself coded on Apple][ computers.  If you were good you coded in Applesoft BASIC or Integer BASIC. If you were really geeky you coded in Assembly language on the 6502 processor.  The Apple][ OS was coded in assembly language, so if you really wanted to understand what…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.