10#include <compile_time_options.h>
15#include <deal.II/base/array_view.h>
16#include <deal.II/base/config.h>
17#include <deal.II/base/exceptions.h>
18#include <deal.II/base/parameter_acceptor.h>
21#include "eos_Interface.h"
42 enum class TableType : EOS_INTEGER {
64 Interface(
const std::vector<std::tuple<EOS_INTEGER, TableType>> &tables)
66 n_tables_ = tables.size();
68 std::transform(std::begin(tables),
70 std::back_inserter(material_ids_),
71 [](
const auto &it) {
return std::get<0>(it); });
73 std::transform(std::begin(tables),
75 std::back_inserter(table_types_),
77 return static_cast<EOS_INTEGER
>(std::get<1>(it));
80 table_handles_.resize(n_tables_);
84 EOS_INTEGER error_code;
85 eos_CreateTables(&n_tables_,
88 table_handles_.data(),
90 check_tables(
"eos_CreateTables");
94 for (EOS_INTEGER i = 0; i < n_tables_; i++) {
97 &table_handles_[i], &EOS_SMOOTH, EOS_NullPtr, &error_code);
98 check_error_code(error_code,
"eos_SetOption", i);
103 eos_LoadTables(&n_tables_, table_handles_.data(), &error_code);
104 check_tables(
"eos_LoadTables");
110 ~Interface() noexcept
112 EOS_INTEGER error_code;
113 eos_DestroyTables(&n_tables_, table_handles_.data(), &error_code);
120 inline DEAL_II_ALWAYS_INLINE
void
121 interpolate_values(
const EOS_INTEGER &index,
122 const dealii::ArrayView<EOS_REAL> &F,
123 const dealii::ArrayView<EOS_REAL> &dFx,
124 const dealii::ArrayView<EOS_REAL> &dFy,
125 const dealii::ArrayView<const EOS_REAL> &X,
126 const dealii::ArrayView<const EOS_REAL> &Y)
128 Assert(index >= 0 && index < n_tables_,
129 dealii::ExcMessage(
"Table index out of range"));
131 EOS_INTEGER n_queries = F.size();
134 const decltype(dFx.size()) size = n_queries;
135 Assert(dFx.size() == size && dFy.size() == size && X.size() == size &&
137 dealii::ExcMessage(
"vector sizes do not match"));
140 EOS_INTEGER error_code;
141 eos_Interpolate(&table_handles_[index],
143 const_cast<EOS_REAL *
>(X.data()),
144 const_cast<EOS_REAL *
>(Y.data()),
155 std::vector<EOS_INTEGER> material_ids_;
156 std::vector<EOS_INTEGER> table_types_;
157 EOS_INTEGER n_tables_;
159 mutable std::vector<EOS_INTEGER> table_handles_;
165 void check_error_code(
166 EOS_INTEGER error_code,
167 const std::string &routine,
168 EOS_INTEGER i = std::numeric_limits<EOS_INTEGER>::max())
const
170 if (error_code != EOS_OK) {
171 std::array<EOS_CHAR, EOS_MaxErrMsgLen> error_message;
172 eos_GetErrorMessage(&error_code, error_message.data());
174 std::stringstream exception_body;
175 exception_body <<
"Error: " << routine;
176 if (i != std::numeric_limits<EOS_INTEGER>::max())
177 exception_body <<
" (table " << i <<
")";
178 exception_body <<
": " << error_code <<
" - " << error_message.data()
180 AssertThrow(
false, dealii::ExcMessage(exception_body.str()));
184 void check_tables(
const std::string &routine)
const
186 for (EOS_INTEGER i = 0; i < n_tables_; i++) {
187 EOS_INTEGER table_error_code = EOS_OK;
188 eos_GetErrorCode(&table_handles_[i], &table_error_code);
189 if (table_error_code != EOS_OK) {
190 std::array<EOS_CHAR, EOS_MaxErrMsgLen> error_message;
191 eos_GetErrorMessage(&table_error_code, error_message.data());
193 std::stringstream exception_body;
194 exception_body <<
"Error: " << routine <<
" (table " << i
195 <<
"): " << table_error_code <<
" - "
196 << error_message.data() << std::flush;
198 AssertThrow(
false, dealii::ExcMessage(exception_body.str()));
206 namespace EquationOfStateLibrary
228 Sesame(
const std::string &subsection)
233 "material id", material_id_,
"The Sesame Material ID");
239 double pressure(
double rho,
double e)
const final
241 eospac_guard_.ensure_initialized([&]() {
242 this->set_up_database();
246 EOS_INTEGER index = 0;
248 double p, p_drho, p_de;
249 const double rho_scaled = rho / 1.0e3;
250 const double e_scaled = e / 1.0e6;
252 eospac_interface_->interpolate_values(
254 dealii::ArrayView<double>(&p, 1),
255 dealii::ArrayView<double>(&p_drho, 1),
256 dealii::ArrayView<double>(&p_de, 1),
257 dealii::ArrayView<const double>(&rho_scaled, 1),
258 dealii::ArrayView<const double>(&e_scaled, 1));
264 void pressure(
const dealii::ArrayView<double> &p,
265 const dealii::ArrayView<double> &rho,
266 const dealii::ArrayView<double> &e)
const final
268 Assert(p.size() == rho.size() && rho.size() == e.size(),
269 dealii::ExcMessage(
"vectors have different size"));
271 eospac_guard_.ensure_initialized([&]() {
272 this->set_up_database();
276 EOS_INTEGER index = 0;
279 thread_local static std::vector<double> p_drho;
280 thread_local static std::vector<double> p_de;
281 p_drho.resize(p.size());
282 p_de.resize(p.size());
285 std::transform(std::begin(rho),
288 [](
double rho) {
return rho / 1.0e3; });
291 std::transform(std::begin(e),
294 [](
auto e) {
return e / 1.0e6; });
296 eospac_interface_->interpolate_values(index,
298 dealii::ArrayView<double>(p_drho),
299 dealii::ArrayView<double>(p_de),
304 std::transform(std::begin(p),
307 [](
auto it) {
return it * 1.0e9; });
313 eospac_guard_.ensure_initialized([&]() {
314 this->set_up_database();
318 EOS_INTEGER index = 1;
320 double e, e_drho, e_dp;
321 const double rho_scaled = rho / 1.0e3;
322 const double p_scaled = p / 1.0e9;
324 eospac_interface_->interpolate_values(
326 dealii::ArrayView<double>(&e, 1),
327 dealii::ArrayView<double>(&e_drho, 1),
328 dealii::ArrayView<double>(&e_dp, 1),
329 dealii::ArrayView<const double>(&rho_scaled, 1),
330 dealii::ArrayView<const double>(&p_scaled, 1));
338 const dealii::ArrayView<double> &rho,
339 const dealii::ArrayView<double> &p)
const final
341 Assert(e.size() == rho.size() && rho.size() == p.size(),
342 dealii::ExcMessage(
"vectors have different size"));
344 eospac_guard_.ensure_initialized([&]() {
345 this->set_up_database();
349 EOS_INTEGER index = 1;
352 thread_local static std::vector<double> e_drho;
353 thread_local static std::vector<double> e_dp;
354 e_drho.resize(e.size());
355 e_dp.resize(e.size());
358 std::transform(std::begin(rho),
361 [](
double rho) {
return rho / 1.0e3; });
364 std::transform(std::begin(p),
367 [](
auto it) {
return it / 1.0e9; });
369 eospac_interface_->interpolate_values(index,
371 dealii::ArrayView<double>(e_drho),
372 dealii::ArrayView<double>(e_dp),
377 std::transform(std::begin(e),
380 [](
auto e) {
return e * 1.0e6; });
388 eospac_guard_.ensure_initialized([&]() {
389 this->set_up_database();
393 AssertThrow(
false, dealii::ExcInternalError());
398 void temperature(
const dealii::ArrayView<double> & ,
399 const dealii::ArrayView<double> & ,
400 const dealii::ArrayView<double> & )
const final
402 eospac_guard_.ensure_initialized([&]() {
403 this->set_up_database();
407 AssertThrow(
false, dealii::ExcInternalError());
414 eospac_guard_.ensure_initialized([&]() {
415 this->set_up_database();
419 AssertThrow(
false, dealii::ExcInternalError());
425 const dealii::ArrayView<double> & ,
426 const dealii::ArrayView<double> & )
const final
428 eospac_guard_.ensure_initialized([&]() {
429 this->set_up_database();
433 AssertThrow(
false, dealii::ExcInternalError());
443 void set_up_database()
const
446 std::filesystem::exists(
"sesameFilesDir.txt"),
448 "For EOSPAC to find the sesame database, we assume that there "
449 "exists a file named 'sesameFilesDir.txt' in the current "
450 "simulation directory. This file should list the path to the "
451 "sesame database. See the EOSPAC manual for more "
453 const std::vector<std::tuple<EOS_INTEGER, eospac::TableType>> tables{
454 {material_id_, eospac::TableType::p_rho_e},
455 {material_id_, eospac::TableType::e_rho_p},
458 eospac_interface_ = std::make_unique<eospac::Interface>(tables);
462 mutable std::unique_ptr<eospac::Interface> eospac_interface_;
470 EOS_INTEGER material_id_;
483 "ryujin has to be configured with eospac support in order to use "
484 "the Sesame EOS database";
488 AssertThrow(
false, dealii::ExcMessage(
message));
494 AssertThrow(
false, dealii::ExcMessage(
message));
500 AssertThrow(
false, dealii::ExcMessage(
message));
506 AssertThrow(
false, dealii::ExcMessage(
message));
virtual double specific_internal_energy(double rho, double p) const =0
virtual double pressure(double rho, double e) const =0
virtual double speed_of_sound(double rho, double e) const =0
virtual double temperature(double rho, double e) const =0
EquationOfState(const std::string &name, const std::string &subsection)
bool prefer_vector_interface_
double pressure(double, double) const final
double speed_of_sound(double, double) const final
double temperature(double, double) const final
Sesame(const std::string &subsection)
double specific_internal_energy(double, double) const final
static constexpr auto message