Example #1
0
 def target_and_gradients(self, xray_structure, to_compute_weight=False):
   if(to_compute_weight):
     xrs = xray_structure.deep_copy_scatterers()
     # This may be useful to explore:
     #xrs.shake_adp_if_all_equal(b_iso_tolerance = 1.e-3)
     #xrs.shake_adp(spread=10, keep_anisotropic= False)
   else:
     xrs = xray_structure
   if(self.refine_adp): params = xrs.extract_u_iso_or_u_equiv()
   if(self.refine_occ): params = xrs.scatterers().extract_occupancies()
   if(to_compute_weight):
     pmin = flex.min(params)
     pmax = flex.max(params)
     if(abs(pmin-pmax)/abs(pmin+pmax)*2*100<1.e-3):
       pmean = flex.mean(params)
       n_par = params.size()
       params = flex.double()
       for i in xrange(n_par):
         params.append(pmean + 0.1 * pmean * random.choice([-1,0,1]))
   return crystal.adp_iso_local_sphere_restraints_energies(
     pair_sym_table           = self.pair_sym_table,
     orthogonalization_matrix = self.orthogonalization_matrix,
     sites_frac               = self.sites_frac,
     u_isos                   = params,
     selection                = self.selection,
     use_u_iso                = self.selection,
     grad_u_iso               = self.selection,
     sphere_radius            = self.sphere_radius,
     distance_power           = 2,
     average_power            = 1,
     min_u_sum                = 1.e-6,
     compute_gradients        = True,
     collect                  = False)
Example #2
0
 def target_and_gradients(self, xray_structure, to_compute_weight=False):
     if (to_compute_weight):
         xrs = xray_structure.deep_copy_scatterers()
         # This may be useful to explore:
         #xrs.shake_adp_if_all_equal(b_iso_tolerance = 1.e-3)
         #xrs.shake_adp(spread=10, keep_anisotropic= False)
     else:
         xrs = xray_structure
     if (self.refine_adp): params = xrs.extract_u_iso_or_u_equiv()
     if (self.refine_occ): params = xrs.scatterers().extract_occupancies()
     if (to_compute_weight):
         pmin = flex.min(params)
         pmax = flex.max(params)
         if (abs(pmin + pmax) != 0.
                 and abs(pmin - pmax) / abs(pmin + pmax) * 2 * 100 < 1.e-3):
             pmean = flex.mean(params)
             n_par = params.size()
             params = flex.double()
             for i in range(n_par):
                 params.append(pmean +
                               0.1 * pmean * random.choice([-1, 0, 1]))
     return crystal.adp_iso_local_sphere_restraints_energies(
         pair_sym_table=self.pair_sym_table,
         orthogonalization_matrix=self.orthogonalization_matrix,
         sites_frac=self.sites_frac,
         u_isos=params,
         selection=self.selection,
         use_u_iso=self.selection,
         grad_u_iso=self.selection,
         sphere_radius=self.sphere_radius,
         distance_power=2,
         average_power=1,
         min_u_sum=1.e-6,
         compute_gradients=True,
         collect=False)
Example #3
0
 def __init__(self,
              geometry_restraints_manager,
              xray_structure,
              parameters,
              use_u_local_only,
              use_hd,
              wilson_b=None,
              compute_gradients=True,
              gradients=None,
              normalization=False,
              collect=False):
     assert geometry_restraints_manager.plain_pair_sym_table is not None
     assert geometry_restraints_manager.plain_pairs_radius is not None
     assert parameters.sphere_radius \
         <= geometry_restraints_manager.plain_pairs_radius
     scitbx.restraints.energies.__init__(
         self,
         compute_gradients=compute_gradients,
         gradients=gradients,
         gradients_size=xray_structure.scatterers().size(),
         gradients_factory=flex.double,
         normalization=normalization)
     unit_cell = xray_structure.unit_cell()
     if (use_u_local_only):
         u_isos = xray_structure.scatterers().extract_u_iso()
         #assert (u_isos < 0.0).count(True) == 0 XXX
     else:
         u_isos = xray_structure.extract_u_iso_or_u_equiv()
     if (use_hd):
         selection = xray_structure.all_selection()
     else:
         selection = ~xray_structure.hd_selection()
     energies = crystal.adp_iso_local_sphere_restraints_energies(
         pair_sym_table=geometry_restraints_manager.plain_pair_sym_table,
         orthogonalization_matrix=unit_cell.orthogonalization_matrix(),
         sites_frac=xray_structure.sites_frac(),
         u_isos=u_isos,
         selection=selection,
         use_u_iso=xray_structure.use_u_iso(),
         grad_u_iso=xray_structure.scatterers().extract_grad_u_iso(),
         sphere_radius=parameters.sphere_radius,
         distance_power=parameters.distance_power,
         average_power=parameters.average_power,
         min_u_sum=1.e-6,
         compute_gradients=compute_gradients,
         collect=collect)
     self.number_of_restraints += energies.number_of_restraints
     self.residual_sum += energies.residual_sum
     if (compute_gradients):
         self.gradients += energies.gradients
     if (not collect):
         self.u_i = None
         self.u_j = None
         self.r_ij = None
     else:
         self.u_i = energies.u_i
         self.u_j = energies.u_j
         self.r_ij = energies.r_ij
     if (wilson_b is not None and wilson_b > 0
             and parameters.wilson_b_weight is not None
             and parameters.wilson_b_weight > 0):
         wilson_u = adptbx.b_as_u(wilson_b)
         u_diff = flex.mean(u_isos) - wilson_u
         self.number_of_restraints += 1
         if (compute_gradients):
             g_wilson = 2. * u_diff / u_isos.size() / wilson_u
             g_wilson = flex.double(u_isos.size(), g_wilson)
             norm1 = self.gradients.norm()
             norm2 = g_wilson.norm()
             if (norm2 > 0 and parameters.wilson_b_weight_auto):
                 w = norm1 / norm2 * parameters.wilson_b_weight
             else:
                 w = parameters.wilson_b_weight
         else:
             w = parameters.wilson_b_weight
         self.residual_sum += w * u_diff**2 / wilson_u
         if (compute_gradients):
             self.gradients = self.gradients + w * g_wilson
     self.finalize_target_and_gradients()
