Exemple #1
0
def main():
    g_inc = np.array([[-1, 0], [0, 0], [1, 5e18], [900, 5e18], [901, 0],
                      [1101, 0]])
    pth = os.path.join(tl.docs(0), "pyfit", "points.pdf")
    dirpth = os.path.dirname(pth)
    if not os.path.isdir(dirpth):
        os.makedirs(dirpth)
    i = 0
    dT = [0.01, 0.001, 0.0005]
    JJ = [2, 3, 4, 5, 10, 20, 30, 40, 50]
    JJ = [4]
    ku = 1e-33
    kd = 1e-33
    D = 2.7e-9
    with PdfPages(pth) as pdf:
        for dt in dT:
            for xpoints in JJ:
                i += 1
                print("{:.1%}".format(float(i) / (len(dT) * len(JJ))))
                fig = run(
                    g_inc,
                    L=20e-6,
                    T=1100,
                    dt=dt,
                    J=xpoints,
                    k_u=ku,
                    k_d=kd,
                    suffix="ku%.3e" % ku,
                    D=D,
                )
                pdf.savefig(fig)
                plt.close()

    pass
Exemple #2
0
def plot_result(**kws):
    u"plot function"
    gpdp = kws.pop("pdp", [0])
    ginc = kws.pop("ginc", [0])
    tm = kws.pop("time", [0])
    gfited = kws.pop("gfited", [0])
    title = kws.pop("title", "")
    savefig = kws.pop("savefig", os.path.join(tools.docs(0), "wave.png"))
    font = {"family": "Times New Roman", "weight": "heavy", "size": 18}
    plt.rc("font", **font)
    plt.rcParams.update({"mathtext.default": "regular"})
    fig = plt.figure(figsize=(7, 7), facecolor="w")
    gs = GridSpec(2, 1)
    gs.update(left=0.05, right=0.9, wspace=0.3, hspace=0.0)
    ax1 = plt.subplot(gs[0, 0])
    ax2 = plt.subplot(gs[1, 0])
    [ax.set_xticklabels([]) for ax in [ax1]]

    ax1.plot(tm, gpdp, "ko", label="experiment")
    ax1.plot(tm, gfited, "r.-", label="fit")
    ax2.plot(tm, ginc, "k.", label="sig")
    ginc_smth = tools.savitzky_golay(ginc, int(0.005 * len(ginc)) + 1, 1)
    ax2.plot(tm, ginc_smth, "r-", label="smooth")

    lbls = ["$\Gamma_{pdp}$", "$\Gamma_{inc}$"]
    [
        ax.set_ylabel(r"%s" % (lbl), fontweight="heavy")
        for ax, lbl in zip([ax1, ax2], lbls)
    ]
    [tools.grid_visual(ax) for ax in fig.axes]
    [tools.ticks_visual(ax) for ax in fig.axes]
    [ax.locator_params("y", nbins=5) for ax in [ax1, ax2]]
    ax2.set_xlabel("time (sec)", fontweight="heavy")
    [ax.get_yaxis().set_label_coords(-0.08, 0.5) for ax in fig.axes]
    ax1.legend(
        labelspacing=0.1,
        columnspacing=0.1,
        handletextpad=0.5,
        numpoints=1,
        framealpha=0,
        bbox_to_anchor=(0.7, 0.5),
    )
    [ax.yaxis.get_major_ticks()[-1].label1.set_visible(False) for ax in [ax2]]
    ax1.margins(0.1)
    ax2.margins(0.1)
    plt.suptitle(title)
    plt.savefig(savefig, dpi=300, bbox_inches="tight")
Exemple #3
0
def scan_parameters():
    """change ku,kd, D and save all results in multipage PDF file for comparison"""
    g_inc = np.array([[-1, 0], [0, 0], [1, 5e18], [900, 5e18], [901, 0],
                      [1101, 0]])

    # dT =[1,0.1,0.01]
    # JJ = [5,10,20,30,40,50]
    pth = os.path.join(tl.docs(0), "pyfit", "D.pdf")
    dirpth = os.path.dirname(pth)
    if not os.path.isdir(dirpth):
        os.makedirs(dirpth)
    i = 0
    ru = np.linspace(5e-34, 5e-32, 30)
    ru = [1e-33]
    rd = [1e-33]
    rD = np.linspace(5e-10, 5e-8, 30)
    # rD = [2.7e-9]
    with PdfPages(pth) as pdf:
        for D in rD:
            for ku in ru:
                for kd in rd:
                    i += 1
                    print("{:.1%}".format(
                        float(i) / (len(ru) * len(rd) * len(rD))))
                    fig = run(
                        g_inc,
                        L=20e-6,
                        T=1100,
                        dt=0.01,
                        J=20,
                        k_u=ku,
                        k_d=kd,
                        suffix="ku%.3e" % ku,
                        D=D,
                    )
                    pdf.savefig(fig)
                    plt.close()
