Beispiel #1
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], )
def run(args):
    for file_name in args:
        print "File name:", file_name
        try:
            pdb_inp = iotbx.pdb.input(file_name=file_name)
        except KeyboardInterrupt:
            raise
        except Exception:
            libtbx.utils.format_exception()
        isotropic_b_factors = flex.double()
        all_eigenvalues = flex.double()
        for atom in pdb_inp.atoms():
            if (atom.uij == (-1, -1, -1, -1, -1, -1)):
                isotropic_b_factors.append(atom.b)
            else:
                all_eigenvalues.extend(
                    flex.double(adptbx.eigenvalues(atom.uij)))
        all_eigenvalues *= adptbx.u_as_b(1)
        print "Number of isotropic atoms:  ", isotropic_b_factors.size()
        print "Number of anisotropic atoms:", all_eigenvalues.size() // 3
        if (isotropic_b_factors.size() != 0):
            print "Histogram of isotropic B-factors:"
            flex.histogram(data=isotropic_b_factors,
                           n_slots=10).show(prefix="  ",
                                            format_cutoffs="%7.2f")
        if (all_eigenvalues.size() != 0):
            print "Histogram of eigenvalues of anisotropic B-factors:"
            flex.histogram(data=all_eigenvalues,
                           n_slots=10).show(prefix="  ",
                                            format_cutoffs="%7.2f")
        print
Beispiel #3
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],)
def run(args):
  for file_name in args:
    print "File name:", file_name
    try:
      pdb_inp = iotbx.pdb.input(file_name=file_name)
    except KeyboardInterrupt: raise
    except Exception:
      libtbx.utils.format_exception()
    isotropic_b_factors = flex.double()
    all_eigenvalues = flex.double()
    for atom in pdb_inp.atoms():
      if (atom.uij == (-1,-1,-1,-1,-1,-1)):
        isotropic_b_factors.append(atom.b)
      else:
        all_eigenvalues.extend(flex.double(adptbx.eigenvalues(atom.uij)))
    all_eigenvalues *= adptbx.u_as_b(1)
    print "Number of isotropic atoms:  ", isotropic_b_factors.size()
    print "Number of anisotropic atoms:", all_eigenvalues.size() // 3
    if (isotropic_b_factors.size() != 0):
      print "Histogram of isotropic B-factors:"
      flex.histogram(data=isotropic_b_factors, n_slots=10).show(
        prefix="  ", format_cutoffs="%7.2f")
    if (all_eigenvalues.size() != 0):
      print "Histogram of eigenvalues of anisotropic B-factors:"
      flex.histogram(data=all_eigenvalues, n_slots=10).show(
        prefix="  ", format_cutoffs="%7.2f")
    print
Beispiel #5
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
Beispiel #6
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
Beispiel #7
0
def check_u_cart(axis, u_cart):
    ev = adptbx.eigenvalues(u_cart)
    assert ev[0] > 0
    assert approx_equal(ev[1:], [0, 0])
    es = adptbx.eigensystem(u_cart)
    evs = es.vectors(0), es.vectors(1), es.vectors(2)
    a1 = math.acos((axis[0]*evs[0][0]+axis[1]*evs[0][1]+axis[2]*evs[0][2])/ \
      (evs[0][0]**2+evs[0][1]**2+evs[0][2]**2)) *180/math.pi
    a2 = math.acos((axis[0]*evs[1][0]+axis[1]*evs[1][1]+axis[2]*evs[1][2])/ \
      (evs[1][0]**2+evs[1][1]**2+evs[1][2]**2)) *180/math.pi
    a3 = math.acos((axis[0]*evs[2][0]+axis[1]*evs[2][1]+axis[2]*evs[2][2])/ \
      (evs[2][0]**2+evs[2][1]**2+evs[2][2]**2)) *180/math.pi
    assert approx_equal(a1, 90.)
