Skip to content

Commit c1a154b

Browse files
committed
Merge branch 'cvode_7.1.1' into SoybeanParameterization
2 parents b703c86 + 099a203 commit c1a154b

File tree

9 files changed

+115
-35
lines changed

9 files changed

+115
-35
lines changed

bin/ePhotosynthesis.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,7 @@ int main(int argc, const char* argv[]) {
175175
readFile(evn, inputs);
176176
if (fileProvided(atpcost, atpcost))
177177
readFile(atpcost, inputs);
178-
#ifdef SUNDIALS_CONTEXT_REQUIRED
179-
SUNContext context;
180-
if (SUNContext_Create(0, &context) < 0) {
181-
std::cout << "SUNContext_Create failed" << std::endl;
182-
throw std::runtime_error("SUNContext_Create");
183-
}
184-
Variables *theVars = new Variables(&context);
185-
#else // SUNDIALS_CONTEXT_REQUIRED
186-
Variables *theVars = new Variables();
187-
#endif // SUNDIALS_CONTEXT_REQUIRED
178+
Variables *theVars = new Variables();
188179
if (fileProvided(enzyme, enzymeFile)) {
189180
std::cerr << "ENZYME DATA PROVIDED" << std::endl;
190181
readFile(enzymeFile, theVars->EnzymeAct, true);
@@ -316,9 +307,6 @@ int main(int argc, const char* argv[]) {
316307
delete theVars;
317308
}
318309
delete maindriver;
319-
#ifdef SUNDIALS_CONTEXT_REQUIRED
320-
SUNContext_Free(&context);
321-
#endif // SUNDIALS_CONTEXT_REQUIRED
322310
return (EXIT_SUCCESS);
323311
} catch (std::exception& e) {
324312
std::cout << "An error occurred: " << e.what() << std:: endl;

cmake/FindSUNDIALS.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ find_path(SUNDIALS_INCLUDE_DIR sundials_types.h
9898
include
9999
include/sundials
100100
)
101+
cmake_path(GET SUNDIALS_INCLUDE_DIR PARENT_PATH SUNDIALS_INCLUDE_DIR_PARENT)
101102
set(SUNDIALS_INCLUDE_DIRS
102-
"${SUNDIALS_INCLUDE_DIR}/.."
103+
"${SUNDIALS_INCLUDE_DIR_PARENT}"
103104
"${SUNDIALS_INCLUDE_DIR}"
104105
)
105106

include/Variables.hpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
**********************************************************************************************************************************************/
2828
#include <map>
29+
#include <memory>
2930
#include "definitions.hpp"
3031
#include "vel/FIVel.hpp"
3132
#include "vel/BFVel.hpp"
@@ -56,17 +57,55 @@
5657

5758
namespace ePhotosynthesis {
5859

60+
#ifdef SUNDIALS_CONTEXT_REQUIRED
61+
extern std::shared_ptr<SUNContext> global_context; //!< Global context
62+
63+
/**
64+
Initialize the global SUNContext.
65+
*/
66+
void init_global_sundials_context();
67+
68+
/**
69+
Destroy the global SUNContext.
70+
*/
71+
void cleanup_global_sundials_context();
72+
73+
#endif // SUNDIALS_CONTEXT_REQUIRED
74+
5975
/**
6076
Structure to hold global variables
6177
*/
6278
class Variables {
6379
public:
6480
#ifdef SUNDIALS_CONTEXT_REQUIRED
81+
82+
private:
83+
std::shared_ptr<SUNContext> _context; /**< Sundials context */
84+
int _context_flags = 0; /**< Flags describing the context */
85+
/** Flags describing context traits */
86+
enum CONTEXT_FLAGS : int {
87+
CONTEXT_FLAG_GLOBAL = 0x00000001, //< The context is global
88+
};
89+
public:
6590
/**
6691
Construct a new variables instance for a given sundials context
6792
\param[in, out] ctx Sundials context.
6893
*/
69-
Variables(SUNContext* ctx) : context(ctx) {}
94+
Variables(SUNContext* ctx = nullptr);
95+
/**
96+
Get the shared pointer to the Sundials context.
97+
\return Sundials context shared pointer.
98+
*/
99+
std::shared_ptr<SUNContext> context_ptr() {
100+
return _context;
101+
}
102+
/**
103+
Get the Sundials context.
104+
\return Sundials context.
105+
*/
106+
SUNContext& context() {
107+
return *(_context.get());
108+
}
70109
#else // SUNDIALS_CONTEXT_REQUIRED
71110
/**
72111
Construct a new variables instance for a given sundials context
@@ -86,9 +125,6 @@ class Variables {
86125
Variables& operator=(const Variables& other);
87126
friend std::ostream& operator<<(std::ostream &out, const Variables *in);
88127

89-
#ifdef SUNDIALS_CONTEXT_REQUIRED
90-
SUNContext* context = NULL; /**< Sundials context */
91-
#endif // SUNDIALS_CONTEXT_REQUIRED
92128
bool record = false;
93129
bool BF_FI_com = false;
94130
bool EPS_SUCS_com = false;

include/definitions.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@
4747
#ifndef sunrealtype
4848
#define sunrealtype double
4949
#endif
50-
#endif //
50+
#endif //
51+
52+
#if (SUNDIALS_VERSION_MAJOR >= 7)
53+
#include <sundials/sundials_errors.h>
54+
#else
55+
typedef int SUNErrCode;
56+
inline const char* SUNGetErrMsg(const SUNErrCode&) { return ""; }
57+
#endif
5158

5259
#ifndef uint
5360
#define uint unsigned int
@@ -58,6 +65,9 @@
5865
#ifndef realtype
5966
#define realtype sunrealtype
6067
#endif
68+
#ifndef SUN_COMM_NULL
69+
#define SUN_COMM_NULL 0
70+
#endif
6171

6272
namespace ePhotosynthesis {
6373

include/drivers/driver.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
**********************************************************************************************************************************************/
2828

2929
#include <vector>
30+
#include <memory>
3031

3132
#include "definitions.hpp"
3233
#include <nvector/nvector_serial.h>
@@ -146,7 +147,13 @@ class Driver {
146147
double smoothPenalty(const std::vector<double>& x, double threshold);
147148

148149
#ifdef SUNDIALS_CONTEXT_REQUIRED
149-
SUNContext* context;
150+
private:
151+
std::shared_ptr<SUNContext> _context;
152+
public:
153+
/** Get the context */
154+
SUNContext& context() {
155+
return *(_context.get());
156+
}
150157
#endif // SUNDIALS_CONTEXT_REQUIRED
151158
realtype abstol; // absolute tolerance
152159
realtype reltol; // relative tolerance

src/Variables.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
1+
#include <cstdlib>
12
#include "Variables.hpp"
23

4+
#ifdef SUNDIALS_CONTEXT_REQUIRED
5+
std::shared_ptr<SUNContext> ePhotosynthesis::global_context(NULL);
6+
7+
void ePhotosynthesis::init_global_sundials_context() {
8+
if (!global_context) {
9+
SUNContext* context = new SUNContext();
10+
SUNErrCode error_code = SUNContext_Create(SUN_COMM_NULL, context);
11+
if (error_code) {
12+
throw std::runtime_error("SUNContext_Create failed: " +
13+
std::string(SUNGetErrMsg(error_code)));
14+
}
15+
global_context.reset(context);
16+
std::atexit(cleanup_global_sundials_context);
17+
}
18+
}
19+
20+
void ePhotosynthesis::cleanup_global_sundials_context() {
21+
if (global_context) {
22+
global_context.reset();
23+
}
24+
}
25+
26+
#endif // SUNDIALS_CONTEXT_REQUIRED
27+
328
using namespace ePhotosynthesis;
429

530
#ifdef SUNDIALS_CONTEXT_REQUIRED
6-
Variables::Variables(const Variables& other) : Variables(other.context) {
31+
32+
Variables::Variables(SUNContext* ctx) : _context(), _context_flags(0) {
33+
if (_context == nullptr) {
34+
init_global_sundials_context();
35+
_context = global_context;
36+
_context_flags |= CONTEXT_FLAG_GLOBAL;
37+
} else {
38+
_context.reset(ctx);
39+
}
40+
}
41+
Variables::Variables(const Variables& other) :
42+
_context(other._context), _context_flags(other._context_flags) {
743
*this = other;
844
}
9-
Variables::Variables(const Variables* other) : Variables(other->context) {
45+
Variables::Variables(const Variables* other) :
46+
_context(other->_context), _context_flags(other->_context_flags) {
1047
*this = *other;
1148
}
1249
#else // SUNDIALS_CONTEXT_REQUIRED
@@ -58,7 +95,8 @@ Variables* Variables::deepcopy() const {
5895

5996
Variables& Variables::operator=(const Variables &other) {
6097
#ifdef SUNDIALS_CONTEXT_REQUIRED
61-
context = other.context;
98+
_context = other._context;
99+
_context_flags = other._context_flags;
62100
#endif // SUNDIALS_CONTEXT_REQUIRED
63101
record = other.record;
64102
BF_FI_com = other.BF_FI_com;

src/drivers/CVodeMem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void CVodeMem::cvode_mem_init(Driver* driver, realtype t0, N_Vector y) {
1212
if (!initialized) {
1313
data = alloc_calc_data();
1414
#ifdef SUNDIALS_CONTEXT_REQUIRED
15-
_cvode_mem = CVodeCreate(CV_BDF, driver->context[0]);
15+
_cvode_mem = CVodeCreate(CV_BDF, driver->context());
1616
#else // SUNDIALS_CONTEXT_REQUIRED
1717
_cvode_mem = CVodeCreate(CV_BDF);
1818
#endif // SUNDIALS_CONTEXT_REQUIRED

src/drivers/driver.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Driver::Driver(Variables *theVars, const double startTime, const double stepSize
4343
const int maxSubsteps, const double atol, const double rtol,
4444
const bool showWarn) {
4545
#ifdef SUNDIALS_CONTEXT_REQUIRED
46-
context = theVars->context;
46+
_context = theVars->context_ptr();
4747
#endif // SUNDIALS_CONTEXT_REQUIRED
4848
inputVars = theVars;
4949
start = startTime;
@@ -72,7 +72,7 @@ arr Driver::run() {
7272
sunindextype N = static_cast<long>(constraints.size());
7373
N_Vector y;
7474
#ifdef SUNDIALS_CONTEXT_REQUIRED
75-
y = N_VNew_Serial(N, *context);
75+
y = N_VNew_Serial(N, context());
7676
#else // SUNDIALS_CONTEXT_REQUIRED
7777
y = N_VNew_Serial(N);
7878
#endif // SUNDIALS_CONTEXT_REQUIRED
@@ -95,9 +95,9 @@ arr Driver::run() {
9595
data->drv = this;
9696

9797
#ifdef SUNDIALS_CONTEXT_REQUIRED
98-
SUNMatrix A = SUNDenseMatrix(N, N, *context);
99-
SUNNonlinearSolver NLS = SUNNonlinSol_Newton(y, *context);
100-
SUNLinearSolver LS = SUNLinSol_Dense(y, A, *context);
98+
SUNMatrix A = SUNDenseMatrix(N, N, context());
99+
SUNNonlinearSolver NLS = SUNNonlinSol_Newton(y, context());
100+
SUNLinearSolver LS = SUNLinSol_Dense(y, A, context());
101101
#else // SUNDIALS_CONTEXT_REQUIRED
102102
SUNMatrix A = SUNDenseMatrix(N, N);
103103
SUNNonlinearSolver NLS = SUNNonlinSol_Newton(y);

src/rate/RedoxReg_Rate.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ void RedoxReg::_Rate(const double t, const RedoxRegCondition* const RedoxReg_Con
6363

6464
N_Vector y, constraints, scaling;
6565
#ifdef SUNDIALS_CONTEXT_REQUIRED
66-
y = N_VNew_Serial(1, theVars->context[0]);
67-
constraints = N_VNew_Serial(1, theVars->context[0]);
68-
scaling = N_VNew_Serial(1, theVars->context[0]);
66+
y = N_VNew_Serial(1, theVars->context());
67+
constraints = N_VNew_Serial(1, theVars->context());
68+
scaling = N_VNew_Serial(1, theVars->context());
6969
#else // SUNDIALS_CONTEXT_REQUIRED
7070
y = N_VNew_Serial(1);
7171
constraints = N_VNew_Serial(1);
@@ -79,10 +79,10 @@ void RedoxReg::_Rate(const double t, const RedoxRegCondition* const RedoxReg_Con
7979
constraints_ptr[0] = ZERO;
8080
void *kmem = nullptr;
8181
#ifdef SUNDIALS_CONTEXT_REQUIRED
82-
kmem = KINCreate(theVars->context[0]);
82+
kmem = KINCreate(theVars->context());
8383

84-
SUNMatrix A = SUNDenseMatrix(1, 1, theVars->context[0]);
85-
SUNLinearSolver LS = SUNLinSol_Dense(y, A, theVars->context[0]);
84+
SUNMatrix A = SUNDenseMatrix(1, 1, theVars->context());
85+
SUNLinearSolver LS = SUNLinSol_Dense(y, A, theVars->context());
8686
#else // SUNDIALS_CONTEXT_REQUIRED
8787
kmem = KINCreate();
8888

0 commit comments

Comments
 (0)