## New publication in J. Phys. Chem. Lett.

| categories: | tags: | View Comments

DFT calculations are extensively used to predict the chemical properties of metal alloy surfaces, but they are expensive which limits the number of calculations that can be practically be calculated. In this paper, we explore a perturbation approach known as alchemy to take previously calculated results and extend them to new compositions. We use oxygen reduction as a prototype reaction, and show that alchemy is often much faster than DFT, with an accuracy within 0.1 eV of the DFT. There are cases where the accuracy is not as good suggesting that further improvements to the perturbation model could be beneficial. Overall, alchemy appears to be a useful tool in high-throughput screening research.

@article{saravanan-2017-alchem-predic,
title =        {Alchemical Predictions for Computational Catalysis: Potential
and Limitations},
year =         2017,
Author =       {Saravanan, Karthikeyan and Kitchin, John R. and von
Lilienfeld, O. Anatole and Keith, John A.},
Bdsk-Url-1 =   {http://dx.doi.org/10.1021/acs.jpclett.7b01974},
Doi =          {10.1021/acs.jpclett.7b01974},
Eprint =       {http://dx.doi.org/10.1021/acs.jpclett.7b01974},
Journal =      {The Journal of Physical Chemistry Letters},
Note =         {PMID: 28938798},
Number =       {ja},
Pages =        {null},
Url =          {http://dx.doi.org/10.1021/acs.jpclett.7b01974},
Volume =       0,
}



org-mode source

Org-mode version = 9.0.7

## Finding similar bibtex entries

| categories: | tags: | View Comments

A common task while writing scientific papers is citing previous research. I use org-ref extensively for that, and it makes it pretty easy to find similar references, e.g. that have common authors, or common keywords. It also lets me find similar articles in Web of Science or Scopus. Suppose that I have cited a particular paper, e.g. e boes-2016-neural-networ, and I want to find similar references to it that are already in my bibtex file, and similar by my definition. With org-ref I can easily search by keyword or author to find similar entries, but these are limited by what I search for, and they are not sorted. Today, I will explore the first step in a recommender system that calculates similarity, and provides a sorted list of candidates with the most relevant ones first.

The idea is to calculate some measure of similarity between the title of that reference, and the titles of other references in my bibtex file, and then sort them by similarity. This is the reference I want to find similar entries for:

Boes, J. R., Groenenboom, M. C., Keith, J. A., & Kitchin, J. R., Neural network and Reaxff comparison for Au properties, Int. J. Quantum Chem., 116(13), 979–987 (2016). http://dx.doi.org/10.1002/qua.25115

The first thing we do is read in our bibtex file, and print a representative entry.

import bibtexparser
from bibtexparser.bparser import BibTexParser

with open('/Users/jkitchin/Dropbox/bibliography/references.bib') as bibtex_file:
parser = BibTexParser()
entries = bib_database.entries

print(entries[10])


{'author': 'Jaan Aarik and Aleks Aidla and V{\\"a}ino Sammelselg and Teet\nUustare', 'title': 'Effect of Growth Conditions on Formation of \\ce{TiO_2}-{II}\nThin Films in Atomic Layer Deposition Process', 'journal': 'Journal of Crystal Growth', 'volume': '181', 'number': '3', 'pages': '259 - 264', 'year': '1997', 'doi': '10.1016/S0022-0248(97)00279-0', 'link': 'http://www.sciencedirect.com/science/article/pii/S0022024897002790', 'issn': '0022-0248', 'ENTRYTYPE': 'article', 'ID': 'aarik-1997-effec-growt'}

Each entry is a dictionary containing the fields and their values. For this exploration, I will only consider similarities between titles. The next step is we find which entry corresponds to the reference we want to find similarities to.

ids = [e['ID'] for e in entries]
i = ids.index('boes-2016-neural-networ')
print(entries[i])


{'author': 'Jacob R. Boes and Mitchell C. Groenenboom and John A. Keith\nand John R. Kitchin', 'title': 'Neural Network and {Reaxff} Comparison for {Au} Properties', 'journal': 'Int. J. Quantum Chem.', 'volume': '116', 'number': '13', 'pages': '979-987', 'year': '2016', 'doi': '10.1002/qua.25115', 'link': 'http://dx.doi.org/10.1002/qua.25115', 'issn': '1097-461X', 'keyword': 'Kohn-Sham density functional theory, neural networks, reactive\nforce fields, potential energy surfaces, machine learning', 'ENTRYTYPE': 'article', 'ID': 'boes-2016-neural-networ'}

It is best if we make the entry we want to find similarities to the first one, so here we swap the first and ith entries.

entries[0], entries[i] = entries[i], entries[0]


Now, we prepare the list of strings to get similarities for.

titles = [e.get('title', '') for e in entries]


We will use term frequency–inverse document frequency to get a vector that represents each title, and then use cosine similarity as a measure of similarity. Here is the place to note that I chose these, and could choose other ones too. Also, it is worth noting that in this measure of similarity I did not choose which keywords to measure similarity on.

The functionality for this is provided by sklearn. It has implemented functions for the algorithms above, and in just a few lines of code you get an array of tf-idf features to analyze. The array we get from our vectorizer contains normalized vectors, so we can get the cosine similarity just from a dot product of the vectors. The first row corresponds to the similarity of the first string to all the others. I want them sorted in descending order. The argsort function returns ascending order, so we use a trick to sort the negative of the similarity score which achieves that. There are certainly more advanced treatments of the text we could use by customizing the vectorizer, e.g. word stemming, but for now we neglect that.

from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(titles)

cosine_similarities = (X * X.T).A[0]

related_docs_indices = (-cosine_similarities).argsort()

print('The top 10 recommendations for {} are:\n'.format(S[0]))
for i, j in enumerate(related_docs_indices[1:11]):
print('{i}. {ID}: {title}, {author}\n'.format(i=i + 1, **entries[j]))


The top 10 recommendations for Neural Network and {Reaxff} Comparison for {Au} Properties are:

1. behler-2010-neural: Neural network potential-energy surfaces for atomistic

simulations, J{\"o}rg Behler

1. boes-2017-neural-networ: Neural Network Predictions of Oxygen Interactions on a Dynamic

{Pd} Surface, Jacob R. Boes and John R. Kitchin

1. eshet-2010-ab: Ab Initio Quality Neural-Network Potential for Sodium, Hagai Eshet and Rustam Z. Khaliullin and Thomas D. K{\"u}hne

and J{\"o}rg Behler and Michele Parrinello

1. behler-2014-repres-poten: Representing Potential Energy Surfaces By High-Dimensional

Neural Network Potentials, J Behler

1. behler-2007-gener-neural: Generalized Neural-Network Representation of High-Dimensional

Potential-Energy Surfaces, J{\"o}rg Behler and Michele Parrinello

1. artrith-2012-high: High-Dimensional Neural Network Potentials for Metal Surfaces:

A Prototype Study for Copper, Nongnuch Artrith and J{\"o}rg Behler

1. behler-2015-const: Constructing High-Dimensional Neural Network Potentials: A

Tutorial Review, J{\"o}rg Behler

1. artrith-2011-high: High-Dimensional Neural-Network Potentials for Multicomponent

Systems: Applications To Zinc Oxide, Nongnuch Artrith and Tobias Morawietz and J{\"o}rg Behler

1. sosso-2012-neural-gete: Neural Network Interatomic Potential for the Phase Change

Material \ce{GeTe}, Gabriele C. Sosso and Giacomo Miceli and Sebastiano Caravati and J{\"o}rg Behler and Marco Bernasconi

1. lorenz-2006-descr: Descriptions of Surface Chemical Reactions Using a Neural

Network Representation of the Potential-Energy Surface, S{\"o}nke Lorenz and Matthias Scheffler and Axel Gross

It is evident that this is showing other references containing the words "neural network"! I guess that is a little disappointing, since these would just as easily been narrowed down in org-ref. On the other hand, they are sorted and grouped, which would not happen in org-ref. This is a comparison of pretty short strings (just the titles), so maybe this would be much more interesting if abstracts were also included. Including authors would give a different set as well (I tried it, and got a bunch of my own references!).

I don't think it would be very difficult to get this into an Emacs selection tool, e.g. helm/ivy. Check this out:

import pycse.lisp

related_docs_indices[1:6].lisp


'(1592 1650 299 1751 103)'

That is a result that can be read directly by lisp, so we could simply write the code above as a shell script that takes an argument, and returns a list of indices to sort the candidates on. The alternative is to implement this in elisp, perhaps via a dynamic module if there is already a good C library for this. My sense is the Python libraries are more advanced in functionality.

This could have a number of other applications. Given some reference content, you could imagine finding emails that are similar to it, finding RSS entries that are similar to it, finding org headlines that are related, similar files, or similarity with any other set of strings that can be gathered, e.g. from Crossref or some other search, etc. I predict there will be more on these topics in the future!

org-mode source

Org-mode version = 9.0.7

## New publication in Journal of Physics Condensed Matter

| categories: | tags: | View Comments

The Atomic Simulation Environment is a powerful python library for setting up, running and analyzing molecular simulations. I have been using it and contributing to it since around 2002 when I used the ASE-2 version in Python 1.5! The new ase-3 version is much simpler to use, and much more powerful. This paper describes some of its design principles and capabilities. If you use ASE, please cite this paper!

@article{larsen-2017-atomic-simul,
author =       {Ask Hjorth Larsen and Jens J{\o}rgen Mortensen and Jakob
Blomqvist and Ivano E Castelli and Rune Christensen and
Marcin Dułak and Jesper Friis and Michael N Groves and
Bj{\o}rk Hammer and Cory Hargus and Eric D Hermes and Paul C
Jennings and Peter Bjerre Jensen and James Kermode and John
R Kitchin and Esben Leonhard Kolsbjerg and Joseph Kubal and
Kristen Kaasbjerg and Steen Lysgaard and J{\'o}n Bergmann
Maronsson and Tristan Maxson and Thomas Olsen and Lars
Pastewka and Andrew Peterson and Carsten Rostgaard and Jakob
Schi{\o}tz and Ole Sch{\"u}tt and Mikkel Strange and Kristian
S Thygesen and Tejs Vegge and Lasse Vilhelmsen and Michael
Walter and Zhenhua Zeng and Karsten W Jacobsen},
title =        {The Atomic Simulation Environment-A Python Library for Working
With Atoms},
journal =      {Journal of Physics: Condensed Matter},
volume =       29,
number =       27,
pages =        273002,
year =         2017,
url =          {http://stacks.iop.org/0953-8984/29/i=27/a=273002},
abstract =     {The atomic simulation environment (ASE) is a software package
written in the Python programming language with the aim of
setting up, steering, and analyzing atomistic simulations. In
ASE, tasks are fully scripted in Python. The powerful syntax
of Python combined with the NumPy array library make it
possible to perform very complex simulation tasks. For
example, a sequence of calculations may be performed with the
use of a simple 'for-loop' construction. Calculations of
energy, forces, stresses and other quantities are performed
through interfaces to many external electronic structure codes
or force fields using a uniform interface. On top of this
calculator interface, ASE provides modules for performing many
standard simulation tasks such as structure optimization,
molecular dynamics, handling of constraints and performing
nudged elastic band calculations.},
}



org-mode source

Org-mode version = 9.0.7

## New publication in Crystal Growth & Design

| categories: | tags: | View Comments

Usually, metal oxides grow in a single, most stable crystal structure at a particular set of conditions. For example, TiO2 grows in the rutile structure for a large range of pressure and temperature conditions, but under some conditions it can also grow in the anatase structure. In this paper we show that epitaxial stabilization can be used to influence which crystal structures are observed for the growth of tin oxide. Tin oxide is normally only observed in the rutile structure. We grew tin oxide as an epitaxial film on a poly-crystalline substrate of CoNb2O6 which has an α-PbO2 crystal structure. We found that both rutile and α-PbO2 structures could be found in the film, and that the structure correlated with the orientation of the underlying grains. In other words, the orientation of a substrate can influence the structure of an epitaxial film, enabling one to grow films in crystal structures that may be metastable, and unobtainable in bulk samples.

@article{wittkamper-2017-compet-growt,
author =       {Wittkamper, Julia and Xu, Zhongnan and Kombaiah, Boopathy and
Ram, Farangis and De Graef, Marc and Kitchin, John R. and
Rohrer, Gregory S. and Salvador, Paul A.},
title =        {Competitive Growth of Scrutinyite ($\alpha$-PbO2) and Rutile
Polymorphs of \ce{SnO2} on All Orientations of Columbite
\ce{CoNb2O6} Substrates},
journal =      {Crystal Growth \& Design},
volume =       17,
number =       7,
pages =        {3929-3939},
year =         2017,
doi =          {10.1021/acs.cgd.7b00569},
url =          {https://doi.org/10.1021/acs.cgd.7b00569},
eprint =       { http://dx.doi.org/10.1021/acs.cgd.7b00569 },
}



org-mode source

Org-mode version = 9.0.7

| categories: | tags: | View Comments

In Python I am used to some simple idioms like this:

print([1, 2, 3] * 2)
print("ab" * 3)


[1, 2, 3, 1, 2, 3] ababab

There is even such fanciness as defining operators for objects, as long as they have the appropriate dunder methods defined:

class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def __str__(self):
return "Point ({}, {})".format(self.x, self.y)

def __mul__(self, a):
return Point(self.x * a, self.y * a)

def __rmul__(self, a):
return Point(self.x * a, self.y * a)

p = Point(1, 1)
print(p * 2)
print(3 * p)


Point (2, 2) Point (3, 3)

Out of the box, these things are not possible in elisp. Operators like * in elisp only take numbers or markers. We have a few options to change this. The worst option is to simply redefine these functions. That is bad because it is not reversible. We could define new functions that have the behavior we want, but then we lose the semantic meaning of "*" that we were aiming for. A better option is to advise these functions. This is reversible, because you can later unadvise them. Today we look at some strategies to do this.

We will use "around" advise because it will let us bypass the original intent of the function when we want to, or use it when we do. First, we create a function that will be the advice and add it to the * function. This first draft won't actually change the behavior of *; if all the args are numbers or markers it will simply use the original function as before.

(require 'dash)

(defun *--*-around (orig-fun &rest args)
"if every arg is a number do *, else do something else."
(cond
((-every? (lambda (x) (or (numberp x) (markerp x))) args)
(apply orig-fun args))))



Let's just confirm

(* 1 2 3)

6



Now, we can start modifying our function to handle some other cases. Let's do the list and string first. The * function is variadic, but in these cases it makes sense to limit to two arguments. We need two cases for each type since we can write (* 2 list) or (* list 2). We also should create a fall-through case that raises an error to alert us we can't multiply things.

(defun *--*-around (orig-fun &rest args)
"if every arg is a number do *, else do something else."
(cond
;; The original behavior
((-every? (lambda (x) (or (numberp x) (markerp x))) args)
(apply orig-fun args))

;; create repeated copies of list
((and (listp (first args))
(integerp (second args))
(= 2 (length args)))
(loop for i from 0 below (second args) append (copy-list (first args))))

((and (integerp (first args))
(listp (second args))
(= 2 (length args)))
(loop for i from 0 below (first args) append (copy-list (second args))))

;; Make repeated string
((and (stringp (first args))
(integerp (second args))
(= 2 (length args)))
(loop for i from 0 below (second args) concat (first args)))

((and (integerp (first args))
(stringp (second args))
(= 2 (length args)))
(loop for i from 0 below (first args) concat (second args)))

(t
(error "You cannot * %s" args))))

*--*-around



Here is the new advice in action.

(list
(* '(a b) 2)
(* 2 '(c d))
(* 2 "ab")
(* "cd" 2))

 (a b a b) (c d c d) abab cdcd

That captures the spirit of overloading * for lists and strings. What about that object example? We have to make some assumptions here. Python looks for an uses a dunder mul method. We will assume a double dash method (–mul–) in a similar spirit. We have to modify the advice one final time. We just add a condition to check if one of the arguments is an eieio-object, and then call the –mul– function on the arguments.

(defun *--*-around (orig-fun &rest args)
"if every arg is a number do *, else do something else."
(cond
;; The original behavior
((-every? (lambda (x) (or (numberp x) (markerp x))) args)
(apply orig-fun args))

;; create repeated copies of list
((and (listp (first args))
(integerp (second args))
(= 2 (length args)))
(loop for i from 0 below (second args) append (copy-list (first args))))

((and (integerp (first args))
(listp (second args))
(= 2 (length args)))
(loop for i from 0 below (first args) append (copy-list (second args))))

;; Make repeated string
((and (stringp (first args))
(integerp (second args))
(= 2 (length args)))
(loop for i from 0 below (second args) concat (first args)))

((and (integerp (first args))
(stringp (second args))
(= 2 (length args)))
(loop for i from 0 below (first args) concat (second args)))

;; Handle object
((or (and (eieio-object-p (first args))
(numberp (second args)))
(and (numberp (first args))
(eieio-object-p (second args))))
(apply '--mul-- args))

(t
(error "You cannot * %s" args))))

*--*-around



Now, we can define a class and the –mul– function and show that our overloaded * function works. Note we can define two signatures of –mul– so it is not necessary to define an –rmul– in this case as it was with Python (although we still create two functions in the end).

(require 'eieio)

(defclass Point ()
((x :initarg :x)
(y :initarg :y)))

(cl-defmethod --mul-- ((p Point) a)
(Point :x (* (oref p :x) a) :y (* (oref p :y) a)))

(cl-defmethod --mul-- (a (p Point))
(Point :x (* (oref p :x) a) :y (* (oref p :y) a)))

(cl-defmethod --str-- ((p Point))
(format "Point (%s, %s)" (oref p :x) (oref p :y)))

(let ((P (Point :x 1 :y 1)))
(list
(--str-- (* P 2))
(--str-- (* 3 P))))

 Point (2, 2) Point (3, 3)

That is pretty awesome. Before going on, here is how you remove the advice:

(advice-remove '* '*--*-around)


This example has been pretty instructive. You have to handle overloading for all the intrinsic types. We did lists and strings here; you might also consider vectors. For objects, it looks like we can at least try using a generic method like –mul–. One detail I neglected to consider here is that * is natively variadic. For these special cases, we did not implement variadic versions. This isn't a feature of Python which uses infix notation, so every call is with two arguments. In some cases it might make sense to support variadic args, but that seems like a generally challenging thing to do. While (* "a" 2 3) might be expected to create a string of "aaaaaa", (* "a" 2 '(3)) doesn't make sense at all.

It would be straightforward to extend this to other operators like '+ to concatenate strings, lists and vectors, or '- to remove chars or elements, including extensions to objects using double-dash functions like –add–, –subtract–, etc. Another nice idea might be to advise print to use –str– on objects.

On the surface this looks useful so far. Python defines a lot of dunder methods that cover all kinds of scenarios including logical comparisons, bit shifting, mod, incrementing operators, casting, comparisons, right/left operations, indexing and assignment, length and others. That would be a lot of advices. This approach is moderately tedious to expand though; you have to keep adding conditional cases.

An alternative to the big conditional statement used in the advice might be the use of a generic function. With this approach we define a generic function that just does multiplication by default. Then we define specific cases with specific signatures that are used for lists, strings, objects, etc. That is basically all our conditional above was doing, matching signatures and executing a chunk of code accordingly.

Here is our default case that does the original behavior. We still use advice to apply the function.

(cl-defgeneric generic-multiply (orig-fun &rest args)
"Generic multiply for when no specific case exists."
(apply orig-fun args))

(defun *--*-around-generic (orig-fun &rest args)
(apply 'generic-multiply orig-fun args))



That should just work as usual for regular multiplication.

(* 1 2 3 4)

24



Sure enough it does. Now, we can define a specific method for a string. We need a specialized method for each signature, e.g. pre and post multiplication.

(cl-defmethod generic-multiply ((orig-fun subr) (s string) (n integer))
(loop for i from 0 below n concat s))

(cl-defmethod generic-multiply ((orig-fun subr) (n integer) (s string))
(loop for i from 0 below n concat s))

(list
(* "Ac" 2)


That works fine, and we did not have to modify our original advice function at all! Next the list:

(cl-defmethod generic-multiply ((orig-fun subr) (L list) (n integer))
(loop for i from 0 below n append (copy-list L)))

(cl-defmethod generic-multiply ((orig-fun subr) (n integer) (L list))
(loop for i from 0 below n append (copy-list L)))

(list (* '(1 2) 2)
(* 2 '(3 4)))

 1 2 1 2 3 4 3 4

That also works fine. Last, our class example. This should work on all objects I think (unless there is some way to make classes that do not inherit the default superclass).

(cl-defmethod generic-multiply ((orig-fun subr) (n integer) (obj eieio-default-superclass))
(--mul-- n obj))

(cl-defmethod generic-multiply ((orig-fun subr) (obj eieio-default-superclass) (n integer))
(--mul-- n obj))

(let ((P (Point :x 1 :y 1)))
(list
(--str-- (* P 2))
(--str-- (* 3 P))))

 Point (2, 2) Point (3, 3)

This is a much better approach to extending the multiplication operator! If I continue this path in the future I would probably take this one. This could be useful to make elisp more like some more popular contemporary languages like Python, as well as to add linear algebra like notation or mathematical operations on objects in elisp. It kind of feels like these operations ought to be generic functions to start with to make this kind of overloading easier from the beginning. Functions like "*" are currently defined in the C source code though, maybe for performance reasons. It is not obvious what the consequences of making them generic might be.

Christopher Wellons pointed out an important limitation of advice: they don't work on byte-compiled functions. Let's see what he means. Here is a simple function that will just multiply a Point object by an integer:

(defun to-be-bytten (p1 n)
(* p1 n))

to-be-bytten



Here it is in action, and here it works fine.

(to-be-bytten (Point :x 1 :y 1) 2)

[eieio-class-tag--Point 2 2]



Now, let's byte-compile that function and try it again:

(byte-compile 'to-be-bytten)

(condition-case err
(to-be-bytten (Point :x 1 :y 1) 2)
((error r)
(message "Doh! Christopher was right. It did not work...\n%s" err)))

Doh! Christopher was right. It did not work...
(wrong-type-argument number-or-marker-p [eieio-class-tag--Point 1 1])



So the advice is pretty limited since most of the functions in Emacs core are likely to be byte-compiled, and it might mean you have to redefine * completely, or define some new function that looks like it. Too bad, the advice was pretty easy!