Esempio n. 1
0
def test_by_sin(npoints=100):
    s = lambda x, y, z: np.sin(2*np.pi*4*r(x,y,z))
    sizelat = 4
    l = sizelat/2
    r = lambda x, y, z: np.sqrt((x-l)**2 + (y-l)**2 + (z-l)**2)

    cube = cube_lattice(lattice_num=npoints, flambda=s, sizelat=sizelat)
    plt.imshow(cube[npoints/2, :,:])
    plt.colorbar()
    plt.show()
    cube -= np.average(cube)

    peakmax = 10.
    lenqs2, intens_mod = straight_intensity(cube, peakmax=peakmax, sizelat=sizelat)
    intens_mod /= lenqs2**2
    plt.plot(lenqs2, intens_mod,'--b')
    plt.plot(lenqs2, intens_mod,'bo')

    plt.title("ФП в точках, ячеек:{}".format(sizelat))
    plt.ylabel("I, a.u.")
    plt.xlabel("q, a.u.")
    plt.show()
    output(name="test3", pic_path=".", lenqs=lenqs2, intens_exp=intens_mod,
           intens_mod=intens_mod, error=0., xaxis=np.array([0.,9.]))

    from Fourier import fourier_base
    lenqs_f, intens_f = fourier_base(cube)
    lenqs_f /= sizelat
    plt.plot(lenqs_f, intens_f/np.max(intens_f),'-r')
    plt.title("Полное ФП, ячеек:{}".format(sizelat))

    plt.ylabel("I, a.u.")
    plt.xlabel("q, a.u.")
    plt.show()
