Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Added interfaces to minimizer module to help with debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Dec 23, 2022
1 parent 7774911 commit 13ae3bb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/fraggle/fraggle_generate.f90
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,6 @@ function radial_objective_function(v_r_mag_input) result(fval)
return
end function radial_objective_function

end subroutine fraggle_generate_rad_vel
end subroutine fraggle_generate_rad_vel

end submodule s_fraggle_generate
77 changes: 70 additions & 7 deletions src/misc/minimizer_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,70 @@ module subroutine minimize_bfgs(f, N, x0, eps, maxloop, lerr, x1)
logical, intent(out) :: lerr
real(DP), dimension(:), intent(out), allocatable :: x1
end subroutine minimize_bfgs

module function gradf(f, N, x1, dx, lerr) result(grad)
implicit none
integer(I4B), intent(in) :: N
class(lambda_obj), intent(inout) :: f
real(DP), dimension(:), intent(in) :: x1
real(DP), intent(in) :: dx
logical, intent(out) :: lerr
real(DP), dimension(N) :: grad
end function gradf

module function minimize1D(f, x0, S, N, eps, lerr) result(astar)
implicit none
integer(I4B), intent(in) :: N
class(lambda_obj), intent(inout) :: f
real(DP), dimension(:), intent(in) :: x0, S
real(DP), intent(in) :: eps
logical, intent(out) :: lerr
real(DP) :: astar
end function minimize1D

module function n2one(f, x0, S, N, a, lerr) result(fnew)
implicit none
integer(I4B), intent(in) :: N
class(lambda_obj), intent(inout) :: f
real(DP), dimension(:), intent(in) :: x0, S
real(DP), intent(in) :: a
logical, intent(out) :: lerr
real(DP) :: fnew
end function n2one

module subroutine bracket(f, x0, S, N, gam, step, lo, hi, lerr)
implicit none
integer(I4B), intent(in) :: N
class(lambda_obj), intent(inout) :: f
real(DP), dimension(:), intent(in) :: x0, S
real(DP), intent(in) :: gam, step
real(DP), intent(inout) :: lo
real(DP), intent(out) :: hi
logical, intent(out) :: lerr
end subroutine bracket

module subroutine golden(f, x0, S, N, eps, lo, hi, lerr)
implicit none
integer(I4B), intent(in) :: N
class(lambda_obj), intent(inout) :: f
real(DP), dimension(:), intent(in) :: x0, S
real(DP), intent(in) :: eps
real(DP), intent(inout) :: lo
real(DP), intent(out) :: hi
logical, intent(out) :: lerr
end subroutine golden

module subroutine quadfit(f, x0, S, N, eps, lo, hi, lerr)
implicit none
! Arguments
integer(I4B), intent(in) :: N
class(lambda_obj), intent(inout) :: f
real(DP), dimension(:), intent(in) :: x0, S
real(DP), intent(in) :: eps
real(DP), intent(inout) :: lo
real(DP), intent(out) :: hi
logical, intent(out) :: lerr
end subroutine quadfit
end interface

contains
Expand Down Expand Up @@ -153,7 +217,7 @@ module subroutine minimize_bfgs(f, N, x0, eps, maxloop, lerr, x1)
end subroutine minimize_bfgs


function gradf(f, N, x1, dx, lerr) result(grad)
module function gradf(f, N, x1, dx, lerr) result(grad)
!! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!! Purpose: Estimates the gradient of a function using a central difference
!! approximation
Expand Down Expand Up @@ -211,7 +275,7 @@ function gradf(f, N, x1, dx, lerr) result(grad)
end function gradf


function minimize1D(f, x0, S, N, eps, lerr) result(astar)
module function minimize1D(f, x0, S, N, eps, lerr) result(astar)
!! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!! This program find the minimum of a function of N variables in a single direction
!! S using in sequence:
Expand Down Expand Up @@ -286,15 +350,14 @@ function minimize1D(f, x0, S, N, eps, lerr) result(astar)
end function minimize1D


function n2one(f, x0, S, N, a, lerr) result(fnew)
module function n2one(f, x0, S, N, a, lerr) result(fnew)
implicit none
! Arguments
integer(I4B), intent(in) :: N
class(lambda_obj), intent(inout) :: f
real(DP), dimension(:), intent(in) :: x0, S
real(DP), intent(in) :: a
logical, intent(out) :: lerr

! Return
real(DP) :: fnew
! Internals
Expand All @@ -313,7 +376,7 @@ function n2one(f, x0, S, N, a, lerr) result(fnew)
end function n2one


subroutine bracket(f, x0, S, N, gam, step, lo, hi, lerr)
module subroutine bracket(f, x0, S, N, gam, step, lo, hi, lerr)
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!! This subroutine brackets the minimum. It recieves as input:
!! f%eval(x) : lambda function object containing the objective function as the eval metho
Expand Down Expand Up @@ -420,7 +483,7 @@ subroutine bracket(f, x0, S, N, gam, step, lo, hi, lerr)
end subroutine bracket


subroutine golden(f, x0, S, N, eps, lo, hi, lerr)
module subroutine golden(f, x0, S, N, eps, lo, hi, lerr)
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!! This function uses the golden section method to reduce the starting interval lo, hi by some amount sigma.
!! It recieves as input:
Expand Down Expand Up @@ -482,7 +545,7 @@ subroutine golden(f, x0, S, N, eps, lo, hi, lerr)
end subroutine golden


subroutine quadfit(f, x0, S, N, eps, lo, hi, lerr)
module subroutine quadfit(f, x0, S, N, eps, lo, hi, lerr)
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!! This function uses a quadratic polynomial fit to locate the minimum of a function
!! to some accuracy eps. It recieves as input:
Expand Down

0 comments on commit 13ae3bb

Please sign in to comment.