1

I'm using python 3 and recently I was able to interface a FORTRAN solver for some type of optimization problems (thanks to eryksun here on stack overflow).

The little issue that is getting on the way is that the solver output in the python notebook (jupyter) does not show up. Note that the solver returns the right solution, but it doesn't display its progress nor any mesages. Although, if call the same FORTRAN solver from a julia notebook, I can see the output in the julia notebook. The solver output/message is just the FORTRAN write() statements to stdout.

For instance, this is what julia notebook shows:

Hello World from Wrapper!

 Iter:   0  Nfun:   1  f1(x) =  24.20000         Eps =  116.4338    
 Iter:   1  Nfun:   2  f1(x) =  24.20000         Eps =  48.68118    
 Iter:   2  Nfun:   3  f1(x) =  6.307350         Eps =  7.505725    
 Iter:   3  Nfun:   4  f1(x) =  6.307350         Eps =  4.854666    
 Iter:   4  Nfun:   5  f1(x) =  3.580435         Eps = 0.1099409    
 Iter:   5  Nfun:   6  f1(x) =  3.580435         Eps = 0.8368376E-01   
 .....

The solver takes as a parameter the desired fileout number. For FORTRAN this is 6 for stdout. When I call the solver from python and julia I set this parameter to 6.

It seems it has to do with the kernel (python vs. julia) in jupyter but I'm not able to find a simple straight forward way to see this output which is crucial for debugging and validation purposes.

Any hints or suggestions?

Thank you in advance!

Followup: here is the code that calls the FORTRAN solver - it has 31 parameters. Parameter num_outcalls (set to 6) and disp (set to 3 for verbose mode). FORTRAN is the loaded fortran library "solver_lib.so"

retval = FORTRAN.c_mpbngc(OptProb.dim.ctypes._as_parameter_,                    \

                          OptProb.X.ctypes._as_parameter_,                          \

                          OptProb.Tx.ctypes._as_parameter_,                         \

                          OptProb.Blox.ctypes._as_parameter_,                   \

                          OptProb.Bupx.ctypes._as_parameter_,                   \

                          OptProb.num_objfunc.ctypes._as_parameter_,                \

                          ct.byref(ct.c_int(OptProb.num_genconstr)),            \

                          ct.byref(ct.c_int(OptProb.num_linconstr)),            \

                          OptProb.Tlinconstr.ctypes._as_parameter_,             \

                          OptProb.Blo_linconstr.ctypes._as_parameter_,          \

                          OptProb.Bup_linconstr.ctypes._as_parameter_,          \

                          OptProb.CClinconstr.ctypes._as_parameter_,                \

                          OptProb.F.ctypes._as_parameter_,                      \

                          OptProb.ptrfuncFASGTYPE(OptProb.ptrObjFunc),          \

                          ct.byref(ct.c_double(OptProb.r_linsear)),             \

                          ct.byref(ct.c_int(OptProb.max_numcallsobjfun_linsear)),\

                          OptProb.Gam.ctypes._as_parameter_,                        \

                          ct.byref(ct.c_double(OptProb.tol)),                   \

                          ct.byref(ct.c_double(OptProb.tol_constrfeas)),            \

                          ct.byref(ct.c_int(OptProb.max_numsubgrads)),          \

                          OptProb.num_iters.ctypes._as_parameter_,              \

                          OptProb.max_numcallsobjfun.ctypes._as_parameter_,     \

                          OptProb.num_outcalls.ctypes._as_parameter_,           \

                          OptProb.disp.ctypes._as_parameter_,                   \

                          OptProb.err_type.ctypes._as_parameter_,               \

                          OptProb.IWORK.ctypes._as_parameter_,                  \

                          ct.byref(ct.c_int(OptProb.LIWORK)),                   \

                          OptProb.WORK.ctypes._as_parameter_,                   \

                          ct.byref(ct.c_int(OptProb.LWORK)),                        \

                          OptProb.ptrfuncFASGTYPE(OptProb.ptrObjFunc),          \

                          OptProb.USER.ctypes._as_parameter_) 
Thomas K
  • 39,200
  • 7
  • 84
  • 86
Lq-Stable
  • 91
  • 1
  • 6
  • 1
    show the python call. How do you get the result? – agentp Feb 23 '16 at 18:23
  • Thanks. It's up there. However, I do think it has more to do with the python notebook itself.. – Lq-Stable Feb 23 '16 at 20:44
  • IPython redirects stdout at the Python level, by reassigning `sys.stdout`. This doesn't affect Fortran or C code that writes to the underlying stdout file descriptor automatically. It's possible, at least on Unix, to temporarily point that fd somewhere else so you can capture the output. Info [here](http://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/) and [here](https://github.com/ipython/ipython/issues/1230). – Thomas K Feb 26 '16 at 17:57
  • Very much appreciated Thomas for the hints. I knew about the second link and your replies within and realized that is will not be straight forward. So I came here to ask! Now I need to chew on the details in the first link.. Thanks again! – Lq-Stable Feb 27 '16 at 15:42
  • here's [how stdout can be redirected at the file descriptors level in Python](http://stackoverflow.com/a/22434262/4279) – jfs Jun 02 '16 at 16:14

0 Answers0