test_energy_operators.py 28.9 KB
Newer Older
Adel Kara Slimane's avatar
Adel Kara Slimane committed
1
2
3
4
5
6
7
8
9
# -*- coding: utf-8 -*-
# Copyright 2016-2019 tkwant authors.
#
# This file is part of tkwant.  It is subject to the license terms in the file
# LICENSE.rst found in the top-level directory of this distribution and at
# http://kwant-project.org/license.  A list of tkwant authors can be found in
# the file AUTHORS.rst at the top-level directory of this distribution and at
# http://kwant-project.org/authors.

10
11
12
13
import sys
import os

sys.path.insert(0, os.path.abspath('../tkwantoperator'))
Adel Kara Slimane's avatar
Adel Kara Slimane committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

import numpy as np
from numpy.testing import assert_allclose

import kwant
import tkwant
import tkwantoperator

from mpi4py import MPI

# correct currents have been calculated from the code of this branch, at a given commit where the results are known to be correct (through the validation-args branch). The idea is to check that results don't change with future changes.

correct_hamil_density = [-0.32130840181218, -0.321308570961271, -0.321308788139793, -0.321309063862908, -0.321309409755335, -0.321309838127939, -0.321310361223265, -0.321310989998866, -0.321311732281735, -0.321312590085748, -0.321313555837119, -0.321314607200975, -0.321315700146829, -0.321316759833955, -0.321317668842936, -0.321318252231199, -0.321318258854314, -0.32131733837859, -0.321315013423404, -0.321310646324607, -0.321303400116002, -0.321292193498233, -0.321275649818586, -0.321252040436465, -0.321219223312636, -0.321174578248669, -0.321114940926475, -0.321036538761213, -0.320934932582363, -0.32080496928639, -0.320640751837804, -0.320435634297819, -0.320182250879828, -0.319872589300186, -0.319498119824968, -0.319049992304121, -0.318519314012629, -0.317897521148768, -0.317176856227078, -0.316350962199617, -0.315415601798889, -0.314369507189755, -0.313215360441781, -0.311960899523846, -0.310620137468871, -0.309214674116158, -0.307775070551574, -0.306342246265281, -0.304968848468512, -0.303720532397236, -0.302677081327348, -0.301933286073336, -0.30159949665508, -0.301801754356697, -0.302681411335338, -0.304394147993214, -0.307108306131335, -0.31100246892609, -0.31626223725315, -0.323076175785492, -0.331630931236331, -0.342105558355713, -0.354665125691884, -0.369453711200657, -0.386586935717765, -0.406144218059406, -0.428160966929403, -0.452620949761931, -0.479449095183062, -0.508504992358598, -0.539577346053839, -0.572379630358836, -0.606547157073745, -0.64163573784235, -0.677122074202518, -0.712405959476023, -0.746814324213402, -0.779607106716484, -0.809984886517798, -0.837098186700324, -0.860058336296703, -0.877949793044657, -0.889843866288888, -0.894813856401417, -0.891951745453238, -0.880386733450134, -0.859306103574333, -0.827979087137246, -0.785784522671083, -0.732243062984895, -0.667054339759816, -0.590138691847143, -0.501681687048858, -0.402177744171191, -0.292466977178681, -0.173757553747393, -0.047625268906047, 0.084016461236758, 0.218979021580615, 0.354890434847515, 0.489307469818706, 0.619834473527424, 0.744234293211375, 0.860519197482211, 0.967014785678667, 1.06239559511296, 1.14569571532987, 1.21630024868359, 1.27392391869836, 1.31858215781119, 1.35055841968095, 1.37036991358953, 1.37873281098608, 1.37652732078299, 1.36476279559719, 1.34454307120908, 1.31703241073453, 1.28342261159543, 1.24490196760555, 1.20262682861725, 1.15769646170781, 1.11113180380974, 1.06385852759479, 1.01669464446032, 0.970342663065301, 0.925386128013065, 0.882290194711967, 0.841405762381348, 0.802976592030788, 0.767148780756388, 0.733981945698394, 0.703461486046393, 0.675511333975545, 0.650006668914065, 0.626786147682887, 0.605663289550639, 0.586436744590987, 0.568899261093234, 0.55284524928514, 0.538076911369047, 0.524408969851924, 0.511672076257884, 0.499715020215473, 0.488405884941284, 0.477632310156958, 0.467301028734863, 0.457336840387587, 0.447681176155368, 0.438290392989208, 0.429133919995853, 0.42019235838559, 0.411455617133911, 0.402921146897172, 0.394592316634014, 0.38647696126347, 0.37858611489928, 0.37093293290149, 0.363531797178341, 0.356397592719982, 0.34954513902176, 0.342988757568578, 0.336741955581934, 0.330817206451424, 0.325225808370829, 0.319977804393142, 0.315081949165097, 0.310545709799449, 0.306375290536087, 0.302575672916667, 0.299150665075249, 0.296102955385205, 0.293434167082131, 0.291144911605718, 0.289234839286454, 0.287702686671058, 0.286546320263941, 0.285762776792564, 0.28534830031339, 0.28529837659057, 0.285607765226513, 0.286270530022845, 0.287280068018811, 0.288629137604798, 0.290309886050962, 0.292313876731569, 0.294632116268939, 0.297255081769406, 0.300172748278648, 0.303374616545423, 0.30684974115088, 0.310586759034551, 0.314573918427106, 0.318799108183138, 0.323249887493831, 0.327913515948766, 0.332776983907674]

