Skip to content

Commit

Permalink
Merge branch 'master' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
b3m2a1 committed Sep 19, 2024
2 parents c60a281 + 13e59c1 commit 1c0ac1b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 29 deletions.
25 changes: 18 additions & 7 deletions Psience/VPT2/DegeneracySpecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,12 @@ class StronglyCoupledDegeneracySpec(DegeneracySpec):
"""
"""
application_order = 'post'
# application_order = 'post'
format = DegenerateSpaceInputFormat.StrongCouplings
default_threshold=.3
def __init__(self, wfc_threshold=None, state_filter=None, extend_spaces=True, iterations=None, **opts):
def __init__(self, wfc_threshold=None, state_filter=None, extend_spaces=True, iterations=None,
evaluator=None,
**opts):
super().__init__(**opts)
if wfc_threshold is None or isinstance(wfc_threshold, str) and wfc_threshold == 'auto':
wfc_threshold = self.default_threshold
Expand All @@ -412,6 +414,11 @@ def __init__(self, wfc_threshold=None, state_filter=None, extend_spaces=True, it
self.extend_spaces=extend_spaces
self._iterations = iterations
self.iterations = iterations
self.evaluator = evaluator

@property
def application_order(self):
return 'post' if self.evaluator is None else 'pre'

repr_opts = ['energy_cutoff', 'wfc_threshold']
def prep_states(self, input_states):
Expand Down Expand Up @@ -446,11 +453,15 @@ def get_groups(self, input_states, couplings=None, solver=None, extra_groups=Non
:return:
:rtype:
"""
if couplings is None:
raise ValueError("need couplings")
if extra_groups is None:
extra_groups = self.extra_groups
return self.get_strong_coupling_space(input_states, couplings, extra_groups=extra_groups)
if self.evaluator is not None:
raise NotImplementedError("in process")
wfcs = self.evaluator.get_test_wfn_corrs(input_states, self.wfc_threshold)
else:
if couplings is None:
raise ValueError("need couplings")
if extra_groups is None:
extra_groups = self.extra_groups
return self.get_strong_coupling_space(input_states, couplings, extra_groups=extra_groups)

@classmethod
def canonicalize(cls, spec):
Expand Down
88 changes: 68 additions & 20 deletions Psience/VPT2/Runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ def __init__(self,
states,
degeneracy_specs=None,
system=None,
frequencies=None
frequencies=None,
evaluator=None
):
"""
:param states: A list of states or a number of quanta to target
Expand Down Expand Up @@ -313,7 +314,9 @@ def __init__(self,
deg_pair_blocks = []
all_states = np.asanyarray(self.state_list)
for spec in degeneracy_specs:
deg_spec, deg_states = self.build_degenerate_state_spaces(spec, states, system=system, freqs=frequencies)
deg_spec, deg_states = self.build_degenerate_state_spaces(spec, states,
evaluator=evaluator,
system=system, freqs=frequencies)
self.degeneracy_specs.append(deg_spec)

if deg_states is not None:
Expand Down Expand Up @@ -416,7 +419,9 @@ def get_state_list_from_quanta(cls, n_quanta, n_modes, target_modes=None, only_t
whee = [p for p in whee if all(j in target_modes or x == 0 for j,x in enumerate(p))]
return whee

def build_degenerate_state_spaces(self, degeneracy_specs, states, system=None, freqs=None) -> '(None|DegeneracySpec, None|list[np.ndarray])':
def build_degenerate_state_spaces(self, degeneracy_specs, states, system=None,
evaluator=None,
freqs=None) -> '(None|DegeneracySpec, None|list[np.ndarray])':
"""
:param degeneracy_specs:
:type degeneracy_specs:
Expand All @@ -425,6 +430,8 @@ def build_degenerate_state_spaces(self, degeneracy_specs, states, system=None, f
"""

spec = DegeneracySpec.from_spec(degeneracy_specs)
if hasattr(spec, 'evalutor') and spec.evalutor is None:
spec.evalutor = evaluator
if hasattr(spec, 'frequencies') and spec.frequencies is None:
if freqs is None:
freqs = system.mol.normal_modes.modes.freqs
Expand Down Expand Up @@ -2276,7 +2283,12 @@ class MultiVPTStateSpace:
"""
Generalizes a VPTStateSpace to pairs of initial and final spaces
"""
def __init__(self, state_space_pairs, system=None, degeneracy_specs=None, **opts):
def __init__(self, state_space_pairs,
system=None,
degeneracy_specs=None,
evaluator=None,
**opts
):
if (
nput.is_numeric(state_space_pairs)
or isinstance(state_space_pairs, VPTStateSpace)
Expand All @@ -2290,10 +2302,13 @@ def __init__(self, state_space_pairs, system=None, degeneracy_specs=None, **opts
[initial_space]
if not nput.is_numeric(initial_space) and nput.is_numeric(initial_space[0]) else
initial_space,
degeneracy_specs=degeneracy_specs, **opts),
degeneracy_specs=degeneracy_specs,
**opts),
VPTStateSpace.from_system_and_spec(system,
target_space,
degeneracy_specs=degeneracy_specs, **opts)
degeneracy_specs=degeneracy_specs,
**opts
)
]
for initial_space, target_space in state_space_pairs
]
Expand All @@ -2314,9 +2329,19 @@ def __init__(self, state_space_pairs, system=None, degeneracy_specs=None, **opts
s.degenerate_states
for pair in self.space_pairs
for s in pair
if s.degenerate_states is not None
]
flat_degs = np.unique(
[
p
for pair in self.space_pairs
for s in pair
for p in (s.degenerate_pairs if s.degenerate_pairs is not None else [])
], axis=0
)
if len(flat_degs) == 0: flat_degs = None
if len(flat_deg_blocks) == 0:
flat_deg_blocks = None
flat_degs = None
elif len(flat_deg_blocks) == 1:
flat_deg_blocks = flat_deg_blocks[0]
else:
Expand All @@ -2335,6 +2360,7 @@ def __init__(self, state_space_pairs, system=None, degeneracy_specs=None, **opts
for d in flat_deg_blocks
]


new_pairs = []
for pair in self.space_pairs:
pp = []
Expand All @@ -2348,16 +2374,6 @@ def __init__(self, state_space_pairs, system=None, degeneracy_specs=None, **opts
new_pairs.append(pp)
self.space_pairs = new_pairs

flat_degs = np.unique(
[
p
for pair in self.space_pairs
for s in pair
for p in (s.degenerate_pairs if s.degenerate_pairs is not None else [])
], axis=0
)
if len(flat_degs) == 0: flat_degs = None

self.flat_space = flat_space
self.flat_space.degenerate_states = flat_deg_blocks
self.flat_space.degenerate_pairs = flat_degs
Expand Down Expand Up @@ -2638,7 +2654,10 @@ def prep_multispace(self, states, freqs, degeneracy_specs=None):

return states
def prep_states(self, states, degeneracy_specs=None):
return self.prep_multispace(states, self.eval.freqs, degeneracy_specs=degeneracy_specs)
return self.prep_multispace(states,
self.eval.freqs,
degeneracy_specs=degeneracy_specs
)
# else:
# return states

Expand Down Expand Up @@ -2878,6 +2897,32 @@ def get_reexpressed_hamiltonian(self, states, order=None,
all_mats.append(sum(corr_mats))
return all_mats, all_corrs

def get_test_wfn_corrs(self, input_states:BasisStateSpace, threshold):
"""
We take the expansions and frequencies that we have and at find the possible terms
that could possibly lead to a correction greater than the specified threshold
To do this, we first determine from the expansions what magnitude of energy difference
could possible lead to terms above this threshold
"""
exc = input_states.excitations
freqs = self.eval.freqs
expansions = self.eval.expansions
threshold_cutoffs = [
sum(np.max(np.abs(e)) for e in exp)
for exp in expansions[1:]
]
freq_sums = []

for ord in range(3, 3+len(threshold_cutoffs)):
freq_sums = ...

for subexp in expansions[1:]:
# We'd like to consider the space of different number of quanta changes
# and find the corresponding
...



def format_energies_table(self, states, energies, energy_corrections, zpe_pos, number_format=".3f"):

nord = len(energy_corrections)
Expand Down Expand Up @@ -3123,7 +3168,10 @@ def run_VPT(self,
s=group,
preformatter=lambda **kw: dict(
kw,
s=self.format_matrix(kw['s'])
s="\n".join(
VPTStateMaker.parse_state(k)
for k in kw['s']
)
))
with self.logger.block(tag="Effective Hamiltonian"):
self.logger.log_print(
Expand Down Expand Up @@ -3231,6 +3279,6 @@ def run_simple(cls,
)

if return_runner:
res = (runner,) + res
return runner, res
else:
return res
39 changes: 37 additions & 2 deletions ci/tests/VPT2Tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,35 @@ def test_HOHAnalytic(self):
expressions_file=os.path.expanduser("~/Desktop/exprs.hdf5")
)

@validationTest
def test_AnalyticWFC(self):

file_name = "OCHH_freq.fchk"
AnalyticVPTRunner.run_simple(
TestManager.test_data(file_name),
[
[
0,
[
[0, 0, 0, 0, 0, 1],
[0, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 1, 0],
],
],
[
[0, 0, 0, 0, 1, 0],
[
[0, 0, 0, 0, 1, 1],
[0, 1, 0, 1, 1, 0]
]
]
],
expressions_file=os.path.expanduser("~/Desktop/exprs.hdf5"),
degeneracy_specs='auto',
handle_degeneracies=True
)

@debugTest
def test_AnalyticOCHHMultiple(self):

Expand All @@ -106,9 +135,9 @@ def test_AnalyticOCHHMultiple(self):
0,
[
[0, 0, 0, 0, 0, 1],
# [0, 1, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 1, 0, 1, 0, 0],
# [0, 0, 0, 1, 1, 0],
# [0, 0, 0, 0, 1, 0],
],
],
[
Expand All @@ -120,6 +149,12 @@ def test_AnalyticOCHHMultiple(self):
]
],
expressions_file=os.path.expanduser("~/Desktop/exprs.hdf5"),
# degeneracy_specs=None,
# degeneracy_specs = {
# 'polyads': [
# [[0, 0, 0, 0, 0, 1], [0, 1, 0, 1, 0, 0]]
# ]
# }
degeneracy_specs=[
{
'polyads': [
Expand Down

0 comments on commit 1c0ac1b

Please sign in to comment.