2015-08-15 41 views
1

我運行以下JModelica腳本:JModelica可以直接將結果打印到文件嗎?

#!/usr/local/jmodelica/bin/jm_python.sh 
import pyjmi 
op = pyjmi.transfer_optimization_problem("BatchReactor", "model.mop") 

opt_opts         = op.optimize_options() 
opt_opts['n_e']       = 40 # Number of elements 
opt_opts['IPOPT_options']['tol']   = 1e-10 
opt_opts['IPOPT_options']['print_level'] = 8 
opt_opts['IPOPT_options']['output_file'] = '/z/out' 

res = op.optimize(options=opt_opts) 

我曾希望的結果(例如時間,X1,X2,&角)將被打印到文件/z/out。但該文件僅包含IPOPT詳細調試/狀態信息。

有沒有辦法打印將存儲在res中的信息直接存儲到文件中?要麼以某種方式編寫資源本身,或者最好讓IPOPT/JModelica編寫結果而不必通過Python?

回答

0

您可能會發現使用result_file_name每隔一個答案在這裏導致輸出文件是difficult to understand

下面產生一個更好的格式:

import StringIO 
import numpy as np 

def PrintResToFile(filename,result): 
    def StripMX(x): 
    return str(x).replace('MX(','').replace(')','') 

    varstr = '#Variable Name={name: <10}, Unit={unit: <7}, Val={val: <10}, Col={col:< 5}, Comment="{comment}"\n' 

    with open(filename,'w') as fout: 
    #Print all variables at the top of the file, along with relevant information 
    #about them. 
    for var in result.model.getAllVariables(): 
     if not result.is_variable(var.getName()): 
     val = result.initial(var.getName()) 
     col = -1 
     else: 
     val = "Varies" 
     col = result.get_column(var.getName()) 

     unit = StripMX(var.getUnit()) 
     if not unit: 
     unit = "X" 

     fout.write(varstr.format(
     name = var.getName(), 
     unit = unit, 
     val  = val, 
     col  = col, 
     comment = StripMX(var.getAttribute('comment')) 
    )) 

    #Ensure that time variable is printed 
    fout.write(varstr.format(
     name = 'time', 
     unit = 's', 
     val  = 'Varies', 
     col  = 0, 
     comment = 'None' 
    )) 

    #The data matrix contains only time-varying variables. So fetch all of 
    #these, couple them in tuples with their column number, sort by column 
    #number, and then extract the name of the variable again. This results in a 
    #list of variable names which are guaranteed to be in the same order as the 
    #data matrix. 
    vkeys_in_order = map(lambda x: x[1], sorted([(result.get_column(x),x) for x in result.keys() if result.is_variable(x)])) 

    for vk in vkeys_in_order: 
     fout.write("{0:>13},".format(vk)) 
    fout.write("\n") 

    sio = StringIO.StringIO() 
    np.savetxt(sio, result.data_matrix, delimiter=',', fmt='%13.5f') 
    fout.write(sio.getvalue()) 

看起來像這樣:

#Variable Name=S0  , Unit=kg  , Val=2.0  , Col=-1 , Comment="Solid Mass" 
#Variable Name=F0  , Unit=kg  , Val=0.0  , Col=-1 , Comment="Fluid Mass" 
#Variable Name=a   , Unit=Hz  , Val=0.2  , Col=-1 , Comment="None" 
#Variable Name=b   , Unit=kg/s , Val=1.0  , Col=-1 , Comment="None" 
#Variable Name=f   , Unit=kg/s , Val=0.05  , Col=-1 , Comment="None" 
#Variable Name=h   , Unit=1/g , Val=0.05  , Col=-1 , Comment="None" 
#Variable Name=der(F) , Unit=X  , Val=Varies , Col= 1 , Comment="None" 
#Variable Name=F   , Unit=kg  , Val=Varies , Col= 3 , Comment="None" 
#Variable Name=der(S) , Unit=X  , Val=Varies , Col= 2 , Comment="None" 
#Variable Name=S   , Unit=kg  , Val=Varies , Col= 4 , Comment="None" 
#Variable Name=u   , Unit=X  , Val=Varies , Col= 5 , Comment="None" 
#Variable Name=startTime , Unit=X  , Val=0.0  , Col=-1 , Comment="None" 
#Variable Name=finalTime , Unit=X  , Val=100.0  , Col=-1 , Comment="None" 
#Variable Name=time  , Unit=s  , Val=Varies , Col= 0 , Comment="None" 
     time,  der(F),  der(S),   F,   S,   u, 
     0.00000,  0.97097,  -0.97097,  0.00000,  2.00000,  0.97097 
     0.38763,  1.07704,  -1.05814,  0.38519,  1.61698,  1.00000 
     1.61237,  0.88350,  -0.80485,  1.70714,  0.35885,  0.65862 
     2.50000,  0.00000,  0.09688,  2.14545,  0.00000,  0.00000 
     2.88763,  0.09842,  -0.00000,  2.18330,  0.00000,  0.06851 
     4.11237,  0.10342,  0.00000,  2.30688,  0.00000,  0.07077 
     5.00000,  0.10716,  0.00000,  2.40033,  0.00000,  0.07240 
     5.38763,  0.10882,  -0.00000,  2.44219,  0.00000,  0.07311 
     6.61237,  0.11421,  0.00000,  2.57875,  0.00000,  0.07535 
0

有一種將信息直接打印到文件的方法。以下完成這一點。請注意,result_file_name是實現此目標的關鍵。

#!/usr/local/jmodelica/bin/jm_python.sh 
import pyjmi 
op = pyjmi.transfer_optimization_problem("BatchReactor", "model.mop") 

opt_opts         = op.optimize_options() 
opt_opts['n_e']       = 40 # Number of elements 
opt_opts['result_file_name']    = '/z/out' 
opt_opts['IPOPT_options']['tol']   = 1e-10 
opt_opts['IPOPT_options']['print_level'] = 0 

res = op.optimize(options=opt_opts) 

不幸的是,文件的內容有些神祕。

相關問題