def run_00():
    time_aniso_u_scaler = 0
    for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric:
        #print symbol, "-"*50
        space_group_info = sgtbx.space_group_info(symbol=symbol)
        xrs = random_structure.xray_structure(
            space_group_info=space_group_info,
            elements=["N"] * 100,
            volume_per_atom=50.0,
            random_u_iso=True)
        # XXX ad a method to adptbx to do this
        point_group = sgtbx.space_group_info(
            symbol=symbol).group().build_derived_point_group()
        adp_constraints = sgtbx.tensor_rank_2_constraints(
            space_group=point_group, reciprocal_space=True)
        u_star = adptbx.u_cart_as_u_star(
            xrs.unit_cell(), adptbx.random_u_cart(u_scale=1, u_min=0.1))
        u_indep = adp_constraints.independent_params(all_params=u_star)
        u_star = adp_constraints.all_params(independent_params=u_indep)
        b_cart_start = adptbx.u_as_b(
            adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star))
        #
        tr = (b_cart_start[0] + b_cart_start[1] + b_cart_start[2]) / 3
        b_cart_start = [
            b_cart_start[0] - tr, b_cart_start[1] - tr, b_cart_start[2] - tr,
            b_cart_start[3], b_cart_start[4], b_cart_start[5]
        ]
        tr = (b_cart_start[0] + b_cart_start[1] + b_cart_start[2]) / 3
        #
        #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr
        F = xrs.structure_factors(d_min=2.0).f_calc()
        u_star = adptbx.u_cart_as_u_star(F.unit_cell(),
                                         adptbx.b_as_u(b_cart_start))
        fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star)
        fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc()
        f_obs = F.customized_copy(data=flex.abs(fc.data() * fbc))
        t0 = time.time()
        #
        obj = bulk_solvent.aniso_u_scaler(
            f_model_abs=flex.abs(fc.data()),
            f_obs=f_obs.data(),
            miller_indices=f_obs.indices(),
            adp_constraint_matrix=adp_constraints.gradient_sum_matrix())
        time_aniso_u_scaler += (time.time() - t0)
        b_cart_final = adptbx.u_as_b(
            adptbx.u_star_as_u_cart(
                f_obs.unit_cell(),
                adp_constraints.all_params(tuple(obj.u_star_independent))))
        #
        obj = bulk_solvent.aniso_u_scaler(f_model_abs=flex.abs(fc.data()),
                                          f_obs=f_obs.data(),
                                          miller_indices=f_obs.indices())
        b_cart_final2 = adptbx.u_as_b(
            adptbx.u_star_as_u_cart(f_obs.unit_cell(), tuple(obj.u_star)))
        #
        assert approx_equal(b_cart_final, b_cart_final2)
        #print "Output b_cart:", " ".join(["%8.4f"%i for i in b_cart_final])
        assert approx_equal(b_cart_start, b_cart_final, 1.e-4)
    print("Time (aniso_u_scaler only): %6.4f" % time_aniso_u_scaler)
def run_00():
  time_aniso_u_scaler = 0
  for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric:
    #print symbol, "-"*50
    space_group_info = sgtbx.space_group_info(symbol = symbol)
    xrs = random_structure.xray_structure(
      space_group_info  = space_group_info,
      elements          = ["N"]*100,
      volume_per_atom   = 50.0,
      random_u_iso      = True)
    # XXX ad a method to adptbx to do this
    point_group = sgtbx.space_group_info(
      symbol=symbol).group().build_derived_point_group()
    adp_constraints = sgtbx.tensor_rank_2_constraints(
      space_group=point_group,
      reciprocal_space=True)
    u_star = adptbx.u_cart_as_u_star(xrs.unit_cell(),
      adptbx.random_u_cart(u_scale=1,u_min=0.1))
    u_indep = adp_constraints.independent_params(all_params=u_star)
    u_star = adp_constraints.all_params(independent_params=u_indep)
    b_cart_start=adptbx.u_as_b(adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star))
    #
    tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3
    b_cart_start = [b_cart_start[0]-tr,b_cart_start[1]-tr,b_cart_start[2]-tr,
           b_cart_start[3],b_cart_start[4],b_cart_start[5]]
    tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3
    #
    #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr
    F = xrs.structure_factors(d_min = 2.0).f_calc()
    u_star = adptbx.u_cart_as_u_star(
      F.unit_cell(), adptbx.b_as_u(b_cart_start))
    fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star)
    fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc()
    f_obs = F.customized_copy(data = flex.abs(fc.data()*fbc))
    t0 = time.time()
    #
    obj = bulk_solvent.aniso_u_scaler(
      f_model_abs    = flex.abs(fc.data()),
      f_obs          = f_obs.data(),
      miller_indices = f_obs.indices(),
      adp_constraint_matrix = adp_constraints.gradient_sum_matrix())
    time_aniso_u_scaler += (time.time()-t0)
    b_cart_final = adptbx.u_as_b(adptbx.u_star_as_u_cart(f_obs.unit_cell(),
      adp_constraints.all_params(tuple(obj.u_star_independent))))
    #
    obj = bulk_solvent.aniso_u_scaler(
      f_model_abs    = flex.abs(fc.data()),
      f_obs          = f_obs.data(),
      miller_indices = f_obs.indices())
    b_cart_final2 = adptbx.u_as_b(adptbx.u_star_as_u_cart(f_obs.unit_cell(),
      tuple(obj.u_star)))
    #
    assert approx_equal(b_cart_final, b_cart_final2)
    #print "Output b_cart:", " ".join(["%8.4f"%i for i in b_cart_final])
    assert approx_equal(b_cart_start, b_cart_final, 1.e-4)
  print "Time (aniso_u_scaler only): %6.4f"%time_aniso_u_scaler
