Neko 1.99.1
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
time_state.f90
Go to the documentation of this file.
1! Copyright (c) 2025, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32
35 use num_types, only : rp
37 use checkpoint, only : chkp_t
38 use json_module, only : json_file
40 implicit none
41 private
42
44 type, public :: time_state_t
45 real(kind=rp), dimension(10) :: tlag = 0.0_rp
46 real(kind=rp), dimension(10) :: dtlag = 0.0_rp
47 real(kind=rp) :: t = 0.0_rp
48 real(kind=rp) :: dt = 0.0_rp
49 real(kind=rp) :: start_time = 0.0_rp
50 real(kind=rp) :: end_time = 0.0_rp
51 integer :: tstep = 0
52
53 contains
54 generic :: init => init_from_components, init_from_json
55 procedure, pass(this) :: init_from_components => &
57 procedure, pass(this) :: init_from_json => time_state_init_from_json
58 procedure, pass(this) :: reset => time_state_reset
59 procedure, pass(this) :: restart => time_state_restart
60 procedure, pass(this) :: status => time_state_status
61 procedure, pass(this) :: is_done => time_state_is_done
62 end type time_state_t
63
64contains
65
67 subroutine time_state_init_from_json(this, params)
68 class(time_state_t), intent(inout) :: this
69 type(json_file), intent(inout) :: params
70
71 real(kind=rp) :: time_step
72 real(kind=rp) :: start_time
73 real(kind=rp) :: end_time
74 logical :: is_variable
75
76 call json_get_or_default(params, 'start_time', start_time, 0.0_rp)
77 call json_get(params, 'end_time', end_time)
78 call json_get_or_default(params, 'variable_timestep', is_variable, .false.)
79 if (.not. is_variable) then
80 call json_get(params, 'timestep', time_step)
81 else
82 ! randomly set an initial dt to get cfl when dt is variable
83 time_step = 1.0_rp
84 end if
85
86 call this%init_from_components(start_time, end_time, time_step)
87
88 end subroutine time_state_init_from_json
89
91 subroutine time_state_init_from_components(this, start_time, end_time, dt)
92 class(time_state_t), intent(inout) :: this
93 real(kind=rp), intent(in) :: start_time
94 real(kind=rp), intent(in) :: end_time
95 real(kind=rp), intent(in) :: dt
96
97 if (dt .gt. 0.0_rp .and. start_time .gt. end_time .or. &
98 dt .lt. 0.0_rp .and. start_time .lt. end_time) then
99 call neko_log%error('Time step size must match direction of time.')
100 end if
101
102 this%start_time = start_time
103 this%end_time = end_time
104 this%dt = dt
105
106 this%t = start_time
107 this%tstep = 0
108 this%tlag = start_time
109 this%dtlag = dt
110
112
114 subroutine time_state_reset(this)
115 class(time_state_t), intent(inout) :: this
116
117 this%t = this%start_time
118 this%tstep = 0
119 this%tlag = this%start_time
120 this%dtlag = this%dt
121
122 end subroutine time_state_reset
123
125 subroutine time_state_restart(this, chkp)
126 class(time_state_t), intent(inout) :: this
127 type(chkp_t), intent(in) :: chkp
128
129 this%t = chkp%t
130 this%dtlag = chkp%dtlag
131 this%tlag = chkp%tlag
132 end subroutine time_state_restart
133
135 subroutine time_state_status(this)
136 class(time_state_t), intent(in) :: this
137 character(len=LOG_SIZE) :: log_buf
138 character(len=38) :: log_fmt
139 real(kind=rp) :: t_prog
140
141 t_prog = 100.0_rp * (this%t - this%start_time) / &
142 (this%end_time - this%start_time)
143
144 write(log_fmt, '(A,I2,A)') &
145 '(A7,1X,I10,1X,A4,E15.7,', log_size - 50, 'X,A2,F6.2,A3)'
146 write(log_buf, log_fmt) 'Step = ', this%tstep, 't = ', this%t, &
147 '[ ', t_prog, '% ]'
148
149 call neko_log%message(repeat('-', log_size - 1))
150 call neko_log%message(log_buf, neko_log_quiet)
151 call neko_log%message(repeat('-', log_size - 1))
152
153 end subroutine time_state_status
154
156 pure function time_state_is_done(this) result(is_done)
157 class(time_state_t), intent(in) :: this
158 logical :: is_done
159
160 is_done = this%t - this%start_time .ge. this%end_time - this%start_time &
161 .and. this%tstep .gt. 0
162
163 end function time_state_is_done
164
165end module time_state
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Retrieves a parameter by name or throws an error.
Defines a checkpoint.
Utilities for retrieving parameters from the case files.
Logging routines.
Definition log.f90:34
integer, parameter, public neko_log_quiet
Always logged.
Definition log.f90:72
type(log_t), public neko_log
Global log stream.
Definition log.f90:70
integer, parameter, public log_size
Definition log.f90:46
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:12
Module with things related to the simulation time.
subroutine time_state_restart(this, chkp)
Restart time state.
subroutine time_state_status(this)
Write status banner.
subroutine time_state_init_from_json(this, params)
Initialize time state from JSON.
pure logical function time_state_is_done(this)
Check if the simulation is done.
subroutine time_state_init_from_components(this, start_time, end_time, dt)
Initialize time state.
subroutine time_state_reset(this)
Reset time state.
A struct that contains all info about the time, expand as needed.