# Math and Science Behind the Atom Simulator

This simulator visualizes electron probability density, not a literal electron orbit. For hydrogen, the code uses the standard separable solution to the nonrelativistic Schrodinger equation for a single electron in a Coulomb potential. For helium, it uses a common screened, hydrogen-like approximation because the exact two-electron problem is not separable.

## Hydrogen Model

Hydrogen has one proton and one electron. In atomic units, its time-independent Schrodinger equation is:

```text
[-1/2 nabla^2 - 1/r] psi(r, theta, phi) = E psi(r, theta, phi)
```

Because the potential depends only on distance `r`, the wavefunction separates into radial and angular parts:

```text
psi(n,l,m)(r,theta,phi) = R(n,l)(r) Y(l,m)(theta,phi)
```

The quantum numbers mean:

- `n`: principal quantum number, controls shell size and energy.
- `l`: angular momentum quantum number, controls orbital shape.
- `m`: magnetic quantum number, controls orientation and angular structure.

The valid ranges are:

```text
n = 1, 2, 3, ...
l = 0, 1, ..., n - 1
m = -l, ..., l
```

The app uses nonnegative `m` values and renders real-valued orbital combinations, which are easier to visualize than complex-valued spherical harmonics.

## Radial Wavefunction

The radial function in the code is:

```text
rho = 2 Z_eff r / n

R(n,l)(r) =
sqrt((2 Z_eff / n)^3 * (n-l-1)! / (2n (n+l)!))
* exp(-rho/2)
* rho^l
* L(n-l-1)^(2l+1)(rho)
```

`L` is an associated Laguerre polynomial. For hydrogen, `Z_eff = 1`.

The radial function sets how far from the nucleus the electron is likely to be found. Larger `n` values generally produce larger orbitals and more radial nodes.

## Angular Wavefunction

The angular part is based on spherical harmonics. The simulator uses real combinations:

```text
Y(l,m)(theta,phi) proportional to P(l,m)(cos theta) cos(m phi)
```

`P(l,m)` is an associated Legendre polynomial.

This creates familiar orbital shapes:

- `s` orbitals: spherical
- `p` orbitals: two lobes
- `d` orbitals: clover-like or toroidal-lobed forms
- `f` orbitals: more complex multi-lobed forms

## Probability Density

Quantum mechanics predicts probabilities from the squared magnitude of the wavefunction:

```text
probability density = |psi|^2
```

In spherical coordinates, the volume element contributes an extra `r^2 sin(theta)` factor. The simulator samples directions uniformly on the sphere, so the radial acceptance calculation includes the `r^2` contribution:

```text
P_sample proportional to |R(n,l)(r) Y(l,m)(theta,phi)|^2 r^2
```

The rendered point cloud is therefore denser where the electron is more likely to be measured.

## Sampling Method

The program uses rejection sampling:

1. Pick a random radius and random direction.
2. Evaluate the orbital probability at that point.
3. Accept or reject the point based on that probability.
4. Repeat until the requested number of particles has been generated.

Each rendered dot is a sample from the probability distribution. It is not a separate electron.

The two colors in hydrogen cloud mode show wavefunction phase. Positive and negative phase regions are important for chemistry and bonding, even though ordinary probability density is always nonnegative.

## Phase A and Phase B Colors

In hydrogen quantum cloud mode, the `Phase A` and `Phase B` color controls do not mean two different electrons. Hydrogen has only one electron. The colors mark the sign, or phase, of the real-valued wavefunction used for the visualization:

```text
Phase A: psi >= 0
Phase B: psi < 0
```

The measured probability is still:

```text
|psi|^2
```

So both positive and negative phase regions can have high probability. The sign matters because wavefunctions can interfere. In chemistry, matching or opposite phase between orbitals affects bonding and antibonding combinations.

For `s` orbitals, the phase usually appears as radial regions separated by nodes. For `p`, `d`, and `f` orbitals, phase often appears as different lobes.

In helium mode, the same two color controls are used more practically: they distinguish the two interleaved electron clouds in the approximate two-electron `1s^2` model. That color split is a visualization aid, not a claim that the electrons have fixed classical paths or permanent identities.

## Helium Model

Helium has two protons and two electrons. Its Hamiltonian contains an electron-electron repulsion term:

```text
H = -1/2 nabla_1^2 - 1/2 nabla_2^2 - 2/r_1 - 2/r_2 + 1/r_12
```

The `1/r_12` term couples the two electrons together. Because of that coupling, helium does not have the same exact closed-form orbital solution as hydrogen.

For the helium ground state, this simulator uses a standard variational approximation:

```text
psi_He approx phi_1s(r1; Z_eff) phi_1s(r2; Z_eff)
Z_eff = 27/16 = 1.6875
```

This means each electron is treated as if it moves in a hydrogen-like `1s` orbital around a partially screened nucleus. The true nuclear charge is `Z = 2`, but the other electron partially shields it, so the effective charge is lower.

The value `Z_eff = 27/16` comes from minimizing the expectation value of the helium Hamiltonian with a simple two-electron trial wavefunction made from hydrogen-like `1s` orbitals. It is a classic first variational estimate for helium.

In the app, helium mode renders two interleaved `1s` probability clouds. This visualizes the compact helium ground-state electron cloud, but it does not fully model electron correlation.

## What The Simulator Does Not Model

This is an educational visualizer, not a precision quantum chemistry package. It does not include:

- Full electron-electron correlation for helium.
- Spin, Pauli antisymmetry, or singlet/triplet state construction.
- Relativistic corrections.
- Fine structure, Lamb shift, or hyperfine effects.
- Time-dependent wavepacket evolution.
- External electric or magnetic fields.
- Numerical Hartree-Fock or configuration interaction calculations.

## Bohr Mode

Bohr mode is included as a simple historical comparison. It draws electrons moving on shell-like paths, but that is not how modern quantum mechanics describes atoms. The quantum cloud mode is the scientifically better representation.

## Code Map

The main math lives in `app.js`:

- `radial(n, l, r, zEff)` computes the radial hydrogen-like function.
- `angular(l, m, theta, phi)` computes the real angular part.
- `probability(n, l, m, r, theta, phi, zEff)` computes the sampling weight.
- `modelSettings(n, l)` selects hydrogen or helium approximation settings.
- `resample()` generates the probability-cloud points.

The visualization then projects the sampled 3D points onto the 2D canvas and colors them by phase or electron index.
