[Ipopt] string array to ipopt-fortran

Fabian Wein fabian.wein at lse.e-technik.uni-erlangen.de
Mon Mar 26 06:24:53 EDT 2012


Hi Jonathan,

thanks for your reply.

My plan is to pass from C++ strings and length's and convert it.

This is my current test:

C-Header:...
          const int* ipopt_nargs,        // ON INPUT (I): Number of 
parameters passed to IPOPT23
          double* ipopt_args,            // ON INPUT (I): Values 
passed to IPOPT23
          char**  ipopt_cargs_c,         // ON INPUT (I): name of 
parameters passed to IPOPT23
          int*    ipopt_cargs_l);          // ON INPUT (I): length's 
of the parameter names


       SUBROUTINE ....
       INTEGER          IPOPT_NARGS,IPOPT_CARGS_L(*),IPOPT_TMP
       DOUBLE PRECISION IPOPT_ARGS(*)
       CHARACTER*(*)    IPOPT_CARGS_C(*)
       CHARACTER*256    IPOPT_CARGS(50), IPOPT_TMP_STR

C     We need to transfer the ipopt subsolver options from C which are
C     an array of strings.
       DO 1220 IPOPT_TMP = 1, IPOPT_NARGS
          IPOPT_TMP_STR(1:IPOPT_CARGS_L(IPOPT_TMP)) =
      +                IPOPT_CARGS_C(IPOPT_TMP)
          IPOPT_CARGS(1) = IPOPT_TMP_STR;
          WRITE(*, *) 'param/leng/str:', IPOPT_TMP,
      +                IPOPT_CARGS_L(IPOPT_TMP), IPOPT_TMP_STR
1220  CONTINUE

This does not work:
  param/leng/str:           1           4     <a lot of garbage>

Why so much garbage? Would expect at maximum 4 characters of garbage ?!

Any further help is very much appreciated. I need this to run first, 
then I want
to replace the old Ipopt with the current one. I started to dig into 
Fortran on Friday only -
and hope to have nothing to do with Fortran any more once I replaced 
Ipopt. :)

Currently I try to understand how to use this iso_c_binding module.

> In general C/Fortran interoperability terms, I'd recommend you use the
> iso_c_binding module.
> Note that C-style strings are generally character*1 arrays on the
> Fortran side. To determine length you'll need to pass the length
> separately to the string (or call strlen from Fortran).
>
> This may mean you need a wrapper on the Fortran side that translates
> arguments nicely and then calls your Fortran subroutine that takes
> character(len=*) arguments. (If I've understood your question directly).
>
> Of course, the Fortran version of Ipopt has been replaced by the C++
> version, so longer term you probably want to move away from
> Ipopt-Fortran.
>
> Jonathan.
>
> On 26/03/12 10:15, Fabian Wein wrote:
>> Hi,
>>
>> I have to use an Fortran code that uses Ipopt-Fortran from C++. I
>> want to pass
>> the Ipopt parameters from C++ via Fortran.
>>
>> Is there any example available on how to use the CHARACTER*(*) CARGS
>> from
>> C? I use gfortran.


More information about the Ipopt mailing list