def extract(s, sel_i, sel_j, tag, isotopes, isotope_list, self_coupling, force_recalc): jDiagProp = JCDiagonal(sel_i=sel_i, sel_j=sel_j, isotopes=isotopes, tag=tag, isotope_list=isotope_list, self_coupling=self_coupling, force_recalc=force_recalc) jc_dict = jDiagProp(s) jc_keys = jc_dict.keys() jc_evals = [jc_dict[ij]['evals'] for ij in jc_keys] jc_evals = _haeb_sort(jc_evals) jc_skew = _asymmetry(jc_evals) return dict(zip(jc_dict.keys(), jc_skew))
def extract(s, save_array): ms_diag = [ np.linalg.eigh((ms + ms.T) / 2.0) for ms in s.get_array('ms') ] ms_evals, ms_evecs = [np.array(a) for a in zip(*ms_diag)] if save_array: s.set_array(MSDiagonal.default_name + '_evals', ms_evals) # Store also the Haeberlen sorted version s.set_array(MSDiagonal.default_name + '_evals_hsort', _haeb_sort(ms_evals)) s.set_array(MSDiagonal.default_name + '_evecs', ms_evecs) return np.array([dict(zip(('evals', 'evecs'), ms)) for ms in ms_diag])
def extract(s, save_array): efg_diag = [ np.linalg.eigh((efg + efg.T) / 2.0) for efg in s.get_array('efg') ] efg_evals, efg_evecs = [np.array(a) for a in zip(*efg_diag)] if save_array: s.set_array(EFGDiagonal.default_name + '_evals', efg_evals) # Store also the Haeberlen sorted version s.set_array(EFGDiagonal.default_name + '_evals_hsort', _haeb_sort(efg_evals)) s.set_array(EFGDiagonal.default_name + '_evecs', efg_evecs) return np.array( [dict(zip(('evals', 'evecs'), efg)) for efg in efg_diag])
def extract(s, tag, save_info): isc_dict = {(i, j): np.array(t) for j, r in enumerate(s.get_array(tag)) for i, t in enumerate(r) if t is not None} isc_diag = {ij: dict(zip(['evals', 'evecs'], np.linalg.eigh((t+t.T)/2.0))) for ij, t in isc_dict.items()} isc_pairs = sorted(isc_diag.keys()) isc_evals = np.array([isc_diag[ij]['evals'] for ij in isc_pairs]) isc_evecs = np.array([isc_diag[ij]['evecs'] for ij in isc_pairs]) if save_info: s.info[ISCDiagonal.default_name + '_' + tag + '_pairs'] = isc_pairs s.info[ISCDiagonal.default_name + '_' + tag + '_evals'] = isc_evals s.info[ISCDiagonal.default_name + '_' + tag + '_evals_hsort'] = _haeb_sort(isc_evals) s.info[ISCDiagonal.default_name + '_' + tag + '_evecs'] = isc_evecs return isc_diag
def nics_buildup(args=None): parser = ap.ArgumentParser() parser.add_argument('seedname', type=str, default=None) parser.add_argument('-R', type=float, default=10, help="Max radius") parser.add_argument('-minR', type=float, default=0, help="Min radius") parser.add_argument('-n', type=int, default=100, help="Number of points") parser.add_argument('-log', action='store_true', default=False, help="Use a logarithmic grid") parser.add_argument('-cfile', type=str, default='current.dat', help="Name of current file") parser.add_argument('-out', type=str, default=None, help=("Prefix for output file names " "(default is the seedname)")) parser.add_argument('-csv', action='store_true', default=False, help="Output NICS tensors in CSV format") args = parser.parse_args() cfile = CurrentFile(args.cfile) cell = io.read(args.seedname + '.cell').get_cell() nfile = nicsfile(args.seedname + '.nicslist') try: fracpoints = np.dot(nfile.nics_points_frac, cell) except TypeError: fracpoints = None abspoints = nfile.nics_points_abs ncomp = NicsCompute(cfile, cell) allpoints = {'frac': fracpoints, 'abs': abspoints} outname = args.seedname if args.out is None else args.out outnics = open(outname + '_nics.txt', 'w') if (args.csv): outcsv = open(outname + '_nics.csv', 'w') csvwriter = csv.writer(outcsv) for ptype, plist in allpoints.items(): if plist is None: continue for i, p in enumerate(plist): nics = ncomp.get_nics(p) rrange, nbuild = ncomp.get_nics_buildup(p, Rmax=args.R, n=args.n, Rmin=args.minR, is_log=args.log) # Print output if (args.csv): csvwriter.writerow([ i + 1, np.trace(nics.nics) / 3.0, np.trace(nics.nics_plus_chi) / 3.0 ]) outnics.write('Point {0}_{1}:\n'.format(ptype, i + 1)) outnics.write('Coordinates: {0} {1} {2}\n'.format(*p)) outnics.write('NICS isotropy: {0} ppm\n'.format( np.trace(nics.nics) / 3.0)) outnics.write('NICS+chi isotropy: {0} ppm\n'.format( np.trace(nics.nics_plus_chi) / 3.0)) outnics.write('NICS tensor:\n{0}\n'.format(nics.nics)) outnics.write('NICS+chi tensor:\n{0}\n'.format(nics.nics_plus_chi)) outnics.write('\n------\n') # For buildup, let's diagonalize them all_evals = np.array( [np.linalg.eigh((nb + nb.T) / 2.0)[0] for nb in nbuild]) all_evals = _haeb_sort(all_evals) iso = np.average(all_evals, axis=1) aniso = _anisotropy(all_evals) asymm = _asymmetry(all_evals) np.savetxt(outname + '_{0}_{1}_nicsbuild.dat'.format(ptype, i + 1), np.array([rrange, iso, aniso, asymm]).T) outnics.close() if (args.csv): outcsv.close()
def __init__(self, data, order=ORDER_INCREASING): """ Initialise the NMRTensor Create an NMRTensor object from a 3x3 matrix. Arguments: data (np.ndarray or tuple): 3x3 matrix containing the tensor, or pair [evals, evecs] for the symmetric part alone. order (str): Order to use for eigenvalues/eigenvectors. Can be 'i' (ORDER_INCREASING), 'd' (ORDER_DECREASING), 'h' (ORDER_HAEBERLEN) or 'n' (ORDER_NQR). Default is 'i'. """ self._order = order if len(data) == 3: self._data = np.array(data) if self._data.shape != (3, 3): raise ValueError('Invalid matrix data passed to NMRTensor') self._symm = (self._data+self._data.T)/2.0 evals, evecs = np.linalg.eigh(self._symm) elif len(data) == 2: evals, evecs = data evecs = np.array(evecs) sort_i = np.argsort(evals) if len(evals) != 3 or evecs.shape != (3, 3): raise ValueError('Invalid eigenvalues/vectors passed to ' 'NMRTensor') evals = evals[sort_i] evecs = evecs[:, sort_i] self._symm = np.linalg.multi_dot([evecs, np.diag(evals), evecs.T]) self._data = self._symm self._incr_evals = evals self._haeb_evals = _haeb_sort([evals])[0] self._anisotropy = None self._redaniso = None self._asymmetry = None self._span = None self._skew = None self._trace = None # Spherical tensor components self._sph = None # Sort eigenvalues and eigenvectors as specified if order != self.ORDER_INCREASING: self._evals, sort_i = _evals_sort([evals], order, True) self._evals = self._evals[0] self._evecs = evecs[:, sort_i[0]] else: # No point in fixing what ain't broken self._evals = evals self._evecs = evecs # Last eigenvector must be the cross product of the first two # (apparently this is much faster than np.cross. Beats me why) self._evecs[0, 2] = (self._evecs[1, 0]*self._evecs[2, 1] - self._evecs[2, 0]*self._evecs[1, 1]) self._evecs[1, 2] = (self._evecs[2, 0]*self._evecs[0, 1] - self._evecs[0, 0]*self._evecs[2, 1]) self._evecs[2, 2] = (self._evecs[0, 0]*self._evecs[1, 1] - self._evecs[1, 0]*self._evecs[0, 1]) self._quat = None