Skip to content

Commit 1debdcf

Browse files
committed
Add test_simple_initial_value_write_init test
1 parent 167f4a0 commit 1debdcf

File tree

4 files changed

+757
-0
lines changed

4 files changed

+757
-0
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
!
2+
! This work (Common Community Physics Package Framework), identified by
3+
! NOAA, NCAR, CU/CIRES, is free of known copyright restrictions and is
4+
! placed in the public domain.
5+
!
6+
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7+
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8+
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
9+
! THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
10+
! IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
11+
! CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12+
13+
14+
!>
15+
!! @brief Auto-generated Initialization-checking source file
16+
!!
17+
!
18+
module phys_vars_init_check_initial_value
19+
20+
21+
implicit none
22+
private
23+
24+
!! public interfaces
25+
public :: mark_as_initialized
26+
public :: mark_as_read_from_file
27+
public :: is_initialized
28+
public :: is_read_from_file
29+
30+
!! Parameterized initialized_vars options - order matters
31+
integer, public, parameter :: UNINITIALIZED = 0
32+
integer, public, parameter :: INITIALIZED = 1
33+
integer, public, parameter :: PARAM = 2
34+
integer, public, parameter :: READ_FROM_FILE = 3
35+
! Total number of physics-related variables:
36+
integer, public, parameter :: phys_var_num = 2
37+
integer, public, parameter :: phys_const_num = 16
38+
39+
!Max length of physics-related variable standard names:
40+
integer, public, parameter :: std_name_len = 25
41+
42+
! Max length of input (IC) file variable names:
43+
integer, public, parameter :: ic_name_len = 12
44+
45+
! Physics-related input variable standard names:
46+
character(len=25), public, protected :: phys_var_stdnames(phys_var_num) = (/ &
47+
'potential_temperature ', &
48+
'air_pressure_at_sea_level' /)
49+
50+
character(len=36), public, protected :: phys_const_stdnames(phys_const_num) = (/ &
51+
"ccpp_constituent_minimum_values ", &
52+
"ccpp_constituent_properties ", &
53+
"ccpp_constituent_tendencies ", &
54+
"ccpp_constituents ", &
55+
"ccpp_error_code ", &
56+
"ccpp_error_message ", &
57+
"do_log_output ", &
58+
"log_output_unit ", &
59+
"mpi_communicator ", &
60+
"mpi_rank ", &
61+
"mpi_root ", &
62+
"number_of_ccpp_advected_constituents", &
63+
"number_of_ccpp_constituents ", &
64+
"number_of_mpi_tasks ", &
65+
"suite_name ", &
66+
"suite_part " /)
67+
!Array storing all registered IC file input names for each variable:
68+
character(len=12), public, protected :: input_var_names(2, phys_var_num) = reshape((/ &
69+
'theta ', 'pot_temp ', &
70+
'slp ', 'sea_lev_pres' /), (/2, phys_var_num/))
71+
72+
! Array indicating whether or not variable is protected:
73+
logical, public, protected :: protected_vars(phys_var_num)= (/ &
74+
.false., &
75+
.false. /)
76+
77+
! Variable state (UNINITIALIZED, INTIIALIZED, PARAM or READ_FROM_FILE):
78+
integer, public, protected :: initialized_vars(phys_var_num)= (/ &
79+
UNINITIALIZED, &
80+
UNINITIALIZED /)
81+
82+
83+
CONTAINS
84+
85+
subroutine mark_as_initialized(varname)
86+
87+
! This subroutine marks the variable, <varname>, as
88+
! INITIALIZED in the `initialized_vars` array,
89+
! which means any initialization check should
90+
! now return True.
91+
92+
! Dummy argument
93+
character(len=*), intent(in) :: varname !Variable name being marked
94+
95+
! Local variable
96+
integer :: stdnam_idx !Standard name array index
97+
98+
! Search for <varname> in the standard name array:
99+
do stdnam_idx = 1, phys_var_num
100+
if (trim(phys_var_stdnames(stdnam_idx)) == trim(varname)) then
101+
! Only set to INITIALIZED if state is UNINITIALIZED
102+
if (initialized_vars(stdnam_idx) < PARAM) then
103+
initialized_vars(stdnam_idx) = INITIALIZED
104+
end if
105+
exit ! Exit loop once variable has been found and initialized
106+
end if
107+
end do
108+
109+
! No match is not an error because <phys_var_stdnames> only
110+
! contains variables required by a physics suite.
111+
112+
end subroutine mark_as_initialized
113+
114+
subroutine mark_as_read_from_file(varname)
115+
116+
! This subroutine marks the varible, <varname>, as READ_FROM_FILE in the
117+
! initialized_vars array
118+
119+
use cam_abortutils, only: endrun
120+
121+
! Dummy argument
122+
character(len=*), intent(in) :: varname ! Variable name being marked
123+
124+
! Local variables
125+
integer :: stdnam_idx ! Standard name array index
126+
logical :: found_var ! .true. if <varname> is in arr.
127+
character(len=*), parameter :: subname = 'mark_as_read_from_file'
128+
129+
found_var = .false.
130+
! Set variable to READ_FROM_FILE:
131+
do stdnam_idx = 1, phys_var_num
132+
if (trim(phys_var_stdnames(stdnam_idx)) == trim(varname)) then
133+
! It is an error if the variable has already been set to PARAM
134+
if (initialized_vars(stdnam_idx) == PARAM) then
135+
call endrun("Variable '"//trim(varname)// &
136+
"' was read from file, but is a parameter")
137+
end if
138+
initialized_vars(stdnam_idx) = READ_FROM_FILE
139+
140+
! Indicate variable has been found:
141+
found_var = .true.
142+
exit ! Exit loop once variable has been found and marked
143+
end if
144+
end do
145+
146+
if (.not. found_var) then
147+
! This condition is an internal error, it should not happen
148+
call endrun(subname//": Variable '"//trim(varname)// &
149+
"' is missing from phys_var_stdnames array.")
150+
end if
151+
152+
end subroutine mark_as_read_from_file
153+
154+
logical function is_initialized(varname)
155+
156+
! This function checks if the variable, <varname>, is already
157+
! initialized according to the 'initialized_vars' array.
158+
159+
use cam_abortutils, only: endrun
160+
161+
! Dummy argument
162+
character(len=*), intent(in) :: varname ! Variable name being checked
163+
164+
! Local variables
165+
integer :: stdnam_idx ! Standard name array index
166+
logical :: found ! Check that <varname> was found
167+
character(len=*), parameter :: subname = 'is_initialized: '
168+
169+
is_initialized = .false.
170+
found = .false.
171+
172+
! Check if variable is initialized (PARAM, INITIALIZED, or READ_FROM_FILE)
173+
do stdnam_idx = 1, phys_var_num
174+
if (trim(phys_var_stdnames(stdnam_idx)) == trim(varname)) then
175+
is_initialized = (initialized_vars(stdnam_idx) > UNINITIALIZED)
176+
found = .true.
177+
exit ! Exit loop once variable has been found and checked
178+
end if
179+
end do
180+
181+
if (.not. found) then
182+
! This condition is an internal error, it should not happen
183+
call endrun(subname//": Variable '"//trim(varname)// &
184+
"' is missing from phys_var_stdnames array.")
185+
end if
186+
187+
end function is_initialized
188+
189+
subroutine is_read_from_file(varname, is_read, stdnam_idx_out)
190+
191+
! This subroutine checks if the variable, <varname>, is read from
192+
! file according to the 'initialized_vars' array.
193+
194+
use cam_abortutils, only: endrun
195+
196+
! Dummy arguments
197+
character(len=*), intent(in) :: varname ! Variable name being checked
198+
logical, intent(out) :: is_read ! Set to .true. if from file
199+
integer, optional, intent(out) :: stdnam_idx_out
200+
201+
! Local variables
202+
203+
integer :: stdnam_idx ! Standard name array index
204+
logical :: found ! Check that <varname> was found
205+
character(len=*), parameter :: subname = 'is_read_from_file: '
206+
207+
is_read = .false.
208+
found = .false.
209+
210+
! Return .true. if the variable's status is READ_FROM_FILE:
211+
do stdnam_idx = 1, phys_var_num
212+
if (trim(phys_var_stdnames(stdnam_idx)) == trim(varname)) then
213+
is_read = (initialized_vars(stdnam_idx) == READ_FROM_FILE)
214+
! Mark as found:
215+
found = .true.
216+
exit ! Exit loop once variable has been found and checked
217+
end if
218+
end do
219+
220+
if (.not. found) then
221+
! Check to see if this is an internally-protected variable
222+
do stdnam_idx = 1, phys_const_num
223+
if (trim(phys_const_stdnames(stdnam_idx)) == trim(varname)) then
224+
found = .true.
225+
exit ! Exit loop once variable has been found
226+
end if
227+
end do
228+
end if
229+
230+
if (.not. found) then
231+
! This condition is an internal error, it should not happen
232+
call endrun(subname//": Variable '"//trim(varname)// &
233+
"' is missing from phys_var_stdnames array.")
234+
end if
235+
if (present(stdnam_idx_out)) then
236+
stdnam_idx_out = stdnam_idx
237+
end if
238+
239+
end subroutine is_read_from_file
240+
241+
end module phys_vars_init_check_initial_value

0 commit comments

Comments
 (0)