Skip to content

Can JModelica print results directly to file?

An answer to this question on Stack Overflow.

Question

I am running the following JModelica script:

#!/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)

I had hoped that the results (e.g. time, x1, x2, &c.) would be printed to the file /z/out. But the file only contains IPOPT verbose debugging/status info.

Is there a way to print the information that would be stored in res directly to a file? Either by somehow writing res itself or, preferably, having IPOPT/JModelica write the results without having to go through Python?

Answer

There is a way to print the information directly to a file. The following accomplishes this. Note that result_file_name is the key to making this happen.

#!/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)

Unfortunately, the contents of the file are somewhat mysterious.