Skip to content

Commit afdeed7

Browse files
committed
made norm a function pointer
1 parent 3127f6f commit afdeed7

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

src/nlesolver_module.F90

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ module nlesolver_module
192192
! custom sparse solver:
193193
procedure(sparse_solver_func),pointer :: custom_solver_sparse => null() !! user-supplied sparse linear solver routine (used for `sparsity_mode=5`)
194194

195+
procedure(norm_func),pointer :: norm => null() !! function for computing the norm of the `f` vector
196+
195197
contains
196198

197199
private
@@ -205,7 +207,6 @@ module nlesolver_module
205207
procedure :: adjust_x_for_bounds
206208
procedure :: adjust_search_direction
207209
procedure :: compute_next_step
208-
procedure :: norm
209210

210211
end type nlesolver_type
211212
!*********************************************************
@@ -290,6 +291,15 @@ subroutine linesearch_func(me,xold,p,x,f,fvec,fjac,fjac_sparse)
290291
real(wp),dimension(:),intent(in),optional :: fjac_sparse !! jacobian matrix [sparse]
291292
end subroutine linesearch_func
292293

294+
pure function norm_func(me, fvec) result(f)
295+
!! function vector norm.
296+
import :: wp,nlesolver_type
297+
implicit none
298+
class(nlesolver_type),intent(in) :: me
299+
real(wp),dimension(me%m),intent(in) :: fvec !! the function vector
300+
real(wp) :: f !! norm of the vector
301+
end function norm_func
302+
293303
end interface
294304

295305
contains
@@ -552,15 +562,17 @@ subroutine initialize_nlesolver_variables(me,&
552562
end if
553563

554564
if (present(norm_mode)) then
555-
if (norm_mode>=1 .and. norm_mode<=3) then ! only valid values are 1,2,3
556-
me%norm_mode = norm_mode
557-
else
565+
select case (norm_mode)
566+
case(NLESOLVER_2_NORM ); me%norm => norm_2
567+
case(NLESOLVER_INF_NORM ); me%norm => norm_inf
568+
case(NLESOLVER_1_NORM ); me%norm => norm_1
569+
case default
558570
status_ok = .false.
559571
call me%set_status(istat = -18, string = 'Error: invalid norm_mode:',i=norm_mode)
560572
return
561-
end if
573+
end select
562574
else
563-
me%norm_mode = NLESOLVER_2_NORM ! default
575+
me%norm => norm_2 ! default
564576
end if
565577

566578
if (present(step_mode)) then
@@ -1177,21 +1189,47 @@ end subroutine compute_next_step
11771189

11781190
!*****************************************************************************************
11791191
!>
1180-
! Compute the norm of the function vector.
1192+
! 2-norm function
1193+
1194+
pure function norm_2(me, fvec) result(f)
1195+
1196+
class(nlesolver_type),intent(in) :: me
1197+
real(wp),dimension(me%m),intent(in) :: fvec !! the function vector
1198+
real(wp) :: f !! norm of the vector
1199+
1200+
f = norm2(fvec)
1201+
1202+
end function norm_2
1203+
!*****************************************************************************************
1204+
1205+
!*****************************************************************************************
1206+
!>
1207+
! 1-norm function
1208+
1209+
pure function norm_1(me, fvec) result(f)
1210+
1211+
class(nlesolver_type),intent(in) :: me
1212+
real(wp),dimension(me%m),intent(in) :: fvec !! the function vector
1213+
real(wp) :: f !! norm of the vector
1214+
1215+
f = sum(abs(fvec))
1216+
1217+
end function norm_1
1218+
!*****************************************************************************************
1219+
1220+
!*****************************************************************************************
1221+
!>
1222+
! Infinity-norm function
11811223

1182-
pure function norm(me, fvec) result(f)
1224+
pure function norm_inf(me, fvec) result(f)
11831225

11841226
class(nlesolver_type),intent(in) :: me
11851227
real(wp),dimension(me%m),intent(in) :: fvec !! the function vector
11861228
real(wp) :: f !! norm of the vector
11871229

1188-
select case (me%norm_mode)
1189-
case(NLESOLVER_2_NORM); f = norm2(fvec)
1190-
case(NLESOLVER_INF_NORM); f = maxval(abs(fvec))
1191-
case(NLESOLVER_1_NORM); f = sum(abs(fvec))
1192-
end select
1230+
f = maxval(abs(fvec))
11931231

1194-
end function norm
1232+
end function norm_inf
11951233
!*****************************************************************************************
11961234

11971235
!*****************************************************************************************

0 commit comments

Comments
 (0)