correct_kin_density = [-0.321307896263907, -0.321307896246911, -0.321307896210384, -0.321307896143774, -0.321307896033713, -0.321307895863459, -0.321307895612322, -0.321307895255091, -0.321307894761529, -0.321307894096036, -0.321307893217591, -0.321307892080181, -0.321307890633952, -0.321307888827426, -0.321307886611229, -0.321307883943894, -0.321307880800471, -0.321307877184835, -0.321307873146805, -0.321307868805414, -0.321307864379897, -0.321307860230278, -0.321307856909626, -0.321307855230389, -0.321307856347322, -0.321307861859755, -0.321307873935871, -0.321307895461587, -0.321307930216172, -0.321307983076114, -0.321308060247592, -0.321308169526442, -0.321308320582393, -0.321308525261753, -0.3213087978995, -0.321309155628109, -0.321309618666434, -0.321310210568016, -0.321310958404694, -0.321311892859031, -0.32131304819856, -0.321314462107297, -0.321316175356039, -0.321318231303725, -0.321320675237787, -0.321323553581665, -0.321326913021092, -0.321330799624073, -0.321335258047738, -0.321340330930503, -0.321346058550278, -0.321352478775835, -0.321359627234734, -0.321367537452559, -0.321376240471829, -0.321385763126814, -0.321396123733312, -0.321407323463571, -0.321419331146823, -0.321432058716556, -0.321445324091215, -0.321458798022508, -0.321471931490908, -0.321483860698608, -0.32149328773159, -0.321498336642028, -0.32149638710751, -0.321483890960149, -0.321456180666986, -0.321407283099976, -0.321329756360209, -0.321214571601542, -0.321051065214964, -0.320826988795874, -0.320528684406348, -0.320141410187934, -0.319649835919326, -0.319038719389801, -0.318293762494396, -0.317402631111115, -0.316356105822617, -0.315149312475325, -0.313782963789527, -0.312264527265615, -0.310609221994037, -0.308840739025138, -0.306991577760975, -0.305102895164018, -0.303223776036823, -0.301409851928617, -0.299721224564498, -0.298219688980269, -0.296965304171097, -0.296012426808393, -0.29540540547487, -0.295174222344425, -0.295330451860923, -0.295863959824003, -0.296740766803555, -0.297902428987244, -0.299267146206173, -0.300732611942678, -0.302180412604971, -0.303481607240001, -0.304503007306141, -0.30511364137008, -0.305190922873535, -0.304626118878104, -0.30332882015388, -0.30123021961003, -0.298285106078175, -0.294472569289583, -0.289795488237125, -0.284278938461888, -0.277967703214371, -0.270923107545697, -0.26321941201709, -0.254940003600625, -0.246173606549778, -0.237010707994841, -0.227540355313556, -0.217847439074481, -0.208010530709364, -0.198100301726236, -0.188178514103929, -0.178297541388244, -0.168500357843855, -0.158820918823563, -0.149284848657043, -0.139910351747895, -0.130709266874074, -0.121688192554218, -0.112849621472623, -0.104193033243483, -0.095715906314847, -0.087414620894361, -0.079285234921632, -0.071324124019031, -0.063528483866582, -0.05589669951653, -0.048428590829141, -0.041125546565348, -0.033990561843674, -0.027028194807999, -0.020244458618352, -0.013646664428837, -0.007243230010637, -0.00104346726069, 0.004942639856385, 0.010704657232357, 0.016231914598889, 0.021513705615795, 0.026539469846634, 0.03129895397236, 0.035782350955482, 0.03998041700141, 0.043884567063898, 0.047486950313734, 0.050780507449819, 0.053759012003045, 0.056417097893696, 0.058750275483084, 0.060754938240234, 0.062428361953889, 0.063768698185144, 0.064774963399348, 0.065447024956014, 0.065785584886671, 0.065792162163021, 0.065469073958047, 0.0648194162341, 0.063847043855378, 0.062556550316279, 0.060953247099332, 0.059043142623338, 0.056832920709958, 0.054329918481323, 0.051542103598523, 0.048478050757356, 0.04514691737072, 0.041558418383765, 0.037722800186508, 0.033650813607488, 0.02935368599015, 0.024843092370225, 0.020131125787088, 0.015230266774673, 0.010153352088072, 0.004913542730461, -0.000475708648212, -0.006000690904168, -0.011647468023916, -0.017401913136324, -0.023249742827636, -0.029176551673276, -0.035167846899808]

