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

Feautre/adapt opt #565

Draft
wants to merge 51 commits into
base: feature/rk_defaults
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
14ecd14
Remove special case of negative base in SUNRpowerR
Steven-Roberts Jul 30, 2024
dcb14bc
Fix assert condition
Steven-Roberts Jul 30, 2024
19f2dc9
Remove magic constant
Steven-Roberts Jul 30, 2024
53f3dde
Remove TINY parameter
Steven-Roberts Jul 30, 2024
e838c4e
Remove TINY from imexgus controller
Steven-Roberts Jul 30, 2024
9cde50a
Switch to assert
Steven-Roberts Jul 30, 2024
2c2444c
Fix sign of controller in nan case
Steven-Roberts Jul 30, 2024
9cb7bba
Start adaptivity unit test
Steven-Roberts Jul 31, 2024
743913a
Add const and comments to test
Steven-Roberts Aug 1, 2024
b75e78e
Simplify Soderlind controller and handle inf/nan better
Steven-Roberts Aug 1, 2024
2caec18
Clean up controllers
Steven-Roberts Aug 1, 2024
3fbda5b
Apply formatter
Steven-Roberts Aug 1, 2024
ef01f92
Merge branch 'develop' into feature/min-err
Steven-Roberts Aug 23, 2024
299cd5c
Variable initialization cleanup
Steven-Roberts Sep 6, 2024
405ff41
Use I controller on first 2 steps of Soderlind
Steven-Roberts Sep 6, 2024
bc9f312
Remove assert for pow testing
Steven-Roberts Sep 6, 2024
54aa1d5
Update docs
Steven-Roberts Sep 6, 2024
bfdf75e
Update IMEX controller docs
Steven-Roberts Sep 6, 2024
dde1379
Update changelog
Steven-Roberts Sep 6, 2024
facfc39
Clean up time step sign
Steven-Roberts Sep 6, 2024
7badd78
Additional simplifications
Steven-Roberts Sep 6, 2024
641ad92
Merge branch 'develop' into feature/min-err
Steven-Roberts Sep 6, 2024
c472ffe
Additional testing of controller
Steven-Roberts Sep 6, 2024
2452bd6
Remove unnecessary header
Steven-Roberts Sep 6, 2024
13cce12
Correct function names
Steven-Roberts Sep 6, 2024
1950356
Convert SUNRpowerR to macro
Steven-Roberts Sep 6, 2024
01b50b6
Switch to proposed adaptivity parameters
Steven-Roberts Sep 8, 2024
b733fa3
Merge branch 'feature/min-err' into feautre/adapt-opt
Steven-Roberts Sep 8, 2024
5bd4790
Update comments based on @drreynolds suggestions
Steven-Roberts Sep 10, 2024
7e325c8
Merge branch 'feature/min-err' into feautre/adapt-opt
Steven-Roberts Sep 10, 2024
9bb63c0
Fix typos in changelog
Steven-Roberts Sep 10, 2024
875ae20
Merge branch 'develop' into feautre/adapt-opt
Steven-Roberts Sep 10, 2024
8d7a267
Fix typos in changelog
Steven-Roberts Sep 10, 2024
a02ca62
Merge branch 'develop' into feature/min-err
Steven-Roberts Sep 10, 2024
9ee43b3
Merge branch 'feature/min-err' into feautre/adapt-opt
Steven-Roberts Sep 10, 2024
65836d5
Merge branch 'feature/rk_defaults' into feautre/adapt-opt
Steven-Roberts Sep 10, 2024
7d5161b
Update test/unit_tests/arkode/C_serial/ark_test_adapt.c
Steven-Roberts Sep 11, 2024
fcb7dc6
Add missing SUN_RCONST
Steven-Roberts Sep 11, 2024
1db5b33
Update test/unit_tests/arkode/C_serial/ark_test_adapt.c
Steven-Roberts Sep 11, 2024
f81a60d
Merge branch 'develop' into feature/min-err
Steven-Roberts Sep 11, 2024
7669c93
Add more asserts to controllers
Steven-Roberts Sep 11, 2024
332a7d0
Merge branch 'feature/rk_defaults' into feautre/adapt-opt
Steven-Roberts Sep 16, 2024
ed094a8
Merge branch 'develop' into feature/min-err
Steven-Roberts Sep 16, 2024
8a49e61
Merge branch 'feature/min-err' into feautre/adapt-opt
Steven-Roberts Sep 16, 2024
a1eb43c
Added constructors for additional 'recommended' controllers from Sylv…
drreynolds Sep 18, 2024
afea9c0
Spelling fix
drreynolds Sep 18, 2024
7fe8654
Formatting
drreynolds Sep 18, 2024
cc26cf2
Fixed typo
drreynolds Sep 20, 2024
47ceedd
Added FAQ on temporal adaptivity controller selection
drreynolds Sep 20, 2024
fa46710
spelling
drreynolds Sep 20, 2024
27fca0a
Added ARKodeSetAdaptControllerByName
drreynolds Oct 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ specific linker flags e.g., MKL.

