Skip to content

Commit

Permalink
Merge branch 'agsys' of github.com:pengbinpeluo/ctsm into agsys
Browse files Browse the repository at this point in the history
  • Loading branch information
pengbinpeluo committed Nov 14, 2019
2 parents c127f40 + cff5a1e commit e5e938d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/agsys/ctsm_interface/AgSysInterface.F90
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,15 @@ end subroutine AgSysDriver
! ========================================================================

!-----------------------------------------------------------------------
subroutine Init(this, bounds)
subroutine Init(this, bounds, patch)
!
! !DESCRIPTION:
! Initialize variables needed by the AgSys model
!
! !ARGUMENTS:
class(agsys_interface_type), intent(inout) :: this
type(bounds_type), intent(in) :: bounds
type(patch_type), intent(in) :: patch
!
! !LOCAL VARIABLES:

Expand All @@ -165,7 +166,7 @@ subroutine Init(this, bounds)
call ReadParams(this%crop_cultivar_params)
call ReadPhases(this%crop_phases)
call InitRuntimeConstants(this%crop_phases)
call this%agsys_inst%Init(bounds)
call this%agsys_inst%Init(bounds, patch)

end subroutine Init

Expand Down
71 changes: 70 additions & 1 deletion src/agsys/ctsm_interface/AgSysType.F90
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ module AgSysType
use decompMod , only : bounds_type
use clm_varpar , only : nlevsoi
use clm_varcon , only : spval
use pftconMod , only : ntmp_corn, nirrig_tmp_corn, ntrp_corn, nirrig_trp_corn
use histFileMod , only : hist_addfld1d
use PatchType , only : patch_type
use AgSysRuntimeConstants, only : agsys_max_phases
use AgSysConstants, only : crop_type_not_handled, crop_type_maize
!
implicit none
private
Expand Down Expand Up @@ -72,6 +75,7 @@ module AgSysType
procedure, public :: Init
procedure, private :: InitAllocate
procedure, private :: InitHistory
procedure, private :: InitCold
end type agsys_type

character(len=*), parameter, private :: sourcefile = &
Expand All @@ -80,14 +84,15 @@ module AgSysType
contains

!-----------------------------------------------------------------------
subroutine Init(this, bounds)
subroutine Init(this, bounds, patch)
!
! !DESCRIPTION:
! Initialize this agsys_type instance
!
! !ARGUMENTS:
class(agsys_type), intent(inout) :: this
type(bounds_type), intent(in) :: bounds
type(patch_type), intent(in) :: patch
!
! !LOCAL VARIABLES:

Expand All @@ -96,6 +101,7 @@ subroutine Init(this, bounds)

call this%InitAllocate(bounds)
call this%InitHistory(bounds)
call this%InitCold(bounds, patch)
end subroutine Init

!-----------------------------------------------------------------------
Expand Down Expand Up @@ -174,5 +180,68 @@ subroutine InitHistory(this, bounds)

end subroutine InitHistory

!-----------------------------------------------------------------------
subroutine InitCold(this, bounds, patch)
!
! !DESCRIPTION:
! Do cold start initialization for this agsys instance
!
! !ARGUMENTS:
class(agsys_type), intent(inout) :: this
type(bounds_type), intent(in) :: bounds
type(patch_type), intent(in) :: patch
!
! !LOCAL VARIABLES:
integer :: p

character(len=*), parameter :: subname = 'InitCold'
!-----------------------------------------------------------------------

associate( &
begp => bounds%begp, &
endp => bounds%endp, &
begc => bounds%begc, &
endc => bounds%endc &
)

do p = begp, endp
associate( &
ptype => patch%itype(p) &
)

if ( ptype == ntmp_corn .or. ptype == nirrig_tmp_corn .or. &
ptype == ntrp_corn .or. ptype == nirrig_trp_corn) then
this%crop_type_patch(p) = crop_type_maize
! TODO(wjs, 2019-11-12) Handle more crop types here
else
this%crop_type_patch(p) = crop_type_not_handled
end if

! TODO(wjs, 2019-11-12) Fix this
this%cultivar_patch(p) = 1

end associate
end do

this%current_stage_patch(begp:endp) = 0._r8
this%emerged_patch(begp:endp) = .false.

this%days_in_phase_patch(:, begp:endp) = 0._r8
this%days_after_phase_patch(:, begp:endp) = 0._r8

