Esempio n. 1
0
def branch_2_mn(small):
    m = []
    n = []
    while (len(n) == 0 or len(m) == 0):
        r1 = random.random()
        r2 = random.random()
        r3 = random.random()
        if (r1 >= r2 and r2 >= r3 and r3 > 0.0):
            r = random.random()
            if (r > 0.5):
                m = [r1, r2, r3, 0, 0, 0]
            else:
                m = [r1, r1, r1, 0, 0, 0]
            assert adptbx.is_positive_definite(m, small)
        if (r1 >= r2 and r2 >= r3 and r3 > 0.0):
            r = random.random()
            if (r > 0.5):
                n = [r1, r3, r2, 0, 0, 0]
            else:
                n = [r1, r1, r1, 0, 0, 0]
            assert adptbx.is_positive_definite(n, small)
    r = random.random()
    if (r > 0.5):
        m = adptbx.random_rotate_ellipsoid(u_cart=m)
        n = adptbx.random_rotate_ellipsoid(u_cart=n)
    return m, n
Esempio n. 2
0
def run(pdb_file_name,
        n_models,
        log,
        eps=1.e-7,
        output_file_name="ensemble.pdb"):
  pdb_inp = iotbx.pdb.input(file_name = pdb_file_name)
  pdb_hierarchy = pdb_inp.construct_hierarchy()
  xrs = pdb_hierarchy.extract_xray_structure(
    crystal_symmetry=pdb_inp.crystal_symmetry_from_cryst1())
  tls_extract = mmtbx.tls.tools.tls_from_pdb_inp(
    remark_3_records = pdb_inp.extract_remark_iii_records(3),
    pdb_hierarchy    = pdb_hierarchy)
  tlso = tls_extract.tls_params
  if(len(tlso)!=1):
    raise Sorry("Only one TLS group per PDB is currently supported.")
  tlso = tlso[0] # XXX one group only
  deg_to_rad_scale = math.pi/180
  # Units: T[A], L[deg**2], S[A*deg]
  T = matrix.sym(sym_mat3=tlso.t)
  L = matrix.sym(sym_mat3=tlso.l)*(deg_to_rad_scale**2)
  S = matrix.sqr(tlso.s)*deg_to_rad_scale
  # sanity check
  if(not adptbx.is_positive_definite(tlso.t, eps)):
    raise Sorry("T matrix is not positive definite.")
  if(not adptbx.is_positive_definite(tlso.l, eps)):
    raise Sorry("L matrix is not positive definite.")
  r = analysis.run(T=T, L=L, S=S, log=log)
  ensemble_generator(
    decompose_tls_object = r,
    pdb_hierarchy        = pdb_hierarchy,
    xray_structure       = xrs,
    n_models             = n_models,
    log                  = log).write_pdb_file(file_name=output_file_name)
Esempio n. 3
0
def branch_2_mn(small):
  m=[]
  n=[]
  while (len(n)==0 or len(m)==0):
    r1 = random.random()
    r2 = random.random()
    r3 = random.random()
    if(r1 >= r2 and r2 >= r3 and r3>  0.0):
       r = random.random()
       if(r > 0.5):
          m = [r1,r2,r3,0,0,0]
       else:
          m = [r1,r1,r1,0,0,0]
       assert adptbx.is_positive_definite(m,small)
    if(r1 >= r2 and r2 >= r3 and r3 >  0.0):
       r = random.random()
       if(r > 0.5):
          n = [r1,r3,r2,0,0,0]
       else:
          n = [r1,r1,r1,0,0,0]
       assert adptbx.is_positive_definite(n,small)
  r = random.random()
  if(r > 0.5):
     m = adptbx.random_rotate_ellipsoid(u_cart = m)
     n = adptbx.random_rotate_ellipsoid(u_cart = n)
  return m,n
Esempio n. 4
0
def branch_3_mn():
    m = None
    n = None
    small = 1.e-15
    for i in range(10000):
        m2 = []
        n2 = []
        for i in range(4):
            r1 = random.random()
            r2 = random.random()
            r3 = random.random()
            if (r3 > 0.1):
                r1 = r2
            m2.append(r1)
            n2.append(r2)
        p1 = 0.5 * (m2[0] + m2[3] + math.sqrt(4 * m2[1] * m2[2] +
                                              (m2[0] - m2[3])**2))
        p2 = 0.5 * (m2[0] + m2[3] - math.sqrt(4 * m2[1] * m2[2] +
                                              (m2[0] - m2[3])**2))
        q1 = 0.5 * (n2[0] + m2[3] + math.sqrt(4 * n2[1] * n2[2] +
                                              (n2[0] - n2[3])**2))
        q2 = 0.5 * (n2[0] + m2[3] - math.sqrt(4 * n2[1] * n2[2] +
                                              (n2[0] - n2[3])**2))
        if (min(p1, p2) > 0.0 and min(q1, q2) > 0.0):
            r = random.random()
            if (r > 0.5):
                r1 = r3
                r2 = r2
            m = [m2[0], m2[3], r1, m2[1], 0, 0]
            n = [n2[0], n2[3], r2, n2[1], 0, 0]
            if ([
                    adptbx.is_positive_definite(m),
                    adptbx.is_positive_definite(n)
            ].count(True) == 2):
                esm = adptbx.eigensystem(m)
                esn = adptbx.eigensystem(n)
                vn = esn.values()
                vm = esm.values()
                mmin = flex.min(flex.double(vm))
                nmin = flex.min(flex.double(vn))
                if (abs(abs(mmin) - abs(nmin)) < small and mmin > 0.
                        and nmin > 0.):
                    for i, v in enumerate(vn):
                        if (abs(abs(nmin) - v) < small): break
                    for j, v in enumerate(vm):
                        if (abs(abs(mmin) - v) < small): break
                    vecn = flex.double(esn.vectors(i))
                    vecm = flex.double(esm.vectors(j))
                    if (flex.mean(vecm - vecn) < small): break
                else:
                    m = None
                    n = None
    assert [m, n] != [None, None]
    assert [adptbx.is_positive_definite(m),
            adptbx.is_positive_definite(n)].count(True) == 2
    r = random.random()
    if (r > 0.5):
        m = adptbx.random_rotate_ellipsoid(u_cart=m)
        n = adptbx.random_rotate_ellipsoid(u_cart=n)
    return m, n
Esempio n. 5
0
def branch_3_mn():
  m = None
  n = None
  small = 1.e-15
  for i in xrange(10000):
      m2=[]
      n2=[]
      for i in xrange(4):
          r1 = random.random()
          r2 = random.random()
          r3 = random.random()
          if(r3 > 0.1):
             r1 = r2
          m2.append(r1)
          n2.append(r2)
      p1 = 0.5 * (m2[0]+m2[3] + math.sqrt(4*m2[1]*m2[2]+(m2[0]-m2[3])**2))
      p2 = 0.5 * (m2[0]+m2[3] - math.sqrt(4*m2[1]*m2[2]+(m2[0]-m2[3])**2))
      q1 = 0.5 * (n2[0]+m2[3] + math.sqrt(4*n2[1]*n2[2]+(n2[0]-n2[3])**2))
      q2 = 0.5 * (n2[0]+m2[3] - math.sqrt(4*n2[1]*n2[2]+(n2[0]-n2[3])**2))
      if(min(p1,p2) > 0.0 and min(q1,q2) > 0.0):
          r = random.random()
          if(r > 0.5):
             r1 = r3
             r2 = r2
          m = [m2[0],m2[3],r1,m2[1],0,0]
          n = [n2[0],n2[3],r2,n2[1],0,0]
          if([adptbx.is_positive_definite(m),
              adptbx.is_positive_definite(n)].count(True)==2):
              esm = adptbx.eigensystem(m)
              esn = adptbx.eigensystem(n)
              vn = esn.values()
              vm = esm.values()
              mmin = flex.min(flex.double(vm))
              nmin = flex.min(flex.double(vn))
              if(abs(abs(mmin) - abs(nmin)) < small and mmin> 0. and nmin> 0.):
                  for i, v in enumerate(vn):
                      if(abs(abs(nmin) - v) < small): break
                  for j, v in enumerate(vm):
                      if(abs(abs(mmin) - v) < small): break
                  vecn = flex.double(esn.vectors(i))
                  vecm = flex.double(esm.vectors(j))
                  if(flex.mean(vecm-vecn) < small): break
              else:
                m = None
                n = None
  assert [m,n] != [None,None]
  assert [adptbx.is_positive_definite(m),
          adptbx.is_positive_definite(n)].count(True)==2
  r = random.random()
  if(r > 0.5):
     m = adptbx.random_rotate_ellipsoid(u_cart = m)
     n = adptbx.random_rotate_ellipsoid(u_cart = n)
  return m,n
