10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-13 17:43:41 +01:00

Pretty printer for localization

This commit is contained in:
Anthony Scemama 2021-02-03 09:23:20 +01:00
parent 32c09e5d14
commit 254e8b5dc8
5 changed files with 142 additions and 67 deletions

View File

@ -3,7 +3,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-02-03 Wed 08:50 -->
<!-- 2021-02-03 Wed 09:30 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Molecular orbitals</title>
@ -272,46 +272,46 @@ org_html_manager.setup(); // activate after the parameters are set
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgc090cb3">1. Summmary</a></li>
<li><a href="#orgbe38590">2. Frozen core</a>
<li><a href="#org0347fac">1. Summmary</a></li>
<li><a href="#org3e52ccb">2. Frozen core</a>
<ul>
<li><a href="#org69d8070">2.1. Type</a></li>
<li><a href="#orgcdbe863">2.2. Creation</a></li>
<li><a href="#org8253c00">2.3. Access</a></li>
<li><a href="#orgdec1d0a">2.4. Printers</a></li>
<li><a href="#org314a379">2.1. Type</a></li>
<li><a href="#org3f41ebf">2.2. Creation</a></li>
<li><a href="#org30d9e55">2.3. Access</a></li>
<li><a href="#orgbfbb067">2.4. Printers</a></li>
</ul>
</li>
<li><a href="#org6309534">3. Orbital localization</a>
<li><a href="#org71f8a17">3. Orbital localization</a>
<ul>
<li><a href="#orgf6eb1aa">3.1. Type</a></li>
<li><a href="#org32bcf2c">3.2. Edmiston-Rudenberg</a></li>
<li><a href="#orgced2d92">3.3. Boys</a></li>
<li><a href="#org19e8774">3.4. Access</a></li>
<li><a href="#orgb7676a6">3.5. Printers</a></li>
<li><a href="#orgfbb396d">3.6. Tests</a></li>
<li><a href="#orgd6cd073">3.1. Type</a></li>
<li><a href="#org5abbc4c">3.2. Edmiston-Rudenberg</a></li>
<li><a href="#org1360ab2">3.3. Boys</a></li>
<li><a href="#org44850fb">3.4. Access</a></li>
<li><a href="#orgb1a2fc0">3.5. Printers</a></li>
<li><a href="#orgc751f7e">3.6. Tests</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-orgc090cb3" class="outline-2">
<h2 id="orgc090cb3"><span class="section-number-2">1</span> Summmary</h2>
<div id="outline-container-org0347fac" class="outline-2">
<h2 id="org0347fac"><span class="section-number-2">1</span> Summmary</h2>
</div>
<div id="outline-container-orgbe38590" class="outline-2">
<h2 id="orgbe38590"><span class="section-number-2">2</span> Frozen core</h2>
<div id="outline-container-org3e52ccb" class="outline-2">
<h2 id="org3e52ccb"><span class="section-number-2">2</span> Frozen core</h2>
<div class="outline-text-2" id="text-2">
<p>
Defines how the core electrons are frozen, for each atom.
</p>
</div>
<div id="outline-container-org69d8070" class="outline-3">
<h3 id="org69d8070"><span class="section-number-3">2.1</span> Type</h3>
<div id="outline-container-org314a379" class="outline-3">
<h3 id="org314a379"><span class="section-number-3">2.1</span> Type</h3>
<div class="outline-text-3" id="text-2-1">
<div class="org-src-container">
<pre class="src src-ocaml" id="org12d811a"><span class="org-tuareg-font-lock-governing">type</span> <span class="org-type">kind</span> <span class="org-tuareg-font-lock-operator">=</span>
<pre class="src src-ocaml" id="org46bfdf8"><span class="org-tuareg-font-lock-governing">type</span> <span class="org-type">kind</span> <span class="org-tuareg-font-lock-operator">=</span>
<span class="org-tuareg-font-lock-operator">|</span> <span class="org-tuareg-font-lock-constructor">All_electron</span>
<span class="org-tuareg-font-lock-operator">|</span> <span class="org-tuareg-font-lock-constructor">Small</span>
<span class="org-tuareg-font-lock-operator">|</span> Large
@ -325,8 +325,8 @@ Defines how the core electrons are frozen, for each atom.
</div>
</div>
<div id="outline-container-orgcdbe863" class="outline-3">
<h3 id="orgcdbe863"><span class="section-number-3">2.2</span> Creation</h3>
<div id="outline-container-org3f41ebf" class="outline-3">
<h3 id="org3f41ebf"><span class="section-number-3">2.2</span> Creation</h3>
<div class="outline-text-3" id="text-2-2">
<div class="org-src-container">
<pre class="src src-ocaml"><span class="org-tuareg-font-lock-governing">val</span> <span class="org-function-name">make</span> <span class="org-tuareg-font-lock-operator">:</span> kind <span class="org-tuareg-font-lock-operator">-&gt;</span> <span class="org-tuareg-font-lock-module">Particles.Nuclei.</span>t <span class="org-tuareg-font-lock-operator">-&gt;</span> t
@ -362,7 +362,7 @@ Defines how the core electrons are frozen, for each atom.
</tbody>
</table>
<pre class="example" id="org981a6c7">
<pre class="example" id="org41e22ed">
let f = Frozen_core.(make Small nuclei) ;;
val f : Frozen_core.t = [|0; 2; 2; 0|]
@ -372,8 +372,8 @@ val f : Frozen_core.t = [|0; 2; 2; 0|]
</div>
</div>
<div id="outline-container-org8253c00" class="outline-3">
<h3 id="org8253c00"><span class="section-number-3">2.3</span> Access</h3>
<div id="outline-container-org30d9e55" class="outline-3">
<h3 id="org30d9e55"><span class="section-number-3">2.3</span> Access</h3>
<div class="outline-text-3" id="text-2-3">
<div class="org-src-container">
<pre class="src src-ocaml"><span class="org-tuareg-font-lock-governing">val</span> <span class="org-function-name">num_elec</span> <span class="org-tuareg-font-lock-operator">:</span> t <span class="org-tuareg-font-lock-operator">-&gt;</span> int
@ -402,7 +402,7 @@ val f : Frozen_core.t = [|0; 2; 2; 0|]
</tbody>
</table>
<pre class="example" id="org807dc16">
<pre class="example" id="org64fff1a">
Frozen_core.num_elec f ;;
- : int = 4
@ -412,8 +412,8 @@ Frozen_core.num_mos f ;;
</div>
</div>
<div id="outline-container-orgdec1d0a" class="outline-3">
<h3 id="orgdec1d0a"><span class="section-number-3">2.4</span> Printers</h3>
<div id="outline-container-orgbfbb067" class="outline-3">
<h3 id="orgbfbb067"><span class="section-number-3">2.4</span> Printers</h3>
<div class="outline-text-3" id="text-2-4">
<div class="org-src-container">
<pre class="src src-ocaml"><span class="org-tuareg-font-lock-governing">val</span> <span class="org-function-name">pp</span> <span class="org-tuareg-font-lock-operator">:</span> <span class="org-tuareg-font-lock-module">Format.</span>formatter <span class="org-tuareg-font-lock-operator">-&gt;</span> t <span class="org-tuareg-font-lock-operator">-&gt;</span> unit
@ -423,8 +423,8 @@ Frozen_core.num_mos f ;;
</div>
</div>
<div id="outline-container-org6309534" class="outline-2">
<h2 id="org6309534"><span class="section-number-2">3</span> Orbital localization</h2>
<div id="outline-container-org71f8a17" class="outline-2">
<h2 id="org71f8a17"><span class="section-number-2">3</span> Orbital localization</h2>
<div class="outline-text-2" id="text-3">
<p>
Molecular orbital localization function.
@ -440,11 +440,11 @@ Edmiston-Rudenberg:
</div>
<div id="outline-container-orgf6eb1aa" class="outline-3">
<h3 id="orgf6eb1aa"><span class="section-number-3">3.1</span> Type</h3>
<div id="outline-container-orgd6cd073" class="outline-3">
<h3 id="orgd6cd073"><span class="section-number-3">3.1</span> Type</h3>
<div class="outline-text-3" id="text-3-1">
<div class="org-src-container">
<pre class="src src-ocaml" id="org60c20d2"><span class="org-tuareg-font-lock-governing">open </span><span class="org-tuareg-font-lock-module">Linear_algebra</span>
<pre class="src src-ocaml" id="orgbd40984"><span class="org-tuareg-font-lock-governing">open </span><span class="org-tuareg-font-lock-module">Linear_algebra</span>
<span class="org-tuareg-font-lock-governing">type</span> <span class="org-type">localization_kind</span> <span class="org-tuareg-font-lock-operator">=</span>
<span class="org-tuareg-font-lock-operator">|</span> <span class="org-tuareg-font-lock-constructor">Edmiston</span>
@ -464,16 +464,16 @@ Edmiston-Rudenberg:
</div>
</div>
<div id="outline-container-org32bcf2c" class="outline-3">
<h3 id="org32bcf2c"><span class="section-number-3">3.2</span> Edmiston-Rudenberg</h3>
<div id="outline-container-org5abbc4c" class="outline-3">
<h3 id="org5abbc4c"><span class="section-number-3">3.2</span> Edmiston-Rudenberg</h3>
</div>
<div id="outline-container-orgced2d92" class="outline-3">
<h3 id="orgced2d92"><span class="section-number-3">3.3</span> Boys</h3>
<div id="outline-container-org1360ab2" class="outline-3">
<h3 id="org1360ab2"><span class="section-number-3">3.3</span> Boys</h3>
</div>
<div id="outline-container-org19e8774" class="outline-3">
<h3 id="org19e8774"><span class="section-number-3">3.4</span> Access</h3>
<div id="outline-container-org44850fb" class="outline-3">
<h3 id="org44850fb"><span class="section-number-3">3.4</span> Access</h3>
<div class="outline-text-3" id="text-3-4">
<div class="org-src-container">
<pre class="src src-ocaml"><span class="org-tuareg-font-lock-governing">val</span> <span class="org-function-name">kind</span> <span class="org-tuareg-font-lock-operator">:</span> t <span class="org-tuareg-font-lock-operator">-&gt;</span> localization_kind
@ -522,26 +522,24 @@ Edmiston-Rudenberg:
</div>
</div>
<div id="outline-container-orgb7676a6" class="outline-3">
<h3 id="orgb7676a6"><span class="section-number-3">3.5</span> Printers</h3>
<div id="outline-container-orgb1a2fc0" class="outline-3">
<h3 id="orgb1a2fc0"><span class="section-number-3">3.5</span> Printers</h3>
<div class="outline-text-3" id="text-3-5">
<div class="org-src-container">
<pre class="src src-ocaml"><span class="org-comment-delimiter">(*</span>
<span class="org-comment"> val pp : Format.formatter -&gt; t -&gt; unit</span>
<span class="org-comment"> </span><span class="org-comment-delimiter">*)</span>
<pre class="src src-ocaml"><span class="org-tuareg-font-lock-governing">val</span> <span class="org-function-name">pp</span> <span class="org-tuareg-font-lock-operator">:</span> <span class="org-tuareg-font-lock-module">Format.</span>formatter <span class="org-tuareg-font-lock-operator">-&gt;</span> t <span class="org-tuareg-font-lock-operator">-&gt;</span> unit
</pre>
</div>
</div>
</div>
<div id="outline-container-orgfbb396d" class="outline-3">
<h3 id="orgfbb396d"><span class="section-number-3">3.6</span> Tests</h3>
<div id="outline-container-orgc751f7e" class="outline-3">
<h3 id="orgc751f7e"><span class="section-number-3">3.6</span> Tests</h3>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Anthony Scemama</p>
<p class="date">Created: 2021-02-03 Wed 08:50</p>
<p class="date">Created: 2021-02-03 Wed 09:30</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>

