Пример #1
0
def write_ini(param, hkl):
    """
    Write ini file for grainspotter, see
    http://fable.wiki.sourceforge.net/grainspotter
    
    Henning Osholm Sorensen, RisoeDTU, 2008.
     """

    for phase in param['phase_list']:
        out = '! input file for GrainSpotter made by PolyXSim\n'

        if param['no_phases'] > 1:
            filename = '%s/%s_phase_%i.ini' % (param['direc'], param['stem'],
                                               phase)
            out = out + 'filespecs %s/%s_phase_%i.gve %s/%s_phase_%i.log\n' % (
                param['direc'], param['stem'], phase, param['direc'],
                param['stem'], phase)
        else:
            filename = '%s/%s.ini' % (param['direc'], param['stem'])
            out = out + 'filespecs %s/%s.gve %s/%s.log\n' % (
                param['direc'], param['stem'], param['direc'], param['stem'])

        unit_cell = param['unit_cell_phase_%i' % phase]

        thkl = hkl[param['phase_list'].index(phase)].copy()
        ds = n.zeros((thkl.shape[0], 1))

        for i in range(thkl.shape[0]):
            ds[i] = 2 * tools.sintl(unit_cell, thkl[i, 0:3])

        ds.sort()
        ds = ds.round(9)
        ds = n.unique(ds)
        families = len(ds)
        Nhkls = n.min([families, 8])
        extra_hkls = 4
        if families - Nhkls < extra_hkls:
            tth_max = 2 * param['theta_max']
        else:
            ds_max = ds[Nhkls + extra_hkls - 1] + 0.001
            tth_max = 2 * n.arcsin(
                ds_max * param['wavelength'] / 2.) * 180. / n.pi

        ds_min = ds[0] - 0.001
        if ds_min < 0.0:
            ds_min = 0.
        tth_min = 2 * n.arcsin(ds_min * param['wavelength'] / 2.) * 180. / n.pi

        out = out + 'spacegroup %i\n' % param['sgno_phase_%i' % phase]
        out = out + 'etarange %f %f\n' % (0.0, 360.0)
        out = out + 'domega %f\n' % param['omega_step']
        out = out + 'omegarange %f %f\n' % (param['omega_start'],
                                            param['omega_end'])
        out = out + 'cuts %i %f %f\n' % (8, 0.6, 0.75)
        out = out + 'eulerstep %f\n' % (5.0)
        out = out + 'uncertainties %f %f %f\n' % (.05, 0.5, 1.0)
        out = out + 'nsigmas %f\n' % (2.0)
        out = out + 'Nhkls_in_indexing %i\n' % (Nhkls)
        out = out + 'tthrange %f %f\n' % (tth_min, tth_max)
        out = out + 'minfracg %f\n' % (0.95)

        f = open(filename, 'w')
        f.write(out)
Пример #2
0
def StructureFactor(hkl, ucell, sgname, atoms, disper=None):
    """
    Calculation of the structure factor of reflection hkl
    
    [Freal Fimg] = StructureFactor(hkl,unit_cell,sg,atoms)
    
    INPUT : hkl =       [h, k, l] 
            unit_cell = [a, b, c, alpha, beta, gamma] 
            sgname:     space group name (e.g. 'P 21/c')
            atoms:      structural parameters (as an object)
    OUTPUT: The real and imaginary parts of the the structure factor
    
    Henning Osholm Sorensen, June 23, 2006.
    Translated to python code April 8, 2008
    """
    mysg = sg.sg(sgname=sgname)
    stl = tools.sintl(ucell, hkl)
    noatoms = len(atoms)

    Freal = 0.0
    Fimg = 0.0

    for i in range(noatoms):
        #Check whether isotrop or anisotropic displacements
        if atoms[i].adp_type == 'Uiso':
            U = atoms[i].adp
            expij = n.exp(-8 * n.pi**2 * U * stl**2)
        elif atoms[i].adp_type == 'Uani':
            # transform Uij to betaij
            betaij = Uij2betaij(atoms[i].adp, ucell)
        else:
            expij = 1
            atoms[i].adp = 'Uiso'
            #logging.error("wrong no of elements in atomlist")

        # Atomic form factors
        f = FormFactor(atoms[i].atomtype, stl)
        if disper == None or disper[atoms[i].atomtype] == None:
            fp = 0.0
            fpp = 0.0
        else:
            fp = disper[atoms[i].atomtype][0]
            fpp = disper[atoms[i].atomtype][1]

        for j in range(mysg.nsymop):
            # atomic displacement factor
            if atoms[i].adp_type == 'Uani':
                betaijrot = n.dot(mysg.rot[j], n.dot(betaij, mysg.rot[j]))
                expij = n.exp(-n.dot(hkl, n.dot(betaijrot, hkl)))

            # exponent for phase factor
            r = n.dot(mysg.rot[j], atoms[i].pos) + mysg.trans[j]
            exponent = 2 * n.pi * n.dot(hkl, r)

            #forming the real and imaginary parts of F
            s = n.sin(exponent)
            c = n.cos(exponent)
            site_pop = atoms[i].occ * atoms[i].symmulti / mysg.nsymop
            Freal = Freal + expij * (c * (f + fp) - s * fpp) * site_pop
            Fimg = Fimg + expij * (s * (f + fp) + c * fpp) * site_pop

    return [Freal, Fimg]