Esempio n. 6
0
def run():
  symmetry = crystal.symmetry(
    unit_cell=(10.67, 10.67, 4.68, 90, 90, 120),
    space_group_symbol="P 3")

  special_position_settings = crystal.special_position_settings(
    crystal_symmetry=symmetry,
    min_distance_sym_equiv=0.5)

  site = (0, 0, 0.236)
  u_cif = ((0.17, 0.17, 0.19, 0.09, 0, 0))

  site_symmetry = special_position_settings.site_symmetry(site)

  print "Input Ucif:", u_cif
  u_star = adptbx.u_cif_as_u_star(symmetry.unit_cell(), u_cif)
  if (not site_symmetry.is_compatible_u_star(u_star)):
    print "Warning: ADP tensor is incompatible with site symmetry."
  u_star = site_symmetry.average_u_star(u_star)
  u_cif = adptbx.u_star_as_u_cif(symmetry.unit_cell(), u_star)
  print "Averaged Ucif:", u_cif

  u_cart = adptbx.u_star_as_u_cart(symmetry.unit_cell(), u_star)
  eigenvalues = adptbx.eigenvalues(u_cart)
  if (not adptbx.is_positive_definite(eigenvalues)):
    print "ADP tensor is not positive definite."

  print "Eigenvectors and values:"
  eigensystem = adptbx.eigensystem(u_cart)
  for i in xrange(3):
    print "  v=(%.5f %.5f %.5f) " % eigensystem.vectors(i),
    print "lambda=%.4f" % (eigensystem.values()[i],)
Esempio n. 7
0
def run():
    symmetry = crystal.symmetry(unit_cell=(10.67, 10.67, 4.68, 90, 90, 120),
                                space_group_symbol="P 3")

    special_position_settings = crystal.special_position_settings(
        crystal_symmetry=symmetry, min_distance_sym_equiv=0.5)

    site = (0, 0, 0.236)
    u_cif = ((0.17, 0.17, 0.19, 0.09, 0, 0))

    site_symmetry = special_position_settings.site_symmetry(site)

    print "Input Ucif:", u_cif
    u_star = adptbx.u_cif_as_u_star(symmetry.unit_cell(), u_cif)
    if (not site_symmetry.is_compatible_u_star(u_star)):
        print "Warning: ADP tensor is incompatible with site symmetry."
    u_star = site_symmetry.average_u_star(u_star)
    u_cif = adptbx.u_star_as_u_cif(symmetry.unit_cell(), u_star)
    print "Averaged Ucif:", u_cif

    u_cart = adptbx.u_star_as_u_cart(symmetry.unit_cell(), u_star)
    eigenvalues = adptbx.eigenvalues(u_cart)
    if (not adptbx.is_positive_definite(eigenvalues)):
        print "ADP tensor is not positive definite."

    print "Eigenvectors and values:"
    eigensystem = adptbx.eigensystem(u_cart)
    for i in xrange(3):
        print "  v=(%.5f %.5f %.5f) " % eigensystem.vectors(i),
        print "lambda=%.4f" % (eigensystem.values()[i], )
Esempio n. 8
0
def exercise_eigen_core(diag):
    u = adptbx.random_rotate_ellipsoid(diag + [0., 0., 0.])
    ev = list(adptbx.eigenvalues(u))
    diag.sort()
    ev.sort()
    for i in xrange(3):
        check_eigenvalue(u, ev[i])
    for i in xrange(3):
        assert abs(diag[i] - ev[i]) < 1.e-4
    if (adptbx.is_positive_definite(ev)):
        es = adptbx.eigensystem(u)
        ev = list(es.values())
        ev.sort()
        for i in xrange(3):
            check_eigenvalue(u, ev[i])
        for i in xrange(3):
            assert abs(diag[i] - ev[i]) < 1.e-4
        evec = []
        for i in xrange(3):
            check_eigenvector(u, es.values()[i], es.vectors(i))
            evec.extend(es.vectors(i))
        return  # XXX following tests disabled for the moment
        # sometimes fail if eigenvalues are very similar but not identical
        sqrt_eval = matrix.diag(flex.sqrt(flex.double(es.values())))
        evec = matrix.sqr(evec).transpose()
        sqrt_u = evec * sqrt_eval * evec.transpose()
        u_full = matrix.sym(sym_mat3=u).elems
        assert approx_equal(u_full, (sqrt_u.transpose() * sqrt_u).elems,
                            eps=1.e-3)
        assert approx_equal(u_full, (sqrt_u * sqrt_u.transpose()).elems,
                            eps=1.e-3)
        assert approx_equal(u_full, (sqrt_u * sqrt_u).elems, eps=1.e-3)
        sqrt_u_plus_shifts = matrix.sym(sym_mat3=[
            x + 10 * (random.random() - .5) for x in sqrt_u.as_sym_mat3()
        ])
        sts = (sqrt_u_plus_shifts.transpose() *
               sqrt_u_plus_shifts).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
        sts = (sqrt_u_plus_shifts *
               sqrt_u_plus_shifts.transpose()).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
        sts = (sqrt_u_plus_shifts * sqrt_u_plus_shifts).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
Esempio n. 9
0
def exercise_eigen_core(diag):
    u = adptbx.random_rotate_ellipsoid(diag + [0.0, 0.0, 0.0])
    ev = list(adptbx.eigenvalues(u))
    diag.sort()
    ev.sort()
    for i in xrange(3):
        check_eigenvalue(u, ev[i])
    for i in xrange(3):
        assert abs(diag[i] - ev[i]) < 1.0e-4
    if adptbx.is_positive_definite(ev):
        es = adptbx.eigensystem(u)
        ev = list(es.values())
        ev.sort()
        for i in xrange(3):
            check_eigenvalue(u, ev[i])
        for i in xrange(3):
            assert abs(diag[i] - ev[i]) < 1.0e-4
        evec = []
        for i in xrange(3):
            check_eigenvector(u, es.values()[i], es.vectors(i))
            evec.extend(es.vectors(i))
        return  # XXX following tests disabled for the moment
        # sometimes fail if eigenvalues are very similar but not identical
        sqrt_eval = matrix.diag(flex.sqrt(flex.double(es.values())))
        evec = matrix.sqr(evec).transpose()
        sqrt_u = evec * sqrt_eval * evec.transpose()
        u_full = matrix.sym(sym_mat3=u).elems
        assert approx_equal(u_full, (sqrt_u.transpose() * sqrt_u).elems, eps=1.0e-3)
        assert approx_equal(u_full, (sqrt_u * sqrt_u.transpose()).elems, eps=1.0e-3)
        assert approx_equal(u_full, (sqrt_u * sqrt_u).elems, eps=1.0e-3)
        sqrt_u_plus_shifts = matrix.sym(sym_mat3=[x + 10 * (random.random() - 0.5) for x in sqrt_u.as_sym_mat3()])
        sts = (sqrt_u_plus_shifts.transpose() * sqrt_u_plus_shifts).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
        sts = (sqrt_u_plus_shifts * sqrt_u_plus_shifts.transpose()).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
        sts = (sqrt_u_plus_shifts * sqrt_u_plus_shifts).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
