StageYann/Boys.ipynb

334 lines
10 KiB
Plaintext
Raw Normal View History

2020-04-23 11:25:03 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rotation d'orbitales"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"On peut écrire une matrice de rotation $R$ comme\n",
"$$\n",
"R = \\exp(X)\n",
"$$\n",
"où X est une matrice réelle anti-symétrique ($X_{ij} = -X_{ji}$) où $X_{ij}$ est la valeurs de l'angle de la rotation entre $i$ et $j$. Par exemple:\n",
"$$\n",
"R = \\left( \\begin{array}{cc}\n",
"\\cos \\theta & -\\sin \\theta \\\\\n",
"\\sin \\theta & \\cos \\theta \\end{array} \\right)\n",
"= \\exp\n",
" \\left( \\begin{array}{cc}\n",
" 0 & \\theta \\\\\n",
"- \\theta & 0 \\end{array} \\right)\n",
"$$\n",
"\n",
"D'abord on diagonalise $X^2$ :\n",
"$$\n",
"X^2 = W(-\\tau^2) W^\\dagger.\n",
"$$\n",
"Ensuite, $R$ peut être écrit comme\n",
"$$\n",
"R = W \\cos(\\tau) W^\\dagger + W \\tau^{-1} \\sin (\\tau) W^\\dagger X\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## TODO\n",
"\n",
"Ecrire une fonction qui calcule $R$ a partir de $X$.\n",
"Tu vas avoir besoin de Lacaml:\n",
"* Intro a Lapack : https://lipn.univ-paris13.fr/~coti/doc/tutolapack.pdf\n",
"* Lapack user's guide : http://www.netlib.org/lapack/lug/\n",
"* Lacaml : https://mmottl.github.io/lacaml/\n",
"\n",
"Pour diagonaliser une matrice : syev\n",
"\n",
"Pour multiplier des matrices : gemm\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Localisation de Boys"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$N$ orthonormal molecular orbitals\n",
"$$\n",
"\\phi_i({\\bf r})= \\sum_{k=1}^N c_{ik} \\chi_k\n",
"$$\n",
"$$\n",
"{\\cal B}_{2|4} = \\sum_{i=1}^N \\langle \\phi_i | ( {\\bf \\vec{r}} - \\langle \\phi_i| {\\bf \\vec{r}} | \\phi_i …\\rangle)^{2|4} | \\phi_i \\rangle\n",
"$$\n",
"$$\n",
"{\\cal B}_2= \\sum_{i=1}^N \\big[ \\langle x^2 \\rangle_i - \\langle x \\rangle^2_i + \\langle y^2 \\rangle_i - …\\langle y \\rangle^2_i + \\langle z^2 \\rangle_i - \\langle z \\rangle^2_i \\big]\n",
"$$\n",
"$$\n",
"{\\cal B}_2 = \\sum_{i=1}^N \\big[ \\langle x^4 \\rangle_i - 4 \\langle x^3 \\rangle_i \\langle x \\rangle_i\n",
" + 6 \\langle x^2 \\rangle_i \\langle x \\rangle^2_i\n",
"- 3 \\langle x \\rangle^4_i \\big] + \\big[ ...y...] + \\big[ ...z...] \n",
"$$\n",
"Minimization of ${\\cal B}$ with respect to an arbitrary rotation $R$\n",
"$$\n",
" \\langle R \\phi_i x^n R \\phi_i \\rangle = \\sum_{k,l=1}^N R_{ik} R_{il} \\langle \\phi_k| x^n | \\phi_l …\\rangle= \n",
"$$\n",
"$$\n",
"\\sum_{k,l=1}^N R_{ik} R_{il} \\sum_{m,o=1}^N c_{km} c_{ln} \\langle \\chi_m | x^n |\\chi_o \\rangle\n",
"$$\n",
"We need to compute\n",
"$$\n",
"S^x_{n;mo}= \\langle \\chi_m | x^n |\\chi_o \\rangle\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rotation de deux orbitales"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rotation of angle $\\theta$\n",
"$$\n",
"\\tilde{\\phi}_1 = cos\\theta \\phi_1 -sin\\theta \\phi_2\n",
"$$\n",
"$$\n",
"\\tilde{\\phi}_2 = sin\\theta \\phi_1 +cos\\theta \\phi_2\n",
"$$\n",
"Let us note\n",
"$$\n",
"{\\cal B}_x(\\theta) = \\langle x^2 \\rangle_{\\tilde{1}} - \\langle x \\rangle^2_{\\tilde{1}} + \\langle x^2 \\rangle_{\\tilde{2}} - \\langle x \\rangle^2_{\\tilde{2}}\n",
"$$\n",
"and\n",
"\\begin{eqnarray}\n",
"A^x_{ij} & = & \\langle \\phi_i| x^2 | \\phi_j \\rangle \\\\\n",
"B^x_{ij} & = & \\langle \\phi_i| x | \\phi_j \\rangle \\;\\; i,j=1,2 \n",
"\\end{eqnarray}\n",
"We have\n",
"$$\n",
"{\\cal B}_x(\\theta) = A^x_{11}+A^x_{22} - ({\\tilde B}^{x2}_{11} + {\\tilde B}^{x2}_{22})\n",
"$$\n",
"with\n",
"$$\n",
"{\\tilde B}^{x2}_{11}= (cos^2\\theta B^x_{11} + sin^2\\theta B^x_{22} - sin2\\theta B^x_{12})^2\n",
"$$\n",
"$$\n",
"{\\tilde B}^{x2}_{22}= (sin^2\\theta B^x_{11} + cos^2\\theta B^x_{22} + sin2\\theta B^x_{12})^2\n",
"$$\n",
"$$\n",
"{\\cal B}(\\theta)= {\\cal B}_x(\\theta)+{\\cal B}_y(\\theta)+{\\cal B}_z(\\theta)=(x) + (y)+(z)\n",
"$$\n",
"with\n",
"$$\n",
"{\\cal B}_x(\\theta)= A^x_{11}+A^x_{22} \n",
"- [(cos^4\\theta+ sin^4\\theta)({B^x_{11}}^2+ {B^x_{22}}^2 )\n",
"$$\n",
"$$\n",
"+ 2 sin^2 2\\theta {B^x_{12}}^2\n",
"+ 2 sin 2\\theta cos 2\\theta (({B^x_{22}} -{B^x_{11}} ) {B^x_{12}}]\n",
"$$\n",
"and idem for $y$ and $z$. \n",
"Using the fact that\n",
"$$\n",
"cos^4\\theta+ sin^4\\theta= \\frac{1}{4} ( 3 + cos4\\theta)\n",
"$$\n",
"$$\n",
"{\\cal B}_x(\\theta)= A^x_{11}+A^x_{22} \n",
"- [ \\frac{1}{4} ( 3 + cos4\\theta)({B^x_{11}}^2+ {B^x_{22}}^2 )\n",
"$$\n",
"$$\n",
"+ (1 -cos 4\\theta) {B^x_{12}}^2\n",
"+ sin 4\\theta (({B^x_{22}} -{B^x_{11}} ) {B^x_{12}}]\n",
"$$\n",
"Finally, we get\n",
"\\begin{equation}\n",
"{\\cal B}(\\theta)= {\\cal B}(0) + \\frac{1}{4} [(1-cos4\\theta)\\beta+sin4\\theta \\gamma] \n",
"\\end{equation}\n",
"where\n",
"$$\n",
"{\\cal B}(0)= A^x_{11}+A^x_{22} -((B^{x}_{11})^2+(B^{x}_{22})^2) + [...y...] + [...z...]\n",
"$$\n",
"$$\n",
"\\beta= (B^x_{11}-B^x_{22})^2 - 4 {(B^x_{12})}^2 + [...y...] + [...z...]\n",
"$$\n",
"and\n",
"$$\n",
"\\gamma= 4 B^x_{12} (B^{x}_{11}-B^x_{22}) + [...y...] + [...z...]\n",
"$$\n",
"Let us compute the derivative; we get\n",
"$$\n",
"\\frac{\\partial {\\cal B}(\\theta)}{\\partial \\theta} = \n",
"\\beta sin4\\theta \n",
"+ \\gamma cos4\\theta\n",
"$$\n",
"Extrema of ${\\cal B}(\\theta)$\n",
"\\begin{equation}\n",
"tg4\\theta= -\\frac{\\gamma}{\\beta} \n",
"\\end{equation}\n",
"There are four extrema:\n",
"$$\n",
"4\\theta; \\;\\; 4\\theta +\\pi; \\;\\; 4\\theta+ 2\\pi; \\;\\; 4\\theta+ 3\\pi\n",
"$$\n",
"Value of the second derivative of $\\cal{B}$ at the extrema\n",
"Value of $\\cal{B}$ at the extrema\n",
"\\begin{equation}\n",
"\\frac{\\partial^2 B(\\theta)}{\\partial \\theta^2}= 4 cos4\\theta \\frac{\\beta^2 + \\gamma^2}{\\beta}\n",
"\\end{equation}\n",
"There are two minima and two maxima since $cos4\\theta= -cos4(\\theta+\\pi)= -cos4(\\theta+2\\pi)=-cos4(\\theta+3\\pi)$.\n",
"Value of $\\cal{B}$ at the extrema\n",
"\\begin{equation}\n",
"{\\cal B}(\\theta)= {\\cal B}(0) + \\frac{1}{4} (\\beta -\\frac{\\beta^2 + \\gamma^2}{\\beta} {cos4\\theta})\n",
"\\end{equation}\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#require \"lacaml.top\";;\n",
"#require \"alcotest\";;\n",
"#require \"str\";;\n",
"#require \"bigarray\";;\n",
"#require \"zarith\";;\n",
"#require \"getopt\";;\n",
"open Lacaml.D"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(*\n",
"NB: il faut que je change les noms des matrices car ce n'est pas toujours clair\n",
"*)\n",
"\n",
"(* Construction de la matrice X n par n *)\n",
"let n =3 \n",
"let ran=Mat.random ~range:1. ~from:0. n n;; (* Construction d'une matrice random n*n *)\n",
"\n",
"(* Antisymétrisation de la matrice et 0 sur la diagonale *)\n",
"let antisym = Mat.init_cols n n (fun i j -> if i>j then -. ran.{i,j} else ran.{j,i});;\n",
"\n",
"(* 0 sur la diagonale -> X *)\n",
"let m = Mat.init_cols n n (fun i j -> if i=j then 0. else antisym.{i,j});; \n",
"\n",
"(* Fonction 2 en un *)\n",
"(*\n",
"let x = Mat.init_cols n n (fun i j -> if i=j then 0.\n",
" else if i>j then -. ran.{i,j} else ran.{j,i});;\n",
" *)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* Mise au carré de la matrice : X -> X² *)\n",
"let mm = gemm m m;;\n",
"\n",
"(* Copie de mm qui va être écrasée : X *)\n",
"let copie_mm = lacpy mm;;\n",
"\n",
"(* Diagonalisation de X² *)\n",
"let diag = syev mm;;\n",
"\n",
"copie_mm;; (* Copie de la matrice avant diagonalisation *)\n",
"mm;; (* Matrice avec les eigenvectors -> W *)\n",
"diag;; (* Vecteur contenant les eigenvalues *)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* Passage de -tau² à tau² *)\n",
"let square_tau= Vec.abs diag;;\n",
"\n",
"(* Passage de tau² à tau *)\n",
"let tau = Vec.sqrt square_tau;;\n",
"\n",
"(* Passage du vecteur tau à la matrice avec les valeurs propres sur la diagonale *)\n",
"let m_tau = Mat.init_cols n n (fun i j -> if i=j then tau.{i} else 0.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* Calcul de cos tau *)\n",
"let cos_tau = Mat.cos m_tau;;\n",
"\n",
"(* Calcul de sin tau *)\n",
"let sin_tau = Mat.sin m_tau;; (* NB: Si on applique sin au vecteur tau il faut ensuité créer une matrice avec\n",
"des 1 en éléments extra diagonaux, c'est peut être mieux de faire comme ça *)\n",
"\n",
"(* Calcul de la transposée de W (mm) *)\n",
"let transpose_mm = Mat.transpose_copy mm;; \n",
"mm;;(* Vérification *) (*OK*)\n",
"\n",
"(* Calcul de tau^-1 *)\n",
"(*\n",
"let tau_1 = (* Je suis en train de chercher comment le calculer *)\n",
"*)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* Calcul de R *)\n",
"(*\n",
"let r = gemm mm (gemm cos_tau transpose_mm) + gemm( mm gemm(tau_1 gemm( sin_tau (gemm transpose_mm m))))\n",
"*)\n",
"(* Ne peut pas fonctionner -> + (je n'ai pas trouvé de fonction ?) et il manque des calculs\n",
"Il faut surement découper ce calcul aussi ? *)"
]
2020-04-23 11:25:03 +02:00
}
],
"metadata": {
"kernelspec": {
"display_name": "OCaml 4.10.0",
"language": "OCaml",
"name": "ocaml-jupyter"
},
"language_info": {
"codemirror_mode": "text/x-ocaml",
"file_extension": ".ml",
"mimetype": "text/x-ocaml",
"name": "OCaml",
"nbconverter_exporter": null,
"pygments_lexer": "OCaml",
"version": "4.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}