.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/discipline/plot_interface_exec.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_discipline_plot_interface_exec.py: Create a discipline from an external executable =============================================== .. GENERATED FROM PYTHON SOURCE LINES 27-40 .. code-block:: default from __future__ import division, unicode_literals import os import subprocess from numpy import array from gemseo.api import configure_logger from gemseo.core.discipline import MDODiscipline configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 41-53 Introduction ------------ Let's consider a binary software computing the float output :math:`c = a^2 + b^2` from two float inputs : :code:`'a'` and :code:`'b'`. The inputs are read in the :code:`'inputs.txt'` file which looks like: `a=1 b=2` and the output is written to: :code:`'outputs.txt'` which looks like `c=5`. Then, the executable can be run using the shell command :code:`'python run.py'`. Let's make a discipline out of this from an initial :code:`'inputs.txt'`. .. GENERATED FROM PYTHON SOURCE LINES 56-75 Implementation of the discipline -------------------------------- The construction of :class:`.MDODiscipline` consists in three steps: 1. Instantiate the :class:`.MDODiscipline` using the super constructor, 2. Initialize the grammars using the :meth:`.JSONGrammar.initialize_from_data_names` method, 3. Set the default inputs from the initial :code:`'inputs.txt'` The :class:`!MDODiscipline._run` method consists in three steps: 1. Get the input data from :attr:`!MDODiscipline.local_data` and write the :code:`'inputs.txt'` file, 2. Run the executable using the :code:`subprocess.run()` command (`see more `_), 3. Get the output values and store them to :attr:`!MDODiscipline.local_data`. Now you can implement the discipline in the following way: .. GENERATED FROM PYTHON SOURCE LINES 75-112 .. code-block:: default def parse_file(file_path): data = {} with open(file_path) as inf: for line in inf.readlines(): if len(line) == 0: continue name, value = line.replace("\n", "").split("=") data[name] = array([float(value)]) return data def write_file(data, file_path): with open(file_path, "w") as outf: for name, value in list(data.items()): outf.write(name + "=" + str(value[0]) + "\n") class ShellExecutableDiscipline(MDODiscipline): def __init__(self): super(ShellExecutableDiscipline, self).__init__("ShellDisc") self.input_grammar.initialize_from_data_names(["a", "b"]) self.output_grammar.initialize_from_data_names(["c"]) self.default_inputs = {"a": array([1.0]), "b": array([2.0])} def _run(self): cwd = os.getcwd() inputs_file = os.path.join(cwd, "inputs.txt") outputs_file = os.path.join(cwd, "outputs.txt") write_file(self.local_data, inputs_file) subprocess.run("python run.py".split(), cwd=cwd) outputs = parse_file(outputs_file) self.local_data.update(outputs) .. GENERATED FROM PYTHON SOURCE LINES 113-116 Execution of the discipline --------------------------- Now we can run it with default input values: .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: default shell_disc = ShellExecutableDiscipline() print(shell_disc.execute()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'a': array([1.]), 'b': array([2.]), 'c': array([5.])} .. GENERATED FROM PYTHON SOURCE LINES 120-121 or run it with new input values: .. GENERATED FROM PYTHON SOURCE LINES 121-122 .. code-block:: default print(shell_disc.execute({"a": array([2.0]), "b": array([3.0])})) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'a': array([2.]), 'b': array([3.]), 'c': array([13.])} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.082 seconds) .. _sphx_glr_download_examples_discipline_plot_interface_exec.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_interface_exec.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_interface_exec.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_