Skip to content

Commit

Permalink
Add src/MD/actions.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
singularitti committed Nov 20, 2023
1 parent 5d3c5fe commit 1bd0d35
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/MD/actions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using AbInitioSoftwareBase: Setter
using Dates: format, now
using EquationsOfStateOfSolids: PressureEquation, Parameters, getparam, vsolve
using ExpressBase: IonDynamics, VariableCellMolecularDynamics
using QuantumESPRESSO.PWscf: PWInput, VerbositySetter
using Accessors: @reset
using UnifiedPseudopotentialFormat # To work with `download_potential`
using Unitful: Pressure, Volume, @u_str
using UnitfulAtomic

import Express.EquationOfState: CreateInput, FitEquationOfState

(::CreateInput{T})(template::PWInput, cell) where {T} =
(customizer(cell) normalizer(T()))(template)

struct CalculationSetter{T} <: Setter
calculation::T
end
function (setter::CalculationSetter)(template::PWInput)
if setter.calculation isa IonDynamics # Functions can be extended, not safe
@reset template.control.calculation = "md"
elseif setter.calculation isa VariableCellMolecularDynamics
@reset template.control.calculation = "vc-md"
@reset template.ions.ion_dynamics = "beeman"
@reset template.cell.cell_dynamics = "w"
else
throw(ArgumentError("this should never happen!"))
end
@reset template.system.nosym = true
@reset template.ions.pot_extrapolation = "second-order"
@reset template.ions.wfc_extrapolation = "second-order"
return template
end

struct PseudoDirSetter <: Setter end
function (x::PseudoDirSetter)(template::PWInput)
@reset template.control.pseudo_dir = abspath(template.control.pseudo_dir)
return template
end

normalizer(calculation) =
VerbositySetter("high") CalculationSetter(calculation) PseudoDirSetter()

struct OutdirSetter <: Setter
timefmt::String
end
function (x::OutdirSetter)(template::PWInput)
# Set `outdir` to `outdir` + a subdirectory.
@reset template.control.outdir = abspath(
joinpath(
template.control.outdir,
join((template.control.prefix, format(now(), x.timefmt), rand(UInt)), '_'),
),
)
if !isdir(template.control.outdir)
mkpath(template.control.outdir)
end
return template
end

function (::ExtractCell)(file)
str = read(file, String)
cell_parameters = last(collect(eachcellparameterscard(str)))
atomic_positions = last(collect(eachatomicpositionscard(str)))
return Cell(cell_parameters, atomic_positions)
end

customizer(cell::Cell) =
OutdirSetter("Y-m-d_H:M:S") CardSetter(CellParametersCard(cell, :bohr))
CardSetter(AtomicPositionsCard(cell))

0 comments on commit 1bd0d35

Please sign in to comment.