def ortogonalize_matrix(m_ij, m_norm_ij): """Ortogonalize matrix. matrix m_ij is defined in coordinate system (a, b, c). It is given as tuple Matrix m_norm used to recalculate coordinates from direct space (a1/|a1|, a2/|a2|, a3/|a3|) to Cartesian one (x||a*, z||c). x_cart = m_norm * x_direct m_norm = [[(1 - cos**2 alpha1 - cos**2 alpha2 - cos**2 alpha3 + \ 2 cos alpha1 cos alpha2 cos alpha3)**0.5/sin(alpha1), 0, 0], [(cos alpha3 - cos alpha1 cos alpha2) / sin alpha1, sin alpha1, 0], [cos alpha2, cos alpha1, 1]] Should be given as m_norm_ij = (_11, _12, _13, _21, _22, _23, _31, _32, _33) output matrix s_ij is defined in Cartezian coordinate system defined as x||a*, z||c, y= [z x] (right handed) """ s_11, s_12, s_13, s_21, s_22, s_23, s_31, s_32, s_33 = calc_mRmCmRT( m_norm_ij, m_ij) return s_11, s_12, s_13, s_21, s_22, s_23, s_31, s_32, s_33
def calc_form_factor_tensor_susceptibility(chi_11, chi_22, chi_33, chi_12, chi_13, chi_23, space_group_symop, form_factor, cell, h, k, l): """Give components of form factor tensor for susceptibility. fft_11, fft_12, fft_13 fft_21, fft_22, fft_23 fft_31, fft_32, fft_33 in 3 dimension (hkl, atoms, symmetry elements). """ ff = numpy.array(form_factor, dtype=float) sthovl = cell.calc_sthovl(h, k, l) # dimension (hkl, atoms) r_11 = numpy.array(space_group_symop.r_11, dtype=float) r_12 = numpy.array(space_group_symop.r_12, dtype=float) r_13 = numpy.array(space_group_symop.r_13, dtype=float) r_21 = numpy.array(space_group_symop.r_21, dtype=float) r_22 = numpy.array(space_group_symop.r_22, dtype=float) r_23 = numpy.array(space_group_symop.r_23, dtype=float) r_31 = numpy.array(space_group_symop.r_31, dtype=float) r_32 = numpy.array(space_group_symop.r_32, dtype=float) r_33 = numpy.array(space_group_symop.r_33, dtype=float) chi_21, chi_31, chi_32 = chi_12, chi_13, chi_23 c11, r11 = numpy.meshgrid(chi_11, r_11, indexing="ij") c22, r22 = numpy.meshgrid(chi_22, r_22, indexing="ij") c33, r33 = numpy.meshgrid(chi_33, r_33, indexing="ij") c12, r12 = numpy.meshgrid(chi_12, r_12, indexing="ij") c13, r13 = numpy.meshgrid(chi_13, r_13, indexing="ij") c23, r23 = numpy.meshgrid(chi_23, r_23, indexing="ij") c21, r21 = numpy.meshgrid(chi_21, r_21, indexing="ij") c31, r31 = numpy.meshgrid(chi_31, r_31, indexing="ij") c32, r32 = numpy.meshgrid(chi_32, r_32, indexing="ij") rcrt_11, rcrt_12, rcrt_13, rcrt_21, rcrt_22, rcrt_23, rcrt_31, rcrt_32, \ rcrt_33 = calc_mRmCmRT( (r11, r12, r13, r21, r22, r23, r31, r32, r33), (c11, c12, c13, c21, c22, c23, c31, c32, c33)) # dimension (hkl, atoms, symmetry) n_a = numpy.newaxis fft_11 = ff[:, :, n_a] * rcrt_11[n_a, :, :] fft_12 = ff[:, :, n_a] * rcrt_12[n_a, :, :] fft_13 = ff[:, :, n_a] * rcrt_13[n_a, :, :] fft_21 = ff[:, :, n_a] * rcrt_21[n_a, :, :] fft_22 = ff[:, :, n_a] * rcrt_22[n_a, :, :] fft_23 = ff[:, :, n_a] * rcrt_23[n_a, :, :] fft_31 = ff[:, :, n_a] * rcrt_31[n_a, :, :] fft_32 = ff[:, :, n_a] * rcrt_32[n_a, :, :] fft_33 = ff[:, :, n_a] * rcrt_33[n_a, :, :] # ortogonalization should be done return fft_11, fft_12, fft_13, fft_21, fft_22, fft_23, fft_31, fft_32, \ fft_33
def calc_moment_2d_by_susceptibility(r_ij, susc_i, m_norm_ij, h_loc): """Recalculate chi_i given according to symmetry elements for each point. chi_i is given in reciprocal unit cell. After susceptibility is multiplied in magnetic field defined r_ij:= r_11, r_12, r_13, r_21, r_22, r_23, r_31, r_32, r_33 susc_i:= chi_11, chi_22, chi_33, chi_12, chi_13, chi_23 Matrix m_norm used to recalculate coordinates from direct space (a1/|a1|, a2/|a2|, a3/|a3|) to Cartesian one (x||a*, z||c). x_cart = m_norm * x_direct m_norm = [[(1 - cos**2 alpha1 - cos**2 alpha2 - cos**2 alpha3 + \ 2 cos alpha1 cos alpha2 cos alpha3)**0.5/sin(alpha1), 0, 0], [(cos alpha3 - cos alpha1 cos alpha2) / sin alpha1, sin alpha1, 0], [cos alpha2, cos alpha1, 1]] Matrix m_norm should be given as m_norm_ij = (_11, _12, _13, _21, _22, _23, _31, _32, _33) Output ------ moment_2d: [points, symmetry] """ n_a = numpy.newaxis r_11, r_12, r_13, r_21, r_22, r_23, r_31, r_32, r_33 = r_ij chi_11, chi_22, chi_33, chi_12, chi_13, chi_23 = susc_i # [ind, symm] chi_2d_ij = calc_mRmCmRT( (r_11[n_a, :], r_12[n_a, :], r_13[n_a, :], r_21[n_a, :], r_22[n_a, :], r_23[n_a, :], r_31[n_a, :], r_32[n_a, :], r_33[n_a, :]), (chi_11[:, n_a], chi_12[:, n_a], chi_13[:, n_a], chi_12[:, n_a], chi_22[:, n_a], chi_23[:, n_a], chi_13[:, n_a], chi_23[:, n_a], chi_33[:, n_a])) chi_orto_ij = ortogonalize_matrix(chi_2d_ij, m_norm_ij) moment_2d = calc_product_matrix_vector(chi_orto_ij, h_loc) return moment_2d
def transfer_to_chi_3d(np_indexes, chi_11, chi_22, chi_33, chi_12, chi_13, chi_23, n_xyz, r_ij, b_i): """ Give six 3D arrays of susceptibility. Input arguments: - np_xyz is numpy array of integer numbers; - val_1, ... are numpy array of float numbers. """ (n_x, n_y, n_z) = n_xyz (i_x, i_y, i_z) = np_indexes chi_3d_11, chi_3d_22 = numpy.zeros(n_xyz), numpy.zeros(n_xyz) chi_3d_33, chi_3d_12 = numpy.zeros(n_xyz), numpy.zeros(n_xyz) chi_3d_13, chi_3d_23 = numpy.zeros(n_xyz), numpy.zeros(n_xyz) for r_11, r_12, r_13, r_21, r_22, r_23, r_31, r_32, r_33, b_1, b_2, b_3 \ in zip(*r_ij, *b_i): np_ind_x = numpy.mod((numpy.around( (i_x * r_11 + i_y * r_12 + i_z * r_13 + n_x * b_1).astype(float), 0)).astype(int), n_x) np_ind_y = numpy.mod((numpy.around( (i_x * r_21 + i_y * r_22 + i_z * r_23 + n_y * b_2).astype(float), 0)).astype(int), n_y) np_ind_z = numpy.mod((numpy.around( (i_x * r_31 + i_y * r_32 + i_z * r_33 + n_z * b_3).astype(float), 0)).astype(int), n_z) chi_out = calc_mRmCmRT( (r_11, r_12, r_13, r_21, r_22, r_23, r_31, r_32, r_33), (chi_11, chi_12, chi_13, chi_12, chi_22, chi_23, chi_13, chi_23, chi_33)) chi_11_rot, chi_12_rot, chi_13_rot, chi_21_rot, chi_22_rot, \ chi_23_rot, chi_31_rot, chi_32_rot, chi_33_rot = chi_out chi_3d_11[np_ind_x, np_ind_y, np_ind_z] = chi_11_rot chi_3d_22[np_ind_x, np_ind_y, np_ind_z] = chi_22_rot chi_3d_33[np_ind_x, np_ind_y, np_ind_z] = chi_33_rot chi_3d_12[np_ind_x, np_ind_y, np_ind_z] = chi_12_rot chi_3d_13[np_ind_x, np_ind_y, np_ind_z] = chi_13_rot chi_3d_23[np_ind_x, np_ind_y, np_ind_z] = chi_23_rot return chi_3d_11, chi_3d_22, chi_3d_33, chi_3d_12, chi_3d_13, chi_3d_23
def calc_for_iint(self, index_h, index_k, index_l, crystal, flag_internal: bool = True): """Calculate the integral intensity for h, k, l reflections.""" setup = self.setup field = float(setup.field) refln = crystal.calc_refln(index_h, index_k, index_l) refln_s = crystal.calc_refln_susceptibility(index_h, index_k, index_l) f_nucl = refln.numpy_f_calc sft_11 = refln_s.numpy_chi_11_calc sft_12 = refln_s.numpy_chi_12_calc sft_13 = refln_s.numpy_chi_13_calc sft_21 = refln_s.numpy_chi_21_calc sft_22 = refln_s.numpy_chi_22_calc sft_23 = refln_s.numpy_chi_23_calc sft_31 = refln_s.numpy_chi_31_calc sft_32 = refln_s.numpy_chi_32_calc sft_33 = refln_s.numpy_chi_33_calc sftm_11 = refln_s.numpy_moment_11_calc sftm_12 = refln_s.numpy_moment_12_calc sftm_13 = refln_s.numpy_moment_13_calc sftm_21 = refln_s.numpy_moment_21_calc sftm_22 = refln_s.numpy_moment_22_calc sftm_23 = refln_s.numpy_moment_23_calc sftm_31 = refln_s.numpy_moment_31_calc sftm_32 = refln_s.numpy_moment_32_calc sftm_33 = refln_s.numpy_moment_33_calc _11, _12 = sftm_11+field*sft_11, sftm_12+field*sft_12 _21, _13 = sftm_21+field*sft_21, sftm_13+field*sft_13 _22, _23 = sftm_22+field*sft_22, sftm_23+field*sft_23 _31, _32 = sftm_31+field*sft_31, sftm_32+field*sft_32 _33 = sftm_33+field*sft_33 _ij = (_11, _12, _13, _21, _22, _23, _31, _32, _33) cell = crystal.cell # k_loc = cell.calc_k_loc(index_h, index_k, index_l) t_ij = cell.calc_m_t(index_h, index_k, index_l) # FIXME: I would like to recheck the expression for T # and expression SIGMA = T^T CHI T t_tr_ij = (t_ij[0], t_ij[3], t_ij[6], t_ij[1], t_ij[4], t_ij[7], t_ij[2], t_ij[5], t_ij[8]) th_11, th_12, th_13, th_21, th_22, th_23, th_31, th_32, th_33 = \ calc_mRmCmRT(t_tr_ij, _ij) # f_m_p_sin_sq = (field**2)*abs(0.5*(th_11*th_11.conjugate()+\ # th_22*th_22.conjugate())+th_12*th_12.conjugate()) # f_m_p_cos_sq = (field**2)*abs(th_13*th_13.conjugate()+\ # th_23*th_23.conjugate()) # f_m_p_field = 0.5*field*(th_11+th_22) f_nucl_sq = abs(f_nucl*f_nucl.conjugate()) f_m_p_sin_sq = abs(0.5 * (th_11 * th_11.conjugate()+th_22 * th_22.conjugate())+th_12 * th_12.conjugate()) f_m_p_cos_sq = abs(th_13 * th_13.conjugate() + th_23 * th_23.conjugate()) f_m_p_field = 0.5 * (th_11+th_22) cross_sin = 2. * (f_nucl.real * f_m_p_field.real + f_nucl.imag * f_m_p_field.imag) return f_nucl_sq, f_m_p_sin_sq, f_m_p_cos_sq, cross_sin, refln, refln_s
def calc_iint(self, index_h, index_k, index_l, crystal: Crystal, flag_internal: bool = True): """Calculate the integrated intensity for h, k, l reflections. Arguments --------- - h, k, l: 1D numpy array of Miller indexes, dtype = int32 - l_crystal: a list of Crystal objects of cryspy library - flag_internal: a flag to calculate or to use internal objects. It should be True if user call the function. It's True by default. Output ------ - iint_u: 1D numpy array of integrated intensity up, dtype = float - iint_d: 1D numpy array of integrated intensity up, dtype = float - refln: ReflnL object of cryspy library (nuclear structure factor) - refln_s: ReflnSusceptibilityL object of cryspy library (nuclear structure factor) """ if flag_internal: try: d_internal_val = self.d_internal_val except AttributeError: d_internal_val = {} self.d_internal_val = d_internal_val else: d_internal_val = {} self.d_internal_val = d_internal_val tof_parameters = self.tof_parameters try: field = tof_parameters.field except AttributeError: field = 0. try: diffrn_radiation = self.diffrn_radiation p_u = float(diffrn_radiation.polarization) p_d = (2.*float(diffrn_radiation.efficiency)-1)*p_u except AttributeError: p_u = 0.0 p_d = 0.0 try: if (not(flag_internal) | crystal.is_variables()): raise KeyError refln = d_internal_val[f"refln_{crystal.data_name:}"] except KeyError: refln = crystal.calc_refln(index_h, index_k, index_l, flag_internal=flag_internal) d_internal_val[f"refln_{crystal.data_name:}"] = refln f_nucl = refln.numpy_f_calc f_nucl_sq = abs(f_nucl*f_nucl.conjugate()) if isinstance(crystal, Crystal): try: if (not(flag_internal) | crystal.is_variables()): raise KeyError refln_s = d_internal_val[ f"refln_susceptibility_{crystal.data_name:}"] except KeyError: refln_s = crystal.calc_refln_susceptibility( index_h, index_k, index_l, flag_internal=flag_internal) d_internal_val[f"refln_susceptibility_{crystal.data_name:}"] \ = refln_s sft_11 = refln_s.numpy_chi_11_calc sft_12 = refln_s.numpy_chi_12_calc sft_13 = refln_s.numpy_chi_13_calc sft_21 = refln_s.numpy_chi_21_calc sft_22 = refln_s.numpy_chi_22_calc sft_23 = refln_s.numpy_chi_23_calc sft_31 = refln_s.numpy_chi_31_calc sft_32 = refln_s.numpy_chi_32_calc sft_33 = refln_s.numpy_chi_33_calc sftm_11 = refln_s.numpy_moment_11_calc sftm_12 = refln_s.numpy_moment_12_calc sftm_13 = refln_s.numpy_moment_13_calc sftm_21 = refln_s.numpy_moment_21_calc sftm_22 = refln_s.numpy_moment_22_calc sftm_23 = refln_s.numpy_moment_23_calc sftm_31 = refln_s.numpy_moment_31_calc sftm_32 = refln_s.numpy_moment_32_calc sftm_33 = refln_s.numpy_moment_33_calc _11, _12 = sftm_11+field*sft_11, sftm_12+field*sft_12 _21, _13 = sftm_21+field*sft_21, sftm_13+field*sft_13 _22, _23 = sftm_22+field*sft_22, sftm_23+field*sft_23 _31, _32 = sftm_31+field*sft_31, sftm_32+field*sft_32 _33 = sftm_33+field*sft_33 _ij = (_11, _12, _13, _21, _22, _23, _31, _32, _33) cell = crystal.cell # k_loc = cell.calc_k_loc(h, k, l) t_ij = cell.calc_m_t(index_h, index_k, index_l) # FIXME: I would like to recheck the expression for T # and expression SIGMA = T^T CHI T t_tr_ij = (t_ij[0], t_ij[3], t_ij[6], t_ij[1], t_ij[4], t_ij[7], t_ij[2], t_ij[5], t_ij[8]) th_11, th_12, th_13, th_21, th_22, th_23, th_31, th_32, th_33 = \ calc_mRmCmRT(t_tr_ij, _ij) # fm_p_sq = (field**2)*abs(0.5*(th_11*th_11.conjugate()+ # th_22*th_22.conjugate())+th_12*th_12.conjugate()) # fm_p_field = field*0.5*(th_11+th_22) fm_p_sq = abs(0.5 * (th_11 * th_11.conjugate() + th_22 * th_22.conjugate()) + th_12 * th_12.conjugate()) fm_p_field = 0.5*(th_11 + th_22) cross = 2.*(f_nucl.real*fm_p_field.real + f_nucl.imag*fm_p_field.imag) iint_u = f_nucl_sq + fm_p_sq + p_u*cross iint_d = f_nucl_sq + fm_p_sq - p_d*cross elif isinstance(crystal, MagCrystal): try: if (not(flag_internal) | crystal.is_variables()): raise KeyError f_mag_perp = d_internal_val[f"f_mag_perp_{crystal.data_name:}"] except KeyError: f_mag_perp = crystal.calc_f_mag_perp(index_h, index_k, index_l) d_internal_val[f"f_mag_perp_{crystal.data_name:}"] = f_mag_perp f_mag_perp_sq = abs(f_mag_perp*f_mag_perp.conjugate()).sum(axis=0) iint_u = f_nucl_sq + f_mag_perp_sq iint_d = f_nucl_sq + f_mag_perp_sq return iint_u, iint_d
def calc_susc_i(self, atom_site_susceptibility: AtomSiteSusceptibilityL): """Calculate susceptibility of point i. Parameters ---------- atom_site_susceptibility : AtomSiteSusceptibilityL DESCRIPTION. Returns ------- susc_11 : TYPE DESCRIPTION. susc_22 : TYPE DESCRIPTION. susc_33 : TYPE DESCRIPTION. susc_12 : TYPE DESCRIPTION. susc_13 : TYPE DESCRIPTION. susc_23 : TYPE DESCRIPTION. """ numpy_basin_atom_label = self.numpy_basin_atom_label numpy_basin_atom_symop = self.numpy_basin_atom_symop if numpy_basin_atom_label is None: numpy_basin_atom_label = numpy.array(self.basin_atom_label, dtype=str) numpy_basin_atom_symop = numpy.array(self.basin_atom_symop, dtype=str) self.numpy_basin_atom_label = numpy_basin_atom_label self.numpy_basin_atom_symop = numpy_basin_atom_symop np_label = numpy.array(atom_site_susceptibility.label, dtype=str) np_chi_11 = numpy.array(atom_site_susceptibility.chi_11, dtype=float) np_chi_22 = numpy.array(atom_site_susceptibility.chi_22, dtype=float) np_chi_33 = numpy.array(atom_site_susceptibility.chi_33, dtype=float) np_chi_12 = numpy.array(atom_site_susceptibility.chi_12, dtype=float) np_chi_13 = numpy.array(atom_site_susceptibility.chi_13, dtype=float) np_chi_23 = numpy.array(atom_site_susceptibility.chi_23, dtype=float) l_r = [] for symop in numpy_basin_atom_symop: r, b = transform_string_to_r_b(symop) r = r.astype(float) l_r.append((r[0, 0], r[0, 1], r[0, 2], r[1, 0], r[1, 1], r[1, 2], r[2, 0], r[2, 1], r[2, 2])) np_r = numpy.array(l_r, dtype=float) np_r_11, np_r_12, np_r_13 = np_r[:, 0], np_r[:, 1], np_r[:, 2] np_r_21, np_r_22, np_r_23 = np_r[:, 3], np_r[:, 4], np_r[:, 5] np_r_31, np_r_32, np_r_33 = np_r[:, 6], np_r[:, 7], np_r[:, 8] susc_11 = numpy.zeros(numpy_basin_atom_label.size, dtype=float) susc_22 = numpy.zeros(numpy_basin_atom_label.size, dtype=float) susc_33 = numpy.zeros(numpy_basin_atom_label.size, dtype=float) susc_12 = numpy.zeros(numpy_basin_atom_label.size, dtype=float) susc_13 = numpy.zeros(numpy_basin_atom_label.size, dtype=float) susc_23 = numpy.zeros(numpy_basin_atom_label.size, dtype=float) for _l, chi_11, chi_22, chi_33, chi_12, chi_13, chi_23 in \ zip(np_label, np_chi_11, np_chi_22, np_chi_33, np_chi_12, np_chi_13, np_chi_23): f_1 = numpy_basin_atom_label == _l chi_ij = (chi_11, chi_12, chi_13, chi_12, chi_22, chi_23, chi_13, chi_23, chi_33) c_11, c_12, c_13, c_21, c_22, c_23, c_31, c_32, c_33 = \ calc_mRmCmRT((np_r_11[f_1], np_r_12[f_1], np_r_13[f_1], np_r_21[f_1], np_r_22[f_1], np_r_23[f_1], np_r_31[f_1], np_r_32[f_1], np_r_33[f_1]), chi_ij) susc_11[f_1] = c_11 susc_22[f_1] = c_22 susc_33[f_1] = c_33 susc_12[f_1] = c_12 susc_13[f_1] = c_13 susc_23[f_1] = c_23 l_rs_i = self.l_rs_i l_c_11, l_c_22, l_c_33, l_c_12, l_c_13, l_c_23 = [], [], [], [], [], [] for s_11, s_22, s_33, s_12, s_13, s_23, rs_i in zip( susc_11, susc_22, susc_33, susc_12, susc_13, susc_23, l_rs_i): r_11, r_12, r_13, r_21, r_22, r_23, r_31, r_32, r_33 = rs_i c_11, c_12, c_13, c_21, c_22, c_23, c_31, c_32, c_33 = \ calc_mRmCmRT(rs_i, (s_11, s_12, s_13, s_12, s_22, s_23, s_13, s_23, s_33)) ns = c_11.size l_c_11.append(c_11.sum()/float(ns)) l_c_22.append(c_22.sum()/float(ns)) l_c_33.append(c_33.sum()/float(ns)) l_c_12.append(c_12.sum()/float(ns)) l_c_13.append(c_13.sum()/float(ns)) l_c_23.append(c_23.sum()/float(ns)) chi_11 = numpy.array(l_c_11, dtype=float) chi_22 = numpy.array(l_c_22, dtype=float) chi_33 = numpy.array(l_c_33, dtype=float) chi_12 = numpy.array(l_c_12, dtype=float) chi_13 = numpy.array(l_c_13, dtype=float) chi_23 = numpy.array(l_c_23, dtype=float) return chi_11, chi_22, chi_33, chi_12, chi_13, chi_23