Exemple #4
0
def wavefit(**kws):
    u"minimize using scipy"
    Nt = kws.pop("Nt", 100)
    T = float(kws.pop("T", 200))
    Tend = float(kws.pop("Tend", 100))
    probe = kws.pop("probe", "PDP4")
    shot = kws.pop("shot", 29733)
    ks = kws.pop("ks", 1.1e18)
    ku = kws.pop("ku", 1e-32)
    silent = kws.pop("silent", True)
    kd = kws.pop("kd", 1e-33)
    global gpdp

    def mbe(x, uinit=False):
        global gpdp
        try:
            x = x[0]
        except:
            pass
        G_stp = np.ones(Nt_stp) * x
        if x < 0:
            # flog.write('%.2e\t[%.2e %.2e]\t%e\t[%.4f]\t%e\n'%
            #     (ey[N*stp],gpdp[0],gpdp[1],x,G_stp[0],1e10))
            return 1e10
        pm.update([
            ("G", G_stp),
            ("T", T_stp),
            ("Tend", Tend_stp),
            ("Nt", Nt_stp),
            ("Uinit", U),
        ])
        res = BE(**pm)
        gpdp = res.pop("pdp")
        # print ey[N*stp],gpdp,x,G_stp,chi(ey[N*stp],gpdp)
        if not uinit:
            # flog.write('%.2e\t[%.2e %.2e]\t%e\t[%.4f]\t%e\n'%
            #     (ey[N*stp],gpdp[0],gpdp[1],x,G_stp[0],chi(ey[N*stp],gpdp)))
            pass
        if uinit:
            return res.pop("concentration")
        else:
            return chi(ey[N * stp], gpdp)

    # pth = os.path.join(docs(0),'Stencils')
    pth = os.path.join(tools.docs(0), "PDP")
    # data = np.loadtxt(os.path.join(pth,'expTest.txt'))
    # ex = data[:,0]; ey = data[:,1]
    PTH = tools.network_drives()
    fpth = os.path.join(PTH["freenas"], "Results", "%s" % probe,
                        "%d_%s.txt" % (shot, probe))
    if not os.path.exists(fpth):
        fpth = os.path.join(PTH["freenas"], "Results", "%s" % probe,
                            "%d_%s.dat" % (shot, probe))
    if not os.path.exists(fpth):
        fpth = os.path.join(tools.docs(0), "PyOut",
                            "%d_%s.txt" % (shot, probe))
    print(fpth)
    data = np.loadtxt(fpth, skiprows=1)
    ex = data[:, 0]
    ey = data[:, 1]
    if ex[-1] < T:
        T = ex[-1]
    pm = parameters()
    Nx = pm.get("Nx")  # ;Nt=pm.get('Nt');T=pm.get('T');Tend=pm.get('Tend')
    U = np.zeros(Nx + 1)
    pm.update([("T", T), ("Tend", Tend), ("Nt", Nt)])
    tm = np.linspace(0, T, Nt + 1)
    dt = tm[1] - tm[0]
    pm.update([("ks", ks), ("ku", ku), ("kd", kd), ("Tend", Tend)])
    print("#{} {} Tend={} T={} N={} ks={:.2e}, ku={:.2e}, kd={:.2e}".format(
        shot, probe, Tend, T, Nt, ks, ku, kd))

    if Nt + 1 != len(ey):
        # print pm['Nt'],len(ey),ex[-1],pm['T']
        ef = interp1d(ex, ey)
        ex = tm
        ey = ef(tm)
    G = np.zeros(Nt + 1)
    stp = 1  # step in points for fitting time window, min=1, best resolution.
    # print int(Tend/T*Nt),Nt
    R = range(int(Tend / T * Nt) + int(Nt * 0.00))  # R = range(Nt)
    # clrs = np.linspace(0,1,len(R))
    # plt.plot(ex,ey,'k.--')
    Nt_stp = 1
    T_stp = dt
    Tend_stp = dt
    gpdp = []
    for N in R:
        # flog.write('{:.2%}\n'.format((N+1)/float(len(R))))
        # res = minimize(mbe,25,options={'maxiter':50})
        # res = minimize(mbe,25,method='CG')#Works well,but slow
        res = minimize(mbe, 25, method="Nelder-Mead")
        if not silent:
            print("{:.2%}\t{:.4f}".format((N + 1) / float(len(R)), res.x[0]))
        if res.x[0] < 0:
            G[N * stp] = 0
            U = mbe(0, True)
        else:
            G[N * stp - 1] = res.x[0]
            U = mbe(res.x[0], True)
        # flog.write('Ginc[%d] = %.4f\n'%(N*stp,res.x[0]))
        # plt.plot(tm_stp+N*stp*dt,gpdp,'-',color=plt.cm.jet(clrs[N]))
        # print gpdp[-1],ey[N*stp]

    G[-1] = 0
    pm.update([("T", T), ("Tend", Tend), ("Nt", Nt), ("G", G),
               ("Uinit", np.zeros(Nx + 1))])
    res = BE(**pm)
    t = res.pop("time")
    pdp = res.pop("pdp")
    with open(os.path.join(pth, "%d_%s.txt" % (shot, probe)), "w") as ff:
        ff.write("time\tGinc\tcalc\texp\n")
        for tg, ginc, clc, exp in zip(t, G * ks, pdp, ey):
            ff.write("%.4f\t%.4e\t%.4e\t%.4e\n" % (tg, ginc, clc, exp))
    plot_result(
        **{
            "time": t,
            "pdp": ey,
            "gfited": pdp,
            "ginc": G * ks,
            "title": "%d %s" % (shot, probe),
            "savefig": os.path.join(pth, "%d_%s.png" % (shot, probe)),
        })
    plt.close()
