Skip to content

Conversation

@spahrenk
Copy link
Contributor

@spahrenk spahrenk commented Mar 19, 2024

Description

  • Prevents simulation hang-up that resulted from nan in cvpak function cvDouble2s().
  • Allows simulation to proceed in case of secant failure in RSYS::rs_AllocateZoneAir().
  • Expands range of solution by defining a reduced tolerance used in ZNR::zn_AmfHvacCR and ZNR::zn_AmfHvacCR.
  • Repeats, while reporting, iteration steps in secant in case of failure.
  • Adds unit test for nummeth, specifically involving secant.

Note that failure in RSYS::rs_AllocateZoneAir() can still occur, when the solution for available supply air temperature approaches within the defined tolerance of the zone air temperatures for any of the served zones. But in the case of such failure, the previous supply air-temperature value is used, allowing the simulation to proceed.

@spahrenk spahrenk added the bug label Mar 19, 2024
@spahrenk spahrenk self-assigned this Mar 19, 2024
@chipbarnaby
Copy link
Contributor

Shouldn't the nan case be intercepted much earlier? Probably in cvin2s()? If the data is double or float and isnan(), emit "nan". There is probably a need to worry about field width.

There already is a cvin2s unit test. First step might be to add one or more "nan" cases so we can easily verify what happens.

@spahrenk spahrenk requested a review from chipbarnaby April 8, 2024 21:04
= 459.67;
#endif
constexpr double tol_tF = 1.e-12; // temperature tolerance (degF)

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure this is worth a global constant. In my experience, tolerances often need case-specific tuning. The fact that 2 situations both used the same tolerance in this case was probably more coincidence than anything. Also, the new value is much smaller. What did this do to runtime? For this kind of simulation, high precision is maybe not needed. Generally it is not worth sacrificing speed to refine a result 0.001 F.

Copy link
Contributor Author

@spahrenk spahrenk Apr 9, 2024

Choose a reason for hiding this comment

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

The tolerance used in these two cases serves to prevent division by zero; it does not correspond to a precision on the fitted value. (The fitting tolerance and number of iterations within the secant method have not been altered.) Even with the tighter tolerance, the secant method will likely fail if the if the condition in these functions is met. (It seems obvious that the the two functions are related, but if there is some justification for using a different value in one or the other, please specify). Given that there are at least two instances of solver functions that return AMFs and contain terms with degF temperature differences in their divisors, the definition of a standard tolerance within some broader scope seems merited. Definition of tol_tF moved to cnloads.

src/cvpak.cpp Outdated
&& cvdd( mfw-1, dfw) ) // format it and see
if ((fabs(val) < maxfit // if now might fit width
&& cvdd( mfw-1, dfw)) || // format it and see
std::isnan(val)) // guard against inf loop
Copy link
Contributor

Choose a reason for hiding this comment

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

I stand by my prior comment that the nan case should be intercepted earlier. Suitable output should be generated, e.g. "nan". Maybe issue warning? (Or maybe not ... printf() does not. What does fmt do?) Also, as suggested add one or more nan cases to the cvpak test to exercise the new capability.

src/nummeth.cpp Outdated
} // ::secant

//-----------------------------------------------------------------------------
int secant( // screen secant success; report calcuation if failure
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the strategy here. There should be high-level explanatory comments. "Try classic secant method. If it fails converge, do x, y, z in an effort to allow execution to continue."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was told to add code to repeat secant upon failure with text output. I have expanded some comments to clarify.

Copy link
Contributor

@chipbarnaby chipbarnaby left a comment

Choose a reason for hiding this comment

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

See individual comments,

@spahrenk spahrenk requested a review from nealkruis April 11, 2024 20:18
@spahrenk spahrenk marked this pull request as ready for review April 11, 2024 20:18
@nealkruis nealkruis requested a review from chipbarnaby May 28, 2024 21:40
@chipbarnaby chipbarnaby merged commit ff384a5 into main Jun 4, 2024
@nealkruis nealkruis deleted the avoid-inf-loop branch September 3, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants