Scientific Computing

Matlab HiDPI support

Matlab can adjust for HiDPI (2K, 4K, etc.) displays. HiDPI Matlab IDE resolution (icon and font size) can be manually corrected from within Matlab on any operating system.

Matlab has programmatic DPI access to get / set information on DPI as well as numerous other Matlab characteristics via settings.

Matlab DPI setting:

S = settings;
S.matlab.desktop.DisplayScaleFactor

returns existing display scale like:

ActiveValue: 1
TemporaryValue: <no value>
PersonalValue: <no value>
FactoryValue: 1

set a new persistent DisplayScaleFactor by setting:

S = settings;
S.matlab.desktop.DisplayScaleFactor.PersonalValue = 1.25  % bigger number, larger icons & text

Verify the settings has taken effect after Matlab restart by:

S = settings;
S.matlab.desktop.DisplayScaleFactor

resulting in:

     ActiveValue: 1.2500
  TemporaryValue: <no value>
   PersonalValue: 1.2500
    FactoryValue: 1

Reference

GDL GNUDataLanguage reading IDL .sav files

While GDL has CMSvlib installed to read / write IDL .sav files, here is how to install CMSvlib if desired.

Download CMSVlib and extract to “~/.local/share/cmsvlib”. Add line to ~/.profile:

export GDL_STARTUP=$HOME/.gdl/gdl-startup.pro

Copy and paste into  “~/.gdl/gdl-startup.pro”

!PATH=!PATH+':'+Expand_Path('+~/.local/share/cmsvlib/')

Reopen Terminal and GDL. Load IDL .sav file like:

restore,'myfile.sav'

Check GDL paths loaded:

print,!PATH

Autogenerated Fortran docs using FORD

This is a basic example of using FORD to document Fortran projects. FORD is a Python program:

pip install ford

Each project will have a ford.md that configures and makes description for the main project landing page.

If your code is all under the top-level directory, set ford.md like: (this example is for the fortran2018-examples repo)

src_dir: ./
output_dir: ./docs
project: Fortran 2018 examples
project_github: https://github.com/scivision/fortran2018-examples
project_website: https://scivision.github.io/fortran2018-examples
summary: Fortran 2018 examples
author: Jane Doe
author_description: SciVision, Inc.
github: https://github.com/scivision
license: by
exclude: CMakeFortranCompilerId.F
display: public
         protected
         private
source: false
graph: true
search: true

Put Markdown test of arbitrary length here.
Within the Fortran code, use `!!` on each line with Markdown, Latex, etc.

When satisfied, move the documentation files to the gh-pages branch. This avoids cluttering Git with several megabytes of frequently changing documentation history.

Notes

Using Meld from Cygwin

To use Meld from Cygwin:

Download and install Meld for Windows. If you get “missing MSVCP100.dll”, install Microsoft Visual C++ 2010 redistributable package

Add to Cygwin ~/.bash_profile

alias meld="/cygdrive/c/Program\ Files/Meld/Meld.exe"

Open a new Cygwin terminal to use Meld from Cygwin.

Many use Meld for git mergetool. For Cygwin:

git config --global diff.tool meld
git config --global merge.tool meld

git config --global mergetool.meld.path "/cygdrive/c/Program Files/Meld/Meld.exe"

Flip/Rotate LaTeX images

LaTeX rotation and flipping of text and images may be accomplished via graphicx.

Optional trim=left lower right upper is flipped vertically too. Trimming “lower” actually trims “upper”.

Rotation

\rotatebox[origin=c]{90}{\includegraphics[]{myimg}}
90
degrees of rotation counter-clockwise
origin=c
rotate about center of object

Vertical flip

\scalebox{1}[-1]{\includegraphics[]{myimg}}

Horizontal flip

\scalebox{-1}[1]{\includegraphics[]{myimg}}

Minimal working example

\documentclass[]{article}

\usepackage{graphicx}

\begin{document}

90 degree rotation \rotatebox[origin=c]{90}{\includegraphics[]{myimg}}

Vertical Flip \scalebox{1}[-1]{\includegraphics[]{myimg}}

Horizontal Flip \scalebox{-1}[1]{\includegraphics[]{myimg}}

\end{document}

graphicx package docs

Get network parameters NetworkManager CLI

The nmcli program gives access to current network parameters on many Linux distros. For example, to get current DNS server:

nmcli dev show | grep DNS

This returns something like:

IP4.DNS[1]:                             1.1.1.1
IP6.DNS[1]:                2606:4700:4700::1111

NMCLI output is made for machine parsing. An example of nmcli Python parsing is Python WiFi scanning.

Linux / Python QR code generation

Command-line QR code generation is enabled on Linux by:

apt install qrencode

Usage

This example is for 300 dpi with Medium error correction, making a PNG image.

qrencode -s10 -d 300 -lM https://example.invalid -o qr-example.png

