Skip to content

Commit

Permalink
restore aenetPyLammps
Browse files Browse the repository at this point in the history
  • Loading branch information
yomichi committed Dec 6, 2024
1 parent eaec337 commit 7f161df
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 22 deletions.
9 changes: 7 additions & 2 deletions abics/applications/latgas_abinitio_interface/aenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
from .params import ALParams, DFTParams
from .util import structure_to_XSF, structure_from_XSF


class AenetSolver(SolverBase):
"""
This class defines the aenet solver.
"""

def __init__(self, path_to_solver: os.PathLike, ignore_species=None, run_scheme="subprocess"):
def __init__(
self, path_to_solver: os.PathLike, ignore_species=None, run_scheme="subprocess"
):
"""
Initialize the solver.
Expand All @@ -49,7 +52,7 @@ def name(self):
return "aenet"

class Input(object):
def __init__(self, ignore_species : str | None, run_scheme="subprocess"):
def __init__(self, ignore_species: str | None, run_scheme="subprocess"):
self.base_info = None
self.pos_info = None
self.ignore_species = ignore_species
Expand Down Expand Up @@ -138,6 +141,8 @@ def cl_args(self, nprocs, nthreads, output_dir):
os.path.join(output_dir, "predict.in"),
os.path.join(output_dir, "structure.xsf"),
]
else:
raise RuntimeError("Invalid run_scheme: {}".format(self.run_scheme))

class Output(object):
def get_results(self, output_dir):
Expand Down
11 changes: 7 additions & 4 deletions abics/applications/latgas_abinitio_interface/aenet_pylammps.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def __init__(self, ignore_species):
cmdargs=["-log", "none", "-screen", "none", "-nocite"], comm=MPI.COMM_SELF
)
self.rank = MPI.COMM_WORLD.Get_rank()

# debug cmdargs=["-log", "test{}.log".format(MPI.COMM_WORLD.Get_rank()), "-screen", "none", "-nocite"], comm=MPI.COMM_SELF

def name(self):
return "aenetPyLammps"

Expand All @@ -82,7 +83,7 @@ def calculate_energy(self, fi, output_dir):
by = np.linalg.norm(np.cross(unit_vec(latt[0]), latt[1]))
cx = np.dot(latt[2], unit_vec(latt[0]))
cy = (np.dot(latt[1], latt[2]) - bx * cx) / by
cz = np.sqrt(np.linalg.norm(latt[2]) ** 2.0 - cx ** 2.0 - cy ** 2.0)
cz = np.sqrt(np.linalg.norm(latt[2]) ** 2.0 - cx**2.0 - cy**2.0)

# lmp = lammps(cmdargs=["-log", "none","-nocite"])
self.calc.command("atom_style atomic")
Expand All @@ -92,9 +93,11 @@ def calculate_energy(self, fi, output_dir):
self.calc.command(f"region box prism 0 {ax} 0 {by} 0 {cz} {bx} {cx} {cy}")
self.calc.command(f"create_box {nspec} box")
types = list(map(lambda x: spec_dict[x.name], st.species))
new_coords = np.dot(st.frac_coords, np.array([[ax,0,0], [bx,by,0], [cx,cy,cz]]))
new_coords = np.dot(
st.frac_coords, np.array([[ax, 0, 0], [bx, by, 0], [cx, cy, cz]])
)
self.calc.create_atoms(natoms, None, types, new_coords.ravel())

for i in range(1, nspec + 1):
self.calc.command(f"mass {i} 1")
self.calc.commands_list(self.input.pair_pot)
Expand Down
1 change: 1 addition & 0 deletions abics/applications/latgas_abinitio_interface/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def from_toml(cls, f):
d = toml.load(f)
return cls.from_dict(d["mlref"]["solver"])


class TrainerParams:
def __init__(self):
self.base_input_dir = []
Expand Down
18 changes: 16 additions & 2 deletions tests/test_aenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from pymatgen.core import Structure

from abics.applications.latgas_abinitio_interface.base_solver import create_solver
from abics.applications.latgas_abinitio_interface.params import DFTParams
from abics.applications.latgas_abinitio_interface.aenet import AenetSolver


Expand All @@ -35,11 +37,17 @@ def setUpClass(cls):
os.makedirs(workdir)

def setUp(self):
self.solver = AenetSolver(".")
params = DFTParams.from_dict(
{"type": "aenet", "path": ".", "run_scheme": "subprocess"}
)
self.solver = create_solver(params.solver, params)
self.rootdir = os.path.dirname(__file__)
self.datadir = os.path.join(self.rootdir, "data", "aenet")
self.workdir = os.path.join(self.rootdir, "res", "aenet")

def test_create_solver(self):
self.assertIsInstance(self.solver, AenetSolver)

def test_get_results(self):
res = self.solver.output.get_results(self.datadir)
res.structure.to("POSCAR", os.path.join(self.workdir, "pos.vasp"))
Expand Down Expand Up @@ -68,4 +76,10 @@ def test_cl_algs(self):
nthreads = 4
workdir = "work"
res = self.solver.input.cl_args(nprocs, nthreads, workdir)
self.assertEqual(res, [os.path.join(workdir, "predict.in"), os.path.join(workdir, "structure.xsf")])
self.assertEqual(
res,
[
os.path.join(workdir, "predict.in"),
os.path.join(workdir, "structure.xsf"),
],
)
57 changes: 57 additions & 0 deletions tests/test_aenetpylammps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# ab-Initio Configuration Sampling tool kit (abICS)
# Copyright (C) 2019- The University of Tokyo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

import os
import shutil
import unittest

import numpy as np

from pymatgen.core import Structure

from abics.applications.latgas_abinitio_interface.base_solver import create_solver
from abics.applications.latgas_abinitio_interface.params import DFTParams
from abics.applications.latgas_abinitio_interface.aenet_pylammps import (
AenetPyLammpsSolver,
)


class TestAenetPyLammps(unittest.TestCase):
@classmethod
def setUpClass(cls):
rootdir = os.path.dirname(__file__)
workdir = os.path.join(rootdir, "res", "aenet")
if os.path.exists(workdir):
shutil.rmtree(workdir)
os.makedirs(workdir)

def setUp(self):
params = DFTParams.from_dict({"type": "aenetPyLammps", "path": "."})

self.imported = False
try:
self.solver = create_solver(params.solver, params)
self.imported = True
except ImportError:
self.imported = False

self.rootdir = os.path.dirname(__file__)
self.datadir = os.path.join(self.rootdir, "data", "aenet")
self.workdir = os.path.join(self.rootdir, "res", "aenet")

def test_create_solver(self):
if self.imported:
self.assertIsInstance(self.solver, AenetPyLammpsSolver)
22 changes: 18 additions & 4 deletions tests/test_openmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@

from pymatgen.core import Structure

from abics.applications.latgas_abinitio_interface.openmx import OpenMXInputFile, OpenMXSolver
from abics.applications.latgas_abinitio_interface.base_solver import create_solver
from abics.applications.latgas_abinitio_interface.params import DFTParams
from abics.applications.latgas_abinitio_interface.openmx import (
OpenMXInputFile,
OpenMXSolver,
)


class TestOpenMX(unittest.TestCase):
Expand All @@ -38,7 +43,17 @@ def setUp(self):
self.rootdir = os.path.dirname(__file__)
self.datadir = os.path.join(self.rootdir, "data", "openmx")
self.workdir = os.path.join(self.rootdir, "res", "openmx")
self.solver = OpenMXSolver(os.path.join(self.datadir, "bin", "openmx.dummy"))
params = DFTParams.from_dict(
{
"type": "openmx",
"path": os.path.join(self.datadir, "bin", "openmx.dummy"),
"run_scheme": "subprocess",
}
)
self.solver = create_solver(params.solver, params)

def test_create_solver(self):
self.assertIsInstance(self.solver, OpenMXSolver)

def test_get_results(self):
self.solver.input.from_directory(os.path.join(self.datadir, "baseinput"))
Expand All @@ -51,7 +66,7 @@ def test_get_results(self):

def test_input(self):
self.solver.input.from_directory(os.path.join(self.datadir, "baseinput"))
A = 4.0*np.eye(3)
A = 4.0 * np.eye(3)
r = np.array([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]])
st = Structure(
A,
Expand All @@ -70,7 +85,6 @@ def test_input(self):

self.assertEqual(res, ref)


def test_cl_algs(self):
self.solver.input.from_directory(os.path.join(self.datadir, "baseinput"))
nprocs = 2
Expand Down
26 changes: 19 additions & 7 deletions tests/test_qe.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from pymatgen.core import Structure

from abics.applications.latgas_abinitio_interface.base_solver import create_solver
from abics.applications.latgas_abinitio_interface.params import DFTParams
from abics.applications.latgas_abinitio_interface.qe import QESolver

from qe_tools.parsers import PwInputFile
Expand All @@ -37,11 +39,15 @@ def setUpClass(cls):
os.makedirs(workdir)

def setUp(self):
self.solver = QESolver(".")
params = DFTParams.from_dict({"type": "qe", "path": "."})
self.solver = create_solver(params.solver, params)
self.rootdir = os.path.dirname(__file__)
self.datadir = os.path.join(self.rootdir, "data", "qe")
self.workdir = os.path.join(self.rootdir, "res", "qe")

def test_create_solver(self):
self.assertIsInstance(self.solver, QESolver)

def test_get_results(self):
res = self.solver.output.get_results(self.datadir)
res.structure.to("POSCAR", os.path.join(self.workdir, "pos.vasp"))
Expand All @@ -52,7 +58,7 @@ def test_get_results(self):

def test_input(self):
self.solver.input.from_directory(os.path.join(self.datadir, "baseinput"))
A = 4.0*np.eye(3)
A = 4.0 * np.eye(3)
r = np.array([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]])
st = Structure(
A,
Expand All @@ -72,15 +78,21 @@ def test_input(self):

self.assertEqual(res.namelists, ref.namelists)

self.assertTrue(np.allclose(res.cell_parameters["cell"], ref.cell_parameters["cell"]))
self.assertTrue(np.allclose(res.atomic_positions["positions"], ref.atomic_positions["positions"]))
self.assertEqual(res.atomic_positions["fixed_coords"], ref.atomic_positions["fixed_coords"])

self.assertTrue(
np.allclose(res.cell_parameters["cell"], ref.cell_parameters["cell"])
)
self.assertTrue(
np.allclose(
res.atomic_positions["positions"], ref.atomic_positions["positions"]
)
)
self.assertEqual(
res.atomic_positions["fixed_coords"], ref.atomic_positions["fixed_coords"]
)

def test_cl_algs(self):
nprocs = 2
nthreads = 4
workdir = "work"
res = self.solver.input.cl_args(nprocs, nthreads, workdir)
self.assertEqual(res, ["-in", os.path.join(workdir, "scf.in")])

11 changes: 8 additions & 3 deletions tests/test_vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from pymatgen.core import Structure
from pymatgen.io.vasp.inputs import VaspInput

from abics.applications.latgas_abinitio_interface.base_solver import create_solver
from abics.applications.latgas_abinitio_interface.params import DFTParams
from abics.applications.latgas_abinitio_interface.vasp import VASPSolver


Expand All @@ -36,11 +38,15 @@ def setUpClass(cls):
os.makedirs(workdir)

def setUp(self):
self.solver = VASPSolver(".")
params = DFTParams.from_dict({"type": "vasp", "path": "."})
self.solver = create_solver(params.solver, params)
self.rootdir = os.path.dirname(__file__)
self.datadir = os.path.join(self.rootdir, "data", "vasp")
self.workdir = os.path.join(self.rootdir, "res", "vasp")

def test_create_solver(self):
self.assertIsInstance(self.solver, VASPSolver)

def test_get_results(self):
os.utime(os.path.join(self.datadir, "output", "OSZICAR"))
res = self.solver.output.get_results(os.path.join(self.datadir, "output"))
Expand Down Expand Up @@ -69,7 +75,7 @@ def test_input(self):

self.assertEqual(res["INCAR"], ref["INCAR"])
self.assertTrue(res["POSCAR"].structure.matches(ref["POSCAR"].structure))
for k,v in ref["POSCAR"].structure.site_properties.items():
for k, v in ref["POSCAR"].structure.site_properties.items():
self.assertTrue(np.allclose(res["POSCAR"].structure.site_properties[k], v))

def test_cl_algs(self):
Expand All @@ -78,4 +84,3 @@ def test_cl_algs(self):
workdir = "work"
res = self.solver.input.cl_args(nprocs, nthreads, workdir)
self.assertEqual(res, [workdir])

0 comments on commit 7f161df

Please sign in to comment.