Scientific Computing

Free 2-D CAD drawing programs

AutoCAD 2-D libre alternatives are available for Linux, macOS and Windows. They generally require retraining for users coming from AutoCAD. Libre 2-D AutoCAD-like choices include FreeCAD, QCAD and LibreCAD.

FreeCAD is 3-D parametric modeling akin to SolidWorks that can import DXF or DWG. QCAD has distinct paid vs. Community Edition features include DWG and DXF read / write. LibreCAD can read / write DXF and read DWG.

The no-cost ODA File Converter converts DXF to / from DWG.

Install Nvidia HPC C, C++, Fortran compilers

The free-to-use Nvidia HPC SDK offers possible speed improvements and CUDA Fortran. A typical reason for using Nvidia HPC SDK is the Cuda GPU features. Nvidia HPC compilers support C11, C++23, and partial Fortran 2008 including submodule and error stop.

Download and install Nvidia HPC SDK. Create a script nvidia.sh:

To use NVIDIA HPC SDK, source the script:

source ~/nvidia.sh

CMake

In CMake, set NVIDIA HPC compiler-specific options in CMakeLists.txt like:

if(CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC")
  add_compile_options($<$<COMPILE_LANGUAGE:Fortran>:-Mdclchk;-Munixlogical>)
endif()

To use newer languages standard features ensure the underlying GCC toolchain is set to a new-enough compiler as per Nvidia HPC SDK documentation. The compiler path can be determined on RHEL-like Linux distros like:

scl enable gcc-toolset-12 "dirname $(which g++)"

If using a CMake toolchain file, instead of CXXFLAGS environment variable, one can set

set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN "/opt/rh/gcc-toolset-12/root/usr/")

Visual Studio /utf-8 source files

The MSVC compiler needs the /utf-8 flag when UTF-8 strings are in the source file. If not specified, too many bytes are assigned to each character, leading to incorrect string lengths. This will lead to failures with string width conversions such as WideCharToMultiByte.

Windows Intel oneAPI compiler didn’t need the /utf-8 flag when tested.

In CMake, apply the /utf-8 flag like:

add_compile_options($<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)

In Meson, the /utf-8 flag is applied automatically to C++ source files with MSVC compilers.

Beggar Barons of Software

Zed Shaw coins the term Beggar Barons in his blog post on being a popular software author begged to make significant changes to software to accommodate the large company’s needs. The recent situation involving Python vis-à-vis the Apple App Store reminds us of this phenomenon via Zed’s article. Python with iOS or Android requires using libpython in a compiled application, rather than the more typical terminal use–although there are apps that bring that experience to mobile devices as well.

Datetime vectors in Matlab / Octave

Generating a range of datetime data is a common data analysis and simulation task across programming languages. Matlab and GNU Octave can also generate datetime vectors.

Matlab datetime deprecates datenum. Generate a sequence of datetime like:

t0 = datetime('2020-01-05 12:30:00');
t1 = datetime('2020-01-06 18:15:10');
dt = hours(5.5);

t = t0:dt:t1;

disp(t)

GNU Octave can use many datetime features via the tablicious package.

pkg install tablicious

Load in Octave prompt:

pkg load tablicious

Then use the same Matlab code above.

No strict aliasing C / C++

Optimizing compilers may enable strict aliasing. For a wide variety of existing projects, strict aliasing provides additional optimization. For some projects, such as MUMPS, memory leaks have been observed that are resolved by disabling strict aliasing using GCC flag “-fno-strict-aliasing”.

Compilers with ability to switch on / off strict aliasing include:

libuv recommends -fno-strict-aliasing due to type punning.

References:

CMake Zstd compression

Zstd is an open file compression standard. Zstd has become widely used and is incorporated in the Linux kernel and GCC. We use Zstd for data archiving particularly for large files where size and speed are a concern. CMake supports Zstd compression throughout, including file(ARCHIVE_CREATE) and file(ARCHIVE_EXTRACT). Zstd is vendored into CMake, so there is no need to worry about system libraries for Zstd.

file(ARCHIVE_CREATE ... WORKING_DIRECTORY ...) is necessary to avoid system-specific relative path issues.

set(archive "example.zst")
set(in_dir "data/")

file(ARCHIVE_CREATE
  OUTPUT ${archive}
  PATHS ${in_dir}
  COMPRESSION Zstd
  COMPRESSION_LEVEL 3
  WORKING_DIRECTORY ${in_dir}
  )
COMPRESSION_LEVEL
arbitrary, bigger value is more compressed.
FORMAT
not used for Zstd.

Captive portal public Wi-Fi URLs

tl;dr: When connected to a captive portal, try visiting http://neverssl.com to trigger the portal.


