Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/JuDFTteam/aiida-kkr into…
Browse files Browse the repository at this point in the history
… develop
  • Loading branch information
Raff-physics committed Mar 28, 2024
2 parents 668fb36 + 3a5e5e3 commit 4ef825c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 84 deletions.
2 changes: 1 addition & 1 deletion aiida_kkr/tools/__init__.py
Expand Up @@ -19,7 +19,7 @@
from .neworder_potential import *
from .find_parent import get_calc_from_remote, get_remote, get_parent
from .bdg_tools import get_anomalous_density_data
from .ldau import get_ldaupot_text
from .ldau import *


# expose structure finder from VoronoiCalculation
Expand Down
82 changes: 2 additions & 80 deletions aiida_kkr/tools/combine_imps.py
Expand Up @@ -15,11 +15,12 @@
pos_exists_already, get_inplane_neighbor, get_scoef_single_imp, get_zimp, combine_clusters,
create_combined_imp_info, create_combined_imp_info_cf
)
from aiida_kkr.tools import get_ldaumatrices, get_LDAU_initmatrices_dict

__copyright__ = (u'Copyright (c), 2020, Forschungszentrum Jülich GmbH, '
'IAS-1/PGI-1, Germany. All rights reserved.')
__license__ = 'MIT license, see LICENSE.txt file'
__version__ = '0.3.3'
__version__ = '0.3.4'
__contributors__ = (u'Philipp Rüßmann, David Antognini Silva')

# activate debug writeout
Expand Down Expand Up @@ -235,40 +236,6 @@ def combine_potentials_cf(kickout_info, pot_imp1, pot_imp2, nspin_node):
return combine_potentials(kickout_info, pot_imp1, pot_imp2, nspin_node)


def get_ldaumatrices(retrieved):
"""
Take retrieved folder of KkrimpCalculation and extract ldaupot file
If it exists we extract the LDAU matrices 'wldau', 'uldau' and 'phi'
"""
has_ldaupot_file = False
txt_dict_ldaumats = {}

# create Sandbox to extract ldaupot file there
with SandboxFolder() as tempfolder:
# extract ldaupot file to tempfolder if it exists
has_ldaupot_file = KkrimpCalculation.get_ldaupot_from_retrieved(retrieved, tempfolder)
# now read ldau matrices and store in txt_dict_ldaumats
if has_ldaupot_file:
with tempfolder.open(KkrimpCalculation._LDAUPOT + '_old') as ldaupot_file:
# read file and look for keywords to identify where matrices are stored in the file
txt = ldaupot_file.readlines()
ii = 0
for line in txt:
if 'wldau' in line:
iwldau = ii
if 'uldau' in line:
iuldau = ii
if 'phi' in line:
iphi = ii
ii += 1
# save matrices to output dict
txt_dict_ldaumats['wldau'] = txt[iwldau + 1:iuldau]
txt_dict_ldaumats['uldau'] = txt[iuldau + 1:iphi]
txt_dict_ldaumats['phi'] = txt[iphi + 1:]

return has_ldaupot_file, txt_dict_ldaumats


