Esempio n. 1
0
    def test_liquid(self):
        """Check liquid target with existing simulation data"""
        # if not sys.version_info <= (2,7):
        #     self.skipTest("Existing pickle file only works with Python 3")

        print("Setting input file to 'single.in'")
        input_file = 'single.in'

        ## The general options and target options that come from parsing the input file
        print("Parsing inputs...")
        options, tgt_opts = parse_inputs(input_file)
        print("options:\n%s\n\ntgt_opts:\n%s\n\n" %
              (str(options), str(tgt_opts)))

        forcefield = FF(options)
        objective = Objective(options, tgt_opts, forcefield)
        ## The optimizer component of the project
        print("Creating optimizer: ")
        optimizer = Optimizer(options, objective, forcefield)
        assert isinstance(optimizer,
                          Optimizer), "Expected forcebalance optimizer object"
        print(str(optimizer))

        ## Actually run the optimizer.
        print("Done setting up! Running optimizer...")
        result = optimizer.Run()
        print("\nOptimizer finished. Final results:")
        print(str(result))

        liquid_obj_value = optimizer.Objective.ObjDict['Liquid']['x']
        assert liquid_obj_value < 20, "Liquid objective function should give < 20 (about 17.23) total value."
Esempio n. 2
0
def Run_ForceBalance(input_file, debug=False, continue_=False):
    """ Create instances of ForceBalance components and run the optimizer.

    The triumvirate, trifecta, or trinity of components are:
    - The force field
    - The objective function
    - The optimizer
    Cipher: "All I gotta do here is pull this plug... and there you have to watch Apoc die"
    Apoc: "TRINITY" *chunk*

    The force field is a class defined in forcefield.py.
    The objective function is a combination of target classes and a penalty function class.
    The optimizer is a class defined in this file.
    """
    try:
        ## The general options and target options that come from parsing the input file
        options, tgt_opts = parse_inputs(input_file)
        ## Set the continue_ option.
        if continue_: options['continue'] = True
        ## The force field component of the project
        forcefield = FF(options)
        ## The objective function
        objective = Objective(options, tgt_opts, forcefield)
        ## The optimizer component of the project
        optimizer = Optimizer(options, objective, forcefield)
        ## Actually run the optimizer.
        optimizer.Run()
    except:
        import traceback
        traceback.print_exc()
        if debug:
            import pdb
            pdb.post_mortem()
Esempio n. 3
0
 def get_optimizer(self):
     """ Return the optimizer object """
     ## The general options and target options that come from parsing the input file
     self.logger.debug("Parsing inputs...\n")
     options, tgt_opts = parse_inputs(self.input_file)
     self.logger.debug("options:\n%s\n\ntgt_opts:\n%s\n\n" %
                       (str(options), str(tgt_opts)))
     assert isinstance(options,
                       dict), "Parser gave incorrect type for options"
     assert isinstance(tgt_opts,
                       list), "Parser gave incorrect type for tgt_opts"
     for target in tgt_opts:
         assert isinstance(
             target, dict), "Parser gave incorrect type for target dict"
     ## The force field component of the project
     forcefield = FF(options)
     assert isinstance(forcefield,
                       FF), "Expected forcebalance forcefield object"
     ## The objective function
     objective = Objective(options, tgt_opts, forcefield)
     assert isinstance(objective,
                       Objective), "Expected forcebalance objective object"
     ## The optimizer component of the project
     self.logger.debug("Creating optimizer: ")
     optimizer = Optimizer(options, objective, forcefield)
     assert isinstance(optimizer,
                       Optimizer), "Expected forcebalance optimizer object"
     self.logger.debug(str(optimizer) + "\n")
     return optimizer
