2019-03-21 00:44:10 +01:00
|
|
|
open Lacaml.D
|
|
|
|
|
|
|
|
type t =
|
|
|
|
{
|
|
|
|
mo_basis : MOBasis.t ;
|
|
|
|
aux_basis : MOBasis.t ;
|
|
|
|
det_space : DeterminantSpace.t ;
|
|
|
|
ci : CI.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
|
|
|
|
let mo_class t = DeterminantSpace.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
|
|
|
let f12_integrals mo_basis =
|
2019-03-22 18:16:41 +01:00
|
|
|
let two_e_ints = MOBasis.f12_ints mo_basis in
|
2019-03-25 09:17:13 +01:00
|
|
|
( (fun _ _ _ -> 0.),
|
2019-03-21 00:44:10 +01:00
|
|
|
(fun i j k l s s' ->
|
2019-03-28 09:37:43 +01:00
|
|
|
if (i=k && j<>l) || (j=l && i<>k) then
|
|
|
|
0.
|
2019-03-21 00:44:10 +01:00
|
|
|
else
|
2019-03-28 09:37:43 +01:00
|
|
|
begin
|
|
|
|
if s' = Spin.other s then
|
|
|
|
0.5 *. (F12.get_phys two_e_ints i j k l)
|
|
|
|
else
|
|
|
|
0.25 *. ((F12.get_phys two_e_ints i j k l) -.
|
|
|
|
(F12.get_phys two_e_ints i j l k))
|
|
|
|
end
|
2019-03-21 00:44:10 +01:00
|
|
|
) )
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-03-21 11:02:58 +01:00
|
|
|
|
2019-03-21 00:44:10 +01:00
|
|
|
let h_ij mo_basis ki kj =
|
|
|
|
let integrals =
|
|
|
|
List.map (fun f -> f mo_basis)
|
|
|
|
[ CI.h_integrals ]
|
|
|
|
in
|
|
|
|
CIMatrixElement.make integrals ki kj
|
|
|
|
|> List.hd
|
|
|
|
|
|
|
|
|
2019-03-25 09:17:13 +01:00
|
|
|
let hf_ij mo_basis ki kj =
|
2019-03-21 00:44:10 +01:00
|
|
|
let integrals =
|
|
|
|
List.map (fun f -> f mo_basis)
|
2019-03-25 09:17:13 +01:00
|
|
|
[ CI.h_integrals ; f12_integrals ]
|
2019-03-21 00:44:10 +01:00
|
|
|
in
|
|
|
|
CIMatrixElement.make integrals ki kj
|
2019-03-25 09:17:13 +01:00
|
|
|
|
|
|
|
|
2019-03-25 15:20:01 +01:00
|
|
|
let f_ij mo_basis ki kj =
|
2019-03-21 00:44:10 +01:00
|
|
|
let integrals =
|
|
|
|
List.map (fun f -> f mo_basis)
|
|
|
|
[ f12_integrals ]
|
|
|
|
in
|
2019-03-25 15:20:01 +01:00
|
|
|
CIMatrixElement.make integrals ki kj
|
|
|
|
|> List.hd
|
2019-03-21 00:44:10 +01:00
|
|
|
|
|
|
|
|
2019-03-22 18:16:41 +01:00
|
|
|
let is_internal det_space =
|
2019-03-26 01:20:17 +01:00
|
|
|
let mo_class = DeterminantSpace.mo_class det_space in
|
|
|
|
let mo_num = Array.length @@ MOClass.mo_class_array mo_class in
|
2019-03-22 18:16:41 +01:00
|
|
|
let m l =
|
|
|
|
List.fold_left (fun accu i ->
|
2019-03-26 01:20:17 +01:00
|
|
|
let j = i-1 in Bitstring.logor accu (Bitstring.shift_left_one mo_num j)
|
|
|
|
) (Bitstring.zero mo_num) l
|
2019-03-22 18:16:41 +01:00
|
|
|
in
|
2019-03-26 01:20:17 +01:00
|
|
|
let aux_mask = m (MOClass.auxiliary_mos mo_class) in
|
2019-03-22 18:16:41 +01:00
|
|
|
fun a ->
|
|
|
|
let alfa =
|
|
|
|
Determinant.alfa a
|
|
|
|
|> Spindeterminant.bitstring
|
|
|
|
in
|
2019-03-26 01:20:17 +01:00
|
|
|
let beta =
|
|
|
|
Determinant.beta a
|
|
|
|
|> Spindeterminant.bitstring
|
|
|
|
in
|
|
|
|
let a = Bitstring.logand aux_mask alfa
|
|
|
|
and b = Bitstring.logand aux_mask beta
|
2019-03-28 09:37:43 +01:00
|
|
|
in
|
|
|
|
match Bitstring.popcount a + Bitstring.popcount b with
|
|
|
|
| 1 | 2 -> false
|
|
|
|
| _ -> true
|
2019-03-26 01:20:17 +01:00
|
|
|
|
2019-03-22 18:16:41 +01:00
|
|
|
|
2019-03-26 10:38:50 +01:00
|
|
|
let dressing_vector ~frozen_core aux_basis 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-03-25 10:54:23 +01:00
|
|
|
|
2019-03-28 09:37:43 +01:00
|
|
|
(* Determinants of the FCI space as a list *)
|
|
|
|
let in_dets =
|
2019-03-29 19:01:51 +01:00
|
|
|
DeterminantSpace.determinant_stream ci.CI.det_space
|
|
|
|
|> Util.stream_to_list
|
2019-03-28 09:37:43 +01:00
|
|
|
in
|
2019-03-25 16:50:36 +01:00
|
|
|
|
2019-03-28 09:37:43 +01:00
|
|
|
(* Stream that generates only singly and doubly excited determinants
|
|
|
|
wrt FCI space *)
|
|
|
|
let out_dets_stream =
|
2019-03-29 19:01:51 +01:00
|
|
|
(* Stream that generates all determinants of FCI space *)
|
|
|
|
let s =
|
|
|
|
DeterminantSpace.fci_of_mo_basis ~frozen_core aux_basis
|
|
|
|
|> DeterminantSpace.determinant_stream
|
2019-03-28 09:37:43 +01:00
|
|
|
|
2019-03-29 19:01:51 +01:00
|
|
|
in
|
|
|
|
(* Select only singly and doubly excited determinants
|
|
|
|
wrt FCI space *)
|
|
|
|
Stream.from (fun _ ->
|
|
|
|
try
|
|
|
|
let rec result () =
|
|
|
|
let ki = Stream.next s in
|
|
|
|
if is_internal ci.CI.det_space ki then
|
|
|
|
result ()
|
|
|
|
else
|
|
|
|
Some ki
|
|
|
|
in
|
|
|
|
result ()
|
|
|
|
with Stream.Failure -> None
|
|
|
|
)
|
2019-03-28 09:37:43 +01:00
|
|
|
in
|
2019-03-25 16:50:36 +01:00
|
|
|
|
2019-03-28 10:32:54 +01:00
|
|
|
let make_h_and_f alpha_list =
|
2019-03-28 09:37:43 +01:00
|
|
|
|
|
|
|
let rec col_vecs_list accu_H accu_F = function
|
2019-03-28 10:32:54 +01:00
|
|
|
| [] ->
|
2019-03-28 09:37:43 +01:00
|
|
|
List.rev accu_H,
|
|
|
|
List.rev accu_F
|
2019-03-28 10:32:54 +01:00
|
|
|
| ki :: rest ->
|
2019-03-28 09:37:43 +01:00
|
|
|
let h, f =
|
|
|
|
List.map (fun kj ->
|
|
|
|
match hf_ij aux_basis ki kj with
|
|
|
|
| [ a ; b ] -> a, b
|
|
|
|
| _ -> assert false ) in_dets
|
|
|
|
|> List.split
|
|
|
|
in
|
|
|
|
let h =
|
|
|
|
Vec.of_list h
|
|
|
|
and f =
|
|
|
|
Vec.of_list f
|
|
|
|
in
|
2019-03-28 10:32:54 +01:00
|
|
|
col_vecs_list (h::accu_H) (f::accu_F) rest
|
2019-03-25 09:17:13 +01:00
|
|
|
in
|
|
|
|
let h, f =
|
2019-03-28 10:32:54 +01:00
|
|
|
col_vecs_list [] [] alpha_list
|
2019-03-25 09:17:13 +01:00
|
|
|
in
|
2019-03-28 09:37:43 +01:00
|
|
|
Mat.of_col_vecs_list h,
|
2019-03-25 09:17:13 +01:00
|
|
|
Mat.of_col_vecs_list f
|
2019-03-22 18:16:41 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
|
|
|
|
let m_HF =
|
2019-03-29 19:01:51 +01:00
|
|
|
let batch_size = 1 + 1_000_000 / (Mat.dim1 f12_amplitudes) in
|
2019-03-28 10:32:54 +01:00
|
|
|
let input_stream =
|
|
|
|
Stream.from (fun i ->
|
|
|
|
let rec make_batch accu = function
|
|
|
|
| 0 -> accu
|
|
|
|
| n -> try
|
|
|
|
let alpha = Stream.next out_dets_stream in
|
|
|
|
let accu = alpha :: accu in
|
|
|
|
make_batch accu (n-1)
|
|
|
|
with Stream.Failure -> accu
|
|
|
|
in
|
|
|
|
let result = make_batch [] batch_size in
|
|
|
|
if result = [] then None else Some result
|
|
|
|
)
|
|
|
|
in
|
2019-03-28 09:37:43 +01:00
|
|
|
let result =
|
2019-04-05 18:05:56 +02:00
|
|
|
let x =
|
|
|
|
try [ Stream.next out_dets_stream ]
|
|
|
|
with Stream.Failure -> failwith "Auxiliary basis set does not produce any excited determinant"
|
|
|
|
in
|
|
|
|
let m_H_aux, m_F_aux = make_h_and_f x in
|
2019-03-28 10:32:54 +01:00
|
|
|
let m_HF =
|
|
|
|
gemm m_H_aux m_F_aux ~transb:`T
|
|
|
|
in
|
|
|
|
gemm m_HF f12_amplitudes
|
2019-03-28 09:37:43 +01:00
|
|
|
in
|
2019-03-28 10:32:54 +01:00
|
|
|
|
|
|
|
let iteration input =
|
2019-03-29 19:01:51 +01:00
|
|
|
Printf.printf ".%!";
|
2019-03-28 10:32:54 +01:00
|
|
|
let m_H_aux, m_F_aux = make_h_and_f input in
|
|
|
|
let m_HF =
|
2019-03-28 09:37:43 +01:00
|
|
|
gemm m_H_aux m_F_aux ~transb:`T
|
|
|
|
in
|
2019-03-28 10:32:54 +01:00
|
|
|
gemm m_HF f12_amplitudes
|
|
|
|
in
|
|
|
|
|
|
|
|
input_stream
|
|
|
|
|> Farm.run ~ordered:false ~f:iteration
|
|
|
|
|> Stream.iter (fun hf ->
|
|
|
|
ignore @@ Mat.add result hf ~c:result );
|
2019-03-29 19:01:51 +01:00
|
|
|
Printf.printf "\n";
|
|
|
|
Parallel.broadcast (lazy result)
|
2019-03-25 19:28:38 +01:00
|
|
|
in
|
|
|
|
|
2019-04-05 18:05:56 +02:00
|
|
|
if Parallel.master then Printf.printf "Done\n%!";
|
2019-03-28 10:32:54 +01:00
|
|
|
Matrix.dense_of_mat m_HF
|
2019-03-21 00:44:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-03-23 15:54:46 +01:00
|
|
|
let make ~simulation ?(threshold=1.e-12) ~frozen_core ~mo_basis ~aux_basis_filename () =
|
2019-03-21 00:44:10 +01:00
|
|
|
|
2019-03-26 10:38:50 +01:00
|
|
|
let f12 = Util.of_some @@ Simulation.f12 simulation in
|
2019-03-21 00:44:10 +01:00
|
|
|
let mo_num = MOBasis.size mo_basis in
|
|
|
|
|
2019-03-28 09:37:43 +01:00
|
|
|
Printf.printf "Add aux basis\n%!";
|
2019-03-21 00:44:10 +01:00
|
|
|
(* Add auxiliary basis set *)
|
|
|
|
let s =
|
|
|
|
let charge = Charge.to_int @@ Simulation.charge simulation
|
|
|
|
and multiplicity = Electrons.multiplicity @@ Simulation.electrons simulation
|
|
|
|
and nuclei = Simulation.nuclei simulation
|
|
|
|
in
|
|
|
|
let general_basis =
|
2019-03-21 21:48:21 +01:00
|
|
|
Basis.general_basis @@ Simulation.basis simulation
|
2019-03-21 00:44:10 +01:00
|
|
|
in
|
|
|
|
GeneralBasis.combine [
|
2019-03-21 21:48:21 +01:00
|
|
|
general_basis ; GeneralBasis.read aux_basis_filename
|
|
|
|
]
|
2019-03-21 00:44:10 +01:00
|
|
|
|> Basis.of_nuclei_and_general_basis nuclei
|
2019-03-26 10:38:50 +01:00
|
|
|
|> Simulation.make ~f12 ~charge ~multiplicity ~nuclei
|
2019-03-21 00:44:10 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
let aux_basis =
|
|
|
|
MOBasis.of_mo_basis s mo_basis
|
|
|
|
in
|
2019-03-28 09:37:43 +01:00
|
|
|
let () =
|
|
|
|
Printf.printf "F12 ints\n%!";
|
|
|
|
ignore @@ MOBasis.f12_ints aux_basis
|
|
|
|
in
|
|
|
|
let () =
|
|
|
|
Printf.printf "2e ints\n%!";
|
|
|
|
ignore @@ MOBasis.two_e_ints aux_basis
|
|
|
|
in
|
2019-03-21 00:44:10 +01:00
|
|
|
|
2019-03-28 09:37:43 +01:00
|
|
|
Printf.printf "det space\n%!";
|
2019-03-21 00:44:10 +01:00
|
|
|
let det_space =
|
2019-03-21 21:48:21 +01:00
|
|
|
DeterminantSpace.fci_f12_of_mo_basis aux_basis ~frozen_core mo_num
|
2019-03-21 00:44:10 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
let ci = CI.make det_space in
|
2019-03-21 11:02:58 +01:00
|
|
|
|
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-22 00:34:00 +01:00
|
|
|
let e_shift =
|
2019-03-21 21:48:21 +01:00
|
|
|
let det =
|
|
|
|
DeterminantSpace.determinant_stream det_space
|
|
|
|
|> Stream.next
|
|
|
|
in
|
2019-03-22 00:34:00 +01:00
|
|
|
h_ij aux_basis det det
|
2019-03-21 21:48:21 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
let rec iteration ?(state=1) psi =
|
2019-03-22 00:34:00 +01:00
|
|
|
let delta =
|
2019-03-28 09:37:43 +01:00
|
|
|
dressing_vector ~frozen_core aux_basis psi ci
|
2019-03-22 00:34:00 +01:00
|
|
|
in
|
|
|
|
|
2019-03-22 22:42:20 +01:00
|
|
|
let f = 1.0 /. psi.{1,1} in
|
|
|
|
let delta_00 =
|
|
|
|
Util.list_range 2 (Mat.dim1 psi)
|
|
|
|
|> List.fold_left (fun accu i -> accu +.
|
|
|
|
(Matrix.get delta i 1) *. psi.{i,1} *. f) 0.
|
|
|
|
in
|
|
|
|
let delta = Matrix.to_mat delta in
|
|
|
|
delta.{1,1} <- delta.{1,1} -. delta_00;
|
|
|
|
|
2019-03-22 00:34:00 +01:00
|
|
|
(*------
|
2019-03-22 19:15:59 +01:00
|
|
|
TODO SINGLE STATE HERE
|
2019-03-22 00:34:00 +01:00
|
|
|
*)
|
|
|
|
let n_states = ci.CI.n_states in
|
2019-03-21 21:48:21 +01:00
|
|
|
let diagonal =
|
2019-03-22 23:42:35 +01:00
|
|
|
Vec.init (Matrix.dim1 m_H) (fun i ->
|
|
|
|
if i = 1 then
|
|
|
|
Matrix.get m_H i i +. delta.{1,1} *. f
|
|
|
|
else
|
|
|
|
Matrix.get m_H i i
|
|
|
|
)
|
2019-03-21 21:48:21 +01:00
|
|
|
in
|
2019-03-23 01:06:38 +01:00
|
|
|
|
2019-03-22 00:34:00 +01:00
|
|
|
let matrix_prod c =
|
2019-03-22 22:42:20 +01:00
|
|
|
let w =
|
|
|
|
Matrix.mm ~transa:`T m_H c
|
|
|
|
|> Matrix.to_mat
|
|
|
|
in
|
2019-03-22 23:42:35 +01:00
|
|
|
let c11 = Matrix.get c 1 1 in
|
2019-03-22 22:42:20 +01:00
|
|
|
Util.list_range 1 (Mat.dim1 w)
|
|
|
|
|> List.iter (fun i ->
|
2019-03-22 23:42:35 +01:00
|
|
|
let dci =
|
|
|
|
delta.{i,1} *. f ;
|
|
|
|
in
|
|
|
|
w.{i,1} <- w.{i,1} +. dci *. c11;
|
2019-03-22 22:42:20 +01:00
|
|
|
if (i <> 1) then
|
2019-03-22 23:42:35 +01:00
|
|
|
w.{1,1} <- w.{1,1} +. dci *. (Matrix.get c i 1);
|
2019-03-22 22:42:20 +01:00
|
|
|
);
|
|
|
|
Matrix.dense_of_mat w
|
2019-03-21 21:48:21 +01:00
|
|
|
in
|
2019-03-23 01:06:38 +01:00
|
|
|
|
2019-03-21 21:48:21 +01:00
|
|
|
let eigenvectors, eigenvalues =
|
|
|
|
Parallel.broadcast (lazy (
|
2019-03-22 23:42:35 +01:00
|
|
|
Davidson.make ~threshold:1.e-6 ~guess:psi ~n_states diagonal matrix_prod
|
2019-03-21 21:48:21 +01:00
|
|
|
))
|
|
|
|
in
|
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-03-25 16:50:36 +01:00
|
|
|
Printf.printf "F12 Convergence : %e %f\n" conv (eigenvalues.{1} +. e_shift
|
|
|
|
+. Simulation.nuclear_repulsion simulation);
|
2019-03-23 01:06:38 +01:00
|
|
|
|
2019-03-21 21:48:21 +01:00
|
|
|
if conv > threshold then
|
2019-03-22 19:15:59 +01:00
|
|
|
iteration eigenvectors
|
2019-03-21 21:48:21 +01:00
|
|
|
else
|
|
|
|
let eigenvalues =
|
|
|
|
Vec.map (fun x -> x +. e_shift) eigenvalues
|
|
|
|
in
|
|
|
|
eigenvectors, eigenvalues
|
|
|
|
in
|
2019-03-22 19:15:59 +01:00
|
|
|
iteration ci_coef
|
2019-03-22 00:34:00 +01:00
|
|
|
|
2019-03-21 21:48:21 +01:00
|
|
|
)
|
|
|
|
in
|
2019-03-26 10:38:50 +01:00
|
|
|
{ mo_basis ; aux_basis ; det_space ; ci ; eigensystem }
|
2019-03-21 00:44:10 +01:00
|
|
|
|
|
|
|
|