Author Topic: Script error in the tutorial of ‘Bloch states in a graphene nano ribbon’  (Read 14785 times)

0 Members and 1 Guest are viewing this topic.

Offline Greg

  • New QuantumATK user
  • *
  • Posts: 4
  • Reputation: 0
    • View Profile
Dear everyone:
When I used the script below, I found that it didn’t work at all.
………………………………………………………………………..
(n_u,n_d) = nlread('F:/ATK/graphene_nanoribbon/density.nc', ElectronDensity)

r = (n_u - n_d)/(n_u + n_d)
nlsave('F:/ATK/graphene_nanoribbon/diff.nc', r)
………………………………………………………………………..
the error message is:
Traceback (most recent call last):
File "c:\users\zhangsg\appdata\local\temp\2550619019667366.py", line 5, in <module>
r = (n_u - n_d)/(n_u + n_d)
TypeError: unsupported operand type(s) for /: 'ElectronDensity' and 'ElectronDensity'
NanoLanguageScript execution failure

Anyone can help me?
Another script also didn’t work:
………………………………………………………………………..
import numpy
import pylab

# Read coordinates and densities
coords     = nlread('F:/ATK/graphene_nanoribbon/bandstructure.nc', BulkConfiguration)[0]
coords     = coords.cartesianCoordinates().inUnitsOf(Angstrom)[:,1:3]
(n_u, n_d) = nlread('F:/ATK/graphene_nanoribbon/density.nc', ElectronDensity)

# Calculate the polarization density
n = (n_u - n_d)/(n_u + n_d)
n = n[:,:,:]
av = numpy.array(n.sum(axis=0))

# Set the 2D axis
y = numpy.array(range(av.shape[0]))/float(av.shape[0])*19.3063
z = numpy.array(range(av.shape[1]))/float(av.shape[1])*2.46100171044

# A 'spin-dependent colorbar'
cdict = {
    'red':   ((0.0, 0.0, 0.0),
              (0.5, 0.0, 1.0),
              (1.0, 1.0, 1.0)),
    'green': ((0.0, 1.0, 1.0),
              (0.5, 0.0, 0.0),
              (1.0, 1.0, 1.0)),
    'blue':  ((0.0, 0.0, 0.0),
              (0.5, 1.0, 0.0),
              (1.0, 0.0, 0.0))
    }
spin_cmap = pylab.matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)

# Build up the plot
pylab.clf()
pylab.figure(figsize=(5,y[-1]/z[-1]*5.))
pylab.xlabel('z / Angstrom')
pylab.ylabel('y / Angstrom')
pylab.contourf(z,y,av,40,colors='k')
pylab.contourf(z,y,av,40,cmap=spin_cmap)
pylab.plot(coords[:,1],coords[:,0],'ko',ms=15.0)
axis = pylab.axis('image')
v = [axis[0],axis[1],4.0,15.3]
pylab.axis(v)
pylab.colorbar()
pylab.savefig('av.png')
…………………………………………………………………………..

the error message is:
Traceback (most recent call last):
File "c:\users\zhangsg\appdata\local\temp\4538397047235477.py", line 12, in <module>
n = (n_u - n_d)/(n_u + n_d)
TypeError: unsupported operand type(s) for /: 'ElectronDensity' and 'ElectronDensity'
NanoLanguageScript execution failure

Almost the same error as the former script.

What should I do to carry out these scripts successfully? Can you help me? Thank you very much in advance, everyone.
 :'(

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5575
  • Country: dk
  • Reputation: 96
    • View Profile
    • QuantumATK at Synopsys
Seems we need to double-check that tutorial a bit. Thanks for reading and trying it, and for reporting the issues. To work around the issue (a more general solution will be provided later), insert 2 lines after the "nlread" line:
Code
n_up = n_u[:,:,:]
n_dn = n_d[:,:,:]
After that, you should be able to use
Code
r = (n_up - n_dn)/(n_up + n_dn + 1e_19)
(I added a small infinitesimal for safety, to avoid division by zero.) Same solution applies to both scripts. However, it will not be possible to save the computed polarization grid in an NCFile, so the "nlsave" line must be removed; you will have to be content with the picture in the manual, for now :)

