QuantumATK Forum

QuantumATK => Scripts, Tutorials and Applications => Topic started by: Nordland on December 27, 2008, 19:02

Title: Exporting electron density etc to XYZ file for plotting in other software
Post by: Nordland on December 27, 2008, 19:02
Hey everyone.

Alot of people have asked for getting the data of the real space properties like the electron density, effective potential etc
printed to a file. I have always used this little script which does the job.

I have attached the script to this post and the usage is pretty easy.
Quote
from export3d import *
export3D(electron_density)
And whola! you get the following output to your screen
Quote
# -----------------------------------------------------
#   x(bohr)      y(bohr)      z(bohr)      G(1/Bohr**3)
# -----------------------------------------------------
           0            0   0.25031613     0.0086930653
           0            0   0.50063226     0.0077889859
           0            0   0.75094839     0.0068486091
           0            0   1.0012645       0.0059856004
........

Enjoy!
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: serhan on December 27, 2008, 20:41
Dear Nordland,

Thank you very much for this script also ;D. Unfortunately I could not get it work:

Code
from ATK.TwoProbe import *

# Enter the filename of the calculation.
filename = 'D:\Atomistix_works\Tut6\lih2li-scf.nc'
scf = restoreSelfConsistentCalculation(filename)
# Calculate the electron density.
electron_density = calculateElectronDensity(scf)
potential = calculateEffectivePotential(scf)
from export3d import *
export3D(electron_density)

gives:

Quote
Running: C:\Program Files\QuantumWise\Virtual NanoLab 2008.10.0\atk\bin\atk.exe c:/docume~1/atomis~1/locals~1/temp/tmpsqhecq.nl
Traceback (most recent call last):
  File "c:/docume~1/atomis~1/locals~1/temp/tmpsqhecq.nl", line 9, in ?
    from export3d import *
ImportError: No module named export3d
Terminated Abnormally

I'm using ATK 2008.10.0 in Windows.

Serhan
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Nordland on December 27, 2008, 20:58
You must run ATK from the place you placed the script? Are you doing that?

Edit: It appears you are running ATK from inside VNL. Then it is a little tricky to use custom scripts.
However it is doable and this is the way you do it.

1) Create a directory for collecting all the scripts you want to use within ATK - for instance a folder for the good scripts you can find here on the forum.
    On my machine I called it "C:\atk_addons"
2) In all my scripts that I run from within VNL, I have to inset this command which will extend the path of ATK.
Quote
import sys; sys.path.append("C:\atk_addons")
After this command you should be able to write from export3d import *, if the file export3d.py is placed in the C:\atk_addons\ directory.
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: serhan on December 28, 2008, 05:57
Dear Nordland,

Thank you again. The problem is that the script "export3d" was not attached to your first post I think  ;)

This was also happenned to my another post once before, I think it is a problem with the forum's web coding  ???

If you could attach the script "export3d" again, I would be grateful  :D

Regards,

Serhan
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Nordland on December 28, 2008, 09:20
Doh! I will attach it to my first post :)

Infinite stupid I am...  ::)
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: serhan on December 28, 2008, 09:24
Very thanks for the script, Nordland  ;D

Serhan
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: carbn9 on February 19, 2009, 08:01
Dear sirs, I tried to modify the script export3d in order to make it write the z-coordinate first like

z(Bohr)     x(Bohr)     y(Bohr)    Value
--------------------------------------
0               0              0           some value
0               0.00121     0           some value
0               0.00124     0           some value


......

I could not make it work. Could you help me how to modify the script to do this?

Maresh
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Nordland on February 19, 2009, 14:46
Hey !

There is a slightly modifed version with the z-axis first.

Cheers :)

Edit:
This is the correct one :)
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: carbn9 on February 19, 2009, 16:14
Hello Nordland, vey thanks but  I tried the new script but it still gives the x coordinate first :( Can you post it again please ? :)
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Nordland on February 19, 2009, 16:25
Reposted above, in the correct version.
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: carbn9 on February 22, 2009, 20:06
Hi again. Thanks for yur post.
When I extract the real space electron density, I get 126 z-coordinate points. How can I increase this number in simulation, i.e. how can we increase the number of mesh in the z-axis?
Maresh
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Nordland on February 23, 2009, 05:47
If you increase the mesh cutoff, then you will have more points along all axises, so it is the correct way.

I suggest that you converge the system at the normal mesh cutoff, and then make a new calculation that you restart using this density with a higher mesh cutoff.
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: carbn9 on February 23, 2009, 08:05
Thanks.
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: serhan on April 30, 2009, 07:14
Hi Nordland,

The new_export3D exports the coordinates as this sort (if z=[z0,z4], x=[x0, x4], y=[y0, y4] and p is the physical quantity to be extracted):

