AMEsim仿真腳本:使用Python腳本運行AMESim模型案例(2)
瀏覽:3073 收藏:1
上期使用Python運行Amesim模型并后處理 我們介紹了使用python腳本運行一簡單的模型,本次帶來一閥控缸的模型。涉及文件命名等具體操作細節可以參考上一期。
該模型所需模塊如上圖所示。
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.001fig = pylab.figure()fig.suptitle('Cylinder Displacement [m]', fontsize=14, fontweight='bold')ax=pylab.subplot(211)for i, k in enumerate(gain_value):# Set parameteramesim.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. timepylab.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. timepylab.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+“腳本文件名”運行腳本即可得到運算結果。
文章來源:基算仿真
技術鄰APP
工程師必備
工程師必備
- 項目客服
- 培訓客服
- 平臺客服
TOP
1




