Offline Greg

  • New QuantumATK user
  • *
  • Posts: 4
  • Reputation: 0
    • View Profile
Seems we need to double-check that tutorial a bit. Thanks for reading and trying it, and for reporting the issues. To work around the issue (a more general solution will be provided later), insert 2 lines after the "nlread" line:
Code
n_up = n_u[:,:,:]
n_dn = n_d[:,:,:]
After that, you should be able to use
Code
r = (n_up - n_dn)/(n_up + n_dn + 1e_19)
(I added a small infinitesimal for safety, to avoid division by zero.) Same solution applies to both scripts. However, it will not be possible to save the computed polarization grid in an NCFile, so the "nlsave" line must be removed; you will have to be content with the picture in the manual, for now :)
Thank you very much, Mr Anders Blom, i will try them at once.

Offline Greg

  • New QuantumATK user
  • *
  • Posts: 4
  • Reputation: 0
    • View Profile
Seems we need to double-check that tutorial a bit. Thanks for reading and trying it, and for reporting the issues. To work around the issue (a more general solution will be provided later), insert 2 lines after the "nlread" line:
Code
n_up = n_u[:,:,:]
n_dn = n_d[:,:,:]
After that, you should be able to use
Code
r = (n_up - n_dn)/(n_up + n_dn + 1e_19)
(I added a small infinitesimal for safety, to avoid division by zero.) Same solution applies to both scripts. However, it will not be possible to save the computed polarization grid in an NCFile, so the "nlsave" line must be removed; you will have to be content with the picture in the manual, for now :)
:(doesn't work Mr Anders Blom, previous problems have not been disposed at all.
« Last Edit: November 23, 2010, 13:58 by Greg »

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5575
  • Country: dk
  • Reputation: 96
    • View Profile
    • QuantumATK at Synopsys
I'm working on updating the tutorial as we speak. There are other things that need fixing too. You can try the attached script! The first part, about the isosurface, will be a bit messier...

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5575
  • Country: dk
  • Reputation: 96
    • View Profile
    • QuantumATK at Synopsys
Ok, here's how to do the isosurface in 10.8.
Code: python
(n_u,n_d) = nlread('density.nc', ElectronDensity)

n_up = n_u[:,:,:].inUnitsOf(n_u.unit())
n_dn = n_d[:,:,:].inUnitsOf(n_d.unit())
n = (n_up - n_dn)/(n_up + n_dn + 1e-19)

# Overwrite values in n_u with the difference density
import NLEngine
n_u._GridValues__grid3d = NLEngine.RealGrid3D(n_u._GridValues__grid3d.gridDescriptor(), \
                                              NLEngine.doubleSequenceToRealVector(n.flatten()))

# Save it!
nlsave('diff.nc', n_u, object_id="Spin Polarization Density")
This will not be part of the official tutorial, we will temporarily remove it for 10.8, but it will be possible to do it in 11.2 - just like the current tutorial states.

Offline Greg

  • New QuantumATK user
  • *
  • Posts: 4
  • Reputation: 0
    • View Profile
Ok, here's how to do the isosurface in 10.8.
Code: python
(n_u,n_d) = nlread('density.nc', ElectronDensity)

n_up = n_u[:,:,:].inUnitsOf(n_u.unit())
n_dn = n_d[:,:,:].inUnitsOf(n_d.unit())
n = (n_up - n_dn)/(n_up + n_dn + 1e-19)

# Overwrite values in n_u with the difference density
import NLEngine
n_u._GridValues__grid3d = NLEngine.RealGrid3D(n_u._GridValues__grid3d.gridDescriptor(), \
                                              NLEngine.doubleSequenceToRealVector(n.flatten()))

# Save it!
nlsave('diff.nc', n_u, object_id="Spin Polarization Density")
This will not be part of the official tutorial, we will temporarily remove it for 10.8, but it will be possible to do it in 11.2 - just like the current tutorial states.
Dear Mr Anders Blom, the problem has been fixed, thanks for your help very much!