If your Qt application is taking too long to load on Android, it is likely because accessing asset-files is really slow on some devices. I just tested an application with about 12 QML-files and it took 40(!) seconds to load. This appears to have been an issue for quite some time, and although efforts have been made to reduce the problem, my app still loads too slow on my Asus Transformer TF300T. In Qt and Android’s defence, this device is known for its extremely slow eMMC storage.

The cure in my case was to use a Qt Resource Collection file for all my QML assets. In Qt Creator, this is as simple as clicking  File > New File or Project > Qt > Qt Resource file and then add a prefix “/” before adding all files from each QML folder. After creating and setting up the .qrc-file, I changed the URL in my main-function to “qrc:/qml/myproject/main.qml”.

This worked flawlessly, and the load time dropped from 40 seconds to less than a second!

You should of course be careful with what you add to your resource file. All listed files will be compiled into your executable, so if there are files you don’t want loaded right away, you might want to keep them outside your .qrc file.

The only downside with this approach is adding all new QML files to the resource file. This  is a bit tedious in the long run, so I suppose I will automate it with a script.

# Focusrite Scarlett 2i2 working flawlessly on Ubuntu with JACK

I needed a new sound card for Ubuntu that would allow me to do some recording and playback easily. After a bit of searching and testing, I figured I would go with the Focusrite Scarlett 2i2, which is a USB sound card with 2 XLR + jack inputs, stereo monitor output and a headphone jack. It also supports +48V phantom power if you need to connect a condenser microphone.

In addition it appears to be well-tested on Linux, with several user reporting that the device just works.

## Popping trouble

However, the device seemed to have some trouble with PulseAudio, which is the default audio library used in Ubuntu. Everything works, meaning that any audio application was able to output sound, and recording in Audacity gave good sound quality, but whenever I started playback or recording after switching between applications, the sound card would hang for 2 seconds and make 4 popping sounds before returning to normal operation.

## The solution: JACK + PulseAudio JACK Sink

If you are going to do any serious recording in Ubuntu, or Linux in general, you should get to know JACK. It is an awesome audio library for Ubuntu that aims to make life easy for musicians, audio engineers or the occasional amateur that wants to do recordings. It runs on top of ALSA, meaning it will support almost any sound card you can think of, but does so in a way that gives you extremely low latency and doesn’t mess with sound quality. The downside? JACK doesn’t allow PulseAudio to play side-by-side with it. And every Ubuntu application tries to play through PulseAudio by default.

The solution is to install the pulseaudio-module-jack package, which will allow applications to connect to JACK, even when they have no support for JACK:

sudo apt-get install pulseaudio-module-jack

In addition, you should install a nice GUI that will allow you to control JACK easily. This is named QjackCtl:

sudo apt-get install qjackctl

After installing, just run QjackCtl from the command line or the Dash and start + connect to the JACK server.

After this is done, you may check the Connect window to see if PulseAudio JACK Sink is also running, which it should do by default.

Then you may open Ubuntu’s Sound Settings and set PulseAudio JACK Sink to be your Output device. From now on, all applications should work even though JACK is running.

If you wish to, you may change QjackCtl’s settings to make JACK start whenever QjackCtl is started, and afterwards add QjackCtl to your Startup Applications in Ubuntu. This way JACK will start whenever you start your computer.

## No more popping

After installing PulseAudio JACK Sink and QjackCtl, I no longer experience any popping, and the FocusRite Scarlett 2i2 works flawlessly. The sound quality is great and the latency minimal, so I would definitely recommend this sound card to any Linux user wanting to do some recording at home or on the go.

Even though I wish it would work right out of the box, installing two packages to get everything perfect is that much work, and I assume that someone figures out a good solution to merge PulseAudio and JACK in a way similar to the one I use by default sometime in the future.

# Writing molecular dynamics data to binary LAMMPS format

In this post I will explain  how to write to the binary LAMMPS file format from C++, using data stored in Armadillo vectors and matrices. After running the example in this post you should be able to open the resulting file in Ovito or any other program capable of reading binary LAMMPS files. The example should also be fairly easy to port to other data structure type, if needed.

For the impatient: You’ll find a working main.cpp file and a qmake project file on GitHub.

