def df2(xs, mesh, vx, vy, i, j):
    """
    Evaluate the model for the 2nd derivatives at the 10 locations of the domain
    at times ``t``.

    It returns a flatten version of the system, i.e.:
    y_1(t_1)
    ...
    y_10(t_1)
    ...
    y_1(t_4)
    ...
    y_10(t_4)
    """
    assert i == 1 or i == 2
    assert j == 1 or j == 2
    ############################################################################
    ############################################################################
    ##### ------------------ Solve the transport equation ---------------- #####
    ############################################################################

    Rd = 2.  # Retardation factor

    convCoeff = fp.CellVariable(mesh=mesh, rank=1)
    convCoeff()[0, :] = vx
    convCoeff()[1, :] = vy
    time = fp.Variable()
    # --- Make Source ---
    q0 = make_source_der_2(xs, mesh, time, i, j)
    # The solution variable
    var = fp.CellVariable(name="variable", mesh=mesh, value=0.)
    # Define the equation
    eqC = fp.TransientTerm(Rd) == -fp.ConvectionTerm(coeff=convCoeff) + q0

    # Solve
    d2U = []
    timeStepDuration = 0.005
    steps = 400
    for step in range(steps):
        time.setValue(time() + timeStepDuration)
        eqC.solve(var=var, dt=timeStepDuration)
        if step == 99 or step == 199 or step == 299 or step == 399:
            d2U = np.hstack([
                d2U,
                np.array([
                    var()[8 * 50 + 14],
                    var()[8 * 50 + 34],
                    var()[18 * 50 + 14],
                    var()[18 * 50 + 34],
                    var()[28 * 50 + 14],
                    var()[28 * 50 + 34],
                    var()[38 * 50 + 14],
                    var()[38 * 50 + 34],
                    var()[48 * 50 + 14],
                    var()[48 * 50 + 34]
                ])
            ])

    return d2U
