Exemple #1
0
def surface_check():
    func = double_diamond
    cube = cube_lattice(lattice_num=70, flambda=func)
    delt = 0.1
    cube_d = (np.abs(cube)<delt).astype(float)
    plt.imshow(cube_d[:,:,-1])
    plt.colorbar()
    plt.show()
    qsvec, intens_str = straight_intensity(cube_d, peakmax=5)
    lenqs = qsvec_to_lenqs(qsvec)
    intens_str /= (intens_str*lenqs*lenqs).sum()
    intens_mod = np.load("../data/dd_model_surf.npy")
    intens_paper = np.load("../data/dd_paper_surf.npy")
    plt.title("DD")
    plt.plot(lenqs,intens_mod,'bo',label="surface appr")
    plt.plot(lenqs, intens_str,'^g', label="direct surface")
    plt.plot(lenqs, intens_paper,'r+',label="paper data")

    #for x in range(len(qsvec)):
     #   print x, qsvec[x]

    cube_b = ((cube)<0).astype(float)
    qsvec, intens_bulk = straight_intensity(cube_b, peakmax=5)
    intens_bulk /= (intens_bulk*lenqs*lenqs).sum()
    plt.plot(lenqs, intens_bulk,'xk', label="direct bulk")
    plt.legend()
    plt.show()

    plt.imshow(cube_b[:,:,-1])
    plt.colorbar()
    plt.show()
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 build_nested_lattice_gyroid(lattice_num, con1, con2, dens_in_slash_out=1, flag_apart=False):
    """
    :param con1: less constant, bigger gyr
    :param con2: bigger constant, smaller gyr
    :param dens_in_slash_out: density inside smaller gyr/density outside smaller but inside bigger gyr
    :param lattice_num: size of one side
    :param flag_apart: if True, returns (cube with inner gyr, cube with outer gyr)
    :return: density lattice
    """
    if con1 > con2:
        C_max = con1
    else:
        C_max = con2
    C_min = con1 + con2 - C_max

    # positive
    f_cube = lambda x, y, z: gyroid(x, y, z, C_min)
    cube_bigger_p = (cube_lattice(lattice_num=lattice_num, flambda=f_cube) < 0).astype(float)

    f_cube = lambda x, y, z: gyroid(x, y, z, C_max)
    cube_smaller_p = ((cube_lattice(lattice_num=lattice_num, flambda=f_cube)) < 0).astype(float)

    cube_positive = cube_smaller_p * dens_in_slash_out
    cube_positive += np.logical_xor(cube_bigger_p.astype(bool), cube_smaller_p.astype(bool)).astype(float)

    # negative
    f_cube = lambda x, y, z: gyroid(x, y, z, -C_min)
    cube_bigger_n = (cube_lattice(lattice_num=lattice_num, flambda=f_cube) > 0).astype(float)

    f_cube = lambda x, y, z: gyroid(x, y, z, -C_max)
    cube_smaller_n = ((cube_lattice(lattice_num=lattice_num, flambda=f_cube)) > 0).astype(float)

    cube_negative = cube_smaller_n * dens_in_slash_out
    cube_negative += np.logical_xor(cube_bigger_n.astype(bool), cube_smaller_n.astype(bool)).astype(float)

    if flag_apart:
        return cube_smaller_n + cube_smaller_p, cube_bigger_n + cube_bigger_p
    cube = cube_positive + cube_negative
    """
    plt.imshow(cube[0])
    plt.colorbar()
    plt.show()
    """
    return cube
def single_sign_lattice(lattice_num, func, con):
    """
    returns cube with double structure, +-C
    :param lattice_num: size of one side
    :param con: constant of structure
    :param func: function(x, y, z), when < 0 defines structure
    :return: lattice
    """
    f_cube_positive = lambda x, y, z: func(x, y, z, con)
    cube = cube_lattice(lattice_num=lattice_num, flambda=f_cube_positive) < 0
    cube = cube.astype(float)
    return cube