View File

@ -3,7 +3,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-02-03 Wed 08:50 -->
<!-- 2021-02-03 Wed 09:30 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Top-level</title>
@ -250,18 +250,18 @@ org_html_manager.setup(); // activate after the parameters are set
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgb4e9044">1. Summmary</a></li>
<li><a href="#org30505a3">1. Summmary</a></li>
</ul>
</div>
</div>
<div id="outline-container-orgb4e9044" class="outline-2">
<h2 id="orgb4e9044"><span class="section-number-2">1</span> Summmary</h2>
<div id="outline-container-org30505a3" class="outline-2">
<h2 id="org30505a3"><span class="section-number-2">1</span> Summmary</h2>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Anthony Scemama</p>
<p class="date">Created: 2021-02-03 Wed 08:50</p>
<p class="date">Created: 2021-02-03 Wed 09:30</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>

View File

@ -15,6 +15,7 @@ type localization_data =
kappa : (loc, loc) Matrix.t ;
scaling : float ;
loc_value : float ;
convergence : float ;
iteration : int ;
}
@ -234,7 +235,7 @@ let ao_basis t = Simulation.ao_basis (simulation t)
*)
let make ~kind ?(max_iter=500) ?(convergence=1.e-8) mo_basis selected_mos =
let make ~kind ?(max_iter=500) ?(convergence=1.e-6) mo_basis selected_mos =
let kappa_loc = kappa ~kind mo_basis in
@ -255,20 +256,27 @@ let make ~kind ?(max_iter=500) ?(convergence=1.e-8) mo_basis selected_mos =
and scaling = 0.5
in
let kappa, loc_value = kappa_loc x in
let convergence = abs_float (Matrix.amax kappa) in
let kappa = Matrix.scale scaling kappa in
let coefficients = next_coef kappa x in
{ coefficients ; kappa ; scaling ; loc_value ; iteration }
{ coefficients ; kappa ; scaling ; convergence ; loc_value ; iteration }
in
let iteration data =
let iteration = data.iteration + 1
and scaling = data.scaling
and x = data.coefficients
in
let kappa, loc_value = kappa_loc x in
let convergence = abs_float (Matrix.amax kappa) in
let scaling =
if convergence <= data.convergence then
min 1. (data.scaling *. 1.1)
else
data.scaling *. 0.75
in
let kappa = Matrix.scale scaling kappa in
let coefficients = next_coef kappa x in
{ coefficients ; kappa ; scaling ; loc_value ; iteration }
{ coefficients ; kappa ; scaling ; convergence ; loc_value ; iteration }
in
let array_data =
@ -289,7 +297,7 @@ let make ~kind ?(max_iter=500) ?(convergence=1.e-8) mo_basis selected_mos =
| Some data -> begin
(* Check convergence *)
let converged =
abs_float data.loc_value < convergence
data.convergence < convergence
in
if converged then
None
@ -327,5 +335,37 @@ let to_basis t =
(* Access:2 ends here *)
(* [[file:~/QCaml/mo/localization.org::*Printers][Printers:2]] *)
let linewidth = 60
let pp_iterations ppf t =
let line = (String.make linewidth '-') in
Format.fprintf ppf "@[%4s%s@]@." "" line;
Format.fprintf ppf "@[%4s@[%5s@]@,@[%16s@]@,@[%16s@]@,@[%11s@]@]@."
"" "#" "Localization " "Convergence" "Scaling";
Format.fprintf ppf "@[%4s%s@]@." "" line;
Array.iter (fun data ->
let data = Lazy.force data in
match data with
| None -> ()
| Some data ->
let loc = data.loc_value in
let conv = data.convergence in
let scaling = data.scaling in
let iteration = data.iteration in
begin
Format.fprintf ppf "@[%4s@[%5d@]@,@[%16.8f@]@,@[%16.4e@]@,@[%11.4f@]@]@." ""
iteration loc conv scaling ;
end
) t.data;
Format.fprintf ppf "@[%4s%s@]@." "" line
let pp ppf t =
Format.fprintf ppf "@.@[%s@]@." (String.make 70 '=');
Format.fprintf ppf "@[%34s %-34s@]@." (match t.kind with
| Boys -> "Boys"
| Edmiston -> "Edmiston-Ruedenberg"
) "MO Localization";
Format.fprintf ppf "@[%s@]@.@." (String.make 70 '=');
Format.fprintf ppf "@[%a@]@." pp_iterations t;
(* Printers:2 ends here *)

