Scientific Computing

Silence Pytest DeprecationWarning

Pytest by default: shows DeprecationWarning and PendingDeprecationWarning.

Warning clutter can be filtered by adding to “pyproject.toml” in the top-level project directory:

[tool.pytest.ini_options]
filterwarnings =
  ignore::DeprecationWarning

We might choose to suppress DeprecationWarning in a repo’s test suite because the warnings are coming from modules developed by others (e.g. Numpy or Xarray).

However, don’t be too carte blanche about suppressing DeprecationWarning.


Related: find source of PyTest warning

Python breakpoint() debugging

Python breakpoint() abstracts away pdb.set_trace() and allows use of other debuggers. breakpoint() also works well with Pytest to conditionally break into code.

x=1
y=0

breakpoint()

z = x/y

breakpoint() is immediately clear as to its purpose.

Reference: PEP0553 breakpoint()

nmap in Cygwin - seamlessly

WSL2 works with Nmap directly and is generally recommended instead of Cygwin. For those who do need Cygwin, it’s possible to use Nmap from Cygwin, silently calling Windows Nmap.

Install by download nmap. Install nmap “self-installer” .exe. When asked, also install Npcap. Cygwin: add to ~/.bash_profile the following. Note the \ and \( are vital for Cygwin shell to interpret the command correctly.

alias nmap="/cygdrive/c/Program\ Files\ \(x86\)/Nmap/nmap.exe"

Open a new Cygwin window to start using nmap

Test nmap in Cygwin:

nmap 8.8.8.8

results in

Starting Nmap ( https://nmap.org )
Nmap scan report for 8.8.8.8
Host is up (0.0092s latency).
Not shown: 998 filtered ports
PORT STATE SERVICE
53/tcp open domain
443/tcp open https

Nmap done: 1 IP address (1 host up) scanned in 7.41 seconds

Troubleshooting:

  • errors about interface → try running Cygwin as Administrator (right click on Cygwin icon).
  • find interface names available to nmap
nmap --iflist

Notes:

  • to find servers with a particular port open on a subnet, try my Python findssh program that scans for servers without nmap.
  • If you don’t install Npcap when asked in the nmap installer, nmap does not work. Nmap claims no host existed at a known working IP address.

Paraview Python plot frozen with Render

Opening Paraview Python “pvpython” terminal is accomplished by:

  • Linux: pvpython should be on $PATH.
  • macOS: Look under /Applications/ParaView*.app/Contents/bin/pvpython
  • Windows: Look under Start Menu > ParaView > pvpython

Paraview’s Python interface scripts that use Render() create a vestigial feature that is frozen. To make the plots interactive, use Interact() instead.

Python Paraview example:

from paraview.simple import *

s = Sphere()
Show()
Interact()

Using CMAKE_ROLE to detect CMake run mode

The operating mode of CMake can be detected via CMAKE_ROLE. This variable must be read with:

get_property(cmake_role GLOBAL PROPERTY CMAKE_ROLE)

Here, the variables cmake_role is created and can then be used in if() statements as needed.

CMake Ninja generator preprocesses Fortran

CMake with the Ninja and Ninja Multi-Config generators virtually always work well together, including with Fortran projects. To generate the dependency graph, CMake directs the Fortran compiler to preprocess each Fortran code file when using the Ninja generator. In rare cases, some legacy Fortran large code files can generate unexpected and spurious syntax errors when using CMake + Ninja.

The workaround seems to be to use the GNU Make backend, we have found that works for the rare case this became an issue.

Android switch to 3-button navigation

Android task switching swipe navigation can be switched to 3-button navigation. Some workflows are much easier via the traditional 3-button navigation used by Android since Android 3.x / 4.x. Also when wearing gloves, using 3-button navigation can work better.

Switch an Android device to traditional 3-button navigation (back, home, switch or triangle, circle square) by going to Settings → System → Gestures → System navigation and select “3-button navigation”.

CMake Fortran design patterns

CMake is excellent for building very complex Fortran projects across operating systems and computing platforms from embedded systems to HPC. Here are a few common Fortran CMake patterns.

specify linker options for all targets using add_link_options()

add_link_options(-myflag)

or for a particular target with target_link_options

For hand-written Makefile the order of libraries matters completely. CMake attempts to determine the graph for library files, starting from the user specified order. CMake will try a finite multiplicity to resolve the graph, but in tough cases of library interdependency library ordering may need to be influenced by add_dependencies() or even object libraries.

“Undefined reference to” occurs at linking of main executable: be sure the library is actually linked to the user library with

cmake --build build -v

Fortran module include: be sure the *.mod file directory is included, particularly to the main executable by setting the target properties like:

include(GNUInstallDirs)

target_include_directories(mymod INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})

set_property(TARGET mymod PROPERTY Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})

this can be done for numerous targets in a loop instead of repeatedly specifying by a foreach() loop:

foreach(t foo bar biz baz)
  target_include_directories(${t} INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})

  set_property(TARGET ${t} PROPERTY Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})
endforeach()

To ensure the build system is completely updated, do

cmake --build build --clean-first

Specify C and CXX for CMake projects with MSVC

A quirk with Visual Studio and CMake for C++ projects is that the C language must also be specified in CMakeLists.txt like:

project(foo LANGUAGES C CXX)

Otherwise, weird linker errors can occur, even on a pure C++ project with no C files.

CMake FindHDF5 improvements

An improved FindHDF5.cmake in h5fortran handles modern HDF5 versions including on Windows.

The important improvements are:

  1. check that HDF5 compiler wrapper is able to compile and link a simple HDF5 program
  2. have more default directories to search for the HDF5 libraries
  3. work with HDF5 across versions
  4. work with HDF5-MPI

Before this, CMake would get confused by the Anaconda Python HDF5 compiler wrapper. This also makes Intel compiler on Windows work with HDF5.