correct_hamil_current_div = [-1.01444924210513E-09, -2.52120015320249E-09, -4.95918805490744E-09, -8.58436524671999E-09, -1.37054738207235E-08, -2.06882392629721E-08, -2.99567010466883E-08, -4.19897679631894E-08, -5.73103699024178E-08, -7.64636778613191E-08, -9.99797550470636E-08, -1.28314655097282E-07, -1.61762383756864E-07, -2.00328273609894E-07, -2.43552188573605E-07, -2.90267595144726E-07, -3.38279945717343E-07, -3.83945086944395E-07, -4.21625618205977E-07, -4.43000419541404E-07, -4.36200096624728E-07, -3.84739046196015E-07, -2.66213447138313E-07, -5.07339579571798E-08, 3.00937499489674E-07, 8.39575533154577E-07, 1.63002873879674E-06, 2.75412646079118E-06, 4.31396681816533E-06, 6.43558756673135E-06, 9.2730074507153E-06, 1.3012606934592E-05, 1.78777914564892E-05, 2.4133844320448E-05, 3.20928252269542E-05, 4.21182975795752E-05, 5.4629564488368E-05, 7.01049495022768E-05, 8.90834623559682E-05, 0.000112163932137, 0.00014000036381, 0.000173291880756, 0.000212765171765, 0.000259146903185, 0.000313123152595, 0.000375282674304, 0.000446040867647, 0.000525541881126, 0.00061353758427, 0.000709244435674, 0.00081118282768, 0.000917008505635, 0.001023352244238, 0.001125692033494, 0.001218291234328, 0.001294245813491, 0.001345692751507, 0.001364238487634, 0.001341668860468, 0.001270998150848, 0.001147902152581, 0.00097255648711, 0.000751865013138, 0.000502013557641, 0.000251222203762, 4.24978320829012E-05, -6.38874262021069E-05, 1.14606763912186E-05, 0.000367885527307, 0.001124031363508, 0.002415272355954, 0.004388816578213, 0.007196183003025, 0.010982911408706, 0.015875587603761, 0.021966542277368, 0.029296895575135, 0.037838947523912, 0.047479225889653, 0.058003762432194, 0.069087339411464, 0.080288498255783, 0.091052008631322, 0.100720249954133, 0.108554566271778, 0.113767141672928, 0.11556333670281, 0.113193751194424, 0.106014538900849, 0.093553663735228, 0.075579792343731, 0.052169293999326, 0.02376535880175, -0.008778295918139, -0.044177865703304, -0.080732947664774, -0.11638028006721, -0.14879078277424, -0.175506492725819, -0.194106435078858, -0.202383903809096, -0.198514232733772, -0.181193175465763, -0.149731148113678, -0.104096072277464, -0.044905113096221, 0.02662854429915, 0.108784328295861, 0.199447565326192, 0.296236912379836, 0.396636408242387, 0.498123827009453, 0.598288288532816, 0.694931417755406, 0.786147808059142, 0.87038215067096, 0.946462085377917, 1.01360748795445, 1.0714183921922, 1.11984491868985, 1.15914336085239, 1.18982292955778, 1.2125876056147, 1.22827716092944, 1.23781077879255, 1.24213593317078, 1.24218437165219, 1.23883626550982, 1.23289289970631, 1.22505770934988, 1.21592504077303, 1.20597572254599, 1.19557836114627, 1.18499520791758, 1.17439145637603, 1.16384690000615, 1.15336899097895, 1.14290647324916, 1.13236290625657, 1.12160953826306, 1.11049712433092, 1.09886640857513, 1.08655710089336, 1.07341527351609, 1.059299181994, 1.04408357887171, 1.02766263695836, 1.00995163378922, 0.990887570810816, 0.97042891143501, 0.948554622961358, 0.925262700132608, 0.900568334489587, 0.874501875450427, 0.847106707810491, 0.818437147671212, 0.788556436005042, 0.757534887272221, 0.725448230596526, 0.692376163597053, 0.658401124427622, 0.62360727603467, 0.588079688055814, 0.551903695939646, 0.515164413458544, 0.477946373428104, 0.440333271727135, 0.402407791220627, 0.364251484540738, 0.325944697537441, 0.287566518285442, 0.249194739600198, 0.210905825904821, 0.172774877886646, 0.134875590620073, 0.097280202682851, 0.060059435258803, 0.023282421325796, -0.01298337418712, -0.048672241877513, -0.083720321297415, -0.118065698531016, -0.151648506135184, -0.184411023757957, -0.216297777908174, -0.247255639536975, -0.277233918289105, -0.306184452474388, -0.334061693988174, -0.360822787568407, -0.386427643913952, -0.410839006303647, -0.434022510449714, -0.455946737394799, -0.476583259321775, -0.495906678192384]

