Example #1
0
 def test_get_cvs_with_qdot_broken(self):
     """Tests get_cvs with a dummy coordinate file, include_qdot = True, and a coordinate file without velocities"""
     settings = configure('../../data/atesa.config')
     settings.include_qdot = True
     settings.topology = '../test_data/test.prmtop'
     settings.cvs = [
         'pytraj.distance(traj, \'@1 @3\')[0]',
         'pytraj.distance(traj, \'@2 @4\')[0]'
     ]
     with pytest.raises(IndexError):
         result = utilities.get_cvs('../test_data/test.rst7', settings)
Example #2
0
 def test_get_cvs_no_qdot_reduce(self):
     """Tests get_cvs with a dummy coordinate file, include_qdot = False, and reduce = True"""
     settings = configure('../../data/atesa.config')
     settings.include_qdot = False
     settings.topology = '../test_data/test.prmtop'
     settings.cvs = [
         'pytraj.distance(traj, \'@1 @3\')[0]',
         'pytraj.distance(traj, \'@2 @4\')[0]'
     ]
     with pytest.raises(FileNotFoundError):  # no as.out present yet
         result = utilities.get_cvs('../test_data/test.rst7',
                                    settings,
                                    reduce=True)
     shutil.copy('../test_data/as.out',
                 'as.out')  # copy in the necessary file
     result = utilities.get_cvs('../test_data/test.rst7',
                                settings,
                                reduce=True)
     assert len(result.split(' ')) == 2
     assert float(result.split(' ')[0]) == pytest.approx(0.571, 1e-2)
     assert float(result.split(' ')[1]) == pytest.approx(0.326, 1e-2)
Example #3
0
 def test_get_cvs_no_qdot(self):
     """Tests get_cvs with a dummy coordinate file and include_qdot = False"""
     settings = configure('../../data/atesa.config')
     settings.include_qdot = False
     settings.topology = '../test_data/test.prmtop'
     settings.cvs = [
         'pytraj.distance(traj, \'@1 @3\')[0]',
         'pytraj.distance(traj, \'@2 @4\')[0]'
     ]
     result = utilities.get_cvs('../test_data/test.rst7', settings)
     assert len(result.split(' ')) == 2
     assert float(result.split(' ')[0]) == pytest.approx(1.09, 1e-3)
     assert float(result.split(' ')[1]) == pytest.approx(1.69, 1e-3)
Example #4
0
 def test_get_cvs_with_qdot(self):
     """Tests get_cvs with a dummy coordinate file, include_qdot = True, and a coordinate file with velocities"""
     settings = configure('../../data/atesa.config')
     settings.include_qdot = True
     settings.topology = '../test_data/test.prmtop'
     settings.cvs = [
         'pytraj.distance(traj, \'@1 @3\')[0]',
         'pytraj.distance(traj, \'@2 @4\')[0]'
     ]
     result = utilities.get_cvs('../test_data/test_velocities.rst7',
                                settings)
     assert len(result.split(' ')) == 4
     assert float(result.split(' ')[0]) == pytest.approx(1.089, 1e-3)
     assert float(result.split(' ')[1]) == pytest.approx(1.728, 1e-3)
     assert float(result.split(' ')[2]) == pytest.approx(-0.252, 1e-2)
     assert float(result.split(' ')[3]) == pytest.approx(0.282, 1e-2)
