def calc_model_full(func_no_const, lattice_num, layer=0.1, const=0.0, ddens=1, peakmax=5):
    """
    :param func_no_const:   function that sets the surface implicitly
    :param lattice_num:     points in one side of mesh
    :param layer:           layer width, fraction of lattice parameter
    :param const:           const for function
    :param peakmax:         max number in hkl
    :param show:            to show or not to show triangulation
    :return:
    """
    def func(x, y, z):
        return func_no_const(x, y, z, const)

    qsvec, lenqs = generate_qvecs_lenqs_deprecated(peakmax)

    cube = cube_lattice(lattice_num, func)

    factor_struct = factor_calc_structural(qsvec, cube)
    factor_mol = factor_calc_molecular(qsvec, layer, ddens)

    amplitude = factor_struct * factor_mol
    intensity = amplitude**2

    multiplicity = factor_calc_multiplicity_intens(qsvec)
    intensity *= multiplicity

    lorentz = lenqs * lenqs
    intensity *= lorentz

    return qsvec, intensity
def calc_model_set_fstruct_doublelayer(factor_structural, layer_in, ddens_in, layer_out, ddens_out, peakmax=5):

    assert layer_in <= layer_out, 'layer out should be greater or equal than layer in'
    qsvec, lenqs = generate_qvecs_lenqs_deprecated(peakmax)

    multipl = factor_calc_multiplicity_intens(qsvec)

    if len(factor_structural) != len(qsvec):
        raise ValueError("different size of struct factor and number of peaks")
#
    factor_molecular = factor_calc_molecular(qsvec, layer_in, ddens_in-ddens_out)\
                       + factor_calc_molecular(qsvec, layer_out, ddens_out)
#
    amplitude = factor_structural * factor_molecular
    intensity = amplitude**2 * np.array(multipl) * lenqs**2

    return qsvec, intensity
def calc_model_set_fstruct(factor_structural, layer, ddens, peakmax=5):
    """

    :param factor_structural:   preset structural factor for all points
    :param layer:               width of layer, fraction of lattice parameter
    :param peakmax:             max number in hkl
    :return:                    qsvec, intensity
    """
    qsvec, lenqs = generate_qvecs_lenqs_deprecated(peakmax)

    multipl = factor_calc_multiplicity_intens(qsvec)

    if len(factor_structural) != len(qsvec):
        raise ValueError("different size of struct factor and number of peaks")
#
    factor_molecular = factor_calc_molecular(qsvec, layer, ddens)
#
    amplitude = factor_structural * factor_molecular
    intensity = amplitude**2 * np.array(multipl) * lenqs**2

    return qsvec, intensity
Example #4
0
def diamond_symmetry(con):
    N = 100
    pm = 4
    tr = 0.05
    sizelat = 1
    func = double_diamond
    cubezero = double_sign_lattice(lattice_num=N, func=func, con=con, sizelat=sizelat)
    print round(cubezero.sum()/N**3,3)

    cubezero /= cubezero.sum()
    qsvec, intens_zero = straight_intensity(cubezero, peakmax=pm, sizelat=sizelat)
    _, lenqs = generate_qvecs_lenqs_deprecated(peakmax=pm)

    plt.plot(lenqs, intens_zero, 'bo')
    plt.plot(lenqs, intens_zero, '--g')
    for num, x in enumerate(qsvec):
        if intens_zero[num]>tr:
            print x, round(lenqs[num],3)
    plt.title(con)
    plt.show()
    slice = cubezero[0, :, :]
    plt.imshow(slice)
    plt.colorbar()
    plt.show()
Example #5
0
def task_surface():
    try:
        mkdir(addr)
    except:
        pass

    #parameter space generation
    qsvec, lenqs = generate_qvecs_lenqs_deprecated(peakmax=peakmax)
    const_s = np.linspace(bounds_const[0], bounds_const[-1], pnum_const)
    layer_s = np.linspace(bounds_const[0], bounds_const[-1], pnum_layer)

    #experimental data correction
    lenqs_exp_grand, intens_exp_grand = read_exp_data(scat_exp_file, intens_exp_file)

    peaks_indices = find_peaks_ind_in_smooth_profile(intens_exp_grand, cond='>')
    lenqs_exp = lenqs_exp_grand[peaks_indices]
    intens_exp = intens_exp_grand[peaks_indices]

    lattice_parameter = get_lattice_parameter(lenqs_exp_abs=lenqs_exp, peaks_file=peaks_file)

    lenqs_exp /= lattice_parameter
    lenqs_exp_grand /= lattice_parameter

    lenqs_exp = peaks_file_to_lenqs(peaks_file)

    exp_ind_in_model = find_exp_indices(lenqs_exp_short=lenqs_exp, lenqs_model=lenqs, corr_file=peaks_file) #corrction of model
    intens_exp = insert_exp_peaks(intensity_exp=intens_exp, indices_exp=exp_ind_in_model, lenqs_model=lenqs)

    #for model correction
    ind_forbid = []
    peak_max = exp_ind_in_model[-1]
    if qsvec_forbid is not None:
        ind_forbid = find_indices(qsvec_forbid, lenqs)
        print("ind forbid: "+str(ind_forbid))

    #for output xaxis
    ind_exp = np.nonzero(intens_exp)[0]
    xaxis = [round(lenqs[ind_exp[0]])-1, round(lenqs[ind_exp[-1]])+1]

    #init
    err_map = np.zeros((pnum_const, pnum_layer))
    lenqs = qsvec_to_lenqs(qsvec)

    #brute_force
    for num_const, val_const in enumerate(const_s):
        func_p = lambda x, y, z: double_diamond(x, y, z, const=val_const)
        cube_p = cube_lattice(lattice_num, func_p)
        func_n = lambda x, y, z: -double_diamond(x, y, z, const=-val_const)
        cube_n = cube_lattice(lattice_num, func_n)

        factor_struct = factor_calc_structural(qsvec, cube_p*cube_n)
        for num_layer, val_layer in enumerate(layer_s):
            temp_intens_exp = deepcopy(intens_exp)
            qsvec_new, intens_mod = calc_model_set_fstruct(factor_struct, val_layer, ddens=1., peakmax=peakmax)

            assert (qsvec_new == qsvec).prod(), "Something went wrong with qsvec's "
            err = calculate_error(lenqs, intens_mod, lenqs, temp_intens_exp, norm='area')

            err_map[num_const, num_layer] = err
            if err<errtr:
                assert (temp_intens_exp/max(temp_intens_exp)).all() == (intens_exp/max(intens_exp)).all(),"intens was changed during err calc"
                name_cur = "p1(%1.2f) p2(%1.2f) " % (val_const, val_layer)
                output(name=name_cur,pic_path=addr, lenqs=lenqs, intens_exp=temp_intens_exp, intens_mod=intens_mod,
                   error=err, lenqs_exp_grand=lenqs_exp_grand, intens_exp_grand=intens_exp_grand, xaxis=xaxis)

    np.save(addr+'/'+model_name+"results.npy", err_map)
    parameters = np.array([const_s, layer_s])
    np.save(addr+'/'+model_name+"params.npy", parameters)
