Exemplo n.º 1
0
    def test_pyxdsm_sellar_no_recurse(self):
        """Makes XDSM for the Sellar problem, with no recursion."""

        filename = FILENAME + '1'
        prob = Problem()
        prob.model = model = SellarNoDerivatives()
        model.add_design_var('z',
                             lower=np.array([-10.0, 0.0]),
                             upper=np.array([10.0, 10.0]),
                             indices=np.arange(2, dtype=int))
        model.add_design_var('x', lower=0.0, upper=10.0)
        model.add_objective('obj')
        model.add_constraint('con1', equals=np.zeros(1))
        model.add_constraint('con2', upper=0.0)

        prob.setup(check=False)
        prob.final_setup()

        # Write output
        write_xdsm(prob,
                   filename=filename,
                   out_format='tex',
                   quiet=True,
                   show_browser=False,
                   recurse=False)
        # Check if file was created
        self.assertTrue(os.path.isfile('.'.join([filename, 'tex'])))
Exemplo n.º 2
0
    def test_xdsmjs_embeddable(self):
        """
        Makes XDSMjs HTML file for the Sellar problem.

        The HTML file is embeddable (no head and body tags).
        """

        filename = 'xdsmjs_embeddable'  # this name is needed for XDSMjs
        prob = Problem()
        prob.model = model = SellarNoDerivatives()
        model.add_design_var('z',
                             lower=np.array([-10.0, 0.0]),
                             upper=np.array([10.0, 10.0]),
                             indices=np.arange(2, dtype=int))
        model.add_design_var('x', lower=0.0, upper=10.0)
        model.add_objective('obj')
        model.add_constraint('con1', equals=np.zeros(1))
        model.add_constraint('con2', upper=0.0)

        prob.setup(check=False)
        prob.final_setup()

        # Write output
        write_xdsm(prob,
                   filename=filename,
                   out_format='html',
                   subs=(),
                   quiet=True,
                   show_browser=False,
                   embed_data=True,
                   embeddable=True)
        # Check if file was created
        self.assertTrue(os.path.isfile('.'.join([filename, 'html'])))
Exemplo n.º 3
0
    def test_pyxdsm_sphere(self):
        """
        Makes an XDSM of the Sphere test case. It also adds a design variable, constraint and
        objective.
        """
        class Rosenbrock(ExplicitComponent):
            def __init__(self, problem):
                super(Rosenbrock, self).__init__()
                self.problem = problem
                self.counter = 0

            def setup(self):
                self.add_input('x', np.array([1.5, 1.5]))
                self.add_output('f', 0.0)
                self.declare_partials('f',
                                      'x',
                                      method='fd',
                                      form='central',
                                      step=1e-4)

            def compute(self,
                        inputs,
                        outputs,
                        discrete_inputs=None,
                        discrete_outputs=None):
                x = inputs['x']
                outputs['f'] = sum(x**2)

        x0 = np.array([1.2, 1.5])
        filename = FILENAME + '2'

        prob = Problem()
        indeps = prob.model.add_subsystem('indeps',
                                          IndepVarComp(problem=prob),
                                          promotes=['*'])
        indeps.add_output('x', list(x0))

        prob.model.add_subsystem('sphere',
                                 Rosenbrock(problem=prob),
                                 promotes=['*'])
        prob.model.add_subsystem('con',
                                 ExecComp('c=sum(x)', x=np.ones(2)),
                                 promotes=['*'])
        prob.driver = ScipyOptimizeDriver()
        prob.model.add_design_var('x')
        prob.model.add_objective('f')
        prob.model.add_constraint('c', lower=1.0)

        prob.setup(check=False)
        prob.final_setup()

        # Write output
        write_xdsm(prob,
                   filename=filename,
                   out_format='tex',
                   quiet=True,
                   show_browser=False)
        # Check if file was created
        self.assertTrue(os.path.isfile('.'.join([filename, 'tex'])))
Exemplo n.º 4
0
def _xdsm_cmd(options):
    """
    Process command line args and call xdsm on the specified file.

    Parameters
    ----------
    options : argparse Namespace
        Command line options.
    """
    filename = options.file[0]

    kwargs = {}
    for name in [
            'box_stacking', 'box_width', 'box_lines', 'numbered_comps',
            'number_alignment'
    ]:
        val = getattr(options, name)
        if val is not None:
            kwargs[name] = val

    if filename.endswith('.py'):
        # the file is a python script, run as a post_setup hook
        def _xdsm(prob):
            write_xdsm(prob,
                       filename=options.outfile,
                       model_path=options.model_path,
                       recurse=options.recurse,
                       include_external_outputs=not options.no_extern_outputs,
                       out_format=options.format,
                       include_solver=options.include_solver,
                       subs=_CHAR_SUBS,
                       show_browser=not options.no_browser,
                       show_parallel=not options.no_parallel,
                       add_process_conns=not options.no_process_conns,
                       output_side=options.output_side,
                       legend=options.legend,
                       **kwargs)
            exit()

        options.func = lambda options: _xdsm

        _post_setup_exec(options)
    else:
        # assume the file is a recording, run standalone
        write_xdsm(filename,
                   filename=options.outfile,
                   model_path=options.model_path,
                   recurse=options.recurse,
                   include_external_outputs=not options.no_extern_outputs,
                   out_format=options.format,
                   include_solver=options.include_solver,
                   subs=_CHAR_SUBS,
                   show_browser=not options.no_browser,
                   show_parallel=not options.no_parallel,
                   add_process_conns=not options.no_process_conns,
                   output_side=options.output_side,
                   **kwargs)