Пример #3
0
def write_gve(param, grain, hkl):
    """
    Write gvector (gve) file, for format see
    http://fable.wiki.sourceforge.net/imaged11+-+file+formats
    
    Henning Osholm Sorensen, RisoeDTU, 2008.
    python translation: Jette Oddershede, Risoe DTU, March 31 2008
    changed October 1, 2008 to new .gve format including detector.par and [xl,yl,zl]
    """

    A = grain[0].refs
    for grainno in range(1, param['no_grains']):
        A = n.concatenate((A, grain[grainno].refs))

    nrefl = A.shape[0]

    # from detector.par
    (z_center, y_center) = detector.detyz_to_xy(
        [param['dety_center'], param['detz_center']], param['o11'],
        param['o12'], param['o21'], param['o22'], param['dety_size'],
        param['detz_size'])
    dout = "# chi 0.0\n"
    dout = dout + "# distance %f\n" % (param['distance'] * 1000.)
    dout = dout + "# fit_tolerance 0.5\n"
    dout = dout + "# o11 %i\n" % param['o11']
    dout = dout + "# o12 %i\n" % param['o12']
    dout = dout + "# o21 %i\n" % param['o21']
    dout = dout + "# o22 %i\n" % param['o22']
    dout = dout + "# omegasign %f\n" % param['omega_sign']
    dout = dout + "# t_x 0\n"
    dout = dout + "# t_y 0\n"
    dout = dout + "# t_z 0\n"
    dout = dout + "# tilt_x %f\n" % param['tilt_x']
    dout = dout + "# tilt_y %f\n" % param['tilt_y']
    dout = dout + "# tilt_z %f\n" % param['tilt_z']
    dout = dout + "# y_center %f\n" % y_center
    dout = dout + "# y_size %f\n" % (param['y_size'] * 1000.)
    dout = dout + "# z_center %f\n" % z_center
    dout = dout + "# z_size %f\n" % (param['z_size'] * 1000.)

    for phase in param['phase_list']:
        if param['no_phases'] > 1:
            filename = '%s/%s_phase_%i.gve' % (param['direc'], param['stem'],
                                               phase)
        else:
            filename = '%s/%s.gve' % (param['direc'], param['stem'])
        f = open(filename, 'w')
        lattice = param['sgname_phase_%i' % phase][0]
        format = "%f " * 6 + "%s " * 1 + "\n"
        unit_cell = param['unit_cell_phase_%i' % phase]
        out = format % (unit_cell[0], unit_cell[1], unit_cell[2], unit_cell[3],
                        unit_cell[4], unit_cell[5], lattice)
        out = out + "# wavelength = %s\n" % (param['wavelength'])
        out = out + "# wedge = %f\n" % (param['wedge'])
        out = out + "# axis = 0.000 0.0000 1.0000\n"
        # insert detector.par as comment
        out = out + "# cell__a %s\n" % unit_cell[0]
        out = out + "# cell__b %s\n" % unit_cell[1]
        out = out + "# cell__c %s\n" % unit_cell[2]
        out = out + "# cell_alpha %s\n" % unit_cell[3]
        out = out + "# cell_beta %s\n" % unit_cell[4]
        out = out + "# cell_gamma %s\n" % unit_cell[5]
        out = out + "# cell_lattice_[P,A,B,C,I,F,R] %s\n" % param[
            'sgname_phase_%i' % phase][0]
        out = out + dout
        # continue with gve format
        out = out + "# ds h k l\n"
        f.write(out)

        thkl = hkl[param['phase_list'].index(phase)].copy()
        ds = n.zeros((thkl.shape[0], 1))

        for i in range(thkl.shape[0]):
            ds[i] = 2 * tools.sintl(unit_cell, thkl[i, 0:3])

        #Add ds values to the thkl array
        thkl = n.concatenate((thkl, ds), 1)

        # sort rows according to ds, descending
        thkl = thkl[n.argsort(thkl, 0)[:, 4], :]

        # Output format
        format = "%f " * 1 + "%d " * 3 + "\n"

        for i in range(thkl.shape[0]):
            out = format % (thkl[i, 4], thkl[i, 0], thkl[i, 1], thkl[i, 2])
            f.write(out)

        R_tilt = tools.detect_tilt(param['tilt_x'], param['tilt_y'],
                                   param['tilt_z'])

        out = "# xr yr zr xc yc ds eta omega spot3d_id xl yl zl\n"
        f.write(out)
        format = "%f " * 8 + "%i " * 1 + "%f " * 3 + "\n"

        for i in range(nrefl):
            (sc, fc) = detector.detyz_to_xy(
                [A[i, A_id['dety']], A[i, A_id['detz']]], param['o11'],
                param['o12'], param['o21'], param['o22'], param['dety_size'],
                param['detz_size'])
            [xl, yl,
             zl] = detector.detector_to_lab(A[i, A_id['dety']],
                                            A[i,
                                              A_id['detz']], param['distance'],
                                            param['y_size'], param['z_size'],
                                            param['dety_center'],
                                            param['detz_center'], R_tilt)
            out = format % (
                A[i, A_id['gv1']] / (2 * n.pi),
                A[i, A_id['gv2']] / (2 * n.pi),
                A[i, A_id['gv3']] / (2 * n.pi),
                sc,  #A[i,A_id['detz']],
                fc,  #param['dety_size']-A[i,A_id['dety']],
                (2 * n.sin(.5 * A[i, A_id['tth']]) / param['wavelength']),
                A[i, A_id['eta']] * 180 / n.pi,
                A[i, A_id['omega']] * 180 / n.pi,
                A[i, A_id['spot_id']],
                xl * 1000.,
                yl * 1000.,
                zl * 1000.)
            f.write(out)

        f.close()
