Scientific Computing

Use LaTeX lowercase package names

When moving files between:

  • case-insensitive filesystems: HFS+, ExFAT, FAT
  • case-sensitive filesystems: Ext4, NTFS, APFS

LaTeX documents may fail to build if filename case differs from LaTeX code. It’s best to always use lowercase for filenames when sharing with case-insensitive systems.

This example is for the UTF8 package. This stanza works for case-insensitive:

\usepackage[UTF8]{inputenc}  % don't use capitals!

but on case-sensitive systems:

UTF8.def not found from inputenc.sty

Fix by always using lowercase LaTeX package names.

\usepackage[utf8]{inputenc}

Raspberry Pi SPI

These are general notes, there are often caveats with GPIO/SPI applications on any embedded device.

Raspberry Pi SPI pinout shows two SPI ports spi0 spi1 for most non-Compute Module Raspberry Pi models. Raspberry Pi Compute Modules have three SPI ports, with the addition of spi2 that’s only available on the Compute Modules.

Software

Python is a popular way to access the Raspberry Pi SPI. spidev is a popular Python module to use SPI. For example, the Raspberry Pi SPI maximum clock speed is set to 1 MHz by

import spidev
spi = spidev.SpiDev()
spi.open(bus, device)
spi.max_speed_hz=1000000

As noted in later sections, having the correct maximum clock speed and correct polarity is essential for SPI communications to work.

Synchronous

You might be familiar with asynchronous protocols such as used in RS232 connections, where the baud rate must be agreed upon in advance by communicating peripherals. Only the SPI controller initiates communications, and the controller must know how many bytes to receive for a given command so as not to cut off the communications too early. Be sure polarity and phase are compatible between devices.

SPI consists of four wires: clock, select, MISO and MOSI.

Clock

The clock line is unidirectional from the controller to peripheral. The peripheral cannot initiate communications. Faster clocks require shorter wires, as remember a 10 MHz square wave needs several harmonics to keep usable shape–perhaps up to 100 MHz. How good of a transmission line is your knotted hookup wire at 100 MHz? not so good perhaps.

Consider clock speed if a peripheral is not responding, or responding erratically, especially considering that the Raspberry Pi 2/3 SPI clock varies with the VPU speed.

Chip Select

The Raspberry Pi can select more than two peripherals by using a binary decoder chip, assuming you have a recent kernel and the appropriate software and configuration.

MISO

MISO is unidirectional from each peripheral to controller. The MISO line is shared between all peripherals. Only one peripheral, selected by Chip Select line may use MISO at once.

MOSI

MOSI is unidirectional from controller to all peripherals. The MOSI line is shared between all peripherals. Only the peripheral selected by Chip Select should respond to MOSI on MISO.

Full duplex

SPI is a full-duplex protocol, that is, the controller can send while the peripheral transmits (for capable hardware).

Debugging

Debugging SPI is most convenient with a four-channel oscilloscope, particularly if the scope has SPI decode.

Notes

Note: In some peripherals, MISO is daisy-chained with MOSI, but your datasheet would tell you for those cases.

See /boot/overlays/README for more details on configuring the GPIO for SPI.

NASA HPFCC FUN3D Fortran competition canceled

On June 16, 2017 NASA announced the cancellation of its FUN3D Fortran supercomputer code improvement competition originally announced May 2, 2017.

The extremely high number of applicants, more than 1,800, coupled with the difficulty in satisfying the extensive vetting requirements to control the public distribution of the software made it unlikely they would achieve the challenge’s original objectives in a timely manner. NASA looked at several alternatives to keep the challenge design intact – things like significantly extending the challenge performance period, and offering a much smaller portion of the code. Neither were considered viable options.

For a one-of-a-kind competition, 1800 participants is a large number, considering the need to verify US citizenship and that the work was done on a personal PC. As a comparison, the notable ACM ICPC competition has several thousand participants each year in teams of three.

I was surprised to see 1800 entries. It may be that a substantial proportion of HPFCC entrants were new to Fortran but experienced with HPC.

It would be interesting to get demographic info on the HPFCC entrants as a snapshot of the future of the Fortran language.

Consolation prize: HeroX Fortran Storytelling competition (ended July 2017) with $1750 in prizes

Further competition: Making industrial cloud competitive with supercomputers

Loading WSPR raw audio data

We encourage amateur radio operators and citizen scientists to save raw WSPR data in the form of two-minute long .wav files. Load each WAV file into Python like:

from scipy.io import wavfile

fn = '170620_1652.wav'  # naming convention is YYMMDD_HHMM.wav

fs, data = wavfile.read(fn)

This will result in fs=12000 Hz and data a 1440000 element 1-D vector of numpy.ndarray.

The WSPR baseband signals are user-selected in the 1400 .. 1600 Hz range. Other signals within the audio passband are useful as reference, for example JT65, FT4, and FT8 for additional paths to analyze.

Fix inability to type in Spyder IDE over VNC

