class Part(JustOnceClass): name = None linear = False # whether the populations are linear in the density matrix. def __init__(self, system, grid, local, slow, lmax, moldens=None): ''' **Arguments:** system The system to be partitioned. grid The integration grid local Whether or not to use local (non-periodic) grids. slow When ``True``, also the AIM properties are computed that use the AIM overlap operators. lmax The maximum angular momentum in multipole expansions. **Optional arguments:** moldens The all-electron density grid data. ''' JustOnceClass.__init__(self) self._system = system self._grid = grid self._local = local self._slow = slow self._lmax = lmax # Caching stuff, to avoid recomputation of earlier results self._cache = Cache() # Caching of work arrays to avoid reallocation if moldens is not None: self._cache.dump('moldens', moldens) # Initialize the subgrids if local: self._init_subgrids() # Some screen logging self._init_log_base() self._init_log_scheme() self._init_log_memory() if log.do_medium: log.blank() def __getitem__(self, key): return self.cache.load(key) def _get_system(self): return self._system system = property(_get_system) def _get_grid(self): return self.get_grid() grid = property(_get_grid) def _get_local(self): return self._local local = property(_get_local) def _get_slow(self): return self._slow slow = property(_get_slow) def _get_lmax(self): return self._lmax lmax = property(_get_lmax) def _get_cache(self): return self._cache cache = property(_get_cache) def __clear__(self): self.clear() def clear(self): '''Discard all cached results, e.g. because wfn changed''' JustOnceClass.clear(self) self.cache.clear() def update_grid(self, grid): '''Specify a new grid **Arguments:** grid The new grid When the new and old grid are the same, no action is taken. When a really new grid is provided, the subgrids are updated and the cache is cleared. ''' if not (grid is self._grid): self._grid = grid if self.local: self._init_subgrids() self.clear() def get_grid(self, index=None): '''Return an integration grid **Optional arguments:** index The index of the atom. If not given, a grid for the entire system is returned. If self.local is False, a full system grid is always returned. ''' if index is None or not self.local: return self._grid else: return self._subgrids[index] def get_moldens(self, index=None, output=None): self.do_moldens() moldens = self.cache.load('moldens') result = self.to_atomic_grid(index, moldens) if output is not None: output[:] = result return result def get_spindens(self, index=None, output=None): self.do_spindens() spindens = self.cache.load('spindens') result = self.to_atomic_grid(index, spindens) if output is not None: output[:] = result return result def get_wcor(self, index): '''Return the weight corrections on a grid See get_grid for the meaning of the optional arguments ''' raise NotImplementedError def _init_subgrids(self): raise NotImplementedError def _init_log_base(self): raise NotImplementedError def _init_log_scheme(self): raise NotImplementedError def _init_log_memory(self): if log.do_medium: # precompute arrays sizes for certain grids nbyte_global = self.grid.size * 8 nbyte_locals = np.array( [self.get_grid(i).size * 8 for i in xrange(self.system.natom)]) # compute and report usage estimates = self.get_memory_estimates() nbyte_total = 0 log('Coarse estimate of memory usage for the partitioning:') log(' Label Memory[GB]') log.hline() for label, nlocals, nglobal in estimates: nbyte = np.dot(nlocals, nbyte_locals) + nglobal * nbyte_global log('%30s %10.3f' % (label, nbyte / 1024.0**3)) nbyte_total += nbyte log('%30s %10.3f' % ('Total', nbyte_total / 1024.0**3)) log.hline() log.blank() def get_memory_estimates(self): return [ ('Atomic weights', np.ones(self.system.natom), 0), ('Promolecule', np.zeros(self.system.natom), 1), ('Working arrays', np.zeros(self.system.natom), 2), ] def to_atomic_grid(self, index, data): raise NotImplementedError def compute_pseudo_population(self, index): grid = self.get_grid(index) dens = self.get_moldens(index) at_weights = self.cache.load('at_weights', index) wcor = self.get_wcor(index) return grid.integrate(at_weights, dens, wcor) @just_once def do_moldens(self): raise NotImplementedError @just_once def do_spindens(self): raise NotImplementedError @just_once def do_partitioning(self): self.update_at_weights() do_partitioning.names = [] def update_at_weights(self): '''Updates the at_weights arrays in the case (and all related arrays)''' raise NotImplementedError @just_once def do_populations(self): populations, new = self.cache.load('populations', alloc=self.system.natom, tags='o') if new: self.do_partitioning() self.do_moldens() pseudo_populations = self.cache.load('pseudo_populations', alloc=self.system.natom, tags='o')[0] if log.do_medium: log('Computing atomic populations.') for i in xrange(self.system.natom): pseudo_populations[i] = self.compute_pseudo_population(i) populations[:] = pseudo_populations populations += self.system.numbers - self.system.pseudo_numbers @just_once def do_charges(self): charges, new = self._cache.load('charges', alloc=self.system.natom, tags='o') if new: self.do_populations() populations = self._cache.load('populations') if log.do_medium: log('Computing atomic charges.') charges[:] = self.system.numbers - populations @just_once def do_spin_charges(self): spin_charges, new = self._cache.load('spin_charges', alloc=self.system.natom, tags='o') if new: if isinstance(self.system.wfn, RestrictedWFN): spin_charges[:] = 0.0 else: try: self.do_spindens() except NotImplementedError: self.cache.clear_item('spin_charges') return self.do_partitioning() if log.do_medium: log('Computing atomic spin charges.') for index in xrange(self.system.natom): grid = self.get_grid(index) spindens = self.get_spindens(index) at_weights = self.cache.load('at_weights', index) wcor = self.get_wcor(index) spin_charges[index] = grid.integrate( at_weights, spindens, wcor) @just_once def do_moments(self): if log.do_medium: log('Computing cartesian and pure AIM multipoles and radial AIM moments.' ) ncart = get_ncart_cumul(self.lmax) cartesian_multipoles, new1 = self._cache.load( 'cartesian_multipoles', alloc=(self._system.natom, ncart), tags='o') npure = get_npure_cumul(self.lmax) pure_multipoles, new1 = self._cache.load('pure_multipoles', alloc=(self._system.natom, npure), tags='o') nrad = self.lmax + 1 radial_moments, new2 = self._cache.load('radial_moments', alloc=(self._system.natom, nrad), tags='o') if new1 or new2: self.do_partitioning() for i in xrange(self._system.natom): # 1) Define a 'window' of the integration grid for this atom center = self._system.coordinates[i] grid = self.get_grid(i) # 2) Compute the AIM aim = self.get_moldens(i) * self.cache.load('at_weights', i) # 3) Compute weight corrections (TODO: needs to be assessed!) wcor = self.get_wcor(i) # 4) Compute Cartesian multipole moments # The minus sign is present to account for the negative electron # charge. cartesian_multipoles[i] = -grid.integrate( aim, wcor, center=center, lmax=self.lmax, mtype=1) cartesian_multipoles[i, 0] += self.system.pseudo_numbers[i] # 5) Compute Pure multipole moments # The minus sign is present to account for the negative electron # charge. pure_multipoles[i] = -grid.integrate( aim, wcor, center=center, lmax=self.lmax, mtype=2) pure_multipoles[i, 0] += self.system.pseudo_numbers[i] # 6) Compute Radial moments # For the radial moments, it is not common to put a minus sign # for the negative electron charge. radial_moments[i] = grid.integrate(aim, wcor, center=center, lmax=self.lmax, mtype=3) def do_all(self): '''Computes all properties and return a list of their names.''' slow_methods = [ 'do_overlap_operators', 'do_bond_order', 'do_noninteracting_response' ] for attr_name in dir(self): attr = getattr(self, attr_name) if callable(attr) and attr_name.startswith( 'do_') and attr_name != 'do_all': if self._slow or (not attr_name in slow_methods): attr() return list(self.cache.iterkeys(tags='o'))
class System(object): def __init__(self, coordinates, numbers, obasis=None, grid=None, wfn=None, lf=None, cache=None, extra=None, cell=None, pseudo_numbers=None, chk=None): """ **Arguments:** coordinates A (N, 3) float numpy array with Cartesian coordinates of the atoms. numbers A (N,) int numpy vector with the atomic numbers. **Optional arguments:** obasis A string or an instance of either the basis set or basis set description classes, e.g. 'STO-3G', GOBasisDesc('STO-3G'), ... for the orbitals. grid A grid object used for molecular integration. wfn A wavefunction object. lf A LinalgFactory instance. When not given, a DenseLinalgFactory is used by default. cache A cache object with computed results that depend on other attributes of the system class. Cached items should be tagged according to the attributes they depend on: - ``o``: obasis - ``c``: coordinates - ``g``: grid When given as a dictionary, each value must consist of two items: the object to be cached and the tags. extra A dictionary with additional information about the system. The keys must be strings. cell A Cell object that describes the (generally triclinic) periodic boundary conditions. So far, this is nearly nowhere supported in Horton, so don't get too excited. pseudo_numbers The core charges of the pseudo potential, if applicable chk A filename for the checkpoint file or an open h5.File object. If the file does not exist yet, it will be created. If the file already exists, it must be an HDF5 file that is structured such that it adheres to the format that Horton creates itself. If chk is an open h5.File object, it will not be closed when the System instance is deleted. """ # A) Assign all attributes self._coordinates = np.array(coordinates, dtype=float, copy=False) self._numbers = np.array(numbers, dtype=int, copy=False) # some checks if len(self._coordinates.shape ) != 2 or self._coordinates.shape[1] != 3: raise TypeError( 'coordinates argument must be a 2D array with three columns') if len(self._numbers.shape) != 1: raise TypeError('numbers must a vector of integers.') if self._numbers.shape[0] != self._coordinates.shape[0]: raise TypeError( 'numbers and coordinates must have compatible array shapes.') # self._grid = grid # self._wfn = wfn # if cache is None: self._cache = Cache() elif isinstance(cache, Cache): self._cache = cache elif isinstance(cache, dict): self._cache = Cache() for key, (value, tags) in cache.iteritems(): self._cache.dump(key, value, tags=tags) else: raise TypeError('Could not interpret the cache argument.') # if lf is None: self._lf = DenseLinalgFactory() else: self._lf = lf # if extra is None: self._extra = {} else: self._extra = extra # self._obasis = None self._obasis_desc = None if obasis is not None: self.update_obasis(obasis) self._cell = cell self._pseudo_numbers = pseudo_numbers # The checkpoint file self._chk = None self._close_chk = False self.assign_chk(chk) self._log_init() def __del__(self): # Close the HD5 checkpoint file. This must be done carefully to avoid # spurious error messages when an unrelated exception occurs. if hasattr(self, '_chk') and self.chk is not None and self._close_chk: self.chk.close() def _get_natom(self): '''The number of atoms''' return len(self.numbers) natom = property(_get_natom) def _get_coordinates(self): '''The positions of the nuclei''' return self._coordinates.view() coordinates = property(_get_coordinates) def _get_numbers(self): '''An array with the atomic numbers''' return self._numbers.view() numbers = property(_get_numbers) def _get_obasis(self): '''The orbital basis''' return self._obasis obasis = property(_get_obasis) def _get_obasis_desc(self): '''The orbital basis description''' return self._obasis_desc obasis_desc = property(_get_obasis_desc) def _get_grid(self): '''The integration grid''' return self._grid grid = property(_get_grid) def _get_wfn(self): '''The wavefunction''' return self._wfn wfn = property(_get_wfn) def _get_lf(self): '''The LinalgFactory for this system''' return self._lf lf = property(_get_lf) def _get_cache(self): '''A cache of intermediate results that depend on the coordinates''' return self._cache cache = property(_get_cache) def _get_extra(self): '''A dictionary with extra properties of the system.''' return self._extra extra = property(_get_extra) def _get_cell(self): '''A Cell object describing the periodic boundary conditions.''' return self._cell cell = property(_get_cell) def _get_pseudo_numbers(self): result = self._pseudo_numbers if result is None: result = self._numbers return result pseudo_numbers = property(_get_pseudo_numbers) def _get_chk(self): '''A ``h5.File`` instance used as checkpoint file or ``None``''' return self._chk chk = property(_get_chk) @classmethod def from_file(cls, *args, **kwargs): """Create a System object from a file. A list of filenames may be provided, which will be loaded in that order. Each file complements or overrides the information loaded from a previous file in the list. Furthermore, keyword arguments may be used to specify additional constructor arguments. The ``lf`` optional argument is picked up from the kwargs list to contstruct (when needed) arrays to store the results loaded from file. When ``lf`` is not given, a DenseLinalgFactory is created by default. The filenames may also contain checkpoint files and open h5.File objects of checkpoint files. The last such checkpoint file will automatically be used as a checkpoint file for this class. If you want to override this behavior, provide the ``chk`` keyword argument (may be None). """ constructor_args = {} lf = kwargs.get('lf') if lf is None: lf = DenseLinalgFactory() for fn in args: fn_args = load_system_args(fn, lf) constructor_args.update(fn_args) constructor_args.update(kwargs) # If the basis comes from an external code and some operators are # loaded, rows and columns may need to be reordered. Similar for the # orbital coefficients and the density matrices. permutation = constructor_args.get('permutation') if permutation is not None: cache = constructor_args.get('cache') if cache is not None: for value, tags in cache.itervalues(): if isinstance(value, LinalgObject): value.apply_basis_permutation(permutation) wfn = constructor_args.get('wfn') if wfn is not None: wfn.apply_basis_permutation(permutation) del constructor_args['permutation'] # After the permutation, correct for different sign conventions of the # orbitals signs = constructor_args.get('signs') if signs is not None: cache = constructor_args.get('cache') if cache is not None: for value, tags in cache.itervalues(): if isinstance(value, LinalgObject): value.apply_basis_signs(signs) wfn = constructor_args.get('wfn') if wfn is not None: wfn.apply_basis_signs(signs) del constructor_args['signs'] return cls(**constructor_args) def _log_init(self): '''Write some basic information about the system to the screen logger.''' if log.do_medium: log('Initialized: %s' % self) log.deflist([('Number of atoms', self.natom)] + [('Number of %s' % periodic[n].symbol, (self.numbers == n).sum()) for n in sorted(np.unique(self.numbers))] + [ ('Linalg Factory', self._lf), ('Orbital basis', self._obasis), ('Wavefunction', self._wfn), ('Checkpoint file', self._chk), ]) if len(self._cache) > 0: log('The following cached items are present: %s' % (', '.join(self._cache.iterkeys()))) if len(self._extra) > 0: log('The following extra attributes are present: %s' % (', '.join(self._extra.iterkeys()))) log.blank() def assign_chk(self, chk): if self.chk is not None and self._close_chk: self.chk.close() if isinstance(chk, basestring): # Suppose a filename is given. Create or open an HDF5 file. self._chk = h5.File(chk) self._close_chk = True elif isinstance(chk, h5.Group) or chk is None: self._chk = chk self._close_chk = False else: raise TypeError( 'The chk argument, when not None, must be a filename or an open h5.Group object.' ) self.update_chk() def update_chk(self, field_name=None): """Write (a part of) the system to the checkpoint file. **Optional Argument:** field A field string that specifies which part must be written to the checkpoint file. When not given, all possible fields are written. The latter is only useful in specific cases, e.g. upon initialization of the system. The available field names are specified in the attribute register dictionary in the module ``horton.checkpoint``. """ if self._chk is not None: from horton.checkpoint import attribute_register if field_name is None: for field_name, field in attribute_register.iteritems(): field.write(self._chk, self) else: field = attribute_register[field_name] field.write(self._chk, self) def to_file(self, filename): '''Write the system to a file **Arguments:** filename The name of the file to write to. The extension of the file is used to determine the file format. ''' dump_system(filename, self) def _get_charge(self): return self.pseudo_numbers.sum() - self.wfn.nel charge = property(_get_charge) def update_coordinates(self, coordinates=None): '''Update all attributes that depend on coodinates and clear related parts of cache **Optional arguments:** coordinates The new atomic coordinates When one wants to set new coordintes, one may also edit the system.coordinates array in-place and then call this method without any arguments. ''' if coordinates is not None: self._coordinates[:] = coordinates if self._obasis is not None: self._obasis.centers[:] = self._coordinates if self._grid is not None: self._grid.update_centers(self) self.cache.clear(tags='cog') self._extra = {} def update_grid(self, grid=None): '''Define a new integration grid and clear related parts of the cache **Optional arguments:** grid The new integration grid. When not given, it is assumed that the grid was modified in-place and that only derived results in the cache need to be pruned. ''' if grid is not None: self._grid = grid self.cache.clear(tags='g') def update_obasis(self, obasis=None): '''Regenerate the orbital basis and clear all attributes that depend on it. **Optional arguments:** obasis The new basis. This may be a string or an instance of GOBasis or GOBasisDesc. When not given, the orbital basis description stored in the system object (_obasis_desc attribute) will be used. ''' # Get the orbital basis and if possible the orbital basis description. from horton.gbasis import GOBasisDesc, GOBasis if isinstance(obasis, str): obasis_desc = GOBasisDesc(obasis) elif isinstance(obasis, GOBasisDesc): obasis_desc = obasis elif isinstance(obasis, GOBasis): obasis_desc = None elif obasis is None: if self.obasis_desc is None: raise TypeError( 'No orbital basis description (obasis_desc) available to update obasis.' ) obasis_desc = self.obasis_desc else: raise TypeError('Could not interpret the obasis argument.') if obasis_desc is not None: obasis = obasis_desc.apply_to(self) # Discard or reset results that depend on orbital basis if self.obasis is not None: self._cache.clear(tags='o') # Ideally, the user of the system object does some sort of # projection of the wavefunction on the new basis. This should be # done outside the system class as their are too many different ways # to handle this. Here, we set the wfn to None, just to force the # user to do something. self._wfn = None self._extra = {} # Assign new obasis self._lf.set_default_nbasis(obasis.nbasis) self._obasis = obasis self._obasis_desc = obasis_desc # Some consistency checks. These are needed when the initial value of # obasis was None. This may occur when the system object is initialized. if self._wfn is not None and self._obasis.nbasis != self._wfn.nbasis: raise TypeError( 'The nbasis attribute of obasis and wfn are inconsistent.') for key, value in self._cache.iteritems(): if isinstance( value, LinalgObject) and value.nbasis != self._obasis.nbasis: raise TypeError( 'The nbasis attribute of the cached object \'%s\' and obasis are inconsistent.' % key) @timer.with_section('OLP integrals') def get_overlap(self): overlap, new = self.cache.load('olp', alloc=self.lf.create_one_body, tags='o') if new: self.obasis.compute_overlap(overlap) self.update_chk('cache.olp') return overlap @timer.with_section('KIN integrals') def get_kinetic(self): kinetic, new = self.cache.load('kin', alloc=self.lf.create_one_body, tags='o') if new: self.obasis.compute_kinetic(kinetic) self.update_chk('cache.kin') return kinetic @timer.with_section('NAI integrals') def get_nuclear_attraction(self): nuclear_attraction, new = self.cache.load( 'na', alloc=self.lf.create_one_body, tags='o') if new: # TODO: ghost atoms and extra charges self.obasis.compute_nuclear_attraction(self.numbers.astype(float), self.coordinates, nuclear_attraction) self.update_chk('cache.na') return nuclear_attraction @timer.with_section('ER integrals') def get_electron_repulsion(self): electron_repulsion, new = self.cache.load( 'er', alloc=self.lf.create_two_body, tags='o') if new: self.obasis.compute_electron_repulsion(electron_repulsion) # ER integrals are not checkpointed by default because they are too heavy. # Can be done manually by user if needed: ``system.update_chk('cache.er')`` #self.update_chk('cache.er') return electron_repulsion @timer.with_section('Orbitals grid') def compute_grid_orbitals(self, points, iorbs=None, orbs=None, select='alpha'): '''Compute the electron density on a grid using self.wfn as input **Arguments:** points A Numpy array with grid points, shape (npoint,3) **Optional arguments:** iorbs The indexes of the orbitals to be computed. If not given, the orbitals with a non-zero occupation number are computed orbs An output array, shape (npoint, len(iorbs)). The results are added to this array. select 'alpha', 'beta' **Returns:** orbs The array with the result. This is the same as the output argument, in case it was provided. ''' exp = self.wfn.get_exp(select) if iorbs is None: iorbs = (exp.occupations > 0).nonzero()[0] shape = (len(points), len(iorbs)) if orbs is None: orbs = np.zeros(shape, float) elif orbs.shape != shape: raise TypeError('The shape of the output array is wrong') self.obasis.compute_grid_orbitals_exp(exp, points, iorbs, orbs) return orbs @timer.with_section('Density grid') def compute_grid_density(self, points, rhos=None, select='full', epsilon=0): '''Compute the electron density on a grid using self.wfn as input **Arguments:** points A Numpy array with grid points, shape (npoint,3) **Optional arguments:** rhos An output array, shape (npoint,). The results are added to this array. select 'alpha', 'beta', 'full' or 'spin'. ('full' is the default.) epsilon Allow errors on the density of this magnitude for the sake of efficiency. **Returns:** rhos The array with the result. This is the same as the output argument, in case it was provided. ''' if rhos is None: rhos = np.zeros(len(points), float) elif rhos.shape != (points.shape[0], ): raise TypeError('The shape of the output array is wrong') dm = self.wfn.get_dm(select) self.obasis.compute_grid_density_dm(dm, points, rhos, epsilon) return rhos @timer.with_section('Gradient grid') def compute_grid_gradient(self, points, gradrhos=None, select='full'): '''Compute the electron density on a grid using self.wfn as input **Arguments:** points A Numpy array with grid points, shape (npoint,3) **Optional arguments:** gradrhos An output array, shape (npoint, 3). The results are added to this array. select 'alpha', 'beta', 'full' or 'spin'. ('full' is the default.) **Returns:** gradrhos The array with the result. This is the same as the output argument, in case it was provided. ''' if gradrhos is None: gradrhos = np.zeros((len(points), 3), float) elif gradrhos.shape != (points.shape[0], 3): raise TypeError('The shape of the output array is wrong') dm = self.wfn.get_dm(select) self.obasis.compute_grid_gradient_dm(dm, points, gradrhos) return gradrhos @timer.with_section('Hartree grid') def compute_grid_hartree(self, points, hartree=None, select='full'): '''Compute the hartree potential on a grid using self.wfn as input **Arguments:** points A Numpy array with grid points, shape (npoint,3) **Optional arguments:** hartree An output array, shape (npoint,). The results are added to this array. select 'alpha', 'beta', 'full' or 'spin'. ('full' is the default.) **Returns:** hartree The array with the result. This is the same as the output argument, in case it was provided. ''' if hartree is None: hartree = np.zeros(len(points), float) elif hartree.shape != (points.shape[0], ): raise TypeError('The shape of the output array is wrong') dm = self.wfn.get_dm(select) self.obasis.compute_grid_hartree_dm(dm, points, hartree) return hartree @timer.with_section('ESP grid') def compute_grid_esp(self, points, esp=None, select='full'): '''Compute the esp on a grid using self.wfn as input **Arguments:** points A Numpy array with grid points, shape (npoint,3) **Optional arguments:** esp An output array, shape (npoint,). The results are added to this array. select 'alpha', 'beta', 'full' or 'spin'. ('full' is the default.) **Returns:** esp The array with the result. This is the same as the output argument, in case it was provided. ''' if esp is None: esp = np.zeros(len(points), float) elif esp.shape != (points.shape[0], ): raise TypeError('The shape of the output array is wrong') dm = self.wfn.get_dm(select) self.obasis.compute_grid_hartree_dm(dm, points, esp) esp *= -1 compute_grid_nucpot(self.numbers, self.coordinates, points, esp) return esp @timer.with_section('Fock grid dens') def compute_grid_density_fock(self, points, weights, pots, fock): '''See documentation self.obasis.compute_grid_density_fock''' self.obasis.compute_grid_density_fock(points, weights, pots, fock) @timer.with_section('Fock grid grad') def compute_grid_gradient_fock(self, points, weights, pots, fock): '''See documentation self.obasis.compute_grid_gradient_fock''' self.obasis.compute_grid_gradient_fock(points, weights, pots, fock) def compute_nucnuc(self): '''Compute interaction energy of the nuclei''' # TODO: move this to low-level code one day. result = 0.0 for i in xrange(self.natom): for j in xrange(i): distance = np.linalg.norm(self.coordinates[i] - self.coordinates[j]) result += self.numbers[i] * self.numbers[j] / distance self._extra['energy_nn'] = result return result
class Part(JustOnceClass): name = None linear = False # whether the populations are linear in the density matrix. def __init__(self, system, grid, local, slow, lmax, moldens=None): ''' **Arguments:** system The system to be partitioned. grid The integration grid local Whether or not to use local (non-periodic) grids. slow When ``True``, also the AIM properties are computed that use the AIM overlap operators. lmax The maximum angular momentum in multipole expansions. **Optional arguments:** moldens The all-electron density grid data. ''' JustOnceClass.__init__(self) self._system = system self._grid = grid self._local = local self._slow = slow self._lmax = lmax # Caching stuff, to avoid recomputation of earlier results self._cache = Cache() # Caching of work arrays to avoid reallocation if moldens is not None: self._cache.dump('moldens', moldens) # Initialize the subgrids if local: self._init_subgrids() # Some screen logging self._init_log_base() self._init_log_scheme() self._init_log_memory() if log.do_medium: log.blank() def __getitem__(self, key): return self.cache.load(key) def _get_system(self): return self._system system = property(_get_system) def _get_grid(self): return self.get_grid() grid = property(_get_grid) def _get_local(self): return self._local local = property(_get_local) def _get_slow(self): return self._slow slow = property(_get_slow) def _get_lmax(self): return self._lmax lmax = property(_get_lmax) def _get_cache(self): return self._cache cache = property(_get_cache) def __clear__(self): self.clear() def clear(self): '''Discard all cached results, e.g. because wfn changed''' JustOnceClass.clear(self) self.cache.clear() def update_grid(self, grid): '''Specify a new grid **Arguments:** grid The new grid When the new and old grid are the same, no action is taken. When a really new grid is provided, the subgrids are updated and the cache is cleared. ''' if not (grid is self._grid): self._grid = grid if self.local: self._init_subgrids() self.clear() def get_grid(self, index=None): '''Return an integration grid **Optional arguments:** index The index of the atom. If not given, a grid for the entire system is returned. If self.local is False, a full system grid is always returned. ''' if index is None or not self.local: return self._grid else: return self._subgrids[index] def get_moldens(self, index=None, output=None): self.do_moldens() moldens = self.cache.load('moldens') result = self.to_atomic_grid(index, moldens) if output is not None: output[:] = result return result def get_spindens(self, index=None, output=None): self.do_spindens() spindens = self.cache.load('spindens') result = self.to_atomic_grid(index, spindens) if output is not None: output[:] = result return result def get_wcor(self, index): '''Return the weight corrections on a grid See get_grid for the meaning of the optional arguments ''' raise NotImplementedError def _init_subgrids(self): raise NotImplementedError def _init_log_base(self): raise NotImplementedError def _init_log_scheme(self): raise NotImplementedError def _init_log_memory(self): if log.do_medium: # precompute arrays sizes for certain grids nbyte_global = self.grid.size*8 nbyte_locals = np.array([self.get_grid(i).size*8 for i in xrange(self.system.natom)]) # compute and report usage estimates = self.get_memory_estimates() nbyte_total = 0 log('Coarse estimate of memory usage for the partitioning:') log(' Label Memory[GB]') log.hline() for label, nlocals, nglobal in estimates: nbyte = np.dot(nlocals, nbyte_locals) + nglobal*nbyte_global log('%30s %10.3f' % (label, nbyte/1024.0**3)) nbyte_total += nbyte log('%30s %10.3f' % ('Total', nbyte_total/1024.0**3)) log.hline() log.blank() def get_memory_estimates(self): return [ ('Atomic weights', np.ones(self.system.natom), 0), ('Promolecule', np.zeros(self.system.natom), 1), ('Working arrays', np.zeros(self.system.natom), 2), ] def to_atomic_grid(self, index, data): raise NotImplementedError def compute_pseudo_population(self, index): grid = self.get_grid(index) dens = self.get_moldens(index) at_weights = self.cache.load('at_weights', index) wcor = self.get_wcor(index) return grid.integrate(at_weights, dens, wcor) @just_once def do_moldens(self): raise NotImplementedError @just_once def do_spindens(self): raise NotImplementedError @just_once def do_partitioning(self): self.update_at_weights() do_partitioning.names = [] def update_at_weights(self): '''Updates the at_weights arrays in the case (and all related arrays)''' raise NotImplementedError @just_once def do_populations(self): populations, new = self.cache.load('populations', alloc=self.system.natom, tags='o') if new: self.do_partitioning() self.do_moldens() pseudo_populations = self.cache.load('pseudo_populations', alloc=self.system.natom, tags='o')[0] if log.do_medium: log('Computing atomic populations.') for i in xrange(self.system.natom): pseudo_populations[i] = self.compute_pseudo_population(i) populations[:] = pseudo_populations populations += self.system.numbers - self.system.pseudo_numbers @just_once def do_charges(self): charges, new = self._cache.load('charges', alloc=self.system.natom, tags='o') if new: self.do_populations() populations = self._cache.load('populations') if log.do_medium: log('Computing atomic charges.') charges[:] = self.system.numbers - populations @just_once def do_spin_charges(self): spin_charges, new = self._cache.load('spin_charges', alloc=self.system.natom, tags='o') if new: if isinstance(self.system.wfn, RestrictedWFN): spin_charges[:] = 0.0 else: try: self.do_spindens() except NotImplementedError: self.cache.clear_item('spin_charges') return self.do_partitioning() if log.do_medium: log('Computing atomic spin charges.') for index in xrange(self.system.natom): grid = self.get_grid(index) spindens = self.get_spindens(index) at_weights = self.cache.load('at_weights', index) wcor = self.get_wcor(index) spin_charges[index] = grid.integrate(at_weights, spindens, wcor) @just_once def do_moments(self): if log.do_medium: log('Computing cartesian and pure AIM multipoles and radial AIM moments.') ncart = get_ncart_cumul(self.lmax) cartesian_multipoles, new1 = self._cache.load('cartesian_multipoles', alloc=(self._system.natom, ncart), tags='o') npure = get_npure_cumul(self.lmax) pure_multipoles, new1 = self._cache.load('pure_multipoles', alloc=(self._system.natom, npure), tags='o') nrad = self.lmax+1 radial_moments, new2 = self._cache.load('radial_moments', alloc=(self._system.natom, nrad), tags='o') if new1 or new2: self.do_partitioning() for i in xrange(self._system.natom): # 1) Define a 'window' of the integration grid for this atom center = self._system.coordinates[i] grid = self.get_grid(i) # 2) Compute the AIM aim = self.get_moldens(i)*self.cache.load('at_weights', i) # 3) Compute weight corrections (TODO: needs to be assessed!) wcor = self.get_wcor(i) # 4) Compute Cartesian multipole moments # The minus sign is present to account for the negative electron # charge. cartesian_multipoles[i] = -grid.integrate(aim, wcor, center=center, lmax=self.lmax, mtype=1) cartesian_multipoles[i, 0] += self.system.pseudo_numbers[i] # 5) Compute Pure multipole moments # The minus sign is present to account for the negative electron # charge. pure_multipoles[i] = -grid.integrate(aim, wcor, center=center, lmax=self.lmax, mtype=2) pure_multipoles[i, 0] += self.system.pseudo_numbers[i] # 6) Compute Radial moments # For the radial moments, it is not common to put a minus sign # for the negative electron charge. radial_moments[i] = grid.integrate(aim, wcor, center=center, lmax=self.lmax, mtype=3) def do_all(self): '''Computes all properties and return a list of their names.''' slow_methods = ['do_overlap_operators', 'do_bond_order', 'do_noninteracting_response'] for attr_name in dir(self): attr = getattr(self, attr_name) if callable(attr) and attr_name.startswith('do_') and attr_name != 'do_all': if self._slow or (not attr_name in slow_methods): attr() return list(self.cache.iterkeys(tags='o'))