The most complicated feature by far was getting electrons to configure properly across shells. Mainly caused by the order in which electrons are added. I assume there are many ways to solve this problem but I felt a recursive solution was the most elegant.
DISCLAIMER: This doc is about mimicking a real world process with code NOT presenting completely accurate scientific information. I’m not an expert on this.
Setup
SPDF Blocks
The s, p, d, and f blocks indicate energy levels within each shell. Each energy level can hold a certain number of electrons (s 2) (p 6) (d 10) (f 14).
In real atoms, electrons are are added to the lowest energy level available (closest to the center).
In general, the patten is add to the highest available f then d then p then s blocks. However, as seen in the configuration order below, the order jumps between shells with the f being 3 shells deep by the time electrons get added to it. This meant I wouldn’t be adding to the Outermost shell every time.
SPDF Order
Each shell has a Maximum number of electrons based on the energy levels present. For Example shell 1 only has an s
block, while shell 3 has s, p, and d blocks.
Checking blocks
And since most shells don’t contain every energy level so checks for whether the shell is full also check the maximum number of particles
Adding Particles
Add helper
Helper method for handling the shell specific & Unity interactions
AddParticle
Actual method that takes care of the logic for configuring particles.
This method is always called on the Outer Shell and works it’s way down from there.
(skipping step 0 for the end)
Step 1: Fill the s block of OuterShell (n)
first, the s block of the outer shell is filled.
These will be the first 2 particles in the shell so somewhere else in the program an outer shell is created just before calling this method on it.
Step 2: Fill the f block of OuterShell.NextShell.NextShell (n-2)
Step 3: Fill the d block of OuterShell.NextShell (n-1)
Step 4: Fill the p block of OuterShell (n)
The next step will be Step 1 on a new OuterShell (n+1)
Step 0: Fill the p block of OuterShell.NextShell (n-1)
This step is necessary because electrons can be removed. Step 4 (the p block) on the previous shell must be fulfilled before completing Step 1 (the s block) on the current shell
Removing Particles
This is where things got really complicated. In the end it’s not that much code, but figuring this out took a lot of time and effort.
Remove helper
Just like when adding, Helper method for handling the shell specific & Unity interactions
Transfer helper
Helper method for moving particles between shells.
Uses the Add and Remove Helper functions
Fall up
Part of the Logic below. Transfers particles from lower energy levels of the f and d block into the current OuterShell
Remove Particle
Actual method that takes care of the logic for configuring particles.
This method is always called on the Outer Shell and works it’s way down from there.
Step 0: Remove from this shell
Fall Up is called to refill any s block electrons
Step 1: Remove from next shell
Keep checking down the shells for the removed particle