Skip to content

Commit

Permalink
reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
maxscheurer committed Jun 2, 2021
1 parent 983b2b0 commit 86e62e0
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 46 deletions.
15 changes: 8 additions & 7 deletions adcc/AdcMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ def __init__(self, method, hf_or_mp, block_orders=None, intermediates=None,
if not isinstance(method, AdcMethod):
method = AdcMethod(method)

if diagonal_precomputed:
if not isinstance(diagonal_precomputed, AmplitudeVector):
raise TypeError("diagonal_precomputed needs to be"
" an AmplitudeVector.")
if diagonal_precomputed.needs_evaluation:
raise ValueError("diagonal_precomputed must already"
" be evaluated.")

self.timer = Timer()
self.method = method
self.ground_state = hf_or_mp
Expand Down Expand Up @@ -159,12 +167,6 @@ def __init__(self, method, hf_or_mp, block_orders=None, intermediates=None,
# TODO Rename to self.block in 0.16.0
self.blocks_ph = {bl: blocks[bl].apply for bl in blocks}
if diagonal_precomputed:
if not isinstance(diagonal_precomputed, AmplitudeVector):
raise TypeError("diagonal_precomputed needs to be"
" an AmplitudeVector.")
if diagonal_precomputed.needs_evaluation:
raise ValueError("diagonal_precomputed must already"
" be evaluated.")
self.__diagonal = diagonal_precomputed
else:
self.__diagonal = sum(bl.diagonal for bl in blocks.values()
Expand Down Expand Up @@ -194,7 +196,6 @@ def patched_apply(ampl, original=orig_app, other=other_app):
self.blocks_ph[sp] = patched_apply
other_diagonal = sum(bl.diagonal for bl in other.blocks.values()
if bl.diagonal)
# iadd does not work with numbers
self.__diagonal = self.__diagonal + other_diagonal
self.__diagonal.evaluate()
self.extra_terms.append(other)
Expand Down
8 changes: 4 additions & 4 deletions adcc/ExcitedStates.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ def __iadd__(self, other):
for k in other:
self += k
else:
raise TypeError("Can only add EnergyCorrection (or list"
" of EnergyCorrection) to"
f" ExcitedState, not '{type(other)}'")
return NotImplemented
return self

def __add__(self, other):
if not isinstance(other, (EnergyCorrection, list)):
return NotImplemented
ret = ExcitedStates(self, self.method, self.property_method)
ret += other
return ret
Expand Down Expand Up @@ -376,7 +376,7 @@ def describe(self, oscillator_strengths=True, rotatory_strengths=False,
ev=self.excitation_energy[i] * eV, **fields)
text += separator + "\n"
if len(self._excitation_energy_corrections):
head_corr = "| excitation energy corrections included:"
head_corr = "| Excitation energy includes these corrections:"
text += head_corr
nspace = len(separator) - len(head_corr) - 1
text += nspace * " " + "|\n"
Expand Down
7 changes: 3 additions & 4 deletions adcc/backends/psi4.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def pe_energy(self, dm, elec_only=True):

@property
def excitation_energy_corrections(self):
ret = {}
ret = []
if self.environment == "pe":
ptlr = EnergyCorrection(
"pe_ptlr_correction",
Expand All @@ -120,9 +120,8 @@ def excitation_energy_corrections(self):
lambda view: self.pe_energy(view.state_diffdm_ao,
elec_only=True)
)
ret["pe_ptlr_correction"] = ptlr
ret["pe_ptss_correction"] = ptss
return ret
ret.extend([ptlr, ptss])
return {ec.name: ec for ec in ret}

@property
def environment(self):
Expand Down
10 changes: 3 additions & 7 deletions adcc/backends/pyscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def pe_energy(self, dm, elec_only=True):

@property
def excitation_energy_corrections(self):
ret = {}
ret = []
if self.environment == "pe":
ptlr = EnergyCorrection(
"pe_ptlr_correction",
Expand All @@ -139,12 +139,8 @@ def excitation_energy_corrections(self):
lambda view: self.pe_energy(view.state_diffdm_ao,
elec_only=True)
)
# NOTE: I don't like the duplicate 'name',
# but this way it's easier to exctract the corrections
# directly
ret["pe_ptlr_correction"] = ptlr
ret["pe_ptss_correction"] = ptss
return ret
ret.extend([ptlr, ptss])
return {ec.name: ec for ec in ret}

@property
def environment(self):
Expand Down
7 changes: 3 additions & 4 deletions adcc/backends/veloxchem.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def pe_energy(self, dm, elec_only=True):

@property
def excitation_energy_corrections(self):
ret = {}
ret = []
if hasattr(self.scfdrv, "pe_drv"):
ptlr = EnergyCorrection(
"pe_ptlr_correction",
Expand All @@ -143,9 +143,8 @@ def excitation_energy_corrections(self):
lambda view: self.pe_energy(view.state_diffdm_ao,
elec_only=True)
)
ret["pe_ptlr_correction"] = ptlr
ret["pe_ptss_correction"] = ptss
return ret
ret.extend([ptlr, ptss])
return {ec.name: ec for ec in ret}

def get_backend(self):
return "veloxchem"
Expand Down
14 changes: 7 additions & 7 deletions adcc/test_AdcMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,19 @@ def test_extra_term(self):
with pytest.raises(ValueError):
adcc.AdcMatrix("adc2", ground_state,
diagonal_precomputed=matrix.diagonal() + 42)
with pytest.raises(TypeError):
AdcExtraTerm(matrix, "fail")
with pytest.raises(TypeError):
AdcExtraTerm(matrix, {"fail": "not_callable"})

shift = -0.3
shifted = AdcMatrixShifted(matrix, shift)
# TODO: need to do this to differentiate between
# TODO: need to use AmplitudeVector to differentiate between
# diagonals for ph and pphh
# if we just pass numbers, i.e., shift
# we get 2*shift on the diagonal :(
# we get 2*shift on the diagonal
ones = matrix.diagonal().ones_like()

with pytest.raises(TypeError):
AdcExtraTerm(matrix, "fail")
with pytest.raises(TypeError):
AdcExtraTerm(matrix, {"fail": "not_callable"})

def __shift_ph(hf, mp, intermediates):
def apply(invec):
return adcc.AmplitudeVector(ph=shift * invec.ph)
Expand All @@ -260,6 +259,7 @@ def apply(invec):
# cannot add to 'pphh_pphh' in ADC(1) matrix
with pytest.raises(ValueError):
matrix_adc1 += extra

shifted_2 = matrix + extra
shifted_3 = extra + matrix
for manual in [shifted_2, shifted_3]:
Expand Down
26 changes: 14 additions & 12 deletions adcc/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,25 +522,27 @@ def setup_environment(matrix, environment):
raise InputError(
"Environment found in reference state, but no environment"
" configuration specified. Please select from the following"
f" schemes: {valid_envs}."
f" schemes: {valid_envs} or set to False."
)
elif environment and not hf.environment:
raise InputError(
"Environment specified, but no environment"
" was found in reference state."
)
elif not hf.environment:
environment = False

if isinstance(environment, bool):
environment = {"ptss": True, "ptlr": True} if environment else {}
elif isinstance(environment, list):
environment = {k: True for k in environment}
elif isinstance(environment, str):
environment = {environment: True}
elif not isinstance(environment, dict):
raise TypeError("Invalid type for environment parameter"
f"' {type(environment)}'.")
environment = {}

convertor = {
bool: lambda value: {"ptss": True, "ptlr": True} if value else {},
list: lambda value: {k: True for k in value},
str: lambda value: {value: True},
dict: lambda value: value,
}
conversion = convertor.get(type(environment), None)
if conversion is None:
raise TypeError("Cannot convert environment parameter of type"
f"'{type(environment)}' to dict.")
environment = conversion(environment)

if any(env not in valid_envs for env in environment):
raise InputError("Invalid key specified for environment."
Expand Down
31 changes: 31 additions & 0 deletions docs/calculations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,37 @@ b) with the linear response scheme. The results of both schemes are then printed
print(state_lr.describe())
The output of the last two lines is::

+--------------------------------------------------------------+
| adc2 singlet , converged |
+--------------------------------------------------------------+
| # excitation energy osc str |v1|^2 |v2|^2 |
| (au) (eV) |
| 0 0.1434972 3.904756 0.0000 0.9187 0.08128 |
| 1 0.1554448 4.229869 0.0000 0.9179 0.08211 |
| 2 0.2102638 5.721569 0.0209 0.8977 0.1023 |
| 3 0.2375643 6.464453 0.6198 0.9033 0.09666 |
| 4 0.2699134 7.344718 0.0762 0.8975 0.1025 |
+--------------------------------------------------------------+
| Excitation energy includes these corrections: |
| - pe_ptss_correction |
| - pe_ptlr_correction |
+--------------------------------------------------------------+

+--------------------------------------------------------------+
| adc2 singlet , converged |
+--------------------------------------------------------------+
| # excitation energy osc str |v1|^2 |v2|^2 |
| (au) (eV) |
| 0 0.1435641 3.906577 0.0000 0.9187 0.08128 |
| 1 0.1555516 4.232775 0.0000 0.9179 0.08211 |
| 2 0.210272 5.721794 0.0212 0.8977 0.1023 |
| 3 0.2378427 6.47203 0.6266 0.9034 0.09663 |
| 4 0.2698889 7.34405 0.0805 0.898 0.102 |
+--------------------------------------------------------------+


Further examples and details
----------------------------
Some further examples can be found in the ``examples`` folder
Expand Down
2 changes: 1 addition & 1 deletion libadcc/TensorImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class TensorImpl : public Tensor {
std::string describe_expression(std::string stage = "unoptimised") const override;

/** Convert object to btensor for use in libtensor functions. */
explicit operator libtensor::btensor<N, scalar_type> &() { return *libtensor_ptr(); }
explicit operator libtensor::btensor<N, scalar_type>&() { return *libtensor_ptr(); }

/** Return inner btensor object
*
Expand Down

0 comments on commit 86e62e0

Please sign in to comment.