### Bug Fixes

Removed error floors from the SUNAdaptController implementations which could
unnecessarily limit the time size growth, particularly after the first step.

On the first two time steps, the Soderlind controller uses an I controller
instead of omitting unavailable terms.

Comment on lines +49 to +54
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure that these are "bug fixes" per-se. Should these be moved to "enhancements" section above?

Fixed `ARKodeResize` not using the default `hscale` when an argument of `0` was
provided.

Expand Down
7 changes: 7 additions & 0 deletions doc/shared/RecentChanges.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ specific linker flags e.g., MKL.

**Bug Fixes**

Removed error floors from the SUNAdaptController implementations which could
unnecessarily limit the time size growth, particularly after the first step.

On the first two time steps, the
:ref:`Soderlind controller <SUNAdaptController.Soderlind>` uses an I controller
instead of omitting unavailable terms.

Comment on lines +59 to +65
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure that these are "bug fixes" per-se. Should these be moved to "enhancements" section above?

Fixed c:func:`ARKodeResize` not using the default ``hscale`` when an argument of
``0`` was provided.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ with the form

In the above formulas, the default values of :math:`k_1^E`, :math:`k_2^E`,
:math:`k_1^I`, and :math:`k_2^I` are 0.367, 0.268, 0.98, and 0.95, respectively,
and :math:`p` is the global order of the time integration method. In these
estimates, a floor of :math:`\varepsilon_* > 10^{-10}` is enforced to avoid
division-by-zero errors.
and :math:`p` is the global order of the time integration method.

The SUNAdaptController_ImExGus controller implements both formulas
:eq:`expGusController` and :eq:`impGusController`, and sets its recommended step
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ and :cite:p:`Sod:06`. This controller has the form

with default parameter values :math:`k_1 = 1.25`, :math:`k_2 = 0.5`,
:math:`k_3 = -0.75`, :math:`k_4 = 0.25`, and :math:`k_5 = 0.75`, where
:math:`p` is the global order of the time integration method. In this estimate,
a floor of :math:`\varepsilon_* > 10^{-10}` is enforced to avoid division-by-zero
errors. During the first two steps (when :math:`\varepsilon_{n-2}`,
:math:`\varepsilon_{n-1}`, :math:`h_{n-2}`, and :math:`h_{n-2}` may be unavailable),
the corresponding terms are merely omitted during estimation of :math:`h'`.
:math:`p` is the global order of the time integration method. During the first
two steps (when :math:`\varepsilon_{n-2}`, :math:`\varepsilon_{n-1}`,
:math:`h_{n-2}`, and :math:`h_{n-2}` may be unavailable), the I controller
:math:`h' = h_n \varepsilon_1^{-1/(p+1)}` is used.

