10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-12-22 12:23:31 +01:00

FCI example

This commit is contained in:
Anthony Scemama 2024-09-10 16:58:28 +02:00
parent 2090aca15e
commit a8ed512c93
7 changed files with 3222 additions and 13 deletions

View File

@ -11,6 +11,7 @@ module Ci_matrix_element = Ci_matrix_element
module Ds = Determinant_space module Ds = Determinant_space
module Sd = Spindeterminant module Sd = Spindeterminant
module Si = Simulation
type num_states type num_states
@ -23,8 +24,11 @@ type t =
m_S2 : (Determinant.t, Determinant.t) Matrix.t lazy_t ; m_S2 : (Determinant.t, Determinant.t) Matrix.t lazy_t ;
eigensystem : ((Determinant.t, num_states) Matrix.t * num_states Vector.t) lazy_t; eigensystem : ((Determinant.t, num_states) Matrix.t * num_states Vector.t) lazy_t;
n_states : int; n_states : int;
simulation : Simulation.t;
} }
let simulation t = t.simulation
let det_space t = t.det_space let det_space t = t.det_space
let n_states t = t.n_states let n_states t = t.n_states
@ -620,8 +624,10 @@ let create_matrix_spin_computed ?(nmax=2) f det_space =
) )
let nuclear_repulsion t =
Si.nuclear_repulsion (simulation t)
let make ?(n_states=1) ?(algo=`Direct) det_space = let make ?(n_states=1) ?(algo=`Direct) ~det_space simulation =
let mo_basis = Ds.mo_basis det_space in let mo_basis = Ds.mo_basis det_space in
@ -635,7 +641,7 @@ let make ?(n_states=1) ?(algo=`Direct) det_space =
Ds.determinant_stream det_space Ds.determinant_stream det_space
|> Stream.next |> Stream.next
in in
h_ij_non_zero mo_basis 0 0 d0 d0 (h_ij_non_zero mo_basis 0 0 d0 d0)
in in
let m_H = let m_H =
@ -680,11 +686,14 @@ let make ?(n_states=1) ?(algo=`Direct) det_space =
let eigenvectors, eigenvalues = let eigenvectors, eigenvalues =
Davidson.make ~threshold:1.e-6 ~n_states diagonal matrix_prod Davidson.make ~threshold:1.e-6 ~n_states diagonal matrix_prod
in in
let eigenvalues = Vector.map (fun x -> x +. e_shift) eigenvalues in let nuclear_repulsion =
Si.nuclear_repulsion simulation
in
let eigenvalues = Vector.map (fun x -> x +. e_shift +. nuclear_repulsion) eigenvalues in
(Conventions.rephase eigenvectors), eigenvalues (Conventions.rephase eigenvectors), eigenvalues
) )
in in
{ det_space ; e_shift ; m_H ; m_S2 ; eigensystem ; n_states } { det_space ; e_shift ; m_H ; m_S2 ; eigensystem ; n_states ; simulation }

View File

