# How to process electron-density data from a CHGCAR for better handling?

We all know that Vesta helps us visualize the electron clouds present in the CHGCAR data, but I am looking for a better way to process the electron-density data we get in a CHGCAR file for compact handling and eventually better understanding.

For example, I have a CHGCAR file on a 64x64x64 grid. But it is a 5 column dataset as shown below:

     64   64   64
0.27296628474E+03 0.27129538645E+03 0.26632236747E+03 0.25816527382E+03 0.24701868651E+03
0.23315320780E+03 0.21691723332E+03 0.19873896341E+03 0.17912393717E+03 0.15864307047E+03
0.13790873569E+03 0.11754046377E+03 0.98125176377E+02 0.80177959210E+02 0.64108387081E+02
0.50195641939E+02 0.38574367354E+02 0.29232778335E+02 0.22024079253E+02 0.16690923309E+02
0.12900228129E+02 0.10283187627E+02 0.84752878195E+01 0.72047216409E+01 0.63226226628E+01
0.57166613639E+01 0.53025284991E+01 0.50199593897E+01 0.48277275324E+01 0.46988015018E+01
0.46162530737E+01 0.45701256193E+01 0.45552760083E+01 0.45701256193E+01 0.46162530737E+01
0.46988015018E+01 0.48277275324E+01 0.50199593897E+01 0.53025284991E+01 0.57166613639E+01
0.63226226628E+01 0.72047216409E+01 0.84752878195E+01 0.10283187627E+02 0.12900228129E+02
0.16690923309E+02 0.22024079253E+02 0.29232778335E+02 0.38574367354E+02 0.50195641939E+02
0.64108387081E+02 0.80177959210E+02 0.98125176377E+02 0.11754046377E+03 0.13790873569E+03
0.15864307047E+03 0.17912393717E+03 0.19873896341E+03 0.21691723332E+03 0.23315320780E+03
0.24701868651E+03 0.25816527382E+03 0.26632236747E+03 0.27129538645E+03 0.27129538645E+03
..................... over a hundred thousand lines of data


I understand that this data is written serially, and as a result there are 5 data per line. If there is a way to parse and convert this data into a 3D Matrix of shape (64,64,64), it could be better handled.

Note: This is related to my prior post What is the format of the electron-density data in a CHGCAR file?, but they are distinct questions. While the initial question was about what format was used for CHGCAR data, this particular question aims to find a post-processing method for this data.

• +1 But could you please put the data in a code block rather than a screenshot? This would help for blind people that are using screen readers, it would help with seachability, readability, reproducability, speed of loading on phones and on browsers with image blockers, etc. Jun 22, 2021 at 19:55
• Thank You for the suggestion. Will include a portion of the data in a codeblock. Jun 22, 2021 at 19:57
• As long as it's fewer than a few thousand lines, it can got in a code block with no problem. Jun 22, 2021 at 20:04
• Does this answer your question? What is the format of the electron-density data in a CHGCAR file? Jun 23, 2021 at 4:34
• No both of these are very different questions. This particular question aims to find a post-processing method for the CHGCAR file. On the other hand,the question that you have mentioned, aims to tell us the method by which the CHGCAR file is written (using Fortran). Jun 23, 2021 at 4:37

I know this was long ago, but for those that find the same problem in the future, the sisl python package might be useful. It has a generic Grid object and it knows how to read the grids from VASP:

import sisl


Then grid is a sisl Grid object, so:

• grid.grid contains the numpy array of values if that's all you need.
• grid.geometry contains the structure to which the grid corresponds (as a sisl Geometry)
• grid has plenty of methods to help you in common manipulations (see docs)
• grid.plot() will allow you to visualize it quickly in python in 1D, 2D and 3D (see docs)

Here's the package documentation and if you have doubts you can ask them on discord.

While its technically more of a direct programming question at this point (since you don't necessarily need to know the source of this data to parse the format), this can be done fairly easily with numpy and pandas.

As a test case, I used a toy version of your data in a file called data.txt:

     11   2   3
0.27296628474E+03 0.27129538645E+03 0.26632236747E+03 0.25816527382E+03 0.24701868651E+03
0.23315320780E+03 0.21691723332E+03 0.19873896341E+03 0.17912393717E+03 0.15864307047E+03
0.13790873569E+03 0.11754046377E+03 0.98125176377E+02 0.80177959210E+02 0.64108387081E+02
0.50195641939E+02 0.38574367354E+02 0.29232778335E+02 0.22024079253E+02 0.16690923309E+02
0.12900228129E+02 0.10283187627E+02 0.84752878195E+01 0.72047216409E+01 0.63226226628E+01
0.57166613639E+01 0.53025284991E+01 0.50199593897E+01 0.48277275324E+01 0.46988015018E+01
0.46162530737E+01 0.45701256193E+01 0.45552760083E+01 0.45701256193E+01 0.46162530737E+01
0.46988015018E+01 0.48277275324E+01 0.50199593897E+01 0.53025284991E+01 0.57166613639E+01
0.63226226628E+01 0.72047216409E+01 0.84752878195E+01 0.10283187627E+02 0.12900228129E+02
0.16690923309E+02 0.22024079253E+02 0.29232778335E+02 0.38574367354E+02 0.50195641939E+02
0.64108387081E+02 0.80177959210E+02 0.98125176377E+02 0.11754046377E+03 0.13790873569E+03
0.15864307047E+03 0.17912393717E+03 0.19873896341E+03 0.21691723332E+03 0.23315320780E+03
0.24701868651E+03 0.25816527382E+03 0.26632236747E+03 0.27129538645E+03 0.27129538645E+03
0.1000000000e+10


I added one extra value to give it a consistent grid dimension and to show how incomplete rows are handled.

import pandas as pd
import numpy as np

#Make a table, elements separated by whitespace,
#Always five columns

#Grab the dimensions, first three elements of the first row
dims=tuple(int(d) for d in in df.iloc[0][:3])

#Find number of missing values, displayed as nan
#remove two for "missing values" in dimension row
nan_count=df.isna().sum().sum()-2

#Convert to 1D numpy array, removing excess nans/dimensions
if nan_count==0:
D=df.to_numpy().flatten()[5:]
else:
D=df.to_numpy().flatten()[5:-nan_count]

#Reshape to 3D array
D=D.reshape(dims)


This will give an $$(11,2,3)$$ numpy array where the dimensions are ordered $$(X,Y,Z)$$.

• Thank You for your answer. Makes a whole lot of sense. Jun 23, 2021 at 17:08