MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
SeqVector.f90
Go to the documentation of this file.
2  use kindmodule, only: i4b, dp
3  use constantsmodule, only: dzero
6  implicit none
7  private
8 
9  type, public, extends(vectorbasetype) :: seqvectortype
10  integer(I4B), private :: size
11  real(dp), dimension(:), pointer, contiguous :: array
12  contains
13  procedure :: create_mm => sqv_create_mm
14  procedure :: create => sqv_create
15  procedure :: destroy => sqv_destroy
16  procedure :: get_array => sqv_get_array
17  procedure :: get_ownership_range => sqv_get_ownership_range
18  procedure :: get_size => sqv_get_size
19  procedure :: get_value_local => sqv_get_value_local
20  procedure :: zero_entries => sqv_zero_entries
21  procedure :: set_value_local => sqv_set_value_local
22  procedure :: axpy => sqv_axpy
23  procedure :: norm2 => sqv_norm2
24  procedure :: print => sqv_print
25  end type seqvectortype
26 
27 contains
28 
29  !> @brief Create a sequential vector: the classic MF6 version,
30  !< storing in the memory manager.
31  subroutine sqv_create_mm(this, n, name, mem_path)
32  class(seqvectortype) :: this !< this vector
33  integer(I4B) :: n !< the nr. of elements in the vector
34  character(len=*) :: name !< the variable name (for access through memory manager)
35  character(len=*) :: mem_path !< memory path for storing the underlying memory items
36 
37  this%size = n
38  this%is_mem_managed = .true.
39  call mem_allocate(this%array, n, name, mem_path)
40  call this%zero_entries()
41 
42  end subroutine sqv_create_mm
43 
44  !> @brief Create a sequential vector: the classic MF6 version
45  !<
46  subroutine sqv_create(this, n)
47  class(seqvectortype) :: this !< this vector
48  integer(I4B) :: n !< the nr. of elements in the vector
49 
50  this%size = n
51  this%is_mem_managed = .false.
52  allocate (this%array(n))
53  call this%zero_entries()
54 
55  end subroutine sqv_create
56 
57  !> @brief Clean up
58  !<
59  subroutine sqv_destroy(this)
60  class(seqvectortype) :: this !< this vector
61 
62  if (this%is_mem_managed) then
63  call mem_deallocate(this%array)
64  else
65  deallocate (this%array)
66  end if
67 
68  end subroutine sqv_destroy
69 
70  !> @brief Get a pointer to the underlying data array
71  !< for this vector
72  function sqv_get_array(this) result(array)
73  class(seqvectortype) :: this !< this vector
74  real(dp), dimension(:), pointer, contiguous :: array !< the underlying data array for this vector
75 
76  array => this%array
77 
78  end function sqv_get_array
79 
80  subroutine sqv_get_ownership_range(this, start, end)
81  class(seqvectortype) :: this !< this vector
82  integer(I4B) :: start !< the index of the first element in the vector
83  integer(I4B) :: end !< the index of the last element in the vector
84 
85  start = 1
86  end = this%size
87 
88  end subroutine sqv_get_ownership_range
89 
90  function sqv_get_size(this) result(size)
91  class(seqvectortype) :: this !< this vector
92  integer(I4B) :: size !< the vector size
93 
94  size = this%size
95 
96  end function sqv_get_size
97 
98  !> @brief Get value at local index
99  !<
100  function sqv_get_value_local(this, idx) result(val)
101  class(seqvectortype) :: this !< this vector
102  integer(I4B) :: idx !< the index in local numbering
103  real(dp) :: val !< the value
104 
105  val = this%array(idx)
106 
107  end function sqv_get_value_local
108 
109  !> @brief set all elements to zero
110  !<
111  subroutine sqv_zero_entries(this)
112  class(seqvectortype) :: this !< this vector
113  ! local
114  integer(I4B) :: i
115 
116  do i = 1, this%size
117  this%array(i) = dzero
118  end do
119 
120  end subroutine sqv_zero_entries
121 
122  !> @brief Set vector value at local index
123  !<
124  subroutine sqv_set_value_local(this, idx, val)
125  class(seqvectortype) :: this !< this vector
126  integer(I4B) :: idx !< the index in local numbering
127  real(DP) :: val !< the value to set
128 
129  this%array(idx) = val
130 
131  end subroutine sqv_set_value_local
132 
133  !> @brief Caculcates AXPY: y = a*x + y
134  !<
135  subroutine sqv_axpy(this, alpha, vec_x)
136  class(seqvectortype) :: this !< this vector
137  real(DP) :: alpha !< the factor
138  class(vectorbasetype), pointer :: vec_x !< the vector to add
139  ! local
140  integer(I4B) :: i
141  real(DP), dimension(:), pointer, contiguous :: x_array
142 
143  x_array => vec_x%get_array()
144  do i = 1, this%size
145  this%array(i) = alpha * x_array(i) + this%array(i)
146  end do
147 
148  end subroutine sqv_axpy
149 
150  !> @brief Calculate the 2-norm
151  !<
152  function sqv_norm2(this) result(n2)
153  class(seqvectortype) :: this !< this vector
154  real(dp) :: n2
155  ! local
156  integer(I4B) :: i
157 
158  n2 = dzero
159  do i = 1, this%size
160  n2 = n2 + this%array(i)**2
161  end do
162  n2 = sqrt(n2)
163 
164  end function sqv_norm2
165 
166  subroutine sqv_print(this)
167  class(seqvectortype) :: this !< this vector
168 
169  write (*, *) this%array
170 
171  end subroutine sqv_print
172 
173 end module seqvectormodule
This module contains simulation constants.
Definition: Constants.f90:9
real(dp), parameter dzero
real constant zero
Definition: Constants.f90:65
This module defines variable data types.
Definition: kind.f90:8
integer(i4b) function sqv_get_size(this)
Definition: SeqVector.f90:91
subroutine sqv_destroy(this)
Clean up.
Definition: SeqVector.f90:60
subroutine sqv_set_value_local(this, idx, val)
Set vector value at local index.
Definition: SeqVector.f90:125
subroutine sqv_create_mm(this, n, name, mem_path)
Create a sequential vector: the classic MF6 version,.
Definition: SeqVector.f90:32
subroutine sqv_get_ownership_range(this, start, end)
Definition: SeqVector.f90:81
real(dp) function, dimension(:), pointer, contiguous sqv_get_array(this)
Get a pointer to the underlying data array.
Definition: SeqVector.f90:73
real(dp) function sqv_get_value_local(this, idx)
Get value at local index.
Definition: SeqVector.f90:101
subroutine sqv_zero_entries(this)
set all elements to zero
Definition: SeqVector.f90:112
subroutine sqv_create(this, n)
Create a sequential vector: the classic MF6 version.
Definition: SeqVector.f90:47
subroutine sqv_print(this)
Definition: SeqVector.f90:167
subroutine sqv_axpy(this, alpha, vec_x)
Caculcates AXPY: y = a*x + y.
Definition: SeqVector.f90:136
real(dp) function sqv_norm2(this)
Calculate the 2-norm.
Definition: SeqVector.f90:153