The result, if rendered in Ovito, is two silicon atoms (in red) and one oxygen atom:

LAMMPS is a molecular dynamics simulation package that is extremely versatile with plenty of interaction potentials and features implemented. However, you may have written some other code involving atoms and found yourself in the position of considering using a standard file format to write atom data to file. In this case, the XYZ-format has likely passed your mind, but because this is a ASCII-based text format, it is slow to read and write. This format also lacks standardized headers for information such as the system boundaries – causing visualizers like Ovito to have to guess for the right boundaries in your system.

# Playing around with Sculpt Mode in Blender

While preparing for a presentation I’m giving on Python scripting in Blender, I figured I’d  also try out some new features . One such feature is in Sculpt Mode, where dynamic topology is now allowing fine-grained control over the level of detail while sculpting. This turned out to be a lot of fun, so I decided to sculpt a human face while at it:

Dynamic topology is such an amazing addition to Blender and exactly what lacked in previous versions. The feature has been around for a while and has truly matured the whole Sculpt Mode since last time I tried it in Blender.

There’s a long list of things I would like to fix in this rendering, but with so much going on in other projects, I will be putting this away for now. Should I pick it up again later, I might even start from scratch and make better use of Blender’s multiresolution features too.

If you want to start learning how to sculpt human faces in Blender (in much greater detail than what I have achieved here), you should take a look at this great video course.

# Copying data files to the build directory with qmake

Sometimes you want to copy some data files to the same folder as your final executable when you are building with qmake in a different directory than your source code. This is what Qt Creator does by default, so this is quite often needed.

Just add the following to your .pro file to copy the folder “data” from the source directory to the build directory whenever you build the source code:

