Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tools and missing workflows #107

Merged
merged 4 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions aiida_kkr/calculations/kkr.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)
from masci_tools.io.common_functions import get_alat_from_bravais, get_Ang2aBohr
from aiida_kkr.tools.tools_kkrimp import make_scoef, write_scoef_full_imp_cls
from aiida_kkr.tools.find_parent import find_parent_structure
from masci_tools.io.kkr_params import __kkr_default_params__, kkrparams
import six
from six.moves import range
Expand Down Expand Up @@ -443,7 +444,7 @@ def _get_structure_inputs(self, parent_calc, parameters):
structure = None
self.logger.info('KkrCalculation: Get structure node from voronoi parent')
try:
structure, voro_parent = VoronoiCalculation.find_parent_structure(parent_calc)
structure, voro_parent = find_parent_structure(parent_calc)
except:
self.logger.error(f'KkrCalculation: Could not get structure from Voronoi parent ({parent_calc}).')
raise ValidationError(f'Cound not find structure node from parent {parent_calc}')
Expand Down Expand Up @@ -626,7 +627,7 @@ def _get_local_copy_list(self, parent_calc, voro_parent, tempfolder, parameters)
# add shapefun file from voronoi parent if needed
if self._SHAPEFUN not in copylist:
try:
struc, voro_parent = VoronoiCalculation.find_parent_structure(parent_calc)
struc, voro_parent = find_parent_structure(parent_calc)
except ValueError:
return self.exit_codes.ERROR_NO_SHAPEFUN_FOUND # pylint: disable=no-member
# copy shapefun from retrieved of voro calc
Expand Down
21 changes: 10 additions & 11 deletions aiida_kkr/calculations/kkrnano.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
"""
Input plug-in for a KKRnano calculation.
"""
from aiida.engine import CalcJob
from aiida.orm import CalcJobNode, Dict, Bool, Float, RemoteData, StructureData
from aiida_kkr.data.strucwithpot import StrucWithPotData
from aiida.common.datastructures import (CalcInfo, CodeInfo)

import numpy as np
from masci_tools.io.common_functions import get_Ang2aBohr
from aiida.orm import CalcJobNode, Dict, Bool, Float, RemoteData, StructureData
from aiida.engine import CalcJob
from aiida.common import NotExistent
from aiida.common.datastructures import (CalcInfo, CodeInfo)
from aiida.common.exceptions import InputValidationError, UniquenessError

from aiida_kkr.calculations.voro import VoronoiCalculation

import numpy as np
from aiida_kkr.tools.find_parent import get_remote, get_parent
from aiida_kkr.data.strucwithpot import StrucWithPotData

__copyright__ = (u'Copyright (c), 2021, Forschungszentrum Jülich GmbH, '
'IAS-1/PGI-1, Germany. All rights reserved.')
__license__ = 'MIT license, see LICENSE.txt file'
__version__ = '0.0.1'
__version__ = '0.0.2'
__contributors__ = ('Markus Struckmann', 'Philipp Rüßmann')