The SUNAdaptController_Soderlind controller is implemented as a derived
SUNAdaptController class, and defines its *content* field as:
Expand Down
53 changes: 43 additions & 10 deletions include/sundials/sundials_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,27 @@ extern "C" {

/*
* -----------------------------------------------------------------
* Function : SUNRpowerI
* Function : SUNRcopysign
* -----------------------------------------------------------------
* Usage : int exponent;
* sunrealtype base, ans;
* ans = SUNRpowerI(base,exponent);
* Usage : sunrealtype z;
* z = SUNRcopysign(x, y);
* -----------------------------------------------------------------
* SUNRpowerI returns the value of base^exponent, where base is of type
* sunrealtype and exponent is of type int.
* SUNRcopysign(x, y) returns x with the sign of y.
* -----------------------------------------------------------------
*/

SUNDIALS_EXPORT sunrealtype SUNRpowerI(sunrealtype base, int exponent);
#ifndef SUNRcopysign
#if defined(SUNDIALS_DOUBLE_PRECISION)
#define SUNRcopysign(x, y) (copysign((x), (y)))
#elif defined(SUNDIALS_SINGLE_PRECISION)
#define SUNRcopysign(x, y) (copysignf((x), (y)))
#elif defined(SUNDIALS_EXTENDED_PRECISION)
#define SUNRcopysign(x, y) (copysignl((x), (y)))
#else
#error \
"SUNDIALS precision not defined, report to github.com/LLNL/sundials/issues"
#endif
#endif

/*
* -----------------------------------------------------------------
Expand All @@ -181,12 +190,36 @@ SUNDIALS_EXPORT sunrealtype SUNRpowerI(sunrealtype base, int exponent);
* ans = SUNRpowerR(base,exponent);
* -----------------------------------------------------------------
* SUNRpowerR returns the value of base^exponent, where both base and
* exponent are of type sunrealtype. If base < ZERO, then SUNRpowerR
* returns ZERO.
* exponent are of type sunrealtype.
* -----------------------------------------------------------------
*/
#ifndef SUNRpowerR
#if defined(SUNDIALS_DOUBLE_PRECISION)
#define SUNRpowerR(base, exponent) (pow(base, exponent))
#elif defined(SUNDIALS_SINGLE_PRECISION)
#define SUNRpowerR(base, exponent) (powf(base, exponent))
#elif defined(SUNDIALS_EXTENDED_PRECISION)
#define SUNRpowerR(base, exponent) (powl(base, exponent))
#else
#error \
"SUNDIALS precision not defined, report to github.com/LLNL/sundials/issues"
#endif
#endif

/*
* -----------------------------------------------------------------
* Function : SUNRpowerI
* -----------------------------------------------------------------
* Usage : int exponent;
* sunrealtype base, ans;
* ans = SUNRpowerI(base,exponent);
* -----------------------------------------------------------------
* SUNRpowerI returns the value of base^exponent, where base is of type
* sunrealtype and exponent is of type int.
* -----------------------------------------------------------------
*/

SUNDIALS_EXPORT sunrealtype SUNRpowerR(sunrealtype base, sunrealtype exponent);
SUNDIALS_EXPORT sunrealtype SUNRpowerI(sunrealtype base, int exponent);

/*
* -----------------------------------------------------------------
Expand Down
30 changes: 16 additions & 14 deletions src/arkode/arkode_adapt.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int arkAdapt(ARKodeMem ark_mem, ARKodeHAdaptMem hadapt_mem, N_Vector ycur,
sunrealtype tcur, sunrealtype hcur, sunrealtype dsm)
{
int retval;
sunrealtype h_acc, h_cfl, int_dir;
sunrealtype h_acc, h_cfl;
int controller_order;

/* Return with no stepsize adjustment if the controller is NULL */
Expand Down Expand Up @@ -130,9 +130,6 @@ int arkAdapt(ARKodeMem ark_mem, ARKodeHAdaptMem hadapt_mem, N_Vector ycur,
return (ARK_CONTROLLER_ERR);
}

/* determine direction of integration */
int_dir = hcur / SUNRabs(hcur);

/* Call explicit stability function */
retval = hadapt_mem->expstab(ycur, tcur, &h_cfl, hadapt_mem->estab_data);
if (retval != ARK_SUCCESS)
Expand All @@ -141,7 +138,7 @@ int arkAdapt(ARKodeMem ark_mem, ARKodeHAdaptMem hadapt_mem, N_Vector ycur,
"Error in explicit stability function.");
return (ARK_ILL_INPUT);
}
if (h_cfl <= ZERO) { h_cfl = SUN_RCONST(1.0e30) * SUNRabs(hcur); }
if (h_cfl <= ZERO) { h_cfl = INFINITY; }

