/usr/share/psi/python/p4util/util.py is in psi4-data 1:0.3-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | #
#@BEGIN LICENSE
#
# PSI4: an ab initio quantum chemistry software package
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#@END LICENSE
#
"""Module with utility functions for use in input files."""
#CUimport psi4
import sys
import os
import math
from p4xcpt import *
def cubefile(filename='psi4.cube', nptsx=50, nptsy=50, nptsz=50, buffer_size=5.0, prop='density',**kwargs):
cube = psi4.CubeFile()
cube.set_filename(filename)
cube.set_npts(nptsx, nptsy, nptsz)
cube.set_buffer(buffer_size)
if prop.upper() == 'DENSITY':
cube.process_density();
else:
raise ValidationError('%s is not a valid property')
def oeprop(*args, **kwargs):
oe = psi4.OEProp()
if 'title' in kwargs:
oe.set_title(kwargs['title'])
for prop in args:
oe.add(prop)
oe.compute()
def cubeprop(*args, **kwargs):
"""Evaluate properties on a grid and generate cube files.
"""
# By default compute the orbitals
if not psi4.has_global_option_changed('CUBEPROP_TASKS'):
psi4.set_global_option('CUBEPROP_TASKS',['ORBITALS'])
cp = psi4.CubeProperties()
cp.compute_properties()
def set_memory(bytes):
"""Function to reset the total memory allocation."""
psi4.set_memory(bytes)
def get_memory():
"""Function to return the total memory allocation."""
return psi4.get_memory()
def set_num_threads(nthread):
"""Function to reset the number of threads to parallelize across."""
psi4.set_nthread(nthread)
def get_num_threads():
"""Function to return the number of threads to parallelize across."""
return psi4.nthread()
def success(label):
"""Function to print a '*label*...PASSED' line to screen.
Used by :py:func:`util.compare_values` family when functions pass.
"""
print('\t{0:.<66}PASSED'.format(label))
sys.stdout.flush()
# Test functions
def compare_values(expected, computed, digits, label):
"""Function to compare two values. Prints :py:func:`util.success`
when value *computed* matches value *expected* to number of *digits*.
Performs a system exit on failure. Used in input files in the test suite.
"""
message = ("\t%s: computed value (%.*f) does not match (%.*f) to %d decimal places." % (label, digits+1, computed, digits+1, expected, digits))
if (abs(expected - computed) > 10 ** (-digits)):
print(message)
raise TestComparisonError(message)
if ( math.isnan(computed) ):
print(message)
print("\tprobably because the computed value is nan.")
raise TestComparisonError(message)
success(label)
def compare_integers(expected, computed, label):
"""Function to compare two integers. Prints :py:func:`util.success`
when value *computed* matches value *expected*.
Performs a system exit on failure. Used in input files in the test suite.
"""
if (expected != computed):
message = ("\t%s: computed value (%d) does not match (%d)." % (label, computed, expected))
raise TestComparisonError(message)
success(label)
def compare_strings(expected, computed, label):
"""Function to compare two strings. Prints :py:func:`util.success`
when string *computed* exactly matches string *expected*.
Performs a system exit on failure. Used in input files in the test suite.
"""
if(expected != computed):
message = ("\t%s: computed value (%s) does not match (%s)." % (label, computed, expected))
raise TestComparisonError(message)
success(label)
def compare_matrices(expected, computed, digits, label):
"""Function to compare two matrices. Prints :py:func:`util.success`
when elements of matrix *computed* match elements of matrix *expected* to
number of *digits*. Performs a system exit on failure to match symmetry
structure, dimensions, or element values. Used in input files in the test suite.
"""
if (expected.nirrep() != computed.nirrep()):
message = ("\t%s has %d irreps, but %s has %d\n." % (expected.name(), expected.nirrep(), computed.name(), computed.nirrep()))
raise TestComparisonError(message)
if (expected.symmetry() != computed.symmetry()):
message = ("\t%s has %d symmetry, but %s has %d\n." % (expected.name(), expected.symmetry(), computed.name(), computed.symmetry()))
raise TestComparisonError(message)
nirreps = expected.nirrep()
symmetry = expected.symmetry()
for irrep in range(nirreps):
if(expected.rows(irrep) != computed.rows(irrep)):
message = ("\t%s has %d rows in irrep %d, but %s has %d\n." % (expected.name(), expected.rows(irrep), irrep, computed.name(), computed.rows(irrep)))
raise TestComparisonError(message)
if(expected.cols(irrep ^ symmetry) != computed.cols(irrep ^ symmetry)):
message = ("\t%s has %d columns in irrep, but %s has %d\n." % (expected.name(), expected.cols(irrep), irrep, computed.name(), computed.cols(irrep)))
raise TestComparisonError(message)
rows = expected.rows(irrep)
cols = expected.cols(irrep ^ symmetry)
failed = 0
for row in range(rows):
for col in range(cols):
if(abs(expected.get(irrep, row, col) - computed.get(irrep, row, col)) > 10 ** (-digits)):
print("\t%s: computed value (%s) does not match (%s)." % (label, expected.get(irrep, row, col), computed.get(irrep, row, col)))
failed = 1
break
if(failed):
print("Check your output file for reporting of the matrices.")
psi4.print_out("The Failed Test Matrices\n")
psi4.print_out("Computed Matrix (2nd matrix passed in)\n")
computed.print_out()
psi4.print_out("Expected Matrix (1st matrix passed in)\n")
expected.print_out()
raise TestComparisonError("\n")
success(label)
def compare_vectors(expected, computed, digits, label):
"""Function to compare two vectors. Prints :py:func:`util.success`
when elements of vector *computed* match elements of vector *expected* to
number of *digits*. Performs a system exit on failure to match symmetry
structure, dimension, or element values. Used in input files in the test suite.
"""
if (expected.nirrep() != computed.nirrep()):
message = ("\t%s has %d irreps, but %s has %d\n." % (expected.name(), expected.nirrep(), computed.name(), computed.nirrep()))
raise TestComparisonError(message)
nirreps = expected.nirrep()
for irrep in range(nirreps):
if(expected.dim(irrep) != computed.dim(irrep)):
message = ("\tThe reference has %d entries in irrep %d, but the computed vector has %d\n." % (expected.dim(irrep), irrep, computed.dim(irrep)))
raise TestComparisonError(message)
dim = expected.dim(irrep)
failed = 0
for entry in range(dim):
if(abs(expected.get(irrep, entry) - computed.get(irrep, entry)) > 10 ** (-digits)):
failed = 1
break
if(failed):
psi4.print_out("The computed vector\n")
computed.print_out()
psi4.print_out("The reference vector\n")
expected.print_out()
message = ("\t%s: computed value (%s) does not match (%s)." % (label, computed.get(irrep, entry), expected.get(irrep, entry)))
raise TestComparisonError(message)
success(label)
def compare_cubes(expected, computed, label):
"""Function to compare two cube files. Prints :py:func:`util.success`
when value *computed* matches value *expected*.
Performs a system exit on failure. Used in input files in the test suite.
"""
# Skip the first six elemets which are just labels
evec = [float(k) for k in expected.split()[6:]]
cvec = [float(k) for k in computed.split()[6:]]
if len(evec) == len(cvec):
for n in xrange(len(evec)):
if (math.fabs(evec[n]-cvec[n]) > 1.0e-5):
message = ("\t%s: computed cube file does not match expected cube file." % label)
raise TestComparisonError(message)
else:
message = ("\t%s: computed cube file does not match expected cube file." % (label, computed, expected))
raise TestComparisonError(message)
success(label)
def copy_file_to_scratch(filename, prefix, namespace, unit, move = False):
"""Function to move file into scratch with correct naming
convention.
Arguments:
@arg filename full path to file
@arg prefix computation prefix, usually 'psi'
@arg namespace context namespace, usually molecule name
@arg unit unit number, e.g. 32
@arg move copy or move? (default copy)
Example:
Assume PID is 12345 and SCRATCH is /scratch/parrish/
copy_file_to_scratch('temp', 'psi', 'h2o', 32):
-cp ./temp /scratch/parrish/psi.12345.h2o.32
copy_file_to_scratch('/tmp/temp', 'psi', 'h2o', 32):
-cp /tmp/temp /scratch/parrish/psi.12345.h2o.32
copy_file_to_scratch('/tmp/temp', 'psi', '', 32):
-cp /tmp/temp /scratch/parrish/psi.12345.32
copy_file_to_scratch('/tmp/temp', 'psi', '', 32, True):
-mv /tmp/temp /scratch/parrish/psi.12345.32
"""
pid = str(os.getpid())
scratch = psi4.IOManager.shared_object().get_file_path(int(unit))
cp = '/bin/cp';
if move:
cp = '/bin/mv';
unit = str(unit)
target = ''
target += prefix
target += '.'
target += pid
if len(namespace):
target += '.'
target += namespace
target += '.'
target += unit
command = ('%s %s %s/%s' % (cp, filename, scratch, target))
os.system(command)
#print command
def copy_file_from_scratch(filename, prefix, namespace, unit, move = False):
"""Function to move file out of scratch with correct naming
convention.
Arguments:
@arg filename full path to target file
@arg prefix computation prefix, usually 'psi'
@arg namespace context namespace, usually molecule name
@arg unit unit number, e.g. 32
@arg move copy or move? (default copy)
Example:
Assume PID is 12345 and SCRATCH is /scratch/parrish/
copy_file_to_scratch('temp', 'psi', 'h2o', 32):
-cp /scratch/parrish/psi.12345.h2o.32 .temp
copy_file_to_scratch('/tmp/temp', 'psi', 'h2o', 32):
-cp /scratch/parrish/psi.12345.h2o.32 /tmp/temp
copy_file_to_scratch('/tmp/temp', 'psi', '', 32):
-cp /scratch/parrish/psi.12345.32 /tmp/temp
copy_file_to_scratch('/tmp/temp', 'psi', '', 32, True):
-mv /scratch/parrish/psi.12345.32 /tmp/temp
"""
pid = str(os.getpid())
scratch = psi4.IOManager.shared_object().get_file_path(int(unit))
cp = '/bin/cp';
if move:
cp = '/bin/mv';
unit = str(unit)
target = ''
target += prefix
target += '.'
target += pid
if len(namespace):
target += '.'
target += namespace
target += '.'
target += unit
command = ('%s %s/%s %s' % (cp, scratch, target, filename))
os.system(command)
def xml2dict(filename=None):
"""Read XML *filename* into nested OrderedDict-s. *filename* defaults to
active CSX file.
"""
import xmltodict as xd
if filename is None:
csx = os.path.splitext(psi4.outfile_name())[0] + '.csx'
else:
csx = filename
with open(csx, 'r') as handle:
csxdict = xd.parse(handle)
return csxdict
def getFromDict(dataDict, mapList):
return reduce(lambda d, k: d[k], mapList, dataDict)
def csx2endict():
"""Grabs the CSX file as a dictionary, encodes translation of PSI variables
to XML blocks, gathers all available energies from CSX file into returned
dictionary.
"""
blockprefix = ['chemicalSemantics', 'molecularCalculation', 'quantumMechanics', 'singleReferenceState', 'singleDeterminant']
blockmidfix = ['energies', 'energy']
prefix = 'cs:'
pv2xml = {
'MP2 CORRELATION ENERGY': [['mp2'], 'correlation'],
'MP2 SAME-SPIN CORRELATION ENERGY': [['mp2'], 'sameSpin correlation'],
'HF TOTAL ENERGY': [['abinitioScf'], 'electronic'],
'NUCLEAR REPULSION ENERGY': [['abinitioScf'], 'nuclearRepulsion'],
'DFT FUNCTIONAL TOTAL ENERGY': [['dft'], 'dftFunctional'],
'DFT TOTAL ENERGY': [['dft'], 'electronic'],
'DOUBLE-HYBRID CORRECTION ENERGY': [['dft'], 'doubleHybrid correction'],
'DISPERSION CORRECTION ENERGY': [['dft'], 'dispersion correction'],
}
csxdict = xml2dict()
enedict = {}
for pv, lpv in pv2xml.iteritems():
address = blockprefix + lpv[0] + blockmidfix
indices = [prefix + bit for bit in address]
try:
qwer = getFromDict(csxdict, indices)
except KeyError:
continue
for v in qwer:
vv = v.values()
if vv[0] == prefix + lpv[1]:
enedict[pv] = float(vv[1])
return enedict
def compare_csx():
"""Function to validate energies in CSX files against PSIvariables. Only
active if write_csx flag on.
"""
if 'csx4psi' in sys.modules.keys():
if psi4.get_global_option('WRITE_CSX'):
enedict = csx2endict()
compare_integers(len(enedict) >= 2, True, 'CSX harvested')
for pv, en in enedict.iteritems():
compare_values(psi4.get_variable(pv), en, 6, 'CSX ' + pv + ' ' + str(round(en, 4)))
|