Skip to content

Commit 0b22fde

Browse files
committed
interface SUNDomEigEstimator module
1 parent 361d3da commit 0b22fde

10 files changed

+310
-1
lines changed

bindings/pysundials/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ set(sundials_SOURCES
6060
sundials/pysundials_adjointstepper.cpp
6161
sundials/pysundials_context.cpp
6262
sundials/pysundials_core.cpp
63+
sundials/pysundials_domeigestimator.cpp
6364
sundials/pysundials_linearsolver.cpp
6465
sundials/pysundials_logger.cpp
6566
sundials/pysundials_matrix.cpp

bindings/pysundials/arkode/pysundials_arkode_mristep.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ void bind_arkode_mristep(nb::module_& m)
4646
// defined in a source file elsewhere. As such, we need to declare it here since its
4747
// not picked up in any header files by the generator.
4848
nb::class_<_MRIStepInnerStepper>(m, "_MRIStepInnerStepper");
49-
// .def(nb::init<>()); // implicit default constructor
5049

5150
nb::class_<MRIStepInnerStepperView>(m, "MRIStepInnerStepperView")
5251
.def_static("Create", &MRIStepInnerStepperView::Create<MRIStepInnerStepper>)

bindings/pysundials/sundials/generate.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ modules:
6262
# nanobind cannot bind to functions which take a **
6363
- "GetProfiler"
6464
- "GetLogger"
65+
sundials_domeigestimator:
66+
path: sundials/pysundials_domeigestimator_generated.hpp
67+
headers:
68+
- ../../include/sundials/sundials_domeigestimator.h
69+
fn_exclude_by_name__regex:
70+
- "Create" # nanobind cannot bind to functions which take a **, so we do something custom
71+
- "SUNDomEigEstimator_SetATimes" # nanobind cannot bind to functions which take a function pointer, so we do something custom
6572
sundials_errors:
6673
path: sundials/pysundials_errors_generated.hpp
6774
headers:

bindings/pysundials/sundials/pysundials_core.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void bind_sunadaptcontroller(nb::module_& m);
3030
void bind_sunadjointcheckpointscheme(nb::module_& m);
3131
void bind_sunadjointstepper(nb::module_& m);
3232
void bind_suncontext(nb::module_& m);
33+
void bind_sundomeigestimator(nb::module_& m);
3334
void bind_sunlinearsolver(nb::module_& m);
3435
void bind_sunlogger(nb::module_& m);
3536
void bind_sunmatrix(nb::module_& m);
@@ -61,6 +62,7 @@ void bind_core(nb::module_& m)
6162
bind_sunadjointcheckpointscheme(m);
6263
bind_sunadjointstepper(m);
6364
bind_suncontext(m);
65+
bind_sundomeigestimator(m);
6466
bind_sunlinearsolver(m);
6567
bind_sunlogger(m);
6668
bind_sunmatrix(m);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* -----------------------------------------------------------------
2+
* Programmer(s): Cody J. Balos @ LLNL
3+
* -----------------------------------------------------------------
4+
* SUNDIALS Copyright Start
5+
* Copyright (c) 2002-2025, Lawrence Livermore National Security
6+
* and Southern Methodist University.
7+
* All rights reserved.
8+
*
9+
* See the top-level LICENSE and NOTICE files for details.
10+
*
11+
* SPDX-License-Identifier: BSD-3-Clause
12+
* SUNDIALS Copyright End
13+
* -----------------------------------------------------------------
14+
* This file is the entrypoint for the Python binding code for the
15+
* SUNDIALS N_Vector class. It contains hand-written code for functions
16+
* that require special treatment, and includes the generated code
17+
* produced with the generate.py script.
18+
* -----------------------------------------------------------------*/
19+
20+
#include <cstdlib>
21+
#include <cstring>
22+
23+
#include <nanobind/nanobind.h>
24+
#include <nanobind/stl/function.h>
25+
26+
#include <sundials/sundials_domeigestimator.hpp>
27+
28+
#include "pysundials_domeigestimator_usersupplied.hpp"
29+
30+
namespace nb = nanobind;
31+
using namespace sundials::experimental;
32+
33+
void bind_sundomeigestimator(nb::module_& m)
34+
{
35+
#include "pysundials_domeigestimator_generated.hpp"
36+
37+
nb::class_<SUNDomEigEstimatorView>(m, "SUNDomEigEstimatorView")
38+
.def_static("Create", &SUNDomEigEstimatorView::Create<SUNDomEigEstimator>)
39+
.def("get", nb::overload_cast<>(&SUNDomEigEstimatorView::get, nb::const_),
40+
nb::rv_policy::reference);
41+
42+
m.def(
43+
"SUNDomEigEstimator_SetATimes",
44+
[](SUNDomEigEstimator dee,
45+
std::function<std::remove_pointer_t<SUNATimesFn>> ATimes) -> SUNErrCode
46+
{
47+
if (!dee->python)
48+
{
49+
dee->python = SUNDomEigEstimatorFunctionTable_Alloc();
50+
}
51+
52+
auto fntable = static_cast<SUNDomEigEstimatorFunctionTable*>(dee->python);
53+
54+
fntable->atimes = nb::cast(ATimes);
55+
56+
if (ATimes)
57+
{
58+
return SUNDomEigEstimator_SetATimes(dee, nullptr,
59+
sundomeigestimator_atimes_wrapper);
60+
}
61+
else { return SUNDomEigEstimator_SetATimes(dee, nullptr, nullptr); }
62+
},
63+
nb::arg("DEE"), nb::arg("ATimes").none());
64+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// #ifndef _SUNDOMEIGEST_H
2+
//
3+
// #ifdef __cplusplus
4+
// #endif
5+
//
6+
7+
auto pyClassSUNDomEigEstimator_Ops_ =
8+
nb::class_<SUNDomEigEstimator_Ops_>(m, "SUNDomEigEstimator_Ops_", "")
9+
.def(nb::init<>()) // implicit default constructor
10+
;
11+
12+
auto pyClassSUNDomEigEstimator_ =
13+
nb::class_<SUNDomEigEstimator_>(m, "SUNDomEigEstimator_", "")
14+
.def(nb::init<>()) // implicit default constructor
15+
;
16+
17+
m.def("SUNDomEigEstimator_SetMaxIters", SUNDomEigEstimator_SetMaxIters,
18+
nb::arg("DEE"), nb::arg("max_iters"));
19+
20+
m.def("SUNDomEigEstimator_SetNumPreprocessIters",
21+
SUNDomEigEstimator_SetNumPreprocessIters, nb::arg("DEE"),
22+
nb::arg("num_iters"));
23+
24+
m.def("SUNDomEigEstimator_SetRelTol", SUNDomEigEstimator_SetRelTol,
25+
nb::arg("DEE"), nb::arg("tol"));
26+
27+
m.def("SUNDomEigEstimator_SetInitialGuess", SUNDomEigEstimator_SetInitialGuess,
28+
nb::arg("DEE"), nb::arg("q"));
29+
30+
m.def("SUNDomEigEstimator_Initialize", SUNDomEigEstimator_Initialize,
31+
nb::arg("DEE"));
32+
33+
m.def(
34+
"SUNDomEigEstimator_Estimate",
35+
[](SUNDomEigEstimator DEE, double lambdaR,
36+
double lambdaI) -> std::tuple<SUNErrCode, double, double>
37+
{
38+
auto SUNDomEigEstimator_Estimate_adapt_modifiable_immutable_to_return =
39+
[](SUNDomEigEstimator DEE, double lambdaR,
40+
double lambdaI) -> std::tuple<SUNErrCode, double, double>
41+
{
42+
double* lambdaR_adapt_modifiable = &lambdaR;
43+
double* lambdaI_adapt_modifiable = &lambdaI;
44+
45+
SUNErrCode r = SUNDomEigEstimator_Estimate(DEE, lambdaR_adapt_modifiable,
46+
lambdaI_adapt_modifiable);
47+
return std::make_tuple(r, lambdaR, lambdaI);
48+
};
49+
50+
return SUNDomEigEstimator_Estimate_adapt_modifiable_immutable_to_return(DEE,
51+
lambdaR,
52+
lambdaI);
53+
},
54+
nb::arg("DEE"), nb::arg("lambdaR"), nb::arg("lambdaI"));
55+
56+
m.def(
57+
"SUNDomEigEstimator_GetRes",
58+
[](SUNDomEigEstimator DEE, double res) -> std::tuple<SUNErrCode, double>
59+
{
60+
auto SUNDomEigEstimator_GetRes_adapt_modifiable_immutable_to_return =
61+
[](SUNDomEigEstimator DEE, double res) -> std::tuple<SUNErrCode, double>
62+
{
63+
double* res_adapt_modifiable = &res;
64+
65+
SUNErrCode r = SUNDomEigEstimator_GetRes(DEE, res_adapt_modifiable);
66+
return std::make_tuple(r, res);
67+
};
68+
69+
return SUNDomEigEstimator_GetRes_adapt_modifiable_immutable_to_return(DEE,
70+
res);
71+
},
72+
nb::arg("DEE"), nb::arg("res"));
73+
74+
m.def(
75+
"SUNDomEigEstimator_GetNumIters",
76+
[](SUNDomEigEstimator DEE, long num_iters) -> std::tuple<SUNErrCode, long>
77+
{
78+
auto SUNDomEigEstimator_GetNumIters_adapt_modifiable_immutable_to_return =
79+
[](SUNDomEigEstimator DEE, long num_iters) -> std::tuple<SUNErrCode, long>
80+
{
81+
long* num_iters_adapt_modifiable = &num_iters;
82+
83+
SUNErrCode r = SUNDomEigEstimator_GetNumIters(DEE,
84+
num_iters_adapt_modifiable);
85+
return std::make_tuple(r, num_iters);
86+
};
87+
88+
return SUNDomEigEstimator_GetNumIters_adapt_modifiable_immutable_to_return(DEE,
89+
num_iters);
90+
},
91+
nb::arg("DEE"), nb::arg("num_iters"));
92+
93+
m.def(
94+
"SUNDomEigEstimator_GetNumATimesCalls",
95+
[](SUNDomEigEstimator DEE, long num_ATimes) -> std::tuple<SUNErrCode, long>
96+
{
97+
auto SUNDomEigEstimator_GetNumATimesCalls_adapt_modifiable_immutable_to_return =
98+
[](SUNDomEigEstimator DEE, long num_ATimes) -> std::tuple<SUNErrCode, long>
99+
{
100+
long* num_ATimes_adapt_modifiable = &num_ATimes;
101+
102+
SUNErrCode r =
103+
SUNDomEigEstimator_GetNumATimesCalls(DEE, num_ATimes_adapt_modifiable);
104+
return std::make_tuple(r, num_ATimes);
105+
};
106+
107+
return SUNDomEigEstimator_GetNumATimesCalls_adapt_modifiable_immutable_to_return(DEE,
108+
num_ATimes);
109+
},
110+
nb::arg("DEE"), nb::arg("num_ATimes"));
111+
112+
m.def("SUNDomEigEstimator_Write", SUNDomEigEstimator_Write, nb::arg("DEE"),
113+
nb::arg("outfile"));
114+
// #ifdef __cplusplus
115+
//
116+
// #endif
117+
//
118+
// #endif
119+
//
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*------------------------------------------------------------------------------
2+
* Programmer(s): Cody J. Balos @ LLNL
3+
*------------------------------------------------------------------------------
4+
* SUNDIALS Copyright Start
5+
* Copyright (c) 2002-2025, Lawrence Livermore National Security
6+
* and Southern Methodist University.
7+
* All rights reserved.
8+
*
9+
* See the top-level LICENSE and NOTICE files for details.
10+
*
11+
* SPDX-License-Identifier: BSD-3-Clause
12+
* SUNDIALS Copyright End
13+
*----------------------------------------------------------------------------*/
14+
15+
#ifndef _PYSUNDIALS_SUNDOMEIGESTIMATOR_USERSUPPLIED_HPP
16+
#define _PYSUNDIALS_SUNDOMEIGESTIMATOR_USERSUPPLIED_HPP
17+
18+
#include <cstdlib>
19+
#include <cstring>
20+
21+
#include <nanobind/nanobind.h>
22+
#include <nanobind/stl/function.h>
23+
24+
#include <sundials/sundials_domeigestimator.hpp>
25+
26+
#include "pysundials_helpers.hpp"
27+
28+
namespace nb = nanobind;
29+
30+
using namespace sundials::experimental;
31+
32+
struct SUNDomEigEstimatorFunctionTable
33+
{
34+
nb::object atimes;
35+
};
36+
37+
inline SUNDomEigEstimatorFunctionTable* SUNDomEigEstimatorFunctionTable_Alloc()
38+
{
39+
// We must use malloc since ARKodeFree calls free
40+
auto fn_table = static_cast<SUNDomEigEstimatorFunctionTable*>(
41+
std::malloc(sizeof(SUNDomEigEstimatorFunctionTable)));
42+
43+
// Zero out the memory
44+
std::memset(fn_table, 0, sizeof(SUNDomEigEstimatorFunctionTable));
45+
46+
return fn_table;
47+
}
48+
49+
SUNErrCode sundomeigestimator_atimes_wrapper(void* A_data, N_Vector v, N_Vector z)
50+
{
51+
return pysundials::user_supplied_fn_caller<
52+
std::remove_pointer_t<SUNATimesFn>, SUNDomEigEstimatorFunctionTable,
53+
3>(&SUNDomEigEstimatorFunctionTable::atimes, A_data, v, z);
54+
}
55+
56+
#endif

include/sundials/sundials_domeigestimator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct SUNDomEigEstimator_Ops_
6464
struct SUNDomEigEstimator_
6565
{
6666
void* content;
67+
void* python;
6768
SUNDomEigEstimator_Ops ops;
6869
SUNContext sunctx;
6970
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* -----------------------------------------------------------------------------
2+
* Programmer(s): Cody J. Balos @ LLNL
3+
* -----------------------------------------------------------------------------
4+
* SUNDIALS Copyright Start
5+
* Copyright (c) 2002-2025, Lawrence Livermore National Security
6+
* and Southern Methodist University.
7+
* All rights reserved.
8+
*
9+
* See the top-level LICENSE and NOTICE files for details.
10+
*
11+
* SPDX-License-Identifier: BSD-3-Clause
12+
* SUNDIALS Copyright End
13+
* -----------------------------------------------------------------------------
14+
* C++ view of SUNDIALS SUNDomEigEstimator
15+
* ---------------------------------------------------------------------------*/
16+
17+
#ifndef _SUNDIALS_DOMEIGESTIMATOR_HPP
18+
#define _SUNDIALS_DOMEIGESTIMATOR_HPP
19+
20+
#include <sundials/sundials_base.hpp>
21+
#include <sundials/sundials_domeigestimator.h>
22+
23+
namespace sundials {
24+
namespace impl {
25+
using BaseDomEigEstimator =
26+
BaseObject<SUNDomEigEstimator_, SUNDomEigEstimator_Ops_>;
27+
} // namespace impl
28+
29+
namespace experimental {
30+
struct SUNDomEigEstimatorDeleter
31+
{
32+
void operator()(SUNDomEigEstimator DEE)
33+
{
34+
if (DEE) { SUNDomEigEstimator_Destroy(&DEE); }
35+
}
36+
};
37+
38+
class SUNDomEigEstimatorView
39+
: public ClassView<SUNDomEigEstimator, SUNDomEigEstimatorDeleter>
40+
{
41+
public:
42+
using ClassView<SUNDomEigEstimator, SUNDomEigEstimatorDeleter>::ClassView;
43+
template<typename... Args>
44+
static SUNDomEigEstimatorView Create(Args&&... args);
45+
};
46+
47+
template<typename... Args>
48+
SUNDomEigEstimatorView SUNDomEigEstimatorView::Create(Args&&... args)
49+
{
50+
return SUNDomEigEstimatorView(std::forward<Args>(args)...);
51+
}
52+
53+
} // namespace experimental
54+
} // namespace sundials
55+
56+
#endif

src/sundials/sundials_domeigestimator.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ SUNDomEigEstimator SUNDomEigEstimator_NewEmpty(SUNContext sunctx)
7171
/* attach ops and initialize content and context to NULL */
7272
DEE->ops = ops;
7373
DEE->content = NULL;
74+
DEE->python = NULL;
7475
DEE->sunctx = sunctx;
7576

7677
return (DEE);
@@ -88,6 +89,9 @@ void SUNDomEigEstimator_FreeEmpty(SUNDomEigEstimator DEE)
8889
if (DEE->ops) { free(DEE->ops); }
8990
DEE->ops = NULL;
9091

92+
if (DEE->python) { free(DEE->python); }
93+
DEE->python = NULL;
94+
9195
/* free overall SUNDomEigEstimator object and return */
9296
free(DEE);
9397
return;

0 commit comments

Comments
 (0)