2019-03-21 00:44:10 +01:00
|
|
|
open Lacaml.D
|
|
|
|
|
2019-10-03 16:58:15 +02:00
|
|
|
module Ds = DeterminantSpace
|
2019-10-10 02:01:17 +02:00
|
|
|
module De = Determinant
|
|
|
|
module Sp = Spindeterminant
|
2019-10-03 16:58:15 +02:00
|
|
|
|
2019-03-21 00:44:10 +01:00
|
|
|
type t =
|
|
|
|
{
|
|
|
|
mo_basis : MOBasis.t ;
|
|
|
|
det_space : DeterminantSpace.t ;
|
|
|
|
ci : CI.t ;
|
2019-10-03 16:58:15 +02:00
|
|
|
hf12_integrals : HF12.t ;
|
2019-03-21 21:48:21 +01:00
|
|
|
eigensystem : (Mat.t * Vec.t) lazy_t;
|
2019-03-21 00:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let ci t = t.ci
|
|
|
|
let mo_basis t = t.mo_basis
|
|
|
|
let det_space t = t.det_space
|
2019-10-03 16:58:15 +02:00
|
|
|
let mo_class t = Ds.mo_class @@ det_space t
|
2019-03-22 00:34:00 +01:00
|
|
|
let eigensystem t = Lazy.force t.eigensystem
|
2019-03-21 00:44:10 +01:00
|
|
|
|
2019-03-21 11:02:58 +01:00
|
|
|
|
2019-03-21 00:44:10 +01:00
|
|
|
|
2019-10-03 16:58:15 +02:00
|
|
|
let hf_ij_non_zero hf12_integrals deg_a deg_b ki kj =
|
|
|
|
let integrals = [
|
|
|
|
let one_e _ _ _ = 0. in
|
2019-10-14 14:16:28 +02:00
|
|
|
let { HF12.
|
|
|
|
simulation ; aux_basis ;
|
|
|
|
hf12 ; hf12_anti ;
|
|
|
|
hf12_single ; hf12_single_anti } = hf12_integrals
|
|
|
|
in
|
2019-10-10 02:01:17 +02:00
|
|
|
let kia = De.alfa ki and kib = De.beta ki
|
|
|
|
and kja = De.alfa kj and kjb = De.beta kj
|
|
|
|
in
|
|
|
|
let mo_a =
|
|
|
|
Bitstring.logand (Sp.bitstring kia) (Sp.bitstring kja)
|
|
|
|
|> Bitstring.to_list
|
|
|
|
and mo_b =
|
|
|
|
Bitstring.logand (Sp.bitstring kib) (Sp.bitstring kjb)
|
|
|
|
|> Bitstring.to_list
|
|
|
|
in
|
2019-10-03 16:58:15 +02:00
|
|
|
let two_e i j k l s s' =
|
|
|
|
if s' = Spin.other s then
|
2019-10-14 14:16:28 +02:00
|
|
|
hf12.{i,j,k,l} -. 1. *. (
|
|
|
|
(List.fold_left (fun accu m -> accu +. hf12_single.{m,i,j,k,l}) 0. mo_a) +.
|
|
|
|
(List.fold_left (fun accu m -> accu +. hf12_single.{m,j,i,l,k}) 0. mo_b)
|
2019-10-10 02:01:17 +02:00
|
|
|
)
|
2019-10-03 16:58:15 +02:00
|
|
|
else
|
2019-10-14 14:16:28 +02:00
|
|
|
hf12_anti.{i,j,k,l} -. 1. *. (
|
|
|
|
(List.fold_left (fun accu m -> accu +. hf12_single_anti.{m,i,j,k,l}) 0. mo_a) +.
|
|
|
|
(List.fold_left (fun accu m -> accu +. hf12_single_anti.{m,j,i,l,k}) 0. mo_b)
|
2019-10-10 02:01:17 +02:00
|
|
|
)
|
2019-10-03 16:58:15 +02:00
|
|
|
in
|
|
|
|
(one_e, two_e)
|
|
|
|
]
|
2019-03-21 00:44:10 +01:00
|
|
|
in
|
2019-10-03 16:58:15 +02:00
|
|
|
CIMatrixElement.non_zero integrals deg_a deg_b ki kj
|
2019-03-25 15:20:01 +01:00
|
|
|
|> List.hd
|
2019-03-21 00:44:10 +01:00
|
|
|
|
2019-08-17 11:36:59 +02:00
|
|
|
|
|
|
|
|
2019-10-10 02:01:17 +02:00
|
|
|
|
2019-10-03 16:58:15 +02:00
|
|
|
let dressing_vector ~frozen_core hf12_integrals f12_amplitudes ci =
|
2019-03-21 00:44:10 +01:00
|
|
|
|
2019-03-29 19:01:51 +01:00
|
|
|
if Parallel.master then
|
|
|
|
Printf.printf "Building matrix\n%!";
|
2019-10-03 16:58:15 +02:00
|
|
|
let det_space =
|
|
|
|
ci.CI.det_space
|
2019-03-22 18:16:41 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
let m_HF =
|
2019-10-03 16:58:15 +02:00
|
|
|
|
|
|
|
let f =
|
|
|
|
match Ds.determinants det_space with
|
|
|
|
| Ds.Arbitrary _ -> CI.create_matrix_arbitrary
|
|
|
|
| Ds.Spin _ -> CI.create_matrix_spin_computed
|
2019-03-28 10:32:54 +01:00
|
|
|
in
|
2019-10-03 16:58:15 +02:00
|
|
|
f (fun deg_a deg_b ki kj ->
|
|
|
|
hf_ij_non_zero hf12_integrals deg_a deg_b ki kj
|
|
|
|
) det_space
|
2019-05-27 17:35:28 +02:00
|
|
|
|
2019-03-25 19:28:38 +01:00
|
|
|
in
|
|
|
|
|
2019-10-03 16:58:15 +02:00
|
|
|
Matrix.mm (Lazy.force m_HF) (Matrix.dense_of_mat f12_amplitudes)
|
2019-03-21 00:44:10 +01:00
|
|
|
|
|
|
|
|
2019-05-07 17:00:44 +02:00
|
|
|
let make ~simulation ?(threshold=1.e-12) ~frozen_core ~mo_basis ~aux_basis_filename ?(state=1) () =
|
2019-03-21 00:44:10 +01:00
|
|
|
|
|
|
|
let det_space =
|
2019-10-03 16:58:15 +02:00
|
|
|
DeterminantSpace.fci_of_mo_basis mo_basis ~frozen_core
|
2019-03-21 00:44:10 +01:00
|
|
|
in
|
|
|
|
|
2019-05-07 17:00:44 +02:00
|
|
|
let ci = CI.make ~n_states:state det_space in
|
2019-03-21 11:02:58 +01:00
|
|
|
|
2019-10-03 16:58:15 +02:00
|
|
|
let hf12_integrals =
|
|
|
|
HF12.make ~simulation ~mo_basis ~aux_basis_filename ()
|
|
|
|
in
|
|
|
|
|
2019-03-22 00:34:00 +01:00
|
|
|
let ci_coef, ci_energy =
|
|
|
|
let x = Lazy.force ci.eigensystem in
|
|
|
|
Parallel.broadcast (lazy x)
|
|
|
|
in
|
2019-03-21 11:02:58 +01:00
|
|
|
|
2019-03-21 21:48:21 +01:00
|
|
|
let eigensystem = lazy (
|
|
|
|
let m_H =
|
|
|
|
Lazy.force ci.CI.m_H
|
|
|
|
in
|
|
|
|
|
2019-05-07 17:00:44 +02:00
|
|
|
|
|
|
|
let rec iteration ~state psi =
|
|
|
|
let column_idx = iamax (Mat.to_col_vecs psi).(state-1) in
|
2019-08-26 22:34:41 +02:00
|
|
|
|
2019-03-22 00:34:00 +01:00
|
|
|
let delta =
|
2019-05-27 17:35:28 +02:00
|
|
|
(* delta_i = {% $\sum_j c_j H_{ij}$ %} *)
|
2019-10-03 16:58:15 +02:00
|
|
|
dressing_vector ~frozen_core hf12_integrals psi ci
|
2019-05-27 17:35:28 +02:00
|
|
|
|> Matrix.to_mat
|
2019-03-22 00:34:00 +01:00
|
|
|
in
|
|
|
|
|
2019-05-29 09:04:43 +02:00
|
|
|
Printf.printf "Cmax : %e\n" psi.{column_idx,state};
|
|
|
|
Printf.printf "Norm : %e\n" (sqrt (gemm ~transa:`T delta delta).{state,state});
|
|
|
|
|
2019-05-07 17:00:44 +02:00
|
|
|
let f = 1.0 /. psi.{column_idx,state} in
|
2019-03-22 22:42:20 +01:00
|
|
|
let delta_00 =
|
2019-05-27 17:35:28 +02:00
|
|
|
(* Delta_00 = {% $\sum_{j \ne x} delta_j c_j / c_x$ %} *)
|
|
|
|
f *. ( (gemm ~transa:`T delta psi).{state,state} -.
|
|
|
|
delta.{column_idx,state} *. psi.{column_idx,state} )
|
2019-03-22 22:42:20 +01:00
|
|
|
in
|
2019-05-29 09:04:43 +02:00
|
|
|
Printf.printf "Delta_00 : %e %e\n" delta.{column_idx,state} delta_00;
|
2019-08-26 22:34:41 +02:00
|
|
|
|
2019-05-07 17:00:44 +02:00
|
|
|
delta.{column_idx,state} <- delta.{column_idx,state} -. delta_00;
|
2019-03-22 22:42:20 +01:00
|
|
|
|
2019-03-23 01:06:38 +01:00
|
|
|
|
2019-08-26 22:34:41 +02:00
|
|
|
let eigenvectors, eigenvalues =
|
|
|
|
|
|
|
|
let delta = lacpy delta in
|
|
|
|
Mat.scal f delta;
|
|
|
|
for k=1 to state-1 do
|
|
|
|
for i=1 to Mat.dim1 delta do
|
|
|
|
delta.{i,k} <- delta.{i,state}
|
|
|
|
done;
|
|
|
|
done;
|
|
|
|
let diagonal =
|
|
|
|
Vec.init (Matrix.dim1 m_H) (fun i ->
|
|
|
|
if i = column_idx then
|
|
|
|
Matrix.get m_H i i +. delta.{column_idx,state}
|
|
|
|
else
|
|
|
|
Matrix.get m_H i i
|
|
|
|
)
|
2019-03-22 22:42:20 +01:00
|
|
|
in
|
2019-08-26 22:34:41 +02:00
|
|
|
|
|
|
|
let matrix_prod c =
|
|
|
|
let w =
|
|
|
|
Matrix.mm ~transa:`T m_H c
|
|
|
|
|> Matrix.to_mat
|
2019-03-22 23:42:35 +01:00
|
|
|
in
|
2019-08-26 22:34:41 +02:00
|
|
|
let c = Matrix.to_mat c in
|
|
|
|
|
|
|
|
for k=1 to state do
|
|
|
|
for i=1 to (Mat.dim1 w) do
|
|
|
|
w.{i,k} <- w.{i,k} +. delta.{i,k} *. c.{column_idx, k} ;
|
|
|
|
w.{column_idx,k} <- w.{column_idx,k} +. delta.{i,k} *. c.{i,k};
|
|
|
|
done;
|
|
|
|
w.{column_idx,k} <- w.{column_idx,k} -.
|
|
|
|
delta.{column_idx,k} *. c.{column_idx,k};
|
|
|
|
done;
|
|
|
|
|
|
|
|
Matrix.dense_of_mat w
|
|
|
|
in
|
|
|
|
|
|
|
|
|
2019-03-21 21:48:21 +01:00
|
|
|
Parallel.broadcast (lazy (
|
2019-08-26 22:34:41 +02:00
|
|
|
Davidson.make ~threshold:1.e-9 ~guess:psi ~n_states:state diagonal matrix_prod
|
2019-03-21 21:48:21 +01:00
|
|
|
))
|
2019-08-26 22:34:41 +02:00
|
|
|
|
2019-03-21 21:48:21 +01:00
|
|
|
in
|
2019-08-26 22:34:41 +02:00
|
|
|
|
|
|
|
|
2019-05-07 17:00:44 +02:00
|
|
|
Vec.iter (fun energy -> Printf.printf "%g\t" energy) eigenvalues;
|
|
|
|
print_newline ();
|
2019-03-23 01:06:38 +01:00
|
|
|
|
2019-03-22 19:15:59 +01:00
|
|
|
let conv =
|
|
|
|
1.0 -. abs_float ( dot
|
|
|
|
(Mat.to_col_vecs psi).(0)
|
|
|
|
(Mat.to_col_vecs eigenvectors).(0) )
|
2019-03-21 21:48:21 +01:00
|
|
|
in
|
2019-03-23 14:54:59 +01:00
|
|
|
if Parallel.master then
|
2019-10-03 16:58:15 +02:00
|
|
|
Printf.printf "F12 Convergence : %e %f\n" conv (eigenvalues.{state}
|
2019-03-25 16:50:36 +01:00
|
|
|
+. Simulation.nuclear_repulsion simulation);
|
2019-03-23 01:06:38 +01:00
|
|
|
|
2019-10-14 14:16:28 +02:00
|
|
|
(*
|
|
|
|
let cabs_singles =
|
|
|
|
let f =
|
|
|
|
Fock.make_rhf ~density ~ao_basis:large_ao_basis
|
|
|
|
in
|
|
|
|
in
|
|
|
|
*)
|
|
|
|
|
2019-03-21 21:48:21 +01:00
|
|
|
if conv > threshold then
|
2019-05-07 17:00:44 +02:00
|
|
|
iteration ~state eigenvectors
|
2019-03-21 21:48:21 +01:00
|
|
|
else
|
|
|
|
let eigenvalues =
|
2019-10-03 16:58:15 +02:00
|
|
|
Vec.map (fun x -> x +. ci.CI.e_shift) eigenvalues
|
2019-03-21 21:48:21 +01:00
|
|
|
in
|
|
|
|
eigenvectors, eigenvalues
|
|
|
|
in
|
2019-05-07 17:00:44 +02:00
|
|
|
iteration ~state ci_coef
|
2019-03-22 00:34:00 +01:00
|
|
|
|
2019-03-21 21:48:21 +01:00
|
|
|
)
|
|
|
|
in
|
2019-10-03 16:58:15 +02:00
|
|
|
{ mo_basis ; det_space ; ci ; hf12_integrals ; eigensystem }
|
|
|
|
|
|
|
|
|
2019-03-21 00:44:10 +01:00
|
|
|
|
|
|
|
|