5#include "bitrl/extern/nlohmann/json/json.hpp"
36 typedef std::array<std::pair<std::string, real_t>, dim>
value_type;
41 template <
int dim1,
int dim2>
52 SysState(std::array<std::pair<std::string, real_t>, dim> &&values);
115 void set(
uint_t i,
const std::pair<std::string, real_t> &value);
127 void add(
const std::vector<real_t> &container);
184 return (*
this)(std::string(name));
195 std::ostream &
print(std::ostream &out)
const;
219 template <
typename Container>
void set(
const Container &container);
232 std::array<std::pair<std::string, real_t>, dim> values_;
236template <
int dim1,
int dim2>
240 static_assert(dim2 <= dim1,
"Invalid dimension dim2 > dim1");
244 auto value = state(name);
245 other.
set(std::string(name), value);
251 std::for_each(values_.begin(), values_.end(),
252 [](std::pair<std::string, real_t> &item)
254 item.first =
"NO NAME";
267 for (
uint_t i = 0; i < dim; ++i)
269 values_[i] = std::pair(names[i], val);
276template <
int other_dim>
279 static_assert(dim <= other_dim,
"Invalid dimension: dim < other_dim");
284 for (
uint_t i = 0; i < dim; ++i)
286 values_[i] = std::make_pair(names[i], values[i]);
298 this->values_ = other.values_;
315 this->values_ = other.values_;
340 std::for_each(values_.begin(), values_.end(), [=](
auto &item) { item.second *= val; });
346 auto itr = std::find_if(values_.begin(), values_.end(),
347 [&](
const std::pair<std::string, real_t> &item)
348 { return item.first == name; });
350 if (itr == values_.end())
352 auto error_msg(
"Invalid variable name. Name ");
353 auto names = get_names();
354 std::string name_strs(
"[");
355 for (
auto &name : names)
357 name_strs += std::string(name);
358 name_strs += std::string(
",");
361 name_strs += std::string(
"]");
362 throw std::invalid_argument(error_msg + name + std::string(
" not in: ") + name_strs);
371 for (
uint_t v = 0; v < values_.size(); ++v)
373 vec(v) = values_[v].second;
381 auto itr = std::find_if(values_.begin(), values_.end(),
382 [&](
const std::pair<std::string, real_t> &item)
383 { return item.first == name; });
385 if (itr == values_.end())
387 auto error_msg(
"Invalid variable name. Name ");
388 auto names = get_names();
389 std::string name_strs(
"[");
390 for (
auto &name : names)
393 name_strs += std::string(name);
394 name_strs += std::string(
",");
397 name_strs += std::string(
"]");
398 throw std::invalid_argument(error_msg + name + std::string(
" not in: ") + name_strs);
407 if (container.size() != dim)
409 throw std::invalid_argument(
410 "Container has incorrect size: " + std::to_string(container.size()) +
" not equal to " +
411 std::to_string(dim));
415 std::for_each(values_.begin(), values_.end(),
416 [&](
auto &arg) { arg.second = container[counter++]; });
428 return values_[i].second;
434 auto itr = std::find_if(values_.begin(), values_.end(),
435 [&](
const std::pair<std::string, real_t> &item)
436 { return item.first == name; });
438 if (itr == values_.end())
440 auto error_msg(
"Invalid variable name. Name ");
441 auto names = get_names();
442 std::string name_strs(
"[");
443 for (
auto &name : names)
446 name_strs += std::string(name);
447 name_strs += std::string(
",");
450 name_strs += std::string(
"]");
451 throw std::invalid_argument(error_msg + name + std::string(
" not in: ") + name_strs);
460 auto itr = std::find_if(values_.begin(), values_.end(),
461 [&](
const std::pair<std::string, real_t> &item)
462 { return item.first == name; });
464 if (itr == values_.end())
466 auto error_msg(
"Invalid variable name. Name ");
467 auto names = get_names();
468 std::string name_strs(
"[");
469 for (
auto &name : names)
472 name_strs += std::string(name);
473 name_strs += std::string(
",");
476 name_strs += std::string(
"]");
477 throw std::invalid_argument(error_msg + name + std::string(
" not in: ") + name_strs);
486 std::for_each(values_.begin(), values_.end(),
487 [](std::pair<std::string, real_t> &item) { item.second = 0.0; });
493 std::array<real_t, dim> copy;
495 for (
uint_t i = 0; i < dim; ++i)
497 copy[i] = values_[i].second;
506 std::vector<std::string_view> copy(dim);
508 for (
uint_t i = 0; i < dim; ++i)
510 copy[i] = values_[i].first;
519 out << std::fixed << std::setprecision(4);
521 std::for_each(values_.begin(), values_.end(),
522 [&](
const std::pair<std::string, real_t> &vals)
523 { out << vals.first <<
":" << vals.second << std::endl; });
534 for (
const auto &[name, value] : values_)
542 result += std::to_string(value);
550 for (
const auto &[name, value] : values_)
559 if (container.size() != dim)
561 throw std::logic_error(
"Invalid container size for update. " +
562 std::to_string(container.size()) +
" should be" +
563 std::to_string(dim));
566 for (
uint_t i = 0; i < dim; ++i)
568 values_[i].second += container[i];
574 if (container.size() != dim)
576 throw std::logic_error(
"Invalid container size for update. " +
577 std::to_string(container.size()) +
" should be" +
578 std::to_string(dim));
581 for (
uint_t i = 0; i < dim; ++i)
583 values_[i].second += container[i];
589 return state.print(out);
SysState utility class describing the state of a system.
Definition system_state.h:25
SysState & operator+=(const DynVec< real_t > &vec)
Add to this state the entries of the give vector.
Definition system_state.h:320
real_t & operator[](uint_t)
Access operator.
Definition system_state.h:424
void scale(real_t val)
Scale the values of the state.
Definition system_state.h:338
static void extract(const SysState< dim1 > &state, SysState< dim2 > &other)
Extract a state of different dimension.
Definition system_state.h:237
void add(const DynVec< real_t > &container)
Set the values of state variables container must be of size dim.
Definition system_state.h:557
DynVec< real_t > as_vector() const
Returns the entries of this state as a DynVec.
Definition system_state.h:368
SysState(const SysState< other_dim > &other)
Copy constructor.
Definition system_state.h:277
SysState(std::array< std::string, dim > &&names, real_t val)
Constructor. Initialize the state with the given names all variables will be initialized with val.
Definition system_state.h:265
real_t get(const std::string &name) const
Returns the value for the variable name.
Definition system_state.h:343
std::array< std::pair< std::string, real_t >, dim > value_type
The type of the stored values.
Definition system_state.h:36
real_t & operator()(const std::string &name)
Access operator.
Definition system_state.h:431
SysState(std::array< std::pair< std::string, real_t >, dim > &&values)
Constructor. Initialize the state with the given names and values.
Definition system_state.h:260
real_t & operator()(const std::string_view name)
Access operator.
Definition system_state.h:177
nlohmann::json as_json() const
Definition system_state.h:547
void set(const Container &container)
Set the values.
Definition system_state.h:404
const real_t & operator()(const std::string &name) const
Access operator.
Definition system_state.h:457
SysState(SysState &&other)
Move copy constructor.
Definition system_state.h:302
void set(const std::string &name, real_t val)
Set the value for the variable name.
Definition system_state.h:378
const std::array< real_t, dim > get_values() const
Returns a copy of the state values.
Definition system_state.h:490
SysState()
Constructor. Initialize the state with no names.
Definition system_state.h:249
uint_t size() const
Returns the size of the system.
Definition system_state.h:132
SysState & operator*=(real_t val)
Scale this state by the given factor.
Definition system_state.h:332
const real_t & operator[](uint_t) const
Access operator.
Definition system_state.h:426
SysState(const SysState< dim > &other)
Copy constructor.
Definition system_state.h:273
const std::vector< std::string_view > get_names() const
Returns a copy of the state names.
Definition system_state.h:503
void clear()
clear the state
Definition system_state.h:483
SysState & operator-=(const DynVec< real_t > &vec)
Subtract from this state the entries of the give vector.
Definition system_state.h:326
void set(uint_t i, const std::pair< std::string, real_t > &value)
Set the name and value of the i-th variable.
Definition system_state.h:419
const real_t & operator[](const std::string &name) const
Access operator.
Definition system_state.h:162
SysState & operator=(const SysState< dim > &other)
Copy assignement constructor.
Definition system_state.h:290
static const int dimension
The dimension of the state.
Definition system_state.h:31
const real_t & operator()(const std::string_view name) const
Access operator.
Definition system_state.h:182
std::ostream & print(std::ostream &out) const
Print the state at the given stream.
Definition system_state.h:516
const std::string as_string() const
Return the state as string.
Definition system_state.h:528
SysState & operator=(SysState &&other)
Move copy constructor.
Definition system_state.h:307
real_t & operator[](const std::string &name)
Access operator.
Definition system_state.h:157
void add(const std::vector< real_t > &container)
Set the values of state variables container must be of size dim.
Definition system_state.h:572
std::ostream & operator<<(std::ostream &out, const SysState< dim > &state)
Definition system_state.h:587
Definition bitrl_consts.h:14
double real_t
real_t
Definition bitrl_types.h:23
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitrl_types.h:74
std::size_t uint_t
uint_t
Definition bitrl_types.h:43