def set_rho(pos, Z, charge, width, rho_tree, prec): # making rho as a GaussFunc power = [0, 0, 0] norm = (width / np.pi) * np.sqrt(width / np.pi) s = 0 z = 0 rho_tot = vp.GaussExp() biggest = [0, -1] for i in range(len(pos)): s += Z[i] * pos[i] z += Z[i] new_rho = vp.GaussFunc(width, norm * float(Z[i]), list(pos[i]), power) rho_tot.append(new_rho) if (Z[i] > biggest[0]): biggest = [Z[i], i] pos_rhoel = s / z width_rho_el = np.log(10) / (np.linalg.norm(pos_rhoel - pos[biggest[1]])** 2) norm_rho_el = (width_rho_el / np.pi) * np.sqrt( width_rho_el / np.pi) * (charge - z) rho_el = vp.GaussFunc(width_rho_el, norm_rho_el, list(pos_rhoel), power) rho_tot.append(rho_el) vp.build_grid(rho_tree, rho_tot) vp.project(prec, rho_tree, rho_tot)
def x_tree_generator(x_tree, dir, prec=10e-3, beta=100.0, mid=0.0): import vampyr3d as vp beta = 100.0 power = [0, 0, 0] power[int(dir)] = 2 x = vp.GaussFunc(beta, 4.0 * beta**2, [mid, mid, mid], power) vp.build_grid(x_tree, x) vp.project(prec, x_tree, x)
def clone_tree(in_tree, out_tree, prec, MRA): def ones(r): return 1 temp_tree = vp.FunctionTree(MRA) vp.project(prec, temp_tree, ones) vp.multiply(prec, out_tree, 1, temp_tree, in_tree) temp_tree.clear() del temp_tree
def test_gaussFunc(): sigma = 0.01 pos = [0.0, 0.0, 0.0] power = [0, 0, 0] alpha = 1.0 / (2.0 * np.pi * sigma**2)**(3 / 2) beta = 1.0 / (2.0 * sigma**2) gauss = vp.GaussFunc(beta, alpha, pos, power) g_tree = vp.FunctionTree(MRA) vp.build_grid(g_tree, gauss) vp.project(prec, g_tree, gauss) assert isclose(g_tree.integrate(), 1.0, rel_tol=prec)
def test_gradient_and_divergence(): grad_tree = vp.FunctionTree(MRA) out_grad_tree = vp.FunctionTree(MRA) vp.project(prec, grad_tree, phi_exact) D = vp.ABGVOperator(MRA, 0.0, 0.0) grad = vp.gradient(D, grad_tree) assert isclose(grad[0][1].evalf([0.1, 0.0, 0.0]), d_phi_exact([0.1, 0.0, 0.0]), rel_tol=prec) grad_tree_vec = [] grad_tree_vec.append(tuple([1.0, grad_tree])) grad_tree_vec.append(tuple([1.0, grad_tree])) grad_tree_vec.append(tuple([1.0, grad_tree])) vp.divergence(out_grad_tree, D, grad_tree_vec) assert isclose(out_grad_tree.evalf([0.1, 0.1, 0.1]), 3.0 * grad[0][1].evalf([0.1, 0.1, 0.1]), rel_tol=prec)
beta = 2000 alpha = (beta / np.pi) * np.sqrt(beta / np.pi) rho_gauss = vp.GaussFunc(beta, alpha, pos, power) vp.build_grid(rho_tree, rho_gauss) vp.project_gauss(prec, rho_tree, rho_gauss) # rho_tree has a GaussFunc for rho # linear dielectric function eps_inv_tree = vp.FunctionTree(MRA) rho_eff_tree = vp.FunctionTree(MRA) Cavity_tree = vp.FunctionTree(MRA) V_tree = vp.FunctionTree(MRA) # making rho_eff_tree containing rho_eff vp.project(prec, eps_inv_tree, sfuncs.diel_f_Lin_inv) vp.multiply(prec, rho_eff_tree, 1, eps_inv_tree, rho_tree) # this will not change vp.project(prec, Cavity_tree, sfuncs.Cavity) Cavity_tree.rescale((e_0 - e_inf)) # start solving the poisson equation with an initial guess sfuncs.poisson_solver(V_tree, rho_eff_tree, P, prec) x_plt = np.linspace(-2, 2, 60) j = 1 error = 1 old_V_tree = vp.FunctionTree(MRA) while (error > prec):
def const_tree_generator(const_tree, prec=10e-3, beta=100.0, mu=0.0, mid=0.0): import vampyr3d as vp power = [0, 0, 0] const = vp.GaussFunc(beta, -6.0 * beta - mu**2, [mid, mid, mid], power) vp.build_grid(const_tree, const) vp.project(prec, const_tree, const)
Heh_p_Z = np.array([8.0, 1.0, 1.0]) while (Radius[0] <= 6.0): initialize_cavity([[0.0, 0.0, 0.0]], Radius, d) change_e_inf(e_inf) gamma_tree = V_SCF_exp(MRA, prec, P, D, charge, rho_tree, V_tree, Heh_p, Heh_p_Z) # finding E_r V_r_tree = vp.FunctionTree(MRA) eps_diff_tree = vp.FunctionTree(MRA) rho_diff_tree = vp.FunctionTree(MRA) poiss_tree = vp.FunctionTree(MRA) integral_tree = vp.FunctionTree(MRA) print('set V_r') vp.project(prec, eps_diff_tree, exp_eps_diff) vp.multiply(prec, rho_diff_tree, 1, eps_diff_tree, rho_tree) vp.add(prec, poiss_tree, 1, gamma_tree, -1, rho_diff_tree) poisson_solver(V_r_tree, poiss_tree, P, prec) # vp.multiply(prec, integral_tree, 1, rho_tree, V_r_tree) print('plotting potentials') x_plt = np.linspace(-7, 7, 1000) z_plt = np.linspace(-7, 7, 1000) X, Z = np.meshgrid(x_plt, z_plt) gamma_plt = np.zeros_like(X) V_r_plt = np.zeros_like(X) for i in range(len(x_plt)): for j in range(len(z_plt)): # gamma_plt[i][j] = gamma_tree.evalf([X[i][j], 0.0, Z[i][j]])] V_r_plt[i][j] = V_r_tree.evalf([X[i][j], 0.0, Z[i][j]]) # plt.plot(x_plt, gamma_plt, 'r')
def V_SCF_exp(MRA, prec, P, D, charge, rho_tree, V_tree, pos, Z): global e_inf global e_0 global Cavity # initializing FunctionTrees eps_inv_tree = vp.FunctionTree(MRA) rho_eff_tree = vp.FunctionTree(MRA) Cavity_tree = vp.FunctionTree(MRA) old_V_tree = vp.FunctionTree(MRA) print('set rho') set_rho(pos, Z, charge, 1000.0, rho_tree, prec) # making rho_eff_tree containing rho_eff print('set cavity functions') vp.project(prec / 100, eps_inv_tree, diel_f_exp_inv) vp.project(prec / 100, Cavity_tree, Cavity) vp.multiply(prec, rho_eff_tree, 1, eps_inv_tree, rho_tree) print("plotting the cavity") x_plt = np.linspace(-7.0, 7.0, 1000) Cavity_plt = np.array([Cavity_tree.evalf([x, 0., 0.]) for x in x_plt]) plt.plot(x_plt, Cavity_plt) plt.show() Cavity_tree.rescale(np.log(e_0 / e_inf)) j = 1 error = 1 poisson_solver(V_tree, rho_eff_tree, P, prec) while (error >= prec): # solving the poisson equation once gamma_tree = V_solver_exp(rho_eff_tree, V_tree, Cavity_tree, D, P, MRA, prec, old_V_tree) # finding error once temp_tree = vp.FunctionTree(MRA) vp.add(prec / 10, temp_tree, 1.0, V_tree, -1.0, old_V_tree) error = np.sqrt(temp_tree.getSquareNorm()) temp_tree.clear() del temp_tree # Reaction_charge = gamma_tree.integrate() # print('iter:\t\t\t%i\nerror:\t\t\t%f\nR charge:\t\t%f' % (j, error, # Reaction_charge)) print('iter:\t', j, '\t error:\t', error) # print('exact Reaction charge:\t%f\n'%((charge)*((1 - e_inf)/e_inf))) j += 1 print('converged total electrostatic potential\n') eps_inv_tree.clear() rho_eff_tree.clear() Cavity_tree.clear() old_V_tree.clear() del eps_inv_tree del rho_eff_tree del Cavity_tree del old_V_tree return gamma_tree