PB3D  [2.45]
Ideal linear high-n MHD stability in 3-D
sol_vars.f90
Go to the documentation of this file.
1 !------------------------------------------------------------------------------!
3 !------------------------------------------------------------------------------!
4 module sol_vars
5 #include <PB3D_macros.h>
6  use str_utilities
7  use messages
8  use num_vars, only: dp, max_str_ln, iu, weight_dp
9  use grid_vars, only: grid_type
10  use x_vars, only: modes_type
11  use output_ops
12 
13  implicit none
14 
15  private
16 #if ldebug
17  public n_alloc_sols
18 #endif
19 
20  ! global variables
21 #if ldebug
22  integer :: n_alloc_sols
23 #endif
24 
30  type, public :: sol_type
31  integer :: n_mod
32  integer :: lim_sec_x(2)
33  integer, allocatable :: n(:,:)
34  integer, allocatable :: m(:,:)
35  complex(dp), allocatable :: vec(:,:,:)
36  complex(dp), allocatable :: val(:)
37 #if ldebug
38  real(dp) :: estim_mem_usage
39 #endif
40  contains
42  procedure :: init => init_sol
44  procedure :: dealloc => dealloc_sol
45  end type
46 
47 contains
59  subroutine init_sol(sol,mds,grid_sol,n_EV,lim_sec_X)
60  use x_vars, only: set_nm_x
61 #if ldebug
62  use num_vars, only: print_mem_usage, rank
63 #endif
64 
65  ! input / output
66  class(sol_type), intent(inout) :: sol
67  type(modes_type), intent(in) :: mds
68  type(grid_type), intent(in) :: grid_sol
69  integer, intent(in) :: n_EV
70  integer, intent(in), optional :: lim_sec_X(2)
71 
72 #if ldebug
73  ! initialize memory usage
74  if (print_mem_usage) sol%estim_mem_usage = 0._dp
75 #endif
76 
77  ! set mode numbers
78  call set_nm_x(mds,grid_sol,sol%lim_sec_X,sol%n,sol%m,lim_sec_x)
79 
80  ! set n_mod
81  sol%n_mod = size(sol%n,2)
82 
83  ! allocate val
84  allocate(sol%val(1:n_ev))
85 
86  ! allocate vec
87  allocate(sol%vec(1:sol%n_mod,1:grid_sol%loc_n_r,1:n_ev))
88 
89 #if ldebug
90  ! set estimated memory usage
91  if (print_mem_usage) sol%estim_mem_usage = sol%estim_mem_usage + &
92  &n_ev * (1 + sol%n_mod*grid_sol%loc_n_r)
93 
94  ! increment n_alloc_sols
96  if (print_mem_usage) call writo('[rank '//trim(i2str(rank))//&
97  &' - Expected memory usage of sol: '//&
98  &trim(r2strt(sol%estim_mem_usage*weight_dp*2))//' kB]',alert=.true.)
99 #endif
100  end subroutine init_sol
101 
105  subroutine dealloc_sol(sol)
106 #if ldebug
107  use num_vars, only: rank, print_mem_usage
108 #endif
109 
110  ! input / output
111  class(sol_type), intent(inout) :: sol
112 
113 #if ldebug
114  ! local variables
115  integer :: mem_diff ! difference in memory
116  real(dp) :: estim_mem_usage ! estimated memory usage
117 
118  ! memory usage before deallocation
119  if (print_mem_usage) then
120  mem_diff = get_mem_usage()
121  estim_mem_usage = sol%estim_mem_usage
122  end if
123 #endif
124 
125  ! deallocate allocatable variables
126  call dealloc_sol_final(sol)
127 
128 #if ldebug
129  ! decrement n_alloc_sols
131 
132  ! memory usage difference after deallocation
133  if (print_mem_usage) then
134  mem_diff = mem_diff - get_mem_usage()
135  call writo('[Rank '//trim(i2str(rank))//' - liberated '//&
136  &trim(i2str(mem_diff))//'kB deallocating sol ('//&
137  &trim(i2str(nint(100*mem_diff/&
138  &(estim_mem_usage*weight_dp*2))))//&
139  &'% of estimated)]',alert=.true.)
140  end if
141 #endif
142  contains
143  ! Note: intent(out) automatically deallocates the variable
145  subroutine dealloc_sol_final(sol)
146  ! input / output
147  type(sol_type), intent(out) :: sol ! solution to be deallocated
148  end subroutine dealloc_sol_final
149  end subroutine dealloc_sol
150 end module sol_vars
num_vars::dp
integer, parameter, public dp
double precision
Definition: num_vars.f90:46
num_vars
Numerical variables used by most other modules.
Definition: num_vars.f90:4
num_vars::max_str_ln
integer, parameter, public max_str_ln
maximum length of strings
Definition: num_vars.f90:50
str_utilities::i2str
elemental character(len=max_str_ln) function, public i2str(k)
Convert an integer to string.
Definition: str_utilities.f90:18
messages::get_mem_usage
integer function, public get_mem_usage()
Returns the memory usage in kilobytes.
Definition: messages.f90:554
num_vars::iu
complex(dp), parameter, public iu
complex unit
Definition: num_vars.f90:85
sol_vars::init_sol
subroutine init_sol(sol, mds, grid_sol, n_EV, lim_sec_X)
Initialize a solution type and allocate the variables.
Definition: sol_vars.f90:60
str_utilities
Operations on strings.
Definition: str_utilities.f90:4
sol_vars::n_alloc_sols
integer, public n_alloc_sols
nr. of allocated grids
Definition: sol_vars.f90:22
grid_vars::grid_type
Type for grids.
Definition: grid_vars.f90:59
str_utilities::r2strt
elemental character(len=max_str_ln) function, public r2strt(k)
Convert a real (double) to string.
Definition: str_utilities.f90:54
sol_vars::sol_type
solution type
Definition: sol_vars.f90:30
num_vars::weight_dp
real(dp), parameter, public weight_dp
size of double precision in kB
Definition: num_vars.f90:49
num_vars::print_mem_usage
logical, public print_mem_usage
print memory usage is printed
Definition: num_vars.f90:149
x_vars
Variables pertaining to the perturbation quantities.
Definition: X_vars.f90:4
x_vars::modes_type
mode number type
Definition: X_vars.f90:36
messages::writo
subroutine, public writo(input_str, persistent, error, warning, alert)
Write output to file identified by output_i.
Definition: messages.f90:275
messages
Numerical utilities related to giving output.
Definition: messages.f90:4
x_vars::set_nm_x
Sets n_X and m_X.
Definition: X_vars.f90:116
grid_vars
Variables pertaining to the different grids used.
Definition: grid_vars.f90:4
sol_vars
Variables pertaining to the solution quantities.
Definition: sol_vars.f90:4
output_ops
Operations concerning giving output, on the screen as well as in output files.
Definition: output_ops.f90:5
num_vars::rank
integer, public rank
MPI rank.
Definition: num_vars.f90:68
sol_vars::dealloc_sol
subroutine dealloc_sol(sol)
Deallocates solution variables.
Definition: sol_vars.f90:106