correct_kin_current_div = [-1.01445319128259E-09, -2.52121328557183E-09, -4.95922220634734E-09, -8.58444262721216E-09, -1.3705633998469E-08, -2.06885494999989E-08, -2.99572712672936E-08, -4.19907712070602E-08, -5.73120678429147E-08, -7.64664500437165E-08, -9.99841259541674E-08, -1.28321307401694E-07, -1.61772136972949E-07, -2.00341993936575E-07, -2.43570582798105E-07, -2.902908309325E-07, -3.38307050313962E-07, -3.83973132306639E-07, -4.216488604505E-07, -4.43009918602645E-07, -4.36185064312915E-07, -3.84692520795383E-07, -2.66147278547778E-07, -5.07121205757563E-08, 3.00731695335501E-07, 8.38712482925688E-07, 1.62760440872937E-06, 2.74836878679049E-06, 4.30157991730852E-06, 6.4106926050304E-06, 9.22548040705943E-06, 1.29255394828147E-05, 1.77237124674533E-05, 2.38692401796724E-05, 3.16503966901905E-05, 4.13963062781936E-05, 5.34775805856453E-05, 6.83053529592897E-05, 8.63282273326133E-05, 0.000108026604522, 0.000133903803664, 0.000164473365491, 0.000200241912606, 0.000241686955527, 0.000289229077947, 0.000343198016148, 0.000403792271047, 0.000471032061503, 0.00054470564733, 0.000624309321631, 0.000708981693682, 0.000797433252773, 0.000887872613731, 0.000977931287239, 0.001064589279778, 0.00114410429304, 0.001211947741914, 0.001262751221605, 0.001290267404003, 0.001287349605489, 0.001245954417102, 0.001157171797871, 0.001011286879783, 0.000797877397047, 0.000505950115692, 0.000124118887941, -0.000359174020455, -0.000955392888997, -0.001675600786352, -0.002530111767923, -0.003528107583716, -0.004677225537347, -0.005983126376367, -0.007449053421047, -0.009075396450141, -0.010859276010944, -0.012794165612993, -0.01486957046277, -0.017070781736246, -0.019378724604562, -0.021769916108719, -0.024216545404246, -0.026686683905359, -0.029144626694335, -0.031551359701498, -0.033865140300793, -0.036042172961037, -0.038037357340146, -0.039805084473027, -0.041300057903277, -0.042478120604382, -0.043297074466306, -0.043717485381993, -0.043703471414351, -0.043223471998233, -0.04225099125063, -0.040765298486792, -0.038752056400901, -0.036203836367643, -0.033120475764628, -0.029509237367581, -0.02538474569167, -0.020768695962529, -0.015689352365682, -0.010180867925865, -0.004282466027684, 0.001962476264138, 0.00850740738836, 0.015303612859963, 0.022301233258534, 0.029450264423208, 0.036701515554083, 0.044007500270951, 0.051323235888698, 0.058606928207195, 0.065820523337656, 0.072930114238584, 0.079906197020434, 0.086723779794398, 0.093362354001137, 0.09980574403268, 0.106041855098745, 0.112062341515802, 0.117862218012282, 0.123439435514745, 0.128794440638155, 0.133929735160007, 0.138849448549236, 0.143558933469468, 0.148064391325846, 0.15237253251696, 0.156490274126773, 0.160424476327348, 0.164181717690904, 0.167768108830968, 0.171189143226163, 0.174449583632491, 0.177553382110993, 0.180503631350106, 0.183302544632097, 0.185951461487105, 0.188450875812545, 0.190800483031014, 0.192999242737001, 0.195045453256906, 0.196936834625686, 0.198670616665914, 0.200243629131607, 0.201652391233658, 0.202893198274846, 0.203962203566682, 0.204855494253847, 0.205569160112571, 0.206099354798193, 0.206442349379538, 0.206594578303756, 0.206552678179462, 0.206313519947402, 0.205874235128923, 0.205232236908766, 0.204385236827378, 0.203331257837723, 0.202068644431292, 0.200596070466524, 0.198912545248113, 0.197017418314771, 0.194910383301871, 0.19259148115835, 0.190061102917909, 0.187319992154797, 0.1843692471956, 0.181210323110736, 0.177845033472395, 0.174275551838669, 0.170504412905433, 0.166534513256828, 0.162369111640607, 0.15801182869484, 0.153466646056202, 0.148737904786346, 0.143830303060635, 0.138748893072151, 0.133499077112741, 0.128086602801555, 0.122517557439749, 0.116798361477604, 0.110935761087184, 0.104936819839753, 0.098808909492592, 0.092559699894611, 0.086197148024297, 0.079729486177272, 0.073165209323967, 0.066513061660879, 0.059782022381557, 0.052981290695915]

