Пример #1
0
def exercise_savitzky_golay_smoothing():

    plot = False

    def rms(flex_double):
        return math.sqrt(flex.mean(flex.pow2(flex_double)))

    for sigma_frac in (0.005, 0.01, 0.05, 0.1):
        mean = random.randint(-5, 5)
        scale = flex.random_double() * 10
        sigma = flex.random_double() * 5 + 1
        gaussian = scitbx.math.curve_fitting.gaussian(scale, mean, sigma)

        x = flex.double(frange(-20, 20, 0.1))
        y = gaussian(x)
        rand_norm = scitbx.random.normal_distribution(mean=0,
                                                      sigma=sigma_frac *
                                                      flex.max_absolute(y))
        g = scitbx.random.variate(rand_norm)
        noise = g(y.size())
        y_noisy = y + noise
        # according to numerical recipes the best results are obtained where the
        # full window width is between 1 and 2 times the number of points at fwhm
        # for polynomials of degree 4
        half_window = int(round(0.5 * 2.355 * sigma * 10))
        y_filtered = savitzky_golay_filter(x,
                                           y_noisy,
                                           half_window=half_window,
                                           degree=4)[1]
        extracted_noise = y_noisy - y_filtered
        rms_noise = rms(noise)
        rms_extracted_noise = rms(extracted_noise)

        assert is_below_limit(value=abs(rand_norm.sigma - rms_noise) /
                              rand_norm.sigma,
                              limit=0.15)
        assert is_below_limit(
            value=abs(rand_norm.sigma - rms_extracted_noise) / rand_norm.sigma,
            limit=0.15)

        diff = y_filtered - y
        assert is_below_limit(value=(rms(diff) / rand_norm.sigma), limit=0.4)

        if plot:
            from matplotlib import pyplot
            pyplot.plot(x, y)
            pyplot.plot(x, noise)
            pyplot.scatter(x, y_noisy, marker="x")
            pyplot.plot(x, y_filtered)
            pyplot.show()
            pyplot.plot(x, extracted_noise)
            pyplot.plot(x, noise)
            pyplot.show()

    return
Пример #2
0
def exercise_savitzky_golay_smoothing():

  plot = False

  def rms(flex_double):
    return math.sqrt(flex.mean(flex.pow2(flex_double)))

  for sigma_frac in (0.005, 0.01, 0.05, 0.1):
    mean = random.randint(-5,5)
    scale = flex.random_double() * 10
    sigma = flex.random_double() * 5 + 1
    gaussian = curve_fitting.gaussian(scale, mean, sigma)

    x = flex.double(frange(-20,20,0.1))
    y = gaussian(x)
    rand_norm = scitbx.random.normal_distribution(
      mean=0, sigma=sigma_frac*flex.max_absolute(y))
    g = scitbx.random.variate(rand_norm)
    noise = g(y.size())
    y_noisy = y + noise
    # according to numerical recipes the best results are obtained where the
    # full window width is between 1 and 2 times the number of points at fwhm
    # for polynomials of degree 4
    half_window = int(round(0.5 * 2.355 * sigma * 10))
    y_filtered = savitzky_golay_filter(x, y_noisy, half_window=half_window, degree=4)[1]
    extracted_noise = y_noisy - y_filtered
    rms_noise = rms(noise)
    rms_extracted_noise = rms(extracted_noise)

    assert is_below_limit(
      value=abs(rand_norm.sigma - rms_noise)/rand_norm.sigma,
      limit=0.15)
    assert is_below_limit(
      value=abs(rand_norm.sigma - rms_extracted_noise)/rand_norm.sigma,
      limit=0.15)

    diff = y_filtered - y
    assert is_below_limit(
      value=(rms(diff)/ rand_norm.sigma),
      limit=0.4)

    if plot:
      from matplotlib import pyplot
      pyplot.plot(x, y)
      pyplot.plot(x, noise)
      pyplot.scatter(x, y_noisy, marker="x")
      pyplot.plot(x, y_filtered)
      pyplot.show()
      pyplot.plot(x, extracted_noise)
      pyplot.plot(x, noise)
      pyplot.show()

  return
Пример #3
0
 def lbfgs(sites_cart):
   for i_lbfgs_restart in xrange(3):
     minimized = cctbx.geometry_restraints.lbfgs.lbfgs(
       sites_cart=sites_cart,
       geometry_restraints_manager=geo_manager)
     assert is_below_limit(value=minimized.final_target_value, limit=1e-10)
   return minimized
 def check(center, radii, rotation):
     a, b, c = radii
     points_principal = flex.vec3_double([(-a, 0, 0), (a, 0, 0), (0, -b, 0),
                                          (0, b, 0), (0, 0, -c), (0, 0, c)])
     points = rotation * points_principal + center
     mce = mce_compute(points)
     assert approx_equal(mce.center, center)
     assert approx_equal(sorted(mce.radii), sorted(radii))
     assert approx_equal(mce.rotation.determinant(), 1)
     points_mce = mce.rotation.inverse().elems * (points - mce.center)
     rms = []
     for r in point_group_432_rotation_matrices:
         rp = r * points_mce
         rms.append(rp.rms_difference(points_principal))
     assert is_below_limit(value=min(rms), limit=1e-8, eps=0)
 def check(center, radii, rotation):
   a,b,c = radii
   points_principal = flex.vec3_double([
     (-a,0,0),
     (a,0,0),
     (0,-b,0),
     (0,b,0),
     (0,0,-c),
     (0,0,c)])
   points = rotation * points_principal + center
   mce = mce_compute(points)
   assert approx_equal(mce.center, center)
   assert approx_equal(sorted(mce.radii), sorted(radii))
   assert approx_equal(mce.rotation.determinant(), 1)
   points_mce = mce.rotation.inverse().elems * (points - mce.center)
   rms = []
   for r in point_group_432_rotation_matrices:
     rp = r * points_mce
     rms.append(rp.rms_difference(points_principal))
   assert is_below_limit(value=min(rms), limit=1e-8, eps=0)
Пример #6
0
def run_shelx76(titl, xray_structure, f_obs):
  write_shelx76_ls(f_obs, xray_structure, titl)
  shelx_out = easy_run.fully_buffered(command="shelx76 < tmp.ins") \
    .raise_if_errors() \
    .stdout_lines
  reflections_key = "REFLEXIONS READ, OF WHICH"
  residuals_key = "RESIDUALS BEFORE CYCLE   1 FOR"
  r = None
  lines = iter(shelx_out)
  for line in lines:
    if (line.find(reflections_key) >= 0):
      flds = line.split()
      assert len(flds) == 7
      assert flds[6] == "REJECTED"
      assert flds[5] == "0"
    elif (line.find(residuals_key) >= 0):
      assert len(lines.next().strip()) == 0
      flds = lines.next().split()
      assert len(flds) == 12
      r = float(flds[2])
  if (r is None):
    raise RuntimeError("Not found in shelx76 output: %s" % residuals_key)
  assert is_below_limit(value=r, limit=0.005)
