{"id":22276255,"url":"https://github.com/grimme-lab/nlopt-f","last_synced_at":"2026-01-05T15:47:33.551Z","repository":{"id":40721468,"uuid":"394602635","full_name":"grimme-lab/nlopt-f","owner":"grimme-lab","description":"Fortran bindings for the NLopt library","archived":false,"fork":false,"pushed_at":"2022-07-01T20:07:24.000Z","size":40,"stargazers_count":29,"open_issues_count":6,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-30T15:29:48.815Z","etag":null,"topics":["mathematical-optimization","nlopt","nonlinear-optimization","numerical-optimization","optimization"],"latest_commit_sha":null,"homepage":"","language":"Fortran","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grimme-lab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-Apache","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-10T09:52:29.000Z","updated_at":"2024-11-11T14:04:34.000Z","dependencies_parsed_at":"2022-07-29T06:08:01.641Z","dependency_job_id":null,"html_url":"https://github.com/grimme-lab/nlopt-f","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimme-lab%2Fnlopt-f","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimme-lab%2Fnlopt-f/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimme-lab%2Fnlopt-f/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimme-lab%2Fnlopt-f/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grimme-lab","download_url":"https://codeload.github.com/grimme-lab/nlopt-f/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245509750,"owners_count":20627045,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mathematical-optimization","nlopt","nonlinear-optimization","numerical-optimization","optimization"],"created_at":"2024-12-03T14:14:17.500Z","updated_at":"2026-01-05T15:47:33.509Z","avatar_url":"https://github.com/grimme-lab.png","language":"Fortran","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fortran bindings for the NLopt library\n\n[![License](https://img.shields.io/badge/license-MIT%7CApache%202.0-blue)](LICENSE-Apache)\n[![CI](https://github.com/grimme-lab/nlopt-f/actions/workflows/build.yml/badge.svg)](https://github.com/grimme-lab/nlopt-f/actions/workflows/build.yml)\n\nFortran bindings for the NLopt library.\nWhile the NLopt library supports Fortran by using implicit interface calling conventions, those are not type-safe.\nThis project offers an alternative interface to the NLopt C-API.\n\n\n## Installation\n\nTo build this project from the source code in this repository you need to have\n\n- a Fortran compiler supporting Fortran 2008 (GCC 5 or newer or Intel Fortran)\n- One of the supported build systems\n\n  - [meson](https://mesonbuild.com) version 0.55 or newer, with\n    a build-system backend, *i.e.* [ninja](https://ninja-build.org) version 1.7 or newer\n  - [cmake](https://cmake.org) version 3.14 or newer, with\n    a build-system backend, *i.e.* [ninja](https://ninja-build.org) version 1.10 or newer\n  - [Fortran package manager (fpm)](https://github.com/fortran-lang/fpm) version 0.2.0 or newer\n\n- [nlopt](https://nlopt.readthedocs.io/en/latest/) version 2.0.0 or later\n\n\n### Building with meson\n\nSetup a build with\n\n```\nmeson setup _build\n```\n\nYou can select the Fortran compiler by the `FC` environment variable.\nTo compile the project run\n\n```\nmeson compile -C _build\n```\n\nYou can run the projects testsuite with\n\n```\nmeson test -C _build --print-errorlogs\n```\n\nTo include ``nlopt-f`` in your project add the following wrap file to your subprojects directory:\n\n```ini\n[wrap-git]\ndirectory = nlopt-f\nurl = https://github.com/grimme-lab/nlopt-f\nrevision = head\n```\n\nYou can retrieve the dependency from the wrap fallback with\n\n```meson\nnlopt_dep = dependency('nlopt-f', fallback: ['nlopt-f', 'nlopt_dep'])\n```\n\nand add it as dependency to your targets.\n\n\n### Building with CMake\n\nAlternatively, this project can be build with CMake (in this case ninja 1.10 or newer is required):\n\n```\ncmake -B _build -G Ninja\n```\n\nTo compile the project with CMake run\n\n```\ncmake --build _build\n```\n\nYou can run the project testsuite with\n\n```\npushd _build \u0026\u0026 ctest \u0026\u0026 popd\n```\n\nFinally, install the project using (the install prefix can be customized with ``-DCMAKE_INSTALL_PREFIX=/path/to/install`` in the first step)\n\n```\ncmake --install _build\n```\n\nNow you can use it in your CMake project by finding it again\n\n```cmake\nif(NOT \"nlopt-f::nlopt-f\")\n  find_package(\"nlopt-f\" REQUIRED)\nendif()\n# ...\ntarget_link_libraries(\"${PROJECT_NAME}-lib\" PRIVATE \"nlopt-f::nlopt-f\")\n```\n\n\n### Building with fpm\n\nInvoke fpm in the project root with\n\n```\nfpm build\n```\n\nTo run the testsuite use\n\n```\nfpm test\n```\n\nTo use ``nlopt-f`` include it as dependency in your package manifest\n\n```toml\n[dependencies]\nnlopt-f.git = \"https://github.com/grimme-lab/nlopt-f\"\n```\n\nThe Fortran bindings target NLopt 2.5.0 by default.\nYou can target a specific NLopt version by adding the preprocessor define ``NLOPT_VERSION`` using a compact integer representation (major * 10000 + minor * 100 + patch).\nTo target NLopt 2.6.2 use\n\n```\nfpm \u003ccmd\u003e --profile debug --flag \"-DNLOPT_VERSION=20602\"\n```\n\nIf NLopt is not installed in a standard location use pkg-config to find it\n\n```\nfpm \u003ccmd\u003e --profile debug --flag \"$(pkg-config nlopt --cflags --libs-only-L)\"\n```\n\nYou can check your installed NLopt version using\n\n```\npkg-config nlopt --modversion\n```\n\nWhich will print the version number.\n\n\n## Usage\n\nThe Fortran bindings allow an object oriented usage of the NLopt C-API:\n\n```f90\nmodule example_funcs\n  implicit none\n\n  integer, parameter :: wp = kind(0.0d0)\n  type :: constraint_data\n    real(wp) :: d(2)\n  end type\n\ncontains\n\nfunction myfunc(x, gradient, func_data) result(f)\n  real(wp), intent(in) :: x(:)\n  real(wp), intent(inout), optional :: gradient(:)\n  class(*), intent(in), optional :: func_data\n  real(wp) :: f\n\n  if (present(gradient)) then\n    gradient(1) = 0.0_wp\n    gradient(2) = 0.5_wp / sqrt(x(2))\n  endif\n  f = sqrt(x(2))\nend function myfunc\n\nfunction myconstraint(x, gradient, func_data) result(f)\n  real(wp), intent(in) :: x(:)\n  real(wp), intent(inout), optional :: gradient(:)\n  class(*), intent(in), optional :: func_data\n  real(wp) :: f\n\n  select type(func_data)\n  type is(constraint_data)\n    associate(a =\u003e func_data%d(1), b =\u003e func_data%d(2))\n      if (present(gradient)) then\n        gradient(1) = 3.0_wp * a * (a*x(1) + b)**2\n        gradient(2) = -1.0_wp\n      endif\n      f = (a*x(1) + b)**3 - x(2)\n    end associate\n  end select\nend function myconstraint\n\nend module example_funcs\n\n\nprogram example\n  use example_funcs\n  use nlopt_wrap, only : nlopt_opt, nlopt_func, create, destroy\n  use nlopt_enum, only : NLOPT_SUCCESS, algorithm_from_string\n  implicit none\n  type(nlopt_opt) :: opt\n  real(wp) :: lb(2), x(2), minf\n  integer :: stat\n  type(constraint_data), target :: d1, d2\n  real(wp), parameter :: xtol = 1.0e-4_wp\n\n  call create(opt, algorithm_from_string(\"LD_MMA\"), 2)\n\n  call opt%get_lower_bounds(lb)\n\n  lb(2) = 0.0_wp\n  call opt%set_lower_bounds(lb)\n\n  d1%d = [+2.0_wp, +0.0_wp]\n  d2%d = [-1.0_wp, +1.0_wp]\n  associate(\u0026\n      \u0026 f =\u003e nlopt_func(myfunc), \u0026\n      \u0026 fc1 =\u003e nlopt_func(myconstraint, d1), \u0026\n      \u0026 fc2 =\u003e nlopt_func(myconstraint, d2))\n    call opt%set_min_objective(f)\n\n    call opt%add_inequality_constraint(fc1, 1.0e-8_wp)\n    call opt%add_inequality_constraint(fc2, 1.0e-8_wp)\n\n    call opt%set_xtol_rel(xtol)\n\n    x = [1.234_wp, 5.678_wp]\n    call opt%optimize(x, minf, stat)\n  end associate\n\n  if (stat \u003c NLOPT_SUCCESS) then\n    write(*, '(a)') \"NLopt failed!\"\n    stop 1\n  endif\n\n  write(*, '(a, *(1x, g0))') \"Found minimum at\", x\n  write(*, '(a, *(1x, g0))') \"Minimum value is\", minf\n\n  call destroy(opt)\nend program example\n```\n\n\n## License\n\nThis project is free software: you can redistribute it and/or modify it under the terms of the [Apache License, Version 2.0](LICENSE-Apache) or [MIT license](LICENSE-MIT) at your opinion.\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an _as is_ basis, without warranties or conditions of any kind, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrimme-lab%2Fnlopt-f","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrimme-lab%2Fnlopt-f","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrimme-lab%2Fnlopt-f/lists"}