Dear all, I need to calculate a system with a variable number of 2D lattices. So, the pseudo code should look like: for i in xrange(2,100): # Form a system with (i) number of lattices, with hoppings that depend on (i) in between the lattices # Calculate stuff, such as the transmission amplitude from lead f of lattice j to lead g of lattice k (j, k <= i) # plot calculated stuff as a function of i I couldn't figure out a straight method to do it, as I have to pass (i) as a function variable when I'm forming the hoppings. That is, in the make_system part of the code, I need to say things like (again in pseudo code) syst[(lat(i, x, y), lat(i+1, x, y) for x in ... for y in ...)] = hopping(i, i+1) which is impossible because a) lattice definitions can only take in realspace coordinates and b) the function on the right side has to have two sites as its first two arguments and the rest will be provided in the solver part of the code. I found two rather inelegant possible solutions to my problem. (I'm trying only the second one). What I'm asking is, is there a better solution? Inelegant (and possibly wrong) solution 1: Make this a truly 3D rectangular prism system, with each layer corresponding to one lattice. This makes the problem of writing the hoppings trivial, but then how would I get the transmission amplitude from the left lead of 2D lattice number 35 to the right lead of 2D lattice number 67? Inelegant solution 2: Code-generating code. This is what I've done. Essentially, I generate a .py file with 100 copies of the make_system function, each with different number of lattices. Later, in the main code, I import the appropriate make_system function from this .py file and solve my system. Thank you all for your time and concern. Cheers, Baris P.
Hi Baris,
Dear all,
I need to calculate a system with a variable number of 2D lattices. So, the pseudo code should look like:
for i in xrange(2,100): # Form a system with (i) number of lattices, with hoppings that depend on (i) in between the lattices # Calculate stuff, such as the transmission amplitude from lead f of lattice j to lead g of lattice k (j, k <= i) # plot calculated stuff as a function of i
It seems that you could generate some lattices like so: lattices = [kwant.lattice.square(name='lattice_%d' %i) for i in xrange (N)] # intra-lattice hamiltonian for i, lat in enumerate(lattices): sys[(lat(x, y) for x in ... for y in ...)] = onsite(i) sys[lat.neighbors()] = intra_lattice_hopping(i) # inter-lattice hamiltonian for i, (lat_from, lat_to) in enumerate(zip(lattices, lattices[1:])): hoppings = (lat_to(x, y), lat_from(x, y) for x in ... for y in ...) sys[hoppings] = inter_lattice_hopping(i, i+1)
which is impossible because a) lattice definitions can only take in realspace coordinates and b) the function on the right side has to have two sites as its first two arguments and the rest will be provided in the solver part of the code.
We can sidestep this issue by having the "onsite", "intra_lattice_hopping" and "inter_lattice_hopping" functions *return functions themselves*: def onsite(lat_index): def inner_onsite(site_i, *args): return lat_index # depends on "lat_index" return inner_onsite The point is that the inner function can use the value of "lat_index", even though when the inner function actually gets called (inside the Kwant solver, when the Hamiltonian is evaluated) the "lat_index" variable has long gone out of scope. This is a technique called a closure [1]. Hope that answers your question Joe [1]: http://www.shutupandship.com/2012/01/python-closures-explained.html
Also, I've got to ask: why do you want to do this? I'm struggling to think of an application where you would have ~100 separate channels like that that are all separately measureable. This is probably due to a lack of imagination on my part ;) Joe
Thank you very much. :) I'll try this tomorrow and post the results.. It's
definitely neater than mymod = __import__('make_sys_file') and
getattrib(mymod, relevant_make_sys_func) with make_sys_file.py containing
100 different make_sys functions.
The application is about an interaction with a big external spin of value
J, and the number of lattices is essentially 2J+1. I want to be able to see
the effect of J vs. transmission etc.
Baris
On Sun, Sep 27, 2015 at 11:11 PM, Joseph Weston
Also, I've got to ask: why do you want to do this? I'm struggling to think of an application where you would have ~100 separate channels like that that are all separately measureable. This is probably due to a lack of imagination on my part ;)
Joe
Hi Baris,
Sounds like an interesting problem! I suspect that the performance
could be better if you calculate the Green's function or the
scattering matrix of the system at different energies once, and couple
to the spin in a separate stage. This is probably more manual work
though.
Keep in touch about how it goes.
Best,
Anton
On Sun, Sep 27, 2015 at 10:45 PM, Baris Pekerten
Thank you very much. :) I'll try this tomorrow and post the results.. It's definitely neater than mymod = __import__('make_sys_file') and getattrib(mymod, relevant_make_sys_func) with make_sys_file.py containing 100 different make_sys functions.
The application is about an interaction with a big external spin of value J, and the number of lattices is essentially 2J+1. I want to be able to see the effect of J vs. transmission etc.
Baris
On Sun, Sep 27, 2015 at 11:11 PM, Joseph Weston
wrote: Also, I've got to ask: why do you want to do this? I'm struggling to think of an application where you would have ~100 separate channels like that that are all separately measureable. This is probably due to a lack of imagination on my part ;)
Joe
Thank you all for the help! :) The code runs, and it's giving results in
the testing stage.
Baris
On Mon, Sep 28, 2015 at 1:03 AM, Anton Akhmerov
Hi Baris,
Sounds like an interesting problem! I suspect that the performance could be better if you calculate the Green's function or the scattering matrix of the system at different energies once, and couple to the spin in a separate stage. This is probably more manual work though.
Keep in touch about how it goes.
Best, Anton
Thank you very much. :) I'll try this tomorrow and post the results.. It's definitely neater than mymod = __import__('make_sys_file') and getattrib(mymod, relevant_make_sys_func) with make_sys_file.py containing 100 different make_sys functions.
The application is about an interaction with a big external spin of value J, and the number of lattices is essentially 2J+1. I want to be able to see
On Sun, Sep 27, 2015 at 10:45 PM, Baris Pekerten
wrote: the effect of J vs. transmission etc.
Baris
On Sun, Sep 27, 2015 at 11:11 PM, Joseph Weston < joseph.weston08@gmail.com> wrote:
Also, I've got to ask: why do you want to do this? I'm struggling to think of an application where you would have ~100 separate channels like that that are all separately measureable. This is probably due to a lack of imagination on my part ;)
Joe
participants (3)
-
Anton Akhmerov
-
Baris Pekerten
-
Joseph Weston