{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basis set optimization with chemtools\n", "\n", "In this tutorial we will go over a few examples illustrating how to use the `chemtools` package to optimize the exponents\n", "of orbital basis sets using various scenarios.\n", "\n", "First some important imports:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from chemtools.basisset import BasisSet\n", "from chemtools.basisopt import BSOptimizer\n", "from chemtools.molecule import Molecule\n", "from chemtools.calculators.molpro import Molpro" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the program that will be used to perform the energy calculations, in this particular case it will be [Molpro](https://www.molpro.net/)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mp = Molpro(exevar=\"MOLPRO_EXE\", runopts=[\"-s\", \"-n\", \"1\", \"-d\", \".\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Optimization of even tempered parameters at the Hartree-Fock level for Be atom\n", "\n", "In the first example we will optimize the *s* exponents for the HF calculations." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "define the system for which the optimization will be performed" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "be = Molecule('Be', atoms=[('Be',)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "template for the input file to be used in sigle point calculations" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "templ = '''***,be\n", "memory,100,m\n", "\n", "%geometry\n", "\n", "%basis\n", "\n", "gthresh,energy=1.0e-9\n", "{rhf; wf,4,1,0}\n", "\n", "'''" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "bso = BSOptimizer(objective='hf total energy', template=templ, code=mp, mol=be,\n", " fsopt={'Be' : [('s', 'et', 8, (0.1, 2.0)),],}, verbose=False)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Script name : /home/lmentel/anaconda3/envs/chemtools/lib/python3.6/site-packages/ipykernel_launcher.py\n", "Workdir : /home/lmentel/anaconda3/envs/chemtools/lib/python3.6/site-packages\n", "Start time : 2017-11-27 14:56:58.315938\n", "================================================================================\n", "=============================STARTING OPTIMIZATION==============================\n", "================================================================================\n", "\n", "======================================CODE======================================\n", "\n", "======================================MOL=======================================\n", "Name: Be Charge: 0 Multiplicty: 1 Electrons: 4 \n", "Atoms:\n", "Element Nuclear Charge\t x y z \n", "Be 4.00\t 0.00000 0.00000 0.00000\n", "=====================================OPTALG=====================================\n", "{'jacob': None,\n", " 'method': 'Nelder-Mead',\n", " 'options': {'disp': True, 'maxiter': 100},\n", " 'tol': 0.0001}Optimization terminated successfully.\n", " Current function value: -14.566522\n", " Iterations: 39\n", " Function evaluations: 73\n", " final_simplex: (array([[ 0.07025538, 3.55536302],\n", " [ 0.07025056, 3.55544753],\n", " [ 0.07025069, 3.55534628]]), array([-14.56652238, -14.56652238, -14.56652238]))\n", " fun: -14.566522375296\n", " message: 'Optimization terminated successfully.'\n", " nfev: 73\n", " nit: 39\n", " status: 0\n", " success: True\n", " x: array([ 0.07025538, 3.55536302])Elapsed time : 18.503 sec" ] } ], "source": [ "bso.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can retrieve the optimized basis directly from the optimized through:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Be': }" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bso.get_basis()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The raw optimization results are also available" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " final_simplex: (array([[ 0.07025538, 3.55536302],\n", " [ 0.07025056, 3.55544753],\n", " [ 0.07025069, 3.55534628]]), array([-14.56652238, -14.56652238, -14.56652238]))\n", " fun: -14.566522375296\n", " message: 'Optimization terminated successfully.'\n", " nfev: 73\n", " nit: 39\n", " status: 0\n", " success: True\n", " x: array([ 0.07025538, 3.55536302])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bso.result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Optimization of diffuse functions for *cc-pvdz* for Be \n", "\n", "Since Be atom doesn't form a stable negative ion diffuse functions for Be are optimized for BeH- " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "beh = Molecule(name=\"BeH-\", atoms=[('Be',), ('H', (0.0, 0.0, 2.724985))], sym=\"cnv 2\", charge=-1, multiplicity=1)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "bsstr = '''basis={\n", "s,Be,2.940000E+03,4.412000E+02,1.005000E+02,2.843000E+01,9.169000E+00,3.196000E+00,1.159000E+00,1.811000E-01,5.890000E-02;\n", "c,1.9,6.800000E-04,5.236000E-03,2.660600E-02,9.999300E-02,2.697020E-01,4.514690E-01,2.950740E-01,1.258700E-02,-3.756000E-03;\n", "c,1.9,-1.230000E-04,-9.660000E-04,-4.831000E-03,-1.931400E-02,-5.328000E-02,-1.207230E-01,-1.334350E-01,5.307670E-01,5.801170E-01;\n", "c,9.9,1.000000E+00;\n", "\n", "p,Be,3.619000E+00,7.110000E-01,1.951000E-01,6.018000E-02;\n", "c,1.4,2.911100E-02,1.693650E-01,5.134580E-01,4.793380E-01;\n", "c,4.4,1.000000E+00;\n", "\n", "d,Be,2.354000E-01;\n", "c,1.1,1.000000E+00;\n", "\n", "s, H , 13.0100000, 1.9620000, 0.4446000, 0.1220000, 0.0297400\n", "c, 1.3, 0.0196850, 0.1379770, 0.4781480\n", "c, 4.4, 1\n", "c, 5.5, 1\n", "p, H , 0.7270000, 0.1410000\n", "c, 1.1, 1\n", "c, 2.2, 1\n", "}\n", "'''\n", "diffusetmp = '''***,be\n", "memory,100,m\n", "\n", "%geometry\n", "\n", "%basis\n", "\n", "%core\n", "\n", "gthresh,energy=1.0e-9\n", "{rhf; wf,6,1,0}\n", "cisd\n", "\n", "'''" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "bsd = BasisSet.from_str(bsstr, fmt='molpro', name='cc-pvdz')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "difffs = {'Be' : [('s', 'exp', 1, (0.02,)), ('p', 'exp', 1, (0.01,)), ('d', 'exp', 1, (0.07,))]}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "bso = BSOptimizer(objective='cisd total energy', template=diffusetmp, code=mp, mol=beh,\n", " fsopt=difffs, staticbs=bsd, core=[1,0,0,0,0,0,0,0], verbose=False)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Script name : /home/lmentel/anaconda3/envs/chemtools/lib/python3.6/site-packages/ipykernel_launcher.py\n", "Workdir : /home/lmentel/anaconda3/envs/chemtools/lib/python3.6/site-packages\n", "Start time : 2017-11-27 15:00:26.887764\n", "================================================================================\n", "=============================STARTING OPTIMIZATION==============================\n", "================================================================================\n", "\n", "======================================CODE======================================\n", "\n", "======================================MOL=======================================\n", "Name: BeH- Charge: -1 Multiplicty: 1 Electrons: 6 \n", "Atoms:\n", "Element Nuclear Charge\t x y z \n", "Be 4.00\t 0.00000 0.00000 0.00000\n", "H 1.00\t 0.00000 0.00000 2.72499\n", "=====================================OPTALG=====================================\n", "{'jacob': None,\n", " 'method': 'Nelder-Mead',\n", " 'options': {'disp': True, 'maxiter': 100},\n", " 'tol': 0.0001}Optimization terminated successfully.\n", " Current function value: -15.158347\n", " Iterations: 71\n", " Function evaluations: 130\n", " final_simplex: (array([[-3.87366099, -1.66449926, -2.56251311],\n", " [-3.87366099, -1.66456835, -2.56251583],\n", " [-3.8736129 , -1.66454853, -2.56246558],\n", " [-3.87374307, -1.66455336, -2.56249475]]), array([-15.15834699, -15.15834699, -15.15834699, -15.15834699]))\n", " fun: -15.158346988389001\n", " message: 'Optimization terminated successfully.'\n", " nfev: 130\n", " nit: 71\n", " status: 0\n", " success: True\n", " x: array([-3.87366099, -1.66449926, -2.56251311])Elapsed time : 47.167 sec" ] } ], "source": [ "bso.run()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Be': , 'H': }" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bso.get_basis()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " final_simplex: (array([[-3.87366099, -1.66449926, -2.56251311],\n", " [-3.87366099, -1.66456835, -2.56251583],\n", " [-3.8736129 , -1.66454853, -2.56246558],\n", " [-3.87374307, -1.66455336, -2.56249475]]), array([-15.15834699, -15.15834699, -15.15834699, -15.15834699]))\n", " fun: -15.158346988389001\n", " message: 'Optimization terminated successfully.'\n", " nfev: 130\n", " nit: 71\n", " status: 0\n", " success: True\n", " x: array([-3.87366099, -1.66449926, -2.56251311])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bso.result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Optimization of tight functions for cc-pvdz Be" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "bsd = BasisSet.from_str(bsstr, fmt='molpro', name='cc-pvdz')\n", "pvdzbe = {k:v for k, v in bsd.items() if k=='Be'}" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "tightfs = {'Be' : [('s', 'exp', 1, (1.8,)), ('p', 'exp', 1, (4.2,))]}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "tighttmp = '''***,be-core\n", "memory,100,m !allocate 500 MW dynamic memory\n", "\n", "%geometry\n", "\n", "%basis\n", "\n", "%core\n", "\n", "{rhf; wf,4,1,0}\n", "cisd\n", "'''" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "bso = BSOptimizer(objective='cisd total energy', template=tighttmp, code=mp, mol=be,\n", " fsopt=tightfs, staticbs=pvdzbe, runcore=True,\n", " core=[[1,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]], verbose=False)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Script name : /home/lmentel/anaconda3/envs/chemtools/lib/python3.6/site-packages/ipykernel_launcher.py\n", "Workdir : /home/lmentel/anaconda3/envs/chemtools/lib/python3.6/site-packages\n", "Start time : 2017-11-27 15:02:52.166124\n", "================================================================================\n", "=============================STARTING OPTIMIZATION==============================\n", "================================================================================\n", "\n", "======================================CODE======================================\n", "\n", "======================================MOL=======================================\n", "Name: Be Charge: 0 Multiplicty: 1 Electrons: 4 \n", "Atoms:\n", "Element Nuclear Charge\t x y z \n", "Be 4.00\t 0.00000 0.00000 0.00000\n", "=====================================OPTALG=====================================\n", "{'jacob': None,\n", " 'method': 'Nelder-Mead',\n", " 'options': {'disp': True, 'maxiter': 100},\n", " 'tol': 0.0001}Optimization terminated successfully.\n", " Current function value: -0.031657\n", " Iterations: 29\n", " Function evaluations: 57\n", " final_simplex: (array([[ 0.62047435, 1.81858876],\n", " [ 0.62049921, 1.81859912],\n", " [ 0.62051376, 1.81852429]]), array([-0.0316572, -0.0316572, -0.0316572]))\n", " fun: -0.031657195978000985\n", " message: 'Optimization terminated successfully.'\n", " nfev: 57\n", " nit: 29\n", " status: 0\n", " success: True\n", " x: array([ 0.62047435, 1.81858876])Elapsed time : 20.818 sec" ] } ], "source": [ "bso.run()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " final_simplex: (array([[ 0.62047435, 1.81858876],\n", " [ 0.62049921, 1.81859912],\n", " [ 0.62051376, 1.81852429]]), array([-0.0316572, -0.0316572, -0.0316572]))\n", " fun: -0.031657195978000985\n", " message: 'Optimization terminated successfully.'\n", " nfev: 57\n", " nit: 29\n", " status: 0\n", " success: True\n", " x: array([ 0.62047435, 1.81858876])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bso.result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Optimization of mid-bond function exponents" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "be2X = Molecule(name=\"Be2\", atoms=[('Be', (0.0, 0.0, -1.5)),\n", " ('H', (0.0, 0.0, 0.0), True),\n", " ('Be', (0.0, 0.0, 1.5))], sym=\"dnh 2\", charge=0, multiplicity=1)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "mbfs = {'H' : [('s', 'et', 4, (0.05, 2.0)), ('p', 'et', 4, (0.04, 2.0))]}" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "mbtmp = '''***,h2o test\n", "memory,100,m !allocate 500 MW dynamic memory\n", "\n", "%geometry\n", "\n", "%basis\n", "\n", "dummy, H\n", "\n", "%core\n", "\n", "{rhf; wf,8,1,0}\n", "cisd\n", "\n", "'''" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "bso = BSOptimizer(objective='cisd total energy', template=mbtmp, code=mp, mol=be2X,\n", " fsopt=mbfs, staticbs=pvdzbe, core=[2,0,0,0,0,0,0,0], verbose=False)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Script name : /home/lmentel/anaconda3/envs/chemtools/lib/python3.6/site-packages/ipykernel_launcher.py\n", "Workdir : /home/lmentel/anaconda3/envs/chemtools/lib/python3.6/site-packages\n", "Start time : 2017-11-27 15:03:13.366257\n", "================================================================================\n", "=============================STARTING OPTIMIZATION==============================\n", "================================================================================\n", "\n", "======================================CODE======================================\n", "\n", "======================================MOL=======================================\n", "Name: Be2 Charge: 0 Multiplicty: 1 Electrons: 8 \n", "Atoms:\n", "Element Nuclear Charge\t x y z \n", "Be 4.00\t 0.00000 0.00000 -1.50000\n", "H 0.00\t 0.00000 0.00000 0.00000\n", "Be 4.00\t 0.00000 0.00000 1.50000\n", "=====================================OPTALG=====================================\n", "{'jacob': None,\n", " 'method': 'Nelder-Mead',\n", " 'options': {'disp': True, 'maxiter': 100},\n", " 'tol': 0.0001}Warning: Maximum number of iterations has been exceeded.\n", " final_simplex: (array([[ 0.05114879, 1.70176733, 0.05504829, 1.9460615 ],\n", " [ 0.05114531, 1.70019891, 0.05505673, 1.94637608],\n", " [ 0.05111266, 1.70488967, 0.05504958, 1.94627325],\n", " [ 0.05116421, 1.7018318 , 0.05507583, 1.94578227],\n", " [ 0.05111091, 1.70251424, 0.05509779, 1.94542874]]), array([-29.16470765, -29.16470765, -29.16470765, -29.16470765, -29.16470765]))\n", " fun: -29.164707647050999\n", " message: 'Maximum number of iterations has been exceeded.'\n", " nfev: 168\n", " nit: 100\n", " status: 2\n", " success: False\n", " x: array([ 0.05114879, 1.70176733, 0.05504829, 1.9460615 ])Elapsed time : 65.526 sec" ] } ], "source": [ "bso.run()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " final_simplex: (array([[ 0.05114879, 1.70176733, 0.05504829, 1.9460615 ],\n", " [ 0.05114531, 1.70019891, 0.05505673, 1.94637608],\n", " [ 0.05111266, 1.70488967, 0.05504958, 1.94627325],\n", " [ 0.05116421, 1.7018318 , 0.05507583, 1.94578227],\n", " [ 0.05111091, 1.70251424, 0.05509779, 1.94542874]]), array([-29.16470765, -29.16470765, -29.16470765, -29.16470765, -29.16470765]))\n", " fun: -29.164707647050999\n", " message: 'Maximum number of iterations has been exceeded.'\n", " nfev: 168\n", " nit: 100\n", " status: 2\n", " success: False\n", " x: array([ 0.05114879, 1.70176733, 0.05504829, 1.9460615 ])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bso.result" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "application/json": { "Software versions": [ { "module": "Python", "version": "3.6.3 64bit [GCC 7.2.0]" }, { "module": "IPython", "version": "6.2.1" }, { "module": "OS", "version": "Linux 4.9.0 4 amd64 x86_64 with debian 9.1" }, { "module": "chemtools", "version": "0.8.4" } ] }, "text/html": [ "
SoftwareVersion
Python3.6.3 64bit [GCC 7.2.0]
IPython6.2.1
OSLinux 4.9.0 4 amd64 x86_64 with debian 9.1
chemtools0.8.4
Mon Nov 27 15:04:18 2017 CET
" ], "text/latex": [ "\\begin{tabular}{|l|l|}\\hline\n", "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", "Python & 3.6.3 64bit [GCC 7.2.0] \\\\ \\hline\n", "IPython & 6.2.1 \\\\ \\hline\n", "OS & Linux 4.9.0 4 amd64 x86\\_64 with debian 9.1 \\\\ \\hline\n", "chemtools & 0.8.4 \\\\ \\hline\n", "\\hline \\multicolumn{2}{|l|}{Mon Nov 27 15:04:18 2017 CET} \\\\ \\hline\n", "\\end{tabular}\n" ], "text/plain": [ "Software versions\n", "Python 3.6.3 64bit [GCC 7.2.0]\n", "IPython 6.2.1\n", "OS Linux 4.9.0 4 amd64 x86_64 with debian 9.1\n", "chemtools 0.8.4\n", "Mon Nov 27 15:04:18 2017 CET" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%version_information chemtools" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 1 }