From a692242b51d5a43254cce183f43ed5d179b7fbbd Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Mon, 7 Nov 2022 11:24:20 +0100 Subject: [PATCH] Improved reading basis --- gaussian/lib/general_basis.ml | 40 ++++++++++++++++++++++------------ gaussian/lib/general_basis.mli | 2 +- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/gaussian/lib/general_basis.ml b/gaussian/lib/general_basis.ml index c1e67c6..93c67e0 100644 --- a/gaussian/lib/general_basis.ml +++ b/gaussian/lib/general_basis.ml @@ -20,7 +20,7 @@ exception No_shell exception Malformed_shell of string -let read_shell line_stream = +let read_shell ?element line_stream = try let shell, n = @@ -40,9 +40,15 @@ let read_shell line_stream = let line = Stream.next line_stream 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)) + with _ -> raise (Malformed_shell + (match element with + | Some element -> Printf.sprintf + "In %s: Expected %d %c contractions.\nError at contraction %d:\n%s" + (Element.to_string element) n shell (n-i+1) line + | None -> Printf.sprintf + "Expected %d %c contractions.\nError at contraction %d:\n%s" + n shell (n-i+1) line + )) in contraction :: loop (pred i) in @@ -54,20 +60,26 @@ let read_shell line_stream = -let read_element line_stream = +let rec read_element line_stream = try let line = Stream.next line_stream in - let element = - Scanf.sscanf line " %s " Element.of_string - in + if String.length line = 0 || line.[0] = '!' then + read_element line_stream + else + let element = + try + Scanf.sscanf line " %s " Element.of_string + with + | Element.ElementError _ -> Element.X + in - let rec loop () = - match read_shell line_stream with - | Some shell -> shell :: loop () - | None -> [] - in - Some (element, Array.of_list (loop ()) ) + let rec loop () = + match read_shell line_stream ~element with + | Some shell -> shell :: loop () + | None -> [] + in + Some (element, Array.of_list (loop ()) ) with | Stream.Failure -> None diff --git a/gaussian/lib/general_basis.mli b/gaussian/lib/general_basis.mli index f087e6c..60c5899 100644 --- a/gaussian/lib/general_basis.mli +++ b/gaussian/lib/general_basis.mli @@ -100,7 +100,7 @@ Some *) -val read_shell : string Stream.t -> general_contracted_shell option +val read_shell : ?element:Element.t -> string Stream.t -> general_contracted_shell option (** Reads a shell from the input [string Stream]. The [string Stream] is a stream of lines, like a text file split on the end-of-line character. For example,