View File

@ -48,7 +48,5 @@ val to_basis : t -> Basis.t
(* [[file:~/QCaml/mo/localization.org::*Printers][Printers:1]] *)
(*
val pp : Format.formatter -> t -> unit
*)
(* Printers:1 ends here *)

View File

@ -50,6 +50,7 @@ type localization_data =
kappa : (loc, loc) Matrix.t ;
scaling : float ;
loc_value : float ;
convergence : float ;
iteration : int ;
}
@ -294,7 +295,7 @@ let ao_basis t = Simulation.ao_basis (simulation t)
,*)
let make ~kind ?(max_iter=500) ?(convergence=1.e-8) mo_basis selected_mos =
let make ~kind ?(max_iter=500) ?(convergence=1.e-6) mo_basis selected_mos =
let kappa_loc = kappa ~kind mo_basis in
@ -315,20 +316,27 @@ let make ~kind ?(max_iter=500) ?(convergence=1.e-8) mo_basis selected_mos =
and scaling = 0.5
in
let kappa, loc_value = kappa_loc x in
let convergence = abs_float (Matrix.amax kappa) in
let kappa = Matrix.scale scaling kappa in
let coefficients = next_coef kappa x in
{ coefficients ; kappa ; scaling ; loc_value ; iteration }
{ coefficients ; kappa ; scaling ; convergence ; loc_value ; iteration }
in
let iteration data =
let iteration = data.iteration + 1
and scaling = data.scaling
and x = data.coefficients
in
let kappa, loc_value = kappa_loc x in
let convergence = abs_float (Matrix.amax kappa) in
let scaling =
if convergence <= data.convergence then
min 1. (data.scaling *. 1.1)
else
data.scaling *. 0.75
in
let kappa = Matrix.scale scaling kappa in
let coefficients = next_coef kappa x in
{ coefficients ; kappa ; scaling ; loc_value ; iteration }
{ coefficients ; kappa ; scaling ; convergence ; loc_value ; iteration }
in
let array_data =
@ -349,7 +357,7 @@ let make ~kind ?(max_iter=500) ?(convergence=1.e-8) mo_basis selected_mos =
| Some data -> begin
(* Check convergence *)
let converged =
abs_float data.loc_value < convergence
data.convergence < convergence
in
if converged then
None
@ -390,12 +398,43 @@ let to_basis t =
** Printers
#+begin_src ocaml :tangle (eval mli)
(*
val pp : Format.formatter -> t -> unit
*)
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
let linewidth = 60
let pp_iterations ppf t =
let line = (String.make linewidth '-') in
Format.fprintf ppf "@[%4s%s@]@." "" line;
Format.fprintf ppf "@[%4s@[%5s@]@,@[%16s@]@,@[%16s@]@,@[%11s@]@]@."
"" "#" "Localization " "Convergence" "Scaling";
Format.fprintf ppf "@[%4s%s@]@." "" line;
Array.iter (fun data ->
let data = Lazy.force data in
match data with
| None -> ()
| Some data ->
let loc = data.loc_value in
let conv = data.convergence in
let scaling = data.scaling in
let iteration = data.iteration in
begin
Format.fprintf ppf "@[%4s@[%5d@]@,@[%16.8f@]@,@[%16.4e@]@,@[%11.4f@]@]@." ""
iteration loc conv scaling ;
end
) t.data;
Format.fprintf ppf "@[%4s%s@]@." "" line
let pp ppf t =
Format.fprintf ppf "@.@[%s@]@." (String.make 70 '=');
Format.fprintf ppf "@[%34s %-34s@]@." (match t.kind with
| Boys -> "Boys"
| Edmiston -> "Edmiston-Ruedenberg"
) "MO Localization";
Format.fprintf ppf "@[%s@]@.@." (String.make 70 '=');
Format.fprintf ppf "@[%a@]@." pp_iterations t;
#+end_src