#if SUNDIALS_LOGGING_LEVEL >= SUNDIALS_LOGGING_INFO
SUNLogger_QueueMsg(ARK_LOGGER, SUN_LOGLEVEL_INFO, "ARKODE::arkAdapt",
Expand All @@ -151,34 +148,39 @@ int arkAdapt(ARKodeMem ark_mem, ARKodeHAdaptMem hadapt_mem, N_Vector ycur,

/* enforce safety factors */
h_acc *= hadapt_mem->safety;
h_cfl *= hadapt_mem->cfl * int_dir;
h_cfl *= hadapt_mem->cfl;

/* enforce maximum bound on time step growth */
h_acc = int_dir * SUNMIN(SUNRabs(h_acc), SUNRabs(hadapt_mem->etamax * hcur));
h_acc = SUNMIN(SUNRabs(h_acc), SUNRabs(hadapt_mem->etamax * hcur));

/* enforce minimum bound time step reduction */
h_acc = int_dir * SUNMAX(SUNRabs(h_acc), SUNRabs(hadapt_mem->etamin * hcur));
h_acc = SUNMAX(h_acc, SUNRabs(hadapt_mem->etamin * hcur));

#if SUNDIALS_LOGGING_LEVEL >= SUNDIALS_LOGGING_INFO
SUNLogger_QueueMsg(ARK_LOGGER, SUN_LOGLEVEL_INFO, "ARKODE::arkAdapt",
"new-step-after-max-min-bounds",
"h_acc = %" RSYM ", h_cfl = %" RSYM, h_acc, h_cfl);
"h_acc = %" RSYM ", h_cfl = %" RSYM,
SUNRcopysign(h_acc, hcur), h_cfl);
#endif

/* increment the relevant step counter, set desired step */
if (SUNRabs(h_acc) < SUNRabs(h_cfl)) { hadapt_mem->nst_acc++; }
else { hadapt_mem->nst_exp++; }
h_acc = int_dir * SUNMIN(SUNRabs(h_acc), SUNRabs(h_cfl));
if (h_acc <= h_cfl) { hadapt_mem->nst_acc++; }
else
{
hadapt_mem->nst_exp++;
h_acc = h_cfl;
}

/* enforce adaptivity bounds to retain Jacobian/preconditioner accuracy */
if (dsm <= ONE)
{
if ((SUNRabs(h_acc) > SUNRabs(hcur * hadapt_mem->lbound * ONEMSM)) &&
(SUNRabs(h_acc) < SUNRabs(hcur * hadapt_mem->ubound * ONEPSM)))
if ((h_acc > SUNRabs(hcur * hadapt_mem->lbound * ONEMSM)) &&
(h_acc < SUNRabs(hcur * hadapt_mem->ubound * ONEPSM)))
{
h_acc = hcur;
}
}
h_acc = SUNRcopysign(h_acc, hcur);

/* set basic value of ark_eta */
ark_mem->eta = h_acc / hcur;
Expand Down
6 changes: 3 additions & 3 deletions src/arkode/arkode_adapt_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ extern "C" {

/* Time step controller default values */
#define CFLFAC SUN_RCONST(0.5)
#define SAFETY SUN_RCONST(0.96) /* CVODE uses 1.0 */
#define SAFETY SUN_RCONST(0.9) /* CVODE uses 1.0 */
#define GROWTH SUN_RCONST(20.0) /* CVODE uses 10.0 */
#define HFIXED_LB SUN_RCONST(1.0) /* CVODE uses 1.0 */
#define HFIXED_UB SUN_RCONST(1.5) /* CVODE uses 1.5 */
#define HFIXED_UB SUN_RCONST(1.0) /* CVODE uses 1.5 */

/* maximum step size change on first step */
#define ETAMX1 SUN_RCONST(10000.0)
Expand All @@ -60,7 +60,7 @@ extern "C" {
DEPRECATED, REMOVE AT SAME TIME AS ARKStepSetAdaptivityMethod */
#define PQ 0
/* adjustment to apply within controller to method order of accuracy */
#define ADJUST -1
#define ADJUST 0

/*===============================================================
ARKODE Time Step Adaptivity Data Structure
Expand Down
2 changes: 2 additions & 0 deletions src/arkode/arkode_arkstep_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,8 @@ int ARKStepSetDefaults(void* arkode_mem)

int ARKStepSetOptimalParams(void* arkode_mem)
{
/* TODO: do we need to do something here? This is depricated with no
* ARKodeSetOptimalParams to replace it */
Steven-Roberts marked this conversation as resolved.
Show resolved Hide resolved
ARKodeMem ark_mem;
ARKodeARKStepMem step_mem;
ARKodeHAdaptMem hadapt_mem;
Expand Down
5 changes: 0 additions & 5 deletions src/arkode/arkode_erkstep_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,6 @@ int erkStep_SetDefaults(ARKodeMem ark_mem)
ark_mem->lrw += lenrw;
}

/* Update some controller parameters */
(void)SUNAdaptController_SetErrorBias(ark_mem->hadapt_mem->hcontroller,
SUN_RCONST(1.2));
(void)SUNAdaptController_SetParams_PI(ark_mem->hadapt_mem->hcontroller,
SUN_RCONST(0.8), -SUN_RCONST(0.31));
return (ARK_SUCCESS);
}

Expand Down
6 changes: 3 additions & 3 deletions src/arkode/arkode_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -936,11 +936,11 @@ int ARKodeSetAdaptController(void* arkode_mem, SUNAdaptController C)
/* On NULL-valued input, create default SUNAdaptController object */
if (C == NULL)
{
C = SUNAdaptController_PID(ark_mem->sunctx);
C = SUNAdaptController_I(ark_mem->sunctx);
if (C == NULL)
{
arkProcessError(ark_mem, ARK_MEM_FAIL, __LINE__, __func__, __FILE__,
"SUNAdaptControllerPID allocation failure");
"SUNAdaptControllerI allocation failure");
return (ARK_MEM_FAIL);
}
ark_mem->hadapt_mem->owncontroller = SUNTRUE;
drreynolds marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -1610,7 +1610,7 @@ int ARKodeSetSafetyFactor(void* arkode_mem, sunrealtype safety)
}

