Scientific Computing

Git stash cleanup

Git stash is often used to hold temporary work that wasn’t yet ready for a commit, perhaps during a rebase. The stash history is per repo. Over time, one may accumulate numerous stash entries that are no longer relevant and may desire to cleanup this clutter.

View Git stash entries by:

git stash list

View the contents of a particular stash entry by:

git stash show -p "stash@{0}"

where the number in braces corresponds to the git stash list index.

To remove a stash entry, sliding up all the entries older than it:

git stash drop "stash@{N}"

where “N” is the entry index to remove.


If one wishes to recover a dropped Git stash entry, it may be possible:

TeXLive from the command line

TeXLive is a well maintained and often updated LaTeX distribution that works for any OS. Download TeXLive net Installer and run. Click Advanced and use the “basic” scheme, which is well under one GB to start.

When building a document and it seems to be missing a package, note the error messages. Find and install needed packages via tlmgr using commands like:

  • find package by filename: tlmgr search --global --file fullpage.sty
  • find fonts/packages by name: tlmgr info ieee
  • install package: tlmgr install lmodern

Use a LaTeX IDE (GUI editor) like TeXstudio.

When a new major version of TeXLive is released, simply repeat the procedure above and point any programs like TeXstudio to the new TeXLive install directory.

Extracting zstd archive with tar

Zstd is a modern performant file archiving standard widely used to replace .zip, gzip, etc. With modern “tar” extract .zst like:

tar -xf arc.zst

If needed for older tar, specify the program “tar” should use to extract .zst:

tar --use-compress-program=zstd -xf arc.zst

Matlab can extract .zst files.


For very old “tar” one may get:

zstd: error 25 : Write error : Broken pipe (cannot write compressed block) tar: Error opening archive: Child process exited with status 25

In this case, use a two-step process to extract the .zst file fully:

zstd -d myfile.zst   # creates tar file "myfile"
tar -xf myfile       # extract the original file/directory hierarchy

Matlab curl instead of websave

Matlab websave might not work in cases where a plain “curl” or “wget” command works. A symptom of this issue is HTML is downloaded instead of the intended binary file. Websites such as Dropbox recognize the HTTP User Agent of curl and Wget and mutate the web server response to be automation-friendly. Since Matlab is much less commonly used than Python, curl, Wget, etc. this user agent-dependent behavior results. We recommend understanding why Matlab websave doesn’t work, or use the low-level Matlab HTTP Interface.


To use curl from Matlab, recognize this may require unique setup for each system, despite that curl is included (preinstalled) in modern operating systems including Windows.

The extra quotes around “url” allow for arbitrary characters to be used in the URL that can confuse shells like zsh. The “-L” option to curl allows redirects.

function curlsave(filename, url)

cmd = "curl -L -o " + filename + " '" + url + "'";

assert(system(cmd) == 0, "download failed: " + url)

end

Linux systems with multiple curl versions installed may need to set an environment variable to prioritize. Set the filename as appropriate for computer (ensure the file exists).

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libcurl.so.4 matlab

Even this doesn’t always work, so we recommend understanding why Matlab websave doesn’t work, or use the low-level Matlab HTTP Interface.

CMake generate Fortran from template

CMake configure_file can serve to generate source code files including Fortran. For templates beyond a few lines of code, it may be useful to read the template from a file. This requires setting CMake directory property CMAKE_CONFIGURE_DEPENDS to have CMake regenerate the file if the template file changes as in the example below.

file(READ my_template.in.f90 template_code)
configure_file(example.in.f90 example.f90 @ONLY)

set_property(DIRECTORY PROPERTY CMAKE_CONFIGURE_DEPENDS my_template.in.f90)

#example use
add_executable(main ${CMAKE_CURRENT_BINARY_DIR}/example.f90)

iftop config file

iftop is a handy utility on macOS, Linux and other Unices for a live Terminal graph of network data flow to particular addresses. On computers with many network interfaces, including virtual interfaces such as on macOS, it is handy to set a default interface in a config file. iftop uses the file ~/.iftoprc.

For example, on macOS you may be interested in interface “en1”. To help determine the desired interface, use ifconfig or ip a to find the interface with the public IP address. Then create ~/.iftoprc containing like:

interface: en1

where “en1” is your desired interface determined as per above.

Red Hat firewalld port add

RHEL uses firewalld to provide network firewall. firewalld has the concept of runtime vs. permanent rules, which help avoid getting the firewall into an unusable state. Permanent rules become live at next restart/reboot, while runtime rules disappear at restart/reboot.

Suppose one wishes to put the SSH server on a non-default port 12345 to mitigate auth log clutter. First configure the SSH server in /etc/ssh/sshd_config, then restart SSH and verify the SSH configuration is working by adding the port to firewalld (here, 12345):

firewall-cmd --add-port=12345/tcp

If this works, make the firewalld rule permanent:

firewall-cmd --permanent --add-port=12345/tcp

SELinux will also need an updated policy to allow the SSH port change, like:

semanage port -a -t ssh_port_t -p tcp 12345

CMake expanduser tilde ~

To have the most reliable path operations in CMake, it’s typically best to resolve paths to the full expanded path. Note: there are a few CMake functions that desire relative paths, but those are clearly spelled out in the docs.

expanduser.cmake for CMake expands the ~ tilde character to the user home directory on all operating systems, including Windows.


Related: