#!/usr/bin/env python3

import sys, os, shutil
import numpy as np

for thetaDEG in [0, 30, 60, 90, 120, 150, 180]:
	for phiDEG in [0]:
		theta = thetaDEG * np.pi/180
		phi   = phiDEG * np.pi/180
		x = np.sin(theta) * np.cos(phi)
		y = np.sin(theta) * np.sin(phi)
		z = np.cos(theta)
		MAGMOMline = "MAGMOM = 0 0 3    0 0 3   18*0"
		SAXISline  = "SAXIS  = {} {} {}".format(x,y,z,x,y,z)
		
		#======== Make directory
		subdir = "THETA{:03d}_PHI{:03d}".format(thetaDEG, phiDEG)
		cmd = "mkdir -p {}".format(subdir)
		print (cmd)
		os.system (cmd)
		
		#======== Copy POSCAR, POTCAR, KPOINTS, script, INCAR into the working directory
		shutil.copy2 ("POSCAR", subdir+"/", follow_symlinks=True)
		shutil.copy2 ("POTCAR", subdir+"/", follow_symlinks=True)
		shutil.copy2 ("KPOINTS", subdir+"/", follow_symlinks=True)
		shutil.copy2 ("script", subdir+"/", follow_symlinks=True)
		shutil.copy2 ("INCAR", subdir+"/", follow_symlinks=True)
		#======== Append MAGMOMline and SAXISline to INCAR
		with open(subdir+"/INCAR", "a") as f:
			f.write (MAGMOMline + "\n")
			f.write (SAXISline + "\n")


