def do_exercise(self, verbose=False): xs = self.xs indices = self.miller_indices(xs.space_group_info()) exp_i_2pi_functor = cctbx.math_module.cos_sin_table(1024) custom_fc_sq = (structure_factors.f_calc_modulus_squared( xs, exp_i_2pi_functor)) std_fc_sq = (structure_factors.f_calc_modulus_squared(xs)) deltas = flex.double() for h in indices: custom_fc_sq.linearise(h) std_fc_sq.linearise(h) deltas.append( abs(custom_fc_sq.f_calc - std_fc_sq.f_calc) / abs(std_fc_sq.f_calc)) stats = median_statistics(deltas) if verbose: if not self.has_printed_header: print("f_calc and sin/cos: |tabulated - std|/|std|") print("median & median absolute deviation") self.has_printed_header = True print("%s: %.12g +/- %.12g" % (xs.space_group_info().type().hall_symbol(), stats.median, stats.median_absolute_deviation)) assert stats.median < 0.01, (str(xs.space_group_info()), stats.median) assert stats.median_absolute_deviation < 0.005, (str( xs.space_group_info()), stats.median_absolute_deviation)
def exercise_trigonometric_ff(): from math import cos, sin, pi sgi = sgtbx.space_group_info("P1") cs = sgi.any_compatible_crystal_symmetry(volume=1000) miller_set = miller.build_set(cs, anomalous_flag=False, d_min=1) miller_set = miller_set.select(flex.random_double(miller_set.size()) < 0.2) for i in range(5): sites = flex.random_double(9) x1, x2, x3 = (matrix.col(sites[:3]), matrix.col(sites[3:6]), matrix.col(sites[6:])) xs = xray.structure(crystal.special_position_settings(cs)) for x in (x1, x2, x3): sc = xray.scatterer(site=x, scattering_type="const") sc.flags.set_grad_site(True) xs.add_scatterer(sc) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in miller_set.indices(): h = matrix.col(h) phi1, phi2, phi3 = 2 * pi * h.dot(x1), 2 * pi * h.dot( x2), 2 * pi * h.dot(x3) fc_mod_sq = 3 + 2 * (cos(phi1 - phi2) + cos(phi2 - phi3) + cos(phi3 - phi1)) g = [] g.extend(-2 * (sin(phi1 - phi2) - sin(phi3 - phi1)) * 2 * pi * h) g.extend(-2 * (sin(phi2 - phi3) - sin(phi1 - phi2)) * 2 * pi * h) g.extend(-2 * (sin(phi3 - phi1) - sin(phi2 - phi3)) * 2 * pi * h) grad_fc_mod_sq = g f_sq.linearise(h) assert approx_equal(f_sq.observable, fc_mod_sq) assert approx_equal(f_sq.grad_observable, grad_fc_mod_sq)
def exercise_trigonometric_ff(): from math import cos, sin, pi sgi = sgtbx.space_group_info("P1") cs = sgi.any_compatible_crystal_symmetry(volume=1000) miller_set = miller.build_set(cs, anomalous_flag=False, d_min=1) miller_set = miller_set.select(flex.random_double(miller_set.size()) < 0.2) for i in xrange(5): sites = flex.random_double(9) x1, x2, x3 = (matrix.col(sites[:3]), matrix.col(sites[3:6]), matrix.col(sites[6:])) xs = xray.structure(crystal.special_position_settings(cs)) for x in (x1, x2, x3): sc = xray.scatterer(site=x, scattering_type="const") sc.flags.set_grad_site(True) xs.add_scatterer(sc) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in miller_set.indices(): h = matrix.col(h) phi1, phi2, phi3 = 2 * pi * h.dot(x1), 2 * pi * h.dot(x2), 2 * pi * h.dot(x3) fc_mod_sq = 3 + 2 * (cos(phi1 - phi2) + cos(phi2 - phi3) + cos(phi3 - phi1)) g = [] g.extend(-2 * (sin(phi1 - phi2) - sin(phi3 - phi1)) * 2 * pi * h) g.extend(-2 * (sin(phi2 - phi3) - sin(phi1 - phi2)) * 2 * pi * h) g.extend(-2 * (sin(phi3 - phi1) - sin(phi2 - phi3)) * 2 * pi * h) grad_fc_mod_sq = g f_sq.linearise(h) assert approx_equal(f_sq.observable, fc_mod_sq) assert approx_equal(f_sq.grad_observable, grad_fc_mod_sq)
def do_exercise(self, verbose=False): xs = self.xs sg = xs.space_group_info().group() origin_centric_case = sg.is_origin_centric() indices = self.miller_indices(xs.space_group_info()) f = structure_factors.f_calc_modulus_squared(xs) f1 = structure_factors.f_calc_modulus_squared(xs) for h in indices: f.linearise(h) fl = f.f_calc f1.evaluate(h) fe = f1.f_calc assert f1.grad_f_calc is None assert approx_equal_relatively(fe, fl, relative_error=1e-12), (fe, fl) if (xs.space_group().is_origin_centric() and not self.inelastic_scattering): for h in indices: f.linearise(h) assert f.f_calc.imag == 0 assert flex.imag(f.grad_f_calc).all_eq(0) eta = 1e-8 xs_forward = xs.deep_copy_scatterers() f_forward = structure_factors.f_calc_modulus_squared(xs_forward) deltas = flex.double() for direction in islice(self.structures_forward(xs, xs_forward, eta), self.n_directions): for h in indices: f.linearise(h) assert approx_equal(abs(f.f_calc)**2, f.observable) f_forward.linearise(h) diff_num = (f_forward.observable - f.observable) / eta diff = f.grad_observable.dot(direction) delta = abs(1 - diff / diff_num) deltas.append(delta) stats = median_statistics(deltas) tol = 1e-5 assert stats.median < tol, (xs.space_group_info().symbol_and_number(), stats.median) assert stats.median_absolute_deviation < tol, ( xs.space_group_info().symbol_and_number(), stats.median_absolute_deviation)
def do_exercise(self, verbose=False): xs = self.xs sg = xs.space_group_info().group() origin_centric_case = sg.is_origin_centric() indices = self.miller_indices(xs.space_group_info()) f = structure_factors.f_calc_modulus_squared(xs) f1 = structure_factors.f_calc_modulus_squared(xs) for h in indices: f.linearise(h) fl = f.f_calc f1.evaluate(h) fe = f1.f_calc assert f1.grad_f_calc is None assert approx_equal_relatively(fe, fl, relative_error=1e-12), (fe, fl) if xs.space_group().is_origin_centric() and not self.inelastic_scattering: for h in indices: f.linearise(h) assert f.f_calc.imag == 0 assert flex.imag(f.grad_f_calc).all_eq(0) eta = 1e-8 xs_forward = xs.deep_copy_scatterers() f_forward = structure_factors.f_calc_modulus_squared(xs_forward) deltas = flex.double() for direction in islice(self.structures_forward(xs, xs_forward, eta), self.n_directions): for h in indices: f.linearise(h) assert approx_equal(abs(f.f_calc) ** 2, f.observable) f_forward.linearise(h) diff_num = (f_forward.observable - f.observable) / eta diff = f.grad_observable.dot(direction) delta = abs(1 - diff / diff_num) deltas.append(delta) stats = median_statistics(deltas) tol = 1e-3 assert stats.median < tol, (str(space_group_info), stats.median) assert stats.median_absolute_deviation < tol, (str(space_group_info), stats.median_absolute_deviation)
def do_exercise(self, verbose=False): xs = self.xs indices = self.miller_indices(xs.space_group_info()) f = structure_factors.f_calc_modulus(xs) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in indices: f.linearise(h) f_sq.linearise(h) assert approx_equal_relatively(f.observable ** 2, f_sq.observable, relative_error=1e-10) grad_f_sq = f_sq.grad_observable two_f_grad_f = 2 * f.observable * f.grad_observable flex.compare_derivatives(two_f_grad_f, grad_f_sq, eps=1e-12)
def do_exercise(self, verbose=False): xs = self.xs indices = self.miller_indices(xs.space_group_info()) f = structure_factors.f_calc_modulus(xs) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in indices: f.linearise(h) f_sq.linearise(h) assert approx_equal_relatively(f.observable**2, f_sq.observable, relative_error=1e-10) grad_f_sq = f_sq.grad_observable two_f_grad_f = (2 * f.observable * f.grad_observable) flex.compare_derivatives(two_f_grad_f, grad_f_sq, eps=1e-12)
def __init__(self, observations, reparametrisation, **kwds): super(klass, self).__init__(reparametrisation.n_independents) self.observations = observations self.reparametrisation = reparametrisation adopt_optional_init_args(self, kwds) if self.f_mask is not None: assert self.f_mask.size() == observations.fo_sq.size() self.one_h_linearisation = direct.f_calc_modulus_squared( self.xray_structure) if self.weighting_scheme == "default": self.weighting_scheme = self.default_weighting_scheme() self.origin_fixing_restraint = self.origin_fixing_restraints_type( self.xray_structure.space_group()) self.taken_step = None self.restraints_normalisation_factor = None
def do_exercise(self, verbose=False): xs = self.xs indices = self.miller_indices(xs.space_group_info()) exp_i_2pi_functor = cctbx.math_module.cos_sin_table(1024) custom_fc_sq = structure_factors.f_calc_modulus_squared(xs, exp_i_2pi_functor) std_fc_sq = structure_factors.f_calc_modulus_squared(xs) deltas = flex.double() for h in indices: custom_fc_sq.linearise(h) std_fc_sq.linearise(h) deltas.append(abs(custom_fc_sq.f_calc - std_fc_sq.f_calc) / abs(std_fc_sq.f_calc)) stats = median_statistics(deltas) if verbose: if not self.has_printed_header: print "f_calc and sin/cos: |tabulated - std|/|std|" print "median & median absolute deviation" self.has_printed_header = True print "%s: %.12g +/- %.12g" % ( xs.space_group_info().type().hall_symbol(), stats.median, stats.median_absolute_deviation, ) assert stats.median < 0.01, (str(xs.space_group_info()), stats.median) assert stats.median_absolute_deviation < 0.005, (str(xs.space_group_info()), stats.median_absolute_deviation)
def __init__(self, observations, reparametrisation, **kwds): super(crystallographic_ls, self).__init__(reparametrisation.n_independents) self.observations = observations self.reparametrisation = reparametrisation adopt_optional_init_args(self, kwds) if self.f_mask is not None: assert self.f_mask.size() == observations.fo_sq.size() self.one_h_linearisation = direct.f_calc_modulus_squared( self.xray_structure) if self.weighting_scheme == "default": self.weighting_scheme = self.default_weighting_scheme() self.origin_fixing_restraint = self.origin_fixing_restraints_type( self.xray_structure.space_group()) self.taken_step = None self.restraints_normalisation_factor = None
def do_exercise(self, verbose=False): xs = self.xs indices = self.miller_indices(xs.space_group_info()) cctbx_structure_factors = xray.structure_factors.from_scatterers_direct( xray_structure=xs, miller_set=miller.set( crystal.symmetry(unit_cell=xs.unit_cell(), space_group_info=xs.space_group_info()), indices)) f = structure_factors.f_calc_modulus_squared(xs) for h, fc in cctbx_structure_factors.f_calc(): f.linearise(h) if fc == 0: assert f.f_calc == 0 else: delta = abs((f.f_calc - fc) / fc) assert delta < 1e-6
def do_exercise(self, verbose=False): xs = self.xs indices = self.miller_indices(xs.space_group_info()) cctbx_structure_factors = xray.structure_factors.from_scatterers_direct( xray_structure=xs, miller_set=miller.set( crystal.symmetry(unit_cell=xs.unit_cell(), space_group_info=xs.space_group_info()), indices ), ) f = structure_factors.f_calc_modulus_squared(xs) for h, fc in cctbx_structure_factors.f_calc(): f.linearise(h) if fc == 0: assert f.f_calc == 0 else: delta = abs((f.f_calc - fc) / fc) assert delta < 1e-6