f.write('X1=@X1\n') f.write('X2=@X2\n') with open('external_program0y.py', 'w') as f: f.write('import sys\n') f.write('exec(open("input.txt").read())\n') f.write('Y0=X0+X1+X2\n') f.write('Y1=X0+X1*X2\n') f.write('with open("output.txt", "w") as f:\n') f.write(' f.write("Y0=%.17e\\n" % Y0)\n') f.write(' f.write("Y1=%.17e\\n" % Y1)\n') input_file = persalys.CouplingInputFile('input_template0y.txt') input_file.setConfiguredPath('input.txt') input_file.setVariables(['X0', 'X1', 'X2'], ['@X0', '@X1', '@X2'], ['', '', '']) resource_file = persalys.CouplingResourceFile('external_program0y.py') output_file = persalys.CouplingOutputFile('output.txt') output_file.setVariables(['Y0', 'Y1'], ['Y0=', 'Y1='], [0, 0], [0, 0], [0, 0]) step = persalys.CouplingStep(sys.executable + ' external_program0y.py', [input_file], [resource_file], [output_file]) model = persalys.YACSCouplingPhysicalModel('A', [step]) # single evaluation x = [1.0, 2.0, 3.0] f = model.getFunction() y = f(x) print(y) ott.assert_almost_equal(y, [6.0, 7.0]) # sampling test ot_function = model.getFunction()
} pathHere = os.path.dirname(os.path.abspath(__file__)) pathResources = os.path.join(pathHere, 'ansysConnector') modelFile = os.path.join(pathResources, 'BEAM.wbpj') ansysExec = dict_exe[key_platform][0] templateFile = 'input.txt.in' step = persalys.CouplingStep() model = persalys.CouplingPhysicalModel('A', [step]) parser = persalys.AnsysParser(modelFile) parser.populateCouplingStep(model, 0, templateFile) # Required for testing because of dummyAnsys step = model.getSteps()[0] resourceFiles = step.getResourceFiles() resourceFiles.add(persalys.CouplingResourceFile(ansysExec)) step.setResourceFiles(resourceFiles) command = step.getCommand() step.setCommand(dict_exe[key_platform][1] + command) model.setSteps([step]) x = [1.0, 2.0, 3.0, 4.0, 5.0] f = model.getFunction() y = f(x) print(y) ott.assert_almost_equal(y, [0.10662]) os.remove(templateFile)
I = persalys.Input('I', 0, '') L = persalys.Input('L', 0, '') deviation = persalys.Output('deviation', '') deviation.setValue(1.26262626263) inputs = [E, F, I, L] outputs = [deviation] steps = [] input_files = [] input_file0 = persalys.CouplingInputFile( '../python/test/coupling_model/beam_input_template.xml') input_file0.setConfiguredPath('beam_input.xml') input_file0.setVariables(['E', 'F', 'I', 'L'], ['@E', '@F', '@I', '@L'], ['{}', '{}', '{}', '{}']) input_files.append(input_file0) resource_files = [] resource_file0 = persalys.CouplingResourceFile( '../python/test/coupling_model/beam.py') resource_files.append(resource_file0) output_files = [] output_file0 = persalys.CouplingOutputFile('beam_output.txt') output_file0.setVariables(['deviation'], ['deviation='], [0], [0], [0]) output_files.append(output_file0) step0 = persalys.CouplingStep('python3 beam.py', input_files, resource_files, output_files) step0.setIsShell(False) steps.append(step0) couplingModel = persalys.CouplingPhysicalModel('couplingModel', inputs, outputs, steps) couplingModel.setCleanupWorkDirectory(True) couplingModel.setCacheFiles('', '') myStudy.add(couplingModel) filename = 'data.csv'
f.write('X1=@X1\n') f.write('X2=@X2\n') with open('external_program.py', 'w') as f: f.write('import sys\n') f.write('exec(open(sys.argv[1]).read())\n') f.write('Y0=X0+X1+X2\n') f.write('Y1=X0+X1*X2\n') f.write('with open("output.txt", "w") as f:\n') f.write(' f.write("Y0=%.17e\\n" % Y0)\n') f.write(' f.write("Y1=%.17e\\n" % Y1)\n') input_file = persalys.CouplingInputFile('input_template.txt') input_file.setConfiguredPath('input.txt') input_file.setVariables(['X0', 'X1', 'X2'], ['@X0', '@X1', '@X2'], ['{:10.6f}', '{:10.6f}', '{:10.6f}']) resource_file = persalys.CouplingResourceFile('external_program.py') output_file = persalys.CouplingOutputFile('output.txt') output_file.setVariables(['Y0', 'Y1'], ['Y0=', 'Y1='], [0, 0], [0, 0], [0, 0]) step = persalys.CouplingStep(sys.executable + ' external_program.py input.txt', [input_file], [resource_file], [output_file]) model = persalys.CouplingPhysicalModel('A', [step]) # single evaluation x = [1.0, 2.0, 3.0] f = model.getFunction() y = f(x) print(y) ott.assert_almost_equal(y, [6.0, 7.0]) # sampling test ot_function = model.getFunction()
f.write('Y_mean = sum(Y) / len(Y)\n') f.write('Y_last = Y[-1]\n') f.write('try:\n idx = next(i for i,y in enumerate(Y) if y > 100e6)\n Y_mean_t = sum(Y[:idx]) / idx\nexcept StopIteration:\n Y_mean_t = 0.0\n') f.write('with open("output.txt", "w") as f:\n') f.write(' f.write("Y_min=%.17g\\n" % Y_min)\n') f.write(' f.write("Y_max=%.17g\\n" % Y_max)\n') f.write(' f.write("Y_mean=%.17g\\n" % Y_mean)\n') f.write(' f.write("Y_last=%.17g\\n" % Y_last)\n') f.write(' f.write("Y_mean_t=%.17g\\n" % Y_mean_t)\n') input_file1 = persalys.CouplingInputFile('input_logistic.txt.in') input_file1.setConfiguredPath('input_logistic.txt') input_file1.setVariables(['y0', 'a', 'b'], ['@y0@', '@a@', '@b@'], ['{:10.6f}', '{:10.6f}', '{:10.6f}']) resource_file1 = persalys.CouplingResourceFile('program_logistic.py') step1 = persalys.CouplingStep(sys.executable + ' program_logistic.py', [input_file1], [resource_file1], []) resource_file2 = persalys.CouplingResourceFile('post_logistic.py') output_file2 = persalys.CouplingOutputFile('output.txt') output_file2.setVariables(['Y_min', 'Y_max', 'Y_mean', 'Y_last', 'Y_mean_t'], ['Y_min=', 'Y_max=', 'Y_mean=', 'Y_last=', 'Y_mean_t='], [0]*5, [0]*5, [0]*5) step2 = persalys.CouplingStep(sys.executable + ' post_logistic.py', [], [resource_file2], [output_file2]) model = persalys.CouplingPhysicalModel('logistic', [step1, step2]) print(model.getInputNames()) print(model.getOutputNames()) assert model.getInputNames() == ['y0', 'a', 'b'] assert model.getOutputNames() == ['Y_min', 'Y_max', 'Y_mean', 'Y_last', 'Y_mean_t'] # single evaluation x = [3.9e6, 0.03134, 1.5887e-10]