Beispiel #8
0
def check_u_cart(axis, u_cart):
  ev = adptbx.eigenvalues(u_cart)
  assert ev[0] > 0
  assert approx_equal(ev[1:],[0,0])
  es = adptbx.eigensystem(u_cart)
  evs = es.vectors(0),es.vectors(1),es.vectors(2)
  a1 = math.acos((axis[0]*evs[0][0]+axis[1]*evs[0][1]+axis[2]*evs[0][2])/ \
    (evs[0][0]**2+evs[0][1]**2+evs[0][2]**2)) *180/math.pi
  a2 = math.acos((axis[0]*evs[1][0]+axis[1]*evs[1][1]+axis[2]*evs[1][2])/ \
    (evs[1][0]**2+evs[1][1]**2+evs[1][2]**2)) *180/math.pi
  a3 = math.acos((axis[0]*evs[2][0]+axis[1]*evs[2][1]+axis[2]*evs[2][2])/ \
    (evs[2][0]**2+evs[2][1]**2+evs[2][2]**2)) *180/math.pi
  assert approx_equal(a1, 90.)
 def random_modify_u_star(self, u_star, gauss_sigma,
                                max_relative_difference=1./3,
                                max_trials=100):
   for trial in xrange(max_trials):
     modified_u_star = []
     for i in xrange(len(u_star)):
       u = u_star[i]
       max_diff = u * max_relative_difference
       modified_u = random.gauss(u, gauss_sigma)
       if (modified_u - u > u + max_diff):
         modified_u = u + max_diff
       elif (u - modified_u > u + max_diff):
         modified_u = u - max_diff
       modified_u_star.append(modified_u)
     u_cart = adptbx.u_star_as_u_cart(self.unit_cell(), modified_u_star)
     eigenvalues = adptbx.eigenvalues(u_cart)
     if (min(eigenvalues) > 0.001):
       return modified_u_star
   raise RuntimeError, "Cannot find suitable u_star."
 def random_modify_u_star(self,
                          u_star,
                          gauss_sigma,
                          max_relative_difference=1. / 3,
                          max_trials=100):
     for trial in xrange(max_trials):
         modified_u_star = []
         for i in xrange(len(u_star)):
             u = u_star[i]
             max_diff = u * max_relative_difference
             modified_u = random.gauss(u, gauss_sigma)
             if (modified_u - u > u + max_diff):
                 modified_u = u + max_diff
             elif (u - modified_u > u + max_diff):
                 modified_u = u - max_diff
             modified_u_star.append(modified_u)
         u_cart = adptbx.u_star_as_u_cart(self.unit_cell(), modified_u_star)
         eigenvalues = adptbx.eigenvalues(u_cart)
         if (min(eigenvalues) > 0.001):
             return modified_u_star
     raise RuntimeError, "Cannot find suitable u_star."
