def run():
  mon_lib_srv = monomer_library.server.server()
  ener_lib = monomer_library.server.ener_lib()
  processed_pdb_file = monomer_library.pdb_interpretation.process(
    mon_lib_srv    = mon_lib_srv,
    ener_lib       = ener_lib,
    file_name      = None,
    raw_records    = pdb_str,
    force_symmetry = True)
  pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
  xray_structure = processed_pdb_file.xray_structure()
  pdb_hierarchy.write_pdb_file(
    file_name        = "distorted.pdb",
    crystal_symmetry = xray_structure.crystal_symmetry())
  geometry_restraints = processed_pdb_file.geometry_restraints_manager(
    show_energies = False)
  #geometry_restraints.write_geo_file(file_name='start.geo')
  states = mmtbx.utils.states(
    xray_structure = xray_structure,
    pdb_hierarchy  = pdb_hierarchy)
  states.add(sites_cart = xray_structure.sites_cart())

  riding_h_manager = riding.manager(
    pdb_hierarchy       = pdb_hierarchy,
    geometry_restraints = geometry_restraints)

  # save shaked model in states
  states.add(sites_cart = xray_structure.sites_cart())
  #
  #xray_structure.scatterers().flags_set_grads(state=False)
  xray_structure.scatterers().flags_set_grad_site(
    iselection = xray_structure.all_selection().iselection())
  #xray_structure.show_scatterer_flags_summary()

  use_riding = True
  minimized = lbfgs(
    xray_structure      = xray_structure,
    states              = states,
    geometry_restraints = geometry_restraints,
    riding_h_manager    = riding_h_manager,
    use_riding          = use_riding,
    verbose             = 0)
  minimized.states.write(
    file_name        = "minimized_all_states.pdb",
    crystal_symmetry = xray_structure.crystal_symmetry())
  pdb_hierarchy.adopt_xray_structure(minimized.xray_structure)
  pdb_hierarchy.write_pdb_file(
    file_name        = "minimized.pdb",
    crystal_symmetry = xray_structure.crystal_symmetry())

  target_final = geometry_restraints.energies_sites(
    sites_cart = xray_structure.sites_cart(),
    compute_gradients = True).target
  assert (target_final < 1.5), 'Target of final riding model is too large'
def run():
    # Read and process PDB file
    mon_lib_srv = monomer_library.server.server()
    ener_lib = monomer_library.server.ener_lib()
    processed_pdb_file = monomer_library.pdb_interpretation.process(
        mon_lib_srv=mon_lib_srv,
        ener_lib=ener_lib,
        file_name=None,
        raw_records=pdb_str,
        force_symmetry=True)
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    xray_structure = processed_pdb_file.xray_structure()
    #
    xrs_dc = xray_structure.deep_copy_scatterers()
    pdb_hierarchy.write_pdb_file(
        file_name="start.pdb",
        crystal_symmetry=xray_structure.crystal_symmetry())
    # Create grm
    geometry_restraints = processed_pdb_file.geometry_restraints_manager(
        show_energies=False)
    states = mmtbx.utils.states(xray_structure=xray_structure,
                                pdb_hierarchy=pdb_hierarchy)
    states.add(sites_cart=xray_structure.sites_cart())
    # shake coordinates
    xray_structure.shake_sites_in_place(rms_difference=5.0)
    pdb_hierarchy.adopt_xray_structure(xray_structure)
    pdb_hierarchy.write_pdb_file(
        file_name="distorted.pdb",
        crystal_symmetry=xray_structure.crystal_symmetry())
    states.add(sites_cart=xray_structure.sites_cart())
    #
    xray_structure.scatterers().flags_set_grads(state=False)
    xray_structure.scatterers().flags_set_grad_site(
        iselection=xray_structure.all_selection().iselection())
    minimized = lbfgs(xray_structure=xray_structure,
                      states=states,
                      geometry_restraints=geometry_restraints,
                      verbose=0)
    minimized.states.write(file_name="minimized_all_states.pdb",
                           crystal_symmetry=xray_structure.crystal_symmetry())
    pdb_hierarchy.adopt_xray_structure(minimized.xray_structure)
    pdb_hierarchy.write_pdb_file(
        file_name="minimized.pdb",
        crystal_symmetry=xray_structure.crystal_symmetry())