Esempio n. 4
0
    def runTest(self):
        """Check continuation from a previous run"""
        self.logger.debug("\nSetting input file to 'test_continue.in'\n")
        input_file = 'test_continue.in'

        ## The general options and target options that come from parsing the input file
        self.logger.debug("Parsing inputs...\n")
        options, tgt_opts = parse_inputs(input_file)
        options['continue'] = True
        self.logger.debug("options:\n%s\n\ntgt_opts:\n%s\n\n" %
                          (str(options), str(tgt_opts)))

        self.assertEqual(dict,
                         type(options),
                         msg="\nParser gave incorrect type for options")
        self.assertEqual(list,
                         type(tgt_opts),
                         msg="\nParser gave incorrect type for tgt_opts")
        for target in tgt_opts:
            self.assertEqual(
                dict,
                type(target),
                msg="\nParser gave incorrect type for target dict")

        ## The force field component of the project
        forcefield = FF(options)
        self.assertEqual(FF,
                         type(forcefield),
                         msg="\nExpected forcebalance forcefield object")

        ## The objective function
        objective = Objective(options, tgt_opts, forcefield)
        self.assertEqual(Objective,
                         type(objective),
                         msg="\nExpected forcebalance objective object")

        ## The optimizer component of the project
        self.logger.debug("Creating optimizer: ")
        optimizer = Optimizer(options, objective, forcefield)
        self.assertEqual(Optimizer,
                         type(optimizer),
                         msg="\nExpected forcebalance optimizer object")
        self.logger.debug(str(optimizer) + "\n")

        ## Actually run the optimizer.
        self.logger.debug("Done setting up! Running optimizer...\n")
        result = optimizer.Run()
        self.logger.debug("\nOptimizer finished. Final results:\n")
        self.logger.debug(str(result) + '\n')

        self.assertEqual(optimizer.iterinit,
                         2,
                         msg="\nInitial iteration counter is incorrect")
        self.assertEqual(optimizer.iteration,
                         2,
                         msg="\nFinal iteration counter is incorrect")
Esempio n. 5
0
    def runTest(self):
        """Check voelz study runs without errors"""
        self.logger.debug("\nSetting input file to 'options.in'\n")
        input_file = 'options.in'

        ## The general options and target options that come from parsing the input file
        self.logger.debug("Parsing inputs...\n")
        options, tgt_opts = parse_inputs(input_file)
        self.logger.debug("options:\n%s\n\ntgt_opts:\n%s\n\n" %
                          (str(options), str(tgt_opts)))

        self.assertEqual(dict,
                         type(options),
                         msg="\nParser gave incorrect type for options")
        self.assertEqual(list,
                         type(tgt_opts),
                         msg="\nParser gave incorrect type for tgt_opts")
        for target in tgt_opts:
            self.assertEqual(
                dict,
                type(target),
                msg="\nParser gave incorrect type for target dict")

        ## The force field component of the project
        self.logger.debug("Creating forcefield using loaded options: ")
        forcefield = FF(options)
        self.logger.debug(str(forcefield) + "\n")
        self.assertEqual(FF,
                         type(forcefield),
                         msg="\nExpected forcebalance forcefield object")

        ## The objective function
        self.logger.debug(
            "Creating object using loaded options and forcefield: ")
        objective = Objective(options, tgt_opts, forcefield)
        self.logger.debug(str(objective) + "\n")
        self.assertEqual(Objective,
                         type(objective),
                         msg="\nExpected forcebalance objective object")

        ## The optimizer component of the project
        self.logger.debug("Creating optimizer: ")
        optimizer = Optimizer(options, objective, forcefield)
        self.logger.debug(str(optimizer) + "\n")
        self.assertEqual(Optimizer,
                         type(optimizer),
                         msg="\nExpected forcebalance optimizer object")

        ## Actually run the optimizer.
        self.logger.debug("Done setting up! Running optimizer...\n")
        result = optimizer.Run()

        self.logger.debug("\nOptimizer finished. Final results:\n")
        self.logger.debug(str(result) + '\n')
