Scientific Computing

Folder comparison with vscode

Visual Studio Code “vscode” can compare folders via vscode extension Compare Folders. In Unix-like shells, an alias can be used to ease command line use:

alias vsdiff="COMPARE_FOLDERS=DIFF code"

Then, to compare two folders, use:

vsdiff folder1 folder2

vsdiff allows applying changes from the left to the right. To “swap sides” click the left-right arrow in the top right corner.

Apple Studio Display brightness adjust from Windows / Linux

The Apple Studio Display is desigend for macOS brightness controls. A Linux or Windows computer has the brightness locked at the last setting. If the Studio Display power is reset, the brightness defaults to maximum.

Small open-source programs allow Linux and Windows computers to adjust the brightness of the Apple Studio Display.

Windows

studio-brightness.exe, is a C++ program downloaded from the Releases, or the program can be built from source. The keyboard uses the HID interface to adjust the brightness of the Apple Studio Display from Windows.

Linux

Choose from either of C++ HID interface asdcontrol or Rust asdbctl to adjust the brightness of the Apple Studio Display from Linux.

macOS

The complement of the above program is the MonitorControl macOS program that allows control of Apple and non-Apple displays from macOS.

Git submodule change remote

Git submodules can switch remotes. This is useful when making a pull request for a project that relies on another pull request submodule. Verify the change in the top project’s “.gitmodules” file.

Example: suppose the directory “subdir” is a Git submodule. In this command, do not put “./subdir” or “subdir/”, just “subdir” by itself. Suppose you also wish to change the branch to “feat1” in the submodule.

git submodule set-url -- subdir https://github.invalid/username/new

git submodule sync

git -C subdir pull

git -C subdir switch feat1

Xcode ld linker workaround

Whether using Clang / LLVM or Homebrew GNU GCC compiler, GNU ld is not supported on macOS. Only the Apple macOS Xcode ld is supported. The ld linker in Xcode 15 breaks numerous projects, including OpenMPI < 4.1.6. The workaround is to use the classic linker, which is still supported in Xcode 15.

Set in ~/.zshrc

export LDFLAGS="$LDFLAGS -Wl,-ld_classic"

or specify on the program command line like:

LDFLAGS="$LDFLAGS -Wl,-ld_classic" make

Note that for CMake, LDFLAGS environment variable is read only on the first CMake configure and cached.

Run Bash scripts from Windows

For certain use cases, it’s feasible to run a Bash script from within Windows using Windows Subsystem for Linux (WSL). Another way to run Bash scripts from within Windows itself without WSL is the Bash shell installed with Git on Windows.

Start the Bash script you want to run from Linux or Windows with the shebang (first line of Bash script file):

#!/bin/bash; C:/Program\ Files/Git/git-bash.exe

This tells the shell (Linux or Windows) to first try /bin/bash which is a Unix-like path, and then try the Git Bash shell on Windows. If Python is on Windows Path, one can use Bash scripts that invoke Python scripts.

CMake override default compiler flags

CMake outputs default compiler flags based on platform and project configuration, which can be overridden. This example shows how to override the default compiler flags by putting the user flags later in the command line

CMake FetchContent ignore build system

CMake FetchContent and ExternalProject bring remote projects into the top-level project to avoid making a monorepo or vendoring code into the top-level project. With FetchContent, the source code is retrieved at CMake configure time, allowing one to ignore the subproject build system and / or use only specific source files.

An example of this is using nRF5 SDK, which is a large project, but one may only wish to use a single source file and header as in this example:

Python collect images to HTML document

In many data analyses we may generate a large number of plots saved to disk. For convenience of sharing these plots, we have created a Python script that collects all images in a directory into a single HTML document that can be exported to PDF via the web browser “save as PDF” function.