Exemple #3
0
def exercise_constrained_lbfgs(xray_structure,
                               constraints_list,
                               t_celsius,
                               d_min=0.5,
                               shake_sites_rmsd=0.5,
                               shake_u_iso_spread=0,
                               shake_u_aniso_spread=0,
                               grad_site=True,
                               grad_u_iso=True,
                               grad_u_aniso=False,
                               grad_occupancy=False,
                               grad_fp_fdp=False,
                               lbfgs_m=5,
                               lbfgs_max_iterations=1000,
                               verbose=0):

    xs = xray_structure
    xray.set_scatterer_grad_flags(scatterers=xs.scatterers(),
                                  site=grad_site,
                                  u_iso=grad_u_iso,
                                  u_aniso=grad_u_aniso,
                                  occupancy=grad_occupancy,
                                  fp=grad_fp_fdp,
                                  fdp=grad_fp_fdp,
                                  tan_u_iso=False,
                                  param=0)

    xs0 = xs.deep_copy_scatterers()
    mi = xs0.build_miller_set(anomalous_flag=False, d_min=d_min)
    fo_sq = mi.structure_factors_from_scatterers(
        xs0, algorithm="direct").f_calc().norm()
    fo_sq = fo_sq.customized_copy(sigmas=flex.double(fo_sq.size(), 1))
    fo_sq.set_observation_type_xray_intensity()
    y_obs = fo_sq
    #y_obs = fo_sq.f_sq_as_f()
    if grad_site:
        xs.shake_sites_in_place(rms_difference=shake_sites_rmsd)
    if not grad_u_aniso: shake_u_aniso_spread = 0
    if not grad_u_iso: shake_u_iso_spread = 0
    if grad_u_aniso or grad_u_iso:
        xs.shake_adp(spread=shake_u_iso_spread,
                     aniso_spread=shake_u_aniso_spread)
    xs1 = xs.deep_copy_scatterers()

    core_params = scitbx.lbfgs.core_parameters(m=lbfgs_m,
                                               maxfev=100,
                                               xtol=1e-5)

    connectivity_table = smtbx.utils.connectivity_table(xs0)

    if constraints_list is None:
        from smtbx.development import generate_hydrogen_constraints
        constraints_list = generate_hydrogen_constraints(
            structure=xs0, connectivity_table=connectivity_table)

    reparametrisation = constraints.reparametrisation(xs,
                                                      constraints_list,
                                                      connectivity_table,
                                                      temperature=t_celsius)

    lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
        traditional_convergence_test=False,
        drop_convergence_test_max_drop_eps=1.e-20,
        drop_convergence_test_iteration_coefficient=1,
        min_iterations=500,
        max_iterations=lbfgs_max_iterations)

    minimizer = lbfgs(
        target_functor=xray.target_functors.unified_least_squares_residual(
            y_obs),
        xray_structure=xs,
        reparametrisation=reparametrisation,
        structure_factor_algorithm="direct",
        lbfgs_termination_params=lbfgs_termination_params,
        lbfgs_core_params=core_params,
        reference_structure=xs0,
        verbose=verbose)

    if verbose > 0:
        print "Total parameters: ", xs.n_parameters()
        print "Independent parameters: ", reparametrisation.n_independents

        print "Reference model: "
        xs0.show_angles(distance_cutoff=1.5)
        print
        print "Starting model: "
        xs1.show_angles(distance_cutoff=1.5)
        print
        print "Refined model: "
        xs.show_angles(distance_cutoff=1.5)
        print

        print "n_iter, n_fun: ", minimizer.minimizer.iter(
        ), minimizer.minimizer.nfun()

    h_selection = xs.element_selection('H')

    diff = xray.meaningful_site_cart_differences(
        xs0.select(h_selection, negate=True),
        xs.select(h_selection, negate=True))
    #diff = xray.meaningful_site_cart_differences(xs0, xs)
    #assert diff.max_absolute() < 1e-3
    if verbose > 0:
        diff.show()
        print
    assert diff.max_absolute() < 2e-2, diff.max_absolute()

    diff = xray.meaningful_site_cart_differences(xs0, xs)
    if verbose > 0:
        diff.show()
        print
    # XXX why does this tolerance have to be so high?
    assert diff.max_absolute() < 0.5, diff.max_absolute()
  def __init__(self, gradient_only):
    print "scitbx.lbfgs: use gradient_only line search: ", gradient_only
    adopt_init_args(self, locals())
    self.lbfgs_core_params = scitbx.lbfgs.core_parameters(
                               stpmin=1.e-10,
                               stpmax=500)
    self.lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
      max_iterations = 6000)
    self.f=None
    self.g=None
    self.x = flex.double(x0)
    self.minimizer = scitbx.lbfgs.run(
      gradient_only             = gradient_only,
      target_evaluator          = self,
      core_params               = self.lbfgs_core_params,
      termination_params        = self.lbfgs_termination_params,
      exception_handling_params = scitbx.lbfgs.exception_handling_parameters(
        ignore_line_search_failed_step_at_lower_bound = True))

  def compute_functional_and_gradients(self):
    self.f = func(x1=self.x[0], x2 = self.x[1], x3 = self.x[2])
    self.g = grad(x1=self.x[0], x2 = self.x[1], x3 = self.x[2])
    return self.f, self.g

