上期使用Python脚本运行Amesim模型 我们介绍了使用python脚本运行一简单的模型,本次带来一阀控缸的模型。涉及文件命名等具体操作细节可以参考上一期。
该模型所需模块如上图所示。
Python脚本代码:
# Import Simcenter Amesim Python module
import amesim
import pylab
import subprocess
# Store the model name in a variable
sname = 'PositionControlScript'
# Open model, check it, compile and close
msg = subprocess.check_output(
'AMECirChecker -g -q --nobackup --nologfile ' + sname + '.ame',
stderr=subprocess.STDOUT,
shell=True)
print(msg)
# Unpack the model file
popen = subprocess.Popen(["AMELoad", sname + '.ame'])
popen.wait()
# Get the parameter and variable name from datapath
# - get the gain parameter
[gain_parname] = amesim.amegetparamnamefromui(sname, 'k@elect01')
# - get the mass displacement variable
[displacement_varname] = amesim.amegetvarnamefromui(sname, 'x1@mass_friction2port')
# Create an array containing 4 different values of gain
gain_value = [100.0, 200.0, 500.0, 1000.0]
# Set simulation end time at 1s and the print interval to 0.001s
sim_opt = amesim.amegetsimopt(sname)
sim_opt.finalTime = 1.0
sim_opt.printInterval = 0.001
fig = pylab.figure()
fig.suptitle('Cylinder Displacement [m]', fontsize=14, fontweight='bold')
ax=pylab.subplot(211)
for i, k in enumerate(gain_value):
# Set parameter
amesim.ameputp(sname, gain_parname, k)
# Run simulation
[ret_stat, msg] = amesim.amerunsingle(sname, sim_opt)
# Get results
[time_data, displacement_data], names = amesim.ameloadvarst(sname, [displacement_varname])
# Plot mass displacement vs. time
pylab.plot(time_data, displacement_data)
pylab.xlim(0.0, 1.0)
pylab.title('- multiple single run simulations -')
# Setup and run a batch simulation
batch_cfg = {'type' : 'set', 'param' : [{'type' : 'real', 'name' : 'k@elect01'}]}
batch_cfg['param'][0]['set'] = gain_value
ret_stat = amesim.ameputbatch(sname, batch_cfg)
[ret_stat, msg] = amesim.amerunbatch(sname, sim_opt)
runs = amesim.amegetbatchrunstatus(sname)
# Present batch simulation results in a subplot
pylab.subplot(212)
for k in runs:
# Get results
[time_data, displacement_data], names = amesim.ameloadvarst(sname, [displacement_varname], k)
# Plot mass displacement vs. time
pylab.plot(time_data, displacement_data)
time_label = names[0]
pylab.xlabel(time_label)
pylab.xlim(0.0, 1.0)
pylab.title('- batch run simulation -')
pylab.show()
# Save and close the system
popen = subprocess.Popen(["AMESave", sname + '.ame'])
将脚本代码与amesim模型文件保存至同一文件夹。
通过在python终端输入:AMEPython+“脚本文件名”运行脚本即可得到运算结果。
关注公众号后台回复:“amepython2”即可获取案例及代码文件
标签:脚本,sname,Python,pylab,amesim,displacement,Amesim,time,data From: https://blog.csdn.net/laughzjh/article/details/139576613