correct_heat_current_0 = [0.026963523467902, 0.026963489123036, 0.026963444599775, 0.026963387628649, 0.026963315679508, 0.026963226040991, 0.026963115967171, 0.026962982918644, 0.026962824932883, 0.02696264116749, 0.026962432670134, 0.026962203440195, 0.026961961859317, 0.026961722580716, 0.026961508979568, 0.026961356278259, 0.026961315469398, 0.026961458164892, 0.026961882498977, 0.026962720204664, 0.026964144963886, 0.026966382098682, 0.026969719620632, 0.026974520585116, 0.026981236602212, 0.026990422234043, 0.027002749856432, 0.027019024379097, 0.027040197003018, 0.027067376947522, 0.027101839807215, 0.027145030907247, 0.027198561725578, 0.027264197158078, 0.027343831136761, 0.027439447898321, 0.027553066069769, 0.027686662725191, 0.027842074710386, 0.028020874869734, 0.028224221379124, 0.02845267922107, 0.028706013952117, 0.028982959308506, 0.029280961848216, 0.029595907676808, 0.02992183825207, 0.030250664166099, 0.030571887477502, 0.030872344389362, 0.03113598059775, 0.03134367123206, 0.031473095773295, 0.031498675554833, 0.031391577444478, 0.031119782288034, 0.030648211111065, 0.029938896638471, 0.028951183376271, 0.02764193750053, 0.02596574943146, 0.0238751185196, 0.021320621773371, 0.018251087548403, 0.014613820355074, 0.010354953140418, 0.00542003605128, -0.000244998031285, -0.006692326009214, -0.01397000739684, -0.022118865770708, -0.031168337675549, -0.04113120733271, -0.051997236083974, -0.063725815847888, -0.076237920451023, -0.089407787433087, -0.103054921386051, -0.116937150319265, -0.130745569720554, -0.144102256842217, -0.156561616488553, -0.167616122756484, -0.176707051850986, -0.183240572218338, -0.18660929017851, -0.186219063777108, -0.181520608541037, -0.172045120946435, -0.157442805392709, -0.137522746962403, -0.112291953579511, -0.081990558518515, -0.047119188584131, -0.008453595451047, 0.032958756620206, 0.075824815111189, 0.118657333736818, 0.159850503576008, 0.197774351529666, 0.230878372045986, 0.257792512672636, 0.277413855634999, 0.288970034625269, 0.292054622816395, 0.286634077135558, 0.273029228388572, 0.251876288368808, 0.224073024319763, 0.190715574167224, 0.153030810967239, 0.112308521295665, 0.069837057340029, 0.026845542361115, -0.015544906998257, -0.056359175004961, -0.094794368569823, -0.130234091272055, -0.162251374855398, -0.19060154616933, -0.215206813260917, -0.236134659377398, -0.25357223314777, -0.267798837668222, -0.279158389105849, -0.288033384473084, -0.294821536968139, -0.299915849014314, -0.303688531806447, -0.30647886891261, -0.308584872925655, -0.310258401919157, -0.311703283436173, -0.313075930273866, -0.314487914430864, -0.316009982475264, -0.317677037014437, -0.319493665836762, -0.321439865195127, -0.323476670856887, -0.325551475776492, -0.32760287375348, -0.329564922520826, -0.331370766503406, -0.332955598725272, -0.334258973158584, -0.335226503579941, -0.33581100326745, -0.335973132258726, -0.335681626073505, -0.334913182493914, -0.333652081918891, -0.331889612701531, -0.329623366466037, -0.326856460385243, -0.323596734435582, -0.319855962323542, -0.315649105603017, -0.310993631887974, -0.305908910318917, -0.300415690771748, -0.294535667814179, -0.28829112614281, -0.281704661127129, -0.274798966044533, -0.267596676474885, -0.26012026197515, -0.252391955407899, -0.244433710990537, -0.236267183117058, -0.227913719152195, -0.219394360603194, -0.210729848254667, -0.20194062794762, -0.193046854655797, -0.184068393339928, -0.175024815736454, -0.165935392765895, -0.156819082639178, -0.147694515014421, -0.138579971731054, -0.129493364742194, -0.12045221189859, -0.111473611225152, -0.102574214288382, -0.093770199192118, -0.085077243668957, -0.076510498662707, -0.068084562727972, -0.059813457509626, -0.051710604509222, -0.043788803297897, -0.036060211296001, -0.028536325207878, -0.021227964175051, -0.014145254691508]

