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

Precision loss when calculating the TotalDuration, as a result, the last report result may not be added in output file #152

Closed
cbuahin opened this issue Nov 20, 2023 · 0 comments
Assignees
Labels
addressed Issues that have been addressed. Pending merge before closing. enhancement
Milestone

Comments

@cbuahin
Copy link
Collaborator

cbuahin commented Nov 20, 2023

@WaterDesk reported the following issue #151

When I am using the latest official SWMM 5.2.4 code, and I found there was a bug fix have not been merged to the root repository which was pushed to https://github.com/pyswmm/Stormwater-Management-Model

The original PR is pyswmm#219, copy the details as below:

Precision loss when calculating the TotalDuration, as a result, the last report result may not be added in output file

SWMM version: 5.1.013

Reproduce Steps:

  1. Use the sample INP file “Example1.inp”, make some changes:
    • END_TIME changes to 11:30 from 12:00
    • REPORT_STEP changes to 00:30:00 from 01:00:00
  2. Open the changed "Example1.inp" file, then Run Simulation
  3. Pick up a subcatchment or node or link, and Create a Time Series Table, then you will find the result data of the END_TIME has not been listed in the table. The current last one is Days 1 Hours 11:00:00, but not 11:30:00.

Analysis:

The Start/End Date and Time is stored as text in INP file, after parsing them from INP file, they will be converted to double values.

The EndTime “11:30:00’’ converts to 0.47916666666666669, when it is added with EndDate “01/02/1998” (35797.000000000000), the new EndDateTime is 35797.479166666664.
Use the below method to calculate the TotalDuration, the result is 127799.0.

    TotalDuration        = floor((EndDateTime - StartDateTime) * SECperDAY);

From the INP file, the StartDateTime is 01/01/1998 00:00:00, and the EndDateTime is 01/02/1998 11:30:00. The duration is 1 day and 11:30:00, it has 127800 seconds.

1 second precision losses now! So the last time result will never been reported in output file.

Solution:

The precision loss usually happens when a large double value plus a small double value. So, we need to avoid such calculations.

Change the calculation method of TotalDuration to be:

        double durationDate = EndDate - StartDate;
        double durationTime = EndTime - StartTime;
        TotalDuration = floor(durationDate * SECperDAY + durationTime * SECperDAY);
@cbuahin cbuahin self-assigned this Jan 17, 2024
@cbuahin cbuahin added this to the v5.3.0 milestone Jan 17, 2024
@cbuahin cbuahin added the addressed Issues that have been addressed. Pending merge before closing. label Jan 17, 2024
@cbuahin cbuahin closed this as completed Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed Issues that have been addressed. Pending merge before closing. enhancement
Projects
None yet
Development

No branches or pull requests

1 participant