def test_simple_values(self):
        import numpy as np
        import openmdao.api as om

        class VectorDoublingComp(om.ExplicitComponent):
            def initialize(self):
                self.options.declare('size', values=[2, 4, 6, 8])

            def setup(self):
                size = self.options['size']

                self.add_input('x', shape=size)
                self.add_output('y', shape=size)
                self.declare_partials('y',
                                      'x',
                                      val=2.,
                                      rows=np.arange(size),
                                      cols=np.arange(size))

            def compute(self, inputs, outputs):
                outputs['y'] = 2 * inputs['x']

        prob = om.Problem()
        prob.model.add_subsystem('double', VectorDoublingComp(size=4))

        prob.setup()

        prob.set_val('double.x', [1., 2., 3., 4.])

        prob.run_model()
        assert_near_equal(prob.get_val('double.y'), [2., 4., 6., 8.])
Exemple #2
0
    def test_simple_bounds_valid(self):
        def check_even(name, value):
            if value % 2 != 0:
                raise ValueError(
                    f"Option '{name}' with value {value} must be an even number."
                )

        class VectorDoublingComp(om.ExplicitComponent):
            def initialize(self):
                self.options.declare('size',
                                     types=int,
                                     lower=2,
                                     upper=8,
                                     check_valid=check_even)

            def setup(self):
                size = self.options['size']

                self.add_input('x', shape=size)
                self.add_output('y', shape=size)
                self.declare_partials('y',
                                      'x',
                                      val=2.,
                                      rows=np.arange(size),
                                      cols=np.arange(size))

            def compute(self, inputs, outputs):
                outputs['y'] = 2 * inputs['x']

        try:
            comp = VectorDoublingComp(size=5)
        except Exception as err:
            self.assertEqual(
                str(err), "Option 'size' with value 5 must be an even number.")
Exemple #3
0
    def test_simple(self):
        prob = om.Problem()
        prob.model.add_subsystem(
            'double', VectorDoublingComp(size=3))  # 'size' is an option

        prob.setup()

        prob.set_val('double.x', [1., 2., 3.])

        prob.run_model()
        assert_near_equal(prob.get_val('double.y'), [2., 4., 6.])
Exemple #4
0
    def test_simple_fail(self):
        prob = om.Problem()
        prob.model.add_subsystem('double',
                                 VectorDoublingComp())  # 'size' not specified

        try:
            prob.setup()
        except RuntimeError as err:
            self.assertEqual(
                str(err),
                "'double' <class VectorDoublingComp>: Option 'size' is required but has not been set."
            )
    def test_simple_fail(self):
        import openmdao.api as om
        from openmdao.test_suite.components.options_feature_vector import VectorDoublingComp

        prob = om.Problem()
        prob.model.add_subsystem('inputs', om.IndepVarComp('x', shape=3))
        prob.model.add_subsystem('double', VectorDoublingComp())  # 'size' not specified
        prob.model.connect('inputs.x', 'double.x')

        try:
            prob.setup()
        except RuntimeError as err:
            self.assertEqual(str(err), "VectorDoublingComp (double): Option 'size' is required but has not been set.")
    def test_simple(self):
        import openmdao.api as om
        from openmdao.test_suite.components.options_feature_vector import VectorDoublingComp

        prob = om.Problem()
        prob.model.add_subsystem(
            'double', VectorDoublingComp(size=3))  # 'size' is an option

        prob.setup()

        prob.set_val('double.x', [1., 2., 3.])

        prob.run_model()
        assert_near_equal(prob.get_val('double.y'), [2., 4., 6.])
    def test_simple(self):
        import openmdao.api as om
        from openmdao.test_suite.components.options_feature_vector import VectorDoublingComp

        prob = om.Problem()
        prob.model.add_subsystem('inputs', om.IndepVarComp('x', shape=3))
        prob.model.add_subsystem('double', VectorDoublingComp(size=3))  # 'size' is an option
        prob.model.connect('inputs.x', 'double.x')

        prob.setup()

        prob['inputs.x'] = [1., 2., 3.]

        prob.run_model()
        assert_rel_error(self, prob['double.y'], [2., 4., 6.])
    def test_simple_fail(self):
        import openmdao.api as om
        from openmdao.test_suite.components.options_feature_vector import VectorDoublingComp

        prob = om.Problem()
        prob.model.add_subsystem('double',
                                 VectorDoublingComp())  # 'size' not specified

        try:
            prob.setup()
        except RuntimeError as err:
            self.assertEqual(
                str(err),
                "'double' <class VectorDoublingComp>: Option 'size' is required but has not been set."
            )
Exemple #9
0
    def test_deprecated_metadata(self):
        prob = Problem()
        prob.model.add_subsystem('inputs', IndepVarComp('x', shape=3))
        prob.model.add_subsystem('double', VectorDoublingComp())

        msg = "The 'metadata' attribute provides backwards compatibility " \
              "with earlier version of OpenMDAO; use 'options' instead."

        with assert_warning(DeprecationWarning, msg):
            prob.model.double.metadata['size'] = 3

        prob.model.connect('inputs.x', 'double.x')
        prob.setup()

        prob['inputs.x'] = [1., 2., 3.]

        prob.run_model()
        assert_rel_error(self, prob['double.y'], [2., 4., 6.])
Exemple #10
0
    def test_deprecated_metadata(self):
        from openmdao.api import Problem, IndepVarComp
        from openmdao.test_suite.components.options_feature_vector import VectorDoublingComp

        prob = Problem()
        prob.model.add_subsystem('inputs', IndepVarComp('x', shape=3))
        prob.model.add_subsystem('double', VectorDoublingComp())

        with warnings.catch_warnings(record=True) as w:
            prob.model.double.metadata['size'] = 3

        self.assertEqual(len(w), 1)
        self.assertTrue(issubclass(w[0].category, DeprecationWarning))
        self.assertEqual(str(w[0].message),
                         "The 'metadata' attribute provides backwards compatibility "
                         "with earlier version of OpenMDAO; use 'options' instead.")

        prob.model.connect('inputs.x', 'double.x')
        prob.setup()

        prob['inputs.x'] = [1., 2., 3.]

        prob.run_model()
        assert_rel_error(self, prob['double.y'], [2., 4., 6.])