def run_02():
    time_aniso_u_scaler = 0
    for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric:
        #print symbol, "-"*50
        space_group_info = sgtbx.space_group_info(symbol=symbol)
        xrs = random_structure.xray_structure(
            space_group_info=space_group_info,
            elements=["N"] * 100,
            volume_per_atom=50.0,
            random_u_iso=True)
        xrs.scattering_type_registry(table="wk1995")
        # XXX ad a method to adptbx to do this
        point_group = sgtbx.space_group_info(
            symbol=symbol).group().build_derived_point_group()
        adp_constraints = sgtbx.tensor_rank_2_constraints(
            space_group=point_group, reciprocal_space=True)
        u_star = adptbx.u_cart_as_u_star(
            xrs.unit_cell(), adptbx.random_u_cart(u_scale=1, u_min=0.1))
        u_indep = adp_constraints.independent_params(all_params=u_star)
        u_star = adp_constraints.all_params(independent_params=u_indep)
        b_cart_start = adptbx.u_as_b(
            adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star))
        #
        tr = (b_cart_start[0] + b_cart_start[1] + b_cart_start[2]) / 3
        b_cart_start = [
            b_cart_start[0] - tr, b_cart_start[1] - tr, b_cart_start[2] - tr,
            b_cart_start[3], b_cart_start[4], b_cart_start[5]
        ]
        tr = (b_cart_start[0] + b_cart_start[1] + b_cart_start[2]) / 3
        #
        #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr
        reg = xrs.scattering_type_registry(table="wk1995", d_min=1 / 12)
        f_000 = reg.sum_of_scattering_factors_at_diffraction_angle_0()
        F = xrs.structure_factors(d_min=2.0).f_calc()
        i = F.indices()
        i.append([0, 0, 0])
        d = F.data()
        d.append(f_000)
        F = F.customized_copy(indices=i, data=d)

        u_star = adptbx.u_cart_as_u_star(F.unit_cell(),
                                         adptbx.b_as_u(b_cart_start))
        fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star)
        fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc()
        f_obs = F.customized_copy(data=flex.abs(fc.data() * fbc))
        #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
        obj = bulk_solvent.aniso_u_scaler(f_model_abs=flex.abs(fc.data()),
                                          f_obs=f_obs.data(),
                                          miller_indices=f_obs.indices(),
                                          unit_cell=f_obs.unit_cell())
        a = obj.a
        ####
        #print "Input a :", " ".join(["%7.3f"%i for i in a])
        overall_anisotropic_scale = mmtbx.f_model.ext.k_anisotropic(
            f_obs.indices(), a, f_obs.unit_cell())
        #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data()*overall_anisotropic_scale)
        f_obs = abs(fc)
        f_obs = f_obs.customized_copy(data=f_obs.data() *
                                      overall_anisotropic_scale)
        #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
        #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
        t0 = time.time()
        obj = bulk_solvent.aniso_u_scaler(f_model_abs=flex.abs(fc.data()),
                                          f_obs=f_obs.data(),
                                          miller_indices=f_obs.indices(),
                                          unit_cell=f_obs.unit_cell())
        time_aniso_u_scaler += (time.time() - t0)
        overall_anisotropic_scale = mmtbx.f_model.ext.k_anisotropic(
            f_obs.indices(), obj.a, f_obs.unit_cell())
        assert approx_equal(
            bulk_solvent.r_factor(f_obs.data(),
                                  fc.data() * overall_anisotropic_scale), 0.0,
            1.e-2)  # XXX seems to be low
        #print "Output a:", " ".join(["%7.3f"%i for i in obj.a])
        assert approx_equal(a, obj.a, 1.e-4)  # XXX can it be smaller?
        assert overall_anisotropic_scale[len(overall_anisotropic_scale) -
                                         1] == 1
    print("Time (aniso_u_scaler only): %6.4f" % time_aniso_u_scaler)