/* check for allowable parameters */
if (safety >= ONE)
if (safety > ONE)
{
arkProcessError(ark_mem, ARK_ILL_INPUT, __LINE__, __func__, __FILE__,
"Illegal safety factor");
Expand Down
41 changes: 23 additions & 18 deletions src/sunadaptcontroller/imexgus/sunadaptcontroller_imexgus.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
#define DEFAULT_K2E SUN_RCONST(0.268)
#define DEFAULT_K1I SUN_RCONST(0.95)
#define DEFAULT_K2I SUN_RCONST(0.95)
#define DEFAULT_BIAS SUN_RCONST(1.5)
#define TINY SUN_RCONST(1.0e-10)
#define DEFAULT_BIAS SUN_RCONST(1.0)

/* -----------------------------------------------------------------
* exported functions
Expand Down Expand Up @@ -128,32 +127,38 @@ SUNErrCode SUNAdaptController_EstimateStep_ImExGus(SUNAdaptController C,
{
SUNFunctionBegin(C->sunctx);

SUNAssert(hnew, SUN_ERR_ARG_CORRUPT);

/* order parameter to use */
const int ord = p + 1;

/* set usable time-step adaptivity parameters -- first step */
const sunrealtype k = -SUN_RCONST(1.0) / ord;
const sunrealtype e = SUNMAX(SACIMEXGUS_BIAS(C) * dsm, TINY);

/* set usable time-step adaptivity parameters -- subsequent steps */
const sunrealtype k1e = -SACIMEXGUS_K1E(C) / ord;
const sunrealtype k2e = -SACIMEXGUS_K2E(C) / ord;
const sunrealtype k1i = -SACIMEXGUS_K1I(C) / ord;
const sunrealtype k2i = -SACIMEXGUS_K2I(C) / ord;
const sunrealtype e1 = SUNMAX(SACIMEXGUS_BIAS(C) * dsm, TINY);
const sunrealtype e2 = e1 / SUNMAX(SACIMEXGUS_EP(C), TINY);
const sunrealtype hrat = h / SACIMEXGUS_HP(C);

/* compute estimated time step size, modifying the first step formula */
if (SACIMEXGUS_FIRSTSTEP(C)) { *hnew = h * SUNRpowerR(e, k); }
if (SACIMEXGUS_FIRSTSTEP(C))
{
/* set usable time-step adaptivity parameters -- first step */
const sunrealtype k = -SUN_RCONST(1.0) / ord;
const sunrealtype e = SACIMEXGUS_BIAS(C) * dsm;
*hnew = h * SUNRpowerR(e, k);
}
else
{
/* set usable time-step adaptivity parameters -- subsequent steps */
const sunrealtype k1e = -SACIMEXGUS_K1E(C) / ord;
const sunrealtype k2e = -SACIMEXGUS_K2E(C) / ord;
const sunrealtype k1i = -SACIMEXGUS_K1I(C) / ord;
const sunrealtype k2i = -SACIMEXGUS_K2I(C) / ord;
const sunrealtype e1 = SACIMEXGUS_BIAS(C) * dsm;
const sunrealtype e2 = e1 / SACIMEXGUS_EP(C);
const sunrealtype hrat = h / SACIMEXGUS_HP(C);
*hnew = h * SUNMIN(hrat * SUNRpowerR(e1, k1i) * SUNRpowerR(e2, k2i),
SUNRpowerR(e1, k1e) * SUNRpowerR(e2, k2e));
}

if (!isfinite(*hnew))
{
/* hnew can be INFINITY or NAN if multiple e's are 0 or there are overflows.
* In that case, make hnew INFINITY with the same sign as h */
*hnew = SUNRcopysign(INFINITY, h);
}

/* return with success */
return SUN_SUCCESS;
}
Expand Down
Loading
Loading