discretize fails for expressions involving exp(f(k_x)) or cos(f(k_x))
I tried to find a way to use code from Mathematica to generate Hamiltonian. The code looks like this:
import kwant
import kwant.continuum as kct
<few more imports>
from sympy.parsing import mathematica as M
import sympy
H11=M.parse_mathematica("<Mathematica code>".replace("*^","*10^(").replace("-10*I","-10)*I").replace("-10*k","-10)*k"))
The replace statements are there just to get rid of certain inaccuracies in parse_mathematica
.
Then I did the following operations:
H11_cont = kct.sympify(str(H11).replace("kx","k_x").replace("ky","k_y").replace("kz","k_z"))
H11_template = kct.discretize(list(H11_cont))
These operations seems to have worked but when I tried to get a few plots using code from this repository made by a colleague (a code used to streamline the process of generating graphs), which is shown below, I got a weird error.
Code:
kx = ("k_x",0)# np.linspace(-np.pi, np.pi, 300))
ky = ("k_y", np.linspace(-np.pi, np.pi, 300))
kz = ("k_z", 0)
params=dict(A=0)
plot_spectrum(syst=H11_template, params=params,kx=kx, ky=ky, kz=kz)
Error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[7], line 6
3 kz = ("k_z", 0)
5 params=dict(A=0)
----> 6 plot_spectrum(syst=H11_template, params=params,kx=kx, ky=ky, kz=kz)
File ~/Workspace/TSM-TBM-Ruihao/utils.py:338, in plot_spectrum(syst, params, kx, ky, kz, ts_dir, L, num_bands, fig_size)
322 """
323 Plot the energy spectrum of a 3D finite/infinite system (with translational symmetry in two directions).
324
(...)
334 - fig: matplotlib figure
335 """
337 if ts_dir is None and L is None:
--> 338 final_syst = kwant.wraparound.wraparound(syst).finalized()
339 fixed_list = []
340 var_list = []
File ~/.conda/envs/kwant/lib/python3.11/site-packages/kwant/wraparound.py:259, in wraparound(builder, keep, coordinate_names)
252 # Store lists of values, so that multiple values can be assigned to the
253 # same site or hopping.
254 for site, val in builder.site_value_pairs():
255 # Every 'site' is in the FD of the original symmetry.
256 # Move the sites to the FD of the remaining symmetry, this guarantees that
257 # every site in the new system is an image of an original FD site translated
258 # purely by the remaining symmetry.
--> 259 sites[ret.symmetry.to_fd(site)] = [bind_site(val) if callable(val) else val]
261 for hop, val in builder.hopping_value_pairs():
262 a, b = hop
File ~/.conda/envs/kwant/lib/python3.11/site-packages/kwant/wraparound.py:47, in _memoize.<locals>.lookup(*args)
45 result = cache.get(key)
46 if result is None:
---> 47 cache[key] = result = f(*args)
48 return result
File ~/.conda/envs/kwant/lib/python3.11/site-packages/kwant/wraparound.py:113, in wraparound.<locals>.bind_site(val)
110 return val(a, *args[:mnp])
112 assert callable(val)
--> 113 _set_signature(f, get_parameters(val) + momenta)
114 return f
File ~/.conda/envs/kwant/lib/python3.11/site-packages/kwant/wraparound.py:65, in _set_signature(func, params)
54 """Set the signature of 'func'.
55
56 Parameters
(...)
61 'POSITIONAL_ONLY' type parameters.
62 """
63 params = [inspect.Parameter(name, inspect.Parameter.POSITIONAL_ONLY)
64 for name in params]
---> 65 func.__signature__ = inspect.Signature(params)
File ~/.conda/envs/kwant/lib/python3.11/inspect.py:3014, in Signature.__init__(self, parameters, return_annotation, __validate_parameters__)
3012 if name in params:
3013 msg = 'duplicate parameter name: {!r}'.format(name)
-> 3014 raise ValueError(msg)
3016 params[name] = param
3017 else:
ValueError: duplicate parameter name: 'k_x'
My colleague said that the error seems to be from the kwant package and most probably occurs because of cos
or exp
functions in sympy
. I found another issue here #392 which seemed similar but is not. I checked following things:
isinstance(kct.sympify(str(M.parse_mathematica("E^(kx)")).replace("kx","k_x")), sympy.Function)
isinstance(kct.sympify(str(M.parse_mathematica("Cos[kx]")).replace("kx","k_x")), sympy.Function)
both returned True
.
Please let me know what I can do to fix this issue.
Thanks a bunch!