Skip to content

Commit bcc3aba

Browse files
peverwheeCourtney Peverley
andauthored
Constituent bugfixes (#608)
Fixes a couple of small bugs in the constituents object 1. Add missing units field to equivalence check and constituent copy 2. Add missing fields to instantiate call 3. Parse mixing ratio type from standard name correctly (if "mixing_ratio_type" not provided to instantiate) 4. Add check of errflg in register to return before allocating if there's an error User interface changes?: No Fixes #587 Testing: test removed: N/A unit tests: All pass system tests: All pass; modified advection test to check new instantiate fields manual testing: Run w/ register phase in CAM-SIMA --------- Co-authored-by: Courtney Peverley <courtneyp@izumi.cgd.ucar.edu>
1 parent 76dbf36 commit bcc3aba

File tree

6 files changed

+126
-16
lines changed

6 files changed

+126
-16
lines changed

scripts/host_cap.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,9 @@ def write_host_cap(host_model, api, module_name, output_dir, run_env):
696696
call_str = suite_part_call_list(host_model, const_dict, spart, False,
697697
dyn_const=True)
698698
cap.write(f"call {suite.name}_{stage}({call_str})", 3)
699+
cap.write("if (errflg /= 0) then", 3)
700+
cap.write("return", 4)
701+
cap.write("end if", 3)
699702
# Allocate the suite's dynamic constituents array
700703
size_string = "0+"
701704
for var in host_local_vars.variable_list():

src/ccpp_constituent_prop_mod.F90

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ subroutine copyConstituent(outConst, inConst)
228228
outConst%molar_mass_val = inConst%molar_mass_val
229229
outConst%thermo_active = inConst%thermo_active
230230
outConst%water_species = inConst%water_species
231+
outConst%var_units = inConst%var_units
232+
outConst%const_water = inConst%const_water
231233
end subroutine copyConstituent
232234

233235
!#######################################################################
@@ -371,7 +373,8 @@ end function ccp_is_instantiated
371373
!#######################################################################
372374

373375
subroutine ccp_instantiate(this, std_name, long_name, units, vertical_dim, &
374-
advected, default_value, min_value, molar_mass, errcode, errmsg)
376+
advected, default_value, min_value, molar_mass, water_species, &
377+
mixing_ratio_type, errcode, errmsg)
375378
! Initialize all fields in <this>
376379