Esempio n. 6
0
    def test_continue(self):
        """Check continuation from a previous run"""
        if sys.version_info < (3, 0):
            pytest.skip("Existing pickle file only works with Python 3")
        self.logger.debug("\nSetting input file to 'test_continue.in'\n")
        input_file = 'test_continue.in'

        ## The general options and target options that come from parsing the input file
        self.logger.debug("Parsing inputs...\n")
        options, tgt_opts = parse_inputs(input_file)
        options['continue'] = True
        self.logger.debug("options:\n%s\n\ntgt_opts:\n%s\n\n" %
                          (str(options), str(tgt_opts)))

        assert isinstance(options,
                          dict), "Parser gave incorrect type for options"
        assert isinstance(tgt_opts,
                          list), "Parser gave incorrect type for tgt_opts"
        for target in tgt_opts:
            assert isinstance(
                target, dict), "Parser gave incorrect type for target dict"

        ## The force field component of the project
        forcefield = FF(options)
        assert isinstance(forcefield,
                          FF), "Expected forcebalance forcefield object"

        ## The objective function
        objective = Objective(options, tgt_opts, forcefield)
        assert isinstance(objective,
                          Objective), "Expected forcebalance objective object"

        ## The optimizer component of the project
        self.logger.debug("Creating optimizer: ")
        optimizer = Optimizer(options, objective, forcefield)
        assert isinstance(optimizer,
                          Optimizer), "Expected forcebalance optimizer object"
        self.logger.debug(str(optimizer) + '\n')

        ## Actually run the optimizer.
        self.logger.debug("Done setting up! Running optimizer...\n")
        result = optimizer.Run()
        self.logger.debug("\nOptimizer finished. Final results:\n")
        self.logger.debug(str(result) + '\n')

        assert optimizer.iterinit == 2, "Initial iteration counter is incorrect"
        assert optimizer.iteration == 2, "Final iteration counter is incorrect"
Esempio n. 7
0
    def runTest(self):
        """Check liquid target with existing simulation data"""
        if not sys.version_info <= (2, 7):
            self.skipTest("Existing pickle file only works with Python 3")

        self.logger.debug("\nSetting input file to 'single.in'\n")
        input_file = 'single.in'

        ## The general options and target options that come from parsing the input file
        self.logger.debug("Parsing inputs...\n")
        options, tgt_opts = parse_inputs(input_file)
        self.logger.debug("options:\n%s\n\ntgt_opts:\n%s\n\n" %
                          (str(options), str(tgt_opts)))

        forcefield = FF(options)
        objective = Objective(options, tgt_opts, forcefield)
        ## The optimizer component of the project
        self.logger.debug("Creating optimizer: ")
        optimizer = Optimizer(options, objective, forcefield)
        self.assertEqual(Optimizer,
                         type(optimizer),
                         msg="\nExpected forcebalance optimizer object")
        self.logger.debug(str(optimizer) + "\n")

        ## Actually run the optimizer.
        self.logger.debug("Done setting up! Running optimizer...\n")
        result = optimizer.Run()
        self.logger.debug("\nOptimizer finished. Final results:\n")
        self.logger.debug(str(result) + '\n')

        liquid_obj_value = optimizer.Objective.ObjDict['Liquid']['x']
        self.assertTrue(
            liquid_obj_value < 20,
            msg=
            "\nLiquid objective function should give < 20 (about 17.23) total value."
        )