correct_heat_current_1 = [-0.184754462209335, -0.184754426297652, -0.184754379238033, -0.184754318493984, -0.184754241213282, -0.184754144303048, -0.184754024574987, -0.184753878990041, -0.184753705039998, -0.184753501313409, -0.184753268304384, -0.184753009535561, -0.184752733080331, -0.184752453584031, -0.1847521948985, -0.184751993458338, -0.184751902539031, -0.184751997545358, -0.184752382480976, -0.184753197744491, -0.184754629380694, -0.184756919884801, -0.184760380608837, -0.184765405749138, -0.184772487798446, -0.18478223422183, -0.184795384959777, -0.184812830172065, -0.184835627411963, -0.184865017162742, -0.184902435380742, -0.18494952137718, -0.185008119043224, -0.185080269091691, -0.185168189669414, -0.185274242405435, -0.185400880723729, -0.185550577089594, -0.18572572580346, -0.185928518033831, -0.186160786023538, -0.1864238138436, -0.186718112741922, -0.187043160078031, -0.187397102091615, -0.187776422367656, -0.188175579883702, -0.188586623006911, -0.18899878879802, -0.18939810051333, -0.189766980285662, -0.190083898577965, -0.190323087036088, -0.190454346618665, -0.190442988020965, -0.190249945948398, -0.189832112072757, -0.189142932686526, -0.188133315178779, -0.186752881428652, -0.184951595005101, -0.182681771789413, -0.179900459746478, -0.17657214303531, -0.172671689160031, -0.168187416994091, -0.163124120825393, -0.157505844635992, -0.151378166122138, -0.144809726632104, -0.137892736716512, -0.13074220263029, -0.123493661385202, -0.116299283833485, -0.109322307569392, -0.102729892077564, -0.096684641990215, -0.09133521121563, -0.086806568024739, -0.083190652748832, -0.080538277436046, -0.078853182428869, -0.078089162363531, -0.078151092173361, -0.078900517434387, -0.080166225135946, -0.081759888961471, -0.083496498323524, -0.085218841533139, -0.086824823452229, -0.088295854501188, -0.0897239540425, -0.091334598962179, -0.093501812603778, -0.096751716860581, -0.101751034589141, -0.109278117352648, -0.120176134461172, -0.135290927244811, -0.155399132979915, -0.181134645466274, -0.212922470509835, -0.250928157040255, -0.295028492462412, -0.344805828245574, -0.399565180187274, -0.458370810436796, -0.520097592123064, -0.583491956552499, -0.64723733029037, -0.710019404656708, -0.770587172628041, -0.827806354518912, -0.880702608361444, -0.928492782270117, -0.970603387206112, -1.00667639185148, -1.03656329086829, -1.0603090993982, -1.078128423777, -1.09037602360223, -1.09751431668554, -1.10008011585767, -1.09865257306167, -1.0938238975704, -1.08617396672555, -1.07624950644416, -1.06454812053962, -1.0515071151587, -1.03749680784881, -1.02281783067525, -1.0077018269957, -0.992314891554736, -0.976763101073727, -0.961099515247736, -0.945332084742438, -0.92943197407234, -0.913341885626438, -0.896984050989399, -0.880267633105857, -0.863095355047275, -0.845369236458618, -0.826995376190301, -0.807887768667155, -0.787971182070124, -0.767183158522554, -0.745475220494263, -0.722813384044065, -0.699178088946604, -0.674563658958237, -0.648977403349397, -0.622438464331084, -0.594976505141786, -0.566630321351223, -0.537446444350119, -0.507477791917609, -0.476782406957343, -0.445422312593863, -0.413462500288787, -0.380970057780068, -0.348013435625919, -0.314661844974795, -0.280984774802522, -0.247051614093197, -0.212931363072252, -0.178692417377921, -0.144402409722456, -0.110128094897289, -0.075935265689614, -0.04188868920558, -0.0080520550771, 0.02551207105822, 0.058742293659175, 0.091578436278206, 0.12396159546696, 0.15583420055243, 0.18714007943441, 0.217824529961755, 0.247834396041693, 0.277118147391648, 0.305625961724657, 0.333309808137171, 0.360123530514588, 0.386022929862023, 0.410965844586535, 0.434912227887486, 0.457824221543, 0.479666225505016, 0.500404962828626, 0.520009539560625, 0.538451499296944, 0.555704872189167, 0.571746218237949, 0.586554664757604, 0.600111937933059, 0.61240238841986]