Esempio n. 10
0
def run(pdb_file_name, n_models, log, output_file_name_prefix, eps=1.e-7):
    pdb_inp = iotbx.pdb.input(file_name=pdb_file_name)
    pdb_hierarchy = pdb_inp.construct_hierarchy()
    asc = pdb_hierarchy.atom_selection_cache()
    cs = pdb_inp.crystal_symmetry_from_cryst1()
    tls_extract = mmtbx.tls.tools.tls_from_pdb_inp(
        remark_3_records=pdb_inp.extract_remark_iii_records(3),
        pdb_hierarchy=pdb_hierarchy)
    for i_group, tls_params_one_group in enumerate(tls_extract.tls_params):
        selection = asc.selection(tls_params_one_group.selection_string)
        pdb_hierarchy_sel = pdb_hierarchy.select(selection)
        xrs = pdb_hierarchy_sel.extract_xray_structure(crystal_symmetry=cs)
        deg_to_rad_scale = math.pi / 180
        # Units: T[A], L[deg**2], S[A*deg]
        T = matrix.sym(sym_mat3=tls_params_one_group.t)
        L = matrix.sym(sym_mat3=tls_params_one_group.l)
        S = matrix.sqr(tls_params_one_group.s)
        origin = tls_params_one_group.origin
        tlso = tools.tlso(t=T.as_sym_mat3(),
                          l=L.as_sym_mat3(),
                          s=S,
                          origin=origin)
        # sanity check
        if (not adptbx.is_positive_definite(tls_params_one_group.t, eps)):
            raise Sorry("T matrix is not positive definite.")
        if (not adptbx.is_positive_definite(tls_params_one_group.l, eps)):
            raise Sorry("L matrix is not positive definite.")
        r = analysis.run(T=T,
                         L=L * (deg_to_rad_scale**2),
                         S=S * deg_to_rad_scale,
                         log=log).self_check()
        ensemble_generator_obj = ensemble_generator(
            tls_from_motions_object=r,
            pdb_hierarchy=pdb_hierarchy_sel,
            xray_structure=xrs,
            n_models=n_models,
            origin=origin,
            log=log)
        ensemble_generator_obj.write_pdb_file(
            file_name=output_file_name_prefix +
            "_ensemble_%s.pdb" % str(i_group))
        # get U from TLS
        u_from_tls = tools.uaniso_from_tls_one_group(
            tlso=tlso, sites_cart=xrs.sites_cart(), zeroize_trace=False)
        # get U from ensemble
        pdb_hierarchy_from_tls = pdb_hierarchy_sel.deep_copy()
        pdb_hierarchy_from_ens = pdb_hierarchy_sel.deep_copy()
        u_from_ens = tools.u_cart_from_ensemble(
            models=ensemble_generator_obj.states.root.models())
        for i in xrange(xrs.sites_cart().size()):
            print "atom %d:" % i
            print "  Ucart(from TLS):", ["%8.5f" % u for u in u_from_tls[i]]
            print "  Ucart(from ens):", ["%8.5f" % u for u in u_from_ens[i]]
        #
        u1, u2 = u_from_tls.as_double(), u_from_ens.as_double()
        cc = flex.linear_correlation(x=u1, y=u2).coefficient()
        r = flex.sum(flex.abs(u1-u2))/\
            flex.sum(flex.abs(flex.abs(u1)+flex.abs(u2)))*2
        print "%6.4f %6.4f" % (cc, r)
        #
        pdb_hierarchy_from_tls.atoms().set_uij(u_from_tls)
        pdb_hierarchy_from_ens.atoms().set_uij(u_from_ens)
        pdb_hierarchy_from_tls.write_pdb_file(
            file_name=output_file_name_prefix +
            "_u_from_tls_%s.pdb" % str(i_group),
            crystal_symmetry=cs)
        pdb_hierarchy_from_ens.write_pdb_file(
            file_name=output_file_name_prefix +
            "_u_from_ensemble_%s.pdb" % str(i_group),
            crystal_symmetry=cs)
    return ensemble_generator_obj
Esempio n. 11
0
def exercise_branch_2_1(small=1.e-9):
    m = [2, 1, 2, 0, 0, 0]
    n = [2, 1, 1, 0, 0, 0]
    counter = 0
    trials = 100000
    i = 0
    branch_0 = 0
    branch_1 = 0
    branch_1_1 = 0
    branch_1_2 = 0
    branch_1_2_1 = 0
    branch_1_2_2 = 0
    branch_1_2_3 = 0
    branch_1_2_3_1 = 0
    branch_1_2_3_2 = 0
    while i < trials:
        i += 1
        m = [2, 1, 2, 0, 0, 0]
        n = [2, 1, 1, 0, 0, 0]
        counter += 1
        c = scitbx.math.euler_angles_as_matrix(
            [random.uniform(0, 360) for j in range(3)], deg=True).elems
        r = random.random()
        if (r < 0.25):
            m = adptbx.c_u_c_transpose(c, m)
            n = adptbx.c_u_c_transpose(c, n)
        elif (r >= 0.25 and r < 0.5):
            m = adptbx.c_u_c_transpose(c, m)
        elif (r >= 0.5 and r < 0.75):
            n = adptbx.c_u_c_transpose(c, n)
        else:
            r = random.random()
            run_away = 1000
            while run_away > 0:
                run_away -= 1
                r = random.random()
                if (r < 0.33):
                    m = adptbx.c_u_c_transpose(c, m)
                    n = adptbx.c_u_c_transpose(c, n)
                elif (r >= 0.33 and r < 0.66):
                    m = adptbx.c_u_c_transpose(c, m)
                else:
                    n = adptbx.c_u_c_transpose(c, n)
                m = adptbx.c_u_c_transpose(c, m)
                n = adptbx.c_u_c_transpose(c, n)
                m = [
                    m[0], m[1], m[2], m[3] * random.choice((0, 1)),
                    m[4] * random.choice((0, 1)), m[5] * random.choice((0, 1))
                ]
                n = [
                    n[0], n[1], n[2], n[3] * random.choice((0, 1)),
                    n[4] * random.choice((0, 1)), n[5] * random.choice((0, 1))
                ]
                m = factor(list(m), i)
                n = factor(list(n), i)
                if (adptbx.is_positive_definite(m, small)
                        and adptbx.is_positive_definite(n, small)):
                    break

        m_dc = copy.deepcopy(m)
        n_dc = copy.deepcopy(n)
        qq = tools.common(n, m, small)
        #assert approx_equal(m,m_dc)
        #assert approx_equal(n,n_dc)

        if (qq.branch_0()): branch_0 += 1
        if (qq.branch_1()): branch_1 += 1
        if (qq.branch_1_1()): branch_1_1 += 1
        if (qq.branch_1_2()): branch_1_2 += 1
        if (qq.branch_1_2_1()): branch_1_2_1 += 1
        if (qq.branch_1_2_2()): branch_1_2_2 += 1
        if (qq.branch_1_2_3()): branch_1_2_3 += 1
        if (qq.branch_1_2_3_1()): branch_1_2_3_1 += 1
        if (qq.branch_1_2_3_2()): branch_1_2_3_2 += 1

        if (counter >= 10000):
            counter = 0
            print("." * 30)
            print("i= ", i, "out of ", trials)
            print("branch_0       = ", branch_0)
            print("branch_1       = ", branch_1)
            print("branch_1_1     = ", branch_1_1)
            print("branch_1_2     = ", branch_1_2)
            print("branch_1_2_1   = ", branch_1_2_1)
            print("branch_1_2_2   = ", branch_1_2_2)
            print("branch_1_2_3   = ", branch_1_2_3)
            print("branch_1_2_3_1 = ", branch_1_2_3_1)
            print("branch_1_2_3_2 = ", branch_1_2_3_2)
            sys.stdout.flush()
Esempio n. 12
0
def tls_from_uanisos(xray_structure,
                     selections,
                     tlsos_initial,
                     number_of_macro_cycles       = 3000,
                     max_iterations               = 1000,
                     refine_T                     = True,
                     refine_L                     = True,
                     refine_S                     = True,
                     verbose                      = -1,
                     enforce_positive_definite_TL = True,
                     out                          = None):
  global time_tls_from_uanisos
  t1 = time.time()
  if(out is None): out = sys.stdout
  if(verbose > 0):
    show_tls(tlsos = tlsos_initial,
             text  = "TLS from ADP: start TLS values", out = out)
  u_cart=xray_structure.scatterers().extract_u_cart(xray_structure.unit_cell())
  T_min = []
  L_min = []
  S_min = []
  group_counter = 0
  for tlso_initial, selection in zip(tlsos_initial, selections):
    group_counter += 1
    T_initial = tlso_initial.t
    L_initial = tlso_initial.l
    S_initial = tlso_initial.s
    if(enforce_positive_definite_TL):
      T_initial = adptbx.eigenvalue_filtering(T_initial)
      L_initial = adptbx.eigenvalue_filtering(L_initial)
    stop_flag = 0
    target_stop = -1.0
    sites_cart_selected = xray_structure.sites_cart().select(selection)
    u_cart_selected = u_cart.select(selection)
    for i in range(1, number_of_macro_cycles+1):
      target_start = target_stop
      minimized = tls_from_uaniso_minimizer(uaniso    = u_cart_selected,
                                            T_initial = T_initial,
                                            L_initial = L_initial,
                                            S_initial = S_initial,
                                            refine_T  = refine_T,
                                            refine_L  = refine_L,
                                            refine_S  = refine_S,
                                            max_iterations = max_iterations,
                                            origin    = tlso_initial.origin,
                                            sites     = sites_cart_selected)
      if(refine_T):  T_initial = minimized.T_min
      else:          assert approx_equal(T_initial, minimized.T_min)
      if(refine_L):  L_initial = minimized.L_min
      else:          assert approx_equal(L_initial, minimized.L_min)
      if(refine_S):  S_initial = minimized.S_min
      else:          assert approx_equal(S_initial, minimized.S_min)
    if(verbose > 0):
      print >> out, "TLS group %d: minimized target = " %(group_counter),minimized.f
    T_min_ = minimized.T_min
    L_min_ = minimized.L_min
    if(enforce_positive_definite_TL):
      T_min_ = adptbx.eigenvalue_filtering(T_min_)
      L_min_ = adptbx.eigenvalue_filtering(L_min_)
    T_min.append(T_min_)
    L_min.append(L_min_)
    S_min.append(minimized.S_min)
    if(enforce_positive_definite_TL):
      assert adptbx.is_positive_definite(T_min_, 1.e-6)
      assert adptbx.is_positive_definite(L_min_, 1.e-6)
  tlsos_result = generate_tlsos(selections     = selections,
                                xray_structure = xray_structure,
                                T              = T_min,
                                L              = L_min,
                                S              = S_min)
  if(verbose > 0):
    show_tls(tlsos = tlsos_result,
             text  = "TLS from ADP: final TLS values", out = out)
  t2 = time.time()
  time_tls_from_uanisos += (t2 - t1)
  return tlsos_result