Exemple #5
0
def BE(**kws):
    """
    Permeation solver. 
    """
    Nx = kws.get("Nx", 30)  # Number of space mesh points
    Nt = kws.get("Nt", 100)  # Number of time mesh points
    T = kws.get("T", None)  # Duration of the calculated permeation [s]
    D = kws.get("D", 1.1e-8)  # Diffusion coeff.
    I = kws.get("I", None)  # I(x) - initial concentration profile function
    ku = kws.get("ku", None)  # upstream recombination coeff. []
    kd = kws.get("kd", None)  # downstream recombination coeff.
    ks = kws.get("ks", None)  # amplitude of the incident flux
    G = kws.get("G", None)  # initial guess for the incident flux
    L = kws.get("L", 2e-5)  # membrane thickness [m]
    PLOT = kws.get("PLOT", False)  # plot or not the results
    saveU = kws.get("saveU", None)  #
    Uinit = kws.get("Uinit", None)  # initial concentration profile
    # ------------------------------------------------------------------------------
    if os.name == "nt":
        # print 'windows'
        ku = 2 * ku
        kd = 2 * kd  # if this is applied, result is same with TMAP7.
    # ------------------------------------------------------------------------------
    G = G * ks
    if len(np.where(G < 0)[0]):
        return {
            "time": np.linspace(0, T, Nt + 1),
            "pdp": np.zeros(Nt + 1),
            "concentration": Uinit,
        }
    # start = time.clock()
    x = np.linspace(0, L, Nx + 1)  # mesh points in space
    t = np.linspace(0, T, Nt + 1)  # mesh points in time
    if I:
        u_1 = np.array([I(i) for i in x])  # initial concentration
    else:
        u_1 = np.copy(Uinit)
    # if len(np.where(U==0)[0]) > 0: print 'zeros'
    dx = x[1] - x[0]
    dt = t[1] - t[0]
    F = D * dt / dx**2
    inlet = []
    outlet = []
    inlet.append(ku * u_1[0]**2)
    outlet.append(kd * u_1[Nx]**2)
    u = np.zeros(Nx + 1)
    if saveU:
        Usave = np.zeros((Nt + 1, Nx + 1), float)
    if saveU:
        Usave[0] = u_1
    if PLOT:
        plt.plot(x / 1e-6, u_1, "k-", lw=4)
    color_idx = np.linspace(0, 1, Nt)
    teta1 = D / (ku * dx)
    teta2 = D / (kd * dx)
    for n in range(0, Nt):
        # calculate u[1] and u[Nx-1] using explicit stencil
        g0 = F * u_1[0] + (1 - 2 * F) * u_1[1] + F * u_1[2]
        gL = F * u_1[Nx - 2] + (1 - 2 * F) * u_1[Nx - 1] + F * u_1[Nx]
        # put 1 for u[0] and u[Nx-1] in A
        A = diags(
            diagonals=[
                [0] + [-F for i in range(Nx - 1)],
                [1] + [1.0 + 2.0 * F for i in range(Nx - 1)] + [1],
                [-F for i in range(Nx - 1)] + [0],
            ],
            offsets=[1, 0, -1],
            shape=(Nx + 1, Nx + 1),
            format="csr",
        )
        # in the b (for BE) put roots of the quadratic equation for the border.
        b = np.array([
            -teta1 / 2.0 +
            0.5 * np.sqrt(teta1**2 + 4 * teta1 * g0 + 4 * G[n] / ku)
        ] + [i for i in u_1[1:Nx]] +
                     [-teta2 / 2.0 + 0.5 * np.sqrt(teta2**2 + 4 * teta2 * gL)])
        # solve SLE
        u[:] = spsolve(A, b)
        u"Instead of u**2 put u*a, where a - u from previous step"
        for _ in range(3):
            a0 = u[0]
            aL = u[Nx]
            A = diags(
                diagonals=[
                    [-D / dx] + [-F for i in range(Nx - 1)],
                    [D / dx + ku * a0] +
                    [1.0 + 2.0 * F
                     for i in range(Nx - 1)] + [D / dx + kd * aL],
                    [-F for i in range(Nx - 1)] + [-D / dx],
                ],
                offsets=[1, 0, -1],
                shape=(Nx + 1, Nx + 1),
                format="csr",
            )
            b = np.array([G[n]] + [i for i in u_1[1:Nx]] + [0])
            u[:] = spsolve(A, b)
        u_1, u = u, u_1
        inlet.append(ku * u_1[0]**2)
        outlet.append(kd * u_1[Nx]**2)
        if PLOT:
            plt.plot(x / 1e-6, u_1, ".-", color=plt.cm.jet(color_idx[n]))
        if saveU:
            Usave[n + 1] = u_1

    if PLOT:
        font = {"family": "Times New Roman", "weight": "heavy", "size": 25}
        plt.rc("font", **font)
        plt.rcParams.update({"mathtext.default": "regular"})
        ax = plt.gca()
        ax.set_xlim(0, L / 1e-6)
        ax.set_xlabel("x ($\mu m$)", fontweight="heavy")
        ax.set_ylabel("concentration ($m^{-3}$)", fontweight="heavy")
        sf = os.path.join(tools.docs(0), "Stencils",
                          "concentraion.png")  # %(F))
        plt.savefig(sf, dpi=300, bbox_inches="tight")
    # end = time.clock()
    result = dict()
    result.update([("reflected", inlet), ("pdp", outlet), ("time", t),
                   ("concentration", u_1)])
    if saveU:
        return Usave, [t, outlet]
    else:
        return result
