{"id":871,"date":"2018-10-23T22:46:24","date_gmt":"2018-10-23T22:46:24","guid":{"rendered":"http:\/\/second-tech.com\/wordpress\/?p=871"},"modified":"2020-01-08T22:57:02","modified_gmt":"2020-01-08T22:57:02","slug":"using-tbtk-to-calculate-the-density-of-states-dos-of-a-1d-2d-and-3d-square-lattice","status":"publish","type":"post","link":"http:\/\/second-tech.com\/wordpress\/index.php\/2018\/10\/23\/using-tbtk-to-calculate-the-density-of-states-dos-of-a-1d-2d-and-3d-square-lattice\/","title":{"rendered":"Using TBTK to calculate the density of states (DOS) of a 1D, 2D, and 3D square lattice"},"content":{"rendered":"<p><i>Most recent TBTK release at the time of writing: v1.0.2<\/i><br \/>\n<i>Updated to work with: v2.0.0<\/i><\/p>\n<p>In this post we will walk through how to calculate and plot the density of state (DOS) of a 1D, 2D, and 3D square lattice, using <a href=\"http:\/\/second-tech.com\/wordpress\/index.php\/tbtk\/\">TBTK<\/a>. To achieve this we will first implement three functions for creating the corresponding models. We will then create a fourth function in which the DOS is calculated for each of the three models.<\/p>\n<p>This example also provides a simple demonstration of how TBTK naturally support the creation of general purpose code, in this case the calculation of the DOS, that can be used independently of the details of the model.<\/p>\n<div class=\"a-single a-4\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block; text-align:center;\"\r\n     data-ad-layout=\"in-article\"\r\n     data-ad-format=\"fluid\"\r\n     data-ad-client=\"ca-pub-3756520172792312\"\r\n     data-ad-slot=\"9656949358\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div>\n<h2>Creating the Model<\/h2>\n<h3>One dimension<\/h3>\n<p>In one dimension the Hamiltonian is given by<\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 41px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-5b5b743722b34882e80f324001af702c_l3.png\" height=\"41\" width=\"224\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#091; &#72;&#95;&#123;&#49;&#68;&#125;&#32;&#38;&#61;&#32;&#45;&#50;&#116;&#92;&#115;&#117;&#109;&#95;&#123;&#107;&#95;&#120;&#125;&#92;&#99;&#111;&#115;&#40;&#107;&#95;&#120;&#41;&#99;&#95;&#123;&#107;&#95;&#120;&#125;&#94;&#123;&#92;&#100;&#97;&#103;&#103;&#101;&#114;&#125;&#99;&#95;&#123;&#107;&#95;&#120;&#125;&#44; &#92;&#093;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p>where <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-6b97bb0f65c75b6cc0fba1868749478d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#116;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"6\" style=\"vertical-align: 0px;\"\/> is a hopping parameter that we will set to one.<\/p>\n<p>To create the model we loop over the summation index and feed each of the hopping amplitudes to the Model. Note how we let the numerical indices run over the positive discrete values 0 to 9999, while the argument of the cos function falls in the interval <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-80a7d8bc972a86dd439cfc2467235120_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#091;&#45;&#92;&#112;&#105;&#44;&#32;&#92;&#112;&#105;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"49\" style=\"vertical-align: -5px;\"\/>.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nModel createModel1D(){\r\n    \/\/Parameters.\r\n    const int SIZE_X = 10000;\r\n    double t = 1;\r\n\r\n    \/\/Create the Model.\r\n    Model model;\r\n    for(int kx = 0; kx &lt; SIZE_X; kx++){\r\n        double KX = 2*M_PI*kx\/(double)SIZE_X - M_PI;\r\n        model &lt;&lt; HoppingAmplitude(\r\n            -2*t*cos(KX),\r\n            {kx},\r\n            {kx}\r\n        );\r\n    }\r\n\r\n    return model;\r\n}\r\n<\/pre>\n<h3>Two dimensions<\/h3>\n<p>In two dimension the Hamiltonian is given by<\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-699d3420c043827aa34a51af1f716556_l3.png\" height=\"43\" width=\"312\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#091; &#72;&#95;&#123;&#50;&#68;&#125;&#32;&#38;&#61;&#32;&#45;&#50;&#116;&#92;&#115;&#117;&#109;&#95;&#123;&#107;&#95;&#120;&#44;&#107;&#95;&#121;&#125;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#99;&#111;&#115;&#40;&#107;&#95;&#120;&#41;&#32;&#43;&#32;&#92;&#99;&#111;&#115;&#40;&#107;&#95;&#121;&#41;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#99;&#95;&#123;&#92;&#109;&#97;&#116;&#104;&#98;&#102;&#123;&#107;&#125;&#125;&#94;&#123;&#92;&#100;&#97;&#103;&#103;&#101;&#114;&#125;&#99;&#95;&#123;&#92;&#109;&#97;&#116;&#104;&#98;&#102;&#123;&#107;&#125;&#125;&#44; &#92;&#093;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p>where <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-541419bb78e2d2983cdd480c53f72eed_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#109;&#97;&#116;&#104;&#98;&#102;&#123;&#107;&#125;&#32;&#61;&#32;&#40;&#107;&#95;&#120;&#44;&#32;&#107;&#95;&#121;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"90\" style=\"vertical-align: -6px;\"\/>.<\/p>\n<p>The corresponding Model is created as follows using a <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-efcb1a1aaaebb12fc98f08001a645189_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#53;&#48;&#48;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#53;&#48;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"13\" width=\"75\" style=\"vertical-align: 0px;\"\/> grid.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nModel createModel2D(){\r\n    \/\/Parameters.\r\n    const int SIZE_X = 500;\r\n    const int SIZE_Y = 500;\r\n    double t = 1;\r\n\r\n    \/\/Create the Model.\r\n    Model model;\r\n    for(int kx = 0; kx &lt; SIZE_X; kx++){\r\n        for(int ky = 0; ky &lt; SIZE_Y; ky++){\r\n            double KX = 2*M_PI*kx\/(double)SIZE_X;\r\n            double KY = 2*M_PI*ky\/(double)SIZE_Y;\r\n\r\n            model &lt;&lt; HoppingAmplitude(\r\n                -2*t*(cos(KX) + cos(KY)),\r\n                {kx, ky},\r\n                {kx, ky}\r\n            );\r\n        }\r\n    }\r\n\r\n    return model;\r\n}\r\n<\/pre>\n<h3>Three dimensions<\/h3>\n<p>In three dimension the Hamiltonian is given by<\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-8597a453c2ae05fb8a8b9b3331f33d82_l3.png\" height=\"43\" width=\"406\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#091; &#72;&#95;&#123;&#51;&#68;&#125;&#32;&#38;&#61;&#32;&#45;&#50;&#116;&#92;&#115;&#117;&#109;&#95;&#123;&#107;&#95;&#120;&#44;&#107;&#95;&#121;&#44;&#107;&#95;&#122;&#125;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#99;&#111;&#115;&#40;&#107;&#95;&#120;&#41;&#32;&#43;&#32;&#92;&#99;&#111;&#115;&#40;&#107;&#95;&#121;&#41;&#32;&#43;&#32;&#92;&#99;&#111;&#115;&#40;&#107;&#95;&#122;&#41;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#99;&#95;&#123;&#92;&#109;&#97;&#116;&#104;&#98;&#102;&#123;&#107;&#125;&#125;&#94;&#123;&#92;&#100;&#97;&#103;&#103;&#101;&#114;&#125;&#99;&#95;&#123;&#92;&#109;&#97;&#116;&#104;&#98;&#102;&#123;&#107;&#125;&#125;&#44; &#92;&#093;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p>where <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-f3da49391096fdca3a6d2e9a74a7a1de_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#109;&#97;&#116;&#104;&#98;&#102;&#123;&#107;&#125;&#32;&#61;&#32;&#40;&#107;&#95;&#120;&#44;&#32;&#107;&#95;&#121;&#44;&#32;&#107;&#95;&#122;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"115\" style=\"vertical-align: -6px;\"\/>.<\/p>\n<p>The corresponding Model is created as follows using a <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-213983efd64de8389e54bff48bc66774_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#50;&#48;&#48;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#50;&#48;&#48;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#50;&#48;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"124\" style=\"vertical-align: 0px;\"\/> grid.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nModel createModel3D(){\r\n    \/\/Parameters.\r\n    const int SIZE_X = 200;\r\n    const int SIZE_Y = 200;\r\n    const int SIZE_Z = 200;\r\n    double t = 1;\r\n\r\n    \/\/Create the Model.\r\n    Model model;\r\n    for(int kx = 0; kx &lt; SIZE_X; kx++){\r\n        for(int ky = 0; ky &lt; SIZE_Y; ky++){\r\n            for(int ky = 0; ky &lt; SIZE_Y; ky++){\r\n                double KX = 2*M_PI*kx\/(double)SIZE_X;\r\n                double KY = 2*M_PI*ky\/(double)SIZE_Y;\r\n                double KZ = 2*M_PI*kz\/(double)SIZE_Z;\r\n\r\n                model &lt;&lt; HoppingAmplitude(\r\n                    -2*t*(cos(KX) + cos(KY) + cos(KZ)),\r\n                    {kx, ky, kz},\r\n                    {kx, ky, kz}\r\n                );\r\n            }\r\n        }\r\n    }\r\n\r\n    return model;\r\n}\r\n<\/pre>\n<h2>Calculating the DOS<\/h2>\n<p>We are now ready to implement the main function for calculating the DOS. To take advantage of that the Hamiltonian consists of independent blocks for each <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-19fd2b52e090738e98256e3c445e03f8_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#109;&#97;&#116;&#104;&#98;&#102;&#123;&#107;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"11\" style=\"vertical-align: 0px;\"\/> value, we will use the Solver::BlockDiagonalizer. Knowing that the density of states will lie in the interval [-6, 6] for all three Models, we next setup the PropertyExtractor to work on the energy interval [-7, 7] and calculate the DOS. Finally we plot the DOS, using gaussian smoothin with <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/ql-cache\/quicklatex.com-b2f66ffed8a08add0870691ec0e9568d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#105;&#103;&#109;&#97;&#32;&#61;&#32;&#48;&#46;&#48;&#53;\" title=\"Rendered by QuickLaTeX.com\" height=\"13\" width=\"65\" style=\"vertical-align: 0px;\"\/> and a convolution window of 101 energy points and save the results to file.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint main(int argc){\r\n    \/\/Initialize TBTK.\r\n    Initialize();\r\n\r\n    \/\/Filenames to save the figures as.\r\n    string filenames&#x5B;3] = {\r\n        &quot;figures\/DOS_1D.png&quot;,\r\n        &quot;figures\/DOS_2D.png&quot;,\r\n        &quot;figures\/DOS_3D.png&quot;,\r\n    };\r\n\r\n    \/\/Loop over 1D, 2D, and 3D.\r\n    for(int n = 0; n &lt; 3; n++){\r\n        \/\/Create the Model.\r\n        Model model;\r\n        switch(n){\r\n        case 0:\r\n            model = createModel1D();\r\n            break;\r\n        case 1:\r\n            model = createModel2D();\r\n            break;\r\n        case 2:\r\n            model = createModel3D();\r\n            break;\r\n        default:\r\n            Streams::out &lt;&lt; &quot;Error: Invalid case value.\\n&quot;;\r\n            exit(1);\r\n        }\r\n\r\n        \/\/Setup and run the Solver.\r\n        Solver::BlockDiagonalizer solver;\r\n        solver.setModel(model);\r\n        solver.run();\r\n\r\n        \/\/Setup the PropertyExtractor.\r\n        PropertyExtractor::BlockDiagonalizer propertyExtractor(solver);\r\n        propertyExtractor.setEnergyWindow(-7, 7, 1000);\r\n        Property::DOS dos = propertyExtractor.calculateDOS();\r\n\r\n        \/\/Normalize the DOS.\r\n        for(unsigned int c = 0; c &lt; dos.getResolution(); c++)\r\n                dos(c) = dos(c)\/model.getBasisSize();\r\n\r\n        \/\/Smooth the DOS.\r\n        const double SMOOTHING_SIGMA = 0.05;\r\n        const unsigned int SMOOTHING_WINDOW = 101;\r\n        dos = Smooth::gaussian(dos, SMOOTHING_SIGMA, SMOOTHING_WINDOW);\r\n\r\n        \/\/Plot and save the result.\r\n        Plotter plotter;\r\n        plotter.plot(dos);\r\n        plotter.save(filenames&#x5B;n]);\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<h2>Results<\/h2>\n<p>Below we show the result of the calculation for the three different cases.<\/p>\n<h3>One dimensional<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1836 size-full\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_1D-2.png\" alt=\"\" width=\"800\" height=\"600\" srcset=\"http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_1D-2.png 800w, http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_1D-2-300x225.png 300w, http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_1D-2-768x576.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<h3>Two dimensional<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1837 size-full\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_2D-2.png\" alt=\"\" width=\"800\" height=\"600\" srcset=\"http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_2D-2.png 800w, http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_2D-2-300x225.png 300w, http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_2D-2-768x576.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<h3>Three dimensional<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1838 size-full\" src=\"http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_3D-2.png\" alt=\"\" width=\"800\" height=\"600\" srcset=\"http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_3D-2.png 800w, http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_3D-2-300x225.png 300w, http:\/\/second-tech.com\/wordpress\/wp-content\/uploads\/2018\/10\/DOS_3D-2-768x576.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<h2>Full code<\/h2>\n<p>Full code is available in <a href=\"https:\/\/github.com\/dafer45\/SecondTechCode\/blob\/master\/2018_10_23\/src\/main.cpp\">src\/main.cpp<\/a> in the project <a href=\"https:\/\/github.com\/dafer45\/SecondTechCode\/tree\/master\/2018_10_23\">2018_10_23<\/a> of the <a href=\"https:\/\/github.com\/dafer45\/SecondTechCode\">Second Tech code package<\/a>. See the README for instructions on how to build and run.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most recent TBTK release at the time of writing: v1.0.2 Updated to work with: v2.0.0 In this post we will walk through how to calculate and plot the density of state (DOS) of a 1D, 2D, and 3D square lattice, using TBTK. To achieve this we will first implement three functions for creating the corresponding &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/second-tech.com\/wordpress\/index.php\/2018\/10\/23\/using-tbtk-to-calculate-the-density-of-states-dos-of-a-1d-2d-and-3d-square-lattice\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using TBTK to calculate the density of states (DOS) of a 1D, 2D, and 3D square lattice&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/871"}],"collection":[{"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=871"}],"version-history":[{"count":66,"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/871\/revisions"}],"predecessor-version":[{"id":1839,"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/871\/revisions\/1839"}],"wp:attachment":[{"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/second-tech.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}