Beispiel #4
0
 def anisotropic_scaling(self, r_start):
     r_expanal, r_poly, r_expmin = None, None, None
     k_anisotropic_expanal, k_anisotropic_poly, \
       k_anisotropic_expmin = None, None, None
     scale_matrix_expanal, scale_matrix_poly, scale_matrix_expmin = None, None, None
     sel = self.selection_work.data()
     f_model_abs = flex.abs(
         self.core.f_model_no_aniso_scale.data().select(sel))
     f_obs = self.f_obs.data().select(sel)
     mi = self.f_obs.indices().select(sel)
     uc = self.f_obs.unit_cell()
     mi_all = self.f_obs.indices()
     # try exp_anal
     if (self.try_expanal):
         obj = bulk_solvent.aniso_u_scaler(
             f_model_abs=f_model_abs,
             f_obs=f_obs,
             miller_indices=mi,
             adp_constraint_matrix=self.adp_constraints.gradient_sum_matrix(
             ))
         u_star = self.adp_constraints.all_params(
             tuple(obj.u_star_independent))
         scale_matrix_expanal = adptbx.u_as_b(
             adptbx.u_star_as_u_cart(uc, u_star))
         k_anisotropic_expanal = ext.k_anisotropic(mi_all, u_star)
         r_expanal = self.try_scale(k_anisotropic=k_anisotropic_expanal)
         if (self.verbose):
             print >> self.log, "      r_expanal: %6.4f" % r_expanal
     # try poly
     if (self.try_poly):
         obj = bulk_solvent.aniso_u_scaler(f_model_abs=f_model_abs,
                                           f_obs=f_obs,
                                           miller_indices=mi,
                                           unit_cell=uc)
         scale_matrix_poly = obj.a
         k_anisotropic_poly = ext.k_anisotropic(mi_all, obj.a, uc)
         r_poly = self.try_scale(k_anisotropic=k_anisotropic_poly)
         if (self.verbose):
             print >> self.log, "      r_poly   : %6.4f" % r_poly
     # pre-analyze
     force_to_use_expmin = False
     if (k_anisotropic_poly is not None and self.auto and r_poly < r_expanal
             and (k_anisotropic_poly <= 0).count(True) > 0):
         force_to_use_expmin = True
         self.try_expmin = True
     # try expmin
     if (self.try_expmin):
         zero = self.f_obs.select(sel).customized_copy(
             data=flex.complex_double(f_obs.size(), 0))
         fm = mmtbx.f_model.manager_kbu(
             f_obs=self.f_obs.select(sel),
             f_calc=self.core.f_model_no_aniso_scale.select(sel),
             f_masks=[zero],
             f_part1=zero,
             f_part2=zero,
             ss=self.ss)
         obj = kbu_refinery.tgc(
             f_obs=self.f_obs.select(sel),
             f_calc=self.core.f_model_no_aniso_scale.select(sel),
             f_masks=[zero],
             ss=self.ss,
             k_sols=[
                 0,
             ],
             b_sols=[
                 0,
             ],
             u_star=[0, 0, 0, 0, 0, 0])
         obj.minimize_u()
         u_star = obj.kbu.u_star()
         scale_matrix_expmin = adptbx.u_as_b(
             adptbx.u_star_as_u_cart(uc, u_star))
         k_anisotropic_expmin = ext.k_anisotropic(mi_all, u_star)
         r_expmin = self.try_scale(k_anisotropic=k_anisotropic_expmin)
         if (self.verbose):
             print >> self.log, "    r_expmin   : %6.4f" % r_expmin
         if (force_to_use_expmin):
             self.core = self.core.update(
                 k_anisotropic=k_anisotropic_expmin)
             if (self.verbose):
                 self.format_scale_matrix(m=scale_matrix_expmin)
             return
     # select best
     r = [(r_expanal, k_anisotropic_expanal, scale_matrix_expanal),
          (r_poly, k_anisotropic_poly, scale_matrix_poly),
          (r_expmin, k_anisotropic_expmin, scale_matrix_expmin)]
     r_best = r_start
     k_anisotropic_best = None
     scale_matrix_best = None
     for result in r:
         r_factor, k_anisotropic, scale_matrix = result
         if (r_factor is not None and r_factor < r_best):
             r_best = r_factor
             k_anisotropic_best = k_anisotropic.deep_copy()
             scale_matrix_best = scale_matrix[:]
     if (scale_matrix_best is None):
         if (self.verbose):
             print >> self.log, "      result rejected due to r-factor increase"
     else:
         self.scale_matrices = scale_matrix_best
         self.core = self.core.update(k_anisotropic=k_anisotropic_best)
         r_aniso = self.r_factor()
         if (self.verbose):
             self.format_scale_matrix()
             print >> self.log, "      r_final  : %6.4f" % r_aniso
