Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add single precision support for RRTMGP #387

Merged
merged 1 commit into from
Aug 10, 2023
Merged

Conversation

sriharshakandala
Copy link
Member

@sriharshakandala sriharshakandala commented Aug 7, 2023

Add single precision support for RRTMGP

Trying: earth-system-radiation/rte-rrtmgp#39 per issue #337


  • I have read and checked the items on the review checklist.

@sriharshakandala sriharshakandala linked an issue Aug 7, 2023 that may be closed by this pull request
@charleskawczynski
Copy link
Member

@sriharshakandala
Copy link
Member Author

Details

We may want to try to do https://github.com/CliMA/RRTMGP.jl/blob/9997abad1060ef899e3d8755b5dad9fcaa01c36c/src/optics/GasOptics.jl#L38C33-L38C41 at higher precision.

This does not seem to be the problem!

@sriharshakandala
Copy link
Member Author

sriharshakandala commented Aug 10, 2023

With the above changes, there is a significant reduction in single precision flux errors to less than 1 to 2 W/m^2 compared to double precision results, depending on the test case. However, we are not yet seeing the differences of about 0.1 W/m^2 we are expecting.

I need to dig deeper, check if there are any differences in tests or lookup data, compared to the FORTRAN code, since we haven't tracked these in a while!

Since these changes do not effect the double precision cases, I was thinking of merging this PR with the current changes and pursue remaining changes a different PR. @simonbyrne @charleskawczynski @szy21

@sriharshakandala
Copy link
Member Author

bors try

bors bot added a commit that referenced this pull request Aug 10, 2023
@bors
Copy link
Contributor

bors bot commented Aug 10, 2023

try

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@szy21
Copy link
Member

szy21 commented Aug 10, 2023

I'm ok with merging this. @charleskawczynski What do you think?

@charleskawczynski
Copy link
Member

I’m definitely onboard with incremental changes, is there anything we can/should to to the test suite? (e.g., can any tests be changed from broken to unbroken?)

@sriharshakandala
Copy link
Member Author

I’m definitely onboard with incremental changes, is there anything we can/should to to the test suite? (e.g., can any tests be changed from broken to unbroken?)

Not yet. This change is just to source code. We need to relax the failure tolerance to about 0.35 W/m^2 for single precision tests, per https://github.com/earth-system-radiation/rte-rrtmgp/pull/222/files#diff-c471496bcb3ed47397b73d2e3e3aa41a8a679c86accaed9e05676fd9d5987434R39 ,
but that can be done later

@sriharshakandala sriharshakandala marked this pull request as ready for review August 10, 2023 17:04
This involed the following steps:
Set k_min for shortwave solver based on floating poing precision
Constrain Rdir and Tdir in 2-stream shortwave solver
Update tau_threshold and use 3rd order expansion in rte_lw_noscat_source!
Update k_min in rte_lw_2stream_source!
Update tau_threshold to be precision dependent in rte_lw_2stream_source!
@sriharshakandala
Copy link
Member Author

bors r+

bors bot added a commit that referenced this pull request Aug 10, 2023
387: Add single precision support for RRTMGP r=sriharshakandala a=sriharshakandala

Add single precision support for RRTMGP

Trying: earth-system-radiation/rte-rrtmgp#39 per issue #337

<!---
Review checklist

I have:
- followed the codebase contribution guide: https://clima.github.io/ClimateMachine.jl/latest/Contributing/
- followed the style guide: https://clima.github.io/ClimateMachine.jl/latest/DevDocs/CodeStyle/
- followed the documentation policy: https://github.com/CliMA/policies/wiki/Documentation-Policy
- checked that this PR does not duplicate an open PR.

In the Content, I have included 
- relevant unit tests, and integration tests, 
- appropriate docstrings on all functions, structs, and modules, and included relevant documentation.

-->

----
- [ ] I have read and checked the items on the review checklist.


Co-authored-by: sriharshakandala <sriharsha.kvs@gmail.com>
@bors
Copy link
Contributor

bors bot commented Aug 10, 2023

Build failed:

@sriharshakandala
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented Aug 10, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit b3057d5 into main Aug 10, 2023
7 checks passed
@bors bors bot deleted the sk/support_single_precision branch August 10, 2023 19:18
@@ -161,40 +163,44 @@ function rte_lw_2stream_source!(
# setting references
(; τ, ssa, g) = op
(; Rdif, Tdif, lev_source, src_up, src_dn) = src_lw

#k_min = FT === Float64 ? FT(1e-12) : FT(1e-4)
k_min = FT(1e4 * eps(FT))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 1e4 factor seems arbitrary. Is there a principled way to make this a function of eps? Otherwise, maybe a fixed minimum (independent of floating point precision may be better).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lw_diff_sec = FT(1.66)
τ_thresh = sqrt(eps(FT))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would seem good to use a consistent threshold (eps^(1/4) above, which made sense there given the 3rd-order expansion).

@inbounds src_up[glay, gcol] = pi * (Zup_top - Rdif[glay, gcol] * Zdn_top - Tdif[glay, gcol] * Zup_bottom)
@inbounds src_dn[glay, gcol] =
pi * (Zdn_bottom - Rdif[glay, gcol] * Zup_bottom - Tdif[glay, gcol] * Zdn_top)
Z = (lev_src_bot - lev_src_top) / (τ_lay * (γ1 + γ2))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we replace this by a 3rd-order Taylor expansion around tau_lay = 0 when tau_lay < tau_lay, as above, and then use tau_thres = eps^(1/4) as above, perhaps we get more accurate results?

Zdn_top = -Z + lev_src_top
Zdn_bottom = -Z + lev_src_bot
@inbounds src_up[glay, gcol] = pi * (Zup_top - Rdif_lay * Zdn_top - Tdif_lay * Zup_bottom)
@inbounds src_dn[glay, gcol] = pi * (Zdn_bottom - Rdif_lay * Zup_bottom - Tdif_lay * Zdn_top)
else
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, perhaps better than using a hard 0 here is to use Taylor expansion for small tau as above.

@sriharshakandala
Copy link
Member Author

sriharshakandala commented Aug 11, 2023

I started a draft PR here (#388) to try out these changes! cc: @szy21 @tapios


coeff = exp(-2 * τ[glay, gcol] * k)
coeff = exp(-2 * τ_lay * k)
# Refactored to avoid rounding errors when k, gamma1 are of very different magnitudes
RT_term = 1 / (k * (1 + coeff) + γ1 * (1 - coeff))
Copy link
Collaborator

@tapios tapios Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, we can use Taylor expansion for 1/k, to 3rd order, and then use k_min = eps^(1/4).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add single precision support for RRTMGP
4 participants