Esempio n. 2
0
def task_direct():
    try:
        mkdir(addr)
    except:
        pass
    global rho_abs_h20, rho_abs_pol, rho_abs_alk

    #parameter space generation
    qsvec, lenqs = generate_qvecs_lenqs(peakmax=lenqsmax)
    phi_s = np.linspace(bounds_phi[0], bounds_phi[-1], phi_pnum)
    phi_delta_s = np.linspace(bounds_phi_delta[0], bounds_phi_delta[-1], phi_delta_pnum)
    dens_s = np.linspace(bounds_dense[0], bounds_dense[-1], dens_pnum)
    debye_s = np.linspace(bounds_debye[0], bounds_debye[-1], debye_pnum)

    #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)
    print("lattice parameter: {:1.3f}".format(1./lattice_parameter))
    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]
    print("inices higher than {} ({}) are forbidden".format(peak_max, qsvec[peak_max]))
    if qsvec_forbid is not None:
        ind_forbid = find_indices(qsvec_forbid, lenqs)
        print("hkl forbid: {}, ind forbid{}:".format(str(qsvec_forbid), 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((phi_pnum, phi_delta_pnum, dens_pnum, debye_pnum))
    lenqs = qsvec_to_lenqs(qsvec)
    #brute force
    for num_phi in range(phi_pnum):
        phi1 = phi_s[num_phi]
        con1 = phi_to_const(phi1)
        cube_in = build_lattice_function(lattice_num=lattice_num, func=model_function, con=con1, warn=global_warn)
        #print("phase th:{:1.3f}, phase_real:{:1.3f}, const:{:1.3f}".format(phi1, (cube_in.sum()/lattice_num**3),con1))

        for num_phi_delta in range(phi_delta_pnum):
            phi2 = phi_s[num_phi] + phi_delta_s[num_phi_delta]
            con2 = phi_to_const(phi2)
            if phi2 < 0.03:
                print("low phi, break")
            cube_out = build_lattice_function(lattice_num=lattice_num, func=model_function, con=con2, warn=global_warn)
            #plt.imshow(((1.-cube_out) + 2*(cube_out - cube_in) + cube_in*3)[0,:,:])
            #plt.colorbar()
            #plt.savefig(addr+"/{:1.2f}_{:1.2f}.png".format(phi1, phi2))
            #plt.clf()
            for num_dens in range(dens_pnum):
                if varied_phase == 'h20':
                    rho_abs_h20 = dens_s[num_dens]
                elif varied_phase == 'pol':
                    rho_abs_pol = dens_s[num_dens]
                else:
                    rho_abs_alk=dens_s[num_dens]

                cube = (1.-cube_out)*rho_abs_alk
                cube += (cube_out - cube_in)*rho_abs_pol
                cube += cube_in*rho_abs_h20
                cube -= cube.sum()/lattice_num**3
                #cube = cube_out + (dens_s[num_dens] - 1)*cube_in
                lenqs2, intens_mod = get_intensity(cube)#straight_intensity(cube, peakmax=lenqsmax, sizelat=sizelat)
                #lenqs2 = qsvec_to_lenqs(qsvec)
                assert lenqs.all()==lenqs2.all(),'trouble!'
                intens_mod[peak_max+1::] = 0.0
                intens_mod[ind_forbid] = 0.0

                for num_debye, val_debye in enumerate(debye_s):
                    temp_intens_exp = deepcopy(intens_exp)
                    debye_factor = np.exp(-(lenqs*val_debye)**2)
                    temp_intens_mod = intens_mod * debye_factor

                    err = calculate_error(lenqs, temp_intens_mod, lenqs, temp_intens_exp, norm='area')
                    err_map[num_phi, num_phi_delta, num_dens, num_debye] = 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({:0.2f}) p2({:0.2f}) dn({:1.2f}) db({:0.2f})m({})".format(phi1, phi2, dens_s[num_dens], val_debye, intens_type)
                        print(name_cur,(rho_abs_h20, rho_abs_pol, rho_abs_alk), err)
                        if intens_type == "F":
                            cached_lenqs, cached_intens = get_intensity(cube, getall=True)
                            cached_intens *= np.exp(-(cached_lenqs*val_debye)**2)
                            cached_intens /= np.max(cached_intens)
                            cached_intens *= np.max(temp_intens_mod)
                            plt.plot(cached_lenqs, cached_intens,'-go')
                            plt.axis([1,xaxis[1],0., np.max(cached_intens)*1.1])
                            plt.savefig(addr+'/'+name_cur+"_full.png")
                            plt.clf()

                        output(name=name_cur,pic_path=addr, lenqs=lenqs, intens_exp=temp_intens_exp, intens_mod=temp_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([phi_s, phi_delta_s, dens_s, debye_s, rho_abs_h20, rho_abs_pol, rho_abs_alk, varied_phase])
    np.save(addr+'/'+model_name+"params.npy", parameters)
    print("error min:{}".format(np.min(err_map)))
Esempio n. 3
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)
Esempio n. 4
0
def single_point(*args):
    try:
        mkdir(addr)
    except:
        pass

    if args is not ():
        phi1 = args[0]
        phi2 = args[1]
        rho_abs_h20, rho_abs_pol, rho_abs_alk = args[2]
        val_debye = args[3]
    else:
        global phi1, phi2, rho_abs_h20
        global rho_abs_pol, rho_abs_alk, val_debye
    print(phi1, phi2, (rho_abs_h20,rho_abs_pol, rho_abs_alk), val_debye)
    qsvec, lenqs = generate_qvecs_lenqs(peakmax=lenqsmax)

    #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)
    print("lattice parameter: {:1.3f}".format(1./lattice_parameter))
    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]

    print("inices higher than {} ({}) are forbidden".format(peak_max, qsvec[peak_max]))
    if qsvec_forbid is not None:
        ind_forbid = find_indices(qsvec_forbid, lenqs)
        print("hkl forbid: {}, ind forbid{}:".format(str(qsvec_forbid), 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
    lenqs = qsvec_to_lenqs(qsvec)
    #brute force
    con1 = phi_to_const(phi1)
    cube_in = build_lattice_function(lattice_num=lattice_num, func=model_function, con=con1, warn=global_warn, sizelat=sizelat)

    con2 = phi_to_const(phi2+phi1)
    cube_out = build_lattice_function(lattice_num=lattice_num, func=model_function, con=con2, warn=global_warn, sizelat=sizelat)

    cube = (1.-cube_out)*rho_abs_alk
    cube += (cube_out - cube_in)*rho_abs_pol
    cube += cube_in*rho_abs_h20
    print(rho_abs_alk, rho_abs_pol, rho_abs_h20)
    plt.title(u"Электронная плотность в срезе")
    plt.imshow(cube[0], extent=[0,1,0,1])
    plt.colorbar()
    plt.show()
    cube -= cube.sum()/lattice_num**3

    lenqs2, intens_mod = straight_intensity(cube, peakmax=lenqsmax, sizelat=sizelat)
    assert lenqs.all()==lenqs2.all(),'trouble!'

    debye_factor = np.exp(-(lenqs*val_debye)**2)
    intens_mod *= debye_factor

    intens_mod[peak_max+1::] = 0.0
    intens_mod[ind_forbid] = 0.0

    err = calculate_error(lenqs, intens_mod, lenqs, intens_exp, norm='area')

    name_cur = "$\phi={}; $\phi2={}, rho=({},{},{}), d={}$".format(phi1, phi2,
                                                                          rho_abs_h20,rho_abs_pol,rho_abs_alk, val_debye)
    output(name=name_cur,pic_path=addr, lenqs=lenqs, intens_exp=intens_exp, intens_mod=intens_mod,
                               error=err, lenqs_exp_grand=lenqs_exp_grand, intens_exp_grand=intens_exp_grand, xaxis=xaxis)