-
Notifications
You must be signed in to change notification settings - Fork 190
error: implement general state_type
handler
#938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2227f1a
`stdlib_error.F90` -> `stdlib_error.fypp`
perazz 3408a7b
Update CMakeLists.txt
perazz dd3c270
linalg_state_type extends state_type
perazz 44c60d6
update specs document
perazz db90df7
Update index.md
perazz 97a26d3
add example programs
perazz 473e1ec
Update doc/specs/stdlib_linalg_state_type.md
perazz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: state_type | ||
--- | ||
|
||
# State and Error Handling Derived Type | ||
|
||
[TOC] | ||
|
||
## Introduction | ||
|
||
The `stdlib_error` module provides a derived type holding information on the state of operations within the standard library and procedures for expert control of workflows. | ||
An optional `state_type` variable to hold such information is provided as a form of expert API. | ||
If the user does not require state information but fatal errors are encountered during execution, the program will undergo a hard stop. | ||
Instead, if the state argument is present, the program will never stop but will return detailed error information into the state handler. | ||
|
||
## Derived types provided | ||
|
||
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
### The `state_type` derived type | ||
|
||
The `state_type` is defined as a derived type containing an integer error flag and fixed-size character strings to store an error message and the location of the error state change. | ||
Fixed-size string storage was chosen to facilitate the compiler's memory allocation and ultimately ensure maximum computational performance. | ||
|
||
A similarly named generic interface, `state_type`, is provided to allow the developer to create diagnostic messages and raise error flags easily. | ||
The call starts with an error flag or the location of the event and is followed by an arbitrary list of `integer`, `real`, `complex`, or `character` variables. | ||
Numeric variables may be provided as either scalars or rank-1 (array) inputs. | ||
|
||
#### Type-bound procedures | ||
|
||
The following convenience type-bound procedures are provided: | ||
- `print()` returns an allocatable character string containing state location, message, and error flag; | ||
- `print_message()` returns an allocatable character string containing the state message; | ||
- `ok()` returns a `logical` flag that is `.true.` in case of successful state (`flag==STDLIB_SUCCESS`); | ||
- `error()` returns a `logical` flag that is `.true.` in case of an error state (`flag/=STDLIB_SUCCESS`). | ||
|
||
#### Status | ||
|
||
Experimental | ||
|
||
#### Example | ||
|
||
```fortran | ||
{!example/error/example_error_state1.f90!} | ||
``` | ||
|
||
## Error flags provided | ||
|
||
The module provides the following state flags: | ||
- `STDLIB_SUCCESS`: Successful execution | ||
- `STDLIB_VALUE_ERROR`: Numerical errors (such as infinity, not-a-number, range bounds) are encountered. | ||
- `STDLIB_LINALG_ERROR`: Linear Algebra errors are encountered, such as non-converging iterations, impossible operations, etc. | ||
- `STDLIB_INTERNAL_ERROR`: Provided as a developer safeguard for internal errors that should never occur. | ||
- `STDLIB_IO_ERROR`: Input/Output-related errors, such as file reading/writing failures. | ||
- `STDLIB_FS_ERROR`: File system-related errors, such as directory access issues. | ||
|
||
## Comparison operators provided | ||
|
||
The module provides overloaded comparison operators for all comparisons of a `state_type` variable with an integer error flag: `<`, `<=`, `==`, `>=`, `>`, `/=`. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
program example_error_state1 | ||
use stdlib_error, only: state_type, STDLIB_VALUE_ERROR, STDLIB_SUCCESS, operator(/=) | ||
implicit none | ||
type(state_type) :: err | ||
|
||
! To create a state variable, we enter its integer state flag, followed by a list of variables | ||
! that will be automatically assembled into a formatted error message. No need to provide string formats | ||
err = state_type(STDLIB_VALUE_ERROR,'just an example with scalar ',& | ||
'integer=',1,'real=',2.0,'complex=',(3.0,1.0),'and array ',[1,2,3],'inputs') | ||
|
||
! Print flag | ||
print *, err%print() | ||
|
||
! Check success | ||
print *, 'Check error: ',err%error() | ||
print *, 'Check flag : ',err /= STDLIB_SUCCESS | ||
|
||
end program example_error_state1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
program example_error_state2 | ||
!! This example shows how to set a `type(state_type)` variable to process output conditions | ||
!! out of a simple division routine. The example is meant to highlight: | ||
!! 1) the different mechanisms that can be used to initialize the `state_type` variable providing | ||
!! strings, scalars, or arrays, on input to it; | ||
!! 2) `pure` setup of the error control | ||
use stdlib_error, only: state_type, STDLIB_VALUE_ERROR, STDLIB_SUCCESS | ||
implicit none | ||
type(state_type) :: err | ||
real :: a_div_b | ||
|
||
! OK | ||
call very_simple_division(0.0,2.0,a_div_b,err) | ||
print *, err%print() | ||
|
||
! Division by zero | ||
call very_simple_division(1.0,0.0,a_div_b,err) | ||
print *, err%print() | ||
|
||
! Out of bounds | ||
call very_simple_division(huge(0.0),0.001,a_div_b,err) | ||
print *, err%print() | ||
|
||
contains | ||
|
||
!> Simple division returning an integer flag (LAPACK style) | ||
elemental subroutine very_simple_division(a,b,a_div_b,err) | ||
real, intent(in) :: a,b | ||
real, intent(out) :: a_div_b | ||
type(state_type), optional, intent(out) :: err | ||
|
||
type(state_type) :: err0 | ||
real, parameter :: MAXABS = huge(0.0) | ||
character(*), parameter :: this = 'simple division' | ||
|
||
!> Check a | ||
if (b==0.0) then | ||
! Division by zero | ||
err0 = state_type(this,STDLIB_VALUE_ERROR,'Division by zero trying ',a,'/',b) | ||
elseif (.not.abs(b)<MAXABS) then | ||
! B is out of bounds | ||
err0 = state_type(this,STDLIB_VALUE_ERROR,'B is infinity in a/b: ',[a,b]) ! use an array | ||
elseif (.not.abs(a)<MAXABS) then | ||
! A is out of bounds | ||
err0 = state_type(this,STDLIB_VALUE_ERROR,'A is infinity in a/b: a=',a,' b=',b) | ||
else | ||
a_div_b = a/b | ||
if (.not.abs(a_div_b)<MAXABS) then | ||
! Result is out of bounds | ||
err0 = state_type(this,STDLIB_VALUE_ERROR,'A/B is infinity in a/b: a=',a,' b=',b) | ||
else | ||
err0%state = STDLIB_SUCCESS | ||
end if | ||
end if | ||
|
||
! Return error flag, or hard stop on failure | ||
call err0%handle(err) | ||
|
||
end subroutine very_simple_division | ||
|
||
|
||
end program example_error_state2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.