Globals Library 1.0
Loading...
Searching...
No Matches
modScratch.f90
Go to the documentation of this file.
1!>-------------------------------------------------------------
2!> @brief Auxiliary module to store an integer and real array
3!> used (typically as optional argument) as scratch in different code.
4!<-------------------------------------------------------------
5module scratch
6
7 use globals
8
9 implicit none
10
11 private
12
13 type, public :: scrt
14 !> Flag for initialization
15 !> Used to check if arrays are allocated
16 logical :: is_initialized = .false.
17 !> Size of integer array `::iaux`
18 integer :: niaux = 0
19 !> Size of real array `::raux`
20 integer :: nraux = 0
21 !> Dimension (`::niaux`)
22 !> Integer scratch array
23 integer, allocatable :: iaux(:)
24 !> Dimension (`::niaux`)
25 !> Real scratch array
26 real(kind=double), allocatable :: raux(:)
27 contains
28 !> Static constructor for `scratch::scrt`
29 procedure, public, pass :: init => init_scrt
30 !> Static destructor for `scratch::scrt`
31 procedure, public, pass :: kill => kill_scrt
32 !> Info procedure for `scratch::scrt`
33 procedure, public, pass :: info => info_scrt
34 !> Check is scratch arrays are big enoguh
35 procedure, public, pass :: check => check_scrt
36 !> Check is scratch arrays are big enoguh
37 procedure, public, pass :: is_enough => check_scrt
38 !> Define `ibegin`, `iend` for assign portion of arrays
39 procedure, public, nopass :: range
40 end type scrt
41
42contains
43
44 !>-------------------------------------------------------------
45 !> @brief Procedure to define portion of member `scrt::iaux`
46 !> or `scrt::raux`
47 !>
48 !> **Example**: partioning `raux` in two portion of length
49 !> `n1` and `n2`, assign it to two two real pointer `v1` and `v2`.
50 !>
51 !> ```fortran
52 !> iend = 0
53 !> call aux%range(n1, ibegin, iend)
54 !> v1 => aux%raux(ibegin:iend)
55 !> call aux%range(n2, ibegin, iend)
56 !> v2 => aux%raux(ibegin:iend)
57 !> ```
58 !>
59 !> @param[in] size integer. Size of array required
60 !> @param[out] ibegin integer. Start position of array portion
61 !> @param[inout] iend integer. End position of array portion.
62 !> It cointains the end position of previous
63 !> array or it must begin initialize to zero.
64 !<----------------------------------------------------------------
65 subroutine range(size, ibegin, iend)
66 implicit none
67 integer, intent(in) :: size
68 integer, intent(out) :: ibegin
69 integer, intent(inout) :: iend
70
71 ibegin = iend + 1
72 iend = ibegin + size - 1
73
74 end subroutine range
75
76 !>-------------------------------------------------------------
77 !> @brief Static constructor for `scratch::scrt`
78 !> @details Set `scrt::niaux` and `scrt::nraux` and allocate
79 !> arrays `scrt::iaux` and `scrt::raux`.
80 !>
81 !> @param[in] lun_err integer, unit for error message output
82 !> @param[in] niaux integer, length integer array
83 !> @param[in] nraux integer, length real array
84 !<-------------------------------------------------------------
85 subroutine init_scrt(this, lun_err, niaux, nraux)
86 implicit none
87 class(scrt), intent(inout) :: this
88 integer, intent(in) :: lun_err
89 integer, intent(in) :: niaux
90 integer, intent(in) :: nraux
91 !local
92 integer :: res
93 logical :: rc
94
95 this%is_initialized = .true.
96 this%niaux = niaux
97 this%nraux = nraux
98 allocate (this%iaux(this%niaux), this%raux(this%nraux), stat=res)
99 if (res .ne. 0) rc = ioerr(lun_err, err_alloc, 'init_aux', &
100 ' array iaux raux', res)
101
102 end subroutine init_scrt
103
104 !>-------------------------------------------------------------
105 !> @brief Destructor for `scratch::scrt`.
106 !> @details Deallocate variables `scrt::iaux` and `scrt::raux`.
107 !>
108 !> @param[in] lun: integer, unit number for error message
109 !<-----------------------------------------------------------
110 subroutine kill_scrt(this, lun)
111 implicit none
112 class(scrt), intent(inout) :: this
113 integer, intent(in) :: lun
114 ! local vars
115 integer :: res
116 logical :: rc
117
118 if (this%is_initialized) then
119 deallocate (this%iaux, this%raux, stat=res)
120 if (res .ne. 0) rc = ioerr(lun, err_dealloc, 'kill_scrt', &
121 'dealloc fail for type scrt member iaux raux', res)
122 end if
123
124 this%is_initialized = .false.
125 this%niaux = 0
126 this%nraux = 0
127
128 end subroutine kill_scrt
129
130 !>-------------------------------------------------------------
131 !> @brief Info procedure for `scratch::scrt`
132 !> @details Prints content of a variable of type `scratch::scrt`
133 !>
134 !> @param[in] lun: integer, unit number for output message
135 !<-------------------------------------------------------------
136 subroutine info_scrt(this, lun)
137 implicit none
138 class(scrt), intent(in) :: this
139 integer, intent(in) :: lun
140
141 if (this%is_initialized) then
142 write (lun, *) 'niaux = ', this%niaux, 'nraux = ', this%nraux
143 else
144 write (lun, *) 'Scratch type not initialized'
145 end if
146
147 end subroutine info_scrt
148
149 !>-------------------------------------------------------------
150 !> @brief Check is scratch arrays are big enoguh
151 !> @details Return false if a variable of type `scratch::scrt`
152 !> is not initialized, or it contains integer or real array that
153 !> are too small.
154 !>
155 !> @param[in] niaux integer. Number of integer required
156 !> @param[in] nraux integer. Number of real required
157 !> @return (logical) `True`: type initialized and big enough,
158 !> `False` type not initialized or not big enough.
159 !<-------------------------------------------------------------
160 function check_scrt(this, niaux, nraux) result(test)
161 implicit none
162 class(scrt), intent(in) :: this
163 integer, intent(in) :: niaux
164 integer, intent(in) :: nraux
165 logical :: test
166
167 test = .true.
168 if (niaux .gt. this%niaux .or. &
169 nraux .gt. this%nraux .or. &
170 .not. this%is_initialized) then
171 test = .false.
172 end if
173
174 end function check_scrt
175
176end module scratch
integer, parameter err_alloc
Error allocation failed.
integer, parameter err_dealloc
Error deallocation failed.
logical function ioerr(lun, errno, call_proc, add_msg, add_int)
Handle and write alert I/O warnings and errors.
Auxiliary module to store an integer and real array used (typically as optional argument) as scratch ...
Definition modScratch.f90:5
subroutine init_scrt(this, lun_err, niaux, nraux)
Static constructor for scratch::scrt.
subroutine info_scrt(this, lun)
Info procedure for scratch::scrt.
subroutine kill_scrt(this, lun)
Destructor for scratch::scrt.
logical function check_scrt(this, niaux, nraux)
Check is scratch arrays are big enoguh.
subroutine range(size, ibegin, iend)
Procedure to define portion of member scrt::iaux or scrt::raux.