Expand Down Expand Up @@ -704,9 +703,9 @@ def find_parent_struc_from_voro_or_stwpd(self, parent_folder):
"""
iiter = 0
Nmaxiter = 1000
parent_folder_tmp = VoronoiCalculation.get_remote(parent_folder)
parent_folder_tmp = get_remote(parent_folder)
while not self._has_struc(parent_folder_tmp) and iiter < Nmaxiter:
parent_folder_tmp = VoronoiCalculation.get_remote(VoronoiCalculation.get_parent(parent_folder_tmp))
parent_folder_tmp = get_remote(get_parent(parent_folder_tmp))
iiter += 1
if iiter % 200 == 0:
print(
Expand Down
83 changes: 4 additions & 79 deletions aiida_kkr/calculations/voro.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from aiida.common.exceptions import (InputValidationError, ValidationError)
from aiida.common.datastructures import (CalcInfo, CodeInfo)
from aiida_kkr.tools.common_workfunctions import generate_inputcard_from_structure, check_2Dinput_consistency, vca_check
from aiida.common.exceptions import UniquenessError, NotExistent
from aiida.common.exceptions import UniquenessError
import os
import six

Expand Down Expand Up @@ -275,86 +275,11 @@ def _is_KkrCalc(self, calc):

return is_KKR

@classmethod
def _get_struc(self, parent_calc):
"""
Get structure from a parent_folder (result of a calculation, typically a remote folder)
"""
return parent_calc.inputs.structure

@classmethod
def _has_struc(self, parent_folder):
"""
Check if parent_folder has structure information in its input
"""
success = True
if 'structure' not in parent_folder.get_incoming().all_link_labels():
success = False
return success

@classmethod
def get_remote(self, parent_folder):
"""
get remote_folder from input if parent_folder is not already a remote folder
"""
parent_folder_tmp0 = parent_folder
try:
parent_folder_tmp = parent_folder_tmp0.get_incoming().get_node_by_label('remote_folder')
except NotExistent:
parent_folder_tmp = parent_folder_tmp0
return parent_folder_tmp

@classmethod
def get_parent(self, input_folder):
"""
get the parent folder of the calculation. If not parent was found return input folder
"""
input_folder_tmp0 = input_folder

# first option: parent_calc_folder (KkrimpCalculation)
try:
parent_folder_tmp = input_folder_tmp0.get_incoming().get_node_by_label('parent_calc_folder')
return_input = False
except NotExistent:
return_input = True

# second option: parent_folder (KkrCalculation)
try:
parent_folder_tmp = input_folder_tmp0.get_incoming().get_node_by_label('parent_folder')
return_input = False
except NotExistent:
return_input = return_input & True

# third option: parent_KKR option (special mode of VoronoiCalculation)
try:
parent_folder_tmp = input_folder_tmp0.get_incoming().get_node_by_label('parent_KKR')
return_input = False
except NotExistent:
return_input = return_input & True

if return_input:
parent_folder_tmp = input_folder_tmp0

return parent_folder_tmp

@classmethod
def find_parent_structure(self, parent_folder):
"""
Find the Structure node recuresively in chain of parent calculations (structure node is input to voronoi calculation)
This is a copy of the find_parent_structure that moved to tools.find_parent to keep backwards compatibility.
"""
iiter = 0
Nmaxiter = 1000
parent_folder_tmp = self.get_remote(parent_folder)
while not self._has_struc(parent_folder_tmp) and iiter < Nmaxiter:
parent_folder_tmp = self.get_remote(self.get_parent(parent_folder_tmp))
iiter += 1
if iiter % 200 == 0:
print(
'Warning: find_parent_structure takes quite long (already searched {} ancestors). Stop after {}'.
format(iiter, Nmaxiter)
)
if self._has_struc(parent_folder_tmp):
struc = self._get_struc(parent_folder_tmp)
return struc, parent_folder_tmp
else:
raise ValueError('structure not found')
from aiida_kkr.tools.find_parent import find_parent_structure
return find_parent_structure(parent_folder)
39 changes: 5 additions & 34 deletions aiida_kkr/data/strucwithpot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from aiida.orm import CalcJobNode, Dict, StructureData, SinglefileData, load_node, Data
from aiida.common.exceptions import InputValidationError
from aiida.common import NotExistent
#from aiida_kkr.tools import find_parent_structure #deprecated
from aiida_kkr.calculations.voro import VoronoiCalculation
from aiida_kkr.tools.find_parent import find_parent_structure, get_parent, get_remote

import os

Expand Down Expand Up @@ -173,7 +172,7 @@ def get_strucwithpot_from_Voronoi(self, calcnode):
"""
#TODO: Check if input is valid

structure = VoronoiCalculation.find_parent_structure(calcnode)[0]
structure = find_parent_structure(calcnode)[0]
cwd = os.getcwd()

#shapefun
Expand Down Expand Up @@ -275,7 +274,7 @@ def get_strucwithpot_from_KKRnanoConvert(self, calcnode):
raise InputValidationError(
'Only the convert step output can be processed! If this has not been done, yet, the parameter `builder.convert=Bool(False)` can be used and the process be run with 1 MPI, to obtain ASCII-potential files'
)
structure = VoronoiCalculation.find_parent_structure(calcnode)[0]
structure = find_parent_structure(calcnode)[0]
pot_list = []
for item in calcnode.outputs.retrieved.list_object_names():
if item.find('vpot') == 0:
Expand All @@ -299,48 +298,20 @@ def get_strucwithpot_from_KKRnanoConvert(self, calcnode):

return structure, shape_list, pot_list

def _get_remote(self, parent_folder):
"""
get remote_folder from input if parent_folder is not already a remote folder
"""
parent_folder_tmp0 = parent_folder
try:
parent_folder_tmp = parent_folder_tmp0.get_incoming().get_node_by_label('remote_folder')
except NotExistent:
parent_folder_tmp = parent_folder_tmp0
return parent_folder_tmp

#TODO remove redundancy aiida_kkr/calculations/voro.py -> There a private file
def _get_parent(self, input_folder):
"""
get the parent folder of the calculation. If not parent was found return input folder
"""
input_folder_tmp0 = input_folder
try:
parent_folder_tmp = input_folder_tmp0.get_incoming().get_node_by_label('parent_calc_folder')
except NotExistent:
try:
parent_folder_tmp = input_folder_tmp0.get_incoming().get_node_by_label('parent_folder')
except NotExistent:
parent_folder_tmp = input_folder_tmp0
return parent_folder_tmp

def find_parent_shapefun(self, parent_folder):
"""
Find the shape files recursively in chain of parent calculations, either to be extracted from "shapefun" file or "shapes" files
"""
iiter = 0
Nmaxiter = 1000

parent_folder_tmp = self._get_parent(parent_folder)
parent_folder_tmp = get_parent(parent_folder)

