4
$\begingroup$

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.

$\endgroup$
8
  • 2
    $\begingroup$ +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. $\endgroup$ – Nike Dattani Jun 22 at 19:55
  • $\begingroup$ Thank You for the suggestion. Will include a portion of the data in a codeblock. $\endgroup$ – Pranoy Ray Jun 22 at 19:57
  • 1
    $\begingroup$ As long as it's fewer than a few thousand lines, it can got in a code block with no problem. $\endgroup$ – Nike Dattani Jun 22 at 20:04
  • 2
    $\begingroup$ Does this answer your question? What is the format of the electron-density data in a CHGCAR file? $\endgroup$ – Susi Lehtola Jun 23 at 4:34
  • 1
    $\begingroup$ 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). $\endgroup$ – Pranoy Ray Jun 23 at 4:37
3
$\begingroup$

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
df = pd.read_table('data.txt', sep='\s+', names=range(5))

#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)$.

$\endgroup$
1
  • $\begingroup$ Thank You for your answer. Makes a whole lot of sense. $\endgroup$ – Pranoy Ray Jun 23 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.