Code
z0  x0  y0  p000
z1  x0  y0  p100
z2  x0  y0  p200
z3  x0  y0  p300
z4  x0  y0  p400
z0  x1  y0  p010
z1  x1  y0  p110
z2  x1  y0  p210
z3  x1  y0  p310
z4  x1  y0  p410
....................
....................
z3  x4  y4  p344
z4  x4  y4  p444

However, I need to export it as sorted in the z-axis coordinate such as:

Code
z0  x0  y0  p000
z0  x1  y0  p010
z0  x2  y0  p020
z0  x3  y0  p030
z0  x4  y0  p040
z0  x0  y1  p001
z0  x1  y1  p011
z0  x2  y1  p021
z0  x3  y1  p031
z0  x4  y1  p041
....................
....................
z0  x3  y4  p034
z0  x4  y4  p044
z1  x0  y0  p100
z1  x1  y0  p110
....................
....................
z4  x4  y3  p443
z4  x4  y4  p444

I could not success modifying the new_export3D.py to write in this sort. Could you please help?

Cheers,
Serhan
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Anders Blom on April 30, 2009, 09:19
The sort order is actually just controlled by permuting the order of the lines

Code
for ii in range(shape[0]):
    for jj in range(shape[1]):
        for kk in range(shape[2]):

To sort as you wish, the only change needed is to instead write

Code
for kk in range(shape[2]):
    for jj in range(shape[1]):
        for ii in range(shape[0]):

Don't forget to change in two places, for spin-polarized and not!

The rule is simple: the coordinate to sort primarily by, comes first (Z is kk in shape[2]), then the next-level sorting (Y, in your case, jj in shape[1]), and finally the third-level (the coordinate which changes immediately, in your case X, ii in shape[0]).

However, I also note that the print order is a bit different in your example; the script prints Z, Y, X, so if you really want the order Z, X, Y, you also must modify the print statement.
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: serhan on April 30, 2009, 21:56
Thank you very much.

Cheers,
Serhan
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: serhan on July 14, 2009, 21:06
Hi all,

I'm trying to export electron density to file using the script given in this thread. I need to extract electron densities of lots of .nc files.

If the exportED.py is like this
Code
scf=restoreSelfConsistenCalculation('/home/master/atk/1.nc')
electron_density=calculateElectronDensity(scf)
from export3d import *
export3D(electron_density)

And I execute this file as

/home/master/atk/atk /home/master/atk/exportED.py > /home/master/atk/electronDensity1.dat &

then, a file named electrondensity1.dat is successfully created in the directory. The code is excellent. However, I have dozens of .nc files of which the electron densities are to be extracted as:

1.nc -> electronDensity1.dat
2.nc -> electronDensity2.dat
.
.
.

etc. How can I achieve this as a batch mode instead of executing

/home/master/atk/atk /home/master/atk/exportED.py > /home/master/atk/electronDensity1.dat & (when 1.nc is pointed in exportED.py)
/home/master/atk/atk /home/master/atk/exportED.py > /home/master/atk/electronDensity2.dat & (when 2.nc is pointed in exportED.py)
.
.

etc. Any help is greatly appreciated  ;)

Cheers,
Serhan

Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Nordland on July 14, 2009, 22:11
Dear Serhan.

I would do it like this.

Quote
import glob
import sys
from export3d import *
nc_files = glob.glob('*.nc')

for nc_file in nc_files:
    sys.stdout = file('electronDensity'+nc_file[:-3]+'.dat','w')
    scf=restoreSelfConsistenCalculation(nc_file)
    electron_density=calculateElectronDensity(scf)
    export3D(electron_density)
    sys.stdout.close()

Copy this script (call it serhan.py ) into the folder with all the nc-files in, and run it using atk serhan.py in that folder,
and wolla! :) No shell scripts needed :)

Edit: Ups - correct an error in the script.
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Anders Blom on July 14, 2009, 22:19
There are a couple of ways to approach this.

You can handle the many files within the script itself, using the "glob" module. This is a bit more complex, however, so I'd recommend using simple bash looping.

To do this, first we need to modify the code slightly, so that we can pass the name of the NC file on the command line:

Code
import sys
scf=restoreSelfConsistenCalculation(sys.argv[2])

Then, we execute the script as

Quote
for i in *.nc ; do atk exportED.py $i > electronDensity$i.dat ; done

You need to add paths etc. This has the slight disadvantage that the files will be called "electronDensity1.nc.dat". If you can live with that, all ok. If you prefer the filename "electronDensity1.dat", we just need to change it a bit:

Quote
for i in *.nc ; do j=`echo $i | sed 's/.nc//g'` ; atk exportED.py $i > $j.dat ; done
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: serhan on July 16, 2009, 07:27
Thank you very much for your answers. I could do it now.

Cheers,
Serhan
Title: Re: Exporting electron density etc to XYZ file for plotting in other software
Post by: Nordland on July 16, 2009, 13:20
Serhan, I have correct a bug in my script, if you are using my script, look above for the modification.