INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

nlopt.f

nlopt.f

(OP)
Hi Everyone!

I'm using Fortran 90 and trying to use the library nlopt.f in my code.
I downloaded the library from NLopt on Windows from http://ab-initio.mit.edu/wiki/index.php/NLopt_on_W...

After downloading the zip code from there I went to the DOS command option and run the line: lib /def:libnlopt-0.def (as suggested by wikipedia). However, I am still not able to use it in my code.

I am using compaq Visual Fortran to code.

I am not sure how to install the library on my computer so that when I write in my code include nlopt.f, I will be able to use the functions that the library has. Can anyone help with this problem?

Thanks in advance,
Shahar

RE: nlopt.f

I have not Visual Fortran compiler, but with the gfortran compiler it works for me as following:
1. I downloaded the package nlopt-2.4.2-dll32.zip from here
http://ab-initio.mit.edu/wiki/index.php/NLopt_on_W...

2. I extracted the package and copied from it these 2 files into my working directory:
libnlopt-0.dll
nlopt.f

3. For trying if it works I took the code from here
http://ab-initio.mit.edu/wiki/index.php/NLopt_Tuto...
and created nlopt_example.f95

Now I have in my working dorectory these 3 files

CODE

libnlopt-0.dll
nlopt.f
nlopt_example.f95 

4. I compiled the example using this command line

CODE

$ gfortran nlopt_example.f95 -o nlopt_example -L. libnlopt-0.dll 
which created the nlopt_example.exe

5. when I run the exe I get the result

CODE

$ nlopt_example
 found min at   0.33333333454526648       0.29629628990899065
 min val =   0.54433104808470245 

RE: nlopt.f

Here is the source I used:

nlopt_example.f95

CODE

! Compile:
! gfortran nlopt_example.f95 -o nlopt_example -L. libnlopt-0.dll
 
program main
  external myfunc, myconstraint
  double precision lb(2)
  integer*8 opt
  double precision d1(2), d2(2)
  double precision x(2), minf
  integer ires
  include 'nlopt.f'
 
  call nlo_create(opt, NLOPT_LD_MMA, 2)
  call nlo_get_lower_bounds(ires, opt, lb)
  lb(2) = 0.0
  call nlo_set_lower_bounds(ires, opt, lb)
  call nlo_set_min_objective(ires, opt, myfunc, 0)
 
  d1(1) = 2.
  d1(2) = 0.
  call nlo_add_inequality_constraint(ires, opt, &
       myconstraint, d1, 1.D-8)
  d2(1) = -1.
  d2(2) = 1.
  call nlo_add_inequality_constraint(ires, opt, &
       myconstraint, d2, 1.D-8)
 
  call nlo_set_xtol_rel(ires, opt, 1.D-4)
 
  x(1) = 1.234
  x(2) = 5.678
  call nlo_optimize(ires, opt, x, minf)
  if (ires.lt.0) then
     write(*,*) 'nlopt failed!'
  else
     write(*,*) 'found min at ', x(1), x(2)
     write(*,*) 'min val = ', minf
  endif

  call nlo_destroy(opt)
end

subroutine myfunc(val, n, x, grad, need_gradient, f_data)
  double precision val, x(n), grad(n)
  integer n, need_gradient
  if (need_gradient.ne.0) then
     grad(1) = 0.0
     grad(2) = 0.5 / dsqrt(x(2))
  endif
  val = dsqrt(x(2))
end

subroutine myconstraint(val, n, x, grad, need_gradient, d)
  integer need_gradient
  double precision val, x(n), grad(n), d(2), a, b
  a = d(1)
  b = d(2)
  if (need_gradient.ne.0) then
     grad(1) = 3. * a * (a*x(1) + b)**2
     grad(2) = -1.0
  endif
  val = (a*x(1) + b)**3 - x(2)
end 

RE: nlopt.f

(OP)
Thank you for your help!

I did everything you are saying except I don't understand what it means in step 2 to add the files into the directory. Where do you add them exactly? How do you do that?

Also what do you mean by compiling here? In your code the compile line has a ! so it is actually just a remark....So I am not sure what you mean by that either. Can you expand on that?

Thanks so much in advance,
Shahar

RE: nlopt.f

Quote (shaberg)


I don't understand what it means in step 2 to add the files into the directory
I copied the 2 files (libnlopt-0.dll and nlopt.f) into my working directory, which is

CODE

C:\MinGW\msys\1.0\home\Roman\fortran\nlopt 
It's so long because I'm using MingW + MSYS (see http://www.mingw.org/wiki/msys)

Quote:


Also what do you mean by compiling here? In your code the compile line has a ! so it is actually just a remark....
Yes, I remarked in the fortran code too, how to compile it.
First, I changed to my working directory (i.e.: C:\MinGW\msys\1.0\home\Roman\fortran\nlopt) and then I compiled it executing the command:

CODE

gfortran nlopt_example.f95 -o nlopt_example -L. libnlopt-0.dll 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close