Problem with coding the band structure of graphene nanoribbons

In summary: Can you provide the equivalent (correct) Matlab code? It will help to have a reference for comparison.Yes, of course. Thank you in advance
  • #1
fatemehae6339
9
1
Hi, I have a Matlab code for calculating the band structure of graphene nanoribbon which is working fine, but I wanted to convert it to python and I've done it. I guess I made a mistake somewhere because the plot is totally wrong. Could someone check it for me, please?
 

Attachments

  • BandStructure.txt
    2.1 KB · Views: 219
Technology news on Phys.org
  • #2
Can you provide the equivalent (correct) Matlab code? It will help to have a reference for comparison.
 
  • Like
Likes fatemehae6339
  • #3
Yeah, of course. Thank you in advance
 

Attachments

  • band_matlab.txt
    1.6 KB · Views: 202
  • #4
In the Python code, you have:
Code:
H_tot=H00+H01*exp(1j*kx*3*a)+H01.T*exp(-1j*kx*3*a)
whereas in Matlab you have:
Code:
 H=Ham+h01'*exp(1j*kx*3*a)+h01*exp(-1j*kx*3*a);

Does H01 = h01? If so, then I think you have used the wrong sign for the exponents (## \pm j ##) in the exponentials?
 
  • #5
You're right, I made a mistake there. I corrected that, but it didn't change the result
 
  • #6
Okay, the next step is to use the debugging tool within the Matlab and set break points at each point within that FOR loop. Does ## d ## in Matlab come out exactly the same as ## Ev ## in Python? If not, then compare (between Python and Matlab) to see whether each of the lines in the FOR loop output a different result. There are only 4 lines of code within that FOR loop so you should be able to determine which, if any, are causing the discrepancies.

If it isn't that, then it will likely be something to do with the plotting code as that is the only bit of code left in Python.
 
  • #7
Obviously not, they're not the same. I think the problem is in the calculating of H_tot. the final matrix is different from the same matrix in Matlab. but I don't know how to correct it.
The procedure is,
First, I must calculate total Hamiltonian, i.e., H_tot for each kx. Then find the eigenvalues of each matrix and put them in each column of a matrix named Ev. Afterward, plot each row in terms of kx values.

and since I am new in python I don't know where I made a mistake
 
Last edited:
  • #8
fatemehae6339 said:
I think the problem is in the calculating of H_tot. the final matrix is different from the same matrix in Matlab.
Which components are different? Did you inspect the individual components within that line to ensure that they are yielding the same values in both languages? There are two matrices (and a transpose) and two constants. Also, in your Python code you aren't iterating through the KX list. You can change the line to:
Code:
for kx in Kpoints:

I am only suggesting this as you have written a note in the Python that you are sure of all the code up until final portion. Therefore, if something has gone wrong it will be at the end.

[EDIT]: I used the Matlab name instead by mistake. Have changed KX to Kpoints
[EDIT #2]: Note, if you do this method, you do NOT need to change the H_tot line as kx will refer to the actual values within the elements. The method BELOW references the INDICES of elements in the array and thus requires H_tot to be changed
 
Last edited:
  • #9
Or if you want to use range in python, note that range(n) goes from 0 to ## n - 1 ## and indexing starts from 0 (unlike Matlab where it starts from 1). Therefore, you could also change the two lines to:
Code:
for i in range (len(Kpoints)):
(example, an array of 4 items will have a length 4 and the above will loop from 0 to 3 which will access all the indices of the elements in the array)

If you are going to use above, then you also need to change your H_tot line to:
Code:
 H_tot=H00+H01*exp(1j*Kpoints[i]*3*a)+H01.T*exp(-1j*Kpoints[i]*3*a)
 
  • #10
Master1022 said:
Which components are different? Did you inspect the individual components within that line to ensure that they are yielding the same values in both languages? There are two matrices (and a transpose) and two constants. Also, in your Python code you aren't iterating through the KX list. You can change the line to:
Code:
for kx in Kpoints:

I am only suggesting this as you have written a note in the Python that you are sure of all the code up until final portion. Therefore, if something has gone wrong it will be at the end.

[EDIT]: I used the Matlab name instead by mistake. Have changed KX to Kpoints
[EDIT #2]: Note, if you do this method, you do NOT need to change the H_tot line as kx will refer to the actual values within the elements. The method BELOW references the INDICES of elements in the array and thus requires H_tot to be changed
 
  • #11
Hello again, and thank you very much
that day I didn't really understand what you mean by kx=Kx. I understood yesterday and changed my code this way:

for kx in Kpoints:
H_tot=H00+H01*exp(-1j*kx*3*a)+H01.T*exp(1j*kx*3*a)
ii=0
Ev[:,ii]=np.linalg.eigvals(H_tot)
Ev=np.sort(Ev)
ii=ii+1

but the result still incorrect
 
  • #12
Master1022 said:
Or if you want to use range in python, note that range(n) goes from 0 to ## n - 1 ## and indexing starts from 0 (unlike Matlab where it starts from 1). Therefore, you could also change the two lines to:
Code:
for i in range (len(Kpoints)):
(example, an array of 4 items will have a length 4 and the above will loop from 0 to 3 which will access all the indices of the elements in the array)

If you are going to use above, then you also need to change your H_tot line to:
Code:
 H_tot=H00+H01*exp(1j*Kpoints[i]*3*a)+H01.T*exp(-1j*Kpoints[i]*3*a)
I tried this way,too:

for i in range(len(Kpoints)):

H_tot=H00+H01*exp(-1j*Kpoints*3*a)+H01.T*exp(1j*Kpoints*3*a)
Ev[:,i]=np.linalg.eigvals(H_tot)
Ev=np.sort(Ev)

and still the same result
 
  • #13
Hi,

I have responded to the code in both methods - feel free to choose either one.

Reference to method #1
fatemehae6339 said:
for kx in Kpoints:
H_tot=H00+H01*exp(-1j*kx*3*a)+H01.T*exp(1j*kx*3*a)
ii=0
Ev[:,ii]=np.linalg.eigvals(H_tot)
Ev=np.sort(Ev)
ii=ii+1

but the result still incorrect

You are accessing the correct values of kx now. This method basically iterates through the elements (i.e. not the indices) of the list. However, within the loop, you have set
Code:
 ii = 0
during each iteration and therefore you will always be accessing Ev[:,0]. I think that definition ought to go outside the loop definition:

Code:
ii=0
for kx in Kpoints:
    H_tot=H00+H01*exp(-1j*kx*3*a)+H01.T*exp(1j*kx*3*a)
    Ev[:,ii]=np.linalg.eigvals(H_tot)
    Ev=np.sort(Ev)
    ii=ii+1

Reference to Method #2
fatemehae6339 said:
I tried this way,too:

for i in range(len(Kpoints)):

H_tot=H00+H01*exp(-1j*Kpoints*3*a)+H01.T*exp(1j*Kpoints*3*a)
Ev[:,i]=np.linalg.eigvals(H_tot)
Ev=np.sort(Ev)

and still the same result

This should instead read:

Code:
for i in range(len(Kpoints)):
    H_tot=H00+H01*exp(-1j*Kpoints[i]*3*a)+H01.T*exp(1j*Kpoints[i]*3*a)
    Ev[:,i]=np.linalg.eigvals(H_tot)
    Ev=np.sort(Ev)

So in this method, we are basically iterating through the indices of the list and store the index in the variable i. Therefore, to access the ## i^{th} ## element, we need to do

Code:
Kpoints[i]

If that still doesn't fix it, it is a good idea to look at each element of the H_tot line and look at what values each variable is taking. One of them will be different that the corresponding values in Matlab and it should help to point the errors out from there. Unfortunately, I don't know much about the physics here so I can't check the equations, but I do know some Python/Matlab. Also, do you need to np.sort the matrix Ev within each iteration of the for loop (as opposed to after the loop)?

Let me know if that didn't make sense.
 
  • #14
Actually, when I commet this line(np.sort...), the result is much more closer to the correct answer, but still incorrect.
 

Attachments

  • plot_matlab.jpg
    plot_matlab.jpg
    44.9 KB · Views: 146
  • plot_python.png
    plot_python.png
    36.1 KB · Views: 126
  • #15
fatemehae6339 said:
Actually, when I commet this line(np.sort...), the result is much more closer to the correct answer, but still incorrect.

Hi,

Is the H_tot line now outputting the same value in both Python and Matlab? If so, then what about the line that grabs the eigenvalues? I think isolating where the error lies in a sequential manner is probably the best thing to do.

In terms of using the sort function, just to confirm: do you want to sort by the real parts of the eigenvalues in ascending order?
 
  • #16
Hi,

Thank you very very much for your help.
I add axis option to the sort command:

Ev=np.sort(Ev,axis=0)

and, the result is now correct.
 

Attachments

  • Figure_1.png
    Figure_1.png
    11.8 KB · Views: 125
  • Like
Likes Master1022

1. What is the band structure of graphene nanoribbons?

The band structure of graphene nanoribbons refers to the energy levels of electrons in the material. It shows the relationship between the energy and momentum of electrons, and can provide information about the material's electrical and optical properties.

2. Why is there a problem with coding the band structure of graphene nanoribbons?

The band structure of graphene nanoribbons is complex and can be difficult to accurately code due to the material's unique properties and the need for precise calculations. Additionally, the band structure can vary depending on the size and shape of the nanoribbon, making it a challenging task.

3. How is the band structure of graphene nanoribbons typically coded?

The band structure of graphene nanoribbons is often coded using advanced computational methods, such as density functional theory (DFT) or tight-binding (TB) models. These methods involve complex mathematical calculations to accurately predict the band structure of the material.

4. What are some potential applications of understanding the band structure of graphene nanoribbons?

Understanding the band structure of graphene nanoribbons can lead to advancements in electronic devices, such as transistors and solar cells, as well as in nanotechnology and materials science. It can also provide valuable insights into the fundamental properties of graphene and other 2D materials.

5. What are some current research efforts to improve the coding of the band structure of graphene nanoribbons?

Scientists are constantly working to improve the accuracy and efficiency of coding the band structure of graphene nanoribbons. This includes developing new computational methods, such as hybrid DFT-TB models, and using high-performance computing to handle the complex calculations. Additionally, experimental techniques are being used to validate and refine theoretical predictions of the band structure.

Similar threads

  • Atomic and Condensed Matter
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Atomic and Condensed Matter
Replies
1
Views
1K
  • Atomic and Condensed Matter
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Atomic and Condensed Matter
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
531
  • Atomic and Condensed Matter
Replies
4
Views
1K
  • Atomic and Condensed Matter
Replies
1
Views
1K
  • Atomic and Condensed Matter
Replies
2
Views
2K
Back
Top