Пример #1
0
def test_screen_fake_sims():
    from generic import NexusError
    from simulation import Simulation
    from project_manager import ProjectManager

    from test_simulation_module import get_test_workflow, n_test_workflows

    sims = []
    for n in range(n_test_workflows):
        sims.extend(get_test_workflow(n).list())
    #end for

    pm = ProjectManager()
    pm.add_simulations(sims)

    pm.screen_fake_sims()

    s = sims[len(sims) // 2]
    s.fake_sim = True

    try:
        pm.screen_fake_sims()
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    Simulation.clear_all_sims()
Пример #2
0
def test_pw2qmcpack_get_result():
    from generic import NexusError,obj

    sim = get_pw2qmcpack_sim()
    
    try:
        sim.get_result('unknown',None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    result = sim.get_result('orbitals',None)

    result_ref = obj(
        h5file   = './runs/pwscf_output/pwscf.pwscf.h5',
        ptcl_xml = './runs/pwscf_output/pwscf.ptcl.xml',
        wfs_xml  = './runs/pwscf_output/pwscf.wfs.xml',
        )

    assert(object_eq(result,result_ref))

    clear_all_sims()
Пример #3
0
def test_pyscf_to_afqmc_get_result():
    from generic import NexusError,obj

    sim = get_pyscf_to_afqmc_sim()
    
    sim.input.output = 'afqmc.h5'

    try:
        sim.get_result('unknown',None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    result_ref = obj(
        h5_file = './runs/afqmc.h5',
        )

    result = sim.get_result('wavefunction',None)

    assert(object_eq(result,result_ref))

    result = sim.get_result('hamiltonian',None)

    assert(object_eq(result,result_ref))

    clear_all_sims()
Пример #4
0
def get_gamess_sim(type='rhf'):
    from machines import job
    from gamess import Gamess, generate_gamess, GamessInput
    from test_gamess_input import get_files

    sim = None

    Gamess.ericfmt = ''

    files = get_files()

    if type == 'rhf':
        gi_input = GamessInput(files['rhf.inp'])

        sim = generate_gamess(
            identifier='rhf',
            path='rhf',
            job=job(machine='ws1', cores=1),
            input=gi_input,
        )
    else:
        failed()
    #end if

    assert (sim is not None)
    assert (isinstance(sim, Gamess))

    return sim
Пример #5
0
def test_pw2qmcpack_incorporate_result():
    from generic import NexusError,obj
    from simulation import Simulation
    from test_pwscf_simulation import pseudo_inputs,get_pwscf_sim

    tpath = testing.setup_unit_test_output_directory('pwscf_simulation','test_check_result',**pseudo_inputs)

    other = Simulation()

    scf = get_pwscf_sim('scf')

    sim = get_pw2qmcpack_sim(path='scf')

    try:
        sim.incorporate_result('unknown',None,other)
        raise TestFailed
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    sim.incorporate_result('orbitals',None,scf)

    clear_all_sims()
    restore_nexus()
Пример #6
0
def test_pw2qmcpack_check_sim_status():
    import os
    from generic import NexusError, obj
    from nexus_base import nexus_core

    tpath = testing.setup_unit_test_output_directory(
        'qmcpack_converter_simulations',
        'test_pw2qmcpack_check_sim_status',
        divert=True)

    nexus_core.runs = ''

    sim = get_pw2qmcpack_sim()

    assert (sim.locdir.rstrip('/') == tpath.rstrip('/'))

    assert (not sim.finished)
    assert (not sim.failed)

    try:
        sim.check_sim_status()
        raise FailedTest
    except IOError:
        None
    except Exception as e:
        failed(str(e))
    #end try

    sim.create_directories()
    outfile = os.path.join(sim.locdir, sim.outfile)
    outfile_text = 'JOB DONE'
    out = open(outfile, 'w')
    out.write(outfile_text)
    out.close()
    assert (outfile_text in open(outfile, 'r').read())
    prefix = sim.input.inputpp.prefix
    outdir = sim.input.inputpp.outdir
    outdir_path = os.path.join(tpath, outdir)
    if not os.path.exists(outdir_path):
        os.makedirs(outdir_path)
    #end if
    filenames = [
        prefix + '.pwscf.h5', prefix + '.ptcl.xml', prefix + '.wfs.xml'
    ]
    for filename in filenames:
        filepath = os.path.join(tpath, outdir, filename)
        f = open(filepath, 'w')
        f.write('')
        f.close()
        assert (os.path.exists(filepath))
    #end for
    sim.job.finished = True

    sim.check_sim_status()

    assert (sim.finished)
    assert (not sim.failed)

    clear_all_sims()
    restore_nexus()
Пример #7
0
def test_get_result():
    from generic import NexusError,obj

    sim = get_quantum_package_sim()
    
    try:
        sim.get_result('unknown',None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    sim.input.run_control.save_for_qmcpack = True

    result = sim.get_result('orbitals',None)

    result_ref = obj(
        outfile = './runs/qp_savewf.out',
        )

    assert(object_eq(result,result_ref))

    clear_all_sims()
Пример #8
0
def test_convert4qmc_get_result():
    from generic import NexusError,obj

    sim = get_convert4qmc_sim()
    
    try:
        sim.get_result('unknown',None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    result = sim.get_result('orbitals',None)

    result_ref = obj(
        location = './runs/sample.wfj.xml',
        )

    assert(object_eq(result,result_ref))

    result = sim.get_result('particles',None)

    result_ref = obj(
        location = './runs/sample.structure.xml',
        )

    assert(object_eq(result,result_ref))

    clear_all_sims()
Пример #9
0
def get_pwscf_sim(type='scf'):
    from nexus_base import nexus_core
    from machines import job
    from pwscf import Pwscf,generate_pwscf

    nexus_core.runs = ''

    sim = None

    if type=='scf':
        sim = generate_pwscf(
            identifier   = 'scf',
            path         = 'scf',
            job          = job(machine='ws1',cores=1),
            input_type   = 'generic',
            calculation  = 'scf',
            input_dft    = 'lda', 
            ecutwfc      = 200,   
            conv_thr     = 1e-8, 
            nosym        = True,
            wf_collect   = True,
            system       = get_system(),
            pseudos      = ['C.BFD.upf'], 
            nogamma      = True,
            )
    else:
        failed()
    #end if

    assert(sim is not None)
    assert(isinstance(sim,Pwscf))

    return sim
Пример #10
0
def test_pyscf_to_afqmc_incorporate_result():
    import os
    from generic import NexusError,obj
    from simulation import Simulation
    from test_pyscf_simulation import get_pyscf_sim

    other = Simulation()

    scf = get_pyscf_sim()

    sim = get_pyscf_to_afqmc_sim()

    try:
        sim.incorporate_result('unknown',None,other)
        raise TestFailed
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    result = obj(
        chkfile = os.path.join(scf.locdir,'scf.chk'),
        )

    assert(sim.input.input==None)

    sim.incorporate_result('wavefunction',result,scf)

    assert(sim.input.input=='scf.chk')

    clear_all_sims()
Пример #11
0
def test_get_result():
    import os
    from generic import NexusError, obj
    from nexus_base import nexus_core

    tpath = testing.setup_unit_test_output_directory('gamess_simulation',
                                                     'test_get_result',
                                                     divert=True)

    nexus_core.runs = ''

    sim = get_gamess_sim('rhf')

    assert (sim.locdir.rstrip('/') == os.path.join(tpath, 'rhf').rstrip('/'))

    sim.create_directories()
    sim.write_inputs()
    if not os.path.exists(sim.imresdir):
        os.makedirs(sim.imresdir)
    #end if
    analyzer = sim.analyzer_type(sim)
    analyzer.save(os.path.join(sim.imresdir, sim.analyzer_image))

    try:
        sim.get_result('unknown', None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    result = sim.get_result('orbitals', None)

    result_ref = obj(
        location='rhf/rhf.out',
        mos=0,
        norbitals=0,
        outfile='rhf/rhf.out',
        scftyp='rohf',
        vec=None,
    )

    result.location = result.location.replace(tpath, '').lstrip('/')
    result.outfile = result.outfile.replace(tpath, '').lstrip('/')

    assert (object_eq(result, result_ref))

    clear_all_sims()
    restore_nexus()
Пример #12
0
def test_pyscf_to_afqmc_check_sim_status():
    import os
    from generic import NexusError, obj
    from nexus_base import nexus_core

    tpath = testing.setup_unit_test_output_directory(
        'qmcpack_converter_simulations',
        'test_pyscf_to_afqmc_check_sim_status',
        divert=True)

    nexus_core.runs = ''

    sim = get_pyscf_to_afqmc_sim()

    assert (sim.locdir.rstrip('/') == tpath.rstrip('/'))

    assert (not sim.finished)
    assert (not sim.failed)

    try:
        sim.check_sim_status()
        raise FailedTest
    except IOError:
        None
    except Exception as e:
        failed(str(e))
    #end try

    sim.input.output = 'afqmc.h5'

    sim.create_directories()
    outfile = os.path.join(sim.locdir, sim.outfile)
    outfile_text = '# Finished.'
    out = open(outfile, 'w')
    out.write(outfile_text)
    out.close()
    assert (outfile_text in open(outfile, 'r').read())
    output_file = os.path.join(sim.locdir, sim.input.output)
    f = open(output_file, 'w')
    f.write('')
    f.close()
    assert (os.path.exists(output_file))
    sim.job.finished = True

    sim.check_sim_status()

    assert (sim.finished)
    assert (not sim.failed)

    clear_all_sims()
    restore_nexus()
Пример #13
0
def test_get_result():
    from generic import NexusError

    tpath = testing.setup_unit_test_output_directory('pwscf_simulation','test_get_result',**pseudo_inputs)

    sim = get_pwscf_sim('scf')

    try:
        sim.get_result('unknown',None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    result  = sim.get_result('charge_density',None)
    result2 = sim.get_result('restart',None)

    assert(object_eq(result,result2))

    result_ref = dict(
        locdir        = 'scf',
        outdir        = 'scf/pwscf_output',
        location      = 'scf/pwscf_output/pwscf.save/charge-density.dat',
        spin_location = 'scf/pwscf_output/pwscf.save/spin-polarization.dat',
        )

    assert(set(result.keys())==set(result_ref.keys()))
    for k,path in result.items():
        path = path.replace(tpath,'').lstrip('/')
        assert(path==result_ref[k])
    #end for

    result = sim.get_result('orbitals',None)

    result_ref = dict(
        location = 'scf/pwscf_output/pwscf.wfc1',
        )

    assert(set(result.keys())==set(result_ref.keys()))
    for k,path in result.items():
        path = path.replace(tpath,'').lstrip('/')
        assert(path==result_ref[k])
    #end for

    clear_all_sims()
    restore_nexus()
Пример #14
0
def test_check_sim_status():
    import os
    tpath = testing.setup_unit_test_output_directory('pwscf_simulation','test_check_sim_status',**pseudo_inputs)

    sim = get_pwscf_sim('scf')

    assert(sim.locdir.rstrip('/')==os.path.join(tpath,'scf').rstrip('/'))
    if not os.path.exists(sim.locdir):
        os.makedirs(sim.locdir)
    #end if

    assert(not sim.finished)

    try:
        sim.check_sim_status()
    except IOError:
        None
    except Exception as e:
        failed(str(e))
    #end try

    assert(not sim.finished)

    out_path = os.path.join(tpath,'scf',sim.outfile)

    out_text = ''
    outfile = open(out_path,'w')
    outfile.write(out_text)
    outfile.close()
    assert(os.path.exists(out_path))

    sim.check_sim_status()

    assert(not sim.finished)

    out_text = 'JOB DONE'
    outfile = open(out_path,'w')
    outfile.write(out_text)
    outfile.close()
    assert(out_text in open(out_path,'r').read())

    sim.check_sim_status()

    assert(sim.finished)
    assert(not sim.failed)

    clear_all_sims()
    restore_nexus()
Пример #15
0
def test_get_result():
    import os
    from generic import NexusError, obj
    from nexus_base import nexus_core

    tpath = testing.setup_unit_test_output_directory('pyscf_simulation',
                                                     'test_get_result',
                                                     divert=True)

    nexus_core.runs = ''

    template_file = 'scf_template.py'
    template_text = 'template $chkfile'
    template_filepath = os.path.join(tpath, template_file)
    f = open(template_filepath, 'w')
    f.write(template_text)
    f.close()

    sim = get_pyscf_sim(
        prefix='scf',
        checkpoint='scf.chk',
        template=template_filepath,
    )

    try:
        sim.get_result('unknown', None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    result = sim.get_result('orbitals', None)

    assert (result.h5_file.replace(tpath, '').lstrip('/') == 'scf.h5')

    result = sim.get_result('wavefunction', None)

    assert (result.chkfile.replace(tpath, '').lstrip('/') == 'scf.chk')

    clear_all_sims()
    restore_nexus()
Пример #16
0
def test_check_sim_status():
    import os
    from generic import NexusError, obj
    from nexus_base import nexus_core

    tpath = testing.setup_unit_test_output_directory('gamess_simulation',
                                                     'test_check_sim_status',
                                                     divert=True)

    nexus_core.runs = ''

    sim = get_gamess_sim('rhf')

    assert (sim.locdir.rstrip('/') == os.path.join(tpath, 'rhf').rstrip('/'))

    assert (not sim.finished)
    assert (not sim.failed)

    try:
        sim.check_sim_status()
        raise FailedTest
    except IOError:
        None
    except Exception as e:
        failed(str(e))
    #end try

    sim.create_directories()
    outfile = os.path.join(sim.locdir, sim.outfile)
    outfile_text = 'EXECUTION OF GAMESS TERMINATED NORMALLY'
    out = open(outfile, 'w')
    out.write(outfile_text)
    out.close()
    assert (outfile_text in open(outfile, 'r').read())

    sim.check_sim_status()

    assert (sim.finished)
    assert (not sim.failed)

    clear_all_sims()
    restore_nexus()
Пример #17
0
def test_incorporate_result():
    from generic import NexusError
    from machines import job

    for cls,gen in get_class_generators():
        sim = gen(job=job(machine='ws1',cores=1))
        assert(isinstance(sim,cls))
        try:
            sim.incorporate_result('anything',None,None)
            raise FailedTest
        except NexusError:
            None
        except FailedTest:
            failed()
        except Exception as e:
            failed(str(e))
        #end try
    #end for

    clear_all_sims()
Пример #18
0
def test_check_sim_status():
    import os
    from generic import NexusError,obj
    from nexus_base import nexus_core

    tpath = testing.setup_unit_test_output_directory('quantum_package_simulation','test_check_sim_status',divert=True)

    nexus_core.runs = ''

    sim = get_quantum_package_sim()

    assert(sim.locdir.rstrip('/')==tpath.rstrip('/'))

    assert(not sim.finished)
    assert(not sim.failed)

    try:
        sim.check_sim_status()
        raise FailedTest
    except IOError:
        None
    except Exception as e:
        failed(str(e))
    #end try

    sim.create_directories()
    outfile = os.path.join(sim.locdir,sim.outfile)
    outfile_text = '* SCF energy'
    out = open(outfile,'w')
    out.write(outfile_text)
    out.close()
    assert(outfile_text in open(outfile,'r').read())
    sim.job.finished = True

    sim.check_sim_status()

    assert(sim.finished)
    assert(not sim.failed)

    clear_all_sims()
    restore_nexus()
Пример #19
0
def get_qmcpack_sim(type='rsqmc', **kwargs):
    from machines import job
    from qmcpack import Qmcpack, generate_qmcpack

    if type == 'rsqmc':
        tiling = kwargs.pop('tiling', (1, 1, 1))

        sim = generate_qmcpack(job=job(machine='ws1', cores=1),
                               system=get_system(tiling=tiling),
                               **kwargs)
    elif type == 'afqmc':
        sim = generate_qmcpack(job=job(machine='ws1', cores=1),
                               input_type='basic_afqmc',
                               **kwargs)
    else:
        failed()
    #end if

    assert (isinstance(sim, Qmcpack))

    return sim
Пример #20
0
def test_resolve_file_collisions():
    from generic import NexusError
    from simulation import Simulation
    from project_manager import ProjectManager

    from test_simulation_module import get_test_workflow, n_test_workflows

    divert_nexus_log()

    sims = []
    for n in range(n_test_workflows):
        sims.extend(get_test_workflow(n).list())
    #end for

    pm = ProjectManager()
    pm.add_simulations(sims)

    pm.resolve_file_collisions()

    s1 = sims[len(sims) // 2]
    s2 = sims[len(sims) // 2 + 1]

    s2.locdir = s1.locdir
    s2.outfile = s1.outfile

    try:
        pm.resolve_file_collisions()
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    restore_nexus_log()

    Simulation.clear_all_sims()
Пример #21
0
def test_incorporate_result():
    import os
    from generic import NexusError, obj

    tpath = testing.setup_unit_test_output_directory(
        'gamess_simulation', 'test_incorporate_result')

    sim = get_gamess_sim('rhf')

    try:
        sim.incorporate_result('unknown', None, None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    result = obj(
        vec='vec text',
        norbitals=10,
    )

    input = sim.input

    assert ('vec' not in input)
    assert ('norb' not in input.guess)
    assert ('prtmo' not in input.guess)

    sim.incorporate_result('orbitals', result, None)

    assert (input.vec.text == 'vec text')
    assert (input.guess.guess == 'moread')
    assert (input.guess.norb == 10)
    assert (input.guess.prtmo == True)

    clear_all_sims()
Пример #22
0
def test_incorporate_result():
    from generic import NexusError,obj
    from machines import job
    from simulation import Simulation
    from gamess import generate_gamess,Gamess

    Gamess.ericfmt = ''

    other = Simulation()

    gms = generate_gamess(
        job = job(machine='ws1',cores=1),
        )

    Gamess.ericfmt = None

    sim = get_quantum_package_sim()
    
    try:
        sim.get_result('unknown',None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try
    
    try:
        sim.get_result('orbitals',other)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    clear_all_sims()
Пример #23
0
def test_extensions():
    # test obj functions
    from generic import obj,NexusError

    # make a simple object
    o = obj(
        a = 1,
        b = 'b',
        c = (1,1,1),
        )
    o[3,4,5] = (5,6,7)

    # test member values
    assert(o.a==1)
    assert(o.b=='b')
    assert(o.c==(1,1,1))
    assert(o[3,4,5]==(5,6,7))

    # test member presence and length
    assert('a' in o)
    assert(2 not in o)
    assert(len(o)==4)

    # test list interface
    vals = [3,'t',6.4,(4,3,2)]
    l = list()
    lo = obj()
    for v in vals:
        l.append(v)
        lo.append(v)
    #end for
    assert(len(l)==len(lo))
    for i in range(len(l)):
        assert(l[i]==lo[i])
    #end for


    # test representations
    # test list representation
    l2 = lo.list()
    assert(isinstance(l2,list))
    assert(l==l2)
    l2 = lo.list_optional(1,3)
    assert(l2==['t',(4,3,2)])
    l2 = o.list_optional('b',(3,4,5))
    assert(l2==['b',(5,6,7)])

    # test tuple representation
    t = lo.tuple()
    assert(isinstance(t,tuple))
    assert(t==tuple(l))
    d = dict(
        a = 1,
        b = 'b',
        c = (1,1,1),
        )
    d[3,4,5] = (5,6,7)

    # test dict representation
    do = o.dict()
    assert(isinstance(do,dict))
    assert(do==d)
    d2 = d.copy()
    d2['d'] = d
    o2 = o.copy()
    o2.d = o
    d2o = o2.to_dict()
    assert(d2o==d2)

    # test obj representation
    o2 = o.obj()
    assert(isinstance(o2,obj))
    assert(id(o2)!=id(o))
    assert(object_eq(o2,o))
    o2 = o.copy().to_obj()
    assert(object_eq(o2,o))
    
    # test list extensions
    # test first
    assert(lo.first()==lo[0])

    # test last
    assert(lo.last()==lo[3])

    # test select_random
    v = lo.select_random()
    assert(v in lo.list())


    # test dict extensions
    # test random_key
    k = o.random_key()
    assert(k in o)
    o2 = obj()
    assert(o2.random_key() is None)

    # test set
    o2 = o.copy()
    o2.set(
        b = 'b2',
        d = ('a','b','c'),
        )
    assert(o2.b=='b2')
    assert(o2.d==tuple('abc'))
    o1 = obj(a=1,b=2)
    o2 = obj(c=3,d=4)
    o3 = obj(e=5,f=6)
    o4 = obj()
    o4.set(o1,o2,o3)
    for on in (o1,o2,o3):
        for k,v in on.items():
            assert(o4[k]==v)
        #end for
    #end for

    # test set optional
    o2 = o.copy()
    o2.set_optional(
        b = 'b2',
        d = ('a','b','c'),
        )
    assert(o2.b=='b')
    assert(o2.d==tuple('abc'))
    o1 = obj(a=1,b=2)
    o2 = obj(c=3,d=4)
    o3 = obj(e=5,f=6)
    o4 = obj()
    o4.set_optional(o1,o2,o3)
    for on in (o1,o2,o3):
        for k,v in on.items():
            assert(o4[k]==v)
        #end for
    #end for

    # test get
    assert(o.get('c')==(1,1,1))
    assert('d' not in o)
    assert(o.get('d') is None)

    # test get optional (identical to get)
    assert(o.get_optional('c')==(1,1,1))
    assert(o.get_optional('d') is None)

    # test get required
    assert(o.get_required('c')==(1,1,1))
    try:
        val = o.get_required('d')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test delete
    o2 = o.copy()
    assert(o2.delete('c')==(1,1,1))
    assert('c' not in o2)
    keys = 'a','b','c',(3,4,5)
    vals = [1,'b',(1,1,1),(5,6,7)]
    o2 = o.copy()
    assert(o2.delete(*keys)==vals)
    assert(len(o2)==0)
    for k in keys:
        assert(k not in o2)
    #end for
    o2 = o.copy()
    assert(o2.delete(keys)==vals)
    assert(len(o2)==0)
    for k in keys:
        assert(k not in o2)
    #end for
    o2 = o.copy()
    try:
        o2.delete('a','d')
        raise FailedTest
    except KeyError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test delete optional
    o2 = o.copy()
    o2.delete_optional('c')
    assert('c' not in o2)
    assert('d' not in o2)
    o2.delete_optional('d')
    assert('d' not in o2)

    # test delete required
    o2 = o.copy()
    o2.delete_required('c')
    assert('c' not in o2)
    try:
        o2.delete_required('d')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test add
    o2 = obj()
    o2.add('a',1)
    try: # add unhashable type
        o2.add([3,4,5],6)
        raise FailedTest
    except TypeError:
        None
    #end if
    
    # test add optional
    o2 = obj(a=1)
    o2.add_optional('a',2)
    o2.add_optional('b',3)
    assert(o2.a==1)
    assert(o2.b==3)


    # test transfer/copy/move functions
    dref = dict(
        a = 1,
        b = 'b',
        c = (1,1,1),
        d = dict(
            e = 5.4,
            f = [3.3,4.5],
            ),
        )
    dref[3,4,5] = (5,6,7)

    # test transfer from
    o = obj()
    o.transfer_from(dref)
    assert(o.to_dict()==dref)
    assert(id(o.d)==id(dref['d']))

    o = obj()
    o.transfer_from(dref,copy=True)
    assert(o.to_dict()==dref)
    assert(id(o.d)!=id(dref['d']))

    osmall = obj(b='b',c=(1,1,1))
    osmall[3,4,5] = (5,6,7)
    o = obj()
    oref = obj(dref)
    assert(oref.to_dict()==dref)
    o.transfer_from(oref,keys=['b','c',(3,4,5)])
    assert(object_eq(o,osmall))

    o = obj(a=6,b=7)
    o.transfer_from(oref,overwrite=False)
    assert(object_neq(o,oref))
    o.transfer_from(oref,overwrite=True)
    assert(object_eq(o,oref))
    assert(o.to_dict()==dref)

    o = obj()
    try:
        o.transfer_from(oref,keys=['a','x'])
        raise FailedTest
    except KeyError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test transfer to
    o = obj()
    oref.transfer_to(o)
    assert(object_eq(o,oref))
    assert(id(o.d)==id(oref.d))

    o = obj()
    oref.transfer_to(o,copy=True)
    assert(object_eq(o,oref))
    assert(id(o.d)!=id(oref.d))

    o = obj()
    oref.transfer_to(o,keys=['b','c',(3,4,5)])
    assert(object_eq(o,osmall))

    o = obj(a=6,b=7)
    oref.transfer_to(o,overwrite=False)
    assert(object_neq(o,oref))
    oref.transfer_to(o,overwrite=True)
    assert(object_eq(o,oref))

    o = obj()
    try:
        oref.transfer_to(o,keys=['a','x'])
        raise FailedTest
    except KeyError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test move from
    d2 = dref.copy()
    o = obj()
    o.move_from(d2)
    assert(len(d2)==0)
    assert(object_eq(o,oref))

    o2 = oref.copy()
    o = obj()
    o.move_from(o2)
    assert(len(o2)==0)
    assert(object_eq(o,oref))

    osmall2 = oref.copy()
    del osmall2.b
    del osmall2.c
    del osmall2[3,4,5]
    o2 = oref.copy()
    o = obj()
    o.move_from(o2,keys=['b','c',(3,4,5)])
    assert(object_eq(o,osmall))
    assert(object_eq(o2,osmall2))

    o2 = oref.copy()
    o = obj()
    o.move_from_optional(o2,keys=['b','c',(3,4,5),'alpha','beta'])
    assert(object_eq(o,osmall))
    assert(object_eq(o2,osmall2))

    o2 = oref.copy()
    o = obj()
    try:
        o.move_from(o2,keys=['a','x'])
        raise FailedTest
    except KeyError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test move to
    o2 = oref.copy()
    d = dict()
    o2.move_to(d)
    assert(len(o2)==0)
    assert(d==dref)

    o2 = oref.copy()
    o = obj()
    o2.move_to(o)
    assert(len(o2)==0)
    assert(object_eq(o,oref))

    o2 = oref.copy()
    o = obj()
    o2.move_to(o,keys=['b','c',(3,4,5)])
    assert(object_eq(o,osmall))
    assert(object_eq(o2,osmall2))

    o2 = oref.copy()
    o = obj()
    o2.move_to_optional(o,keys=['b','c',(3,4,5),'alpha','beta'])
    assert(object_eq(o,osmall))
    assert(object_eq(o2,osmall2))

    o2 = oref.copy()
    o = obj()
    try:
        o2.move_to(o,keys=['a','x'])
        raise FailedTest
    except KeyError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test copy from
    o = obj()
    o.copy_from(dref)
    assert(o.to_dict()==dref)
    assert(id(o.d)!=id(dref['d']))

    o = obj()
    o.copy_from(dref,deep=False)
    assert(o.to_dict()==dref)
    assert(id(o.d)==id(dref['d']))

    osmall = obj(b='b',c=(1,1,1))
    osmall[3,4,5] = (5,6,7)
    o = obj()
    oref = obj(dref)
    assert(oref.to_dict()==dref)
    o.copy_from(oref,keys=['b','c',(3,4,5)])
    assert(object_eq(o,osmall))

    o = obj()
    try:
        o.copy_from(oref,keys=['a','x'])
        raise FailedTest
    except KeyError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test copy to
    o = obj()
    oref.copy_to(o)
    assert(object_eq(o,oref))
    assert(id(o.d)!=id(oref.d))

    o = obj()
    oref.copy_to(o,deep=False)
    assert(object_eq(o,oref))
    assert(id(o.d)==id(oref.d))

    o = obj()
    oref.copy_to(o,keys=['b','c',(3,4,5)])
    assert(object_eq(o,osmall))

    o = obj()
    try:
        oref.copy_to(o,keys=['a','x'])
        raise FailedTest
    except KeyError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test extract
    o = oref.copy()
    o2 = o.extract()
    assert(len(o)==0)
    assert(object_eq(o2,oref))

    o = oref.copy()
    o2 = o.extract(['b','c',(3,4,5)])
    assert(object_eq(o2,osmall))
    assert(object_eq(o,osmall2))

    o = oref.copy()
    o2 = o.extract_optional(['b','c',(3,4,5),'alpha','beta'])
    assert(object_eq(o2,osmall))
    assert(object_eq(o,osmall2))


    # test check_required
    oref.check_required(['a','d',(3,4,5)])
    try:
        oref.check_required(['alpha','beta'])
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test check_types
    types = dict(
        a = int,
        b = str,
        c = tuple,
        d = dict,
        )
    types[3,4,5] = tuple
    oref.check_types(types)

    types['b'] = int
    try:
        oref.check_types(types)
        raise FailedTest
    except:
        None
    #end try

    types['b'] = str
    types['alpha'] = float
    types['beta'] = list
    oref.check_types_optional(types)

    # test shallow_copy
    class DerivedObj(obj):
        None
    #end class DerivedObj
    do = DerivedObj(
        a = 1,
        b = 'b',
        c = (1,1,1),
        )
    do[3,4,5] = (5,6,7)
    do2 = do.shallow_copy()
    assert(isinstance(do2,DerivedObj))
    assert(object_eq(do2,do))
    assert(id(do2.c)==id(do.c))

    # test inverse
    oi = do.inverse()
    assert(set(oi.keys())==set(do.values()))
    assert(set(oi.values())==set(do.keys()))

    assert(oi[1]=='a')
    assert(oi.b=='b')
    assert(oi[1,1,1]=='c')
    assert(oi[5,6,7]==(3,4,5))

    # test path operations
    # test path exists
    o2 = obj()
    o2.this = obj()
    o2.this.new = obj()
    o2.this.new.variable = 'here'
    path1 = ['this','new','variable']
    path2 = 'this/new/variable'
    assert(o2.path_exists(path1))
    assert(o2.path_exists(path2))

    # test set path
    o3 = obj()
    o3.set_path(path1,'here')
    assert(o3.path_exists(path1))
    assert(o3.path_exists(path2))
    assert(object_eq(o2,o3))
    o4 = obj()
    o4.set_path(path2,'here')
    assert(o4.path_exists(path1))
    assert(o4.path_exists(path2))
    assert(object_eq(o3,o4))
    
    # test get path
    assert(o2.get_path(path1)=='here')
    assert(o2.get_path(path2)=='here')

    # test serial
    o = obj(
        a = obj(
            a0 = 0,
            a1 = obj(
                a10 = 1,
                ),
            ),
        b = obj(
            b0 = 0,
            b1 = obj(
                b10 = 1,
                ),
            b2 = obj(
                b20 = obj(
                    b200 = 2,
                    ),
                b21 = obj(
                    b210 = obj(
                        b2100 = 3,
                        ),
                    ),
                ),
            ),
        c = obj(
            c0 = 0,
            c1 = obj(
                c10 = 1,
                ),
            c2 = obj(
                c20 = obj(
                    c200 = 2,
                    ),
                c21 = obj(
                    c210 = obj(
                        c2100 = 3,
                        ),
                    ),
                ),
            c3 = obj(
                c30 = obj(
                    c300 = obj(
                        c3000 = obj(
                            c30000 = 4,
                            ),
                        ),
                    c301 = obj(
                        c3010 = obj(
                            c30100 = obj(
                                c301000 = 5,
                                ),
                            ),
                        ),
                    ),
                c31 = obj(
                    c310 = obj(
                        c3100 = obj(
                            c31000 = obj(
                                c310000 = obj(
                                    c3100000 = 6,
                                    ),
                                ),
                            ),
                        ),
                    c311 = obj(
                        c3110 = obj(
                            c31100 = obj(
                                c311000 = obj(
                                    c3110000 = obj(
                                        c3110000 = 7,
                                        )
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        )

    oref = obj({
        'a/a0':0,
        'a/a1/a10':1,
        'b/b0':0,
        'b/b1/b10':1,
        'b/b2/b20/b200':2,
        'b/b2/b21/b210/b2100':3,
        'c/c0':0,
        'c/c1/c10':1,
        'c/c2/c20/c200':2,
        'c/c2/c21/c210/c2100':3,
        'c/c3/c30/c300/c3000/c30000':4,
        'c/c3/c30/c301/c3010/c30100/c301000':5,
        'c/c3/c31/c310/c3100/c31000/c310000/c3100000':6,
        'c/c3/c31/c311/c3110/c31100/c311000/c3110000/c3110000':7,
        })

    for k,v in oref.items():
        assert(o.get_path(k)==v)
    #end for
    o2 = o.serial()
    assert(object_eq(o2,oref))
Пример #24
0
def test_intrinsics():
    # test object_interface functions
    import os
    from generic import obj,object_interface
    from generic import generic_settings,NexusError
    from numpy import array,bool_

    tpath = testing.setup_unit_test_output_directory('generic','test_intrinsics')

    # test object set/get
    # make a simple object
    o = obj()
    o.a = 1
    o.b = 'b'
    o['c'] = (1,1,1)
    o[3,4,5] = (5,6,7)

    # test member values
    assert(o.a==1)
    assert(o.b=='b')
    assert(o.c==(1,1,1))
    assert(o[3,4,5]==(5,6,7))

    # test member presence and length
    assert('a' in o)
    assert(2 not in o)
    assert(len(o)==4)
    del o.a
    assert('a' not in o)
    assert(len(o)==3)
    try:
        del o.d
        raise FailedTest
    except AttributeError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test add/access failures
    try: # add unhashable type
        o[{1:2,3:4}] = 5
        raise FailedTest
    except TypeError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try
    try: # access missing member
        v = o.d
        raise FailedTest
    except AttributeError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try
    try: # access missing member
        v = o['d']
        raise FailedTest
    except KeyError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test iterability
    # test list-like iterability
    l = list()
    for v in o:
        l.append(v)
    #end for
    l = set(l)
    l2 = {'b', (1, 1, 1), (5, 6, 7)}
    assert(l2==l)

    # test dict-like iterability
    d = dict()
    for k,v in o.items():
        d[k] = v
    #end for
    o2 = obj()
    o2.__dict__ = d
    assert(object_eq(o,o2))
    assert(set(o.keys())==set(d.keys()))
    assert(set(o.values())==set(d.values()))
    assert(set(o.items())==set(d.items()))

    # test repr
    ro = '''
  b                     str                 
  c                     tuple               
  (3, 4, 5)             tuple               
'''
    assert(repr(o)==ro[1:])
    o2 = obj(
        a = obj(a1=1,a2=2,a3=3),
        b = obj(b1='b1',b2='b2'),
        c = obj(c1=5,c2=('a',3,4)),
        d = array([3,4,5],dtype=int),
        )
    ro2 = '''
  a                     obj                 
  b                     obj                 
  c                     obj                 
  d                     ndarray             
'''
    assert(repr(o2)==ro2[1:])

    # test str
    so = '''
  b               = b
  c               = (1, 1, 1)
  (3, 4, 5)       = (5, 6, 7)
'''
    assert(str(o)==so[1:])
    so2 = '''
  d               = [3 4 5]
  a
    a1              = 1
    a2              = 2
    a3              = 3
  end a
  b
    b1              = b1
    b2              = b2
  end b
  c
    c1              = 5
    c2              = ('a', 3, 4)
  end c
'''
    assert(str(o2)==so2[1:])
    o3 = o2

    # test tree
    #   not committed to output, only check execution
    assert(isinstance(o2.tree(),str))
    assert(isinstance(o2.tree(depth=1),str))
    assert(isinstance(o2.tree(types=True),str))
    assert(isinstance(o2.tree(all=True),str))
    assert(isinstance(o2.tree(nindent=2),str))

    # test deepcopy
    o2 = o.copy()
    assert(id(o)!=id(o2))
    assert(object_eq(o,o2))
    o2.a=1
    assert(object_neq(o,o2))
    
    # test eq
    assert(o==o2)
    o4 = o3.copy()
    v = o3==o4
    assert(isinstance(v,bool_))
    assert(bool(v))
    assert(object_eq(o3,o4))

    # test clear
    o2.clear()
    assert(len(o2)==0)
    assert('a' not in o2)

    # test save/load
    save_file = os.path.join(tpath,'o.p')
    o.save(save_file)
    o2 = obj()
    o2.load(save_file)
    assert(object_eq(o,o2))

    # test class-level set/get methods
    class objint(object_interface):
        a = 1
        b = 'b'
        c = (1,1,1)
    #end class objint

    # test class_has
    assert(objint.class_has('c'))

    # test class_get
    assert(objint.class_get('c')==(1,1,1))
    try:
        val = objint.class_get('d')
        raise FailedTest
    except AttributeError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # test class_keys
    ck = objint.class_keys()
    for v in 'abc':
        assert(v in ck)
    #end for

    # test class_set_single
    objint.class_set_single('d',1.34)
    assert(objint.class_has('d'))

    # test class_set
    objint.class_set(
        e = 45,
        f = 'a phrase',
        g = {4:6,'a':2}
        )
    for v in 'efg':
        assert(objint.class_has(v))
    #end for

    # test class_set_optional
    objint.class_set_optional(h=2)
    assert(objint.class_has('h'))
    assert(objint.class_get('h')==2)
    objint.class_set_optional(a=6)
    assert(objint.class_get('a')==1)
    

    # test logging functions

    # test open log, write, close log
    o = obj()
    o.open_log('log.out')
    s = 'log output'
    o.write(s)
    o.close_log()
    f = open('log.out','r')
    so = f.read()
    f.close()
    os.remove('log.out')
    assert(so==s)

    # send messages to object rather than stdout
    divert_nexus_log()
    logfile = object_interface._logfile

    #   simple message
    class DerivedObj(obj):
        None
    #end class DerivedObj
    o = DerivedObj()
    s = 'simple message'
    logfile.reset()
    o.log(s)
    assert(logfile.s==s+'\n')

    #   list of items
    items = ['a','b','c',1,2,3]
    logfile.reset()
    o.log(*items)
    assert(logfile.s=='a b c 1 2 3 \n')

    #   message with indentation
    s = 'a message\nwith indentation'
    logfile.reset()
    o.log(s,indent='  ')
    assert(logfile.s=='  a message\n  with indentation\n')

    logfile.reset()
    o.log(s,indent='msg: ')
    assert(logfile.s=='msg: a message\nmsg: with indentation\n')
    
    #   writing to separate log files
    logfile2 = FakeLog()
    s1 = 'message to log 1'
    s2 = 'message to log 2'
    logfile.reset()
    logfile2.reset()
    o.log(s1)
    assert(logfile.s==s1+'\n')
    assert(logfile2.s=='')

    logfile.reset()
    logfile2.reset()
    o.log(s2,logfile=logfile2)
    assert(logfile.s=='')
    assert(logfile2.s==s2+'\n')
    

    # test warn
    logfile.reset()
    s = 'this is a warning'
    o.warn(s)
    so = '''
  DerivedObj warning:
    this is a warning
'''
    assert(logfile.s==so)
    logfile.reset()
    s = 'this is a warning'
    o.warn(s,header='Special')
    so = '''
  Special warning:
    this is a warning
'''
    assert(logfile.s==so)

    # test error
    #   in testing environment, should raise an error
    try:
        o.error('testing environment')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try
    #   in standard/user environment, should print message
    generic_settings.raise_error = False
    logfile.reset()
    o.error('this is an error',exit=False)
    so = '''
  DerivedObj error:
    this is an error
'''
    assert(logfile.s==so)
    logfile.reset()
    o.error('this is an error',header='User',exit=False)
    so = '''
  User error:
    this is an error
'''
    assert(logfile.s==so)
    generic_settings.raise_error = True
    #   in testing environment, should raise an error
    try:
        o.error('testing environment')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # restore logging function
    restore_nexus_log()
Пример #25
0
def test_get_result():
    import os
    import shutil
    from numpy import array
    from generic import obj, NexusError

    tpath = testing.setup_unit_test_output_directory('vasp_simulation',
                                                     'test_get_result',
                                                     **pseudo_inputs)

    sim = setup_vasp_sim(tpath, identifier='diamond', files=True)

    try:
        sim.get_result('unknown', None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    pcfile = os.path.join(tpath, 'diamond_POSCAR')
    ccfile = os.path.join(tpath, sim.identifier + '.CONTCAR')

    assert (sim.locdir.strip('/') == tpath.strip('/'))

    shutil.copy2(pcfile, ccfile)

    assert (os.path.exists(ccfile))

    result = sim.get_result('structure', None)

    result_ref = obj(structure=obj(
        axes=array([[3.57, 3.57, 0.], [0., 3.57, 3.57], [3.57, 0., 3.57]],
                   dtype=float),
        background_charge=0,
        bconds=array([]),
        center=array([3.57, 3.57, 3.57], dtype=float),
        dim=3,
        elem=array([
            'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C',
            'C', 'C', 'C'
        ],
                   dtype=object),
        folded_structure=None,
        frozen=None,
        kaxes=array([[0.87999794, 0.87999794, -0.87999794],
                     [-0.87999794, 0.87999794, 0.87999794],
                     [0.87999794, -0.87999794, 0.87999794]],
                    dtype=float),
        kpoints=array([], dtype=float),
        kweights=array([], dtype=float),
        mag=None,
        pos=array([[0., 0., 0.], [0.8925, 0.8925, 0.8925], [1.785, 1.785, 0.],
                   [2.6775, 2.6775, 0.8925], [0., 1.785, 1.785],
                   [0.8925, 2.6775, 2.6775], [1.785, 3.57, 1.785],
                   [2.6775, 4.4625, 2.6775], [1.785, 0., 1.785],
                   [2.6775, 0.8925, 2.6775], [3.57, 1.785, 1.785],
                   [4.4625, 2.6775, 2.6775], [1.785, 1.785, 3.57],
                   [2.6775, 2.6775, 4.4625], [3.57, 3.57, 3.57],
                   [4.4625, 4.4625, 4.4625]],
                  dtype=float),
        scale=1.0,
        units='A',
    ), )

    assert (check_object_eq(result, result_ref))

    clear_all_sims()
    restore_nexus()
Пример #26
0
def test_unavailable():
    from generic import generic_settings, NexusError
    from developer import Void
    from developer import unavailable, available

    gre = generic_settings.raise_error
    generic_settings.raise_error = True

    try:
        import keyword
    except:
        keyword = unavailable('keyword')
    #end try

    assert (not isinstance(keyword, Void))
    assert (available(keyword))

    try:
        import an_unavailable_module
    except:
        an_unavailable_module = unavailable('an_unavailable_module')
    #end try

    assert (isinstance(an_unavailable_module, Void))
    assert (not available(an_unavailable_module))

    try:
        from an_unavailable_module import a, b, c, d, e, f, g
    except:
        a, b, c, d, e, f, g = unavailable('an_unavailable_module', 'a', 'b',
                                          'c', 'd', 'e', 'f', 'g')
    #end try

    void_imports = an_unavailable_module, b, c, d, e, f, g

    for v in void_imports:
        assert (isinstance(v, Void))
        assert (not available(v))
    #end for

    operations = [
        dir,
        len,
        repr,
        str,
        complex,
        int,
        float,
        lambda v: v == 0,
        lambda v: v != 0,
        lambda v: v > 0,
        lambda v: v < 0,
        lambda v: v >= 0,
        lambda v: v <= 0,
        lambda v: v(),
        lambda v: v.a,
        lambda v: v['a'],
        lambda v: setattr(v, 'a', 0),
        lambda v: getattr(v, 'a'),
        lambda v: delattr(v, 'a'),
        lambda v: v + 0,
        lambda v: v - 0,
        lambda v: v * 0,
        lambda v: v / 0,
        lambda v: v % 0,
        lambda v: v & 0,
        lambda v: v | 0,
    ]
    for op in operations:
        for v in void_imports:
            try:
                op(v)
                raise FailedTest
            except NexusError:
                None
            except FailedTest:
                failed()
            except Exception as e:
                failed(str(e))
            #end try
        #end for
    #end for

    generic_settings.raise_error = gre
Пример #27
0
def test_logging():
    from generic import log,message,warn,error
    from generic import generic_settings,NexusError

    # send messages to object rather than stdout
    divert_nexus_log()

    logfile = generic_settings.devlog

    # test log
    #   simple message
    s = 'simple message'
    logfile.reset()
    log(s)
    assert(logfile.s==s+'\n')

    #   list of items
    items = ['a','b','c',1,2,3]
    logfile.reset()
    log(*items)
    assert(logfile.s=='a b c 1 2 3 \n')

    #   message with indentation
    s = 'a message\nwith indentation'
    logfile.reset()
    log(s,indent='  ')
    assert(logfile.s=='  a message\n  with indentation\n')

    logfile.reset()
    log(s,indent='msg: ')
    assert(logfile.s=='msg: a message\nmsg: with indentation\n')
    
    #   writing to separate log files
    logfile2 = FakeLog()
    s1 = 'message to log 1'
    s2 = 'message to log 2'
    logfile.reset()
    logfile2.reset()
    log(s1)
    assert(logfile.s==s1+'\n')
    assert(logfile2.s=='')

    logfile.reset()
    logfile2.reset()
    log(s2,logfile=logfile2)
    assert(logfile.s=='')
    assert(logfile2.s==s2+'\n')
    

    # test warn
    logfile.reset()
    s = 'this is a warning'
    warn(s)
    so = '''
  warning:
    this is a warning
'''
    assert(logfile.s==so)
    logfile.reset()
    s = 'this is a warning'
    warn(s,header='Special')
    so = '''
  Special warning:
    this is a warning
'''
    assert(logfile.s==so)

    # test error
    #   in testing environment, should raise an error
    try:
        error('testing environment')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try
    #   in standard/user environment, should print message
    generic_settings.raise_error = False
    logfile.reset()
    error('this is an error',header='User',exit=False)
    so = '''
  User error:
    this is an error
'''
    assert(logfile.s==so)
    generic_settings.raise_error = True
    #   in testing environment, should raise an error
    try:
        error('testing environment')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    restore_nexus_log()
Пример #28
0
def test_convert4qmc_incorporate_result():
    from generic import NexusError,obj
    from simulation import Simulation
    from gamess import Gamess
    from pyscf_sim import Pyscf
    from quantum_package import QuantumPackage
    from test_gamess_simulation import get_gamess_sim
    from test_pyscf_simulation import get_pyscf_sim
    from test_quantum_package_simulation import get_quantum_package_sim

    other = Simulation()

    gms = get_gamess_sim('rhf')
    Gamess.ericfmt = None
    gms_result = obj(
        location        = './rhf/rhf.out',
        mos             = 0,
        norbitals       = 0,
        outfile         = './rhf/rhf.out',
        scftyp          = 'rohf',
        vec             = None,
        )

    pscf = get_pyscf_sim()
    pscf_result = obj(
        h5_file = './scf.h5',
        )

    qp = get_quantum_package_sim()
    QuantumPackage.qprc = None
    qp_result = obj(
        outfile = './qp_savewf.out',
        )

    sim_start = get_convert4qmc_sim()

    sim = sim_start.copy()
    try:
        sim.incorporate_result('unknown',None,other)
        raise TestFailed
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # incorporate orbitals from gamess
    sim = sim_start.copy()

    assert(sim.input_code is None)
    assert(sim.input.gamess_ascii is None)
    assert(sim.job.app_command=='convert4qmc')

    sim.incorporate_result('orbitals',gms_result,gms)

    assert(sim.input_code=='gamess')
    assert(sim.input.gamess_ascii=='../rhf/rhf.out')
    assert(sim.job.app_command=='convert4qmc -gamessAscii ../rhf/rhf.out')

    # incorporate orbitals from pyscf
    sim = sim_start.copy()

    assert(sim.input_code is None)
    assert(sim.input.pyscf is None)

    sim.incorporate_result('orbitals',pscf_result,pscf)

    assert(sim.input_code=='pyscf')
    assert(sim.input.pyscf=='../scf.h5')
    
    # incorporate orbitals from quantum package
    sim = sim_start.copy()

    assert(sim.input_code is None)
    assert(sim.input.qp is None)

    sim.incorporate_result('orbitals',qp_result,qp)

    assert(sim.input_code=='qp')
    assert(sim.input.qp=='../qp_savewf.out')

    clear_all_sims()
Пример #29
0
def test_defined_attribute_base():
    from generic import obj,NexusError
    from observables import AttributeProperties,DefinedAttributeBase

    # empty init
    p = AttributeProperties()
    o = DefinedAttributeBase()

    pref = obj(
        assigned        = set(),
        deepcopy        = False,
        default         = None,
        dest            = None,
        name            = None,
        no_default      = False,
        required        = False,
        type            = None,
        )
    assert(check_object_eq(p,pref))
    assert(len(o)==0)

    # init
    p = AttributeProperties(
        default  = 2,
        dest     = 'nest',
        deepcopy = True,
        )
    
    pref = obj(
        assigned        = {'dest', 'default', 'deepcopy'},
        deepcopy        = True,
        default         = 2,
        dest            = 'nest',
        name            = None,
        no_default      = False,
        required        = False,
        type            = None,
        )
    assert(check_object_eq(p,pref))


    # define attributes
    class DA(DefinedAttributeBase):
        None
    #end class DA

    da_attributes = obj(
        a = obj(
            default    = 1,
            ),         
        b = obj(       
            default    = 2,
            type       = int,
            required   = True,
            ),         
        c = obj(       
            dest       = 'nest',
            type       = str,
            ),
        d = obj(
            type       = dict,
            deepcopy   = True,
            no_default = True,
            ),
        nest = obj(
            type       = obj,
            default    = obj,
            ),
        )

    DA.define_attributes(**da_attributes)

    def get_class_dict(cls):
        o = obj()
        o.transfer_from(cls.__dict__)
        for k in list(o.keys()):
            if k.startswith('_'):
                del o[k]
            #end if
        #end for
        return o
    #end def get_class_dict

    o = get_class_dict(DA)

    oref = obj(
        deepcopy_attributes = {'d'},
        required_attributes = {'b'},
        sublevel_attributes = {'c'},
        toplevel_attributes = {'d', 'a', 'b', 'nest'},
        typed_attributes    = {'d', 'c', 'b', 'nest'},
        attribute_definitions = obj(
            a = obj(
                assigned        = {'default'},
                deepcopy        = False,
                default         = 1,
                dest            = None,
                name            = 'a',
                no_default      = False,
                required        = False,
                type            = None,
                ),
            b = obj(
                assigned        = {'required', 'default', 'type'},
                deepcopy        = False,
                default         = 2,
                dest            = None,
                name            = 'b',
                no_default      = False,
                required        = True,
                type            = int,
                ),
            c = obj(
                assigned        = {'dest', 'type'},
                deepcopy        = False,
                default         = None,
                dest            = 'nest',
                name            = 'c',
                no_default      = False,
                required        = False,
                type            = str,
                ),
            d = obj(
                assigned        = {'deepcopy', 'no_default', 'type'},
                deepcopy        = True,
                default         = None,
                dest            = None,
                name            = 'd',
                no_default      = True,
                required        = False,
                type            = dict,
                ),
            nest = obj(
                assigned        = {'default', 'type'},
                deepcopy        = False,
                default         = obj,
                no_default      = False,
                dest            = None,
                name            = 'nest',
                required        = False,
                type            = obj,
                ),
            ),
        )
    
    assert(check_object_eq(o,oref))


    class DA2(DA):
        None
    #end class DA2

    DA2.define_attributes(
        DA,
        e = obj(
            required = True,
            )
        )

    o = get_class_dict(DA)
    assert(check_object_eq(o,oref))

    o2 = get_class_dict(DA2)
    oref.required_attributes.add('e')
    oref.toplevel_attributes.add('e')
    oref.attribute_definitions.e = obj(
        assigned        = {'required'},
        deepcopy        = False,
        default         = None,
        dest            = None,
        name            = 'e',
        no_default      = False,
        required        = True,
        type            = None,
        )
    assert(check_object_eq(o2,oref))


    # empty init
    da = DA()
    assert(len(da)==0)
    assert(not da.check_attributes())


    # set_default_attributes
    da.set_default_attributes()
    
    da_ref = obj(
        a    = 1,
        b    = 2,
        nest = obj(
            c = None,
            ),
        )

    assert(check_object_eq(da,da_ref))
    assert(not da.check_attributes())


    # set_attributes/init
    da = DA(
        a = 2,
        b = 3,
        c = 'hi',
        d = dict(a=2),
        )

    da_ref = obj(
        a = 2,
        b = 3,
        d = {'a': 2},
        nest = obj(
            c = 'hi',
            ),
        )

    assert(check_object_eq(da,da_ref))
    assert(da.check_attributes())


    # set_attribute
    da = DA()

    assert('b' not in da)
    da.set_attribute('b',5)
    assert('b' in da)
    assert(da.b==5)

    try:
        da.set_attribute('unknown',None)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    da.set_attribute('b',3)
    assert(da.b==3)

    try:
        da.set_attribute('b',3.5)
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try


    # get_attribute
    da = DA()

    da.set_attribute('b',5)
    assert('b' in da)
    assert(da.b==5)

    assert(da.get_attribute('b')==5)
    assert(da.get_attribute('a',None) is None)
    assert(da.get_attribute('c',None) is None)

    try:
        da.get_attribute('unknown')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    try:
        da.get_attribute('a')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    try:
        da.get_attribute('c')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try


    # default values
    class DA_def(DefinedAttributeBase):
        None
    #end class DA_def
    DA_def.set_unassigned_default(None)

    class DA_def2(DA_def):
        None
    #end class DA_def2

    DA_def2.define_attributes(**da_attributes)

    assert(not DefinedAttributeBase.class_has('unassigned_default'))
    assert(DA_def.class_has('unassigned_default'))
    assert(DA_def2.class_has('unassigned_default'))
    assert(DA_def.unassigned_default is None)
    assert(DA_def2.unassigned_default is None)

    da = DA_def2()
    assert('a' not in da)
    assert('c' not in da)
    assert('nest' not in da)
    assert(da.get_attribute('a',None) is None)
    assert(da.get_attribute('c',None) is None)

    try:
        da.get_attribute('a')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    try:
        da.get_attribute('c')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    da.set_default_attributes()

    da_ref = obj(
        a = 1,
        b = 2,
        nest = obj(
            c = None,
            )
        )
    
    assert(check_object_eq(da,da_ref))

    da.b = None

    assert(da.get_attribute('a')==1)
    assert(da.get_attribute('b',assigned=False) is None)
    assert(da.get_attribute('c',assigned=False) is None)
    assert(da.get_attribute('a',2)==1)
    assert(da.get_attribute('a',assigned=False)==1)

    try:
        da.get_attribute('b')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    try:
        da.get_attribute('c')
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
Пример #30
0
def test_bundle():
    from generic import NexusError
    from machines import job, get_machine
    from bundle import bundle
    from bundle import SimulationBundle

    from test_simulation_module import get_test_workflow

    # empty init
    try:
        bundle()
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    # init with wrong types
    try:
        bundle([1, 2, 3])
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    def get_workflow():
        machine = get_machine('theta')
        machine.account = 'ABC123'
        test_job = job(machine='theta', nodes=1, app_command='test.x')
        sims = get_test_workflow(7, job=test_job)

        levels = []
        levels.append([sims.s0, sims.s1, sims.s2])
        levels.append([sims.s3, sims.s5, sims.s7, sims.s10])
        levels.append([sims.s4, sims.s6, sims.s8, sims.s11])
        levels.append([sims.s9, sims.s12])

        return sims, levels

    #end def get_workflow

    sims, levels = get_workflow()

    # attempt to bundle sims that depend on each other
    try:
        bundle(levels[1] + [sims.s8])
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try
    try:
        bundle(levels[1] + [sims.s9])
        raise FailedTest
    except NexusError:
        None
    except FailedTest:
        failed()
    except Exception as e:
        failed(str(e))
    #end try

    sims, levels = get_workflow()

    for n, level in enumerate(levels):
        for s in level:
            assert (not s.bundled)
            assert (s.bundler is None)
            assert (not s.skip_submit)
        #end for

        b = bundle(level, identifier='bundle_' + str(n))

        for s in level:
            assert (s.bundled)
            assert (id(s.bundler) == id(b))
            assert (s.skip_submit)
        #end for

        assert (isinstance(b, SimulationBundle))
        assert (isinstance(b.sims, list))
        assert (b.system is None)
        assert (b.infile is None)
        assert (not b.allow_create_directories)
        assert (not b.allow_get_dependencies)
        assert (not b.allow_write_inputs)
        assert (not b.allow_send_files)
        assert (not b.allow_submit)
        assert (not b.allow_get_output)
        assert (not b.allow_analyze)

        for sb, s in zip(b.sims, level):
            assert (id(sb) == id(s))
        #end for

        bj = b.job
        j = level[0].job
        ns = len(level)
        assert (bj.nodes == ns * j.nodes)
        assert (bj.cores == ns * j.cores)
        assert (object_eq(bj.get_time(), j.get_time()))

    #end for

    testing.clear_all_sims()