Пример #4
0
def write_delta_gve(param,voxel,hkl):
    if not memory_safety_check(param):
        return
    """
    Write gvector (gve) file, for format see
    http://fable.wiki.sourceforge.net/imaged11+-+file+formats

    python translation: March 31 2008
    changed October 1, 2008 to new .gve format including detector.par and [xl,yl,zl]
    """


    # from detector.par
    (z_center, y_center) = detector.detyz_to_xy([param['dety_center'],param['detz_center']],
                            param['o11'],
                            param['o12'],
                            param['o21'],
                            param['o22'],
                            param['dety_size'],
                            param['detz_size'])
    dout = "# chi 0.0\n"
    dout = dout + "# distance %f\n" %(param['distance']*1000.)
    dout = dout + "# fit_tolerance 0.5\n"
    dout = dout + "# o11 %i\n" %param['o11']
    dout = dout + "# o12 %i\n" %param['o12']
    dout = dout + "# o21 %i\n" %param['o21']
    dout = dout + "# o22 %i\n" %param['o22']
    dout = dout + "# omegasign %f\n" %param['omega_sign']
    dout = dout + "# t_x 0\n"
    dout = dout + "# t_y 0\n"
    dout = dout + "# t_z 0\n"
    dout = dout + "# tilt_x %f\n" %param['tilt_x']
    dout = dout + "# tilt_y %f\n" %param['tilt_y']
    dout = dout + "# tilt_z %f\n" %param['tilt_z']
    dout = dout + "# y_center %f\n" %y_center
    dout = dout + "# y_size %f\n" %(param['y_size']*1000.)
    dout = dout + "# z_center %f\n" %z_center
    dout = dout + "# z_size %f\n" %(param['z_size']*1000.)

    for phase in param['phase_list']:
        if param['no_phases'] > 1:
            filename = '%s/%s%s_phase_%i.gve' %(param['direc'],'delta_peaks_',param['stem'],phase)
        else:
            filename = '%s/%s%s.gve' %(param['direc'],'delta_peaks_',param['stem'])

        with open(filename,'w') as f:
            lattice = param['sgname_phase_%i' %phase][0]
            format = "%f "*6 + "%s "*1 +"\n"
            unit_cell = param['unit_cell_phase_%i' %phase]
            out = format %(unit_cell[0],unit_cell[1],unit_cell[2],
                        unit_cell[3],unit_cell[4],unit_cell[5],
                        lattice)
            out = out + "# wavelength = %s\n" %(param['wavelength'])
            out = out + "# wedge = %f\n" %(param['wedge'])
            out = out + "# axis = 0.000 0.0000 1.0000\n"
            # insert detector.par as comment
            out = out + "# cell__a %s\n" %unit_cell[0]
            out = out + "# cell__b %s\n" %unit_cell[1]
            out = out + "# cell__c %s\n" %unit_cell[2]
            out = out + "# cell_alpha %s\n" %unit_cell[3]
            out = out + "# cell_beta %s\n" %unit_cell[4]
            out = out + "# cell_gamma %s\n" %unit_cell[5]
            out = out + "# cell_lattice_[P,A,B,C,I,F,R] %s\n" %param['sgname_phase_%i' %phase][0]
            out = out + dout
            # continue with gve format
            out = out +"# ds h k l\n"
            f.write(out)

            thkl = hkl[param['phase_list'].index(phase)].copy()
            ds = n.zeros((thkl.shape[0],1))

            for i in range(thkl.shape[0]):
                ds[i] = 2*tools.sintl(unit_cell,thkl[i,0:3])

            #Add ds values to the thkl array
            thkl = n.concatenate((thkl,ds),1)

            # sort rows according to ds, descending
            thkl = thkl[n.argsort(thkl,0)[:,4],:]

            # Output format
            format = "%f "*1 + "%d "*3 +"\n"

            for i in range(thkl.shape[0]):
                out = format %(thkl[i,4],
                            thkl[i,0],
                            thkl[i,1],
                            thkl[i,2]
                            )
                f.write(out)

            R_tilt = tools.detect_tilt(param['tilt_x'],param['tilt_y'],param['tilt_z'])

            out = "# xr yr zr xc yc ds eta omega spot3d_id xl yl zl\n"
            f.write(out)

            # TODO: if we are writing huge files we need to be a little
            # bit tender with the ram usage. Which is still kinda stupid
            # as whoever tries to read such a file to RAM will get a
            # nasty suprise. Perhaps a better solution is to write several .flt files
            # Anyway, this should not be a problem once peakmerging is implemented

            if param['no_voxels']>100:
                collected_voxels = 0
                ranges = []
                while( True ):
                    low_line = collected_voxels
                    top_line = collected_voxels + 100
                    if top_line < param['no_voxels']:
                        ranges.append([low_line,top_line])
                    else:
                        ranges.append([low_line,param['no_voxels']])
                        break
                    collected_voxels = top_line
            else:
                ranges = [[0,param['no_voxels']]]


            #Local implementation of detecter.detyz_to_xy() for speed
            #--------------------------------------------------------
            if abs(param['o11']) == 1:
                if (abs(param['o22']) != 1) or (param['o12'] != 0) or (param['o21'] != 0):
                    raise ValueError('detector orientation makes no sense 1')
            elif abs(param['o12']) == 1:
                if abs(param['o21']) != 1 or (param['o11'] != 0) or (param['o22'] != 0):
                    raise ValueError('detector orientation makes no sense 2')
            else:
                raise ValueError('detector orientation makes no sense 3')

            omat = n.array([[param['o11'], param['o12']],
                            [param['o21'], param['o22']]])
            omat = n.linalg.inv(omat)
            det_size = n.array([param['detz_size']-1,
                                param['dety_size']-1])
            term2 = n.clip(n.dot(omat, det_size),-n.max(det_size), 0)
            def detyz_to_xy(coor,omat,det_size,term2):
                coor = n.array([coor[1], coor[0]])
                coor = n.dot(omat, coor) - term2
                return coor
            #-------------------------------------------------------------

            format = "%f "*8 + "%i "*1+ "%f "*3+"\n"
            nrefl = 0
            for ran in ranges:
                out=""
                A = voxel[ ran[0] ].refs
                for voxelno in range(ran[0]+1,ran[1]):
                        A = n.concatenate((A,voxel[voxelno].refs))
                for i in range(A.shape[0]):
                    (sc, fc) = detector.detyz_to_xy([A[i,A_id['dety']],A[i,A_id['detz']]],
                                                param['o11'],
                                                param['o12'],
                                                param['o21'],
                                                param['o22'],
                                                param['dety_size'],
                                                param['detz_size'])

                    [xl,yl,zl] = detector.detector_to_lab(A[i,A_id['dety']],A[i,A_id['detz']],
                                                        param['distance'],
                                                        param['y_size'],param['z_size'],
                                                        param['dety_center'],param['detz_center'],
                                                    R_tilt)
                    out = out + (format %(A[i,A_id['gv1']]/(2*n.pi),
                                A[i,A_id['gv2']]/(2*n.pi),
                                A[i,A_id['gv3']]/(2*n.pi),
                                sc,#A[i,A_id['detz']],
                                fc,#param['dety_size']-A[i,A_id['dety']],
                                (2*n.sin(.5*A[i,A_id['tth']])/param['wavelength']),
                                A[i,A_id['eta']]*180/n.pi,
                                A[i,A_id['omega']]*180/n.pi,
                                A[i,A_id['spot_id']],
                                xl*1000.,
                                yl*1000.,
                                zl*1000.
                                ) )

                f.write(out)
                nrefl = nrefl + A.shape[0]
                print('\rFinished %3i of %3i voxels. Written %3i reflections to file' %(ran[1],param['no_voxels'],nrefl), end=' ')
                sys.stdout.flush()
            print("\n")