def solve_DiracIC(saveplot = False, R_from = 0.7, R_to = 1.0, nr = 1000, duration = 0.001, nt = 1000, diracLoc = 0.85, diracCoeff = 1., diracPercentage = 2, conv_file = 'convC.txt', diff_file = 'diffC.txt', plotcoeff = False, levels = 300, logdiff = 6, 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 diracWidth = int((nr / 100) * diracPercentage) n.setValue(delta_func(mesh.x - diracLoc, diracWidth * dr, diracCoeff)) 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]) 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 eq = (fp.TransientTerm() == fp.DiffusionTerm(coeff=diffCoeff) - fp.ConvectionTerm(coeff=convCoeff)) for i in range(nt): eq.solve(var=n, dt=dt) solution[i,0:nr,1]=copy.deepcopy(n.value) 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="DiracIC",solution=solution, conv=conv_i, diff=diff_i, duration=duration) else: pass return solution
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
def createMesh(self, rIn, rOut, n=100): """Creates the radial (1D) mesh""" dx = float(rOut - rIn) / n self.mesh = fp.CylindricalGrid1D(origin=rIn, nx=n, dx=dx) print("oops")