Example #6
0
def diamond_paper_res():
    peakmax = 6
    qsvec, lenqs = generate_qvecs_lenqs_deprecated(peakmax)
    func_p = lambda x, y, z: double_diamond(x, y, z, const=0)
    gart_list = np.array([0.]*len(qsvec))#len(qsvec))

    """
    #gyr
    gart_list[5] = 0.65
    gart_list[6] = 0.43
    gart_list[12] = 0.097
    gart_list[13] = 0.34
    gart_list[17] = 0.33
    gart_list[19] = 0.47
    gart_list[20] = 0.27
    gart_list[22] = 0.189
    gart_list[30] = 0.41
    """
    for num, q in enumerate(qsvec):
        print num, q
    #dd

    gart_list[6] = 0.958
    gart_list[10] = 0.97
    gart_list[13] = 0.523
    gart_list[20] = 0.424
    gart_list[26] = 0.516
    #gart_list[30] = 0.56
    qsvec_new, intens_paper = calc_model_set_fstruct(gart_list, 0.05, ddens=1., peakmax=peakmax)
    intens_paper /= (intens_paper*lenqs*lenqs).sum()

    sizelat = 1.

    intenses = []
    Ns = [30, 110]
    for N in Ns:
        #print N
        cube_p = cube_lattice(N, func_p, sizelat=sizelat)
        qsvec, lenqs = generate_qvecs_lenqs_deprecated(peakmax)

        factor_struct = factor_calc_structural(qsvec, cube_p)
        #print factor_struct

        qsvec_new, intens_mod = calc_model_set_fstruct(factor_struct, layer=0.05, ddens=1., peakmax=peakmax)
        print max(intens_mod)
        intens_mod /= (intens_mod*lenqs*lenqs).sum()
        print max(intens_mod)
        print
        intenses.append(intens_mod)
        #plt.plot(lenqs, intens_mod,'o',label='m surface %d'%(N))

    plt.plot(lenqs, intens_paper, '^', label='external data')
    plt.title("DD")



    cube_n = cube_lattice(80, func_p, sizelat=sizelat)

    for err in [0.20]:
        cube_p = (cube_n<0)#err
        qsvec, intens_dir = straight_intensity(lattice=cube_p, peakmax=peakmax,sizelat=sizelat)
        intens_dir /= (intens_dir*lenqs*lenqs).sum()
        print ((intens_dir - intens_paper)**2).sum()
        plt.plot(lenqs, intens_dir,"s", label="m direct {:1.3f}".format((cube_p>0).astype(float).sum()/80**3))

        cube_p = (np.abs(cube_n)<err)
        qsvec, intens_dir = straight_intensity(lattice=cube_p, peakmax=peakmax, sizelat=sizelat)
        intens_dir /= (intens_dir*lenqs*lenqs).sum()
        print ((intens_dir - intens_paper)**2).sum()
        plt.plot(lenqs, intens_dir,"s", label="m direct surf {:1.3f}, {:1.3f}".format((cube_p>0).astype(float).sum()/80**3, err))


    plt.legend(bbox_to_anchor=(0.8, 1.1))


        #up = np.sqrt(((intens_paper-intens_mod)**2).sum())
        #down1 = np.sqrt((intens_paper**2).sum())
        #down2 = np.sqrt((intens_mod**2).sum())
        #print down1, down2
        #print up/down1, up/down2

        #plt.show()

        #diff = intens_paper/intens_mod
        #diff = diff[np.nonzero(diff)]



    plt.show()
    plt.imshow(cube_p[0,:,:])
    plt.show()
    np.save("../data/dd_model_surf.npy", intenses[0])