Пример #7
0
def compare_masks(struc, opts):
  tolerance = opts.tolerance
  resolution = opts.resolution
  solvent_radius = opts.solvent_radius
  shrink_radius = opts.shrink_radius
  verbose = opts.verbose
  cout.truncate(0)
  time_p1 = 0.0
  time_asu = 0.0
  time_orig = 0.0
  params = masks.mask_master_params.extract()
  params.ignore_hydrogens = False
  params.ignore_zero_occupancy_atoms = False
  params.solvent_radius = solvent_radius
  params.shrink_truncation_radius = shrink_radius
  fc = struc.structure_factors(d_min = resolution).f_calc()
  while fc.data().size() <= 3 :
    resolution /= 1.2
    assert resolution > 1.0E-3
    fc = struc.structure_factors( d_min = resolution).f_calc()
  print >>cout, "Resolution= ", resolution, "  solvent radius= ", \
      solvent_radius, "  shrink radius= ", shrink_radius,  "  Tolerance= ", \
      tolerance, "  Number of reflection= ", fc.data().size()
  struc.show_summary(cout)
  group = struc.space_group()
  print >>cout, "Cell volume= ", struc.unit_cell().volume(), \
    "  Group order= ", group.order_z(), " p= ", group.order_p()
  print >>cout, "Hall  symbol: ", group.type().hall_symbol()
  #check_group(group)

  tb = time.time()
  asu_mask = masks.atom_mask(
      unit_cell = struc.unit_cell(),
      group = struc.space_group(),
      resolution = fc.d_min(),
      grid_step_factor = params.grid_step_factor,
      solvent_radius = params.solvent_radius,
      shrink_truncation_radius = params.shrink_truncation_radius )
  te = time.time()
  time_asu += (te-tb)
  grid =  asu_mask.grid_size()
  print >>cout, "asu mask grid = ", grid
  zero_test(asu_mask, fc, tolerance = tolerance)
  radii = get_radii(struc)
  assert len(radii) == len(struc.sites_frac())
  tb = time.time()
  asu_mask.compute( struc.sites_frac(), radii )
  te = time.time()
  time_asu += (te-tb)
  print >>cout, "   n asu atoms= ", asu_mask.n_asu_atoms(), \
      "   has-enclosed= ", asu_mask.debug_has_enclosed_box
  tb = time.time()
  fm_asu = asu_mask.structure_factors( fc.indices() )
  fm_asu = fc.set().array( data = fm_asu )
  te = time.time()
  time_asu_sf = te-tb
  time_asu += (te-tb)
  # save files
  if not opts.save_files is None:
    tmp_file = open(opts.save_files + ".pdb", "w")
    print >>tmp_file, struc.as_pdb_file()
    tmp_file.close()
    asu_mask.xplor_write_map(opts.save_files + "_mask.map")
    asu_mask.xplor_write_map(opts.save_files + "_inverted_mask.map", 1, True)
    # also save structure factors
    import iotbx.mtz
    mtzo = iotbx.mtz.object()
    mtzo.set_title("mask test")
    mtzo.add_history(line="start")
    mtzo.set_space_group_info(fm_asu.space_group_info())
    mtzo.set_hkl_base(fm_asu.unit_cell())
    crystal = mtzo.add_crystal(
      name="mask_test_crystal",
      project_name="mask_test_project",
      unit_cell=fm_asu.unit_cell())
    dataset = crystal.add_dataset(
      name="mask_test_dataset",
      wavelength=1)
    assert dataset.add_miller_array(
      miller_array=fm_asu,
      column_root_label="F",
      #column_types=column_types
      ) is dataset
    mtzo.add_history(line="done")
    mtzo.write(opts.save_files + "_sf.mtz")
  #
  # ========= old mask =============
  #
  tb = time.time()
  struc_p1 = struc.expand_to_p1()
  te = time.time()
  time_p1_exp = (te-tb)
  time_p1 += (te-tb)
  fc_p1 = fc.deep_copy()
  fc_p1 = fc_p1.customized_copy(crystal_symmetry = struc_p1.crystal_symmetry())
  tb = time.time()
  blk_p1 = masks.bulk_solvent(
    xray_structure = struc_p1,
    gridding_n_real = grid,
    ignore_zero_occupancy_atoms = params.ignore_zero_occupancy_atoms,
    ignore_hydrogen_atoms = params.ignore_hydrogens,
    solvent_radius = params.solvent_radius,
    shrink_truncation_radius = params.shrink_truncation_radius)
  te = time.time()
  time_p1_msk = (te-tb)
  time_p1 += (te-tb)
  tb = time.time()
  fm_p1 = blk_p1.structure_factors( miller_set = fc_p1 )
  te = time.time()
  time_p1_sf = (te-tb)
  time_p1 += (te-tb)
  blk_p1.show_summary(cout)
  ### original mask
  tb = time.time()
  blk_o = masks.bulk_solvent(
    xray_structure = struc,
    gridding_n_real = grid,
    ignore_zero_occupancy_atoms = params.ignore_zero_occupancy_atoms,
    ignore_hydrogen_atoms = params.ignore_hydrogens,
    solvent_radius = params.solvent_radius,
    shrink_truncation_radius = params.shrink_truncation_radius)
  te = time.time()
  time_orig_msk = (te-tb)
  time_orig += (te-tb)
  tb = time.time()
  fm_o = blk_o.structure_factors( miller_set = fc )
  te = time.time()
  time_orig_sf = (te-tb)
  time_orig += (te-tb)
  print >>cout, "Number of reflections ::: Fm asu = ", fm_asu.data().size(), \
    "Fm P1 = ", fm_p1.data().size()
  print >>cout, "Time ( ms )    P1= ", time_p1*1000.0, "   orig= ", \
      time_orig*1000.0, "    asu= ", time_asu*1000.0
  print >>cout, "Times ( ms ) mask_asu= ", asu_mask.debug_mask_asu_time, \
      " atoms_to_asu= ", asu_mask.debug_atoms_to_asu_time, \
      " accessible= ", asu_mask.debug_accessible_time, \
      " contact= ", asu_mask.debug_contact_time, \
      " Fc= ", time_asu_sf*1000.0, \
      " fft= ", asu_mask.debug_fft_time
  print >>cout, "Times ( ms ) orig:  mask= ", time_orig_msk*1000.0, "  Fc=", \
      time_orig_sf*1000.0
  print >>cout, "Times ( ms ) p1 :  expand= ", time_p1_exp*1000.0, "  mask= ", \
      time_p1_msk*1000.0, "  Fc=", time_p1_sf*1000.0
  assert fm_asu.data().size() == fm_o.data().size()
  t_v1 = asu_mask.contact_surface_fraction
  t_v2 = blk_p1.contact_surface_fraction
  t_v3 = max( abs(t_v1), abs(t_v2) )
  if t_v3 > 1.0E-6:
    t_v4 = abs( t_v1 - t_v2 ) / t_v3
  else:
    t_v4 = 0.0
  if( t_v4>1.0E-6 ):
    if not opts.failed_file is None:
      tmp_file = open(opts.failed_file, "w")
      print >>tmp_file, struc.as_pdb_file()
      tmp_file.close()
    raise "Not equal solvent volume"

  assert approx_equal(
    asu_mask.contact_surface_fraction, blk_p1.contact_surface_fraction)
  assert approx_equal(
    asu_mask.accessible_surface_fraction, blk_p1.accessible_surface_fraction)
  assert is_below_limit(
    value=asu_mask.accessible_surface_fraction,
    limit=asu_mask.contact_surface_fraction)
  n_compared = compare_fc(fm_asu, fm_p1, tolerance = tolerance)
  assert n_compared == fm_asu.data().size(), \
    "N compared refls: "+str(n_compared) + " != " + str(fm_asu.data().size())
  assert n_compared >0
  if verbose:
    print cout.getvalue()
  # test that second calculation will produce the same results
  asu_mask.compute( struc.sites_frac(), radii )
  fm_asu2 = asu_mask.structure_factors( fc.indices() )
  fm_asu2 = fc.set().array( data = fm_asu2 )
  n_compared = compare_fc(fm_asu, fm_asu2, tolerance = tolerance)
  assert n_compared == fm_asu.data().size(), \
    "N compared refls: "+str(n_compared) + " != " + str(fm_asu.data().size())
  cout.truncate(0)
