File size: 4,340 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
! BOB Quantum Civilization Engine - Error Handling
! Module: bob_errors
! Purpose: Stable error codes and diagnostic message management
! Standard: Fortran 2018

module bob_errors
    use bob_kinds
    implicit none
    private
    
    ! Error codes (stable ABI)
    integer(i4), parameter, public :: BOB_SUCCESS = 0
    integer(i4), parameter, public :: BOB_ERROR_INVALID_ARGUMENT = 1
    integer(i4), parameter, public :: BOB_ERROR_ALLOCATION = 2
    integer(i4), parameter, public :: BOB_ERROR_DIMENSION_MISMATCH = 3
    integer(i4), parameter, public :: BOB_ERROR_NOT_NORMALIZED = 4
    integer(i4), parameter, public :: BOB_ERROR_NOT_HERMITIAN = 5
    integer(i4), parameter, public :: BOB_ERROR_NOT_UNITARY = 6
    integer(i4), parameter, public :: BOB_ERROR_INVALID_STATE = 7
    integer(i4), parameter, public :: BOB_ERROR_INVALID_GATE = 8
    integer(i4), parameter, public :: BOB_ERROR_INVALID_LATTICE = 9
    integer(i4), parameter, public :: BOB_ERROR_BUFFER_TOO_SMALL = 10
    integer(i4), parameter, public :: BOB_ERROR_INTEGRATION_FAILED = 10
    integer(i4), parameter, public :: BOB_ERROR_IO = 11
    integer(i4), parameter, public :: BOB_ERROR_NOT_IMPLEMENTED = 99
    
    ! Maximum diagnostic message length
    integer, parameter, public :: BOB_MAX_ERROR_MSG_LEN = 512
    
    ! Thread-local error state
    type, public :: bob_error_state
        integer(i4) :: code = BOB_SUCCESS
        character(len=BOB_MAX_ERROR_MSG_LEN) :: message = ""
        character(len=256) :: location = ""
    end type bob_error_state
    
    ! Global error state (thread-local in OpenMP builds)
    type(bob_error_state), public, save :: g_error_state
    !$omp threadprivate(g_error_state)
    
    public :: bob_set_error
    public :: bob_get_last_error
    public :: bob_clear_error
    public :: bob_error_message
    
contains

    !> Set error state with code, message, and location
    subroutine bob_set_error(code, message, location)
        integer(i4), intent(in) :: code
        character(len=*), intent(in) :: message
        character(len=*), intent(in), optional :: location
        
        g_error_state%code = code
        g_error_state%message = trim(message)
        
        if (present(location)) then
            g_error_state%location = trim(location)
        else
            g_error_state%location = ""
        end if
    end subroutine bob_set_error
    
    !> Get last error code
    function bob_get_last_error() result(code)
        integer(i4) :: code
        code = g_error_state%code
    end function bob_get_last_error
    
    !> Clear error state
    subroutine bob_clear_error()
        g_error_state%code = BOB_SUCCESS
        g_error_state%message = ""
        g_error_state%location = ""
    end subroutine bob_clear_error
    
    !> Get human-readable error message for error code
    function bob_error_message(code) result(message)
        integer(i4), intent(in) :: code
        character(len=256) :: message
        
        select case (code)
        case (BOB_SUCCESS)
            message = "Success"
        case (BOB_ERROR_INVALID_ARGUMENT)
            message = "Invalid argument"
        case (BOB_ERROR_ALLOCATION)
            message = "Memory allocation failed"
        case (BOB_ERROR_DIMENSION_MISMATCH)
            message = "Dimension mismatch"
        case (BOB_ERROR_NOT_NORMALIZED)
            message = "State not normalized"
        case (BOB_ERROR_NOT_HERMITIAN)
            message = "Operator not Hermitian"
        case (BOB_ERROR_NOT_UNITARY)
            message = "Operator not unitary"
        case (BOB_ERROR_INVALID_STATE)
            message = "Invalid quantum state"
        case (BOB_ERROR_INVALID_GATE)
            message = "Invalid quantum gate"
        case (BOB_ERROR_INVALID_LATTICE)
            message = "Invalid lattice configuration"
        case (BOB_ERROR_INTEGRATION_FAILED)
            message = "Time integration failed"
        case (BOB_ERROR_IO)
            message = "I/O error"
        case (BOB_ERROR_NOT_IMPLEMENTED)
            message = "Feature not implemented"
        case default
            message = "Unknown error"
        end select
    end function bob_error_message

end module bob_errors

! Made with Bob