From 7dba59e4cd655314d0392a50a0f6d477b767c1a0 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Thu, 8 Mar 2018 23:29:08 +0100 Subject: [PATCH] Removed gamess parser --- .ocamlinit | 14 +----- Basis/Basis.ml | 4 +- Basis/Basis.mli | 4 +- Basis/BasisLexer.mll | 33 ------------- Basis/GamessParser.mly | 49 ------------------- Basis/GamessReader.ml | 18 ------- Basis/GeneralBasis.ml | 82 ++++++++++++++++++++++++++++--- Basis/GeneralBasis.mli | 108 +++++++++++++++++++++++++++++++++++++++++ Simulation.ml | 4 +- 9 files changed, 190 insertions(+), 126 deletions(-) delete mode 100644 Basis/BasisLexer.mll delete mode 100644 Basis/GamessParser.mly delete mode 100644 Basis/GamessReader.ml create mode 100644 Basis/GeneralBasis.mli diff --git a/.ocamlinit b/.ocamlinit index 55aa028..7d69cc6 100644 --- a/.ocamlinit +++ b/.ocamlinit @@ -7,22 +7,10 @@ let () = #require "lacaml";; #directory "_build";; #directory "_build/Utils";; +#directory "_build/Nuclei";; #directory "_build/Basis";; #directory "_build/HartreeFock";; -#load "Powers.cmo";; -#load "Zkey.cmo";; -#load "AngularMomentum.cmo";; -#load "Constants.cmo";; -#load "Util.cmo";; -#load "Coordinate.cmo";; -#load "Zmap.cmo";; -#load "Zkey.cmo";; -#load "ContractedShell.cmo";; -#load "ShellPair.cmo";; -#load "TwoElectronRR.cmo";; -#load "ERI.cmo";; - #use "topfind";; diff --git a/Basis/Basis.ml b/Basis/Basis.ml index 3795ba1..a56f7c7 100644 --- a/Basis/Basis.ml +++ b/Basis/Basis.ml @@ -65,9 +65,9 @@ let to_string b = -let of_nuclei_and_basis_filename ~nuclei ~filename = +let of_nuclei_and_basis_filename ~nuclei filename = let general_basis = - GamessReader.read ~filename + GeneralBasis.read filename in of_nuclei_and_general_basis nuclei general_basis diff --git a/Basis/Basis.mli b/Basis/Basis.mli index 3b9dc2c..ae57a2d 100644 --- a/Basis/Basis.mli +++ b/Basis/Basis.mli @@ -11,7 +11,7 @@ val to_string : t -> string (** Pretty prints the basis set in a string. *) -val of_nuclei_and_general_basis : Nuclei.t -> GeneralBasis.t list -> t +val of_nuclei_and_general_basis : Nuclei.t -> GeneralBasis.t -> t (** Takes an array of {!Nuclei.t}, and a {!GeneralBasis.t} (such as cc-pVDZ for instance) and creates the corresponding atomic basis set. All the {!Element.t}s of the array of {!Nuclei.t} are searched in @@ -22,7 +22,7 @@ val of_nuclei_and_general_basis : Nuclei.t -> GeneralBasis.t list -> t -val of_nuclei_and_basis_filename : nuclei:Nuclei.t -> filename:string -> t +val of_nuclei_and_basis_filename : nuclei:Nuclei.t -> string -> t (** Same as {!of_nuclei_and_general_basis}, but taking the {!GeneralBasis.t} from a file. *) diff --git a/Basis/BasisLexer.mll b/Basis/BasisLexer.mll deleted file mode 100644 index 9162984..0000000 --- a/Basis/BasisLexer.mll +++ /dev/null @@ -1,33 +0,0 @@ -{ -exception SyntaxError of string - -open GamessParser - -} - -let eol = ['\n'] -let white = [' ' '\t']+ -let element = ['A'-'Z' 'a'-'z']+ white? eol -let ang_mom = ['S' 'P' 'D' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' - 's' 'p' 'd' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' ] - white -let integer = ['0'-'9']+ -let real = '-'? integer '.' integer (['e' 'E'] ('+'|'-')? integer)? - - -rule read_all_rule = parse - | eol { EOL } - | white { SPACE } - | ang_mom as a { ANG_MOM (a.[0]) } - | element as e { ELEMENT (String.trim e) } - | integer as i { INTEGER (int_of_string i) } - | real as f { FLOAT (float_of_string f) } - | eof { EOF } - - -{ - let rec read_all lexbuf = - match read_all_rule lexbuf with - | SPACE -> read_all_rule lexbuf - | x -> x -} diff --git a/Basis/GamessParser.mly b/Basis/GamessParser.mly deleted file mode 100644 index 35e3c68..0000000 --- a/Basis/GamessParser.mly +++ /dev/null @@ -1,49 +0,0 @@ -/* Parses basis sets GAMESS format */ - -%{ - -%} - -%token ELEMENT -%token ANG_MOM -%token INTEGER -%token FLOAT -%token SPACE -%token EOL -%token EOF - -%start input -%type input - -%% /* Grammar rules and actions follow */ - -input: - | basis { $1 } - | EOL input { $2 } - -basis: - | element shell_array EOL { ($1, $2) } - | element shell_array EOF { ($1, $2) } - -element: - | ELEMENT { Element.of_string $1 } - -shell_array: - | shell_list { Array.of_list @@ List.rev $1 } - -shell_list: - | { [] } - | shell_list shell { $2 :: $1 } - -shell: - | ANG_MOM INTEGER EOL primitive_list { (AngularMomentum.of_char $1, Array.of_list @@ List.rev $4 ) } - -primitive_list: - | { [] } - | primitive_list primitive { $2 :: $1 } - -primitive: - | INTEGER FLOAT FLOAT EOL { GeneralBasis.{exponent=$2 ; coefficient=$3 } } - - - diff --git a/Basis/GamessReader.ml b/Basis/GamessReader.ml deleted file mode 100644 index eca4fb9..0000000 --- a/Basis/GamessReader.ml +++ /dev/null @@ -1,18 +0,0 @@ -(** Read a basis set file in GAMESS format and return an association list where the key is an - Element.t and the value is the parsed basis set. *) -let read ~filename = - let lexbuf = - let ic = open_in filename in - Lexing.from_channel ic - in - let rec aux accu = - try - let key, basis = - GamessParser.input BasisLexer.read_all lexbuf - in - aux ((key, basis)::accu) - with - | Parsing.Parse_error -> List.rev accu - in - aux [] - diff --git a/Basis/GeneralBasis.ml b/Basis/GeneralBasis.ml index 470c3d6..73abc9e 100644 --- a/Basis/GeneralBasis.ml +++ b/Basis/GeneralBasis.ml @@ -1,16 +1,84 @@ (** General basis set read from a file *) -type primitive = { - exponent: float ; - coefficient: float - } + +type primitive = + { + exponent : float ; + coefficient: float ; + } type general_contracted_shell = AngularMomentum.t * (primitive array) -type t = Element.t * (general_contracted_shell array) +type element_basis = Element.t * (general_contracted_shell array) + +type t = element_basis list -module Am = AngularMomentum +exception No_shell +exception Malformed_shell of string + +let read_shell ic = + try + + let shell, n = + try + Scanf.sscanf (input_line ic) " %c %d " (fun shell n -> shell, n) + with + | End_of_file -> raise No_shell + | Scanf.Scan_failure m -> raise (Malformed_shell m) + in + + let rec loop = function + | 0 -> [] + | i -> let contraction = + let line = (input_line ic) in + try Scanf.sscanf line " %_d %f %f " + (fun exponent coefficient -> { exponent ; coefficient }) + with _ -> raise (Malformed_shell (Printf.sprintf + "Expected %d %c contractions, error at contraction %d:\n%s" + n shell (n-i+1) line)) + in + contraction :: loop (pred i) + in + Some (AngularMomentum.of_char shell, Array.of_list (loop n)) + + with + | No_shell -> None + + + + +let read_element ic = + try + + let element = + Scanf.sscanf (input_line ic) " %s " Element.of_string + in + + let rec loop () = + match read_shell ic with + | Some shell -> shell :: loop () + | None -> [] + in + Some (element, Array.of_list (loop ()) ) + + with + | Element.ElementError _ -> None + | End_of_file -> None + + + +let read filename = + let ic = open_in filename in + let rec loop () = + match read_element ic with + | Some e -> e :: loop () + | None -> [] + in + loop () + + + let string_of_primitive ?id prim = match id with | None -> (string_of_float prim.exponent)^" "^(string_of_float prim.coefficient) @@ -22,7 +90,7 @@ let string_of_contracted_shell (angular_momentum, prim_array) = Array.length prim_array in Printf.sprintf "%s %d\n%s" - (Am.to_string angular_momentum) n + (AngularMomentum.to_string angular_momentum) n (Array.init n (fun i -> string_of_primitive ~id:(i+1) prim_array.(i)) |> Array.to_list |> String.concat "\n") diff --git a/Basis/GeneralBasis.mli b/Basis/GeneralBasis.mli new file mode 100644 index 0000000..40aad8f --- /dev/null +++ b/Basis/GeneralBasis.mli @@ -0,0 +1,108 @@ +(** General basis set read from a file. + +Basis set files are stored in GAMESS format, for example: +{[ +HYDROGEN +S 3 + 1 13.0100000 0.0196850 + 2 1.9620000 0.1379770 + 3 0.4446000 0.4781480 +S 1 + 1 0.1220000 1.0000000 +P 1 + 1 0.7270000 1.0000000 + +HELIUM +S 3 + 1 38.3600000 0.0238090 + 2 5.7700000 0.1548910 + 3 1.2400000 0.4699870 +S 1 + 1 0.2976000 1.0000000 +P 1 + 1 1.2750000 1.0000000 +]} + +When building the atomic basis set of a molecular system, the basis functions +are created by picking the data from the general basis set. This data structure +simplifies the creation of the atomic basis set. +*) + +type primitive = private + { + exponent : float ; + coefficient: float ; + } + +type general_contracted_shell = AngularMomentum.t * (primitive array) + +type element_basis = Element.t * (general_contracted_shell array) + +type t = element_basis list + + +exception No_shell +exception Malformed_shell of string + + +val read : string -> t +(** Reads a basis set file and return an association list where + the key is an {!Element.t} and the value is the parsed basis set. +*) + + +val read_element : in_channel -> element_basis option +(** Reads an element from the input channel [ic]. For example, +{[ +HYDROGEN +S 3 + 1 13.0100000 0.0196850 + 2 1.9620000 0.1379770 + 3 0.4446000 0.4781480 +S 1 + 1 0.1220000 1.0000000 +P 1 + 1 0.7270000 1.0000000 + +}] +returns +{[ +Some + (Element.H, + [(AngularMomentum.S, + [{exponent = 13.01; coefficient = 0.019685}; + {exponent = 1.962; coefficient = 0.137977}; + {exponent = 0.4446; coefficient = 0.478148}]); + (AngularMomentum.S, [{exponent = 0.122; coefficient = 1.}]); + (AngularMomentum.P, [{exponent = 0.727; coefficient = 1.}])]) +]} + +@raise Malformed_shell if the input is not well formed. +*) + + +val read_shell : in_channel -> general_contracted_shell option +(** Reads a shell from the input channel [ic]. For example, +{[ +S 3 + 1 13.0100000 0.0196850 + 2 1.9620000 0.1379770 + 3 0.4446000 0.4781480 +]} +returns +{[ +Some + (AngularMomentum.S, + [{exponent = 13.01 ; coefficient = 0.019685}; + {exponent = 1.962 ; coefficient = 0.137977}; + {exponent = 0.4446; coefficient = 0.478148}]) +]} + +@raise Malformed_shell if the input is not well formed. +*) + + +val to_string : string * (general_contracted_shell array) -> string +(** Pretty-prints the basis set of an {Element.t}. *) + + diff --git a/Simulation.ml b/Simulation.ml index 157d1ed..193c0fa 100644 --- a/Simulation.ml +++ b/Simulation.ml @@ -37,12 +37,12 @@ let make ?multiplicity:(multiplicity=1) ?charge:(charge=0) ~nuclei basis = } -let of_filenames ?multiplicity:(multiplicity=1) ?charge:(charge=0) ~nuclei basis = +let of_filenames ?multiplicity:(multiplicity=1) ?charge:(charge=0) ~nuclei basis_filename = let nuclei = Nuclei.of_filename nuclei in let basis = - Basis.of_nuclei_and_basis_filename ~nuclei ~filename:basis + Basis.of_nuclei_and_basis_filename ~nuclei basis_filename in make ~charge ~multiplicity ~nuclei basis