Problem with coding the band structure of graphene nanoribbons

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Python implementation of a code originally written in Matlab for calculating the band structure of graphene nanoribbons. Participants are exploring potential errors in the code conversion process, focusing on matrix calculations and plotting results.

Discussion Character

  • Technical explanation
  • Debugging
  • Mathematical reasoning

Main Points Raised

  • One participant has successfully implemented a Matlab code for band structure calculations but encounters issues after converting it to Python, leading to incorrect plots.
  • Another participant requests the original Matlab code for reference to identify discrepancies.
  • Discussions include specific lines of code where participants suggest checking for consistency between Matlab and Python, particularly regarding matrix operations and exponent signs.
  • There are suggestions to use debugging tools in Matlab and to inspect individual components of matrices to identify differences in outputs between the two programming languages.
  • Participants discuss the need to iterate correctly through lists in Python, noting differences in indexing between Matlab and Python.
  • One participant realizes that setting a variable inside a loop resets it on each iteration, which affects the storage of eigenvalues in the results matrix.
  • There is a mention of the sorting function applied to the eigenvalues matrix, with one participant noting that commenting it out yields results closer to the expected outcome.
  • Participants express uncertainty about whether the eigenvalue extraction line is functioning correctly and suggest isolating errors methodically.

Areas of Agreement / Disagreement

Participants generally agree on the need to identify and correct discrepancies between the Matlab and Python implementations. However, no consensus is reached on the exact source of the errors, and multiple competing views on debugging strategies and code adjustments remain present.

Contextual Notes

Participants highlight potential issues with variable indexing, matrix calculations, and the effects of sorting eigenvalues, but the discussion does not resolve these uncertainties or provide definitive solutions.

fatemehae6339
Messages
9
Reaction score
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

Technology news on Phys.org
Can you provide the equivalent (correct) Matlab code? It will help to have a reference for comparison.
 
  • Like
Likes   Reactions: fatemehae6339
Yeah, of course. Thank you in advance
 

Attachments

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?
 
You're right, I made a mistake there. I corrected that, but it didn't change the result
 
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.
 
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:
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:
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: 220
  • plot_python.png
    plot_python.png
    36.1 KB · Views: 203
  • #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: 202
  • Like
Likes   Reactions: Master1022

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
Replies
64
Views
10K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K