Issue 171 – November 4, 2023

Running OpenFOAM with VS Code Tasks

Hey there 👋

It’s Robin from CFD Engine & you know me, I love an automation 😍

This week I’m borrowing VS Code’s build system to run OpenFOAM scripts.

Nothing too drastic, just a single config that launches Allrun/Allclean scripts straight from the command palette.

It’s probably not for launching real cases, but it’s great when you’re developing a mesh recipe or debugging dictionaries & neater than hopping back-&-forth between the editor & the terminal all the time.

If you want to launch an Allrun script with ctrl + shift + b without leaving your editor, then this is for you.

Context

I know from the survey results that lots of you use VS Code, so I’ve been using it a bit more too.

Partly to see what I’m missing & partly to use the super-handy Live Share extension with coaching clients (more on that one in another email).

Modern code editors have so many “quality-of-life” features for developers, it would be nice to borrow some for CFD purposes 🤔

Tasks is one of those borrowable features.

Tasks

If you use VS Code, you probably use its built-in terminal to run scripts & ad-hoc commands.

Why not go an extra step & get VS Code to run your scripts for you?

That’s where Tasks comes in. It’s normally used for building & testing source code, but it’s essentially just a script runner, so let’s give it some CFD scripts to run.

Open an OpenFOAM case directory in VS Code & play along…

A task of your own

Tap ctrl + shift + p to bring up the command palette, type Open User Tasks & press enter.

Choose the Others option & VS Code will create a fresh tasks.json file in your user space & pop it open in the editor.

You should see a short JSON file with an example echo task, like this 👇

An example tasks.json config file

To run it, select Run Task (it’s under Terminal in the menu bar), choose echo from the dropdown list & tell it to continue without scanning the output.

The built-in terminal will open & say “Hello” (just as you suspected) 🤓

Tip: You can also launch tasks via the command palette. Tap ctrl + shift + p to bring up the palette, type run task, select your task & press enter. Frequently used commands & tasks will float to the top as you use them.

This is the basic scaffolding for our own tasks, we just need to edit this file.

A super-simple example would be to mod the echo task to look something like this…

A simple task config to run an Allrun script

We’ve changed it’s name, changed the command to ./Allrun & added a blank problemMatcher to stop it asking to scan the output every time we run it 👍

And that’s just the beginning, there are a TON of other features to play with – check out the docs for more info.

You can have as many tasks as you like, you can group them up, you can have different commands for Windows, Linux & Mac, you can even have dependencies to chain tasks together.

Here’s my own tasks.json file which includes Allrun & Allclean tasks, plus an Allclean/Allrun combo that uses dependencies. Feel free to grab it (or crib from it) if it’s helpful.

I’ve designated my “clean & run” task as my default build task & so it launches by tapping ctrl + shift + b – you can add more shortcut keys if you like.

Automate all the things

I’m just scratching the surface of what VS Code can do, but tasks, plus the OpenFOAM language extension, plus Live Share, plus the WSL, SSH & Docker integrations make it a pretty productive place to go OpenFOAM-ing.

Let me know if you give this a try (& if you find it useful) 🤞

If you’re a VS Code pro, what other bit & bobs should I check out?

Until next week, stay safe,

Signed Robin K