Pylons looking towards Tarrès, above Sant Bonifaci, shown mirrored: a high-voltage line runs downhill, its conductors sweeping from the top of the frame to a row of lattice pylons on a wooded slope, under a band of cloud.
case30, PowerModels.jl from PureScript

Grid
Explorer


A demo of Jurist, a Julia backend for PureScript. Write PureScript; use PowerModels.jl.

Julia is a fine language for fast numerical work. PureScript is a fine language for typed models you can check. Here they run together, in one Julia process on your own machine: an N-1 and protection analysis of a 30-bus power network, written in PureScript and compiled by Jurist, with every power flow solved by PowerModels.jl.

The Lab's N-1 view: the 30-bus network drawn as buses and lines, line 9 and line 39 in red, and beside it the load factor at 0.70 and the count 2 critical, 5 warning, 34 safe.
A recording of the Lab running against PowerModels.jl. Each line of case30, a 30-bus test network, is taken out in turn: 41 outages, each a power flow solved by PowerModels and judged in PureScript. As the load factor moves from 0.70 to 0.90 and back, the count of critical outages goes from 2 to 20.
1

Both directions

PureScript, with Julia’s numerics

A PureScript programmer gets Julia’s numerical libraries. PowerModels solves case30 in about 0.4 ms, and the analysis that asks for each solve stays in PureScript.

Julia, with PureScript where it helps

Someone working in Julia can write some parts in PureScript and keep Julia for the numerics. Here that is the typed model of the network, the relays as state machines held as checkable data with Glassbox, and the API’s contract, one PureScript module shared with the web page, so the server and the page cannot drift apart. It will not appeal to everyone.

The two meet in one Julia process. The Julia that starts the demo is written by hand; it loads the compiled PureScript as a package and runs its entry point:

columns/julia/main.jl
using GridExplorerJulia
GridExplorerJulia.Main_.main()

The timings are hand-written Julia too. bench/compare.jl builds the solver, the network and the relays by calling PureScript, through a small module of entry points written for it, runs the analysis, and compares every field of its answers with the same analysis written in plain Julia:

columns/julia/bench/compare.jl, excerpts
using GridExplorerJulia
const G = GridExplorerJulia
const E = G.Check_Entry
…
const SOLVER = E.solver(CASEDIR)()
const CASEDATA = E.caseData(SOLVER)()
const MACHINES = E.machines(joinpath(ROOT, "devices", "machines"))()

A PureScript function is a Julia closure, applied one argument at a time, and an effect is a closure of no arguments: the last () runs it. Jurist’s README lists how each PureScript value looks in Julia.

That is all the hand-written Julia here that calls PureScript: the entry point, the build’s warm-up and the timings. No Julia programmer has yet brought something they wanted to build this way, and that is what would tell us what Jurist needs next. If you have one, please say so in Jurist’s issues.

2

Two calls into PowerModels

The binding has two foreign calls: one reads the network, one solves a power flow. Everything around them is PureScript, including reading PowerModels’ answer the way pandapower reports one. PowerModels has no N-1 routine, so the sweep over outages is a loop in PureScript: the intact network, then one power flow per outage.

columns/julia/src/Grid/PowerModels.purs, excerpts
foreign import readCaseImpl :: EffectFn1 String Json

foreign import powerFlowImpl :: EffectFn3 String Json Number Json
…
    sweep s = do
      intact <- flows s.base
      outages <- for s.outages \outage -> do
        row <- flows (s.base { branchesOut = s.base.branchesOut <> [ outage ] })
        pure { outage, row }
      pure { intact, outages }

The Julia behind the two calls is 95 lines. Each power flow starts from a fresh copy of the network, so no solve sees another’s changes.

3

Held to a second implementation