Esempio n. 13
0
def tls_from_u_cart(xray_structure,
                    tlsos_initial,
                    tls_selections,
                    number_of_macro_cycles = 100,
                    max_iterations         = 100):
  global time_tls_from_u_cart
  timer = user_plus_sys_time()
  uc = xray_structure.unit_cell()
  xray_structure.tidy_us(u_min = 1.e-6)
  ueq = xray_structure.extract_u_iso_or_u_equiv()
  assert (ueq < 0.0).count(True) == 0
  u_cart = xray_structure.scatterers().extract_u_cart(uc)
  for tls_selection in tls_selections:
    u_cart_selected = u_cart.select(tls_selection)
    assert adptbx.is_positive_definite(u_cart_selected,1.e-6).count(False)==0
  xray_structure.tidy_us(u_min = 1.e-6)
  t = []
  l = []
  s = []
  lim_l = 10.0
  for tls_selection, tlso in zip(tls_selections, tlsos_initial):
    t.append( tlso.t )
    #if(abs(tlso.t[0]) < eps and abs(tlso.t[1]) < eps and abs(tlso.t[2]) < eps and
    #   abs(tlso.t[3]) < eps and abs(tlso.t[4]) < eps and abs(tlso.t[5]) < eps):
    #   t.append( t_from_u_cart(u_cart.select(tls_selection), 1.e-6) )
    #else:
    #   t.append( tlso.t )
    #l.append( [0,0,0,0,0,0] )
    if abs(tlso.l[0])>lim_l: l1 = tlso.l[0]/5.
    else:                    l1 = tlso.l[0]
    if abs(tlso.l[1])>lim_l: l2 = tlso.l[1]/5.
    else:                    l2 = tlso.l[1]
    if abs(tlso.l[2])>lim_l: l3 = tlso.l[2]/5.
    else:                    l3 = tlso.l[2]
    if abs(tlso.l[3])>lim_l: l4 = tlso.l[3]/5.
    else:                    l4 = tlso.l[3]
    if abs(tlso.l[4])>lim_l: l5 = tlso.l[4]/5.
    else:                    l5 = tlso.l[4]
    if abs(tlso.l[5])>lim_l: l6 = tlso.l[5]/5.
    else:                    l6 = tlso.l[5]
    l.append( [l1,l2,l3,l4,l5,l6] )
    s.append( tlso.s )
  tlsos = generate_tlsos(selections     = tls_selections,
                         xray_structure = xray_structure,
                         T = t, L = l, S = s)
  #for rt,rl,rs in [[0,1,1],[1,0,0]]*3:
  for rt,rl,rs in [[0,1,1],[1,0,0],[1,1,1]]:
    tlsos_ = tls_from_uanisos(xray_structure        = xray_structure,
                              selections             = tls_selections,
                              tlsos_initial          = tlsos,
                              number_of_macro_cycles = number_of_macro_cycles,
                              max_iterations         = max_iterations,
                              refine_T               = rt,
                              refine_L               = rl,
                              refine_S               = rs,
                              verbose                = -1,
                              out                    = None)
    tlsos = tlsos_
  t = []
  l = []
  s = []
  for tlso in tlsos:
    t.append( tlso.t )
    if abs(tlso.l[0])>lim_l: l1 = lim_l/5.
    else:                    l1 = tlso.l[0]
    if abs(tlso.l[1])>lim_l: l2 = lim_l/5.
    else:                    l2 = tlso.l[1]
    if abs(tlso.l[2])>lim_l: l3 = lim_l/5.
    else:                    l3 = tlso.l[2]
    if abs(tlso.l[3])>lim_l: l4 = lim_l/5.
    else:                    l4 = tlso.l[3]
    if abs(tlso.l[4])>lim_l: l5 = lim_l/5.
    else:                    l5 = tlso.l[4]
    if abs(tlso.l[5])>lim_l: l6 = lim_l/5.
    else:                    l6 = tlso.l[5]
    l.append( [l1,l2,l3,l4,l5,l6] )
    s.append( tlso.s )
  tlsos = generate_tlsos(selections     = tls_selections,
                         xray_structure = xray_structure,
                         T = t, L = l, S = s)
  time_tls_from_u_cart += timer.elapsed()
  return tlsos
Esempio n. 14
0
def tls_from_u_cart(xray_structure,
                    tlsos_initial,
                    tls_selections,
                    number_of_macro_cycles=100,
                    max_iterations=100):
    global time_tls_from_u_cart
    timer = user_plus_sys_time()
    uc = xray_structure.unit_cell()
    xray_structure.tidy_us(u_min=1.e-6)
    ueq = xray_structure.extract_u_iso_or_u_equiv()
    assert (ueq < 0.0).count(True) == 0
    u_cart = xray_structure.scatterers().extract_u_cart(uc)
    for tls_selection in tls_selections:
        u_cart_selected = u_cart.select(tls_selection)
        assert adptbx.is_positive_definite(u_cart_selected,
                                           1.e-6).count(False) == 0
    xray_structure.tidy_us(u_min=1.e-6)
    t = []
    l = []
    s = []
    lim_l = 10.0
    for tls_selection, tlso in zip(tls_selections, tlsos_initial):
        t.append(tlso.t)
        #if(abs(tlso.t[0]) < eps and abs(tlso.t[1]) < eps and abs(tlso.t[2]) < eps and
        #   abs(tlso.t[3]) < eps and abs(tlso.t[4]) < eps and abs(tlso.t[5]) < eps):
        #   t.append( t_from_u_cart(u_cart.select(tls_selection), 1.e-6) )
        #else:
        #   t.append( tlso.t )
        #l.append( [0,0,0,0,0,0] )
        if abs(tlso.l[0]) > lim_l: l1 = tlso.l[0] / 5.
        else: l1 = tlso.l[0]
        if abs(tlso.l[1]) > lim_l: l2 = tlso.l[1] / 5.
        else: l2 = tlso.l[1]
        if abs(tlso.l[2]) > lim_l: l3 = tlso.l[2] / 5.
        else: l3 = tlso.l[2]
        if abs(tlso.l[3]) > lim_l: l4 = tlso.l[3] / 5.
        else: l4 = tlso.l[3]
        if abs(tlso.l[4]) > lim_l: l5 = tlso.l[4] / 5.
        else: l5 = tlso.l[4]
        if abs(tlso.l[5]) > lim_l: l6 = tlso.l[5] / 5.
        else: l6 = tlso.l[5]
        l.append([l1, l2, l3, l4, l5, l6])
        s.append(tlso.s)
    tlsos = generate_tlsos(selections=tls_selections,
                           xray_structure=xray_structure,
                           T=t,
                           L=l,
                           S=s)
    #for rt,rl,rs in [[0,1,1],[1,0,0]]*3:
    for rt, rl, rs in [[0, 1, 1], [1, 0, 0], [1, 1, 1]]:
        tlsos_ = tls_from_uanisos(
            xray_structure=xray_structure,
            selections=tls_selections,
            tlsos_initial=tlsos,
            number_of_macro_cycles=number_of_macro_cycles,
            max_iterations=max_iterations,
            refine_T=rt,
            refine_L=rl,
            refine_S=rs,
            verbose=-1,
            out=None)
        tlsos = tlsos_
    t = []
    l = []
    s = []
    for tlso in tlsos:
        t.append(tlso.t)
        if abs(tlso.l[0]) > lim_l: l1 = lim_l / 5.
        else: l1 = tlso.l[0]
        if abs(tlso.l[1]) > lim_l: l2 = lim_l / 5.
        else: l2 = tlso.l[1]
        if abs(tlso.l[2]) > lim_l: l3 = lim_l / 5.
        else: l3 = tlso.l[2]
        if abs(tlso.l[3]) > lim_l: l4 = lim_l / 5.
        else: l4 = tlso.l[3]
        if abs(tlso.l[4]) > lim_l: l5 = lim_l / 5.
        else: l5 = tlso.l[4]
        if abs(tlso.l[5]) > lim_l: l6 = lim_l / 5.
        else: l6 = tlso.l[5]
        l.append([l1, l2, l3, l4, l5, l6])
        s.append(tlso.s)
    tlsos = generate_tlsos(selections=tls_selections,
                           xray_structure=xray_structure,
                           T=t,
                           L=l,
                           S=s)
    time_tls_from_u_cart += timer.elapsed()
    return tlsos
