def string(self): 'Convert the statistics of the array into a human readable string' if self.num == 0: return '' else: s = u'%9.3f \u00B1 %9.3f ' % ( self.mean/parse_unit(self.unit), self.std/parse_unit(self.unit)) + \ self.unit + ' '*(15-len(self.unit) ) return s.encode('utf-8')
def read_config_file(self, fn): with open(fn, 'r') as f: for iline, line in enumerate(f.readlines()): line = line.split('#')[0].lstrip().rstrip().rstrip('\n') if len(line)==0: continue elif not ':' in line: raise IOError('Line %i in %s does not contain a colon' %(iline, fn)) else: words = line.split(':') key = words[0].lstrip().rstrip() value = ':'.join(words[1:]).lstrip().rstrip() if value.lower()=='none': value = None elif value.lower()=='true': value = True elif value.lower()=='false': value = False else: dtypes = [int, float] found_dtype = False for dtype in dtypes: try: value = dtype(value) found_dtype = True break except ValueError: try: value = parse_unit(value) found_dtype = True break except ValueError: pass self.set(key, value)
def process_units(self, pardef): '''Load parameter conversion information **Arguments:** pardef An instance of the ParameterDefinition class. Returns a dictionary with (name, converion) pairs. ''' result = {} expected_names = [name for name, dtype in self.par_info if dtype is float] for counter, line in pardef: words = line.split() if len(words) != 2: pardef.complain(counter, 'must have two arguments in UNIT suffix.') name = words[0].upper() if name not in expected_names: pardef.complain(counter, 'specifies a unit for an unknown parameter. (Must be one of %s, but got %s.)' % (expected_names, name)) try: result[name] = parse_unit(words[1]) except (NameError, ValueError): pardef.complain(counter, 'has a UNIT suffix with an unknown unit.') if len(result) != len(expected_names): raise IOError('Not all units are specified for generator %s in file %s. Got %s, should have %s.' % ( self.prefix, pardef.complain.filename, result.keys(), expected_names )) return result
def read_config_file(self, fn): with open(fn, 'r') as f: for iline, line in enumerate(f.readlines()): line = line.split('#')[0].lstrip().rstrip().rstrip('\n') if len(line)==0: continue elif not ':' in line: raise IOError('Line %i in %s does not contain a colon' %(iline, fn)) else: key, value = line.split(':') key = key.lstrip().rstrip() value = value.lstrip().rstrip() value = value.lstrip().rstrip() if value.lower()=='none': value = None elif value.lower()=='true': value = True elif value.lower()=='false': value = False else: dtypes = [int, float] found_dtype = False for dtype in dtypes: try: value = dtype(value) found_dtype = True break except ValueError: try: value = parse_unit(value) found_dtype = True break except ValueError: pass self.set(key, value)
def iter_unit_cells(unit_cell_str, sub=None): sub = fix_slice(sub) if len(unit_cell_str) == 0: uc = UnitCell( numpy.array([[1,0,0],[0,1,0],[0,0,1]], float), numpy.array([False,False,False]), ) while True: yield uc if "," in unit_cell_str: parameters = list(parse_unit(word) for word in unit_cell_str.split(",") if len(word) > 0) if len(parameters) == 1: a= parameters[0] uc = UnitCell( numpy.array([[a,0,0],[0,a,0],[0,0,a]], float), numpy.array([True, True, True]), ) elif len(parameters) == 3: a,b,c = parameters uc = UnitCell( numpy.array([[a,0,0],[0,b,0],[0,0,c]], float), numpy.array([True, True, True]), ) elif len(parameters) == 6: a,b,c,alpha,beta,gamma = parameters uc = UnitCell.from_parameters3( numpy.array([a,b,c]), numpy.array([alpha,beta,gamma]) ) elif len(parameters) == 9: uc = UnitCell( numpy.array(parameters, float).reshape((3,3)), numpy.array([True, True, True]), ) else: raise ValueError("If the --cell option contains comma's, one, three, six or nine value(s) are expected.") while True: yield uc else: filenames = ["%s.%s" % (unit_cell_str, suffix) for suffix in ["a.x", "a.y", "a.z", "b.x", "b.y", "b.z", "c.x", "c.y", "c.z"]] dtype = numpy.dtype([("cell", float, (3,3))]) mtr = MultiTracksReader(filenames, dtype, sub=sub) for row in mtr: yield UnitCell( numpy.array(row["cell"], float), numpy.array([True, True, True]), )
def plot(self, ic, trajectory, filename, eunit='kjmol'): ''' Method to plot the energy contributions along a perturbation trajectory associated to a given ic. **Arguments** ic an instance of the class :class:`quickff.ic.IC` defining for which ic the plot will be made. trajectory a (F,N,3) numpy array defining the perturbation trajectory associated to the given ic. It contains F frames of (N,3)-dimensional geometry arrays. filename a string defining the name of the figure **Optional Arguments** eunit a string describing the conversion of the unit of energy. More info regarding possible strings can be found in the `MolMod documentation <http://molmod.github.io/molmod/reference/const.html#module-molmod.units>`_. ''' import matplotlib.pyplot as pp evaluators = [eval_ic(ic), eval_energy('ai'), eval_energy('ei'), eval_energy('vdw')] qs, tot, ei, vdw = self.analyze(trajectory, evaluators) label = {} for (name, obs) in zip(['ai', 'ei', 'vdw', 'cov'], [tot, ei, vdw, tot-ei-vdw]): pars = fitpar(qs, obs, rcond=1e-6) k = 2*pars[0] if k != 0.0: q0 = -0.5*pars[1]/pars[0] label[name] = '(K=%.0f q0=%.3f)' %( k/parse_unit(ic.kunit), q0/parse_unit(ic.qunit) ) else: label[name] = '(K=0.0 q0=None)' if name=='cov': cov = (pars[0]*qs**2+pars[1]*qs+pars[2]) fig, ax = pp.subplots() ax.plot( qs/parse_unit(ic.qunit), tot/parse_unit(eunit), 'k--', linewidth=4, label='AI total %s' %label['ai'] ) ax.plot( qs/parse_unit(ic.qunit), ei/parse_unit(eunit), 'b--', linewidth=2, label='FF elec %s' %label['ei'] ) ax.plot( qs/parse_unit(ic.qunit), vdw/parse_unit(eunit), 'g--', linewidth=2, label='FF vdW %s' %label['vdw'] ) ax.plot( qs/parse_unit(ic.qunit), cov/parse_unit(eunit), 'r-', linewidth=2, label='FF cov %s' %label['cov'] ) ax.set_title(ic.name) ax.set_xlabel('%s [%s]' % (ic.name.split('/')[0], ic.qunit), fontsize=16) ax.set_ylabel('Energy [%s]' %eunit, fontsize=16) ax.grid() ax.legend(loc='best', fontsize=16) fig.set_size_inches([8, 8]) fig.savefig(filename)