【问题标题】:What is the format of JModelica result_file_name output?JModelica result_file_name 输出的格式是什么?
【发布时间】:2015-08-15 22:36:53
【问题描述】:

我正在使用以下代码从 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['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)

查看文件/z/out,其结构如下:

#1
char Aclass(3,11)
Atrajectory
1.1

char name(14,9)
time
S0
F0
a
b
f
h
startTime
finalTime
der(F)
der(S)
F
S
u

char description(14,17)
Time in [s]
Fluid Mass
Solid Mass












int dataInfo(14,4)
0 1 0 -1 # time
1 2 0 -1 # S0
1 3 0 -1 # F0
1 4 0 -1 # a
1 5 0 -1 # b
1 6 0 -1 # f
1 7 0 -1 # h
1 8 0 -1 # startTime
1 9 0 -1 # finalTime
2 2 0 -1 # der(F)
2 3 0 -1 # der(S)
2 4 0 -1 # F
2 5 0 -1 # S
2 6 0 -1 # u

float data_1(2,9)
0.00000000000000E+00 5.10000000000000E+00 0.00000000000000E+00 2.00000000000000E-01 1.00000000000000E+00 5.00000000000000E-02 5.00000000000000E-02 0.00000000000000E+00 2.00000000000000E+01
2.00000000000000E+01 5.10000000000000E+00 0.00000000000000E+00 2.00000000000000E-01 1.00000000000000E+00 5.00000000000000E-02 5.00000000000000E-02 0.00000000000000E+00 2.00000000000000E+01

float data_2(121,6)
 0.00000000000000E+00 3.86148766414463E-06 -3.86148766414463E-06 0.00000000000000E+00 5.10000000000000E+00 3.86148766414463E-06
 7.75255128608411E-02 2.47477765642993E-06 -2.46259047556037E-06 2.43743619355378E-07 5.09999975693332E+00 2.47477757686100E-06
 3.22474487139159E-01 -7.12360587129927E-09 3.14767685363840E-08 4.87063261121143E-07 5.09999951841489E+00 0.00000000000000E+00
 5.00000000000000E-01 -2.27821146604573E-09 2.54684613056978E-08 4.63805003867590E-07 5.09999954601731E+00 0.00000000000000E+00
 5.77525512860841E-01 -8.07822248879287E-09 3.12397120851912E-08 4.63229798975842E-07 5.09999954838959E+00 0.00000000000000E+00
 8.22474487139159E-01 -9.05155979680226E-09 3.21027761722239E-08 4.61024334454370E-07 5.09999955625463E+00 0.00000000000000E+00
 1.00000000000000E+00 -6.46538054719305E-09 2.94456844114636E-08 4.59606084166055E-07 5.09999956175866E+00 0.00000000000000E+00
 1.07752551286084E+00 -9.03814030251626E-09 3.19848463370596E-08 4.58934127540645E-07 5.09999956421108E+00 0.00000000000000E+00
 1.32247448713916E+00 -9.43227406481859E-09 3.22625894212407E-08 4.56606313871655E-07 5.09999957214518E+00 0.00000000000000E+00
 1.50000000000000E+00 -7.70752804891954E-09 3.04605248620022E-08 4.55059942934373E-07 5.09999957773756E+00 0.00000000000000E+00
 1.57752551286084E+00 -9.35854831586841E-09 3.20762829272391E-08 4.54354698868064E-07 5.09999958020553E+00 0.00000000000000E+00
 1.82247448713916E+00 -9.59489214423911E-09 3.21941728450351E-08 4.51985620549206E-07 5.09999958812461E+00 0.00000000000000E+00
 2.00000000000000E+00 -8.30366982351327E-09 3.08226050857767E-08 4.50378711706052E-07 5.09999959373626E+00 0.00000000000000E+00
 2.07752551286084E+00 -9.51883430217167E-09 3.20016584318585E-08 4.49656489022018E-07 5.09999959620300E+00 0.00000000000000E+00
 2.32247448713916E+00 -9.68509482363463E-09 3.20484384547136E-08 4.47266878942693E-07 5.09999960408497E+00 0.00000000000000E+00
 ...

... 意味着更多行直到finalTime

但这一切意味着什么?此输出的格式在哪里指定?

【问题讨论】:

    标签: python jmodelica


    【解决方案1】:

    我找不到有关文件格式的文档。但是,在我看来,以下会产生更好的输出:

    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="Reproductive Mass"
    #Variable Name=F0        , Unit=kg     , Val=0.0       , Col=-1   , Comment="Vegetative 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
    ...
    

    【讨论】:

    • 感谢@Richard。我正在尝试理解 Jmodelica 输出文件。似乎 result.model.getAllVariables() 不再起作用并返回 AttributeError: 'pyfmi.fmi.FMUModelME1' object has no attribute 'getAllVariables'。我检查了对象的目录,似乎没有任何方法可以检索所有变量。
    猜你喜欢
    • 2013-04-15
    • 2014-12-28
    • 2011-09-25
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    相关资源
    最近更新 更多