Exemplo n.º 1
0
def check_replace():
    print(("=== " + sys._getframe().f_code.co_name))
    print("= check replace std")
    template = create_template()
    template_out = template + ".replaced"
    coupling_tools.replace(infile=template, outfile=template_out,
                           tokens=["@E"], values=[2])
    check_outfile(template_out, semi_parsed)
    remove_file(template_out)
    remove_file(template)

    print("= check replace more vars")
    template = create_template()
    coupling_tools.replace(infile=template, outfile=template_out,
                           tokens=["@E", "@F", "@Z"],
                           values=[1.6, 5, 6])
    check_outfile(template_out, parsed)
    remove_file(template_out)
    remove_file(template)

    print("= check replace inplace")
    template = create_template()
    coupling_tools.replace(infile=template, outfile=template,
                           tokens=["@E", "@F", "@Z"], values=[1.6, 5, 6])
    check_outfile(template, parsed)
    remove_file(template)

    print("= check replace inplace with None")
    template = create_template()
    coupling_tools.replace(infile=template, outfile=None,
                           tokens=["@E", "@F", "@Z"], values=[1.6, 5, 6])
    check_outfile(template, parsed)
    remove_file(template)

    print("= check replace big template")
    start_time = time.time()
    template = create_big_template()
    sys.stderr.write(
        "big template created in : " + str(time.time() - start_time) + "s\n")

    template_out = template + ".replaced"
    start_time = time.time()
    coupling_tools.replace(infile=template, outfile=template_out,
                           tokens=["@E"], values=[2])
    time_to_parse = str(int(time.time() - start_time))
    check_outfile(template_out, semi_parsed)
    remove_file(template_out)
    remove_file(template)
    sys.stderr.write("parsed template in: " + time_to_parse + "s\n")
    # parsed template=3G -> 25s on bx (ssd, core [email protected])
    if int(time_to_parse) > max_time:
        print(('time to get token took too long (should be ' +
               str(max_time) + 's max)'))
        exit(1)
    else:
        print('check replace big template: ok')
Exemplo n.º 2
0
def check_replace():
    print(("=== " + sys._getframe().f_code.co_name))
    print("= check replace std")
    template = create_template()
    template_out = template + ".replaced"
    coupling_tools.replace(infile=template, outfile=template_out,
                           tokens=["@E"], values=[2])
    check_outfile(template_out, semi_parsed)
    remove_file(template_out)
    remove_file(template)

    print("= check replace more vars")
    template = create_template()
    coupling_tools.replace(infile=template, outfile=template_out,
                           tokens=["@E", "@F", "@Z"],
                           values=[1.6, 5, 6])
    check_outfile(template_out, parsed)
    remove_file(template_out)
    remove_file(template)

    print("= check replace inplace")
    template = create_template()
    coupling_tools.replace(infile=template, outfile=template,
                           tokens=["@E", "@F", "@Z"], values=[1.6, 5, 6])
    check_outfile(template, parsed)
    remove_file(template)

    print("= check replace inplace with None")
    template = create_template()
    coupling_tools.replace(infile=template, outfile=None,
                           tokens=["@E", "@F", "@Z"], values=[1.6, 5, 6])
    check_outfile(template, parsed)
    remove_file(template)

    print("= check replace big template")
    start_time = time.time()
    template = create_big_template()
    sys.stderr.write(
        "big template created in : " + str(time.time() - start_time) + "s\n")

    template_out = template + ".replaced"
    start_time = time.time()
    coupling_tools.replace(infile=template, outfile=template_out,
                           tokens=["@E"], values=[2])
    time_to_parse = str(int(time.time() - start_time))
    check_outfile(template_out, semi_parsed)
    remove_file(template_out)
    remove_file(template)
    sys.stderr.write("parsed template in: " + time_to_parse + "s\n")
    # parsed template=3G -> 25s on bx (ssd, core [email protected])
    if int(time_to_parse) > max_time:
        print(('time to get token took too long (should be ' +
               str(max_time) + 's max)'))
        exit(1)
    else:
        print('check replace big template: ok')
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
Exemplo n.º 4
0
    def _create_input_file(self, X):
        """Create the input file required by the code.

        Replace the values of the vector :math:`X` to their corresponding tokens
        on the :file:`beam_input_template.xml` and create the input file :file:`beam.xml`
        on the current working directory.

        Parameters
        ----------
        X : 1D array (e.g. ot.Point or a 1D np.array)
            Input vector of size :math:`n` on which the model will be evaluated
        """
        otct.replace(self.input_template, 'beam.xml', ['@F', '@E', '@L', '@I'],
                     X)
Exemplo n.º 5
0
 def _create_input_file(self, X):
     coupling_tools.replace(
           self.template,
           'beam.xml',
           ['@F','@E','@L','@I'],
          X)
#
# `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.

# %%
X = [1.2, 45, 91.8]
infile = "input_template.txt"
outfile = "input.txt"
tokens = ["@X0", "@X1", "@X2"]
ct.replace(infile, outfile, tokens, X)

# %%
# To see the change, let us look at the `input.txt` file.

# %%
f = open("input.txt", "r")
print(f.read())

# %%
# 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)`