From 9d1f3ab81112a86bcaad235a19b8404a3909d21a Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Thu, 17 Jul 2025 20:55:49 +0000 Subject: [PATCH 1/3] Give an error if the output will likely be lost when redirected to one file --- gpu4pyscf/properties/eda.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gpu4pyscf/properties/eda.py b/gpu4pyscf/properties/eda.py index db0a006c..9b5380ca 100644 --- a/gpu4pyscf/properties/eda.py +++ b/gpu4pyscf/properties/eda.py @@ -29,6 +29,7 @@ from gpu4pyscf.lib.diis import DIIS from gpu4pyscf.lib import logger import time +import warnings # np.set_printoptions(linewidth = np.iinfo(np.int32).max, threshold = np.iinfo(np.int32).max, precision = 16, suppress = True) @@ -1080,6 +1081,7 @@ def _make_mf(mol, if_kernel = True, dispersion_free_xc = None): mf.direct_scf_tol = 1e-16 if if_kernel: energy = mf.kernel() + mf.mol.stdout.flush() assert mf.converged return mf, energy else: @@ -1092,9 +1094,23 @@ def _get_gradient(mf): gradient = grad_obj.kernel() if isinstance(gradient, cp.ndarray): gradient = gradient.get() + mf.mol.stdout.flush() return grad_obj.kernel() n_frag = len(mol_list) + for i_frag in range(n_frag): + for j_frag in range(i_frag + 1, n_frag): + if mol_list[i_frag].stdout != mol_list[j_frag].stdout: + warnings.warn("The stdout of each mol in mol_list is not consistent. We do not guarantee which stdout to write.") + if mol_list[i_frag].stdout.name == mol_list[j_frag].stdout.name: + import os + if mol_list[i_frag].stdout.name != os.devnull: + raise ValueError("The stdout of each mol in mol_list points to the same file but has different handle, " + "which usually means the same file is opened more than once by multiple mols. " + "This will cause outputs of earlier-created mols lost into vacuum. If you encounter this error message, " + "when creating mol objects, please specify the \"output\" field of each mol to None, and afterward, " + "open the desired output file, and change each mol.stdout to the file handle of that file.") + mf_list = [] frag_energy_list = [] frag_gradient_list = [] From d5d4caf0cbe6d364d90d5045b319cd62e6ffbb67 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Thu, 17 Jul 2025 22:09:24 +0000 Subject: [PATCH 2/3] Fix eda example --- examples/36-amlo_eda.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/36-amlo_eda.py b/examples/36-amlo_eda.py index 1574e828..178f7786 100644 --- a/examples/36-amlo_eda.py +++ b/examples/36-amlo_eda.py @@ -42,8 +42,9 @@ mol_list = [mol1, mol2] -eda_result = eda.eval_ALMO_EDA_2_energies(mol_list, xc = "wB97M-V") +eda_result, dft_result = eda.eval_ALMO_EDA_2_energies(mol_list, xc = "wB97M-V") print(f"EDA result in dict form: {eda_result}") +print(f"DFT energies of each fragment and the total system: {dft_result}") ### Reference output: # Fragment 0 energy = -76.4334344665 Hartree From 635dec7455bdb473401ce28983418d02fb5aeb8c Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Wed, 23 Jul 2025 00:36:16 +0000 Subject: [PATCH 3/3] Not checking the stdout with the same filename, but only providing a general warning --- gpu4pyscf/properties/eda.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/gpu4pyscf/properties/eda.py b/gpu4pyscf/properties/eda.py index 9b5380ca..1c4a82dc 100644 --- a/gpu4pyscf/properties/eda.py +++ b/gpu4pyscf/properties/eda.py @@ -1101,15 +1101,9 @@ def _get_gradient(mf): for i_frag in range(n_frag): for j_frag in range(i_frag + 1, n_frag): if mol_list[i_frag].stdout != mol_list[j_frag].stdout: - warnings.warn("The stdout of each mol in mol_list is not consistent. We do not guarantee which stdout to write.") - if mol_list[i_frag].stdout.name == mol_list[j_frag].stdout.name: - import os - if mol_list[i_frag].stdout.name != os.devnull: - raise ValueError("The stdout of each mol in mol_list points to the same file but has different handle, " - "which usually means the same file is opened more than once by multiple mols. " - "This will cause outputs of earlier-created mols lost into vacuum. If you encounter this error message, " - "when creating mol objects, please specify the \"output\" field of each mol to None, and afterward, " - "open the desired output file, and change each mol.stdout to the file handle of that file.") + warnings.warn("The stdout of each mol in mol_list is not consistent. We do not guarantee which stdout to write. " + "Notice if the mol objects share the same \"output\" value, then the same output file is opened " + "more than once, and the outputs of earlier-created mol will be lost.") mf_list = [] frag_energy_list = []