fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def solve_poisson_boltzmann(charge_density, epsilon, kappa, dx, max_iter=1000, tol=1e-6):
  5. """
  6. Solve the Poisson-Boltzmann equation in 1D with Dirichlet boundary conditions.
  7.  
  8. Args:
  9. - charge_density (ndarray): Array containing the charge density.
  10. - epsilon (float): Dielectric constant of the medium.
  11. - kappa (float): Inverse Debye length.
  12. - dx (float): Grid spacing.
  13. - max_iter (int): Maximum number of iterations for the solver.
  14. - tol (float): Tolerance for convergence.
  15.  
  16. Returns:
  17. - potential (ndarray): Array containing the electrostatic potential.
  18. """
  19. n = len(charge_density)
  20. potential = np.zeros(n)
  21. for _ in range(max_iter):
  22. old_potential = np.copy(potential)
  23. for i in range(1, n - 1):
  24. potential[i] = 0.5 * (old_potential[i - 1] + old_potential[i + 1] +
  25. dx**2 / epsilon * charge_density[i] -
  26. kappa**2 * np.sinh(old_potential[i]))
  27. if np.max(np.abs(potential - old_potential)) < tol:
  28. break
  29. return potential
  30.  
  31. # Parameters
  32. charge_density = np.array([0, 1, 0, -1, 0])
  33. epsilon = 1.0
  34. kappa = 0.1
  35. dx = 1.0
  36.  
  37. # Solve Poisson-Boltzmann equation
  38. potential = solve_poisson_boltzmann(charge_density, epsilon, kappa, dx)
  39.  
  40. # Plotting
  41. plt.figure()
  42. plt.plot(np.arange(len(charge_density)), charge_density, label='Charge Density')
  43. plt.plot(np.arange(len(potential)), potential, label='Potential')
  44. plt.xlabel('Position')
  45. plt.ylabel('Value')
  46. plt.legend()
  47. plt.show()
Success #stdin #stdout 0.85s 55540KB
stdin
Standard input is empty
stdout
Standard output is empty