With Ubuntu at the remote and local ends, if unable to type anything into the Spyder IDE when connecting remotely over VNC, try TurboVNC on the laptop. Download the latest TurboVNC .deb file and Install the .deb file

gdebi turbovnc*.deb

Now, the command vncviewer maps to the more advanced TurboVNC instead of TightVNC.


Before using TurboVNC, errors included

Qt: XKEYBOARD extension not present on the X server.

I tried putting in the remote system’s ~/.profile the line

export XKB_DEFAULT_RULES=base

based on Spyder Github Issue but with no luck.

Fortran sound playback - platform-independent

Some Fortran code used proprietary modules from Visual Fortran and the like for among other things, playing back sound from Fortran on Windows. I believe a more reliable and platform-independent way to playback sound from Fortran is to use an external application like ffplay, included with FFmpeg.

FFplay will play prerecorded audio files. Playback particular sounds based on filename.

This technique is not good for background music in a game, but is good for short sound effects to let you know your processing is done or for game sound effects in Fortran.

NOTE: use all lowercase filename, and don’t use spaces in names for best cross-platform compatibility and reliability.

Fortran program with sound example: use / as a file/directory separator on Fortran. // concatenates strings in Fortran.

program mysound

use iso_fortran_env, only : input_unit

implicit none

! configure ffplay -- could make if/else to allow other players
character(*), parameter :: playexe='ffplay'
character(*), parameter :: cmdopts='-autoexit -loglevel quiet -nodisp'

character(1000) :: fn, cmd

print *,'input sound filename to play'
read(input_unit,'(A)') fn  ! must be (A) and not *

cmd = playexe//' '//cmdopts//' '//trim(fn)
print *,trim(cmd) ! for debugging

! exitstat only works for wait=.true. by Fortran standard.
call execute_command_line(cmd, wait=.false.)

end program

More Fortran sound playback examples: robust Fortran sound playback example play_sound.f90 or implemented as a Fortran game with sound sound.f90.

Fortran terminal ISO standard

iso_fortran_env is respected by all common Fortran compilers, as implied by its name. iso_fortran_env is part of the Fortran 2003 specification and popular Fortran compilers added it a decade ago (Gfortran since at least GCC 4.3).

Why use iso_fortran_env terminal I/O: legacy programs written before Fortran 2003 often write to terminal with:

write(*,*) 'The value of X is ',x

or

write(6,*) 'The value of X is ',x

This is a problem when trying to debug with text output to terminal, especially where someone has used “6” for file I/O unit by mistake. Or, if trying to write to a file with an uninitialized unit number, stderr gets redirected to file fort.0.

Example

Print repeatedly to the same line, and combine prompt text on the same line with input.

iso_fortran_env terminal I/O: example prints to stdout, then stderr and finally asks for user input with a prompt on the same line.

program myterm
use iso_fortran_env
implicit none (type, external)
character(1000) :: usertxt  ! 1000 is an arbitrarily large number
integer :: ios

! could also just use print *,'printed to stdout'
write(output_unit,*) 'Printed to stdout'

write(error_unit,*) 'printed to stderr'

! prompt with caret on same line as input, here using a greater than sign >
write(output_unit,'(A)',advance='no') ' >'
flush(output_unit)

read(input_unit,"(A)", iostat=ios) usertxt
! trap Ctrl-D EOF on Unix-like systems to avoid crashing program
if (ios/=0) backspace(input_unit)  ! ctrl D gobble

write(output_unit,*) usertxt

end program

If stderr goes to file fort.0

If stderr from error_unit gets written to a file fort.0 instead of being printed to screen, this is an indication of open(u)ing a file without first setting a value for u, which might default to 0.

Unless needing to persist a file opening between calls of a function/subroutine, normally open a file with newunit.

program myfile
use iso_fortran_env
implicit none (type, external)

integer :: u, ios
character(1000) :: fn ! 1000 is an arbitrarily large number
character(1000) :: dat

print *, "please input file to open"

read(input_unit, '(a)', iostat=ios) fn
if(is_iostat_end(ios)) stop

! open file, using better to ask forgiveness than permission principle
! status='old' means generate error if file doesn't exist
open(newunit=u,file=fn, status='old',action='read',iostat=ios)
if (ios /= 0) then
    write(error_unit,*) 'could not open file ',trim(fn)
    error stop 'file IO error'
endif

read(u,'(A)') dat
print *,'first two lines of ',trim(fn),' are:'
print *,trim(dat)
read(u,'(A)') dat
print *,trim(dat)

close(u)  ! implicitly closed at end of program, but as good practice...
end program

Related:

Copying or moving WINE to a new PC

A new PC with different username cause two issues for WINE when copying old PC user files to a new PC. If the Linux username changed, you have to update the “exec” lines of the .desktop files that reside in ~/.local/share/applications/wine/Programs/ (or create softlinks). Sometimes the drive links in ~/.wine/dosdevices differ after copying. Update your shortcuts/softlinks as necessary.