Esempio n. 8
0
    def runTest(self):
        """Check water tutorial study runs without errors"""
        self.logger.debug("\nSetting input file to 'very_simple.in'\n")
        input_file = 'very_simple.in'

        ## The general options and target options that come from parsing the input file
        self.logger.debug("Parsing inputs...\n")
        options, tgt_opts = parse_inputs(input_file)
        self.logger.debug("options:\n%s\n\ntgt_opts:\n%s\n\n" %
                          (str(options), str(tgt_opts)))

        self.assertEqual(dict,
                         type(options),
                         msg="\nParser gave incorrect type for options")
        self.assertEqual(list,
                         type(tgt_opts),
                         msg="\nParser gave incorrect type for tgt_opts")
        for target in tgt_opts:
            self.assertEqual(
                dict,
                type(target),
                msg="\nParser gave incorrect type for target dict")

        ## The force field component of the project
        forcefield = FF(options)
        self.assertEqual(FF,
                         type(forcefield),
                         msg="\nExpected forcebalance forcefield object")

        ## The objective function
        objective = Objective(options, tgt_opts, forcefield)
        self.assertEqual(Objective,
                         type(objective),
                         msg="\nExpected forcebalance objective object")

        ## The optimizer component of the project
        self.logger.debug("Creating optimizer: ")
        optimizer = Optimizer(options, objective, forcefield)
        self.assertEqual(Optimizer,
                         type(optimizer),
                         msg="\nExpected forcebalance optimizer object")
        self.logger.debug(str(optimizer) + "\n")

        ## Actually run the optimizer.
        self.logger.debug("Done setting up! Running optimizer...\n")
        result = optimizer.Run()
        self.logger.debug("\nOptimizer finished. Final results:\n")
        self.logger.debug(str(result) + '\n')

        self.assertNdArrayEqual(
            EXPECTED_WATER_RESULTS,
            result,
            delta=0.001,
            msg=
            "\nCalculation results have changed from previously calculated values.\n"
            "If this seems reasonable, update EXPECTED_WATER_RESULTS in test_system.py with these values"
        )

        # Fail if calculation takes longer than previously to converge
        self.assertGreaterEqual(ITERATIONS_TO_CONVERGE, Counter(), msg="\nCalculation took longer than expected to converge (%d iterations vs previous of %d)" %\
        (ITERATIONS_TO_CONVERGE, Counter()))
Esempio n. 9
0
    def runTest(self):
        """Check implicit hydration free energy study (Hydration target) converges to expected results"""
        self.logger.debug("\nSetting input file to 'optimize.in'\n")
        input_file = 'optimize.in'

        ## The general options and target options that come from parsing the input file
        self.logger.debug("Parsing inputs...\n")
        options, tgt_opts = parse_inputs(input_file)
        self.logger.debug("options:\n%s\n\ntgt_opts:\n%s\n\n" %
                          (str(options), str(tgt_opts)))

        self.assertEqual(dict,
                         type(options),
                         msg="\nParser gave incorrect type for options")
        self.assertEqual(list,
                         type(tgt_opts),
                         msg="\nParser gave incorrect type for tgt_opts")
        for target in tgt_opts:
            self.assertEqual(
                dict,
                type(target),
                msg="\nParser gave incorrect type for target dict")

        ## The force field component of the project
        self.logger.debug("Creating forcefield using loaded options: ")
        forcefield = FF(options)
        self.logger.debug(str(forcefield) + "\n")
        self.assertEqual(FF,
                         type(forcefield),
                         msg="\nExpected forcebalance forcefield object")

        ## The objective function
        self.logger.debug(
            "Creating object using loaded options and forcefield: ")
        objective = Objective(options, tgt_opts, forcefield)
        self.logger.debug(str(objective) + "\n")
        self.assertEqual(Objective,
                         type(objective),
                         msg="\nExpected forcebalance objective object")

        ## The optimizer component of the project
        self.logger.debug("Creating optimizer: ")
        optimizer = Optimizer(options, objective, forcefield)
        self.logger.debug(str(optimizer) + "\n")
        self.assertEqual(Optimizer,
                         type(optimizer),
                         msg="\nExpected forcebalance optimizer object")

        ## Actually run the optimizer.
        self.logger.debug("Done setting up! Running optimizer...\n")
        result = optimizer.Run()

        self.logger.debug("\nOptimizer finished. Final results:\n")
        self.logger.debug(str(result) + '\n')

        self.assertNdArrayEqual(
            EXPECTED_ETHANOL_RESULTS,
            forcefield.create_pvals(result),
            delta=0.02,
            msg=
            "\nCalculation results have changed from previously calculated values.\n"
            "If this seems reasonable, update EXPECTED_ETHANOL_RESULTS in test_system.py with these values"
        )