@calcfunction
def combine_settings_ldau(**kwargs):
"""
Expand Down Expand Up @@ -330,48 +297,3 @@ def combine_settings_ldau(**kwargs):
settings_LDAU_combined['initial_matrices'] = get_LDAU_initmatrices_dict(txts_ldaumat2, noffset)

return Dict(settings_LDAU_combined)


def get_LDAU_initmatrices_dict(txts_ldaumat1, offset=0):
"""
Put the initial matrices text content of `get_ldaumatrices` output in a
Dictionnary form sorted by atoms.
"""

# Loop through characters ('wldau', 'uldau', 'phi')
for char in ['wldau', 'uldau', 'phi']:
atom_line = []
iatoms = []

# Save atom number and lines in which atoms information starts
for iline in range(len(txts_ldaumat1[char])):
if txts_ldaumat1[char][iline][:4] == 'atom':
iatom = txts_ldaumat1[char][iline].split()[1]
iatoms.append(iatom)
atom_line.append(iline)
atom_line.append(len(txts_ldaumat1[char]))

mat = []
# Extract matrices based on atom lines
for iatom in range(len(atom_line) - 1):
mat.append(txts_ldaumat1[char][atom_line[iatom] + 1:atom_line[iatom + 1]])

# Assign matrices to corresponding variables based on the character
if char == 'wldau':
wldaumat = mat
elif char == 'uldau':
uldaumat = mat
elif char == 'phi':
phimat = mat

# Create a dictionary to store the organized data
LDAU_initmatrices_dict = {}

# Fill the dictionary with atom-wise information
for iatom in range(len(iatoms)):
LDAU_initmatrices_dict[f'iatom={int(iatoms[iatom])-1+offset}'] = {}
LDAU_initmatrices_dict[f'iatom={int(iatoms[iatom])-1+offset}']['wldau'] = wldaumat[iatom]
LDAU_initmatrices_dict[f'iatom={int(iatoms[iatom])-1+offset}']['uldau'] = uldaumat[iatom]
LDAU_initmatrices_dict[f'iatom={int(iatoms[iatom])-1+offset}']['phi'] = phimat[iatom]

return LDAU_initmatrices_dict
82 changes: 82 additions & 0 deletions aiida_kkr/tools/ldau.py
Expand Up @@ -87,3 +87,85 @@ def get_ldaupot_text(ldau_settings, ef_Ry, natom, initialize=True, return_luj=Fa
return txt
else:
return txt, lopt, jeff, ueff, eref, iatoms_ldau


def get_ldaumatrices(retrieved):
"""
Take retrieved folder of KkrimpCalculation and extract ldaupot file
If it exists we extract the LDAU matrices 'wldau', 'uldau' and 'phi'
"""
from aiida.common.folders import SandboxFolder
from aiida_kkr.calculations import KkrimpCalculation

has_ldaupot_file = False
txt_dict_ldaumats = {}

# create Sandbox to extract ldaupot file there
with SandboxFolder() as tempfolder:
# extract ldaupot file to tempfolder if it exists
has_ldaupot_file = KkrimpCalculation.get_ldaupot_from_retrieved(retrieved, tempfolder)
# now read ldau matrices and store in txt_dict_ldaumats
if has_ldaupot_file:
with tempfolder.open(KkrimpCalculation._LDAUPOT + '_old') as ldaupot_file:
# read file and look for keywords to identify where matrices are stored in the file
txt = ldaupot_file.readlines()
ii = 0
for line in txt:
if 'wldau' in line:
iwldau = ii
if 'uldau' in line:
iuldau = ii
if 'phi' in line:
iphi = ii
ii += 1
# save matrices to output dict
txt_dict_ldaumats['wldau'] = txt[iwldau + 1:iuldau]
txt_dict_ldaumats['uldau'] = txt[iuldau + 1:iphi]
txt_dict_ldaumats['phi'] = txt[iphi + 1:]

return has_ldaupot_file, txt_dict_ldaumats


def get_LDAU_initmatrices_dict(txts_ldaumat1, offset=0):
"""
Put the initial matrices text content of `get_ldaumatrices` output in a
Dictionnary form sorted by atoms.
"""

# Loop through characters ('wldau', 'uldau', 'phi')
for char in ['wldau', 'uldau', 'phi']:
atom_line = []
iatoms = []

# Save atom number and lines in which atoms information starts
for iline in range(len(txts_ldaumat1[char])):
if txts_ldaumat1[char][iline][:4] == 'atom':
iatom = txts_ldaumat1[char][iline].split()[1]
iatoms.append(iatom)
atom_line.append(iline)
atom_line.append(len(txts_ldaumat1[char]))

mat = []
# Extract matrices based on atom lines
for iatom in range(len(atom_line) - 1):
mat.append(txts_ldaumat1[char][atom_line[iatom] + 1:atom_line[iatom + 1]])

# Assign matrices to corresponding variables based on the character
if char == 'wldau':
wldaumat = mat
elif char == 'uldau':
uldaumat = mat
elif char == 'phi':
phimat = mat

# Create a dictionary to store the organized data
LDAU_initmatrices_dict = {}

# Fill the dictionary with atom-wise information
for iatom in range(len(iatoms)):
LDAU_initmatrices_dict[f'iatom={int(iatoms[iatom])-1+offset}'] = {}
LDAU_initmatrices_dict[f'iatom={int(iatoms[iatom])-1+offset}']['wldau'] = wldaumat[iatom]
LDAU_initmatrices_dict[f'iatom={int(iatoms[iatom])-1+offset}']['uldau'] = uldaumat[iatom]
LDAU_initmatrices_dict[f'iatom={int(iatoms[iatom])-1+offset}']['phi'] = phimat[iatom]

return LDAU_initmatrices_dict
22 changes: 19 additions & 3 deletions aiida_kkr/workflows/kkr_imp_sub.py
Expand Up @@ -8,7 +8,7 @@
from aiida.orm import Float, Code, CalcJobNode, RemoteData, StructureData, Dict, SinglefileData, FolderData, Bool
from aiida.engine import WorkChain, ToContext, while_, if_, calcfunction
from masci_tools.io.kkr_params import kkrparams
from aiida_kkr.tools import test_and_get_codenode, get_inputs_kkrimp, kick_out_corestates_wf
from aiida_kkr.tools import test_and_get_codenode, get_inputs_kkrimp, kick_out_corestates_wf, get_ldaumatrices, get_LDAU_initmatrices_dict
from aiida_kkr.calculations.kkrimp import KkrimpCalculation
from numpy import array
import tarfile, os
Expand All @@ -17,8 +17,8 @@
__copyright__ = (u'Copyright (c), 2017, Forschungszentrum Jülich GmbH, '
'IAS-1/PGI-1, Germany. All rights reserved.')
__license__ = 'MIT license, see LICENSE.txt file'
__version__ = '0.10.1'
__contributors__ = (u'Fabian Bertoldo', u'Philipp Ruessmann')
__version__ = '0.10.3'
__contributors__ = (u'Fabian Bertoldo', u'Philipp Ruessmann', u'David Antognini Silva')

#TODO: work on return results function
#TODO: edit inspect_kkrimp function
Expand Down Expand Up @@ -925,6 +925,12 @@ def inspect_kkrimp(self):
# check rms
self.ctx.rms.append(last_calc_output['convergence_group']['rms'])
rms_all_iter_last_calc = list(last_calc_output['convergence_group']['rms_all_iterations'])
# check rms of LDAU pot (if LDAU is set)
rms_LDAU = last_calc_output['convergence_group']['rms_LDAU']
self.ctx.rms_LDAU = rms_LDAU
if rms_LDAU != 0.0:
rms_LDAU_all_iter_last_calc = list(last_calc_output['convergence_group']['rms_LDAU_all_iterations'])
self.ctx.last_rms_LDAU_all = rms_LDAU_all_iter_last_calc

# add lists of last iterations
self.ctx.last_rms_all = rms_all_iter_last_calc
Expand Down Expand Up @@ -1033,6 +1039,11 @@ def convergence_on_track(self):
message = 'INFO: convergence check: rms goes up too fast, convergence is not expected'
self.report(message)
on_track = False
if self.ctx.rms_LDAU != 0:
if self.ctx.last_rms_LDAU_all[-1] < self.ctx.last_rms_LDAU_all[0]:
message = 'INFO: convergence check: rms LDAU potential goes down, convergence could still be expected in the next KkrimpCalulation run'
self.report(message)
on_track = True
elif len(self.ctx.last_rms_all) == 1:
message = 'INFO: convergence check: already converged after single iteration'
self.report(message)
Expand All @@ -1041,6 +1052,11 @@ def convergence_on_track(self):
message = 'INFO: convergence check: rms does not shrink fast enough, convergence is not expected'
self.report(message)
on_track = False
if self.ctx.rms_LDAU != 0:
if self.ctx.last_rms_LDAU_all[-1] < self.ctx.last_rms_LDAU_all[0]:
message = 'INFO: convergence check: rms LDAU potential goes down, convergence could still be expected in the next KkrimpCalulation run'
self.report(message)
on_track = True
elif calc_reached_qbound:
message = 'INFO: convergence check: calculation reached QBOUND'
self.report(message)
Expand Down

0 comments on commit 4ef825c

Please sign in to comment.