Exemple #1
0
 def gradients(self):
     return -1. * maptbx.real_space_gradients_simple(
         unit_cell=self.unit_cell,
         density_map=self.density_map,
         sites_cart=self.sites_cart,
         delta=self.real_space_gradients_delta,
         selection=self.selection)
 def _compute_gradients(self):
   return -1.*maptbx.real_space_gradients_simple(
     unit_cell   = self.unit_cell,
     density_map = self.target_map,
     sites_cart  = self.sites_cart,
     delta       = self.real_space_gradients_delta,
     selection   = flex.bool(self.sites_cart.size(), True))
 def _compute_gradients(self):
     return -1. * maptbx.real_space_gradients_simple(
         unit_cell=self.unit_cell,
         density_map=self.target_map,
         sites_cart=self.sites_cart,
         delta=self.real_space_gradients_delta,
         selection=flex.bool(self.sites_cart.size(), True))
Exemple #4
0
 def compute_functional_and_gradients(O):
   if (O.number_of_function_evaluations == 0):
     O.number_of_function_evaluations += 1
     return O.f_start, O.g_start
   O.number_of_function_evaluations += 1
   O.sites_cart_residue = flex.vec3_double(O.x)
   rs_f = maptbx.real_space_target_simple(
     unit_cell=O.unit_cell,
     density_map=O.density_map,
     sites_cart=O.sites_cart_residue,
     selection=flex.bool(O.sites_cart_residue.size(),True))
   O.real_space_target = rs_f
   rs_g = maptbx.real_space_gradients_simple(
     unit_cell=O.unit_cell,
     density_map=O.density_map,
     sites_cart=O.sites_cart_residue,
     delta=O.real_space_gradients_delta,
     selection=flex.bool(O.sites_cart_residue.size(),True))
   O.rs_f = rs_f
   rs_f *= -O.real_space_target_weight
   rs_g *= -O.real_space_target_weight
   if (O.geometry_restraints_manager is None):
     f = rs_f
     g = rs_g
   else:
     O.sites_cart_all.set_selected(O.residue_i_seqs, O.sites_cart_residue)
     gr_e = O.geometry_restraints_manager.energies_sites(
       sites_cart=O.sites_cart_all, compute_gradients=True)
     f = rs_f + gr_e.target
     g = rs_g + gr_e.gradients.select(indices=O.residue_i_seqs)
   return f, g.as_double()
 def gradients(self):
   return -1.*maptbx.real_space_gradients_simple(
     unit_cell   = self.unit_cell,
     density_map = self.density_map,
     sites_cart  = self.sites_cart,
     delta       = self.real_space_gradients_delta,
     selection   = self.selection)
Exemple #6
0
 def compute_functional_and_gradients(O):
   if (O.number_of_function_evaluations == 0):
     O.number_of_function_evaluations += 1
     return O.f_start, O.g_start
   O.number_of_function_evaluations += 1
   O.sites_cart_variable = flex.vec3_double(O.x)
   if (O.real_space_target_weight == 0):
     rs_f = 0.
     rs_g = flex.vec3_double(O.sites_cart_variable.size(), (0,0,0))
   else:
     if (O.local_standard_deviations_radius is None):
       rs_f = maptbx.real_space_target_simple(
         unit_cell   = O.unit_cell,
         density_map = O.density_map,
         sites_cart  = O.sites_cart_variable,
         selection   = O.selection_variable_real_space)
       rs_g = maptbx.real_space_gradients_simple(
         unit_cell   = O.unit_cell,
         density_map = O.density_map,
         sites_cart  = O.sites_cart_variable,
         delta       = O.real_space_gradients_delta,
         selection   = O.selection_variable_real_space)
     else:
       rs_f = local_standard_deviations_target(
         unit_cell=O.unit_cell,
         density_map=O.density_map,
         weight_map=O.weight_map,
         weight_map_scale_factor=O.weight_map_scale_factor,
         sites_cart=O.sites_cart_variable,
         site_radii=O.site_radii)
       rs_g = local_standard_deviations_gradients(
         unit_cell=O.unit_cell,
         density_map=O.density_map,
         weight_map=O.weight_map,
         weight_map_scale_factor=O.weight_map_scale_factor,
         sites_cart=O.sites_cart_variable,
         site_radii=O.site_radii,
         delta=O.real_space_gradients_delta)
     rs_f *= -O.real_space_target_weight
     rs_g *= -O.real_space_target_weight
   if (O.geometry_restraints_manager is None):
     f = rs_f
     g = rs_g
   else:
     if (O.selection_variable is None):
       O.sites_cart = O.sites_cart_variable
     else:
       O.sites_cart.set_selected(O.selection_variable, O.sites_cart_variable)
       if(O.states_collector is not None):
         O.states_collector.add(sites_cart = O.sites_cart)
     gr_e = O.geometry_restraints_manager.energies_sites(
       sites_cart=O.sites_cart,
       compute_gradients=True)
     gr_e_gradients = gr_e.gradients
     if (O.selection_variable is not None):
       gr_e_gradients = gr_e.gradients.select(O.selection_variable)
     f = rs_f + gr_e.target
     g = rs_g + gr_e_gradients
   return f, g.as_double()