Notes

The QtQR GUI program allows easy generation of QR codes.

apt install qtqr

Fortran and short-circuit logic

Never assume that eager or short-circuit evaluation of logical statements will occur in Fortran.

Fortran standards from Fortran I (1957) through Fortran 2018 do not mandate or prohibit short-circuit logic. That has resulted in some compilers (e.g. Gfortran) sometimes using short-circuit logic, while other compilers (e.g. Intel) do not use short circuit logic. This causes breakage when the programmer tests with one Fortran compiler handles compound logic with eager evaluation, but other compiler uses short-circuit evaluation.

Example code

Compilers not short-circuiting (standard Fortran behavior):

  • Gfortran -O0
  • NAG
  • Intel

Short circuiting (non-standard behavior)

  • Gfortran -O1 or higher

Proper handling of compound logic in Fortran: one should carefully avoid assumptions that either eager or short-circuit evaluation of compound logical statements will occur, because neither is guaranteed by any Fortran standard.

Assuming eager evaluation of Fortran logical statements, this can cause breakage where a function that’s part of the statement has side effects. For example, a function that modifies a module variable, or simply an intent(inout) variable will find those variables unmodified if a compiler uses short-circuit logic.

Fix: break up the compound if statement so that the function is always evaluated (e.g. just before the if statement).

Assumptions that short circuit evaluation occurs commonly causes breakage of present(arg) for optional :: arg optional dummy arguments.

subroutine myfun(a,b)
real, intent(inout) :: a
real, intent(in), optional :: b

! don't do this!
if (present(b) .and. b < 0)  a = b*a

! instead, do this:
if present(b) then
  if (b < 0) a = b*a
endif

Build OpenMPI for Fortran

OpenMPI is often available from package managers across computing platforms. Users might build OpenMPI from source to get the latest version or to support other compilers. OpenMPI takes several minutes to build.

Download latest OpenMPI source. Configure OpenMPI with Fortran compiler:

./configure --prefix=~/.local/openmpi CC=gcc CXX=g++ FC=gfortran

Don’t just use ~/.local as that spills OpenMPI files into common directories, making it hard to use multiple OpenMPI versions or multiple compilers.

Build and install OpenMPI:

make -j

make install   # no sudo

Linux: add to “~/.profile”:

export LD_LIBRARY_PATH=$HOME/.local/openmpi/lib:$LD_LIBRARY_PATH

The FindMPI CMake module allows switching between multiple OpenMPI versions installed with MPI_ROOT variable:

cmake -DMPI_ROOT=~/.local/openmpi ..

As in general for CMake packages, the MPI imported target is highly recommended over the legacy discrete variables, as in this minimal CMakeLists.txt:

find_package(MPI REQUIRED COMPONENTS Fortran)

add_executable(mpivers mpivers.f90)
target_link_libraries(mpivers MPI::MPI_Fortran)

Troubleshooting:

If it doesn’t work with CMake, try ~/.local/openmpi/bin/mpif90 myprog.f90. If that works, try something like:

FC=gfortran CC=gcc cmake -DMPI_Fortran_COMPILER=~/.local/openmpi/bin/mpif90 ..

Fortran MPI examples

Related: MPI for MinGW GCC / GFortran on Windows

git push to multiple sites

The October 2018 day-long GitHub outage led many to consider having a live backup of their Git repos. Multi-remote git push is intrinsic to Git itself. Thus, automatic multi-site pushes are easy to configure for an arbitrarily large number of backup sites (GitHub, GitLab, Bitbucket, Dropbox, …).

This article covers the typical case where the main Git repository is on GitHub, but a public backup is desired on GitLab or similar site. The article assumes an existing working GitHub repo cloned to the computer username/myprog. We also assume SSH keys on both GitHub and GitLab. Use different SSH key pairs for each site for best security.

Back up the GitHub repo username/myprog to GitLab with every git push automatically as in the following section.

GitHub with GitLab backup

  1. check that the GitHub repo is setup normally on your computer:

    cd myprog
    
    git remote -v

    should be like:

    origin	https://github.invalid/username/myprog (fetch)
    origin	ssh://github.invalid/username/myprog (push)
  2. Create a new repo username/myprog on GitLab or other backup site

  3. Add the GitLab (or other) backup site:

    git remote set-url origin --push --add ssh://github.invalid/username/myprog
    git remote set-url origin --push --add ssh://gitlab.invalid/username/myprog
  4. Verify that BOTH push sites are there (in this example, github.com and gitlab.com):

    git remote -v

    should be like:

    origin	https://github.invalid/username/myprog (fetch)
    origin	ssh://gitlab.invalid/username/myprog (push)
    origin	ssh://github.invalid/username/myprog (push)

When pushing, you will see multiple Everything up-to-date – one for each site you’re pushing to.