def exercise_tensor_constraints_core(crystal_symmetry): from cctbx import crystal from cctbx import adptbx from scitbx import matrix site_symmetry = crystal.special_position_settings( crystal_symmetry).site_symmetry(site=(0, 0, 0)) unit_cell = crystal_symmetry.unit_cell() group = crystal_symmetry.space_group() assert site_symmetry.n_matrices() == group.order_p() for reciprocal_space in [False, True]: c_tensor_constraints = sgtbx.tensor_rank_2_constraints( space_group=group, reciprocal_space=reciprocal_space).row_echelon_form() p_tensor_constraints = python_tensor_constraints( self=group, reciprocal_space=reciprocal_space) assert c_tensor_constraints.all_eq(p_tensor_constraints) adp_constraints = group.adp_constraints() u_cart_p1 = adptbx.random_u_cart() u_star_p1 = adptbx.u_cart_as_u_star(unit_cell, u_cart_p1) u_star = site_symmetry.average_u_star(u_star_p1) f = unit_cell.volume()**(2 / 3.) assert approx_equal( list(matrix.col(group.average_u_star(u_star=u_star_p1)) * f), list(matrix.col(u_star) * f)) independent_params = adp_constraints.independent_params(u_star) assert adp_constraints.n_independent_params() == len(independent_params) assert adp_constraints.n_independent_params() \ + adp_constraints.n_dependent_params() == 6 u_star_vfy = adp_constraints.all_params(independent_params) u_cart = adptbx.u_star_as_u_cart(unit_cell, u_star) u_cart_vfy = adptbx.u_star_as_u_cart(unit_cell, list(u_star_vfy)) assert approx_equal(u_cart_vfy, u_cart)
def exercise_derivatives(space_group_info, out): crystal_symmetry = space_group_info.any_compatible_crystal_symmetry( volume=1000) space_group = space_group_info.group() adp_constraints = space_group.adp_constraints() m = adp_constraints.row_echelon_form() print >> out, matrix.rec(m, (m.size()//6, 6)).mathematica_form( one_row_per_line=True) print >> out, list(adp_constraints.independent_indices) u_cart_p1 = adptbx.random_u_cart() u_star_p1 = adptbx.u_cart_as_u_star(crystal_symmetry.unit_cell(), u_cart_p1) u_star = space_group.average_u_star(u_star_p1) miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, d_min=3, anomalous_flag=False) for h in miller_set.indices(): grads_fin = d_dw_d_u_star_finite(h=h, u_star=u_star) print >> out, "grads_fin:", list(grads_fin) grads_ana = d_dw_d_u_star_analytical(h=h, u_star=u_star) print >> out, "grads_ana:", list(grads_ana) compare_derivatives(grads_ana, grads_fin) curvs_fin = d2_dw_d_u_star_d_u_star_finite(h=h, u_star=u_star) print >> out, "curvs_fin:", list(curvs_fin) curvs_ana = d2_dw_d_u_star_d_u_star_analytical(h=h, u_star=u_star) print >> out, "curvs_ana:", list(curvs_ana) compare_derivatives(curvs_ana, curvs_fin) # u_indep = adp_constraints.independent_params(u_star) grads_indep_fin = d_dw_d_u_indep_finite( adp_constraints=adp_constraints, h=h, u_indep=u_indep) print >> out, "grads_indep_fin:", list(grads_indep_fin) grads_indep_ana = flex.double(adp_constraints.independent_gradients( all_gradients=list(grads_ana))) print >> out, "grads_indep_ana:", list(grads_indep_ana) compare_derivatives(grads_indep_ana, grads_indep_fin) curvs_indep_fin = d2_dw_d_u_indep_d_u_indep_finite( adp_constraints=adp_constraints, h=h, u_indep=u_indep) print >> out, "curvs_indep_fin:", list(curvs_indep_fin) curvs_indep_ana = adp_constraints.independent_curvatures( all_curvatures=curvs_ana) print >> out, "curvs_indep_ana:", list(curvs_indep_ana) compare_derivatives(curvs_indep_ana, curvs_indep_fin) # curvs_indep_mm = None if (str(space_group_info) == "P 1 2 1"): assert list(adp_constraints.independent_indices) == [0,1,2,4] curvs_indep_mm = p2_curv(h, u_star) elif (str(space_group_info) == "P 4"): assert list(adp_constraints.independent_indices) == [1,2] curvs_indep_mm = p4_curv(h, u_star) elif (str(space_group_info) in ["P 3", "P 6"]): assert list(adp_constraints.independent_indices) == [2,3] curvs_indep_mm = p3_curv(h, u_star) elif (str(space_group_info) == "P 2 3"): assert list(adp_constraints.independent_indices) == [2] curvs_indep_mm = p23_curv(h, u_star) if (curvs_indep_mm is not None): curvs_indep_mm = flex.double( curvs_indep_mm).matrix_symmetric_as_packed_u() print >> out, "curvs_indep_mm:", list(curvs_indep_mm) compare_derivatives(curvs_indep_ana, curvs_indep_mm)
def exercise_tensor_constraints_core(crystal_symmetry): from cctbx import crystal from cctbx import adptbx from scitbx import matrix site_symmetry = crystal.special_position_settings( crystal_symmetry).site_symmetry(site=(0,0,0)) unit_cell = crystal_symmetry.unit_cell() group = crystal_symmetry.space_group() assert site_symmetry.n_matrices() == group.order_p() for reciprocal_space in [False, True]: c_tensor_constraints = sgtbx.tensor_rank_2_constraints( space_group=group, reciprocal_space=reciprocal_space).row_echelon_form() p_tensor_constraints = python_tensor_constraints( self=group, reciprocal_space=reciprocal_space) assert c_tensor_constraints.all_eq(p_tensor_constraints) adp_constraints = group.adp_constraints() u_cart_p1 = adptbx.random_u_cart() u_star_p1 = adptbx.u_cart_as_u_star(unit_cell, u_cart_p1) u_star = site_symmetry.average_u_star(u_star_p1) f = unit_cell.volume()**(2/3.) assert approx_equal( list(matrix.col(group.average_u_star(u_star=u_star_p1))*f), list(matrix.col(u_star)*f)) independent_params = adp_constraints.independent_params(u_star) assert adp_constraints.n_independent_params() == len(independent_params) assert adp_constraints.n_independent_params() \ + adp_constraints.n_dependent_params() == 6 u_star_vfy = adp_constraints.all_params(independent_params) u_cart = adptbx.u_star_as_u_cart(unit_cell, u_star) u_cart_vfy = adptbx.u_star_as_u_cart(unit_cell, list(u_star_vfy)) assert approx_equal(u_cart_vfy, u_cart)
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 ru(crystal_symmetry, u_scale=1, u_min=0.1): from cctbx import sgtbx symbol = crystal_symmetry.space_group().type().lookup_symbol() 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(crystal_symmetry.unit_cell(), adptbx.random_u_cart(u_scale=u_scale,u_min=u_min)) u_indep = adp_constraints.independent_params(all_params=u_star) u_star = adp_constraints.all_params(independent_params=u_indep) r = flex.sym_mat3_double() r.append(adptbx.u_star_as_u_cart(crystal_symmetry.unit_cell(), u_star)) return r
def ru(crystal_symmetry, u_scale=1, u_min=0.1): from cctbx import sgtbx symbol = crystal_symmetry.space_group().type().lookup_symbol() 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( crystal_symmetry.unit_cell(), adptbx.random_u_cart(u_scale=u_scale, u_min=u_min)) u_indep = adp_constraints.independent_params(all_params=u_star) u_star = adp_constraints.all_params(independent_params=u_indep) r = flex.sym_mat3_double() r.append(adptbx.u_star_as_u_cart(crystal_symmetry.unit_cell(), u_star)) return r
def __init__(self): sgi = sgtbx.space_group_info("Hall: %s" % self.hall) cs = sgi.any_compatible_crystal_symmetry(volume=1000) xs = xray.structure(crystal.special_position_settings(cs)) for i, sc in enumerate(self.scatterers()): sc.flags.set_use_u_iso(False).set_use_u_aniso(True)\ .set_grad_u_aniso(True) xs.add_scatterer(sc) site_symm = xs.site_symmetry_table().get(i) u_cart = adptbx.random_u_cart(u_scale=self.random_u_cart_scale) u_star = adptbx.u_cart_as_u_star(cs.unit_cell(), u_cart) xs.scatterers()[-1].u_star = site_symm.average_u_star(u_star) self.xray_structure = xs mi = cs.build_miller_set(d_min=0.5, anomalous_flag=False) ma = mi.structure_factors_from_scatterers(xs, algorithm="direct").f_calc() self.fo_sq = ma.norm().customized_copy( sigmas=flex.double(ma.size(), 1.))
def exercise_02_b_cart_sym_constr(d_min = 2.0, tolerance = 1.e-6): for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric: space_group_info = sgtbx.space_group_info(symbol = symbol) xray_structure = get_xray_structure_random(space_group_info) sg = xray_structure.space_group() uc = xray_structure.unit_cell() u_cart_p1 = adptbx.random_u_cart(u_scale=5, u_min=5) u_star_p1 = adptbx.u_cart_as_u_star(uc, u_cart_p1) b_cart_1 = adptbx.u_star_as_u_cart(uc, u_star_p1) b_cart_2 = adptbx.u_star_as_u_cart(uc, sg.average_u_star(u_star = u_star_p1)) for b_cart in (b_cart_1, b_cart_2): f_obs, r_free_flags = \ get_f_obs_freer(d_min = d_min, k_sol = 0, b_sol = 0, b_cart = b_cart, xray_structure = xray_structure) fmodel = mmtbx.f_model.manager( r_free_flags = r_free_flags, f_obs = f_obs, xray_structure = xray_structure) for flag in (True, False): params = bss.master_params.extract() params.bulk_solvent = False params.anisotropic_scaling = True params.k_sol_b_sol_grid_search = False params.minimization_k_sol_b_sol = False params.minimization_b_cart = True params.symmetry_constraints_on_b_cart = flag params.max_iterations = 50 params.min_iterations = 50 result = bss.bulk_solvent_and_scales( fmodel_kbu = fmodel.fmodel_kbu(), params = params) if(flag == False and approx_equal(b_cart, b_cart_1, out=None)): assert approx_equal(result.b_cart(), b_cart, tolerance) if(flag == True and approx_equal(b_cart, b_cart_2, out=None)): assert approx_equal(result.b_cart(), b_cart, tolerance) if(flag == False and approx_equal(b_cart, b_cart_2, out=None)): assert approx_equal(result.b_cart(), b_cart, tolerance) if(flag == True and approx_equal(b_cart, b_cart_1, out=None)): for u2, ufm in zip(b_cart_2, result.b_cart()): if(abs(u2) < 1.e-6): assert approx_equal(ufm, 0.0, tolerance)
def exercise_02_b_cart_sym_constr(d_min=2.0, tolerance=1.e-6): for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric: space_group_info = sgtbx.space_group_info(symbol=symbol) xray_structure = get_xray_structure_random(space_group_info) sg = xray_structure.space_group() uc = xray_structure.unit_cell() u_cart_p1 = adptbx.random_u_cart(u_scale=5, u_min=5) u_star_p1 = adptbx.u_cart_as_u_star(uc, u_cart_p1) b_cart_1 = adptbx.u_star_as_u_cart(uc, u_star_p1) b_cart_2 = adptbx.u_star_as_u_cart(uc, sg.average_u_star(u_star=u_star_p1)) for b_cart in (b_cart_1, b_cart_2): f_obs, r_free_flags = \ get_f_obs_freer(d_min = d_min, k_sol = 0, b_sol = 0, b_cart = b_cart, xray_structure = xray_structure) fmodel = mmtbx.f_model.manager(r_free_flags=r_free_flags, f_obs=f_obs, xray_structure=xray_structure) flag = True params = bss.master_params.extract() params.number_of_macro_cycles = 3 params.bulk_solvent = False params.anisotropic_scaling = True params.k_sol_b_sol_grid_search = False params.minimization_k_sol_b_sol = False params.minimization_b_cart = True params.symmetry_constraints_on_b_cart = flag params.max_iterations = 50 params.min_iterations = 50 result = bss.bulk_solvent_and_scales( fmodel_kbu=fmodel.fmodel_kbu(), params=params) if (flag == True and approx_equal(b_cart, b_cart_2, out=None)): assert approx_equal(result.b_cart(), b_cart, tolerance) if (flag == True and approx_equal(b_cart, b_cart_1, out=None)): for u2, ufm in zip(b_cart_2, result.b_cart()): if (abs(u2) < 1.e-6): assert approx_equal(ufm, 0.0, tolerance)
def exercise(small=1.e-9): for symbol in ["P 1"]: space_group_info = sgtbx.space_group_info(symbol=symbol) random_xray_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=["N"] * 10, volume_per_atom=50.0, random_u_iso=False, u_iso=adptbx.b_as_u(20.0)) sg = random_xray_structure.space_group() uc = random_xray_structure.unit_cell() print(symbol, uc) print() sys.stdout.flush() counter = 0 trials = 100000 i = 0 branch_0 = 0 branch_1 = 0 branch_1_1 = 0 branch_1_2 = 0 branch_1_2_1 = 0 branch_1_2_2 = 0 branch_1_2_3 = 0 branch_1_2_3_1 = 0 branch_1_2_3_2 = 0 while i < trials: i += 1 counter += 1 r = random.random() if (r < 0.333): m = adptbx.random_u_cart(u_scale=20. * random.random(), u_min=0) n = adptbx.random_u_cart(u_scale=20. * random.random(), u_min=0) while 1: for ind in range(6): r = random.random() m = flex.double(m) if (r > 0.5): m[ind] = n[ind] m = list(m) if (adptbx.is_positive_definite(m, 0) and adptbx.is_positive_definite(n, 0)): break elif (r >= 0.333 and r < 0.66): m, n = branch_3_mn() else: m, n = branch_2_mn(0) m_dc = copy.deepcopy(m) n_dc = copy.deepcopy(n) m = factor(list(m), i) n = factor(list(n), i) qq = tools.common(n, m, small) #assert approx_equal(m,m_dc) #assert approx_equal(n,n_dc) if (qq.branch_0()): branch_0 += 1 if (qq.branch_1()): branch_1 += 1 if (qq.branch_1_1()): branch_1_1 += 1 if (qq.branch_1_2()): branch_1_2 += 1 if (qq.branch_1_2_1()): branch_1_2_1 += 1 if (qq.branch_1_2_2()): branch_1_2_2 += 1 if (qq.branch_1_2_3()): branch_1_2_3 += 1 if (qq.branch_1_2_3_1()): branch_1_2_3_1 += 1 if (qq.branch_1_2_3_2()): branch_1_2_3_2 += 1 if (counter >= 10000): counter = 0 print("." * 30, symbol) print("i= ", i, "out of ", trials) print("branch_0 = ", branch_0) print("branch_1 = ", branch_1) print("branch_1_1 = ", branch_1_1) print("branch_1_2 = ", branch_1_2) print("branch_1_2_1 = ", branch_1_2_1) print("branch_1_2_2 = ", branch_1_2_2) print("branch_1_2_3 = ", branch_1_2_3) print("branch_1_2_3_1 = ", branch_1_2_3_1) print("branch_1_2_3_2 = ", branch_1_2_3_2) sys.stdout.flush()
def build_scatterers(self, elements, sites_frac=None, grid=None, t_centre_of_inversion=None): existing_sites = [scatterer.site for scatterer in self.scatterers()] if (sites_frac is None): all_sites = random_sites( special_position_settings=self, existing_sites=existing_sites, n_new=len(elements), min_hetero_distance=self.min_distance, general_positions_only=self.general_positions_only, grid=grid, t_centre_of_inversion=t_centre_of_inversion) else: assert len(sites_frac) == len(elements) all_sites = existing_sites + list(sites_frac) assert len(all_sites) <= self.n_scatterers sf_dict = {} for element in elements: if (not sf_dict.has_key(element)): sf_dict[element] = eltbx.xray_scattering.best_approximation(element) fp = 0 fdp = 0 n_existing = self.scatterers().size() i_label = n_existing for element,site in zip(elements, all_sites[n_existing:]): i_label += 1 scatterer = xray.scatterer( label=element + str(i_label), scattering_type=element, site=site) site_symmetry = scatterer.apply_symmetry( self.unit_cell(), self.space_group(), self.min_distance_sym_equiv()) if (self.random_f_prime_d_min): f0 = sf_dict[element].at_d_star_sq(1./self.random_f_prime_d_min**2) assert f0 > 0 fp = -f0 * random.random() * self.random_f_prime_scale if (self.random_f_double_prime): f0 = sf_dict[element].at_d_star_sq(0) fdp = f0 * random.random() * self.random_f_double_prime_scale scatterer.fp = fp scatterer.fdp = fdp if (self.use_u_iso_): scatterer.flags.set_use_u_iso_only() u_iso = self.u_iso if (not u_iso and self.random_u_iso): u_iso = random.random() * self.random_u_iso_scale \ + self.random_u_iso_min scatterer.u_iso = u_iso if (self.use_u_aniso): scatterer.flags.set_use_u_aniso_only() run_away_counter = 0 while 1: run_away_counter += 1 assert run_away_counter < 100 u_cart = adptbx.random_u_cart(u_scale=self.random_u_cart_scale) scatterer.u_star = site_symmetry.average_u_star( adptbx.u_cart_as_u_star( self.unit_cell(), u_cart)) u_cart = adptbx.u_star_as_u_cart(self.unit_cell(), scatterer.u_star) eigenvalues = adptbx.eigenvalues(u_cart) if (min(eigenvalues) > 0.001): break if (self.random_occupancy): scatterer.occupancy = self.random_occupancy_min \ + (1-self.random_occupancy_min)*random.random() self.add_scatterer(scatterer)
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)
def exercise(space_group_info, n_elements=10, table="wk1995", d_min=2.0, k_sol=0.35, b_sol=45.0, b_cart=None, quick=False, verbose=0): xray_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=(("O", "N", "C") * (n_elements // 3 + 1))[:n_elements], volume_per_atom=100, min_distance=1.5, general_positions_only=True, random_u_iso=False, random_occupancy=False) xray_structure.scattering_type_registry(table=table) sg = xray_structure.space_group() uc = xray_structure.unit_cell() u_cart_1 = adptbx.random_u_cart(u_scale=5, u_min=5) u_star_1 = adptbx.u_cart_as_u_star(uc, u_cart_1) b_cart = adptbx.u_star_as_u_cart(uc, sg.average_u_star(u_star=u_star_1)) for anomalous_flag in [False, True]: scatterers = xray_structure.scatterers() if (anomalous_flag): assert scatterers.size() >= 7 for i in [1, 7]: scatterers[i].fp = -0.2 scatterers[i].fdp = 5 have_non_zero_fdp = True else: for i in [1, 7]: scatterers[i].fp = 0 scatterers[i].fdp = 0 have_non_zero_fdp = False f_obs = abs( xray_structure.structure_factors( d_min=d_min, anomalous_flag=anomalous_flag, cos_sin_table=sfg_params.cos_sin_table, algorithm=sfg_params.algorithm).f_calc()) f_obs_comp = f_obs.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm=sfg_params.algorithm, cos_sin_table=sfg_params.cos_sin_table).f_calc() f_obs = abs(f_obs_comp) flags = f_obs.generate_r_free_flags(fraction=0.1, max_free=99999999) #flags = flags.array(data = flex.bool(f_obs.data().size(), False)) xrs = xray_structure.deep_copy_scatterers() xrs.shake_sites_in_place(rms_difference=0.3) for target in mmtbx.refinement.targets.target_names: if target == "mli": continue if (quick): if (target not in ["ls_wunit_k1", "ml", "mlhl", "ml_sad"]): continue if (target == "mlhl"): if (have_non_zero_fdp): continue # XXX gradients not correct! experimental_phases = generate_random_hl(miller_set=f_obs) else: experimental_phases = None if (target == "ml_sad" and (not anomalous_flag or mmtbx.refinement.targets.phaser is None)): continue print(" ", target) xray.set_scatterer_grad_flags(scatterers=xrs.scatterers(), site=True) ss = 1. / flex.pow2(f_obs.d_spacings().data()) / 4. u_star = adptbx.u_cart_as_u_star(f_obs.unit_cell(), adptbx.b_as_u(b_cart)) k_anisotropic = mmtbx.f_model.ext.k_anisotropic( f_obs.indices(), u_star) k_mask = mmtbx.f_model.ext.k_mask(ss, k_sol, b_sol) fmodel = mmtbx.f_model.manager( xray_structure=xrs, f_obs=f_obs, r_free_flags=flags, target_name=target, abcd=experimental_phases, sf_and_grads_accuracy_params=sfg_params, k_mask=k_mask, k_anisotropic=k_anisotropic, mask_params=masks.mask_master_params.extract()) fmodel.update_xray_structure(xray_structure=xrs, update_f_calc=True, update_f_mask=True) xray.set_scatterer_grad_flags( scatterers=fmodel.xray_structure.scatterers(), site=True) fmodel.update_xray_structure(update_f_calc=True) t_f = fmodel.target_functor() t_f.prepare_for_minimization() gs = t_f(compute_gradients=True).d_target_d_site_cart().as_double() gfd = finite_differences_site(target_functor=t_f) cc = flex.linear_correlation(gs, gfd).coefficient() if (0 or verbose): print("ana:", list(gs)) print("fin:", list(gfd)) print("rat:", [f / a for a, f in zip(gs, gfd)]) print(target, "corr:", cc, space_group_info) print() diff = gs - gfd diff /= max(1, flex.max(flex.abs(gfd))) tolerance = 1.2e-5 assert approx_equal(abs(flex.min(diff)), 0.0, tolerance) assert approx_equal(abs(flex.mean(diff)), 0.0, tolerance) assert approx_equal(abs(flex.max(diff)), 0.0, tolerance) assert approx_equal(cc, 1.0, tolerance) fmodel.model_error_ml()
def build_scatterers(self, elements, sites_frac=None, grid=None, t_centre_of_inversion=None): existing_sites = [scatterer.site for scatterer in self.scatterers()] if (sites_frac is None): all_sites = random_sites( special_position_settings=self, existing_sites=existing_sites, n_new=len(elements), min_hetero_distance=self.min_distance, general_positions_only=self.general_positions_only, grid=grid, t_centre_of_inversion=t_centre_of_inversion) else: assert len(sites_frac) == len(elements) all_sites = existing_sites + list(sites_frac) assert len(all_sites) <= self.n_scatterers sf_dict = {} for element in elements: if (not sf_dict.has_key(element)): sf_dict[element] = eltbx.xray_scattering.best_approximation( element) fp = 0 fdp = 0 n_existing = self.scatterers().size() i_label = n_existing for element, site in zip(elements, all_sites[n_existing:]): i_label += 1 scatterer = xray.scatterer(label=element + str(i_label), scattering_type=element, site=site) site_symmetry = scatterer.apply_symmetry( self.unit_cell(), self.space_group(), self.min_distance_sym_equiv()) if (self.random_f_prime_d_min): f0 = sf_dict[element].at_d_star_sq( 1. / self.random_f_prime_d_min**2) assert f0 > 0 fp = -f0 * random.random() * self.random_f_prime_scale if (self.random_f_double_prime): f0 = sf_dict[element].at_d_star_sq(0) fdp = f0 * random.random() * self.random_f_double_prime_scale scatterer.fp = fp scatterer.fdp = fdp if (self.use_u_iso_): scatterer.flags.set_use_u_iso_only() u_iso = self.u_iso if (not u_iso and self.random_u_iso): u_iso = random.random() * self.random_u_iso_scale \ + self.random_u_iso_min scatterer.u_iso = u_iso if (self.use_u_aniso): scatterer.flags.set_use_u_aniso_only() run_away_counter = 0 while 1: run_away_counter += 1 assert run_away_counter < 100 u_cart = adptbx.random_u_cart( u_scale=self.random_u_cart_scale) scatterer.u_star = site_symmetry.average_u_star( adptbx.u_cart_as_u_star(self.unit_cell(), u_cart)) u_cart = adptbx.u_star_as_u_cart(self.unit_cell(), scatterer.u_star) eigenvalues = adptbx.eigenvalues(u_cart) if (min(eigenvalues) > 0.001): break if (self.random_occupancy): scatterer.occupancy = self.random_occupancy_min \ + (1-self.random_occupancy_min)*random.random() self.add_scatterer(scatterer)
def random_aniso_adp(space_group, unit_cell, u_scale=2, u_min=0): return adptbx.u_star_as_u_cart(unit_cell, space_group.average_u_star( u_star = adptbx.u_cart_as_u_star(unit_cell, adptbx.random_u_cart( u_scale=u_scale, u_min=u_min))))
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
def exercise(space_group_info, n_elements = 10, table = "wk1995", d_min = 2.0, k_sol = 0.35, b_sol = 45.0, b_cart = None, quick=False, verbose=0): xray_structure = random_structure.xray_structure( space_group_info = space_group_info, elements =(("O","N","C")*(n_elements//3+1))[:n_elements], volume_per_atom = 100, min_distance = 1.5, general_positions_only = True, random_u_iso = False, random_occupancy = False) xray_structure.scattering_type_registry(table = table) sg = xray_structure.space_group() uc = xray_structure.unit_cell() u_cart_1 = adptbx.random_u_cart(u_scale=5, u_min=5) u_star_1 = adptbx.u_cart_as_u_star(uc, u_cart_1) b_cart = adptbx.u_star_as_u_cart(uc, sg.average_u_star(u_star = u_star_1)) for anomalous_flag in [False, True]: scatterers = xray_structure.scatterers() if (anomalous_flag): assert scatterers.size() >= 7 for i in [1,7]: scatterers[i].fp = -0.2 scatterers[i].fdp = 5 have_non_zero_fdp = True else: for i in [1,7]: scatterers[i].fp = 0 scatterers[i].fdp = 0 have_non_zero_fdp = False f_obs = abs(xray_structure.structure_factors( d_min = d_min, anomalous_flag = anomalous_flag, cos_sin_table = sfg_params.cos_sin_table, algorithm = sfg_params.algorithm).f_calc()) f_obs_comp = f_obs.structure_factors_from_scatterers( xray_structure = xray_structure, algorithm = sfg_params.algorithm, cos_sin_table = sfg_params.cos_sin_table).f_calc() f_obs = abs(f_obs_comp) flags = f_obs.generate_r_free_flags(fraction = 0.1, max_free = 99999999) #flags = flags.array(data = flex.bool(f_obs.data().size(), False)) xrs = xray_structure.deep_copy_scatterers() xrs.shake_sites_in_place(rms_difference=0.3) for target in mmtbx.refinement.targets.target_names: if (quick): if (target not in ["ls_wunit_k1", "ml", "mlhl", "ml_sad"]): continue if (target == "mlhl"): if (have_non_zero_fdp): continue # XXX gradients not correct! experimental_phases = generate_random_hl(miller_set=f_obs) else: experimental_phases = None if (target == "ml_sad" and (not anomalous_flag or mmtbx.refinement.targets.phaser is None)): continue print " ",target xray.set_scatterer_grad_flags( scatterers = xrs.scatterers(), site = True) ss = 1./flex.pow2(f_obs.d_spacings().data()) / 4. u_star = adptbx.u_cart_as_u_star( f_obs.unit_cell(), adptbx.b_as_u(b_cart)) k_anisotropic = mmtbx.f_model.ext.k_anisotropic( f_obs.indices(), u_star) k_mask = mmtbx.f_model.ext.k_mask(ss, k_sol, b_sol) fmodel = mmtbx.f_model.manager( xray_structure = xrs, f_obs = f_obs, r_free_flags = flags, target_name = target, abcd = experimental_phases, sf_and_grads_accuracy_params = sfg_params, k_mask = k_mask, k_anisotropic = k_anisotropic, mask_params = masks.mask_master_params.extract()) fmodel.update_xray_structure( xray_structure=xrs, update_f_calc=True, update_f_mask=True) xray.set_scatterer_grad_flags( scatterers=fmodel.xray_structure.scatterers(), site=True) fmodel.update_xray_structure(update_f_calc=True) t_f = fmodel.target_functor() t_f.prepare_for_minimization() gs = t_f(compute_gradients=True).d_target_d_site_cart().as_double() gfd = finite_differences_site(target_functor=t_f) cc = flex.linear_correlation(gs, gfd).coefficient() if (0 or verbose): print "ana:", list(gs) print "fin:", list(gfd) print "rat:", [f/a for a,f in zip(gs,gfd)] print target, "corr:", cc, space_group_info print diff = gs - gfd diff /= max(1, flex.max(flex.abs(gfd))) tolerance = 1.2e-5 assert approx_equal(abs(flex.min(diff) ), 0.0, tolerance) assert approx_equal(abs(flex.mean(diff)), 0.0, tolerance) assert approx_equal(abs(flex.max(diff) ), 0.0, tolerance) assert approx_equal(cc, 1.0, tolerance) fmodel.model_error_ml()
def random_aniso_adp(space_group, unit_cell, u_scale=2, u_min=0): return adptbx.u_star_as_u_cart( unit_cell, space_group.average_u_star(u_star=adptbx.u_cart_as_u_star( unit_cell, adptbx.random_u_cart(u_scale=u_scale, u_min=u_min))))
def exercise(small = 1.e-9): for symbol in ["P 1"]: space_group_info = sgtbx.space_group_info(symbol = symbol) random_xray_structure = random_structure.xray_structure( space_group_info = space_group_info, elements = ["N"]*10, volume_per_atom = 50.0, random_u_iso = False, u_iso = adptbx.b_as_u(20.0)) sg = random_xray_structure.space_group() uc = random_xray_structure.unit_cell() print symbol, uc print sys.stdout.flush() counter = 0 trials = 100000 i = 0 branch_0 = 0 branch_1 = 0 branch_1_1 = 0 branch_1_2 = 0 branch_1_2_1 = 0 branch_1_2_2 = 0 branch_1_2_3 = 0 branch_1_2_3_1 = 0 branch_1_2_3_2 = 0 while i < trials: i += 1 counter += 1 r = random.random() if(r < 0.333): m = adptbx.random_u_cart(u_scale=20.*random.random(), u_min=0) n = adptbx.random_u_cart(u_scale=20.*random.random(), u_min=0) while 1: for ind in xrange(6): r = random.random() m = flex.double(m) if(r > 0.5): m[ind] = n[ind] m = list(m) if(adptbx.is_positive_definite(m,0) and adptbx.is_positive_definite(n,0)): break elif(r>=0.333 and r<0.66): m,n = branch_3_mn() else: m,n = branch_2_mn(0) m_dc = copy.deepcopy(m) n_dc = copy.deepcopy(n) m = factor(list(m), i) n = factor(list(n), i) qq = tools.common(n,m,small) #assert approx_equal(m,m_dc) #assert approx_equal(n,n_dc) if(qq.branch_0() ): branch_0 += 1 if(qq.branch_1() ): branch_1 += 1 if(qq.branch_1_1() ): branch_1_1 += 1 if(qq.branch_1_2() ): branch_1_2 += 1 if(qq.branch_1_2_1() ): branch_1_2_1 += 1 if(qq.branch_1_2_2() ): branch_1_2_2 += 1 if(qq.branch_1_2_3() ): branch_1_2_3 += 1 if(qq.branch_1_2_3_1()): branch_1_2_3_1 += 1 if(qq.branch_1_2_3_2()): branch_1_2_3_2 += 1 if (counter >= 10000): counter = 0 print "."*30, symbol print "i= ", i, "out of ", trials print "branch_0 = ", branch_0 print "branch_1 = ", branch_1 print "branch_1_1 = ", branch_1_1 print "branch_1_2 = ", branch_1_2 print "branch_1_2_1 = ", branch_1_2_1 print "branch_1_2_2 = ", branch_1_2_2 print "branch_1_2_3 = ", branch_1_2_3 print "branch_1_2_3_1 = ", branch_1_2_3_1 print "branch_1_2_3_2 = ", branch_1_2_3_2 sys.stdout.flush()
def exercise_negative_parameters(verbose=0): structure_default = xray.structure( crystal_symmetry = crystal.symmetry( unit_cell=((10,13,17,75,80,85)), space_group_symbol="P 1"), scatterers=flex.xray_scatterer([ xray.scatterer(label="C", site=(0,0,0), u=0.25)])) negative_gaussian = eltbx.xray_scattering.gaussian((1,2), (2,3), -4) for i_trial in xrange(7): structure = structure_default.deep_copy_scatterers() scatterer = structure.scatterers()[0] if (i_trial == 1): scatterer.occupancy *= -1 elif (i_trial == 2): structure.scattering_type_registry(custom_dict={"C": negative_gaussian}) elif (i_trial == 3): scatterer.u_iso *= -1 elif (i_trial == 4): u_cart = adptbx.random_u_cart(u_scale=1, u_min=-1.1) assert max(adptbx.eigenvalues(u_cart)) < 0 u_star = adptbx.u_cart_as_u_star(structure.unit_cell(), u_cart) scatterer.u_star = u_star scatterer.flags.set_use_u_aniso_only() elif (i_trial == 5): scatterer.fp = -10 elif (i_trial == 6): scatterer.fp = -3 f_direct = structure.structure_factors( d_min=1, algorithm="direct", cos_sin_table=False).f_calc() f_fft = structure.structure_factors( d_min=1, algorithm="fft", quality_factor=1.e8, wing_cutoff=1.e-10).f_calc() if (i_trial == 2): assert negative_gaussian.at_d_star_sq(f_fft.d_star_sq().data()).all_lt(0) if (i_trial in [5,6]): f = structure.scattering_type_registry().gaussian_not_optional( scattering_type="C").at_d_star_sq(f_fft.d_star_sq().data()) if (i_trial == 5): assert flex.max(f) + scatterer.fp < 0 else: assert flex.max(f) + scatterer.fp > 0 assert flex.min(f) + scatterer.fp < 0 cc = flex.linear_correlation( abs(f_direct).data(), abs(f_fft).data()).coefficient() if (cc < 0.999): raise AssertionError("i_trial=%d, correlation=%.6g" % (i_trial, cc)) elif (0 or verbose): print "correlation=%.6g" % cc # # very simple test of gradient calculations with negative parameters structure_factor_gradients = \ cctbx.xray.structure_factors.gradients( miller_set=f_direct, cos_sin_table=False) target_functor = xray.target_functors.intensity_correlation( f_obs=abs(f_direct)) target_result = target_functor(f_fft, True) xray.set_scatterer_grad_flags(scatterers = structure.scatterers(), site = True, u_iso = True, u_aniso = True, occupancy = True, fp = True, fdp = True) for algorithm in ["direct", "fft"]: grads = structure_factor_gradients( xray_structure=structure, u_iso_refinable_params=None, miller_set=f_direct, d_target_d_f_calc=target_result.derivatives(), n_parameters = structure.n_parameters(), algorithm=algorithm).packed()
def exercise_negative_parameters(verbose=0): structure_default = xray.structure( crystal_symmetry=crystal.symmetry(unit_cell=((10, 13, 17, 75, 80, 85)), space_group_symbol="P 1"), scatterers=flex.xray_scatterer( [xray.scatterer(label="C", site=(0, 0, 0), u=0.25)])) negative_gaussian = eltbx.xray_scattering.gaussian((1, 2), (2, 3), -4) for i_trial in range(7): structure = structure_default.deep_copy_scatterers() scatterer = structure.scatterers()[0] if (i_trial == 1): scatterer.occupancy *= -1 elif (i_trial == 2): structure.scattering_type_registry( custom_dict={"C": negative_gaussian}) elif (i_trial == 3): scatterer.u_iso *= -1 elif (i_trial == 4): u_cart = adptbx.random_u_cart(u_scale=1, u_min=-1.1) assert max(adptbx.eigenvalues(u_cart)) < 0 u_star = adptbx.u_cart_as_u_star(structure.unit_cell(), u_cart) scatterer.u_star = u_star scatterer.flags.set_use_u_aniso_only() elif (i_trial == 5): scatterer.fp = -10 elif (i_trial == 6): scatterer.fp = -3 f_direct = structure.structure_factors(d_min=1, algorithm="direct", cos_sin_table=False).f_calc() f_fft = structure.structure_factors(d_min=1, algorithm="fft", quality_factor=1.e8, wing_cutoff=1.e-10).f_calc() if (i_trial == 2): assert negative_gaussian.at_d_star_sq( f_fft.d_star_sq().data()).all_lt(0) if (i_trial in [5, 6]): f = structure.scattering_type_registry().gaussian_not_optional( scattering_type="C").at_d_star_sq(f_fft.d_star_sq().data()) if (i_trial == 5): assert flex.max(f) + scatterer.fp < 0 else: assert flex.max(f) + scatterer.fp > 0 assert flex.min(f) + scatterer.fp < 0 cc = flex.linear_correlation(abs(f_direct).data(), abs(f_fft).data()).coefficient() if (cc < 0.999): raise AssertionError("i_trial=%d, correlation=%.6g" % (i_trial, cc)) elif (0 or verbose): print("correlation=%.6g" % cc) # # very simple test of gradient calculations with negative parameters structure_factor_gradients = \ cctbx.xray.structure_factors.gradients( miller_set=f_direct, cos_sin_table=False) target_functor = xray.target_functors.intensity_correlation( f_obs=abs(f_direct)) target_result = target_functor(f_fft, True) xray.set_scatterer_grad_flags(scatterers=structure.scatterers(), site=True, u_iso=True, u_aniso=True, occupancy=True, fp=True, fdp=True) for algorithm in ["direct", "fft"]: grads = structure_factor_gradients( xray_structure=structure, u_iso_refinable_params=None, miller_set=f_direct, d_target_d_f_calc=target_result.derivatives(), n_parameters=structure.n_parameters(), algorithm=algorithm).packed()