Skip to content

Commit 6be6202

Browse files
authored
Bugfix: CVODE(S) Inequality Constraints (#787)
Fix a bug in the CVODE(S) inequality constraint handling where the predicted state was used to compute the step size reduction factor instead of the prior solution which could lead to an incorrect reduction in the step size or, when the prediction violates the constraints, an infinitely large step size in the next step attempt. (Fixes #702) Add a unit test for inequality constraint handling with all the integrators. In CVODE(S) and IDA(S), separate inequality constraint check from the nonlinear solve check. Add functions to set the maximum number of inequality constraint failures and get total number of failures (constraint failures are no longer included in the number of step failures due to a solver failure). Add a function to get the number of steps modified but not failed due to constraint violations. Synchronize some differences between CVODE/CVODES and IDA/IDAS source and docs. Add missing return flag names to `CVodeGetReturnFlagName` in CVODES and `IDAGetReturnFlagName` in IDA.
1 parent fe37795 commit 6be6202

File tree

217 files changed

+6306
-2812
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+6306
-2812
lines changed

CHANGELOG.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,31 @@ interfaces to most features of SUNDIALS.
1010

1111
### New Features and Enhancements
1212

13+
Added functions to CVODE(S) and IDA(S) to set the maximum number of inequality
14+
constraint failures in a step attempt (`CVodeSetMaxNumConstraintFails` and
15+
`IDASetMaxNumConstraintFails`) and to retrieve the total number of failed step
16+
attempts due to an inequality constraint violation (`CVodeGetNumConstraintFails`
17+
and `IDAGetNumConstraintFails`). As a result, constraint failures are no longer
18+
included in the number of step failures due to a solver failure (i.e., the
19+
values returned by `CVodeGetNumStepSolveFails` and `IDAGetNumStepSolveFails`).
20+
The functions `CVodeGetNumConstraintCorrections` and
21+
`IDAGetNumConstraintCorrections` were also added to retrieve the number of steps
22+
where the corrector was modified to satisfy an inequality constraint without
23+
failing the step.
24+
1325
The functions `CVodeGetUserDataB` and `IDAGetUserDataB` were added to CVODES
1426
and IDAS, respectively.
1527

1628
### Bug Fixes
1729

30+
Fixed a bug in the CVODE(S) inequality constraint handling where the predicted
31+
state was used to compute the step size reduction factor which could lead to an
32+
insufficient reduction in the step size or, when the prediction violates the
33+
constraints, an infinitely large step size in the next step attempt.
34+
35+
In CVODES and IDA, added missing return flag names to `CVodeGetReturnFlagName`
36+
and `IDAGetReturnFlagName`, respectively.
37+
1838
Fixed a CMake bug which resulted in static targets depending on shared targets
1939
when building both types of libraries in the same build
2040
([Issue #692](https://github.com/LLNL/sundials/issues/692)).
@@ -55,19 +75,21 @@ called even when informational logging was disabled.
5575

5676
### Deprecation Notices
5777

58-
`SUNDIALSFileOpen` and `SUNDIALSFileClose` will be removed in the next major release.
59-
Use `SUNFileOpen` and `SUNFileClose` instead.
78+
`SUNDIALSFileOpen` and `SUNDIALSFileClose` will be removed in the next major
79+
release. Use `SUNFileOpen` and `SUNFileClose` instead.
6080

61-
The `Convert` methods on the `sundials::kokkos:Vector`, `sundials::kokkos::DenseMatrix`,
62-
`sundials::ginkgo::Matrix`, `sundials::ginkgo::BatchMatrix`, `sundials::kokkos::DenseLinearSolver`,
63-
`sundials::ginkgo::LinearSolver`, and `sundials::ginkgo::BatchLinearSolver` classes have
64-
been deprecated and will be removed in the next major release. The method `get`, should
65-
be used instead.
81+
The `Convert` methods on the `sundials::kokkos:Vector`,
82+
`sundials::kokkos::DenseMatrix`, `sundials::ginkgo::Matrix`,
83+
`sundials::ginkgo::BatchMatrix`, `sundials::kokkos::DenseLinearSolver`,
84+
`sundials::ginkgo::LinearSolver`, and `sundials::ginkgo::BatchLinearSolver`
85+
classes have been deprecated and will be removed in the next major release. The
86+
method `get`, should be used instead.
6687

67-
The `CSC_MAT` and `CSR_MAT` macros defined in `sunmatrix_sparse.h` will be removed in
68-
the next major release. Use `SUN_CSC_MAT` and `SUN_CSR_MAT` instead.
88+
The `CSC_MAT` and `CSR_MAT` macros defined in `sunmatrix_sparse.h` will be
89+
removed in the next major release. Use `SUN_CSC_MAT` and `SUN_CSR_MAT` instead.
6990

70-
The `N_Vector_S` typedef to `N_Vector*` is deprecated and will be removed in the next major release.
91+
The `N_Vector_S` typedef to `N_Vector*` is deprecated and will be removed in the
92+
next major release.
7193

7294
## Changes to SUNDIALS in release 7.5.0
7395

bindings/sundials4py/cvodes/cvodes_generated.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ m.def("CVodeSVtolerances", CVodeSVtolerances, nb::arg("cvode_mem"),
114114
m.def("CVodeSetConstraints", CVodeSetConstraints, nb::arg("cvode_mem"),
115115
nb::arg("constraints"));
116116

117+
m.def("CVodeSetMaxNumConstraintFails", CVodeSetMaxNumConstraintFails,
118+
nb::arg("cvode_mem"), nb::arg("max_fails"));
119+
117120
m.def("CVodeSetDeltaGammaMaxLSetup", CVodeSetDeltaGammaMaxLSetup,
118121
nb::arg("cvode_mem"), nb::arg("dgmax_lsetup"));
119122

@@ -681,6 +684,45 @@ m.def(
681684
},
682685
nb::arg("cvode_mem"));
683686

687+
m.def(
688+
"CVodeGetNumConstraintFails",
689+
[](void* cvode_mem) -> std::tuple<int, long>
690+
{
691+
auto CVodeGetNumConstraintFails_adapt_modifiable_immutable_to_return =
692+
[](void* cvode_mem) -> std::tuple<int, long>
693+
{
694+
long num_fails_out_adapt_modifiable;
695+
696+
int r = CVodeGetNumConstraintFails(cvode_mem,
697+
&num_fails_out_adapt_modifiable);
698+
return std::make_tuple(r, num_fails_out_adapt_modifiable);
699+
};
700+
701+
return CVodeGetNumConstraintFails_adapt_modifiable_immutable_to_return(
702+
cvode_mem);
703+
},
704+
nb::arg("cvode_mem"));
705+
706+
m.def(
707+
"CVodeGetNumConstraintCorrections",
708+
[](void* cvode_mem) -> std::tuple<int, long>
709+
{
710+
auto CVodeGetNumConstraintCorrections_adapt_modifiable_immutable_to_return =
711+
[](void* cvode_mem) -> std::tuple<int, long>
712+
{
713+
long num_corrections_out_adapt_modifiable;
714+
715+
int r =
716+
CVodeGetNumConstraintCorrections(cvode_mem,
717+
&num_corrections_out_adapt_modifiable);
718+
return std::make_tuple(r, num_corrections_out_adapt_modifiable);
719+
};
720+
721+
return CVodeGetNumConstraintCorrections_adapt_modifiable_immutable_to_return(
722+
cvode_mem);
723+
},
724+
nb::arg("cvode_mem"));
725+
684726
m.def("CVodePrintAllStats", CVodePrintAllStats, nb::arg("cvode_mem"),
685727
nb::arg("outfile"), nb::arg("fmt"));
686728

bindings/sundials4py/idas/idas_generated.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ m.def("IDASetId", IDASetId, nb::arg("ida_mem"), nb::arg("id"));
128128
m.def("IDASetConstraints", IDASetConstraints, nb::arg("ida_mem"),
129129
nb::arg("constraints"));
130130

131+
m.def("IDASetMaxNumConstraintFails", IDASetMaxNumConstraintFails,
132+
nb::arg("ida_mem"), nb::arg("max_fails"));
133+
131134
m.def("IDASetEtaFixedStepBounds", IDASetEtaFixedStepBounds, nb::arg("ida_mem"),
132135
nb::arg("eta_min_fx"), nb::arg("eta_max_fx"));
133136

@@ -508,6 +511,43 @@ m.def("IDAGetErrWeights", IDAGetErrWeights, nb::arg("ida_mem"),
508511
m.def("IDAGetEstLocalErrors", IDAGetEstLocalErrors, nb::arg("ida_mem"),
509512
nb::arg("ele"));
510513

514+
m.def(
515+
"IDAGetNumConstraintFails",
516+
[](void* ida_mem) -> std::tuple<int, long>
517+
{
518+
auto IDAGetNumConstraintFails_adapt_modifiable_immutable_to_return =
519+
[](void* ida_mem) -> std::tuple<int, long>
520+
{
521+
long num_fails_out_adapt_modifiable;
522+
523+
int r = IDAGetNumConstraintFails(ida_mem, &num_fails_out_adapt_modifiable);
524+
return std::make_tuple(r, num_fails_out_adapt_modifiable);
525+
};
526+
527+
return IDAGetNumConstraintFails_adapt_modifiable_immutable_to_return(ida_mem);
528+
},
529+
nb::arg("ida_mem"));
530+
531+
m.def(
532+
"IDAGetNumConstraintCorrections",
533+
[](void* ida_mem) -> std::tuple<int, long>
534+
{
535+
auto IDAGetNumConstraintCorrections_adapt_modifiable_immutable_to_return =
536+
[](void* ida_mem) -> std::tuple<int, long>
537+
{
538+
long num_corrections_out_adapt_modifiable;
539+
540+
int r =
541+
IDAGetNumConstraintCorrections(ida_mem,
542+
&num_corrections_out_adapt_modifiable);
543+
return std::make_tuple(r, num_corrections_out_adapt_modifiable);
544+
};
545+
546+
return IDAGetNumConstraintCorrections_adapt_modifiable_immutable_to_return(
547+
ida_mem);
548+
},
549+
nb::arg("ida_mem"));
550+
511551
m.def(
512552
"IDAGetNumGEvals",
513553
[](void* ida_mem) -> std::tuple<int, long>

0 commit comments

Comments
 (0)