Exemple #1
0
    def test_remove_whitespace(self):
        self.assertEqual(remove_whitespace(self.test_str), 'abcdef')

        self.assertEqual(remove_whitespace(self.test_str, right=True),
                         ws + 'abc' + ws + 'def')

        self.assertEqual(remove_whitespace(self.test_str, left=True),
                         'abc' + ws + 'def' + ws)

        self.assertEqual(
            remove_whitespace(self.test_str, right=True, left=True),
            'abc' + ws + 'def')
    def test_simple_list_vars_options(self):

        import openmdao.api as om

        prob = om.Problem()
        model = prob.model

        model.add_subsystem(
            'p1',
            om.IndepVarComp('x',
                            12.0,
                            lower=1.0,
                            upper=100.0,
                            ref=1.1,
                            ref0=2.1,
                            units='inch',
                            desc='indep x'))
        model.add_subsystem(
            'p2',
            om.IndepVarComp('y',
                            1.0,
                            lower=2.0,
                            upper=200.0,
                            ref=1.2,
                            res_ref=2.2,
                            units='ft',
                            desc='indep y'))
        model.add_subsystem(
            'comp',
            om.ExecComp('z=x+y',
                        x={
                            'value': 0.0,
                            'units': 'inch'
                        },
                        y={
                            'value': 0.0,
                            'units': 'inch'
                        },
                        z={
                            'value': 0.0,
                            'units': 'inch'
                        }))
        model.connect('p1.x', 'comp.x')
        model.connect('p2.y', 'comp.y')

        prob.setup()

        # list outputs before model has been run will raise an exception
        outputs = dict(prob.model.list_outputs(out_stream=None))
        expected = {
            'p1.x': {
                'value': 12.
            },
            'p2.y': {
                'value': 1.
            },
            'comp.z': {
                'value': 0.
            },
        }
        self.assertEqual(outputs, expected)

        # list_inputs on a component before run is okay, using relative names
        expl_inputs = prob.model.comp.list_inputs(out_stream=None)
        expected = {'x': {'value': 0.}, 'y': {'value': 0.}}
        self.assertEqual(dict(expl_inputs), expected)

        expl_inputs = prob.model.comp.list_inputs(includes='x',
                                                  out_stream=None)
        self.assertEqual(dict(expl_inputs), {'x': {'value': 0.}})

        expl_inputs = prob.model.comp.list_inputs(excludes='x',
                                                  out_stream=None)
        self.assertEqual(dict(expl_inputs), {'y': {'value': 0.}})

        # specifying prom_name should not cause an error
        expl_inputs = prob.model.comp.list_inputs(prom_name=True,
                                                  out_stream=None)
        self.assertEqual(
            dict(expl_inputs), {
                'x': {
                    'value': 0.,
                    'prom_name': 'x'
                },
                'y': {
                    'value': 0.,
                    'prom_name': 'y'
                },
            })

        # list_outputs on a component before run is okay, using relative names
        stream = StringIO()
        expl_outputs = prob.model.p1.list_outputs(out_stream=stream)
        expected = {'x': {'value': 12.}}
        self.assertEqual(dict(expl_outputs), expected)

        text = stream.getvalue().split('\n')
        expected_text = [
            "1 Explicit Output(s) in 'p1'", "----------------------------", "",
            "varname  value", "-------  -----", "x        [12.]", "", "",
            "0 Implicit Output(s) in 'p1'", "----------------------------"
        ]
        for i, line in enumerate(expected_text):
            if line and not line.startswith('-'):
                self.assertEqual(remove_whitespace(text[i]),
                                 remove_whitespace(line))

        expl_outputs = prob.model.p1.list_outputs(includes='x',
                                                  out_stream=None)
        self.assertEqual(dict(expl_outputs), expected)

        expl_outputs = prob.model.p1.list_outputs(excludes='x',
                                                  out_stream=None)
        self.assertEqual(dict(expl_outputs), {})

        # specifying residuals_tol should not cause an error
        expl_outputs = prob.model.p1.list_outputs(residuals_tol=.01,
                                                  out_stream=None)
        self.assertEqual(dict(expl_outputs), expected)

        # specifying prom_name should not cause an error
        expl_outputs = prob.model.p1.list_outputs(prom_name=True,
                                                  out_stream=None)
        self.assertEqual(dict(expl_outputs),
                         {'x': {
                             'value': 12.,
                             'prom_name': 'x'
                         }})

        # run model
        prob.set_solver_print(level=0)
        prob.run_model()

        # list_inputs tests
        # Can't do exact equality here because units cause comp.y to be slightly different than 12.0
        stream = StringIO()
        inputs = prob.model.list_inputs(units=True,
                                        shape=True,
                                        out_stream=stream)
        tol = 1e-7
        for actual, expected in zip(sorted(inputs), [('comp.x', {
                'value': [12.],
                'shape': (1, ),
                'units': 'inch'
        }), ('comp.y', {
                'value': [12.],
                'shape': (1, ),
                'units': 'inch'
        })]):
            self.assertEqual(expected[0], actual[0])
            self.assertEqual(expected[1]['units'], actual[1]['units'])
            self.assertEqual(expected[1]['shape'], actual[1]['shape'])
            assert_near_equal(expected[1]['value'], actual[1]['value'], tol)

        text = stream.getvalue().split('\n')
        expected_text = [
            "2 Input(s) in 'model'", "---------------------", "",
            "varname  value  units  shape", "-------  -----  -----  -----",
            "comp", "  x    [12.]  inch   (1,)", "  y    [12.]  inch   (1,)"
        ]
        for i, line in enumerate(expected_text):
            if line and not line.startswith('-'):
                self.assertEqual(
                    remove_whitespace(text[i]).replace('1L', ''),
                    remove_whitespace(line))

        # list_outputs tests

        # list outputs for implicit comps - should get none
        outputs = prob.model.list_outputs(implicit=True,
                                          explicit=False,
                                          out_stream=None)
        self.assertEqual(outputs, [])

        # list outputs with out_stream and all the optional display values True
        stream = StringIO()
        outputs = prob.model.list_outputs(values=True,
                                          units=True,
                                          shape=True,
                                          bounds=True,
                                          desc=True,
                                          residuals=True,
                                          scaling=True,
                                          print_arrays=False,
                                          out_stream=stream)

        self.assertEqual([
            ('comp.z', {
                'value': [24.],
                'resids': [0.],
                'units': 'inch',
                'shape': (1, ),
                'desc': '',
                'lower': None,
                'upper': None,
                'ref': 1.0,
                'ref0': 0.0,
                'res_ref': 1.0
            }),
            ('p1.x', {
                'value': [12.],
                'resids': [0.],
                'units': 'inch',
                'shape': (1, ),
                'desc': 'indep x',
                'lower': [1.],
                'upper': [100.],
                'ref': 1.1,
                'ref0': 2.1,
                'res_ref': 1.1
            }),
            ('p2.y', {
                'value': [1.],
                'resids': [0.],
                'units': 'ft',
                'shape': (1, ),
                'desc': 'indep y',
                'lower': [2.],
                'upper': [200.],
                'ref': 1.2,
                'ref0': 0.0,
                'res_ref': 2.2
            }),
        ], sorted(outputs))

        text = stream.getvalue().split('\n')
        expected_text = [
            "3 Explicit Output(s) in 'model'",
            "-------------------------------",
            "",
            "varname  value  resids  units  shape  lower  upper   ref  ref0  res_ref  desc",
            "-------  -----  ------  -----  -----  -----  ------  ---  ----  -------  -------",
            "p1",
            "  x    [12.]  [0.]    inch   (1,)   [1.]   [100.]  1.1  2.1   1.1      indep x",
            "p2",
            "  y    [1.]   [0.]    ft     (1,)   [2.]   [200.]  1.2  0.0   2.2      indep y",
            "comp",
            "  z    [24.]  [0.]    inch   (1,)   None   None    1.0  0.0   1.0",
            "",
            "",
            "0 Implicit Output(s) in 'model'",
            "-------------------------------",
        ]
        for i, line in enumerate(expected_text):
            if line and not line.startswith('-'):
                self.assertEqual(
                    remove_whitespace(text[i]).replace('1L', ''),
                    remove_whitespace(line))
    def test_list_inputs_outputs_with_parallel_comps(self):
        class TestComp(om.ExplicitComponent):
            def initialize(self):
                self.options['distributed'] = False

            def setup(self):
                self.add_input('x', shape=1)
                self.add_output('y', shape=1)
                self.declare_partials('y', 'x')

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

            def compute_partials(self, inputs, J):
                J['y', 'x'] = 2 * inputs['x']

        prob = om.Problem()
        model = prob.model

        model.add_subsystem('p1', om.IndepVarComp('x', 1.0))
        model.add_subsystem('p2', om.IndepVarComp('x', 1.0))

        parallel = model.add_subsystem('parallel', om.ParallelGroup())
        parallel.add_subsystem('c1', TestComp())
        parallel.add_subsystem('c2', TestComp())

        model.add_subsystem('c3', om.ExecComp(['y=3.0*x1+7.0*x2']))

        model.connect("parallel.c1.y", "c3.x1")
        model.connect("parallel.c2.y", "c3.x2")

        model.connect("p1.x", "parallel.c1.x")
        model.connect("p2.x", "parallel.c2.x")

        prob.setup()
        prob.run_model()

        stream = StringIO()
        prob.model.list_outputs(all_procs=True, out_stream=stream)

        if self.comm.rank == 0:

            text = stream.getvalue().split('\n')
            expected_text = [
                "5 Explicit Output(s) in 'model'",
                "-------------------------------",
                "",
                "varname     value",
                "----------  -----",
                "p1",
                "  x       [1.]",
                "p2",
                "  x       [1.]",
                "parallel",
                "  c1",
                "    y     [1.]",
                "  c2",
                "    y     [1.]",
                "c3",
                "  y       [10.]",
                "",
                "",
                "0 Implicit Output(s) in 'model'",
                "-------------------------------",
            ]
            for i, line in enumerate(expected_text):
                if line and not line.startswith('-'):
                    self.assertEqual(remove_whitespace(text[i]),
                                     remove_whitespace(line))

        stream = StringIO()
        prob.model.list_inputs(all_procs=True, out_stream=stream)

        if self.comm.rank == 0:

            text = stream.getvalue().split('\n')
            expected_text = [
                "4 Input(s) in 'model'",
                "---------------------",
                "",
                "varname     value",
                "----------  -----",
                "parallel",
                "  c1",
                "    x     [1.]",
                "  c2",
                "    x     [1.]",
                "c3",
                "  x1      [1.]",
                "  x2      [1.]",
            ]

            for i, line in enumerate(expected_text):
                if line and not line.startswith('-'):
                    self.assertEqual(remove_whitespace(text[i]),
                                     remove_whitespace(line))
