#!/bin/bash

# generate a table of ml1,ms1,ml2,ms2 values for two electrons with
# angular momentum "l"  consistent with the Pauli exclusion principle
# ouput: 
#	Microstates.txt  (table of ml1,ms1,ml2,ms2)
#	LS.txt (table of ML,MS, ordered by decreasing ML)
#       LS_Master.txt (=LS.txt)

# Usage: Generate2Electrons l 
#        l=1,2,3 for p,d,f

	#if angular momentum is missing, quit
[[ $# -eq 0 ]] && { echo "missing angular momentum (0,1,2..)"; exit -1;}

		#delete Orbital*.txt and reset counter 
[ -e Orbital.txt ] && { rm Orbit*.txt; echo "Orbital*.txt deleted";} 
echo "1" > VERSION                  #Orbital counter stored in this file

echo "M_L M_S	 		L	S"
awk -v l=$1 'BEGIN{
	for (x=-l; x<=l; x++){ ml[++j]=x}  #Generate ml array
        nl=length(ml)
	
        ms[1]=-0.5; ms[2]=+0.5;		  #Populate ms array
	ns=length(ms)
        
	for (i=1; i<=nl;i++)              #complete set of two quantum numbers [ml,ms]
          for (j=1; j<=ns; j++)		  
            A[++ind]= sprintf("%0.1d %3.1f",ml[i], ms[j]) 
        n=ind

        for (j=1; j<=n; j++)		#Microstates subject to Pauli principle
          for (k=j+1; k<=n; k++)        #and indistinguishability of electrons
             printf ("%8s %8s\n", A[j],A[k])
	}'  |                                               
	tee Microstates.txt | awk '{print  $1+$3,$2+$4}'  |
 	sort -k1nr  > LS.txt
	tr ' ' '\t' < LS.txt > LSt.txt
	paste Microstates.txt LSt.txt | tee LS_Master.txt
        
echo "LS.txt has $(wc -l < LS_Master.txt) microstates"