Esempio n. 15
0
def exercise_interface():
  episq = 8*(math.pi**2)
  assert approx_equal(adptbx.u_as_b(2.3), 2.3*episq)
  assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(2.3)), 2.3)
  u = (3,4,9, 2,1,7)
  assert approx_equal(adptbx.u_as_b(u), [x*episq for x in u])
  assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(u)), u)
  uc = uctbx.unit_cell((5,4,7,80,110,100))
  for fw,bw in ((adptbx.u_cif_as_u_star, adptbx.u_star_as_u_cif),
                (adptbx.u_cart_as_u_star, adptbx.u_star_as_u_cart),
                (adptbx.u_cart_as_u_cif, adptbx.u_cif_as_u_cart),
                (adptbx.u_cart_as_beta, adptbx.beta_as_u_cart),
                (adptbx.u_cif_as_beta, adptbx.beta_as_u_cif)):
    assert approx_equal(bw(uc, fw(uc, u)), u)
  assert approx_equal(adptbx.beta_as_u_star(adptbx.u_star_as_beta(u)), u)
  assert approx_equal(adptbx.u_cart_as_u_iso(adptbx.u_iso_as_u_cart(2.3)), 2.3)
  for fw,bw in ((adptbx.u_iso_as_u_star, adptbx.u_star_as_u_iso),
                (adptbx.u_iso_as_u_cif, adptbx.u_cif_as_u_iso),
                (adptbx.u_iso_as_beta, adptbx.beta_as_u_iso)):
    assert approx_equal(bw(uc, fw(uc, 2.3)), 2.3)
  fc = adptbx.factor_u_cart_u_iso(u_cart=u)
  assert approx_equal(fc.u_iso, adptbx.u_cart_as_u_iso(u))
  assert approx_equal(
    fc.u_cart_minus_u_iso,
    [uii-fc.u_iso for uii in u[:3]]+list(u[3:]))
  f = adptbx.factor_u_star_u_iso(
    unit_cell=uc, u_star=adptbx.u_cart_as_u_star(uc, u))
  assert approx_equal(f.u_iso, fc.u_iso)
  assert approx_equal(
    f.u_star_minus_u_iso,
    adptbx.u_cart_as_u_star(uc, fc.u_cart_minus_u_iso))
  f = adptbx.factor_u_cif_u_iso(
    unit_cell=uc, u_cif=adptbx.u_cart_as_u_cif(uc, u))
  assert approx_equal(f.u_iso, fc.u_iso)
  assert approx_equal(
    f.u_cif_minus_u_iso,
    adptbx.u_cart_as_u_cif(uc, fc.u_cart_minus_u_iso))
  f = adptbx.factor_beta_u_iso(
    unit_cell=uc, beta=adptbx.u_cart_as_beta(uc, u))
  assert approx_equal(f.u_iso, fc.u_iso)
  assert approx_equal(
    f.beta_minus_u_iso,
    adptbx.u_cart_as_beta(uc, fc.u_cart_minus_u_iso))
  assert approx_equal(adptbx.debye_waller_factor_b_iso(0.25,2.3),
                      math.exp(-2.3*0.25))
  assert approx_equal(adptbx.debye_waller_factor_u_iso(0.25,2.3),
                      math.exp(-2.3*episq*0.25))
  assert approx_equal(adptbx.debye_waller_factor_b_iso(uc, (1,2,3), 2.3),
                      adptbx.debye_waller_factor_u_iso(uc, (1,2,3), 2.3/episq))
  u_star = adptbx.u_cart_as_u_star(uc, u)
  dw = adptbx.debye_waller_factor_u_star((1,2,3), u_star)
  assert approx_equal(dw, adptbx.debye_waller_factor_beta((1,2,3),
                            adptbx.u_star_as_beta(u_star)))
  assert approx_equal(dw, adptbx.debye_waller_factor_u_cif(uc, (1,2,3),
                            adptbx.u_star_as_u_cif(uc, u_star)))
  assert approx_equal(dw, adptbx.debye_waller_factor_u_cart(uc, (1,2,3),
                            adptbx.u_star_as_u_cart(uc, u_star)))
  for e in adptbx.eigenvalues(u):
    check_eigenvalue(u, e)
  assert not adptbx.is_positive_definite(adptbx.eigenvalues(u))
  assert not adptbx.is_positive_definite(adptbx.eigenvalues(u), 0)
  assert adptbx.is_positive_definite(adptbx.eigenvalues(u), 1.22)
  assert not adptbx.is_positive_definite(u)
  assert not adptbx.is_positive_definite(u, 0)
  assert adptbx.is_positive_definite(u, 1.22)
  up = (0.534, 0.812, 0.613, 0.0166, 0.134, -0.0124)
  s = adptbx.eigensystem(up)
  assert approx_equal(s.values(), (0.813132, 0.713201, 0.432668))
  for i in xrange(3):
    check_eigenvector(up, s.values()[i], s.vectors(i))
  c = (1,2,3, 3,-4,5, 4,5,6)
  v = (198,18,1020,116,447,269)
  assert approx_equal(adptbx.c_u_c_transpose(c, u), v)
  assert approx_equal(adptbx.eigensystem(u).values(),
    (14.279201519086316, 2.9369143826320214, -1.2161159017183376))
  s = adptbx.eigensystem(up)
  try: s.vectors(4)
  except RuntimeError, e: assert str(e).endswith("Index out of range.")
  else: raise Exception_expected
  uf = adptbx.eigenvalue_filtering(u_cart=u, u_min=0)
  assert approx_equal(uf, (3.0810418, 4.7950710, 9.3400030,
                           1.7461615, 1.1659954, 6.4800706))
  uf = adptbx.eigenvalue_filtering(u_cart=u, u_min=0, u_max=3)
  assert approx_equal(uf, (2.7430890, 1.0378360, 2.1559895,
                           0.6193215, -0.3921632, 1.2846854))
  uf = adptbx.eigenvalue_filtering(u_cart=u, u_min=0, u_max=3)
  assert approx_equal(scitbx.linalg.eigensystem.real_symmetric(u).values(),
                      (14.2792015, 2.9369144, -1.2161159))
  assert approx_equal(scitbx.linalg.eigensystem.real_symmetric(uf).values(),
                      (3, 2.9369144, 0))
  uf = adptbx.eigenvalue_filtering(up)
  assert approx_equal(uf, up)
Esempio n. 16
0
def tls_from_uanisos(xray_structure,
                     selections,
                     tlsos_initial,
                     number_of_macro_cycles=3000,
                     max_iterations=1000,
                     refine_T=True,
                     refine_L=True,
                     refine_S=True,
                     verbose=-1,
                     enforce_positive_definite_TL=True,
                     out=None):
    global time_tls_from_uanisos
    t1 = time.time()
    if (out is None): out = sys.stdout
    if (verbose > 0):
        show_tls(tlsos=tlsos_initial,
                 text="TLS from ADP: start TLS values",
                 out=out)
    u_cart = xray_structure.scatterers().extract_u_cart(
        xray_structure.unit_cell())
    T_min = []
    L_min = []
    S_min = []
    group_counter = 0
    for tlso_initial, selection in zip(tlsos_initial, selections):
        group_counter += 1
        T_initial = tlso_initial.t
        L_initial = tlso_initial.l
        S_initial = tlso_initial.s
        if (enforce_positive_definite_TL):
            T_initial = adptbx.eigenvalue_filtering(T_initial)
            L_initial = adptbx.eigenvalue_filtering(L_initial)
        stop_flag = 0
        target_stop = -1.0
        sites_cart_selected = xray_structure.sites_cart().select(selection)
        u_cart_selected = u_cart.select(selection)
        for i in range(1, number_of_macro_cycles + 1):
            target_start = target_stop
            minimized = tls_from_uaniso_minimizer(
                uaniso=u_cart_selected,
                T_initial=T_initial,
                L_initial=L_initial,
                S_initial=S_initial,
                refine_T=refine_T,
                refine_L=refine_L,
                refine_S=refine_S,
                max_iterations=max_iterations,
                origin=tlso_initial.origin,
                sites=sites_cart_selected)
            if (refine_T): T_initial = minimized.T_min
            else: assert approx_equal(T_initial, minimized.T_min)
            if (refine_L): L_initial = minimized.L_min
            else: assert approx_equal(L_initial, minimized.L_min)
            if (refine_S): S_initial = minimized.S_min
            else: assert approx_equal(S_initial, minimized.S_min)
        if (verbose > 0):
            print >> out, "TLS group %d: minimized target = " % (
                group_counter), minimized.f
        T_min_ = minimized.T_min
        L_min_ = minimized.L_min
        if (enforce_positive_definite_TL):
            T_min_ = adptbx.eigenvalue_filtering(T_min_)
            L_min_ = adptbx.eigenvalue_filtering(L_min_)
        T_min.append(T_min_)
        L_min.append(L_min_)
        S_min.append(minimized.S_min)
        if (enforce_positive_definite_TL):
            assert adptbx.is_positive_definite(T_min_, 1.e-6)
            assert adptbx.is_positive_definite(L_min_, 1.e-6)
    tlsos_result = generate_tlsos(selections=selections,
                                  xray_structure=xray_structure,
                                  T=T_min,
                                  L=L_min,
                                  S=S_min)
    if (verbose > 0):
        show_tls(tlsos=tlsos_result,
                 text="TLS from ADP: final TLS values",
                 out=out)
    t2 = time.time()
    time_tls_from_uanisos += (t2 - t1)
    return tlsos_result