Exemple #4
0
    def test_distribcomp_list_vars(self):
        print_opts = {'linewidth': 1024}

        if LooseVersion(np.__version__) >= LooseVersion("1.14"):
            print_opts['legacy'] = '1.13'

        size = 15

        model = om.Group()
        model.add_subsystem("indep", om.IndepVarComp('x', np.zeros(size)))
        model.add_subsystem("C2", DistribComp(size=size))
        model.add_subsystem("C3", Summer(size=size))

        model.connect('indep.x', 'C2.invec')
        model.connect('C2.outvec', 'C3.invec', src_indices=om.slicer[:])

        prob = om.Problem(model)
        prob.setup()

        # prior to model execution, the global shape of a distributed variable is not available
        # and only the local portion of the value is available
        stream = StringIO()
        with printoptions(**print_opts):
            model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True,
                                 print_arrays=True, out_stream=stream)

        if prob.comm.rank == 0:
            text = stream.getvalue().split('\n')

            expected = [
                "1 Input(s) in 'C2'",
                '',
                'varname  val              shape  global_shape',
                '-------  ---------------  -----  ------------',
                'invec    |3.87298334621|  (8,)   (15,)',
                '         val:',
                '         array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])'
            ]

            for i, line in enumerate(expected):
                if line and not line.startswith('-'):
                    self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line),
                                     '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        stream = StringIO()
        with printoptions(**print_opts):
            model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True,
                                  print_arrays=True, out_stream=stream)

        if prob.comm.rank == 0:
            text = stream.getvalue().split('\n')

            expected = [
                "1 Explicit Output(s) in 'C2'",
                '',
                'varname  val              shape  global_shape',
                '-------  ---------------  -----  ------------',
                'outvec   |3.87298334621|  (8,)   (15,)',
                '         val:',
                '         array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])'
            ]

            for i, line in enumerate(expected):
                if line and not line.startswith('-'):
                    self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line),
                                     '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        # run the model
        prob['indep.x'] = np.ones(size)
        prob.run_model()

        # after model execution, the global shape of a distributed variable is available
        # and the complete global value is available
        stream = StringIO()
        with printoptions(**print_opts):
            model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True,
                                 print_arrays=True, out_stream=stream)

        if prob.comm.rank == 0:
            text = stream.getvalue().split('\n')

            expected = [
                "1 Input(s) in 'C2'",
                '',
                'varname  val              shape  global_shape',
                '-------  ---------------  -----  ------------',
                'invec    |3.87298334621|  (8,)   (15,)',
                '         val:',
                '         array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])'
            ]
            for i, line in enumerate(expected):
                if line and not line.startswith('-'):
                    self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line),
                                     '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        stream = StringIO()
        with printoptions(**print_opts):
            model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True,
                                  print_arrays=True, out_stream=stream)

        if prob.comm.rank == 0:
            text = stream.getvalue().split('\n')

            expected = [
                "1 Explicit Output(s) in 'C2'",
                '',
                'varname  val             shape  global_shape',
                '-------  --------------  -----  ------------',
                'outvec   |9.74679434481|  (8,)   (15,)',
                '         val:',
                '         array([ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2., -3., -3., -3., -3., -3., -3., -3.])'
            ]
            for i, line in enumerate(expected):
                if line and not line.startswith('-'):
                    self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line),
                                     '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        stream = StringIO()
        with printoptions(**print_opts):
            model.C3.list_inputs(hierarchical=False, shape=True, global_shape=True, all_procs=True,
                                 print_arrays=True, out_stream=stream)

        text = stream.getvalue().split('\n')

        print('\n'.join(text))

        norm = '|9.74679434481|'
        shape = (15,)
        value = '[2., 2., 2., 2., 2., 2., 2., 2., -3., -3., -3., -3., -3., -3., -3.]'

        expected = [
            "1 Input(s) in 'C3'",
            '',
            'varname  val                  shape  global_shape',
            '-------  -------------------  -----  ------------',
            'invec  {}  {}   {}        '.format(norm, shape, shape),
            '         val:',
            '         array({})'.format(value),
        ]

        for i, line in enumerate(expected):
            if line and not line.startswith('-'):
                self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line),
                                 '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        assert_near_equal(prob['C3.sum'], -5.)
    def test_distribcomp_list_vars(self):
        from openmdao.test_suite.components.distributed_components import DistribComp, Summer

        print_opts = {'linewidth': 1024}

        from distutils.version import LooseVersion
        if LooseVersion(np.__version__) >= LooseVersion("1.14"):
            print_opts['legacy'] = '1.13'

        size = 15

        model = om.Group()
        model.add_subsystem("indep", om.IndepVarComp('x', np.zeros(size)))
        model.add_subsystem("C2", DistribComp(size=size))
        model.add_subsystem("C3", Summer(size=size))

        model.connect('indep.x', 'C2.invec')
        model.connect('C2.outvec', 'C3.invec')

        prob = om.Problem(model)
        prob.setup()

        # prior to model execution, the global shape of a distributed variable is not available
        # and only the local portion of the value is available
        stream = cStringIO()
        with printoptions(**print_opts):
            model.C2.list_inputs(hierarchical=False,
                                 shape=True,
                                 global_shape=True,
                                 print_arrays=True,
                                 out_stream=stream)

        if prob.comm.rank == 0:
            text = stream.getvalue().split('\n')

            expected = [
                "1 Input(s) in 'C2'", '------------------', '',
                'varname  value            shape  global_shape',
                '-------  ---------------  -----  ------------',
                'invec    |2.82842712475|  (8,)   Unavailable ',
                '         value:',
                '         array([1., 1., 1., 1., 1., 1., 1., 1.])'
            ]

            for i, line in enumerate(expected):
                if line and not line.startswith('-'):
                    self.assertEqual(
                        remove_whitespace(text[i]), remove_whitespace(line),
                        '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        stream = cStringIO()
        with printoptions(**print_opts):
            model.C2.list_outputs(hierarchical=False,
                                  shape=True,
                                  global_shape=True,
                                  print_arrays=True,
                                  out_stream=stream)

        if prob.comm.rank == 0:
            text = stream.getvalue().split('\n')

            expected = [
                "1 Explicit Output(s) in 'C2'", '----------------------------',
                '', 'varname  value            shape  global_shape',
                '-------  ---------------  -----  ------------',
                'outvec   |2.82842712475|  (8,)   Unavailable ',
                '         value:',
                '         array([1., 1., 1., 1., 1., 1., 1., 1.])'
            ]

            for i, line in enumerate(expected):
                if line and not line.startswith('-'):
                    self.assertEqual(
                        remove_whitespace(text[i]), remove_whitespace(line),
                        '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        # run the model
        prob['indep.x'] = np.ones(size)
        prob.run_model()

        # after model execution, the global shape of a distributed variable is available
        # and the complete global value is available
        stream = cStringIO()
        with printoptions(**print_opts):
            model.C2.list_inputs(hierarchical=False,
                                 shape=True,
                                 global_shape=True,
                                 print_arrays=True,
                                 out_stream=stream)

        if prob.comm.rank == 0:
            text = stream.getvalue().split('\n')

            expected = [
                "1 Input(s) in 'C2'", '------------------', '',
                'varname   value            shape  global_shape',
                '--------  ---------------  -----  ------------',
                'C2.invec  |3.87298334621|  (8,)   (15,)       ',
                '          value:',
                '          array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])'
            ]
            for i, line in enumerate(expected):
                if line and not line.startswith('-'):
                    self.assertEqual(
                        remove_whitespace(text[i]), remove_whitespace(line),
                        '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        stream = cStringIO()
        with printoptions(**print_opts):
            model.C2.list_outputs(hierarchical=False,
                                  shape=True,
                                  global_shape=True,
                                  print_arrays=True,
                                  out_stream=stream)

        if prob.comm.rank == 0:
            text = stream.getvalue().split('\n')

            expected = [
                "1 Explicit Output(s) in 'C2'", '----------------------------',
                '', 'varname    value           shape  global_shape',
                '---------  --------------  -----  ------------',
                'C2.outvec  |9.74679434481|  (8,)   (15,)       ',
                '           value:',
                '           array([ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2., -3., -3., -3., -3., -3., -3., -3.])'
            ]
            for i, line in enumerate(expected):
                if line and not line.startswith('-'):
                    self.assertEqual(
                        remove_whitespace(text[i]), remove_whitespace(line),
                        '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        # note that the shape of the input variable for the non-distributed Summer component
        # is different on each processor, use the all_procs argument to display on all processors
        stream = cStringIO()
        with printoptions(**print_opts):
            model.C3.list_inputs(hierarchical=False,
                                 shape=True,
                                 global_shape=True,
                                 all_procs=True,
                                 print_arrays=True,
                                 out_stream=stream)

        text = stream.getvalue().split('\n')

        if prob.comm.rank == 0:
            norm = '|5.65685424949|'
            shape = (8, )
            value = '[2., 2., 2., 2., 2., 2., 2., 2.]'
        else:
            norm = '|7.93725393319|'
            shape = (7, )
            value = '[-3., -3., -3., -3., -3., -3., -3.]'

        expected = [
            "1 Input(s) in 'C3'",
            '------------------',
            '',
            'varname   value                shape  global_shape',
            '--------  -------------------  -----  ------------',
            'C3.invec  {}  {}   {}        '.format(norm, shape, shape),
            '          value:',
            '          array({})'.format(value),
        ]

        for i, line in enumerate(expected):
            if line and not line.startswith('-'):
                self.assertEqual(
                    remove_whitespace(text[i]), remove_whitespace(line),
                    '\nExpected: %s\nReceived: %s\n' % (line, text[i]))

        assert_rel_error(self, prob['C3.out'], -5.)
Exemple #6
0
    def test_list_inputs_outputs_promoted(self):
        model = om.Group()

        model.add_subsystem('expl', ModCompEx(3), promotes_inputs=['x'])
        model.add_subsystem('impl', ModCompIm(3), promotes_inputs=['x'])

        prob = om.Problem(model)
        prob.setup()

        prob['x'] = 11

        prob.run_model()

        #
        # list inputs
        #
        stream = StringIO()

        model.list_inputs(hierarchical=False, prom_name=True, out_stream=stream)

        text = stream.getvalue().split('\n')

        expected = [
            "3 Input(s) in 'model'",
            "---------------------",
            "",
            "varname  value  prom_name",
            "-------  -----  ---------",
            "expl.a   [10.]  expl.a",
            "expl.x   11     x",
            "impl.x   11     x",
        ]

        for i, line in enumerate(expected):
            if line and not line.startswith('-'):
                self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line))

        #
        # list outputs
        #
        stream = StringIO()

        model.list_outputs(prom_name=True, out_stream=stream, list_autoivcs=True)

        text = stream.getvalue().split('\n')

        expected = [
            "4 Explicit Output(s) in 'model'",
            "-------------------------------",
            "",
            "varname  value  prom_name",
            "-------  -----  ---------",
            "_auto_ivc",
            "  v0    [10.]  _auto_ivc.v0",
            "  v1    11     _auto_ivc.v1",
            "expl",
            "  b    [20.]  expl.b",
            "  y    2      expl.y",
            "",
            "",
            "1 Implicit Output(s) in 'model'",
            "-------------------------------",
            "",
            "varname  value  prom_name",
            "-------  -----  ---------",
            "impl",
            "  y    2      impl.y",
        ]

        for i, line in enumerate(expected):
            if line and not line.startswith('-'):
                self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line))