QuantumATK Forum

QuantumATK => General Questions and Answers => Topic started by: Heinz on February 20, 2015, 15:47

Title: Export to MATLAB or Scilab
Post by: Heinz on February 20, 2015, 15:47
Hi,

I need to export electron density to MATLAB or Scilab. I searched the forum and found a relevant post: http://quantumwise.com/forum/index.php?topic=39.0
However the code given there doesn't work with ATK 2014 version. How can I do that?

Note: I tried to export as CUBE file and then into MATLAB but MATLAB shows NaN (not a number) errors for that. I attached the beginning of the  CUBE file got from the electron density in ATK.

Best
Heinz
Title: Re: Export to MATLAB or Scilab
Post by: Anders Blom on February 20, 2015, 22:21
In 2014 the cube export is a built-in feature, just use the corresponding LabFloor plugin in the right-hand side panel.
Title: Re: Export to MATLAB or Scilab
Post by: Heinz on February 21, 2015, 05:48
Thanks for your reply. However I need the following format (xyz format) to export:

x      y           z          value  (e.g. electron density)
0     0           1           1E-4
0     0           2           1.1E-4
.      .              .            .
.      .              .            .

The script given in the old post is as follows:

Code
"""
Moedule for export grid data into xyz format.
"""
import numpy

def export3D(grid):
    """
    Function for exporting grid into xyz format.
    """
    # Fetch the cell.
    cell = grid._cell()
    # Fetch the raw numbers.
    data = grid.toArray()
    # Fetch the shape of the data.
    shape = data.shape
    # Get grid samplling vectors.
    # Check the dimension of the grid.
    spin_polarized = (len(shape) == 4)
    # Plot the data to the screen
    if (not spin_polarized):
        i = numpy.array((cell.a().x(),cell.a().y(),cell.a().z()))/shape[0]
        j = numpy.array((cell.b().x(),cell.b().y(),cell.b().z()))/shape[1]
        k = numpy.array((cell.c().x(),cell.c().y(),cell.c().z()))/shape[2]
        print '# -----------------------------------------------------'
        print '#   z(bohr)      y(bohr)      x(bohr)      G(%s)'%(grid.toUnit())
        print '# -----------------------------------------------------'
        for ii in range(shape[0]):
            for jj in range(shape[1]):
                for kk in range(shape[2]):
                    xyz = ii*i + jj*j + kk*k
                    print '%12.8g %12.8g %12.8g %16.8g'%(xyz[2],xyz[1],xyz[0],data[ii,jj,kk])
    else:                   
        i = numpy.array((cell.a().x(),cell.a().y(),cell.a().z()))/shape[1]
        j = numpy.array((cell.b().x(),cell.b().y(),cell.b().z()))/shape[2]
        k = numpy.array((cell.c().x(),cell.c().y(),cell.c().z()))/shape[3]
        print '# -----------------------------------------------------'
        print '#   z(bohr)      y(bohr)      z(bohr)      G(up)(%s)      G(down)(%s)'%(grid.toUnit(),grid.toUnit())
        print '# -----------------------------------------------------'
        for ii in range(shape[1]):
            for jj in range(shape[2]):
                for kk in range(shape[3]):
                    xyz = ii*i + jj*j + kk*k
                    print '%12.8g %12.8g %12.8g %16.8g %16.8g'%(xyz[2],xyz[1],xyz[0],data[0,ii,jj,kk],data[1,ii,jj,kk])


In ATK 2014


 # Fetch the cell.
    cell = grid._cell()

part doesn't work. How can I make a modification to get it work in new version?
Thanks.
Title: Re: Export to MATLAB or Scilab
Post by: Anders Blom on February 21, 2015, 23:33
grid.unitCell() should work (it helps to read the manual ;) )
http://quantumwise.com/documents/manuals/latest/ReferenceManual/index.html/ref.electrondensity.html
Title: Re: Export to MATLAB or Scilab
Post by: Heinz on February 22, 2015, 10:55
Hi,

Thanks but using unitCell() method gives this error:

Quote
Traceback (most recent call last):
  File "c:\users\user\appdata\local\temp\9233111929565636.py", line 20, in <module>
    i = numpy.array((cell.a().x(),cell.a().y(),cell.a().z()))/shape[0]
AttributeError: 'tuple' object has no attribute 'a'

Could you please give another way to do this?
Thank you a lot.
Heinz
Title: Re: Export to MATLAB or Scilab
Post by: Anders Blom on February 22, 2015, 21:59
Very simple modifications needed - instead of y(), just use [1], and so on.
However, the script may fail for noncollinear spin, I didn't test that.
Title: Re: Export to MATLAB or Scilab
Post by: Heinz on February 23, 2015, 08:30
Works perfect. Thanks a lot.

Heinz