Making 2D MOS electrostatic simulation from scratch

Problem

MOS

MOS (Metal-Oxide-Semiconductor) is an important structure in the field of semiconductors. The problem is to calculate electrostatic potential of this structure.

Simulation

The simulation condition is shown above.

The basic poisson equation of electrostatics ∇⋅(ε∇ψ)=−ρ\nabla\cdot(\varepsilon\nabla\psi) = -\rho should be true in all points, where ψ\psi is electrostatic potential (voltage), ρ\rho is charge density and ε\varepsilon is permittivity.

There is no free charge in oxide: ρ=0\rho=0 in oxide.

In semiconductor, the charge density depends on the potential: ρ=−q[p0e−ψ/VT−n0eψ/VT+ND−NA]\rho=-q[p_0 e^{-\psi/V_T} - n_0 e^{\psi/V_T} + N_D - N_A] where n0n_0 and p0p_0 are thermal equilibrium concentration of electron and hole, NDN_D and NAN_A are doner and acceptor concentration and VT=kbT/qV_T=k_bT/q is thermal voltage.

How the algorithm/baseline code works

This algorithm updates ψ\psi until convergence.

for (iter = 1; iter <= max_iters; ++iter) {
  for (sweep = 0; sweep < inner_jacobi; ++sweep) {
    update_psi();
  }
  if (converged) break;
}

I actually have no idea why there are double loops.

Inside update_psi()

for (j = 1; j < ny - 1; ++j) {
  for (i = 1; i < nx - 1; ++i) {
    rho = calculate_rho_from_psi();
    g = calculate_residual(rho);
    psi_new[j][i] = calculate_update_amount(psi[j][i], g);
  }
}
copy_psi_new_to_psi();

Residual gg is an error of the poisson equation, which should be zero ideally. We can calculate how much to update ψ\psi by using gg and dg/dψdg/d\psi.

I manually added comments for better understanding and TODOs.

Where is parallelism