Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion biogeophys/LeafBiophysicsMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ end subroutine CiFunc
subroutine CiBisection(ft,vcmax,jmax,kp,co2_cpoint,mm_kco2,mm_ko2, &
can_co2_ppress,can_o2_ppress,can_press,can_vpress,lmr,par_abs,gb,veg_tempk,veg_esat, &
gs0,gs1,gs2,ci_tol, &
anet,agross,gs,ci,solve_iter)
anet,agross,gs,ci,solve_iter,ierr)

! -----------------------------------------------------------------------------------
!
Expand Down Expand Up @@ -1115,6 +1115,10 @@ subroutine CiBisection(ft,vcmax,jmax,kp,co2_cpoint,mm_kco2,mm_ko2, &
real(r8), intent(out) :: gs ! stomatal conductance (umol h2o/m2/s)
real(r8), intent(out) :: ci ! Input (trial) intracellular leaf CO2 (Pa)
integer, intent(inout) :: solve_iter ! number of bisections required
! Optional error flag: if present, the max-iterations failure sets ierr=1
! and returns instead of calling endrun(). Existing callers that omit ierr
! retain the original abort behaviour. Intended for unit tests only.
integer, optional, intent(out) :: ierr

! With bisection, we need to keep track of three different ci values at any given time
! The high, the low and the bisection.
Expand All @@ -1128,6 +1132,8 @@ subroutine CiBisection(ft,vcmax,jmax,kp,co2_cpoint,mm_kco2,mm_ko2, &
! Maximum number of iterations on intracelluar co2 solver until is quits
integer, parameter :: max_iters = 200

if (present(ierr)) ierr = 0 ! initialise to success

! Find the starting points (end-points) for bisection
! We dont need stomatal slope because we just want the two extremes
! which is the intercept and infinite conductance
Expand Down Expand Up @@ -1201,6 +1207,12 @@ subroutine CiBisection(ft,vcmax,jmax,kp,co2_cpoint,mm_kco2,mm_ko2, &
end if

if( solve_iter == max_iters) then
if (present(ierr)) then
! Graceful return for unit tests: signal failure via ierr
! rather than crashing. See optional ierr declaration above.
ierr = 1
return
end if
write (fates_log(),*) 'Ci bisection during photosynthesis failed'
write (fates_log(),*) 'try increasing tolerance or widening the starting points'
call endrun(msg=errMsg(sourcefile, __LINE__))
Expand Down
2 changes: 2 additions & 0 deletions testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ add_subdirectory(tests/unit/count_cohorts_test fates_count_cohorts_utest)
add_subdirectory(tests/unit/fire_equations_test fates_fire_equations_utest)
add_subdirectory(tests/unit/quadratic_roots_test fates_quadratic_roots_utest)
add_subdirectory(tests/unit/great_circle_test fates_great_circle_utest)
add_subdirectory(tests/unit/leaf_biophysics_test fates_leaf_biophysics_utest)
add_subdirectory(tests/unit/leaf_biophysics_solver_test fates_leaf_biophysics_solver_utest)
5 changes: 5 additions & 0 deletions testing/config/unit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ test_dir = fates_quadratic_roots_utest
[great_circle]
test_dir = fates_great_circle_utest

[leaf_biophysics]
test_dir = fates_leaf_biophysics_utest

[leaf_biophysics_solver]
test_dir = fates_leaf_biophysics_solver_utest
5 changes: 5 additions & 0 deletions testing/tests/unit/leaf_biophysics_solver_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(pfunit_sources test_LeafBiophysicsSolvers.pf)

add_pfunit_ctest(LeafBiophysicsSolvers
TEST_SOURCES "${pfunit_sources}"
LINK_LIBRARIES fates csm_share)
Loading