Пример #8
0
def exercise(verbose=0):
    distance_ideal = 1.8
    default_vdw_distance = 3.6
    vdw_1_4_factor = 3.5 / 3.6
    sites_cart_manual = flex.vec3_double([(1, 3, 0), (2, 3, 0), (3, 2, 0),
                                          (3, 1, 0), (4, 1, 0), (3, 4, 0),
                                          (4, 3, 0), (5, 3, 0), (6, 2, 0),
                                          (7, 2, 0), (8, 3, 0), (7, 4, 0),
                                          (6, 4, 0), (7, 5, 0), (6, 6, 0),
                                          (8, 6, 0)])
    bond_proxies = geometry_restraints.bond_sorted_asu_proxies(
        asu_mappings=None)
    for i_seqs in [(0, 1), (1, 2), (2, 3), (3, 4), (1, 5), (2, 6), (5, 6),
                   (6, 7), (7, 8), (8, 9), (9, 10), (10, 11), (11, 12),
                   (12, 7), (11, 13), (13, 14), (14, 15), (15, 13)]:
        bond_proxies.process(
            geometry_restraints.bond_simple_proxy(
                i_seqs=i_seqs, distance_ideal=distance_ideal, weight=100))
    angle_proxies = geometry_restraints.shared_angle_proxy()
    for i_seqs, angle_ideal in [[(0, 1, 2), 135], [(0, 1, 5), 135],
                                [(1, 2, 3), 135], [(3, 2, 6), 135],
                                [(2, 3, 4), 120], [(1, 2, 6), 90],
                                [(2, 6, 5), 90], [(6, 5, 1), 90],
                                [(5, 1, 2), 90], [(2, 6, 7), 135],
                                [(5, 6, 7), 135], [(6, 7, 8), 120],
                                [(6, 7, 12), 120], [(7, 8, 9), 120],
                                [(8, 9, 10), 120], [(9, 10, 11), 120],
                                [(10, 11, 12), 120], [(11, 12, 7), 120],
                                [(12, 7, 8), 120], [(10, 11, 13), 120],
                                [(12, 11, 13), 120], [(11, 13, 15), 150],
                                [(11, 13, 14), 150], [(13, 15, 14), 60],
                                [(15, 14, 13), 60], [(14, 13, 15), 60]]:
        angle_proxies.append(
            geometry_restraints.angle_proxy(i_seqs=i_seqs,
                                            angle_ideal=angle_ideal,
                                            weight=1))
    if (0 or verbose):
        dump_pdb(file_name="manual.pdb", sites_cart=sites_cart_manual)
    for traditional_convergence_test in [True, False]:
        for sites_cart_selection in [True, False]:
            sites_cart = sites_cart_manual.deep_copy()
            if sites_cart_selection:
                sites_cart_selection = flex.bool(sites_cart.size(), True)
                sites_cart_selection[1] = False
            assert bond_proxies.asu.size() == 0
            bond_params_table = geometry_restraints.extract_bond_params(
                n_seq=sites_cart.size(),
                bond_simple_proxies=bond_proxies.simple)
            manager = geometry_restraints.manager.manager(
                bond_params_table=bond_params_table,
                angle_proxies=angle_proxies)
            minimized = geometry_restraints.lbfgs.lbfgs(
                sites_cart=sites_cart,
                geometry_restraints_manager=manager,
                lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                    traditional_convergence_test=traditional_convergence_test,
                    drop_convergence_test_max_drop_eps=1.e-20,
                    drop_convergence_test_iteration_coefficient=1,
                    max_iterations=1000),
                sites_cart_selection=sites_cart_selection,
            )
            assert minimized.minimizer.iter() > 100
            sites_cart_minimized_1 = sites_cart.deep_copy()
            if (0 or verbose):
                dump_pdb(file_name="minimized_1.pdb",
                         sites_cart=sites_cart_minimized_1)
            bond_deltas = geometry_restraints.bond_deltas(
                sites_cart=sites_cart_minimized_1, proxies=bond_proxies.simple)
            angle_deltas = geometry_restraints.angle_deltas(
                sites_cart=sites_cart_minimized_1, proxies=angle_proxies)
            if (0 or verbose):
                for proxy, delta in zip(bond_proxies.simple, bond_deltas):
                    print "bond:", proxy.i_seqs, delta
                for proxy, delta in zip(angle_proxies, angle_deltas):
                    print "angle:", proxy.i_seqs, delta
            assert is_below_limit(value=flex.max(flex.abs(bond_deltas)),
                                  limit=0,
                                  eps=1.e-6)
            assert is_below_limit(value=flex.max(flex.abs(angle_deltas)),
                                  limit=0,
                                  eps=2.e-6)
    sites_cart += matrix.col((1, 1, 0)) - matrix.col(sites_cart.min())
    unit_cell_lengths = list(
        matrix.col(sites_cart.max()) + matrix.col((1, -1.2, 4)))
    unit_cell_lengths[1] *= 2
    unit_cell_lengths[2] *= 2
    xray_structure = xray.structure(crystal_symmetry=crystal.symmetry(
        unit_cell=unit_cell_lengths, space_group_symbol="P112"))
    for serial, site in zip(count(1), sites_cart):
        xray_structure.add_scatterer(
            xray.scatterer(
                label="C%02d" % serial,
                site=xray_structure.unit_cell().fractionalize(site)))
    if (0 or verbose):
        xray_structure.show_summary().show_scatterers()
    p1_structure = (xray_structure.apply_shift(
        (-.5, -.5, 0)).expand_to_p1().apply_shift((.5, .5, 0)))
    for shift in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
        p1_structure.add_scatterers(
            p1_structure.apply_shift(shift).scatterers())
    if (0 or verbose):
        open("p1_structure.pdb", "w").write(p1_structure.as_pdb_file())
    nonbonded_cutoff = 6.5
    asu_mappings = xray_structure.asu_mappings(
        buffer_thickness=nonbonded_cutoff)
    bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
    geometry_restraints.add_pairs(bond_asu_table, bond_proxies.simple)
    shell_asu_tables = crystal.coordination_sequences.shell_asu_tables(
        pair_asu_table=bond_asu_table, max_shell=3)
    shell_sym_tables = [
        shell_asu_table.extract_pair_sym_table()
        for shell_asu_table in shell_asu_tables
    ]
    bond_params_table = geometry_restraints.extract_bond_params(
        n_seq=sites_cart.size(), bond_simple_proxies=bond_proxies.simple)
    atom_energy_types = flex.std_string(sites_cart.size(), "Default")
    nonbonded_params = geometry_restraints.nonbonded_params(
        factor_1_4_interactions=vdw_1_4_factor,
        const_shrink_1_4_interactions=0,
        default_distance=default_vdw_distance)
    nonbonded_params.distance_table.setdefault(
        "Default")["Default"] = default_vdw_distance
    pair_proxies = geometry_restraints.pair_proxies(
        bond_params_table=bond_params_table,
        shell_asu_tables=shell_asu_tables,
        model_indices=None,
        conformer_indices=None,
        nonbonded_params=nonbonded_params,
        nonbonded_types=atom_energy_types,
        nonbonded_distance_cutoff_plus_buffer=nonbonded_cutoff)
    if (0 or verbose):
        print "pair_proxies.bond_proxies.n_total():", \
               pair_proxies.bond_proxies.n_total(),
        print "simple:", pair_proxies.bond_proxies.simple.size(),
        print "sym:", pair_proxies.bond_proxies.asu.size()
        print "pair_proxies.nonbonded_proxies.n_total():", \
               pair_proxies.nonbonded_proxies.n_total(),
        print "simple:", pair_proxies.nonbonded_proxies.simple.size(),
        print "sym:", pair_proxies.nonbonded_proxies.asu.size()
        print "min_distance_nonbonded: %.2f" % flex.min(
            geometry_restraints.nonbonded_deltas(
                sites_cart=sites_cart,
                sorted_asu_proxies=pair_proxies.nonbonded_proxies))
    s = StringIO()
    pair_proxies.bond_proxies.show_histogram_of_model_distances(
        sites_cart=sites_cart, f=s, prefix="[]")
    assert s.getvalue().splitlines()[0] == "[]Histogram of bond lengths:"
    assert s.getvalue().splitlines()[5].startswith("[]      1.80 -     1.80:")
    s = StringIO()
    pair_proxies.bond_proxies.show_histogram_of_deltas(sites_cart=sites_cart,
                                                       f=s,
                                                       prefix="][")
    assert s.getvalue().splitlines()[0] == "][Histogram of bond deltas:"
    assert s.getvalue().splitlines()[5].startswith("][     0.000 -    0.000:")
    s = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="residual",
                                          sites_cart=sites_cart,
                                          max_items=3,
                                          f=s,
                                          prefix=":;")
    l = s.getvalue().splitlines()
    assert l[0] == ":;Bond restraints: 18"
    assert l[1] == ":;Sorted by residual:"
    assert l[2].startswith(":;bond ")
    assert l[3].startswith(":;     ")
    assert l[4] == ":;  ideal  model  delta    sigma   weight residual"
    for i in [5, -2]:
        assert l[i].startswith(":;  1.800  1.800 ")
    assert l[-1] == ":;... (remaining 15 not shown)"
    s = StringIO()
    pair_proxies.nonbonded_proxies.show_histogram_of_model_distances(
        sites_cart=sites_cart, f=s, prefix="]^")
    assert not show_diff(
        s.getvalue(), """\
]^Histogram of nonbonded interaction distances:
]^      2.16 -     3.03: 3
]^      3.03 -     3.89: 12
]^      3.89 -     4.75: 28
]^      4.75 -     5.61: 44
]^      5.61 -     6.48: 54
""")
    s = StringIO()
    pair_proxies.nonbonded_proxies.show_sorted(by_value="delta",
                                               sites_cart=sites_cart,
                                               max_items=7,
                                               f=s,
                                               prefix=">,")
    assert not show_diff(s.getvalue(),
                         """\
>,Nonbonded interactions: 141
>,Sorted by model distance:
>,nonbonded 15
>,          15
>,   model   vdw sym.op.
>,   2.164 3.600 -x+2,-y+1,z
...
>,nonbonded 4
>,          8
>,   model   vdw
>,   3.414 3.600
>,... (remaining 134 not shown)
""",
                         selections=[range(6), range(-5, 0)])
    vdw_1_sticks = []
    vdw_2_sticks = []
    for proxy in pair_proxies.nonbonded_proxies.simple:
        if (proxy.vdw_distance == default_vdw_distance):
            vdw_1_sticks.append(
                pml_stick(begin=sites_cart[proxy.i_seqs[0]],
                          end=sites_cart[proxy.i_seqs[1]]))
        else:
            vdw_2_sticks.append(
                pml_stick(begin=sites_cart[proxy.i_seqs[0]],
                          end=sites_cart[proxy.i_seqs[1]]))
    mps = asu_mappings.mappings()
    for proxy in pair_proxies.nonbonded_proxies.asu:
        if (proxy.vdw_distance == default_vdw_distance):
            vdw_1_sticks.append(
                pml_stick(begin=mps[proxy.i_seq][0].mapped_site(),
                          end=mps[proxy.j_seq][proxy.j_sym].mapped_site()))
        else:
            vdw_2_sticks.append(
                pml_stick(begin=mps[proxy.i_seq][0].mapped_site(),
                          end=mps[proxy.j_seq][proxy.j_sym].mapped_site()))
    if (0 or verbose):
        pml_write(f=open("vdw_1.pml", "w"), label="vdw_1", sticks=vdw_1_sticks)
        pml_write(f=open("vdw_2.pml", "w"), label="vdw_2", sticks=vdw_2_sticks)
    #
    i_pdb = count(2)
    for use_crystal_symmetry in [False, True]:
        if (not use_crystal_symmetry):
            crystal_symmetry = None
            site_symmetry_table = None
        else:
            crystal_symmetry = xray_structure
            site_symmetry_table = xray_structure.site_symmetry_table()
        for sites_cart in [
                sites_cart_manual.deep_copy(),
                sites_cart_minimized_1.deep_copy()
        ]:
            manager = geometry_restraints.manager.manager(
                crystal_symmetry=crystal_symmetry,
                site_symmetry_table=site_symmetry_table,
                nonbonded_params=nonbonded_params,
                nonbonded_types=atom_energy_types,
                nonbonded_function=geometry_restraints.
                prolsq_repulsion_function(),
                bond_params_table=bond_params_table,
                shell_sym_tables=shell_sym_tables,
                nonbonded_distance_cutoff=nonbonded_cutoff,
                nonbonded_buffer=1,
                angle_proxies=angle_proxies,
                plain_pairs_radius=5)
            manager = manager.select(
                selection=flex.bool(sites_cart.size(), True))
            manager = manager.select(iselection=flex.size_t_range(
                stop=sites_cart.size()))
            pair_proxies = manager.pair_proxies(sites_cart=sites_cart)
            minimized = geometry_restraints.lbfgs.lbfgs(
                sites_cart=sites_cart,
                geometry_restraints_manager=manager,
                lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                    max_iterations=1000))
            if (0 or verbose):
                minimized.final_target_result.show()
                print "number of function evaluations:", minimized.minimizer.nfun(
                )
                print "n_updates_pair_proxies:", manager.n_updates_pair_proxies
            if (not use_crystal_symmetry):
                assert minimized.final_target_result.bond_residual_sum < 1.e-3
                assert minimized.final_target_result.nonbonded_residual_sum < 0.1
            else:
                assert minimized.final_target_result.bond_residual_sum < 1.e-2
                assert minimized.final_target_result.nonbonded_residual_sum < 0.1
            assert minimized.final_target_result.angle_residual_sum < 1.e-3
            if (0 or verbose):
                pdb_file_name = "minimized_%d.pdb" % i_pdb.next()
                print "Writing file:", pdb_file_name
                dump_pdb(file_name=pdb_file_name, sites_cart=sites_cart)
            if (manager.site_symmetry_table is None):
                additional_site_symmetry_table = None
            else:
                additional_site_symmetry_table = sgtbx.site_symmetry_table()
            assert manager.new_including_isolated_sites(
              n_additional_sites=0,
              site_symmetry_table=additional_site_symmetry_table,
              nonbonded_types=flex.std_string()).plain_pairs_radius \
                == manager.plain_pairs_radius
            if (crystal_symmetry is not None):
                assert len(manager.plain_pair_sym_table) == 16
                if (0 or verbose):
                    manager.plain_pair_sym_table.show()
    #
    xray_structure.set_u_iso(values=flex.double([
        0.77599982480241358, 0.38745781137212021, 0.20667558236418682,
        0.99759840171302094, 0.8917287406687805, 0.64780251325379845,
        0.24878590382983534, 0.59480621182194615, 0.58695637792905142,
        0.33997130213653637, 0.51258699130743735, 0.79760289141276675,
        0.39996577657875021, 0.4329328819341467, 0.70422156561726479,
        0.87260110626999332
    ]))

    class parameters:
        pass

    parameters.sphere_radius = 5
    parameters.distance_power = 0.7
    parameters.average_power = 0.9
    parameters.wilson_b_weight = 1.3952
    parameters.wilson_b_weight_auto = False
    adp_energies = adp_restraints.energies_iso(
        geometry_restraints_manager=manager,
        xray_structure=xray_structure,
        parameters=parameters,
        wilson_b=None,
        use_hd=False,
        use_u_local_only=False,
        compute_gradients=False,
        gradients=None,
        normalization=False,
        collect=True)
    assert adp_energies.number_of_restraints == 69
    assert approx_equal(adp_energies.residual_sum, 6.24865382467)
    assert adp_energies.gradients is None
    assert adp_energies.u_i.size() == adp_energies.number_of_restraints
    assert adp_energies.u_j.size() == adp_energies.number_of_restraints
    assert adp_energies.r_ij.size() == adp_energies.number_of_restraints
    for wilson_b in [None, 10, 100]:
        finite_difference_gradients = flex.double()
        eps = 1.e-6
        for i_scatterer in xrange(xray_structure.scatterers().size()):
            rs = []
            for signed_eps in [eps, -eps]:
                xray_structure_eps = xray_structure.deep_copy_scatterers()
                xray_structure_eps.scatterers(
                )[i_scatterer].u_iso += signed_eps
                adp_energies = adp_restraints.energies_iso(
                    geometry_restraints_manager=manager,
                    xray_structure=xray_structure_eps,
                    parameters=parameters,
                    wilson_b=wilson_b,
                    use_u_local_only=False,
                    use_hd=False,
                    compute_gradients=True,
                    gradients=None,
                    normalization=False,
                    collect=False)
                rs.append(adp_energies.residual_sum)
                assert adp_energies.gradients.size() \
                    == xray_structure.scatterers().size()
                assert adp_energies.u_i == None
                assert adp_energies.u_j == None
                assert adp_energies.r_ij == None
            finite_difference_gradients.append((rs[0] - rs[1]) / (2 * eps))
        sel = flex.bool(xray_structure.scatterers().size(), True)
        xray_structure.scatterers().flags_set_grad_u_iso(sel.iselection())
        adp_energies = adp_restraints.energies_iso(
            geometry_restraints_manager=manager,
            xray_structure=xray_structure,
            parameters=parameters,
            wilson_b=wilson_b,
            use_u_local_only=False,
            use_hd=False,
            compute_gradients=True,
            gradients=None,
            normalization=False,
            collect=False)
        assert approx_equal(adp_energies.gradients,
                            finite_difference_gradients)
    print "OK"
