QuantumATK Forum

QuantumATK => General Questions and Answers => Topic started by: NasiLemak on April 11, 2011, 07:48

Title: Access to Hamiltonian or Density Matrix
Post by: NasiLemak on April 11, 2011, 07:48
I wonder if there is any method that the users of ATK can access or export Hamiltonian / Density Matrix so that they can do some other analysis of the system.
Hopefully ATK developer can give a positive answer.
Thank you so much.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: Nordland on April 11, 2011, 10:09
I should be able to help you, but first you must give me a little more information.

What kind of configurations is it for?
Title: Re: Access to Hamiltonian or Density Matrix
Post by: NasiLemak on April 12, 2011, 03:33
Thank you so much.
What I want to do is to extract the density matrix or Hamiltonian so that I can calculate some other physical properties.
First, I want to try a bulk configuration, and further more, if possible, I also want to try the scattering region of a device configuration.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: Nordland on April 12, 2011, 12:00
I have cooked up a little code snippet that should get the job done for all your needs.
Code: python
def extractHamiltonianAndOverlap(configuration, kx=0.0, ky=0.0, kz=0.0):
    """
    Routine for getting the hamiltonian and overlap for a given k-point.
    """
    from NL.ComputerScienceUtilities.Functions import realMatrixToNumpy
    # Get the density matrix calculator.
    dmc = configuration.calculator()._densityMatrixCalculator()
    # Get the neighbourlist.
    nb = dmc.neighbourlist()
    # Calculate the phases for this k-point.
    phases = nb.calculatePhases(NLEngine.Cartesian3D(kx,ky,kz))
    # Get a reference for the sparse data structure.
    sparse_H = dmc.hamiltonian()
    sparse_S = dmc.overlap()
    # Perform the fourierTransform to get the dense representation.
    # Change to NLEngine.DOWNDOWN to get spin down.
    spin = NLEngine.UPUP
    denseS = NLEngine.fourierTransform(sparse_S.getSparseCSR(spin), phases)
    denseH = NLEngine.fourierTransform(sparse_H.getSparseCSR(spin), phases)
    # Convert to numpy for easy use in Python.
    real_numpy_S = realMatrixToNumpy(NLEngine.realPart(denseS))
    imag_numpy_S = realMatrixToNumpy(NLEngine.imaginaryPart(denseS))
    real_numpy_H = realMatrixToNumpy(NLEngine.realPart(denseH))
    imag_numpy_H = realMatrixToNumpy(NLEngine.imaginaryPart(denseH))
    # Construct complex numpy.
    H = real_numpy_H + complex(0.0,1.0)*imag_numpy_H
    S = real_numpy_S + complex(0.0,1.0)*imag_numpy_S
    # Return
    return H,S

It is not sure that this exacted snippet will work forever, but it is likely.

An example of the usage:
Code: python
H, S = extractHamiltonianAndOverlap(bulk_configuration, kx=0.0, ky=0.5, kz=0.2)
print H
print S

I hope it helps your forward.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: NasiLemak on April 14, 2011, 05:39
Thank you so much. That is what I want. I will try that later.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: zhangguangping on April 16, 2011, 04:54
I have cooked up a little code snippet that should get the job done for all your needs.
I hope it helps your forward.

Dear Nordland,
From your code. I see, you just move the interactions the unitcell with the neighborings into the unitcell with multiplying the phase,then we have a N*N dimension of H and S,where  N is the dimension of the unit cell. If we want to get the bandstructure, we should construct the H and S like this for every k points and then solve the equation ES{Φ}=H{Φ}? In which {Φ} is expanded in the basis orbits in the unit cell.

Thanks for your answer in advance.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: Nordland on April 16, 2011, 09:15
Exactly. Well spotted!

If you only are after calculating the band-structure, I would recommend that you use the build-in routine for it
since it will be done in native C++ rather than in Python.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: NasiLemak on April 26, 2011, 05:30

I would like to point out one small typo in line 19. This line is for H but not S, thus the "sparse_S" should be "sparse_H"。
After correct this typo, the script can do the extraction of H and S exactly.

Thank you so much!
Title: Re: Access to Hamiltonian or Density Matrix
Post by: Nordland on April 26, 2011, 07:07
Good spotted.