Exemple #7
0
 def e_pot(O, sites_moved):
   if (   O.last_sites_moved is None
       or O.last_sites_moved.id() is not sites_moved.id()):
     O.last_sites_moved = sites_moved
     sites_cart = sites_moved
     #
     if (O.reduced_geo_manager is None):
       flags = None
       if (O.orca_experiments):
         flags = cctbx.geometry_restraints.flags.flags(
           nonbonded=False, default=True)
     else:
       # computing nonbonded interactions only with geo_manager,
       # contributions from other restraints below with reduced_geo_manager
       flags = cctbx.geometry_restraints.flags.flags(
         nonbonded=True, default=False)
     geo_energies = O.geo_manager.energies_sites(
       sites_cart=sites_cart,
       flags=flags,
       custom_nonbonded_function=O.custom_nonbonded_function,
       compute_gradients=True)
     if (0): # XXX orca_experiments
       print "geo_energies:"
       geo_energies.show()
     if (0): # XXX orca_experiments
       O.geo_manager.show_sorted(site_labels=O.site_labels)
     O.f = geo_energies.target
     O.g = geo_energies.gradients
     if (O.reduced_geo_manager is not None):
       reduced_geo_energies = O.reduced_geo_manager.energies_sites(
         sites_cart=sites_cart,
         compute_gradients=True)
       O.f += reduced_geo_energies.target
       O.g += reduced_geo_energies.gradients
     O.last_grms = group_args(geo=flex.mean_sq(O.g.as_double())**0.5)
     #
     if (O.density_map is not None):
       rs_f = maptbx.real_space_target_simple(
         unit_cell=O.geo_manager.crystal_symmetry.unit_cell(),
         density_map=O.density_map,
         sites_cart=sites_cart,
         selection=flex.bool(sites_cart.size(),True))
       rs_g = maptbx.real_space_gradients_simple(
         unit_cell=O.geo_manager.crystal_symmetry.unit_cell(),
         density_map=O.density_map,
         sites_cart=sites_cart,
         delta=O.real_space_gradients_delta,
         selection=flex.bool(sites_cart.size(),True))
       rs_f *= -O.real_space_target_weight
       rs_g *= -O.real_space_target_weight
       O.f += rs_f
       O.g += rs_g
       O.last_grms.real = flex.mean_sq(rs_g.as_double())**0.5
       O.last_grms.real_or_xray = "real"
     #
     O.last_grms.total = flex.mean_sq(O.g.as_double())**0.5
   return O.f
Exemple #8
0
 def check():
   map = flex.double(flex.grid(22,26,36).set_focus(22,26,34))
   site_frac = [i/n for i,n in zip(grid_point, map.focus())]
   sites_cart = flex.vec3_double([uc.orthogonalize(site_frac)])
   target = maptbx.real_space_target_simple(
     unit_cell=uc, density_map=map, sites_cart=sites_cart,
     selection=flex.bool(sites_cart.size(), True))
   assert approx_equal(target, 0)
   terms = maptbx.real_space_target_simple_per_site(
     unit_cell=uc, density_map=map, sites_cart=sites_cart)
   assert approx_equal(terms, [0])
   grads = maptbx.real_space_gradients_simple(
     unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1,
     selection=flex.bool(sites_cart.size(), True))
   assert approx_equal(grads, [(0,0,0)])
   grid_point_mod = [i%n for i,n in zip(grid_point, map.focus())]
   map[grid_point_mod] = 1
   target = maptbx.real_space_target_simple(
     unit_cell=uc, density_map=map, sites_cart=sites_cart,
     selection=flex.bool(sites_cart.size(), True))
   assert approx_equal(target, 1)
   terms = maptbx.real_space_target_simple_per_site(
     unit_cell=uc, density_map=map, sites_cart=sites_cart)
   assert approx_equal(terms, [1])
   grads = maptbx.real_space_gradients_simple(
     unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1,
     selection=flex.bool(sites_cart.size(), True))
   assert approx_equal(grads, [(0,0,0)])
   i,j,k = grid_point_mod
   u,v,w = map.focus()
   map[((i+1)%u,j,k)] = 0.3
   map[(i,(j+1)%v,k)] = 0.5
   map[(i,j,(k+1)%w)] = 0.7
   target = maptbx.real_space_target_simple(
     unit_cell=uc, density_map=map, sites_cart=sites_cart,
     selection=flex.bool(sites_cart.size(), True))
   assert approx_equal(target, 1)
   for delta in [0.1, 0.2]:
     grads = maptbx.real_space_gradients_simple(
       unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=delta,
       selection=flex.bool(sites_cart.size(), True))
     assert approx_equal(grads, [(0.3,0.5,0.7)])
