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

Commit

Permalink
Browse files Browse the repository at this point in the history
Added new lambda function class to enable passing of complex functions into the BFGS minimizer
  • Loading branch information
daminton committed May 13, 2021
1 parent f89f2e2 commit 32dbbcc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ SWIFTEST_MODULES = swiftest_globals.f90 \
module_swiftestalloc.f90 \
io.f90 \
module_interfaces.f90 \
lambda_function.f90\
swiftest.f90

include Makefile.Defines
Expand Down
50 changes: 50 additions & 0 deletions src/modules/lambda_function.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module lambda_function
!! Defines a class that can enable objects that behave like lambda functions.
use swiftest_globals
implicit none

type, public :: lambda_obj
private
procedure(lambda0), pointer, nopass :: lambdaptr => null()
contains
generic :: init => lambda_init_0
procedure :: eval => lambda_eval_0
procedure :: lambda_init_0
end type

abstract interface
function lambda0(x)
! Template for a 0 argument function
import DP
real(DP), intent(in) :: x
real(DP) :: lambda
end function
end interface

contains
subroutine lambda_init_0(self, lambda)
implicit none
! Arguments
class(lambda_obj), intent(out) :: self
procedure(lambda0) :: lambda

self%lambdaptr => lambda
end subroutine lambda_init_0

function lambda_eval_0(self, x) result(y)
implicit none
! Arguments
class(lambda_obj), intent(in) :: self
real(DP), intent(in) :: x
! Result
real(DP) :: y

if (associated(self%lambdaptr)) then
y = self%lambdaptr(x)
else
error stop "Initialize the object (call init) before computing values (call exec)!"
end if
end function lambda_eval_0

end module lambda_function

1 change: 1 addition & 0 deletions src/modules/swiftest.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module swiftest
use swiftest_data_structures
use io
use user
use lambda_function
!$ use omp_lib
implicit none
public
Expand Down

0 comments on commit 32dbbcc

Please sign in to comment.