Beispiel #11
0
def exercise_negative_parameters(verbose=0):
    structure_default = xray.structure(
        crystal_symmetry=crystal.symmetry(unit_cell=((10, 13, 17, 75, 80, 85)),
                                          space_group_symbol="P 1"),
        scatterers=flex.xray_scatterer(
            [xray.scatterer(label="C", site=(0, 0, 0), u=0.25)]))
    negative_gaussian = eltbx.xray_scattering.gaussian((1, 2), (2, 3), -4)
    for i_trial in range(7):
        structure = structure_default.deep_copy_scatterers()
        scatterer = structure.scatterers()[0]
        if (i_trial == 1):
            scatterer.occupancy *= -1
        elif (i_trial == 2):
            structure.scattering_type_registry(
                custom_dict={"C": negative_gaussian})
        elif (i_trial == 3):
            scatterer.u_iso *= -1
        elif (i_trial == 4):
            u_cart = adptbx.random_u_cart(u_scale=1, u_min=-1.1)
            assert max(adptbx.eigenvalues(u_cart)) < 0
            u_star = adptbx.u_cart_as_u_star(structure.unit_cell(), u_cart)
            scatterer.u_star = u_star
            scatterer.flags.set_use_u_aniso_only()
        elif (i_trial == 5):
            scatterer.fp = -10
        elif (i_trial == 6):
            scatterer.fp = -3
        f_direct = structure.structure_factors(d_min=1,
                                               algorithm="direct",
                                               cos_sin_table=False).f_calc()
        f_fft = structure.structure_factors(d_min=1,
                                            algorithm="fft",
                                            quality_factor=1.e8,
                                            wing_cutoff=1.e-10).f_calc()
        if (i_trial == 2):
            assert negative_gaussian.at_d_star_sq(
                f_fft.d_star_sq().data()).all_lt(0)
        if (i_trial in [5, 6]):
            f = structure.scattering_type_registry().gaussian_not_optional(
                scattering_type="C").at_d_star_sq(f_fft.d_star_sq().data())
            if (i_trial == 5):
                assert flex.max(f) + scatterer.fp < 0
            else:
                assert flex.max(f) + scatterer.fp > 0
                assert flex.min(f) + scatterer.fp < 0
        cc = flex.linear_correlation(abs(f_direct).data(),
                                     abs(f_fft).data()).coefficient()
        if (cc < 0.999):
            raise AssertionError("i_trial=%d, correlation=%.6g" %
                                 (i_trial, cc))
        elif (0 or verbose):
            print("correlation=%.6g" % cc)
        #
        # very simple test of gradient calculations with negative parameters
        structure_factor_gradients = \
          cctbx.xray.structure_factors.gradients(
            miller_set=f_direct,
            cos_sin_table=False)
        target_functor = xray.target_functors.intensity_correlation(
            f_obs=abs(f_direct))
        target_result = target_functor(f_fft, True)
        xray.set_scatterer_grad_flags(scatterers=structure.scatterers(),
                                      site=True,
                                      u_iso=True,
                                      u_aniso=True,
                                      occupancy=True,
                                      fp=True,
                                      fdp=True)
        for algorithm in ["direct", "fft"]:
            grads = structure_factor_gradients(
                xray_structure=structure,
                u_iso_refinable_params=None,
                miller_set=f_direct,
                d_target_d_f_calc=target_result.derivatives(),
                n_parameters=structure.n_parameters(),
                algorithm=algorithm).packed()
