#!/bin/bash

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

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

[[ $# -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


echo "M_L M_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;		#generate ms array
	ns=length(ms)
        
	for (i=1; i<=nl;i++)		#generate microstates (pairs of (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++)	#generate all possible microstates
          for (k=j+1; k<=n; k++)	#subject to Pauli & indistinguisability
	     for (p=k+1; p<=n; p++)
                printf ("%8s %8s %8s\n", A[j],A[k],A[p])
	}'                                               | 
	tee Microstates.txt | awk '{print $1+$3+$5,$2+$4+$6}'  |
	sort -k1nr | tee LS.txt LS_Master.txt

echo "LS.txt has $(wc -l < LS_Master.txt) microstates"