Example #5
0
def main(working_directory, rc_definition, as_out_file, extrema=False):
    """
    The main function of rc_eval.py. Accepts an aimless shooting working directory and a reaction coordinate definition,
    producing in that directory a new file named 'rc.out' (overwriting if one already exists) identifying each shooting
    point (files in the directory whose names end in "_init.rst7") and its corresponding reaction coordinate value in a
    sorted list.

    If extrema == True, skips producing rc.out and just returns the minimum and maximum RC value for a single accepted
    shooting move, which is useful when preparing umbrella sampling simulations.

    Parameters
    ----------
    working_directory : str
        The path to the aimless shooting working directory in which to act
    rc_definition : str
        A reaction coordinate definition formatted as a string of python-readable code with "CV[X]" standing in for the
        Xth CV value (one-indexed); this RC definition should be in terms of reduced variables (values between 0 and 1)
    as_out_file : str
        Path to the aimless shooting output file used to build the reaction coordinate. Usually this should be a
        decorrelated file (named with "decorr").
    extrema : bool
        If True, skips producing rc.out and just returns the minimum and maximum RC value for a single accepted shooting
        move, which is useful when preparing umbrella sampling simulations.

    Returns
    -------
    None

    """

    # Change to working directory
    os.chdir(working_directory)

    # Unpickle settings object for use in utilities.get_cvs
    try:
        settings = pickle.load(open('settings.pkl', 'rb'))
    except FileNotFoundError:  # replace with more informative error message
        raise FileNotFoundError(
            'the working directory must contain a valid settings.pkl file, which is generated '
            'automatically when running ATESA, but one was not found in the working directory: '
            + working_directory)

    if not settings.job_type == 'aimless_shooting':
        raise RuntimeError(
            'rc_eval.py can only be called on an aimless shooting working directory, but the provided '
            'directory (' + working_directory +
            ') contains a settings.pkl file with job_type = ' +
            settings.job_type)

    settings.as_out_file = as_out_file  # for reducing CVs properly
    settings.include_qdot = False  # unnecessary for our purposes

    if extrema:
        from atesa.main import Thread
        print(
            'Evaluating final RC values of forward and backward trajectories from an accepted shooting move...'
        )
        result = []
        allthreads = pickle.load(open('restart.pkl', 'rb'))
        for thread in allthreads:
            if thread.history.last_accepted > -1:  # if accepted move exists in thread
                for job_index in range(2):
                    cvs = utilities.get_cvs(thread.history.prod_trajs[
                        thread.history.last_accepted][job_index],
                                            settings,
                                            reduce=True).split(' ')
                    result.append(utilities.evaluate_rc(rc_definition, cvs))
                print(' Shooting move name: ' + thread.history.init_coords[
                    thread.history.last_accepted][0])
                print(' extrema: ' +
                      str([float('%.4f' % float(item)) for item in result]))
                return None  # to exit the script after returning extrema
        raise RuntimeError(
            'none of the shooting moves in the working directory appear to contain any accepted moves.'
        )

    # Obtain list of shooting point coordinate files
    file_list = glob.glob('*_init.rst7')
    if not file_list:
        raise FileNotFoundError(
            'no valid shooting point files (as given by names ending in \'_init.rst7\') were found '
            'in the working directory \'' + working_directory +
            '\'. Is this an aimless shooting '
            'working directory?')

    # Iterate through the list, calling evaluate_rc for each one and storing the result
    results = []
    count = 0
    count_to = len(file_list)
    update_progress(0, 'Evaluating RC values')
    speed_data = [0, 0]
    for file in file_list:
        t = time.time()
        cv_list = utilities.get_cvs(file, settings, reduce=True).split(' ')
        results.append(
            [file + ': ',
             utilities.evaluate_rc(rc_definition, cv_list)])
        this_speed = time.time() - t
        speed_data = [
            (speed_data[1] * speed_data[0] + this_speed) / (speed_data[1] + 1),
            speed_data[1] + 1
        ]
        count += 1
        eta = (count_to - count) * speed_data[0]
        update_progress(count / count_to, 'Evaluating RC values', eta=eta)
    results = sorted(
        results,
        key=lambda x: abs(float(x[1])))  # sort results by absolute value of RC

    # Create and write to rc.out file
    open('rc.out', 'w').close()
    with open('rc.out', 'a') as f:
        for result in results:
            f.write(result[0] + str(result[1]) + '\n')
        f.close()