Пример #9
0
def run(args):
  assert args in [[], ["--verbose"]]
  if (len(args) != 0):
    cout = sys.stdout
  else:
    cout = null_out()
  edge_list_bonds = [(0,1),(0,4),(1,2),(2,3),(3,4)]
  bond_list = [
    (("C1*", "C2*"), 1.529),
    (("C1*", "O4*"), 1.412),
    (("C2*", "C3*"), 1.526),
    (("C3*", "C4*"), 1.520),
    (("C4*", "O4*"), 1.449)]
  angle_list = [
    (("C1*", "C2*", "C3*"), 101.3),
    (("C2*", "C3*", "C4*"), 102.3),
    (("C3*", "C4*", "O4*"), 104.2),
    (("C4*", "O4*", "C1*"), 110.0)]
  sites_cart, geo_manager = cctbx.geometry_restraints.manager \
    .construct_non_crystallographic_conserving_bonds_and_angles(
      sites_cart=sites_cart_3p,
      edge_list_bonds=edge_list_bonds,
      edge_list_angles=[])
  for bond_atom_names,distance_ideal in bond_list:
    i,j = [atom_names.index(atom_name) for atom_name in bond_atom_names]
    bond_params = geo_manager.bond_params_table[i][j]
    assert approx_equal(bond_params.distance_ideal, distance_ideal, eps=1.e-2)
    bond_params.distance_ideal = distance_ideal
    bond_params.weight = 1/0.02**2
  assert geo_manager.angle_proxies is None
  geo_manager.angle_proxies = cctbx.geometry_restraints.shared_angle_proxy()
  for angle_atom_names,angle_ideal in angle_list:
    i_seqs = [atom_names.index(atom_name) for atom_name in angle_atom_names]
    geo_manager.angle_proxies.append(cctbx.geometry_restraints.angle_proxy(
      i_seqs=i_seqs,
      angle_ideal=angle_ideal,
      weight=1/3**2))
  geo_manager.show_sorted(
    site_labels=atom_names, sites_cart=sites_cart, f=cout)
  def lbfgs(sites_cart):
    for i_lbfgs_restart in xrange(3):
      minimized = cctbx.geometry_restraints.lbfgs.lbfgs(
        sites_cart=sites_cart,
        geometry_restraints_manager=geo_manager)
      assert is_below_limit(value=minimized.final_target_value, limit=1e-10)
    return minimized
  lbfgs(sites_cart=sites_cart_3p)
  lbfgs(sites_cart=sites_cart_2p)
  conformer_counts = [0] * 4
  sites_cart = sites_cart.deep_copy()
  mt = flex.mersenne_twister(seed=0)
  for i_trial in xrange(20):
    while True:
      for i in xrange(sites_cart.size()):
        sites_cart[i] = mt.random_double_point_on_sphere()
      try:
        lbfgs(sites_cart=sites_cart)
      except RuntimeError, e:
        if (not str(e).startswith(
              "Bond distance > max_reasonable_bond_distance: ")):
          raise
      else:
        break
    rmsd_list = flex.double()
    for reference_sites in [
          sites_cart_3p,
          sites_cart_2p,
          sites_cart_a,
          sites_cart_b]:
      sup = scitbx.math.superpose.least_squares_fit(
        reference_sites=reference_sites,
        other_sites=sites_cart)
      rmsd = reference_sites.rms_difference(sup.other_sites_best_fit())
      rmsd_list.append(rmsd)
    oline = " ".join(["%.3f" % rmsd for rmsd in rmsd_list])
    print >> cout, oline
    assert is_below_limit(min(rmsd_list), 1e-3)
    conformer_counts[flex.min_index(rmsd_list)] += 1
Пример #10
0
def run(args):
    assert args in [[], ["--verbose"]]
    if (len(args) != 0):
        cout = sys.stdout
    else:
        cout = null_out()
    edge_list_bonds = [(0, 1), (0, 4), (1, 2), (2, 3), (3, 4)]
    bond_list = [(("C1*", "C2*"), 1.529), (("C1*", "O4*"), 1.412),
                 (("C2*", "C3*"), 1.526), (("C3*", "C4*"), 1.520),
                 (("C4*", "O4*"), 1.449)]
    angle_list = [
        (("C1*", "C2*", "C3*"), 101.3), (("C2*", "C3*", "C4*"), 102.3),
        (("C3*", "C4*", "O4*"), 104.2), (("C4*", "O4*", "C1*"), 110.0)
    ]
    sites_cart, geo_manager = cctbx.geometry_restraints.manager \
      .construct_non_crystallographic_conserving_bonds_and_angles(
        sites_cart=sites_cart_3p,
        edge_list_bonds=edge_list_bonds,
        edge_list_angles=[])
    for bond_atom_names, distance_ideal in bond_list:
        i, j = [atom_names.index(atom_name) for atom_name in bond_atom_names]
        bond_params = geo_manager.bond_params_table[i][j]
        assert approx_equal(bond_params.distance_ideal,
                            distance_ideal,
                            eps=1.e-2)
        bond_params.distance_ideal = distance_ideal
        bond_params.weight = 1 / 0.02**2
    assert geo_manager.angle_proxies is None
    geo_manager.angle_proxies = cctbx.geometry_restraints.shared_angle_proxy()
    for angle_atom_names, angle_ideal in angle_list:
        i_seqs = [
            atom_names.index(atom_name) for atom_name in angle_atom_names
        ]
        geo_manager.angle_proxies.append(
            cctbx.geometry_restraints.angle_proxy(i_seqs=i_seqs,
                                                  angle_ideal=angle_ideal,
                                                  weight=1 / 3**2))
    geo_manager.show_sorted(site_labels=atom_names,
                            sites_cart=sites_cart,
                            f=cout)

    def lbfgs(sites_cart):
        for i_lbfgs_restart in range(3):
            minimized = cctbx.geometry_restraints.lbfgs.lbfgs(
                sites_cart=sites_cart, geometry_restraints_manager=geo_manager)
            assert is_below_limit(value=minimized.final_target_value,
                                  limit=1e-10)
        return minimized

    lbfgs(sites_cart=sites_cart_3p)
    lbfgs(sites_cart=sites_cart_2p)
    conformer_counts = [0] * 4
    sites_cart = sites_cart.deep_copy()
    mt = flex.mersenne_twister(seed=0)
    for i_trial in range(20):
        while True:
            for i in range(sites_cart.size()):
                sites_cart[i] = mt.random_double_point_on_sphere()
            try:
                lbfgs(sites_cart=sites_cart)
            except RuntimeError as e:
                if (not str(e).startswith(
                        "Bond distance > max_reasonable_bond_distance: ")):
                    raise
            else:
                break
        rmsd_list = flex.double()
        for reference_sites in [
                sites_cart_3p, sites_cart_2p, sites_cart_a, sites_cart_b
        ]:
            sup = scitbx.math.superpose.least_squares_fit(
                reference_sites=reference_sites, other_sites=sites_cart)
            rmsd = reference_sites.rms_difference(sup.other_sites_best_fit())
            rmsd_list.append(rmsd)
        oline = " ".join(["%.3f" % rmsd for rmsd in rmsd_list])
        print(oline, file=cout)
        assert is_below_limit(min(rmsd_list), 1e-3)
        conformer_counts[flex.min_index(rmsd_list)] += 1
    print("conformer_counts:", conformer_counts)
    #
    if (libtbx.env.has_module("iotbx")):
        import iotbx.pdb.hierarchy
        hierarchy = iotbx.pdb.hierarchy.root()
        model = iotbx.pdb.hierarchy.model(id="")
        chain = iotbx.pdb.hierarchy.chain(id="A")
        model.append_chain(chain)
        hierarchy.append_model(model)
        #
        sites_cart_pentagon = pentagon_sites_cart()
        for i_stack, sites_cart in enumerate(
            [sites_cart_3p, sites_cart_2p, sites_cart_a, sites_cart_b]):
            atom_group = iotbx.pdb.hierarchy.atom_group(resname="  U",
                                                        altloc="")
            sup = scitbx.math.superpose.least_squares_fit(
                reference_sites=sites_cart_pentagon, other_sites=sites_cart)
            sites_cart_out = sup.other_sites_best_fit()
            for site_label, site_cart in zip(atom_names, sites_cart_out):
                atom = iotbx.pdb.hierarchy.atom()
                atom.name = " %-3s" % site_label
                atom.xyz = matrix.col(site_cart) + matrix.col(
                    (0, 0, i_stack * 1.5))
                atom.occ = 1
                atom.b = 20
                atom.element = " " + site_label[0]
                atom_group.append_atom(atom)
            residue_group = iotbx.pdb.hierarchy.residue_group(resseq="%4d" %
                                                              (i_stack + 1),
                                                              icode=" ")
            residue_group.append_atom_group(atom_group)
            chain.append_residue_group(residue_group)
        hierarchy.atoms().reset_serial()
        pdb_str = hierarchy.as_pdb_string(append_end=True)
        file_name = "puckers.pdb"
        print("Writing file:", file_name)
        open(file_name, "w").write("""\
REMARK random_puckers.py
REMARK 1 = 3'
REMARK 2 = 2'
REMARK 3 = A
REMARK 4 = B
""" + pdb_str)
    #
    print("OK")