Exemple #6
0
def run(g_inc,
        L=20e-6,
        T=900,
        dt=1,
        J=40,
        k_u=1e-33,
        k_d=1e-30,
        suffix="a",
        D=1e-9):
    u""" T = time, dt - time step, J - x steps, k_u, k_d - recombination """
    dx = float(L) / float(J - 1)  # Grid parameter
    x_grid = np.array([j * dx for j in range(J)])  # Grid
    N = 1 + int(float(T) / float(dt))  # Time step
    t_grid = np.array([n * dt for n in range(N)])  # Time grid
    # plt.plot(t_grid,'.')
    # plt.show()
    T_membrane = 573.0
    # D=2.9e-7*np.exp(-0.23*1.6e-19/(1.38e-23*T_membrane)) # Diffusion coeffitient for U
    sigma = float(D * dt) / float((2.0 * dx * dx))
    if 0:
        print("%.3e:\tN" % N)
        print("%.3e:\tdx" % dx)
        print("%.3e:\tsigma" % sigma)
        print("%.3e:\tdt" % dt)
    # suffix = '%.0es %.0e'%(dt, dx)
    u""" initial concentration """
    U = np.array([0.0 for _ in range(J)])
    ff = interp1d(g_inc[:, 0], g_inc[:, 1])
    g_inc = ff(t_grid)
    if 0:
        plt.plot(t_grid, g_inc, ".")
        plt.margins(0.1)
        plt.show()

    def f_vec(U, ti):
        "ti - time index"
        upstream = -D / (2.0 * k_u * dx) + 0.5 * np.sqrt(
            (D / (k_u * dx))**2 + 4 * D * U[1] /
            (k_u * dx) + 4 * g_inc[ti - 1] / k_u)
        downstream = -D / (2.0 * k_d * dx) + 0.5 * np.sqrt(
            (D / (k_d * dx))**2 + 4 * D * U[-2] / (k_d * dx))
        vivec = np.array([0.0 for _ in range(J)])
        vivec[0] = upstream
        vivec[-1] = downstream
        return vivec

    plt.gca().margins(0.1)
    u""" Matrixes """
    A = (np.diagflat([-sigma for _ in range(J - 1 - 1)] + [0.0], -1) +
         np.diagflat([1.0] + [1.0 + 2.0 * sigma
                              for _ in range(J - 2)] + [1.0]) +
         np.diagflat([0.0] + [-sigma for _ in range(J - 1 - 1)], 1))

    B = (np.diagflat([sigma for _ in range(J - 1 - 1)] + [0.0], -1) +
         np.diagflat([0.0] + [1.0 - 2.0 * sigma
                              for _ in range(J - 2)] + [0.0]) +
         np.diagflat([0.0] + [sigma for _ in range(J - 1 - 1)], 1))

    U_record = []
    U_record.append(U)
    # U_record.append([U[0],U[-1]])
    u""" Solving matrix equation for all time-layers"""
    for ti in range(1, N):
        U_new = np.linalg.solve(A, B.dot(U) + f_vec(U, ti))  # numpy Gauss
        U = U_new
        U_record.append(U)
        # U_record.append([U[0],U[-1]])
    fig = plt.figure()
    if 1:
        ax2 = fig.add_subplot(2, 1, 2)
        plt.xlabel("x")
        plt.ylabel("concentration")
        nn = 20
        tt = np.linspace(0 + T / float(nn), T - T / float(nn), nn)
        plt.plot(x_grid, U_record[0], "k.", label="t = %.1f" % t_grid[0], lw=2)
        color_idx = np.linspace(0, 1, nn)
        for ij, t1 in enumerate(tt):
            plt.plot(
                x_grid,
                U_record[int(t1 / dt)],
                label="t = %.2f" % t_grid[int(t1 / dt)],
                color=plt.cm.jet(color_idx[ij]),
            )  # @UndefinedVariable

        plt.plot(x_grid, U, "k--", label="t = %.1f" % t_grid[-1], lw=2)
        # legend(framealpha = 0.8)
        plt.margins(0.1)
        pth = os.path.join(tl.docs(0), "pyfit", "diff_profile%s.png" % suffix)
        dirpth = os.path.dirname(pth)
        if not os.path.isdir(dirpth):
            os.makedirs(dirpth)
        # plt.savefig(pth,bbox_inches = 'tight', dpi = 300)
    ax2 = fig.add_subplot(2, 1, 1)
    U_r = np.array(U_record)
    try:
        plt.plot(t_grid, U_r[:, -1]**2 * k_d, label="out")
    except:
        plt.plot(t_grid, U_r[1]**2 * k_d, label="out")
    # plot(t_grid,U_r[:,0]**2*k_u,label = 'input')
    if 0:
        "plot 24248 PDP6 experimental result"
        epth = os.path.join(tl.docs(5), "workspace", "fit", "output",
                            "24248_PDP6_gamma_pdp_exp.txt")
        expdata = np.loadtxt(epth, skiprows=1)
        plt.plot(expdata[:, 0], expdata[:, 1], label="29005 PDP6")
    plt.legend(framealpha=0.8)
    plt.margins(0.1)
    pth = os.path.join(tl.docs(0), "pyfit", "diff_fluxes%s.png" % suffix)
    plt.suptitle(r"$%ss\;%sm\;k_u = %s;k_d = %s;D = %s$" % (ltexp(
        dt, 0), ltexp(dx, 1), ltexp(k_u, 1), ltexp(k_d, 1), ltexp(D, 2)))
    plt.grid(True)
    return plt.gcf()