Esempio n. 17
0
def exercise(small=1.e-9):
    for symbol in ["P 1"]:
        space_group_info = sgtbx.space_group_info(symbol=symbol)
        random_xray_structure = random_structure.xray_structure(
            space_group_info=space_group_info,
            elements=["N"] * 10,
            volume_per_atom=50.0,
            random_u_iso=False,
            u_iso=adptbx.b_as_u(20.0))
        sg = random_xray_structure.space_group()
        uc = random_xray_structure.unit_cell()
        print(symbol, uc)
        print()
        sys.stdout.flush()
        counter = 0
        trials = 100000
        i = 0
        branch_0 = 0
        branch_1 = 0
        branch_1_1 = 0
        branch_1_2 = 0
        branch_1_2_1 = 0
        branch_1_2_2 = 0
        branch_1_2_3 = 0
        branch_1_2_3_1 = 0
        branch_1_2_3_2 = 0

        while i < trials:
            i += 1
            counter += 1
            r = random.random()
            if (r < 0.333):
                m = adptbx.random_u_cart(u_scale=20. * random.random(),
                                         u_min=0)
                n = adptbx.random_u_cart(u_scale=20. * random.random(),
                                         u_min=0)
                while 1:
                    for ind in range(6):
                        r = random.random()
                        m = flex.double(m)
                        if (r > 0.5):
                            m[ind] = n[ind]
                    m = list(m)
                    if (adptbx.is_positive_definite(m, 0)
                            and adptbx.is_positive_definite(n, 0)):
                        break
            elif (r >= 0.333 and r < 0.66):
                m, n = branch_3_mn()
            else:
                m, n = branch_2_mn(0)
            m_dc = copy.deepcopy(m)
            n_dc = copy.deepcopy(n)
            m = factor(list(m), i)
            n = factor(list(n), i)
            qq = tools.common(n, m, small)
            #assert approx_equal(m,m_dc)
            #assert approx_equal(n,n_dc)

            if (qq.branch_0()): branch_0 += 1
            if (qq.branch_1()): branch_1 += 1
            if (qq.branch_1_1()): branch_1_1 += 1
            if (qq.branch_1_2()): branch_1_2 += 1
            if (qq.branch_1_2_1()): branch_1_2_1 += 1
            if (qq.branch_1_2_2()): branch_1_2_2 += 1
            if (qq.branch_1_2_3()): branch_1_2_3 += 1
            if (qq.branch_1_2_3_1()): branch_1_2_3_1 += 1
            if (qq.branch_1_2_3_2()): branch_1_2_3_2 += 1

            if (counter >= 10000):
                counter = 0
                print("." * 30, symbol)
                print("i= ", i, "out of ", trials)
                print("branch_0       = ", branch_0)
                print("branch_1       = ", branch_1)
                print("branch_1_1     = ", branch_1_1)
                print("branch_1_2     = ", branch_1_2)
                print("branch_1_2_1   = ", branch_1_2_1)
                print("branch_1_2_2   = ", branch_1_2_2)
                print("branch_1_2_3   = ", branch_1_2_3)
                print("branch_1_2_3_1 = ", branch_1_2_3_1)
                print("branch_1_2_3_2 = ", branch_1_2_3_2)
                sys.stdout.flush()
Esempio n. 18
0
def run(pdb_file_name,
        n_models,
        log,
        output_file_name_prefix,
        eps=1.e-7):
  pdb_inp = iotbx.pdb.input(file_name = pdb_file_name)
  pdb_hierarchy = pdb_inp.construct_hierarchy()
  asc = pdb_hierarchy.atom_selection_cache()
  cs = pdb_inp.crystal_symmetry_from_cryst1()
  tls_extract = mmtbx.tls.tools.tls_from_pdb_inp(
    remark_3_records = pdb_inp.extract_remark_iii_records(3),
    pdb_hierarchy    = pdb_hierarchy)
  for i_group, tls_params_one_group in enumerate(tls_extract.tls_params):
    selection = asc.selection(tls_params_one_group.selection_string)
    pdb_hierarchy_sel = pdb_hierarchy.select(selection)
    xrs = pdb_hierarchy_sel.extract_xray_structure(crystal_symmetry=cs)
    deg_to_rad_scale = math.pi/180
    # Units: T[A], L[deg**2], S[A*deg]
    T = matrix.sym(sym_mat3=tls_params_one_group.t)
    L = matrix.sym(sym_mat3=tls_params_one_group.l)
    S = matrix.sqr(tls_params_one_group.s)
    origin = tls_params_one_group.origin
    tlso = tools.tlso(
      t      = T.as_sym_mat3(),
      l      = L.as_sym_mat3(),
      s      = S,
      origin = origin)
    # sanity check
    if(not adptbx.is_positive_definite(tls_params_one_group.t, eps)):
      raise Sorry("T matrix is not positive definite.")
    if(not adptbx.is_positive_definite(tls_params_one_group.l, eps)):
      raise Sorry("L matrix is not positive definite.")
    r = analysis.run(T=T, L=L*(deg_to_rad_scale**2), S=S*deg_to_rad_scale,
      log=log).self_check()
    ensemble_generator_obj = ensemble_generator(
      tls_from_motions_object = r,
      pdb_hierarchy           = pdb_hierarchy_sel,
      xray_structure          = xrs,
      n_models                = n_models,
      origin                  = origin,
      log                     = log)
    ensemble_generator_obj.write_pdb_file(
      file_name=output_file_name_prefix+"_ensemble_%s.pdb"%str(i_group))
    # get U from TLS
    u_from_tls = tools.uaniso_from_tls_one_group(
      tlso          = tlso,
      sites_cart    = xrs.sites_cart(),
      zeroize_trace = False)
    # get U from ensemble
    pdb_hierarchy_from_tls = pdb_hierarchy_sel.deep_copy()
    pdb_hierarchy_from_ens = pdb_hierarchy_sel.deep_copy()
    u_from_ens = tools.u_cart_from_ensemble(
      models = ensemble_generator_obj.states.root.models())
    for i in xrange(xrs.sites_cart().size()):
      print "atom %d:"%i
      print "  Ucart(from TLS):", ["%8.5f"%u for u in u_from_tls[i]]
      print "  Ucart(from ens):", ["%8.5f"%u for u in u_from_ens[i]]
    #
    u1, u2 = u_from_tls.as_double(), u_from_ens.as_double()
    cc = flex.linear_correlation(x=u1, y=u2).coefficient()
    r = flex.sum(flex.abs(u1-u2))/\
        flex.sum(flex.abs(flex.abs(u1)+flex.abs(u2)))*2
    print "%6.4f %6.4f"%(cc, r)
    #
    pdb_hierarchy_from_tls.atoms().set_uij(u_from_tls)
    pdb_hierarchy_from_ens.atoms().set_uij(u_from_ens)
    pdb_hierarchy_from_tls.write_pdb_file(
      file_name = output_file_name_prefix+"_u_from_tls_%s.pdb"%str(i_group),
      crystal_symmetry = cs)
    pdb_hierarchy_from_ens.write_pdb_file(
      file_name = output_file_name_prefix+"_u_from_ensemble_%s.pdb"%str(i_group),
      crystal_symmetry = cs)
  return ensemble_generator_obj