print(parent_folder_tmp)
parent_folder_tmp_listdir = parent_folder_tmp.listdir(
) #requires remote ssh connection, therefore much quicker this way
while not 'shape.0000001' in parent_folder_tmp_listdir and not 'shapefun' in parent_folder_tmp_listdir and iiter < Nmaxiter:
parent_folder_tmp = self._get_remote(
self._get_parent(parent_folder_tmp)
) #at this point the result is a CalcNode!
parent_folder_tmp = get_remote(get_parent(parent_folder_tmp)) #at this point the result is a CalcNode!
parent_folder_tmp_listdir = parent_folder_tmp.outputs.remote_folder.listdir()
iiter += 1
print(parent_folder_tmp)
Expand Down
1 change: 0 additions & 1 deletion aiida_kkr/parsers/kkrnano.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from aiida.parsers.parser import Parser
from aiida.orm import Dict, CalcJobNode
from aiida_kkr.calculations.kkrnano import KKRnanoCalculation
#from aiida_kkr.tools import find_parent_structure
from masci_tools.io.common_functions import (search_string, open_general)
import numpy as np
from io import StringIO
Expand Down
10 changes: 7 additions & 3 deletions aiida_kkr/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .multi_imps_data_extract import MultiImpuritiesData
from .kick_out_core_states import *
from .neworder_potential import *
from .find_parent import get_calc_from_remote, get_remote, get_parent


# expose structure finder from VoronoiCalculation
Expand All @@ -29,8 +30,11 @@ def find_parent_structure(calc_or_remote, return_voro=False):
:param calc_or_remote: CalcJobNode or RemoteData node of VoronoiCalculation or KkrCalculation
:return struc: parent StructureData node
"""
from aiida_kkr.calculations.voro import VoronoiCalculation
from .find_parent import find_parent_structure

struc, voro_calc = VoronoiCalculation.find_parent_structure(calc_or_remote)
struc, voro_calc = find_parent_structure(calc_or_remote)

return struc
if return_voro:
return struc, voro_calc
else:
return struc
107 changes: 107 additions & 0 deletions aiida_kkr/tools/find_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
Helper tools to find parent calculations or structures.
"""

from aiida.common.exceptions import NotExistent


def get_struc(parent_calc):
"""
Get structure from a parent_folder (result of a calculation, typically a remote folder)
"""
return parent_calc.inputs.structure


def has_struc(parent_folder):
"""
Check if parent_folder has structure information in its input
"""
success = True
if 'structure' not in parent_folder.get_incoming().all_link_labels():
success = False
return success


def get_remote(parent_folder):
"""
get remote_folder from input if parent_folder is not already a remote folder
"""
parent_folder_tmp0 = parent_folder
try:
parent_folder_tmp = parent_folder_tmp0.get_incoming().get_node_by_label('remote_folder')
except NotExistent:
parent_folder_tmp = parent_folder_tmp0
return parent_folder_tmp


def get_parent(input_folder):
"""
Get the parent folder of the calculation. If no parent was found return input folder
"""
input_folder_tmp0 = input_folder

# first option: parent_calc_folder (KkrimpCalculation)
try:
parent_folder_tmp = input_folder_tmp0.get_incoming().get_node_by_label('parent_calc_folder')
return_input = False
except NotExistent:
return_input = True

# second option: parent_folder (KkrCalculation)
try:
parent_folder_tmp = input_folder_tmp0.get_incoming().get_node_by_label('parent_folder')
return_input = False
except NotExistent:
return_input = return_input & True

# third option: parent_KKR option (special mode of VoronoiCalculation)
try:
parent_folder_tmp = input_folder_tmp0.get_incoming().get_node_by_label('parent_KKR')
return_input = False
except NotExistent:
return_input = return_input & True

if return_input:
parent_folder_tmp = input_folder_tmp0

return parent_folder_tmp


def find_parent_structure(parent_folder):
"""
Find the Structure node recuresively in chain of parent calculations (structure node is input to voronoi calculation)
"""
iiter = 0
Nmaxiter = 1000
parent_folder_tmp = get_remote(parent_folder)
while not has_struc(parent_folder_tmp) and iiter < Nmaxiter:
parent_folder_tmp = get_remote(get_parent(parent_folder_tmp))
iiter += 1
if iiter % 200 == 0:
print(
'Warning: find_parent_structure takes quite long (already searched {} ancestors). Stop after {}'.format(
iiter, Nmaxiter
)
)
if has_struc(parent_folder_tmp):
struc = get_struc(parent_folder_tmp)
return struc, parent_folder_tmp
else:
raise ValueError('structure not found')


def get_calc_from_remote(calc_remote):
"""
Get the parent calculation from a RemoteData
"""
from aiida.orm import CalcJobNode
from aiida.orm import RemoteData

if not isinstance(calc_remote, RemoteData):
raise ValueError('input node is not a RemoteData folder')

parents = calc_remote.get_incoming(node_class=CalcJobNode).all()
if len(parents) != 1:
raise ValueError('Parent is not unique!')

return parents[0].node
Loading