Пример #11
0
def compare_essence_and_fast_tardy_models(etm,
                                          have_singularity=False,
                                          run_minimization=False):
    etm = scitbx.rigid_body.essence.tardy.model(  # new instance to reset q, qd
        labels=etm.labels,
        sites=etm.sites,
        masses=etm.masses,
        tardy_tree=etm.tardy_tree,
        potential_obj=etm.potential_obj,
        near_singular_hinges_angular_tolerance_deg=etm.
        near_singular_hinges_angular_tolerance_deg)
    ftm = etm_as_ftm(etm=etm)
    #
    assert list(ftm.root_indices()) == etm.root_indices()
    #
    q_packed_zero = etm.pack_q()
    qd_packed_zero = etm.pack_qd()

    #
    def check_packed():
        e = etm.pack_q()
        f = ftm.pack_q()
        assert approx_equal(e, f)
        e = etm.pack_qd()
        f = ftm.pack_qd()
        assert approx_equal(e, f)

    check_packed()
    mt = flex.mersenne_twister(seed=0)
    q_packed_rand = mt.random_double(size=q_packed_zero.size()) * 2 - 1
    qd_packed_rand = mt.random_double(size=qd_packed_zero.size()) * 2 - 1
    for tm in [etm, ftm]:
        tm.unpack_q(q_packed=q_packed_rand)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_q(q_packed=q_packed_zero)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_qd(qd_packed=qd_packed_rand)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_qd(qd_packed=qd_packed_zero)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_q(q_packed=q_packed_rand)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_qd(qd_packed=qd_packed_rand)
    check_packed()
    #
    fnosiet = ftm.number_of_sites_in_each_tree()
    assert list(fnosiet) == etm.number_of_sites_in_each_tree()
    e = etm.sum_of_masses_in_each_tree()
    f = ftm.sum_of_masses_in_each_tree()
    assert approx_equal(e, f)

    #
    def check_mean_linear_velocity():
        e = etm.mean_linear_velocity(number_of_sites_in_each_tree=None)
        for f in [
                ftm.mean_linear_velocity(number_of_sites_in_each_tree=None),
                ftm.mean_linear_velocity(number_of_sites_in_each_tree=fnosiet)
        ]:
            if (e is None): assert f is None
            else: assert approx_equal(e, f)

    check_mean_linear_velocity()
    value = matrix.col(mt.random_double(size=3) * 2 - 1)
    for tm in [etm, ftm]:
        tm.subtract_from_linear_velocities(number_of_sites_in_each_tree=None,
                                           value=value)
    check_mean_linear_velocity()
    for tm in [etm, ftm]:
        tm.subtract_from_linear_velocities(
            number_of_sites_in_each_tree=fnosiet, value=value)
    check_mean_linear_velocity()
    #
    e = etm.sites_moved()
    f = ftm.sites_moved()
    assert approx_equal(e, f)
    e = etm.e_pot()
    f = ftm.e_pot()
    assert approx_equal(e, f)
    e = etm.d_e_pot_d_sites()
    f = ftm.d_e_pot_d_sites()
    assert approx_equal(e, f)
    e = etm.d_e_pot_d_q_packed()
    f = ftm.d_e_pot_d_q_packed()
    if (e.size() == 0):
        assert f.size() == 0
    else:
        e_max_abs = flex.max(flex.abs(e))
        if (etm.potential_obj is None):
            assert is_below_limit(value=e_max_abs, limit=1e-10)
        else:
            # note: e_max_abs is random generator dependent
            # by change much smaller e_max_abs are possible
            assert is_above_limit(value=e_max_abs, limit=0.1)
        assert approx_equal(e, f)
    #
    e = etm.e_kin()
    f = ftm.e_kin()
    assert approx_equal(e, f)
    e = etm.e_tot()
    f = ftm.e_tot()
    assert approx_equal(e, f)
    etm.reset_e_kin(e_kin_target=1.234)
    ftm.reset_e_kin(e_kin_target=1.234)
    e = etm.e_kin()
    if (etm.degrees_of_freedom == 0):
        assert approx_equal(e, 0)
    else:
        assert approx_equal(e, 1.234)
    f = ftm.e_kin()
    assert approx_equal(e, f)
    try:
        ftm.reset_e_kin(e_kin_target=1, e_kin_epsilon=0)
    except RuntimeError, e:
        assert str(e).find("e_kin_epsilon > 0") > 0
Пример #12
0
 tau2_array_e = etm.inverse_dynamics(qdd_array=qdd_array_e,
                                     f_ext_array=f_ext_array,
                                     grav_accn=grav_accn)
 tau2_packed_e = pack_array(array=tau2_array_e,
                            packed_size=etm.degrees_of_freedom)
 tau2_packed_f = ftm.inverse_dynamics_packed(
     qdd_packed=qdd_packed_f,
     f_ext_packed=f_ext_packed,
     grav_accn=grav_accn_f)
 assert approx_equal(tau2_packed_e, tau2_packed_f)
 if (etm.degrees_of_freedom == 0):
     assert tau2_packed_e.size() == 0
 elif (not have_singularity):
     if (tau_packed is None):
         assert is_below_limit(flex.max(
             flex.abs(tau2_packed_e)),
                               0,
                               eps=1e-5)
     else:
         assert approx_equal(tau2_packed_e,
                             tau_packed,
                             eps=1e-5)
 qdd2_array_e = etm.forward_dynamics_ab(tau_array=tau2_array_e,
                                        f_ext_array=f_ext_array,
                                        grav_accn=grav_accn)
 qdd2_packed_e = pack_array(array=qdd2_array_e,
                            packed_size=etm.degrees_of_freedom)
 qdd2_packed_f = ftm.forward_dynamics_ab_packed(
     tau_packed=tau2_packed_f,
     f_ext_packed=f_ext_packed,
     grav_accn=grav_accn_f)
 assert approx_equal(qdd2_packed_e, qdd2_packed_f)
Пример #13
0
def compare_essence_and_fast_tardy_models(
      etm,
      have_singularity=False,
      run_minimization=False):
  etm = scitbx.rigid_body.essence.tardy.model( # new instance to reset q, qd
    labels=etm.labels,
    sites=etm.sites,
    masses=etm.masses,
    tardy_tree=etm.tardy_tree,
    potential_obj=etm.potential_obj,
    near_singular_hinges_angular_tolerance_deg=
      etm.near_singular_hinges_angular_tolerance_deg)
  ftm = etm_as_ftm(etm=etm)
  #
  assert list(ftm.root_indices()) == etm.root_indices()
  #
  q_packed_zero = etm.pack_q()
  qd_packed_zero = etm.pack_qd()
  #
  def check_packed():
    e = etm.pack_q()
    f = ftm.pack_q()
    assert approx_equal(e, f)
    e = etm.pack_qd()
    f = ftm.pack_qd()
    assert approx_equal(e, f)
  check_packed()
  mt = flex.mersenne_twister(seed=0)
  q_packed_rand = mt.random_double(size=q_packed_zero.size())*2-1
  qd_packed_rand = mt.random_double(size=qd_packed_zero.size())*2-1
  for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_rand)
  check_packed()
  for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_zero)
  check_packed()
  for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_rand)
  check_packed()
  for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_zero)
  check_packed()
  for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_rand)
  check_packed()
  for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_rand)
  check_packed()
  #
  fnosiet = ftm.number_of_sites_in_each_tree()
  assert list(fnosiet) == etm.number_of_sites_in_each_tree()
  e = etm.sum_of_masses_in_each_tree()
  f = ftm.sum_of_masses_in_each_tree()
  assert approx_equal(e, f)
  #
  def check_mean_linear_velocity():
    e = etm.mean_linear_velocity(number_of_sites_in_each_tree=None)
    for f in [ftm.mean_linear_velocity(number_of_sites_in_each_tree=None),
              ftm.mean_linear_velocity(number_of_sites_in_each_tree=fnosiet)]:
      if (e is None): assert f is None
      else:           assert approx_equal(e, f)
  check_mean_linear_velocity()
  value = matrix.col(mt.random_double(size=3)*2-1)
  for tm in [etm, ftm]:
    tm.subtract_from_linear_velocities(
      number_of_sites_in_each_tree=None,
      value=value)
  check_mean_linear_velocity()
  for tm in [etm, ftm]:
    tm.subtract_from_linear_velocities(
      number_of_sites_in_each_tree=fnosiet,
      value=value)
  check_mean_linear_velocity()
  #
  e = etm.sites_moved()
  f = ftm.sites_moved()
  assert approx_equal(e, f)
  e = etm.e_pot()
  f = ftm.e_pot()
  assert approx_equal(e, f)
  e = etm.d_e_pot_d_sites()
  f = ftm.d_e_pot_d_sites()
  assert approx_equal(e, f)
  e = etm.d_e_pot_d_q_packed()
  f = ftm.d_e_pot_d_q_packed()
  if (e.size() == 0):
    assert f.size() == 0
  else:
    e_max_abs = flex.max(flex.abs(e))
    if (etm.potential_obj is None):
      assert is_below_limit(value=e_max_abs, limit=1e-10)
    else:
      # note: e_max_abs is random generator dependent
      # by change much smaller e_max_abs are possible
      assert is_above_limit(value=e_max_abs, limit=0.1)
    assert approx_equal(e, f)
  #
  e = etm.e_kin()
  f = ftm.e_kin()
  assert approx_equal(e, f)
  e = etm.e_tot()
  f = ftm.e_tot()
  assert approx_equal(e, f)
  etm.reset_e_kin(e_kin_target=1.234)
  ftm.reset_e_kin(e_kin_target=1.234)
  e = etm.e_kin()
  if (etm.degrees_of_freedom == 0):
    assert approx_equal(e, 0)
  else:
    assert approx_equal(e, 1.234)
  f = ftm.e_kin()
  assert approx_equal(e, f)
  try:
    ftm.reset_e_kin(e_kin_target=1, e_kin_epsilon=0)
  except RuntimeError, e:
    assert str(e).find("e_kin_epsilon > 0") > 0
Пример #14
0
   qdd_array=qdd_array_e,
   f_ext_array=f_ext_array,
   grav_accn=grav_accn)
 tau2_packed_e = pack_array(
   array=tau2_array_e,
   packed_size=etm.degrees_of_freedom)
 tau2_packed_f = ftm.inverse_dynamics_packed(
   qdd_packed=qdd_packed_f,
   f_ext_packed=f_ext_packed,
   grav_accn=grav_accn_f)
 assert approx_equal(tau2_packed_e, tau2_packed_f)
 if (etm.degrees_of_freedom == 0):
   assert tau2_packed_e.size() == 0
 elif (not have_singularity):
   if (tau_packed is None):
     assert is_below_limit(
       flex.max(flex.abs(tau2_packed_e)), 0, eps=1e-5)
   else:
     assert approx_equal(tau2_packed_e, tau_packed, eps=1e-5)
 qdd2_array_e = etm.forward_dynamics_ab(
   tau_array=tau2_array_e,
   f_ext_array=f_ext_array,
   grav_accn=grav_accn)
 qdd2_packed_e = pack_array(
   array=qdd2_array_e,
   packed_size=etm.degrees_of_freedom)
 qdd2_packed_f = ftm.forward_dynamics_ab_packed(
   tau_packed=tau2_packed_f,
   f_ext_packed=f_ext_packed,
   grav_accn=grav_accn_f)
 assert approx_equal(qdd2_packed_e, qdd2_packed_f)
 assert approx_equal(qdd2_packed_e, qdd_packed_e, eps=1e-4)
