def test_write_read_two_old_format(self): f1 = open(tmp_path+'_tmp.pol', 'w') write_ascii_polynomial(f1, self.p1, is_xxpp_format=1) f1.close() f1 = open(tmp_path+'_tmp.pol', 'r') p1 = read_ascii_polynomial(f1, is_xxpp_format=1) f1.close() self.assert_(p1 == self.p1, (p1, self.p1))
def test_write_read_single_new_format(self): f0 = open(tmp_path+'_tmp.pol', 'w') write_ascii_polynomial(f0, self.p0, is_xxpp_format=0) f0.close() f0 = open(tmp_path+'_tmp.pol', 'r') p0 = read_ascii_polynomial(f0, is_xxpp_format=0) f0.close() self.assert_(p0 == self.p0)
def read_ma_poly(name): global ma_path import os import sys logger.info('reading') logger.info(name) ma_path = os.path.join(sys.path[0], ma_path) in_file = open(os.path.join(ma_path, name), 'r') p = read_ascii_polynomial(in_file, is_xxpp_format=True) in_file.close() logger.info('done') return p
def hill_about_l1(self): #print 'reading hill hamiltonian for test...' in_name = '../../test/ma-files/hill_l1_18_equi_to_tham.pol' in_file = open(in_name, 'r') h = read_ascii_polynomial(in_file, is_xxpp_format=1) in_file.close() #print 'done' terms = {Powers((2, 0, 0, 0, 0, 0)): -4.0, #x^2 Powers((0, 0, 2, 0, 0, 0)): +2.0, #y^2 Powers((0, 0, 0, 0, 2, 0)): +2.0, #z^2 Powers((0, 2, 0, 0, 0, 0)): +0.5, #px^2 Powers((0, 0, 0, 2, 0, 0)): +0.5, #py^2 Powers((0, 0, 0, 0, 0, 2)): +0.5, #pz^2 Powers((1, 0, 0, 1, 0, 0)): -1.0, #xpy Powers((0, 1, 1, 0, 0, 0)): +1.0} #ypx assert len(terms) == 8 h_2 = self.alg.polynomial(terms) assert (h_2 == self.alg.isograde(h, 2)) return h
def hill_about_l1(self): in_name = os.path.join(test_dir, 'ma-files/hill_l1_18_equi_to_tham.pol') in_file = open(in_name, 'r') h = read_ascii_polynomial(in_file, is_xxpp_format=1) in_file.close() terms = {Powers((2, 0, 0, 0, 0, 0)): -4.0, #x^2 Powers((0, 0, 2, 0, 0, 0)): +2.0, #y^2 Powers((0, 0, 0, 0, 2, 0)): +2.0, #z^2 Powers((0, 2, 0, 0, 0, 0)): +0.5, #px^2 Powers((0, 0, 0, 2, 0, 0)): +0.5, #py^2 Powers((0, 0, 0, 0, 0, 2)): +0.5, #pz^2 Powers((1, 0, 0, 1, 0, 0)): -1.0, #xpy Powers((0, 1, 1, 0, 0, 0)): +1.0} #ypx assert len(terms) == 8 dof = 3 alg = LieAlgebra(dof) h_2 = alg.polynomial(terms) self.assert_(h_2 == alg.isograde(h, 2)) return h
def __init__(self): name = "eckartasmm_equi_to_tham.pol" logger.info('reading') logger.info(name) # Get the executable path, not the run dir., and add ../../test exedir = sys.path[0] in_file = open(os.path.join(exedir, "../../test", name), 'r') p = read_ascii_polynomial(in_file, is_xxpp_format=False) in_file.close() logger.info('done') dof = 3 self.lie = LieAlgebra(dof) self.h_er = p grade = 10 self.prefix='EckartasMM--semi-classical' logfile_name = 'EckartasMM--semi-classical--grade-%d.log'%grade h_er_trunc = self.lie.isograde(self.h_er, 0, grade+1) self.nf = SemiclassicalNormalForm(self.lie, h_er_trunc) self.nf.set_tolerance(config["tolerance"])
def read_normal_form_data(self, dir_name, order_f=None, is_xxpp_format=False, degree=None): """ Read the NF data and reorder the variables xxpp to xpxp and the order of the NF planes as required for Mathematica data """ def check_poly_degree(poly, name, degree): if degree: polyd = poly.degree() if polyd != degree: print "poly %s is degree %d not degree %d" % (name, polyd, degree) def check_vec_degree(poly_vec, name, degree): for i in xrange(len(poly_vec)): check_poly_degree(poly_vec[i], name+"[%d]"%i, degree) def read_file_vec_polynomials( dir_name, file_name, is_xxpp_format, order=None, degree=degree): file_istr = open(os.path.join(dir_name, file_name), 'r') vec_poly = read_ascii_vec_polynomials( file_istr, is_xxpp_format, order=order) file_istr.close() check_vec_degree(vec_poly, file_name, degree) return vec_poly # see what we have file_istr = open(os.path.join(dir_name, "norm_to_ints.vec"), 'r') line = file_istr.next() n_vars = int(line) assert n_vars >= 0 line = file_istr.next() n_ints = int(line) assert n_ints >= 0 file_istr.close() self._n_vars = n_vars assert n_ints == n_vars / 2 dof = n_ints #dof = p.n_vars() / 2 lie = LieAlgebra(dof) #nf = NormalForm(lie, h_er) # TODO If there's nothing here then do a run to generate data #These are dim dof, is_xxpp_format doesn't apply file_istr = open(os.path.join(dir_name, "ints_to_freq.vec"), 'r') self.ints_to_freq = read_ascii_vec_polynomials(file_istr, False) file_istr.close() print 'Primary frequencies:', [poly((0.0,)*n_ints) for poly in self.ints_to_freq] my_order_f = [poly((0.0,)*n_ints) for poly in self.ints_to_freq] # Don't do reordering for first NF only second with order_f set new_order = None # Find the order in which the supplied frequencies are found if order_f: degree = check_poly_degree(self.ints_to_freq[0], "the second normal form", degree) degree = 18 new_order = [] for i in xrange(n_ints): for j in xrange(n_ints): if (abs(my_order_f[j] - order_f[i]) < 1.0e-4): new_order.append(j) print "new_order", new_order assert len(new_order) == n_ints #RE-Read in new order, These are dim dof- no is_xxpp_format file_istr = open(os.path.join(dir_name, "ints_to_freq.vec"), 'r') self.ints_to_freq = read_ascii_vec_polynomials(file_istr, False, order=new_order) file_istr.close() print 'Primary frequencies:', [poly((0.0,)*n_ints) for poly in self.ints_to_freq] # equi_to_tham - equi coords are not reordered until diagonalised file_istr = open(os.path.join(dir_name, "equi_to_tham.pol"), 'r') self.equi_to_tham = read_ascii_polynomial( file_istr, is_xxpp_format=is_xxpp_format) file_istr.close() check_poly_degree(self.equi_to_tham, "equi_to_tham.pol", degree) self.equi_to_tvec = read_file_vec_polynomials( dir_name, "equi_to_tvec.vec", is_xxpp_format=is_xxpp_format, degree=degree-1) # now reorder to the Mathematica order of the planes file_istr = open(os.path.join(dir_name, "ints_to_tham.pol"), 'r') self.ints_to_tham = read_ascii_polynomial(file_istr, False, order=new_order) file_istr.close() check_poly_degree(self.ints_to_tham, "ints_to_tham.pol", degree/2) # The matrices are different they do the reordering on diag side file_istr = open(os.path.join(dir_name, "diag_to_equi.mat"), 'r') mat = read_ascii_matrix(file_istr, is_xxpp_format) file_istr.close() # reorder input variables if order_f: for i in xrange(len(mat)): mat[i] = reorder_dof(mat[i], new_order) assert n_vars == len(mat) assert n_vars == len(mat[1]) self.diag_to_equi = mat file_istr = open(os.path.join(dir_name, "equi_to_diag.mat"), 'r') mat = read_ascii_matrix(file_istr, is_xxpp_format) file_istr.close() # reorder output variables if order_f: mat = reorder_dof(mat, new_order) assert n_vars == len(mat) assert n_vars == len(mat[1]) self.equi_to_diag = mat # reorder all for norm_to_diag, diag_to_norm self.norm_to_diag = read_file_vec_polynomials( dir_name, "norm_to_diag.vec", is_xxpp_format=is_xxpp_format, order=new_order, degree=degree-1) self.diag_to_norm = read_file_vec_polynomials( dir_name, "diag_to_norm.vec", is_xxpp_format=is_xxpp_format, order=new_order, degree=degree-1) # norm_to_ints has is_xxpp_format on input vars but not o/p in r_a_v_p self.norm_to_ints = read_file_vec_polynomials( dir_name, "norm_to_ints.vec", is_xxpp_format=is_xxpp_format, order=new_order, degree=2)
def test_read_two_old_format(self): p1 = read_ascii_polynomial(iter(self.f1), is_xxpp_format=1) self.assert_(p1 == self.p1)
def test_read_single_new_format(self): p0 = read_ascii_polynomial(iter(self.f0), is_xxpp_format=0) self.assert_(p0 == self.p0)