Author Topic: Export to MATLAB or Scilab  (Read 4095 times)

0 Members and 1 Guest are viewing this topic.

Offline Heinz

  • Heavy QuantumATK user
  • ***
  • Posts: 51
  • Country: de
  • Reputation: 1
    • View Profile
Export to MATLAB or Scilab
« 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
« Last Edit: February 20, 2015, 15:50 by Heinz »

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5565
  • Country: dk
  • Reputation: 93
    • View Profile
    • QuantumATK at Synopsys
Re: Export to MATLAB or Scilab
« Reply #1 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.

Offline Heinz

  • Heavy QuantumATK user
  • ***
  • Posts: 51
  • Country: de
  • Reputation: 1
    • View Profile
Re: Export to MATLAB or Scilab
« Reply #2 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.
« Last Edit: February 21, 2015, 09:22 by Heinz »

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5565
  • Country: dk
  • Reputation: 93
    • View Profile
    • QuantumATK at Synopsys
Re: Export to MATLAB or Scilab
« Reply #3 on: February 21, 2015, 23:33 »

Offline Heinz

  • Heavy QuantumATK user
  • ***
  • Posts: 51
  • Country: de
  • Reputation: 1
    • View Profile
Re: Export to MATLAB or Scilab
« Reply #4 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

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5565
  • Country: dk
  • Reputation: 93
    • View Profile
    • QuantumATK at Synopsys
Re: Export to MATLAB or Scilab
« Reply #5 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.

Offline Heinz

  • Heavy QuantumATK user
  • ***
  • Posts: 51
  • Country: de
  • Reputation: 1
    • View Profile
Re: Export to MATLAB or Scilab
« Reply #6 on: February 23, 2015, 08:30 »
Works perfect. Thanks a lot.

Heinz