copydata.commands = $(COPY_DIR) $$PWD/data$$OUT_PWD first.depends =$(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata

The meaning of the different names in the above are as follows:

• \$(COPY_DIR) – Holds a platform-dependent copy-command that makes sure to copy recursively.
• $$PWD – Holds the name of the source code directory, where your .pro file resides. •$$OUT_PWD – Holds the name of the build directory. Note that this may not work if you are running qmake and make in the same directory as your source code, as you will be copying into the same folder as you copy from.
• copydata – Just a name we choose. You can replace this with whatever you want.
• first - This is a build step that already exists in the Makefile generated by qmake. We attach the copy build step to this build step.
• export – Makes the variable in the argument of the function available in a global scope.
• QMAKE_EXTRA_TARGETS - Tells qmake that you want to build more targets than the ones it adds by default to the Makefile.

This method is a bit messy, and I wish the Qt developers would make it easier to do this, but it works. It also ensures that the data is always copied, ensuring that any changes in the data folder are pushed to the build folder.

# Multiple billboards in Qt3D

While Qt3D has a transform for drawing billboards, it is not very useful if you are drawing thousands of particles. This is because the transform can only be applied to one object at the time, and using thousands of objects is never a good idea in neither OpenGL or Qt. This is simply because the overhead of each object is very large, both in terms of memory and performance.

The solution I found was to write my own custom class that inherits QQuickItem3D and is capable of drawing a list of 3D points as billboards by directly turning each sprite towards the camera at every draw call. This is not very efficient, but it works and is way faster than the options: Drawing spheres or creating one billboard per particle. The ideal option is of course to use geometry shaders, but they are not easily available in Qt3D yet. Most likely because its a new feature in OpenGL 3.2.

The class is available on GitHub and the resulting output is shown in this screenshot:

# Using Qt3D today

Qt3D is an amazing library for Qt that gives you the ability to render your own 3D stuff together with your existing widget or QML based GUI. The library was started by the Qt developers a few years back, but has not yet been released with the official Qt SDK. It was announced that it would be bundled with Qt5.0, but because of the state of the library at the time, its release was postponed. Even so, the library is still very mature and appears to work very well in my opinion. So I would urge you to test it out.

## Simple install on Ubuntu

To use the library you have a couple of different choices. If you are running Ubuntu, you may just run the following commands:

sudo apt-get install qt3d5-dev qtdeclarative5-qt3d-plugin

And you should be good to go. Look for examples in the Qt3D documentation.

Note that Ubuntu currently doesn’t have QtQuick.Controls available, so you will have to follow the instructions below to combine Qt3D with QtQuick.Controls.

## Install Qt3D into the newest Qt release

Currently Qt5.1.1 is the newest stable release of Qt and is the version I’d recommend that you use with Qt3D.

2. Clone Qt3D from Gitorious:
git clone git://gitorious.org/qt/qt3d.git qt3d
3. Open QtCreator from your new Qt installation.
4. Open the qt3d.pro file from the cloned repository and configure it to use the platforms on which you want to use Qt3D (desktop, Android, etc.).
5. Build for each platform you want to use. Set the build to Release instead of Debug mode while building. You should receive about one hundred warnings if you are building for Android. As always, ignore these.
6. Open the build folder in a terminal and run
make install

This will not install to your system, but to your Qt installation, so that it gets the ability to do awesome 3D stuff.

You should now have Qt3D installed in your Qt installation and be ready to run projects that are using Qt3D.

# When variational Monte Carlo becomes visual

Today I was playing around with the output of the code from Jørgen’s thesis, and figured it would be fun to visualize the random walk in the variational Monte Carlo calculation.

For the interested, this is a visualization of a variational Monte Carlo calculation for the hydrogen molecule. The algorithm used is the Metropolis-Hastings algorithm. The two blue spheres represent the atomic nuclei, while the green spheres are the current electron configuration being sampled. The distance between the nuclei is about two times the distance for hydrogen in the ground state. The red spheres represent some of the history of the computation. If the complete history was included, the red spheres could have been interpreted as the electron density, but as you may notice, the history included is too short, so that the electron density fluctuates through the video. (Hint: Look at the density between the atoms – it varies between different times in the video.)

# Monitoring your unit tests without lifting a finger

I love unit testing. First of all, I think it is a good idea to test separate units of the code, but after doing so for some time, I’ve come to realize that unit tests are great for managing the software development cycle too. It all boils down to the idea that you should write tests before you write your code.

Now, this is something that I and others apparently struggle a lot with. How do you write a test for some code that doesn’t even exist yet? Even worse, how do you write a test for a piece of software that you’re not yet sure how will be used?

In computational physics, this problem arises often because we are writing code at the same time as we are trying to understand the physics, mathematics and algorithms at hand. And this is a good thing. You might want to think that one should structure all code before it is written, but this is generally a bad approach in computational physics. Especially if you’re working on something new. The reason is that you will often understand the problem and algorithms better while developing, rather than just reading about them and trying to analyze them blindly.

## Keeping the tests and code healthy

But enough with the talk, let’s just assume that you are convinced that you should (or have to) implement some unit tests. At one point you are likely to be in a position where you find it tiresome to have to go into that folder where the tests are defined and run them manually. This is where Jenkins comes in to play.

# Speeding up compilation on Ubuntu with Qt Creator

Are you reading random stuff on the web while waiting for your C++ compilation to finish? Then you have come to the right place. In this post I will tell you about two really nice tweaks you may do to speed up your compilations, namely ccache and the make -j flag, and how you may set these up in Qt Creator.

## ccache

ccache is a clever tool that wraps you compiler (g++ or mpicxx) and understands whether the file you are compiling has been compiled before with exactly the same contents and settings. If it has, it just returns a cached result.

Unlike regular make, ccache is extremely good at detecting the true state of what you are compiling. I have never had any trouble with ccache.

This really speeds up the compilation when you are using make clean, especially if you are switching git branches. In other words, it is a much simpler solution to achieve fast compilation with git branches than to create separate build folders for each branch.

To enable ccache, install it with

sudo apt-get install ccache

QMAKE_CXX = ccache g++

Replace g++ with mpicxx if you are using MPI.

## The make -j flag

I realized when compiling the Qt source that make has a -j flag that enables threaded compilation on all available processors on the machine. This also speeds up compilation significantly, and I made a 3.55x performance gain on a 4 core CPU. To enable this flag, go to the Projects view in Qt Creator and add the following arguments to the make build step:

-j

This should look something like this afterwards:

This is how your project settings should look like after adding the -j option to make.

If you prefer not to use all available processors for compilation, you may add a number after -j to set the number of processors. For instance make -j 3 would compile with 3 processors.