def run_02():
  time_aniso_u_scaler = 0
  for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric:
    #print symbol, "-"*50
    space_group_info = sgtbx.space_group_info(symbol = symbol)
    xrs = random_structure.xray_structure(
      space_group_info  = space_group_info,
      elements          = ["N"]*100,
      volume_per_atom   = 50.0,
      random_u_iso      = True)
    xrs.scattering_type_registry(table = "wk1995")
    # XXX ad a method to adptbx to do this
    point_group = sgtbx.space_group_info(
      symbol=symbol).group().build_derived_point_group()
    adp_constraints = sgtbx.tensor_rank_2_constraints(
      space_group=point_group,
      reciprocal_space=True)
    u_star = adptbx.u_cart_as_u_star(xrs.unit_cell(),
      adptbx.random_u_cart(u_scale=1,u_min=0.1))
    u_indep = adp_constraints.independent_params(all_params=u_star)
    u_star = adp_constraints.all_params(independent_params=u_indep)
    b_cart_start=adptbx.u_as_b(adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star))
    #
    tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3
    b_cart_start = [b_cart_start[0]-tr,b_cart_start[1]-tr,b_cart_start[2]-tr,
           b_cart_start[3],b_cart_start[4],b_cart_start[5]]
    tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3
    #
    #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr
    reg = xrs.scattering_type_registry(table="wk1995", d_min=1/12)
    f_000 = reg.sum_of_scattering_factors_at_diffraction_angle_0()
    F = xrs.structure_factors(d_min = 2.0).f_calc()
    i = F.indices()
    i.append([0,0,0])
    d = F.data()
    d.append(f_000)
    F = F.customized_copy(indices = i, data = d)

    u_star = adptbx.u_cart_as_u_star(
      F.unit_cell(), adptbx.b_as_u(b_cart_start))
    fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star)
    fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc()
    f_obs = F.customized_copy(data = flex.abs(fc.data()*fbc))
    #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
    obj = bulk_solvent.aniso_u_scaler(
      f_model        = fc.data(),
      f_obs          = f_obs.data(),
      miller_indices = f_obs.indices(),
      unit_cell      = f_obs.unit_cell())
    a = obj.a
    ####
    #print "Input a :", " ".join(["%7.3f"%i for i in a])
    overall_anisotropic_scale = mmtbx.f_model.ext.k_anisotropic(
      f_obs.indices(), a, f_obs.unit_cell())
    #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data()*overall_anisotropic_scale)
    f_obs = abs(fc)
    f_obs = f_obs.customized_copy(data = f_obs.data() * overall_anisotropic_scale)
    #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
    #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
    t0 = time.time()
    obj = bulk_solvent.aniso_u_scaler(
      f_model        = fc.data(),
      f_obs          = f_obs.data(),
      miller_indices = f_obs.indices(),
      unit_cell      = f_obs.unit_cell())
    time_aniso_u_scaler += (time.time()-t0)
    overall_anisotropic_scale = mmtbx.f_model.ext.k_anisotropic(
      f_obs.indices(), obj.a, f_obs.unit_cell())
    assert approx_equal(bulk_solvent.r_factor(f_obs.data(),
      fc.data()*overall_anisotropic_scale), 0.0, 1.e-2) # XXX seems to be low
    #print "Output a:", " ".join(["%7.3f"%i for i in obj.a])
    assert approx_equal(a, obj.a, 1.e-4) # XXX can it be smaller?
    assert overall_anisotropic_scale[len(overall_anisotropic_scale)-1]==1
  print "Time (aniso_u_scaler only): %6.4f"%time_aniso_u_scaler
