MP-API Write CHGCAR file format

Hello world,

I preface by indicating that I am not a computational scientist, and have a bare minimum Python literacy (enough to be dangerous, I suppose …).

I am hoping to query from mp-api the CHGCAR charge density files for a small number of archetypical structures for educational purposes (i.e., creating my own charge density contour maps, etc.), and am getting stuck figuring out how to properly write the objects in a format suitable for visualization in VESTA.

I am able to query the database using code provided by users on this forum, but cannot use pickle to dump the file to a readable format. Here is my code:

from mp_api.client import MPRester

with MPRester("<my key is here>", monty_decode=False) as mpr:
    chgcar, task_doc = mpr.get_charge_density_from_material_id("mp-149", inc_task_doc=True)
    poscar = task_doc.orig_inputs.poscar
    
import pickle

fileObj = open('CHGCAR_mp-149.vasp', 'wb')
pickle.dump(chgcar,fileObj)
fileObj.close()

The resulting file ‘CHGCAR_mp-149.vasp’ is written to my home directory (3381 kb in present case), but is not able to be read by VESTA or any other visualization software. I suspect the issue is not with VESTA, but rather in the formatting for the object output. My understanding is that pickle simply outputs a single long string, which is probably not what I want.

I cannot seem to find anything in the documentation to guide me as to what to do with the data object sitting in my workspace once it has been pulled via api. Any helpful tips would be most appreciated!

Thanks,
Dave

Hi @dlipke,

You should be able to call the write_file method of the de-serialized CHGCAR object and open the resulting file in VESTA. Something like this:

from mp_api.client import MPRester

with MPRester("your_api_key") as mpr:
    chgcar = mpr.get_charge_density_from_material_id("mp-149")
    chgcar.write_file("mp-149_chgcar.vasp")

– Jason

Jason,

Thank you, this worked perfectly!

If I might follow up, is it possible to obtain partial charge densities via the API at this time? I am interested in charge density difference plots in addition to the total charge densities.

Cheers,
Dave

No problem! Unfortunately, we don’t store the partial charge densities with our calculations right now, and so they are not available through the API.

– Jason