def exercise_03():
    """
  Exercise maptbx.target_and_gradients_simple.
  """
    def compute_map(xray_structure, d_min=1.5, resolution_factor=1. / 4):
        fc = xray_structure.structure_factors(d_min=d_min).f_calc()
        fft_map = fc.fft_map(resolution_factor=resolution_factor)
        fft_map.apply_sigma_scaling()
        result = fft_map.real_map_unpadded()
        return result, fc, fft_map

    xrs = random_structure.xray_structure(
        space_group_info=sgtbx.space_group_info("P212121"),
        elements=["N", "C", "O", "S", "P"] * 10,
        volume_per_atom=50)
    map_target, tmp, tmp = compute_map(xray_structure=xrs)
    xrs_sh = xrs.deep_copy_scatterers()
    xrs_sh.shake_sites_in_place(mean_distance=0.8)
    #
    t1 = maptbx.real_space_target_simple(unit_cell=xrs.unit_cell(),
                                         density_map=map_target,
                                         sites_cart=xrs_sh.sites_cart(),
                                         selection=flex.bool(
                                             xrs_sh.scatterers().size(), True))
    g1 = maptbx.real_space_gradients_simple(unit_cell=xrs.unit_cell(),
                                            density_map=map_target,
                                            sites_cart=xrs_sh.sites_cart(),
                                            delta=0.25,
                                            selection=flex.bool(
                                                xrs_sh.scatterers().size(),
                                                True))
    o = maptbx.target_and_gradients_simple(unit_cell=xrs.unit_cell(),
                                           map_target=map_target,
                                           sites_cart=xrs_sh.sites_cart(),
                                           delta=0.25,
                                           selection=flex.bool(
                                               xrs_sh.scatterers().size(),
                                               True))
    assert approx_equal(t1, o.target())
    for gi, gj in zip(g1, o.gradients()):
        assert approx_equal(gi, gj)
Exemple #10
0
def exercise_real_space_gradients_simple(timing):
  uc = uctbx.unit_cell((11,13,17))
  def check():
    map = flex.double(flex.grid(22,26,36).set_focus(22,26,34))
    site_frac = [i/n for i,n in zip(grid_point, map.focus())]
    sites_cart = flex.vec3_double([uc.orthogonalize(site_frac)])
    target = maptbx.real_space_target_simple(
      unit_cell=uc, density_map=map, sites_cart=sites_cart,
      selection=flex.bool(sites_cart.size(), True))
    assert approx_equal(target, 0)
    terms = maptbx.real_space_target_simple_per_site(
      unit_cell=uc, density_map=map, sites_cart=sites_cart)
    assert approx_equal(terms, [0])
    grads = maptbx.real_space_gradients_simple(
      unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1,
      selection=flex.bool(sites_cart.size(), True))
    assert approx_equal(grads, [(0,0,0)])
    grid_point_mod = [i%n for i,n in zip(grid_point, map.focus())]
    map[grid_point_mod] = 1
    target = maptbx.real_space_target_simple(
      unit_cell=uc, density_map=map, sites_cart=sites_cart,
      selection=flex.bool(sites_cart.size(), True))
    assert approx_equal(target, 1)
    terms = maptbx.real_space_target_simple_per_site(
      unit_cell=uc, density_map=map, sites_cart=sites_cart)
    assert approx_equal(terms, [1])
    grads = maptbx.real_space_gradients_simple(
      unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1,
      selection=flex.bool(sites_cart.size(), True))
    assert approx_equal(grads, [(0,0,0)])
    i,j,k = grid_point_mod
    u,v,w = map.focus()
    map[((i+1)%u,j,k)] = 0.3
    map[(i,(j+1)%v,k)] = 0.5
    map[(i,j,(k+1)%w)] = 0.7
    target = maptbx.real_space_target_simple(
      unit_cell=uc, density_map=map, sites_cart=sites_cart,
      selection=flex.bool(sites_cart.size(), True))
    assert approx_equal(target, 1)
    for delta in [0.1, 0.2]:
      grads = maptbx.real_space_gradients_simple(
        unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=delta,
        selection=flex.bool(sites_cart.size(), True))
      assert approx_equal(grads, [(0.3,0.5,0.7)])
  for grid_point in [(0,0,0), (3,4,5), (-3,15,20)]:
    check()
  for i_trial in xrange(10):
    grid_point = [random.randrange(-100,100) for i in [0,1,2]]
    check()
  if (timing): n = 1000000
  else:        n = 10
  sites_cart = flex.vec3_double(flex.random_double(size=n*3)*40-20)
  map = flex.double(flex.grid(22,26,36).set_focus(22,26,34), 1)
  target = maptbx.real_space_target_simple(
    unit_cell=uc, density_map=map, sites_cart=sites_cart,
    selection=flex.bool(sites_cart.size(), True))
  assert approx_equal(target, n)
  t0 = time.time()
  maptbx.real_space_gradients_simple(
    unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1,
    selection=flex.bool(sites_cart.size(), True))
  tm = time.time() - t0
  msg = "real_space_gradients_simple: %.2f s / %d sites" % (tm, n)
  if (tm >= 0.01): msg += ", %.0f sites / s" % (n / tm)
  if (timing): print msg