13#include <sundials/sundials_nvector.h>
14#include <sundials/sundials_core.hpp>
15#include <nvector/nvector_serial.h>
16#include <nvector/nvector_kokkos.hpp>
17#include <arkode/arkode_arkstep.h>
18#include <arkode/arkode_erkstep.h>
24namespace NeoN::sundials
32inline auto SUN_CONTEXT_DELETER = [](SUNContext* ctx)
45inline auto SUN_ARK_DELETER = [](
char* ark)
49 void* arkodMem =
reinterpret_cast<void*
>(ark);
50 ARKodeFree(&arkodMem);
60inline ARKODE_ERKTableID stringToERKTable(
const std::string& key)
62 if (key ==
"Forward-Euler")
return ARKODE_FORWARD_EULER_1_1;
65 NF_ERROR_EXIT(
"Currently unsupported until field time step-stage indexing resolved.");
66 return ARKODE_HEUN_EULER_2_1_2;
68 if (key ==
"Midpoint")
70 NF_ERROR_EXIT(
"Currently unsupported until field time step-stage indexing resolved.");
71 return ARKODE_EXPLICIT_MIDPOINT_EULER_2_1_2;
74 "Unsupported Runge-Kutta time integration method selectied: " + key +
".\n"
75 +
"Supported methods are: Forward-Euler, Heun, Midpoint."
77 return ARKODE_ERK_NONE;
88template<
typename SKVectorType,
typename ValueType>
91 auto view = ::sundials::kokkos::GetVec<SKVectorType>(vector)->View();
92 auto fieldView = field.
view();
94 field.
exec(), field.
range(), KOKKOS_LAMBDA(
const localIdx i) { view(i) = fieldView[i]; }
105template<
typename ValueType>
109 if (std::holds_alternative<NeoN::GPUExecutor>(field.
exec()))
111 fieldToSunNVectorImpl<::sundials::kokkos::Vector<Kokkos::DefaultExecutionSpace>>(
116 if (std::holds_alternative<NeoN::CPUExecutor>(field.
exec()))
118 fieldToSunNVectorImpl<::sundials::kokkos::Vector<Kokkos::DefaultHostExecutionSpace>>(
123 if (std::holds_alternative<NeoN::SerialExecutor>(field.
exec()))
125 fieldToSunNVectorImpl<::sundials::kokkos::Vector<Kokkos::Serial>>(field, vector);
139template<
typename SKVectorType,
typename ValueType>
142 auto view = ::sundials::kokkos::GetVec<SKVectorType>(vector)->View();
143 ValueType* fieldData = field.
data();
145 field.
exec(), field.
range(), KOKKOS_LAMBDA(
const localIdx i) { fieldData[i] = view(i); }
155template<
typename ValueType>
158 if (std::holds_alternative<NeoN::GPUExecutor>(field.
exec()))
160 sunNVectorToVectorImpl<::sundials::kokkos::Vector<Kokkos::DefaultExecutionSpace>>(
165 if (std::holds_alternative<NeoN::CPUExecutor>(field.
exec()))
167 sunNVectorToVectorImpl<::sundials::kokkos::Vector<Kokkos::DefaultHostExecutionSpace>>(
172 if (std::holds_alternative<NeoN::SerialExecutor>(field.
exec()))
174 sunNVectorToVectorImpl<::sundials::kokkos::Vector<Kokkos::Serial>>(vector, field);
194template<
typename SolutionVectorType>
195int explicitRKSolve([[maybe_unused]] sunrealtype t, N_Vector y, N_Vector ydot,
void* userData)
198 using ValueType =
typename SolutionVectorType::VectorValueType;
201 sunrealtype* yDotArray = N_VGetArrayPointer(ydot);
202 sunrealtype* yArray = N_VGetArrayPointer(y);
205 yDotArray !=
nullptr && yArray !=
nullptr && pdeExpre !=
nullptr,
206 "Failed to dereference pointers in sundails."
209 auto size =
static_cast<localIdx>(N_VGetLength(y));
213 NeoN::sundials::fieldToSunNVector(source, ydot);
227template<
typename Vector>
228void initNVector(
size_t size, std::shared_ptr<SUNContext> context, Vector& vec)
230 vec.initNVector(size, context);
239template<
typename Vector>
240const N_Vector& sunNVector(
const Vector& vec)
242 return vec.sunNVector();
251template<
typename Vector>
252N_Vector& sunNVector(Vector& vec)
254 return vec.sunNVector();
263template<
typename ValueType>
269 ~SKVectorSerial() =
default;
270 SKVectorSerial(
const SKVectorSerial& other)
271 : kvector_(other.kvector_), svector_(other.kvector_) {};
272 SKVectorSerial(SKVectorSerial&& other) noexcept
273 : kvector_(std::move(other.kvector_)), svector_(std::move(other.svector_)) {};
274 SKVectorSerial& operator=(
const SKVectorSerial& other) =
delete;
275 SKVectorSerial& operator=(SKVectorSerial&& other) =
delete;
278 using KVector = ::sundials::kokkos::Vector<Kokkos::Serial>;
279 void initNVector(
size_t size, std::shared_ptr<SUNContext> context)
281 kvector_ = KVector(size, *context);
284 const N_Vector& sunNVector()
const {
return svector_; };
285 N_Vector& sunNVector() {
return svector_; };
290 N_Vector svector_ {
nullptr};
298template<
typename ValueType>
299class SKVectorHostDefault
303 using KVector = ::sundials::kokkos::Vector<Kokkos::DefaultHostExecutionSpace>;
305 SKVectorHostDefault() =
default;
306 ~SKVectorHostDefault() =
default;
307 SKVectorHostDefault(
const SKVectorHostDefault& other)
308 : kvector_(other.kvector_), svector_(other.kvector_) {};
309 SKVectorHostDefault(SKVectorHostDefault&& other) noexcept
310 : kvector_(std::move(other.kvector_)), svector_(std::move(other.svector_)) {};
311 SKVectorHostDefault& operator=(
const SKVectorHostDefault& other) =
delete;
312 SKVectorHostDefault& operator=(SKVectorHostDefault&& other) =
delete;
314 void initNVector(
size_t size, std::shared_ptr<SUNContext> context)
316 kvector_ = KVector(size, *context);
319 const N_Vector& sunNVector()
const {
return svector_; };
320 N_Vector& sunNVector() {
return svector_; };
325 N_Vector svector_ {
nullptr};
333template<
typename ValueType>
338 using KVector = ::sundials::kokkos::Vector<Kokkos::DefaultExecutionSpace>;
340 SKVectorDefault() =
default;
341 ~SKVectorDefault() =
default;
342 SKVectorDefault(
const SKVectorDefault& other)
343 : kvector_(other.kvector_), svector_(other.kvector_) {};
344 SKVectorDefault(SKVectorDefault&& other) noexcept
345 : kvector_(std::move(other.kvector_)), svector_(std::move(other.svector_)) {};
346 SKVectorDefault& operator=(
const SKVectorDefault& other) =
delete;
347 SKVectorDefault& operator=(SKVectorDefault&& other) =
delete;
349 void initNVector(
size_t size, std::shared_ptr<SUNContext> context)
351 kvector_ = KVector(size, *context);
355 const N_Vector& sunNVector()
const {
return svector_; };
357 N_Vector& sunNVector() {
return svector_; };
362 N_Vector svector_ {
nullptr};
371template<
typename ValueType>
376 using SKVectorSerialV = SKVectorSerial<ValueType>;
377 using SKVectorHostDefaultV = SKVectorHostDefault<ValueType>;
378 using SKDefaultVectorV = SKVectorDefault<ValueType>;
379 using SKVectorVariant = std::variant<SKVectorSerialV, SKVectorHostDefaultV, SKDefaultVectorV>;
384 SKVector() { vector_.template emplace<SKVectorHostDefaultV>(); };
389 ~SKVector() =
default;
395 SKVector(
const SKVector&) =
default;
400 SKVector& operator=(
const SKVector&) =
delete;
406 SKVector(SKVector&&) noexcept = default;
411 SKVector& operator=(SKVector&&) noexcept = delete;
417 void setExecutor(const
NeoN::Executor& exec)
419 if (std::holds_alternative<NeoN::GPUExecutor>(exec))
421 vector_.template emplace<SKDefaultVectorV>();
424 if (std::holds_alternative<NeoN::CPUExecutor>(exec))
426 vector_.template emplace<SKVectorHostDefaultV>();
429 if (std::holds_alternative<NeoN::SerialExecutor>(exec))
431 vector_.template emplace<SKVectorSerialV>();
436 "Unsupported NeoN executor: "
437 << std::visit([](
const auto& e) {
return e.name(); }, exec) <<
"."
446 void initNVector(
size_t size, std::shared_ptr<SUNContext> context)
449 [size, &context](
auto& vec) { detail::initNVector(size, context, vec); }, vector_
457 const N_Vector& sunNVector()
const
460 [](
const auto& vec) ->
const N_Vector& {
return detail::sunNVector(vec); }, vector_
468 N_Vector& sunNVector()
470 return std::visit([](
auto& vec) -> N_Vector& {
return detail::sunNVector(vec); }, vector_);
477 const SKVectorVariant& variant()
const {
return vector_; }
483 SKVectorVariant& variant() {
return vector_; }
487 SKVectorVariant vector_;
A class to contain the data and executors for a field and define some basic operations.
ValueType * data()
Direct access to the underlying field data.
std::pair< localIdx, localIdx > range() const
Gets the range of the field.
const Executor & exec() const
Gets the executor associated with the field.
View< ValueType > view() &&=delete
Vector< ValueType > explicitOperation(localIdx nCells) const
const Executor & exec() const
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
SpatialOperator< scalar > source(fvcc::VolumeField< scalar > &coeff, fvcc::VolumeField< scalar > &phi)
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
void fence(const Executor &exec)