Beispiel #12
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)
def exercise_negative_parameters(verbose=0):
  structure_default = xray.structure(
    crystal_symmetry = crystal.symmetry(
      unit_cell=((10,13,17,75,80,85)),
      space_group_symbol="P 1"),
    scatterers=flex.xray_scatterer([
      xray.scatterer(label="C", site=(0,0,0), u=0.25)]))
  negative_gaussian = eltbx.xray_scattering.gaussian((1,2), (2,3), -4)
  for i_trial in xrange(7):
    structure = structure_default.deep_copy_scatterers()
    scatterer = structure.scatterers()[0]
    if (i_trial == 1):
      scatterer.occupancy *= -1
    elif (i_trial == 2):
      structure.scattering_type_registry(custom_dict={"C": negative_gaussian})
    elif (i_trial == 3):
      scatterer.u_iso *= -1
    elif (i_trial == 4):
      u_cart = adptbx.random_u_cart(u_scale=1, u_min=-1.1)
      assert max(adptbx.eigenvalues(u_cart)) < 0
      u_star = adptbx.u_cart_as_u_star(structure.unit_cell(), u_cart)
      scatterer.u_star = u_star
      scatterer.flags.set_use_u_aniso_only()
    elif (i_trial == 5):
      scatterer.fp = -10
    elif (i_trial == 6):
      scatterer.fp = -3
    f_direct = structure.structure_factors(
      d_min=1, algorithm="direct", cos_sin_table=False).f_calc()
    f_fft = structure.structure_factors(
      d_min=1, algorithm="fft",
      quality_factor=1.e8, wing_cutoff=1.e-10).f_calc()
    if (i_trial == 2):
      assert negative_gaussian.at_d_star_sq(f_fft.d_star_sq().data()).all_lt(0)
    if (i_trial in [5,6]):
      f = structure.scattering_type_registry().gaussian_not_optional(
        scattering_type="C").at_d_star_sq(f_fft.d_star_sq().data())
      if (i_trial == 5):
        assert flex.max(f) + scatterer.fp < 0
      else:
        assert flex.max(f) + scatterer.fp > 0
        assert flex.min(f) + scatterer.fp < 0
    cc = flex.linear_correlation(
      abs(f_direct).data(),
      abs(f_fft).data()).coefficient()
    if (cc < 0.999):
      raise AssertionError("i_trial=%d, correlation=%.6g" % (i_trial, cc))
    elif (0 or verbose):
      print "correlation=%.6g" % cc
    #
    # very simple test of gradient calculations with negative parameters
    structure_factor_gradients = \
      cctbx.xray.structure_factors.gradients(
        miller_set=f_direct,
        cos_sin_table=False)
    target_functor = xray.target_functors.intensity_correlation(
      f_obs=abs(f_direct))
    target_result = target_functor(f_fft, True)
    xray.set_scatterer_grad_flags(scatterers = structure.scatterers(),
                                  site       = True,
                                  u_iso      = True,
                                  u_aniso    = True,
                                  occupancy  = True,
                                  fp         = True,
                                  fdp        = True)
    for algorithm in ["direct", "fft"]:
      grads = structure_factor_gradients(
        xray_structure=structure,
        u_iso_refinable_params=None,
        miller_set=f_direct,
        d_target_d_f_calc=target_result.derivatives(),
        n_parameters = structure.n_parameters(),
        algorithm=algorithm).packed()
