# How to automatically plot atomic structures with magnetic moments?

I work with magnetic materials and I haven't found a way to automatically plot structures with their corresponding magnetic moments. For this specific project I am using VASP. I am trying to plot something like this:

My POSCAR and OUTCAR files are uploaded to the Matter Modeling SE github page under the folder for question #1780: Here.

• Maybe this will help however I don't know what you mean automatically – Cody Aldaz Jul 28 at 0:15
• Thanks! I'll download VMD and see if it's easier to visualize magnetic moments using that. With the software that I am currently using, you have to define a point and a vector (it's basically a graphing calculator). It looks nice, but it's a lot of work and there's probably a much easier way. – Daniel Maldonado Jul 28 at 0:27
• @DanielMaldonado If you look at Cody's input file for VMD (he gave the input file at the bottom of that answer), then you can see that he gave the coordinates and the modes. Is that something you want to avoid? – Nike Dattani Jul 28 at 20:39
• @NikeDattani This type of format would be ideal, it should be simple to arrange the magnetic moments in this order and paste into the input file. However, I think Tristan's answer might be more thorough because it has the ability to be visualized by multiple different programs, including VMD. – Daniel Maldonado Jul 29 at 20:43

This took some digging, but I can supply at least a partial answer.

Assuming you can copy the list of magnetic moments to a list, you could trick whatever software you are using for visualization into outputting it indirectly. A typical POSCAR can look like the following.

H
1.00000000000000
25 0 0
0 25 0
0 0 25
H
2
Selective dynamics
Cartesian
10 10 10   F   F   F
10 10 11   F   F   F

0  0  0
0  0  0


The last 2 sets of vectors represent a velocity and some? softwares will automatically view and let you plot velocities in the way that you want. Simply copying your magnetic moments to these will allow you to visualize it in anything you want (VMD, ASE, etc).

Added on 7/31/2020 Here is an implementation using ASE to visualize vectors.

from sys import argv
from ase.visualize import view
import numpy as np

file_in = argv[1]

orbital = 4
if len(argv) > 3:
orbital = argv[2]

x, y, z = None, None, None

def process_mag(index):
return_array = []
while "--------" not in text[index]:
return_array.append(float(text[index].split()[orbital]))
index += 1
return return_array

for index in range(len(text)):
if " magnetization (x)" in text[index]:
index += 4
x = process_mag(index)
if " magnetization (y)" in text[index]:
index += 4
y = process_mag(index)
if " magnetization (z)" in text[index]:
index += 4
z = process_mag(index)

directions = zip(x, y, z)

for X, Y, Z in directions:
print("{} {} {}". format(X, Y, Z))

atoms.set_velocities(np.array(directions)/10)
view(atoms)


This can be run with the OUTCAR as the first argument and optionally the magnetization orbital as the second (although it will default to total). Then by clicking "show velocities" under "view" menu, the vectors will be shown. The script will also print the X Y Z data for you to plot in another software.

It seems that most software packages ignore the POSCAR velocities, this is a somewhat annoying consequence of the file format being badly documented. Maybe there is another approach that someone can give since this is a bad visualization for publications.

• The reality is, this isn't a common use case so most people that need to do this probably have to homebrew up something. It would be good to document and provide something for people searching in the future. – Tristan Maxson Jul 29 at 18:23
• One of the nice things about being in the Stack Exchange network (rather than just making our own Q/A site) is that SEO (search engine optimization) is done very well here by the SE developers, so when people search on Google for something, our site will often come up at the top of the list of search results. So your python code could be helpful to many users in the future. If it's a few hundreds lines it can easily fit in a code block here; otherwise I made a Git repository for the input & output files I made when solving the user's problem for question #1715. – Nike Dattani Jul 29 at 18:35
• I will try to do this in the next 24 hours. – Tristan Maxson Jul 29 at 21:52
• This must be a trick with POSCAR. For example, a similar trick can be done with PDBs. The "Occupancy" column of a pdb can be filled with numerical values such as partial charges to plot different atoms with different colors – Cody Aldaz Jul 30 at 3:26
• This was a slight nightmare, I couldnt actually find any software that correctly supported reading velocities and plotting vectors for a POSCAR. I have updated my answer though with what I was able to do. – Tristan Maxson Jul 31 at 15:44