377380
! Dummy arguments
@@ -384,6 +387,8 @@ subroutine ccp_instantiate(this, std_name, long_name, units, vertical_dim, &
384387
real(kind_phys), optional, intent(in) :: default_value
385388
real(kind_phys), optional, intent(in) :: min_value
386389
real(kind_phys), optional, intent(in) :: molar_mass
390+
logical, optional, intent(in) :: water_species
391+
character(len=*), optional, intent(in) :: mixing_ratio_type
387392
integer, intent(out) :: errcode
388393
character(len=*), intent(out) :: errmsg
389394

@@ -414,6 +419,9 @@ subroutine ccp_instantiate(this, std_name, long_name, units, vertical_dim, &
414419
if (present(molar_mass)) then
415420
this%molar_mass_val = molar_mass
416421
end if
422+
if (present(water_species)) then
423+
this%water_species = water_species
424+
end if
417425
end if
418426
if (errcode == 0) then
419427
if (index(this%var_std_name, "volume_mixing_ratio") > 0) then
@@ -426,14 +434,29 @@ subroutine ccp_instantiate(this, std_name, long_name, units, vertical_dim, &
426434
end if
427435
if (errcode == 0) then
428436
! Determine if this mixing ratio is dry, moist, or "wet".
429-
if (index(this%var_std_name, "wrt_moist_air") > 0) then
430-
this%const_water = moist_mixing_ratio
431-
else if (this%var_std_name == "specific_humidity") then
432-
this%const_water = moist_mixing_ratio
433-
else if (this%var_std_name == "wrt_total_mass") then
434-
this%const_water = wet_mixing_ratio
437+
! If a type was provided, use that (if it's valid)
438+
if (present(mixing_ratio_type)) then
439+
if (trim(mixing_ratio_type) == 'wet') then
440+
this%const_water = wet_mixing_ratio
441+
else if (trim(mixing_ratio_type) == 'moist') then
442+
this%const_water = moist_mixing_ratio
443+
else if (trim(mixing_ratio_type) == 'dry') then
444+
this%const_water = dry_mixing_ratio
445+
else
446+
errcode = 1
447+
write(errmsg, *) 'ccp_instantiate: invalid mixing ratio type. ', &
448+
'Must be one of: "wet", "moist", or "dry". Got: "', &
449+
trim(mixing_ratio_type), '"'
450+
end if
435451
else
436-
this%const_water = dry_mixing_ratio
452+
! Otherwise, parse it from the standard name
453+
if (index(this%var_std_name, "wrt_moist_air_and_condensed_water") > 0) then
454+
this%const_water = wet_mixing_ratio
455+
else if (index(this%var_std_name, "wrt_moist_air") > 0) then
456+
this%const_water = moist_mixing_ratio
457+
else
458+
this%const_water = dry_mixing_ratio
459+
end if
437460
end if
438461
end if
439462
if (errcode /= 0) then
@@ -740,11 +763,13 @@ subroutine ccp_is_equivalent(this, oconst, equiv, errcode, errmsg)
740763
equiv = (trim(this%var_std_name) == trim(oconst%var_std_name)) .and. &
741764
(trim(this%var_long_name) == trim(oconst%var_long_name)) .and. &
742765
(trim(this%vert_dim) == trim(oconst%vert_dim)) .and. &
766+
(trim(this%var_units) == trim(oconst%var_units)) .and. &
743767
(this%advected .eqv. oconst%advected) .and. &
744768
(this%const_default_value == oconst%const_default_value) .and. &
745769
(this%min_val == oconst%min_val) .and. &
746770
(this%molar_mass_val == oconst%molar_mass_val) .and. &
747771
(this%thermo_active .eqv. oconst%thermo_active) .and. &
772+
(this%const_water == oconst%const_water) .and. &
748773
(this%water_species .eqv. oconst%water_species)
749774
else
750775
equiv = .false.

test/advection_test/cld_ice.F90

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ subroutine cld_ice_register(dyn_const_ice, errmsg, errcode)
3636
call dyn_const_ice(1)%instantiate(std_name='dyn_const1', long_name='dyn const1', &
3737
units='kg kg-1', default_value=0._kind_phys, &
3838
vertical_dim='vertical_layer_dimension', advected=.true., &
39-
min_value=1000._kind_phys, errcode=errcode, errmsg=errmsg)
39+
min_value=1000._kind_phys, water_species=.true., mixing_ratio_type='wet', &
40+
errcode=errcode, errmsg=errmsg)
4041
call dyn_const_ice(2)%instantiate(std_name='dyn_const2_wrt_moist_air', long_name='dyn const2', &
4142
units='kg kg-1', default_value=0._kind_phys, &
4243
vertical_dim='vertical_layer_dimension', advected=.true., &
43-
errcode=errcode, errmsg=errmsg)
44+
water_species=.false., errcode=errcode, errmsg=errmsg)
4445

4546
end subroutine cld_ice_register
4647

test/advection_test/cld_liq.F90

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,18 @@ subroutine cld_liq_register(dyn_const, errmsg, errflg)
2323
character(len=512), intent(out) :: errmsg
2424
integer, intent(out) :: errflg
2525

26-
character(len=256) :: stdname
27-
2826
errmsg = ''
2927
errflg = 0
3028
allocate(dyn_const(1), stat=errflg)
3129
if (errflg /= 0) then
3230
errmsg = 'Error allocating dyn_const in cld_liq_register'
3331
return
3432
end if
35-
call dyn_const(1)%instantiate(std_name="dyn_const3", long_name='dyn const3', &
33+
call dyn_const(1)%instantiate(std_name="dyn_const3_wrt_moist_air_and_condensed_water", long_name='dyn const3', &
3634
units='kg kg-1', default_value=1._kind_phys, &
3735
vertical_dim='vertical_layer_dimension', advected=.true., &
36+
water_species=.true., mixing_ratio_type='dry', &
3837
errcode=errflg, errmsg=errmsg)
39-
call dyn_const(1)%standard_name(stdname, errcode=errflg, errmsg=errmsg)
4038

4139
end subroutine cld_liq_register
4240

test/advection_test/test_host.F90

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ subroutine test_host(retval, test_suites)
456456
call test_host_const_get_index('dyn_const2_wrt_moist_air', index_dyn2, errflg, errmsg)
457457
call check_errflg(subname//".index_dyn_const2", errflg, errmsg, &
458458
errflg_final)
459-
call test_host_const_get_index('dyn_const3', index_dyn3, errflg, errmsg)
459+
call test_host_const_get_index('dyn_const3_wrt_moist_air_and_condensed_water', index_dyn3, errflg, errmsg)
460460
call check_errflg(subname//".index_dyn_const3", errflg, errmsg, &
461461
errflg_final)
462462

@@ -602,7 +602,38 @@ subroutine test_host(retval, test_suites)
602602
! Reset error flag to continue testing other properties:
603603
errflg = 0
604604
end if
605-
! Check moist mixing ratio for a dynamic constituent
605+
! Check wet mixing ratio for dynamic constituent 1
606+
call const_props(index_dyn1)%is_dry(const_log, errflg, errmsg)
607+
if (errflg /= 0) then
608+
write(6, '(a,i0,a,a,i0,/,a)') "ERROR: Error, ", errflg, " trying ", &
609+
"to get dry prop for dyn_const1 index = ", index_dyn1, trim(errmsg)
610+
errflg_final = -1 ! Notify test script that a failure occurred
611+
end if
612+
if (errflg == 0) then
613+
if (const_log) then
614+
write(6, *) "ERROR: dyn_const1 is dry and should be wet"
615+
errflg_final = -1
616+
end if
617+
else
618+
! Reset error flag to continue testing other properties:
619+
errflg = 0
620+
end if
621+
call const_props(index_dyn1)%is_wet(const_log, errflg, errmsg)
622+
if (errflg /= 0) then
623+
write(6, '(a,i0,a,a,i0,/,a)') "ERROR: Error, ", errflg, " trying ", &
624+
"to get wet prop for dyn_const1 index = ", index_dyn1, trim(errmsg)
625+
errflg_final = -1 ! Notify test script that a failure occurred
626+
end if
627+
if (errflg == 0) then
628+
if (.not. const_log) then
629+
write(6, *) "ERROR: dyn_const1 is not wet but should be"
630+
errflg_final = -1
631+
end if
632+
else
633+
! Reset error flag to continue testing other properties:
634+
errflg = 0
635+
end if
636+
! Check moist mixing ratio for dynamic constituent 2
606637
call const_props(index_dyn2)%is_dry(const_log, errflg, errmsg)
607638
if (errflg /= 0) then
608639
write(6, '(a,i0,a,a,i0,/,a)') "ERROR: Error, ", errflg, " trying ", &
@@ -633,6 +664,22 @@ subroutine test_host(retval, test_suites)
633664
! Reset error flag to continue testing other properties:
634665
errflg = 0
635666
end if
667+
! Check dry mixing ratio for dynamic constituent 3
668+
call const_props(index_dyn3)%is_dry(const_log, errflg, errmsg)
669+
if (errflg /= 0) then
670+
write(6, '(a,i0,a,a,i0,/,a)') "ERROR: Error, ", errflg, " trying ", &
671+
"to get dry prop for dyn_const3 index = ", index_dyn3, trim(errmsg)
672+
errflg_final = -1 ! Notify test script that a failure occurred
673+
end if
674+
if (errflg == 0) then
675+
if (.not. const_log) then
676+
write(6, *) "ERROR: dyn_const3 is not dry and should be"
677+
errflg_final = -1
678+
end if
679+
else
680+
! Reset error flag to continue testing other properties:
681+
errflg = 0
682+
end if
636683

637684
! -------------------
638685

@@ -866,6 +913,41 @@ subroutine test_host(retval, test_suites)
866913
! Reset error flag to continue testing other properties:
867914
errflg = 0
868915
end if
916+
917+
! Check that setting a constituent to be a water species via the
918+
! instantiate call works as expected
919+
call const_props(index_dyn1)%is_water_species(check, errflg, errmsg)
920+
if (errflg /= 0) then
921+
write(6, '(a,i0,a,i0,/,a)') "ERROR: Error, ", errflg, &
922+
"trying to get water_species prop for dyn_const1 index = ", &
923+
index_dyn1, trim(errmsg)
924+
end if
925+
if (errflg == 0) then
926+
if (.not. check) then ! Should now be True
927+
write(6,*) "ERROR: 'water_species=.true. did not set", &
928+
" water_species constituent property correctly"
929+
errflg_final = -1 ! Notify test script that a failure occurred
930+
end if
931+
else
932+
! Reset error flag to continue testing other properties:
933+
errflg = 0
934+
end if
935+
call const_props(index_dyn2)%is_water_species(check, errflg, errmsg)
936+
if (errflg /= 0) then
937+
write(6, '(a,i0,a,i0,/,a)') "ERROR: Error, ", errflg, &
938+
"trying to get water_species prop for dyn_const2 index = ", &
939+
index_dyn2, trim(errmsg)
940+
end if
941+
if (errflg == 0) then
942+
if (check) then ! Should now be False
943+
write(6,*) "ERROR: 'water_species=.false. did not set", &
944+
" water_species constituent property correctly"
945+
errflg_final = -1 ! Notify test script that a failure occurred
946+
end if
947+
else
948+
! Reset error flag to continue testing other properties:
949+
errflg = 0
950+
end if
869951
! -------------------
870952

871953
! Check that setting a constituent's default value works as expected

test/advection_test/test_host_data.F90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ subroutine allocate_physics_state(cols, levels, constituents, state)
2424
deallocate(state%ps)
2525
end if
2626
allocate(state%ps(cols))
27+
state%ps = 0.0_kind_phys
2728
if (allocated(state%temp)) then
2829
deallocate(state%temp)
2930
end if

0 commit comments

Comments
 (0)