PB3D [2.47]
Ideal linear high-n MHD stability in 3-D
Loading...
Searching...
No Matches
sol_vars.f90
Go to the documentation of this file.
1!------------------------------------------------------------------------------!
2!> Variables pertaining to the solution quantities.
3!------------------------------------------------------------------------------!
4module sol_vars
5#include <PB3D_macros.h>
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 !< nr. of allocated grids \ldebug
23#endif
24
25 !> solution type
26 !!
27 !! The arrays here are of the form:
28 !! - \c val: <tt>(1:n_EV)</tt>
29 !! - \c vec: <tt>(1:n_mod,1:loc_n_r,1:n_EV)</tt>
30 type, public :: sol_type
31 integer :: n_mod !< size of n and m (nr. of modes)
32 integer :: lim_sec_x(2) !< limits of \c m_X (pol. flux) or \c n_X (tor. flux)
33 integer, allocatable :: n(:,:) !< vector of toroidal mode numbers
34 integer, allocatable :: m(:,:) !< vector of poloidal mode numbers
35 complex(dp), allocatable :: vec(:,:,:) !< Eigenvector solution
36 complex(dp), allocatable :: val(:) !< Eigenvalue solution
37#if ldebug
38 real(dp) :: estim_mem_usage !< estimated memory usage \ldebug
39#endif
40 contains
41 !> initialize
42 procedure :: init => init_sol
43 !> deallocate
44 procedure :: dealloc => dealloc_sol
45 end type
46
47contains
48 !> \public Initialize a solution type and allocate the variables.
49 !!
50 !! The number of modes as well as \c n and \c m are also set up.
51 !!
52 !! Optionally, the secondary mode number can be specified (\c m if poloidal
53 !! flux is used as normal coordinate and c n if toroidal flux). By default,
54 !! they are taken from the global \c X_vars variables.
55 !!
56 !! \note If the lowest limits of the grid is not 1 (e.g. <tt>grid_sol%i_min
57 !! = 1</tt> for first process), the input variable \c i_min should be set to
58 !! set correctly. For a full grid, it should be set to 1.
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 !< solution variables
67 type(modes_type), intent(in) :: mds !< general modes variables
68 type(grid_type), intent(in) :: grid_sol !< solution grid
69 integer, intent(in) :: n_EV !< nr. of Eigenvalues
70 integer, intent(in), optional :: lim_sec_X(2) !< limits of \c m_X (pol. flux) or \c n_X (tor. flux)
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
102 !> \public Deallocates solution variables.
103 !!
104 !! \note intent(out) automatically deallocates the variable
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 !< solution variables to be deallocated
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
144 !> \private
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
150end module sol_vars
Sets n_X and m_X.
Definition X_vars.f90:116
Variables pertaining to the different grids used.
Definition grid_vars.f90:4
Numerical utilities related to giving output.
Definition messages.f90:4
integer function, public get_mem_usage()
Returns the memory usage in kilobytes.
Definition messages.f90:554
subroutine, public writo(input_str, persistent, error, warning, alert)
Write output to file identified by output_i.
Definition messages.f90:275
Numerical variables used by most other modules.
Definition num_vars.f90:4
integer, parameter, public dp
double precision
Definition num_vars.f90:46
logical, public print_mem_usage
print memory usage is printed
Definition num_vars.f90:149
complex(dp), parameter, public iu
complex unit
Definition num_vars.f90:85
integer, parameter, public max_str_ln
maximum length of strings
Definition num_vars.f90:50
integer, public rank
MPI rank.
Definition num_vars.f90:68
real(dp), parameter, public weight_dp
size of double precision in kB
Definition num_vars.f90:49
Operations concerning giving output, on the screen as well as in output files.
Definition output_ops.f90:5
Variables pertaining to the solution quantities.
Definition sol_vars.f90:4
integer, public n_alloc_sols
nr. of allocated grids
Definition sol_vars.f90:22
subroutine dealloc_sol(sol)
Deallocates solution variables.
Definition sol_vars.f90:106
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
Operations on strings.
elemental character(len=max_str_ln) function, public i2str(k)
Convert an integer to string.
elemental character(len=max_str_ln) function, public r2strt(k)
Convert a real (double) to string.
Variables pertaining to the perturbation quantities.
Definition X_vars.f90:4
Type for grids.
Definition grid_vars.f90:59
solution type
Definition sol_vars.f90:30
mode number type
Definition X_vars.f90:36