def debye_waller_factors(self, miller_index=None, miller_indices=None, u_iso=None, b_iso=None, u_cart=None, b_cart=None, u_cif=None, u_star=None, exp_arg_limit=50, truncate_exp_arg=False): assert [miller_index, miller_indices].count(None) == 1 assert [u_iso, b_iso, u_cart, b_cart, u_cif, u_star].count(None) == 5 from cctbx import adptbx h = miller_index if (h is None): h = miller_indices if (u_iso is not None): b_iso = adptbx.u_as_b(u_iso) if (b_iso is not None): return adptbx.debye_waller_factor_b_iso( self.stol_sq(h), b_iso, exp_arg_limit, truncate_exp_arg) if (b_cart is not None): u_cart = adptbx.b_as_u(b_cart) if (u_cart is not None): u_star = adptbx.u_cart_as_u_star(self, u_cart) if (u_cif is not None): u_star = adptbx.u_cif_as_u_star(self, u_cif) assert u_star is not None return adptbx.debye_waller_factor_u_star( h, u_star, exp_arg_limit, truncate_exp_arg)
def debye_waller_factors(self, miller_index=None, miller_indices=None, u_iso=None, b_iso=None, u_cart=None, b_cart=None, u_cif=None, u_star=None, exp_arg_limit=50, truncate_exp_arg=False): assert [miller_index, miller_indices].count(None) == 1 assert [u_iso, b_iso, u_cart, b_cart, u_cif, u_star].count(None) == 5 from cctbx import adptbx h = miller_index if (h is None): h = miller_indices if (u_iso is not None): b_iso = adptbx.u_as_b(u_iso) if (b_iso is not None): return adptbx.debye_waller_factor_b_iso( self.stol_sq(h), b_iso, exp_arg_limit, truncate_exp_arg) if (b_cart is not None): u_cart = adptbx.b_as_u(b_cart) if (u_cart is not None): u_star = adptbx.u_cart_as_u_star(self, u_cart) if (u_cif is not None): u_star = adptbx.u_cif_as_u_star(self, u_cif) assert u_star is not None return adptbx.debye_waller_factor_u_star( h, u_star, exp_arg_limit, truncate_exp_arg)
def d_dw_d_u_star_finite(h, u_star, eps=1.e-6): result = flex.double() for ip in xrange(len(u_star)): vs = [] for signed_eps in [eps,-eps]: u_star_eps = list(u_star) u_star_eps[ip] += signed_eps vs.append(adptbx.debye_waller_factor_u_star(h, u_star_eps)) result.append((vs[0]-vs[1])/(2*eps)) return result
def d_dw_d_u_indep_finite(adp_constraints, h, u_indep, eps=1.e-6): result = flex.double() for i_indep in xrange(len(u_indep)): vs = [] for signed_eps in [eps,-eps]: u_indep_eps = list(u_indep) u_indep_eps[i_indep] += signed_eps u_eps = adp_constraints.all_params(u_indep_eps) vs.append(adptbx.debye_waller_factor_u_star(h, u_eps)) result.append((vs[0]-vs[1])/(2*eps)) return result
def p23_curv(h, u_star): """\ h = {h0,h1,h2} u = {{u22,0,0},{0,u22,0},{0,0,u22}} dw=Exp[mtps*h.u.h] FortranForm[D[dw,u22,u22]/dw] """ dw = adptbx.debye_waller_factor_u_star(h, u_star) h0, h1, h2 = h u22 = u_star[2] mtps = -2 * math.pi**2 return [[dw * ((h0**2 + h1**2 + h2**2)**2 * mtps**2)]]
def p23_curv(h, u_star): """\ h = {h0,h1,h2} u = {{u22,0,0},{0,u22,0},{0,0,u22}} dw=Exp[mtps*h.u.h] FortranForm[D[dw,u22,u22]/dw] """ dw = adptbx.debye_waller_factor_u_star(h, u_star) h0, h1, h2 = h u22 = u_star[2] mtps = -2*math.pi**2 return [ [dw * ((h0**2 + h1**2 + h2**2)**2*mtps**2)]]
def p2_curv(h, u_star): """\ h = {h0,h1,h2} u = {{u00,0,u02},{0,u11,0},{u02,0,u22}} dw=Exp[mtps*h.u.h] FortranForm[D[dw,u00,u00]/dw] FortranForm[D[dw,u00,u11]/dw] FortranForm[D[dw,u00,u22]/dw] FortranForm[D[dw,u00,u02]/dw] FortranForm[D[dw,u11,u00]/dw] FortranForm[D[dw,u11,u11]/dw] FortranForm[D[dw,u11,u22]/dw] FortranForm[D[dw,u11,u02]/dw] FortranForm[D[dw,u22,u00]/dw] FortranForm[D[dw,u22,u11]/dw] FortranForm[D[dw,u22,u22]/dw] FortranForm[D[dw,u22,u02]/dw] FortranForm[D[dw,u02,u00]/dw] FortranForm[D[dw,u02,u11]/dw] FortranForm[D[dw,u02,u22]/dw] FortranForm[D[dw,u02,u02]/dw] """ dw = adptbx.debye_waller_factor_u_star(h, u_star) h0, h1, h2 = h u00 = u_star[0] u11 = u_star[1] u22 = u_star[2] u02 = u_star[4] mtps = -2 * math.pi**2 return [[ dw * (h0**4 * mtps**2), dw * (h0**2 * h1**2 * mtps**2), dw * (h0**2 * h2**2 * mtps**2), dw * (2 * h0**3 * h2 * mtps**2) ], [ dw * (h0**2 * h1**2 * mtps**2), dw * (h1**4 * mtps**2), dw * (h1**2 * h2**2 * mtps**2), dw * (2 * h0 * h1**2 * h2 * mtps**2) ], [ dw * (h0**2 * h2**2 * mtps**2), dw * (h1**2 * h2**2 * mtps**2), dw * (h2**4 * mtps**2), dw * (2 * h0 * h2**3 * mtps**2) ], [ dw * (2 * h0**3 * h2 * mtps**2), dw * (2 * h0 * h1**2 * h2 * mtps**2), dw * (2 * h0 * h2**3 * mtps**2), dw * (4 * h0**2 * h2**2 * mtps**2) ]]
def p2_curv(h, u_star): """\ h = {h0,h1,h2} u = {{u00,0,u02},{0,u11,0},{u02,0,u22}} dw=Exp[mtps*h.u.h] FortranForm[D[dw,u00,u00]/dw] FortranForm[D[dw,u00,u11]/dw] FortranForm[D[dw,u00,u22]/dw] FortranForm[D[dw,u00,u02]/dw] FortranForm[D[dw,u11,u00]/dw] FortranForm[D[dw,u11,u11]/dw] FortranForm[D[dw,u11,u22]/dw] FortranForm[D[dw,u11,u02]/dw] FortranForm[D[dw,u22,u00]/dw] FortranForm[D[dw,u22,u11]/dw] FortranForm[D[dw,u22,u22]/dw] FortranForm[D[dw,u22,u02]/dw] FortranForm[D[dw,u02,u00]/dw] FortranForm[D[dw,u02,u11]/dw] FortranForm[D[dw,u02,u22]/dw] FortranForm[D[dw,u02,u02]/dw] """ dw = adptbx.debye_waller_factor_u_star(h, u_star) h0, h1, h2 = h u00 = u_star[0] u11 = u_star[1] u22 = u_star[2] u02 = u_star[4] mtps = -2*math.pi**2 return [ [dw * (h0**4*mtps**2), dw * (h0**2*h1**2*mtps**2), dw * (h0**2*h2**2*mtps**2), dw * (2*h0**3*h2*mtps**2)], [dw * (h0**2*h1**2*mtps**2), dw * (h1**4*mtps**2), dw * (h1**2*h2**2*mtps**2), dw * (2*h0*h1**2*h2*mtps**2)], [dw * (h0**2*h2**2*mtps**2), dw * (h1**2*h2**2*mtps**2), dw * (h2**4*mtps**2), dw * (2*h0*h2**3*mtps**2)], [dw * (2*h0**3*h2*mtps**2), dw * (2*h0*h1**2*h2*mtps**2), dw * (2*h0*h2**3*mtps**2), dw * (4*h0**2*h2**2*mtps**2)]]
def calc_debye_waller_factor(self, observations, b_cart): #apply space-group constraints on B-tensor space_group = self.observations.space_group() adp_constraints = sgtbx.tensor_rank_2_constraints( space_group=space_group, reciprocal_space=True) #convert B tensor direct-space to reciprocal space fmx = sqr(self.unit_cell.fractionalization_matrix()) u_cart = b_cart / (8 * math.pi**2) u_star_mat = fmx * u_cart * fmx.transpose() u_star = u_star_mat.as_sym_mat3() u_star = space_group.average_u_star(u_star) u_indep = adp_constraints.independent_params(all_params=u_star) u_star = adp_constraints.all_params(independent_params=u_indep) dw_c = adptbx.debye_waller_factor_u_star(observations.indices(), u_star) return dw_c
def p3_curv(h, u_star): """\ h = {h0,h1,h2} u = {{2*u01,u01,0},{u01,2*u01,0},{0,0,u22}} dw=Exp[mtps*h.u.h] FortranForm[D[dw,u22,u22]/dw] FortranForm[D[dw,u22,u01]/dw] FortranForm[D[dw,u01,u22]/dw] FortranForm[D[dw,u01,u01]/dw] """ dw = adptbx.debye_waller_factor_u_star(h, u_star) h0, h1, h2 = h u22 = u_star[2] u01 = u_star[3] mtps = -2*math.pi**2 return [ [dw * (h2**4*mtps**2), dw * ((h0*(2*h0 + h1) + h1*(h0 + 2*h1))*h2**2*mtps**2)], [dw * ((h0*(2*h0 + h1) + h1*(h0 + 2*h1))*h2**2*mtps**2), dw * ((h0*(2*h0 + h1) + h1*(h0 + 2*h1))**2*mtps**2)]]
def structure_factors(self, d_min): print "WARNING: RESULTS NOT VERIFIED" # XXX miller_set = miller.build_set( crystal_symmetry=self, anomalous_flag=True, # XXX always True? d_min=d_min) f_calc = flex.complex_double() for h in miller_set.indices(): fc = 0j for scatterer in self.scatterers(): site_symmetry = self.site_symmetry(scatterer.site) equiv_sites = sgtbx.sym_equiv_sites(site_symmetry) sum_exp_j_two_pi_hx = 0j for i_symop, x in enumerate(equiv_sites.coordinates()): sum_hx = 0 for i in xrange(3): sum_hx += h[i] * x[i] phase = 2 * math.pi * sum_hx exp_j_two_pi_hx = complex(math.cos(phase), math.sin(phase)) if (scatterer.anisotropic_flag): r = self.space_group()(i_symop).r() hr = h dw = adptbx.debye_waller_factor_u_star( hr, scatterer.u_star) exp_j_two_pi_hx *= dw sum_exp_j_two_pi_hx += exp_j_two_pi_hx b_j = scatterer.scattering_info.bound_coh_scatt_length() fc_site = scatterer.weight() * b_j * sum_exp_j_two_pi_hx if (not scatterer.anisotropic_flag): d_star_sq = self.unit_cell().d_star_sq(h) dw = adptbx.debye_waller_factor_u_iso( d_star_sq / 4, scatterer.u_iso) fc_site *= dw fc += fc_site f_calc.append(fc) return miller.array(miller_set=miller_set, data=f_calc)
def structure_factors(self, d_min): print "WARNING: RESULTS NOT VERIFIED" # XXX miller_set = miller.build_set( crystal_symmetry=self, anomalous_flag=True, # XXX always True? d_min=d_min) f_calc = flex.complex_double() for h in miller_set.indices(): fc = 0j for scatterer in self.scatterers(): site_symmetry = self.site_symmetry(scatterer.site) equiv_sites = sgtbx.sym_equiv_sites(site_symmetry) sum_exp_j_two_pi_hx = 0j for i_symop,x in enumerate(equiv_sites.coordinates()): sum_hx = 0 for i in xrange(3): sum_hx += h[i] * x[i] phase = 2 * math.pi * sum_hx exp_j_two_pi_hx = complex(math.cos(phase), math.sin(phase)) if (scatterer.anisotropic_flag): r = self.space_group()(i_symop).r() hr = h dw = adptbx.debye_waller_factor_u_star(hr, scatterer.u_star) exp_j_two_pi_hx *= dw sum_exp_j_two_pi_hx += exp_j_two_pi_hx b_j = scatterer.scattering_info.bound_coh_scatt_length() fc_site = scatterer.weight() * b_j * sum_exp_j_two_pi_hx if (not scatterer.anisotropic_flag): d_star_sq = self.unit_cell().d_star_sq(h) dw = adptbx.debye_waller_factor_u_iso(d_star_sq/4, scatterer.u_iso) fc_site *= dw fc += fc_site f_calc.append(fc) return miller.array( miller_set=miller_set, data=f_calc)
def exercise_interface(): episq = 8 * (math.pi**2) assert approx_equal(adptbx.u_as_b(2.3), 2.3 * episq) assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(2.3)), 2.3) u = (3, 4, 9, 2, 1, 7) assert approx_equal(adptbx.u_as_b(u), [x * episq for x in u]) assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(u)), u) uc = uctbx.unit_cell((5, 4, 7, 80, 110, 100)) for fw, bw in ((adptbx.u_cif_as_u_star, adptbx.u_star_as_u_cif), (adptbx.u_cart_as_u_star, adptbx.u_star_as_u_cart), (adptbx.u_cart_as_u_cif, adptbx.u_cif_as_u_cart), (adptbx.u_cart_as_beta, adptbx.beta_as_u_cart), (adptbx.u_cif_as_beta, adptbx.beta_as_u_cif)): assert approx_equal(bw(uc, fw(uc, u)), u) assert approx_equal(adptbx.beta_as_u_star(adptbx.u_star_as_beta(u)), u) assert approx_equal(adptbx.u_cart_as_u_iso(adptbx.u_iso_as_u_cart(2.3)), 2.3) for fw, bw in ((adptbx.u_iso_as_u_star, adptbx.u_star_as_u_iso), (adptbx.u_iso_as_u_cif, adptbx.u_cif_as_u_iso), (adptbx.u_iso_as_beta, adptbx.beta_as_u_iso)): assert approx_equal(bw(uc, fw(uc, 2.3)), 2.3) fc = adptbx.factor_u_cart_u_iso(u_cart=u) assert approx_equal(fc.u_iso, adptbx.u_cart_as_u_iso(u)) assert approx_equal(fc.u_cart_minus_u_iso, [uii - fc.u_iso for uii in u[:3]] + list(u[3:])) f = adptbx.factor_u_star_u_iso(unit_cell=uc, u_star=adptbx.u_cart_as_u_star(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal(f.u_star_minus_u_iso, adptbx.u_cart_as_u_star(uc, fc.u_cart_minus_u_iso)) f = adptbx.factor_u_cif_u_iso(unit_cell=uc, u_cif=adptbx.u_cart_as_u_cif(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal(f.u_cif_minus_u_iso, adptbx.u_cart_as_u_cif(uc, fc.u_cart_minus_u_iso)) f = adptbx.factor_beta_u_iso(unit_cell=uc, beta=adptbx.u_cart_as_beta(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal(f.beta_minus_u_iso, adptbx.u_cart_as_beta(uc, fc.u_cart_minus_u_iso)) assert approx_equal(adptbx.debye_waller_factor_b_iso(0.25, 2.3), math.exp(-2.3 * 0.25)) assert approx_equal(adptbx.debye_waller_factor_u_iso(0.25, 2.3), math.exp(-2.3 * episq * 0.25)) assert approx_equal( adptbx.debye_waller_factor_b_iso(uc, (1, 2, 3), 2.3), adptbx.debye_waller_factor_u_iso(uc, (1, 2, 3), 2.3 / episq)) u_star = adptbx.u_cart_as_u_star(uc, u) dw = adptbx.debye_waller_factor_u_star((1, 2, 3), u_star) assert approx_equal( dw, adptbx.debye_waller_factor_beta((1, 2, 3), adptbx.u_star_as_beta(u_star))) assert approx_equal( dw, adptbx.debye_waller_factor_u_cif(uc, (1, 2, 3), adptbx.u_star_as_u_cif(uc, u_star))) assert approx_equal( dw, adptbx.debye_waller_factor_u_cart(uc, (1, 2, 3), adptbx.u_star_as_u_cart(uc, u_star))) for e in adptbx.eigenvalues(u): check_eigenvalue(u, e) assert not adptbx.is_positive_definite(adptbx.eigenvalues(u)) assert not adptbx.is_positive_definite(adptbx.eigenvalues(u), 0) assert adptbx.is_positive_definite(adptbx.eigenvalues(u), 1.22) assert not adptbx.is_positive_definite(u) assert not adptbx.is_positive_definite(u, 0) assert adptbx.is_positive_definite(u, 1.22) up = (0.534, 0.812, 0.613, 0.0166, 0.134, -0.0124) s = adptbx.eigensystem(up) assert approx_equal(s.values(), (0.813132, 0.713201, 0.432668)) for i in xrange(3): check_eigenvector(up, s.values()[i], s.vectors(i)) c = (1, 2, 3, 3, -4, 5, 4, 5, 6) v = (198, 18, 1020, 116, 447, 269) assert approx_equal(adptbx.c_u_c_transpose(c, u), v) assert approx_equal( adptbx.eigensystem(u).values(), (14.279201519086316, 2.9369143826320214, -1.2161159017183376)) s = adptbx.eigensystem(up) try: s.vectors(4) except RuntimeError, e: assert str(e).endswith("Index out of range.")
def exercise_interface(): episq = 8 * (math.pi ** 2) assert approx_equal(adptbx.u_as_b(2.3), 2.3 * episq) assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(2.3)), 2.3) u = (3, 4, 9, 2, 1, 7) assert approx_equal(adptbx.u_as_b(u), [x * episq for x in u]) assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(u)), u) uc = uctbx.unit_cell((5, 4, 7, 80, 110, 100)) for fw, bw in ( (adptbx.u_cif_as_u_star, adptbx.u_star_as_u_cif), (adptbx.u_cart_as_u_star, adptbx.u_star_as_u_cart), (adptbx.u_cart_as_u_cif, adptbx.u_cif_as_u_cart), (adptbx.u_cart_as_beta, adptbx.beta_as_u_cart), (adptbx.u_cif_as_beta, adptbx.beta_as_u_cif), ): assert approx_equal(bw(uc, fw(uc, u)), u) assert approx_equal(adptbx.beta_as_u_star(adptbx.u_star_as_beta(u)), u) assert approx_equal(adptbx.u_cart_as_u_iso(adptbx.u_iso_as_u_cart(2.3)), 2.3) for fw, bw in ( (adptbx.u_iso_as_u_star, adptbx.u_star_as_u_iso), (adptbx.u_iso_as_u_cif, adptbx.u_cif_as_u_iso), (adptbx.u_iso_as_beta, adptbx.beta_as_u_iso), ): assert approx_equal(bw(uc, fw(uc, 2.3)), 2.3) fc = adptbx.factor_u_cart_u_iso(u_cart=u) assert approx_equal(fc.u_iso, adptbx.u_cart_as_u_iso(u)) assert approx_equal(fc.u_cart_minus_u_iso, [uii - fc.u_iso for uii in u[:3]] + list(u[3:])) f = adptbx.factor_u_star_u_iso(unit_cell=uc, u_star=adptbx.u_cart_as_u_star(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal(f.u_star_minus_u_iso, adptbx.u_cart_as_u_star(uc, fc.u_cart_minus_u_iso)) f = adptbx.factor_u_cif_u_iso(unit_cell=uc, u_cif=adptbx.u_cart_as_u_cif(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal(f.u_cif_minus_u_iso, adptbx.u_cart_as_u_cif(uc, fc.u_cart_minus_u_iso)) f = adptbx.factor_beta_u_iso(unit_cell=uc, beta=adptbx.u_cart_as_beta(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal(f.beta_minus_u_iso, adptbx.u_cart_as_beta(uc, fc.u_cart_minus_u_iso)) assert approx_equal(adptbx.debye_waller_factor_b_iso(0.25, 2.3), math.exp(-2.3 * 0.25)) assert approx_equal(adptbx.debye_waller_factor_u_iso(0.25, 2.3), math.exp(-2.3 * episq * 0.25)) assert approx_equal( adptbx.debye_waller_factor_b_iso(uc, (1, 2, 3), 2.3), adptbx.debye_waller_factor_u_iso(uc, (1, 2, 3), 2.3 / episq), ) u_star = adptbx.u_cart_as_u_star(uc, u) dw = adptbx.debye_waller_factor_u_star((1, 2, 3), u_star) assert approx_equal(dw, adptbx.debye_waller_factor_beta((1, 2, 3), adptbx.u_star_as_beta(u_star))) assert approx_equal(dw, adptbx.debye_waller_factor_u_cif(uc, (1, 2, 3), adptbx.u_star_as_u_cif(uc, u_star))) assert approx_equal(dw, adptbx.debye_waller_factor_u_cart(uc, (1, 2, 3), adptbx.u_star_as_u_cart(uc, u_star))) for e in adptbx.eigenvalues(u): check_eigenvalue(u, e) assert not adptbx.is_positive_definite(adptbx.eigenvalues(u)) assert not adptbx.is_positive_definite(adptbx.eigenvalues(u), 0) assert adptbx.is_positive_definite(adptbx.eigenvalues(u), 1.22) assert not adptbx.is_positive_definite(u) assert not adptbx.is_positive_definite(u, 0) assert adptbx.is_positive_definite(u, 1.22) up = (0.534, 0.812, 0.613, 0.0166, 0.134, -0.0124) s = adptbx.eigensystem(up) assert approx_equal(s.values(), (0.813132, 0.713201, 0.432668)) for i in xrange(3): check_eigenvector(up, s.values()[i], s.vectors(i)) c = (1, 2, 3, 3, -4, 5, 4, 5, 6) v = (198, 18, 1020, 116, 447, 269) assert approx_equal(adptbx.c_u_c_transpose(c, u), v) assert approx_equal(adptbx.eigensystem(u).values(), (14.279201519086316, 2.9369143826320214, -1.2161159017183376)) s = adptbx.eigensystem(up) try: s.vectors(4) except RuntimeError, e: assert str(e).endswith("Index out of range.")
def d_dw_d_u_star_analytical(h, u_star): dw = adptbx.debye_waller_factor_u_star(h, u_star) gc = flex.double(adptbx.debye_waller_factor_u_star_gradient_coefficients(h)) return -2*math.pi**2 * gc * dw
def d2_dw_d_u_star_d_u_star_analytical(h, u_star): dw = adptbx.debye_waller_factor_u_star(h, u_star) cc = flex.double(adptbx.debye_waller_factor_u_star_curvature_coefficients(h)) return 4*math.pi**4 * cc * dw
def exercise_interface(): episq = 8*(math.pi**2) assert approx_equal(adptbx.u_as_b(2.3), 2.3*episq) assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(2.3)), 2.3) u = (3,4,9, 2,1,7) assert approx_equal(adptbx.u_as_b(u), [x*episq for x in u]) assert approx_equal(adptbx.b_as_u(adptbx.u_as_b(u)), u) uc = uctbx.unit_cell((5,4,7,80,110,100)) for fw,bw in ((adptbx.u_cif_as_u_star, adptbx.u_star_as_u_cif), (adptbx.u_cart_as_u_star, adptbx.u_star_as_u_cart), (adptbx.u_cart_as_u_cif, adptbx.u_cif_as_u_cart), (adptbx.u_cart_as_beta, adptbx.beta_as_u_cart), (adptbx.u_cif_as_beta, adptbx.beta_as_u_cif)): assert approx_equal(bw(uc, fw(uc, u)), u) assert approx_equal(adptbx.beta_as_u_star(adptbx.u_star_as_beta(u)), u) assert approx_equal(adptbx.u_cart_as_u_iso(adptbx.u_iso_as_u_cart(2.3)), 2.3) for fw,bw in ((adptbx.u_iso_as_u_star, adptbx.u_star_as_u_iso), (adptbx.u_iso_as_u_cif, adptbx.u_cif_as_u_iso), (adptbx.u_iso_as_beta, adptbx.beta_as_u_iso)): assert approx_equal(bw(uc, fw(uc, 2.3)), 2.3) fc = adptbx.factor_u_cart_u_iso(u_cart=u) assert approx_equal(fc.u_iso, adptbx.u_cart_as_u_iso(u)) assert approx_equal( fc.u_cart_minus_u_iso, [uii-fc.u_iso for uii in u[:3]]+list(u[3:])) f = adptbx.factor_u_star_u_iso( unit_cell=uc, u_star=adptbx.u_cart_as_u_star(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal( f.u_star_minus_u_iso, adptbx.u_cart_as_u_star(uc, fc.u_cart_minus_u_iso)) f = adptbx.factor_u_cif_u_iso( unit_cell=uc, u_cif=adptbx.u_cart_as_u_cif(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal( f.u_cif_minus_u_iso, adptbx.u_cart_as_u_cif(uc, fc.u_cart_minus_u_iso)) f = adptbx.factor_beta_u_iso( unit_cell=uc, beta=adptbx.u_cart_as_beta(uc, u)) assert approx_equal(f.u_iso, fc.u_iso) assert approx_equal( f.beta_minus_u_iso, adptbx.u_cart_as_beta(uc, fc.u_cart_minus_u_iso)) assert approx_equal(adptbx.debye_waller_factor_b_iso(0.25,2.3), math.exp(-2.3*0.25)) assert approx_equal(adptbx.debye_waller_factor_u_iso(0.25,2.3), math.exp(-2.3*episq*0.25)) assert approx_equal(adptbx.debye_waller_factor_b_iso(uc, (1,2,3), 2.3), adptbx.debye_waller_factor_u_iso(uc, (1,2,3), 2.3/episq)) u_star = adptbx.u_cart_as_u_star(uc, u) dw = adptbx.debye_waller_factor_u_star((1,2,3), u_star) assert approx_equal(dw, adptbx.debye_waller_factor_beta((1,2,3), adptbx.u_star_as_beta(u_star))) assert approx_equal(dw, adptbx.debye_waller_factor_u_cif(uc, (1,2,3), adptbx.u_star_as_u_cif(uc, u_star))) assert approx_equal(dw, adptbx.debye_waller_factor_u_cart(uc, (1,2,3), adptbx.u_star_as_u_cart(uc, u_star))) for e in adptbx.eigenvalues(u): check_eigenvalue(u, e) assert not adptbx.is_positive_definite(adptbx.eigenvalues(u)) assert not adptbx.is_positive_definite(adptbx.eigenvalues(u), 0) assert adptbx.is_positive_definite(adptbx.eigenvalues(u), 1.22) assert not adptbx.is_positive_definite(u) assert not adptbx.is_positive_definite(u, 0) assert adptbx.is_positive_definite(u, 1.22) up = (0.534, 0.812, 0.613, 0.0166, 0.134, -0.0124) s = adptbx.eigensystem(up) assert approx_equal(s.values(), (0.813132, 0.713201, 0.432668)) for i in xrange(3): check_eigenvector(up, s.values()[i], s.vectors(i)) c = (1,2,3, 3,-4,5, 4,5,6) v = (198,18,1020,116,447,269) assert approx_equal(adptbx.c_u_c_transpose(c, u), v) assert approx_equal(adptbx.eigensystem(u).values(), (14.279201519086316, 2.9369143826320214, -1.2161159017183376)) s = adptbx.eigensystem(up) try: s.vectors(4) except RuntimeError, e: assert str(e).endswith("Index out of range.") else: raise Exception_expected uf = adptbx.eigenvalue_filtering(u_cart=u, u_min=0) assert approx_equal(uf, (3.0810418, 4.7950710, 9.3400030, 1.7461615, 1.1659954, 6.4800706)) uf = adptbx.eigenvalue_filtering(u_cart=u, u_min=0, u_max=3) assert approx_equal(uf, (2.7430890, 1.0378360, 2.1559895, 0.6193215, -0.3921632, 1.2846854)) uf = adptbx.eigenvalue_filtering(u_cart=u, u_min=0, u_max=3) assert approx_equal(scitbx.linalg.eigensystem.real_symmetric(u).values(), (14.2792015, 2.9369144, -1.2161159)) assert approx_equal(scitbx.linalg.eigensystem.real_symmetric(uf).values(), (3, 2.9369144, 0)) uf = adptbx.eigenvalue_filtering(up) assert approx_equal(uf, up)
def compute_map_coefficients(self): f_obs = self.f_obs_complete.select(self.f_obs_complete.d_spacings().data() >= self.d_min) f_calc = f_obs.structure_factors_from_map(self.map, use_sg=True) f_obs_active = f_obs.select_indices(self.active_indices) minimized = relative_scaling.ls_rel_scale_driver( f_obs_active, f_calc.as_amplitude_array().select_indices(self.active_indices), use_intensities=False, use_weights=False) #minimized.show() f_calc = f_calc.customized_copy(data=f_calc.data()\ * math.exp(-minimized.p_scale)\ * adptbx.debye_waller_factor_u_star( f_calc.indices(), minimized.u_star)) f_calc_active = f_calc.common_set(f_obs_active) matched_indices = f_obs.match_indices(self.f_obs_active) lone_indices_selection = matched_indices.single_selection(0) from mmtbx.max_lik import maxlik alpha_beta_est = maxlik.alpha_beta_est_manager( f_obs=f_obs_active, f_calc=f_calc_active, free_reflections_per_bin=140, flags=flex.bool(f_obs_active.size()), interpolation=True, epsilons=f_obs_active.epsilons().data().as_double()) alpha, beta = alpha_beta_est.alpha_beta_for_each_reflection( f_obs=self.f_obs_complete.select(self.f_obs_complete.d_spacings().data() >= self.d_min)) f_obs.data().copy_selected( lone_indices_selection.iselection(), flex.abs(f_calc.data())) t = maxlik.fo_fc_alpha_over_eps_beta( f_obs=f_obs, f_model=f_calc, alpha=alpha, beta=beta) hl_coeff = flex.hendrickson_lattman( t * flex.cos(f_calc.phases().data()), t * flex.sin(f_calc.phases().data())) dd = alpha.data() # hl_array = f_calc.array( data=self.hl_coeffs_start.common_set(f_calc).data()+hl_coeff) self.compute_phase_source(hl_array) fom = flex.abs(self.phase_source.data()) mFo = hl_array.array(data=f_obs.data()*self.phase_source.data()) DFc = hl_array.array(data=dd*f_calc.as_amplitude_array().phase_transfer( self.phase_source).data()) centric_flags = f_obs.centric_flags().data() acentric_flags = ~centric_flags fo_scale = flex.double(centric_flags.size()) fc_scale = flex.double(centric_flags.size()) fo_scale.set_selected(acentric_flags, 2) fo_scale.set_selected(centric_flags, 1) fc_scale.set_selected(acentric_flags, 1) fc_scale.set_selected(centric_flags, 0) fo_scale.set_selected(lone_indices_selection, 0) fc_scale.set_selected(lone_indices_selection, -1) self.map_coeffs = hl_array.array( data=mFo.data()*fo_scale - DFc.data()*fc_scale) self.fom = hl_array.array(data=fom) self.hl_coeffs = hl_array # statistics self.r1_factor = f_obs_active.r1_factor(f_calc_active) fom = fom.select(matched_indices.pair_selection(0)) self.r1_factor_fom = flex.sum( fom * flex.abs(f_obs_active.data() - f_calc_active.as_amplitude_array().data())) \ / flex.sum(fom * f_obs_active.data()) phase_source, phase_source_previous = self.phase_source.common_sets( self.phase_source_previous) self.mean_delta_phi = phase_error( flex.arg(phase_source.data()), flex.arg(phase_source_previous.data())) phase_source, phase_source_initial = self.phase_source.common_sets( self.phase_source_initial) self.mean_delta_phi_initial = phase_error( flex.arg(phase_source.data()), flex.arg(phase_source_initial.data())) self.mean_fom = flex.mean(fom) fom = f_obs_active.array(data=fom) if fom.data().size()<1000: # 2013-12-14 was hard-wired at 1000 tt reflections_per_bin=fom.data().size() else: reflections_per_bin=1000 fom.setup_binner(reflections_per_bin=reflections_per_bin) self.mean_fom_binned = fom.mean(use_binning=True)
def compute_map_coefficients(self): f_obs = self.f_obs_complete.select( self.f_obs_complete.d_spacings().data() >= self.d_min) f_calc = f_obs.structure_factors_from_map(self.map, use_sg=True) f_obs_active = f_obs.select_indices(self.active_indices) minimized = relative_scaling.ls_rel_scale_driver( f_obs_active, f_calc.as_amplitude_array().select_indices(self.active_indices), use_intensities=False, use_weights=False) #minimized.show() f_calc = f_calc.customized_copy(data=f_calc.data()\ * math.exp(-minimized.p_scale)\ * adptbx.debye_waller_factor_u_star( f_calc.indices(), minimized.u_star)) f_calc_active = f_calc.common_set(f_obs_active) matched_indices = f_obs.match_indices(self.f_obs_active) lone_indices_selection = matched_indices.single_selection(0) from mmtbx.max_lik import maxlik alpha_beta_est = maxlik.alpha_beta_est_manager( f_obs=f_obs_active, f_calc=f_calc_active, free_reflections_per_bin=140, flags=flex.bool(f_obs_active.size()), interpolation=True, epsilons=f_obs_active.epsilons().data().as_double()) alpha, beta = alpha_beta_est.alpha_beta_for_each_reflection( f_obs=self.f_obs_complete.select( self.f_obs_complete.d_spacings().data() >= self.d_min)) f_obs.data().copy_selected(lone_indices_selection.iselection(), flex.abs(f_calc.data())) t = maxlik.fo_fc_alpha_over_eps_beta(f_obs=f_obs, f_model=f_calc, alpha=alpha, beta=beta) hl_coeff = flex.hendrickson_lattman( t * flex.cos(f_calc.phases().data()), t * flex.sin(f_calc.phases().data())) dd = alpha.data() # hl_array = f_calc.array( data=self.hl_coeffs_start.common_set(f_calc).data() + hl_coeff) self.compute_phase_source(hl_array) fom = flex.abs(self.phase_source.data()) mFo = hl_array.array(data=f_obs.data() * self.phase_source.data()) DFc = hl_array.array(data=dd * f_calc.as_amplitude_array().phase_transfer( self.phase_source).data()) centric_flags = f_obs.centric_flags().data() acentric_flags = ~centric_flags fo_scale = flex.double(centric_flags.size()) fc_scale = flex.double(centric_flags.size()) fo_scale.set_selected(acentric_flags, 2) fo_scale.set_selected(centric_flags, 1) fc_scale.set_selected(acentric_flags, 1) fc_scale.set_selected(centric_flags, 0) fo_scale.set_selected(lone_indices_selection, 0) fc_scale.set_selected(lone_indices_selection, -1) self.map_coeffs = hl_array.array(data=mFo.data() * fo_scale - DFc.data() * fc_scale) self.fom = hl_array.array(data=fom) self.hl_coeffs = hl_array # statistics self.r1_factor = f_obs_active.r1_factor(f_calc_active) fom = fom.select(matched_indices.pair_selection(0)) self.r1_factor_fom = flex.sum( fom * flex.abs(f_obs_active.data() - f_calc_active.as_amplitude_array().data())) \ / flex.sum(fom * f_obs_active.data()) phase_source, phase_source_previous = self.phase_source.common_sets( self.phase_source_previous) self.mean_delta_phi = phase_error( flex.arg(phase_source.data()), flex.arg(phase_source_previous.data())) phase_source, phase_source_initial = self.phase_source.common_sets( self.phase_source_initial) self.mean_delta_phi_initial = phase_error( flex.arg(phase_source.data()), flex.arg(phase_source_initial.data())) self.mean_fom = flex.mean(fom) fom = f_obs_active.array(data=fom) if fom.data().size() < 1000: # 2013-12-14 was hard-wired at 1000 tt reflections_per_bin = fom.data().size() else: reflections_per_bin = 1000 fom.setup_binner(reflections_per_bin=reflections_per_bin) self.mean_fom_binned = fom.mean(use_binning=True)
def run(): # # try these Hall symbols: # "P 1", "P 2", "P 3", "P 3*", "P 4", "P 6", "P 2 2 3" # space_group = sgtbx.space_group("P 3*") # Hall symbol # # initialization of space group symmetry constraints # adp_constraints = sgtbx.tensor_rank_2_constraints(space_group=space_group, reciprocal_space=True) # # number of independent u_star parameters # n_indep = adp_constraints.n_independent_params() # # arbitrary Miller index and u_star tensor # h = (3, 1, 2) u_star = (0.000004, 0.000004, 0.000007, 0.000002, 0.0000000, 0.0000000) # optional: enforce symmetry at the beginning u_star = space_group.average_u_star(u_star) # # pass u_indep to the minimizer # u_indep = adp_constraints.independent_params(all_params=u_star) assert len(u_indep) == n_indep # # "expand" the independent parameters modified by the minimizer # u_star = adp_constraints.all_params(independent_params=u_indep) assert len(u_star) == 6 # # these calculations are completely independent of the symmetry # dwf = adptbx.debye_waller_factor_u_star(h, u_star) gc = adptbx.debye_waller_factor_u_star_gradient_coefficients(h) # all_gradients is an array of six values all_gradients = [-2 * math.pi**2 * dwf * c for c in gc] assert len(all_gradients) == 6 cc = adptbx.debye_waller_factor_u_star_curvature_coefficients(h) # all_curvatures is an array of 21 values (upper triangle of 6x6 matrix) all_curvatures = (-2 * math.pi**2)**2 * dwf * cc assert len(all_curvatures) == 6 * (6 + 1) // 2 # # here we apply the symmetry constraints to the gradients and curvatures # # g_indep is an array of n_indep values g_indep = adp_constraints.independent_gradients( all_gradients=all_gradients) assert len(g_indep) == n_indep # c_indep is an array of n_indep*(n_indep+1)/2 values (upper triangle) c_indep = adp_constraints.independent_curvatures( all_curvatures=all_curvatures) assert len(c_indep) == n_indep * (n_indep + 1) // 2 # feed g_indep and c_indep to the minimizer # # initialization of site symmetry constraints # (for sites on special positions) # unit_cell = uctbx.unit_cell((12, 12, 15, 90, 90, 120)) space_group = sgtbx.space_group_info("P 6").group() site_symmetry = sgtbx.site_symmetry( unit_cell=unit_cell, space_group=space_group, original_site=(1 / 3., 2 / 3., 0), # site on 3-fold axis min_distance_sym_equiv=0.5) assert len(site_symmetry.matrices()) == 3 adp_constraints = site_symmetry.adp_constraints() # use adp_constraints as before print("OK")
def run(): # # try these Hall symbols: # "P 1", "P 2", "P 3", "P 3*", "P 4", "P 6", "P 2 2 3" # space_group = sgtbx.space_group("P 3*") # Hall symbol # # initialization of space group symmetry constraints # adp_constraints = sgtbx.tensor_rank_2_constraints( space_group=space_group, reciprocal_space=True) # # number of independent u_star parameters # n_indep = adp_constraints.n_independent_params() # # arbitrary Miller index and u_star tensor # h = (3,1,2) u_star=(0.000004, 0.000004, 0.000007, 0.000002, 0.0000000, 0.0000000) # optional: enforce symmetry at the beginning u_star = space_group.average_u_star(u_star) # # pass u_indep to the minimizer # u_indep = adp_constraints.independent_params(all_params=u_star) assert len(u_indep) == n_indep # # "expand" the independent parameters modified by the minimizer # u_star = adp_constraints.all_params(independent_params=u_indep) assert len(u_star) == 6 # # these calculations are completely independent of the symmetry # dwf = adptbx.debye_waller_factor_u_star(h, u_star) gc = adptbx.debye_waller_factor_u_star_gradient_coefficients(h) # all_gradients is an array of six values all_gradients = [-2*math.pi**2 * dwf * c for c in gc] assert len(all_gradients) == 6 cc = adptbx.debye_waller_factor_u_star_curvature_coefficients(h) # all_curvatures is an array of 21 values (upper triangle of 6x6 matrix) all_curvatures = (-2*math.pi**2)**2 * dwf * cc assert len(all_curvatures) == 6*(6+1)//2 # # here we apply the symmetry constraints to the gradients and curvatures # # g_indep is an array of n_indep values g_indep = adp_constraints.independent_gradients( all_gradients=all_gradients) assert len(g_indep) == n_indep # c_indep is an array of n_indep*(n_indep+1)/2 values (upper triangle) c_indep = adp_constraints.independent_curvatures( all_curvatures=all_curvatures) assert len(c_indep) == n_indep*(n_indep+1)//2 # feed g_indep and c_indep to the minimizer # # initialization of site symmetry constraints # (for sites on special positions) # unit_cell = uctbx.unit_cell((12,12,15,90,90,120)) space_group = sgtbx.space_group_info("P 6").group() site_symmetry = sgtbx.site_symmetry( unit_cell=unit_cell, space_group=space_group, original_site=(1/3.,2/3.,0), # site on 3-fold axis min_distance_sym_equiv=0.5) assert len(site_symmetry.matrices()) == 3 adp_constraints = site_symmetry.adp_constraints() # use adp_constraints as before print "OK"