Ejemplo n.º 1
0
### testcase using ansys connector
key_platform = platform.system()
dict_exe = {
    'Linux': ['dummyAnsys', './dummyAnsys'],
    'Windows': ['dummyAnsys.exe', 'dummyAnsys.exe']
}

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()
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'
cDist = ot.ComposedDistribution(
    [ot.Normal(), ot.Gumbel(),
     ot.Normal(), ot.Uniform()],
    ot.ComposedCopula([ot.IndependentCopula(2),
                       ot.GumbelCopula()]))
sample = cDist.getSample(200)
sample.add([float('nan'), float('inf'), 0., 0.])
sample.exportToCSVFile(filename, ' ')

# Designs of Experiment ##
    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()
Y = ot.CompositeRandomVector(ot_function, ot.RandomVector(ot.Normal(3)))
y = Y.getSample(50)
mean, stddev = y.computeMean(), y.computeStandardDeviationPerComponent()
print(mean, stddev)
ott.assert_almost_equal(mean, [0, 0], 0.3, 0.3)
Ejemplo n.º 4
0
    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]
f = model.getFunction()
y = f(x)
print(y)
ott.assert_almost_equal(y, [3.9e+06, 3.8498e+09, 5.64037e+08, 3.8498e+09, 2.94863e+07])

# cleanup
os.remove('input_logistic.txt.in')
os.remove('program_logistic.py')