def exercise_geometry(): xs = quartz() uc = xs.unit_cell() flags = xs.scatterer_flags() for f in flags: f.set_grad_site(True) xs.set_scatterer_flags(flags) cov = flex.double( (1e-8, 1e-9, 2e-9, 3e-9, 4e-9, 5e-9, 2e-8, 1e-9, 2e-9, 3e-9, 4e-9, 3e-8, 1e-9, 2e-9, 3e-9, 2e-8, 1e-9, 2e-9, 3e-8, 1e-9, 4e-8)) cell_vcv = flex.double((3e-2, 3e-2, 0, 0, 0, 0, 3e-2, 0, 0, 0, 0, 4e-2, 0, 0, 0, 0, 0, 0, 0, 0, 0)) param_map = xs.parameter_map() cov_cart = covariance.orthogonalize_covariance_matrix(cov, uc, param_map) O = matrix.sqr(uc.orthogonalization_matrix()) F = matrix.sqr(uc.fractionalization_matrix()) sites_cart = xs.sites_cart() sites_frac = xs.sites_frac() # distances rt_mx_ji = sgtbx.rt_mx('-y,x-y,z-1/3') sites = (sites_cart[0], uc.orthogonalize(rt_mx_ji * sites_frac[1])) d = geometry.distance(sites) assert approx_equal(d.distance_model, 1.6159860469110217) v = matrix.col(sites[1]) - matrix.col(sites[0]) r_inv_cart = (O * matrix.sqr(rt_mx_ji.r().inverse().as_double()) * F) g = d.d_distance_d_sites() g = matrix.row(g[0] + tuple(r_inv_cart * matrix.col(g[1]))) f = g * matrix.sqr(cov_cart.matrix_packed_u_as_symmetric()) * g.transpose() assert approx_equal(d.variance(cov_cart, uc, rt_mx_ji), f[0], eps=1e-15) assert approx_equal(0.0018054494791580823, d.variance(cov_cart, cell_vcv, uc, rt_mx_ji)) rt_mx_ji = sgtbx.rt_mx('x+1,y,z') sites = (sites_cart[0], uc.orthogonalize(rt_mx_ji * sites_frac[0])) d = geometry.distance(sites) assert approx_equal(d.distance_model, uc.parameters()[0]) assert approx_equal(cell_vcv.matrix_packed_u_diagonal()[0], d.variance(cov_cart, cell_vcv, uc, rt_mx_ji)) # angles rt_mx_ji = sgtbx.rt_mx('x-y,x,z-2/3') rt_mx_ki = sgtbx.rt_mx('-y,x-y,z-1/3') r_inv_cart_ji = (O * matrix.sqr(rt_mx_ji.r().inverse().as_double()) * F) r_inv_cart_ki = (O * matrix.sqr(rt_mx_ki.r().inverse().as_double()) * F) cov_a = covariance.extract_covariance_matrix_for_sites( flex.size_t([1, 0, 1]), cov_cart, param_map) sites = (uc.orthogonalize(rt_mx_ji * sites_frac[1]), sites_cart[0], uc.orthogonalize(rt_mx_ki * sites_frac[1])) a = geometry.angle(sites) assert approx_equal(a.angle_model, 101.30738566828551) g = a.d_angle_d_sites() g = matrix.row( tuple(r_inv_cart_ji * matrix.col(g[0])) + g[1] + tuple(r_inv_cart_ki * matrix.col(g[2]))) f = g * matrix.sqr(cov_a.matrix_packed_u_as_symmetric()) * g.transpose() assert approx_equal(a.variance(cov_a, uc, (rt_mx_ji, sgtbx.rt_mx(), rt_mx_ki)), f[0], eps=1e-15) assert approx_equal( 0.0042632511984529199, a.variance(cov_a, cell_vcv, uc, (rt_mx_ji, sgtbx.rt_mx(), rt_mx_ki)))
def is_coplanar(x,y,z): #triple product is zero; (X x Y).Z from scitbx import matrix x = matrix.row(x) y = matrix.row(y) z = matrix.row(z) return x.cross(y).dot(z)==0
def is_coplanar(x, y, z): #triple product is zero; (X x Y).Z from scitbx import matrix x = matrix.row(x) y = matrix.row(y) z = matrix.row(z) return x.cross(y).dot(z) == 0
def __init__(self,params): import cPickle as pickle from dxtbx.model.beam import beam_factory from dxtbx.model.detector import detector_factory from dxtbx.model.crystal import crystal_model from cctbx.crystal_orientation import crystal_orientation,basis_type from dxtbx.model.experiment.experiment_list import Experiment, ExperimentList from scitbx import matrix self.experiments = ExperimentList() self.unique_file_names = [] self.params = params data = pickle.load(open(self.params.output.prefix+"_frame.pickle","rb")) frames_text = data.split("\n") for item in frames_text: tokens = item.split(' ') wavelength = float(tokens[order_dict["wavelength"]]) beam = beam_factory.simple(wavelength = wavelength) detector = detector_factory.simple( sensor = detector_factory.sensor("PAD"), # XXX shouldn't hard code for XFEL distance = float(tokens[order_dict["distance"]]), beam_centre = [float(tokens[order_dict["beam_x"]]), float(tokens[order_dict["beam_y"]])], fast_direction = "+x", slow_direction = "+y", pixel_size = [self.params.pixel_size,self.params.pixel_size], image_size = [1795,1795], # XXX obviously need to figure this out ) reciprocal_matrix = matrix.sqr([float(tokens[order_dict[k]]) for k in [ 'res_ori_1','res_ori_2','res_ori_3','res_ori_4','res_ori_5','res_ori_6','res_ori_7','res_ori_8','res_ori_9']]) ORI = crystal_orientation(reciprocal_matrix, basis_type.reciprocal) direct = matrix.sqr(ORI.direct_matrix()) crystal = crystal_model( real_space_a = matrix.row(direct[0:3]), real_space_b = matrix.row(direct[3:6]), real_space_c = matrix.row(direct[6:9]), space_group_symbol = "P63", # XXX obviously another gap in the database paradigm mosaicity = float(tokens[order_dict["half_mosaicity_deg"]]), ) crystal.domain_size = float(tokens[order_dict["domain_size_ang"]]) #if isoform is not None: # newB = matrix.sqr(isoform.fractionalization_matrix()).transpose() # crystal.set_B(newB) self.experiments.append(Experiment(beam=beam, detector=None, #dummy for now crystal=crystal)) self.unique_file_names.append(tokens[order_dict["unique_file_name"]]) self.show_summary()
def exercise(args): verbose = "--verbose" in args if (not verbose): out = StringIO() else: out = sys.stdout for i_trial in xrange(10): for n_sites in xrange(2,5+1): ops = [] for i in xrange(3): ops.append(matrix.sqr(flex.random_double(size=9, factor=2)-1)) sites = [] for i in xrange(n_sites): sites.append(matrix.col(flex.random_double(size=3, factor=4)-2)) hkl = matrix.row(flex.random_double(size=3, factor=4)-2) sf = exp_i_hx(sites=sites, ops=ops, hkl=hkl) for obs_factor in [1, 1.1]: obs = abs(sf.f()) * obs_factor grads_fin = d_exp_i_hx_d_sites_finite( sites=sites, ops=ops, obs=obs, hkl=hkl) print >> out, "grads_fin:", list(grads_fin) tf = least_squares(obs=obs, calc=sf.f()) grads_ana = sf.d_target_d_sites(target=tf) print >> out, "grads_ana:", list(grads_ana) compare_derivatives(grads_ana, grads_fin) curvs_fin = d2_exp_i_hx_d_sites_finite( sites=sites, ops=ops, obs=obs, hkl=hkl) print >> out, "curvs_fin:", list(curvs_fin) curvs_ana = sf.d2_target_d_sites(target=tf) print >> out, "curvs_ana:", list(curvs_ana) compare_derivatives(curvs_ana, curvs_fin) print >> out print "OK"
def exercise(args): verbose = "--verbose" in args if (not verbose): out = StringIO() else: out = sys.stdout for i_trial in range(100): ops = [] for i in range(3): ops.append(matrix.sqr(flex.random_double(size=9, factor=4) - 2)) sites = [] for i in range(2): sites.append(matrix.col(flex.random_double(size=3, factor=4) - 2)) hkl = matrix.row(flex.random_double(size=3, factor=4) - 2) ca = cos_alpha(sites=sites, ops=ops, hkl=hkl) grads_fin = d_cos_alpha_d_sites_finite(sites=sites, ops=ops, hkl=hkl) print("grads_fin:", list(grads_fin), file=out) grads_ana = ca.d_sites() print("grads_ana:", list(grads_ana), file=out) assert approx_equal(grads_ana, grads_fin) curvs_fin = d2_cos_alpha_d_sites_finite(sites=sites, ops=ops, hkl=hkl) print("curvs_fin:", list(curvs_fin), file=out) curvs_ana = ca.d2_sites() print("curvs_ana:", list(curvs_ana), file=out) assert approx_equal(curvs_ana, curvs_fin, 1.e-5) print(file=out) print("OK")
def exercise(args): verbose = "--verbose" in args if (not verbose): out = StringIO() else: out = sys.stdout for i_trial in xrange(100): ops = [] for i in xrange(3): ops.append(matrix.sqr(flex.random_double(size=9, factor=4) - 2)) site = matrix.col(flex.random_double(size=3, factor=4) - 2) hkl = matrix.row(flex.random_double(size=3, factor=4) - 2) ca = cos_alpha(site=site, ops=ops, hkl=hkl) grads_fin = d_cos_alpha_d_site_finite(site=site, ops=ops, hkl=hkl) print >> out, "grads_fin:", list(grads_fin) grads_ana = ca.d_site() print >> out, "grads_ana:", list(grads_ana) assert approx_equal(grads_ana, grads_fin) curvs_fin = d2_cos_alpha_d_site_finite(site=site, ops=ops, hkl=hkl) print >> out, "curvs_fin:", list(curvs_fin) curvs_ana = ca.d2_site() print >> out, "curvs_ana:", list(curvs_ana) assert approx_equal(curvs_ana, curvs_fin) print >> out print "OK"
def absence_detected(self, hkllist): self.hkl = hkllist self.N = self.hkl.size() self.flag = None from cctbx.sgtbx.sub_lattice_tools import generate_matrix allGenerators = [] # include identity for on_means calculation for mod in [6, 5, 4, 3, 2]: for matS in generate_matrix(mod): allGenerators.append(matS) self.allGenerators = allGenerators for idx, matS in enumerate(allGenerators): invS = matS.inverse() idx_possible = True for miller in [matrix.row(i) for i in hkllist]: transformed_miller = miller * invS.transpose() #print transformed_miller for element in (transformed_miller).elems: if element.denominator() > 1: idx_possible = False #print "transformation",invS.transpose().elems,{True:"is",False:"not"}[idx_possible],"possible" if idx_possible: print "There are systematic absences. Applying transformation", invS.transpose( ).elems self.cb_op = invS.transpose().inverse( ) # not sure if transpose.inverse or just inverse self.flag = True return 1 return 0
def exercise(): ma = miller.array( miller.set(crystal.symmetry(unit_cell=(5,5,5, 90, 90, 90), space_group=sgtbx.space_group('P 2x')), indices=flex.miller_index( [(1,0,0), (0,1,0), (0,0,1), (-1,0,0), (0,-1,0), (0,0,-1), (1,1,0), (1,0,1), (0,1,1), (-1,-1,0), (-1,0,-1), (0,-1,-1), (1,-1,0), (1,0,-1), (0,1,-1), (-1,1,0), (-1,0,1), (0,-1,1), (1,1,1), (-1,1,1), (1,-1,1), (1,1,-1), (-1,-1,-1), (1,-1,-1), (-1,1,-1), (-1,-1,1)])), data=flex.complex_double(flex.random_double(26), flex.random_double(26))) f_at_h = dict(zip(ma.indices(), ma.data())) for op in ("-x, y+1/2, -z", "x+1/2, -y, z-1/2"): op = sgtbx.rt_mx(op) original, transformed = ma.common_sets( ma.change_basis(sgtbx.change_of_basis_op(op.inverse()))) for h, f in original: assert f == f_at_h[h] for h, op_f in transformed: assert approx_equal( op_f, f_at_h[h*op.r()]*exp(1j*2*pi*row(h).dot(col(op.t().as_double()))))
def absence_detected(self,hkllist): self.hkl = hkllist self.N = self.hkl.size() self.flag = None from cctbx.sgtbx.sub_lattice_tools import generate_matrix allGenerators=[] # include identity for on_means calculation for mod in [6,5,4,3,2]: for matS in generate_matrix(mod): allGenerators.append(matS) self.allGenerators = allGenerators for idx,matS in enumerate(allGenerators): invS = matS.inverse() idx_possible = True for miller in [matrix.row(i) for i in hkllist]: transformed_miller = miller*invS.transpose() #print transformed_miller for element in (transformed_miller).elems: if element.denominator() > 1: idx_possible = False #print "transformation",invS.transpose().elems,{True:"is",False:"not"}[idx_possible],"possible" if idx_possible: print "There are systematic absences. Applying transformation",invS.transpose().elems self.cb_op = invS.transpose().inverse() # not sure if transpose.inverse or just inverse self.flag = True return 1 return 0
def exercise(args): verbose = "--verbose" in args if (not verbose): out = StringIO() else: out = sys.stdout for i_trial in xrange(100): ops = [] for i in xrange(3): ops.append(matrix.sqr(flex.random_double(size=9, factor=4)-2)) u = matrix.col((flex.random_double(size=6, factor=2)-1)*1.e-3) hkl = matrix.row(flex.random_double(size=3, factor=4)-2) dw = debye_waller(u=u, ops=ops, hkl=hkl) grads_fin = d_debye_waller_d_u_finite(u=u, ops=ops, hkl=hkl) print >> out, "grads_fin:", list(grads_fin) grads_ana = dw.d_u() print >> out, "grads_ana:", list(grads_ana) compare_derivatives(grads_ana, grads_fin) curvs_fin = d2_debye_waller_d_u_finite(u=u, ops=ops, hkl=hkl) print >> out, "curvs_fin:", list(curvs_fin) curvs_ana = dw.d2_u() print >> out, "curvs_ana:", list(curvs_ana) compare_derivatives(curvs_ana, curvs_fin) print >> out print "OK"
def exercise(args): verbose = "--verbose" in args if (not verbose): out = StringIO() else: out = sys.stdout for i_trial in range(100): ops = [] for i in range(3): ops.append(matrix.sqr(flex.random_double(size=9, factor=4) - 2)) u = matrix.col((flex.random_double(size=6, factor=2) - 1) * 1.e-3) hkl = matrix.row(flex.random_double(size=3, factor=4) - 2) dw = debye_waller(u=u, ops=ops, hkl=hkl) grads_fin = d_debye_waller_d_u_finite(u=u, ops=ops, hkl=hkl) print("grads_fin:", list(grads_fin), file=out) grads_ana = dw.d_u() print("grads_ana:", list(grads_ana), file=out) compare_derivatives(grads_ana, grads_fin) curvs_fin = d2_debye_waller_d_u_finite(u=u, ops=ops, hkl=hkl) print("curvs_fin:", list(curvs_fin), file=out) curvs_ana = dw.d2_u() print("curvs_ana:", list(curvs_ana), file=out) compare_derivatives(curvs_ana, curvs_fin) print(file=out) print("OK")
def exercise(args): verbose = "--verbose" in args if (not verbose): out = StringIO() else: out = sys.stdout for i_trial in range(10): for n_sites in range(2,5+1): ops = [] for i in range(3): ops.append(matrix.sqr(flex.random_double(size=9, factor=2)-1)) sites = [] for i in range(n_sites): sites.append(matrix.col(flex.random_double(size=3, factor=4)-2)) hkl = matrix.row(flex.random_double(size=3, factor=4)-2) sf = exp_i_hx(sites=sites, ops=ops, hkl=hkl) for obs_factor in [1, 1.1]: obs = abs(sf.f()) * obs_factor grads_fin = d_exp_i_hx_d_sites_finite( sites=sites, ops=ops, obs=obs, hkl=hkl) print("grads_fin:", list(grads_fin), file=out) tf = least_squares(obs=obs, calc=sf.f()) grads_ana = sf.d_target_d_sites(target=tf) print("grads_ana:", list(grads_ana), file=out) compare_derivatives(grads_ana, grads_fin) curvs_fin = d2_exp_i_hx_d_sites_finite( sites=sites, ops=ops, obs=obs, hkl=hkl) print("curvs_fin:", list(curvs_fin), file=out) curvs_ana = sf.d2_target_d_sites(target=tf) print("curvs_ana:", list(curvs_ana), file=out) compare_derivatives(curvs_ana, curvs_fin) print(file=out) print("OK")
def f(self): result = 0 tphkl = 2 * math.pi * matrix.col(self.hkl) for scatterer in self.scatterers: w = scatterer.weight() if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) gaussian = self.scattering_type_registry.gaussian_not_optional( scattering_type=scatterer.scattering_type) f0 = gaussian.at_d_star_sq(self.d_star_sq) ffp = f0 + scatterer.fp fdp = scatterer.fdp ff = ffp + 1j * fdp for s in self.space_group: s_site = s * scatterer.site alpha = matrix.col(s_site).dot(tphkl) if (scatterer.flags.use_u_aniso()): r = s.r().as_rational().as_float() s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j*alpha) result += w * dw * ff * e return result
def __init__(self, h, site_constraints, independent_params): self.h = matrix.row(h) self.site_constraints = site_constraints self.independent_params = independent_params self.site = matrix.col( self.site_constraints.all_params( independent_params=self.independent_params))
def exercise(): ma = miller.array(miller.set( crystal.symmetry(unit_cell=(5, 5, 5, 90, 90, 90), space_group=sgtbx.space_group('P 2x')), indices=flex.miller_index([(1, 0, 0), (0, 1, 0), (0, 0, 1), (-1, 0, 0), (0, -1, 0), (0, 0, -1), (1, 1, 0), (1, 0, 1), (0, 1, 1), (-1, -1, 0), (-1, 0, -1), (0, -1, -1), (1, -1, 0), (1, 0, -1), (0, 1, -1), (-1, 1, 0), (-1, 0, 1), (0, -1, 1), (1, 1, 1), (-1, 1, 1), (1, -1, 1), (1, 1, -1), (-1, -1, -1), (1, -1, -1), (-1, 1, -1), (-1, -1, 1)])), data=flex.complex_double(flex.random_double(26), flex.random_double(26))) f_at_h = dict(zip(ma.indices(), ma.data())) for op in ("-x, y+1/2, -z", "x+1/2, -y, z-1/2"): op = sgtbx.rt_mx(op) original, transformed = ma.common_sets( ma.change_basis(sgtbx.change_of_basis_op(op.inverse()))) for h, f in original: assert f == f_at_h[h] for h, op_f in transformed: assert approx_equal( op_f, f_at_h[h * op.r()] * exp(1j * 2 * pi * row(h).dot(col(op.t().as_double()))))
def exercise(args): verbose = "--verbose" in args if not verbose: out = StringIO() else: out = sys.stdout for i_trial in xrange(100): ops = [] for i in xrange(3): ops.append(matrix.sqr(flex.random_double(size=9, factor=4) - 2)) sites = [] for i in xrange(2): sites.append(matrix.col(flex.random_double(size=3, factor=4) - 2)) hkl = matrix.row(flex.random_double(size=3, factor=4) - 2) ca = cos_alpha(sites=sites, ops=ops, hkl=hkl) grads_fin = d_cos_alpha_d_sites_finite(sites=sites, ops=ops, hkl=hkl) print >> out, "grads_fin:", list(grads_fin) grads_ana = ca.d_sites() print >> out, "grads_ana:", list(grads_ana) assert approx_equal(grads_ana, grads_fin) curvs_fin = d2_cos_alpha_d_sites_finite(sites=sites, ops=ops, hkl=hkl) print >> out, "curvs_fin:", list(curvs_fin) curvs_ana = ca.d2_sites() print >> out, "curvs_ana:", list(curvs_ana) assert approx_equal(curvs_ana, curvs_fin, 1.0e-5) print >> out print "OK"
def f(self): result = 0 tphkl = 2 * math.pi * matrix.col(self.hkl) for scatterer in self.scatterers: w = scatterer.weight() if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) gaussian = self.scattering_type_registry.gaussian_not_optional( scattering_type=scatterer.scattering_type) f0 = gaussian.at_d_star_sq(self.d_star_sq) ffp = f0 + scatterer.fp fdp = scatterer.fdp ff = ffp + 1j * fdp for s in self.space_group: s_site = s * scatterer.site alpha = matrix.col(s_site).dot(tphkl) if (scatterer.flags.use_u_aniso()): r = s.r().as_rational().as_float() s_u_star_s = r * matrix.sym( sym_mat3=scatterer.u_star) * r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot( matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j * alpha) result += w * dw * ff * e return result
def f(self): result = 0 for op in self.ops: op_u = (op*matrix.sym(sym_mat3=self.u)*op.transpose()).as_sym_mat3() huh = (matrix.row(self.hkl) \ * matrix.sym(sym_mat3=op_u)).dot(matrix.col(self.hkl)) result += math.exp(mtps * huh) return result
def __init__(self, crystal_symmetry, cell_covariance_matrix=None, format="coreCIF"): self.format = format.lower() assert self.format in ("corecif", "mmcif") if self.format == "mmcif": self.separator = '.' else: self.separator = '_' self.cif_block = model.block() cell_prefix = '_cell%s' % self.separator if crystal_symmetry.space_group() is not None: sym_loop = model.loop(data=OrderedDict(( ('_space_group_symop' + self.separator + 'id', range(1, len(crystal_symmetry.space_group()) + 1)), ('_space_group_symop' + self.separator + 'operation_xyz', [s.as_xyz() for s in crystal_symmetry.space_group()])))) self.cif_block.add_loop(sym_loop) sg_prefix = '_space_group%s' % self.separator sg_type = crystal_symmetry.space_group_info().type() sg = sg_type.group() self.cif_block[sg_prefix + 'crystal_system'] = sg.crystal_system().lower() self.cif_block[sg_prefix + 'IT_number'] = sg_type.number() self.cif_block[sg_prefix + 'name_H-M_alt'] = sg_type.lookup_symbol() self.cif_block[sg_prefix + 'name_Hall'] = sg_type.hall_symbol() sg_prefix = '_symmetry%s' % self.separator self.cif_block[sg_prefix + 'space_group_name_H-M'] = sg_type.lookup_symbol() self.cif_block[sg_prefix + 'space_group_name_Hall'] = sg_type.hall_symbol() self.cif_block[sg_prefix + 'Int_Tables_number'] = sg_type.number() if crystal_symmetry.unit_cell() is not None: uc = crystal_symmetry.unit_cell() params = list(uc.parameters()) volume = uc.volume() if cell_covariance_matrix is not None: diag = cell_covariance_matrix.matrix_packed_u_diagonal() for i in range(6): if diag[i] > 0: params[i] = format_float_with_su( params[i], math.sqrt(diag[i])) d_v_d_params = matrix.row(uc.d_volume_d_params()) vcv = matrix.sqr( cell_covariance_matrix.matrix_packed_u_as_symmetric()) var_v = (d_v_d_params * vcv).dot(d_v_d_params) volume = format_float_with_su(volume, math.sqrt(var_v)) a, b, c, alpha, beta, gamma = params self.cif_block[cell_prefix + 'length_a'] = a self.cif_block[cell_prefix + 'length_b'] = b self.cif_block[cell_prefix + 'length_c'] = c self.cif_block[cell_prefix + 'angle_alpha'] = alpha self.cif_block[cell_prefix + 'angle_beta'] = beta self.cif_block[cell_prefix + 'angle_gamma'] = gamma self.cif_block[cell_prefix + 'volume'] = volume
def f(self): result = 0 for op in self.ops: op_u = (op * matrix.sym(sym_mat3=self.u) * op.transpose()).as_sym_mat3() huh = (matrix.row(self.hkl) \ * matrix.sym(sym_mat3=op_u)).dot(matrix.col(self.hkl)) result += math.exp(mtps * huh) return result
def test_direction(): from cctbx.array_family import flex from cctbx import uctbx, xray, crystal from smtbx.refinement import constraints from scitbx.matrix import col, row from libtbx.test_utils import approx_equal uc = uctbx.unit_cell((1, 2, 3)) xs = xray.structure(crystal_symmetry=crystal.symmetry( unit_cell=uc, space_group_symbol='hall: P 2x 2y'), scatterers=flex.xray_scatterer(( xray.scatterer('C0', site=(0, 0, 0)), xray.scatterer('C1', site=(0, 2, 0)), xray.scatterer('C2', site=(1, 1, 0)), xray.scatterer('C3', site=(3, 1, 0)), ))) r = constraints.ext.reparametrisation(xs.unit_cell()) sc = xs.scatterers() site_0 = r.add(constraints.independent_site_parameter, sc[0]) site_1 = r.add(constraints.independent_site_parameter, sc[1]) site_2 = r.add(constraints.independent_site_parameter, sc[2]) site_3 = r.add(constraints.independent_site_parameter, sc[3]) d = constraints.vector_direction((site_0, site_1, site_2)).direction(uc) sd = constraints.static_direction.calc_best_line(uc, (site_0, site_1, site_2)) assert approx_equal(d, sd, eps=1e-15) d = constraints.vector_direction((site_0, site_1)).direction(uc) assert approx_equal( d, row(uc.orthogonalize(col(sc[1].site) - col(sc[0].site))).normalize(), eps=1e-15) n = constraints.static_direction.calc_best_plane_normal( uc, (site_0, site_1, site_2)) n1 = constraints.static_direction.calc_best_plane_normal( uc, (site_0, site_1, site_2, site_3)) v01 = uc.orthogonalize(col(sc[0].site) - col(sc[1].site)) v21 = uc.orthogonalize(col(sc[2].site) - col(sc[1].site)) nc = row(v01).cross(row(v21)).normalize() assert approx_equal(n, n1, eps=1e-15) assert approx_equal(n, nc, eps=1e-15)
def prepare_dxtbx_models(self, setting_specific_ai, sg, isoform=None): from dxtbx.model import BeamFactory beam = BeamFactory.simple(wavelength=self.inputai.wavelength) from dxtbx.model import DetectorFactory detector = DetectorFactory.simple( sensor=DetectorFactory.sensor("PAD"), distance=setting_specific_ai.distance(), beam_centre=[ setting_specific_ai.xbeam(), setting_specific_ai.ybeam() ], fast_direction="+x", slow_direction="+y", pixel_size=[self.pixel_size, self.pixel_size], image_size=[self.inputpd['size1'], self.inputpd['size1']], ) direct = matrix.sqr( setting_specific_ai.getOrientation().direct_matrix()) from dxtbx.model import Crystal crystal = Crystal( real_space_a=matrix.row(direct[0:3]), real_space_b=matrix.row(direct[3:6]), real_space_c=matrix.row(direct[6:9]), space_group_symbol=sg, ) crystal.set_mosaicity(setting_specific_ai.getMosaicity()) if isoform is not None: newB = matrix.sqr(isoform.fractionalization_matrix()).transpose() crystal.set_B(newB) from dxtbx.model import Experiment, ExperimentList experiments = ExperimentList() experiments.append( Experiment(beam=beam, detector=detector, crystal=crystal)) print beam print detector print crystal return experiments
def _gradient_map_coeff(self): coeff = self.miller_set().array( data=self.d_target_d_f_calc().deep_copy()) multiplier = (self.manager().unit_cell().volume() / matrix.row(self.manager().rfft().n_real()).product() * self.manager().space_group().n_ltr()) if (not coeff.anomalous_flag() and not coeff.space_group().is_centric()): multiplier /= 2 ext.apply_u_extra(self.manager().unit_cell(), self._results.u_extra(), coeff.indices(), coeff.data(), multiplier) return coeff
def df_d_params(self): result = [] tphkl = 2 * math.pi * matrix.col(self.hkl) h,k,l = self.hkl d_exp_huh_d_u_star = matrix.col([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l]) for scatterer in self.scatterers: assert scatterer.scattering_type == "const" w = scatterer.occupancy if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) ffp = 1 + scatterer.fp fdp = scatterer.fdp ff = ffp + 1j * fdp d_site = matrix.col([0,0,0]) if (not scatterer.flags.use_u_aniso()): d_u_iso = 0 d_u_star = None else: d_u_iso = None d_u_star = matrix.col([0,0,0,0,0,0]) d_occ = 0 d_fp = 0 d_fdp = 0 for s in self.space_group: r = s.r().as_rational().as_float() s_site = s * scatterer.site alpha = matrix.col(s_site).dot(tphkl) if (scatterer.flags.use_u_aniso()): s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j*alpha) site_gtmx = r.transpose() d_site += site_gtmx * ( w * dw * ff * e * 1j * tphkl) if (not scatterer.flags.use_u_aniso()): d_u_iso += w * dw * ff * e * mtps * self.d_star_sq else: u_star_gtmx = matrix.sqr(tensor_rank_2_gradient_transform_matrix(r)) d_u_star += u_star_gtmx * ( w * dw * ff * e * mtps * d_exp_huh_d_u_star) d_occ += dw * ff * e d_fp += w * dw * e d_fdp += w * dw * e * 1j result.append(gradients( site=d_site, u_iso=d_u_iso, u_star=d_u_star, occupancy=d_occ, fp=d_fp, fdp=d_fdp)) return result
def ft_dp(self, dp, u_extra): multiplier = (self.unit_cell().volume() / matrix.row(self.rfft().n_real()).product() * self.space_group().order_z() / dp.multiplicities().data().as_double()) coeff = dp.deep_copy() xray.apply_u_extra(self.unit_cell(), u_extra, coeff.indices(), coeff.data()) coeff_data = coeff.data() coeff_data *= flex.polar(multiplier, 0) return miller.fft_map(crystal_gridding=self.crystal_gridding(), fourier_coefficients=coeff)
def d_u(self): result = flex.double(6, 0) h,k,l = self.hkl d_exp_huh_d_u = matrix.col([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l]) for op in self.ops: op_u = (op*matrix.sym(sym_mat3=self.u)*op.transpose()).as_sym_mat3() huh = (matrix.row(self.hkl) \ * matrix.sym(sym_mat3=op_u)).dot(matrix.col(self.hkl)) d_op_u = math.exp(mtps * huh) * mtps * d_exp_huh_d_u gtmx = tensor_rank_2_gradient_transform_matrix(op) d_u = gtmx.matrix_multiply(flex.double(d_op_u)) result += d_u return result
def __init__(self, reduced_cell, rot_mx, deg=False): orth = matrix.sqr(reduced_cell.orthogonalization_matrix()) frac = matrix.sqr(reduced_cell.fractionalization_matrix()) r_info = rot_mx.info() self.type = r_info.type() self.u = rot_mx.info().ev() self.h = rot_mx.transpose().info().ev() self.t = orth * matrix.col(self.u) self.tau = matrix.row(self.h) * frac if (abs(self.type) == 1): self.delta = 0.0 else: self.delta = self.t.accute_angle(self.tau, deg=deg)
def prepare_dxtbx_models(self,setting_specific_ai,sg,isoform=None): from dxtbx.model.beam import beam_factory beam = beam_factory.simple(wavelength = self.inputai.wavelength) from dxtbx.model.detector import detector_factory detector = detector_factory.simple( sensor = detector_factory.sensor("PAD"), distance = setting_specific_ai.distance(), beam_centre = [setting_specific_ai.xbeam(), setting_specific_ai.ybeam()], fast_direction = "+x", slow_direction = "+y", pixel_size = [self.pixel_size,self.pixel_size], image_size = [self.inputpd['size1'],self.inputpd['size1']], ) direct = matrix.sqr(setting_specific_ai.getOrientation().direct_matrix()) from dxtbx.model.crystal import crystal_model crystal = crystal_model( real_space_a = matrix.row(direct[0:3]), real_space_b = matrix.row(direct[3:6]), real_space_c = matrix.row(direct[6:9]), space_group_symbol = sg, mosaicity = setting_specific_ai.getMosaicity() ) if isoform is not None: newB = matrix.sqr(isoform.fractionalization_matrix()).transpose() crystal.set_B(newB) from dxtbx.model.experiment.experiment_list import Experiment, ExperimentList experiments = ExperimentList() experiments.append(Experiment(beam=beam, detector=detector, crystal=crystal)) print beam print detector print crystal return experiments
def exercise_rotation(self): self.reset_sites() r = constraints.ext.reparametrisation(self.uc) sc = self.xs.scatterers() pivot = r.add(constraints.independent_site_parameter, sc[0]) size = r.add(constraints.independent_scalar_parameter, value=1, variable=False) r_x = r.add(constraints.independent_scalar_parameter, value=pi, variable=True) r_y = r.add(constraints.independent_scalar_parameter, value=pi/2, variable=True) r_z = r.add(constraints.independent_scalar_parameter, value=pi/3, variable=True) rg = r.add(constraints.rigid_rotatable_expandable_group, pivot=pivot, size = size, alpha = r_x, beta = r_y, gamma = r_z, scatterers=(sc[1], sc[2], sc[3])) r.finalise() r.linearise() r.store() rx_m = mat.sqr((1, 0, 0, 0, math.cos(self.rx), -math.sin(self.rx), 0, math.sin(self.rx), math.cos(self.rx))) ry_m = mat.sqr((math.cos(self.ry), 0, math.sin(self.ry), 0, 1, 0, -math.sin(self.ry), 0, math.cos(self.ry))) rz_m = mat.sqr((math.cos(self.rz), -math.sin(self.rz), 0, math.sin(self.rz), math.cos(self.rz), 0, 0, 0, 1)) R = rx_m*ry_m*rz_m #comulative rotation matrix shift = col(self.sites[0])-col(mat.row(col(self.sites[0])-self.center)*R) for i in xrange(1,4): calc_site = col(mat.row(col(self.sites[i])-self.center)*R) + shift assert approx_equal( self.uc.distance( calc_site, col(sc[i].site)), 0, eps=1e-14)
def exercise_direction(): uc = uctbx.unit_cell((1, 2, 3)) xs = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=uc, space_group_symbol='hall: P 2x 2y'), scatterers=flex.xray_scatterer(( xray.scatterer('C0', site=(0,0,0)), xray.scatterer('C1', site=(0,2,0)), xray.scatterer('C2', site=(1,1,0)), xray.scatterer('C3', site=(3,1,0)), ))) r = constraints.ext.reparametrisation(xs.unit_cell()) sc = xs.scatterers() site_0 = r.add(constraints.independent_site_parameter, sc[0]) site_1 = r.add(constraints.independent_site_parameter, sc[1]) site_2 = r.add(constraints.independent_site_parameter, sc[2]) site_3 = r.add(constraints.independent_site_parameter, sc[3]) d = constraints.vector_direction((site_0, site_1, site_2)).direction(uc) sd = constraints.static_direction.calc_best_line(uc, (site_0, site_1, site_2)) assert approx_equal(d, sd, eps=1e-15) d = constraints.vector_direction((site_0, site_1)).direction(uc) assert approx_equal(d, row(uc.orthogonalize(col(sc[1].site)-col(sc[0].site))).normalize(), eps=1e-15) n = constraints.static_direction.calc_best_plane_normal( uc, (site_0, site_1, site_2)) n1 = constraints.static_direction.calc_best_plane_normal( uc, (site_0, site_1, site_2, site_3)) v01 = uc.orthogonalize(col(sc[0].site)-col(sc[1].site)) v21 = uc.orthogonalize(col(sc[2].site)-col(sc[1].site)) nc = row(v01).cross(row(v21)).normalize() assert approx_equal(n, n1, eps=1e-15) assert approx_equal(n, nc, eps=1e-15)
def d2_u(self): result = flex.double(flex.grid(6,6), 0) h,k,l = self.hkl d_exp_huh_d_u = flex.double([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l]) d2_exp_huh_d_uu = d_exp_huh_d_u.matrix_outer_product(d_exp_huh_d_u) for op in self.ops: op_u = (op*matrix.sym(sym_mat3=self.u)*op.transpose()).as_sym_mat3() huh = (matrix.row(self.hkl) \ * matrix.sym(sym_mat3=op_u)).dot(matrix.col(self.hkl)) d2_op_u = math.exp(mtps * huh) * mtps**2 * d2_exp_huh_d_uu gtmx = tensor_rank_2_gradient_transform_matrix(op) d2_u = gtmx.matrix_multiply(d2_op_u).matrix_multiply( gtmx.matrix_transpose()) result += d2_u return result
def _gradient_map_coeff(self): coeff = self.miller_set().array(data=self.d_target_d_f_calc().deep_copy()) multiplier = ( self.manager().unit_cell().volume() / matrix.row(self.manager().rfft().n_real()).product() * self.manager().space_group().n_ltr()) if ( not coeff.anomalous_flag() and not coeff.space_group().is_centric()): multiplier /= 2 ext.apply_u_extra( self.manager().unit_cell(), self._results.u_extra(), coeff.indices(), coeff.data(), multiplier) return coeff
def d_u(self): result = flex.double(6, 0) h, k, l = self.hkl d_exp_huh_d_u = matrix.col( [h**2, k**2, l**2, 2 * h * k, 2 * h * l, 2 * k * l]) for op in self.ops: op_u = (op * matrix.sym(sym_mat3=self.u) * op.transpose()).as_sym_mat3() huh = (matrix.row(self.hkl) \ * matrix.sym(sym_mat3=op_u)).dot(matrix.col(self.hkl)) d_op_u = math.exp(mtps * huh) * mtps * d_exp_huh_d_u gtmx = tensor_rank_2_gradient_transform_matrix(op) d_u = gtmx.matrix_multiply(flex.double(d_op_u)) result += d_u return result
def ft_dp(self, dp, u_extra): multiplier = ( self.unit_cell().volume() / matrix.row(self.rfft().n_real()).product() * self.space_group().order_z() / dp.multiplicities().data().as_double()) coeff = dp.deep_copy() xray.apply_u_extra( self.unit_cell(), u_extra, coeff.indices(), coeff.data()) coeff_data = coeff.data() coeff_data *= flex.polar(multiplier, 0) return miller.fft_map( crystal_gridding=self.crystal_gridding(), fourier_coefficients=coeff)
def d2_u(self): result = flex.double(flex.grid(6, 6), 0) h, k, l = self.hkl d_exp_huh_d_u = flex.double( [h**2, k**2, l**2, 2 * h * k, 2 * h * l, 2 * k * l]) d2_exp_huh_d_uu = d_exp_huh_d_u.matrix_outer_product(d_exp_huh_d_u) for op in self.ops: op_u = (op * matrix.sym(sym_mat3=self.u) * op.transpose()).as_sym_mat3() huh = (matrix.row(self.hkl) \ * matrix.sym(sym_mat3=op_u)).dot(matrix.col(self.hkl)) d2_op_u = math.exp(mtps * huh) * mtps**2 * d2_exp_huh_d_uu gtmx = tensor_rank_2_gradient_transform_matrix(op) d2_u = gtmx.matrix_multiply(d2_op_u).matrix_multiply( gtmx.matrix_transpose()) result += d2_u return result
def __init__(self, points, epsilon=None, radius_if_one_or_no_points=1, center_if_no_points=(0,0,0)): assert len(points) > 0 or radius_if_one_or_no_points >= 0 if (epsilon is None): epsilon = 1.e-6 self._n_iterations = 0 if (len(points) == 0): self._center = center_if_no_points self._radius = radius_if_one_or_no_points return if (len(points) == 1): self._center = tuple(points[0]) self._radius = radius_if_one_or_no_points return n_dim = len(points[0].elems) w = 1./len(points) weights = matrix.row([w for i in xrange(len(points))]) while 1: x = matrix.col([0]*n_dim) for w,t in zip(weights.elems,points): x += w * t radii = matrix.col([abs(x-t) for t in points]) sigma = 0 for w,r in zip(weights.elems,radii.elems): sigma += w * r**2 sigma = math.sqrt(sigma) tau = radii.max() if (tau - sigma < tau * epsilon): break w_r = [] for w,r in zip(weights.elems,radii.elems): w_r.append(w * r) w_r = matrix.col(w_r) sum_w_r = w_r.sum() assert sum_w_r != 0 weights = w_r / sum_w_r self._n_iterations += 1 self._center = x.elems self._radius = tau
def __init__(self, points, epsilon=None, radius_if_one_or_no_points=1, center_if_no_points=(0, 0, 0)): assert len(points) > 0 or radius_if_one_or_no_points >= 0 if (epsilon is None): epsilon = 1.e-6 self._n_iterations = 0 if (len(points) == 0): self._center = center_if_no_points self._radius = radius_if_one_or_no_points return if (len(points) == 1): self._center = tuple(points[0]) self._radius = radius_if_one_or_no_points return n_dim = len(points[0].elems) w = 1. / len(points) weights = matrix.row([w for i in xrange(len(points))]) while 1: x = matrix.col([0] * n_dim) for w, t in zip(weights.elems, points): x += w * t radii = matrix.col([abs(x - t) for t in points]) sigma = 0 for w, r in zip(weights.elems, radii.elems): sigma += w * r**2 sigma = math.sqrt(sigma) tau = radii.max() if (tau - sigma < tau * epsilon): break w_r = [] for w, r in zip(weights.elems, radii.elems): w_r.append(w * r) w_r = matrix.col(w_r) sum_w_r = w_r.sum() assert sum_w_r != 0 weights = w_r / sum_w_r self._n_iterations += 1 self._center = x.elems self._radius = tau
def f(self): result = 0 tphkl = 2 * math.pi * matrix.col(self.hkl) for scatterer in self.scatterers: assert scatterer.scattering_type == "const" w = scatterer.occupancy if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) ffp = 1 + scatterer.fp fdp = scatterer.fdp ff = ffp + 1j * fdp for s in self.space_group: s_site = s * scatterer.site alpha = matrix.col(s_site).dot(tphkl) if (scatterer.flags.use_u_aniso()): r = s.r().as_rational().as_float() s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j*alpha) result += w * dw * ff * e return result
def df_d_params(self): tphkl = 2 * math.pi * matrix.col(self.hkl) h, k, l = self.hkl d_exp_huh_d_u_star = matrix.col( [h**2, k**2, l**2, 2 * h * k, 2 * h * l, 2 * k * l]) for i_scatterer, scatterer in enumerate(self.scatterers): site_symmetry_ops = None if (self.site_symmetry_table.is_special_position(i_scatterer)): site_symmetry_ops = self.site_symmetry_table.get(i_scatterer) site_constraints = site_symmetry_ops.site_constraints() if (scatterer.flags.use_u_aniso()): adp_constraints = site_symmetry_ops.adp_constraints() w = scatterer.weight() wwo = scatterer.weight_without_occupancy() if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) gaussian = self.scattering_type_registry.gaussian_not_optional( scattering_type=scatterer.scattering_type) f0 = gaussian.at_d_star_sq(self.d_star_sq) ffp = f0 + scatterer.fp fdp = scatterer.fdp ff = ffp + 1j * fdp d_site = matrix.col([0, 0, 0]) if (not scatterer.flags.use_u_aniso()): d_u_iso = 0 d_u_star = None else: d_u_iso = None d_u_star = matrix.col([0, 0, 0, 0, 0, 0]) d_occ = 0j d_fp = 0j d_fdp = 0j for s in self.space_group: r = s.r().as_rational().as_float() s_site = s * scatterer.site alpha = matrix.col(s_site).dot(tphkl) if (scatterer.flags.use_u_aniso()): s_u_star_s = r * matrix.sym( sym_mat3=scatterer.u_star) * r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot( matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j * alpha) site_gtmx = r.transpose() d_site += site_gtmx * (w * dw * ff * e * 1j * tphkl) if (not scatterer.flags.use_u_aniso()): d_u_iso += w * dw * ff * e * mtps * self.d_star_sq else: u_star_gtmx = matrix.sqr( tensor_rank_2_gradient_transform_matrix(r)) d_u_star += u_star_gtmx * (w * dw * ff * e * mtps * d_exp_huh_d_u_star) d_occ += wwo * dw * ff * e d_fp += w * dw * e d_fdp += w * dw * e * 1j if (site_symmetry_ops is not None): gsm = site_constraints.gradient_sum_matrix() gsm = matrix.rec(elems=gsm, n=gsm.focus()) d_site = gsm * d_site if (scatterer.flags.use_u_aniso()): gsm = adp_constraints.gradient_sum_matrix() gsm = matrix.rec(elems=gsm, n=gsm.focus()) d_u_star = gsm * d_u_star result = flex.complex_double(d_site) if (not scatterer.flags.use_u_aniso()): result.append(d_u_iso) else: result.extend(flex.complex_double(d_u_star)) result.extend(flex.complex_double([d_occ, d_fp, d_fdp])) yield result
def __init__(self, crystal_symmetry, cell_covariance_matrix=None, format="coreCIF", numeric_format="%.3f"): self.format = format.lower() assert self.format in ("corecif", "mmcif") if self.format == "mmcif": self.separator = "." else: self.separator = "_" assert numeric_format.startswith("%") self.cif_block = model.block() cell_prefix = "_cell%s" % self.separator if crystal_symmetry.space_group() is not None: sym_loop = model.loop( data=OrderedDict( ( ( "_space_group_symop" + self.separator + "id", range(1, len(crystal_symmetry.space_group()) + 1), ), ( "_space_group_symop" + self.separator + "operation_xyz", [s.as_xyz() for s in crystal_symmetry.space_group()], ), ) ) ) self.cif_block.add_loop(sym_loop) sg_prefix = "_space_group%s" % self.separator sg_type = crystal_symmetry.space_group_info().type() sg = sg_type.group() self.cif_block[sg_prefix + "crystal_system"] = sg.crystal_system().lower() self.cif_block[sg_prefix + "IT_number"] = sg_type.number() self.cif_block[sg_prefix + "name_H-M_alt"] = sg_type.lookup_symbol() self.cif_block[sg_prefix + "name_Hall"] = sg_type.hall_symbol() sg_prefix = "_symmetry%s" % self.separator self.cif_block[sg_prefix + "space_group_name_H-M"] = sg_type.lookup_symbol() self.cif_block[sg_prefix + "space_group_name_Hall"] = sg_type.hall_symbol() self.cif_block[sg_prefix + "Int_Tables_number"] = sg_type.number() if crystal_symmetry.unit_cell() is not None: uc = crystal_symmetry.unit_cell() params = list(uc.parameters()) volume = uc.volume() if cell_covariance_matrix is not None: diag = cell_covariance_matrix.matrix_packed_u_diagonal() for i in range(6): if diag[i] > 0: params[i] = format_float_with_su(params[i], math.sqrt(diag[i])) d_v_d_params = matrix.row(uc.d_volume_d_params()) vcv = matrix.sqr(cell_covariance_matrix.matrix_packed_u_as_symmetric()) var_v = (d_v_d_params * vcv).dot(d_v_d_params) volume = format_float_with_su(volume, math.sqrt(var_v)) numeric_format = "%s" a, b, c, alpha, beta, gamma = params self.cif_block[cell_prefix + "length_a"] = numeric_format % a self.cif_block[cell_prefix + "length_b"] = numeric_format % b self.cif_block[cell_prefix + "length_c"] = numeric_format % c self.cif_block[cell_prefix + "angle_alpha"] = numeric_format % alpha self.cif_block[cell_prefix + "angle_beta"] = numeric_format % beta self.cif_block[cell_prefix + "angle_gamma"] = numeric_format % gamma self.cif_block[cell_prefix + "volume"] = numeric_format % volume
def run(server_info, inp, status): print "<pre>" from scitbx import matrix p = p_from_string(string=inp.cb_expr) assert inp.p_or_q in ["P", "Q"] if (inp.p_or_q == "Q"): p = p.inverse() assert inp.p_transpose in ["off", "on"] if (inp.p_transpose == "on"): p = matrix.rt((p.r.transpose(), p.t)) print "P:" display_rt(p) print q = p.inverse() print "Q:" display_rt(q) print if (len(inp.obj_expr.strip()) != 0): if (inp.obj_type in ["xyz", "hkl"]): triple = xyz_from_string(string=inp.obj_expr) if (inp.obj_type == "xyz"): print "Transformation law: (Q,q) xyz" print print " xyz:", triple print print " xyz':", (q.r * matrix.col(triple) + q.t).elems print else: print "Transformation law: hkl P" print print " hkl:", triple print print " hkl':", (matrix.row(triple) * p.r).elems print elif (inp.obj_type == "unit_cell"): from cctbx import uctbx uc = uctbx.unit_cell(inp.obj_expr) print "Transformation law: Pt G P" print print "unit cell:", uc print g = matrix.sym(sym_mat3=uc.metrical_matrix()) print "metrical matrix:" display_r(g) print gp = p.r.transpose() * g * p.r print "metrical matrix':" display_r(gp) print ucp = uctbx.unit_cell(metrical_matrix=gp.as_sym_mat3()) print "unit cell':", ucp print elif (inp.obj_type == "Ww"): w = w_from_string(string=inp.obj_expr) print "Transformation law: (Q,q) (W,w) (P,p)" print print "(W, w):" display_rt(w) print wp = q * w * p print "(W, w)':" display_rt(wp) print else: raise RuntimeError("Unknown obj_type: %s" % inp.obj_type) print "</pre>"
def __init__(self, xray_structure, covariance_matrix=None, cell_covariance_matrix=None): crystal_symmetry_as_cif_block.__init__( self, xray_structure.crystal_symmetry(), cell_covariance_matrix=cell_covariance_matrix ) scatterers = xray_structure.scatterers() uc = xray_structure.unit_cell() if covariance_matrix is not None: param_map = xray_structure.parameter_map() covariance_diagonal = covariance_matrix.matrix_packed_u_diagonal() u_star_to_u_cif_linear_map_pow2 = flex.pow2(flex.double(uc.u_star_to_u_cif_linear_map())) u_star_to_u_iso_linear_form = matrix.row(uc.u_star_to_u_iso_linear_form()) fmt = "%.6f" # _atom_site_* loop atom_site_loop = model.loop( header=( "_atom_site_label", "_atom_site_type_symbol", "_atom_site_fract_x", "_atom_site_fract_y", "_atom_site_fract_z", "_atom_site_U_iso_or_equiv", "_atom_site_adp_type", "_atom_site_occupancy", ) ) for i_seq, sc in enumerate(scatterers): site = occu = u_iso_or_equiv = None # site if covariance_matrix is not None: params = param_map[i_seq] if sc.flags.grad_site() and params.site >= 0: site = [] for i in range(3): site.append(format_float_with_su(sc.site[i], math.sqrt(covariance_diagonal[params.site + i]))) # occupancy if sc.flags.grad_occupancy() and params.occupancy >= 0: occu = format_float_with_su(sc.occupancy, math.sqrt(covariance_diagonal[params.occupancy])) # Uiso/eq if sc.flags.grad_u_iso() or sc.flags.grad_u_aniso(): if sc.flags.grad_u_iso(): u_iso_or_equiv = format_float_with_su( sc.u_iso, math.sqrt(covariance.variance_for_u_iso(i_seq, covariance_matrix, param_map)) ) else: cov = covariance.extract_covariance_matrix_for_u_aniso( i_seq, covariance_matrix, param_map ).matrix_packed_u_as_symmetric() var = (u_star_to_u_iso_linear_form * matrix.sqr(cov)).dot(u_star_to_u_iso_linear_form) u_iso_or_equiv = format_float_with_su(sc.u_iso_or_equiv(uc), math.sqrt(var)) if site is None: site = [fmt % sc.site[i] for i in range(3)] if occu is None: occu = fmt % sc.occupancy if u_iso_or_equiv is None: u_iso_or_equiv = fmt % sc.u_iso_or_equiv(uc) if sc.flags.use_u_aniso(): adp_type = "Uani" else: adp_type = "Uiso" atom_site_loop.add_row( (sc.label, sc.scattering_type, site[0], site[1], site[2], u_iso_or_equiv, adp_type, occu) ) self.cif_block.add_loop(atom_site_loop) # _atom_site_aniso_* loop aniso_scatterers = scatterers.select(scatterers.extract_use_u_aniso()) if aniso_scatterers.size(): labels = list(scatterers.extract_labels()) aniso_loop = model.loop( header=( "_atom_site_aniso_label", "_atom_site_aniso_U_11", "_atom_site_aniso_U_22", "_atom_site_aniso_U_33", "_atom_site_aniso_U_12", "_atom_site_aniso_U_13", "_atom_site_aniso_U_23", ) ) for sc in aniso_scatterers: u_cif = adptbx.u_star_as_u_cif(uc, sc.u_star) if covariance_matrix is not None: row = [sc.label] idx = param_map[labels.index(sc.label)].u_aniso if idx > -1: var = covariance_diagonal[idx : idx + 6] * u_star_to_u_cif_linear_map_pow2 for i in range(6): if var[i] > 0: row.append(format_float_with_su(u_cif[i], math.sqrt(var[i]))) else: row.append(fmt % u_cif[i]) else: row = [sc.label] + [fmt % u_cif[i] for i in range(6)] else: row = [sc.label] + [fmt % u_cif[i] for i in range(6)] aniso_loop.add_row(row) self.cif_block.add_loop(aniso_loop) self.cif_block.add_loop(atom_type_cif_loop(xray_structure))
def integrate_coset(self, experiments, indexed): TRANS = self.params.integration.coset.transformation # here get a deepcopy that we are not afraid to modify: experiments_local = copy.deepcopy(experiments) print("*" * 80) print("Coset Reflections for modeling or validating the background") print("*" * 80) from dials.algorithms.profile_model.factory import ProfileModelFactory from dials.algorithms.integration.integrator import create_integrator # XXX Fixme later implement support for non-primitive lattices NKS base_set = miller_set( crystal_symmetry = symmetry( unit_cell = experiments_local[0].crystal.get_unit_cell(), space_group = experiments_local[0].crystal.get_space_group()), indices = indexed["miller_index"] ) triclinic = base_set.customized_copy( crystal_symmetry=symmetry(unit_cell = experiments_local[0].crystal.get_unit_cell(),space_group="P1")) # ================ # Compute the profile model # Predict the reflections # Create the integrator # This creates a reference to the experiment, not a copy: experiments_local = ProfileModelFactory.create(self.params, experiments_local, indexed) # for debug SLT[TRANS].show_summary() for e in experiments_local: e.crystal.set_space_group(triclinic.space_group()) Astar = e.crystal.get_A() # debug OriAstar = crystal_orientation(Astar,True) # debug OriAstar.show(legend="old ") Astarprime = sqr(Astar)* ( sqr(SLT[TRANS]._reindex_N).transpose().inverse() ) e.crystal.set_A(Astarprime) # debug OriAstarprime = crystal_orientation(Astarprime,True) # debug OriAstarprime.show(legend="new ") print("Predicting coset reflections") print("") predicted = flex.reflection_table.from_predictions_multi( experiments_local, dmin=self.params.prediction.d_min, dmax=self.params.prediction.d_max, margin=self.params.prediction.margin, force_static=self.params.prediction.force_static, ) print("sublattice total predictions %d"%len(predicted)) # filter the sublattice, keep only the coset indices miller = predicted["miller_index"] # coset of modulus 2, wherein there is a binary choice # see Sauter & Zwart, Acta D (2009) 65:553, Table 1; select the valid coset using eqn(5). coset_select_algorithm_2 = flex.bool() M_mat = SLT[TRANS].matS() # the transformation M_p = M_mat.inverse() for idx in miller: H_row = row(idx) h_orig_setting = H_row * M_p on_coset=False for icom in h_orig_setting.elems: if icom.denominator() > 1: on_coset=True; break coset_select_algorithm_2.append(on_coset) predicted = predicted.select(coset_select_algorithm_2) print("of which %d are in coset %d"%(len(predicted), TRANS)) print("") integrator = create_integrator(self.params, experiments_local, predicted) # Integrate the reflections integrated = integrator.integrate() # Delete the shoeboxes used for intermediate calculations, if requested if self.params.integration.debug.delete_shoeboxes and "shoebox" in integrated: del integrated["shoebox"] if self.params.output.composite_output: if ( self.params.output.coset_experiments_filename or self.params.output.coset_filename ): assert ( self.params.output.coset_experiments_filename is not None and self.params.output.coset_filename is not None ) n = len(self.all_coset_experiments) self.all_coset_experiments.extend(experiments_local) for i, experiment in enumerate(experiments_local): refls = integrated.select(integrated["id"] == i) refls["id"] = flex.int(len(refls), n) del refls.experiment_identifiers()[i] refls.experiment_identifiers()[n] = experiment.identifier self.all_coset_reflections.extend(refls) n += 1 else: # Dump experiments to disk if self.params.output.coset_experiments_filename: experiments_local.as_json(self.params.output.coset_experiments_filename) if self.params.output.coset_filename: # Save the reflections self.save_reflections( integrated, self.params.output.coset_filename ) rmsd_indexed, _ = calc_2D_rmsd_and_displacements(indexed) log_str = "coset RMSD indexed (px): %f\n" % (rmsd_indexed) log_str += "integrated %d\n"%len(integrated) for i in range(6): bright_integrated = integrated.select( ( integrated["intensity.sum.value"] / flex.sqrt(integrated["intensity.sum.variance"]) ) >= i ) if len(bright_integrated) > 0: rmsd_integrated, _ = calc_2D_rmsd_and_displacements(bright_integrated) else: rmsd_integrated = 0 log_str += ( "N reflections integrated at I/sigI >= %d: % 4d, RMSD (px): %f\n" % (i, len(bright_integrated), rmsd_integrated) ) for crystal_model in experiments_local.crystals(): if hasattr(crystal_model, "get_domain_size_ang"): log_str += ( ". Final ML model: domain size angstroms: %f, half mosaicity degrees: %f" % ( crystal_model.get_domain_size_ang(), crystal_model.get_half_mosaicity_deg(), ) ) print(log_str) print("")
def __init__(self, params): import cPickle as pickle from dxtbx.model import BeamFactory from dxtbx.model import DetectorFactory from dxtbx.model.crystal import crystal_model from cctbx.crystal_orientation import crystal_orientation, basis_type from dxtbx.model import Experiment, ExperimentList from scitbx import matrix self.experiments = ExperimentList() self.unique_file_names = [] self.params = params data = pickle.load( open(self.params.output.prefix + "_frame.pickle", "rb")) frames_text = data.split("\n") for item in frames_text: tokens = item.split(' ') wavelength = float(tokens[order_dict["wavelength"]]) beam = BeamFactory.simple(wavelength=wavelength) detector = DetectorFactory.simple( sensor=DetectorFactory.sensor( "PAD"), # XXX shouldn't hard code for XFEL distance=float(tokens[order_dict["distance"]]), beam_centre=[ float(tokens[order_dict["beam_x"]]), float(tokens[order_dict["beam_y"]]) ], fast_direction="+x", slow_direction="+y", pixel_size=[self.params.pixel_size, self.params.pixel_size], image_size=[1795, 1795], # XXX obviously need to figure this out ) reciprocal_matrix = matrix.sqr([ float(tokens[order_dict[k]]) for k in [ 'res_ori_1', 'res_ori_2', 'res_ori_3', 'res_ori_4', 'res_ori_5', 'res_ori_6', 'res_ori_7', 'res_ori_8', 'res_ori_9' ] ]) ORI = crystal_orientation(reciprocal_matrix, basis_type.reciprocal) direct = matrix.sqr(ORI.direct_matrix()) crystal = crystal_model( real_space_a=matrix.row(direct[0:3]), real_space_b=matrix.row(direct[3:6]), real_space_c=matrix.row(direct[6:9]), space_group_symbol=self.params.target_space_group.type(). lookup_symbol(), mosaicity=float(tokens[order_dict["half_mosaicity_deg"]]), ) crystal.domain_size = float(tokens[order_dict["domain_size_ang"]]) #if isoform is not None: # newB = matrix.sqr(isoform.fractionalization_matrix()).transpose() # crystal.set_B(newB) self.experiments.append( Experiment( beam=beam, detector=None, #dummy for now crystal=crystal)) self.unique_file_names.append( tokens[order_dict["unique_file_name"]]) self.show_summary()
def run(server_info, inp, status): print "<pre>" from scitbx import matrix p = p_from_string(string=inp.cb_expr) assert inp.p_or_q in ["P", "Q"] if (inp.p_or_q == "Q"): p = p.inverse() assert inp.p_transpose in ["off", "on"] if (inp.p_transpose == "on"): p = matrix.rt((p.r.transpose(), p.t)) print "P:" display_rt(p) print q = p.inverse() print "Q:" display_rt(q) print if (len(inp.obj_expr.strip()) != 0): if (inp.obj_type in ["xyz", "hkl"]): triple = xyz_from_string(string=inp.obj_expr) if (inp.obj_type == "xyz"): print "Transformation law: (Q,q) xyz" print print " xyz:", triple print print " xyz':", ( q.r * matrix.col(triple) + q.t).elems print else: print "Transformation law: hkl P" print print " hkl:", triple print print " hkl':", (matrix.row(triple) * p.r).elems print elif (inp.obj_type == "unit_cell"): from cctbx import uctbx uc = uctbx.unit_cell(inp.obj_expr) print "Transformation law: Pt G P" print print "unit cell:", uc print g = matrix.sym(sym_mat3=uc.metrical_matrix()) print "metrical matrix:" display_r(g) print gp = p.r.transpose() * g * p.r print "metrical matrix':" display_r(gp) print ucp = uctbx.unit_cell(metrical_matrix=gp.as_sym_mat3()) print "unit cell':", ucp print elif (inp.obj_type == "Ww"): w = w_from_string(string=inp.obj_expr) print "Transformation law: (Q,q) (W,w) (P,p)" print print "(W, w):" display_rt(w) print wp = q * w * p print "(W, w)':" display_rt(wp) print else: raise RuntimeError("Unknown obj_type: %s" % inp.obj_type) print "</pre>"
def d2f_d_params(self): tphkl = 2 * math.pi * flex.double(self.hkl) tphkl_outer = tphkl.matrix_outer_product(tphkl) \ .matrix_symmetric_as_packed_u() h, k, l = self.hkl d_exp_huh_d_u_star = flex.double( [h**2, k**2, l**2, 2 * h * k, 2 * h * l, 2 * k * l]) d2_exp_huh_d_u_star_u_star = d_exp_huh_d_u_star.matrix_outer_product( d_exp_huh_d_u_star).matrix_symmetric_as_packed_u() for i_scatterer, scatterer in enumerate(self.scatterers): site_symmetry_ops = None if (self.site_symmetry_table.is_special_position(i_scatterer)): site_symmetry_ops = self.site_symmetry_table.get(i_scatterer) site_constraints = site_symmetry_ops.site_constraints() if (scatterer.flags.use_u_aniso()): adp_constraints = site_symmetry_ops.adp_constraints() w = scatterer.weight() wwo = scatterer.weight_without_occupancy() if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) gaussian = self.scattering_type_registry.gaussian_not_optional( scattering_type=scatterer.scattering_type) f0 = gaussian.at_d_star_sq(self.d_star_sq) ffp = f0 + scatterer.fp fdp = scatterer.fdp ff = (ffp + 1j * fdp) d2_site_site = flex.complex_double(3 * (3 + 1) // 2, 0j) if (not scatterer.flags.use_u_aniso()): d2_site_u_iso = flex.complex_double(flex.grid(3, 1), 0j) d2_site_u_star = None else: d2_site_u_iso = None d2_site_u_star = flex.complex_double(flex.grid(3, 6), 0j) d2_site_occ = flex.complex_double(flex.grid(3, 1), 0j) d2_site_fp = flex.complex_double(flex.grid(3, 1), 0j) d2_site_fdp = flex.complex_double(flex.grid(3, 1), 0j) if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso = 0j d2_u_iso_occ = 0j d2_u_iso_fp = 0j d2_u_iso_fdp = 0j else: d2_u_star_u_star = flex.complex_double(6 * (6 + 1) // 2, 0j) d2_u_star_occ = flex.complex_double(flex.grid(6, 1), 0j) d2_u_star_fp = flex.complex_double(flex.grid(6, 1), 0j) d2_u_star_fdp = flex.complex_double(flex.grid(6, 1), 0j) d2_occ_fp = 0j d2_occ_fdp = 0j for s in self.space_group: r = s.r().as_rational().as_float() s_site = s * scatterer.site alpha = tphkl.dot(flex.double(s_site)) if (scatterer.flags.use_u_aniso()): s_u_star_s = r * matrix.sym( sym_mat3=scatterer.u_star) * r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot( matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j * alpha) site_gtmx = flex.double(r.transpose()) site_gtmx.reshape(flex.grid(3, 3)) d2_site_site += (w * dw * ff * e * (-1)) * ( site_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose( tphkl_outer)) if (not scatterer.flags.use_u_aniso()): d2_site_u_iso += (w * dw * ff * e * 1j * mtps * self.d_star_sq) \ * site_gtmx.matrix_multiply(tphkl) else: u_star_gtmx = tensor_rank_2_gradient_transform_matrix(r) d2_site_u_star += (w * dw * ff * e * 1j * mtps) \ * site_gtmx.matrix_multiply( tphkl.matrix_outer_product(d_exp_huh_d_u_star)) \ .matrix_multiply(u_star_gtmx.matrix_transpose()) site_gtmx_tphkl = site_gtmx.matrix_multiply(tphkl) d2_site_occ += (wwo * dw * ff * e * 1j) * site_gtmx_tphkl d2_site_fp += (w * dw * e * 1j) * site_gtmx_tphkl d2_site_fdp += (w * dw * e * (-1)) * site_gtmx_tphkl if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso += w * dw * ff * e * (mtps * self.d_star_sq)**2 d2_u_iso_occ += wwo * dw * ff * e * mtps * self.d_star_sq d2_u_iso_fp += w * dw * e * mtps * self.d_star_sq d2_u_iso_fdp += 1j * w * dw * e * mtps * self.d_star_sq else: d2_u_star_u_star +=(w * dw * ff * e * mtps**2) \ * u_star_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose( d2_exp_huh_d_u_star_u_star) u_star_gtmx_d_exp_huh_d_u_star = u_star_gtmx.matrix_multiply( d_exp_huh_d_u_star) d2_u_star_occ += (wwo * dw * ff * e * mtps) \ * u_star_gtmx_d_exp_huh_d_u_star d2_u_star_fp += (w * dw * e * mtps) \ * u_star_gtmx_d_exp_huh_d_u_star d2_u_star_fdp += (w * dw * 1j * e * mtps) \ * u_star_gtmx_d_exp_huh_d_u_star d2_occ_fp += wwo * dw * e d2_occ_fdp += wwo * dw * e * 1j if (site_symmetry_ops is None): i_u = 3 else: i_u = site_constraints.n_independent_params() if (not scatterer.flags.use_u_aniso()): i_occ = i_u + 1 elif (site_symmetry_ops is None): i_occ = i_u + 6 else: i_occ = i_u + adp_constraints.n_independent_params() i_fp, i_fdp, np = i_occ + 1, i_occ + 2, i_occ + 3 if (site_symmetry_ops is not None): gsm = site_constraints.gradient_sum_matrix() d2_site_site = gsm.matrix_multiply_packed_u_multiply_lhs_transpose( packed_u=d2_site_site) if (not scatterer.flags.use_u_aniso()): d2_site_u_iso = gsm.matrix_multiply(d2_site_u_iso) else: d2_site_u_star = gsm.matrix_multiply(d2_site_u_star) d2_site_occ = gsm.matrix_multiply(d2_site_occ) d2_site_fp = gsm.matrix_multiply(d2_site_fp) d2_site_fdp = gsm.matrix_multiply(d2_site_fdp) if (scatterer.flags.use_u_aniso()): gsm = adp_constraints.gradient_sum_matrix() d2_site_u_star = d2_site_u_star.matrix_multiply( gsm.matrix_transpose()) d2_u_star_u_star = gsm \ .matrix_multiply_packed_u_multiply_lhs_transpose( packed_u=d2_u_star_u_star) d2_u_star_occ = gsm.matrix_multiply(d2_u_star_occ) d2_u_star_fp = gsm.matrix_multiply(d2_u_star_fp) d2_u_star_fdp = gsm.matrix_multiply(d2_u_star_fdp) dp = flex.complex_double(flex.grid(np, np), 0j) paste = dp.matrix_paste_block_in_place paste(d2_site_site.matrix_packed_u_as_symmetric(), 0, 0) if (not scatterer.flags.use_u_aniso()): paste(d2_site_u_iso, 0, i_u) paste(d2_site_u_iso.matrix_transpose(), i_u, 0) else: paste(d2_site_u_star, 0, i_u) paste(d2_site_u_star.matrix_transpose(), i_u, 0) paste(d2_site_occ, 0, i_occ) paste(d2_site_occ.matrix_transpose(), i_occ, 0) paste(d2_site_fp, 0, i_fp) paste(d2_site_fp.matrix_transpose(), i_fp, 0) paste(d2_site_fdp, 0, i_fdp) paste(d2_site_fdp.matrix_transpose(), i_fdp, 0) if (not scatterer.flags.use_u_aniso()): dp[i_u * np + i_u] = d2_u_iso_u_iso dp[i_u * np + i_occ] = d2_u_iso_occ dp[i_occ * np + i_u] = d2_u_iso_occ dp[i_u * np + i_fp] = d2_u_iso_fp dp[i_fp * np + i_u] = d2_u_iso_fp dp[i_u * np + i_fdp] = d2_u_iso_fdp dp[i_fdp * np + i_u] = d2_u_iso_fdp else: paste(d2_u_star_u_star.matrix_packed_u_as_symmetric(), i_u, i_u) paste(d2_u_star_occ, i_u, i_occ) paste(d2_u_star_occ.matrix_transpose(), i_occ, i_u) paste(d2_u_star_fp, i_u, i_fp) paste(d2_u_star_fp.matrix_transpose(), i_fp, i_u) paste(d2_u_star_fdp, i_u, i_fdp) paste(d2_u_star_fdp.matrix_transpose(), i_fdp, i_u) dp[i_occ * np + i_fp] = d2_occ_fp dp[i_fp * np + i_occ] = d2_occ_fp dp[i_occ * np + i_fdp] = d2_occ_fdp dp[i_fdp * np + i_occ] = d2_occ_fdp yield dp
def __init__(self, h, site_constraints, independent_params): self.h = matrix.row(h) self.site_constraints = site_constraints self.independent_params = independent_params self.site = matrix.col(self.site_constraints.all_params( independent_params=self.independent_params))
def exercise_geometry(): xs = quartz() uc = xs.unit_cell() flags = xs.scatterer_flags() for f in flags: f.set_grad_site(True) xs.set_scatterer_flags(flags) cov = flex.double((1e-8,1e-9,2e-9,3e-9,4e-9,5e-9, 2e-8,1e-9,2e-9,3e-9,4e-9, 3e-8,1e-9,2e-9,3e-9, 2e-8,1e-9,2e-9, 3e-8,1e-9, 4e-8)) cell_vcv = flex.double((3e-2,3e-2,0,0,0,0, 3e-2,0,0,0,0, 4e-2,0,0,0, 0,0,0, 0,0, 0)) param_map = xs.parameter_map() cov_cart = covariance.orthogonalize_covariance_matrix(cov, uc, param_map) O = matrix.sqr(uc.orthogonalization_matrix()) F = matrix.sqr(uc.fractionalization_matrix()) sites_cart = xs.sites_cart() sites_frac = xs.sites_frac() # distances rt_mx_ji = sgtbx.rt_mx('-y,x-y,z-1/3') sites = (sites_cart[0], uc.orthogonalize(rt_mx_ji*sites_frac[1])) d = geometry.distance(sites) assert approx_equal(d.distance_model, 1.6159860469110217) v = matrix.col(sites[1]) - matrix.col(sites[0]) r_inv_cart = (O * matrix.sqr(rt_mx_ji.r().inverse().as_double()) * F) g = d.d_distance_d_sites() g = matrix.row(g[0] + tuple(r_inv_cart*matrix.col(g[1]))) f = g * matrix.sqr(cov_cart.matrix_packed_u_as_symmetric()) * g.transpose() assert approx_equal(d.variance(cov_cart, uc, rt_mx_ji), f[0], eps=1e-15) assert approx_equal( 0.0018054494791580823, d.variance(cov_cart, cell_vcv, uc, rt_mx_ji)) rt_mx_ji = sgtbx.rt_mx('x+1,y,z') sites = (sites_cart[0], uc.orthogonalize(rt_mx_ji*sites_frac[0])) d = geometry.distance(sites) assert approx_equal(d.distance_model, uc.parameters()[0]) assert approx_equal(cell_vcv.matrix_packed_u_diagonal()[0], d.variance(cov_cart, cell_vcv, uc, rt_mx_ji)) # angles rt_mx_ji = sgtbx.rt_mx('x-y,x,z-2/3') rt_mx_ki = sgtbx.rt_mx('-y,x-y,z-1/3') r_inv_cart_ji = (O * matrix.sqr(rt_mx_ji.r().inverse().as_double()) * F) r_inv_cart_ki = (O * matrix.sqr(rt_mx_ki.r().inverse().as_double()) * F) cov_a = covariance.extract_covariance_matrix_for_sites(flex.size_t([1,0,1]), cov_cart, param_map) sites = (uc.orthogonalize(rt_mx_ji*sites_frac[1]), sites_cart[0], uc.orthogonalize(rt_mx_ki*sites_frac[1])) a = geometry.angle(sites) assert approx_equal(a.angle_model, 101.30738566828551) g = a.d_angle_d_sites() g = matrix.row(tuple(r_inv_cart_ji*matrix.col(g[0])) + g[1] + tuple(r_inv_cart_ki*matrix.col(g[2]))) f = g * matrix.sqr(cov_a.matrix_packed_u_as_symmetric()) * g.transpose() assert approx_equal( a.variance(cov_a, uc, (rt_mx_ji, sgtbx.rt_mx(), rt_mx_ki)), f[0], eps=1e-15) assert approx_equal(0.0042632511984529199, a.variance(cov_a, cell_vcv, uc, (rt_mx_ji, sgtbx.rt_mx(), rt_mx_ki)))
def d2f_d_params_diag(self): tphkl = 2 * math.pi * flex.double(self.hkl) tphkl_outer = tphkl.matrix_outer_product(tphkl) \ .matrix_symmetric_as_packed_u() h,k,l = self.hkl d_exp_huh_d_u_star = flex.double([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l]) d2_exp_huh_d_u_star_u_star = d_exp_huh_d_u_star.matrix_outer_product( d_exp_huh_d_u_star).matrix_symmetric_as_packed_u() for i_scatterer,scatterer in enumerate(self.scatterers): site_symmetry_ops = None if (self.site_symmetry_table.is_special_position(i_scatterer)): site_symmetry_ops = self.site_symmetry_table.get(i_scatterer) site_constraints = site_symmetry_ops.site_constraints() if (scatterer.flags.use_u_aniso()): adp_constraints = site_symmetry_ops.adp_constraints() w = scatterer.weight() if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) gaussian = self.scattering_type_registry.gaussian_not_optional( scattering_type=scatterer.scattering_type) f0 = gaussian.at_d_star_sq(self.d_star_sq) ffp = f0 + scatterer.fp fdp = scatterer.fdp ff = (ffp + 1j * fdp) d2_site_site = flex.complex_double(3*(3+1)//2, 0j) if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso = 0j else: d2_u_star_u_star = flex.complex_double(6*(6+1)//2, 0j) for s in self.space_group: r = s.r().as_rational().as_float() s_site = s * scatterer.site alpha = tphkl.dot(flex.double(s_site)) if (scatterer.flags.use_u_aniso()): s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j*alpha) site_gtmx = flex.double(r.transpose()) site_gtmx.reshape(flex.grid(3,3)) d2_site_site += (w * dw * ff * e * (-1)) * ( site_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose( tphkl_outer)) if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso += w * dw * ff * e * (mtps * self.d_star_sq)**2 else: u_star_gtmx = tensor_rank_2_gradient_transform_matrix(r) d2_u_star_u_star +=(w * dw * ff * e * mtps**2) \ * u_star_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose( d2_exp_huh_d_u_star_u_star) if (site_symmetry_ops is None): i_u = 3 else: i_u = site_constraints.n_independent_params() if (not scatterer.flags.use_u_aniso()): i_occ = i_u + 1 elif (site_symmetry_ops is None): i_occ = i_u + 6 else: i_occ = i_u + adp_constraints.n_independent_params() np = i_occ+3 if (site_symmetry_ops is not None): gsm = site_constraints.gradient_sum_matrix() d2_site_site = gsm.matrix_multiply_packed_u_multiply_lhs_transpose( packed_u=d2_site_site) if (scatterer.flags.use_u_aniso()): gsm = adp_constraints.gradient_sum_matrix() d2_u_star_u_star = gsm \ .matrix_multiply_packed_u_multiply_lhs_transpose( packed_u=d2_u_star_u_star) # dpd = flex.complex_double(flex.grid(np,1), 0j) def paste(d, i): d.reshape(flex.grid(d.size(),1)) dpd.matrix_paste_block_in_place(d, i,0) paste(d2_site_site.matrix_packed_u_diagonal(), 0) if (not scatterer.flags.use_u_aniso()): dpd[i_u] = d2_u_iso_u_iso else: paste(d2_u_star_u_star.matrix_packed_u_diagonal(), i_u) yield dpd
def d2f_d_params_diag(self): tphkl = 2 * math.pi * flex.double(self.hkl) tphkl_outer = tphkl.matrix_outer_product(tphkl) \ .matrix_symmetric_as_packed_u() h, k, l = self.hkl d_exp_huh_d_u_star = flex.double( [h**2, k**2, l**2, 2 * h * k, 2 * h * l, 2 * k * l]) d2_exp_huh_d_u_star_u_star = d_exp_huh_d_u_star.matrix_outer_product( d_exp_huh_d_u_star).matrix_symmetric_as_packed_u() for i_scatterer, scatterer in enumerate(self.scatterers): site_symmetry_ops = None if (self.site_symmetry_table.is_special_position(i_scatterer)): site_symmetry_ops = self.site_symmetry_table.get(i_scatterer) site_constraints = site_symmetry_ops.site_constraints() if (scatterer.flags.use_u_aniso()): adp_constraints = site_symmetry_ops.adp_constraints() w = scatterer.weight() if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) gaussian = self.scattering_type_registry.gaussian_not_optional( scattering_type=scatterer.scattering_type) f0 = gaussian.at_d_star_sq(self.d_star_sq) ffp = f0 + scatterer.fp fdp = scatterer.fdp ff = (ffp + 1j * fdp) d2_site_site = flex.complex_double(3 * (3 + 1) // 2, 0j) if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso = 0j else: d2_u_star_u_star = flex.complex_double(6 * (6 + 1) // 2, 0j) for s in self.space_group: r = s.r().as_rational().as_float() s_site = s * scatterer.site alpha = tphkl.dot(flex.double(s_site)) if (scatterer.flags.use_u_aniso()): s_u_star_s = r * matrix.sym( sym_mat3=scatterer.u_star) * r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot( matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j * alpha) site_gtmx = flex.double(r.transpose()) site_gtmx.reshape(flex.grid(3, 3)) d2_site_site += (w * dw * ff * e * (-1)) * ( site_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose( tphkl_outer)) if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso += w * dw * ff * e * (mtps * self.d_star_sq)**2 else: u_star_gtmx = tensor_rank_2_gradient_transform_matrix(r) d2_u_star_u_star +=(w * dw * ff * e * mtps**2) \ * u_star_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose( d2_exp_huh_d_u_star_u_star) if (site_symmetry_ops is None): i_u = 3 else: i_u = site_constraints.n_independent_params() if (not scatterer.flags.use_u_aniso()): i_occ = i_u + 1 elif (site_symmetry_ops is None): i_occ = i_u + 6 else: i_occ = i_u + adp_constraints.n_independent_params() np = i_occ + 3 if (site_symmetry_ops is not None): gsm = site_constraints.gradient_sum_matrix() d2_site_site = gsm.matrix_multiply_packed_u_multiply_lhs_transpose( packed_u=d2_site_site) if (scatterer.flags.use_u_aniso()): gsm = adp_constraints.gradient_sum_matrix() d2_u_star_u_star = gsm \ .matrix_multiply_packed_u_multiply_lhs_transpose( packed_u=d2_u_star_u_star) # dpd = flex.complex_double(flex.grid(np, 1), 0j) def paste(d, i): d.reshape(flex.grid(d.size(), 1)) dpd.matrix_paste_block_in_place(d, i, 0) paste(d2_site_site.matrix_packed_u_diagonal(), 0) if (not scatterer.flags.use_u_aniso()): dpd[i_u] = d2_u_iso_u_iso else: paste(d2_u_star_u_star.matrix_packed_u_diagonal(), i_u) yield dpd
def df_d_params(self): tphkl = 2 * math.pi * matrix.col(self.hkl) h,k,l = self.hkl d_exp_huh_d_u_star = matrix.col([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l]) for i_scatterer,scatterer in enumerate(self.scatterers): site_symmetry_ops = None if (self.site_symmetry_table.is_special_position(i_scatterer)): site_symmetry_ops = self.site_symmetry_table.get(i_scatterer) site_constraints = site_symmetry_ops.site_constraints() if (scatterer.flags.use_u_aniso()): adp_constraints = site_symmetry_ops.adp_constraints() w = scatterer.weight() wwo = scatterer.weight_without_occupancy() if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) gaussian = self.scattering_type_registry.gaussian_not_optional( scattering_type=scatterer.scattering_type) f0 = gaussian.at_d_star_sq(self.d_star_sq) ffp = f0 + scatterer.fp fdp = scatterer.fdp ff = ffp + 1j * fdp d_site = matrix.col([0,0,0]) if (not scatterer.flags.use_u_aniso()): d_u_iso = 0 d_u_star = None else: d_u_iso = None d_u_star = matrix.col([0,0,0,0,0,0]) d_occ = 0j d_fp = 0j d_fdp = 0j for s in self.space_group: r = s.r().as_rational().as_float() s_site = s * scatterer.site alpha = matrix.col(s_site).dot(tphkl) if (scatterer.flags.use_u_aniso()): s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j*alpha) site_gtmx = r.transpose() d_site += site_gtmx * ( w * dw * ff * e * 1j * tphkl) if (not scatterer.flags.use_u_aniso()): d_u_iso += w * dw * ff * e * mtps * self.d_star_sq else: u_star_gtmx = matrix.sqr(tensor_rank_2_gradient_transform_matrix(r)) d_u_star += u_star_gtmx * ( w * dw * ff * e * mtps * d_exp_huh_d_u_star) d_occ += wwo * dw * ff * e d_fp += w * dw * e d_fdp += w * dw * e * 1j if (site_symmetry_ops is not None): gsm = site_constraints.gradient_sum_matrix() gsm = matrix.rec(elems=gsm, n=gsm.focus()) d_site = gsm * d_site if (scatterer.flags.use_u_aniso()): gsm = adp_constraints.gradient_sum_matrix() gsm = matrix.rec(elems=gsm, n=gsm.focus()) d_u_star = gsm * d_u_star result = flex.complex_double(d_site) if (not scatterer.flags.use_u_aniso()): result.append(d_u_iso) else: result.extend(flex.complex_double(d_u_star)) result.extend(flex.complex_double([d_occ, d_fp, d_fdp])) yield result
def d2f_d_params(self): tphkl = 2 * math.pi * matrix.col(self.hkl) tphkl_outer = tphkl.outer_product() h,k,l = self.hkl d_exp_huh_d_u_star = matrix.col([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l]) d2_exp_huh_d_u_star_u_star = d_exp_huh_d_u_star.outer_product() for scatterer in self.scatterers: assert scatterer.scattering_type == "const" w = scatterer.occupancy if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) ffp = 1 + scatterer.fp fdp = scatterer.fdp ff = (ffp + 1j * fdp) d2_site_site = flex.complex_double(flex.grid(3,3), 0j) if (not scatterer.flags.use_u_aniso()): d2_site_u_iso = flex.complex_double(flex.grid(3,1), 0j) d2_site_u_star = None else: d2_site_u_iso = None d2_site_u_star = flex.complex_double(flex.grid(3,6), 0j) d2_site_occ = flex.complex_double(flex.grid(3,1), 0j) d2_site_fp = flex.complex_double(flex.grid(3,1), 0j) d2_site_fdp = flex.complex_double(flex.grid(3,1), 0j) if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso = 0j d2_u_iso_occ = 0j d2_u_iso_fp = 0j d2_u_iso_fdp = 0j else: d2_u_star_u_star = flex.complex_double(flex.grid(6,6), 0j) d2_u_star_occ = flex.complex_double(flex.grid(6,1), 0j) d2_u_star_fp = flex.complex_double(flex.grid(6,1), 0j) d2_u_star_fdp = flex.complex_double(flex.grid(6,1), 0j) d2_occ_fp = 0j d2_occ_fdp = 0j for s in self.space_group: r = s.r().as_rational().as_float() s_site = s * scatterer.site alpha = matrix.col(s_site).dot(tphkl) if (scatterer.flags.use_u_aniso()): s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j*alpha) site_gtmx = r.transpose() d2_site_site += flex.complex_double( site_gtmx * (w * dw * ff * e * (-1) * tphkl_outer) * site_gtmx.transpose()) if (not scatterer.flags.use_u_aniso()): d2_site_u_iso += flex.complex_double(site_gtmx * ( w * dw * ff * e * 1j * mtps * self.d_star_sq * tphkl)) else: u_star_gtmx = matrix.sqr(tensor_rank_2_gradient_transform_matrix(r)) d2_site_u_star += flex.complex_double( site_gtmx * ((w * dw * ff * e * 1j * tphkl).outer_product( mtps * d_exp_huh_d_u_star)) * u_star_gtmx.transpose()) d2_site_occ += flex.complex_double(site_gtmx * ( dw * ff * e * 1j * tphkl)) d2_site_fp += flex.complex_double(site_gtmx * ( w * dw * e * 1j * tphkl)) d2_site_fdp += flex.complex_double(site_gtmx * ( w * dw * e * (-1) * tphkl)) if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso += w * dw * ff * e * (mtps * self.d_star_sq)**2 d2_u_iso_occ += dw * ff * e * mtps * self.d_star_sq d2_u_iso_fp += w * dw * e * mtps * self.d_star_sq d2_u_iso_fdp += 1j * w * dw * e * mtps * self.d_star_sq else: d2_u_star_u_star += flex.complex_double( u_star_gtmx * (w * dw * ff * e * mtps**2 * d2_exp_huh_d_u_star_u_star) * u_star_gtmx.transpose()) d2_u_star_occ += flex.complex_double(u_star_gtmx * ( dw * ff * e * mtps * d_exp_huh_d_u_star)) d2_u_star_fp += flex.complex_double(u_star_gtmx * ( w * dw * e * mtps * d_exp_huh_d_u_star)) d2_u_star_fdp += flex.complex_double(u_star_gtmx * ( w * dw * 1j * e * mtps * d_exp_huh_d_u_star)) d2_occ_fp += dw * e d2_occ_fdp += dw * e * 1j if (not scatterer.flags.use_u_aniso()): i_occ, i_fp, i_fdp, np = 4, 5, 6, 7 else: i_occ, i_fp, i_fdp, np = 9, 10, 11, 12 dp = flex.complex_double(flex.grid(np,np), 0j) paste = dp.matrix_paste_block_in_place paste(d2_site_site, 0,0) if (not scatterer.flags.use_u_aniso()): paste(d2_site_u_iso, 0,3) paste(d2_site_u_iso.matrix_transpose(), 3,0) else: paste(d2_site_u_star, 0,3) paste(d2_site_u_star.matrix_transpose(), 3,0) paste(d2_site_occ, 0,i_occ) paste(d2_site_occ.matrix_transpose(), i_occ,0) paste(d2_site_fp, 0,i_fp) paste(d2_site_fp.matrix_transpose(), i_fp,0) paste(d2_site_fdp, 0,i_fdp) paste(d2_site_fdp.matrix_transpose(), i_fdp,0) if (not scatterer.flags.use_u_aniso()): dp[3*7+3] = d2_u_iso_u_iso dp[3*7+4] = d2_u_iso_occ dp[4*7+3] = d2_u_iso_occ dp[3*7+5] = d2_u_iso_fp dp[5*7+3] = d2_u_iso_fp dp[3*7+6] = d2_u_iso_fdp dp[6*7+3] = d2_u_iso_fdp else: paste(d2_u_star_u_star, 3,3) paste(d2_u_star_occ, 3, 9) paste(d2_u_star_occ.matrix_transpose(), 9, 3) paste(d2_u_star_fp, 3, 10) paste(d2_u_star_fp.matrix_transpose(), 10, 3) paste(d2_u_star_fdp, 3, 11) paste(d2_u_star_fdp.matrix_transpose(), 11, 3) dp[i_occ*np+i_fp] = d2_occ_fp dp[i_fp*np+i_occ] = d2_occ_fp dp[i_occ*np+i_fdp] = d2_occ_fdp dp[i_fdp*np+i_occ] = d2_occ_fdp yield dp
def d2f_d_params(self): tphkl = 2 * math.pi * flex.double(self.hkl) tphkl_outer = tphkl.matrix_outer_product(tphkl) \ .matrix_symmetric_as_packed_u() h,k,l = self.hkl d_exp_huh_d_u_star = flex.double([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l]) d2_exp_huh_d_u_star_u_star = d_exp_huh_d_u_star.matrix_outer_product( d_exp_huh_d_u_star).matrix_symmetric_as_packed_u() for i_scatterer,scatterer in enumerate(self.scatterers): site_symmetry_ops = None if (self.site_symmetry_table.is_special_position(i_scatterer)): site_symmetry_ops = self.site_symmetry_table.get(i_scatterer) site_constraints = site_symmetry_ops.site_constraints() if (scatterer.flags.use_u_aniso()): adp_constraints = site_symmetry_ops.adp_constraints() w = scatterer.weight() wwo = scatterer.weight_without_occupancy() if (not scatterer.flags.use_u_aniso()): huh = scatterer.u_iso * self.d_star_sq dw = math.exp(mtps * huh) gaussian = self.scattering_type_registry.gaussian_not_optional( scattering_type=scatterer.scattering_type) f0 = gaussian.at_d_star_sq(self.d_star_sq) ffp = f0 + scatterer.fp fdp = scatterer.fdp ff = (ffp + 1j * fdp) d2_site_site = flex.complex_double(3*(3+1)//2, 0j) if (not scatterer.flags.use_u_aniso()): d2_site_u_iso = flex.complex_double(flex.grid(3,1), 0j) d2_site_u_star = None else: d2_site_u_iso = None d2_site_u_star = flex.complex_double(flex.grid(3,6), 0j) d2_site_occ = flex.complex_double(flex.grid(3,1), 0j) d2_site_fp = flex.complex_double(flex.grid(3,1), 0j) d2_site_fdp = flex.complex_double(flex.grid(3,1), 0j) if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso = 0j d2_u_iso_occ = 0j d2_u_iso_fp = 0j d2_u_iso_fdp = 0j else: d2_u_star_u_star = flex.complex_double(6*(6+1)//2, 0j) d2_u_star_occ = flex.complex_double(flex.grid(6,1), 0j) d2_u_star_fp = flex.complex_double(flex.grid(6,1), 0j) d2_u_star_fdp = flex.complex_double(flex.grid(6,1), 0j) d2_occ_fp = 0j d2_occ_fdp = 0j for s in self.space_group: r = s.r().as_rational().as_float() s_site = s * scatterer.site alpha = tphkl.dot(flex.double(s_site)) if (scatterer.flags.use_u_aniso()): s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose() huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl)) dw = math.exp(mtps * huh) e = cmath.exp(1j*alpha) site_gtmx = flex.double(r.transpose()) site_gtmx.reshape(flex.grid(3,3)) d2_site_site += (w * dw * ff * e * (-1)) * ( site_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose( tphkl_outer)) if (not scatterer.flags.use_u_aniso()): d2_site_u_iso += (w * dw * ff * e * 1j * mtps * self.d_star_sq) \ * site_gtmx.matrix_multiply(tphkl) else: u_star_gtmx = tensor_rank_2_gradient_transform_matrix(r) d2_site_u_star += (w * dw * ff * e * 1j * mtps) \ * site_gtmx.matrix_multiply( tphkl.matrix_outer_product(d_exp_huh_d_u_star)) \ .matrix_multiply(u_star_gtmx.matrix_transpose()) site_gtmx_tphkl = site_gtmx.matrix_multiply(tphkl) d2_site_occ += (wwo * dw * ff * e * 1j) * site_gtmx_tphkl d2_site_fp += (w * dw * e * 1j) * site_gtmx_tphkl d2_site_fdp += (w * dw * e * (-1)) * site_gtmx_tphkl if (not scatterer.flags.use_u_aniso()): d2_u_iso_u_iso += w * dw * ff * e * (mtps * self.d_star_sq)**2 d2_u_iso_occ += wwo * dw * ff * e * mtps * self.d_star_sq d2_u_iso_fp += w * dw * e * mtps * self.d_star_sq d2_u_iso_fdp += 1j * w * dw * e * mtps * self.d_star_sq else: d2_u_star_u_star +=(w * dw * ff * e * mtps**2) \ * u_star_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose( d2_exp_huh_d_u_star_u_star) u_star_gtmx_d_exp_huh_d_u_star = u_star_gtmx.matrix_multiply( d_exp_huh_d_u_star) d2_u_star_occ += (wwo * dw * ff * e * mtps) \ * u_star_gtmx_d_exp_huh_d_u_star d2_u_star_fp += (w * dw * e * mtps) \ * u_star_gtmx_d_exp_huh_d_u_star d2_u_star_fdp += (w * dw * 1j * e * mtps) \ * u_star_gtmx_d_exp_huh_d_u_star d2_occ_fp += wwo * dw * e d2_occ_fdp += wwo * dw * e * 1j if (site_symmetry_ops is None): i_u = 3 else: i_u = site_constraints.n_independent_params() if (not scatterer.flags.use_u_aniso()): i_occ = i_u + 1 elif (site_symmetry_ops is None): i_occ = i_u + 6 else: i_occ = i_u + adp_constraints.n_independent_params() i_fp, i_fdp, np = i_occ+1, i_occ+2, i_occ+3 if (site_symmetry_ops is not None): gsm = site_constraints.gradient_sum_matrix() d2_site_site = gsm.matrix_multiply_packed_u_multiply_lhs_transpose( packed_u=d2_site_site) if (not scatterer.flags.use_u_aniso()): d2_site_u_iso = gsm.matrix_multiply(d2_site_u_iso) else: d2_site_u_star = gsm.matrix_multiply(d2_site_u_star) d2_site_occ = gsm.matrix_multiply(d2_site_occ) d2_site_fp = gsm.matrix_multiply(d2_site_fp) d2_site_fdp = gsm.matrix_multiply(d2_site_fdp) if (scatterer.flags.use_u_aniso()): gsm = adp_constraints.gradient_sum_matrix() d2_site_u_star = d2_site_u_star.matrix_multiply( gsm.matrix_transpose()) d2_u_star_u_star = gsm \ .matrix_multiply_packed_u_multiply_lhs_transpose( packed_u=d2_u_star_u_star) d2_u_star_occ = gsm.matrix_multiply(d2_u_star_occ) d2_u_star_fp = gsm.matrix_multiply(d2_u_star_fp) d2_u_star_fdp = gsm.matrix_multiply(d2_u_star_fdp) dp = flex.complex_double(flex.grid(np,np), 0j) paste = dp.matrix_paste_block_in_place paste(d2_site_site.matrix_packed_u_as_symmetric(), 0,0) if (not scatterer.flags.use_u_aniso()): paste(d2_site_u_iso, 0,i_u) paste(d2_site_u_iso.matrix_transpose(), i_u,0) else: paste(d2_site_u_star, 0,i_u) paste(d2_site_u_star.matrix_transpose(), i_u,0) paste(d2_site_occ, 0,i_occ) paste(d2_site_occ.matrix_transpose(), i_occ,0) paste(d2_site_fp, 0,i_fp) paste(d2_site_fp.matrix_transpose(), i_fp,0) paste(d2_site_fdp, 0,i_fdp) paste(d2_site_fdp.matrix_transpose(), i_fdp,0) if (not scatterer.flags.use_u_aniso()): dp[i_u*np+i_u] = d2_u_iso_u_iso dp[i_u*np+i_occ] = d2_u_iso_occ dp[i_occ*np+i_u] = d2_u_iso_occ dp[i_u*np+i_fp] = d2_u_iso_fp dp[i_fp*np+i_u] = d2_u_iso_fp dp[i_u*np+i_fdp] = d2_u_iso_fdp dp[i_fdp*np+i_u] = d2_u_iso_fdp else: paste(d2_u_star_u_star.matrix_packed_u_as_symmetric(), i_u, i_u) paste(d2_u_star_occ, i_u, i_occ) paste(d2_u_star_occ.matrix_transpose(), i_occ, i_u) paste(d2_u_star_fp, i_u, i_fp) paste(d2_u_star_fp.matrix_transpose(), i_fp, i_u) paste(d2_u_star_fdp, i_u, i_fdp) paste(d2_u_star_fdp.matrix_transpose(), i_fdp, i_u) dp[i_occ*np+i_fp] = d2_occ_fp dp[i_fp*np+i_occ] = d2_occ_fp dp[i_occ*np+i_fdp] = d2_occ_fdp dp[i_fdp*np+i_occ] = d2_occ_fdp yield dp
def __init__(self, xray_structure, covariance_matrix=None, cell_covariance_matrix=None): crystal_symmetry_as_cif_block.__init__( self, xray_structure.crystal_symmetry(), cell_covariance_matrix=cell_covariance_matrix) scatterers = xray_structure.scatterers() uc = xray_structure.unit_cell() if covariance_matrix is not None: param_map = xray_structure.parameter_map() covariance_diagonal = covariance_matrix.matrix_packed_u_diagonal() u_star_to_u_cif_linear_map_pow2 = flex.pow2( flex.double(uc.u_star_to_u_cif_linear_map())) u_star_to_u_iso_linear_form = matrix.row( uc.u_star_to_u_iso_linear_form()) fmt = "%.6f" # _atom_site_* loop atom_site_loop = model.loop( header=('_atom_site_label', '_atom_site_type_symbol', '_atom_site_fract_x', '_atom_site_fract_y', '_atom_site_fract_z', '_atom_site_U_iso_or_equiv', '_atom_site_adp_type', '_atom_site_occupancy')) for i_seq, sc in enumerate(scatterers): # site if covariance_matrix is not None and sc.flags.grad_site(): site = [] for i in range(3): idx = param_map[i_seq].site if idx > -1: var = covariance_diagonal[idx + i] else: var = 0 if var > 0: site.append( format_float_with_su(sc.site[i], math.sqrt(var))) else: site.append(fmt % sc.site[i]) else: site = [fmt % sc.site[i] for i in range(3)] # u_eq if (covariance_matrix is not None and (sc.flags.grad_u_iso() or sc.flags.grad_u_aniso())): if sc.flags.grad_u_iso(): u_iso_or_equiv = format_float_with_su( sc.u_iso, math.sqrt( covariance.variance_for_u_iso( i_seq, covariance_matrix, param_map))) else: cov = covariance.extract_covariance_matrix_for_u_aniso( i_seq, covariance_matrix, param_map).matrix_packed_u_as_symmetric() var = (u_star_to_u_iso_linear_form * matrix.sqr(cov)).dot(u_star_to_u_iso_linear_form) u_iso_or_equiv = format_float_with_su( sc.u_iso_or_equiv(uc), math.sqrt(var)) else: u_iso_or_equiv = fmt % sc.u_iso_or_equiv(uc) if sc.flags.use_u_aniso(): adp_type = 'Uani' else: adp_type = 'Uiso' atom_site_loop.add_row( (sc.label, sc.scattering_type, site[0], site[1], site[2], u_iso_or_equiv, adp_type, fmt % sc.occupancy)) self.cif_block.add_loop(atom_site_loop) # _atom_site_aniso_* loop aniso_scatterers = scatterers.select(scatterers.extract_use_u_aniso()) if aniso_scatterers.size(): labels = list(scatterers.extract_labels()) aniso_loop = model.loop( header=('_atom_site_aniso_label', '_atom_site_aniso_U_11', '_atom_site_aniso_U_22', '_atom_site_aniso_U_33', '_atom_site_aniso_U_12', '_atom_site_aniso_U_13', '_atom_site_aniso_U_23')) for sc in aniso_scatterers: u_cif = adptbx.u_star_as_u_cif(uc, sc.u_star) if covariance_matrix is not None: row = [sc.label] idx = param_map[labels.index(sc.label)].u_aniso if idx > -1: var = covariance_diagonal[ idx:idx + 6] * u_star_to_u_cif_linear_map_pow2 for i in range(6): if var[i] > 0: row.append( format_float_with_su( u_cif[i], math.sqrt(var[i]))) else: row.append(fmt % u_cif[i]) else: row = [sc.label] + [fmt % u_cif[i] for i in range(6)] else: row = [sc.label] + [fmt % u_cif[i] for i in range(6)] aniso_loop.add_row(row) self.cif_block.add_loop(aniso_loop) self.cif_block.add_loop(atom_type_cif_loop(xray_structure))