def test_multiply_vec(): multiply_vec = [] multiply_vec.append(tuple([1.0, phi_tree])) multiply_vec.append(tuple([1.0, phi_tree])) vp.multiply(prec / 10, mult_vec_tree, multiply_vec) assert isclose(mult_vec_tree.evalf([0, 0, 0]), phi_exact([0, 0, 0])**2, rel_tol=prec)
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 dot_product(prec, factor_array1, factor_array2, out_tree, MRA): ''' performs the dot product operation on two tree arrays of size 3. the operation is performed as if each of the arrays is a 3D vector and each of the trees are a value in that vector. ''' add_tree1 = vp.FunctionTree(MRA) mult_tree1 = vp.FunctionTree(MRA) mult_tree2 = vp.FunctionTree(MRA) mult_tree3 = vp.FunctionTree(MRA) vp.multiply(prec, mult_tree1, 1, factor_array1[0], factor_array2[0]) vp.multiply(prec, mult_tree2, 1, factor_array1[1], factor_array2[1]) vp.multiply(prec, mult_tree3, 1, factor_array1[2], factor_array2[2]) vp.add(prec / 10, add_tree1, 1.0, mult_tree1, 1.0, mult_tree2) vp.add(prec / 10, out_tree, 1.0, mult_tree3, 1.0, add_tree1) mult_tree1.clear() mult_tree2.clear() mult_tree3.clear() add_tree1.clear() del mult_tree1 del mult_tree2 del mult_tree3 del add_tree1
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): # solving the poisson equation once
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') # plt.plot(x_plt, V_r_plt, 'b')
def test_multiply(): mult_tree = vp.FunctionTree(MRA) vp.multiply(prec, mult_tree, 1, phi_tree, phi_tree_pois) assert isclose(mult_tree.evalf([0, 0, 0]), phi_exact([0, 0, 0])**2, rel_tol=prec)
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
def gamma_Lin(eps_inv_tree, DC_vector, DV_vector, out_tree, prec, MRA): temp_tree = vp.FunctionTree(MRA) dot_product(prec, DV_vector, DC_vector, temp_tree, MRA) vp.multiply(prec, out_tree, 1, temp_tree, eps_inv_tree) out_tree.rescale((1 / (4 * np.pi))) # probably wrong recheck