Skip to content

Provide consistency in setting of prevRingTime for alarms when changing ref time #1290

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

Merged

Conversation

mgduda
Copy link
Contributor

@mgduda mgduda commented Feb 20, 2025

This PR aims to provide consistency in the ringing behavior of recurring alarms after their reference time has been adjusted with a call to mpas_adjust_alarm_to_reference_time. Now, adjusting the reference time for an alarm will always leave that alarm in a state such that it is considered by the mpas_is_alarm_ringing routine to be ringing at the current time.

For simplicity of discussion, consider only a clock that is running forward in time. The objective of the mpas_adjust_alarm_to_reference_time routine is to set the prevRingTime (previous ring time) of a recurring alarm to (1) a time that differs from the specified reference time by an integer multiple of the alarm interval; and (2) the latest such time that is not after the current time on the clock.

Prior to this PR, the logic in the mpas_adjust_alarm_to_reference_time routine resulted in one of two outcomes:

(1) If the difference between the reference time and the current time is divisible by the alarm interval, the prevRingTime becomes the current time, as illustrated below.

    ring            ring            ring            ring
     v               v               v               v
  -----------------------------------------------------------
                                     ^
                                    now
                                prevRingTime

(2) Otherwise, the prevRingTime becomes the latest time before the current time that lies on an integer multiple of the alarm interval away from the reference time, as illustrated below.

    ring            ring            ring            ring
     v               v               v               v
  -----------------------------------------------------------
                     ^           ^
                prevRingTime    now

To determine whether a recurring alarm is ringing, the alarm's interval is added to the alarm's prevRingTime. If the result is on or before the current time, the alarm is ringing; otherwise, if the result is later than the current time, the alarm is not ringing. As a consequence, outcome (1) from the mpas_adjust_alarm_to_reference_time leads to an alarm that is ringing after the call to the routine, while outcome(2) leads to an alarm that is not yet ringing.

In order to rectify the inconsistency in whether an alarm is ringing depending on where its reference time is set relative to the current time, the prevRingTime for an alarm is always set to be at least one full alarm interval before the current time. Whether the difference between the current time and the reference time is evenly divisible by the alarm's ring interval or not, a query of the alarm's status will always show that it is ringing.

This PR makes changes to the logic for both a forward and a backward running clock in the mpas_adjust_alarm_to_reference_time, although it appears that no code actually makes use of a backward running clock at present.

This PR also adds unit tests for the mpas_adjust_alarm_to_reference_time routine in the test core.

@mgduda mgduda force-pushed the framework/adjust_alarm_prevRingTime branch from 02292ee to 612799b Compare February 20, 2025 22:06
This commit adds unit tests for the mpas_adjust_alarm_to_reference_time routine
to the test_core_timekeeping_tests module in the test core.

Implied in these tests is the requirement that, after a call to
mpas_adjust_alarm_to_reference_time, the prevRingTime of the alarm is set to a
value that ensures that an immediately subsequent call to mpas_is_alarm_ringing
will show that the alarm is ringing.

In order to get more verbose printout, including the internal state of alarms,
the MPAS_ADJUST_ALARM_VERBOSE macro can be modfied near the top of the
test_core_timekeeping_tests module so that its argument is not turned into a
comment.
@mgduda mgduda force-pushed the framework/adjust_alarm_prevRingTime branch from 612799b to f6039c4 Compare March 5, 2025 15:53
@mgduda mgduda requested review from gdicker1 and abishekg7 May 19, 2025 22:56
@mgduda mgduda marked this pull request as ready for review May 19, 2025 22:56
@mgduda
Copy link
Contributor Author

mgduda commented May 19, 2025

@abishekg7 @gdicker1 For more background, the fix in this PR to the mpas_adjust_alarm_to_reference_time routine are needed so that we can properly restart limited-area simulations between LBC update times (for example, restart a simulation at 01:30 UTC when LBCs are updated every three hours at 00:00, 03:00, 06:00, etc.)

@mgduda
Copy link
Contributor Author

mgduda commented May 19, 2025

When reviewing the changes to the mpas_adjust_alarm_to_reference_time routine, the diff may be a bit confusing, so it may be easier to just look at the new state of the routine (and then to take out a pencil and paper and make some drawings).

@gdicker1
Copy link
Collaborator

gdicker1 commented May 20, 2025

After noodling this around a bit, the changes make sense.

