Skip to content

Commit

Permalink
Merge pull request #123 from MineralsCloud:fix-input
Browse files Browse the repository at this point in the history
Fix regex `CELL_PARAMETERS` to work both in input & output
  • Loading branch information
singularitti authored Oct 28, 2023
2 parents cafbbc9 + f24768d commit 68dd1a4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 157 deletions.
89 changes: 51 additions & 38 deletions src/PWscf/input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using QuantumESPRESSOBase.PWscf:
CellParametersCard,
PWInput

# This regular expression is taken from https://github.com/aiidateam/qe-tools/blob/aedee19/qe_tools/parsers/_input_base.py
# This regular expression is taken from https://github.com/aiidateam/qe-tools/blob/aedee19/qe_tools/parsers/_input_base.py#L374-L413.
const ATOMIC_POSITIONS_BLOCK = r"""
^ \s* ATOMIC_POSITIONS \s* # Atomic positions start with that string
[{(]? \s* (?P<units>\S+?)? \s* [)}]? \s* $\R # The units are after the string in optional brackets
Expand Down Expand Up @@ -62,29 +62,29 @@ const ATOMIC_POSITIONS_BLOCK = r"""
)+ # A positions block should be one or more lines
)
"""imx
# This regular expression is referenced from https://github.com/aiidateam/qe-tools/blob/aedee19/qe_tools/parsers/_input_base.py.
# This regular expression is referenced from https://github.com/aiidateam/qe-tools/blob/aedee19/qe_tools/parsers/_input_base.py#L548-L593.
const CELL_PARAMETERS_BLOCK = r"""
^ [ \t]*
^[ \t]*
CELL_PARAMETERS [ \t]*
[{(]? \s* (?P<option>[a-z]*) \s* [)}]? \h* (?:[\#!].*)? \v # Match option, do not match comment
[{(]? [ \t]* (?P<option>[a-zA-Z]*) [ \t]* =? [ \t]* ([-+]?[0-9]*\.[0-9]+|[0-9]+\.?[0-9]*)? [)}]? [ \t]* (?:[\#!].*)? \R # Match option, do not match comment
(?P<data>
(?:
\s* # White space in front of the element spec is ok
(?:
[-+]?(?:[0-9]*\.[0-9]+|[0-9]+\.?[0-9]*)(?:[ED][-+]?[0-9]+)? # First number
\s* # White space in front of the element spec is ok
(?:
\s+ # White space between numbers
[-+]?(?:[0-9]*\.[0-9]+|[0-9]+\.?[0-9]*)(?:[ED][-+]?[0-9]+)?
){2} # I expect 3 numbers
|
\# # If a line is commented out, that is also ok
|
! # If a line is commented out, that is also ok
)
.* # I do not care what is after the comment or the vector
| # Or
^\s*$ # A line only containing white space
){3} # I need exactly 3 vectors
[-+]?(?:[0-9]*\.[0-9]+|[0-9]+\.?[0-9]*)(?:[ED][-+]?[0-9]+)? # First number
\s+ # White space between numbers
[-+]?(?:[0-9]*\.[0-9]+|[0-9]+\.?[0-9]*)(?:[ED][-+]?[0-9]+)?
\s+
[-+]?(?:[0-9]*\.[0-9]+|[0-9]+\.?[0-9]*)(?:[ED][-+]?[0-9]+)?
|
\# # If a line is commented out, that is also ok
|
! # If a line is commented out, that is also ok
)
.* # I do not care what is after the comment or the vector
| # Or
^\s*$ # A line only containing white space
){3} # I need exactly 3 vectors
)
"""imx
# This regular expression is referenced from https://github.com/aiidateam/qe-tools/blob/aedee19/qe_tools/parsers/_input_base.py#L683-L691
Expand Down Expand Up @@ -239,14 +239,6 @@ function Base.tryparse(::Type{SpecialPointsCard}, str::AbstractString)
)
end
end
function Base.tryparse(::Type{KPointsCard}, str::AbstractString)
for T in (GammaPointCard, KMeshCard, SpecialPointsCard)
x = tryparse(T, str)
if x !== nothing
return x
end
end
end
function Base.tryparse(::Type{CellParametersCard}, str::AbstractString)
m = match(CELL_PARAMETERS_BLOCK, str)
# Function `match` only searches for the first match of the regular expression, so it could be a `nothing`
Expand All @@ -271,13 +263,29 @@ function Base.tryparse(::Type{CellParametersCard}, str::AbstractString)
end
end

function Base.parse(::Type{T}, str::AbstractString) where {T<:Card}
x = tryparse(T, str)
if x === nothing
throw(Meta.ParseError("cannot find card `$(groupname(T))`!"))
else
return x
end
function Base.parse(::Type{AtomicSpeciesCard}, str::AbstractString)
obj = tryparse(AtomicSpeciesCard, str)
isnothing(obj) ? throw(ParseError("no matched string found!")) : return obj
end
function Base.parse(::Type{AtomicPositionsCard}, str::AbstractString)
obj = tryparse(AtomicPositionsCard, str)
isnothing(obj) ? throw(ParseError("no matched string found!")) : return obj
end
function Base.parse(::Type{CellParametersCard}, str::AbstractString)
obj = tryparse(CellParametersCard, str)
isnothing(obj) ? throw(ParseError("no matched string found!")) : return obj
end
function Base.parse(::Type{GammaPointCard}, str::AbstractString)
obj = tryparse(GammaPointCard, str)
isnothing(obj) ? throw(ParseError("no matched string found!")) : return obj
end
function Base.parse(::Type{KMeshCard}, str::AbstractString)
obj = tryparse(KMeshCard, str)
isnothing(obj) ? throw(ParseError("no matched string found!")) : return obj
end
function Base.parse(::Type{SpecialPointsCard}, str::AbstractString)
obj = tryparse(SpecialPointsCard, str)
isnothing(obj) ? throw(ParseError("no matched string found!")) : return obj
end
function Base.parse(::Type{PWInput}, str::AbstractString)
args = []
Expand All @@ -288,12 +296,17 @@ function Base.parse(::Type{PWInput}, str::AbstractString)
nml = tryparse(T, str)
push!(args, f => (nml === nothing ? T() : nml))
end
for (f, T) in zip(
(:atomic_species, :atomic_positions, :k_points),
(AtomicSpeciesCard, AtomicPositionsCard, KPointsCard),
) # Must-have cards, or else error
for (f, T) in
zip((:atomic_species, :atomic_positions), (AtomicSpeciesCard, AtomicPositionsCard)) # Must-have cards, or else error
push!(args, f => parse(T, str))
end
for (T) in (GammaPointCard, KMeshCard, SpecialPointsCard)
card = tryparse(T, str)
if !isnothing(card)
push!(args, :k_points => card)
break
end
end
for (f, T) in zip((:cell_parameters,), (CellParametersCard,))
card = tryparse(T, str) # Optional cards
push!(args, f => card)
Expand Down
118 changes: 0 additions & 118 deletions src/PWscf/output/atomicstructure.jl

This file was deleted.

8 changes: 8 additions & 0 deletions src/PWscf/output/each.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export eachstep,
eachunconvergedenergy,
eachconvergedenergy,
each_energy_by_step,
eachcellparameterscard,
eachatomicpositionscard,
eachtimeditem

abstract type Each end
Expand Down Expand Up @@ -281,6 +283,12 @@ eachconvergedenergy(str::AbstractString) =

function each_energy_by_step end

eachcellparameterscard(str::AbstractString) =
EachParsed{CellParametersCard}(CELL_PARAMETERS_BLOCK, str)

eachatomicpositionscard(str::AbstractString) =
EachParsed{AtomicPositionsCard}(ATOMIC_POSITIONS_BLOCK, str)

struct TimedItem <: PWOutputItem
name::String
cpu::Millisecond
Expand Down
1 change: 0 additions & 1 deletion src/PWscf/output/output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ abstract type PWOutputItem end
include("regexes.jl")
include("once.jl")
include("each.jl")
include("atomicstructure.jl")
include("misc.jl")

function parse_symmetries(str::AbstractString)
Expand Down

0 comments on commit 68dd1a4

Please sign in to comment.