Beispiel #2
0
def solve_both_withI(saveplot=False,
                     R_from=0.7,
                     R_to=1.0,
                     nr=1000,
                     duration=0.001,
                     nt=1000,
                     conv_file='convC.txt',
                     diff_file='diffC.txt',
                     plotcoeff=False,
                     levels=300,
                     logdiff=5,
                     ticks=None,
                     figsize=(10, 5),
                     hdf5=False):

    dr = (R_to -
          R_from) / nr  ## distance between the centers of the mesh cells
    dt = duration / nt  ## length of one timestep
    solution = np.zeros((nt, nr, 2))
    for j in range(nr):
        solution[:, j, 0] = (j * dr) + (dr / 2) + R_from

    mesh = fp.CylindricalGrid1D(
        dx=dr, nx=nr)  ## 1D mesh based on the radial coordinates
    mesh = mesh + (R_from, )  ## translation of the mesh to R_from
    n = fp.CellVariable(
        mesh=mesh
    )  ## fipy.CellVariable for the density solution in each timestep
    conv_data = np.genfromtxt(conv_file, delimiter=',')
    diff_data = np.genfromtxt(diff_file, delimiter=',')
    conv_i = np.zeros((nr, 2))
    diff_i = np.zeros((nr, 2))
    for i in range(conv_i.shape[0]):
        conv_i[i, 0] = R_from + (i * dr) + (dr / 2)

    for i in range(diff_i.shape[0]):
        diff_i[i, 0] = R_from + (i * dr) + (dr / 2)

    conv_i[:, 1] = np.interp(conv_i[:, 0], conv_data[:, 0], conv_data[:, 1])
    diff_i[:, 1] = np.interp(diff_i[:, 0], diff_data[:, 0], diff_data[:, 1])
    dC = diff_i[:, 1]
    diffCoeff = fp.CellVariable(mesh=mesh, value=dC)
    cC = conv_i[:, 1]
    convCoeff = fp.CellVariable(mesh=mesh, value=[cC])

    n.setValue(0.0)

    idata = np.genfromtxt('island_data.csv', delimiter=',')
    islands_ratio = np.zeros((nr, 2))
    for i in range(nr):
        islands_ratio[i, 0] = R_from + (i * dr) + (dr / 2)

    islands_ratio[:, 1] = np.interp(islands_ratio[:, 0], idata[:, 0], idata[:,
                                                                            1])
    w_length = math.ceil(nr / 20)
    if (w_length % 2) == 0:
        w_length = w_length + 1
    else:
        pass

    islands_ratio[:, 1] = savgol_filter(islands_ratio[:, 1], w_length, 3)
    islands_ratio[islands_ratio < 0] = 0
    re_ratio = islands_ratio[:, 1]
    re_in_islands = re_ratio * n.value

    gradLeft = (
        0.,
    )  ## density gradient (at the "left side of the radius") - must be a vector
    valueRight = 0.  ## density value (at the "right end of the radius")
    n.faceGrad.constrain(
        gradLeft, where=mesh.facesLeft)  ## applying Neumann boundary condition
    n.constrain(valueRight,
                mesh.facesRight)  ## applying Dirichlet boundary condition
    convCoeff.setValue(
        0, where=mesh.x <
        (R_from + dr))  ## convection coefficient 0 at the inner edge
    diffCoeff.setValue(
        0.001, where=mesh.x <
        (R_from + dr))  ## diffusion coefficient almost 0 at inner edge

    modules = MODULE(b"hc_formula_63", False, b"rosenbluth_putvinski", False,
                     False, 1.0, 1.0001)
    electron_temperature = ct.c_double(300.)
    electron_density = ct.c_double(1e20)
    effective_charge = ct.c_double(1.)
    electric_field = ct.c_double(3.66)
    magnetic_field = ct.c_double(1.)
    inv_asp_ratio = ct.c_double(0.30303)
    rate_values = (ct.c_double * 4)(0., 0., 0., 0.)

    eq = (fp.TransientTerm() == fp.DiffusionTerm(coeff=diffCoeff) -
          fp.ConvectionTerm(coeff=convCoeff))
    for i in range(nt):
        for j in range(nr):
            plasma_local = PLASMA(ct.c_double(mesh.x[j]), electron_density,
                                  electron_temperature, effective_charge,
                                  electric_field, magnetic_field,
                                  ct.c_double(n.value[j]))
            n.value[j] = adv_RE_pop(ct.byref(plasma_local), dt, inv_asp_ratio,
                                    ct.c_double(mesh.x[j]), ct.byref(modules),
                                    rate_values)

        print("{:.1f}".format((i / nt) * 100), '%', end='\r')
        eq.solve(var=n, dt=dt)
        if i == 0:
            solution[i, 0:nr, 1] = copy.deepcopy(n.value)
            re_in_islands = re_ratio * copy.deepcopy(n.value)
            n.value = copy.deepcopy(n.value) - re_in_islands
        else:
            re_local = PLASMA(ct.c_double(mesh.x[j]), electron_density,
                              electron_temperature, effective_charge,
                              electric_field, magnetic_field,
                              ct.c_double(re_in_islands[j]))
            re_in_islands[j] = adv_RE_pop(ct.byref(re_local),
                                          dt, inv_asp_ratio,
                                          ct.c_double(mesh.x[j]),
                                          ct.byref(modules), rate_values)

        re_in_islands[nr - 1] = 0
        solution[i, 0:nr, 1] = copy.deepcopy(n.value) + re_in_islands

    plot_solution(solution,
                  ticks=ticks,
                  levels=levels,
                  logdiff=logdiff,
                  figsize=figsize,
                  duration=duration,
                  nt=nt,
                  saveplot=saveplot)
    if plotcoeff == True:
        coeff_plot(conv_i=conv_i, diff_i=diff_i)
    else:
        pass

    if hdf5 == True:
        hdf5_save(fname="Dreicer_and_avalanche",
                  solution=solution,
                  conv=conv_i,
                  diff=diff_i,
                  duration=duration)
    else:
        pass

    return solution
Beispiel #3
0
var = fp.CellVariable(name="variable", mesh=mesh)

# --- Make source term
rho = 0.35
q0 = 1. / (np.pi * rho**2)
T = 0.3
xs_1 = np.array([1.25, 2.])
sourceTerm_1 = fp.CellVariable(name="Source term", mesh=mesh, value=0.)
for i in range(sourceTerm_1().shape[0]):
    sourceTerm_1()[i] = q0 * np.exp(-(
        (mesh.cellCenters[0]()[i] - xs_1[0])**2 +
        (mesh.cellCenters[1]()[i] - xs_1[1])**2) / (2 * rho**2)) * (time() < T)

eqC = fp.TransientTerm(
    Rd) == -fp.ConvectionTerm(coeff=convCoeff) + sourceTerm_1

if __name__ == '__main__':
    viewer = fp.Viewer(vars=var, datamin=0., datamax=2.5)
    viewer.plot()

data = []
timeStepDuration = 0.005
steps = 400
for step in range(steps):
    time.setValue(time() + timeStepDuration)
    eqC.solve(var=var, dt=timeStepDuration)
    if step == 99 or step == 199 or step == 299 or step == 399:
        data = np.hstack([
            data,
            np.array([