def test_reciprocal_lattice(self): Mg2Si = Lattice.from_parameters(1.534, 0.405, 0.683, 90., 106., 90., x_aligned_with_a=False) [astar, bstar, cstar] = Mg2Si.reciprocal_lattice() self.assertAlmostEqual(astar[0], 0.678, 3) self.assertAlmostEqual(astar[1], 0., 3) self.assertAlmostEqual(astar[2], 0., 3) self.assertAlmostEqual(bstar[0], 0., 3) self.assertAlmostEqual(bstar[1], 2.469, 3) self.assertAlmostEqual(bstar[2], 0., 3) self.assertAlmostEqual(cstar[0], 0.420, 3) self.assertAlmostEqual(cstar[1], 0., 3) self.assertAlmostEqual(cstar[2], 1.464, 3)
def test_110_normal_monoclinic(self): """Testing (110) plane normal in monoclinic crystal structure. This test comes from http://www.mse.mtu.edu/~drjohn/my3200/stereo/sg5.html corrected for a few errors in the html page. In this test, the lattice is defined with the c-axis aligned with the Z direction of the Cartesian frame. """ Mg2Si = Lattice.from_parameters(1.534, 0.405, 0.683, 90., 106., 90., x_aligned_with_a=False) a = Mg2Si.matrix[0] b = Mg2Si.matrix[1] c = Mg2Si.matrix[2] self.assertAlmostEqual(a[0], 1.475, 3) self.assertAlmostEqual(a[1], 0., 3) self.assertAlmostEqual(a[2], -0.423, 3) self.assertAlmostEqual(b[0], 0., 3) self.assertAlmostEqual(b[1], 0.405, 3) self.assertAlmostEqual(b[2], 0., 3) self.assertAlmostEqual(c[0], 0., 3) self.assertAlmostEqual(c[1], 0., 3) self.assertAlmostEqual(c[2], 0.683, 3) p = HklPlane(1, 1, 1, Mg2Si) Gc = p.scattering_vector() self.assertAlmostEqual(Gc[0], 1.098, 3) self.assertAlmostEqual(Gc[1], 2.469, 3) self.assertAlmostEqual(Gc[2], 1.464, 3) self.assertAlmostEqual(p.interplanar_spacing(), 0.325, 3) Ghkl = np.dot(Mg2Si.matrix, Gc) self.assertEqual(Ghkl[0], 1.) # h self.assertEqual(Ghkl[1], 1.) # k self.assertEqual(Ghkl[2], 1.) # l
def load(file_path='experiment.txt'): with open(file_path, 'r') as f: dict_exp = json.load(f) sample = Sample() sample.set_name(dict_exp['Sample']['Name']) sample.set_position(dict_exp['Sample']['Position']) if 'Geometry' in dict_exp['Sample']: sample_geo = ObjectGeometry() sample_geo.set_type(dict_exp['Sample']['Geometry']['Type']) sample.set_geometry(sample_geo) if 'Material' in dict_exp['Sample']: a, b, c = dict_exp['Sample']['Material']['Lengths'] alpha, beta, gamma = dict_exp['Sample']['Material']['Angles'] centering = dict_exp['Sample']['Material']['Centering'] symmetry = Symmetry.from_string( dict_exp['Sample']['Material']['Symmetry']) material = Lattice.from_parameters(a, b, c, alpha, beta, gamma, centering=centering, symmetry=symmetry) sample.set_material(material) if 'Microstructure' in dict_exp['Sample']: micro = Microstructure( dict_exp['Sample']['Microstructure']['Name']) for i in range(len( dict_exp['Sample']['Microstructure']['Grains'])): dict_grain = dict_exp['Sample']['Microstructure']['Grains'][i] grain = Grain( dict_grain['Id'], Orientation.from_euler( dict_grain['Orientation']['Euler Angles (degrees)'])) grain.position = np.array(dict_grain['Position']) grain.volume = dict_grain['Volume'] micro.grains.append(grain) sample.set_microstructure(micro) exp = Experiment() exp.set_sample(sample) source = XraySource() source.set_position(dict_exp['Source']['Position']) if 'Min Energy (keV)' in dict_exp['Source']: source.set_min_energy(dict_exp['Source']['Min Energy (keV)']) if 'Max Energy (keV)' in dict_exp['Source']: source.set_max_energy(dict_exp['Source']['Max Energy (keV)']) exp.set_source(source) for i in range(len(dict_exp['Detectors'])): dict_det = dict_exp['Detectors'][i] if dict_det['Class'] == 'Detector2d': det = Detector2d(size=dict_det['Size (pixels)']) det.ref_pos = dict_det['Reference Position (mm)'] if dict_det['Class'] == 'RegArrayDetector2d': det = RegArrayDetector2d(size=dict_det['Size (pixels)']) det.pixel_size = dict_det['Pixel Size (mm)'] det.ref_pos = dict_det['Reference Position (mm)'] if 'Binning' in dict_det: det.set_binning(dict_det['Binning']) det.u_dir = np.array(dict_det['u_dir']) det.v_dir = np.array(dict_det['v_dir']) det.w_dir = np.array(dict_det['w_dir']) exp.add_detector(det) return exp
def load(file_path='experiment.txt'): with open(file_path, 'r') as f: dict_exp = json.load(f) name = dict_exp['Sample']['Name'] sample = Sample(name=name) sample.data_dir = dict_exp['Sample']['Data Dir'] sample.set_position(dict_exp['Sample']['Position']) if 'Geometry' in dict_exp['Sample']: sample_geo = ObjectGeometry() sample_geo.set_type(dict_exp['Sample']['Geometry']['Type']) sample.set_geometry(sample_geo) if 'Material' in dict_exp['Sample']: a, b, c = dict_exp['Sample']['Material']['Lengths'] alpha, beta, gamma = dict_exp['Sample']['Material']['Angles'] centering = dict_exp['Sample']['Material']['Centering'] symmetry = Symmetry.from_string( dict_exp['Sample']['Material']['Symmetry']) material = Lattice.from_parameters(a, b, c, alpha, beta, gamma, centering=centering, symmetry=symmetry) sample.set_material(material) if 'Microstructure' in dict_exp['Sample']: micro = Microstructure( name=dict_exp['Sample']['Microstructure']['Name'], file_path=os.path.dirname(file_path)) # crystal lattice if 'Lattice' in dict_exp['Sample']['Microstructure']: a, b, c = dict_exp['Sample']['Microstructure']['Lattice'][ 'Lengths'] alpha, beta, gamma = dict_exp['Sample']['Microstructure'][ 'Lattice']['Angles'] centering = dict_exp['Sample']['Microstructure']['Lattice'][ 'Centering'] symmetry = Symmetry.from_string( dict_exp['Sample']['Microstructure']['Lattice'] ['Symmetry']) lattice = Lattice.from_parameters(a, b, c, alpha, beta, gamma, centering=centering, symmetry=symmetry) micro.set_lattice(lattice) grain = micro.grains.row for i in range(len( dict_exp['Sample']['Microstructure']['Grains'])): dict_grain = dict_exp['Sample']['Microstructure']['Grains'][i] grain['idnumber'] = int(dict_grain['Id']) euler = dict_grain['Orientation']['Euler Angles (degrees)'] grain['orientation'] = Orientation.from_euler(euler).rod grain['center'] = np.array(dict_grain['Position']) grain['volume'] = dict_grain['Volume'] # if 'hkl_planes' in dict_grain: # grain.hkl_planes = dict_grain['hkl_planes'] grain.append() micro.grains.flush() sample.set_microstructure(micro) sample.microstructure.autodelete = True # lazy behaviour, we load only the grain_ids path, the actual array is loaded in memory if needed sample.grain_ids_path = dict_exp['Sample']['Grain Ids Path'] exp = Experiment() exp.set_sample(sample) source = XraySource() source.set_position(dict_exp['Source']['Position']) if 'Min Energy (keV)' in dict_exp['Source']: source.set_min_energy(dict_exp['Source']['Min Energy (keV)']) if 'Max Energy (keV)' in dict_exp['Source']: source.set_max_energy(dict_exp['Source']['Max Energy (keV)']) exp.set_source(source) for i in range(len(dict_exp['Detectors'])): dict_det = dict_exp['Detectors'][i] if dict_det['Class'] == 'Detector2d': det = Detector2d(size=dict_det['Size (pixels)']) det.ref_pos = dict_det['Reference Position (mm)'] if dict_det['Class'] == 'RegArrayDetector2d': det = RegArrayDetector2d(size=dict_det['Size (pixels)']) det.pixel_size = dict_det['Pixel Size (mm)'] det.ref_pos = dict_det['Reference Position (mm)'] if 'Min Energy (keV)' in dict_exp['Detectors']: det.tilt = dict_det['Tilts (deg)'] if 'Binning' in dict_det: det.set_binning(dict_det['Binning']) det.u_dir = np.array(dict_det['u_dir']) det.v_dir = np.array(dict_det['v_dir']) det.w_dir = np.array(dict_det['w_dir']) exp.add_detector(det) return exp