diff --git a/qcmanybody/tests/test_computer_het4_gradient.py b/qcmanybody/tests/test_computer_het4_gradient.py index 582f209..a211ea3 100644 --- a/qcmanybody/tests/test_computer_het4_gradient.py +++ b/qcmanybody/tests/test_computer_het4_gradient.py @@ -243,11 +243,10 @@ def het_tetramer(): 15, id="4b_nocp_rtd_ee"), ]) -def test_nbody_het4_grad(mbe_keywords, anskeyE, anskeyG, bodykeys, outstrs, calcinfo_nmbe, het_tetramer, request, mbe_data_grad_dtz): +def test_nbody_het4_grad(mbe_keywords, anskeyE, anskeyG, bodykeys, outstrs, calcinfo_nmbe, het_tetramer, request, mbe_data_grad_dtz, monkeypatch): _inner = request.node.name.split("[")[1].split("]")[0] kwdsln, pattern, progln = _inner, "", "psi4" - print("LANE", kwdsln, pattern, progln) - os.environ["QCMANYBODY_EMBEDDING_CHARGES"] = "1" + monkeypatch.setenv("QCMANYBODY_EMBEDDING_CHARGES", "1") mbe_keywords = ManyBodyKeywords(**mbe_keywords) mbe_data_grad_dtz["molecule"] = het_tetramer diff --git a/qcmanybody/utils.py b/qcmanybody/utils.py index e368e73..892d76e 100644 --- a/qcmanybody/utils.py +++ b/qcmanybody/utils.py @@ -2,6 +2,7 @@ import ast import json +import math import re import string from typing import Any, Dict, Iterable, List, Literal, Mapping, Optional, Set, Tuple, Union @@ -222,13 +223,22 @@ def sum_cluster_data( shape = find_shape(data[first_key]) ret = shaped_zero(shape) - for frag, bas in compute_list: - egh = data[labeler(mc_level_lbl, frag, bas)] - - if vmfc: - sign = (-1) ** (nb - len(frag)) - - ret += sign * egh + precise_sum_func = math.fsum if isinstance(ret, float) else np.sum + ret = precise_sum_func( + (((-1) ** (nb - len(frag))) if vmfc else 1) * (data[labeler(mc_level_lbl, frag, bas)]) + for frag, bas in compute_list + ) + + # A more readable format for the above but not ammenable to using specialty summation functions + # ``` + # for frag, bas in compute_list: + # egh = data[labeler(mc_level_lbl, frag, bas)] + # + # if vmfc: + # sign = (-1) ** (nb - len(frag)) + # + # ret += sign * egh + # ``` return ret