Ejemplo n.º 1
0
    def read_outputs_file(self, file, outputs, discrete_outputs=None):
        # type: (Union[str, etree._ElementTree], Vector, Optional[dict]) -> None
        """Read the outputs from a given XML file and store them in this `Component`'s variables.

        Parameters
        ----------
            file : str or :obj:`etree._ElementTree`
                Path to or :obj:`etree._ElementTree` of an output XML file.

            outputs : Vector
                Output vector of this `Component`.

            discrete_outputs : dict
                Discrete (i.e. not treated as floats) outputs.
        """
        output_rename_map = self.output_rename_map
        discrete_output_rename_map = self.discrete_output_rename_map

        # Extract the results from the output xml
        for xpath, value in xml_to_dict(file).items():
            name = xpath_to_param(xpath)
            if name in self.outputs_from_xml:
                # Rename output
                if name in output_rename_map:
                    name = output_rename_map[name][0]
                elif name in discrete_output_rename_map:
                    name = discrete_output_rename_map[name][0]

                if name in outputs:
                    outputs[name] = value
                elif discrete_outputs is not None and name in discrete_outputs:
                    discrete_outputs[name] = value
Ejemplo n.º 2
0
    def initialize_from_xml(self, xml):
        # type: (Union[str, _ElementTree]) -> None
        """Initialize the problem with initial values from an XML file.

        Parameters
        ----------
            xml : str or :obj:`etree._ElementTree`
                Path to an XML file or an instance of `etree._ElementTree` representing it.
                """
        self.initialize()
        for xpath, value in xml_to_dict(xml).items():
            name = xpath_to_param(xpath)
            if name in self.model._var_allprocs_prom2abs_list['input'] or \
                    name in self.model._var_allprocs_prom2abs_list['output']:
                self[name] = value
            if name in self.model.mapped_parameters_inv:
                for mapping in self.model.mapped_parameters_inv[name]:
                    if mapping in self.model._var_allprocs_prom2abs_list['input'] or \
                    mapping in self.model._var_allprocs_prom2abs_list['output']:
                        try:
                            self[mapping] = value
                        except RuntimeError as e:
                            if 'The promoted name' in e[0] and 'is invalid' in e[0]:
                                warnings.warn('Could not automatically set this invalid promoted name from the XML: '
                                              '{}.'.format(mapping))
                            else:
                                raise RuntimeError(e)
Ejemplo n.º 3
0
    def initialize_from_xml(self, xml):
        # type: (Union[str, _ElementTree]) -> None
        """Initialize the problem with initial values from an XML file.

        Parameters
        ----------
            xml : str or :obj:`etree._ElementTree`
                Path to an XML file or an instance of `etree._ElementTree` representing it.
                """
        self.initialize()
        for xpath, value in xml_to_dict(xml).items():
            name = xpath_to_param(xpath)
            prom2abs_list_inputs = self.model._var_allprocs_prom2abs_list[
                'input']
            prom2abs_list_outputs = self.model._var_allprocs_prom2abs_list[
                'output']
            if self.comm.size > 1:  # Small workaround for issue in OpenMDAO with mpirun
                if name in prom2abs_list_inputs:
                    for abs_name in prom2abs_list_inputs[name]:
                        self[abs_name] = value
                if name in prom2abs_list_outputs:
                    for abs_name in prom2abs_list_outputs[name]:
                        self[abs_name] = value
            else:
                if name in prom2abs_list_inputs or name in prom2abs_list_outputs:
                    self[name] = value
            if name in self.model.mapped_parameters_inv:
                for mapping in self.model.mapped_parameters_inv[name]:
                    if mapping in prom2abs_list_inputs or mapping in prom2abs_list_outputs:
                        try:
                            # Small workaround for issue in OpenMDAO with mpirun
                            if self.comm.size > 1:
                                abs_names = []
                                if mapping in prom2abs_list_inputs:
                                    abs_names.extend([
                                        abs_name for abs_name in
                                        prom2abs_list_inputs[mapping]
                                    ])
                                if mapping in prom2abs_list_outputs:
                                    abs_names.extend([
                                        abs_name for abs_name in
                                        prom2abs_list_outputs[mapping]
                                    ])
                                for abs_name in abs_names:
                                    self[abs_name] = value
                            else:
                                self[mapping] = value
                        except RuntimeError as e:
                            if 'The promoted name' in e[
                                    0] and 'is invalid' in e[0]:
                                warnings.warn(
                                    'Could not automatically set this invalid promoted '
                                    'name from the XML: {}.'.format(mapping))
                            else:
                                raise RuntimeError(e)
Ejemplo n.º 4
0
    def set_outputs_from_xml(self, output_xml):
        # type: (Union[str, etree._ElementTree]) -> None
        """Set outputs to the `Component` based on an output XML template file.

        Parameter names correspond to their XML elements' full XPaths, converted to valid ``OpenMDAO`` names using the
        `xpath_to_param()` method.

        Parameters
        ----------
            output_xml : str or :obj:`etree._ElementTree`
                Path to or an `etree._ElementTree` of an output XML file.
        """
        self.outputs_from_xml.clear()
        for xpath, value in xml_to_dict(output_xml).items():
            name = xpath_to_param(xpath)
            self.outputs_from_xml.update({name: value})
Ejemplo n.º 5
0
    def initialize_from_xml(self, xml):
        # type: (Union[str, _ElementTree]) -> None
        """Initialize the problem with initial values from an XML file.

        This function can only be called after the problem's setup method has been called.

        Parameters
        ----------
            xml : str or :obj:`etree._ElementTree`
                Path to an XML file or an instance of `etree._ElementTree` representing it.
        """
        for xpath, value in xml_to_dict(xml).items():
            name = xpath_to_param(xpath)
            if name in self._outputs:
                self._outputs[name] = value
            elif name in self._inputs:
                self._inputs[name] = value
Ejemplo n.º 6
0
    def read_outputs_file(self, file, outputs):
        # type: (Union[str, etree._ElementTree], Vector) -> None
        """Read the outputs from a given XML file and store them in this `Component`'s variables.

        Parameters
        ----------
            file : str or :obj:`etree._ElementTree`
                Path to or :obj:`etree._ElementTree` of an output XML file.

            outputs : Vector
                Output vector of this `Component`.
        """
        # Extract the results from the output xml
        for xpath, value in xml_to_dict(file).items():
            name = xpath_to_param(xpath)
            if name in self.outputs_from_xml and name in outputs:
                outputs[name] = value