#!/usr/bin/env python

import os
import sys
import shutil

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from runtest_dirac import Filter, TestRun


test = TestRun(__file__, sys.argv)

f = Filter()
f.add(from_string = ' TOTAL ENERGY',
      num_lines   = 11,
      rel_tolerance   = 1.0e-8)

test.run(['he.inp'], ['he.xyz'], f, args='--outcmo')
if os.path.isfile('CHECKPOINT.h5'):
    ext = '.h5'
elif os.path.isfile('CHECKPOINT.noh5.tar.gz'):
    ext = '.noh5.tar.gz'
shutil.move('CHECKPOINT'+ext, 'X2CMAT_002'+ext)

test.run(['li.inp'], ['li.xyz'], f, args='--outcmo')
shutil.move('CHECKPOINT'+ext, 'X2CMAT_003'+ext)

test.run(['lihe-4c.inp'], ['lihe.mol'], f, args='--outcmo')
shutil.move('CHECKPOINT'+ext, 'X2CMAT_005'+ext)

# molecular U matrix (from molecular core h1^{4c}) + AMFI 2e-SO corrections
test.run(['molecular-x2c-amfi.inp'], ['lihe.mol'], f)

test.run(['atoms.inp'], ['atoms.xyz'],
                 args='--replace closed_occ="4" --replace open_e="5" --replace open_orb="6" --replace myatom="F" --outcmo')
shutil.move('CHECKPOINT'+ext, 'X2CMAT_009'+ext)

test.run(['atoms.inp'], ['atoms.xyz'],
                 args='--replace closed_occ="12" --replace open_e="4" --replace open_orb="6" --replace myatom="S" --outcmo')
shutil.move('CHECKPOINT'+ext, 'X2CMAT_016'+ext)

# molecular U matrix (from molecular core h1^{4c}) + AMFI 2e-SO corrections
test.run(['large-molecular-x2c-amfi.inp'], ['sf6.xyz'], f)

# atomic U matrices + AMFI 2e-SO corrections
test.run(['large-atomic-x2c-amfi.inp'], ['sf6.xyz'], f, args='--put="X2CMAT_009.h5 X2CMAT_016.h5"')

os.unlink('X2CMAT_002'+ext)
os.unlink('X2CMAT_003'+ext)
os.unlink('X2CMAT_005'+ext)
os.unlink('X2CMAT_009'+ext)
os.unlink('X2CMAT_016'+ext)

sys.exit(test.return_code)