Exemplo n.º 5
0
 def _xdsm(prob):
     write_xdsm(prob,
                filename=options.outfile,
                model_path=options.model_path,
                recurse=options.recurse,
                include_external_outputs=not options.no_extern_outputs,
                out_format=options.format,
                include_solver=options.include_solver,
                subs=_CHAR_SUBS,
                show_browser=not options.no_browser,
                show_parallel=not options.no_parallel,
                add_process_conns=not options.no_process_conns,
                output_side=options.output_side,
                **kwargs)
     exit()
Exemplo n.º 6
0
    def test_wrong_out_format(self):
        """Incorrect output format error."""

        filename = 'xdsm'  # this name is needed for XDSMjs
        prob = Problem()
        prob.model = SellarNoDerivatives()

        prob.setup(check=False)
        prob.final_setup()

        # no output checking, just make sure no exceptions raised
        with self.assertRaises(ValueError):
            write_xdsm(prob,
                       filename=filename,
                       out_format='jpg',
                       subs=(),
                       quiet=True,
                       show_browser=False)
Exemplo n.º 7
0
    def test_pyxdsm_identical_relative_names(self):
        class TimeComp(ExplicitComponent):
            def setup(self):
                self.add_input('t_initial', val=0.)
                self.add_input('t_duration', val=1.)
                self.add_output('time', shape=(2, ))

            def compute(self, inputs, outputs):
                t_initial = inputs['t_initial']
                t_duration = inputs['t_duration']

                outputs['time'][0] = t_initial
                outputs['time'][1] = t_initial + t_duration

        class Phase(Group):
            def setup(self):
                super(Phase, self).setup()

                indep = IndepVarComp()
                for var in ['t_initial', 't_duration']:
                    indep.add_output(var, val=1.0)

                self.add_subsystem('time_extents',
                                   indep,
                                   promotes_outputs=['*'])

                time_comp = TimeComp()
                self.add_subsystem('time', time_comp)

                self.connect('t_initial', 'time.t_initial')
                self.connect('t_duration', 'time.t_duration')

                self.set_order(['time_extents', 'time'])

        p = Problem()
        p.driver = ScipyOptimizeDriver()
        orbit_phase = Phase()
        p.model.add_subsystem('orbit_phase', orbit_phase)

        systems_phase = Phase()
        p.model.add_subsystem('systems_phase', systems_phase)

        systems_phase = Phase()
        p.model.add_subsystem('extra_phase', systems_phase)
        p.model.add_design_var('orbit_phase.t_initial')
        p.model.add_design_var('orbit_phase.t_duration')
        p.setup(check=True)

        p.run_model()
        # Test non unique local names
        write_xdsm(p,
                   'xdsm3',
                   out_format='tex',
                   quiet=True,
                   show_browser=False)
        self.assertTrue(os.path.isfile('.'.join(['xdsm3', 'tex'])))
        self.assertTrue(os.path.isfile('.'.join(['xdsm3', 'pdf'])))

        # Check formatting

        # Max character box formatting
        write_xdsm(p,
                   'xdsm4',
                   out_format='tex',
                   quiet=True,
                   show_browser=False,
                   box_stacking='cut_chars',
                   box_width=15)
        self.assertTrue(os.path.isfile('.'.join(['xdsm4', 'tex'])))
        self.assertTrue(os.path.isfile('.'.join(['xdsm4', 'pdf'])))
        # Cut characters box formatting
        write_xdsm(p,
                   'xdsm5',
                   out_format='tex',
                   quiet=True,
                   show_browser=False,
                   box_stacking='max_chars',
                   box_width=15)
        self.assertTrue(os.path.isfile('.'.join(['xdsm5', 'tex'])))
        self.assertTrue(os.path.isfile('.'.join(['xdsm5', 'pdf'])))
        write_xdsm(p, 'xdsmjs_orbit', out_format='html', show_browser=False)

        self.assertTrue(os.path.isfile('.'.join(['xdsmjs_orbit', 'html'])))