<jupyter_start><jupyter_text>Finite Differences CapSol ExampleMinimal example of using capsol python code to compute the voltage between a SPM tip (sphere, cone, and cantilever body disk) and a metal surface.<jupyter_code>%load_ext autoreload
%autoreload 2
%matplotlib inline

import numpy as np
from scipy import linalg
from scipy import sparse
from scipy.sparse import linalg as la
import capsol
import matplotlib.pyplot as plt

params= capsol.Params(Nr=950, Nz_plus=950, rhoMax=20*40000.0, zMax=20*40000.0, d=200.0)
sim = capsol.CapSol(params)
%time sim.setup_matrices()
%time sim.solve()
%time sim.process()
sim.energy_z
sim.r_ratio
plt.imshow(sim.spm_tip[::-1, :])


fig, ax = plt.subplots()
s = np.s_[:, :]
plt.contourf(sim.R[s], sim.Z[s], sim.u.reshape((sim.Nz, sim.Nr))[s], levels=20)
plt.colorbar()
ax.set_aspect('equal')
plt.xlim(0, 1000)
plt.ylim(-200, 1000)
params2 = capsol.Params(Nr=950, Nz_plus=950, rhoMax=20*40000.0, zMax=20*40000.0, d=200.0+0.4*2)
sim2 = capsol.CapSol(params2)
sim2.setup_matrices()
sim2.solve()
sim2.process()
params3 = capsol.Params(Nr=950, Nz_plus=950, rhoMax=20*40000.0, zMax=20*40000.0, d=200.0-0.4*2)
sim3 = capsol.CapSol(params3)
sim3.setup_matrices()
sim3.solve()
sim3.process()
C = sim.energy # 1 V applied, so E(J) = C(F)
C_z = (sim3.energy - sim2.energy ) / ((sim3.params.d - sim2.params.d)*1e-9)
C_zz = (sim3.energy + sim2.energy - 2*sim.energy ) / (((sim3.params.d - sim2.params.d)**2)*1e-18)
2*C_z**2 / (C_zz * C)
(sim3.energy_z, sim.energy_z, sim2.energy_z)
energy_z = 0.5 * np.sum(dV * capsol.E_field(u, sim.r, sim.z).imag**2) * 1e-9 * 8.854e-12
energy_z
# All the energy related to the radial field is not helpful / useful...
E_J = energy * 1e-9 * 8.854e-12
E_J
sim
# E-field is V/m, here V/nm...
# dV = nm^3
# Units are V^2 * nm...
class arrayBuilder:
    def __init__(self, estimated_size=None):
        self.rows = []
        self.cols = []
        self.data = []
    
    def __call__(self, row, col, data):
        self.rows.append(row)
        self.cols.append(col)
        self.data.append(data)
def poisson_variable_spacing_radial(x, y):
    Nx = len(x)
    Ny = len(y)
    hx = np.diff(x)
    hy = np.diff(y)
    rows = []
    cols = []
    data = []
    ab = arrayBuilder()
    for i in range(Ny):
        for j in range(Nx): # Radial
            ind = i * Nx + j # This point
            ixp = ind + 1    # +x 
            ixn = ind - 1    # -x
            iyp = (i+1)*Nx + j  # +y
            iyn = (i-1)*Nx + j  # -y
            
            
            Dx_plus = hx[j] if j < (Nx-1) else 0.0
            Dx_minus = hx[j-1] if j > 0 else hx[j]
            x0 = x[j]
            Dy_plus = hy[i] if i < (Ny-1) else 0.0
            Dy_minus = hy[i-1] if i > 0 else 0.0
            
            prefactor_x = 4/((Dx_plus+Dx_minus)*(Dx_plus**2 + Dx_minus**2))
            
            prefactor_y = 4/((Dy_plus+Dy_minus)*(Dy_plus**2 + Dy_minus**2))

            

            diag_ind = (Dx_plus+Dx_minus) * prefactor_x + (Dy_plus+Dy_minus) * prefactor_y
            if j == 0:
                ab(ind, ixp, -2 * Dx_minus * prefactor_x) # That's it, no radial derivative here...
            elif j < (Nx - 1):
                ab(ind, ixp, -1 * Dx_minus * prefactor_x + -1 / (x0 * (Dx_plus+Dx_minus)))
            
                
            
            if j > 0:
                ab(ind, ixn, -1 * Dx_plus * prefactor_x + 1 / (x0 * (Dx_plus+Dx_minus)))
            
            if j == (Nx - 1):
                diag_ind += -1 / (x0 * (Dx_plus+Dx_minus)) # 1st order difference uses the grid point here...
            
            ab(ind, ind, diag_ind)
            
            if i > 0:
                ab(ind, iyn,  -1 * Dy_plus * prefactor_y)
            if i < (Ny-1):
                ab(ind, iyp, -1 * Dy_minus * prefactor_y)
    
    return sparse.csr_matrix(sparse.coo_matrix((ab.data, (ab.rows, ab.cols)), shape=(Nx*Ny, Nx*Ny))) # Convert to better
%time capsol.poisson_variable_spacing_radial(sim.r, sim.z)
%time capsol.poisson_variable_spacing_radial_faster(sim.r, sim.z)
sim.
sparse.coo_matrix()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15052451817516468, 'fraction_numerical': 0.04732861673578922, 'mean_word_length': 2.259141494435612, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '433524', 'license': 'MIT', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1662, 'n_words': 368, 'path': 'capsol/Finite Differences CapSol Test Far.ipynb', 'repo_name': 'maroniea/xsede-spm', 'size': '4099'}