Neko 1.99.2
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
41 implicit none
42 private
43
45 type, public :: time_state_t
46 real(kind=rp), dimension(10) :: tlag = 0.0_rp
47 real(kind=rp), dimension(10) :: dtlag = 0.0_rp
48 real(kind=rp) :: t = 0.0_rp
49 real(kind=rp) :: dt = 0.0_rp
50 real(kind=rp) :: start_time = 0.0_rp
51 real(kind=rp) :: end_time = 0.0_rp
52 integer :: tstep = 0
53
54 contains
55 generic :: init => init_from_components, init_from_json
56 procedure, pass(this) :: init_from_components => &
58 procedure, pass(this) :: init_from_json => time_state_init_from_json
59 procedure, pass(this) :: reset => time_state_reset
60 procedure, pass(this) :: restart => time_state_restart
61 procedure, pass(this) :: status => time_state_status
62 procedure, pass(this) :: is_done => time_state_is_done
63 end type time_state_t
64
65contains
66
68 subroutine time_state_init_from_json(this, params)
69 class(time_state_t), intent(inout) :: this
70 type(json_file), intent(inout) :: params
71
72 real(kind=rp) :: time_step
73 real(kind=rp) :: start_time
74 real(kind=rp) :: end_time
75 logical :: is_variable
76
77 call json_get_or_lookup_or_default(params, 'start_time', start_time, 0.0_rp)
78 call json_get_or_lookup(params, 'end_time', end_time)
79 call json_get_or_default(params, 'variable_timestep', is_variable, .false.)
80 if (.not. is_variable) then
81 call json_get_or_lookup(params, 'timestep', time_step)
82 else
83 ! randomly set an initial dt to get cfl when dt is variable
84 time_step = 1.0_rp
85 end if
86
87 call this%init_from_components(start_time, end_time, time_step)
88
89 end subroutine time_state_init_from_json
90
92 subroutine time_state_init_from_components(this, start_time, end_time, dt)
93 class(time_state_t), intent(inout) :: this
94 real(kind=rp), intent(in) :: start_time
95 real(kind=rp), intent(in) :: end_time
96 real(kind=rp), intent(in) :: dt
97
98 if (dt .gt. 0.0_rp .and. start_time .gt. end_time .or. &
99 dt .lt. 0.0_rp .and. start_time .lt. end_time) then
100 call neko_log%error('Time step size must match direction of time.')
101 end if
102
103 this%start_time = start_time
104 this%end_time = end_time
105 this%dt = dt
106
107 this%t = start_time
108 this%tstep = 0
109 this%tlag = start_time
110 this%dtlag = dt
111
113
115 subroutine time_state_reset(this)
116 class(time_state_t), intent(inout) :: this
117
118 this%t = this%start_time
119 this%tstep = 0
120 this%tlag = this%start_time
121 this%dtlag = this%dt
122
123 end subroutine time_state_reset
124
126 subroutine time_state_restart(this, chkp)
127 class(time_state_t), intent(inout) :: this
128 type(chkp_t), intent(in) :: chkp
129
130 this%t = chkp%t
131 this%dtlag = chkp%dtlag
132 this%tlag = chkp%tlag
133 end subroutine time_state_restart
134
136 subroutine time_state_status(this)
137 class(time_state_t), intent(in) :: this
138 character(len=LOG_SIZE) :: log_buf
139 character(len=38) :: log_fmt
140 real(kind=rp) :: t_prog
141
142 t_prog = 100.0_rp * (this%t - this%start_time) / &
143 (this%end_time - this%start_time)
144
145 write(log_fmt, '(A,I2,A)') &
146 '(A7,1X,I10,1X,A4,E15.7,', log_size - 50, 'X,A2,F6.2,A3)'
147 write(log_buf, log_fmt) 'Step = ', this%tstep, 't = ', this%t, &
148 '[ ', t_prog, '% ]'
149
150 call neko_log%message(repeat('-', log_size - 1))
151 call neko_log%message(log_buf, neko_log_quiet)
152 call neko_log%message(repeat('-', log_size - 1))
153
154 end subroutine time_state_status
155
157 pure function time_state_is_done(this) result(is_done)
158 class(time_state_t), intent(in) :: this
159 logical :: is_done
160
161 is_done = this%t - this%start_time .ge. this%end_time - this%start_time &
162 .and. this%tstep .gt. 0
163
164 end function time_state_is_done
165
166end module time_state
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
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:79
type(log_t), public neko_log
Global log stream.
Definition log.f90:77
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.