Scientific Computing

GFortran duplicate use of submodule / module

Gfortran 9.3.0 is sensitive to overlapping / duplicated use elements in a module - submodule hierarchy. That is, if a procedure is used in multiple places in the module - submodule hierarchy, only use the procedure once at the highest necessary level of the hierarchy.

This is perhaps best shown by example:

module foo
implicit none (type, external)
contains
subroutine bar()
end subroutine bar
end module foo

module parent
use foo, only : bar
implicit none (type, external)
interface
module subroutine baz
end subroutine baz
end interface
end module parent

submodule (parent) child
use foo, only : bar  !< this is unnecessary and triggers the Gfortran 9.3.0 error
implicit none (type, external)
contains
module procedure baz
end procedure baz
end submodule child

The error message from Gfortran 9.3.0 is like:

$ gfortran -c .\dupe.f90

dupe.f90:17:17:

   17 | submodule (parent) child
      |                 1
   18 | use foo, only : bar
      |                   2

Pytest ignoring Meson subprojects

Meson projects may contain Python code, including Meson subprojects. However, the Meson subproject code may not be relevant to the top-level Meson project Python code. Then, Pytest Python test suites may fail when the subprojects/ directory tree is searched and unwanted tests are run.

Ignore directories with Pytest: while this example is for Meson subprojects, it is obviously applicable to many other Python projects.

Add to file “pyproject.toml” in project top-level directory:

[tool.pytest.ini_options]
norecursedirs = subprojects .* build dist CVS _darcs {arch} *.egg venv

Python paramiko SFTP example

Paramiko Python SSH library provides a convenient SFTPClient that allows easy transfer of files over SSH. A distinction from the command line utility sftp is that “put"ing a file must include the full destination path including filename, to avoid OSError.

This Paramiko example shows copying a file from local computer to remote computer over SSH in Python.

from paramiko import SSHClient

source = "foo.txt"
dest = "~/Documents/foo.txt"

with SSHClient() as ssh:

    ssh.load_system_host_keys()
    ssh.connect("server.invalid")
    with ssh.open_sftp() as sftp:
        sftp.put(source, dest)

Matlab dependency graph

Matlab can generate dependency graphs and export to GraphML files. The GUI dependency analyzer also shows missing variable and function definitions for the whole project in one view. The implicit static analysis of the dependency analyzer seems to be more thorough than the command requiredFilesAndProducts.

Even if you don’t typically use the Matlab “Project” feature, it’s useful for quality assurance to run this tool on Matlab code to find hidden problems that escape the imperfect nature of unit tests. Regardless of coding language, we use a layered approach for code quality assurance including static analysis, unit tests and integration tests.


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.