I think the message for the commit modifying mpas_adjust_alarm_to_reference_time could use some work. I think it would at least be nice if the newly consistent behavior was added to the first paragraph, even if the terms will be defined or expanded on more later. Though, I'm having trouble phrasing it (maybe not understanding as well as I'd like). Could be something like:

... for a recurring alarm. After mpas_adjust_alarm_to_reference_time, the prevRingTime for the alarm is now consistently set so that it is one ringing interval away from the current time and prevRingTime is now set so that the alarm is ringing.

@abishekg7
Copy link
Collaborator

The changes to mpas_adjust_alarm_to_reference_time and the unit test look good, just had a minor comment.

@mgduda
Copy link
Contributor Author

mgduda commented May 20, 2025

After noodling this around a bit, the changes make sense.

I think the message for the commit modifying mpas_adjust_alarm_to_reference_time could use some work. I think it would at least be nice if the newly consistent behavior was added to the first paragraph, even if the terms will be defined or expanded on more later. Though, I'm having trouble phrasing it (maybe not understanding as well as I'd like). Could be something like:

... for a recurring alarm. After mpas_adjust_alarm_to_reference_time, the prevRingTime for the alarm is now consistently set so that it is one ringing interval away from the current time and prevRingTime is now set so that the alarm is ringing.

How about the following for the first part of the commit message?

This commit aims to provide consistency in the ringing behavior of recurring 
alarms after their reference time has been adjusted with a call to
`mpas_adjust_alarm_to_reference_time`. Now, adjusting the reference time for an
alarm will always leave that alarm in a state such that it is considered by the
`mpas_is_alarm_ringing` routine to be ringing at the current time.

@gdicker1
Copy link
Collaborator

@mgduda, that looks great!

Copy link
Collaborator

@gdicker1 gdicker1 left a comment

Choose a reason for hiding this comment

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

Trusting that the commit message will be updated, this PR has my approval.

…ng ref time

This commit aims to provide consistency in the ringing behavior of recurring
alarms after their reference time has been adjusted with a call to
mpas_adjust_alarm_to_reference_time. Now, adjusting the reference time for an
alarm will always leave that alarm in a state such that it is considered by the
mpas_is_alarm_ringing routine to be ringing at the current time.

For simplicity of discussion, consider only a clock that is running forward in
time. The objective of the mpas_adjust_alarm_to_reference_time routine is to set
the prevRingTime (previous ring time) of a recurring alarm to (1) a time that
differs from the specified reference time by an integer multiple of the alarm
interval; and (2) the latest such time that is not after the current time on the
clock.

Prior to this commit, the logic in the mpas_adjust_alarm_to_reference_time
routine resulted in one of two outcomes:

(1) If the difference between the reference time and the current time is
divisible by the alarm interval, the prevRingTime becomes the current time, as
illustrated below.

    ring            ring            ring            ring
     v               v               v               v
  -----------------------------------------------------------
                                     ^
                                    now
                                prevRingTime

(2) Otherwise, the prevRingTime becomes the latest time before the current time
that lies on an integer multiple of the alarm interval away from the reference
time, as illustrated below.

    ring            ring            ring            ring
     v               v               v               v
  -----------------------------------------------------------
                     ^           ^
                prevRingTime    now

To determine whether a recurring alarm is ringing, the alarm's interval is added
to the alarm's prevRingTime. If the result is on or before the current time, the
alarm is ringing; otherwise, if the result is later than the current time, the
alarm is not ringing. As a consequence, outcome (1) from the
mpas_adjust_alarm_to_reference_time leads to an alarm that is ringing after the
call to the routine, while outcome(2) leads to an alarm that is not yet ringing.

In order to rectify the inconsistency in whether an alarm is ringing depending
on where its reference time is set relative to the current time, the
prevRingTime for an alarm is always set to be at least one full alarm interval
before the current time. Whether the difference between the current time and the
reference time is evenly divisible by the alarm's ring interval or not, a query
of the alarm's status immediately following the call to
mpas_adjust_alarm_to_reference_time will always show that it is ringing.

This commit makes changes to the logic for both a forward and a backward running
clock in the mpas_adjust_alarm_to_reference_time routine, although it appears
that no code actually makes use of a backward running clock at present.

Note: At present, the logic is not quite as elegant as one might expect it
should be. In future, if the interval_division routine handled negative
intervals like the analog of

  (-2.75 % 1.0) => 0.25

then there may be no need for if-tests within blocks for each clock direction.
@mgduda mgduda force-pushed the framework/adjust_alarm_prevRingTime branch from f6039c4 to 66d310a Compare May 20, 2025 21:00
@mgduda
Copy link
Contributor Author