Пример #15
0
Файл: tst.py Проект: dials/cctbx
def compare_essence_and_fast_tardy_models(etm,
                                          have_singularity=False,
                                          run_minimization=False):
    etm = scitbx.rigid_body.essence.tardy.model(  # new instance to reset q, qd
        labels=etm.labels,
        sites=etm.sites,
        masses=etm.masses,
        tardy_tree=etm.tardy_tree,
        potential_obj=etm.potential_obj,
        near_singular_hinges_angular_tolerance_deg=etm.
        near_singular_hinges_angular_tolerance_deg)
    ftm = etm_as_ftm(etm=etm)
    #
    assert list(ftm.root_indices()) == etm.root_indices()
    #
    q_packed_zero = etm.pack_q()
    qd_packed_zero = etm.pack_qd()

    #
    def check_packed():
        e = etm.pack_q()
        f = ftm.pack_q()
        assert approx_equal(e, f)
        e = etm.pack_qd()
        f = ftm.pack_qd()
        assert approx_equal(e, f)

    check_packed()
    mt = flex.mersenne_twister(seed=0)
    q_packed_rand = mt.random_double(size=q_packed_zero.size()) * 2 - 1
    qd_packed_rand = mt.random_double(size=qd_packed_zero.size()) * 2 - 1
    for tm in [etm, ftm]:
        tm.unpack_q(q_packed=q_packed_rand)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_q(q_packed=q_packed_zero)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_qd(qd_packed=qd_packed_rand)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_qd(qd_packed=qd_packed_zero)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_q(q_packed=q_packed_rand)
    check_packed()
    for tm in [etm, ftm]:
        tm.unpack_qd(qd_packed=qd_packed_rand)
    check_packed()
    #
    fnosiet = ftm.number_of_sites_in_each_tree()
    assert list(fnosiet) == etm.number_of_sites_in_each_tree()
    e = etm.sum_of_masses_in_each_tree()
    f = ftm.sum_of_masses_in_each_tree()
    assert approx_equal(e, f)

    #
    def check_mean_linear_velocity():
        e = etm.mean_linear_velocity(number_of_sites_in_each_tree=None)
        for f in [
                ftm.mean_linear_velocity(number_of_sites_in_each_tree=None),
                ftm.mean_linear_velocity(number_of_sites_in_each_tree=fnosiet)
        ]:
            if (e is None): assert f is None
            else: assert approx_equal(e, f)

    check_mean_linear_velocity()
    value = matrix.col(mt.random_double(size=3) * 2 - 1)
    for tm in [etm, ftm]:
        tm.subtract_from_linear_velocities(number_of_sites_in_each_tree=None,
                                           value=value)
    check_mean_linear_velocity()
    for tm in [etm, ftm]:
        tm.subtract_from_linear_velocities(
            number_of_sites_in_each_tree=fnosiet, value=value)
    check_mean_linear_velocity()
    #
    e = etm.sites_moved()
    f = ftm.sites_moved()
    assert approx_equal(e, f)
    e = etm.e_pot()
    f = ftm.e_pot()
    assert approx_equal(e, f)
    e = etm.d_e_pot_d_sites()
    f = ftm.d_e_pot_d_sites()
    assert approx_equal(e, f)
    e = etm.d_e_pot_d_q_packed()
    f = ftm.d_e_pot_d_q_packed()
    if (e.size() == 0):
        assert f.size() == 0
    else:
        e_max_abs = flex.max(flex.abs(e))
        if (etm.potential_obj is None):
            assert is_below_limit(value=e_max_abs, limit=1e-10)
        else:
            # note: e_max_abs is random generator dependent
            # by change much smaller e_max_abs are possible
            assert is_above_limit(value=e_max_abs, limit=0.1)
        assert approx_equal(e, f)
    #
    e = etm.e_kin()
    f = ftm.e_kin()
    assert approx_equal(e, f)
    e = etm.e_tot()
    f = ftm.e_tot()
    assert approx_equal(e, f)
    etm.reset_e_kin(e_kin_target=1.234)
    ftm.reset_e_kin(e_kin_target=1.234)
    e = etm.e_kin()
    if (etm.degrees_of_freedom == 0):
        assert approx_equal(e, 0)
    else:
        assert approx_equal(e, 1.234)
    f = ftm.e_kin()
    assert approx_equal(e, f)
    try:
        ftm.reset_e_kin(e_kin_target=1, e_kin_epsilon=0)
    except RuntimeError as e:
        assert str(e).find("e_kin_epsilon > 0") > 0
    else:
        raise Exception_expected
    etm.assign_zero_velocities()
    ftm.assign_zero_velocities()
    assert etm.e_kin() == 0
    assert ftm.e_kin() == 0
    random.seed(0)
    etm.assign_random_velocities()
    random.seed(0)
    ftm.assign_random_velocities()
    e = etm.e_kin()
    f = ftm.e_kin()
    assert approx_equal(e, f)
    #
    mt = flex.mersenne_twister(seed=0)
    qdd_rand_array = []
    for body in etm.bodies:
        qdd_rand_array.append(
            matrix.col(
                mt.random_double(size=body.joint.degrees_of_freedom) * 2 - 1))
    tau_rand_array = []
    for body in etm.bodies:
        tau_rand_array.append(
            matrix.col(
                mt.random_double(size=body.joint.degrees_of_freedom) * 2 - 1))
    f_ext_rand_array = []
    for ib in range(len(etm.bodies)):
        f_ext_rand_array.append(matrix.col(mt.random_double(size=6) * 2 - 1))
    grav_accn_rand = matrix.col(mt.random_double(size=6) * 2 - 1)

    def pack_array(array, packed_size=None):
        result = flex.double()
        if (packed_size is not None):
            result.reserve(packed_size)
        for sub in array:
            result.extend(flex.double(sub))
        if (packed_size is not None):
            assert result.size() == packed_size
        return result

    qdd_rand_packed = pack_array(array=qdd_rand_array,
                                 packed_size=etm.degrees_of_freedom)
    tau_rand_packed = pack_array(array=tau_rand_array,
                                 packed_size=etm.degrees_of_freedom)
    f_ext_rand_packed = pack_array(array=f_ext_rand_array,
                                   packed_size=len(etm.bodies) * 6)

    def etm_inverse_dynamics_packed(qdd_array=None,
                                    f_ext_array=None,
                                    grav_accn=None):
        return pack_array(array=etm.inverse_dynamics(qdd_array=qdd_array,
                                                     f_ext_array=f_ext_array,
                                                     grav_accn=grav_accn),
                          packed_size=etm.degrees_of_freedom)

    def etm_forward_dynamics_ab_packed(tau_array=None,
                                       f_ext_array=None,
                                       grav_accn=None):
        return pack_array(array=etm.forward_dynamics_ab(
            tau_array=tau_array, f_ext_array=f_ext_array, grav_accn=grav_accn),
                          packed_size=etm.degrees_of_freedom)

    #
    e = etm_inverse_dynamics_packed(qdd_array=qdd_rand_array)
    f = ftm.inverse_dynamics_packed(qdd_packed=qdd_rand_packed)
    assert approx_equal(e, f)
    e = etm_inverse_dynamics_packed(f_ext_array=f_ext_rand_array)
    f = ftm.inverse_dynamics_packed(f_ext_packed=f_ext_rand_packed)
    assert approx_equal(e, f)
    e = etm_inverse_dynamics_packed(grav_accn=grav_accn_rand)
    f = ftm.inverse_dynamics_packed(grav_accn=flex.double(grav_accn_rand))
    assert approx_equal(e, f)
    for qdd_array, qdd_packed in [(None, None),
                                  (qdd_rand_array, qdd_rand_packed)]:
        for f_ext_array, f_ext_packed in [(None, None),
                                          (f_ext_rand_array, f_ext_rand_packed)
                                          ]:
            for grav_accn in [None, grav_accn_rand]:
                if ([qdd_array, f_ext_array, grav_accn].count(None) >= 2):
                    continue  # exercised above already
                e = etm_inverse_dynamics_packed(qdd_array=qdd_rand_array,
                                                f_ext_array=f_ext_rand_array,
                                                grav_accn=grav_accn_rand)
                f = ftm.inverse_dynamics_packed(
                    qdd_packed=qdd_rand_packed,
                    f_ext_packed=f_ext_rand_packed,
                    grav_accn=flex.double(grav_accn_rand))
                assert approx_equal(e, f)
    #
    e = etm_forward_dynamics_ab_packed()
    f = ftm.forward_dynamics_ab_packed()
    assert approx_equal(e, f)
    e = etm_forward_dynamics_ab_packed(tau_array=tau_rand_array)
    f = ftm.forward_dynamics_ab_packed(tau_packed=tau_rand_packed)
    assert approx_equal(e, f)
    e = etm_forward_dynamics_ab_packed(f_ext_array=f_ext_rand_array)
    f = ftm.forward_dynamics_ab_packed(f_ext_packed=f_ext_rand_packed)
    assert approx_equal(e, f)
    e = etm_forward_dynamics_ab_packed(grav_accn=grav_accn_rand)
    f = ftm.forward_dynamics_ab_packed(grav_accn=flex.double(grav_accn_rand))
    assert approx_equal(e, f)
    for tau_array, tau_packed in [(None, None),
                                  (tau_rand_array, tau_rand_packed)]:
        for f_ext_array, f_ext_packed in [(None, None),
                                          (f_ext_rand_array, f_ext_rand_packed)
                                          ]:
            for grav_accn in [None, grav_accn_rand]:
                if (grav_accn is None):
                    grav_accn_f = None
                else:
                    grav_accn_f = flex.double(grav_accn)
                qdd_array_e = etm.forward_dynamics_ab(tau_array=tau_array,
                                                      f_ext_array=f_ext_array,
                                                      grav_accn=grav_accn)
                qdd_packed_e = pack_array(array=qdd_array_e,
                                          packed_size=etm.degrees_of_freedom)
                qdd_packed_f = ftm.forward_dynamics_ab_packed(
                    tau_packed=tau_packed,
                    f_ext_packed=f_ext_packed,
                    grav_accn=grav_accn_f)
                assert approx_equal(qdd_packed_e, qdd_packed_f)
                tau2_array_e = etm.inverse_dynamics(qdd_array=qdd_array_e,
                                                    f_ext_array=f_ext_array,
                                                    grav_accn=grav_accn)
                tau2_packed_e = pack_array(array=tau2_array_e,
                                           packed_size=etm.degrees_of_freedom)
                tau2_packed_f = ftm.inverse_dynamics_packed(
                    qdd_packed=qdd_packed_f,
                    f_ext_packed=f_ext_packed,
                    grav_accn=grav_accn_f)
                assert approx_equal(tau2_packed_e, tau2_packed_f)
                if (etm.degrees_of_freedom == 0):
                    assert tau2_packed_e.size() == 0
                elif (not have_singularity):
                    if (tau_packed is None):
                        assert is_below_limit(flex.max(
                            flex.abs(tau2_packed_e)),
                                              0,
                                              eps=1e-5)
                    else:
                        assert approx_equal(tau2_packed_e,
                                            tau_packed,
                                            eps=1e-5)
                qdd2_array_e = etm.forward_dynamics_ab(tau_array=tau2_array_e,
                                                       f_ext_array=f_ext_array,
                                                       grav_accn=grav_accn)
                qdd2_packed_e = pack_array(array=qdd2_array_e,
                                           packed_size=etm.degrees_of_freedom)
                qdd2_packed_f = ftm.forward_dynamics_ab_packed(
                    tau_packed=tau2_packed_f,
                    f_ext_packed=f_ext_packed,
                    grav_accn=grav_accn_f)
                assert approx_equal(qdd2_packed_e, qdd2_packed_f)
                assert approx_equal(qdd2_packed_e, qdd_packed_e, eps=1e-4)
    #
    def check_qdd():
        e = pack_array(array=etm.qdd_array(),
                       packed_size=etm.degrees_of_freedom)
        f = ftm.qdd_packed()
        assert approx_equal(e, f)

    check_qdd()
    ftm.flag_positions_as_changed()
    assert not ftm.sites_moved_is_cached()
    assert not ftm.qdd_array_is_cached()
    ftm.sites_moved()
    assert ftm.sites_moved_is_cached()
    assert not ftm.qdd_array_is_cached()
    check_qdd()
    assert ftm.sites_moved_is_cached()
    assert ftm.qdd_array_is_cached()
    ftm.unpack_q(q_packed=ftm.pack_q())
    assert not ftm.sites_moved_is_cached()
    assert not ftm.qdd_array_is_cached()
    ftm.qdd_packed()
    if (ftm.potential_obj is None):
        assert not ftm.sites_moved_is_cached()
        ftm.sites_moved()
    assert ftm.sites_moved_is_cached()
    assert ftm.qdd_array_is_cached()
    ftm.unpack_qd(qd_packed=ftm.pack_qd())
    assert ftm.sites_moved_is_cached()
    assert not ftm.qdd_array_is_cached()
    check_qdd()
    #
    delta_t = 0.1234
    etm.dynamics_step(delta_t=delta_t)
    ftm.dynamics_step(delta_t=delta_t)
    assert not ftm.sites_moved_is_cached()
    assert not ftm.qdd_array_is_cached()
    check_packed()
    check_qdd()
    #
    etm.assign_zero_velocities()
    ftm.assign_zero_velocities()
    e = etm_inverse_dynamics_packed(f_ext_array=f_ext_rand_array)
    f = ftm.inverse_dynamics_packed(f_ext_packed=f_ext_rand_packed)
    assert approx_equal(e, f)
    e_ref = e
    e = pack_array(array=etm.f_ext_as_tau(f_ext_array=f_ext_array),
                   packed_size=etm.degrees_of_freedom)
    assert approx_equal(e_ref, e)
    f = ftm.f_ext_as_tau_packed(f_ext_packed=f_ext_packed)
    assert approx_equal(e_ref, f)
    #
    if (run_minimization):
        check_packed()
        etm.minimization(max_iterations=3)
        ftm.minimization(max_iterations=3)
        check_packed()
