A collection of tutorials and potentially reusable code snippets#
Add links to the code with short descriptions in the external table of contents.
WIP: xArray + HoloViews: Finding an ultimate way for gridded data#
See xarray_holoviews_gridded.ipynb
What is axis orientation when doing 2D plots#
http://nbviewer.jupyter.org/urls/gitlab.kwant-project.org/qt/cookbook/raw/master/axis_2d.ipynb
ArXiv feed watcher#
Source
Keep track of new papers on the arXiv without having to look at all the new abstracts every day. It fetches the RSS feed, runs a simple search for keywords in the title/abstract and authors and sends an email with the abstracts that match any of the criteria. It uses feedparser
, which is on io
. I run it with cron at 03:00 Monday to Friday. To set it up you need to fill out the search criteria and the SMTP email options so it can send an email from user1@server1.com to user2@server2.com.
Sparse diagonalization with MUMPS#
Example on using mumps
with scipy.sparse.linalg.eigsh
.
hpc05 usage checker#
hpc05-usage.py
See how many cores are currently in total and per user. Add an alias
to your ~/.bash_profile
for easy usage:
alias stat="python /home/$USER/full_path_to_script"

colormap normalizers#
cmap-normalizers.py
Nomalize your colorscale by pinning the middle of the colormap with MidpointNormalize
or by binning the data such in different parts of the colormap such that every range of data points is represented in the plot with HistogramNormalize
.
Spyview#
Did an experimentalist sent you a .mtx
Spyview file and you don’t run Windows? Do not despair:
def open_mtx(fname):
with open(fname, 'rb') as f:
line1 = f.readline()
*shape, entry_size = [int(i) for i in f.readline().split()]
data = np.fromfile(f, (np.float64 if entry_size == 8 else np.float32))
data = data.reshape(shape).squeeze()
keys = ['units', 'plot_settings', 'xname', 'xmin', 'xmax',
'yname', 'ymin', 'ymax', 'zname', 'zmin', 'zmax']
values = line1.decode("utf-8").replace('\n', '').split(',')
meta_data = dict(zip(keys, values))
return meta_data, data
LCAO Hamiltonian generator#
Tight-binding Hamiltonians are often given in the LCAO (linear combination of atomic orbitals) formalism.
You may have encountered large tables of parameters containing entries like $V_{pp\pi}$.
This utility computes the corresponding hopping matrices automatically, for example lcao_term(1, 1, 1, [0, 0, 1])
will return $H_{pp\pi}$ for a bond in the [0, 0, 1]
direction.
See docstring in LCAO Hamiltonian generator
for more details.
How to compile Quantum Espresso on hpc05#
ssh in hpc05 and download quantum espresso:
wget https://github.com/QEF/q-e/archive/refs/tags/qe-6.4.1.tar.gz
(hpc05 is not able to compile later versions)uncompress:
tar zxvf q-e-qe-6.4.1.gz
load the correct modules:
load module intel/2019u2 mpi/openmpi-1.8.8-intel
(need older intel to compile older QE version)go to the main folder and compile:
cd q-e-qe-6.4.1
,./compile
make the submodules you are interested in, usually PW or PP:
make pw
,make pp
run dft-hands-on tutorial to check if everything is working
owncloud
/ nextcloud
in your CLI#
You may use it, for example, to perform real-time visualisations of your running hpc jobs. You will be able to track the progress without the need to vpn/login to the cluster. This example is for surfdrive.
First, create a new webdav
password in your
settings page.
Then, upload any file with curl -u "user-id@tudelft.nl:webdav-pass" -X PUT "https://surfdrive.surf.nl/files/remote.php/nonshib-webdav/path/to/folder/$1" --data-binary @"$1"
.
You have to replace user-id
, webdav-pass
, $1
with your user name, the
generated password and the file name, respectively.
The file will appear as an ordinary file in your cloud. You may, for example, share it via link: new uploads under the same name will keep sharing settings.
Uploaded images can be displayed directly in the browser (and shared with your colleagues). Sometimes, browsers prefer to not download updated image previews: this might depend on OC/NC server settings.
Make sure to keep your
webdav
password in secret. If you prepare a script please make sure to setchmod 700 your-script
as a minimal measure.similarly, you may download and remove files, create folders in your cloud by replacing
PUT
with a properhttp
directive.
Voice notifications in executable cells#
If you’re running long simulations and want some sound notification when they’ve finished, someone worked out how to do it. All you need is one handy function:
def speak(text, voice=0):
'''
You can select a voice by putting in an integer. If you overshoot the length of the voice list, the default voice (0) is used.
'''
from IPython.display import Javascript as js, clear_output
# Escape single quotes
text = text.replace("'", r"\'")
display(
js(
f"""
if(window.speechSynthesis) {{
var synth = window.speechSynthesis;
var voices = synth.getVoices();
var utterThis = new window.SpeechSynthesisUtterance('{text}');
utterThis.voice = voices[{voice}];
synth.speak(utterThis);
}}
"""
)
)
# Clear the JS so that the notebook doesn't speak again when reopened/refreshed
clear_output(False)
Live plotting on JupyterLab#
Example stolen from here.
import numpy as np
from collections import defaultdict
from matplotlib import pyplot as plt
from IPython.display import clear_output
def live_plot(data_dict, figsize=(7,5), title=''):
clear_output(wait=True)
plt.figure(figsize=figsize)
for label,data in data_dict.items():
plt.plot(data, label=label)
plt.title(title)
plt.grid(True)
plt.xlabel(f'epoch')
plt.legend(loc='center left')
plt.show();
data = defaultdict(list)
for i in range(100):
data['foo'].append(np.sin(i/10) + 0.1*np.random.random())
data['bar'].append(np.random.random()+0.3)
live_plot(data)
Quick access to papers outside the TU Delft network#
TU Delft students and employees can access papers outside of the university network by saving this link as a bookmarklet: [access a paper PDF](javascript:(() => {window.location.href = ‘https://tudelft.idm.oclc.org/login?url=’+window.location.href})();) Then go to a paper url and click on the bookmarklet. This will prompt you to log in using your TU Delft netID, and you will then be able to access the paper pdf.
For more information, see Anton’s explanation.