{ "cells": [ { "cell_type": "markdown", "id": "834a73d0-8d03-420b-bae3-5e3e844feeb0", "metadata": {}, "source": [ "# Projected Density of States\n", "This notebook will demonstrate use of the PDoS module for computing the Projected Density of States. We will show how it is used in both the cubic and linear scaling mode. After that, we'll do some extra activities related to fragments. Finally, we will cover some current limitations of the O(N) mode." ] }, { "cell_type": "code", "execution_count": 1, "id": "76c269a6-dbb6-4eb9-8f33-f2c795697e08", "metadata": {}, "outputs": [], "source": [ "geom = \"\"\"HETATM 1 O HOH 1 -0.702 -0.056 0.010 1.00 0.00 O \n", "HETATM 2 H HOH 1 -1.022 0.847 -0.011 1.00 0.00 H \n", "HETATM 3 H HOH 1 0.258 0.042 0.005 1.00 0.00 H \n", "HETATM 4 O HOH 2 2.221 0.027 0.001 1.00 0.00 O \n", "HETATM 5 H HOH 2 2.597 -0.412 0.767 1.00 0.00 H \n", "HETATM 6 H HOH 2 2.593 -0.449 -0.745 1.00 0.00 H \"\"\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "d1d88e1a-68af-4f1a-ad9a-326ee57d84fe", "metadata": {}, "outputs": [], "source": [ "from io import StringIO\n", "from BigDFT.IO import read_pdb\n", "with StringIO(geom) as ifile:\n", " sys = read_pdb(ifile)" ] }, { "cell_type": "code", "execution_count": 3, "id": "15703251-f2d1-418a-a46e-ada2f390e4bd", "metadata": {}, "outputs": [], "source": [ "from BigDFT.Calculators import SystemCalculator\n", "code = SystemCalculator(skip=True, verbose=False)" ] }, { "cell_type": "markdown", "id": "722cfc16-1967-4f4f-81e8-b982d67b6d69", "metadata": {}, "source": [ "## Cubic Scaling Mode\n", "First, we will show how to plot the PDoS in the cubic scaling mode." ] }, { "cell_type": "code", "execution_count": 4, "id": "3f3310e4-c135-4fc0-85f4-d54853196706", "metadata": {}, "outputs": [], "source": [ "from BigDFT.Inputfiles import Inputfile\n", "inp = Inputfile()\n", "inp.set_hgrid(0.4)\n", "inp.set_xc(\"PBE\")" ] }, { "cell_type": "markdown", "id": "d0649971-2b05-4d76-9932-3e13ed1eb306", "metadata": {}, "source": [ "We need to activate the PDoS feature. We will also add some extra empty orbitals for later comparison with the linear mode, which automatically has some empty states." ] }, { "cell_type": "code", "execution_count": 5, "id": "f64c0111-cd53-4a23-9ab4-de8bf4f6193d", "metadata": {}, "outputs": [], "source": [ "inp.calculate_pdos()\n", "inp.add_empty_scf_orbitals(4)" ] }, { "cell_type": "code", "execution_count": 6, "id": "4294cf3b-d057-43b7-abe2-e5a96af35289", "metadata": {}, "outputs": [], "source": [ "log = code.run(sys=sys, input=inp, name=\"pdos-C\")" ] }, { "cell_type": "markdown", "id": "96516135-eb7f-4bea-b33b-9cb3a7f346d7", "metadata": {}, "source": [ "Once the calculation has been completed, we access which projections are available." ] }, { "cell_type": "code", "execution_count": 7, "id": "fe183c93-3ee0-433d-bf11-6fa36dcf6bf2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'O': {'s': [0, 6], 'px': [1, 7], 'py': [2, 8], 'pz': [3, 9]}, 'H': {'s': [4, 5, 10, 11]}}\n" ] } ], "source": [ "from BigDFT.PDoS import cubic_projection_info\n", "proj_list = cubic_projection_info(log)\n", "print(proj_list)" ] }, { "cell_type": "markdown", "id": "82ef237f-5d08-40c9-a247-7c37135417ee", "metadata": {}, "source": [ "Then using this information, we can process the `pdos.dat` file to get the weights." ] }, { "cell_type": "code", "execution_count": 8, "id": "23967b7c-7880-4211-8d12-9c7c182e5187", "metadata": {}, "outputs": [], "source": [ "from BigDFT.PDoS import cubic_projection_weights\n", "weights = cubic_projection_weights(log, proj_list)" ] }, { "cell_type": "markdown", "id": "6ce0bdbb-9cc2-48bb-a181-6492ecfee97a", "metadata": {}, "source": [ "Then it is time to create a DoS object to help with the plotting." ] }, { "cell_type": "code", "execution_count": 9, "id": "143a7828-b7b8-4116-8cc8-c8a1160166c2", "metadata": {}, "outputs": [], "source": [ "from BigDFT.DoS import DoS\n", "dos = DoS(energies=log.evals[0][0], units='AU', label=\"Total\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "eb0e1ca7-9b03-42a2-be01-e3bed6bb731e", "metadata": {}, "outputs": [], "source": [ "from numpy import array # Needed since we're hopping over the k points\n", "dos.append(energies=array(log.evals[0]), units='AU',\n", " norm=[[x['H']['s'] for x in weights]], label=\"Hs\")\n", "dos.append(energies=array(log.evals[0]), units='AU',\n", " norm=[[x['O']['s'] for x in weights]], label=\"Os\")\n", "dos.append(energies=array(log.evals[0]), units='AU',\n", " norm=[[x['O']['px'] + x['O']['py'] + x['O']['pz'] for x in weights]], label=\"Op\")" ] }, { "cell_type": "markdown", "id": "c4385e7a-4d7c-4b63-b788-4955b47fa887", "metadata": {}, "source": [ "Add the sum as a sanity check." ] }, { "cell_type": "code", "execution_count": 11, "id": "da35b136-12f9-43e0-9ee4-405904fdf74c", "metadata": {}, "outputs": [], "source": [ "dos.append(energies=array(log.evals[0]), units='AU',\n", " norm=[[x['H']['s'] + \n", " x['O']['s'] + x['O']['px'] + x['O']['py'] + x['O']['pz'] \n", " for x in weights]], label=\"Sum\")" ] }, { "cell_type": "markdown", "id": "441ad191-e755-407a-9898-7f84281a882a", "metadata": {}, "source": [ "Now we are ready to make a picture." ] }, { "cell_type": "code", "execution_count": 12, "id": "54141289-68d9-41fb-83a2-40b8b34911c7", "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "(function(root) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " const force = true;\n", "\n", " if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n", " root._bokeh_onload_callbacks = [];\n", " root._bokeh_is_loading = undefined;\n", " }\n", "\n", "const JS_MIME_TYPE = 'application/javascript';\n", " const HTML_MIME_TYPE = 'text/html';\n", " const EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n", " const CLASS_NAME = 'output_bokeh rendered_html';\n", "\n", " /**\n", " * Render data to the DOM node\n", " */\n", " function render(props, node) {\n", " const script = document.createElement(\"script\");\n", " node.appendChild(script);\n", " }\n", "\n", " /**\n", " * Handle when an output is cleared or removed\n", " */\n", " function handleClearOutput(event, handle) {\n", " function drop(id) {\n", " const view = Bokeh.index.get_by_id(id)\n", " if (view != null) {\n", " view.model.document.clear()\n", " Bokeh.index.delete(view)\n", " }\n", " }\n", "\n", " const cell = handle.cell;\n", "\n", " const id = cell.output_area._bokeh_element_id;\n", " const server_id = cell.output_area._bokeh_server_id;\n", "\n", " // Clean up Bokeh references\n", " if (id != null) {\n", " drop(id)\n", " }\n", "\n", " if (server_id !== undefined) {\n", " // Clean up Bokeh references\n", " const cmd_clean = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n", " cell.notebook.kernel.execute(cmd_clean, {\n", " iopub: {\n", " output: function(msg) {\n", " const id = msg.content.text.trim()\n", " drop(id)\n", " }\n", " }\n", " });\n", " // Destroy server and session\n", " const cmd_destroy = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n", " cell.notebook.kernel.execute(cmd_destroy);\n", " }\n", " }\n", "\n", " /**\n", " * Handle when a new output is added\n", " */\n", " function handleAddOutput(event, handle) {\n", " const output_area = handle.output_area;\n", " const output = handle.output;\n", "\n", " // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n", " if ((output.output_type != \"display_data\") || (!Object.prototype.hasOwnProperty.call(output.data, EXEC_MIME_TYPE))) {\n", " return\n", " }\n", "\n", " const toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n", "\n", " if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n", " toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n", " // store reference to embed id on output_area\n", " output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n", " }\n", " if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n", " const bk_div = document.createElement(\"div\");\n", " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", " const script_attrs = bk_div.children[0].attributes;\n", " for (let i = 0; i < script_attrs.length; i++) {\n", " toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n", " toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\n", " }\n", " // store reference to server id on output_area\n", " output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n", " }\n", " }\n", "\n", " function register_renderer(events, OutputArea) {\n", "\n", " function append_mime(data, metadata, element) {\n", " // create a DOM node to render to\n", " const toinsert = this.create_output_subarea(\n", " metadata,\n", " CLASS_NAME,\n", " EXEC_MIME_TYPE\n", " );\n", " this.keyboard_manager.register_events(toinsert);\n", " // Render to node\n", " const props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n", " render(props, toinsert[toinsert.length - 1]);\n", " element.append(toinsert);\n", " return toinsert\n", " }\n", "\n", " /* Handle when an output is cleared or removed */\n", " events.on('clear_output.CodeCell', handleClearOutput);\n", " events.on('delete.Cell', handleClearOutput);\n", "\n", " /* Handle when a new output is added */\n", " events.on('output_added.OutputArea', handleAddOutput);\n", "\n", " /**\n", " * Register the mime type and append_mime function with output_area\n", " */\n", " OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n", " /* Is output safe? */\n", " safe: true,\n", " /* Index of renderer in `output_area.display_order` */\n", " index: 0\n", " });\n", " }\n", "\n", " // register the mime type if in Jupyter Notebook environment and previously unregistered\n", " if (root.Jupyter !== undefined) {\n", " const events = require('base/js/events');\n", " const OutputArea = require('notebook/js/outputarea').OutputArea;\n", "\n", " if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n", " register_renderer(events, OutputArea);\n", " }\n", " }\n", " if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n", " root._bokeh_timeout = Date.now() + 5000;\n", " root._bokeh_failed_load = false;\n", " }\n", "\n", " const NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"
\\n\"+\n", " \"\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"\\n\"+\n",
" \"\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"
\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"\\n\"+\n \"