# 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 Jul 28 '20 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. Jul 28 '20 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? Jul 28 '20 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. Jul 29 '20 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.io import read
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))


• 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. Jul 29 '20 at 18:35