Ejemplo n.º 1
0
def load_sample_simulation(mof='single',
                           setup='run',
                           tests_dir=tests_dir,
                           parameters=k_parameters.copy()):
    """ Load a sample simulation object from the tests directory """
    from thermof import Simulation

    if mof == 'single':
        trial = os.path.join(tests_dir, 'ideal-mof-trial')
        if setup == 'run':
            sample_simulation = Simulation(read=os.path.join(trial, 'Run1'),
                                           setup='run',
                                           parameters=parameters)
        elif setup == 'trial':
            sample_simulation = Simulation(read=trial,
                                           setup='trial',
                                           parameters=parameters)
    else:
        trial = os.path.join(tests_dir, 'ip-mof-trial')
        if setup == 'run':
            sample_simulation = Simulation(read=os.path.join(trial, 'Run1'),
                                           setup='run',
                                           parameters=parameters)
        elif setup == 'trial':
            sample_simulation = Simulation(read=trial,
                                           setup='trial',
                                           parameters=parameters)
    return sample_simulation
Ejemplo n.º 2
0
def test_isotropic_read_run():
    """Test reading a single run and averaging directions"""
    k_par = k_parameters.copy()
    k_par['isotropic'] = True
    run_data = read_run(os.path.join(trial_dir, 'Run2'), k_par=k_par)

    flux_file_x = os.path.join(trial_dir, 'Run2', 'J0Jt_tx.dat')
    flux_x, time = read_thermal_flux(flux_file_x)
    J_x = calculate_k(flux_x, k_par=k_par)
    k_x = estimate_k(J_x, time, t0=5, t1=10)
    assert np.allclose(run_data['k']['x'], J_x)
    assert np.isclose(run_data['k_est']['x'], k_x)

    flux_file_y = os.path.join(trial_dir, 'Run2', 'J0Jt_ty.dat')
    flux_y, time = read_thermal_flux(flux_file_y)
    J_y = calculate_k(flux_y, k_par=k_par)
    k_y = estimate_k(J_y, time, t0=5, t1=10)
    assert np.allclose(run_data['k']['y'], J_y)
    assert np.isclose(run_data['k_est']['y'], k_y)

    flux_file_z = os.path.join(trial_dir, 'Run2', 'J0Jt_tz.dat')
    flux_z, time = read_thermal_flux(flux_file_z)
    J_z = calculate_k(flux_z, k_par=k_par)
    k_z = estimate_k(J_z, time, t0=5, t1=10)
    assert np.allclose(run_data['k']['z'], J_z)
    assert np.isclose(run_data['k_est']['z'], k_z)

    J_iso = average_k([J_x, J_y, J_z])
    assert np.allclose(run_data['k']['iso'], J_iso)

    k_iso = estimate_k(J_iso, time, t0=5, t1=10)
    assert np.allclose(run_data['k_est']['iso'], k_iso)
Ejemplo n.º 3
0
def test_read_run_input_error():
    """Tests whether read_run raises exception for missing flux file correctly"""
    run_dir = os.path.join(trial_dir, 'Run1')
    k_par = k_parameters.copy()
    k_par['prefix'] = 'wrong-name'
    with pytest.raises(FluxFileNotFoundError):
        read_run(run_dir, k_par=k_par)
Ejemplo n.º 4
0
def test_thermal_conductivity_estimation():
    """Tests thermal conductivity estimation for given time range"""
    flux, time = read_thermal_flux(flux_file)
    k_par = k_parameters.copy()
    J = calculate_k(flux, k_par=k_par)
    k = estimate_k(J, time, t0=5, t1=10)
    assert np.isclose(k, 0.8778570946468635)
Ejemplo n.º 5
0
def test_read_run_read_thermo():
    """Tests whether read_run raises exception for missing flux file correctly"""
    run_dir = os.path.join(trial_dir, 'Run1')
    k_par = k_parameters.copy()
    k_par['read_thermo'] = True
    run = read_run(run_dir, k_par=k_par)
    assert set(list(run['thermo'].keys())) == set(['NVT', 'NVE1', 'NVE2'])
    assert set(list(run['thermo']['NVT'].keys())) == set(
        ['step', 'temp', 'epair', 'emol', 'etotal', 'press'])
    assert len(run['thermo']['NVT']['step']) == 3001
    assert run['thermo']['NVT']['step'][0] == 0
    assert run['thermo']['NVT']['step'][-1] == 300000
Ejemplo n.º 6
0
def test_read_run():
    """Test reading a single run with read_run"""
    k_par = k_parameters.copy()
    run_data = read_run(os.path.join(trial_dir, 'Run1'), k_par=k_par)
    with open(k_ref_file, 'r') as kref:
        k_ref = yaml.load(kref)
    with open(time_ref_file, 'r') as tref:
        time_ref = yaml.load(tref)
    assert run_data['name'] == 'Run1'
    assert np.isclose(run_data['k_est']['x'], 0.8778570946468635)
    assert np.allclose(run_data['time'], time_ref)
    assert np.allclose(run_data['k']['x'], k_ref)
