Scientific Computing

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.

Matlab upgrade from Terminal

Matlab version upgrade can be initiated from the system Terminal or from the Matlab Help menu. Running the upgrade or Add-On install requires a graphical connection to the computer (Remote Desktop, VNC, or similar).

Matlab release upgrade

The bell icon on the upper right corner of the Matlab Desktop typically shows when a Matlab update is available. To force checking for install, even if the bell is not showing an update, under the Matlab binary directory look for “MathWorksUpdateInstaller”

fullfile(matlabroot, "bin", computer("arch"))

Matlab Add-Ons

Matlab Add-On Explorer is normally launched from the Matlab Desktop. The Add-On Explorer requires a graphical connection. To launch Add-On explorer from the Matlab binary directory look for executable “AddOnInstaller”

Troubleshooting

If the graphical connection has a problem like:

terminating with uncaught exception of type (anonymous namespace)::DisplayError: No display available.

try for diagnosis:

w = matlab.internal.webwindow('www.mathworks.com');

w.bringToFront();

The first command (object instantiation) should not error. The method “bringToFront” should bring up a window if the connection / system allows.


If on Linux with a graphical connection and errors result, try renaming libcrypto.so.1.1 like:

r=$(matlab -batch "disp(fullfile(matlabroot, 'bin', computer('arch')))" | tail -n1)

mv libcrypto.so.1.1 libcrypto.so.1.1.bak

Related: Matlab install on Linux

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 -forge tablicious

Load in Octave prompt:

pkg load tablicious

Then use the same Matlab code above.

CMake version recommendations and install

Downloading the latest release of CMake is usually easy. Admin / sudo is not required.

  • Linux: snap install cmake
  • macOS: brew install cmake
  • Windows: winget install Kitware.CMake
  • PyPI CMake package: python -m pip install cmake

For platforms where CMake binaries aren’t easily available, build CMake using scripts/build_cmake.cmake.

To see the merge requests for a certain release, use a URL like: https://gitlab.kitware.com/cmake/cmake/-/merge_requests?milestone_title=3.32.0&scope=all&state=all


CMake 3.32 enables CMAKE_LINK_WARNING_AS_ERROR boolean option sets most compilers to error if a compile warning occurs, which is generally a good setting for CI systems. target_link_libraries() can use a LINKER: prefix to abstract options. CTest –interactive-debug-mode is particularly useful on Windows to enable debug dumps and error popup windows.


CMake 3.31 warns if cmake_minimum_required() is < 3.10. TLS ≥ 1.2 is required by default for internet operations e.g. file(DOWNLOAD), ExternalProject, FetchContent, and similar. file(ARCHIVE_CREATE) gained a long-needed WORKING_DIRECTORY parameter that is essentially necessary to avoid machine-specific paths being embedded in the archive. CMAKE_LINK_LIBRARIES_STRATEGY allows specifying a strategy for ordering target direct link dependencies.


CMake 3.30 adds C++26 support. CMAKE_TLS_VERIFY environment variable was added to set TLS verification (true, false). CMake 3.31 defaults CMAKE_TLS_VERIFY to on, where previously it was off.


CMake 3.29 adds cmake_language(EXIT code) to exit CMake script mode with a specific return code. This is useful when using CMake as a platform-agnostic scripting language instead of shell script.

Environment variable CMAKE_INSTALL_PREFIX is used to set the default install prefix across projects–it can be overridden as typical by cmake -DCMAKE_INSTALL_PREFIX= option.

Target property TEST_LAUNCHER allows specifying a test launcher. For MPI program this allows deduplicating or making more programmatic test runner scripts.

Linker information variables including CMAKE__COMPILER_LINKER_ID have been added to allow programmatic logic like setting target_link_options() based on the particular linker.

ctest --parallel without a number or 0 will use unbounded test run parallelism.


CMake 3.28 changes PATH behavior for Windows find_{library,path,file}() to no longer search PATH. This may break some projects that rely on PATH for finding libraries. The MSYS2-distributed CMake is patched to include PATH like earlier CMake, which can be confusing for CI etc. not using MSYS CMake with that patch. Windows CI/user may need to specify CMAKE_PREFIX_PATH like

cmake -DCMAKE_PREFIX_PATH=$Env:SYSTEMDRIVE/msys64/ucrt64/lib -B build

Support for C++20 modules is considerably improved and most users will want at least CMake 3.28 to make C++ modules usable. Generator expressions $<IF> $<AND> $<OR> now short circuit. Test properties now have a DIRECTORY parameter, useful for setting test parameters from the project’s top level CMakeLists.txt. CMake 3.28.4 fixed a long-standing bug in Ninja Fortran targets that use include statements.


CMake 3.27 emits warning for cmake_minimum_required(VERSION) < 3.5. CTest test properties TIMEOUT_SIGNAL_NAME and TIMEOUT_SIGNAL_GRACE_PERIOD specify a POSIX signal to send to a timed out test process. Interactive CMake debugger added by cmake --debugger is used with an IDE such as Visual Studio. CMake script command cmake_file_api() allows querying CMake File API from within CMake. NOTE: Fortran + Ninja was broken for OBJECT libraries in CMake 3.27.0..3.27.8 and fixed in 3.27.9.


Older CMake changelog

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 shared 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.

Wouxun KG-S72C antenna

The Wouxun KG-S72C is a modern CB radio with AM and FM capabilities, which are recommended for anyone buying a new CB radio. The KG-S72C has CTCSS and DCS coded squelch, which allows hearing only parties with the same subaudible code. Only a few other models of CB radio have this capability, so most users might only occasionally use coded squelch. An important point to note is that no one sells the KG-S72C factory antenna replacement, so be very careful not to lose or damage the factory antenna. Since the KG-S72C receives an SMA female antenna (the radio has SMA female connector), using long replacement antennas are mechanically fragile and could break the antenna jack.

The sound quality on receive and transmit is good, especially in FM mode as for most AM / FM CB radios.

Cons

The KG-S72C has a few cons, which are tolerable.

  1. Lack of RF gain control, which is a fundamental requirement for any CB radio to make listening tolerable in AM mode. Using the KG-S72C as a mobile radio with an appropriate SMA adapter to the mobile antenna coax is OK, but the lack of RF gain control can be fatiguing and is the second con.
  2. The squelch mode seems to be noise squelch only, which can be frustrating as it closes intermittently on loud AM modulation. It would benefit from a signal strength squelch as traditional CB radios use.

Captive portal public WiFi URLs

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 -w %{http_code} URL
Platform server HTTPS response code
Firefox detectportal.firefox.com/success.txt 200
Chromium clients3.google.com/generate_204 204
Windows www.msftconnecttest.com/connecttest.txt 200
macOS captive.apple.com/hotspot-detect.html 200
Android connectivitycheck.gstatic.com/generate_204 204
Ubuntu connectivity-check.ubuntu.com 204
Fedora fedoraproject.org/static/hotspot.txt 200

To disable connectivity checking in Ubuntu: Settings → Privacy → “Network Connectivity Checking”

Cobra 19 Ultra 6 CB radio

The Cobra 19 Ultra 6 appears to be a relabeled AnyTone Smart II. While previous Cobra 19 models had significant features missing, the Cobra 19 Ultra 6 has the necessary features to be a good choice in entry-level very compact CB radio.

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 on an AM CB radio is best done by opening the squelch – static is heard. Then set the RF Gain so that only very faint static is heard. Then set the signal squelch just closed.

Channel Scan

On firmware version 1.1, entering scan mode is different than the 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