I have updated the script above for future users.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: davalenciah on August 17, 2011, 22:50
thanks for the code, but
when I used its
I got

Hamiltonian =
[[ -1.79177926e-01+0.j   4.31381157e-04+0.j   7.47164661e-04+0.j ...,
    1.58944871e-02+0.j   1.67094370e-06+0.j  -2.75295477e-02+0.j]
 [  0.00000000e+00+0.j   4.49252502e-03+0.j  -9.36283703e-04+0.j ...,
    2.22503996e-05+0.j  -4.14151484e-07+0.j   1.12248995e-05+0.j]
 [  0.00000000e+00+0.j   0.00000000e+00+0.j   3.41157733e-03+0.j ...,
    5.75047611e-07+0.j   9.49960993e-04+0.j  -2.47628152e-06+0.j]
 ...,
 [  0.00000000e+00+0.j   0.00000000e+00+0.j   0.00000000e+00+0.j ...,
    7.31291547e-01+0.j   1.20404864e-06+0.j   4.20901658e-03+0.j]
 [  0.00000000e+00+0.j   0.00000000e+00+0.j   0.00000000e+00+0.j ...,
    0.00000000e+00+0.j   9.79341845e-01+0.j  -1.15453173e-05+0.j]
 [  0.00000000e+00+0.j   0.00000000e+00+0.j   0.00000000e+00+0.j ...,
    0.00000000e+00+0.j   0.00000000e+00+0.j   7.29846714e-01+0.j]]

I would like to get the complete Hamiltonian and overlap not just a part.
How can I get the complete matrix

Thanks
Title: Re: Access to Hamiltonian or Density Matrix
Post by: Anders Blom on August 18, 2011, 04:17
The objects H and S do contain the full Hamiltonian/overlap matrices. It's only when numpy prints them, in the default format, to the terminal that it shortens the output, so you don't get 100,000 lines of text output.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: davalenciah on August 18, 2011, 04:24
thanks

how could I change the default print i numpy to see
all elemnet of matrix H?
Title: Re: Access to Hamiltonian or Density Matrix
Post by: Anders Blom on August 18, 2011, 04:47
Hmm, you should keep in mind this is potentially a very large matrix. I'm not sure that printing it to the screen is very helpful.

You can either do

Code: python
for i in range(X.shape[0]):
    for j in range(X.shape[1]):
        print X[i,j],
    print

or simpler

Code: python
numpy.set_printoptions(threshold='nan')
print X

However in this case you get the braces [ and ] too.

And do pipe the output to a file!
Title: Re: Access to Hamiltonian or Density Matrix
Post by: davalenciah on August 20, 2011, 00:01
Thanks for all replies  ;)

I was wondering if the units of Hamiltonian elements
are eV, Hartree or another one?
Title: Re: Access to Hamiltonian or Density Matrix
Post by: Anders Blom on August 20, 2011, 00:04
All internal quantities in ATK are typically in Hartree and Bohr or combinations thereof.
Title: Re: Access to Hamiltonian or Density Matrix
Post by: davalenciah on August 20, 2011, 00:26
Ok it means if I defined  the mesh cut-off in Rydberg
and the other parameters in Hartree thus
hamiltonian elements have Hartree units
is true that?   ???
Title: Re: Access to Hamiltonian or Density Matrix
Post by: Anders Blom on August 20, 2011, 00:34
Any user-accessible data is always provided as a PhysicalQuantity, where you can "choose" the unit. So when you input the mesh cut-off, you have to give both value and unit, but whether you say 75*Hartree or 150*Rydberg, it's the same. The same for output, if you ask for the total energy, it will be returned as, say, E=50.5*eV, but you can ask for the corresponding value in Hartree by doing E.inUnitsOf(Hartree).

All input quantities are converted to internal units, in which the calculations are performed. These are so-called atomic units, in which the mass of the electron and hbar etc all have the value 1 (see http://en.wikipedia.org/wiki/Atomic_units). This means, that all energies are expressed in Hartree, automatically, and all lengths in Bohr, etc.

We don't design ATK so that users will have access to internal quantities, so you are a bit on your own there. But the Hamiltonian as extracted by the script is in Hartree, completely independently of how you specified the input.