diff --git a/src/aiida_epw/tools/workchain.py b/src/aiida_epw/tools/workchain.py index 5dfc9d8a..1e3d5471 100644 --- a/src/aiida_epw/tools/workchain.py +++ b/src/aiida_epw/tools/workchain.py @@ -342,3 +342,23 @@ def get_default_target_basepath(computer): else: raise ValueError(f"Unsupported transport type: {computer.transport_type}") return target_basepath + + +def set_auto_temps( + parameters, allen_dynes_tc, lower_bound=0.5, upper_bound=2.0, nstemp=10 +): + """Automatically set EPW calculation temps parameter from Allen-Dynes critical temperature of a previous calculation. + + If `temps` is not specified or is None in inputs.parameters['INPUTEPW'], + it sets `nstemp` to 10 and `temps` to be a range between 0.5 * Tc and 2.0 * Tc. + """ + inputepw = parameters.setdefault("INPUTEPW", {}) + + if "temps" not in inputepw or inputepw.get("temps") is None: + tmin = lower_bound * allen_dynes_tc + tmax = upper_bound * allen_dynes_tc + + inputepw["nstemp"] = nstemp + inputepw["temps"] = f"{tmin:.4f} {tmax:.4f}" + + return parameters diff --git a/src/aiida_epw/workflows/supercon.py b/src/aiida_epw/workflows/supercon.py index 6cd7edde..d275ba32 100644 --- a/src/aiida_epw/workflows/supercon.py +++ b/src/aiida_epw/workflows/supercon.py @@ -11,6 +11,7 @@ from aiida_epw.calculations.epw import serialize_restart_type from aiida_epw.workflows.base import EpwBaseWorkChain from aiida_epw.data import A2fData +from aiida_epw.tools.workchain import set_auto_temps from aiida.engine import calcfunction @@ -462,6 +463,10 @@ def run_final_epw_iso(self): inputs = AttributeDict( self.exposed_inputs(EpwBaseWorkChain, namespace="epw_final_iso") ) + allen_dynes_tc = self.ctx.epw_interp[-1].outputs.output_parameters[ + "Allen_Dynes_Tc" + ] + new_parameters = set_auto_temps(inputs.parameters.get_dict(), allen_dynes_tc) inputs.structure = self.inputs.structure parent_folder_epw = self.ctx.epw_interp[-1].outputs.remote_folder @@ -472,9 +477,9 @@ def run_final_epw_iso(self): inputs.restart_type = serialize_restart_type("ephread") if self.ctx.degaussq: - parameters = inputs.parameters.get_dict() - parameters["INPUTEPW"]["degaussq"] = self.ctx.degaussq - inputs.parameters = orm.Dict(parameters) + new_parameters["INPUTEPW"]["degaussq"] = self.ctx.degaussq + + inputs.parameters = orm.Dict(new_parameters) inputs.metadata.call_link_label = "epw_final_iso" @@ -501,6 +506,11 @@ def run_final_epw_aniso(self): self.exposed_inputs(EpwBaseWorkChain, namespace="epw_final_aniso") ) + allen_dynes_tc = self.ctx.epw_interp[-1].outputs.output_parameters[ + "Allen_Dynes_Tc" + ] + new_parameters = set_auto_temps(inputs.parameters.get_dict(), allen_dynes_tc) + inputs.structure = self.inputs.structure parent_folder_epw = self.ctx.epw_interp[-1].outputs.remote_folder inputs.parent_folder_epw = parent_folder_epw @@ -509,6 +519,8 @@ def run_final_epw_aniso(self): inputs.restart_type = serialize_restart_type("ephread") + inputs.parameters = orm.Dict(new_parameters) + inputs.metadata.call_link_label = "epw_final_aniso" workchain_node = self.submit(EpwBaseWorkChain, **inputs) self.report( diff --git a/tests/tools/test_workchain.py b/tests/tools/test_workchain.py index ad7710de..06259081 100644 --- a/tests/tools/test_workchain.py +++ b/tests/tools/test_workchain.py @@ -218,3 +218,25 @@ def get_configuration(self): ssh_computer = MockSshComputer() assert get_default_target_basepath(ssh_computer) == "/remote/testuser/stash" + + +def test_set_auto_temps(): + """Test set_auto_temps correctly parses Allen_Dynes_Tc and configures input parameters.""" + from aiida_epw.tools.workchain import set_auto_temps + + # 1. Mock parameters and tc + parameters = {"INPUTEPW": {}} + allen_dynes_tc = 12.0 + + # 2. Run set_auto_temps + result = set_auto_temps(parameters, allen_dynes_tc) + + # 3. Assert outputs + assert result["INPUTEPW"]["nstemp"] == 10 + assert result["INPUTEPW"]["temps"] == "6.0000 24.0000" + + # 4. Assert that if temps is already defined, it is not overwritten + params_predefined = {"INPUTEPW": {"temps": "5.0000 45.0000", "nstemp": 40}} + result_predefined = set_auto_temps(params_predefined, allen_dynes_tc) + assert result_predefined["INPUTEPW"]["nstemp"] == 40 + assert result_predefined["INPUTEPW"]["temps"] == "5.0000 45.0000"