def make_system():
    # Create the Bravais lattice and the system
    lat = kwant.lattice.chain(1.0, norbs=1)
    builder = kwant.Builder()

    # Define the region of study : add the time-dependent potential
    def qdot_potential(site, time, ε0, Δε):
        return ε0 + Δε * (np.sin(8 * (time - 1.0)) * np.exp(-15 * (time - 1.0)**2) + 0.5 * (1 + np.tanh(10 * (time - 1.0))))

    # lat(0) is the dot, the rest belongs already formally to the leads
    builder[(lat(0))] = qdot_potential

    # Define and attach the leads
    added_cells = [[], []]

    lead = kwant.Builder(kwant.TranslationalSymmetry((-1.,)))
    lead[lat(0)] = 0
    lead[lat.neighbors()] = -1.
    added_cells[0] = builder.attach_lead(lead, add_cells=2)
    added_cells[1] = builder.attach_lead(lead.reversed(), add_cells=2)

    return builder, lat, added_cells


def am_master():
    return MPI.COMM_WORLD.rank == 0


def test_energy_operator(show_progress=False):
    # Create the initial quantum dot system

    # Simulation times
    tmax = 2.  # Duration of the simulation
    dt = 0.01  # Interval between two measures of the operator over the system
    times = np.arange(0, tmax, dt)

    μL = 0.5
    μR = -0.6
    TL = 1
    TR = 0.0
    ε0 = 0.5
    Δε = 2.5

    # Create system
    builder, lat, added_cells = make_system()
    syst = builder.finalized()

    hamil_current_div_op = tkwantoperator.EnergyCurrentDivergence(syst, where=[lat(0)], baseOperator='hamiltonian')
    kin_current_div_op = tkwantoperator.EnergyCurrentDivergence(syst, where=[lat(0)], baseOperator='kinetic+')

    hamil_density_op = tkwantoperator.EnergyDensity(syst, where=[lat(0)], baseOperator='hamiltonian')
    kin_density_op = tkwantoperator.EnergyDensity(syst, where=[lat(0)], baseOperator='kinetic+')

    hamil_source_op = tkwantoperator.EnergySource(syst, where=[lat(0)], baseOperator='hamiltonian')
    kin_source_op = tkwantoperator.EnergySource(syst, where=[lat(0)], baseOperator='kinetic+')

    heat_current_0_op = tkwantoperator.LeadHeatCurrent(syst, chemical_potential=μL, added_lead_sites=added_cells[0])
    heat_current_1_op = tkwantoperator.LeadHeatCurrent(syst, chemical_potential=μR, added_lead_sites=added_cells[1])

    # Occupation, for each lead
    occupation = [None] * len(syst.leads)
    occupation[0] = tkwant.manybody.lead_occupation(chemical_potential=μL, temperature=TL)
    occupation[1] = tkwant.manybody.lead_occupation(chemical_potential=μR, temperature=TR)

    # Initialize the solver
    solver = tkwant.manybody.State(syst, tmax, occupation, params={'ε0': ε0, 'Δε': Δε})

    # Have the system evolve forward in time, calculating the operator over the system
    hamil_current_div = []
    kin_current_div = []

    hamil_density = []
    kin_density = []

    dt_hamil_density = []
    dt_kin_density = []

    hamil_source = []
    kin_source = []

    heat_current_0 = []
    heat_current_1 = []

    for time_index, time in enumerate(times):
        # evolve scattering states in time
        solver.evolve(time)
        solver.refine_intervals()

        hamil_current_div.append(solver.evaluate(hamil_current_div_op))
        kin_current_div.append(solver.evaluate(kin_current_div_op))

        hamil_density.append(solver.evaluate(hamil_density_op))
        kin_density.append(solver.evaluate(kin_density_op))

        hamil_source.append(solver.evaluate(hamil_source_op))
        kin_source.append(solver.evaluate(kin_source_op))

        heat_current_0.append(solver.evaluate(heat_current_0_op))
        heat_current_1.append(solver.evaluate(heat_current_1_op))

        if am_master() and show_progress:
            print("Progress = {:.1f} %".format(time / tmax * 100))

    if am_master():
        for time_index in range(len(times)):
            if time_index < 2 or time_index >= len(times) - 2:
                dt_hamil_density.append(0.)
                dt_kin_density.append(0.)
            else:
                fm2 = hamil_density[time_index - 2]
                fm1 = hamil_density[time_index - 1]
                fp1 = hamil_density[time_index + 1]
                fp2 = hamil_density[time_index + 2]
                dt_hamil_density.append((fm2 - 8 * fm1 + 8 * fp1 - fp2) / (12 * dt))

                fm2 = kin_density[time_index - 2]
                fm1 = kin_density[time_index - 1]
                fp1 = kin_density[time_index + 1]
                fp2 = kin_density[time_index + 2]
                dt_kin_density.append((fm2 - 8 * fm1 + 8 * fp1 - fp2) / (12 * dt))


        n = len(times) - 2

        assert_allclose(hamil_current_div[2:n], correct_hamil_current_div, rtol=1E-4)
        assert_allclose(kin_current_div[2:n], correct_kin_current_div, rtol=1E-4)

        assert_allclose(hamil_density[2:n], correct_hamil_density, rtol=1E-4)
        assert_allclose(kin_density[2:n], correct_kin_density, rtol=1E-4)

        assert_allclose(heat_current_0[2:n], correct_heat_current_0, rtol=1E-4)
        assert_allclose(heat_current_1[2:n], correct_heat_current_1, rtol=1E-4)

        assert_allclose(dt_hamil_density[2:n], [hamil_source[i] - hamil_current_div[i] for i in range(2, len(times) - 2)], rtol=1E-2)
        assert_allclose(dt_kin_density[2:n], [kin_source[i] - kin_current_div[i] for i in range(2, len(times) - 2)], rtol=1E-2)


if __name__ == "__main__":
    test_energy_operator(show_progress=True)