Skip to content

Commit 55a6f25

Browse files
Add error handling
1 parent b8e182e commit 55a6f25

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

src/fstats_errors.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module fstats_errors
1515
integer(int32), parameter :: FS_UNDERDEFINED_PROBLEM_ERROR = 10004
1616
integer(int32), parameter :: FS_TOLERANCE_TOO_SMALL_ERROR = 10005
1717
integer(int32), parameter :: FS_TOO_FEW_ITERATION_ERROR = 10006
18+
integer(int32), parameter :: FS_INVALID_ARGUMENT_ERROR = 10007
1819

1920
! ------------------------------------------------------------------------------
2021
integer(int32), private, parameter :: MESSAGE_SIZE = 1024

src/fstats_experimental_design.f90

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ function doe_fit_model(nway, x, y, map, alpha, err) result(rst)
228228
!! relative to one another.
229229
!! - FS_MEMORY_ERROR: Occurs if there is a memory allocation
230230
!! error.
231+
!! - FS_INVALID_ARGUMENT_ERROR: Occurs if nway is out of range, or if
232+
!! map is used to "turn off" all model parameters.
231233
type(doe_model) :: rst
232234
!! The resulting model.
233235

@@ -259,7 +261,10 @@ function doe_fit_model(nway, x, y, map, alpha, err) result(rst)
259261

260262
! Input Checking
261263
if (nway < 1 .or. nway > 3) then
262-
! TO DO: Error - must be at least 1, but not more than 3
264+
call errmgr%report_error("doe_fit_model", &
265+
"The number of interaction levels must be between one and three.", &
266+
FS_INVALID_ARGUMENT_ERROR)
267+
return
263268
end if
264269

265270
! Determine the parameter count
@@ -271,13 +276,16 @@ function doe_fit_model(nway, x, y, map, alpha, err) result(rst)
271276
! Set up the map parameters
272277
if (present(map)) then
273278
if (size(map) /= nparam) then
274-
! TO DO: Error - map is not sized correctly
279+
call report_array_size_error(errmgr, "doe_fit_model", "map", &
280+
nparam, size(map))
281+
return
275282
end if
276283
mapptr => map
277284
else
278285
allocate(nmap(nparam), stat = flag, source = .true.)
279286
if (flag /= 0) then
280-
! TO DO: Error - memory issue
287+
call report_memory_error(errmgr, "doe_fit_model", flag)
288+
return
281289
end if
282290
mapptr => nmap
283291
end if
@@ -288,13 +296,17 @@ function doe_fit_model(nway, x, y, map, alpha, err) result(rst)
288296
if (.not.mapptr(i)) n = n - 1
289297
end do
290298
if (n < 1) then
291-
! TO DO: Error. there must be at least one parameter
299+
call errmgr%report_error("doe_fit_model", &
300+
"There must be at least one active model parameter.", &
301+
FS_INVALID_ARGUMENT_ERROR)
302+
return
292303
end if
293304

294305
! Local memory allocations
295306
allocate(xc(m, n), c(n, n), cxt(n, m), coeffs(n), stat = flag)
296307
if (flag /= 0) then
297-
! TO DO: Memory error
308+
call report_memory_error(errmgr, "doe_fit_model", flag)
309+
return
298310
end if
299311

300312
! Create the design matrix
@@ -322,7 +334,8 @@ function doe_fit_model(nway, x, y, map, alpha, err) result(rst)
322334
if (flag == 0) allocate(rst%stats(nparam), stat = flag)
323335
if (flag == 0) allocate(rst%map(nparam), stat = flag, source = mapptr)
324336
if (flag /= 0) then
325-
! TO DO: Memory error
337+
call report_memory_error(errmgr, "doe_fit_model", flag)
338+
return
326339
end if
327340
j = 0
328341
do i = 1, nparam
@@ -459,8 +472,15 @@ function doe_evaluate_model_1(nway, beta, x, map, err) result(rst)
459472
integer(int32) :: m, n, nparam, flag
460473
logical, pointer, dimension(:) :: mapptr
461474
logical, allocatable, target, dimension(:) :: nmap
462-
475+
class(errors), pointer :: errmgr
476+
type(errors), target :: deferr
477+
463478
! Initialization
479+
if (present(err)) then
480+
errmgr => err
481+
else
482+
errmgr => deferr
483+
end if
464484
m = size(x, 1)
465485
n = size(x, 2)
466486

@@ -474,25 +494,31 @@ function doe_evaluate_model_1(nway, beta, x, map, err) result(rst)
474494
if (nway >= 2) nparam = nparam + n * (n - 1)
475495
if (nway >= 3) nparam = nparam + n * (n**2 - 1)
476496
if (size(beta) /= nparam) then
477-
! TO DO: Error - beta is not sized correctly
497+
call report_array_size_error(errmgr, "doe_evaluate_model_1", "beta", &
498+
nparam, size(beta))
499+
return
478500
end if
479501

480502
! Memory Allocations
481503
allocate(rst(m), stat = flag)
482504
if (flag /= 0) then
483-
! TO DO: Error - memory issue
505+
call report_memory_error(errmgr, "doe_evaluate_model_1", flag)
506+
return
484507
end if
485508

486509
! Set up the map parameters
487510
if (present(map)) then
488511
if (size(map) /= nparam) then
489-
! TO DO: Error - map is not sized correctly
512+
call report_array_size_error(errmgr, "doe_evaluate_model_1", &
513+
"map", nparam, size(map))
514+
return
490515
end if
491516
mapptr => map
492517
else
493518
allocate(nmap(nparam), stat = flag, source = .true.)
494519
if (flag /= 0) then
495-
! TO DO: Error - memory issue
520+
call report_memory_error(errmgr, "doe_evaluate_model_1", flag)
521+
return
496522
end if
497523
mapptr => nmap
498524
end if

0 commit comments

Comments
 (0)