Ejemplo n.º 7
0
def test_read_trial_set():
    """Test method for reading a set of trial with multiple runs"""
    with open(k_ref_file, 'r') as kref:
        k_ref = yaml.load(kref)
    with open(time_ref_file, 'r') as tref:
        time_ref = yaml.load(tref)
    k_par = k_parameters.copy()
    k_par['isotropic'] = True
    k_par['average'] = True
    trial_set = read_trial_set(trial_set_dir, k_par=k_par)
    assert np.allclose(trial_set['data']['trial1']['data']['Run1']['k']['x'], k_ref)
    assert np.allclose(trial_set['data']['trial2']['data']['Run3']['time'], time_ref)
    assert np.isclose(trial_set['data']['trial1']['data']['Run1']['k_est']['iso'], k_est_iso_ref[0])
    assert np.isclose(trial_set['data']['trial1']['data']['Run2']['k_est']['iso'], k_est_iso_ref[1])
    assert np.isclose(trial_set['data']['trial2']['data']['Run3']['k_est']['iso'], k_est_iso_ref[2])
    assert np.isclose(trial_set['data']['trial2']['data']['Run4']['k_est']['iso'], k_est_iso_ref[3])
    assert np.isclose(trial_set['data']['trial1']['avg']['k_est']['iso'], (k_est_iso_ref[0] + k_est_iso_ref[1]) / 2)
    assert np.isclose(trial_set['data']['trial2']['avg']['k_est']['iso'], (k_est_iso_ref[2] + k_est_iso_ref[3]) / 2)
Ejemplo n.º 8
0
def test_read_trial():
    """Test method for reading a trial with multiple runs"""
    with open(k_ref_file, 'r') as kref:
        k_ref = yaml.load(kref)
    with open(time_ref_file, 'r') as tref:
        time_ref = yaml.load(tref)
    k_par = k_parameters.copy()
    k_par['isotropic'] = True
    k_par['average'] = True
    trial = read_trial(trial_dir, k_par=k_par)
    assert np.allclose(trial['data']['Run5']['time'], time_ref)
    assert np.allclose(trial['data']['Run1']['k']['x'], k_ref)
    assert np.isclose(trial['data']['Run1']['k_est']['x'], 0.8778570946468635)
    for i, k_ref in enumerate(k_est_iso_ref, start=1):
        assert np.isclose(trial['data']['Run%i' % i]['k_est']['iso'], k_ref)
    assert np.isclose(trial['avg']['k_est']['iso'], 0.817262775736688)
    assert np.isclose(trial['avg']['k_est']['stats']['iso']['std'],
                      np.std(k_est_iso_ref))
    assert np.isclose(trial['avg']['k_est']['stats']['iso']['max'],
                      max(k_est_iso_ref))
    assert np.isclose(trial['avg']['k_est']['stats']['iso']['min'],
                      min(k_est_iso_ref))
Ejemplo n.º 9
0
def test_read_run_directory_not_found_error():
    """Tests whether read_run raises exception for missing flux file correctly"""
    run_dir = os.path.join(trial_dir, 'Run11')
    k_par = k_parameters.copy()
    with pytest.raises(RunDirectoryNotFoundError):
        read_run(run_dir, k_par=k_par)
Ejemplo n.º 10
0
"""
Reads thermo data for a list of trials and saves results to a yaml file
"""
import os
import yaml
import numpy as np
from thermof.parameters import k_parameters
from thermof.read import read_run_info, read_thermo, read_log

# --------------------------------------------------------------------------------------------------
main = ''                                                           # Directory of trials
results_file = '%s-kest-results.yaml' % os.path.basename(main)      # Name of results file
# --------------------------------------------------------------------------------------------------
trial_list = [os.path.join(main, i) for i in os.listdir(main) if os.path.isdir(os.path.join(main, i))]
results = dict(temp=[], press=[], e_pair=[], e_mol=[], tot_eng=[], epsilon=[], sigma=[], trial=[])
k_par = k_parameters.copy()
fix_list = ['NVE1', 'NVE2', 'NVT']
var_list = ['e_pair', 'temp', 'e_mol', 'tot_eng', 'press']

for trial_index, trial in enumerate(trial_list, start=1):
    trial_name = os.path.basename(trial)
    print('\n%i / %i | %s #################################' % (trial_index, len(trial_list), trial_name), flush=True)

    ri = read_run_info(os.path.join(trial, 'Run1'))
    results['sigma'].append(ri['sigma'])
    results['epsilon'].append(ri['epsilon'])
    results['trial'].append(os.path.basename(trial))

    if trial_name not in ['S6.00-E0.80', 'S6.00-E1.00']:
        for run in os.listdir(trial):
            run_dir = os.path.join(trial, run)
Ejemplo n.º 11
0
def test_get_flux_directions_exception():
    """Tests whether thermal flux files note found exception is raised correctly"""
    k_par = k_parameters.copy()
    k_par['prefix'] = 'wrong-name'
    with pytest.raises(FluxFileNotFoundError):
        get_flux_directions(os.path.join(trial_dir, 'Run1'), k_par=k_par)