Skip to content

Commit

Permalink
deploy: af0ee75
Browse files Browse the repository at this point in the history
  • Loading branch information
DPotoyan committed Dec 13, 2024
1 parent 0ed1bbf commit c84f53f
Show file tree
Hide file tree
Showing 95 changed files with 1,861 additions and 3,056 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
175 changes: 174 additions & 1 deletion _sources/ch08/demo_benzene.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,180 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build and Visualize Molecules"
"### Diatomics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from pyscf import gto, scf, tools\n",
"\n",
"def compute_h2_bond_curve(distances, basis='sto-3g'):\n",
" \"\"\"\n",
" Computes the total energy of H2 molecule at various bond distances.\n",
" \n",
" Parameters:\n",
" - distances (array-like): List of H-H bond distances to calculate energy.\n",
" - basis (str): Basis set for the calculation.\n",
" \n",
" Returns:\n",
" - energies (list): Total electronic energies for each bond distance.\n",
" - mo_energies_list (list of lists): Molecular orbital energies for each distance.\n",
" - mo_coeffs_list (list of arrays): Molecular orbital coefficients for each distance.\n",
" \"\"\"\n",
" energies = []\n",
" mo_energies_list = []\n",
" mo_coeffs_list = []\n",
"\n",
" for r in distances:\n",
" mol = gto.Mole()\n",
" mol.atom = f'H 0 0 0; H 0 0 {r}'\n",
" mol.basis = basis\n",
" mol.spin = 0 # Singlet state\n",
" mol.build()\n",
" \n",
" # Perform Hartree-Fock calculation\n",
" mf = scf.RHF(mol)\n",
" total_energy = mf.kernel()\n",
" \n",
" # Store results\n",
" energies.append(total_energy)\n",
" mo_energies_list.append(mf.mo_energy)\n",
" mo_coeffs_list.append(mf.mo_coeff)\n",
" \n",
" print(f\"Bond distance: {r:.2f} Å, Total Energy: {total_energy:.6f} Hartree\")\n",
" \n",
" return energies, mo_energies_list, mo_coeffs_list\n",
"\n",
"def plot_bond_curve(distances, energies):\n",
" \"\"\"\n",
" Plots the bond energy curve of H2 molecule.\n",
" \n",
" Parameters:\n",
" - distances (array-like): H-H bond distances.\n",
" - energies (list): Total electronic energies for each bond distance.\n",
" \"\"\"\n",
" plt.figure(figsize=(8, 5))\n",
" plt.plot(distances, energies, marker='o', label='Total Energy (HF)')\n",
" plt.xlabel('H-H Bond Distance (Å)')\n",
" plt.ylabel('Total Energy (Hartree)')\n",
" plt.title('Bond Curve for H2 (HF/STO-3G)')\n",
" plt.grid(True)\n",
" plt.legend()\n",
" plt.show()\n",
"\n",
"def plot_molecular_orbitals(distances, mo_energies_list):\n",
" \"\"\"\n",
" Plots the molecular orbital energies as a function of bond distance.\n",
" \n",
" Parameters:\n",
" - distances (array-like): H-H bond distances.\n",
" - mo_energies_list (list of lists): Molecular orbital energies for each distance.\n",
" \"\"\"\n",
" plt.figure(figsize=(8, 5))\n",
" for i in range(len(mo_energies_list[0])): # Number of orbitals\n",
" orbital_energies = [mo_energies[i] for mo_energies in mo_energies_list]\n",
" plt.plot(distances, orbital_energies, marker='o', label=f'MO {i+1}')\n",
" \n",
" plt.xlabel('H-H Bond Distance (Å)')\n",
" plt.ylabel('Molecular Orbital Energy (Hartree)')\n",
" plt.title('Molecular Orbital Energies for H2 (HF/STO-3G)')\n",
" plt.grid(True)\n",
" plt.legend()\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define bond distances (in Ångstroms)\n",
"distances = np.linspace(0.5, 3.0, 20) # From 0.5 to 3.0 Å\n",
"\n",
"# Run the bond curve calculation\n",
"energies, mo_energies_list, mo_coeffs_list = compute_h2_bond_curve(distances)\n",
"\n",
"# Plot the bond curve\n",
"plot_bond_curve(distances, energies)\n",
"\n",
"# Plot the molecular orbital energies as a function of bond distance\n",
"plot_molecular_orbitals(distances, mo_energies_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from pyscf import gto, scf, tools\n",
"import py3Dmol # For 3D visualization of orbitals\n",
"\n",
"def visualize_h2_mo_cubes(bond_distance=0.74, basis='sto-3g', mo_index=0, output_cube_file='mo.cube'):\n",
" \"\"\"\n",
" Generate and visualize cube files for molecular orbitals of H2.\n",
" \n",
" Parameters:\n",
" - bond_distance (float): H-H bond distance (in Ångstroms).\n",
" - basis (str): Basis set for the calculation (e.g., 'sto-3g', '6-31g').\n",
" - mo_index (int): Index of the molecular orbital to visualize (0-indexed).\n",
" - output_cube_file (str): The name of the cube file to save.\n",
" \"\"\"\n",
" # 1. Build the H2 molecule\n",
" mol = gto.Mole()\n",
" mol.atom = f'H 0 0 0; H 0 0 {bond_distance}'\n",
" mol.basis = basis\n",
" mol.spin = 0 # Singlet state\n",
" mol.build()\n",
"\n",
" # 2. Perform Hartree-Fock calculation\n",
" mf = scf.RHF(mol)\n",
" mf.kernel()\n",
"\n",
" # 3. Generate cube file for the specified molecular orbital (MO)\n",
" print(f\"Generating cube file for MO {mo_index + 1} (0-indexed as {mo_index})\")\n",
" tools.cubegen.orbital(mol, output_cube_file, mf.mo_coeff[:, mo_index], nx=80, ny=80, nz=80)\n",
"\n",
" # 4. Visualize the molecular orbital using py3Dmol\n",
" print(f\"Visualizing cube file: {output_cube_file}\")\n",
" cube_view = py3Dmol.view(width=400, height=400)\n",
" with open(output_cube_file, 'r') as cube_file:\n",
" cube_data = cube_file.read()\n",
" \n",
" # Add isosurfaces for positive and negative parts of the orbital\n",
" cube_view.addVolumetricData(cube_data, \"cube\", {'isoval': -0.03, 'color': \"red\", 'opacity': 0.85})\n",
" cube_view.addVolumetricData(cube_data, \"cube\", {'isoval': 0.03, 'color': \"blue\", 'opacity': 0.85})\n",
" \n",
" # Add the molecular structure as well\n",
" cube_view.addModel(mol.tostring(format=\"xyz\"), 'xyz')\n",
" cube_view.setStyle({'stick': {}, 'sphere': {'radius': 0.4}})\n",
" cube_view.setBackgroundColor('0xeeeeee')\n",
" cube_view.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Set the bond distance, basis, and the index of the molecular orbital to visualize\n",
"visualize_h2_mo_cubes(bond_distance=0.74, basis='sto-3g', mo_index=1, output_cube_file='mo_1.cube') # MO 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build and Visualize Benzene"
]
},
{
Expand Down
Loading

0 comments on commit c84f53f

Please sign in to comment.