The same analysis is also compiled to Python, by Pythia, where pandapower solves the power flows. Three checks, all part of make check, hold the two to the same answers:

  • The analysis, run here from pandapower’s recorded answers, gives the other’s answers byte for byte: 6,180,948 characters.
  • The relays’ conformance vectors, generated on Julia, match the committed ones byte for byte.
  • The two, asked the same 313 requests, agree on every status, verdict, ranking, timeline and refusal. The solvers’ numbers agree to within 1e-5, or 1e-6 apart; the largest relative difference measured is 5.1e-7.

The difference that is left, up to 3.3e-6 of a value, is pandapower stopping at its default tolerance, 1e-8 MVA of mismatch. Run to 1e-13 MVA instead, pandapower agrees with PowerModels to 8.2e-12 at worst, in the three cases tried: the two solve the same network to about 1e-11.

4

What it costs, in numbers

PowerModels is so quick that the compiled PureScript is most of each request. Against the same N-1 and metrics written in plain Julia, it takes 3.8x and 17x as long. Still, N-1 over HTTP takes 0.12 s here, against 0.40 s for the Python demo; the overview sets the rest side by side. The demo starts answering 2.52 s after it is started, once make has precompiled it.

RequestCompiled PureScriptPlain JuliaRatioPureScript share
N-1, 42 solves118.9 ms31.4 ms3.79x74.2 %
Metrics, 1 solve15.3 ms0.9 ms17.3x94.7 %

In the same process, median of three, case30 at load factor 0.7, on a Mac Mini (M4). The plain-Julia version is columns/julia/bench/native.jl. Part of the PureScript’s share is work a fuller library would do itself: the N-1 loop, and reading PowerModels’ answer in the terms the analysis uses, which are pandapower’s.

5

Run the demo

The demo is the Julia program and its page, the Lab: move the load factor, take out any line, change every relay setting, and read N-1 over all 41 lines, every answer solved by PowerModels on your own machine. The code is public, at github.com/afcondon/grid-explorer.

What you need

  • spago, purs and node
  • Julia 1.12 or later; give its path with make JULIA=/path/to/julia if it is not on your PATH
  • a Jurist checkout with purejl built (stack build there), at or after Jurist #15; give its path with make JURIST=/path/to/Jurist
git clone https://github.com/afcondon/grid-explorer
cd grid-explorer
make web julia   # the pages; the API (PureScript -> Julia), precompiled
make run-julia   # API on 127.0.0.1:3035, pages on http://localhost:3036

Then open http://localhost:3036/lab/. make check compares the two demos, so it needs the Python one built too: see its instructions.

Once both demos are built, make bundles packs a ready-to-run archive of each in dist/.

6

What is not checked

  • Only case30, which has no transformers. The translation refuses a network with one, and no other network has been written out for PowerModels.
  • The server answers one request at a time.
  • pandapower and PowerModels could disagree on whether a network near collapse can be solved at all. One run in the checks fails to solve on both; nothing looks for others.
  • Built and checked with Julia 1.13.3 only.
  • The two agreeing says the source means the same in both. It does not say the model is right.
7

Try it in your browser

There is also a version you can try without installing anything: a replay, on a page called The Relays. Its recorded power flows are pandapower’s, from the Python demo, not PowerModels’. The analysis you interact with is the same PureScript, compiled to JavaScript.

8

Credits

PowerModels.jl
The power flows. lanl-ansi/PowerModels.jl
case30
MATPOWER’s 30-bus case, a modified IEEE 30-bus system, as pandapower ships it. matpower.org
Jurist
purejl, PureScript to Julia. afcondon/Jurist
HTTP.jl
The API’s HTTP server. JuliaWeb/HTTP.jl
Glassbox
State machines as typed, checkable data, with their conformance generator. afcondon/purescript-glassbox
Pages
Halogen, drawn with Hylograph’s HATS.
Photograph
“Pylons looking towards Tarrès, above Sant Bonifaci”, Andrew Condon. Shown mirrored here.
Type
IBM Plex Sans and IBM Plex Mono, SIL Open Font License 1.1.

Grid Explorer, a Hylograph exhibit. Overview. The Python door. Source on GitHub. Jurist on GitHub.