mgduda commented May 20, 2025

Trusting that the commit message will be updated, this PR has my approval.

I've updated the commit message as well as the PR description.

@mgduda mgduda merged commit 1858b7d into MPAS-Dev:hotfix-v8.2.3 May 20, 2025
mgduda added a commit that referenced this pull request May 23, 2025
This merge addresses several issues in the MPAS-Atmosphere model and in the MPAS
infrastructure. Specific changes include:

 * Correction of the pool from which lbc_scalar constituent indices are obtained
   in the init_atm_thompson_aerosols_lbc routine. Rather than obtaining
   index_nifa and index_nwfa from the state pool, the indices of lbc_nifa and
   lbc_nwfa should be obtained from the lbc_state pool. (PR #1249)

 * Correction to the computation of the soil temperature (TSLB) in the Noah-MP
   land surface scheme through the addition of initialization of the soil liquid
   water (SH2O) in the noahmp_init subroutine in module
   mpas_atmphys_lsm_noahmpinit.F prior to calling NoahmpInitMain. (PR #1244)

 * Correction of the units of the fields 'greenfrac', 'shdmin', 'shdmax',
   'vegfra', and 'albedo12m' from "unitless" to "percent" in the
   init_atmosphere and atmosphere core Registry.xml files. Also, a correction to
   the spelling of 'greenness' in several places. (PR #1248)

 * Removal of a duplicate allocation of indexToEdgeID % array in the
   mpas_io_setup_edge_block_fields routine that was the source of a memory leak.
   (PR #1258)

 * Fix for a memory leak in mpas_block_creator_build_cell_halos by deallocating
   the cellLimitField field before the routine returns. (PR #1264)

 * Fix for a bug in the logic for determining when decompositions can be reused
   by the SMIOL library. In almost any practical situation, however, this bug
   created no issues. (PR #1288)

 * Changes in the init_atmosphere core to provide more reliable error messages
   in case config_nfglevels is not set to a value that is at least as large as
   the number of vertical levels in the first-guess intermediate file. (PR #1291)

 * Correction of the loop for Noah-MP snow initialization, capping snow water
   equivalent maximum at 2000 mm. (PR #1300)

 * Fix for a bug in the horizontal 2nd-order filter for the CAM upper absorbing
   layer, where the wrong level in the kdiff field was being used when enforcing
   a lower-bound on kdiff. This absorbing layer is active only when
   config_mpas_cam_coef > 0.0. (PR #1302)

 * Fix in the mountain wave idealized test case initialization when multiple MPI
   tasks are used. The 'xc' variable, which represents the center-point location
   of the mountain, was previously computed based on the maximum xCell values
   local to an MPI task, leading to inconsistent values on each MPI rank. By
   finding the maximum of xCell over all MPI ranks and ensuring that all MPI
   ranks use this global maximum, the terrain field is computed consistently
   between serial and parallel runs of the init_atmosphere_model program for the
   mountain wave test case (config_init_case = 6). (PR #1312)

 * Correction to the calculation of the 2-meter diagnostics (T2M, TH2M, and Q2)
   when using the Noah-MP land surface scheme. While the computation of 2-meter
   diagnostics is the same for Noah and Noah-MP over oceans, it is different
   between the two land surface schemes over land. In Noah-MP, the 2-meter
   diagnostics are weighted as functions of their respective diagnostics over
   bare soil and over vegetation. The updated diagnostics for Noah and Noah-MP
   are now computed in the new file mpas_atmphys_sfc_diagnostics.F. (PR #1242)

 * Fix to provide consistency in the ringing behavior of recurring alarms after
   their reference time has been adjusted with a call to
   mpas_adjust_alarm_to_reference_time. Now, adjusting the reference time for an
   alarm will always leave that alarm in a state such that it is considered by
   the mpas_is_alarm_ringing routine to be ringing at the current time. With
   this fix, limited-area simulations can be restarted at times between LBC
   updates, provided the reference_time attribute for the 'lbc_in' stream is set
   to the simulation initial time in the streams.atmosphere file. (PR #1290).

 * Correction of an indexing error for rvcuten in code blocks specific to the
   Grell-Freitas scheme in the convection driver. Specifically, in the
   convection_from_MPAS and convection_to_MPAS routines, rvcuten used (k,k) as
   indexing in a loop, where (k,i) is needed. Since the Grell-Freitas scheme
   does not provide momentum tendencies, the changes in this merge have no
   impact on results. (PR #1283)
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.

3 participants