def double_sign_lattice(lattice_num, func, con, warn=True, sizelat=1.0):
    """
    returns cube with double structure, +-C
    :param lattice_num: size of one side
    :param con: constant of structure
    :param func: function(x, y, z), when < 0 defines structure
    :return: lattice
    """
    f_cube_positive = lambda x, y, z: func(x, y, z, con)
    cube_pos = cube_lattice(lattice_num=lattice_num, flambda=f_cube_positive, sizelat=sizelat) < 0
    cube_pos = cube_pos.astype(float)

    f_cube_negative = lambda x, y, z: -func(x, y, z, -con)
    cube_neg = cube_lattice(lattice_num=lattice_num, flambda=f_cube_negative, sizelat=sizelat) < 0
    cube_neg = cube_neg.astype(float)
    if cube_neg.sum() != cube_pos.sum() and warn:
        mes = "Positive and negative lattices are different: {}, {} ".format(
            cube_neg.sum() / lattice_num ** 3, cube_pos.sum() / lattice_num ** 3
        )
        warnings.warn(mes, RuntimeWarning)
    cube = cube_neg + cube_pos
    return cube
Exemple #6
0
def build_spheres(filname, radi=0.05, numb=50, flagshow=False):
    coords = take_coord(filname)
    if flagshow:
        show_coord(coords)
    rsphere = lambda x,y,z,x0,y0,z0: _sphere( x, y, z, x0, y0, z0, r=radi)
    cube = np.zeros((numb, numb, numb)) + 1.
    print(cube.sum()/numb**3)
    for point in coords:
        p=point
        f = lambda x, y, z: rsphere(x,y,z,x0=p[0], y0=p[1], z0=p[2])
        cube *= cube_lattice(numb, f).astype(float)

    cube -= 1
    cube *= -1
    print (cube.sum()/numb**3)
    plt.imshow(cube[0])
    plt.colorbar()
    plt.show()
    q, i = fourier_base(cube, dimension=1)
    plt.plot(q, i)
    plt.show()
Exemple #7
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)
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])
Exemple #9
0
def research_diamond():
    interp_func = double_diamond
    save_interpolate = False
    const_pnum = 100
    N = 50
    fit_degree = 6
    bounds_const = (0.7, 1.4)
    const_s = np.linspace(bounds_const[0], bounds_const[1], const_pnum)
    phi_s = np.zeros((len(const_s)))
    phi = lambda cube: float((cube>0).sum())/N**3

    """
    for num, con in enumerate(const_s):
        ff = lambda x,y,z: double_diamond(x,y,z, const=con)
        cube = cube_lattice(flambda=ff, lattice_num=N)
        ff = lambda x,y,z: -double_diamond(x,y,z, const=-con)
        cube2 = cube_lattice(flambda=ff, lattice_num=N)
        triangulate_and_show_cube(cube)
        triangulate_and_show_cube(cube2, color='r')
        cube *= cube2
        triangulate_and_show_cube(cube, color='g')
    """
    n = 61
    c = cube_lattice(lattice_num=n, flambda=interp_func)
    c = (c>0).astype(float)
    print c.sum()*2,n**3
    for num, con in enumerate(const_s):
        cube = double_sign_lattice(lattice_num=N, func=interp_func, con=con, warn=False)
        print con, cube.sum()/N**3
        if num%10==0:
            pass
            #triangulate_and_show_cube(cube)
        phi_s[num] = phi(cube)
    interp = np.polyfit(phi_s, const_s, fit_degree)

    def res_inter(x):
        res = 0
        N = len(interp)
        extents = range(N)
        for num in extents:
            res += interp[num]*x**(N-1-num)
        return res

    interp_const = res_inter(phi_s)
    interp = np.around(interp, decimals=3)
    plt.plot(phi_s, const_s, 'xr', label="data")
    plt.plot(phi_s, interp_const,'-g', label="interpolation")
    plt.xlabel("$\phi$")
    plt.ylabel("C(const)")
    name = "diamond half C="
    N = len(interp)
    extents = range(N)
    countsize = len(name)
    threshold = 40
    for num in extents:
        if num!=0:
            temp = "%+.2f"%(interp[num])
        else:
            temp = "%.2f"%(interp[num])
        if (N-1-num)!=0:
            temp+=("${\phi}^{%d}$" % (N-1-num))
        name += temp
        countsize += len(temp)
        if countsize>threshold:
            countsize = 0
            name +='\n'
    name += '\n'+'error:'+"%.3g"%(((interp_const-const_s)**2).sum()/(const_s**2).sum())

    phi_s_red = phi_s[phi_s<0.2]
    plt.plot(phi_s, phi_to_const_double_double_diam(phi_s), '--b', label="function")

    plt.title(name)
    plt.legend(loc=3)
    plt.tight_layout()
    cube_test = single_sign_lattice(lattice_num=N, func=_diamond, con=1.087)
    print (cube_test.sum())/N**3
    plt.show()
    if save_interpolate:
        f = open("inter_res.py", 'w')