if(__name__ == "__main__"):
  t=lbfgs(True)
  assert approx_equal(t.x, [2, -5, 500])
  t=lbfgs(False)
  assert approx_equal(t.x, [2, -5, 500])
Exemple #5
0
def exercise_constrained_lbfgs(xray_structure,
                               constraints_list,
                               t_celsius,
                               d_min=0.5,
                               shake_sites_rmsd=0.5,
                               shake_u_iso_spread=0,
                               shake_u_aniso_spread=0,
                               grad_site=True,
                               grad_u_iso=True,
                               grad_u_aniso=False,
                               grad_occupancy=False,
                               grad_fp_fdp=False,
                               lbfgs_m=5,
                               lbfgs_max_iterations=1000,
                               verbose=0):

  xs = xray_structure
  xray.set_scatterer_grad_flags(scatterers=xs.scatterers(),
                                site=grad_site,
                                u_iso=grad_u_iso,
                                u_aniso=grad_u_aniso,
                                occupancy=grad_occupancy,
                                fp=grad_fp_fdp,
                                fdp=grad_fp_fdp,
                                tan_u_iso=False,
                                param=0)

  xs0 = xs.deep_copy_scatterers()
  mi = xs0.build_miller_set(anomalous_flag=False, d_min=d_min)
  fo_sq = mi.structure_factors_from_scatterers(
    xs0, algorithm="direct").f_calc().norm()
  fo_sq = fo_sq.customized_copy(sigmas=flex.double(fo_sq.size(), 1))
  fo_sq.set_observation_type_xray_intensity()
  y_obs = fo_sq
  #y_obs = fo_sq.f_sq_as_f()
  if grad_site:
    xs.shake_sites_in_place(rms_difference=shake_sites_rmsd)
  if not grad_u_aniso: shake_u_aniso_spread = 0
  if not grad_u_iso: shake_u_iso_spread = 0
  if grad_u_aniso or grad_u_iso:
    xs.shake_adp(spread=shake_u_iso_spread, aniso_spread=shake_u_aniso_spread)
  xs1 = xs.deep_copy_scatterers()

  core_params = scitbx.lbfgs.core_parameters(m=lbfgs_m, maxfev=100, xtol=1e-5)

  connectivity_table = smtbx.utils.connectivity_table(xs0)

  if constraints_list is None:
    from smtbx.development import generate_hydrogen_constraints
    constraints_list = generate_hydrogen_constraints(
      structure=xs0, connectivity_table=connectivity_table)

  reparametrisation = constraints.reparametrisation(
    xs,
    constraints_list,
    connectivity_table,
    temperature=t_celsius)

  lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
    traditional_convergence_test=False,
    drop_convergence_test_max_drop_eps=1.e-20,
    drop_convergence_test_iteration_coefficient=1,
    min_iterations=500,
    max_iterations=lbfgs_max_iterations)

  minimizer = lbfgs(
    target_functor=xray.target_functors.unified_least_squares_residual(y_obs),
    xray_structure=xs,
    reparametrisation=reparametrisation,
    structure_factor_algorithm="direct",
    lbfgs_termination_params=lbfgs_termination_params,
    lbfgs_core_params=core_params,
    reference_structure=xs0,
    verbose=verbose)

  if verbose > 0:
    print "Total parameters: ", xs.n_parameters()
    print "Independent parameters: ", reparametrisation.n_independents

    print "Reference model: "
    xs0.show_angles(distance_cutoff=1.5)
    print
    print "Starting model: "
    xs1.show_angles(distance_cutoff=1.5)
    print
    print "Refined model: "
    xs.show_angles(distance_cutoff=1.5)
    print

    print "n_iter, n_fun: ", minimizer.minimizer.iter(), minimizer.minimizer.nfun()

  h_selection = xs.element_selection('H')

  diff = xray.meaningful_site_cart_differences(
    xs0.select(h_selection, negate=True),
    xs.select(h_selection, negate=True))
  #diff = xray.meaningful_site_cart_differences(xs0, xs)
  #assert diff.max_absolute() < 1e-3
  if verbose > 0:
    diff.show()
    print
  assert diff.max_absolute() < 2e-2, diff.max_absolute()

  diff = xray.meaningful_site_cart_differences(xs0, xs)
  if verbose > 0:
    diff.show()
    print
  # XXX why does this tolerance have to be so high?
  assert diff.max_absolute() < 0.5, diff.max_absolute()