Skip to content

Commit

Permalink
Sketching unit tests for new factory functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gmatteo committed Oct 14, 2024
1 parent 011e6f0 commit 47fada0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
30 changes: 17 additions & 13 deletions abipy/abio/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,16 +1381,17 @@ def scf_for_phonons(structure, pseudos, kppa=None, ecut=None, pawecutdg=None, nb

def ddkpert_from_gsinput(gs_input, ddk_pert, nband=None, use_symmetries=False, ddk_tol=None, manager=None) -> AbinitInput:
"""
Returns an |AbinitInput| to perform a DDK calculations for a specific perturbation and based on a ground state |AbinitInput|.
Returns an |AbinitInput| to perform a DDK calculations for a specific perturbation based on a ground state |AbinitInput|.
Args:
gs_input: an |AbinitInput| representing a ground state calculation, likely the SCF performed to get the WFK.
gs_input: an |AbinitInput| representing a ground state calculation, likely the SCF performed to get the WFK.
ddk_pert: dict with the Abinit variables defining the perturbation
Example: {'idir': 1, 'ipert': 4, 'qpt': [0.0, 0.0, 0.0]},
use_symmetries: boolean that determines if the irreducible components of the perturbation are used.
Default to False. (TODO: Should be implemented)
ddk_tol: a dictionary with a single key defining the type of tolerance used for the DDK calculations and its value. Default: {"tolvrs": 1.0e-22}.
manager: |TaskManager| of the task. If None, the manager is initialized from the config file.
ddk_tol: a dictionary with a single key defining the type of tolerance used for the DDK calculations and its value.
Default: {"tolvrs": 1.0e-22}.
manager: |TaskManager| of the task. If None, the manager is initialized from the config file.
"""
gs_input = gs_input.deepcopy()
gs_input.pop_irdvars()
Expand All @@ -1409,19 +1410,20 @@ def ddkpert_from_gsinput(gs_input, ddk_pert, nband=None, use_symmetries=False, d

return ddk_inp


def ddepert_from_gsinput(gs_input, dde_pert, use_symmetries=True, dde_tol=None, manager=None) -> AbinitInput:
"""
Returns an |AbinitInput| to perform a DDE calculations for a specific perturbation and based on a ground state |AbinitInput|.
Returns an |AbinitInput| to perform a DDE calculations for a specific perturbation based on a ground state |AbinitInput|.
Args:
gs_input: an |AbinitInput| representing a ground state calculation, likely the SCF performed to get the WFK.
gs_input: an |AbinitInput| representing a ground state calculation, likely the SCF performed to get the WFK.
dde_pert: dict with the Abinit variables defining the perturbation
Example: {'idir': 1, 'ipert': 4, 'qpt': [0.0, 0.0, 0.0]},
Example: {'idir': 1, 'ipert': 4, 'qpt': [0.0, 0.0, 0.0]},
use_symmetries: boolean that determines if the irreducible components of the perturbation are used.
Default to True. Should be set to False for nonlinear coefficients calculation.
dde_tol: a dictionary with a single key defining the type of tolerance used for the DDE calculations and
dde_tol: a dictionary with a single key defining the type of tolerance used for the DDE calculations and
its value. Default: {"tolvrs": 1.0e-22}.
manager: |TaskManager| of the task. If None, the manager is initialized from the config file.
manager: |TaskManager| of the task. If None, the manager is initialized from the config file.
"""
gs_input = gs_input.deepcopy()
gs_input.pop_irdvars()
Expand All @@ -1434,15 +1436,16 @@ def ddepert_from_gsinput(gs_input, dde_pert, use_symmetries=True, dde_tol=None,

return dde_inp


def dtepert_from_gsinput(gs_input, dte_pert, manager=None) -> AbinitInput:
"""
Returns an |AbinitInput| to perform a DTE calculations for a specific perturbation and based on a ground state |AbinitInput|.
Args:
gs_input: an |AbinitInput| representing a ground state calculation, likely the SCF performed to get the WFK.
gs_input: an |AbinitInput| representing a ground state calculation, likely the SCF performed to get the WFK.
dte_pert: dict with the Abinit variables defining the perturbation
Example: {'idir': 1, 'ipert': 4, 'qpt': [0.0, 0.0, 0.0]},
manager: |TaskManager| of the task. If None, the manager is initialized from the config file.
Example: {'idir': 1, 'ipert': 4, 'qpt': [0.0, 0.0, 0.0]},
manager: |TaskManager| of the task. If None, the manager is initialized from the config file.
"""
gs_input = gs_input.deepcopy()
gs_input.pop_irdvars()
Expand All @@ -1452,6 +1455,7 @@ def dtepert_from_gsinput(gs_input, dte_pert, manager=None) -> AbinitInput:

return dte_inp


def dte_from_gsinput(gs_input, use_phonons=True, ph_tol=None, ddk_tol=None, dde_tol=None,
skip_dte_permutations=False, manager=None) -> MultiDataset:
"""
Expand Down Expand Up @@ -1509,7 +1513,7 @@ def dte_from_gsinput(gs_input, use_phonons=True, ph_tol=None, ddk_tol=None, dde_
gs_input.set_vars(nband=nband)
gs_input.pop('nbdbuf', None)
multi_dte = gs_input.make_dte_inputs(phonon_pert=use_phonons, skip_permutations=skip_dte_permutations,
manager=manager)
manager=manager)
multi_dte.add_tags(atags.DTE)
multi.extend(multi_dte)

Expand Down
43 changes: 37 additions & 6 deletions abipy/abio/tests/test_factories.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import abipy.data as abidata
import abipy.abilab as abilab

Expand All @@ -7,8 +8,9 @@
from abipy.abio.factories import *
from abipy.abio.factories import (BandsFromGsFactory, IoncellRelaxFromGsFactory, HybridOneShotFromGsFactory,
ScfForPhononsFactory, PhononsFromGsFactory, PiezoElasticFactory, PiezoElasticFromGsFactory, ShiftMode)
from abipy.abio.factories import _find_nscf_nband_from_gsinput
import json
from abipy.abio.factories import _find_nscf_nband_from_gsinput, minimal_scf_input
from abipy.abio.input_tags import DDK, DDE, PH_Q_PERT, STRAIN, DTE, PH_Q_PERT


write_inputs_to_json = False

Expand Down Expand Up @@ -304,7 +306,6 @@ def test_phonons_from_gsinput(self):
with_bec=False, ph_tol=None, ddk_tol=None, dde_tol=None)
self.abivalidate_multi(multi)

from abipy.abio.input_tags import DDK, DDE, PH_Q_PERT
inp_ddk = multi.filter_by_tags(DDK)[0]
inp_dde = multi.filter_by_tags(DDE)[0]
inp_ph_q_pert_1 = multi.filter_by_tags(PH_Q_PERT)[0]
Expand Down Expand Up @@ -499,7 +500,7 @@ def test_dfpt_from_gsinput(self):
do_dte=True, ph_tol=None, ddk_tol=None, dde_tol=None)
self.abivalidate_multi(multi)

from abipy.abio.input_tags import DDK, DDE, PH_Q_PERT, STRAIN, DTE

inp_ddk = multi.filter_by_tags(DDK)[0]
inp_dde = multi.filter_by_tags(DDE)[0]
inp_ph_q_pert_1 = multi.filter_by_tags(PH_Q_PERT)[0]
Expand Down Expand Up @@ -530,10 +531,40 @@ def test_dfpt_from_gsinput(self):
self.assert_input_equality('dfpt_from_gsinput_dte.json', inp_dte)

def test_minimal_scf_input(self):
from abipy.abio.factories import minimal_scf_input
inp = minimal_scf_input(self.si_structure, self.si_pseudo)

self.abivalidate_input(inp)

self.assertEqual(inp["nband"], 1)
self.assertEqual(inp["nstep"], 0)

def test_ddkpert_from_gsinput(self):
gs_inp = gs_input(self.si_structure, self.si_pseudo, kppa=None, ecut=2, spin_mode="unpolarized")
gs_inp["nband"] = 4
gs_inp["autoparal"] = 1
gs_inp["npfft"] = 10

ddk_pert = {'idir': 1, 'ipert': 3, 'qpt': [0.0, 0.0, 0.0]}
ddk_input = ddkpert_from_gsinput(gs_inp, ddk_pert)
assert ddk_input["tolwfr"] == 1.0e-22
assert "autoparal" not in ddk_input
assert "npfft" not in ddk_input
self.abivalidate_input(ddk_input)

dde_pert = {'idir': 1, 'ipert': 4, 'qpt': [0.0, 0.0, 0.0]}
dde_input = ddepert_from_gsinput(gs_inp, dde_pert)
assert "autoparal" not in dde_input
assert "npfft" not in dde_input
assert dde_input["tolvrs"] == 1.0e-22
self.abivalidate_input(dde_input)

#dte_pert = {'i1dir': 1, 'ipert': 4, 'qpt': [0.0, 0.0, 0.0]}
#dte_input = dtepert_from_gsinput(gs_inp, dte_pert)
#assert "autoparal" not in dte_input
#assert "npfft" not in dte_input
#assert dte_input["tolvrs"] == 1.0e-22
#self.abivalidate_input(dte_input)

#def test_dte_from_gsinput(self):
# gs_inp = gs_input(self.si_structure, self.si_pseudo, kppa=None, ecut=2, spin_mode="unpolarized")
# multi = dte_from_gsinput(gs_inp, use_phonons=True)
# self.abivalidate_input(multi)

0 comments on commit 47fada0

Please sign in to comment.