Handle crystalline symmetry in the browser, using pure JavaScript

Tilde
Materials Informatics Lab
2 min readNov 18, 2015

--

After RasmolJS the next Emscripten experiment on C-to-JavaScript conversion was the Spglib symmetry library, created by Dr. Atsushi Togo-san. The process was quite straightforward; the result was called SpglibJS.

Spglib in C + JS = SpglibJS

Now to determine crystalline symmetry only a browser suffices (obviously, without any plugins). To foster any further ideas and usecases, an example web app was created: it allows symmetry validation for CIF and POSCAR files. An explicit symmetry information is absent in POSCARs and often omitted in CIFs, so this web app provides a quick way of checking the symmetry for such cases. No server is used after page loading. It is possible to drop a file at the webpage and check a space group of the crystalline structure without Internet connection (and without any concerns about privacy).

A few details for those wishing to dive in. It’s clear that the JavaScript and C data structures are absolutely different. Emscripten emulates C environment in the browser and provides an interface to access webpage environment from the compiled C code and, vise versa, to access C memory from the “normal” JavaScript at the webpage. Thus, to pass a crystal from the webpage to the Spglib compiled code and get the result back we do the following:

  • First, we “flatten” all many-dimensional JavaScript structures used at the webpage to store the CIF or POSCARs. Method to_flatten of the matinfio.js library is responsible for that.
  • Second, we write our flattened data structures into Emscripten memory stack. This happens in get_spacegroup function in validator.html. Right here we then call wrap_spg_get_syminfo function (defined in wrapper.c), to start the Spglib compiled code.
  • Third, we define a wrap_spg_handle_result function in validator.html, to be called from Spglib compiled code, when it’s ready with the result.

As soon as we get the result into disposal of our “normal” JavaScript at the webpage, we are free to do what we want, e.g. to display it in HTML.

--

--

Intelligent software for computational materials science and cheminformatics. Free and open-source. Inspired by #BlueObelisk Web: https://tilde.pro