Example #4
0
 def __init__(self,
       geometry_restraints_manager,
       xray_structure,
       parameters,
       use_u_local_only,
       use_hd,
       wilson_b=None,
       compute_gradients=True,
       gradients=None,
       normalization=False,
       collect=False):
   assert geometry_restraints_manager.plain_pair_sym_table is not None
   assert geometry_restraints_manager.plain_pairs_radius is not None
   assert parameters.sphere_radius \
       <= geometry_restraints_manager.plain_pairs_radius
   scitbx.restraints.energies.__init__(self,
     compute_gradients=compute_gradients,
     gradients=gradients,
     gradients_size=xray_structure.scatterers().size(),
     gradients_factory=flex.double,
     normalization=normalization)
   unit_cell = xray_structure.unit_cell()
   if(use_u_local_only):
     u_isos = xray_structure.scatterers().extract_u_iso()
     #assert (u_isos < 0.0).count(True) == 0 XXX
   else:
     u_isos = xray_structure.extract_u_iso_or_u_equiv()
   if(use_hd):
     selection = xray_structure.all_selection()
   else:
     selection = ~xray_structure.hd_selection()
   energies = crystal.adp_iso_local_sphere_restraints_energies(
     pair_sym_table=geometry_restraints_manager.plain_pair_sym_table,
     orthogonalization_matrix=unit_cell.orthogonalization_matrix(),
     sites_frac=xray_structure.sites_frac(),
     u_isos=u_isos,
     selection = selection,
     use_u_iso = xray_structure.use_u_iso(),
     grad_u_iso= xray_structure.scatterers().extract_grad_u_iso(),
     sphere_radius=parameters.sphere_radius,
     distance_power=parameters.distance_power,
     average_power=parameters.average_power,
     min_u_sum=1.e-6,
     compute_gradients=compute_gradients,
     collect=collect)
   self.number_of_restraints += energies.number_of_restraints
   self.residual_sum += energies.residual_sum
   if (compute_gradients):
     self.gradients += energies.gradients
   if (not collect):
     self.u_i = None
     self.u_j = None
     self.r_ij = None
   else:
     self.u_i = energies.u_i
     self.u_j = energies.u_j
     self.r_ij = energies.r_ij
   if (    wilson_b is not None
       and wilson_b > 0
       and parameters.wilson_b_weight is not None
       and parameters.wilson_b_weight > 0):
     wilson_u = adptbx.b_as_u(wilson_b)
     u_diff = flex.mean(u_isos) - wilson_u
     self.number_of_restraints += 1
     if(compute_gradients):
       g_wilson = 2.*u_diff/u_isos.size()/wilson_u
       g_wilson = flex.double(u_isos.size(), g_wilson)
       norm1 = self.gradients.norm()
       norm2 = g_wilson.norm()
       if(norm2 > 0 and parameters.wilson_b_weight_auto):
         w = norm1 / norm2 * parameters.wilson_b_weight
       else:
         w = parameters.wilson_b_weight
     else:
       w = parameters.wilson_b_weight
     self.residual_sum += w * u_diff**2 / wilson_u
     if (compute_gradients):
       self.gradients = self.gradients + w * g_wilson
   self.finalize_target_and_gradients()