Public WiFi often has captive portal login. Portals may coerce users to accept terms and conditions and absolution of liability before accessing the Internet. The portals may be worked around by DNS tunneling, MAC spoofing, etc. Many users just tolerate the portals.

Web browsers may try to trigger captive portals by checking servers, in case the OS hasn’t already triggered the captive portal. Sometimes captive portals aren’t triggered. HSTS blocks HTTP captive portal redirects. Try visiting a deliberately non-HTTPS portal-triggering site like http://neverssl.com

Devices on networks that block the platform’s automatic network checking may indicate like:

Connected, no Internet

If there’s not a captive sign-in webpage, the network connection may still work to non-Google sites.

Check connectivity manually using curl like:

curl -I -w "%{response_code}" http://neverssl.com
-I
show header only
-w "%{response_code}"
write the HTTP response code to console

Use cURL to check connectivity through captive portals

Return code 200:

  • Firefox
    curl -I -w "%{response_code}" http://detectportal.firefox.com/success.txt
  • Windows
    curl -I -w "%{response_code}" http://www.msftconnecttest.com/connecttest.txt
  • macOS
    curl -I -w "%{response_code}" http://captive.apple.com/hotspot-detect.html
  • Fedora
    curl -I -w "%{response_code}" http://fedoraproject.org/static/hotspot.txt

Return code 204:

  • Google
    curl -I -w "%{response_code}" http://connectivitycheck.gstatic.com/generate_204
  • Android
    curl -I -w "%{response_code}" http://connectivitycheck.android.com/generate_204
  • Ubuntu
    curl -I -w "%{response_code}" http://connectivity-check.ubuntu.com/generate_204

Python script to check connectivity through captive portals

Using Python requests, one can script testing of URLs with friendly feedback.

python ./captive_portal.py

Firefox: http://detectportal.firefox.com/success.txt returned 200
Windows: http://www.msftconnecttest.com/connecttest.txt returned 200
macOS: http://captive.apple.com/hotspot-detect.html returned 200
Fedora: http://fedoraproject.org/static/hotspot.txt returned 200
Google: http://connectivitycheck.gstatic.com/generate_204 returned 204
Android: http://connectivitycheck.android.com/generate_204 returned 204
Ubuntu: http://connectivity-check.ubuntu.com/generate_204 returned 204

Cobra 19 Ultra 6 CB radio

The Cobra 19 Ultra 6 appears similar to (but has different firmware from) radios like

The importance of the firmware difference is that the Cobra 19 Ultra 6 has a more limited feature set, and jumpering either of the white outlined jumper holes that the Anytone or Albrecht radios use for expansion next to the CPU has no effect on the Cobra 19 Ultra 6. Users should instead consider the Radioddity CS-47 over the Cobra 19 Ultra 6 for expandibility and key features including NRC noise reduction and CTCSS / DCS coded squelch that the Cobra 19 Ultra 6 lacks.

From practical in-vehicle use, the speaker is adequately loud. Both AM and FM mode work well on transmit and receive. There is no “dual watch” channel capability, but this may not be a requirement for many users.

RF Gain is essential on any CB radio and the RF gain adjustment range of the Cobra 19 Ultra 6 is good. Typically RF gain between 24 and 39 is useful.

The radio appears to use audio compandoring, which helps improve apparent audio SNR. The radio has a traditional signal squelch as well as “auto” noise squelch. The general problem with noise squelch across CB radios is that overmodulated AM signals can close the squelch regardless of how strong the signal is.

Setting the RF Gain and Squelch for any AM CB radio is best done by:

  1. open the squelch – static is heard.
  2. set the RF Gain so that faint static and/or skip signals are heard.
  3. set the signal squelch just until closed.

Channel Scan

On firmware version 1.1, entering scan mode is different than the user manual states. To enter channel scan, press and hold the microphone “up” or “down” button until the radio goes once through all 40 channels, then the radio beeps and release the button. The radio then scans all 40 channels repeatedly. Unfortunately, there does not appear to be a way to skip channels in scan mode. This means the radio will often stop on channels 6, 11, etc. with high power stations that may not be what the user wants to listen to.

Matlab read FITS image stacks

In Matlab, fitsread is used to read specific frame(s) from a FITS file. Read frames one at a time from a large multi-frame FITS file in MATLAB. This avoids overwhelming RAM or taking an excessive time to load just one or a few image frames from a FITS file.

Example: sequentially read and plot each frame of a 4096-frame FITS file, with each frame being 256 x 256 pixels.

for i = 1:4096
  currFrame = fitsread('myFile.fits', PixelRegion={[1 256],[1 256],i});
  imagesc(currFrame)
  pause(0.05)
end

GNU Octave FITS reading is done with the cfitsio package using a similar API.


Related: read FITS image stack in Python