Beispiel #14
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.")
Beispiel #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.")
 def build_scatterers(self,
                      elements,
                      sites_frac=None,
                      grid=None,
                      t_centre_of_inversion=None):
     existing_sites = [scatterer.site for scatterer in self.scatterers()]
     if (sites_frac is None):
         all_sites = random_sites(
             special_position_settings=self,
             existing_sites=existing_sites,
             n_new=len(elements),
             min_hetero_distance=self.min_distance,
             general_positions_only=self.general_positions_only,
             grid=grid,
             t_centre_of_inversion=t_centre_of_inversion)
     else:
         assert len(sites_frac) == len(elements)
         all_sites = existing_sites + list(sites_frac)
     assert len(all_sites) <= self.n_scatterers
     sf_dict = {}
     for element in elements:
         if (not sf_dict.has_key(element)):
             sf_dict[element] = eltbx.xray_scattering.best_approximation(
                 element)
     fp = 0
     fdp = 0
     n_existing = self.scatterers().size()
     i_label = n_existing
     for element, site in zip(elements, all_sites[n_existing:]):
         i_label += 1
         scatterer = xray.scatterer(label=element + str(i_label),
                                    scattering_type=element,
                                    site=site)
         site_symmetry = scatterer.apply_symmetry(
             self.unit_cell(), self.space_group(),
             self.min_distance_sym_equiv())
         if (self.random_f_prime_d_min):
             f0 = sf_dict[element].at_d_star_sq(
                 1. / self.random_f_prime_d_min**2)
             assert f0 > 0
             fp = -f0 * random.random() * self.random_f_prime_scale
         if (self.random_f_double_prime):
             f0 = sf_dict[element].at_d_star_sq(0)
             fdp = f0 * random.random() * self.random_f_double_prime_scale
         scatterer.fp = fp
         scatterer.fdp = fdp
         if (self.use_u_iso_):
             scatterer.flags.set_use_u_iso_only()
             u_iso = self.u_iso
             if (not u_iso and self.random_u_iso):
                 u_iso = random.random() * self.random_u_iso_scale \
                       + self.random_u_iso_min
             scatterer.u_iso = u_iso
         if (self.use_u_aniso):
             scatterer.flags.set_use_u_aniso_only()
             run_away_counter = 0
             while 1:
                 run_away_counter += 1
                 assert run_away_counter < 100
                 u_cart = adptbx.random_u_cart(
                     u_scale=self.random_u_cart_scale)
                 scatterer.u_star = site_symmetry.average_u_star(
                     adptbx.u_cart_as_u_star(self.unit_cell(), u_cart))
                 u_cart = adptbx.u_star_as_u_cart(self.unit_cell(),
                                                  scatterer.u_star)
                 eigenvalues = adptbx.eigenvalues(u_cart)
                 if (min(eigenvalues) > 0.001):
                     break
         if (self.random_occupancy):
             scatterer.occupancy = self.random_occupancy_min \
                                 + (1-self.random_occupancy_min)*random.random()
         self.add_scatterer(scatterer)
 def build_scatterers(self,
       elements,
       sites_frac=None,
       grid=None,
       t_centre_of_inversion=None):
   existing_sites = [scatterer.site for scatterer in self.scatterers()]
   if (sites_frac is None):
     all_sites = random_sites(
       special_position_settings=self,
       existing_sites=existing_sites,
       n_new=len(elements),
       min_hetero_distance=self.min_distance,
       general_positions_only=self.general_positions_only,
       grid=grid,
       t_centre_of_inversion=t_centre_of_inversion)
   else:
     assert len(sites_frac) == len(elements)
     all_sites = existing_sites + list(sites_frac)
   assert len(all_sites) <= self.n_scatterers
   sf_dict = {}
   for element in elements:
     if (not sf_dict.has_key(element)):
       sf_dict[element] = eltbx.xray_scattering.best_approximation(element)
   fp = 0
   fdp = 0
   n_existing = self.scatterers().size()
   i_label = n_existing
   for element,site in zip(elements, all_sites[n_existing:]):
     i_label += 1
     scatterer = xray.scatterer(
       label=element + str(i_label),
       scattering_type=element,
       site=site)
     site_symmetry = scatterer.apply_symmetry(
       self.unit_cell(),
       self.space_group(),
       self.min_distance_sym_equiv())
     if (self.random_f_prime_d_min):
       f0 = sf_dict[element].at_d_star_sq(1./self.random_f_prime_d_min**2)
       assert f0 > 0
       fp = -f0 * random.random() * self.random_f_prime_scale
     if (self.random_f_double_prime):
       f0 = sf_dict[element].at_d_star_sq(0)
       fdp = f0 * random.random() * self.random_f_double_prime_scale
     scatterer.fp = fp
     scatterer.fdp = fdp
     if (self.use_u_iso_):
       scatterer.flags.set_use_u_iso_only()
       u_iso = self.u_iso
       if (not u_iso and self.random_u_iso):
         u_iso = random.random() * self.random_u_iso_scale \
               + self.random_u_iso_min
       scatterer.u_iso = u_iso
     if (self.use_u_aniso):
       scatterer.flags.set_use_u_aniso_only()
       run_away_counter = 0
       while 1:
         run_away_counter += 1
         assert run_away_counter < 100
         u_cart = adptbx.random_u_cart(u_scale=self.random_u_cart_scale)
         scatterer.u_star = site_symmetry.average_u_star(
                              adptbx.u_cart_as_u_star(
                                self.unit_cell(), u_cart))
         u_cart = adptbx.u_star_as_u_cart(self.unit_cell(), scatterer.u_star)
         eigenvalues = adptbx.eigenvalues(u_cart)
         if (min(eigenvalues) > 0.001):
           break
     if (self.random_occupancy):
       scatterer.occupancy = self.random_occupancy_min \
                           + (1-self.random_occupancy_min)*random.random()
     self.add_scatterer(scatterer)