Scientific Computing

Modern Fortran submodule vs. preprocessor

Fortran has a long legacy of preprocessing systems, reaching back at least to the 1970s when C was not yet widely known. I would argue that modern Fortran ≥ 2008 has eliminated most preprocessors use cases. It’s usually much cleaner and quicker (build/runtime) to use Fortran submodule to switch functionality such as MKL vs. Atlas vs. Netlib Lapack using CMake, for example.

Numerous Python preprocessors have arisen (and sunk) along with the usual C/C++-based preprocessors. The only thing we use preprocessors for is very simple single-line statements, where adding a submodule would be significantly more cumbersome than a one-liner preprocessor statement.

Historically, very elaborate Fortran preprocessors were developed such that they nearly became a language unto themselves.

FLECS and Ratfor (also ratfiv, ratfor90, etc.)

These extensive preprocessors have been made obsolete by advances from Fortran 77 onward.

EZNEC antenna design tips

A few quick start tips for using EZNEC antenna design software for yagi antennas. This is assuming a yagi antenna for 440 MHz band.

  • Ground: start with “free space”. If the antenna model is too close to the ground, the simulation can be unrealistic. Once satisfied in free space, consider real ground (non-free space)
  • Number of segments: suggest 5 per wire, with free EZNEC license
  • Number of yagi elements: 3..5 elements are possible with free EZNEC at 440 MHz.
  • Source: 1 source, middle (50%) of “driven” wire

Example antennas

  • commercial yagi datasheets
  • coat-hanger yagi page 19 starting point 4 element yagi

Typical yagi performance

How good is good enough?

  • Gain: > 6 dBi
  • Backlobe (front/back ratio) (opposite direction of desired mainlobe): < -15 dB or so
  • Sidelobes (off side of desired direction): < -15 - -20 dB or so
  • SWR (439-441 MHz): Ideally less than 2.0. 3.0 is perhaps tolerable.

SWR is a measure of how efficient an antenna is at coupling energy to/from the air to a radio device.

Γ = (SWR - 1) / (SWR + 1)

  • Mismatch loss [dB] (energy loss due to SWR): -10 * (1 - Γ)^2

Example

Suppose: SWR = 3.0

  • Mismatch loss: -10 (1 - ((3-1) / (3+1))^2) = 1.25 dB

Power loss (gain) factors:

  • 3dB ~ factor of 2
  • 10dB ~ factor of 10
  • 20dB ~ factor of 100

CMake force linking static libs

CMake by default searches for shared libs before static libs. Sometimes a project needs to specifically link external or internal static libs, even if shared libs are present. A simple cross-platform way to do this is to set CMAKE_FIND_LIBRARY_SUFFIXES before any find_library() or find_package():

set(CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)

This takes advantage of de facto static library extensions for macOS, Windows and Linux, using CMake system variables.

The default values for CMAKE_FIND_LIBRARY_SUFFIXES are set by (depending on platform and compiler) CMake

Modules/CMakeGenericSystem.cmake
Modules/Compiler/
Modules/Platform/

read large RINEX files in Python with GeoRINEX

Reading RINEX files in Python or other languages historically required compiling or buying complex software. The GeoRINEX Python 3 program and library to be used with RINEX OBS and NAV files, including Hatanaka compressed files or other compression wrappers like ZIP.

GeoRINEX can even read only every N seconds from a RINEX file with option georinex.load(..., interval=N). This greatly speeds up reading where coarser time intervals than the RINEX file provides is needed. GeoRINEX uses performance-oriented techniques to read RINEX files at speeds approaching compiled languages, using Pure Python + Numpy and Xarray for metadata rich results.

If you use GNSS and RINEX in your work, you may like GeoRINEX for Python 3.

Disable Windows Fast Boot

For those dual-booting Windows and Linux, it’s useful to disable Windows Fast startup so that you can access the Windows partition from Linux. Disabling Fast Boot may add a few seconds to boot time. When you shutdown your PC, Fast Boot actually reboots and then hibernates. This locks the Windows partition so that Linux can’t access it.

Disable Fast Boot: +r powercfg.cpl → “Choose what the power button does”. We typically disable hibernate as well to save on SSD wear.

Disable fast boot

BitLocker and dual-booting: If BitLocker is enabled, Linux cannot access the Windows partition.

Fast Boot SSD wear: Fast Boot just like hibernate increases solid state drive (SSD) wear, since you are dumping an image ~ RAM size to disk on each Fast Boot shutdown.

Python pathlib cast to str for subprocess

Python pathlib is part of Python standard library. When using Python subprocess, in general, do str(mypath) where type(mypath) is pathlib.Path.

Example:

#!/usr/bin/env python
from pathlib import Path
import subprocess

P = Path('~').expanduser()

O = subprocess.check_output(['dir', str(P)])

print(O)

if you don’t cast pathlib.Path to str:

TypeError: argument of type ‘WindowsPath’ is not iterable.

Notes

You should rarely if ever need shell=True. On Windows in particular, give the full path to a non-system executable.


Related: Idiomatic pathlib Python

Recursive sed find and replace text

Find and replace text in files recursively on Linux, macOS and Windows Subsystem for Linux with recursive_sed.sh:

#!/usr/bin/env bash
#
# The quoted variables allow using backticks and other "nasty" characters in sed.
#
# usage:
# ./recursive_sed.sh  pathtofiles yuck yay
#
# References:
# https://stackoverflow.com/a/1585810
# https://unix.stackexchange.com/a/128758

set -o errexit
set -o nounset

path=$1
old=$2
new=$3

echo "${old} => ${new}"

find $path -not -path '*/.git*' -type f -execdir sed -i 's/'"${old}"'/'"${new}"'/g' {} +

Pdoc Python quickstart

Pdoc is vastly simpler to use than Sphinx for small projects, with good HTML formatting.

In distinction from Sphinx, pdoc does not parse RST docstrings, but Pdoc does allow the following docstring formats:

At this time, pdoc3 doesn’t consider Python type hinting.

Install

pdoc is a light minimal install.

pip install pdoc3

Usage

We use the pymap3d project with auto-generated docs as an example.

From the top-level pymap3d/ directory, type

pdoc --html pymap3d

This creates several *.html files under html/pymap3d, including index.html.

Check the output by:

firefox html/pymap3d/index.html

If the output seems satisfactory, move the files to the gh-pages branch, or at least to a docs/ folder:

mv html/pymap3d/*.html docs/

Publish the docs to the web by:

  1. In the Github repo Settings → Github Pages → Source: “gh-pages”
  2. git commit and push. It takes a few seconds for Github Pages to deploy HTML.

Set Python version with update-alternatives

Python users typically use a non-system Python distribution such as Miniconda. Python distributions typically allow easy switching of Python version. Where Linux system Python must be used, the default Python version can be switched persistently with update-alternatives.

Update-alternatives without sudo: a one-time setup.

Configure shell to use ~/.local/bin instead of system-wide /usr as follows:

mkdir ~/.local/bin

Add to ~/.profile:

export PATH="$HOME/.local/bin:$PATH"

Enable switching Python default between Python versions with these one-time commands:

update-alternatives --install $HOME/.local/bin/python python /usr/bin/python3 20

After the setup above, at any time select the default system Python version without sudo:

update-alternatives --config python