def exercise(verbose=0):
  distance_ideal = 1.8
  default_vdw_distance = 3.6
  vdw_1_4_factor = 3.5/3.6
  sites_cart_manual = flex.vec3_double([
    (1,3,0), (2,3,0), (3,2,0), (3,1,0), (4,1,0), (3,4,0), (4,3,0), (5,3,0),
    (6,2,0), (7,2,0), (8,3,0), (7,4,0), (6,4,0), (7,5,0), (6,6,0), (8,6,0)])
  bond_proxies = geometry_restraints.bond_sorted_asu_proxies(asu_mappings=None)
  for i_seqs in [(0,1),(1,2),(2,3),(3,4),(1,5),(2,6),(5,6),
                 (6,7),(7,8),(8,9),(9,10),(10,11),(11,12),
                 (12,7),(11,13),(13,14),(14,15),(15,13)]:
    bond_proxies.process(geometry_restraints.bond_simple_proxy(
      i_seqs=i_seqs, distance_ideal=distance_ideal, weight=100))
  angle_proxies = geometry_restraints.shared_angle_proxy()
  for i_seqs,angle_ideal in [[(0,1,2),135],
                             [(0,1,5),135],
                             [(1,2,3),135],
                             [(3,2,6),135],
                             [(2,3,4),120],
                             [(1,2,6),90],
                             [(2,6,5),90],
                             [(6,5,1),90],
                             [(5,1,2),90],
                             [(2,6,7),135],
                             [(5,6,7),135],
                             [(6,7,8),120],
                             [(6,7,12),120],
                             [(7,8,9),120],
                             [(8,9,10),120],
                             [(9,10,11),120],
                             [(10,11,12),120],
                             [(11,12,7),120],
                             [(12,7,8),120],
                             [(10,11,13),120],
                             [(12,11,13),120],
                             [(11,13,15),150],
                             [(11,13,14),150],
                             [(13,15,14),60],
                             [(15,14,13),60],
                             [(14,13,15),60]]:
    angle_proxies.append(geometry_restraints.angle_proxy(
      i_seqs=i_seqs, angle_ideal=angle_ideal, weight=1))
  if (0 or verbose):
    dump_pdb(file_name="manual.pdb", sites_cart=sites_cart_manual)
  for traditional_convergence_test in [True,False]:
    for sites_cart_selection in [True, False]:
      sites_cart = sites_cart_manual.deep_copy()
      if sites_cart_selection:
        sites_cart_selection = flex.bool(sites_cart.size(), True)
        sites_cart_selection[1] = False
      assert bond_proxies.asu.size() == 0
      bond_params_table = geometry_restraints.extract_bond_params(
        n_seq=sites_cart.size(),
        bond_simple_proxies=bond_proxies.simple)
      manager = geometry_restraints.manager.manager(
        bond_params_table=bond_params_table,
        angle_proxies=angle_proxies)
      minimized = geometry_restraints.lbfgs.lbfgs(
        sites_cart=sites_cart,
        geometry_restraints_manager=manager,
        lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
          traditional_convergence_test=traditional_convergence_test,
          drop_convergence_test_max_drop_eps=1.e-20,
          drop_convergence_test_iteration_coefficient=1,
          max_iterations=1000),
        sites_cart_selection=sites_cart_selection,
        )
      assert minimized.minimizer.iter() > 100
      sites_cart_minimized_1 = sites_cart.deep_copy()
      if (0 or verbose):
        dump_pdb(
          file_name="minimized_1.pdb", sites_cart=sites_cart_minimized_1)
      bond_deltas = geometry_restraints.bond_deltas(
        sites_cart=sites_cart_minimized_1,
        proxies=bond_proxies.simple)
      angle_deltas = geometry_restraints.angle_deltas(
        sites_cart=sites_cart_minimized_1,
        proxies=angle_proxies)
      if (0 or verbose):
        for proxy,delta in zip(bond_proxies.simple, bond_deltas):
          print "bond:", proxy.i_seqs, delta
        for proxy,delta in zip(angle_proxies, angle_deltas):
          print "angle:", proxy.i_seqs, delta
      assert is_below_limit(
        value=flex.max(flex.abs(bond_deltas)), limit=0, eps=1.e-6)
      assert is_below_limit(
        value=flex.max(flex.abs(angle_deltas)), limit=0, eps=2.e-6)
  sites_cart += matrix.col((1,1,0)) - matrix.col(sites_cart.min())
  unit_cell_lengths = list(  matrix.col(sites_cart.max())
                           + matrix.col((1,-1.2,4)))
  unit_cell_lengths[1] *= 2
  unit_cell_lengths[2] *= 2
  xray_structure = xray.structure(
    crystal_symmetry=crystal.symmetry(
      unit_cell=unit_cell_lengths,
      space_group_symbol="P112"))
  for serial,site in zip(count(1), sites_cart):
    xray_structure.add_scatterer(xray.scatterer(
      label="C%02d"%serial,
      site=xray_structure.unit_cell().fractionalize(site)))
  if (0 or verbose):
    xray_structure.show_summary().show_scatterers()
  p1_structure = (xray_structure
    .apply_shift((-.5,-.5,0))
    .expand_to_p1()
    .apply_shift((.5,.5,0)))
  for shift in [(1,0,0), (0,1,0), (0,0,1)]:
    p1_structure.add_scatterers(p1_structure.apply_shift(shift).scatterers())
  if (0 or verbose):
    open("p1_structure.pdb", "w").write(p1_structure.as_pdb_file())
  nonbonded_cutoff = 6.5
  asu_mappings = xray_structure.asu_mappings(
    buffer_thickness=nonbonded_cutoff)
  bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  geometry_restraints.add_pairs(bond_asu_table, bond_proxies.simple)
  shell_asu_tables = crystal.coordination_sequences.shell_asu_tables(
    pair_asu_table=bond_asu_table,
    max_shell=3)
  shell_sym_tables = [shell_asu_table.extract_pair_sym_table()
    for shell_asu_table in shell_asu_tables]
  bond_params_table = geometry_restraints.extract_bond_params(
    n_seq=sites_cart.size(),
    bond_simple_proxies=bond_proxies.simple)
  atom_energy_types = flex.std_string(sites_cart.size(), "Default")
  nonbonded_params = geometry_restraints.nonbonded_params(
    factor_1_4_interactions=vdw_1_4_factor,
    const_shrink_1_4_interactions=0,
    default_distance=default_vdw_distance)
  nonbonded_params.distance_table.setdefault(
    "Default")["Default"] = default_vdw_distance
  pair_proxies = geometry_restraints.pair_proxies(
    bond_params_table=bond_params_table,
    shell_asu_tables=shell_asu_tables,
    model_indices=None,
    conformer_indices=None,
    nonbonded_params=nonbonded_params,
    nonbonded_types=atom_energy_types,
    nonbonded_distance_cutoff_plus_buffer=nonbonded_cutoff)
  if (0 or verbose):
    print "pair_proxies.bond_proxies.n_total():", \
           pair_proxies.bond_proxies.n_total(),
    print "simple:", pair_proxies.bond_proxies.simple.size(),
    print "sym:", pair_proxies.bond_proxies.asu.size()
    print "pair_proxies.nonbonded_proxies.n_total():", \
           pair_proxies.nonbonded_proxies.n_total(),
    print "simple:", pair_proxies.nonbonded_proxies.simple.size(),
    print "sym:", pair_proxies.nonbonded_proxies.asu.size()
    print "min_distance_nonbonded: %.2f" % flex.min(
      geometry_restraints.nonbonded_deltas(
        sites_cart=sites_cart,
        sorted_asu_proxies=pair_proxies.nonbonded_proxies))
  s = StringIO()
  pair_proxies.bond_proxies.show_histogram_of_model_distances(
    sites_cart=sites_cart,
    f=s,
    prefix="[]")
  assert s.getvalue().splitlines()[0] == "[]Histogram of bond lengths:"
  assert s.getvalue().splitlines()[5].startswith("[]      1.80 -     1.80:")
  s = StringIO()
  pair_proxies.bond_proxies.show_histogram_of_deltas(
    sites_cart=sites_cart,
    f=s,
    prefix="][")
  assert s.getvalue().splitlines()[0] == "][Histogram of bond deltas:"
  assert s.getvalue().splitlines()[5].startswith("][     0.000 -    0.000:")
  s = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="residual",
    sites_cart=sites_cart,
    max_items=3,
    f=s,
    prefix=":;")
  l = s.getvalue().splitlines()
  assert l[0] == ":;Bond restraints: 18"
  assert l[1] == ":;Sorted by residual:"
  assert l[2].startswith(":;bond ")
  assert l[3].startswith(":;     ")
  assert l[4] == ":;  ideal  model  delta    sigma   weight residual"
  for i in [5,-2]:
    assert l[i].startswith(":;  1.800  1.800 ")
  assert l[-1] == ":;... (remaining 15 not shown)"
  s = StringIO()
  pair_proxies.nonbonded_proxies.show_histogram_of_model_distances(
    sites_cart=sites_cart,
    f=s,
    prefix="]^")
  assert not show_diff(s.getvalue(), """\
]^Histogram of nonbonded interaction distances:
]^      2.16 -     3.03: 3
]^      3.03 -     3.89: 12
]^      3.89 -     4.75: 28
]^      4.75 -     5.61: 44
]^      5.61 -     6.48: 54
""")
  s = StringIO()
  pair_proxies.nonbonded_proxies.show_sorted(
    by_value="delta",
    sites_cart=sites_cart,
    max_items=7,
    f=s,
    prefix=">,")
  assert not show_diff(s.getvalue(), """\
>,Nonbonded interactions: 141
>,Sorted by model distance:
>,nonbonded 15
>,          15
>,   model   vdw sym.op.
>,   2.164 3.600 -x+2,-y+1,z
...
>,nonbonded 4
>,          8
>,   model   vdw
>,   3.414 3.600
>,... (remaining 134 not shown)
""",
    selections=[range(6), range(-5,0)])
  vdw_1_sticks = []
  vdw_2_sticks = []
  for proxy in pair_proxies.nonbonded_proxies.simple:
    if (proxy.vdw_distance == default_vdw_distance):
      vdw_1_sticks.append(pml_stick(
        begin=sites_cart[proxy.i_seqs[0]],
        end=sites_cart[proxy.i_seqs[1]]))
    else:
      vdw_2_sticks.append(pml_stick(
        begin=sites_cart[proxy.i_seqs[0]],
        end=sites_cart[proxy.i_seqs[1]]))
  mps = asu_mappings.mappings()
  for proxy in pair_proxies.nonbonded_proxies.asu:
    if (proxy.vdw_distance == default_vdw_distance):
      vdw_1_sticks.append(pml_stick(
        begin=mps[proxy.i_seq][0].mapped_site(),
        end=mps[proxy.j_seq][proxy.j_sym].mapped_site()))
    else:
      vdw_2_sticks.append(pml_stick(
        begin=mps[proxy.i_seq][0].mapped_site(),
        end=mps[proxy.j_seq][proxy.j_sym].mapped_site()))
  if (0 or verbose):
    pml_write(f=open("vdw_1.pml", "w"), label="vdw_1", sticks=vdw_1_sticks)
    pml_write(f=open("vdw_2.pml", "w"), label="vdw_2", sticks=vdw_2_sticks)
  #
  i_pdb = count(2)
  for use_crystal_symmetry in [False, True]:
    if (not use_crystal_symmetry):
      crystal_symmetry = None
      site_symmetry_table = None
    else:
      crystal_symmetry = xray_structure
      site_symmetry_table = xray_structure.site_symmetry_table()
    for sites_cart in [sites_cart_manual.deep_copy(),
                       sites_cart_minimized_1.deep_copy()]:
      manager = geometry_restraints.manager.manager(
        crystal_symmetry=crystal_symmetry,
        site_symmetry_table=site_symmetry_table,
        nonbonded_params=nonbonded_params,
        nonbonded_types=atom_energy_types,
        nonbonded_function=geometry_restraints.prolsq_repulsion_function(),
        bond_params_table=bond_params_table,
        shell_sym_tables=shell_sym_tables,
        nonbonded_distance_cutoff=nonbonded_cutoff,
        nonbonded_buffer=1,
        angle_proxies=angle_proxies,
        plain_pairs_radius=5)
      manager = manager.select(selection=flex.bool(sites_cart.size(), True))
      manager = manager.select(
        iselection=flex.size_t_range(stop=sites_cart.size()))
      pair_proxies = manager.pair_proxies(sites_cart=sites_cart)
      minimized = geometry_restraints.lbfgs.lbfgs(
        sites_cart=sites_cart,
        geometry_restraints_manager=manager,
        lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
          max_iterations=1000))
      if (0 or verbose):
        minimized.final_target_result.show()
        print "number of function evaluations:", minimized.minimizer.nfun()
        print "n_updates_pair_proxies:", manager.n_updates_pair_proxies
      if (not use_crystal_symmetry):
        assert minimized.final_target_result.bond_residual_sum < 1.e-3
        assert minimized.final_target_result.nonbonded_residual_sum < 0.1
      else:
        assert minimized.final_target_result.bond_residual_sum < 1.e-2
        assert minimized.final_target_result.nonbonded_residual_sum < 0.1
      assert minimized.final_target_result.angle_residual_sum < 1.e-3
      if (0 or verbose):
        pdb_file_name = "minimized_%d.pdb" % i_pdb.next()
        print "Writing file:", pdb_file_name
        dump_pdb(file_name=pdb_file_name, sites_cart=sites_cart)
      if (manager.site_symmetry_table is None):
        additional_site_symmetry_table = None
      else:
        additional_site_symmetry_table = sgtbx.site_symmetry_table()
      assert manager.new_including_isolated_sites(
        n_additional_sites=0,
        site_symmetry_table=additional_site_symmetry_table,
        nonbonded_types=flex.std_string()).plain_pairs_radius \
          == manager.plain_pairs_radius
      if (crystal_symmetry is not None):
        assert len(manager.plain_pair_sym_table) == 16
        if (0 or verbose):
          manager.plain_pair_sym_table.show()
  #
  xray_structure.set_u_iso(values=flex.double([
    0.77599982480241358, 0.38745781137212021, 0.20667558236418682,
    0.99759840171302094, 0.8917287406687805, 0.64780251325379845,
    0.24878590382983534, 0.59480621182194615, 0.58695637792905142,
    0.33997130213653637, 0.51258699130743735, 0.79760289141276675,
    0.39996577657875021, 0.4329328819341467, 0.70422156561726479,
    0.87260110626999332]))
  class parameters: pass
  parameters.sphere_radius = 5
  parameters.distance_power = 0.7
  parameters.average_power = 0.9
  parameters.wilson_b_weight = 1.3952
  parameters.wilson_b_weight_auto = False
  adp_energies = adp_restraints.energies_iso(
    geometry_restraints_manager=manager,
    xray_structure=xray_structure,
    parameters=parameters,
    wilson_b=None,
    use_hd=False,
    use_u_local_only = False,
    compute_gradients=False,
    gradients=None,
    normalization=False,
    collect=True)
  assert adp_energies.number_of_restraints == 69
  assert approx_equal(adp_energies.residual_sum, 6.24865382467)
  assert adp_energies.gradients is None
  assert adp_energies.u_i.size() == adp_energies.number_of_restraints
  assert adp_energies.u_j.size() == adp_energies.number_of_restraints
  assert adp_energies.r_ij.size() == adp_energies.number_of_restraints
  for wilson_b in [None, 10, 100]:
    finite_difference_gradients = flex.double()
    eps = 1.e-6
    for i_scatterer in xrange(xray_structure.scatterers().size()):
      rs = []
      for signed_eps in [eps, -eps]:
        xray_structure_eps = xray_structure.deep_copy_scatterers()
        xray_structure_eps.scatterers()[i_scatterer].u_iso += signed_eps
        adp_energies = adp_restraints.energies_iso(
          geometry_restraints_manager=manager,
          xray_structure=xray_structure_eps,
          parameters=parameters,
          wilson_b=wilson_b,
          use_u_local_only = False,
          use_hd=False,
          compute_gradients=True,
          gradients=None,
          normalization=False,
          collect=False)
        rs.append(adp_energies.residual_sum)
        assert adp_energies.gradients.size() \
            == xray_structure.scatterers().size()
        assert adp_energies.u_i == None
        assert adp_energies.u_j == None
        assert adp_energies.r_ij == None
      finite_difference_gradients.append((rs[0]-rs[1])/(2*eps))
    sel = flex.bool(xray_structure.scatterers().size(), True)
    xray_structure.scatterers().flags_set_grad_u_iso(sel.iselection())
    adp_energies = adp_restraints.energies_iso(
      geometry_restraints_manager=manager,
      xray_structure=xray_structure,
      parameters=parameters,
      wilson_b=wilson_b,
      use_u_local_only = False,
      use_hd=False,
      compute_gradients=True,
      gradients=None,
      normalization=False,
      collect=False)
    assert approx_equal(adp_energies.gradients, finite_difference_gradients)
  print "OK"