@ -1,5 +1,6 @@
(executables (executables
(names (names
ex_fci
ex_integrals ex_integrals
ex_hartree_fock ex_hartree_fock
ex_localization ex_localization

102
examples/ex_fci.ml Normal file
View File

@ -0,0 +1,102 @@
(* [[file:ex_fci.org::*Header][Header:1]] *)
open Qcaml
open Common
open Linear_algebra
let () =
(* Header:1 ends here *)
(* [[file:ex_fci.org::*Definition][Definition:1]] *)
let open Command_line in
begin
set_header_doc (Sys.argv.(0));
set_description_doc "Computes the one- and two-electron hartree_fock on the Gaussian atomic basis set.";
set_specs
[ { short='b' ; long="basis" ; opt=Mandatory;
arg=With_arg "<string>";
doc="Name of the file containing the basis set"; } ;
{ short='x' ; long="xyz" ; opt=Mandatory;
arg=With_arg "<string>";
doc="Name of the file containing the nuclear coordinates in xyz format"; } ;
{ short='m' ; long="multiplicity" ; opt=Optional;
arg=With_arg "<int>";
doc="Spin multiplicity (2S+1). Default is singlet"; } ;
{ short='c' ; long="charge" ; opt=Optional;
arg=With_arg "<int>";
doc="Total charge of the molecule. Specify negative charges with 'm' instead of the minus sign, for example m1 instead of -1. Default is 0"; } ;
]
end;
(* Definition:1 ends here *)
(* [[file:ex_fci.org::*Interpretation][Interpretation:1]] *)
let basis_file = Util.of_some @@ Command_line.get "basis" in
let nuclei_file = Util.of_some @@ Command_line.get "xyz" in
let charge =
match Command_line.get "charge" with
| Some x -> ( if x.[0] = 'm' then
~- (int_of_string (String.sub x 1 (String.length x - 1)))
else
int_of_string x )
| None -> 0
in
let multiplicity =
match Command_line.get "multiplicity" with
| Some x -> int_of_string x
| None -> 1
in
(* Interpretation:1 ends here *)
(* [[file:ex_fci.org::*Computation][Computation:1]] *)
let nuclei =
Particles.Nuclei.of_xyz_file nuclei_file
in
(* Computation:1 ends here *)
(* [[file:ex_fci.org::*Computation][Computation:2]] *)
let ao_basis =
Ao.Basis.of_nuclei_and_basis_filename ~nuclei basis_file
in
(* Computation:2 ends here *)
(* [[file:ex_fci.org::*Computation][Computation:3]] *)
let simulation =
Simulation.make ~multiplicity ~charge ~nuclei ao_basis
in
(* Computation:3 ends here *)
(* [[file:ex_fci.org::*Computation][Computation:4]] *)
let hf =
Mo.Hartree_fock.make ~guess:`Huckel simulation
in
(* Computation:4 ends here *)
(* [[file:ex_fci.org::*Computation][Computation:5]] *)
let mo_basis =
Mo.Basis.of_hartree_fock hf
in
let frozen_core =
Mo.Frozen_core.(make Large nuclei)
in
let det_space =
Ci.Determinant_space.fci_of_mo_basis ~frozen_core mo_basis
in
(* Computation:5 ends here *)
(* [[file:ex_fci.org::*Computation][Computation:6]] *)
let ci =
Ci.make ~det_space simulation
in
(* Computation:6 ends here *)
(* [[file:ex_fci.org::*Output][Output:1]] *)
Format.printf "@[HF energy : %f@]\n" (Mo.Hartree_fock.energy hf) ;
Format.printf "@[FCI energy : %a@]\n" Vector.pp (Ci.eigenvalues ci)
(* Output:1 ends here *)

142
examples/ex_fci.org Normal file
View File

@ -0,0 +1,142 @@
#+TITLE: Full-CI
#+PROPERTY: header-args :tangle ex_fci.ml :comments link :exports code
In this example, we write a program that makes a valence Full CI calculation with
Hartree-Fock orbitals. The molecule is read in =xyz= format and a Gaussian
atomic basis set in GAMESS format.
* Header
#+BEGIN_SRC ocaml
open Qcaml
open Common
open Linear_algebra
let () =
#+END_SRC
* Command-line arguments
We use the =Command_line= module to define the following possible
arguments:
- =-b --basis= : The name of the file containing the basis set
- =-x --xyz= : The name of the file containing the atomic coordinates
- =-c --charge= : The charge of the molecule
- =-m --multiplicity= : The spin multiplicity
** Definition
#+BEGIN_SRC ocaml
let open Command_line in
begin
set_header_doc (Sys.argv.(0));
set_description_doc "Computes the one- and two-electron hartree_fock on the Gaussian atomic basis set.";
set_specs
[ { short='b' ; long="basis" ; opt=Mandatory;
arg=With_arg "<string>";
doc="Name of the file containing the basis set"; } ;
{ short='x' ; long="xyz" ; opt=Mandatory;
arg=With_arg "<string>";
doc="Name of the file containing the nuclear coordinates in xyz format"; } ;
{ short='m' ; long="multiplicity" ; opt=Optional;
arg=With_arg "<int>";
doc="Spin multiplicity (2S+1). Default is singlet"; } ;
{ short='c' ; long="charge" ; opt=Optional;
arg=With_arg "<int>";
doc="Total charge of the molecule. Specify negative charges with 'm' instead of the minus sign, for example m1 instead of -1. Default is 0"; } ;
]
end;
#+END_SRC
** Interpretation
#+BEGIN_SRC ocaml
let basis_file = Util.of_some @@ Command_line.get "basis" in
let nuclei_file = Util.of_some @@ Command_line.get "xyz" in
let charge =
match Command_line.get "charge" with
| Some x -> ( if x.[0] = 'm' then
~- (int_of_string (String.sub x 1 (String.length x - 1)))
else
int_of_string x )
| None -> 0
in
let multiplicity =
match Command_line.get "multiplicity" with
| Some x -> int_of_string x
| None -> 1
in
#+END_SRC
* Computation
We first read the =xyz= file to create a molecule:
#+BEGIN_SRC ocaml
let nuclei =
Particles.Nuclei.of_xyz_file nuclei_file
in
#+END_SRC
Then we create a Gaussian AO basis using the atomic coordinates:
#+BEGIN_SRC ocaml
let ao_basis =
Ao.Basis.of_nuclei_and_basis_filename ~nuclei basis_file
in
#+END_SRC
We create a simulation from the nuclei and the basis set:
#+BEGIN_SRC ocaml
let simulation =
Simulation.make ~multiplicity ~charge ~nuclei ao_basis
in
#+END_SRC
and we make the Hartree-Fock computation:
#+BEGIN_SRC ocaml
let hf =
Mo.Hartree_fock.make ~guess:`Huckel simulation
in
#+END_SRC
We define the FCI determinant space:
#+BEGIN_SRC ocaml
let mo_basis =
Mo.Basis.of_hartree_fock hf
in
let frozen_core =
Mo.Frozen_core.(make Large nuclei)
in
let det_space =
Ci.Determinant_space.fci_of_mo_basis ~frozen_core mo_basis
in
#+END_SRC
And we run the FCI calculation:
#+BEGIN_SRC ocaml
let ci =
Ci.make ~det_space simulation
in
#+END_SRC
* Output
We print the FCI energy:
#+BEGIN_SRC ocaml
Format.printf "@[HF energy : %f@]\n" (Mo.Hartree_fock.energy hf) ;
Format.printf "@[FCI energy : %a@]\n" Vector.pp (Ci.eigenvalues ci)
#+END_SRC

View File

@ -1,6 +1,7 @@
#+TITLE: Hartree-Fock #+TITLE: Hartree-Fock
#+PROPERTY #+PROPERTY: header-args :tangle ex_hartree_fock.ml :comments link :exports code
In this example, we write a program that makes a Hartree-Fock In this example, we write a program that makes a Hartree-Fock
calculation. The molecule is read in =xyz= format and a Gaussian calculation. The molecule is read in =xyz= format and a Gaussian
@ -8,7 +9,7 @@ atomic basis set in GAMESS format.
* Header * Header
#+BEGIN_SRC ocaml :comments link :exports code :tangle ex_hartree_fock.ml #+BEGIN_SRC ocaml
module Command_line = Qcaml.Common.Command_line module Command_line = Qcaml.Common.Command_line
module Util = Qcaml.Common.Util module Util = Qcaml.Common.Util
@ -26,7 +27,7 @@ let () =
** Definition ** Definition
#+BEGIN_SRC ocaml :comments link :exports code :tangle ex_hartree_fock.ml #+BEGIN_SRC ocaml
let open Command_line in let open Command_line in
begin begin
set_header_doc (Sys.argv.(0)); set_header_doc (Sys.argv.(0));
@ -54,7 +55,7 @@ end;
** Interpretation ** Interpretation
#+BEGIN_SRC ocaml :comments link :exports code :tangle ex_hartree_fock.ml #+BEGIN_SRC ocaml
let basis_file = Util.of_some @@ Command_line.get "basis" in let basis_file = Util.of_some @@ Command_line.get "basis" in
let nuclei_file = Util.of_some @@ Command_line.get "xyz" in let nuclei_file = Util.of_some @@ Command_line.get "xyz" in
@ -78,33 +79,33 @@ in
We first read the =xyz= file to create a molecule: We first read the =xyz= file to create a molecule:
#+BEGIN_SRC ocaml :comments link :exports code :tangle ex_hartree_fock.ml #+BEGIN_SRC ocaml
let nuclei = let nuclei =
Qcaml.Particles.Nuclei.of_xyz_file nuclei_file Qcaml.Particles.Nuclei.of_xyz_file nuclei_file
in in
#+END_SRC #+END_SRC
Then we create a Gaussian AO basis using the atomic coordinates: Then we create a Gaussian AO basis using the atomic coordinates:
#+BEGIN_SRC ocaml :comments link :exports code :tangle ex_hartree_fock.ml #+BEGIN_SRC ocaml
let ao_basis = let ao_basis =
Qcaml.Ao.Basis.of_nuclei_and_basis_filename ~nuclei basis_file Qcaml.Ao.Basis.of_nuclei_and_basis_filename ~nuclei basis_file
in in
#+END_SRC #+END_SRC
We create a simulation from the nuclei and the basis set: We create a simulation from the nuclei and the basis set:
#+BEGIN_SRC ocaml :comments link :exports code :tangle ex_hartree_fock.ml #+BEGIN_SRC ocaml
let simulation = Qcaml.Simulation.make ~multiplicity ~charge ~nuclei ao_basis in let simulation = Qcaml.Simulation.make ~multiplicity ~charge ~nuclei ao_basis in
#+END_SRC #+END_SRC
and we can make the Hartree-Fock computation: and we can make the Hartree-Fock computation:
#+BEGIN_SRC ocaml :comments link :exports code :tangle ex_hartree_fock.ml #+BEGIN_SRC ocaml
let hf = Qcaml.Mo.Hartree_fock.make ~guess:`Huckel simulation in let hf = Qcaml.Mo.Hartree_fock.make ~guess:`Huckel simulation in
#+END_SRC #+END_SRC
* Output * Output
We print the convergence of the calculation: We print the convergence of the calculation:
#+BEGIN_SRC ocaml :comments link :exports code :tangle ex_hartree_fock.ml #+BEGIN_SRC ocaml
Format.printf "@[%a@]" (Mo.Hartree_fock.pp) hf Format.printf "@[%a@]" (Mo.Hartree_fock.pp) hf
#+END_SRC #+END_SRC

View File

@ -9,3 +9,7 @@ module Operators = Operators
module Particles = Particles module Particles = Particles
module Perturbation = Perturbation module Perturbation = Perturbation
module Simulation = Simulation module Simulation = Simulation
let (%.) = Linear_algebra.Vector.(%.) ;;
let (%:) = Linear_algebra.Matrix.(%:) ;;

2950
test/sto-6g Normal file

File diff suppressed because it is too large Load Diff