Charge density plot with py4vasp

Question on input files/tags, interpreting output, etc.

Please check whether the answer to your question is given in the VASP online manual or has been discussed in this forum previously!

Moderators: Global Moderator, Moderator

Locked
Message
Author
hatedark1
Newbie
Newbie
Posts: 21
Joined: Fri Mar 24, 2023 1:19 pm

Charge density plot with py4vasp

#1 Post by hatedark1 » Mon May 15, 2023 6:20 pm

Dear fellows,

I followed the procedure described by martin.schlipf in https://w-ww.vasp.at/forum/viewtopic.php?t=18533 to plot the charge density using py4vasp:
from py4vasp import Calculation
import h5py
calc = Calculation.from_path("path_to_your_VASP_calculation")
fig = calc.structure.plot()
with h5py.File(calc.path() / "vaspwave.h5", "r") as h5f:
fig.show_isosurface(h5f["charge/charge"][0], isolevel=0.2)
fig
and it worked very well. However, since the resulting image shows the charge density as an opaque grey surface, it is not very interesting to see (first image below), because the atom is hidden inside. Is there any way to make the surface semi-transparent, so that the atom inside can be seen (similarly to the second image below)? Other than that, is it possible to show multiple isosurfaces in the same plot (similarly to the third image below)?

Best regards,
Renan Lira.

Screenshot 2023-05-15 150552.png
Screenshot 2023-05-15 151209.png
Screenshot 2023-05-15 151205.png
You do not have the required permissions to view the files attached to this post.

martin.schlipf
Global Moderator
Global Moderator
Posts: 495
Joined: Fri Nov 08, 2019 7:18 am

Re: Charge density plot with py4vasp

#2 Post by martin.schlipf » Fri May 19, 2023 1:40 pm

It is not really the intention of py4vasp to provide this kind of advanced plotting functionality, because there are already many tools to that. py4vasp aims to make it easy for you to interface these tools, but for the structure viewer this interface could still be improved. The easiest way at the moment is to access several hidden variables

Code: Select all

from py4vasp import Calculation
import h5py
import mrcfile
import os
import tempfile

calc = Calculation.from_path("path_to_your_VASP_calculation")
fig = calc.structure.plot()
view = fig._ngl
with h5py.File(calc.path() / "vaspwave.h5", "r") as h5f:
    charge = h5f["charge/charge"][0]

with tempfile.TemporaryDirectory() as tmp:
    filename = os.path.join(tmp, "data.mrc")
    with mrcfile.new(filename, overwrite=True) as data_file:
        data_file.set_data(charge.astype(np.float32))
        data_file.header.cella = fig._lengths
        data_file.header.cellb = fig._angles
        
    component = view.add_component(filename, defaultRepresentation=False)
    component.add_surface(isolevel=..., opacity=...)
    
view
If you want several layers, you would repeat the setup of the components. Note that I just took the existing code of py4vasp to setup this example, I did not verify that it works for an actual example.

hatedark1
Newbie
Newbie
Posts: 21
Joined: Fri Mar 24, 2023 1:19 pm

Re: Charge density plot with py4vasp

#3 Post by hatedark1 » Mon Jun 12, 2023 7:59 pm

Sorry for the late response.

Thank you for your reply. I understand your point regarding the complexity of the plot and the availability of other tools for this; however, since I am starting my journey in materials science, being able to use an enclosed environment such as VASP and py4vasp is very efficient.

I will try your suggestion as soon as possible and post the results for other users to view if needed in the future.

Locked