Esempio n. 19
0
def exercise_branch_2_1(small = 1.e-9):
  m = [2,1,2,0,0,0]
  n = [2,1,1,0,0,0]
  counter = 0
  trials = 100000
  i = 0
  branch_0       = 0
  branch_1       = 0
  branch_1_1     = 0
  branch_1_2     = 0
  branch_1_2_1   = 0
  branch_1_2_2   = 0
  branch_1_2_3   = 0
  branch_1_2_3_1 = 0
  branch_1_2_3_2 = 0
  while i < trials:
      i += 1
      m = [2,1,2,0,0,0]
      n = [2,1,1,0,0,0]
      counter += 1
      c = scitbx.math.euler_angles_as_matrix(
                    [random.uniform(0,360) for j in xrange(3)], deg=True).elems
      r = random.random()
      if(r<0.25):
         m = adptbx.c_u_c_transpose(c, m)
         n = adptbx.c_u_c_transpose(c, n)
      elif(r>=0.25 and r < 0.5):
         m = adptbx.c_u_c_transpose(c, m)
      elif(r>=0.5 and r < 0.75):
         n = adptbx.c_u_c_transpose(c, n)
      else:
         r = random.random()
         run_away = 1000
         while run_away > 0:
            run_away -= 1
            r = random.random()
            if(r<0.33):
               m = adptbx.c_u_c_transpose(c, m)
               n = adptbx.c_u_c_transpose(c, n)
            elif(r>=0.33 and r < 0.66):
               m = adptbx.c_u_c_transpose(c, m)
            else:
               n = adptbx.c_u_c_transpose(c, n)
            m = adptbx.c_u_c_transpose(c, m)
            n = adptbx.c_u_c_transpose(c, n)
            m = [m[0],m[1],m[2],m[3]*random.choice((0,1)),m[4]*random.choice((0,1)),m[5]*random.choice((0,1))]
            n = [n[0],n[1],n[2],n[3]*random.choice((0,1)),n[4]*random.choice((0,1)),n[5]*random.choice((0,1))]
            m = factor(list(m), i)
            n = factor(list(n), i)
            if(adptbx.is_positive_definite(m,small) and
               adptbx.is_positive_definite(n,small)): break


      m_dc = copy.deepcopy(m)
      n_dc = copy.deepcopy(n)
      qq = tools.common(n,m,small)
      #assert approx_equal(m,m_dc)
      #assert approx_equal(n,n_dc)

      if(qq.branch_0()      ):   branch_0       += 1
      if(qq.branch_1()      ):   branch_1       += 1
      if(qq.branch_1_1()    ):   branch_1_1     += 1
      if(qq.branch_1_2()    ):   branch_1_2     += 1
      if(qq.branch_1_2_1()  ):   branch_1_2_1   += 1
      if(qq.branch_1_2_2()  ):   branch_1_2_2   += 1
      if(qq.branch_1_2_3()  ):   branch_1_2_3   += 1
      if(qq.branch_1_2_3_1()):   branch_1_2_3_1 += 1
      if(qq.branch_1_2_3_2()):   branch_1_2_3_2 += 1

      if (counter >= 10000):
         counter = 0
         print "."*30
         print "i= ", i, "out of ", trials
         print "branch_0       = ", branch_0
         print "branch_1       = ", branch_1
         print "branch_1_1     = ", branch_1_1
         print "branch_1_2     = ", branch_1_2
         print "branch_1_2_1   = ", branch_1_2_1
         print "branch_1_2_2   = ", branch_1_2_2
         print "branch_1_2_3   = ", branch_1_2_3
         print "branch_1_2_3_1 = ", branch_1_2_3_1
         print "branch_1_2_3_2 = ", branch_1_2_3_2
         sys.stdout.flush()
Esempio n. 20
0
def exercise(small = 1.e-9):
  for symbol in ["P 1"]:
      space_group_info = sgtbx.space_group_info(symbol = symbol)
      random_xray_structure = random_structure.xray_structure(
                                       space_group_info  = space_group_info,
                                       elements          = ["N"]*10,
                                       volume_per_atom   = 50.0,
                                       random_u_iso      = False,
                                       u_iso             = adptbx.b_as_u(20.0))
      sg = random_xray_structure.space_group()
      uc = random_xray_structure.unit_cell()
      print symbol, uc
      print
      sys.stdout.flush()
      counter = 0
      trials = 100000
      i = 0
      branch_0       = 0
      branch_1       = 0
      branch_1_1     = 0
      branch_1_2     = 0
      branch_1_2_1   = 0
      branch_1_2_2   = 0
      branch_1_2_3   = 0
      branch_1_2_3_1 = 0
      branch_1_2_3_2 = 0

      while i < trials:
          i += 1
          counter += 1
          r = random.random()
          if(r < 0.333):
             m = adptbx.random_u_cart(u_scale=20.*random.random(), u_min=0)
             n = adptbx.random_u_cart(u_scale=20.*random.random(), u_min=0)
             while 1:
               for ind in xrange(6):
                   r = random.random()
                   m = flex.double(m)
                   if(r > 0.5):
                      m[ind] = n[ind]
               m = list(m)
               if(adptbx.is_positive_definite(m,0) and
                  adptbx.is_positive_definite(n,0)): break
          elif(r>=0.333 and r<0.66):
             m,n = branch_3_mn()
          else:
             m,n = branch_2_mn(0)
          m_dc = copy.deepcopy(m)
          n_dc = copy.deepcopy(n)
          m = factor(list(m), i)
          n = factor(list(n), i)
          qq = tools.common(n,m,small)
          #assert approx_equal(m,m_dc)
          #assert approx_equal(n,n_dc)

          if(qq.branch_0()      ):   branch_0       += 1
          if(qq.branch_1()      ):   branch_1       += 1
          if(qq.branch_1_1()    ):   branch_1_1     += 1
          if(qq.branch_1_2()    ):   branch_1_2     += 1
          if(qq.branch_1_2_1()  ):   branch_1_2_1   += 1
          if(qq.branch_1_2_2()  ):   branch_1_2_2   += 1
          if(qq.branch_1_2_3()  ):   branch_1_2_3   += 1
          if(qq.branch_1_2_3_1()):   branch_1_2_3_1 += 1
          if(qq.branch_1_2_3_2()):   branch_1_2_3_2 += 1


          if (counter >= 10000):
             counter = 0
             print "."*30, symbol
             print "i= ", i, "out of ", trials
             print "branch_0       = ", branch_0
             print "branch_1       = ", branch_1
             print "branch_1_1     = ", branch_1_1
             print "branch_1_2     = ", branch_1_2
             print "branch_1_2_1   = ", branch_1_2_1
             print "branch_1_2_2   = ", branch_1_2_2
             print "branch_1_2_3   = ", branch_1_2_3
             print "branch_1_2_3_1 = ", branch_1_2_3_1
             print "branch_1_2_3_2 = ", branch_1_2_3_2
             sys.stdout.flush()
