AMEsim仿真脚本:使用Python脚本运行AMESim模型案例(2)

上期使用Python运行Amesim模型并后处理 我们介绍了使用python脚本运行一简单的模型,本次带来一阀控缸的模型。涉及文件命名等具体操作细节可以参考上一期。

AMEsim仿真脚本:使用Python脚本运行AMESim模型案例(2)的图1

该模型所需模块如上图所示。

Python脚本代码:

# Import Simcenter Amesim Python moduleimport amesimimport pylabimport subprocess
# Store the model name in a variablesname = 'PositionControlScript'
# Open model, check it, compile and closemsg = subprocess.check_output(         'AMECirChecker -g -q --nobackup --nologfile ' + sname + '.ame',         stderr=subprocess.STDOUT,         shell=True)print(msg)
# Unpack the model filepopen = 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 gaingain_value = [100.0, 200.0, 500.0, 1000.0]
# Set simulation end time at 1s and the print interval to 0.001ssim_opt = amesim.amegetsimopt(sname)sim_opt.finalTime = 1.0sim_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 simulationbatch_cfg = {'type' : 'set', 'param' : [{'type' : 'real', 'name' : 'k@elect01'}]}batch_cfg['param'][0]['set'] = gain_valueret_stat = amesim.ameputbatch(sname, batch_cfg)[ret_stat, msg] = amesim.amerunbatch(sname, sim_opt)runs = amesim.amegetbatchrunstatus(sname)
# Present batch simulation results in a subplotpylab.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 systempopen = subprocess.Popen(["AMESave", sname + '.ame'])

将脚本代码与amesim模型文件保存至同一文件夹。

通过在python终端输入:AMEPython+“脚本文件名”运行脚本即可得到运算结果。

AMEsim仿真脚本:使用Python脚本运行AMESim模型案例(2)的图2

文章来源:基算仿真

默认 最新
当前暂无评论,小编等你评论哦!
点赞 评论 收藏
关注