Beispiel #6
0
 def anisotropic_scaling(self, r_start):
   r_expanal, r_poly, r_expmin = None,None,None
   k_anisotropic_expanal, k_anisotropic_poly, \
     k_anisotropic_expmin = None, None, None
   scale_matrix_expanal, scale_matrix_poly, scale_matrix_expmin= None,None,None
   sel         = self.selection_work.data()
   f_model_abs = flex.abs(self.core.f_model_no_aniso_scale.data().select(sel))
   f_obs       = self.f_obs.data().select(sel)
   mi          = self.f_obs.indices().select(sel)
   uc          = self.f_obs.unit_cell()
   mi_all      = self.f_obs.indices()
   # try exp_anal
   if(self.try_expanal):
     obj = bulk_solvent.aniso_u_scaler(
       f_model_abs    = f_model_abs,
       f_obs          = f_obs,
       miller_indices = mi,
       adp_constraint_matrix = self.adp_constraints.gradient_sum_matrix())
     u_star = self.adp_constraints.all_params(tuple(obj.u_star_independent))
     scale_matrix_expanal = adptbx.u_as_b(adptbx.u_star_as_u_cart(uc, u_star))
     k_anisotropic_expanal = ext.k_anisotropic(mi_all, u_star)
     r_expanal = self.try_scale(k_anisotropic = k_anisotropic_expanal)
     if(self.verbose):
       print >> self.log, "      r_expanal: %6.4f"%r_expanal
   # try poly
   if(self.try_poly):
     obj = bulk_solvent.aniso_u_scaler(
       f_model_abs    = f_model_abs,
       f_obs          = f_obs,
       miller_indices = mi,
       unit_cell      = uc)
     scale_matrix_poly = obj.a
     k_anisotropic_poly = ext.k_anisotropic(mi_all, obj.a, uc)
     r_poly = self.try_scale(k_anisotropic = k_anisotropic_poly)
     if(self.verbose):
       print >> self.log, "      r_poly   : %6.4f"%r_poly
   # pre-analyze
   force_to_use_expmin=False
   if(k_anisotropic_poly is not None and self.auto and r_poly<r_expanal and
      (k_anisotropic_poly<=0).count(True)>0):
     force_to_use_expmin = True
     self.try_expmin = True
   # try expmin
   if(self.try_expmin):
     zero = self.f_obs.select(sel).customized_copy(data =
       flex.complex_double(f_obs.size(), 0))
     fm = mmtbx.f_model.manager_kbu(
       f_obs         = self.f_obs.select(sel),
       f_calc        = self.core.f_model_no_aniso_scale.select(sel),
       f_masks       = [zero],
       f_part1       = zero,
       f_part2       = zero,
       ss            = self.ss)
     obj = kbu_refinery.tgc(
       f_obs   = self.f_obs.select(sel),
       f_calc  = self.core.f_model_no_aniso_scale.select(sel),
       f_masks = [zero],
       ss      = self.ss,
       k_sols  = [0,],
       b_sols  = [0,],
       u_star  = [0,0,0,0,0,0])
     obj.minimize_u()
     u_star = obj.kbu.u_star()
     scale_matrix_expmin = adptbx.u_as_b(adptbx.u_star_as_u_cart(uc, u_star))
     k_anisotropic_expmin = ext.k_anisotropic(mi_all, u_star)
     r_expmin = self.try_scale(k_anisotropic = k_anisotropic_expmin)
     if(self.verbose): print >> self.log, "    r_expmin   : %6.4f"%r_expmin
     if(force_to_use_expmin):
       self.core = self.core.update(k_anisotropic = k_anisotropic_expmin)
       if(self.verbose):
         self.format_scale_matrix(m=scale_matrix_expmin)
       return
   # select best
   r = [(r_expanal, k_anisotropic_expanal, scale_matrix_expanal),
        (r_poly,    k_anisotropic_poly,    scale_matrix_poly),
        (r_expmin,  k_anisotropic_expmin,  scale_matrix_expmin)]
   r_best = r_start
   k_anisotropic_best = None
   scale_matrix_best = None
   for result in r:
     r_factor, k_anisotropic, scale_matrix = result
     if(r_factor is not None and r_factor < r_best):
       r_best = r_factor
       k_anisotropic_best = k_anisotropic.deep_copy()
       scale_matrix_best = scale_matrix[:]
   if(scale_matrix_best is None):
     if(self.verbose):
       print >> self.log, "      result rejected due to r-factor increase"
   else:
     self.scale_matrices = scale_matrix_best
     self.core = self.core.update(k_anisotropic = k_anisotropic_best)
     r_aniso = self.r_factor()
     if(self.verbose):
       self.format_scale_matrix()
       print >> self.log, "      r_final  : %6.4f"%r_aniso