Esempio n. 21
0
def exercise_interface():
    episq = 8 * (math.pi**2)
    assert approx_equal(adptbx.u_as_b(2.3), 2.3 * episq)
    assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(2.3)), 2.3)
    u = (3, 4, 9, 2, 1, 7)
    assert approx_equal(adptbx.u_as_b(u), [x * episq for x in u])
    assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(u)), u)
    uc = uctbx.unit_cell((5, 4, 7, 80, 110, 100))
    for fw, bw in ((adptbx.u_cif_as_u_star, adptbx.u_star_as_u_cif),
                   (adptbx.u_cart_as_u_star, adptbx.u_star_as_u_cart),
                   (adptbx.u_cart_as_u_cif, adptbx.u_cif_as_u_cart),
                   (adptbx.u_cart_as_beta, adptbx.beta_as_u_cart),
                   (adptbx.u_cif_as_beta, adptbx.beta_as_u_cif)):
        assert approx_equal(bw(uc, fw(uc, u)), u)
    assert approx_equal(adptbx.beta_as_u_star(adptbx.u_star_as_beta(u)), u)
    assert approx_equal(adptbx.u_cart_as_u_iso(adptbx.u_iso_as_u_cart(2.3)),
                        2.3)
    for fw, bw in ((adptbx.u_iso_as_u_star, adptbx.u_star_as_u_iso),
                   (adptbx.u_iso_as_u_cif, adptbx.u_cif_as_u_iso),
                   (adptbx.u_iso_as_beta, adptbx.beta_as_u_iso)):
        assert approx_equal(bw(uc, fw(uc, 2.3)), 2.3)
    fc = adptbx.factor_u_cart_u_iso(u_cart=u)
    assert approx_equal(fc.u_iso, adptbx.u_cart_as_u_iso(u))
    assert approx_equal(fc.u_cart_minus_u_iso,
                        [uii - fc.u_iso for uii in u[:3]] + list(u[3:]))
    f = adptbx.factor_u_star_u_iso(unit_cell=uc,
                                   u_star=adptbx.u_cart_as_u_star(uc, u))
    assert approx_equal(f.u_iso, fc.u_iso)
    assert approx_equal(f.u_star_minus_u_iso,
                        adptbx.u_cart_as_u_star(uc, fc.u_cart_minus_u_iso))
    f = adptbx.factor_u_cif_u_iso(unit_cell=uc,
                                  u_cif=adptbx.u_cart_as_u_cif(uc, u))
    assert approx_equal(f.u_iso, fc.u_iso)
    assert approx_equal(f.u_cif_minus_u_iso,
                        adptbx.u_cart_as_u_cif(uc, fc.u_cart_minus_u_iso))
    f = adptbx.factor_beta_u_iso(unit_cell=uc,
                                 beta=adptbx.u_cart_as_beta(uc, u))
    assert approx_equal(f.u_iso, fc.u_iso)
    assert approx_equal(f.beta_minus_u_iso,
                        adptbx.u_cart_as_beta(uc, fc.u_cart_minus_u_iso))
    assert approx_equal(adptbx.debye_waller_factor_b_iso(0.25, 2.3),
                        math.exp(-2.3 * 0.25))
    assert approx_equal(adptbx.debye_waller_factor_u_iso(0.25, 2.3),
                        math.exp(-2.3 * episq * 0.25))
    assert approx_equal(
        adptbx.debye_waller_factor_b_iso(uc, (1, 2, 3), 2.3),
        adptbx.debye_waller_factor_u_iso(uc, (1, 2, 3), 2.3 / episq))
    u_star = adptbx.u_cart_as_u_star(uc, u)
    dw = adptbx.debye_waller_factor_u_star((1, 2, 3), u_star)
    assert approx_equal(
        dw,
        adptbx.debye_waller_factor_beta((1, 2, 3),
                                        adptbx.u_star_as_beta(u_star)))
    assert approx_equal(
        dw,
        adptbx.debye_waller_factor_u_cif(uc, (1, 2, 3),
                                         adptbx.u_star_as_u_cif(uc, u_star)))
    assert approx_equal(
        dw,
        adptbx.debye_waller_factor_u_cart(uc, (1, 2, 3),
                                          adptbx.u_star_as_u_cart(uc, u_star)))
    for e in adptbx.eigenvalues(u):
        check_eigenvalue(u, e)
    assert not adptbx.is_positive_definite(adptbx.eigenvalues(u))
    assert not adptbx.is_positive_definite(adptbx.eigenvalues(u), 0)
    assert adptbx.is_positive_definite(adptbx.eigenvalues(u), 1.22)
    assert not adptbx.is_positive_definite(u)
    assert not adptbx.is_positive_definite(u, 0)
    assert adptbx.is_positive_definite(u, 1.22)
    up = (0.534, 0.812, 0.613, 0.0166, 0.134, -0.0124)
    s = adptbx.eigensystem(up)
    assert approx_equal(s.values(), (0.813132, 0.713201, 0.432668))
    for i in xrange(3):
        check_eigenvector(up, s.values()[i], s.vectors(i))
    c = (1, 2, 3, 3, -4, 5, 4, 5, 6)
    v = (198, 18, 1020, 116, 447, 269)
    assert approx_equal(adptbx.c_u_c_transpose(c, u), v)
    assert approx_equal(
        adptbx.eigensystem(u).values(),
        (14.279201519086316, 2.9369143826320214, -1.2161159017183376))
    s = adptbx.eigensystem(up)
    try:
        s.vectors(4)
    except RuntimeError, e:
        assert str(e).endswith("Index out of range.")
Esempio n. 22
0
def exercise_interface():
    episq = 8 * (math.pi ** 2)
    assert approx_equal(adptbx.u_as_b(2.3), 2.3 * episq)
    assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(2.3)), 2.3)
    u = (3, 4, 9, 2, 1, 7)
    assert approx_equal(adptbx.u_as_b(u), [x * episq for x in u])
    assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(u)), u)
    uc = uctbx.unit_cell((5, 4, 7, 80, 110, 100))
    for fw, bw in (
        (adptbx.u_cif_as_u_star, adptbx.u_star_as_u_cif),
        (adptbx.u_cart_as_u_star, adptbx.u_star_as_u_cart),
        (adptbx.u_cart_as_u_cif, adptbx.u_cif_as_u_cart),
        (adptbx.u_cart_as_beta, adptbx.beta_as_u_cart),
        (adptbx.u_cif_as_beta, adptbx.beta_as_u_cif),
    ):
        assert approx_equal(bw(uc, fw(uc, u)), u)
    assert approx_equal(adptbx.beta_as_u_star(adptbx.u_star_as_beta(u)), u)
    assert approx_equal(adptbx.u_cart_as_u_iso(adptbx.u_iso_as_u_cart(2.3)), 2.3)
    for fw, bw in (
        (adptbx.u_iso_as_u_star, adptbx.u_star_as_u_iso),
        (adptbx.u_iso_as_u_cif, adptbx.u_cif_as_u_iso),
        (adptbx.u_iso_as_beta, adptbx.beta_as_u_iso),
    ):
        assert approx_equal(bw(uc, fw(uc, 2.3)), 2.3)
    fc = adptbx.factor_u_cart_u_iso(u_cart=u)
    assert approx_equal(fc.u_iso, adptbx.u_cart_as_u_iso(u))
    assert approx_equal(fc.u_cart_minus_u_iso, [uii - fc.u_iso for uii in u[:3]] + list(u[3:]))
    f = adptbx.factor_u_star_u_iso(unit_cell=uc, u_star=adptbx.u_cart_as_u_star(uc, u))
    assert approx_equal(f.u_iso, fc.u_iso)
    assert approx_equal(f.u_star_minus_u_iso, adptbx.u_cart_as_u_star(uc, fc.u_cart_minus_u_iso))
    f = adptbx.factor_u_cif_u_iso(unit_cell=uc, u_cif=adptbx.u_cart_as_u_cif(uc, u))
    assert approx_equal(f.u_iso, fc.u_iso)
    assert approx_equal(f.u_cif_minus_u_iso, adptbx.u_cart_as_u_cif(uc, fc.u_cart_minus_u_iso))
    f = adptbx.factor_beta_u_iso(unit_cell=uc, beta=adptbx.u_cart_as_beta(uc, u))
    assert approx_equal(f.u_iso, fc.u_iso)
    assert approx_equal(f.beta_minus_u_iso, adptbx.u_cart_as_beta(uc, fc.u_cart_minus_u_iso))
    assert approx_equal(adptbx.debye_waller_factor_b_iso(0.25, 2.3), math.exp(-2.3 * 0.25))
    assert approx_equal(adptbx.debye_waller_factor_u_iso(0.25, 2.3), math.exp(-2.3 * episq * 0.25))
    assert approx_equal(
        adptbx.debye_waller_factor_b_iso(uc, (1, 2, 3), 2.3),
        adptbx.debye_waller_factor_u_iso(uc, (1, 2, 3), 2.3 / episq),
    )
    u_star = adptbx.u_cart_as_u_star(uc, u)
    dw = adptbx.debye_waller_factor_u_star((1, 2, 3), u_star)
    assert approx_equal(dw, adptbx.debye_waller_factor_beta((1, 2, 3), adptbx.u_star_as_beta(u_star)))
    assert approx_equal(dw, adptbx.debye_waller_factor_u_cif(uc, (1, 2, 3), adptbx.u_star_as_u_cif(uc, u_star)))
    assert approx_equal(dw, adptbx.debye_waller_factor_u_cart(uc, (1, 2, 3), adptbx.u_star_as_u_cart(uc, u_star)))
    for e in adptbx.eigenvalues(u):
        check_eigenvalue(u, e)
    assert not adptbx.is_positive_definite(adptbx.eigenvalues(u))
    assert not adptbx.is_positive_definite(adptbx.eigenvalues(u), 0)
    assert adptbx.is_positive_definite(adptbx.eigenvalues(u), 1.22)
    assert not adptbx.is_positive_definite(u)
    assert not adptbx.is_positive_definite(u, 0)
    assert adptbx.is_positive_definite(u, 1.22)
    up = (0.534, 0.812, 0.613, 0.0166, 0.134, -0.0124)
    s = adptbx.eigensystem(up)
    assert approx_equal(s.values(), (0.813132, 0.713201, 0.432668))
    for i in xrange(3):
        check_eigenvector(up, s.values()[i], s.vectors(i))
    c = (1, 2, 3, 3, -4, 5, 4, 5, 6)
    v = (198, 18, 1020, 116, 447, 269)
    assert approx_equal(adptbx.c_u_c_transpose(c, u), v)
    assert approx_equal(adptbx.eigensystem(u).values(), (14.279201519086316, 2.9369143826320214, -1.2161159017183376))
    s = adptbx.eigensystem(up)
    try:
        s.vectors(4)
    except RuntimeError, e:
        assert str(e).endswith("Index out of range.")