Skip to content

Commit 73f9ab6

Browse files
committed
Use more robust logic in UpdateSun.c
util/UpdateSun.c - Update the computation to avoid integer division, which can lead to non-deterministic results (AI generated fix) - Ensure that Tlocal is in the range 0..24 CHANGELOG.md - Updated accordingly Signed-off-by: Bob Yantosca <[email protected]>
1 parent 468b3c3 commit 73f9ab6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Fixed a bug that prevented `.ci-pipelines/ci-cleanup-script.sh` from removing KPP-generated files for MCM mechanisms
2828
- Fixed typo in error message in `int/rosenbrock_autoreduce.f90`
2929
- Fixed several issues in `int/runge_kutta.c` that were causing numerical instability or non-deterministic results
30+
- Fixed computation of sun angle in `util/UpdateSun.c` to ensure deterministic behavior
3031

3132
### Removed
3233
- Removed C-I tests on Microsoft Azure Dev Pipelines

util/UpdateSun.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@ void Update_SUN()
22
{
33
KPP_REAL SunRise, SunSet;
44
KPP_REAL Thour, Tlocal, Ttmp;
5+
KPP_REAL days;
56
const KPP_REAL PI = 3.14159265358979;
67

78
SunRise = 4.5;
89
SunSet = 19.5;
910
Thour = TIME/3600.0;
10-
Tlocal = Thour - ((int)Thour/24)*24;
11+
// Calculate integer number of days, handling both positive and negative correctly
12+
// Use explicit floor operation: for positive values, truncation works;
13+
// for negative, we need floor behavior
14+
// Fix generated by AI and implemented by @yantosca, 31 Oct 2025
15+
if (Thour >= 0.0) {
16+
days = (KPP_REAL)((long)(Thour / 24.0));
17+
} else {
18+
/* For negative, subtract 1 to get floor behavior */
19+
days = (KPP_REAL)((long)(Thour / 24.0));
20+
if ((Thour - days*24.0) < 0.0) {
21+
days -= 1.0;
22+
}
23+
}
24+
Tlocal = Thour - days*24.0;
25+
// Ensure Tlocal is in [0, 24) range for determinism
26+
// Fix generated by AI and implemented by @yantosca, 31 Oct 2025
27+
if (Tlocal >= 24.0) Tlocal -= 24.0;
28+
if (Tlocal < 0.0) Tlocal += 24.0;
1129

1230
if ( (Tlocal >= SunRise) && (Tlocal <= SunSet) ) {
1331
Ttmp = (2.0*Tlocal-SunRise-SunSet)/(SunSet-SunRise);

0 commit comments

Comments
 (0)