this%acc_emerged_thermal_time_patch(begp:endp) = 0._r8
this%acc_thermal_time_in_phase_patch(:, begp:endp) = 0._r8
this%acc_thermal_time_after_phase_patch(:, begp:endp) = 0._r8
this%acc_vernalization_days_patch(:) = 0._r8

! TODO(wjs, 2019-11-12) We may be able to remove this initialization once we properly
! initialize the accumulator field related to this variable
this%h2osoi_liq_24hr_col(begc:endc, :) = 0._r8

this%days_after_sowing_patch(begp:endp) = 0

end associate

end subroutine InitCold

end module AgSysType
4 changes: 4 additions & 0 deletions src/agsys/science/AgSysConstants.F90
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module AgSysConstants
! !PUBLIC DATA:

! Crop types
!
! NOTE(wjs, 2019-11-12) When changing these constants or adding new crop types, also
! change the corresponding unit test in
! src/agsys/test/AgSys_Enumerations_test/test_enumerations.pf.
integer, parameter, public :: crop_type_minval = 0
integer, parameter, public :: crop_type_not_handled = 0 ! For crop types not handled by AgSys
integer, parameter, public :: crop_type_maize = 1
Expand Down
10 changes: 10 additions & 0 deletions src/agsys/science/AgSysPhases.F90
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ module AgSysPhases

! !PUBLIC DATA:

! Phase types
!
! NOTE(wjs, 2019-11-12) When changing these constants or adding new phase types, also
! change the corresponding unit test in
! src/agsys/test/AgSys_Enumerations_test/test_enumerations.pf.
integer, parameter, public :: phase_type_minval = -1
integer, parameter, public :: phase_type_unused = -1
integer, parameter, public :: phase_type_generic = 0
Expand All @@ -25,6 +30,11 @@ module AgSysPhases
integer, parameter, public :: phase_type_end = 7
integer, parameter, public :: phase_type_maxval = 7

! Composite phase types
!
! NOTE(wjs, 2019-11-12) When changing these constants or adding new composite phase
! types, also change the corresponding unit test in
! src/agsys/test/AgSys_Enumerations_test/test_enumerations.pf.
integer, parameter, public :: composite_phase_type_vernalization = 1
integer, parameter, public :: composite_phase_type_emerge_to_end_of_juvenile = 2
integer, parameter, public :: composite_phase_type_maxval = 2
Expand Down
24 changes: 22 additions & 2 deletions src/agsys/test/AgSys_Enumerations_test/test_enumerations.pf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ module test_enumerations
! There are various enumerations defined in the AgSys code. The tests in this module
! ensure that the enumerations don't have missing or duplicate values.

! NOTE(wjs, 2019-11-12) These tests won't catch the case where a new constant 'bar' is
! added that has the same integer value as an existing constant 'foo', and you also
! forget to add 'bar' to the unit test. The hope is that you will update the unit test
! whenever the enumeration changes in the code, and so you have a double-checking
! mechanism; it's less likely that you'd make a mistake in both places.

! TODO(wjs, 2019-11-12) This idea of testing enumerations is probably useful in other
! parts of CTSM, too. I think it would be helpful to have some general-purpose
! enumeration testing facility - maybe a .pf file in unit_test_shr. (I think individual
! unit test builds would need to include this additional .pf file in their build - as
! opposed to this being built into the clm library - because it needs to be handled by
! the pfunit preprocessor.) This would be a custom assertion routine that accepts
! minval, maxval, and a list of elements in the enumeration; also a message that would
! be printed if the assertion fails. (Then make sure: (1) if I change the unit tests
! here to use that, they should still pass; (2) if minval is incorrect, they should
! fail; (3) if maxval is incorrect, they should fail; (4) with a missing element, they
! should fail; (5) with two elements having the same value, they should fail.) (Also
! add the above NOTE comment in that new file.)

use pfunit_mod
use AgSysConstants
use AgSysPhases
Expand Down Expand Up @@ -41,12 +60,13 @@ contains
subroutine test_crop_types(this)
! Make sure that each crop type integer value appears once and only once
class(TestEnumerations), intent(inout) :: this
integer :: counts(crop_type_maxval)
integer :: expected_counts(crop_type_maxval)
integer :: counts(crop_type_minval:crop_type_maxval)
integer :: expected_counts(crop_type_minval:crop_type_maxval)

counts(:) = 0
expected_counts(:) = 1

call add_one(counts(crop_type_not_handled))
call add_one(counts(crop_type_maize))
call add_one(counts(crop_type_soybean))
call add_one(counts(crop_type_wheat))
Expand Down

0 comments on commit e5e938d

Please sign in to comment.