.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_functional_modeling/link_to_an_external_code/plot_link_computer_code_coupling_tools.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_auto_functional_modeling_link_to_an_external_code_plot_link_computer_code_coupling_tools.py: Link to a computer code with coupling tools =========================================== .. GENERATED FROM PYTHON SOURCE LINES 6-7 In this example we show how to use the `coupling_tools` module, which allows to create a function from a computer code based on text file exchanges. We show the main features of the module on a simple example and focus on the `replace` and `get` functions. .. GENERATED FROM PYTHON SOURCE LINES 9-34 Introduction ------------ The `coupling_tools` module is useful when the external computer code reads (on input) and write (on output) text files. .. figure:: ../../_static/link_computer_code.svg :align: center :width: 50% File exchange coupling The main features of the `coupling_tools` module are: * `replace`: writes a file based on a file template, by replacing tokens with values, * `execute`: executes an external computer code, * `get` (and `get_line_col`): reads values from a file. Moreover, the `coupling_tools` module can be useful outside the library, for example to evaluate a design of experiments on a cluster. There are several advantages over basic Python scripting while using the module. * It is much simpler than using regular expressions. * Skipping lines, columns or text blocks is allowed. It is easy to use if the input or output files are based on structured text files. With a little more Python scripting, it is even possible to parallelize it. .. GENERATED FROM PYTHON SOURCE LINES 36-48 Example ------- We have the computer code in the `external_program.py` script: * it reads the `"input.txt"` file, * evaluates the output, * writes the `"output.txt"` file. The command line to evaluate the code is: `python external_program.py input.py` .. GENERATED FROM PYTHON SOURCE LINES 50-57 .. code-block:: default import openturns as ot import openturns.coupling_tools as ct import sys import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 58-59 The following is the content of the `external_program.py` script. .. GENERATED FROM PYTHON SOURCE LINES 61-80 .. code-block:: default code = ''' # -*- coding: utf-8 -*- # 1. Get input import sys inFile = sys.argv[1] exec(open(inFile).read()) # 2. Compute Y0 = X0 + X1 + X2 Y1 = X0 + X1 * X2 # 3. Write output f = open("output.txt", "w") f.write("Y0=%.17e\\n" % (Y0)) f.write("Y1=%.17e\\n" % (Y1)) f.close() ''' .. GENERATED FROM PYTHON SOURCE LINES 81-85 .. code-block:: default f = open("external_program.py", "w") f.write(code) f.close() .. GENERATED FROM PYTHON SOURCE LINES 86-87 Let us see the content of the `input.txt` file: the content is in Python format, so that reading the file is easier. .. GENERATED FROM PYTHON SOURCE LINES 89-95 .. code-block:: default content = ''' X0=1.2 X1=45 X2=91.8 ''' .. GENERATED FROM PYTHON SOURCE LINES 96-100 .. code-block:: default f = open("input.txt", "w") f.write(content) f.close() .. GENERATED FROM PYTHON SOURCE LINES 101-102 The content of the `output.txt` file has a simple format. .. GENERATED FROM PYTHON SOURCE LINES 104-109 .. code-block:: default content = ''' Y0=1.38e+02 Y1=4.1322e+03 ''' .. GENERATED FROM PYTHON SOURCE LINES 110-114 .. code-block:: default f = open("output.txt", "w") f.write(content) f.close() .. GENERATED FROM PYTHON SOURCE LINES 115-116 The `input_template.py` file is a template which will be used to generate the actual file `input.txt`. .. GENERATED FROM PYTHON SOURCE LINES 118-124 .. code-block:: default content = ''' X0=@X0 X1=@X1 X2=@X2 ''' .. GENERATED FROM PYTHON SOURCE LINES 125-130 .. code-block:: default f = open("input_template.txt", "w") f.write(content) f.close() .. GENERATED FROM PYTHON SOURCE LINES 131-136 The simulator is implemented this way: * we first use the `replace` function in order to generate the actual input file, * then we evaluate the external computer code with a system command with the `execute` function, * and we read the output file with the `get` function. .. GENERATED FROM PYTHON SOURCE LINES 138-153 .. code-block:: default def mySimulator ( X ): # 1. Create input file infile ="input_template.txt" outfile = "input.txt" tokens =["@X0","@X1","@X2"] ct.replace (infile , outfile ,tokens ,X) # 2. Compute program = sys.executable + " external_program.py" cmd = program +" "+ outfile ct.execute (cmd) # 3. Parse output file Y = ct.get ("output.txt", tokens =["Y0=","Y1="]) return Y .. GENERATED FROM PYTHON SOURCE LINES 154-155 In order to create the function, we simply use the `PythonFunction` class. .. GENERATED FROM PYTHON SOURCE LINES 157-159 .. code-block:: default myWrapper = ot.PythonFunction (3 ,2 , mySimulator ) .. GENERATED FROM PYTHON SOURCE LINES 160-161 We can check that this function can be evaluated. .. GENERATED FROM PYTHON SOURCE LINES 163-167 .. code-block:: default X = [1.2 , 45, 91.8] Y = myWrapper(X) print(Y) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [138,4132.2] .. GENERATED FROM PYTHON SOURCE LINES 168-181 Write the input file with the replace function ---------------------------------------------- The simplest calling sequence is: `replace(infile, outfile, tokens, values)` where * `infile` is a string, the (template) file to read, * `outfile` is a string, the file to write, * `tokens` is a list of N items, the regular expressions to find, * `values` is a list of N items (strings, floats, etc...), the values to replace. .. GENERATED FROM PYTHON SOURCE LINES 183-189 .. code-block:: default X = [1.2 , 45, 91.8] infile ="input_template.txt" outfile ="input.txt" tokens =["@X0", "@X1", "@X2"] ct.replace (infile , outfile , tokens , X) .. GENERATED FROM PYTHON SOURCE LINES 190-191 To see the change, let us look at the `input.txt` file. .. GENERATED FROM PYTHON SOURCE LINES 193-196 .. code-block:: default f = open("input.txt", "r") print(f.read()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none X0=1.2 X1=45 X2=91.8 .. GENERATED FROM PYTHON SOURCE LINES 197-216 Read the output with the get function ------------------------------------- The simplest calling sequence to get a list of values is: `Y = get(filename, tokens=None, skip_tokens=None, skip_lines=None, skip_cols=None)` where * `filename` is a string (the file to read), * `tokens` is a list of N items (regular expressions to search), * `skip_tokens` is a list of N items (number of tokens to ignore before reading the value), * `skip_lines` is a list of N items (number of rows to ignore before reading the value), * `skip_cols` is a list of N items (number of columns to ignore before reading the value), * `Y` is a list of N floats. And to get a single value: `Y = get_value(filename, token=None, skip_token=0, skip_line=0, skip_col=0)` .. GENERATED FROM PYTHON SOURCE LINES 218-219 Consider for example the following file. .. GENERATED FROM PYTHON SOURCE LINES 221-227 .. code-block:: default content = ''' 1 2 3 04 5 6 7 8 9 10 11 12 13 14 ''' .. GENERATED FROM PYTHON SOURCE LINES 228-232 .. code-block:: default f = open("results.txt", "w") f.write(content) f.close() .. GENERATED FROM PYTHON SOURCE LINES 233-234 We want to read the number `9`. .. GENERATED FROM PYTHON SOURCE LINES 236-238 .. code-block:: default Y = ct.get_value ("results.txt" , skip_line = 1, skip_col = 2) Y .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 3.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.053 seconds) .. _sphx_glr_download_auto_functional_modeling_link_to_an_external_code_plot_link_computer_code_coupling_tools.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_link_computer_code_coupling_tools.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_link_computer_code_coupling_tools.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_