1 #ifndef DIFFCLEXPARAMPACK_HH
2 #define DIFFCLEXPARAMPACK_HH
12 #include "casm/external/fadbad/badiff.h"
13 #include "casm/external/fadbad/fadiff.h"
20 namespace DiffClexParamPack_impl {
88 std::vector<Index>
const &_offset = {},
89 std::vector<Index>
const &_stride = {})
90 : ClexParamPack_impl::BaseKey(_name, _standalone, _offset, _stride),
140 std::vector<std::vector<DiffScalar> >
m_data;
156 Index num_params,
Index _lbegin,
bool _independent)
162 m_cache(param_dim, num_params) {
206 for (
auto &dvec :
m_data) {
207 for (
auto &datum : dvec) {
208 datum.val().diff(l++, N);
218 for (
auto &dvec :
m_data) {
219 for (
auto &datum : dvec) {
257 template <
typename Scalar>
260 template <
typename Scalar>
318 Eigen::Ref<const Eigen::MatrixXd>
const &_val)
override {
323 Eigen::Ref<const Eigen::MatrixXd>
const &_val);
332 double _val)
override {
342 std::string
const &_mode)
override {
344 from_string<EvalMode>(_mode));
353 for (
auto const &eval :
m_eval) {
366 if (datum.m_independent) {
378 if (!datum.m_independent) {
431 std::vector<Index>
const &_offset = {},
432 std::vector<Index>
const &_stride = {})
440 throw std::runtime_error(
"Requested Gradient parameter " +
name() +
441 " for incompatible evaluation mode " +
461 std::vector<Index>
const &_offset = {},
462 std::vector<Index>
const &_stride = {})
470 throw std::runtime_error(
"Requested Hessian parameter " +
name() +
471 " for incompatible evaluation mode " +
491 std::vector<std::string> >
511 Eigen::Ref<const Eigen::MatrixXd>
const &_val) {
515 template <
typename Scalar2>
521 template <
typename Scalar2>
545 Eigen::Ref<const Eigen::MatrixXd>
const &_val) {
546 assert(_val.rows() == _pack.
m_data[_key.
index()].m_data.size());
547 for (
Index i = 0; i < _val.rows(); ++i) {
548 assert(_val.cols() == _pack.
m_data[_key.
index()].m_data[i].size());
549 for (
Index j = 0; j < _val.cols(); ++j) {
550 _pack.
m_data[_key.
index()].m_data[i][j] = _val(i, j);
555 template <
typename Scalar2>
561 template <
typename Scalar2>
569 BaseKey const &_key, Eigen::Ref<const Eigen::MatrixXd>
const &_val) {
571 throw std::runtime_error(
"Cannot write to dependent parameter " +
584 throw std::runtime_error(
"Cannot write to dependent parameter " +
598 throw std::runtime_error(
"Cannot write to dependent parameter " +
610 std::string
const &_keyname,
Index _rows,
Index _cols,
bool _independent) {
611 auto it =
keys().find(_keyname);
612 if (it !=
keys().end())
613 throw std::runtime_error(
614 "Naming collision in DiffClexParamPack::allocate(), ClexParamPack "
615 "already managing parameter allocation corresponding to name " +
618 m_keys[_keyname] = protokey;
625 if (!
m_data[f].m_independent) {
626 std::string gradname =
629 {
m_data[f].m_cache.cols()});
632 if (
m_data[j].m_independent) {
633 std::string hessname = gradname +
"/" +
m_data[j].m_name;
649 if (
m_data[i].m_independent) {
650 std::string gradname =
653 {
m_data[f].m_cache.cols()});
656 if (
m_data[j].m_independent) {
657 std::string hessname = gradname +
"/" +
m_data[j].m_name;
674 sout << to_string<DiffClexParamPack::EvalMode>(val);
682 val = from_string<DiffClexParamPack::EvalMode>(
s);
688 const std::multimap<DiffClexParamPack::EvalMode, std::vector<std::string> >
691 {
"Default",
"DEFAULT",
"default"}},
693 {
"Diff",
"DIFF",
"diff",
"Differential",
"differential",
697 {
"Dynamic",
"Dynam",
"DYNAMIC",
"DYNAM",
"dynamic",
"dynam"}}};
std::set< std::string > & s
Key for indexing clexulator parameters Contains pointer to implementation of the Key.
ClexParamPack_impl::BaseKey const * ptr() const
Returns pointer to Key implementation.
BaseKey is base class from which all ClexParamKey implementation classes inherit. Hides implementatio...
bool standalone() const
Returns true if key refers directly to managed data, false if it refers to values derived from manage...
std::string const & name() const
Name of this Key, should be descriptive of managed data. Ex.: "disp_var" of "magspin_site_basis".
Index _l(Index i) const
Returns i'th linear index identifier.
Abstract base class for reading/writing clexulator parameters Parameters are assumed to be stored as,...
std::map< std::string, ClexParamKey > const & keys() const
Obtain registry of all keys for data blocks managed by this ClexParamPack.
std::map< std::string, ClexParamKey > m_keys
ClexParamPack_impl::BaseKey * _clone() const override
Clone the ClexParamKey.
Eigen::MatrixXd const & eval(DiffScalarContainer const &_data, DiffClexParamPack::EvalMode mode) const override
Operate on associated DiffScalarContainer to ensure the standalone or derived value associated with t...
ClexParamPack::size_type size_type
DiffClexParamGradKey(std::string const &_name="", size_type _ix=-1, std::vector< Index > const &_offset={}, std::vector< Index > const &_stride={})
DiffClexParamHessKey(std::string const &_name="", size_type _ix=-1, std::vector< Index > const &_offset={}, std::vector< Index > const &_stride={})
ClexParamPack_impl::BaseKey * _clone() const override
Clone the ClexParamKey.
ClexParamPack::size_type size_type
Eigen::MatrixXd const & eval(DiffScalarContainer const &_data, DiffClexParamPack::EvalMode mode) const override
Operate on associated DiffScalarContainer to ensure the standalone or derived value associated with t...
Abstract base class for all keys that interact with DiffClexParamPack DiffClexParamPack values are as...
virtual ~DiffClexParamKey()
Destructor of abstract base class must be virtual.
size_type index() const
Access the primary identifier of this value (which determines the associated standalone value and/or ...
DiffClexParamKey(std::string const &_name, bool _standalone, size_type _ind, std::vector< Index > const &_offset={}, std::vector< Index > const &_stride={})
DiffClexParamKey Constructor.
ClexParamPack::size_type size_type
virtual Eigen::MatrixXd const & eval(DiffScalarContainer const &_data, DiffClexParamPack_impl::EvalMode mode) const =0
Operate on associated DiffScalarContainer to ensure the standalone or derived value associated with t...
Class for managing the set of all dependent and independent paramters used/generated by clexulator.
void write(ClexParamKey const &_key, size_type _i, size_type _j, double _val) override
Write element to 2D parameter array for parameter specified by.
DiffClexParamPack_impl::EvalMode EvalMode
void set_eval_mode(ClexParamKey const &_key, std::string const &_mode) override
Sets evaluation mode for ClexParamPack parameter Choices are at least.
static const EvalMode READ
Eigen::MatrixXd::CoeffReturnType DoubleReference
size_type size(BaseKey const &_key) const
Eigen::MatrixXd const & read(ClexParamKey const &_key) const override
Returns const reference to parameter array for parameter specified by.
std::vector< EvalMode > m_eval
size_type size(ClexParamKey const &_key) const override
'N' dimension of parameter array (either '1', or size of unit cell neighborhood) returns number of co...
DiffClexParamPack_impl::DiffScalar DiffScalar
size_type dim(ClexParamKey const &_key) const override
'm' dimension of parameter array returns number of rows in parameter array
Eigen::MatrixXd const & read(BaseKey const &_key) const
size_type dim(BaseKey const &_key) const
Key allocate(std::string const &_keyname, Index _rows, Index _cols, bool _independent)
static const EvalMode DYNAM
void pre_eval()
Prepares all independent parameters before function evaluation if derivatives are desired.
EvalMode eval_mode(BaseKey const &_key) const
double const & read(ClexParamKey const &_key, size_type _ind) const override
Returns const reference to element of 1D parameter array for parameter specified by.
DiffClexParamPack()
Default constructor initializes evaluation mode and zeros numbers of managed parameters.
double const & read(ClexParamKey const &_key, size_type _i, size_type _j) const override
Returns const reference to element of 2D parameter array for parameter specified by.
double const & read(BaseKey const &_key, size_type _ind) const
void write(ClexParamKey const &_key, Eigen::Ref< const Eigen::MatrixXd > const &_val) override
Write parameter array for parameter specified by.
void post_eval()
Processes all dependent parameters after function evaluation if derivatives are desired.
std::vector< DiffScalarContainer > m_data
static const EvalMode DEFAULT
void set_eval_mode(BaseKey const &_key, EvalMode _mode)
Set evaluation mode of specified parameter type.
std::string eval_mode(ClexParamKey const &_key) const override
Check evaluation mode for ClexParamPack parameter Choices are at least.
EvalMode eval_mode() const
void write(ClexParamKey const &_key, size_type _i, double _val) override
Write element to 1D parameter array for parameter specified by.
static const EvalMode DIFF
double const & read(BaseKey const &_key, size_type _i, size_type _j) const
ClexParamPack::size_type size_type
ClexParamPack_impl::BaseKey * _clone() const override
Clone the ClexParamKey.
DiffClexParamValKey(std::string const &_name="", size_type _ind=-1)
Eigen::MatrixXd const & eval(DiffScalarContainer const &_data, DiffClexParamPack::EvalMode mode) const override
Operate on associated DiffScalarContainer to ensure the standalone or derived value associated with t...
std::string to_string(ENUM val)
Return string representation of enum class.
fadbad::B< fadbad::F< double > > DiffScalar
std::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
std::istream & operator>>(std::istream &_in, std::vector< T > &vec)
INDEX_TYPE Index
For long integer indexing:
container struct for storing a standalone value as 2D arrays of both autodiff Scalars doubles
void eval_double()
Copy values of autodiff scalars from m_data to m_data_double (matrix of doubles) in preparation for e...
bool m_independent
Flag, set to true if this container holds independent values (false if dependent)
Index linear_index_end() const
Returns one past end of max linear index range allowed for this parameter set.
Index lbegin
linear offset for start of value index in linear array FADBAD forward (backward) autodiff types refer...
std::vector< std::vector< DiffScalar > > m_data
Stored data represented as autodif scalar type.
Eigen::MatrixXd const & grad(Index f) const
Gradient of dependent function index f wrt the independent variables contained in here.
DiffClexParamPack_impl::DiffScalar DiffScalar
Eigen::MatrixXd const & hess(Index f, Index a) const
Hessian components of dependent function index f corresponding to independent variable index 'a' and ...
Eigen::MatrixXd m_cache
Derived data cache, represented as matrix of doubles.
void post_eval(Index N)
Must be called on each dependent parameter after function evaluation if derivatives are desired.
std::string m_name
Name of the standalone value stored in this container. Should match name of associated key.
Eigen::MatrixXd m_data_double
Stored data, represented as matrix of doubles.
DiffScalarContainer(std::string const &_name, Index param_dim, Index num_params, Index _lbegin, bool _independent)
DiffSclarContainer constructor.
void pre_eval(Index N)
Must be called on each independent parameter before function evaluation if derivatives are desired.
static void set(DiffClexParamPack &_pack, DiffClexParamKey const &_key, size_type i, size_type j, Scalar2 const &_val)
static void set(DiffClexParamPack &_pack, DiffClexParamKey const &_key, size_type i, Scalar2 const &_val)
static double const & get(DiffClexParamPack const &_pack, DiffClexParamKey const &_key, size_type i)
static void set(DiffClexParamPack &_pack, DiffClexParamKey const &_key, Eigen::Ref< const Eigen::MatrixXd > const &_val)
BasicClexParamPack::size_type size_type
static double const & get(DiffClexParamPack const &_pack, DiffClexParamKey const &_key, size_type i, size_type j)
static void set(DiffClexParamPack &_pack, DiffClexParamKey const &_key, size_type i, Scalar2 const &_val)
static DiffClexParamPack::DiffScalar const & get(DiffClexParamPack const &_pack, DiffClexParamKey const &_key, size_type i, size_type j)
DiffClexParamPack::size_type size_type
static DiffClexParamPack::DiffScalar const & get(DiffClexParamPack const &_pack, DiffClexParamKey const &_key, size_type i)
static void set(DiffClexParamPack &_pack, DiffClexParamKey const &_key, Eigen::Ref< const Eigen::MatrixXd > const &_val)
static void set(DiffClexParamPack &_pack, DiffClexParamKey const &_key, size_type i, size_type j, Scalar2 const &_val)
static const std::string name
static const std::multimap< DiffClexParamPack::EvalMode, std::vector< std::string > > strval